2015-02-23 13:25:48 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-05-16 06:06:38 -05:00
|
|
|
use FireflyIII\Helpers\Collection\Account as AccountCollection;
|
|
|
|
use FireflyIII\Helpers\Collection\Expense;
|
|
|
|
use FireflyIII\Helpers\Collection\Income;
|
2015-02-23 13:25:48 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|