mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-23 09:26:29 -06:00
Expanded the repeated expenses.
This commit is contained in:
parent
6b1d8d3aaa
commit
1bb49fa496
@ -1,15 +1,135 @@
|
||||
<?php
|
||||
|
||||
use FireflyIII\Exception\FireflyException;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
class RepeatedExpenseController extends BaseController
|
||||
{
|
||||
public function __construct() {
|
||||
View::share('title','Repeated expenses');
|
||||
View::share('mainTitleIcon','fa-rotate-left');
|
||||
public function __construct()
|
||||
{
|
||||
View::share('title', 'Repeated expenses');
|
||||
View::share('mainTitleIcon', 'fa-rotate-left');
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
/** @var \FireflyIII\Database\Account $acct */
|
||||
$acct = App::make('FireflyIII\Database\Account');
|
||||
|
||||
$periods = Config::get('firefly.piggybank_periods');
|
||||
|
||||
|
||||
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
|
||||
|
||||
return View::make('repeatedexpense.create', compact('accounts', 'periods'))->with('subTitle', 'Create new repeated expense')->with(
|
||||
'subTitleIcon', 'fa-plus'
|
||||
);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
||||
$subTitle = 'Overview';
|
||||
return View::make('repeatedexpense.index',compact('subTitle'));
|
||||
|
||||
/** @var \FireflyIII\Database\RepeatedExpense $repository */
|
||||
$repository = App::make('FireflyIII\Database\RepeatedExpense');
|
||||
|
||||
$expenses = $repository->get();
|
||||
|
||||
$expenses->each(
|
||||
function (\Piggybank $piggyBank) {
|
||||
// do something with "parts".
|
||||
$piggyBank->currentRep = $piggyBank->currentRelevantRep();
|
||||
if (!is_null($piggyBank->reminder)) {
|
||||
switch ($piggyBank->reminder) {
|
||||
default:
|
||||
throw new FireflyException('Cannot handle "' . $piggyBank->reminder . '" reminders for repeated expenses');
|
||||
break;
|
||||
case 'month':
|
||||
$piggyBank->parts = $piggyBank->currentRep->startdate->diffInMonths($piggyBank->currentRep->targetdate);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$piggyBank->parts = 1;
|
||||
}
|
||||
|
||||
// number of bars:
|
||||
$piggyBank->barCount = floor(12 / $piggyBank->parts);
|
||||
$amountPerBar = $piggyBank->targetamount / $piggyBank->parts;
|
||||
$currentAmount = $amountPerBar;
|
||||
$bars = [];
|
||||
$currentDate = clone $piggyBank->currentRep->startdate;
|
||||
for ($i = 0; $i < $piggyBank->parts; $i++) {
|
||||
// niet elke keer een andere dinges pakken? om target te redden?
|
||||
|
||||
$bars[] = [
|
||||
'amount' => $currentAmount,
|
||||
'date' => $currentDate
|
||||
];
|
||||
if (!is_null($piggyBank->reminder)) {
|
||||
$currentDate = \DateKit::addPeriod($currentDate, $piggyBank->reminder, 0);
|
||||
}
|
||||
|
||||
$currentAmount += $amountPerBar;
|
||||
}
|
||||
$piggyBank->bars = $bars;
|
||||
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
return View::make('repeatedexpense.index', compact('expenses', 'subTitle'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
$data = Input::all();
|
||||
$data['repeats'] = 1;
|
||||
/** @var \FireflyIII\Database\RepeatedExpense $repository */
|
||||
$repository = App::make('FireflyIII\Database\RepeatedExpense');
|
||||
|
||||
switch ($data['post_submit_action']) {
|
||||
default:
|
||||
throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"');
|
||||
break;
|
||||
case 'create_another':
|
||||
case 'store':
|
||||
$messages = $repository->validate($data);
|
||||
/** @var MessageBag $messages ['errors'] */
|
||||
if ($messages['errors']->count() > 0) {
|
||||
Session::flash('warnings', $messages['warnings']);
|
||||
Session::flash('successes', $messages['successes']);
|
||||
Session::flash('error', 'Could not save repeated expense: ' . $messages['errors']->first());
|
||||
|
||||
return Redirect::route('repeated.create')->withInput()->withErrors($messages['errors']);
|
||||
}
|
||||
// store!
|
||||
$repeated = $repository->store($data);
|
||||
|
||||
/*
|
||||
* Create the relevant repetition per Event.
|
||||
*/
|
||||
Event::fire('piggybank.store', [$repeated]); // new and used.
|
||||
|
||||
Session::flash('success', 'New repeated expense stored!');
|
||||
|
||||
if ($data['post_submit_action'] == 'create_another') {
|
||||
return Redirect::route('repeated.create')->withInput();
|
||||
} else {
|
||||
return Redirect::route('repeated.index');
|
||||
}
|
||||
break;
|
||||
case 'validate_only':
|
||||
$messageBags = $repository->validate($data);
|
||||
Session::flash('warnings', $messageBags['warnings']);
|
||||
Session::flash('successes', $messageBags['successes']);
|
||||
Session::flash('errors', $messageBags['errors']);
|
||||
|
||||
return Redirect::route('repeated.create')->withInput();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user