/** * @param Carbon $date * * @return TransactionJournal */ protected function createSavings(Carbon $date) { $date = new Carbon($date->format('Y-m') . '-24'); // paid on 24th. $toAccount = $this->findAccount('Savings'); $fromAccount = $this->findAccount('MyBank Checking Account'); $category = Category::firstOrCreateEncrypted(['name' => 'Money management', 'user_id' => $this->user->id]); // create journal: $journal = TransactionJournal::create(['user_id' => $this->user->id, 'transaction_type_id' => 3, 'transaction_currency_id' => 1, 'description' => 'Save money', 'completed' => 1, 'date' => $date]); Transaction::create(['account_id' => $fromAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => -150]); Transaction::create(['account_id' => $toAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => 150]); $journal->categories()->save($category); return $journal; }
/** * @param $description * @param Carbon $date * @param $amount * * @return TransactionJournal */ protected function createWater($description, Carbon $date, $amount) { $date = new Carbon($date->format('Y-m') . '-10'); // paid on 10th $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); $toAccount = TestData::findAccount($this->user, 'Vitens'); $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); $journal = TransactionJournal::create(['user_id' => $this->user->id, 'transaction_type_id' => 1, 'transaction_currency_id' => 1, 'description' => $description, 'completed' => 1, 'date' => $date]); Transaction::create(['account_id' => $fromAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $amount * -1]); Transaction::create(['account_id' => $toAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $amount]); $journal->categories()->save($category); $journal->budgets()->save($budget); return $journal; }
/** * @param array $data * * @return TransactionJournal */ public function createJournal(array $data) { $user = User::whereEmail('*****@*****.**')->first(); $billID = isset($data['bill']) ? $data['bill']->id : null; /** @var TransactionJournal $journal */ $journal = TransactionJournal::create(['user_id' => $user->id, 'transaction_type_id' => $data['transactionType']->id, 'transaction_currency_id' => $data['transactionCurrency']->id, 'bill_id' => $billID, 'description' => $data['description'], 'completed' => 1, 'date' => $data['date']]); Transaction::create(['account_id' => $data['from']->id, 'transaction_journal_id' => $journal->id, 'amount' => $data['amount'] * -1]); Transaction::create(['account_id' => $data['to']->id, 'transaction_journal_id' => $journal->id, 'amount' => $data['amount']]); if (isset($data['budget'])) { $journal->budgets()->save($data['budget']); } if (isset($data['category'])) { $journal->categories()->save($data['category']); } return $journal; }
/** * @param User $user */ private function openingBalanceSavings(User $user) { // opposing account for opening balance: $opposing = Account::create(['user_id' => $user->id, 'account_type_id' => 6, 'name' => 'Opposing for savings', 'active' => 1, 'encrypted' => 1]); // savings $savings = TestData::findAccount($user, 'TestData Savings'); $journal = TransactionJournal::create(['user_id' => $user->id, 'transaction_type_id' => 4, 'transaction_currency_id' => 1, 'description' => 'Opening balance for savings account', 'completed' => 1, 'date' => $this->start->format('Y-m-d')]); // transactions Transaction::create(['account_id' => $opposing->id, 'transaction_journal_id' => $journal->id, 'amount' => -10000]); Transaction::create(['account_id' => $savings->id, 'transaction_journal_id' => $journal->id, 'amount' => 10000]); }
/** * @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; }
/** * * @return TransactionJournal|string */ protected function createTransactionJournal() { bcscale(2); $date = $this->importData['date']; if (is_null($this->importData['date'])) { $date = $this->importData['date-rent']; } $transactionType = $this->getTransactionType(); // defaults to deposit $errors = new MessageBag(); $journal = TransactionJournal::create(['user_id' => Auth::user()->id, 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $this->importData['currency']->id, 'description' => $this->importData['description'], 'completed' => 0, 'date' => $date, 'bill_id' => $this->importData['bill-id']]); if ($journal->getErrors()->count() == 0) { // first transaction $accountId = $this->importData['asset-account-object']->id; // create first transaction: $amount = $this->importData['amount']; $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); $errors = $transaction->getErrors(); // second transaction $accountId = $this->importData['opposing-account-object']->id; // create second transaction: $amount = bcmul($this->importData['amount'], -1); $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); $errors = $transaction->getErrors()->merge($errors); } if ($errors->count() == 0) { $journal->completed = 1; $journal->save(); } else { $text = join(',', $errors->all()); return $text; } $this->saveBudget($journal); $this->saveCategory($journal); $this->saveTags($journal); // some debug info: $journalId = $journal->id; $type = $journal->getTransactionType(); /** @var Account $asset */ $asset = $this->importData['asset-account-object']; /** @var Account $opposing */ $opposing = $this->importData['opposing-account-object']; Log::info('Created journal #' . $journalId . ' of type ' . $type . '!'); Log::info('Asset account ****** (#' . $asset->id . ') lost/gained: ' . $this->importData['amount']); Log::info($opposing->accountType->type . ' ****** (#' . $opposing->id . ') lost/gained: ' . bcmul($this->importData['amount'], -1)); return $journal; }
/** * @param $entry * * @return TransactionJournal */ private function storeJournal($entry) : TransactionJournal { $billId = is_null($entry->fields['bill']) || intval($entry->fields['bill']->id) === 0 ? null : intval($entry->fields['bill']->id); $journalData = ['user_id' => $entry->user->id, 'transaction_type_id' => $entry->fields['transaction-type']->id, 'bill_id' => $billId, 'transaction_currency_id' => $entry->fields['currency']->id, 'description' => $entry->fields['description'], 'date' => $entry->fields['date-transaction'], 'interest_date' => $entry->fields['date-interest'], 'book_date' => $entry->fields['date-book'], 'process_date' => $entry->fields['date-process'], 'completed' => 0]; /** @var TransactionJournal $journal */ $journal = TransactionJournal::create($journalData); foreach ($journal->getErrors()->all() as $err) { Log::error('Error when storing journal: ' . $err); } Log::debug('Created journal', ['id' => $journal->id]); // save hash as meta value: $meta = new TransactionJournalMeta(); $meta->name = 'originalImportHash'; $meta->data = $entry->hash; $meta->transactionJournal()->associate($journal); $meta->save(); return $journal; }
/** * @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; }