mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-10 12:24:50 +00:00
New event to create budget limits, new handler to handle said event, new routes for budget control, new routes for limit control, extended migration, extended models, extended JS. [skip-ci]
This commit is contained in:
84
app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php
Normal file
84
app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sander
|
||||
* Date: 20/07/14
|
||||
* Time: 13:43
|
||||
*/
|
||||
|
||||
namespace Firefly\Storage\Limit;
|
||||
|
||||
|
||||
class EloquentLimitRepository implements LimitRepositoryInterface
|
||||
{
|
||||
public function store($data)
|
||||
{
|
||||
$budget = \Budget::find($data['budget_id']);
|
||||
if (is_null($budget)) {
|
||||
\Session::flash('error', 'No such budget.');
|
||||
return new \Limit;
|
||||
}
|
||||
// set the date to the correct start period:
|
||||
$date = new \Carbon\Carbon($data['startdate']);
|
||||
switch ($data['period']) {
|
||||
case 'daily':
|
||||
$date->startOfDay();
|
||||
break;
|
||||
case 'weekly':
|
||||
$date->startOfWeek();
|
||||
break;
|
||||
case 'monthly':
|
||||
$date->startOfMonth();
|
||||
break;
|
||||
case 'quarterly':
|
||||
$date->firstOfQuarter();
|
||||
break;
|
||||
case 'half-year':
|
||||
|
||||
if (intval($date->format('m')) >= 7) {
|
||||
$date->startOfYear();
|
||||
$date->addMonths(6);
|
||||
} else {
|
||||
$date->startOfYear();
|
||||
}
|
||||
break;
|
||||
case 'yearly':
|
||||
$date->startOfYear();
|
||||
break;
|
||||
}
|
||||
// find existing:
|
||||
$count = \Limit::
|
||||
leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
|
||||
'components.user_id', \Auth::user()->id
|
||||
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
|
||||
'repeat_freq', $data['period']
|
||||
)->count();
|
||||
if ($count > 0) {
|
||||
\Session::flash('error', 'There already is an entry for these parameters.');
|
||||
return new \Limit;
|
||||
}
|
||||
// create new limit:
|
||||
$limit = new \Limit;
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->startdate = $date;
|
||||
$limit->amount = floatval($data['amount']);
|
||||
$limit->repeats = isset($data['repeats']) ? intval($data['repeats']) : 0;
|
||||
$limit->repeat_freq = $data['period'];
|
||||
if (!$limit->save()) {
|
||||
Session::flash('error', 'Could not save: ' . $limit->errors()->first());
|
||||
}
|
||||
return $limit;
|
||||
}
|
||||
|
||||
public function getTJByBudgetAndDateRange(\Budget $budget, \Carbon\Carbon $start, \Carbon\Carbon $end)
|
||||
{
|
||||
$type = \TransactionType::where('type', 'Withdrawal')->first();
|
||||
|
||||
$result = $budget->transactionjournals()->after($start)->
|
||||
before($end)->get();
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user