mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Some changes to facilitate piggy bank events (see issue #6) and extra view info for clarity. [skip ci]
This commit is contained in:
@@ -71,7 +71,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
{
|
||||
$piggies = \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
||||
|
||||
foreach($piggies as $pig) {
|
||||
foreach ($piggies as $pig) {
|
||||
$pig->leftInAccount = $this->leftOnAccount($pig->account);
|
||||
}
|
||||
return $piggies;
|
||||
@@ -104,12 +104,17 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
public function modifyAmount(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
\Log::debug('Amount before: ' . $rep->currentamount);
|
||||
$rep->currentamount += $amount;
|
||||
\Log::debug('Amount after: ' . $rep->currentamount);
|
||||
\Log::debug('validates: ' . $rep->validate());
|
||||
\Log::debug(print_r($rep->toArray(),true));
|
||||
$rep->save();
|
||||
if (!is_null($rep)) {
|
||||
$rep->currentamount += $amount;
|
||||
$rep->save();
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
@@ -129,7 +134,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
if ($data['reminder'] == 'none') {
|
||||
unset($data['reminder']);
|
||||
}
|
||||
if($data['startdate'] == '') {
|
||||
if ($data['startdate'] == '') {
|
||||
unset($data['startdate']);
|
||||
}
|
||||
|
||||
|
@@ -51,24 +51,35 @@ class EloquentPiggybankTrigger
|
||||
// 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) {
|
||||
if ($rep->currentamount == 0) {
|
||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$query->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
);
|
||||
}
|
||||
$sum = $query->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$query->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
);
|
||||
}
|
||||
|
||||
// get events for piggy bank, save those as well:
|
||||
$eventSumQuery = $piggy->piggybankevents();
|
||||
if(!is_null($rep->startdate)) {
|
||||
$eventSumQuery->where('date','>=',$rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if(!is_null($rep->targetdate)) {
|
||||
$eventSumQuery->where('date','<=',$rep->targetdate->format('Y-m-d'));
|
||||
}
|
||||
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
|
||||
|
||||
$sum = $query->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum) + $eventSum;
|
||||
$rep->save();
|
||||
|
||||
|
||||
@@ -138,8 +149,8 @@ class EloquentPiggybankTrigger
|
||||
$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');
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$rep->save();
|
||||
|
||||
|
Reference in New Issue
Block a user