Now with the added capability of adding money to a piggy bank from a transfer. Also various fixes (see issue #6). [skip ci]

This commit is contained in:
James Cole
2014-08-16 12:13:50 +02:00
parent 7c57ce8504
commit d645a38aec
13 changed files with 239 additions and 211 deletions

View File

@@ -45,6 +45,24 @@ class EloquentPiggybankTrigger
} catch (QueryException $e) {
}
}
// whatever we did here, we now have all repetitions for this
// piggy bank, and we can find transactions that fall within
// that repetition (to fix the "saved amount".
$reps = $piggy->piggybankrepetitions()->get();
/** @var \PiggybankRepetition $rep */
foreach ($reps as $rep) {
$sum = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
)->sum('transactions.amount');
$rep->currentamount = floatval($sum);
$rep->save();
}
}
unset($piggy, $piggybanks, $rep);
@@ -59,14 +77,8 @@ class EloquentPiggybankTrigger
$rep->targetdate = $repeated->targetdate;
$rep->currentamount = 0;
try {
\Log::debug(
'Creating initial rep ('.$repeated->name.') (from ' . ($rep->startdate ? $rep->startdate->format('d-m-Y')
: 'NULL') . ' to '
. ($rep->targetdate ? $rep->targetdate->format('d-m-Y') : 'NULL') . ')'
);
$rep->save();
} catch (QueryException $e) {
\Log::error('FAILED initital repetition.');
}
unset($rep);
@@ -109,6 +121,19 @@ class EloquentPiggybankTrigger
}
}
}
$reps = $repeated->piggybankrepetitions()->get();
/** @var \PiggybankRepetition $rep */
foreach ($reps as $rep) {
$sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
)->sum('transactions.amount');
$rep->currentamount = floatval($sum);
$rep->save();
}
}
}
}