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

439 lines
15 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* PiggyBankController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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
* along with Firefly III. If not, see <http://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 ExpandedForm;
use FireflyIII\Http\Requests\PiggyBankFormRequest;
2016-05-20 04:02:07 -05:00
use FireflyIII\Models\AccountType;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-02-25 12:32:33 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
2015-02-24 14:10:25 -06:00
use Illuminate\Support\Collection;
2016-01-24 13:38:58 -06:00
use Log;
use Preferences;
2016-12-18 10:54:11 -06:00
use Response;
2015-02-25 12:32:33 -06:00
use Session;
2015-02-24 14:10:25 -06:00
use Steam;
2015-04-07 11:26:14 -05:00
use View;
2015-02-23 14:55:52 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class PiggyBankController.
2015-02-23 14:55:52 -06:00
*/
2015-02-25 12:32:33 -06:00
class PiggyBankController extends Controller
{
2015-02-24 14:10:25 -06:00
/**
2016-02-04 00:27:03 -06:00
*
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) {
View::share('title', trans('firefly.piggyBanks'));
View::share('mainTitleIcon', 'fa-sort-amount-asc');
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
*
* @param PiggyBank $piggyBank
*
* @return View
2016-06-16 13:52:59 -05:00
*/
2016-10-09 23:47:42 -05:00
public function add(PiggyBank $piggyBank)
2016-06-16 13:52:59 -05:00
{
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
2016-10-09 23:47:42 -05:00
$leftOnAccount = $piggyBank->leftOnAccount($date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount ?? '0';
2016-06-16 13:52:59 -05:00
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
return view('piggy-banks.add', compact('piggyBank', 'maxAmount'));
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
*
2016-01-01 05:41:00 -06:00
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return View
2015-02-25 12:32:33 -06:00
*/
2016-10-09 23:47:42 -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());
2016-10-09 23:47:42 -05:00
$leftOnAccount = $piggyBank->leftOnAccount($date);
2017-07-23 01:32:51 -05:00
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount ?? '0';
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);
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount'));
2015-02-25 12:32:33 -06:00
}
/**
2016-10-10 00:49:39 -05:00
* @param AccountRepositoryInterface $repository
2015-05-03 05:58:55 -05:00
*
2016-05-20 04:02:07 -05:00
* @return View
2015-02-25 12:32:33 -06:00
*/
2016-10-10 00:49:39 -05:00
public function create(AccountRepositoryInterface $repository)
2015-02-25 12:32:33 -06:00
{
2016-10-10 00:49:39 -05:00
$accounts = ExpandedForm::makeSelectList($repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]));
2015-07-26 00:39:04 -05:00
$subTitle = trans('firefly.new_piggy_bank');
2015-02-25 12:32:33 -06:00
$subTitleIcon = 'fa-plus';
2017-11-15 05:25:49 -06:00
if (0 === count($accounts)) {
Session::flash('error', strval(trans('firefly.need_at_least_one_account')));
return redirect(route('new-user.index'));
}
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
}
Session::forget('piggy-banks.create.fromStore');
2016-05-20 10:28:07 -05:00
return view('piggy-banks.create', compact('accounts', 'subTitle', 'subTitleIcon'));
2015-02-25 12:32:33 -06:00
}
/**
* @param PiggyBank $piggyBank
*
* @return View
2015-02-25 12:32:33 -06:00
*/
public function delete(PiggyBank $piggyBank)
{
2015-05-25 15:16:00 -05:00
$subTitle = 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
}
/**
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-03 05:54:39 -05:00
public function destroy(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.deleted_piggy_bank', ['name' => $piggyBank->name])));
Preferences::mark();
2015-04-20 14:57:20 -05:00
$repository->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
}
/**
2016-10-10 00:49:39 -05:00
* @param AccountRepositoryInterface $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2015-05-03 05:54:39 -05:00
* @return View
2015-02-25 12:32:33 -06:00
*/
2016-10-10 00:49:39 -05:00
public function edit(AccountRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2016-10-10 00:49:39 -05:00
$accounts = ExpandedForm::makeSelectList($repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]));
2015-05-25 02:23:45 -05:00
$subTitle = 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,
2017-11-15 05:25:49 -06:00
'note' => null === $note ? '' : $note->text,
2015-02-25 12:32:33 -06:00
];
Session::flash('preFilled', $preFilled);
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
}
Session::forget('piggy-banks.edit.fromUpdate');
2016-05-20 10:28:07 -05:00
return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'preFilled'));
2015-02-25 12:32:33 -06:00
}
2015-02-24 14:10:25 -06:00
/**
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $piggyRepository
*
* @return View
2015-02-24 14:10:25 -06:00
*/
2016-10-09 23:47:42 -05:00
public function index(PiggyBankRepositoryInterface $piggyRepository)
2015-02-24 14:10:25 -06:00
{
/** @var Collection $piggyBanks */
2015-04-20 14:57:20 -05:00
$piggyBanks = $piggyRepository->getPiggyBanks();
/** @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
$accounts = [];
Log::debug('Looping piggues');
2015-02-24 14:10:25 -06:00
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
$piggyBank->savedSoFar = $piggyBank->currentRelevantRep()->currentamount ?? '0';
2017-11-15 05:25:49 -06:00
$piggyBank->percentage = 0 !== bccomp('0', $piggyBank->savedSoFar) ? intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100) : 0;
2016-05-20 04:53:34 -05:00
$piggyBank->leftToSave = bcsub($piggyBank->targetamount, strval($piggyBank->savedSoFar));
2016-04-21 04:03:04 -05:00
$piggyBank->percentage = $piggyBank->percentage > 100 ? 100 : $piggyBank->percentage;
2015-02-24 14:10:25 -06:00
2017-11-15 05:25:49 -06:00
// Fill account information:
2015-02-24 14:10:25 -06:00
$account = $piggyBank->account;
2017-04-08 12:05:37 -05:00
$new = false;
2015-02-24 14:10:25 -06:00
if (!isset($accounts[$account->id])) {
2017-04-08 12:05:37 -05:00
$new = true;
2015-02-24 14:10:25 -06:00
$accounts[$account->id] = [
'name' => $account->name,
2016-04-27 12:21:47 -05:00
'balance' => Steam::balanceIgnoreVirtual($account, $end),
2016-10-09 23:47:42 -05:00
'leftForPiggyBanks' => $piggyBank->leftOnAccount($end),
2016-02-12 08:01:32 -06:00
'sumOfSaved' => strval($piggyBank->savedSoFar),
2016-12-30 06:45:02 -06:00
'sumOfTargets' => $piggyBank->targetamount,
2016-01-15 16:12:52 -06:00
'leftToSave' => $piggyBank->leftToSave,
2015-02-24 14:10:25 -06:00
];
2017-03-17 10:34:57 -05:00
}
2017-11-15 05:25:49 -06:00
if (isset($accounts[$account->id]) && false === $new) {
2016-05-20 04:53:34 -05:00
$accounts[$account->id]['sumOfSaved'] = bcadd($accounts[$account->id]['sumOfSaved'], strval($piggyBank->savedSoFar));
2015-07-06 11:04:13 -05:00
$accounts[$account->id]['sumOfTargets'] = bcadd($accounts[$account->id]['sumOfTargets'], $piggyBank->targetamount);
$accounts[$account->id]['leftToSave'] = bcadd($accounts[$account->id]['leftToSave'], $piggyBank->leftToSave);
2015-02-24 14:10:25 -06:00
}
}
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-03-15 12:00:33 -05:00
/**
2016-12-28 06:02:56 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
2016-12-18 10:54:11 -06:00
*
* @return \Illuminate\Http\JsonResponse
2015-03-15 12:00:33 -05:00
*/
2016-12-28 06:02:56 -06:00
public function order(Request $request, PiggyBankRepositoryInterface $repository)
2015-03-15 12:00:33 -05:00
{
2016-12-28 06:02:56 -06:00
$data = $request->get('order');
2015-03-15 12:00:33 -05:00
// set all users piggy banks to zero:
$repository->reset();
if (is_array($data)) {
foreach ($data as $order => $id) {
2015-07-26 12:42:28 -05:00
$repository->setOrder(intval($id), ($order + 1));
2015-03-15 12:00:33 -05:00
}
}
2016-12-19 14:07:22 -06:00
2016-12-18 10:54:11 -06:00
return Response::json(['result' => 'ok']);
2015-03-15 12:00:33 -05:00
}
2015-02-25 08:19:14 -06:00
2015-02-25 12:32:33 -06:00
/**
2016-12-28 06:02:56 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2016-12-28 06:02:56 -06:00
public function postAdd(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2017-10-23 06:25:12 -05:00
$amount = $request->get('amount') ?? '0';
2017-10-05 04:49:06 -05:00
$currency = app('amount')->getDefaultCurrency();
if ($repository->canAddAmount($piggyBank, $amount)) {
$repository->addAmount($piggyBank, $amount);
Session::flash(
2017-11-15 03:52:29 -06:00
'success',
strval(
2017-11-15 04:33:07 -06:00
trans(
'firefly.added_amount_to_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
)
)
);
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.');
Session::flash(
2017-11-15 03:52:29 -06:00
'error',
strval(
2017-11-15 04:33:07 -06:00
trans(
'firefly.cannot_add_amount_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
)
)
);
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
}
/**
2016-12-28 06:02:56 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2016-12-28 06:02:56 -06:00
public function postRemove(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2017-10-23 06:25:12 -05:00
$amount = $request->get('amount') ?? '0';
2017-10-05 04:49:06 -05:00
$currency = app('amount')->getDefaultCurrency();
if ($repository->canRemoveAmount($piggyBank, $amount)) {
$repository->removeAmount($piggyBank, $amount);
2016-03-20 05:38:01 -05:00
Session::flash(
2017-06-05 04:12:50 -05:00
'success',
2017-10-05 04:49:06 -05:00
strval(
trans(
2017-11-15 03:52:29 -06:00
'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
);
Preferences::mark();
2016-04-29 01:56:56 -05:00
return redirect(route('piggy-banks.index'));
2015-02-25 12:32:33 -06:00
}
2017-04-08 12:05:37 -05:00
$amount = strval(round($request->get('amount'), 12));
2017-06-05 04:12:50 -05:00
Session::flash(
2017-11-15 03:52:29 -06:00
'error',
strval(
2017-11-15 04:33:07 -06:00
trans(
'firefly.cannot_remove_from_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
)
)
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
}
/**
* @param PiggyBank $piggyBank
*
* @return View
2015-02-25 12:32:33 -06:00
*/
public function remove(PiggyBank $piggyBank)
{
2015-02-27 04:09:23 -06:00
return view('piggy-banks.remove', compact('piggyBank'));
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
*
* @return View
*/
public function removeMobile(PiggyBank $piggyBank)
{
return view('piggy-banks.remove-mobile', compact('piggyBank'));
}
2015-02-25 12:32:33 -06:00
/**
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
2015-05-03 05:54:39 -05:00
* @return View
2015-02-25 12:32:33 -06:00
*/
2015-05-03 05:54:39 -05:00
public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
$note = $piggyBank->notes()->first();
2015-06-23 15:13:13 -05:00
$events = $repository->getEvents($piggyBank);
2015-02-25 12:32:33 -06:00
$subTitle = e($piggyBank->name);
2016-10-22 03:13:49 -05:00
return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'note'));
2015-02-25 12:32:33 -06:00
}
/**
* @param PiggyBankFormRequest $request
* @param PiggyBankRepositoryInterface $repository
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 12:32:33 -06:00
*/
public function store(PiggyBankFormRequest $request, PiggyBankRepositoryInterface $repository)
{
2017-11-22 14:41:48 -06:00
$data = $request->getPiggyBankData();
if (null === $data['startdate']) {
$data['startdate'] = new Carbon;
}
2016-10-23 05:10:22 -05:00
$piggyBank = $repository->store($data);
2015-02-25 12:32:33 -06:00
2017-11-22 14:41:48 -06:00
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.stored_piggy_bank', ['name' => $piggyBank->name])));
Preferences::mark();
2015-02-25 12:32:33 -06:00
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('create_another'))) {
// @codeCoverageIgnoreStart
2015-04-01 11:59:34 -05:00
Session::put('piggy-banks.create.fromStore', true);
2015-04-07 11:26:14 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('piggy-banks.create'))->withInput();
// @codeCoverageIgnoreEnd
}
2017-07-07 09:53:44 -05:00
return redirect($this->getPreviousUri('piggy-banks.create.uri'));
2015-02-25 12:32:33 -06:00
}
/**
2015-05-03 05:58:55 -05:00
* @param PiggyBankRepositoryInterface $repository
* @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
*/
2015-05-03 05:54:39 -05:00
public function update(PiggyBankRepositoryInterface $repository, PiggyBankFormRequest $request, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2016-10-29 00:44:46 -05:00
$data = $request->getPiggyBankData();
2016-10-23 05:10:22 -05:00
$piggyBank = $repository->update($piggyBank, $data);
2015-02-25 12:32:33 -06:00
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name])));
Preferences::mark();
2015-02-25 12:32:33 -06:00
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('return_to_edit'))) {
// @codeCoverageIgnoreStart
2015-04-01 11:59:34 -05:00
Session::put('piggy-banks.edit.fromUpdate', true);
2015-04-07 11:26:14 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('piggy-banks.edit', [$piggyBank->id]));
// @codeCoverageIgnoreEnd
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('piggy-banks.edit.uri'));
2015-02-25 12:32:33 -06:00
}
2015-02-23 14:55:52 -06:00
}