firefly-iii/app/Http/Controllers/PiggyBankController.php

487 lines
17 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* PiggyBankController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2017-04-08 12:05:37 -05:00
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-23 14:55:52 -06:00
2015-02-25 12:32:33 -06:00
use Carbon\Carbon;
use FireflyIII\Http\Requests\PiggyBankFormRequest;
use FireflyIII\Models\PiggyBank;
2018-04-21 16:48:54 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2015-02-25 12:32:33 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2018-04-21 16:48:54 -05:00
use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\PiggyBankTransformer;
2018-04-28 14:54:48 -05:00
use Illuminate\Http\JsonResponse;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\RedirectResponse;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
2017-12-28 02:53:21 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
2018-04-21 16:48:54 -05:00
use Illuminate\Support\Collection;
2016-01-24 13:38:58 -06:00
use Log;
2018-04-21 16:48:54 -05:00
use Symfony\Component\HttpFoundation\ParameterBag;
2015-02-23 14:55:52 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class PiggyBankController.
2018-07-20 07:34:56 -05:00
*
2015-02-23 14:55:52 -06:00
*/
2015-02-25 12:32:33 -06:00
class PiggyBankController extends Controller
{
2018-04-21 16:48:54 -05:00
/** @var AccountRepositoryInterface The account repository */
2018-04-21 16:48:54 -05:00
private $accountRepos;
/** @var CurrencyRepositoryInterface The currency repository */
2018-04-21 16:48:54 -05:00
private $currencyRepos;
2018-07-22 01:10:16 -05:00
/** @var PiggyBankRepositoryInterface Piggy bank repository. */
2018-04-21 16:48:54 -05:00
private $piggyRepos;
2015-02-24 14:10:25 -06:00
/**
2018-07-22 01:10:16 -05:00
* PiggyBankController constructor.
2019-07-21 10:15:06 -05:00
* @codeCoverageIgnore
2015-02-24 14:10:25 -06:00
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.piggyBanks'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-sort-amount-asc');
2016-10-29 00:44:46 -05:00
2018-04-21 16:48:54 -05:00
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->accountRepos = app(AccountRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-24 14:10:25 -06:00
}
2016-06-16 13:52:59 -05:00
/**
2017-11-15 05:25:49 -06:00
* Add money to piggy bank.
2016-06-16 13:52:59 -05:00
*
2018-04-21 16:48:54 -05:00
* @param PiggyBank $piggyBank
2016-06-16 13:52:59 -05:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2016-06-16 13:52:59 -05:00
*/
2018-04-21 16:48:54 -05:00
public function add(PiggyBank $piggyBank)
2016-06-16 13:52:59 -05:00
{
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
2018-04-21 16:48:54 -05:00
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date);
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
2016-06-16 13:52:59 -05:00
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
2019-09-27 13:36:52 -05:00
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
2018-04-21 16:48:54 -05:00
return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency'));
2016-06-16 13:52:59 -05:00
}
2015-02-25 12:32:33 -06:00
/**
2017-11-15 05:25:49 -06:00
* Add money to piggy bank (for mobile devices).
2015-02-25 12:32:33 -06:00
*
2018-04-21 16:48:54 -05:00
* @param PiggyBank $piggyBank
2018-04-02 08:10:40 -05:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
2018-04-21 16:48:54 -05:00
public function addMobile(PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2016-02-05 08:41:40 -06:00
/** @var Carbon $date */
2016-02-04 00:27:03 -06:00
$date = session('end', Carbon::now()->endOfMonth());
2018-04-21 16:48:54 -05:00
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date);
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
2015-07-26 12:42:28 -05:00
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
2015-02-25 12:32:33 -06:00
$maxAmount = min($leftOnAccount, $leftToSave);
2019-09-27 13:36:52 -05:00
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
2018-04-21 16:48:54 -05:00
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Create a piggy bank.
*
2018-03-07 03:18:22 -06:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
2018-03-07 03:18:22 -06:00
public function create()
2015-02-25 12:32:33 -06:00
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.new_piggy_bank');
2015-02-25 12:32:33 -06:00
$subTitleIcon = 'fa-plus';
2015-04-01 11:59:34 -05:00
// put previous url in session if not redirect from store (not "create another").
2017-11-15 05:25:49 -06:00
if (true !== session('piggy-banks.create.fromStore')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('piggy-banks.create.uri');
2015-04-01 11:59:34 -05:00
}
2018-04-22 10:12:22 -05:00
session()->forget('piggy-banks.create.fromStore');
2015-04-01 11:59:34 -05:00
2018-03-07 03:18:22 -06:00
return view('piggy-banks.create', compact('subTitle', 'subTitleIcon'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Delete a piggy bank.
*
2015-02-25 12:32:33 -06:00
* @param PiggyBank $piggyBank
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
public function delete(PiggyBank $piggyBank)
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.delete_piggy_bank', ['name' => $piggyBank->name]);
2015-02-25 12:32:33 -06:00
2015-04-01 11:59:34 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('piggy-banks.delete.uri');
2015-04-01 11:59:34 -05:00
2015-03-01 12:26:30 -06:00
return view('piggy-banks.delete', compact('piggyBank', 'subTitle'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Destroy the piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse
2015-02-25 12:32:33 -06:00
*/
2018-07-08 05:28:42 -05:00
public function destroy(PiggyBank $piggyBank): RedirectResponse
2015-02-25 12:32:33 -06:00
{
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.deleted_piggy_bank', ['name' => $piggyBank->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2018-04-21 16:48:54 -05:00
$this->piggyRepos->destroy($piggyBank);
2015-02-25 12:32:33 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('piggy-banks.delete.uri'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Edit a piggy bank.
*
2018-03-07 03:18:22 -06:00
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
2018-03-07 03:18:22 -06:00
public function edit(PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.update_piggy_title', ['name' => $piggyBank->name]);
2015-02-25 12:32:33 -06:00
$subTitleIcon = 'fa-pencil';
2016-04-29 01:56:56 -05:00
$targetDate = null;
2017-11-22 14:41:48 -06:00
$startDate = null;
$note = $piggyBank->notes()->first();
2017-11-15 05:25:49 -06:00
// Flash some data to fill the form.
if (null !== $piggyBank->targetdate) {
2016-05-20 01:00:35 -05:00
$targetDate = $piggyBank->targetdate->format('Y-m-d');
2015-02-25 12:32:33 -06:00
}
2017-11-22 14:41:48 -06:00
if (null !== $piggyBank->startdate) {
$startDate = $piggyBank->startdate->format('Y-m-d');
}
2016-04-29 01:56:56 -05:00
2015-02-25 12:32:33 -06:00
$preFilled = ['name' => $piggyBank->name,
2015-06-06 16:09:12 -05:00
'account_id' => $piggyBank->account_id,
'targetamount' => $piggyBank->targetamount,
'targetdate' => $targetDate,
2017-11-22 14:41:48 -06:00
'startdate' => $startDate,
'notes' => null === $note ? '' : $note->text,
2015-02-25 12:32:33 -06:00
];
2018-04-22 10:10:11 -05:00
session()->flash('preFilled', $preFilled);
2015-02-25 12:32:33 -06:00
2015-04-01 11:59:34 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('piggy-banks.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('piggy-banks.edit.uri');
2015-04-01 11:59:34 -05:00
}
2018-04-22 10:12:22 -05:00
session()->forget('piggy-banks.edit.fromUpdate');
2015-04-01 11:59:34 -05:00
2018-03-07 03:18:22 -06:00
return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'preFilled'));
2015-02-25 12:32:33 -06:00
}
2015-02-24 14:10:25 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show overview of all piggy banks.
*
2018-04-24 12:26:16 -05:00
* @param Request $request
2015-05-03 05:54:39 -05:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-24 14:10:25 -06:00
*/
2018-04-21 16:48:54 -05:00
public function index(Request $request)
2015-02-24 14:10:25 -06:00
{
2018-04-28 14:54:48 -05:00
$this->piggyRepos->correctOrder();
2018-04-21 16:48:54 -05:00
$collection = $this->piggyRepos->getPiggyBanks();
2017-12-28 02:53:21 -06:00
$total = $collection->count();
2018-04-02 08:10:40 -05:00
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-04-21 16:48:54 -05:00
$accounts = [];
/** @var Carbon $end */
2016-02-08 14:09:00 -06:00
$end = session('end', Carbon::now()->endOfMonth());
2015-02-24 14:10:25 -06:00
2018-04-21 16:48:54 -05:00
// transform piggies using the transformer:
$parameters = new ParameterBag;
$parameters->set('end', $end);
2018-12-17 00:09:44 -06:00
$transformed = new Collection;
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);
$transformer->setParameters(new ParameterBag);
/** @var AccountTransformer $accountTransformer */
$accountTransformer = app(AccountTransformer::class);
$accountTransformer->setParameters($parameters);
2018-04-21 16:48:54 -05:00
/** @var PiggyBank $piggy */
foreach ($collection as $piggy) {
$array = $transformer->transform($piggy);
$account = $accountTransformer->transform($piggy->account);
2020-02-08 05:59:43 -06:00
$accountId = (int)$account['id'];
2018-04-21 16:48:54 -05:00
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;
2015-02-24 14:10:25 -06:00
}
2018-04-21 16:48:54 -05:00
// 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);
2015-02-24 14:10:25 -06:00
}
2018-04-21 16:48:54 -05:00
$transformed = $transformed->slice(($page - 1) * $pageSize, $pageSize);
$piggyBanks = new LengthAwarePaginator($transformed, $total, $pageSize, $page);
2017-12-28 02:53:21 -06:00
$piggyBanks->setPath(route('piggy-banks.index'));
2015-02-24 15:53:38 -06:00
return view('piggy-banks.index', compact('piggyBanks', 'accounts'));
2015-02-24 14:10:25 -06:00
}
2015-02-23 14:55:52 -06:00
2015-02-25 12:32:33 -06:00
/**
2018-07-22 01:10:16 -05:00
* Add money to piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param Request $request
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse
2015-02-25 12:32:33 -06:00
*/
2018-07-08 05:28:42 -05:00
public function postAdd(Request $request, PiggyBank $piggyBank): RedirectResponse
2015-02-25 12:32:33 -06:00
{
2019-09-27 13:36:52 -05:00
$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');
2018-04-21 16:48:54 -05:00
}
if ($this->piggyRepos->canAddAmount($piggyBank, $amount)) {
$this->piggyRepos->addAmount($piggyBank, $amount);
2018-04-22 10:10:11 -05:00
session()->flash(
2017-11-15 03:52:29 -06:00
'success',
2018-04-02 08:10:40 -05:00
(string)trans(
'firefly.added_amount_to_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
2017-11-15 04:33:07 -06:00
)
);
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2016-04-29 01:56:56 -05:00
return redirect(route('piggy-banks.index'));
2015-02-25 12:32:33 -06:00
}
Log::error('Cannot add ' . $amount . ' because canAddAmount returned false.');
2018-04-22 10:10:11 -05:00
session()->flash(
2017-11-15 03:52:29 -06:00
'error',
2018-04-02 08:10:40 -05:00
(string)trans(
'firefly.cannot_add_amount_piggy',
2019-08-02 22:08:35 -05:00
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)]
2017-11-15 04:33:07 -06:00
)
);
2016-04-29 01:56:56 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('piggy-banks.index'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Remove money from piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param Request $request
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse
2015-02-25 12:32:33 -06:00
*/
2018-07-08 05:28:42 -05:00
public function postRemove(Request $request, PiggyBank $piggyBank): RedirectResponse
2015-02-25 12:32:33 -06:00
{
2019-09-27 13:36:52 -05:00
$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');
2018-04-21 16:48:54 -05:00
}
if ($this->piggyRepos->canRemoveAmount($piggyBank, $amount)) {
$this->piggyRepos->removeAmount($piggyBank, $amount);
2018-04-22 10:10:11 -05:00
session()->flash(
2017-06-05 04:12:50 -05:00
'success',
2018-04-02 08:10:40 -05:00
(string)trans(
'firefly.removed_amount_from_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
2017-10-05 04:49:06 -05:00
)
2016-03-20 05:38:01 -05:00
);
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2016-04-29 01:56:56 -05:00
return redirect(route('piggy-banks.index'));
2015-02-25 12:32:33 -06:00
}
2018-04-02 08:10:40 -05:00
$amount = (string)round($request->get('amount'), 12);
2018-04-22 10:10:11 -05:00
session()->flash(
2017-11-15 03:52:29 -06:00
'error',
2018-04-02 08:10:40 -05:00
(string)trans(
'firefly.cannot_remove_from_piggy',
2019-08-02 22:08:35 -05:00
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)]
2017-11-15 04:33:07 -06:00
)
2017-06-05 04:12:50 -05:00
);
2016-04-29 01:56:56 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('piggy-banks.index'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Remove money from piggy bank form.
*
2015-02-25 12:32:33 -06:00
* @param PiggyBank $piggyBank
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
public function remove(PiggyBank $piggyBank)
{
2018-04-21 16:48:54 -05:00
$repetition = $this->piggyRepos->getRepetition($piggyBank);
2019-09-27 13:36:52 -05:00
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
2018-04-21 16:48:54 -05:00
return view('piggy-banks.remove', compact('piggyBank', 'repetition', 'currency'));
2015-02-25 12:32:33 -06:00
}
/**
2017-11-15 05:25:49 -06:00
* Remove money from piggy bank (for mobile devices).
*
* @param PiggyBank $piggyBank
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function removeMobile(PiggyBank $piggyBank)
{
2018-04-21 16:48:54 -05:00
$repetition = $this->piggyRepos->getRepetition($piggyBank);
2019-09-27 13:36:52 -05:00
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
2018-04-21 16:48:54 -05:00
return view('piggy-banks.remove-mobile', compact('piggyBank', 'repetition', 'currency'));
}
2018-04-28 14:54:48 -05:00
/**
2018-07-22 01:10:16 -05:00
* Set the order of a piggy bank.
*
2018-04-28 14:54:48 -05:00
* @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']);
}
2015-02-25 12:32:33 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show a single piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 12:32:33 -06:00
*/
2018-04-21 16:48:54 -05:00
public function show(PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2018-05-26 00:48:49 -05:00
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
// transform piggies using the transformer:
$parameters = new ParameterBag;
$parameters->set('end', $end);
2018-12-17 00:09:44 -06:00
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);
$transformer->setParameters($parameters);
2019-02-13 10:38:41 -06:00
$piggy = $transformer->transform($piggyBank);
$events = $this->piggyRepos->getEvents($piggyBank);
$subTitle = $piggyBank->name;
2015-02-25 12:32:33 -06:00
2018-05-26 00:48:49 -05:00
return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'piggy'));
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Store a new piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param PiggyBankFormRequest $request
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 12:32:33 -06:00
*/
2018-04-21 16:48:54 -05:00
public function store(PiggyBankFormRequest $request)
2015-02-25 12:32:33 -06:00
{
2017-11-22 14:41:48 -06:00
$data = $request->getPiggyBankData();
if (null === $data['startdate']) {
$data['startdate'] = new Carbon;
}
2018-04-21 16:48:54 -05:00
$piggyBank = $this->piggyRepos->store($data);
2015-02-25 12:32:33 -06:00
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.stored_piggy_bank', ['name' => $piggyBank->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('piggy-banks.create.uri'));
2015-02-25 12:32:33 -06:00
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('create_another')) {
// @codeCoverageIgnoreStart
2018-04-22 10:12:22 -05:00
session()->put('piggy-banks.create.fromStore', true);
2015-04-07 11:26:14 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('piggy-banks.create'))->withInput();
// @codeCoverageIgnoreEnd
}
2018-07-08 05:08:53 -05:00
return $redirect;
2015-02-25 12:32:33 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Update a piggy bank.
*
2018-04-24 12:26:16 -05:00
* @param PiggyBankFormRequest $request
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2017-01-05 03:06:46 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 12:32:33 -06:00
*/
2018-04-21 16:48:54 -05:00
public function update(PiggyBankFormRequest $request, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2016-10-29 00:44:46 -05:00
$data = $request->getPiggyBankData();
2018-04-21 16:48:54 -05:00
$piggyBank = $this->piggyRepos->update($piggyBank, $data);
2015-02-25 12:32:33 -06:00
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('piggy-banks.edit.uri'));
2015-02-25 12:32:33 -06:00
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('return_to_edit')) {
// @codeCoverageIgnoreStart
2018-04-22 10:12:22 -05:00
session()->put('piggy-banks.edit.fromUpdate', true);
2015-04-07 11:26:14 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('piggy-banks.edit', [$piggyBank->id]));
// @codeCoverageIgnoreEnd
}
2018-07-08 05:08:53 -05:00
return $redirect;
2015-02-25 12:32:33 -06:00
}
2015-02-23 14:55:52 -06:00
}