mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Added the ability to manually add or remove money from piggy banks (issue #6) [skip ci]
This commit is contained in:
@@ -69,7 +69,51 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
||||
$piggies = \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
||||
|
||||
foreach($piggies as $pig) {
|
||||
$pig->leftInAccount = $this->leftOnAccount($pig->account);
|
||||
}
|
||||
return $piggies;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function leftOnAccount(\Account $account)
|
||||
{
|
||||
$balance = $account->balance();
|
||||
/** @var \Piggybank $p */
|
||||
foreach ($account->piggybanks()->get() as $p) {
|
||||
$balance -= $p->currentRelevantRep()->currentamount;
|
||||
}
|
||||
|
||||
return $balance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
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();
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,18 +226,4 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function updateAmount(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$piggyBank->amount = floatval($amount);
|
||||
if ($piggyBank->validate()) {
|
||||
$piggyBank->save();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -45,6 +45,23 @@ interface PiggybankRepositoryInterface
|
||||
*/
|
||||
public function get();
|
||||
|
||||
/**
|
||||
* Will tell you how much money is left on this account.
|
||||
*
|
||||
* @param \Account $account
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function leftOnAccount(\Account $account);
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function modifyAmount(\Piggybank $piggyBank, $amount);
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
@@ -60,12 +77,5 @@ interface PiggybankRepositoryInterface
|
||||
*/
|
||||
public function update(\Piggybank $piggy, $data);
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateAmount(\Piggybank $piggyBank, $amount);
|
||||
|
||||
}
|
@@ -52,12 +52,15 @@ class EloquentPiggybankTrigger
|
||||
$reps = $piggy->piggybankrepetitions()->get();
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
foreach ($reps as $rep) {
|
||||
$sum = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
if ($rep->currentamount == 0) {
|
||||
$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);
|
||||
'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