firefly-iii/app/Helpers/Report/ReportHelper.php

313 lines
9.5 KiB
PHP
Raw Normal View History

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 07:51:23 -05:00
use FireflyIII\Helpers\Collection\Balance;
use FireflyIII\Helpers\Collection\BalanceEntry;
use FireflyIII\Helpers\Collection\BalanceHeader;
use FireflyIII\Helpers\Collection\BalanceLine;
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:51:23 -05:00
use FireflyIII\Models\Budget as BudgetModel;
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 07:51:23 -05:00
/**
*
* The balance report contains a Balance object which in turn contains:
*
* A BalanceHeader object which contains all relevant user asset accounts for the report.
*
* A number of BalanceLine objects, which hold:
* - A budget
* - A number of BalanceEntry objects.
*
* The BalanceEntry object holds:
* - The same budget (again)
* - A user asset account as mentioned in the BalanceHeader
* - The amount of money spent on the budget by the user asset account
*
* @param Carbon $start
* @param Carbon $end
* @param boolean $shared
*
* @return Balance
*/
public function getBalanceReport(Carbon $start, Carbon $end, $shared)
{
$repository = App::make('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$balance = new Balance;
// build a balance header:
$header = new BalanceHeader;
$accounts = $this->query->getAllAccounts($start, $end, $shared);
$budgets = $repository->getBudgets();
foreach ($accounts as $account) {
$header->addAccount($account);
}
2015-05-16 08:43:58 -05:00
/** @var BudgetModel $budget */
2015-05-16 07:51:23 -05:00
foreach ($budgets as $budget) {
$line = new BalanceLine;
$line->setBudget($budget);
2015-05-16 08:43:58 -05:00
// get budget amount for current period:
2015-05-16 08:52:09 -05:00
$rep = $repository->getCurrentRepetition($budget, $start);
if ($rep) {
2015-05-16 08:43:58 -05:00
$line->setBudgetAmount($rep->amount);
}
2015-05-16 07:51:23 -05:00
// loop accounts:
foreach ($accounts as $account) {
$balanceEntry = new BalanceEntry;
$balanceEntry->setAccount($account);
2015-05-16 08:43:58 -05:00
// get spent:
$spent = $this->query->spentInBudget($account, $budget, $start, $end, $shared); // I think shared is irrelevant.
$balanceEntry->setSpent($spent);
2015-05-16 07:51:23 -05:00
$line->addBalanceEntry($balanceEntry);
}
// add line to balance:
$balance->addBalanceLine($line);
}
2015-05-16 08:43:58 -05:00
// then a new line for without budget.
2015-05-16 09:47:52 -05:00
$empty = new BalanceLine;
foreach ($accounts as $account) {
$spent = $this->query->spentNoBudget($account, $start, $end);
$balanceEntry = new BalanceEntry;
$balanceEntry->setAccount($account);
$balanceEntry->setSpent($spent);
$empty->addBalanceEntry($balanceEntry);
}
$balance->addBalanceLine($empty);
2015-05-16 08:43:58 -05:00
2015-05-16 07:51:23 -05:00
$balance->setBalanceHeader($header);
return $balance;
}
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)
{
2015-05-16 07:51:23 -05:00
$object = new CategoryCollection;
/**
* GET CATEGORIES:
*/
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
$repository = App::make('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$set = $repository->getCategories();
foreach ($set as $category) {
$spent = $repository->spentInPeriod($category, $start, $end, $shared);
$category->spent = $spent;
$object->addCategory($category);
$object->addTotal($spent);
}
return $object;
2015-05-16 06:53:08 -05:00
}
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
}