2015-02-23 13:25:48 -06:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* ReportHelper.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* 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-05-20 05:27:31 -05:00
|
|
|
*/
|
|
|
|
|
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;
|
2015-05-17 10:54:13 -05:00
|
|
|
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;
|
2016-01-29 10:08:06 -06:00
|
|
|
use FireflyIII\Helpers\FiscalHelperInterface;
|
2015-05-17 10:54:13 -05:00
|
|
|
use FireflyIII\Models\Bill;
|
2016-04-26 02:21:57 -05:00
|
|
|
use FireflyIII\Models\Category;
|
2016-11-05 05:24:15 -05:00
|
|
|
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;
|
2015-12-06 06:11:43 -06:00
|
|
|
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
|
|
|
*/
|
2016-11-09 23:23:21 -06: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
|
|
|
}
|
|
|
|
|
2015-12-11 09:36:40 -06: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.
|
2015-12-11 09:36:40 -06:00
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
2016-01-19 06:59:54 -06:00
|
|
|
* @return BillCollection
|
2015-12-11 09:36:40 -06:00
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function getBillReport(Carbon $start, Carbon $end, Collection $accounts): BillCollection
|
2015-12-11 09:36:40 -06:00
|
|
|
{
|
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()]);
|
2016-11-05 05:24:15 -05:00
|
|
|
$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);
|
2016-08-26 02:30:52 -05:00
|
|
|
$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(
|
2016-11-05 05:24:15 -05:00
|
|
|
function (Transaction $transaction) use ($bill) {
|
|
|
|
return $transaction->bill_id === $bill->id;
|
2016-01-19 06:59:54 -06:00
|
|
|
}
|
|
|
|
);
|
2016-02-06 13:17:55 -06:00
|
|
|
$first = $entry->first();
|
|
|
|
if (!is_null($first)) {
|
|
|
|
$billLine->setTransactionJournalId($first->id);
|
2016-11-05 05:24:15 -05:00
|
|
|
$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()) {
|
2016-02-04 01:55:06 -06:00
|
|
|
$collection->addBill($billLine);
|
|
|
|
}
|
2015-12-11 09:36:40 -06:00
|
|
|
}
|
2016-11-20 05:08:43 -06:00
|
|
|
$collection->filterBills();
|
|
|
|
|
2016-01-19 06:59:54 -06:00
|
|
|
return $collection;
|
2015-12-11 09:36:40 -06:00
|
|
|
}
|
2015-12-11 10:53:17 -06:00
|
|
|
|
2015-12-11 11:45:39 -06:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
2016-01-19 06:59:54 -06:00
|
|
|
* @return CategoryCollection
|
2015-12-11 11:45:39 -06:00
|
|
|
*/
|
2016-12-06 01:59:08 -06:00
|
|
|
public function getCategoryReport(Collection $accounts, Carbon $start, Carbon $end): CategoryCollection
|
2015-12-11 11:45:39 -06:00
|
|
|
{
|
2016-01-19 06:59:54 -06:00
|
|
|
$object = new CategoryCollection;
|
2016-05-08 06:45:23 -05:00
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
2016-05-02 13:49:19 -05:00
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
2016-05-08 06:45:23 -05:00
|
|
|
$categories = $repository->getCategories();
|
2015-12-11 11:45:39 -06:00
|
|
|
|
2016-05-08 06:45:23 -05:00
|
|
|
/** @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);
|
2015-12-11 11:45:39 -06:00
|
|
|
}
|
|
|
|
|
2016-01-19 06:59:54 -06:00
|
|
|
return $object;
|
|
|
|
}
|
2015-12-11 11:45:39 -06:00
|
|
|
|
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
|
|
|
{
|
2016-01-29 10:08:06 -06:00
|
|
|
/** @var FiscalHelperInterface $fiscalHelper */
|
2016-05-02 13:49:19 -05:00
|
|
|
$fiscalHelper = app(FiscalHelperInterface::class);
|
2016-01-29 10:09:23 -06:00
|
|
|
$start = clone $date;
|
2016-02-05 21:32:30 -06:00
|
|
|
$start->startOfMonth();
|
|
|
|
$end = Carbon::now();
|
|
|
|
$end->endOfMonth();
|
|
|
|
$months = [];
|
2015-12-12 03:33:19 -06:00
|
|
|
|
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] = [
|
2016-01-29 10:13:54 -06:00
|
|
|
'fiscal_start' => $fiscalHelper->startOfFiscalYear($start)->format('Y-m-d'),
|
|
|
|
'fiscal_end' => $fiscalHelper->endOfFiscalYear($start)->format('Y-m-d'),
|
2016-01-29 10:09:23 -06:00
|
|
|
'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
|
|
|
];
|
|
|
|
}
|
2015-12-12 03:33:19 -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();
|
2015-12-12 03:33:19 -06:00
|
|
|
}
|
|
|
|
|
2016-01-19 06:59:54 -06:00
|
|
|
return $months;
|
2015-12-12 03:33:19 -06:00
|
|
|
}
|
2016-01-01 06:54:23 -06:00
|
|
|
|
2016-01-09 01:20:55 -06:00
|
|
|
}
|