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-15 13:58:35 -05:00
|
|
|
use Firefly\Exception\FireflyException;
|
|
|
|
|
2014-07-05 09:19:15 -05:00
|
|
|
class EloquentTransactionJournalRepository implements TransactionJournalRepositoryInterface
|
2014-07-03 14:31:32 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
public function createSimpleJournal(\Account $from, \Account $to, $description, $amount, \Carbon\Carbon $date)
|
|
|
|
{
|
|
|
|
|
2014-07-06 08:18:11 -05:00
|
|
|
\Log::debug('Creating tranaction "' . $description . '".');
|
2014-07-03 14:31:32 -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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// amounts:
|
|
|
|
$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-07-15 15:16:29 -05:00
|
|
|
\Session::flash('error', 'The amount should not be empty or zero.');
|
2014-07-15 13:58:35 -05:00
|
|
|
throw new \Firefly\Exception\FireflyException('Could not figure out transaction type.');
|
|
|
|
}
|
|
|
|
// same account:
|
2014-07-15 15:16:29 -05:00
|
|
|
if ($from->id == $to->id) {
|
2014-07-15 13:58:35 -05:00
|
|
|
\Log::error('Accounts cannot be equal');
|
2014-07-15 15:16:29 -05:00
|
|
|
\Session::flash('error', 'Select two different accounts.');
|
2014-07-15 13:58:35 -05:00
|
|
|
throw new \Firefly\Exception\FireflyException('Select two different accounts.');
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
// account types for both:
|
|
|
|
$toAT = $to->accountType->description;
|
|
|
|
$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-06 14:07:52 -05:00
|
|
|
case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0):
|
|
|
|
$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-03 14:31:32 -05:00
|
|
|
// both are yours:
|
|
|
|
case ($fromAT == 'Default account' && $toAT == 'Default account'):
|
|
|
|
// determin transaction type. If both accounts are new, it's an initial
|
|
|
|
// balance transfer.
|
|
|
|
$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
|
|
|
|
. ' and AccountTo "' . $to->name . '" will gain/lose ' . $amountTo
|
|
|
|
);
|
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!');
|
|
|
|
throw new \Firefly\Exception\FireflyException('Could not figure out transaction type.');
|
|
|
|
}
|
|
|
|
|
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!');
|
|
|
|
throw new \Firefly\Exception\FireflyException('No currency for journal!');
|
|
|
|
}
|
2014-07-03 14:31:32 -05:00
|
|
|
|
|
|
|
// new journal:
|
|
|
|
$journal = new \TransactionJournal();
|
|
|
|
$journal->transactionType()->associate($journalType);
|
|
|
|
$journal->transactionCurrency()->associate($currency);
|
|
|
|
$journal->completed = false;
|
|
|
|
$journal->description = $description;
|
|
|
|
$journal->date = $date;
|
2014-07-15 10:09:59 -05:00
|
|
|
if (!$journal->save()) {
|
2014-07-05 09:19:15 -05:00
|
|
|
\Log::error('Cannot create valid journal.');
|
2014-07-15 10:09:59 -05:00
|
|
|
\Log::error('Errors: ' . print_r($journal->errors()->all(), true));
|
2014-07-15 15:16:29 -05:00
|
|
|
\Session::flash('error', 'Could not create journal: ' . $journal->errors()->first());
|
2014-07-05 09:19:15 -05:00
|
|
|
throw new \Firefly\Exception\FireflyException('Cannot create valid 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-07-15 10:09:59 -05:00
|
|
|
if (!$fromTransaction->save()) {
|
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-05 09:19:15 -05:00
|
|
|
throw new \Firefly\Exception\FireflyException('Cannot create valid transaction (from).');
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
|
|
|
$fromTransaction->save();
|
|
|
|
|
|
|
|
$toTransaction = new \Transaction;
|
|
|
|
$toTransaction->account()->associate($to);
|
|
|
|
$toTransaction->transactionJournal()->associate($journal);
|
|
|
|
$toTransaction->description = null;
|
|
|
|
$toTransaction->amount = $amountTo;
|
2014-07-15 10:09:59 -05:00
|
|
|
if (!$toTransaction->save()) {
|
|
|
|
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
|
|
|
|
\Log::error('Errors: ' . print_r($toTransaction->errors()->all(), true));
|
|
|
|
throw new \Firefly\Exception\FireflyException('Cannot create valid transaction (to).');
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
|
|
|
$toTransaction->save();
|
|
|
|
|
|
|
|
$journal->completed = true;
|
|
|
|
$journal->save();
|
2014-07-05 12:44:26 -05:00
|
|
|
return $journal;
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
2014-07-06 14:07:52 -05:00
|
|
|
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-09 05:56:06 -05:00
|
|
|
public function homeBudgetChart(\Carbon\Carbon $start, \Carbon\Carbon $end)
|
|
|
|
{
|
|
|
|
return $this->homeComponentChart($start, $end, 'Budget');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function homeComponentChart(\Carbon\Carbon $start, \Carbon\Carbon $end, $chartType)
|
|
|
|
{
|
|
|
|
|
|
|
|
// lets make this simple.
|
|
|
|
$types = [];
|
2014-07-14 23:58:08 -05:00
|
|
|
foreach (\TransactionType::whereIn('type', ['Withdrawal'])->get() as $t) {
|
2014-07-09 05:56:06 -05:00
|
|
|
$types[] = $t->id;
|
|
|
|
}
|
|
|
|
unset($t);
|
|
|
|
|
|
|
|
// get all journals, partly filtered:
|
|
|
|
$journals = \TransactionJournal::
|
|
|
|
with(
|
|
|
|
['components' => function ($q) use ($chartType) {
|
|
|
|
$q->where('class', $chartType);
|
|
|
|
}, 'transactions' => function ($q) {
|
|
|
|
$q->where('amount', '>', 0);
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
->after($start)->before($end)
|
2014-07-15 15:16:29 -05:00
|
|
|
->where('completed', 1)
|
2014-07-09 05:56:06 -05:00
|
|
|
->whereIn('transaction_type_id', $types)
|
|
|
|
->get(['transaction_journals.*']);
|
|
|
|
unset($types);
|
2014-07-14 23:58:08 -05:00
|
|
|
$result = [];
|
2014-07-09 05:56:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
// has to be one:
|
2014-07-15 15:16:29 -05:00
|
|
|
if (!isset($journal->transactions[0])) {
|
|
|
|
throw new FireflyException('Journal #' . $journal->id . ' has ' . count($journal->transactions)
|
|
|
|
. ' transactions!');
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
2014-07-09 05:56:06 -05:00
|
|
|
$transaction = $journal->transactions[0];
|
|
|
|
$amount = floatval($transaction->amount);
|
|
|
|
|
|
|
|
|
|
|
|
// MIGHT be one:
|
|
|
|
$budget = isset($journal->components[0]) ? $journal->components[0] : null;
|
|
|
|
if (!is_null($budget)) {
|
|
|
|
$name = $budget->name;
|
|
|
|
} else {
|
|
|
|
$name = '(no budget)';
|
|
|
|
}
|
|
|
|
$result[$name] = isset($result[$name]) ? $result[$name] + $amount : $amount;
|
|
|
|
|
|
|
|
}
|
|
|
|
unset($journal, $transaction, $budget, $name, $amount);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function homeCategoryChart(\Carbon\Carbon $start, \Carbon\Carbon $end)
|
|
|
|
{
|
|
|
|
return $this->homeComponentChart($start, $end, 'Category');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function homeBeneficiaryChart(\Carbon\Carbon $start, \Carbon\Carbon $end)
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
// lets make this simple.
|
|
|
|
$types = [];
|
2014-07-14 23:58:08 -05:00
|
|
|
foreach (\TransactionType::whereIn('type', ['Withdrawal'])->get() as $t) {
|
2014-07-09 05:56:06 -05:00
|
|
|
$types[] = $t->id;
|
|
|
|
}
|
|
|
|
unset($t);
|
|
|
|
|
|
|
|
// account type we want to see:
|
|
|
|
$accountType = \AccountType::where('description', 'Beneficiary account')->first();
|
|
|
|
$accountTypeID = $accountType->id;
|
|
|
|
|
|
|
|
// get all journals, partly filtered:
|
|
|
|
$journals = \TransactionJournal::
|
|
|
|
with(
|
|
|
|
['transactions', 'transactions.account' => function ($q) use ($accountTypeID) {
|
|
|
|
$q->where('account_type_id', $accountTypeID);
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
->after($start)->before($end)
|
|
|
|
->whereIn('transaction_type_id', $types)
|
|
|
|
->get(['transaction_journals.*']);
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
foreach ($journal->transactions as $t) {
|
|
|
|
if (!is_null($t->account)) {
|
|
|
|
$name = $t->account->name;
|
|
|
|
$amount = floatval($t->amount) < 0 ? floatval($t->amount) * -1 : floatval($t->amount);
|
|
|
|
|
2014-07-15 10:09:59 -05:00
|
|
|
$result[$name] = isset($result[$name]) ? $result[$name] + $amount : $amount;
|
2014-07-09 05:56:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-07-06 14:07:52 -05:00
|
|
|
public function getByAccount(\Account $account, $count = 25)
|
|
|
|
{
|
|
|
|
$accountID = $account->id;
|
|
|
|
$query = \TransactionJournal::
|
|
|
|
with(
|
|
|
|
[
|
|
|
|
'transactions',
|
|
|
|
'transactioncurrency',
|
|
|
|
'transactiontype'
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->take($count)
|
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
|
|
|
->where('accounts.id', $accountID)
|
|
|
|
->orderBy('transaction_journals.date', 'DESC')
|
|
|
|
->orderBy('transaction_journals.id', 'DESC')
|
|
|
|
->take($count)
|
|
|
|
->get(['transaction_journals.*']);
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|