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

412 lines
14 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* PiggyBankController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
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 Amount;
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
/**
2015-05-26 01:17:58 -05:00
*
*
2015-02-23 14:55:52 -06:00
* Class PiggyBankController
*
* @package FireflyIII\Http\Controllers
*/
2015-02-25 12:32:33 -06:00
class PiggyBankController extends Controller
{
2015-02-23 14:55:52 -06:00
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
/**
* 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
/**
* 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);
$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
2016-10-10 00:49:39 -05:00
*
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';
if (count($accounts) === 0) {
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").
2016-02-04 00:27:03 -06:00
if (session('piggy-banks.create.fromStore') !== true) {
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');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'piggy-banks');
Session::flash('gaEventAction', 'create');
2015-04-01 11:59:34 -05:00
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-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'piggy-banks');
Session::flash('gaEventAction', 'delete');
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
{
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.deleted_piggy_bank', ['name' => e($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;
$note = $piggyBank->notes()->first();
2015-02-25 12:32:33 -06:00
/*
* Flash some data to fill the form.
*/
2016-05-20 01:00:35 -05:00
if (!is_null($piggyBank->targetdate)) {
$targetDate = $piggyBank->targetdate->format('Y-m-d');
2015-02-25 12:32:33 -06:00
}
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,
'note' => is_null($note) ? '' : $note->text,
2015-02-25 12:32:33 -06:00
];
Session::flash('preFilled', $preFilled);
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'piggy-banks');
Session::flash('gaEventAction', 'edit');
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").
2016-02-04 00:27:03 -06:00
if (session('piggy-banks.edit.fromUpdate') !== true) {
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';
$piggyBank->percentage = bccomp('0', $piggyBank->savedSoFar) !== 0 ? 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
/*
* Fill account information:
*/
$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-04-08 12:05:37 -05:00
if (isset($accounts[$account->id]) && $new === false) {
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();
2015-03-15 12:00:33 -05:00
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
{
2016-12-30 06:45:02 -06:00
$amount = $request->get('amount');
2015-03-03 01:46:14 -06:00
if ($repository->canAddAmount($piggyBank, $amount)) {
$repository->addAmount($piggyBank, $amount);
Session::flash('success', strval(trans('firefly.added_amount_to_piggy', ['amount' => Amount::format($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.');
2016-04-29 01:56:56 -05:00
Session::flash('error', strval(trans('firefly.cannot_add_amount_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
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
{
$amount = $request->get('amount');
if ($repository->canRemoveAmount($piggyBank, $amount)) {
$repository->removeAmount($piggyBank, $amount);
2016-03-20 05:38:01 -05:00
Session::flash(
'success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => $piggyBank->name]))
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));
2016-04-29 01:56:56 -05:00
Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
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
}
/**
* 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)
{
2016-10-29 00:44:46 -05:00
$data = $request->getPiggyBankData();
2016-10-23 05:10:22 -05:00
$piggyBank = $repository->store($data);
2015-02-25 12:32:33 -06:00
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.stored_piggy_bank', ['name' => e($piggyBank->name)])));
Preferences::mark();
2015-02-25 12:32:33 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('create_another')) === 1) {
// @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-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('piggy-banks.edit.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
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.updated_piggy_bank', ['name' => e($piggyBank->name)])));
Preferences::mark();
2015-02-25 12:32:33 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('return_to_edit')) === 1) {
// @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
}