From 36329e596eae518eb979e0286200b27c4600e7b8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 21 Apr 2018 23:48:54 +0200 Subject: [PATCH] Fix #1334 --- app/Factory/PiggyBankEventFactory.php | 4 +- app/Http/Controllers/PiggyBankController.php | 192 +++++++++++------- app/Http/Requests/PiggyBankFormRequest.php | 1 - .../PiggyBank/PiggyBankRepository.php | 36 +++- .../PiggyBankRepositoryInterface.php | 13 +- app/Transformers/AccountTransformer.php | 16 +- app/Transformers/PiggyBankTransformer.php | 59 ++++-- resources/views/list/piggy-banks.twig | 48 ++--- resources/views/piggy-banks/add-mobile.twig | 4 +- resources/views/piggy-banks/add.twig | 6 +- resources/views/piggy-banks/create.twig | 2 +- resources/views/piggy-banks/edit.twig | 2 +- resources/views/piggy-banks/index.twig | 22 +- .../views/piggy-banks/remove-mobile.twig | 6 +- resources/views/piggy-banks/remove.twig | 6 +- 15 files changed, 263 insertions(+), 154 deletions(-) diff --git a/app/Factory/PiggyBankEventFactory.php b/app/Factory/PiggyBankEventFactory.php index 441bc9a130..d55ba7dc8f 100644 --- a/app/Factory/PiggyBankEventFactory.php +++ b/app/Factory/PiggyBankEventFactory.php @@ -62,8 +62,8 @@ class PiggyBankEventFactory $piggyRepos->setUser($journal->user); // repetition exists? - $repetition = $piggyRepos->getRepetition($piggyBank, $journal->date); - if (null === $repetition->id) { + $repetition = $piggyRepos->getRepetition($piggyBank); + if (null === $repetition) { Log::error(sprintf('No piggy bank repetition on %s!', $journal->date->format('Y-m-d'))); return null; diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php index e4b79dc5ea..8b73c8fb14 100644 --- a/app/Http/Controllers/PiggyBankController.php +++ b/app/Http/Controllers/PiggyBankController.php @@ -25,13 +25,18 @@ namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use FireflyIII\Http\Requests\PiggyBankFormRequest; use FireflyIII\Models\PiggyBank; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use FireflyIII\Transformers\AccountTransformer; +use FireflyIII\Transformers\PiggyBankTransformer; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; use Log; use Preferences; use Session; -use Steam; +use Symfony\Component\HttpFoundation\ParameterBag; use View; /** @@ -39,6 +44,14 @@ use View; */ class PiggyBankController extends Controller { + + /** @var AccountRepositoryInterface */ + private $accountRepos; + /** @var CurrencyRepositoryInterface */ + private $currencyRepos; + /** @var PiggyBankRepositoryInterface */ + private $piggyRepos; + /** * */ @@ -51,6 +64,10 @@ class PiggyBankController extends Controller app('view')->share('title', trans('firefly.piggyBanks')); app('view')->share('mainTitleIcon', 'fa-sort-amount-asc'); + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + $this->currencyRepos = app(CurrencyRepositoryInterface::class); + $this->accountRepos = app(AccountRepositoryInterface::class); + return $next($request); } ); @@ -59,43 +76,53 @@ class PiggyBankController extends Controller /** * Add money to piggy bank. * - * @param PiggyBank $piggyBank - * - * @param PiggyBankRepositoryInterface $repository + * @param PiggyBank $piggyBank * * @return View */ - public function add(PiggyBank $piggyBank, PiggyBankRepositoryInterface $repository) + public function add(PiggyBank $piggyBank) { /** @var Carbon $date */ $date = session('end', Carbon::now()->endOfMonth()); - $leftOnAccount = $piggyBank->leftOnAccount($date); - $savedSoFar = $repository->getCurrentAmount($piggyBank); + $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date); + $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $maxAmount = min($leftOnAccount, $leftToSave); - return view('piggy-banks.add', compact('piggyBank', 'maxAmount')); + // get currency: + $currency = app('amount')->getDefaultCurrency(); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + + return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency')); } /** * Add money to piggy bank (for mobile devices). * - * @param PiggyBank $piggyBank - * - * @param PiggyBankRepositoryInterface $repository + * @param PiggyBank $piggyBank * * @return View */ - public function addMobile(PiggyBank $piggyBank, PiggyBankRepositoryInterface $repository) + public function addMobile(PiggyBank $piggyBank) { /** @var Carbon $date */ $date = session('end', Carbon::now()->endOfMonth()); - $leftOnAccount = $piggyBank->leftOnAccount($date); - $savedSoFar = $repository->getCurrentAmount($piggyBank); + $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date); + $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $maxAmount = min($leftOnAccount, $leftToSave); - return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount')); + // get currency: + $currency = app('amount')->getDefaultCurrency(); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + + return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency')); } /** @@ -131,16 +158,15 @@ class PiggyBankController extends Controller } /** - * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse */ - public function destroy(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) + public function destroy(PiggyBank $piggyBank) { Session::flash('success', (string)trans('firefly.deleted_piggy_bank', ['name' => $piggyBank->name])); Preferences::mark(); - $repository->destroy($piggyBank); + $this->piggyRepos->destroy($piggyBank); return redirect($this->getPreviousUri('piggy-banks.delete.uri')); } @@ -185,53 +211,52 @@ class PiggyBankController extends Controller /** * @param Request $request - * @param PiggyBankRepositoryInterface $piggyRepository * * @return View */ - public function index(Request $request, PiggyBankRepositoryInterface $piggyRepository) + public function index(Request $request) { - $collection = $piggyRepository->getPiggyBanks(); + $collection = $this->piggyRepos->getPiggyBanks(); $total = $collection->count(); $page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); $pageSize = (int)Preferences::get('listPageSize', 50)->data; + $accounts = []; /** @var Carbon $end */ $end = session('end', Carbon::now()->endOfMonth()); - $accounts = []; - Log::debug('Looping piggues'); - /** @var PiggyBank $piggyBank */ - foreach ($collection as $piggyBank) { + // transform piggies using the transformer: + $parameters = new ParameterBag; + $parameters->set('end', $end); + $transformed = new Collection; + $transformer = new PiggyBankTransformer(new ParameterBag); + $accountTransformer = new AccountTransformer($parameters); + /** @var PiggyBank $piggy */ + foreach ($collection as $piggy) { + $array = $transformer->transform($piggy); + $account = $accountTransformer->transform($piggy->account); + $accountId = $account['id']; + if (!isset($accounts[$accountId])) { + // create new: + $accounts[$accountId] = $account; - $piggyBank->savedSoFar = $piggyRepository->getCurrentAmount($piggyBank); - $piggyBank->percentage = (int)(0 !== bccomp('0', $piggyBank->savedSoFar) ? $piggyBank->savedSoFar / $piggyBank->targetamount * 100 : 0); - $piggyBank->leftToSave = bcsub($piggyBank->targetamount, (string)$piggyBank->savedSoFar); - $piggyBank->percentage = $piggyBank->percentage > 100 ? 100 : $piggyBank->percentage; + // add some interesting details: + $accounts[$accountId]['left'] = $accounts[$accountId]['current_balance']; + $accounts[$accountId]['saved'] = 0; + $accounts[$accountId]['target'] = 0; + $accounts[$accountId]['to_save'] = 0; + } - // Fill account information: - $account = $piggyBank->account; - $new = false; - if (!isset($accounts[$account->id])) { - $new = true; - $accounts[$account->id] = [ - 'name' => $account->name, - 'balance' => Steam::balanceIgnoreVirtual($account, $end), - 'leftForPiggyBanks' => $piggyBank->leftOnAccount($end), - 'sumOfSaved' => (string)$piggyBank->savedSoFar, - 'sumOfTargets' => $piggyBank->targetamount, - 'leftToSave' => $piggyBank->leftToSave, - ]; - } - if (isset($accounts[$account->id]) && false === $new) { - $accounts[$account->id]['sumOfSaved'] = bcadd($accounts[$account->id]['sumOfSaved'], (string)$piggyBank->savedSoFar); - $accounts[$account->id]['sumOfTargets'] = bcadd($accounts[$account->id]['sumOfTargets'], $piggyBank->targetamount); - $accounts[$account->id]['leftToSave'] = bcadd($accounts[$account->id]['leftToSave'], $piggyBank->leftToSave); - } + // calculate new interesting fields: + $accounts[$accountId]['left'] -= $array['current_amount']; + $accounts[$accountId]['saved'] += $array['current_amount']; + $accounts[$accountId]['target'] += $array['target_amount']; + $accounts[$accountId]['to_save'] += ($array['target_amount'] - $array['current_amount']); + $array['account_name'] = $account['name']; + $transformed->push($array); } - // paginate piggy banks - $collection = $collection->slice(($page - 1) * $pageSize, $pageSize); - $piggyBanks = new LengthAwarePaginator($collection, $total, $pageSize, $page); + $transformed = $transformed->slice(($page - 1) * $pageSize, $pageSize); + $piggyBanks = new LengthAwarePaginator($transformed, $total, $pageSize, $page); $piggyBanks->setPath(route('piggy-banks.index')); return view('piggy-banks.index', compact('piggyBanks', 'accounts')); @@ -239,20 +264,19 @@ class PiggyBankController extends Controller /** * @param Request $request - * @param PiggyBankRepositoryInterface $repository * * @return \Illuminate\Http\JsonResponse */ - public function order(Request $request, PiggyBankRepositoryInterface $repository) + public function order(Request $request) { $data = $request->get('order'); // set all users piggy banks to zero: - $repository->reset(); + $this->piggyRepos->reset(); if (is_array($data)) { foreach ($data as $order => $id) { - $repository->setOrder((int)$id, $order + 1); + $this->piggyRepos->setOrder((int)$id, $order + 1); } } @@ -261,17 +285,20 @@ class PiggyBankController extends Controller /** * @param Request $request - * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse */ - public function postAdd(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) + public function postAdd(Request $request, PiggyBank $piggyBank) { $amount = $request->get('amount') ?? '0'; $currency = app('amount')->getDefaultCurrency(); - if ($repository->canAddAmount($piggyBank, $amount)) { - $repository->addAmount($piggyBank, $amount); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + if ($this->piggyRepos->canAddAmount($piggyBank, $amount)) { + $this->piggyRepos->addAmount($piggyBank, $amount); Session::flash( 'success', (string)trans( @@ -298,17 +325,20 @@ class PiggyBankController extends Controller /** * @param Request $request - * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse */ - public function postRemove(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) + public function postRemove(Request $request, PiggyBank $piggyBank) { $amount = $request->get('amount') ?? '0'; $currency = app('amount')->getDefaultCurrency(); - if ($repository->canRemoveAmount($piggyBank, $amount)) { - $repository->removeAmount($piggyBank, $amount); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + if ($this->piggyRepos->canRemoveAmount($piggyBank, $amount)) { + $this->piggyRepos->removeAmount($piggyBank, $amount); Session::flash( 'success', (string)trans( @@ -341,7 +371,15 @@ class PiggyBankController extends Controller */ public function remove(PiggyBank $piggyBank) { - return view('piggy-banks.remove', compact('piggyBank')); + $repetition = $this->piggyRepos->getRepetition($piggyBank); + // get currency: + $currency = app('amount')->getDefaultCurrency(); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + + return view('piggy-banks.remove', compact('piggyBank', 'repetition', 'currency')); } /** @@ -353,19 +391,27 @@ class PiggyBankController extends Controller */ public function removeMobile(PiggyBank $piggyBank) { - return view('piggy-banks.remove-mobile', compact('piggyBank')); + $repetition = $this->piggyRepos->getRepetition($piggyBank); + // get currency: + $currency = app('amount')->getDefaultCurrency(); + $currencyId = (int)$this->accountRepos->getMetaValue($piggyBank->account, 'currency_id'); + if ($currencyId > 0) { + $currency = $this->currencyRepos->findNull($currencyId); + } + + + return view('piggy-banks.remove-mobile', compact('piggyBank', 'repetition', 'currency')); } /** - * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return View */ - public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) + public function show(PiggyBank $piggyBank) { $note = $piggyBank->notes()->first(); - $events = $repository->getEvents($piggyBank); + $events = $this->piggyRepos->getEvents($piggyBank); $subTitle = $piggyBank->name; return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'note')); @@ -373,17 +419,16 @@ class PiggyBankController extends Controller /** * @param PiggyBankFormRequest $request - * @param PiggyBankRepositoryInterface $repository * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function store(PiggyBankFormRequest $request, PiggyBankRepositoryInterface $repository) + public function store(PiggyBankFormRequest $request) { $data = $request->getPiggyBankData(); if (null === $data['startdate']) { $data['startdate'] = new Carbon; } - $piggyBank = $repository->store($data); + $piggyBank = $this->piggyRepos->store($data); Session::flash('success', (string)trans('firefly.stored_piggy_bank', ['name' => $piggyBank->name])); Preferences::mark(); @@ -400,16 +445,15 @@ class PiggyBankController extends Controller } /** - * @param PiggyBankRepositoryInterface $repository * @param PiggyBankFormRequest $request * @param PiggyBank $piggyBank * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function update(PiggyBankRepositoryInterface $repository, PiggyBankFormRequest $request, PiggyBank $piggyBank) + public function update(PiggyBankFormRequest $request, PiggyBank $piggyBank) { $data = $request->getPiggyBankData(); - $piggyBank = $repository->update($piggyBank, $data); + $piggyBank = $this->piggyRepos->update($piggyBank, $data); Session::flash('success', (string)trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name])); Preferences::mark(); diff --git a/app/Http/Requests/PiggyBankFormRequest.php b/app/Http/Requests/PiggyBankFormRequest.php index 76cd62e376..fae23428f7 100644 --- a/app/Http/Requests/PiggyBankFormRequest.php +++ b/app/Http/Requests/PiggyBankFormRequest.php @@ -65,7 +65,6 @@ class PiggyBankFormRequest extends Request 'name' => $nameRule, 'account_id' => 'required|belongsToUser:accounts', 'targetamount' => 'required|numeric|more:0', - 'amount_currency_id_targetamount' => 'required|exists:transaction_currencies,id', 'startdate' => 'date', 'targetdate' => 'date|nullable', 'order' => 'integer|min:1', diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php index aaca349ba8..005dcd7023 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepository.php +++ b/app/Repositories/PiggyBank/PiggyBankRepository.php @@ -197,8 +197,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface */ public function getCurrentAmount(PiggyBank $piggyBank): string { - /** @var PiggyBankRepetition $rep */ - $rep = $piggyBank->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']); + $rep = $this->getRepetition($piggyBank); if (null === $rep) { return '0'; } @@ -301,19 +300,40 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface } /** + * @param PiggyBank $piggyBank + * + * @return PiggyBankRepetition|null + */ + public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition + { + return $piggyBank->piggyBankRepetitions()->first(); + } + + /** + * Get for piggy account what is left to put in piggies. + * * @param PiggyBank $piggyBank * @param Carbon $date * - * @return PiggyBankRepetition + * @return string */ - public function getRepetition(PiggyBank $piggyBank, Carbon $date): PiggyBankRepetition + public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string { - $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($date)->first(); - if (null === $repetition) { - return new PiggyBankRepetition; + + $balance = app('steam')->balanceIgnoreVirtual($piggyBank->account, $date); + + /** @var Collection $piggies */ + $piggies = $piggyBank->account->piggyBanks; + + /** @var PiggyBank $current */ + foreach ($piggies as $current) { + $repetition = $this->getRepetition($current); + if(null !== $repetition) { + $balance = bcsub($balance, $repetition->currentamount); + } } - return $repetition; + return $balance; } /** diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php index 57fe6ce14b..59e217d2f3 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php +++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php @@ -162,12 +162,21 @@ interface PiggyBankRepositoryInterface public function getPiggyBanksWithAmount(): Collection; /** + * @param PiggyBank $piggyBank + * + * @return PiggyBankRepetition|null + */ + public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition; + + /** + * Get for piggy account what is left to put in piggies. + * * @param PiggyBank $piggyBank * @param Carbon $date * - * @return PiggyBankRepetition + * @return string */ - public function getRepetition(PiggyBank $piggyBank, Carbon $date): PiggyBankRepetition; + public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string; /** * @param PiggyBank $piggyBank diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 3ff4d66239..c238a28dd8 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -151,13 +151,15 @@ class AccountTransformer extends TransformerAbstract if ($type !== AccountType::ASSET || (string)$role === '') { $role = null; } - $currencyId = (int)$this->repository->getMetaValue($account, 'currency_id'); - $currencyCode = null; - $decimalPlaces = 2; + $currencyId = (int)$this->repository->getMetaValue($account, 'currency_id'); + $currencyCode = null; + $currencySymbol = null; + $decimalPlaces = 2; if ($currencyId > 0) { - $currency = TransactionCurrency::find($currencyId); - $currencyCode = $currency->code; - $decimalPlaces = $currency->decimal_places; + $currency = TransactionCurrency::find($currencyId); + $currencyCode = $currency->code; + $decimalPlaces = $currency->decimal_places; + $currencySymbol = $currency->symbol; } $date = new Carbon; @@ -196,6 +198,8 @@ class AccountTransformer extends TransformerAbstract 'type' => $type, 'currency_id' => $currencyId, 'currency_code' => $currencyCode, + 'currency_symbol' => $currencySymbol, + 'currency_dp' => $decimalPlaces, 'current_balance' => round(app('steam')->balance($account, $date), $decimalPlaces), 'current_balance_date' => $date->format('Y-m-d'), 'notes' => $this->repository->getNoteText($account), diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php index 02a4831c8d..4c7172476a 100644 --- a/app/Transformers/PiggyBankTransformer.php +++ b/app/Transformers/PiggyBankTransformer.php @@ -24,8 +24,10 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Models\Account; use FireflyIII\Models\Note; use FireflyIII\Models\PiggyBank; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use League\Fractal\Resource\Collection as FractalCollection; @@ -116,39 +118,60 @@ class PiggyBankTransformer extends TransformerAbstract */ public function transform(PiggyBank $piggyBank): array { - $account = $piggyBank->account; - $currencyId = (int)$account->getMeta('currency_id'); - $decimalPlaces = 2; + /** @var Account $account */ + $account = $piggyBank->account; + /** @var AccountRepositoryInterface $accountRepos */ + $accountRepos = app(AccountRepositoryInterface::class); + $accountRepos->setUser($account->user); + $currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id'); + + if ($currencyId === 0) { + $currency = app('amount')->getDefaultCurrencyByUser($account->user); + } + if ($currencyId > 0) { /** @var CurrencyRepositoryInterface $repository */ $repository = app(CurrencyRepositoryInterface::class); $repository->setUser($account->user); - $currency = $repository->findNull($currencyId); - $decimalPlaces = $currency->decimal_places; + $currency = $repository->findNull($currencyId); } + $decimalPlaces = $currency->decimal_places; + // get currently saved amount: /** @var PiggyBankRepositoryInterface $piggyRepos */ $piggyRepos = app(PiggyBankRepositoryInterface::class); $piggyRepos->setUser($account->user); - $currentAmount = round($piggyRepos->getCurrentAmount($piggyBank), $decimalPlaces); + // current amount in piggy bank: + $currentAmountStr = $piggyRepos->getCurrentAmount($piggyBank); + $currentAmount = round($currentAmountStr, $decimalPlaces); + + // left to save to target: + $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr); $startDate = null === $piggyBank->startdate ? null : $piggyBank->startdate->format('Y-m-d'); $targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d'); $targetAmount = round($piggyBank->targetamount, $decimalPlaces); + $percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmount / $targetAmount * 100 : 0); $data = [ - 'id' => (int)$piggyBank->id, - 'updated_at' => $piggyBank->updated_at->toAtomString(), - 'created_at' => $piggyBank->created_at->toAtomString(), - 'name' => $piggyBank->name, - 'target_amount' => $targetAmount, - 'current_amount' => $currentAmount, - 'startdate' => $startDate, - 'targetdate' => $targetDate, - 'order' => (int)$piggyBank->order, - 'active' => (int)$piggyBank->active === 1, - 'notes' => null, - 'links' => [ + 'id' => (int)$piggyBank->id, + 'updated_at' => $piggyBank->updated_at->toAtomString(), + 'created_at' => $piggyBank->created_at->toAtomString(), + 'name' => $piggyBank->name, + 'currency_id' => $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_dp' => $currency->decimal_places, + 'target_amount' => $targetAmount, + 'percentage' => $percentage, + 'current_amount' => $currentAmount, + 'left_to_save' => round($leftToSave, $decimalPlaces), + 'startdate' => $startDate, + 'targetdate' => $targetDate, + 'order' => (int)$piggyBank->order, + 'active' => (int)$piggyBank->active === 1, + 'notes' => null, + 'links' => [ [ 'rel' => 'self', 'uri' => '/piggy_banks/' . $piggyBank->id, diff --git a/resources/views/list/piggy-banks.twig b/resources/views/list/piggy-banks.twig index 241a532f1f..24387cf7b8 100644 --- a/resources/views/list/piggy-banks.twig +++ b/resources/views/list/piggy-banks.twig @@ -13,15 +13,15 @@ - {% for piggyBank in piggyBanks %} - + {% for piggy in piggyBanks %} +  
- - + +
@@ -30,53 +30,53 @@
- - + +
- {{ piggyBank.name }} + {{ piggy.name }} - {{ piggyBank.savedSoFar|formatAmount }} + {{ formatAmountBySymbol(piggy.current_amount,piggy.currency_symbol,piggy.currency_dp) }} - {% if piggyBank.savedSoFar > 0 %} - - + {% if piggy.current_amount > 0 %} + + {% endif %}
- {{ piggyBank.percentage }}% + role="progressbar" aria-valuenow="{{ piggy.percentage }}" aria-valuemin="0" aria-valuemax="100" + style="min-width: 30px;width: {{ piggy.percentage }}%;"> + {{ piggy.percentage }}%
- {% if piggyBank.leftToSave > 0 %} - - + {% if piggy.left_to_save > 0 %} + + {% endif %} - - {{ piggyBank.targetamount|formatAmount }} + + {{ formatAmountBySymbol(piggy.target_amount,piggy.currency_symbol,piggy.currency_dp) }} - - {% if piggyBank.leftToSave > 0 %} - {{ piggyBank.leftToSave|formatAmount }} + + {% if piggy.left_to_save > 0 %} + {{ formatAmountBySymbol(piggy.left_to_save,piggy.currency_symbol,piggy.currency_dp) }} {% endif %} diff --git a/resources/views/piggy-banks/add-mobile.twig b/resources/views/piggy-banks/add-mobile.twig index 05c4b9667b..586e932204 100644 --- a/resources/views/piggy-banks/add-mobile.twig +++ b/resources/views/piggy-banks/add-mobile.twig @@ -16,11 +16,11 @@
{% if maxAmount > 0 %}

