diff --git a/app/Http/Controllers/PiggyBank/AmountController.php b/app/Http/Controllers/PiggyBank/AmountController.php new file mode 100644 index 0000000000..eb82627d01 --- /dev/null +++ b/app/Http/Controllers/PiggyBank/AmountController.php @@ -0,0 +1,201 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + $this->accountRepos = app(AccountRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Add money to piggy bank. + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function add(PiggyBank $piggyBank) + { + $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, new Carbon); + $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); + $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); + $maxAmount = min($leftOnAccount, $leftToSave); + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + + return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency')); + } + + /** + * Add money to piggy bank (for mobile devices). + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function addMobile(PiggyBank $piggyBank) + { + /** @var Carbon $date */ + $date = session('end', new Carbon); + $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date); + $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); + $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); + $maxAmount = min($leftOnAccount, $leftToSave); + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + + return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency')); + } + + /** + * Add money to piggy bank. + * + * @param Request $request + * @param PiggyBank $piggyBank + * + * @return RedirectResponse + */ + public function postAdd(Request $request, PiggyBank $piggyBank): RedirectResponse + { + $amount = $request->get('amount') ?? '0'; + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + // if amount is negative, make positive and continue: + if (-1 === bccomp($amount, '0')) { + $amount = bcmul($amount, '-1'); + } + if ($this->piggyRepos->canAddAmount($piggyBank, $amount)) { + $this->piggyRepos->addAmount($piggyBank, $amount); + session()->flash( + 'success', + (string) trans( + 'firefly.added_amount_to_piggy', + ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name] + ) + ); + app('preferences')->mark(); + + return redirect(route('piggy-banks.index')); + } + + Log::error('Cannot add ' . $amount . ' because canAddAmount returned false.'); + session()->flash( + 'error', + (string) trans( + 'firefly.cannot_add_amount_piggy', + ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)] + ) + ); + + return redirect(route('piggy-banks.index')); + } + + /** + * Remove money from piggy bank. + * + * @param Request $request + * @param PiggyBank $piggyBank + * + * @return RedirectResponse + */ + public function postRemove(Request $request, PiggyBank $piggyBank): RedirectResponse + { + $amount = $request->get('amount') ?? '0'; + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + // if amount is negative, make positive and continue: + if (-1 === bccomp($amount, '0')) { + $amount = bcmul($amount, '-1'); + } + if ($this->piggyRepos->canRemoveAmount($piggyBank, $amount)) { + $this->piggyRepos->removeAmount($piggyBank, $amount); + session()->flash( + 'success', + (string) trans( + 'firefly.removed_amount_from_piggy', + ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name] + ) + ); + app('preferences')->mark(); + + return redirect(route('piggy-banks.index')); + } + + $amount = (string) round($request->get('amount'), 12); + + session()->flash( + 'error', + (string) trans( + 'firefly.cannot_remove_from_piggy', + ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)] + ) + ); + + return redirect(route('piggy-banks.index')); + } + + /** + * Remove money from piggy bank form. + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function remove(PiggyBank $piggyBank) + { + $repetition = $this->piggyRepos->getRepetition($piggyBank); + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + + return view('piggy-banks.remove', compact('piggyBank', 'repetition', 'currency')); + } + + /** + * Remove money from piggy bank (for mobile devices). + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function removeMobile(PiggyBank $piggyBank) + { + $repetition = $this->piggyRepos->getRepetition($piggyBank); + $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); + + return view('piggy-banks.remove-mobile', compact('piggyBank', 'repetition', 'currency')); + } +} diff --git a/app/Http/Controllers/PiggyBank/CreateController.php b/app/Http/Controllers/PiggyBank/CreateController.php new file mode 100644 index 0000000000..de7c18ff72 --- /dev/null +++ b/app/Http/Controllers/PiggyBank/CreateController.php @@ -0,0 +1,112 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->attachments = app(AttachmentHelperInterface::class); + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Create a piggy bank. + * + * @return Factory|View + */ + public function create() + { + $subTitle = (string) trans('firefly.new_piggy_bank'); + $subTitleIcon = 'fa-plus'; + + // put previous url in session if not redirect from store (not "create another"). + if (true !== session('piggy-banks.create.fromStore')) { + $this->rememberPreviousUri('piggy-banks.create.uri'); + } + session()->forget('piggy-banks.create.fromStore'); + + return view('piggy-banks.create', compact('subTitle', 'subTitleIcon')); + } + + + /** + * Store a new piggy bank. + * + * @param PiggyBankFormRequest $request + * + * @return RedirectResponse|Redirector + */ + public function store(PiggyBankFormRequest $request) + { + $data = $request->getPiggyBankData(); + if (null === $data['startdate']) { + $data['startdate'] = new Carbon; + } + $piggyBank = $this->piggyRepos->store($data); + + session()->flash('success', (string) trans('firefly.stored_piggy_bank', ['name' => $piggyBank->name])); + app('preferences')->mark(); + + // store attachment(s): + /** @var array $files */ + $files = $request->hasFile('attachments') ? $request->file('attachments') : null; + if (null !== $files && !auth()->user()->hasRole('demo')) { + $this->attachments->saveAttachmentsForModel($piggyBank, $files); + } + if (null !== $files && auth()->user()->hasRole('demo')) { + session()->flash('info', (string) trans('firefly.no_att_demo_user')); + } + + if (count($this->attachments->getMessages()->get('attachments')) > 0) { + $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore + } + + + $redirect = redirect($this->getPreviousUri('piggy-banks.create.uri')); + + if (1 === (int) $request->get('create_another')) { + // @codeCoverageIgnoreStart + session()->put('piggy-banks.create.fromStore', true); + + $redirect = redirect(route('piggy-banks.create'))->withInput(); + // @codeCoverageIgnoreEnd + } + + return $redirect; + } +} diff --git a/app/Http/Controllers/PiggyBank/DeleteController.php b/app/Http/Controllers/PiggyBank/DeleteController.php new file mode 100644 index 0000000000..3db81000b8 --- /dev/null +++ b/app/Http/Controllers/PiggyBank/DeleteController.php @@ -0,0 +1,75 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Delete a piggy bank. + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function delete(PiggyBank $piggyBank) + { + $subTitle = (string) trans('firefly.delete_piggy_bank', ['name' => $piggyBank->name]); + + // put previous url in session + $this->rememberPreviousUri('piggy-banks.delete.uri'); + + return view('piggy-banks.delete', compact('piggyBank', 'subTitle')); + } + + /** + * Destroy the piggy bank. + * + * @param PiggyBank $piggyBank + * + * @return RedirectResponse + */ + public function destroy(PiggyBank $piggyBank): RedirectResponse + { + session()->flash('success', (string) trans('firefly.deleted_piggy_bank', ['name' => $piggyBank->name])); + app('preferences')->mark(); + $this->piggyRepos->destroy($piggyBank); + + return redirect($this->getPreviousUri('piggy-banks.delete.uri')); + } +} diff --git a/app/Http/Controllers/PiggyBank/EditController.php b/app/Http/Controllers/PiggyBank/EditController.php new file mode 100644 index 0000000000..54b90ed02e --- /dev/null +++ b/app/Http/Controllers/PiggyBank/EditController.php @@ -0,0 +1,132 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->attachments = app(AttachmentHelperInterface::class); + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Edit a piggy bank. + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function edit(PiggyBank $piggyBank) + { + $subTitle = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); + $subTitleIcon = 'fa-pencil'; + $targetDate = null; + $startDate = null; + $note = $piggyBank->notes()->first(); + // Flash some data to fill the form. + if (null !== $piggyBank->targetdate) { + $targetDate = $piggyBank->targetdate->format('Y-m-d'); + } + if (null !== $piggyBank->startdate) { + $startDate = $piggyBank->startdate->format('Y-m-d'); + } + + $preFilled = ['name' => $piggyBank->name, + 'account_id' => $piggyBank->account_id, + 'targetamount' => $piggyBank->targetamount, + 'targetdate' => $targetDate, + 'startdate' => $startDate, + 'notes' => null === $note ? '' : $note->text, + ]; + session()->flash('preFilled', $preFilled); + + // put previous url in session if not redirect from store (not "return_to_edit"). + if (true !== session('piggy-banks.edit.fromUpdate')) { + $this->rememberPreviousUri('piggy-banks.edit.uri'); + } + session()->forget('piggy-banks.edit.fromUpdate'); + + return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'preFilled')); + } + + + /** + * Update a piggy bank. + * + * @param PiggyBankFormRequest $request + * @param PiggyBank $piggyBank + * + * @return RedirectResponse|Redirector + */ + public function update(PiggyBankFormRequest $request, PiggyBank $piggyBank) + { + $data = $request->getPiggyBankData(); + $piggyBank = $this->piggyRepos->update($piggyBank, $data); + + session()->flash('success', (string) trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name])); + app('preferences')->mark(); + + // store new attachment(s): + /** @var array $files */ + $files = $request->hasFile('attachments') ? $request->file('attachments') : null; + if (null !== $files && !auth()->user()->hasRole('demo')) { + $this->attachments->saveAttachmentsForModel($piggyBank, $files); + } + if (null !== $files && auth()->user()->hasRole('demo')) { + session()->flash('info', (string) trans('firefly.no_att_demo_user')); + } + + if (count($this->attachments->getMessages()->get('attachments')) > 0) { + $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore + } + + + $redirect = redirect($this->getPreviousUri('piggy-banks.edit.uri')); + + if (1 === (int) $request->get('return_to_edit')) { + // @codeCoverageIgnoreStart + session()->put('piggy-banks.edit.fromUpdate', true); + + $redirect = redirect(route('piggy-banks.edit', [$piggyBank->id])); + // @codeCoverageIgnoreEnd + } + + return $redirect; + } + +} diff --git a/app/Http/Controllers/PiggyBank/IndexController.php b/app/Http/Controllers/PiggyBank/IndexController.php new file mode 100644 index 0000000000..d612d76e29 --- /dev/null +++ b/app/Http/Controllers/PiggyBank/IndexController.php @@ -0,0 +1,134 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Show overview of all piggy banks. + * + * @param Request $request + * + * @return Factory|View + */ + public function index(Request $request) + { + $this->piggyRepos->correctOrder(); + $collection = $this->piggyRepos->getPiggyBanks(); + $accounts = []; + /** @var Carbon $end */ + $end = session('end', Carbon::now()->endOfMonth()); + + // transform piggies using the transformer: + $parameters = new ParameterBag; + $parameters->set('end', $end); + + // make piggy bank groups: + $piggyBanks = [ + 0 => [ // the index is the order, not the ID. + 'object_group_id' => 0, + 'object_group_title' => (string) trans('firefly.default_group_title_name'), + 'piggy_banks' => [], + ], + ]; + + /** @var PiggyBankTransformer $transformer */ + $transformer = app(PiggyBankTransformer::class); + $transformer->setParameters(new ParameterBag); + + /** @var AccountTransformer $accountTransformer */ + $accountTransformer = app(AccountTransformer::class); + $accountTransformer->setParameters($parameters); + /** @var PiggyBank $piggy */ + foreach ($collection as $piggy) { + $array = $transformer->transform($piggy); + $groupOrder = $array['object_group_order']; + // make group array if necessary: + $piggyBanks[$groupOrder] = $piggyBanks[$groupOrder] ?? [ + 'object_group_id' => $array['object_group_id'], + 'object_group_title' => $array['object_group_title'], + 'piggy_banks' => [], + ]; + + $account = $accountTransformer->transform($piggy->account); + $accountId = (int) $account['id']; + $array['attachments'] = $this->piggyRepos->getAttachments($piggy); + if (!isset($accounts[$accountId])) { + // create new: + $accounts[$accountId] = $account; + + // add some interesting details: + $accounts[$accountId]['left'] = $accounts[$accountId]['current_balance']; + $accounts[$accountId]['saved'] = 0; + $accounts[$accountId]['target'] = 0; + $accounts[$accountId]['to_save'] = 0; + } + + // 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']; + $piggyBanks[$groupOrder]['piggy_banks'][] = $array; + } + + return view('piggy-banks.index', compact('piggyBanks', 'accounts')); + } + + /** + * Set the order of a piggy bank. + * + * @param Request $request + * @param PiggyBank $piggyBank + * + * @return JsonResponse + */ + public function setOrder(Request $request, PiggyBank $piggyBank): JsonResponse + { + $newOrder = (int) $request->get('order'); + $this->piggyRepos->setOrder($piggyBank, $newOrder); + + return response()->json(['data' => 'OK']); + } +} diff --git a/app/Http/Controllers/PiggyBank/ShowController.php b/app/Http/Controllers/PiggyBank/ShowController.php new file mode 100644 index 0000000000..43dec1eea4 --- /dev/null +++ b/app/Http/Controllers/PiggyBank/ShowController.php @@ -0,0 +1,69 @@ +middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.piggyBanks')); + app('view')->share('mainTitleIcon', 'fa-bullseye'); + + $this->piggyRepos = app(PiggyBankRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** + * Show a single piggy bank. + * + * @param PiggyBank $piggyBank + * + * @return Factory|View + */ + public function show(PiggyBank $piggyBank) + { + /** @var Carbon $end */ + $end = session('end', Carbon::now()->endOfMonth()); + // transform piggies using the transformer: + $parameters = new ParameterBag; + $parameters->set('end', $end); + + /** @var PiggyBankTransformer $transformer */ + $transformer = app(PiggyBankTransformer::class); + $transformer->setParameters($parameters); + $piggy = $transformer->transform($piggyBank); + $events = $this->piggyRepos->getEvents($piggyBank); + $subTitle = $piggyBank->name; + $attachments = $this->piggyRepos->getAttachments($piggyBank); + + return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'piggy', 'attachments')); + } +} diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php deleted file mode 100644 index d3a6853761..0000000000 --- a/app/Http/Controllers/PiggyBankController.php +++ /dev/null @@ -1,525 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Http\Controllers; - -use Carbon\Carbon; -use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; -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\Contracts\View\Factory; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; -use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Routing\Redirector; -use Illuminate\Support\Collection; -use Illuminate\View\View; -use Log; -use Symfony\Component\HttpFoundation\ParameterBag; - -/** - * Class PiggyBankController. - * - */ -class PiggyBankController extends Controller -{ - - /** @var AccountRepositoryInterface The account repository */ - private $accountRepos; - /** @var CurrencyRepositoryInterface The currency repository */ - private $currencyRepos; - /** @var PiggyBankRepositoryInterface Piggy bank repository. */ - private $piggyRepos; - - /** @var AttachmentHelperInterface Helper for attachments. */ - private $attachments; - - /** - * PiggyBankController constructor. - * - * @codeCoverageIgnore - */ - public function __construct() - { - parent::__construct(); - - $this->middleware( - function ($request, $next) { - app('view')->share('title', (string) trans('firefly.piggyBanks')); - app('view')->share('mainTitleIcon', 'fa-bullseye'); - - $this->attachments = app(AttachmentHelperInterface::class); - $this->piggyRepos = app(PiggyBankRepositoryInterface::class); - $this->currencyRepos = app(CurrencyRepositoryInterface::class); - $this->accountRepos = app(AccountRepositoryInterface::class); - - return $next($request); - } - ); - } - - /** - * Add money to piggy bank. - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function add(PiggyBank $piggyBank) - { - $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, new Carbon); - $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); - $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); - $maxAmount = min($leftOnAccount, $leftToSave); - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - - return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency')); - } - - /** - * Add money to piggy bank (for mobile devices). - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function addMobile(PiggyBank $piggyBank) - { - /** @var Carbon $date */ - $date = session('end', new Carbon); - $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date); - $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); - $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); - $maxAmount = min($leftOnAccount, $leftToSave); - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - - return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency')); - } - - /** - * Create a piggy bank. - * - * @return Factory|View - */ - public function create() - { - $subTitle = (string) trans('firefly.new_piggy_bank'); - $subTitleIcon = 'fa-plus'; - - // put previous url in session if not redirect from store (not "create another"). - if (true !== session('piggy-banks.create.fromStore')) { - $this->rememberPreviousUri('piggy-banks.create.uri'); - } - session()->forget('piggy-banks.create.fromStore'); - - return view('piggy-banks.create', compact('subTitle', 'subTitleIcon')); - } - - /** - * Delete a piggy bank. - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function delete(PiggyBank $piggyBank) - { - $subTitle = (string) trans('firefly.delete_piggy_bank', ['name' => $piggyBank->name]); - - // put previous url in session - $this->rememberPreviousUri('piggy-banks.delete.uri'); - - return view('piggy-banks.delete', compact('piggyBank', 'subTitle')); - } - - /** - * Destroy the piggy bank. - * - * @param PiggyBank $piggyBank - * - * @return RedirectResponse - */ - public function destroy(PiggyBank $piggyBank): RedirectResponse - { - session()->flash('success', (string) trans('firefly.deleted_piggy_bank', ['name' => $piggyBank->name])); - app('preferences')->mark(); - $this->piggyRepos->destroy($piggyBank); - - return redirect($this->getPreviousUri('piggy-banks.delete.uri')); - } - - /** - * Edit a piggy bank. - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function edit(PiggyBank $piggyBank) - { - $subTitle = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); - $subTitleIcon = 'fa-pencil'; - $targetDate = null; - $startDate = null; - $note = $piggyBank->notes()->first(); - // Flash some data to fill the form. - if (null !== $piggyBank->targetdate) { - $targetDate = $piggyBank->targetdate->format('Y-m-d'); - } - if (null !== $piggyBank->startdate) { - $startDate = $piggyBank->startdate->format('Y-m-d'); - } - - $preFilled = ['name' => $piggyBank->name, - 'account_id' => $piggyBank->account_id, - 'targetamount' => $piggyBank->targetamount, - 'targetdate' => $targetDate, - 'startdate' => $startDate, - 'notes' => null === $note ? '' : $note->text, - ]; - session()->flash('preFilled', $preFilled); - - // put previous url in session if not redirect from store (not "return_to_edit"). - if (true !== session('piggy-banks.edit.fromUpdate')) { - $this->rememberPreviousUri('piggy-banks.edit.uri'); - } - session()->forget('piggy-banks.edit.fromUpdate'); - - return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'preFilled')); - } - - /** - * Show overview of all piggy banks. - * - * @param Request $request - * - * @return Factory|View - */ - public function index(Request $request) - { - $this->piggyRepos->correctOrder(); - $collection = $this->piggyRepos->getPiggyBanks(); - $total = $collection->count(); - $page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); - $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; - $accounts = []; - /** @var Carbon $end */ - $end = session('end', Carbon::now()->endOfMonth()); - - // transform piggies using the transformer: - $parameters = new ParameterBag; - $parameters->set('end', $end); - $transformed = new Collection; - - /** @var PiggyBankTransformer $transformer */ - $transformer = app(PiggyBankTransformer::class); - $transformer->setParameters(new ParameterBag); - - /** @var AccountTransformer $accountTransformer */ - $accountTransformer = app(AccountTransformer::class); - $accountTransformer->setParameters($parameters); - /** @var PiggyBank $piggy */ - foreach ($collection as $piggy) { - $array = $transformer->transform($piggy); - $account = $accountTransformer->transform($piggy->account); - $accountId = (int) $account['id']; - $array['attachments'] = $this->piggyRepos->getAttachments($piggy); - if (!isset($accounts[$accountId])) { - // create new: - $accounts[$accountId] = $account; - - // add some interesting details: - $accounts[$accountId]['left'] = $accounts[$accountId]['current_balance']; - $accounts[$accountId]['saved'] = 0; - $accounts[$accountId]['target'] = 0; - $accounts[$accountId]['to_save'] = 0; - } - - // 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); - } - - $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')); - } - - /** - * Add money to piggy bank. - * - * @param Request $request - * @param PiggyBank $piggyBank - * - * @return RedirectResponse - */ - public function postAdd(Request $request, PiggyBank $piggyBank): RedirectResponse - { - $amount = $request->get('amount') ?? '0'; - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - // if amount is negative, make positive and continue: - if (-1 === bccomp($amount, '0')) { - $amount = bcmul($amount, '-1'); - } - if ($this->piggyRepos->canAddAmount($piggyBank, $amount)) { - $this->piggyRepos->addAmount($piggyBank, $amount); - session()->flash( - 'success', - (string) trans( - 'firefly.added_amount_to_piggy', - ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name] - ) - ); - app('preferences')->mark(); - - return redirect(route('piggy-banks.index')); - } - - Log::error('Cannot add ' . $amount . ' because canAddAmount returned false.'); - session()->flash( - 'error', - (string) trans( - 'firefly.cannot_add_amount_piggy', - ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)] - ) - ); - - return redirect(route('piggy-banks.index')); - } - - /** - * Remove money from piggy bank. - * - * @param Request $request - * @param PiggyBank $piggyBank - * - * @return RedirectResponse - */ - public function postRemove(Request $request, PiggyBank $piggyBank): RedirectResponse - { - $amount = $request->get('amount') ?? '0'; - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - // if amount is negative, make positive and continue: - if (-1 === bccomp($amount, '0')) { - $amount = bcmul($amount, '-1'); - } - if ($this->piggyRepos->canRemoveAmount($piggyBank, $amount)) { - $this->piggyRepos->removeAmount($piggyBank, $amount); - session()->flash( - 'success', - (string) trans( - 'firefly.removed_amount_from_piggy', - ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name] - ) - ); - app('preferences')->mark(); - - return redirect(route('piggy-banks.index')); - } - - $amount = (string) round($request->get('amount'), 12); - - session()->flash( - 'error', - (string) trans( - 'firefly.cannot_remove_from_piggy', - ['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)] - ) - ); - - return redirect(route('piggy-banks.index')); - } - - /** - * Remove money from piggy bank form. - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function remove(PiggyBank $piggyBank) - { - $repetition = $this->piggyRepos->getRepetition($piggyBank); - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - - return view('piggy-banks.remove', compact('piggyBank', 'repetition', 'currency')); - } - - /** - * Remove money from piggy bank (for mobile devices). - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function removeMobile(PiggyBank $piggyBank) - { - $repetition = $this->piggyRepos->getRepetition($piggyBank); - $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); - - return view('piggy-banks.remove-mobile', compact('piggyBank', 'repetition', 'currency')); - } - - /** - * Set the order of a piggy bank. - * - * @param Request $request - * @param PiggyBank $piggyBank - * - * @return JsonResponse - */ - public function setOrder(Request $request, PiggyBank $piggyBank): JsonResponse - { - $newOrder = (int) $request->get('order'); - $this->piggyRepos->setOrder($piggyBank, $newOrder); - - return response()->json(['data' => 'OK']); - } - - /** - * Show a single piggy bank. - * - * @param PiggyBank $piggyBank - * - * @return Factory|View - */ - public function show(PiggyBank $piggyBank) - { - /** @var Carbon $end */ - $end = session('end', Carbon::now()->endOfMonth()); - // transform piggies using the transformer: - $parameters = new ParameterBag; - $parameters->set('end', $end); - - /** @var PiggyBankTransformer $transformer */ - $transformer = app(PiggyBankTransformer::class); - $transformer->setParameters($parameters); - $piggy = $transformer->transform($piggyBank); - $events = $this->piggyRepos->getEvents($piggyBank); - $subTitle = $piggyBank->name; - $attachments = $this->piggyRepos->getAttachments($piggyBank); - - return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'piggy', 'attachments')); - } - - /** - * Store a new piggy bank. - * - * @param PiggyBankFormRequest $request - * - * @return RedirectResponse|Redirector - */ - public function store(PiggyBankFormRequest $request) - { - $data = $request->getPiggyBankData(); - if (null === $data['startdate']) { - $data['startdate'] = new Carbon; - } - $piggyBank = $this->piggyRepos->store($data); - - session()->flash('success', (string) trans('firefly.stored_piggy_bank', ['name' => $piggyBank->name])); - app('preferences')->mark(); - - // store attachment(s): - /** @var array $files */ - $files = $request->hasFile('attachments') ? $request->file('attachments') : null; - if (null !== $files && !auth()->user()->hasRole('demo')) { - $this->attachments->saveAttachmentsForModel($piggyBank, $files); - } - if (null !== $files && auth()->user()->hasRole('demo')) { - session()->flash('info',(string)trans('firefly.no_att_demo_user')); - } - - if (count($this->attachments->getMessages()->get('attachments')) > 0) { - $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore - } - - - $redirect = redirect($this->getPreviousUri('piggy-banks.create.uri')); - - if (1 === (int) $request->get('create_another')) { - // @codeCoverageIgnoreStart - session()->put('piggy-banks.create.fromStore', true); - - $redirect = redirect(route('piggy-banks.create'))->withInput(); - // @codeCoverageIgnoreEnd - } - - return $redirect; - } - - /** - * Update a piggy bank. - * - * @param PiggyBankFormRequest $request - * @param PiggyBank $piggyBank - * - * @return RedirectResponse|Redirector - */ - public function update(PiggyBankFormRequest $request, PiggyBank $piggyBank) - { - $data = $request->getPiggyBankData(); - $piggyBank = $this->piggyRepos->update($piggyBank, $data); - - session()->flash('success', (string) trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name])); - app('preferences')->mark(); - - // store new attachment(s): - /** @var array $files */ - $files = $request->hasFile('attachments') ? $request->file('attachments') : null; - if (null !== $files && !auth()->user()->hasRole('demo')) { - $this->attachments->saveAttachmentsForModel($piggyBank, $files); - } - if (null !== $files && auth()->user()->hasRole('demo')) { - session()->flash('info',(string)trans('firefly.no_att_demo_user')); - } - - if (count($this->attachments->getMessages()->get('attachments')) > 0) { - $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore - } - - - $redirect = redirect($this->getPreviousUri('piggy-banks.edit.uri')); - - if (1 === (int) $request->get('return_to_edit')) { - // @codeCoverageIgnoreStart - session()->put('piggy-banks.edit.fromUpdate', true); - - $redirect = redirect(route('piggy-banks.edit', [$piggyBank->id])); - // @codeCoverageIgnoreEnd - } - - return $redirect; - } -}