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

389 lines
13 KiB
PHP
Raw Normal View History

2015-02-23 14:55:52 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-02-25 12:32:33 -06:00
use Amount;
use Carbon\Carbon;
use Config;
use ExpandedForm;
use FireflyIII\Http\Requests\PiggyBankFormRequest;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
2015-02-25 12:32:33 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2015-02-24 14:10:25 -06:00
use Illuminate\Support\Collection;
2015-02-25 12:32:33 -06:00
use Input;
2016-01-24 13:38:58 -06:00
use Log;
use Preferences;
2015-02-25 12:32:33 -06:00
use Session;
2015-02-24 14:10:25 -06:00
use Steam;
2015-04-01 11:59:34 -05:00
use URL;
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();
2015-05-14 08:53:56 -05:00
View::share('title', trans('firefly.piggyBanks'));
2015-02-24 14:10:25 -06:00
View::share('mainTitleIcon', 'fa-sort-amount-asc');
}
2015-02-25 12:32:33 -06:00
/**
* Add money to piggy bank
*
2016-01-01 05:41:00 -06:00
* @param ARI $repository
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return $this
*/
public function add(ARI $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2015-07-26 12:42:28 -05:00
bcscale(2);
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());
2015-05-17 02:35:49 -05:00
$leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date);
2015-02-25 12:32:33 -06:00
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
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);
2015-02-27 04:09:23 -06:00
return view('piggy-banks.add', compact('piggyBank', 'maxAmount'));
2015-02-25 12:32:33 -06:00
}
/**
* @param ARI $repository
2015-05-03 05:58:55 -05:00
*
2015-02-25 12:32:33 -06:00
* @return mixed
*/
public function create(ARI $repository)
2015-02-25 12:32:33 -06:00
{
2015-05-04 15:33:03 -05:00
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account']));
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';
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) {
2015-04-01 11:59:34 -05:00
Session::put('piggy-banks.create.url', URL::previous());
}
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
2015-02-27 04:09:23 -06:00
return view('piggy-banks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
2015-02-25 12:32:33 -06:00
}
/**
* @param PiggyBank $piggyBank
*
* @return $this
*/
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
Session::put('piggy-banks.delete.url', URL::previous());
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
{
2015-04-20 14:57:20 -05:00
2015-02-25 12:32:33 -06:00
Session::flash('success', 'Piggy bank "' . e($piggyBank->name) . '" deleted.');
Preferences::mark();
2015-04-20 14:57:20 -05:00
$repository->destroy($piggyBank);
2015-02-25 12:32:33 -06:00
2016-02-04 00:27:03 -06:00
return redirect(session('piggy-banks.delete.url'));
2015-02-25 12:32:33 -06:00
}
/**
2016-01-01 05:41:00 -06:00
* @param ARI $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
*/
public function edit(ARI $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
$periods = Config::get('firefly.piggy_bank_periods');
2015-04-20 14:57:20 -05:00
$accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account']));
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';
/*
* Flash some data to fill the form.
*/
if (is_null($piggyBank->targetdate) || $piggyBank->targetdate == '') {
$targetDate = null;
} else {
$targetDate = new Carbon($piggyBank->targetdate);
$targetDate = $targetDate->format('Y-m-d');
}
$preFilled = ['name' => $piggyBank->name,
2015-06-06 16:09:12 -05:00
'account_id' => $piggyBank->account_id,
'targetamount' => $piggyBank->targetamount,
'targetdate' => $targetDate,
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) {
2015-04-01 11:59:34 -05:00
Session::put('piggy-banks.edit.url', URL::previous());
}
Session::forget('piggy-banks.edit.fromUpdate');
2015-02-27 04:09:23 -06:00
return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'periods', 'preFilled'));
2015-02-25 12:32:33 -06:00
}
2015-02-24 14:10:25 -06:00
/**
2016-01-15 06:08:25 -06:00
* @param ARI $repository
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $piggyRepository
*
* @return View
2015-02-24 14:10:25 -06:00
*/
public function index(ARI $repository, 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-07-06 11:04:13 -05:00
bcscale(2);
2015-02-24 14:10:25 -06:00
$accounts = [];
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
2015-07-26 12:07:02 -05:00
$piggyBank->savedSoFar = round($piggyBank->currentRelevantRep()->currentamount, 2);
2015-04-20 14:57:20 -05:00
$piggyBank->percentage = $piggyBank->savedSoFar != 0 ? intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100) : 0;
2015-07-26 12:42:28 -05:00
$piggyBank->leftToSave = bcsub($piggyBank->targetamount, $piggyBank->savedSoFar);
2015-02-24 14:10:25 -06:00
/*
* Fill account information:
*/
$account = $piggyBank->account;
if (!isset($accounts[$account->id])) {
$accounts[$account->id] = [
'name' => $account->name,
2015-05-17 02:35:49 -05:00
'balance' => Steam::balance($account, $end, true),
'leftForPiggyBanks' => $repository->leftOnAccount($account, $end),
2015-02-24 14:10:25 -06:00
'sumOfSaved' => $piggyBank->savedSoFar,
2015-07-26 12:07:02 -05:00
'sumOfTargets' => round($piggyBank->targetamount, 2),
2016-01-15 16:12:52 -06:00
'leftToSave' => $piggyBank->leftToSave,
2015-02-24 14:10:25 -06:00
];
} else {
2015-07-06 11:04:13 -05:00
$accounts[$account->id]['sumOfSaved'] = bcadd($accounts[$account->id]['sumOfSaved'], $piggyBank->savedSoFar);
$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
/**
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
2015-03-15 12:00:33 -05:00
*/
public function order(PiggyBankRepositoryInterface $repository)
{
$data = Input::get('order');
// 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
}
}
}
2015-02-25 08:19:14 -06:00
2015-02-25 12:32:33 -06:00
/**
2015-05-03 05:54:39 -05:00
* @param PiggyBankRepositoryInterface $repository
2016-01-01 05:41:00 -06:00
* @param ARI $accounts
2015-05-03 05:54:39 -05:00
* @param PiggyBank $piggyBank
2015-02-25 12:32:33 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postAdd(PiggyBankRepositoryInterface $repository, ARI $accounts, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2015-07-26 12:42:28 -05:00
bcscale(2);
2016-02-08 14:09:00 -06:00
$amount = round(Input::get('amount'), 2);
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());
2015-05-17 02:35:49 -05:00
$leftOnAccount = $accounts->leftOnAccount($piggyBank->account, $date);
2015-02-25 12:32:33 -06:00
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
2015-07-26 12:42:28 -05:00
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
2015-02-25 12:32:33 -06:00
$maxAmount = round(min($leftOnAccount, $leftToSave), 2);
if ($amount <= $maxAmount) {
2015-07-06 11:04:13 -05:00
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount = bcadd($repetition->currentamount, $amount);
2015-02-25 12:32:33 -06:00
$repetition->save();
2015-04-20 14:57:20 -05:00
// create event
$repository->createEvent($piggyBank, $amount);
2015-03-03 01:46:14 -06:00
2015-02-25 12:32:33 -06:00
Session::flash('success', 'Added ' . Amount::format($amount, false) . ' to "' . e($piggyBank->name) . '".');
Preferences::mark();
2015-02-25 12:32:33 -06:00
} else {
2016-01-24 13:38:58 -06:00
Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')');
2015-02-25 12:32:33 -06:00
Session::flash('error', 'Could not add ' . Amount::format($amount, false) . ' to "' . 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
}
/**
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 postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
2015-02-25 12:32:33 -06:00
{
2015-07-26 12:07:02 -05:00
$amount = round(Input::get('amount'), 2);
2015-07-06 11:06:31 -05:00
bcscale(2);
2015-02-25 12:32:33 -06:00
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
if ($amount <= $savedSoFar) {
2015-07-06 11:06:31 -05:00
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
2015-02-25 12:32:33 -06:00
$repetition->save();
2015-04-20 14:57:20 -05:00
// create event
2016-02-08 14:09:00 -06:00
$repository->createEvent($piggyBank, bcmul($amount, '-1'));
2015-02-25 12:32:33 -06:00
Session::flash('success', 'Removed ' . Amount::format($amount, false) . ' from "' . e($piggyBank->name) . '".');
Preferences::mark();
2015-02-25 12:32:33 -06:00
} else {
Session::flash('error', 'Could not remove ' . Amount::format($amount, false) . ' from "' . 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 \Illuminate\View\View
*/
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
}
/**
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
{
2015-06-23 15:13:13 -05:00
$events = $repository->getEvents($piggyBank);
2015-02-25 12:32:33 -06:00
$subTitle = e($piggyBank->name);
2015-02-27 04:09:23 -06:00
return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle'));
2015-02-25 12:32:33 -06:00
}
/**
* @param PiggyBankFormRequest $request
* @param PiggyBankRepositoryInterface $repository
2015-02-25 12:32:33 -06:00
*
* @return $this|\Illuminate\Http\RedirectResponse
2015-02-25 12:32:33 -06:00
*/
public function store(PiggyBankFormRequest $request, PiggyBankRepositoryInterface $repository)
{
2015-04-20 14:57:20 -05:00
2015-02-25 12:32:33 -06:00
$piggyBankData = [
2015-06-23 15:13:13 -05:00
'name' => $request->get('name'),
'startdate' => new Carbon,
'account_id' => intval($request->get('account_id')),
2015-07-26 12:07:02 -05:00
'targetamount' => round($request->get('targetamount'), 2),
2015-06-23 15:13:13 -05:00
'remind_me' => false,
'reminder_skip' => 0,
2016-01-24 13:38:58 -06:00
'order' => $repository->getMaxOrder() + 1,
2015-06-23 15:13:13 -05:00
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
2015-02-25 12:32:33 -06:00
];
$piggyBank = $repository->store($piggyBankData);
Session::flash('success', 'Stored piggy bank "' . e($piggyBank->name) . '".');
Preferences::mark();
2015-02-25 12:32:33 -06:00
if (intval(Input::get('create_another')) === 1) {
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();
}
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('piggy-banks.create.url'));
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
*
* @return $this
*/
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
{
$piggyBankData = [
2015-06-23 15:13:13 -05:00
'name' => $request->get('name'),
'startdate' => is_null($piggyBank->startdate) ? $piggyBank->created_at : $piggyBank->startdate,
'account_id' => intval($request->get('account_id')),
2015-07-26 12:07:02 -05:00
'targetamount' => round($request->get('targetamount'), 2),
2015-06-23 15:13:13 -05:00
'remind_me' => false,
'reminder_skip' => 0,
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
2015-02-25 12:32:33 -06:00
];
$piggyBank = $repository->update($piggyBank, $piggyBankData);
Session::flash('success', 'Updated piggy bank "' . e($piggyBank->name) . '".');
Preferences::mark();
2015-02-25 12:32:33 -06:00
if (intval(Input::get('return_to_edit')) === 1) {
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]));
}
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('piggy-banks.edit.url'));
2015-02-25 12:32:33 -06:00
}
2015-02-23 14:55:52 -06:00
}