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-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;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// is withdrawal from one of your own accounts:
|
|
|
|
case ($fromAT == 'Default account'):
|
|
|
|
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
|
|
|
|
break;
|
|
|
|
// 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;
|
|
|
|
case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0):
|
|
|
|
$journalType = \TransactionType::where('type', 'Opening balance')->first();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// is deposit into one of your own accounts:
|
|
|
|
case ($toAT == 'Default account'):
|
|
|
|
$journalType = \TransactionType::where('type', 'Deposit')->first();
|
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
if (!$journal->isValid()) {
|
2014-07-05 09:19:15 -05:00
|
|
|
\Log::error('Cannot create valid journal.');
|
|
|
|
\Log::error('Errors: ' . print_r($journal->validator->messages()->all(), true));
|
|
|
|
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;
|
|
|
|
if (!$fromTransaction->isValid()) {
|
2014-07-05 09:19:15 -05:00
|
|
|
\Log::error('Cannot create valid transaction (from) for journal #' . $journal->id);
|
|
|
|
\Log::error('Errors: ' . print_r($fromTransaction->validator->messages()->all(), true));
|
|
|
|
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;
|
|
|
|
if (!$toTransaction->isValid()) {
|
2014-07-05 09:19:15 -05:00
|
|
|
if (!$toTransaction->isValid()) {
|
|
|
|
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
|
|
|
|
\Log::error('Errors: ' . print_r($toTransaction->validator->messages()->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
|
|
|
}
|
|
|
|
}
|