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

162 lines
5.7 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php
/**
* ReportHelper.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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 07:41:58 -06:00
* along with Firefly III. If not, see <http://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\Collection\Bill as BillCollection;
use FireflyIII\Helpers\Collection\BillLine;
2016-11-20 05:08:43 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
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;
2015-02-23 13:25:48 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class ReportHelper.
2015-02-23 13:25:48 -06:00
*/
class ReportHelper implements ReportHelperInterface
{
2017-11-15 05:25:49 -06:00
/** @var BudgetRepositoryInterface */
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.
2015-05-23 01:51:24 -05:00
*
2015-05-17 02:18:44 -05:00
*
2016-01-15 15:13:38 -06: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.
*
2016-12-28 09:45:44 -06:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
*
2016-01-19 06:59:54 -06:00
* Excludes bills which have not had a payment on the mentioned accounts.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
2016-01-19 06:59:54 -06:00
* @return BillCollection
*/
2016-04-06 09:37:28 -05:00
public function getBillReport(Carbon $start, Carbon $end, Collection $accounts): BillCollection
{
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);
2017-12-02 00:10:36 -06:00
2016-01-19 06:59:54 -06:00
$collection = new BillCollection;
2016-11-20 05:08:43 -06:00
$collection->setStartDate($start);
$collection->setEndDate($end);
2016-01-19 06:59:54 -06:00
/** @var Bill $bill */
foreach ($bills as $bill) {
2017-12-02 00:10:36 -06:00
$expectedDates = $repository->getPayDatesInRange($bill, $start, $end);
2017-12-22 11:32:43 -06:00
foreach ($expectedDates as $payDate) {
2017-12-02 00:10:36 -06:00
$endOfPayPeriod = app('navigation')->endOfX($payDate, $bill->repeat_freq, null);
2017-12-22 11:32:43 -06:00
$collector = app(JournalCollectorInterface::class);
2017-12-02 00:10:36 -06:00
$collector->setAccounts($accounts)->setRange($payDate, $endOfPayPeriod)->setBills($bills);
2017-12-22 11:32:43 -06:00
$journals = $collector->getJournals();
2017-12-02 00:10:36 -06:00
$billLine = new BillLine;
$billLine->setBill($bill);
$billLine->setPayDate($payDate);
$billLine->setEndOfPayDate($endOfPayPeriod);
2018-04-02 07:42:07 -05:00
$billLine->setMin((string)$bill->amount_min);
$billLine->setMax((string)$bill->amount_max);
2017-12-02 00:10:36 -06:00
$billLine->setHit(false);
$entry = $journals->filter(
function (Transaction $transaction) use ($bill) {
return $transaction->bill_id === $bill->id;
}
);
$first = $entry->first();
if (null !== $first) {
$billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount);
$billLine->setLastHitDate($first->date);
$billLine->setHit(true);
}
if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine);
2016-01-19 06:59:54 -06:00
}
}
}
2016-11-20 05:08:43 -06:00
$collection->filterBills();
2016-01-19 06:59:54 -06:00
return $collection;
}
2016-01-19 06:59:54 -06:00
/**
* @param Carbon $date
*
* @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();
$end = Carbon::now();
$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
2016-01-19 06:59:54 -06:00
if (!isset($months[$year])) {
$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'][] = [
'formatted' => $start->formatLocalized('%B %Y'),
'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;
}
}