- {{ 'max_amount_add'|_ }}: {{ maxAmount|formatAmount }}. + {{ 'max_amount_add'|_ }}: {{ formatAmountByCurrency(currency,maxAmount) }}.

-
{{ getCurrencySymbol()|raw }}
+
{{ currency.symbol|raw }}
diff --git a/resources/views/piggy-banks/add.twig b/resources/views/piggy-banks/add.twig index 78af5e46cc..9c4da8d832 100644 --- a/resources/views/piggy-banks/add.twig +++ b/resources/views/piggy-banks/add.twig @@ -11,12 +11,12 @@

- {{ 'max_amount_add'|_ }}: {{ maxAmount|formatAmount }}. + {{ 'max_amount_add'|_ }}: {{ formatAmountByCurrency(currency,maxAmount) }}.

-
{{ getCurrencySymbol()|raw }}
- +
{{ currency.symbol|raw }}
+
diff --git a/resources/views/piggy-banks/edit.twig b/resources/views/piggy-banks/edit.twig index 427a9670a5..7b99a5be8d 100644 --- a/resources/views/piggy-banks/edit.twig +++ b/resources/views/piggy-banks/edit.twig @@ -20,7 +20,7 @@ {{ ExpandedForm.text('name') }} {{ ExpandedForm.assetAccountList('account_id', null, {label: 'saveOnAccount'|_ }) }} - {{ ExpandedForm.amount('targetamount') }} + {{ ExpandedForm.amountNoCurrency('targetamount') }} diff --git a/resources/views/piggy-banks/index.twig b/resources/views/piggy-banks/index.twig index 145a4a4a71..9f735be2ab 100644 --- a/resources/views/piggy-banks/index.twig +++ b/resources/views/piggy-banks/index.twig @@ -5,7 +5,7 @@ {% endblock %} {% block content %} - {% if piggyBanks.count == 0 %} + {% if piggyBanks|length == 0 %} {% include 'partials.empty' with {what: 'default', type: 'piggies',route: route('piggy-banks.create')} %} {% else %}
@@ -49,11 +49,21 @@ {% for id,info in accounts %} {{ info.name }} - {{ info.balance|formatAmount }} - {{ info.leftForPiggyBanks|formatAmount }} - {{ info.sumOfTargets|formatAmount }} - {{ info.sumOfSaved|formatAmount }} - {{ info.leftToSave|formatAmount }} + + {{ formatAmountBySymbol(info.current_balance,info.currency_symbol,info.currency_dp) }} + + + {{ formatAmountBySymbol(info.left,info.currency_symbol,info.currency_dp) }} + + + {{ formatAmountBySymbol(info.target,info.currency_symbol,info.currency_dp) }} + + + {{ formatAmountBySymbol(info.saved,info.currency_symbol,info.currency_dp) }} + + + {{ formatAmountBySymbol(info.to_save,info.currency_symbol,info.currency_dp) }} + {% endfor %} diff --git a/resources/views/piggy-banks/remove-mobile.twig b/resources/views/piggy-banks/remove-mobile.twig index 18ae39a048..f5fbe224e2 100644 --- a/resources/views/piggy-banks/remove-mobile.twig +++ b/resources/views/piggy-banks/remove-mobile.twig @@ -15,12 +15,12 @@

- {{ 'max_amount_remove'|_ }}: {{ currentRelevantRepAmount(piggyBank)|formatAmount }}. + {{ 'max_amount_remove'|_ }}: {{ formatAmountByCurrency(currency, repetition.currentamount) }}.

-
{{ getCurrencySymbol()|raw }}
- {{ currency.symbol|raw }}
+

diff --git a/resources/views/piggy-banks/remove.twig b/resources/views/piggy-banks/remove.twig index 190616cc24..7527b7d798 100644 --- a/resources/views/piggy-banks/remove.twig +++ b/resources/views/piggy-banks/remove.twig @@ -11,12 +11,12 @@