2016-11-09 21:36:54 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* MonthReportGenerator.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-11-09 21:36:54 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-11-09 21:36:54 +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-11-09 21:36:54 +01:00
|
|
|
*/
|
2018-07-07 07:18:49 +02:00
|
|
|
|
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
|
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Generator\Report\Audit;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2018-07-07 07:18:49 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-11-09 21:36:54 +01:00
|
|
|
use FireflyIII\Generator\Report\ReportGeneratorInterface;
|
2019-05-29 21:52:08 +02:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2016-11-09 21:36:54 +01:00
|
|
|
use FireflyIII\Models\Account;
|
2018-07-07 07:18:49 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-06-06 07:18:09 +02:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2016-11-09 21:36:54 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-04 17:30:06 +02:00
|
|
|
use Log;
|
|
|
|
|
use Throwable;
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class MonthReportGenerator.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
|
class MonthReportGenerator implements ReportGeneratorInterface
|
|
|
|
|
{
|
2018-07-07 07:18:49 +02:00
|
|
|
/** @var Collection The accounts used. */
|
2016-11-09 21:36:54 +01:00
|
|
|
private $accounts;
|
2018-07-07 07:18:49 +02:00
|
|
|
/** @var Carbon End date of the report. */
|
2016-11-09 21:36:54 +01:00
|
|
|
private $end;
|
2018-07-07 07:18:49 +02:00
|
|
|
/** @var Carbon Start date of the report. */
|
2016-11-09 21:36:54 +01:00
|
|
|
private $start;
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Generates the report.
|
|
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return string
|
2018-07-23 21:49:15 +02:00
|
|
|
* @throws FireflyException
|
2018-08-24 16:07:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
|
public function generate(): string
|
|
|
|
|
{
|
|
|
|
|
$auditData = [];
|
|
|
|
|
$dayBefore = clone $this->start;
|
|
|
|
|
$dayBefore->subDay();
|
|
|
|
|
/** @var Account $account */
|
|
|
|
|
foreach ($this->accounts as $account) {
|
|
|
|
|
// balance the day before:
|
2016-12-27 20:07:28 +01:00
|
|
|
$id = $account->id;
|
|
|
|
|
$auditData[$id] = $this->getAuditReport($account, $dayBefore);
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
2016-12-04 18:02:19 +01:00
|
|
|
$defaultShow = ['icon', 'description', 'balance_before', 'amount', 'balance_after', 'date', 'to'];
|
2016-11-28 20:52:56 +01:00
|
|
|
$reportType = 'audit';
|
2018-04-02 14:42:07 +02:00
|
|
|
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
|
2016-11-09 21:36:54 +01:00
|
|
|
$hideable = ['buttons', 'icon', 'description', 'balance_before', 'amount', 'balance_after', 'date',
|
2019-05-29 21:52:08 +02:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
'from', 'to', 'budget', 'category', 'bill',
|
2019-05-29 21:52:08 +02:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
// more new optional fields
|
|
|
|
|
'create_date', 'update_date',
|
|
|
|
|
];
|
2018-08-04 17:30:06 +02:00
|
|
|
try {
|
|
|
|
|
$result = view('reports.audit.report', compact('reportType', 'accountIds', 'auditData', 'hideable', 'defaultShow'))
|
|
|
|
|
->with('start', $this->start)->with('end', $this->end)->with('accounts', $this->accounts)
|
|
|
|
|
->render();
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
Log::error(sprintf('Cannot render reports.audit.report: %s', $e->getMessage()));
|
2019-06-21 19:10:02 +02:00
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
|
$result = sprintf('Could not render report view: %s', $e->getMessage());
|
2018-08-04 17:30:06 +02:00
|
|
|
}
|
2016-12-04 18:02:19 +01:00
|
|
|
|
2018-08-04 17:30:06 +02:00
|
|
|
return $result;
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Account collection setter.
|
|
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param Collection $accounts
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
|
public function setAccounts(Collection $accounts): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->accounts = $accounts;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-08 21:50:20 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Budget collection setter.
|
|
|
|
|
*
|
2016-12-08 21:50:20 +01:00
|
|
|
* @param Collection $budgets
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-12-08 21:50:20 +01:00
|
|
|
*/
|
|
|
|
|
public function setBudgets(Collection $budgets): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 06:23:21 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Category collection setter.
|
|
|
|
|
*
|
2016-11-10 06:23:21 +01:00
|
|
|
* @param Collection $categories
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-11-10 06:23:21 +01:00
|
|
|
*/
|
|
|
|
|
public function setCategories(Collection $categories): ReportGeneratorInterface
|
|
|
|
|
{
|
2016-11-18 20:06:08 +01:00
|
|
|
return $this;
|
2016-11-10 06:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* End date setter.
|
|
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
|
public function setEndDate(Carbon $date): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->end = $date;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-09 21:49:19 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Expenses collection setter.
|
|
|
|
|
*
|
2017-12-09 21:49:19 +01:00
|
|
|
* @param Collection $expense
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2017-12-09 21:49:19 +01:00
|
|
|
*/
|
|
|
|
|
public function setExpense(Collection $expense): ReportGeneratorInterface
|
|
|
|
|
{
|
2019-06-21 19:10:02 +02:00
|
|
|
// doesn't use expense collection.
|
2017-12-09 21:49:19 +01:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Start date collection setter.
|
|
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
|
public function setStartDate(Carbon $date): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
$this->start = $date;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2016-12-27 20:07:28 +01:00
|
|
|
|
2017-02-15 22:02:02 +01:00
|
|
|
/**
|
2018-07-07 07:18:49 +02:00
|
|
|
* Tags collection setter.
|
|
|
|
|
*
|
2017-02-15 22:02:02 +01:00
|
|
|
* @param Collection $tags
|
|
|
|
|
*
|
|
|
|
|
* @return ReportGeneratorInterface
|
2018-08-24 07:18:33 +02:00
|
|
|
* @codeCoverageIgnore
|
2017-02-15 22:02:02 +01:00
|
|
|
*/
|
|
|
|
|
public function setTags(Collection $tags): ReportGeneratorInterface
|
|
|
|
|
{
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2019-06-21 19:10:02 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the audit report.
|
|
|
|
|
*
|
|
|
|
|
* @param Account $account
|
|
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*
|
|
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
|
|
|
|
public function getAuditReport(Account $account, Carbon $date): array
|
|
|
|
|
{
|
|
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
|
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
|
|
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
|
$accountRepository->setUser($account->user);
|
|
|
|
|
|
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
|
$collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end)
|
|
|
|
|
->withAccountInformation();
|
|
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
|
$dayBeforeBalance = app('steam')->balance($account, $date);
|
|
|
|
|
$startBalance = $dayBeforeBalance;
|
|
|
|
|
$currency = $currencyRepos->findNull((int)$accountRepository->getMetaValue($account, 'currency_id'));
|
|
|
|
|
|
|
|
|
|
if (null === $currency) {
|
|
|
|
|
throw new FireflyException('Unexpected NULL value in account currency preference.'); // @codeCoverageIgnore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($journals as $index => $journal) {
|
|
|
|
|
$journals[$index]['balance_before'] = $startBalance;
|
|
|
|
|
$transactionAmount = $journal['amount'];
|
|
|
|
|
|
|
|
|
|
if ($currency->id === $journal['foreign_currency_id']) {
|
|
|
|
|
$transactionAmount = $journal['foreign_amount'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newBalance = bcadd($startBalance, $transactionAmount);
|
|
|
|
|
$journals[$index]['balance_after'] = $newBalance;
|
|
|
|
|
$startBalance = $newBalance;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return = [
|
|
|
|
|
'journals' => $journals,
|
|
|
|
|
'exists' => count($journals) > 0,
|
|
|
|
|
'end' => $this->end->formatLocalized((string)trans('config.month_and_day')),
|
|
|
|
|
'endBalance' => app('steam')->balance($account, $this->end),
|
|
|
|
|
'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')),
|
|
|
|
|
'dayBeforeBalance' => $dayBeforeBalance,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
|
}
|
2016-12-22 19:42:45 +01:00
|
|
|
}
|