2016-12-09 07:07:53 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* MonthReportGenerator.php
|
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
|
*
|
|
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
|
*
|
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-09 07:44:22 +02:00
|
|
|
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;
|
2016-12-28 11:34:00 +01:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2017-04-29 08:22:56 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class MonthReportGenerator
|
|
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Generator\Report\Budget
|
|
|
|
|
*/
|
2017-04-29 08:22:56 +02:00
|
|
|
class MonthReportGenerator implements ReportGeneratorInterface
|
2016-12-09 07:07:53 +01:00
|
|
|
{
|
|
|
|
|
/** @var Collection */
|
|
|
|
|
private $accounts;
|
|
|
|
|
/** @var Collection */
|
|
|
|
|
private $budgets;
|
|
|
|
|
/** @var Carbon */
|
|
|
|
|
private $end;
|
|
|
|
|
/** @var Collection */
|
|
|
|
|
private $expenses;
|
|
|
|
|
/** @var Collection */
|
|
|
|
|
private $income;
|
|
|
|
|
/** @var Carbon */
|
|
|
|
|
private $start;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MonthReportGenerator constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->income = new Collection;
|
|
|
|
|
$this->expenses = new Collection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function generate(): string
|
|
|
|
|
{
|
2016-12-10 06:50:13 +01:00
|
|
|
$accountIds = join(',', $this->accounts->pluck('id')->toArray());
|
2016-12-16 08:07:31 +01:00
|
|
|
$budgetIds = join(',', $this->budgets->pluck('id')->toArray());
|
2016-12-10 06:50:13 +01:00
|
|
|
$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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Collection $accounts
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setAccounts(Collection $accounts): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->accounts = $accounts;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Collection $budgets
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setBudgets(Collection $budgets): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->budgets = $budgets;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Collection $categories
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setCategories(Collection $categories): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setEndDate(Carbon $date): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->end = $date;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setStartDate(Carbon $date): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->start = $date;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* @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)
|
2016-12-10 06:50:13 +01:00
|
|
|
->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
|
|
|
|
2017-04-29 08:22:56 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Collection $collection
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2016-12-10 06:50:13 +01:00
|
|
|
private function summarizeByBudget(Collection $collection): array
|
2016-12-09 07:07:53 +01:00
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
|
foreach ($collection as $transaction) {
|
2016-12-10 06:50:13 +01:00
|
|
|
$jrnlBudId = intval($transaction->transaction_journal_budget_id);
|
|
|
|
|
$transBudId = intval($transaction->transaction_budget_id);
|
|
|
|
|
$budgetId = max($jrnlBudId, $transBudId);
|
|
|
|
|
$result[$budgetId] = $result[$budgetId] ?? '0';
|
|
|
|
|
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
|
2016-12-09 07:07:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2016-12-22 19:42:45 +01:00
|
|
|
}
|