Files
firefly-iii/app/Transformers/BillTransformer.php

231 lines
7.5 KiB
PHP
Raw Normal View History

2018-02-06 07:49:56 +01:00
<?php
/**
* BillTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2018-02-06 19:49:29 +01:00
namespace FireflyIII\Transformers;
2018-02-06 07:49:56 +01:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2018-02-06 07:49:56 +01:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
2018-02-11 07:46:34 +01:00
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
2018-02-06 07:49:56 +01:00
use League\Fractal\TransformerAbstract;
2018-02-17 10:47:06 +01:00
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;
2018-02-06 07:49:56 +01:00
/**
* Class BillTransformer
*/
class BillTransformer extends TransformerAbstract
{
/** @var ParameterBag */
protected $parameters;
2018-02-06 07:49:56 +01:00
2018-09-27 07:43:30 +02:00
/** @var BillRepositoryInterface */
private $repository;
2018-02-06 07:49:56 +01:00
/**
* BillTransformer constructor.
*
* @codeCoverageIgnore
*
* @param ParameterBag $parameters
2018-02-06 07:49:56 +01:00
*/
public function __construct(ParameterBag $parameters)
2018-02-06 07:49:56 +01:00
{
$this->parameters = $parameters;
2018-09-27 07:43:30 +02:00
$this->repository = app(BillRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
2018-02-06 07:49:56 +01:00
}
/**
* Transform the bill.
*
2018-02-06 07:49:56 +01:00
* @param Bill $bill
*
* @return array
*/
public function transform(Bill $bill): array
{
$paidData = $this->paidData($bill);
$payDates = $this->payDates($bill);
2018-09-27 07:43:30 +02:00
$this->repository->setUser($bill->user);
$data = [
2018-02-06 07:49:56 +01:00
'id' => (int)$bill->id,
2018-02-11 07:46:34 +01:00
'created_at' => $bill->created_at->toAtomString(),
2018-12-12 20:30:25 +01:00
'updated_at' => $bill->updated_at->toAtomString(),
2018-04-13 17:28:11 +02:00
'currency_id' => $bill->transaction_currency_id,
'currency_code' => $bill->transactionCurrency->code,
2018-08-12 10:06:20 +02:00
'currency_symbol' => $bill->transactionCurrency->symbol,
2018-12-12 20:30:25 +01:00
'currency_dp' => $bill->transactionCurrency->decimal_places,
'name' => $bill->name,
'amount_min' => round((float)$bill->amount_min, 2),
'amount_max' => round((float)$bill->amount_max, 2),
2018-02-06 07:49:56 +01:00
'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
2018-06-24 06:51:22 +02:00
'active' => $bill->active,
2018-09-27 07:43:30 +02:00
'notes' => $this->repository->getNoteText($bill),
2018-02-06 07:49:56 +01:00
'next_expected_match' => $paidData['next_expected_match'],
2018-12-12 20:30:25 +01:00
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
2018-02-06 07:49:56 +01:00
'links' => [
[
'rel' => 'self',
2018-02-11 07:46:34 +01:00
'uri' => '/bills/' . $bill->id,
2018-02-06 07:49:56 +01:00
],
],
];
return $data;
}
/**
* Returns the latest date in the set, or start when set is empty.
*
* @param Collection $dates
* @param Carbon $default
*
* @return Carbon
*/
protected function lastPaidDate(Collection $dates, Carbon $default): Carbon
{
if (0 === $dates->count()) {
return $default; // @codeCoverageIgnore
}
$latest = $dates->first();
/** @var Carbon $date */
foreach ($dates as $date) {
if ($date->gte($latest)) {
$latest = $date;
}
}
return $latest;
}
/**
* Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether or not it is there already, is not relevant.
*
* @param Bill $bill
* @param Carbon $date
*
* @return \Carbon\Carbon
*/
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
{
$start = clone $bill->date;
while ($start < $date) {
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
return $start;
}
/**
2018-02-17 10:47:06 +01:00
* Get the data the bill was paid and predict the next expected match.
*
2018-02-06 07:49:56 +01:00
* @param Bill $bill
*
* @return array
*/
protected function paidData(Bill $bill): array
{
2018-02-17 10:47:06 +01:00
Log::debug(sprintf('Now in paidData for bill #%d', $bill->id));
2018-04-02 14:50:17 +02:00
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
2018-02-17 10:47:06 +01:00
Log::debug('parameters are NULL, return empty array');
return [
'paid_dates' => [],
'next_expected_match' => null,
];
}
2018-02-06 07:49:56 +01:00
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user);
2018-02-17 10:47:06 +01:00
$set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
2018-02-06 07:49:56 +01:00
$simple = $set->map(
function (Carbon $date) {
return $date->format('Y-m-d');
}
);
// calculate next expected match:
$lastPaidDate = $this->lastPaidDate($set, $this->parameters->get('start'));
2018-02-06 07:49:56 +01:00
$nextMatch = clone $bill->date;
while ($nextMatch < $lastPaidDate) {
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
$end = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
$journalCount = $repository->getPaidDatesInRange($bill, $nextMatch, $end)->count();
if ($journalCount > 0) {
$nextMatch = clone $end;
}
return [
'paid_dates' => $simple->toArray(),
'next_expected_match' => $nextMatch->format('Y-m-d'),
];
}
/**
* @param Bill $bill
*
* @return array
*/
protected function payDates(Bill $bill): array
{
2018-04-02 14:50:17 +02:00
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
return [];
}
2018-02-06 07:49:56 +01:00
$set = new Collection;
$currentStart = clone $this->parameters->get('start');
while ($currentStart <= $this->parameters->get('end')) {
2018-02-06 07:49:56 +01:00
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
// If nextExpectedMatch is after end, we continue:
if ($nextExpectedMatch > $this->parameters->get('end')) {
2018-02-06 07:49:56 +01:00
break;
}
// add to set
$set->push(clone $nextExpectedMatch);
$nextExpectedMatch->addDay();
$currentStart = clone $nextExpectedMatch;
}
$simple = $set->map(
function (Carbon $date) {
return $date->format('Y-m-d');
}
);
return $simple->toArray();
}
2018-03-05 19:35:58 +01:00
}