public function run()
 {
     DB::table('transaction_types')->delete();
     TransactionType::create(['type' => TransactionType::WITHDRAWAL]);
     TransactionType::create(['type' => TransactionType::DEPOSIT]);
     TransactionType::create(['type' => TransactionType::TRANSFER]);
     TransactionType::create(['type' => TransactionType::OPENING_BALANCE]);
 }
 public function run()
 {
     DB::table('transaction_types')->delete();
     TransactionType::create(['type' => 'Withdrawal']);
     TransactionType::create(['type' => 'Deposit']);
     TransactionType::create(['type' => 'Transfer']);
     TransactionType::create(['type' => 'Opening balance']);
 }
Esempio n. 3
0
 /**
  *
  */
 protected function createPiggyBankEvent()
 {
     // piggy bank event
     // add money to this piggy bank
     // create a piggy bank event to match:
     $checking = $this->findAccount('Checking account');
     $savings = $this->findAccount('Savings account');
     $transfer = TransactionType::whereType('Transfer')->first();
     $euro = TransactionCurrency::whereCode('EUR')->first();
     $groceries = $this->findBudget('Groceries');
     $house = $this->findCategory('House');
     $piggyBank = $this->findPiggyBank('New camera');
     $intoPiggy = $this->createJournal(['from' => $checking, 'to' => $savings, 'amount' => 100, 'transactionType' => $transfer, 'description' => 'Money for piggy', 'date' => $this->yaeom, 'transactionCurrency' => $euro, 'category' => $house, 'budget' => $groceries]);
     PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'transaction_journal_id' => $intoPiggy->id, 'date' => $this->yaeom, 'amount' => 100]);
 }
Esempio n. 4
0
 public function testIsOpeningBalance()
 {
     $transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first();
     $this->assertTrue($transactionType->isOpeningBalance());
 }
 /**
  * @param $attribute
  *
  * @return bool
  */
 public function validateRuleTriggerValue($attribute) : bool
 {
     // get the index from a string like "rule-trigger-value.2".
     $parts = explode('.', $attribute);
     $index = $parts[count($parts) - 1];
     // loop all rule-triggers.
     // check if rule-value matches the thing.
     if (is_array($this->data['rule-trigger'])) {
         $name = $this->getRuleTriggerName($index);
         $value = $this->getRuleTriggerValue($index);
         // break on some easy checks:
         switch ($name) {
             case 'amount_less':
                 $result = is_numeric($value);
                 if ($result === false) {
                     return false;
                 }
                 break;
             case 'transaction_type':
                 $count = TransactionType::where('type', $value)->count();
                 if (!($count === 1)) {
                     return false;
                 }
                 break;
             case 'invalid':
                 return false;
         }
         // still a special case where the trigger is
         // triggered in such a way that it would trigger ANYTHING. We can check for such things
         // with function willmatcheverything
         // we know which class it is so dont bother checking that.
         $classes = Config::get('firefly.rule-triggers');
         /** @var TriggerInterface $class */
         $class = $classes[$name];
         return !$class::willMatchEverything($value);
     }
     return false;
 }
Esempio n. 6
0
 /**
  * @param Account $account
  * @param Account $opposing
  * @param array   $data
  *
  * @return TransactionJournal
  */
 protected function storeInitialBalance(Account $account, Account $opposing, array $data)
 {
     $transactionType = TransactionType::whereType('Opening balance')->first();
     $journal = TransactionJournal::create(['user_id' => $data['user'], 'transaction_type_id' => $transactionType->id, 'bill_id' => null, 'transaction_currency_id' => $data['openingBalanceCurrency'], 'description' => 'Initial balance for "' . $account->name . '"', 'completed' => true, 'date' => $data['openingBalanceDate'], 'encrypted' => true]);
     if ($data['openingBalance'] < 0) {
         $firstAccount = $opposing;
         $secondAccount = $account;
         $firstAmount = $data['openingBalance'] * -1;
         $secondAmount = $data['openingBalance'];
     } else {
         $firstAccount = $account;
         $secondAccount = $opposing;
         $firstAmount = $data['openingBalance'];
         $secondAmount = $data['openingBalance'] * -1;
     }
     $one = new Transaction(['account_id' => $firstAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $firstAmount]);
     $one->save();
     // first transaction: from
     $two = new Transaction(['account_id' => $secondAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $secondAmount]);
     $two->save();
     // second transaction: to
     return $journal;
 }
