Files
firefly-iii/app/Generator/Report/Budget/MonthReportGenerator.php

233 lines
6.5 KiB
PHP
Raw Normal View History

2016-12-09 07:07:53 +01:00
<?php
/**
* MonthReportGenerator.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-12-09 07:07:53 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
2016-12-09 07:07:53 +01:00
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-12-09 07:07:53 +01:00
*/
/** @noinspection MultipleReturnStatementsInspection */
/** @noinspection PhpUndefinedMethodInspection */
declare(strict_types=1);
2016-12-09 07:07:53 +01:00
namespace FireflyIII\Generator\Report\Budget;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
2017-04-29 08:55:50 +02:00
use FireflyIII\Generator\Report\Support;
2016-12-28 11:34:00 +01:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
2017-04-28 20:08:25 +02:00
use FireflyIII\Helpers\Filter\TransferFilter;
2016-12-09 07:07:53 +01:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class MonthReportGenerator.
2016-12-09 07:07:53 +01:00
*/
2017-04-29 08:55:50 +02:00
class MonthReportGenerator extends Support implements ReportGeneratorInterface
2016-12-09 07:07:53 +01:00
{
/** @var Collection The accounts in the report. */
2016-12-09 07:07:53 +01:00
private $accounts;
/** @var Collection The budgets in the report. */
2016-12-09 07:07:53 +01:00
private $budgets;
/** @var Carbon The end date. */
2016-12-09 07:07:53 +01:00
private $end;
/** @var Collection The expenses in the report. */
2016-12-09 07:07:53 +01:00
private $expenses;
/** @var Carbon The start date. */
2016-12-09 07:07:53 +01:00
private $start;
/**
* MonthReportGenerator constructor.
*/
public function __construct()
{
$this->expenses = new Collection;
}
/**
* Generates the report.
*
2016-12-09 07:07:53 +01:00
* @return string
2018-07-23 21:49:15 +02:00
* @throws \Throwable
2016-12-09 07:07:53 +01:00
*/
public function generate(): string
{
2018-04-02 14:42:07 +02:00
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
$budgetIds = implode(',', $this->budgets->pluck('id')->toArray());
$expenses = $this->getExpenses();
$accountSummary = $this->summarizeByAccount($expenses);
$budgetSummary = $this->summarizeByBudget($expenses);
$averageExpenses = $this->getAverages($expenses, SORT_ASC);
$topExpenses = $this->getTopExpenses();
2016-12-09 07:07:53 +01:00
// render!
2016-12-16 08:07:31 +01:00
return view('reports.budget.month', compact('accountIds', 'budgetIds', 'accountSummary', 'budgetSummary', 'averageExpenses', 'topExpenses'))
2016-12-09 07:07:53 +01:00
->with('start', $this->start)->with('end', $this->end)
->with('budgets', $this->budgets)
->with('accounts', $this->accounts)
->render();
}
/**
* Set the involved accounts.
*
2016-12-09 07:07:53 +01:00
* @param Collection $accounts
*
* @return ReportGeneratorInterface
*/
public function setAccounts(Collection $accounts): ReportGeneratorInterface
{
$this->accounts = $accounts;
return $this;
}
/**
* Set the involved budgets.
*
2016-12-09 07:07:53 +01:00
* @param Collection $budgets
*
* @return ReportGeneratorInterface
*/
public function setBudgets(Collection $budgets): ReportGeneratorInterface
{
$this->budgets = $budgets;
return $this;
}
/**
* Unused category setter.
*
2016-12-09 07:07:53 +01:00
* @param Collection $categories
*
* @return ReportGeneratorInterface
*/
public function setCategories(Collection $categories): ReportGeneratorInterface
{
return $this;
}
/**
* Set the end date of the report.
*
2016-12-09 07:07:53 +01:00
* @param Carbon $date
*
* @return ReportGeneratorInterface
*/
public function setEndDate(Carbon $date): ReportGeneratorInterface
{
$this->end = $date;
return $this;
2017-12-09 21:49:19 +01:00
}
/**
* Unused expense setter.
*
2017-12-09 21:49:19 +01:00
* @param Collection $expense
*
* @return ReportGeneratorInterface
*/
public function setExpense(Collection $expense): ReportGeneratorInterface
{
return $this;
2016-12-09 07:07:53 +01:00
}
/**
* Set the start date of the report.
*
2016-12-09 07:07:53 +01:00
* @param Carbon $date
*
* @return ReportGeneratorInterface
*/
public function setStartDate(Carbon $date): ReportGeneratorInterface
{
$this->start = $date;
return $this;
}
2017-02-15 20:07:10 +01:00
/**
* Unused tags setter.
*
2017-02-15 20:07:10 +01:00
* @param Collection $tags
*
* @return ReportGeneratorInterface
*/
public function setTags(Collection $tags): ReportGeneratorInterface
{
return $this;
}
2016-12-09 07:07:53 +01:00
/**
* Get the expenses.
*
2016-12-09 07:07:53 +01:00
* @return Collection
*/
2017-02-24 20:01:35 +01:00
protected function getExpenses(): Collection
2016-12-09 07:07:53 +01:00
{
if ($this->expenses->count() > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
2016-12-28 11:34:00 +01:00
/** @var JournalCollectorInterface $collector */
2017-02-05 16:16:15 +01:00
$collector = app(JournalCollectorInterface::class);
2016-12-09 07:07:53 +01:00
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL])
2017-04-28 20:08:25 +02:00
->setBudgets($this->budgets)->withOpposingAccount();
$collector->removeFilter(TransferFilter::class);
2016-12-09 07:07:53 +01:00
$collector->addFilter(OpposingAccountFilter::class);
$collector->addFilter(PositiveAmountFilter::class);
2016-12-09 07:07:53 +01:00
$transactions = $collector->getJournals();
$this->expenses = $transactions;
return $transactions;
}
/**
* Summarize a collection by its budget.
*
2016-12-09 07:07:53 +01:00
* @param Collection $collection
*
* @return array
*/
private function summarizeByBudget(Collection $collection): array
2016-12-09 07:07:53 +01:00
{
2018-01-12 21:08:59 +01:00
$result = [
'sum' => '0',
];
2016-12-09 07:07:53 +01:00
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
2018-04-02 14:42:07 +02:00
$jrnlBudId = (int)$transaction->transaction_journal_budget_id;
$transBudId = (int)$transaction->transaction_budget_id;
$budgetId = max($jrnlBudId, $transBudId);
$result[$budgetId] = $result[$budgetId] ?? '0';
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
2018-01-12 21:08:59 +01:00
$result['sum'] = bcadd($result['sum'], $transaction->transaction_amount);
2016-12-09 07:07:53 +01:00
}
return $result;
}
}