/** * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse */ public function postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) { $amount = round(Input::get('amount'), 2); bcscale(2); $savedSoFar = $piggyBank->currentRelevantRep()->currentamount; if ($amount <= $savedSoFar) { $repetition = $piggyBank->currentRelevantRep(); $repetition->currentamount = bcsub($repetition->currentamount, $amount); $repetition->save(); // create event $repository->createEvent($piggyBank, $amount * -1); Session::flash('success', 'Removed ' . Amount::format($amount, false) . ' from "' . e($piggyBank->name) . '".'); Preferences::mark(); } else { Session::flash('error', 'Could not remove ' . Amount::format($amount, false) . ' from "' . e($piggyBank->name) . '".'); } return redirect(route('piggy-banks.index')); }
/** * @param PiggyBank $piggyBank * @param array $data * * @return PiggyBank */ public function update(PiggyBank $piggyBank, array $data) : PiggyBank { $piggyBank->name = $data['name']; $piggyBank->account_id = intval($data['account_id']); $piggyBank->targetamount = round($data['targetamount'], 2); $piggyBank->targetdate = $data['targetdate']; $piggyBank->startdate = $data['startdate']; $piggyBank->save(); // if the piggy bank is now smaller than the current relevant rep, // remove money from the rep. $repetition = $piggyBank->currentRelevantRep(); if ($repetition->currentamount > $piggyBank->targetamount) { $repetition->currentamount = $piggyBank->targetamount; $repetition->save(); } return $piggyBank; }
/** * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse */ public function postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) { $amount = strval(round(Input::get('amount'), 2)); $savedSoFar = $piggyBank->currentRelevantRep()->currentamount; if ($amount <= $savedSoFar) { $repetition = $piggyBank->currentRelevantRep(); $repetition->currentamount = bcsub($repetition->currentamount, $amount); $repetition->save(); // create event $repository->createEvent($piggyBank, bcmul($amount, '-1')); Session::flash('success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); Preferences::mark(); return redirect(route('piggy-banks.index')); } Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); return redirect(route('piggy-banks.index')); }