Esempio n. 7
0
 /**
  * @return TransactionType
  */
 protected function getTransactionType()
 {
     $transactionType = TransactionType::where('type', TransactionType::DEPOSIT)->first();
     if ($this->importData['amount'] < 0) {
         $transactionType = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
     }
     if (in_array($this->importData['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) {
         $transactionType = TransactionType::where('type', TransactionType::TRANSFER)->first();
     }
     return $transactionType;
 }
 /**
  * Store journal only, uncompleted, with attachments if necessary.
  *
  * @param array $data
  *
  * @return TransactionJournal
  */
 public function storeJournal(array $data) : TransactionJournal
 {
     // find transaction type.
     $transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
     // store actual journal.
     $journal = new TransactionJournal(['user_id' => $data['user'], 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $data['amount_currency_id_amount'], 'description' => $data['description'], 'completed' => 0, 'date' => $data['date']]);
     $result = $journal->save();
     if ($result) {
         return $journal;
     }
     return new TransactionJournal();
 }
 /**
  * @param TransactionJournal $journal
  * @param Tag                $tag
  *
  * @return bool
  */
 protected function connectBalancingAct(TransactionJournal $journal, Tag $tag) : bool
 {
     /** @var TransactionType $withdrawal */
     $withdrawal = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
     $withdrawals = $tag->transactionJournals()->where('transaction_type_id', $withdrawal->id)->count();
     /** @var TransactionType $transfer */
     $transfer = TransactionType::whereType(TransactionType::TRANSFER)->first();
     $transfers = $tag->transactionJournals()->where('transaction_type_id', $transfer->id)->count();
     // only if this is the only withdrawal.
     if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     // and only if this is the only transfer
     if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     // ignore expense
     return false;
 }
Esempio n. 10
0
 /**
  * @return TransactionType
  */
 protected function getTransactionType()
 {
     $transactionType = TransactionType::where('type', 'Deposit')->first();
     if ($this->importData['amount'] < 0) {
         $transactionType = TransactionType::where('type', 'Withdrawal')->first();
     }
     if (in_array($this->importData['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) {
         $transactionType = TransactionType::where('type', 'Transfer')->first();
     }
     return $transactionType;
 }
Esempio n. 11
0
 /**
  * @param array $data
  *
  * @return TransactionJournal
  */
 public function store(array $data)
 {
     // find transaction type.
     $transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
     // store actual journal.
     $journal = new TransactionJournal(['user_id' => $data['user'], 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $data['amount_currency_id'], 'description' => $data['description'], 'completed' => 0, 'date' => $data['date']]);
     $journal->save();
     // store or get category
     if (strlen($data['category']) > 0) {
         $category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]);
         $journal->categories()->save($category);
     }
     // store or get budget
     if (intval($data['budget_id']) > 0) {
         /** @var \FireflyIII\Models\Budget $budget */
         $budget = Budget::find($data['budget_id']);
         $journal->budgets()->save($budget);
     }
     // store accounts (depends on type)
     list($from, $to) = $this->storeAccounts($transactionType, $data);
     // store accompanying transactions.
     Transaction::create(['account_id' => $from->id, 'transaction_journal_id' => $journal->id, 'amount' => $data['amount'] * -1]);
     Transaction::create(['account_id' => $to->id, 'transaction_journal_id' => $journal->id, 'amount' => $data['amount']]);
     $journal->completed = 1;
     $journal->save();
     // store tags
     if (isset($data['tags']) && is_array($data['tags'])) {
         $this->saveTags($journal, $data['tags']);
     }
     return $journal;
 }
 /**
  * @param ImportEntry $entry
  *
  * @return ImportEntry
  */
 private function setTransactionType(ImportEntry $entry) : ImportEntry
 {
     Log::debug(sprintf('Opposing account is of type %s', $entry->fields['opposing-account']->accountType->type));
     $type = $entry->fields['opposing-account']->accountType->type;
     switch ($type) {
         case AccountType::EXPENSE:
             $entry->fields['transaction-type'] = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
             Log::debug('Transaction type is now withdrawal.');
             return $entry;
         case AccountType::REVENUE:
             $entry->fields['transaction-type'] = TransactionType::whereType(TransactionType::DEPOSIT)->first();
             Log::debug('Transaction type is now deposit.');
             return $entry;
         case AccountType::ASSET:
             $entry->fields['transaction-type'] = TransactionType::whereType(TransactionType::TRANSFER)->first();
             Log::debug('Transaction type is now transfer.');
             return $entry;
     }
     Log::warning(sprintf('Opposing account is of type %s, cannot handle this.', $type));
     $entry->valid = false;
     $entry->errors->push(sprintf('Opposing account is of type %s, cannot handle this.', $type));
     return $entry;
 }
Esempio n. 13
0
 /**
  * @param TransactionJournal $journal
  * @param Tag                $tag
  *
  * @return boolean
  */
 protected function connectAdvancePayment(TransactionJournal $journal, Tag $tag)
 {
     /** @var TransactionType $transfer */
     $transfer = TransactionType::whereType('Transfer')->first();
     /** @var TransactionType $withdrawal */
     $withdrawal = TransactionType::whereType('Withdrawal')->first();
     /** @var TransactionType $deposit */
     $deposit = TransactionType::whereType('Deposit')->first();
     $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
     $deposits = $tag->transactionjournals()->where('transaction_type_id', $deposit->id)->count();
     // advance payments cannot accept transfers:
     if ($journal->transaction_type_id == $transfer->id) {
         return false;
     }
     // the first transaction to be attached to this
     // tag is attached just like that:
     if ($withdrawals < 1 && $deposits < 1) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     // if withdrawal and already has a withdrawal, return false:
     if ($journal->transaction_type_id == $withdrawal->id && $withdrawals == 1) {
         return false;
     }
     // if already has transaction journals, must match ALL asset account id's:
     if ($deposits > 0 || $withdrawals == 1) {
         return $this->matchAll($journal, $tag);
     }
     // this statement is unreachable.
     return false;
     // @codeCoverageIgnore
 }
Esempio n. 14
0
 /**
  * @param $attribute
  *
  * @return bool
  */
 public function validateRuleTriggerValue($attribute)
 {
     // get the index from a string like "rule-trigger-value.2".
     $parts = explode('.', $attribute);
     $index = $parts[count($parts) - 1];
     // loop all rule-triggers.
     // check if rule-value matches the thing.
     if (is_array($this->data['rule-trigger'])) {
         $name = $this->getRuleTriggerName($index);
         $value = $this->getRuleTriggerValue($index);
         switch ($name) {
             default:
                 return true;
             case 'amount_less':
                 return is_numeric($value);
             case 'transaction_type':
                 $count = TransactionType::where('type', $value)->count();
                 return $count === 1;
             case 'invalid':
                 return false;
         }
     }
     return false;
 }
Esempio n. 15
0
 /**
  * @param Account $account
  * @param array   $data
  *
  * @return TransactionJournal
  */
 protected function storeInitialBalance(Account $account, array $data) : TransactionJournal
 {
     $amount = $data['openingBalance'];
     $user = $data['user'];
     $name = $data['name'];
     $opposing = $this->storeOpposingAccount($amount, $user, $name);
     $transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first();
     $journal = TransactionJournal::create(['user_id' => $data['user'], 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $data['openingBalanceCurrency'], 'description' => 'Initial balance for "' . $account->name . '"', 'completed' => true, 'date' => $data['openingBalanceDate'], 'encrypted' => true]);
     $firstAccount = $account;
     $secondAccount = $opposing;
     $firstAmount = $amount;
     $secondAmount = $amount * -1;
     if ($data['openingBalance'] < 0) {
         $firstAccount = $opposing;
         $secondAccount = $account;
         $firstAmount = $amount * -1;
         $secondAmount = $amount;
     }
     $one = new Transaction(['account_id' => $firstAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $firstAmount]);
     $one->save();
     // first transaction: from
     $two = new Transaction(['account_id' => $secondAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $secondAmount]);
     $two->save();
     // second transaction: to
     return $journal;
 }