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

159 lines
5.7 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php
2022-12-29 12:41:57 -06:00
/**
* ReportHelper.php
2020-01-28 01:46:01 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-23 13:25:48 -06:00
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2019-06-21 12:10:02 -05:00
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Models\Bill;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2016-01-15 14:50:57 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
2019-02-13 10:38:41 -06:00
2015-02-23 13:25:48 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class ReportHelper.
2018-08-30 13:58:07 -05:00
*
2015-02-23 13:25:48 -06:00
*/
class ReportHelper implements ReportHelperInterface
{
/** @var BudgetRepositoryInterface The budget repository */
2016-01-15 14:50:57 -06:00
protected $budgetRepository;
2015-05-16 06:06:38 -05:00
/**
2016-01-15 15:13:38 -06:00
* ReportHelper constructor.
*
2023-06-21 05:34:58 -05:00
* @param BudgetRepositoryInterface $budgetRepository
2015-05-16 06:06:38 -05:00
*/
public function __construct(BudgetRepositoryInterface $budgetRepository)
2015-05-16 06:06:38 -05:00
{
2016-01-15 14:50:57 -06:00
$this->budgetRepository = $budgetRepository;
2015-05-16 06:06:38 -05:00
}
/**
2016-01-19 06:59:54 -06:00
* This method generates a full report for the given period on all
* the users bills and their payments.
*
* Excludes bills which have not had a payment on the mentioned accounts.
*
2023-06-21 05:34:58 -05:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
2019-08-16 14:21:38 -05:00
* @return array
*/
2019-08-16 14:21:38 -05:00
public function getBillReport(Collection $accounts, Carbon $start, Carbon $end): array
{
2016-05-02 13:49:19 -05:00
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
2016-01-19 06:59:54 -06:00
$bills = $repository->getBillsForAccounts($accounts);
2019-08-16 14:21:38 -05:00
$report = [
'bills' => [],
];
/** @var Bill $bill */
foreach ($bills as $bill) {
$expectedDates = $repository->getPayDatesInRange($bill, $start, $end);
$billId = $bill->id;
$currency = $bill->transactionCurrency;
$current = [
'id' => $bill->id,
'name' => $bill->name,
'active' => $bill->active,
'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max,
'currency_id' => $bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'expected_dates' => $expectedDates->toArray(),
'paid_moments' => [],
];
2021-04-26 23:42:07 -05:00
/** @var Carbon $expectedStart */
2019-08-16 14:21:38 -05:00
foreach ($expectedDates as $expectedStart) {
$expectedEnd = app('navigation')->endOfX($expectedStart, $bill->repeat_freq, null);
// is paid in this period maybe?
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($expectedStart, $expectedEnd)->setBill($bill);
$current['paid_moments'][] = $collector->getExtractedJournals();
}
// append to report:
$report['bills'][$billId] = $current;
}
return $report;
}
2016-01-19 06:59:54 -06:00
/**
* Generate a list of months for the report.
*
2023-06-21 05:34:58 -05:00
* @param Carbon $date
2016-01-19 06:59:54 -06:00
*
* @return array
*/
2016-04-06 09:37:28 -05:00
public function listOfMonths(Carbon $date): array
2016-01-19 06:59:54 -06:00
{
/** @var FiscalHelperInterface $fiscalHelper */
2016-05-02 13:49:19 -05:00
$fiscalHelper = app(FiscalHelperInterface::class);
$start = clone $date;
2016-02-05 21:32:30 -06:00
$start->startOfMonth();
2023-02-11 00:36:45 -06:00
$end = today(config('app.timezone'));
2016-02-05 21:32:30 -06:00
$end->endOfMonth();
$months = [];
2016-01-19 06:59:54 -06:00
while ($start <= $end) {
2016-04-27 12:21:47 -05:00
$year = $fiscalHelper->endOfFiscalYear($start)->year; // current year
2021-04-07 00:28:43 -05:00
if (!array_key_exists($year, $months)) {
2016-01-19 06:59:54 -06:00
$months[$year] = [
'fiscal_start' => $fiscalHelper->startOfFiscalYear($start)->format('Y-m-d'),
'fiscal_end' => $fiscalHelper->endOfFiscalYear($start)->format('Y-m-d'),
'start' => Carbon::createFromDate($year, 1, 1)->format('Y-m-d'),
'end' => Carbon::createFromDate($year, 12, 31)->format('Y-m-d'),
'months' => [],
2016-01-19 06:59:54 -06:00
];
}
2016-01-19 06:59:54 -06:00
$currentEnd = clone $start;
$currentEnd->endOfMonth();
$months[$year]['months'][] = [
2022-12-29 12:41:57 -06:00
'formatted' => $start->isoFormat((string)trans('config.month_js')),
2016-01-19 06:59:54 -06:00
'start' => $start->format('Y-m-d'),
'end' => $currentEnd->format('Y-m-d'),
'month' => $start->month,
'year' => $year,
];
2016-02-05 21:32:30 -06:00
2016-04-27 12:21:47 -05:00
$start = clone $currentEnd; // to make the hop to the next month properly
2016-02-05 21:32:30 -06:00
$start->addDay();
}
2016-01-19 06:59:54 -06:00
return $months;
}
}