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

363 lines
14 KiB
PHP
Raw Normal View History

2018-02-06 07:49:56 +01:00
<?php
/**
* BillTransformer.php
2020-02-16 13:57:18 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-02-06 07:49:56 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-06 07:49:56 +01:00
*
* 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.
2018-02-06 07:49:56 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-02-06 07:49:56 +01: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.
2018-02-06 07:49:56 +01: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/>.
2018-02-06 07:49:56 +01:00
*/
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;
2022-03-29 15:10:05 +02:00
use Carbon\CarbonInterface;
2018-02-06 07:49:56 +01:00
use FireflyIII\Models\Bill;
2020-06-30 19:06:05 +02:00
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionJournal;
2018-02-06 07:49:56 +01:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
/**
* Class BillTransformer
*/
2018-12-16 13:55:19 +01:00
class BillTransformer extends AbstractTransformer
2018-02-06 07:49:56 +01:00
{
2023-07-01 12:18:07 +02:00
private BillRepositoryInterface $repository;
2018-09-27 07:43:30 +02:00
2018-02-06 07:49:56 +01:00
/**
* BillTransformer constructor.
*
2023-02-12 07:23:57 +01:00
2018-02-06 07:49:56 +01:00
*/
2018-12-16 13:55:19 +01:00
public function __construct()
2018-02-06 07:49:56 +01:00
{
2018-09-27 07:43:30 +02:00
$this->repository = app(BillRepositoryInterface::class);
2018-02-06 07:49:56 +01:00
}
/**
* Transform the bill.
*
2023-06-21 12:34:58 +02:00
* @param Bill $bill
2018-02-06 07:49:56 +01:00
*
* @return array
*/
public function transform(Bill $bill): array
{
$paidData = $this->paidData($bill);
$payDates = $this->payDates($bill);
2018-12-19 06:06:01 +01:00
$currency = $bill->transactionCurrency;
2023-10-27 17:47:12 +02:00
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
2018-09-27 07:43:30 +02:00
$this->repository->setUser($bill->user);
2020-06-30 19:06:05 +02:00
2023-10-27 17:47:12 +02:00
$objectGroupId = null;
2020-06-30 19:06:05 +02:00
$objectGroupOrder = null;
$objectGroupTitle = null;
/** @var ObjectGroup $objectGroup */
$objectGroup = $bill->objectGroups->first();
if (null !== $objectGroup) {
2023-10-27 17:47:12 +02:00
$objectGroupId = (int)$objectGroup->id;
2022-12-29 19:42:26 +01:00
$objectGroupOrder = (int)$objectGroup->order;
2020-06-30 19:06:05 +02:00
$objectGroupTitle = $objectGroup->title;
}
2021-04-01 06:47:56 +02:00
$paidDataFormatted = [];
$payDatesFormatted = [];
2021-07-18 14:51:30 +02:00
foreach ($paidData['paid_dates'] as $object) {
2023-10-27 17:47:12 +02:00
$object['date'] = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'))->toAtomString();
2021-07-18 14:51:30 +02:00
$paidDataFormatted[] = $object;
2021-04-01 06:47:56 +02:00
}
foreach ($payDates as $string) {
$payDatesFormatted[] = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'))->toAtomString();
}
2021-04-02 06:57:31 +02:00
$nextExpectedMatch = null;
2021-07-18 14:51:30 +02:00
if (null !== $paidData['next_expected_match']) {
2021-04-02 06:57:31 +02:00
$nextExpectedMatch = Carbon::createFromFormat('!Y-m-d', $paidData['next_expected_match'], config('app.timezone'))->toAtomString();
}
2021-07-18 14:51:30 +02:00
$nextExpectedMatchDiff = trans('firefly.not_expected_period');
// converting back and forth is bad code but OK.
$temp = new Carbon($nextExpectedMatch);
if ($temp->isToday()) {
$nextExpectedMatchDiff = trans('firefly.today');
}
$current = $payDatesFormatted[0] ?? null;
if (null !== $current && !$temp->isToday()) {
2023-10-27 17:47:12 +02:00
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
2023-07-01 12:18:07 +02:00
$nextExpectedMatchDiff = $temp2->diffForHumans(today(config('app.timezone')), CarbonInterface::DIFF_RELATIVE_TO_NOW);
2021-07-18 14:51:30 +02:00
}
unset($temp, $temp2);
2020-10-23 19:11:25 +02:00
return [
2023-10-27 17:47:12 +02:00
'id' => (int)$bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => (string)$bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int)$currency->decimal_places,
'name' => $bill->name,
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
'active' => $bill->active,
'order' => (int)$bill->order,
'notes' => $notes,
'object_group_id' => $objectGroupId ? (string)$objectGroupId : null,
'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle,
2021-07-18 14:51:30 +02:00
// these fields need work:
2023-10-27 17:47:12 +02:00
'next_expected_match' => $nextExpectedMatch,
2021-07-18 14:51:30 +02:00
'next_expected_match_diff' => $nextExpectedMatchDiff,
2023-10-27 17:47:12 +02:00
'pay_dates' => $payDatesFormatted,
'paid_dates' => $paidDataFormatted,
'links' => [
2018-02-06 07:49:56 +01:00
[
'rel' => 'self',
2023-06-21 12:34:58 +02:00
'uri' => '/bills/' . $bill->id,
2018-02-06 07:49:56 +01:00
],
],
];
}
/**
2018-02-17 10:47:06 +01:00
* Get the data the bill was paid and predict the next expected match.
*
2023-06-21 12:34:58 +02:00
* @param Bill $bill
2018-02-06 07:49:56 +01:00
*
* @return array
*/
protected function paidData(Bill $bill): array
{
2023-10-06 18:21:49 +02:00
app('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')) {
2023-10-06 18:21:49 +02:00
app('log')->debug('parameters are NULL, return empty array');
2018-02-17 10:47:06 +01:00
return [
2023-10-27 17:47:12 +02:00
'paid_dates' => [],
'next_expected_match' => null,
];
}
2023-07-01 12:18:07 +02:00
// 2023-07-1 sub one day from the start date to fix a possible bug (see #7704)
// 2023-07-18 this particular date is used to search for the last paid date.
// 2023-07-18 the cloned $searchDate is used to grab the correct transactions.
2023-07-01 12:18:07 +02:00
/** @var Carbon $start */
2023-10-27 17:47:12 +02:00
$start = clone $this->parameters->get('start');
$searchStart = clone $start;
2023-07-01 12:18:07 +02:00
$start->subDay();
2023-10-06 18:21:49 +02:00
app('log')->debug(sprintf('Parameters are start: %s end: %s', $start->format('Y-m-d'), $this->parameters->get('end')->format('Y-m-d')));
app('log')->debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d')));
2020-01-09 19:59:53 +01:00
/*
* Get from database when bill was paid.
*/
$set = $this->repository->getPaidDatesInRange($bill, $searchStart, $this->parameters->get('end'));
app('log')->debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
2018-02-06 07:49:56 +01:00
2020-01-09 19:59:53 +01:00
/*
* Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
*/
app('log')->debug(sprintf('Grab last paid date from function, return %s if it comes up with nothing.', $start->format('Y-m-d')));
2023-07-01 12:18:07 +02:00
$lastPaidDate = $this->lastPaidDate($set, $start);
app('log')->debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d')));
2020-01-09 19:59:53 +01:00
/*
* The next expected match (nextMatch) is, initially, the bill's date.
*/
2020-01-09 19:52:47 +01:00
$nextMatch = clone $bill->date;
/*
* Diff in months (or other period) between bill start and last paid date or $start.
*/
2023-10-27 17:47:12 +02:00
$steps = app('navigation')->diffInPeriods($bill->repeat_freq, $bill->skip, $start, $nextMatch);
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $steps);
if ($nextMatch->lt($lastPaidDate)) {
/*
* Add another period because it's before the last paid date
*/
app('log')->debug('Because the last paid date was before our next expected match, add another period.');
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
2021-03-21 09:15:40 +01:00
if ($nextMatch->isSameDay($lastPaidDate)) {
2020-01-09 20:04:42 +01:00
/*
2023-07-01 12:18:07 +02:00
* Add another period because it's the same day as the last paid date.
2020-01-09 20:04:42 +01:00
*/
app('log')->debug('Because the last paid date was on the same day as our next expected match, add another day.');
2020-01-09 20:04:42 +01:00
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
2020-01-09 19:59:53 +01:00
/*
* At this point the "next match" is exactly after the last time the bill was paid.
*/
$result = [];
foreach ($set as $entry) {
$result[] = [
2023-10-27 17:47:12 +02:00
'transaction_group_id' => (int)$entry->transaction_group_id,
2022-12-29 19:42:26 +01:00
'transaction_journal_id' => (int)$entry->id,
2023-10-27 17:47:12 +02:00
'date' => $entry->date->format('Y-m-d'),
];
}
2021-03-21 09:15:40 +01:00
app('log')->debug(sprintf('Next match: %s', $nextMatch->toIso8601String()));
2020-01-09 19:52:47 +01:00
2021-05-24 08:50:17 +02:00
return [
2023-10-27 17:47:12 +02:00
'paid_dates' => $result,
2021-05-24 08:50:17 +02:00
'next_expected_match' => $nextMatch->format('Y-m-d'),
];
2018-02-06 07:49:56 +01:00
}
/**
2023-06-21 12:34:58 +02:00
* Returns the latest date in the set, or start when set is empty.
*
* @param Collection $dates
2023-10-27 17:47:12 +02:00
* @param Carbon $default
2023-06-21 12:34:58 +02:00
*
* @return Carbon
*/
protected function lastPaidDate(Collection $dates, Carbon $default): Carbon
{
if (0 === $dates->count()) {
return $default;
}
$latest = $dates->first()->date;
/** @var TransactionJournal $journal */
foreach ($dates as $journal) {
if ($journal->date->gte($latest)) {
$latest = $journal->date;
}
}
return $latest;
}
/**
* @param Bill $bill
2018-02-06 07:49:56 +01:00
*
* @return array
*/
protected function payDates(Bill $bill): array
{
2023-10-06 18:21:49 +02:00
app('log')->debug(sprintf('Now in payDates() for bill #%d', $bill->id));
2018-04-02 14:50:17 +02:00
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
2023-10-06 18:21:49 +02:00
app('log')->debug('No start or end date, give empty array.');
2019-12-20 05:38:26 +01:00
return [];
}
2023-10-27 17:47:12 +02:00
app('log')->debug(sprintf('Start: %s, end: %s', $this->parameters->get('start')->format('Y-m-d'), $this->parameters->get('end')->format('Y-m-d')));
$set = new Collection();
$currentStart = clone $this->parameters->get('start');
2023-06-26 06:10:16 +02:00
// 2023-06-23 subDay to fix 7655
$currentStart->subDay();
2023-07-01 12:18:07 +02:00
$loop = 0;
2023-10-27 17:47:12 +02:00
app('log')->debug('start of loop');
/*
* De eerste dag van de bill telt sowieso. Vanaf daarna gaan we door tellen.
* Weekly die start op 01-10
* 01-10: dit is hem dus.
* alle
*/
/*
* In de eerste week blijft aantal steps hangen op 0.
* Dus dan krijg je:
* 1 okt: 0
* 2 okt: 0
* 3 okt 0
* en daarna pas begint-ie te lopen.
* maar je moet sowieso een periode verder kijken.
*
* dus stel je begint op 1 oktober monthly.
* dan is de eerste hit (want subday) vanaf 30 sept gerekend.
*/
while ($currentStart <= $this->parameters->get('end')) {
2023-10-27 17:47:12 +02:00
app('log')->debug(sprintf('Current start is %s', $currentStart->format('Y-m-d')));
2018-02-06 07:49:56 +01:00
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
2023-10-06 18:21:49 +02:00
2023-10-27 17:47:12 +02:00
2018-02-06 07:49:56 +01:00
// If nextExpectedMatch is after end, we continue:
if ($nextExpectedMatch > $this->parameters->get('end')) {
2023-10-06 18:21:49 +02:00
app('log')->debug('Next expected match is after END, so stop looking');
2018-02-06 07:49:56 +01:00
break;
}
2023-10-27 17:47:12 +02:00
app('log')->debug(sprintf('Next expected match is %s', $nextExpectedMatch->format('Y-m-d')));
2023-10-27 18:07:56 +02:00
// add to set, if the date is ON or after the start parameter
if($nextExpectedMatch->gte($this->parameters->get('start'))) {
$set->push(clone $nextExpectedMatch);
}
2023-10-27 17:47:12 +02:00
// 2023-10
// for the next loop, go to end of period, THEN add day.
//$nextExpectedMatch = app('navigation')->endOfPeriod($nextExpectedMatch, $bill->repeat_freq);
2018-02-06 07:49:56 +01:00
$nextExpectedMatch->addDay();
$currentStart = clone $nextExpectedMatch;
2023-10-27 17:47:12 +02:00
2019-12-20 05:38:26 +01:00
$loop++;
2022-03-29 15:00:29 +02:00
if ($loop > 4) {
2022-02-05 08:53:45 +01:00
break;
}
2018-02-06 07:49:56 +01:00
}
2023-10-27 17:47:12 +02:00
app('log')->debug('end of loop');
2018-02-06 07:49:56 +01:00
$simple = $set->map(
2019-09-06 06:30:01 +02:00
static function (Carbon $date) {
2018-02-06 07:49:56 +01:00
return $date->format('Y-m-d');
}
);
app('log')->debug(sprintf('Found %d pay dates', $set->count()), $simple->toArray());
2021-07-18 14:51:30 +02:00
2021-05-24 08:06:56 +02:00
return $simple->toArray();
2018-02-06 07:49:56 +01:00
}
2023-06-21 12:34:58 +02:00
/**
* Given a bill and a date, this method will tell you at which moment this bill expects its next
2023-10-27 17:47:12 +02:00
* transaction. That date must be AFTER $date as a sanity check.
2023-06-21 12:34:58 +02:00
*
2023-10-27 17:47:12 +02:00
* @param Bill $bill
2023-06-21 12:34:58 +02:00
* @param Carbon $date
*
* @return Carbon
*/
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
{
2023-10-27 17:47:12 +02:00
app('log')->debug(sprintf('Now in nextDateMatch(#%d, %s)', $bill->id, $date->format('Y-m-d')));
2023-06-21 12:34:58 +02:00
$start = clone $bill->date;
2023-10-06 18:21:49 +02:00
app('log')->debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
2023-10-27 17:47:12 +02:00
if ($start->gt($date)) {
app('log')->debug('Start is after bill start, just return bill start date.');
return clone $start;
}
2023-06-21 12:34:58 +02:00
2023-10-27 17:47:12 +02:00
$steps = app('navigation')->diffInPeriods($bill->repeat_freq, $bill->skip, $start, $date);
$result = clone $start;
if ($steps > 0) {
$steps = $steps - 1;
app('log')->debug(sprintf('Steps is %d, because addPeriod already adds 1.', $steps));
$result = app('navigation')->addPeriod($start, $bill->repeat_freq, $steps);
}
app('log')->debug(sprintf('Number of steps is %d, result is %s', $steps, $result->format('Y-m-d')));
2023-10-06 18:21:49 +02:00
return $result;
2023-06-21 12:34:58 +02:00
}
2018-03-05 19:35:58 +01:00
}