firefly-iii/app/controllers/PiggybankController.php

357 lines
12 KiB
PHP
Raw Normal View History

<?php
2014-07-31 15:01:52 -05:00
2014-12-22 00:10:18 -06:00
use Carbon\Carbon;
use FireflyIII\Database\PiggyBank\PiggyBank as Repository;
use FireflyIII\Exception\FireflyException;
use Illuminate\Support\Collection;
2014-07-31 15:01:52 -05:00
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
* @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
* @SuppressWarnings("TooManyMethods") // I'm also fine with this.
* @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
*
*
* Class PiggyBankController
*
2014-07-31 15:01:52 -05:00
*/
class PiggyBankController extends BaseController
2014-07-31 15:01:52 -05:00
{
/** @var Repository */
protected $_repository;
/**
* @param Repository $repository
*/
public function __construct(Repository $repository)
{
$this->_repository = $repository;
2014-12-22 00:10:18 -06:00
View::share('title', 'Piggy banks');
View::share('mainTitleIcon', 'fa-sort-amount-asc');
}
2014-11-12 15:36:02 -06:00
/**
* Add money to piggy bank
2014-11-15 04:36:27 -06:00
*
* @param PiggyBank $piggyBank
2014-11-12 15:36:02 -06:00
*
* @return $this
*/
public function add(PiggyBank $piggyBank)
2014-11-12 15:36:02 -06:00
{
$leftOnAccount = $this->_repository->leftOnAccount($piggyBank->account);
2014-12-25 01:07:17 -06:00
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$leftToSave = $piggyBank->targetamount - $savedSoFar;
$maxAmount = min($leftOnAccount, $leftToSave);
2014-11-12 15:36:02 -06:00
\Log::debug('Now going to view for piggy bank #' . $piggyBank->id . ' (' . $piggyBank->name . ')');
2014-12-23 14:13:42 -06:00
return View::make('piggy_banks.add', compact('piggyBank', 'maxAmount'));
2014-11-12 15:36:02 -06:00
}
/**
2014-11-13 09:13:32 -06:00
* @return mixed
*/
2014-10-30 13:26:52 -05:00
public function create()
{
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Account\Account $acct */
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
2014-12-22 00:10:18 -06:00
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$subTitle = 'Create new piggy bank';
$subTitleIcon = 'fa-plus';
return View::make('piggy_banks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
}
2014-08-10 08:01:46 -05:00
/**
* @param PiggyBank $piggyBank
2014-08-10 08:01:46 -05:00
*
* @return $this
*/
public function delete(PiggyBank $piggyBank)
{
$subTitle = 'Delete "' . e($piggyBank->name) . '"';
2014-12-22 00:10:18 -06:00
return View::make('piggy_banks.delete', compact('piggyBank', 'subTitle'));
}
2014-08-10 08:01:46 -05:00
/**
* @param PiggyBank $piggyBank
2014-08-10 08:01:46 -05:00
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(PiggyBank $piggyBank)
{
2014-12-22 00:10:18 -06:00
Session::flash('success', 'Piggy bank "' . e($piggyBank->name) . '" deleted.');
$this->_repository->destroy($piggyBank);
2014-11-02 09:47:01 -06:00
return Redirect::route('piggy_banks.index');
}
2014-08-10 08:01:46 -05:00
/**
* @param PiggyBank $piggyBank
2014-08-10 08:01:46 -05:00
*
* @return $this
*/
public function edit(PiggyBank $piggyBank)
{
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Account\Account $acct */
$acct = App::make('FireflyIII\Database\Account\Account');
$periods = Config::get('firefly.piggy_bank_periods');
2014-12-22 00:10:18 -06:00
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
$subTitle = 'Edit piggy bank "' . e($piggyBank->name) . '"';
2014-12-22 00:10:18 -06:00
$subTitleIcon = 'fa-pencil';
/*
* Flash some data to fill the form.
*/
if (is_null($piggyBank->targetdate) || $piggyBank->targetdate == '') {
2014-12-22 00:10:18 -06:00
$targetDate = null;
} else {
$targetDate = new Carbon($piggyBank->targetdate);
2014-12-22 00:10:18 -06:00
$targetDate = $targetDate->format('Y-m-d');
}
$preFilled = ['name' => $piggyBank->name,
'account_id' => $piggyBank->account_id,
'targetamount' => $piggyBank->targetamount,
2014-12-22 00:10:18 -06:00
'targetdate' => $targetDate,
'reminder' => $piggyBank->reminder,
'remind_me' => intval($piggyBank->remind_me) == 1 || !is_null($piggyBank->reminder) ? true : false
];
2014-12-07 08:37:53 -06:00
Session::flash('preFilled', $preFilled);
return View::make('piggy_banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'periods', 'preFilled'));
}
2014-12-06 10:53:25 -06:00
/**
* @return $this
*/
2014-11-12 15:36:02 -06:00
public function index()
{
/** @var Collection $piggyBanks */
$piggyBanks = $this->_repository->get();
2014-11-12 15:36:02 -06:00
$accounts = [];
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
$piggyBank->savedSoFar = floatval($piggyBank->currentRelevantRep()->currentamount);
$piggyBank->percentage = intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100);
$piggyBank->leftToSave = $piggyBank->targetamount - $piggyBank->savedSoFar;
2014-11-12 15:36:02 -06:00
/*
* Fill account information:
*/
$account = $piggyBank->account;
2014-11-12 15:36:02 -06:00
if (!isset($accounts[$account->id])) {
2014-12-23 14:13:42 -06:00
$accounts[$account->id] = [
'name' => $account->name,
'balance' => Steam::balance($account),
2014-12-24 14:20:47 -06:00
'leftForPiggyBanks' => $this->_repository->leftOnAccount($account),
'sumOfSaved' => $piggyBank->savedSoFar,
'sumOfTargets' => floatval($piggyBank->targetamount),
'leftToSave' => $piggyBank->leftToSave
2014-12-23 14:13:42 -06:00
];
2014-11-12 15:36:02 -06:00
} else {
$accounts[$account->id]['sumOfSaved'] += $piggyBank->savedSoFar;
$accounts[$account->id]['sumOfTargets'] += floatval($piggyBank->targetamount);
$accounts[$account->id]['leftToSave'] += $piggyBank->leftToSave;
2014-11-12 15:36:02 -06:00
}
}
return View::make('piggy_banks.index', compact('piggyBanks', 'accounts'));
}
/**
* POST add money to piggy bank
2014-11-15 04:36:27 -06:00
*
* @param PiggyBank $piggyBank
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postAdd(PiggyBank $piggyBank)
{
2014-11-08 04:38:50 -06:00
$amount = round(floatval(Input::get('amount')), 2);
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $acct */
2014-12-25 01:07:17 -06:00
$piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank');
2014-12-25 01:07:17 -06:00
$leftOnAccount = $piggyRepository->leftOnAccount($piggyBank->account);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$leftToSave = $piggyBank->targetamount - $savedSoFar;
2014-11-08 04:38:50 -06:00
$maxAmount = round(min($leftOnAccount, $leftToSave), 2);
if ($amount <= $maxAmount) {
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount += $amount;
$repetition->save();
/*
* Create event!
*/
Event::fire('piggy_bank.addMoney', [$piggyBank, $amount]); // new and used.
Session::flash('success', 'Added ' . mf($amount, false) . ' to "' . e($piggyBank->name) . '".');
} else {
Session::flash('error', 'Could not add ' . mf($amount, false) . ' to "' . e($piggyBank->name) . '".');
}
return Redirect::route('piggy_banks.index');
}
/**
* @param PiggyBank $piggyBank
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postRemove(PiggyBank $piggyBank)
{
$amount = floatval(Input::get('amount'));
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
if ($amount <= $savedSoFar) {
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount -= $amount;
$repetition->save();
/*
* Create event!
*/
Event::fire('piggy_bank.removeMoney', [$piggyBank, $amount]); // new and used.
Session::flash('success', 'Removed ' . mf($amount, false) . ' from "' . e($piggyBank->name) . '".');
} else {
Session::flash('error', 'Could not remove ' . mf($amount, false) . ' from "' . e($piggyBank->name) . '".');
}
2014-11-12 15:36:02 -06:00
return Redirect::route('piggy_banks.index');
}
2014-11-12 15:36:02 -06:00
/**
* @param PiggyBank $piggyBank
2014-11-12 15:36:02 -06:00
*
2014-12-31 09:17:43 -06:00
* @SuppressWarnings("Unused")
*
2014-11-12 15:36:02 -06:00
* @return \Illuminate\View\View
*/
public function remove(PiggyBank $piggyBank)
{
2014-12-25 01:07:17 -06:00
return View::make('piggy_banks.remove', compact('piggyBank'));
}
2014-12-06 10:53:25 -06:00
/**
* @param PiggyBank $piggyBank
2014-12-06 10:53:25 -06:00
*
* @return $this
*/
public function show(PiggyBank $piggyBank)
{
$events = $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
2014-11-15 04:36:27 -06:00
/*
* Number of reminders:
*/
$amountPerReminder = $piggyBank->amountPerReminder();
$remindersCount = $piggyBank->countFutureReminders();
$subTitle = e($piggyBank->name);
2014-11-15 04:36:27 -06:00
return View::make('piggy_banks.show', compact('amountPerReminder', 'remindersCount', 'piggyBank', 'events', 'subTitle'));
2014-11-08 12:11:51 -06:00
}
/**
*
*/
2014-10-30 13:26:52 -05:00
public function store()
{
$data = Input::all();
$data['repeats'] = 0;
$data['user_id'] = Auth::user()->id;
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not store piggy bank: ' . $messages['errors']->first());
}
// return to create screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('piggy_banks.create')->withInput();
}
2015-01-01 22:52:38 -06:00
// store
$piggyBank = $this->_repository->store($data);
Event::fire('piggy_bank.store', [$piggyBank]); // new and used.
Session::flash('success', 'Piggy bank "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('piggy_banks.index');
}
return Redirect::route('piggy_banks.create')->withInput();
}
2014-11-08 04:36:20 -06:00
/**
* @param PiggyBank $piggyBank
2014-11-08 04:36:20 -06:00
*
2014-12-06 10:53:25 -06:00
* @return $this
* @throws FireflyException
2014-11-08 04:36:20 -06:00
*/
public function update(PiggyBank $piggyBank)
{
2014-11-08 04:36:20 -06:00
2014-12-14 13:40:02 -06:00
$data = Input::except('_token');
$data['rep_every'] = 0;
$data['reminder_skip'] = 0;
$data['order'] = 0;
2014-12-14 14:24:20 -06:00
$data['remind_me'] = isset($data['remind_me']) ? 1 : 0;
$data['user_id'] = Auth::user()->id;
2014-12-30 08:24:10 -06:00
$data['repeats'] = 0;
// always validate:
$messages = $this->_repository->validate($data);
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update piggy bank: ' . $messages['errors']->first());
}
// return to update screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('piggy_banks.edit', $piggyBank->id)->withInput();
}
// update
$this->_repository->update($piggyBank, $data);
Session::flash('success', 'Piggy bank "' . e($data['name']) . '" updated.');
2014-11-08 04:36:20 -06:00
// go back to list
if ($data['post_submit_action'] == 'update') {
return Redirect::route('piggy_banks.index');
2014-11-08 04:36:20 -06:00
}
2014-08-10 04:30:14 -05:00
// go back to update screen.
return Redirect::route('piggy_banks.edit', $piggyBank->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
2015-01-01 23:16:49 -06:00
}