/** * */ 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]); }
/** * @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; }
public function testIsOpeningBalance() { $transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first(); $this->assertTrue($transactionType->isOpeningBalance()); }
/** * @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; }
/** * @param $type * * @return TransactionType */ public function getTransactionType($type) { return TransactionType::whereType($type)->first(); }
/** * @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; }
/** * @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 }
/** * @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; }