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