2015-02-23 13:25:48 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
2015-05-16 07:14:22 -05:00
|
|
|
use App;
|
2015-02-23 13:25:48 -06:00
|
|
|
use Carbon\Carbon;
|
2015-05-16 06:06:38 -05:00
|
|
|
use FireflyIII\Helpers\Collection\Account as AccountCollection;
|
2015-05-16 06:53:08 -05:00
|
|
|
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
2015-05-16 07:14:22 -05:00
|
|
|
use FireflyIII\Helpers\Collection\BudgetLine;
|
2015-05-16 06:53:08 -05:00
|
|
|
use FireflyIII\Helpers\Collection\Category as CategoryCollection;
|
2015-05-16 06:06:38 -05:00
|
|
|
use FireflyIII\Helpers\Collection\Expense;
|
|
|
|
use FireflyIII\Helpers\Collection\Income;
|
2015-02-23 13:25:48 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2015-05-16 07:14:22 -05:00
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2015-02-23 13:25:48 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ReportHelper
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Report
|
|
|
|
*/
|
|
|
|
class ReportHelper implements ReportHelperInterface
|
|
|
|
{
|
|
|
|
|
2015-05-16 06:06:38 -05:00
|
|
|
/** @var ReportQueryInterface */
|
|
|
|
protected $query;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ReportHelperInterface $helper
|
|
|
|
*/
|
|
|
|
public function __construct(ReportQueryInterface $query)
|
|
|
|
{
|
|
|
|
$this->query = $query;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
/**
|
2015-05-16 06:06:38 -05:00
|
|
|
* This method generates a full report for the given period on all
|
|
|
|
* the users asset and cash accounts.
|
2015-02-23 13:25:48 -06:00
|
|
|
*
|
|
|
|
* @param Carbon $date
|
2015-05-16 06:06:38 -05:00
|
|
|
* @param Carbon $end
|
|
|
|
* @param $shared
|
2015-02-23 13:25:48 -06:00
|
|
|
*
|
2015-05-16 06:06:38 -05:00
|
|
|
* @return Account
|
2015-02-23 13:25:48 -06:00
|
|
|
*/
|
2015-05-16 06:06:38 -05:00
|
|
|
public function getAccountReport(Carbon $date, Carbon $end, $shared)
|
2015-02-23 13:25:48 -06:00
|
|
|
{
|
2015-05-16 06:06:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
$accounts = $this->query->getAllAccounts($date, $end, $shared);
|
|
|
|
$start = 0;
|
|
|
|
$end = 0;
|
|
|
|
$diff = 0;
|
|
|
|
|
|
|
|
// summarize:
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$start += $account->startBalance;
|
|
|
|
$end += $account->endBalance;
|
|
|
|
$diff += ($account->endBalance - $account->startBalance);
|
2015-02-23 13:25:48 -06:00
|
|
|
}
|
|
|
|
|
2015-05-16 06:06:38 -05:00
|
|
|
$object = new AccountCollection;
|
|
|
|
$object->setStart($start);
|
|
|
|
$object->setEnd($end);
|
|
|
|
$object->setDifference($diff);
|
|
|
|
$object->setAccounts($accounts);
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2015-05-16 06:53:08 -05:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param boolean $shared
|
|
|
|
*
|
|
|
|
* @return BudgetCollection
|
|
|
|
*/
|
|
|
|
public function getBudgetReport(Carbon $start, Carbon $end, $shared)
|
|
|
|
{
|
2015-05-16 07:14:22 -05:00
|
|
|
$object = new BudgetCollection;
|
|
|
|
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
|
|
|
|
$repository = App::make('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
|
|
|
$set = $repository->getBudgets();
|
|
|
|
|
|
|
|
foreach ($set as $budget) {
|
|
|
|
|
|
|
|
$repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
|
|
|
|
|
|
|
|
// no repetition(s) for this budget:
|
|
|
|
if ($repetitions->count() == 0) {
|
|
|
|
$spent = $repository->spentInPeriod($budget, $start, $end, $shared);
|
|
|
|
$budgetLine = new BudgetLine;
|
|
|
|
$budgetLine->setBudget($budget);
|
|
|
|
$budgetLine->setOverspent($spent);
|
|
|
|
$object->addOverspent($spent);
|
|
|
|
$object->addBudgetLine($budgetLine);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// one or more repetitions for budget:
|
|
|
|
/** @var LimitRepetition $repetition */
|
|
|
|
foreach ($repetitions as $repetition) {
|
|
|
|
$budgetLine = new BudgetLine;
|
|
|
|
$budgetLine->setBudget($budget);
|
|
|
|
$budgetLine->setRepetition($repetition);
|
|
|
|
$expenses = $repository->spentInPeriod($budget, $repetition->startdate, $repetition->enddate, $shared);
|
|
|
|
$left = $expenses < floatval($repetition->amount) ? floatval($repetition->amount) - $expenses : 0;
|
|
|
|
$spent = $expenses > floatval($repetition->amount) ? 0 : $expenses;
|
|
|
|
$overspent = $expenses > floatval($repetition->amount) ? $expenses - floatval($repetition->amount) : 0;
|
|
|
|
|
|
|
|
$budgetLine->setLeft($left);
|
|
|
|
$budgetLine->setSpent($spent);
|
|
|
|
$budgetLine->setOverspent($overspent);
|
|
|
|
$budgetLine->setBudgeted($repetition->amount);
|
|
|
|
|
|
|
|
$object->addBudgeted($repetition->amount);
|
|
|
|
$object->addSpent($spent);
|
|
|
|
$object->addLeft($left);
|
|
|
|
$object->addOverspent($overspent);
|
|
|
|
$object->addBudgetLine($budgetLine);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// stuff outside of budgets:
|
|
|
|
$noBudget = $repository->getWithoutBudgetSum($start, $end);
|
|
|
|
$budgetLine = new BudgetLine;
|
|
|
|
$budgetLine->setOverspent($noBudget);
|
|
|
|
$object->addOverspent($noBudget);
|
|
|
|
$object->addBudgetLine($budgetLine);
|
|
|
|
|
|
|
|
return $object;
|
2015-05-16 06:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param boolean $shared
|
|
|
|
*
|
|
|
|
* @return CategoryCollection
|
|
|
|
*/
|
|
|
|
public function getCategoryReport(Carbon $start, Carbon $end, $shared)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-05-16 06:06:38 -05:00
|
|
|
/**
|
|
|
|
* Get a full report on the users expenses during the period.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param boolean $shared
|
|
|
|
*
|
|
|
|
* @return Expense
|
|
|
|
*/
|
|
|
|
public function getExpenseReport($start, $end, $shared)
|
|
|
|
{
|
|
|
|
$object = new Expense;
|
|
|
|
$set = $this->query->expenseInPeriod($start, $end, $shared);
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$object->addToTotal($entry->queryAmount);
|
|
|
|
$object->addOrCreateExpense($entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a full report on the users incomes during the period.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param boolean $shared
|
|
|
|
*
|
|
|
|
* @return Income
|
|
|
|
*/
|
|
|
|
public function getIncomeReport($start, $end, $shared)
|
|
|
|
{
|
|
|
|
$object = new Income;
|
|
|
|
$set = $this->query->incomeInPeriod($start, $end, $shared);
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$object->addToTotal($entry->queryAmount);
|
|
|
|
$object->addOrCreateIncome($entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object;
|
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
|
|
|
}
|