2014-07-03 14:31:32 -05:00
|
|
|
<?php
|
2014-07-06 08:18:11 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
|
|
|
|
namespace Firefly\Storage\TransactionJournal;
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Firefly\Exception\FireflyException;
|
2014-07-03 14:31:32 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* Class EloquentTransactionJournalRepository
|
|
|
|
*
|
|
|
|
* @package Firefly\Storage\TransactionJournal
|
|
|
|
*/
|
2014-07-05 09:19:15 -05:00
|
|
|
class EloquentTransactionJournalRepository implements TransactionJournalRepositoryInterface
|
2014-07-03 14:31:32 -05:00
|
|
|
{
|
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* We're building this thinking the money goes from A to B.
|
|
|
|
* If the amount is negative however, the money still goes
|
|
|
|
* from A to B but the balances are reversed.
|
|
|
|
*
|
|
|
|
* Aka:
|
|
|
|
*
|
|
|
|
* Amount = 200
|
|
|
|
* A loses 200 (-200). * -1
|
|
|
|
* B gains 200 (200). * 1
|
|
|
|
*
|
|
|
|
* Final balance: -200 for A, 200 for B.
|
|
|
|
*
|
|
|
|
* When the amount is negative:
|
|
|
|
*
|
|
|
|
* Amount = -200
|
|
|
|
* A gains 200 (200). * -1
|
|
|
|
* B loses 200 (-200). * 1
|
|
|
|
*
|
|
|
|
* @param \Account $from
|
2014-07-25 06:02:01 -05:00
|
|
|
* @param \Account $toAccount
|
2014-07-16 14:11:43 -05:00
|
|
|
* @param $description
|
|
|
|
* @param $amount
|
|
|
|
* @param \Carbon\Carbon $date
|
|
|
|
*
|
|
|
|
* @return \TransactionJournal
|
|
|
|
* @throws \Firefly\Exception\FireflyException
|
|
|
|
*/
|
2014-07-25 06:02:01 -05:00
|
|
|
public function createSimpleJournal(\Account $from, \Account $toAccount, $description, $amount, Carbon $date)
|
2014-07-03 14:31:32 -05:00
|
|
|
{
|
2014-07-06 08:18:11 -05:00
|
|
|
\Log::debug('Creating tranaction "' . $description . '".');
|
2014-08-02 00:34:38 -05:00
|
|
|
$journal = new \TransactionJournal;
|
2014-07-16 14:11:43 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$amountFrom = $amount * -1;
|
|
|
|
$amountTo = $amount;
|
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
if (round(floatval($amount), 2) == 0.00) {
|
2014-07-15 13:58:35 -05:00
|
|
|
\Log::error('Transaction will never save: amount = 0');
|
2014-08-02 00:34:38 -05:00
|
|
|
$journal->errors()->add('amount', 'Amount must not be zero.');
|
|
|
|
|
|
|
|
return $journal;
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
|
|
|
// same account:
|
2014-07-25 06:02:01 -05:00
|
|
|
if ($from->id == $toAccount->id) {
|
2014-07-15 13:58:35 -05:00
|
|
|
\Log::error('Accounts cannot be equal');
|
2014-08-02 00:34:38 -05:00
|
|
|
$journal->errors()->add('account_id', 'Must be different accounts.');
|
|
|
|
$journal->errors()->add('account_from_id', 'Must be different accounts.');
|
|
|
|
|
|
|
|
return $journal;
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
// account types for both:
|
2014-07-25 06:02:01 -05:00
|
|
|
$toAT = $toAccount->accountType->description;
|
2014-07-03 14:31:32 -05:00
|
|
|
$fromAT = $from->accountType->description;
|
|
|
|
|
2014-07-05 09:19:15 -05:00
|
|
|
$journalType = null;
|
2014-07-03 14:31:32 -05:00
|
|
|
|
|
|
|
switch (true) {
|
2014-07-25 06:02:01 -05:00
|
|
|
case ($from->transactions()->count() == 0 && $toAccount->transactions()->count() == 0):
|
2014-07-06 14:07:52 -05:00
|
|
|
$journalType = \TransactionType::where('type', 'Opening balance')->first();
|
2014-07-03 14:31:32 -05:00
|
|
|
break;
|
2014-07-06 14:07:52 -05:00
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
case ($fromAT == 'Default account' && $toAT == 'Default account'): // both are yours:
|
|
|
|
// determin transaction type. If both accounts are new, it's an initial balance transfer.
|
2014-07-03 14:31:32 -05:00
|
|
|
$journalType = \TransactionType::where('type', 'Transfer')->first();
|
|
|
|
break;
|
2014-07-06 14:07:52 -05:00
|
|
|
case ($amount < 0):
|
|
|
|
$journalType = \TransactionType::where('type', 'Deposit')->first();
|
2014-07-03 14:31:32 -05:00
|
|
|
break;
|
2014-07-06 14:07:52 -05:00
|
|
|
// is deposit into one of your own accounts:
|
2014-07-03 14:31:32 -05:00
|
|
|
case ($toAT == 'Default account'):
|
|
|
|
$journalType = \TransactionType::where('type', 'Deposit')->first();
|
|
|
|
break;
|
2014-07-06 14:07:52 -05:00
|
|
|
// is withdrawal from one of your own accounts:
|
|
|
|
case ($fromAT == 'Default account'):
|
|
|
|
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
|
|
|
|
break;
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
2014-07-05 12:44:26 -05:00
|
|
|
|
|
|
|
// some debug information:
|
2014-07-06 08:18:11 -05:00
|
|
|
\Log::debug(
|
|
|
|
$journalType->type . ': AccountFrom "' . $from->name . '" will gain/lose ' . $amountFrom
|
2014-07-25 06:02:01 -05:00
|
|
|
. ' and AccountTo "' . $toAccount->name . '" will gain/lose ' . $amountTo
|
2014-07-06 08:18:11 -05:00
|
|
|
);
|
2014-07-05 12:44:26 -05:00
|
|
|
|
2014-07-05 09:19:15 -05:00
|
|
|
if (is_null($journalType)) {
|
|
|
|
\Log::error('Could not figure out transacion type!');
|
2014-07-25 06:02:01 -05:00
|
|
|
throw new FireflyException('Could not figure out transaction type.');
|
2014-07-05 09:19:15 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
// always the same currency:
|
|
|
|
$currency = \TransactionCurrency::where('code', 'EUR')->first();
|
2014-07-05 09:19:15 -05:00
|
|
|
if (is_null($currency)) {
|
|
|
|
\Log::error('No currency for journal!');
|
2014-07-25 06:02:01 -05:00
|
|
|
throw new FireflyException('No currency for journal!');
|
2014-07-05 09:19:15 -05:00
|
|
|
}
|
2014-07-03 14:31:32 -05:00
|
|
|
|
|
|
|
// new journal:
|
2014-08-02 00:34:38 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$journal->transactionType()->associate($journalType);
|
|
|
|
$journal->transactionCurrency()->associate($currency);
|
2014-07-16 14:11:43 -05:00
|
|
|
$journal->user()->associate(\Auth::user());
|
2014-07-03 14:31:32 -05:00
|
|
|
$journal->completed = false;
|
|
|
|
$journal->description = $description;
|
|
|
|
$journal->date = $date;
|
2014-08-02 00:34:38 -05:00
|
|
|
if (!$journal->validate()) {
|
|
|
|
return $journal;
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
// create transactions:
|
|
|
|
$fromTransaction = new \Transaction;
|
|
|
|
$fromTransaction->account()->associate($from);
|
|
|
|
$fromTransaction->transactionJournal()->associate($journal);
|
|
|
|
$fromTransaction->description = null;
|
|
|
|
$fromTransaction->amount = $amountFrom;
|
2014-08-02 00:34:38 -05:00
|
|
|
if (!$fromTransaction->validate()) {
|
2014-07-05 09:19:15 -05:00
|
|
|
\Log::error('Cannot create valid transaction (from) for journal #' . $journal->id);
|
2014-07-15 10:09:59 -05:00
|
|
|
\Log::error('Errors: ' . print_r($fromTransaction->errors()->all(), true));
|
2014-07-25 06:02:01 -05:00
|
|
|
throw new FireflyException('Cannot create valid transaction (from).');
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
|
|
|
$fromTransaction->save();
|
|
|
|
|
|
|
|
$toTransaction = new \Transaction;
|
2014-07-25 06:02:01 -05:00
|
|
|
$toTransaction->account()->associate($toAccount);
|
2014-07-03 14:31:32 -05:00
|
|
|
$toTransaction->transactionJournal()->associate($journal);
|
|
|
|
$toTransaction->description = null;
|
|
|
|
$toTransaction->amount = $amountTo;
|
2014-08-02 00:34:38 -05:00
|
|
|
if (!$toTransaction->validate()) {
|
2014-07-15 10:09:59 -05:00
|
|
|
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
|
|
|
|
\Log::error('Errors: ' . print_r($toTransaction->errors()->all(), true));
|
2014-07-25 06:02:01 -05:00
|
|
|
throw new FireflyException('Cannot create valid transaction (to).');
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
|
|
|
$toTransaction->save();
|
|
|
|
|
|
|
|
$journal->completed = true;
|
|
|
|
$journal->save();
|
2014-07-29 12:37:52 -05:00
|
|
|
|
2014-07-05 12:44:26 -05:00
|
|
|
return $journal;
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
2014-08-02 00:34:38 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $journalId
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function find($journalId)
|
|
|
|
{
|
|
|
|
return \Auth::user()->transactionjournals()->with(
|
|
|
|
['transactions' => function ($q) {
|
|
|
|
return $q->orderBy('amount', 'ASC');
|
|
|
|
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
|
|
|
|
'transactions.account.accounttype']
|
|
|
|
)
|
|
|
|
->where('id', $journalId)->first();
|
|
|
|
}
|
2014-07-06 14:07:52 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-07-06 14:07:52 -05:00
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param \Account $account
|
2014-08-02 00:34:38 -05:00
|
|
|
* @param Carbon $date
|
2014-07-25 06:02:01 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function getByAccountAndDate(\Account $account, Carbon $date)
|
2014-07-09 05:56:06 -05:00
|
|
|
{
|
2014-07-17 13:52:54 -05:00
|
|
|
$accountID = $account->id;
|
|
|
|
$query = \Auth::user()->transactionjournals()->with(
|
|
|
|
[
|
|
|
|
'transactions',
|
2014-08-02 00:34:38 -05:00
|
|
|
'transactions.account',
|
2014-07-17 13:52:54 -05:00
|
|
|
'transactioncurrency',
|
|
|
|
'transactiontype'
|
|
|
|
]
|
|
|
|
)
|
2014-08-02 00:34:38 -05:00
|
|
|
->distinct()
|
2014-07-17 13:52:54 -05:00
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
2014-08-02 00:34:38 -05:00
|
|
|
->where('transactions.account_id', $accountID)
|
|
|
|
->where('transaction_journals.date', $date->format('Y-m-d'))
|
2014-07-17 13:52:54 -05:00
|
|
|
->orderBy('transaction_journals.date', 'DESC')
|
|
|
|
->orderBy('transaction_journals.id', 'DESC')
|
|
|
|
->get(['transaction_journals.*']);
|
2014-07-29 12:37:52 -05:00
|
|
|
|
2014-07-17 13:52:54 -05:00
|
|
|
return $query;
|
2014-07-09 05:56:06 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
2014-08-02 00:34:38 -05:00
|
|
|
* @param \Account $account
|
|
|
|
* @param int $count
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2014-07-25 06:02:01 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function getByAccountInDateRange(\Account $account, $count = 25, Carbon $start, Carbon $end)
|
2014-07-25 00:17:56 -05:00
|
|
|
{
|
2014-08-02 00:34:38 -05:00
|
|
|
$accountID = $account->id;
|
2014-07-25 00:17:56 -05:00
|
|
|
$query = \Auth::user()->transactionjournals()->with(
|
|
|
|
[
|
2014-08-02 00:34:38 -05:00
|
|
|
'transactions',
|
2014-07-25 00:17:56 -05:00
|
|
|
'transactioncurrency',
|
|
|
|
'transactiontype'
|
|
|
|
]
|
|
|
|
)
|
2014-08-02 00:34:38 -05:00
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
|
|
|
->where('accounts.id', $accountID)
|
|
|
|
->where('date', '>=', $start->format('Y-m-d'))
|
|
|
|
->where('date', '<=', $end->format('Y-m-d'))
|
2014-07-25 00:17:56 -05:00
|
|
|
->orderBy('transaction_journals.date', 'DESC')
|
|
|
|
->orderBy('transaction_journals.id', 'DESC')
|
|
|
|
->take($count)
|
2014-08-02 00:34:38 -05:00
|
|
|
->get(['transaction_journals.*']);
|
2014-07-29 12:37:52 -05:00
|
|
|
|
2014-07-25 00:17:56 -05:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getByDateRange(Carbon $start, Carbon $end)
|
2014-07-09 05:56:06 -05:00
|
|
|
{
|
2014-07-29 12:37:52 -05:00
|
|
|
die('no impl');
|
2014-07-09 05:56:06 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
2014-08-02 00:34:38 -05:00
|
|
|
* @param int $count
|
2014-07-25 06:02:01 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function paginate($count = 25)
|
2014-07-06 14:07:52 -05:00
|
|
|
{
|
2014-07-17 13:52:54 -05:00
|
|
|
$query = \Auth::user()->transactionjournals()->with(
|
|
|
|
[
|
2014-08-02 00:34:38 -05:00
|
|
|
'transactions' => function ($q) {
|
|
|
|
return $q->orderBy('amount', 'ASC');
|
|
|
|
},
|
2014-07-17 13:52:54 -05:00
|
|
|
'transactions.account',
|
2014-08-02 00:34:38 -05:00
|
|
|
'transactions.account.accounttype',
|
2014-07-17 13:52:54 -05:00
|
|
|
'transactioncurrency',
|
|
|
|
'transactiontype'
|
|
|
|
]
|
|
|
|
)
|
2014-07-06 14:07:52 -05:00
|
|
|
->orderBy('transaction_journals.date', 'DESC')
|
|
|
|
->orderBy('transaction_journals.id', 'DESC')
|
2014-08-02 00:34:38 -05:00
|
|
|
->paginate($count);
|
2014-07-29 12:37:52 -05:00
|
|
|
|
2014-07-06 14:07:52 -05:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
public function store($what, $data)
|
|
|
|
{
|
|
|
|
// $fromAccount and $toAccount are found
|
|
|
|
// depending on the $what
|
|
|
|
|
|
|
|
$fromAccount = null;
|
|
|
|
$toAccount = null;
|
|
|
|
|
|
|
|
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
|
|
|
|
|
|
|
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $catRepository */
|
|
|
|
$catRepository = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
|
|
|
|
|
|
|
|
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budRepository */
|
|
|
|
$budRepository = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
|
|
|
|
|
|
|
|
|
|
|
|
switch ($what) {
|
|
|
|
case 'withdrawal':
|
|
|
|
$fromAccount = $accountRepository->find(intval($data['account_id']));
|
|
|
|
$toAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
|
|
|
|
break;
|
|
|
|
case 'deposit':
|
|
|
|
$fromAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
|
|
|
|
$toAccount = $accountRepository->find(intval($data['account_id']));
|
|
|
|
break;
|
|
|
|
case 'transfer':
|
|
|
|
$fromAccount = $accountRepository->find(intval($data['account_from_id']));
|
|
|
|
$toAccount = $accountRepository->find(intval($data['account_to_id']));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall back to cash if necessary:
|
|
|
|
$fromAccount = is_null($fromAccount) ? $fromAccount = $accountRepository->getCashAccount() : $fromAccount;
|
|
|
|
$toAccount = is_null($toAccount) ? $toAccount = $accountRepository->getCashAccount() : $toAccount;
|
|
|
|
|
|
|
|
// create or find category:
|
|
|
|
$category = isset($data['category']) ? $catRepository->createOrFind($data['category']) : null;
|
|
|
|
|
|
|
|
// find budget:
|
|
|
|
$budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null;
|
|
|
|
//
|
|
|
|
// // find amount & description:
|
|
|
|
$description = trim($data['description']);
|
|
|
|
$amount = floatval($data['amount']);
|
|
|
|
$date = new \Carbon\Carbon($data['date']);
|
|
|
|
|
|
|
|
// try to create a journal:
|
|
|
|
$transactionJournal = $this->createSimpleJournal($fromAccount, $toAccount, $description, $amount, $date);
|
|
|
|
if (!$transactionJournal->id || $transactionJournal->completed == 0) {
|
|
|
|
return $transactionJournal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach:
|
|
|
|
if (!is_null($budget)) {
|
|
|
|
$transactionJournal->budgets()->save($budget);
|
|
|
|
}
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$transactionJournal->categories()->save($category);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transactionJournal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(\TransactionJournal $journal, $data)
|
|
|
|
{
|
|
|
|
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $catRepository */
|
|
|
|
$catRepository = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
|
|
|
|
|
|
|
|
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgetRepository */
|
|
|
|
$budRepository = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
|
|
|
|
|
|
|
|
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
|
|
|
|
|
|
|
|
|
|
|
// update basics first:
|
|
|
|
$journal->description = $data['description'];
|
|
|
|
$journal->date = $data['date'];
|
|
|
|
$amount = floatval($data['amount']);
|
|
|
|
|
|
|
|
// remove previous category, if any:
|
|
|
|
if (!is_null($journal->categories()->first())) {
|
|
|
|
$journal->categories()->detach($journal->categories()->first()->id);
|
|
|
|
}
|
|
|
|
// remove previous budget, if any:
|
|
|
|
if (!is_null($journal->budgets()->first())) {
|
|
|
|
$journal->budgets()->detach($journal->budgets()->first()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$category = isset($data['category']) ? $catRepository->findByName($data['category']) : null;
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$journal->categories()->attach($category);
|
|
|
|
}
|
|
|
|
// update the amounts:
|
|
|
|
/** @var \Transaction $transaction */
|
|
|
|
$transactions = $journal->transactions()->orderBy('amount', 'ASC')->get();
|
2014-08-08 23:32:55 -05:00
|
|
|
$transactions[0]->amount = $amount * -1;
|
2014-08-02 00:34:38 -05:00
|
|
|
$transactions[1]->amount = $amount;
|
|
|
|
|
|
|
|
// switch on type to properly change things:
|
|
|
|
switch ($journal->transactiontype->type) {
|
|
|
|
case 'Withdrawal':
|
|
|
|
// means transaction[0] is the users account.
|
|
|
|
$account = $accountRepository->find($data['account_id']);
|
2014-08-08 23:12:49 -05:00
|
|
|
$beneficiary = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
|
2014-08-02 00:34:38 -05:00
|
|
|
$transactions[0]->account()->associate($account);
|
|
|
|
$transactions[1]->account()->associate($beneficiary);
|
|
|
|
|
|
|
|
// do budget:
|
|
|
|
$budget = $budRepository->find($data['budget_id']);
|
2014-08-10 04:30:14 -05:00
|
|
|
if (!is_null($budget)) {
|
2014-08-07 00:44:37 -05:00
|
|
|
$journal->budgets()->attach($budget);
|
|
|
|
}
|
2014-08-02 00:34:38 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'Deposit':
|
|
|
|
// means transaction[0] is the beneficiary.
|
|
|
|
$account = $accountRepository->find($data['account_id']);
|
2014-08-08 23:12:49 -05:00
|
|
|
$beneficiary = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
|
2014-08-02 00:34:38 -05:00
|
|
|
$journal->transactions[0]->account()->associate($beneficiary);
|
|
|
|
$journal->transactions[1]->account()->associate($account);
|
|
|
|
break;
|
|
|
|
case 'Transfer':
|
|
|
|
// means transaction[0] is account that sent the money (from).
|
|
|
|
$fromAccount = $accountRepository->find($data['account_from_id']);
|
|
|
|
$toAccount = $accountRepository->find($data['account_to_id']);
|
|
|
|
$journal->transactions[0]->account()->associate($fromAccount);
|
|
|
|
$journal->transactions[1]->account()->associate($toAccount);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Firefly\Exception\FireflyException('Cannot edit this!');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transactions[0]->save();
|
|
|
|
$transactions[1]->save();
|
|
|
|
if ($journal->validate()) {
|
|
|
|
$journal->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 14:07:52 -05:00
|
|
|
|
|
|
|
}
|