firefly-iii/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php

131 lines
4.3 KiB
PHP
Raw Normal View History

2014-07-15 00:08:13 -05:00
<?php
namespace Firefly\Storage\Budget;
class EloquentBudgetRepository implements BudgetRepositoryInterface
{
public function getAsSelectList()
{
$list = \Auth::user()->budgets()->with(
['limits', 'limits.limitrepetitions']
)->orderBy('name', 'ASC')->get();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
return $return;
}
2014-07-23 09:44:20 -05:00
public function getWithRepetitionsInPeriod(\Carbon\Carbon $date, $range)
{
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = \App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$dates = $toolkit->getDateRangeDates();
2014-07-23 09:44:20 -05:00
$start = $dates[0];
$result = [];
$set = \Auth::user()->budgets()->with(
['limits' => function ($q) use ($date) {
$q->orderBy('limits.startdate', 'ASC');
// $q->where('startdate',$date->format('Y-m-d'));
}, 'limits.limitrepetitions' => function ($q) use ($date) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
$q->where('startdate', $date->format('Y-m-d'));
2014-07-23 09:44:20 -05:00
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
$budget->count = 0;
foreach ($budget->limits as $limit) {
/** @var $rep \LimitRepetition */
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
// overspent:
if ($rep->left < 0) {
$rep->spent = ($rep->left * -1) + $rep->amount;
$rep->overspent = $rep->left * -1;
$total = $rep->spent + $rep->overspent;
$rep->spent_pct = round(($rep->spent / $total) * 100);
$rep->overspent_pct = 100 - $rep->spent_pct;
} else {
$rep->spent = $rep->amount - $rep->left;
$rep->spent_pct = round(($rep->spent / $rep->amount) * 100);
$rep->left_pct = 100 - $rep->spent_pct;
}
}
2014-07-23 09:44:20 -05:00
$budget->count += count($limit->limitrepetitions);
}
}
return $set;
}
2014-07-22 23:57:51 -05:00
public function store($data)
{
$budget = new \Budget;
$budget->name = $data['name'];
$budget->user()->associate(\Auth::user());
$budget->save();
// if limit, create limit (repetition itself will be picked up elsewhere).
if ($data['amount'] > 0) {
$limit = new \Limit;
$limit->budget()->associate($budget);
$startDate = new \Carbon\Carbon;
switch ($data['repeat_freq']) {
case 'daily':
$startDate->startOfDay();
break;
case 'weekly':
$startDate->startOfWeek();
break;
case 'monthly':
$startDate->startOfMonth();
break;
case 'quarterly':
$startDate->firstOfQuarter();
break;
case 'half-year':
$startDate->startOfYear();
if (intval($startDate->format('m')) >= 7) {
$startDate->addMonths(6);
}
break;
case 'yearly':
$startDate->startOfYear();
break;
}
$limit->startdate = $startDate;
$limit->amount = $data['amount'];
$limit->repeats = $data['repeats'];
$limit->repeat_freq = $data['repeat_freq'];
$limit->save();
}
return $budget;
}
public function get()
{
return \Auth::user()->budgets()->with(
2014-07-23 09:44:20 -05:00
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) {
2014-07-23 09:44:20 -05:00
$q->orderBy('limit_repetitions.startdate', 'ASC');
}]
)->orderBy('name', 'ASC')->get();
}
public function find($id)
{
return \Auth::user()->budgets()->find($id);
}
2014-07-15 00:08:13 -05:00
}