2015-02-23 13:25:48 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
2015-03-29 05:25:46 -05:00
|
|
|
use App;
|
2015-02-23 13:25:48 -06:00
|
|
|
use Auth;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
use Illuminate\Support\Collection;
|
2015-03-10 13:58:05 -05:00
|
|
|
use Steam;
|
2015-02-23 13:25:48 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ReportHelper
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Report
|
|
|
|
*/
|
|
|
|
class ReportHelper implements ReportHelperInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method gets some kind of list for a monthly overview.
|
|
|
|
*
|
|
|
|
* @param Carbon $date
|
2015-05-15 13:07:51 -05:00
|
|
|
* @param bool $includeShared
|
2015-02-23 13:25:48 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2015-05-15 13:07:51 -05:00
|
|
|
public function getBudgetsForMonth(Carbon $date, $includeShared = false)
|
2015-02-23 13:25:48 -06:00
|
|
|
{
|
2015-03-29 05:25:46 -05:00
|
|
|
/** @var \FireflyIII\Helpers\Report\ReportQueryInterface $query */
|
|
|
|
$query = App::make('FireflyIII\Helpers\Report\ReportQueryInterface');
|
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
$start = clone $date;
|
|
|
|
$start->startOfMonth();
|
|
|
|
$end = clone $date;
|
|
|
|
$end->endOfMonth();
|
2015-03-29 05:25:46 -05:00
|
|
|
$set = Auth::user()->budgets()->orderBy('budgets.name', 'ASC')
|
2015-03-27 13:34:37 -05:00
|
|
|
->leftJoin(
|
|
|
|
'budget_limits', function (JoinClause $join) use ($date) {
|
|
|
|
$join->on('budget_limits.budget_id', '=', 'budgets.id')->where('budget_limits.startdate', '=', $date->format('Y-m-d'));
|
|
|
|
}
|
|
|
|
)
|
2015-04-09 14:14:15 -05:00
|
|
|
->get(['budgets.*', 'budget_limits.amount as queryAmount']);
|
2015-02-23 13:25:48 -06:00
|
|
|
|
2015-04-09 14:14:15 -05:00
|
|
|
$budgets = Steam::makeArray($set);
|
2015-05-15 13:07:51 -05:00
|
|
|
$amountSet = $query->journalsByBudget($start, $end, $includeShared);
|
2015-04-09 14:14:15 -05:00
|
|
|
$amounts = Steam::makeArray($amountSet);
|
|
|
|
$budgets = Steam::mergeArrays($budgets, $amounts);
|
|
|
|
$budgets[0]['spent'] = isset($budgets[0]['spent']) ? $budgets[0]['spent'] : 0.0;
|
|
|
|
$budgets[0]['queryAmount'] = isset($budgets[0]['queryAmount']) ? $budgets[0]['queryAmount'] : 0.0;
|
|
|
|
$budgets[0]['name'] = 'No budget';
|
2015-02-23 13:25:48 -06:00
|
|
|
|
2015-03-29 05:25:46 -05:00
|
|
|
// find transactions to shared asset accounts, which are without a budget by default:
|
|
|
|
// which is only relevant when shared asset accounts are hidden.
|
2015-05-15 13:07:51 -05:00
|
|
|
if ($includeShared === false) {
|
2015-04-11 05:36:17 -05:00
|
|
|
$transfers = $query->sharedExpenses($start, $end)->sum('queryAmount');
|
|
|
|
$budgets[0]['spent'] += floatval($transfers) * -1;
|
2015-02-23 13:25:48 -06:00
|
|
|
}
|
|
|
|
|
2015-03-29 05:25:46 -05:00
|
|
|
return $budgets;
|
2015-02-23 13:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listOfMonths(Carbon $date)
|
|
|
|
{
|
2015-05-14 06:41:21 -05:00
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
$start = clone $date;
|
|
|
|
$end = Carbon::now();
|
|
|
|
$months = [];
|
|
|
|
while ($start <= $end) {
|
2015-05-05 03:30:39 -05:00
|
|
|
$year = $start->year;
|
2015-03-02 04:54:20 -06:00
|
|
|
$months[$year][] = [
|
2015-05-14 06:41:21 -05:00
|
|
|
'formatted' => $start->formatLocalized('%B %Y'),
|
2015-05-05 03:30:39 -05:00
|
|
|
'month' => $start->month,
|
|
|
|
'year' => $year,
|
2015-02-23 13:25:48 -06:00
|
|
|
];
|
|
|
|
$start->addMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $months;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|