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

176 lines
5.8 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php
/**
* ReportHelper.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.
*/
2016-02-05 05:08:25 -06:00
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;
2015-05-16 06:53:08 -05:00
use FireflyIII\Helpers\Collection\Category as CategoryCollection;
2016-11-20 05:08:43 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill;
2016-04-26 02:21:57 -05:00
use FireflyIII\Models\Category;
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;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
2015-02-23 13:25:48 -06:00
/**
* Class ReportHelper
*
* @package FireflyIII\Helpers\Report
*/
class ReportHelper implements ReportHelperInterface
{
2016-01-15 14:50:57 -06:00
/** @var BudgetRepositoryInterface */
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);
2016-11-20 05:08:43 -06:00
$collector = app(JournalCollectorInterface::class, [auth()->user()]);
$collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills);
$journals = $collector->getJournals();
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) {
$billLine = new BillLine;
$billLine->setBill($bill);
$billLine->setMin(strval($bill->amount_min));
$billLine->setMax(strval($bill->amount_max));
2016-04-27 12:21:47 -05:00
$billLine->setHit(false);
2016-01-19 06:59:54 -06:00
$entry = $journals->filter(
function (Transaction $transaction) use ($bill) {
return $transaction->bill_id === $bill->id;
2016-01-19 06:59:54 -06:00
}
);
$first = $entry->first();
if (!is_null($first)) {
$billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount);
2016-11-20 05:08:43 -06:00
$billLine->setLastHitDate($first->date);
2016-01-19 06:59:54 -06:00
$billLine->setHit(true);
}
2016-10-24 11:01:15 -05:00
if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine);
}
}
2016-11-20 05:08:43 -06:00
$collection->filterBills();
2016-01-19 06:59:54 -06:00
return $collection;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
2016-01-19 06:59:54 -06:00
* @return CategoryCollection
*/
2016-12-06 01:59:08 -06:00
public function getCategoryReport(Collection $accounts, Carbon $start, Carbon $end): CategoryCollection
{
2016-01-19 06:59:54 -06:00
$object = new CategoryCollection;
/** @var CategoryRepositoryInterface $repository */
2016-05-02 13:49:19 -05:00
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
// CategoryCollection expects the amount in $spent:
$category->spent = $spent;
2016-01-19 06:59:54 -06:00
$object->addCategory($category);
}
2016-01-19 06:59:54 -06:00
return $object;
}
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;
}
2016-01-01 06:54:23 -06:00
}