firefly-iii/app/Transformers/BillTransformer.php

307 lines
12 KiB
PHP
Raw Normal View History

2018-02-06 00:49:56 -06:00
<?php
/**
* BillTransformer.php
2020-02-16 06:57:18 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-02-06 00:49:56 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-06 00:49:56 -06: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 00:49:56 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-06 00:49:56 -06: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 00:49:56 -06: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 00:49:56 -06:00
*/
declare(strict_types=1);
2018-02-06 12:49:29 -06:00
namespace FireflyIII\Transformers;
2018-02-06 00:49:56 -06:00
use Carbon\Carbon;
2022-03-29 08:10:05 -05:00
use Carbon\CarbonInterface;
2018-02-06 00:49:56 -06:00
use FireflyIII\Models\Bill;
2020-06-30 12:06:05 -05:00
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionJournal;
2018-02-06 00:49:56 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
/**
* Class BillTransformer
*/
2018-12-16 06:55:19 -06:00
class BillTransformer extends AbstractTransformer
2018-02-06 00:49:56 -06:00
{
2018-09-27 00:43:30 -05:00
/** @var BillRepositoryInterface */
private $repository;
2018-02-06 00:49:56 -06:00
/**
* BillTransformer constructor.
*
2023-02-12 00:23:57 -06:00
2018-02-06 00:49:56 -06:00
*/
2018-12-16 06:55:19 -06:00
public function __construct()
2018-02-06 00:49:56 -06:00
{
2018-09-27 00:43:30 -05:00
$this->repository = app(BillRepositoryInterface::class);
2018-02-06 00:49:56 -06:00
}
/**
* Transform the bill.
*
2022-12-29 12:42:26 -06:00
* @param Bill $bill
2018-02-06 00:49:56 -06:00
*
* @return array
*/
public function transform(Bill $bill): array
{
$paidData = $this->paidData($bill);
$payDates = $this->payDates($bill);
2019-12-19 22:38:26 -06:00
2018-12-18 23:06:01 -06:00
$currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
2018-09-27 00:43:30 -05:00
$this->repository->setUser($bill->user);
2020-06-30 12:06:05 -05:00
$objectGroupId = null;
$objectGroupOrder = null;
$objectGroupTitle = null;
/** @var ObjectGroup $objectGroup */
$objectGroup = $bill->objectGroups->first();
if (null !== $objectGroup) {
2022-12-29 12:42:26 -06:00
$objectGroupId = (int)$objectGroup->id;
$objectGroupOrder = (int)$objectGroup->order;
2020-06-30 12:06:05 -05:00
$objectGroupTitle = $objectGroup->title;
}
2021-03-31 23:47:56 -05:00
$paidDataFormatted = [];
$payDatesFormatted = [];
2021-07-18 07:51:30 -05:00
foreach ($paidData['paid_dates'] as $object) {
$object['date'] = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'))->toAtomString();
$paidDataFormatted[] = $object;
2021-03-31 23:47:56 -05:00
}
foreach ($payDates as $string) {
$payDatesFormatted[] = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'))->toAtomString();
}
2021-04-01 23:57:31 -05:00
$nextExpectedMatch = null;
2021-07-18 07:51:30 -05:00
if (null !== $paidData['next_expected_match']) {
2021-04-01 23:57:31 -05:00
$nextExpectedMatch = Carbon::createFromFormat('!Y-m-d', $paidData['next_expected_match'], config('app.timezone'))->toAtomString();
}
2021-03-31 23:47:56 -05:00
2021-07-18 07:51:30 -05: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()) {
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
2022-03-29 08:10:05 -05:00
$nextExpectedMatchDiff = $temp2->diffForHumans(today(), CarbonInterface::DIFF_RELATIVE_TO_NOW);
2021-07-18 07:51:30 -05:00
}
unset($temp, $temp2);
2020-10-23 12:11:25 -05:00
return [
2022-12-29 12:42:26 -06:00
'id' => (int)$bill->id,
2021-07-18 07:51:30 -05:00
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
2022-12-29 12:42:26 -06:00
'currency_id' => (string)$bill->transaction_currency_id,
2021-07-18 07:51:30 -05:00
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
2022-12-29 12:42:26 -06:00
'currency_decimal_places' => (int)$currency->decimal_places,
2021-07-18 07:51:30 -05:00
'name' => $bill->name,
2022-12-27 14:13:18 -06:00
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
2021-07-18 07:51:30 -05:00
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),
'repeat_freq' => $bill->repeat_freq,
2022-12-29 12:42:26 -06:00
'skip' => (int)$bill->skip,
2021-07-18 07:51:30 -05:00
'active' => $bill->active,
2022-12-29 12:42:26 -06:00
'order' => (int)$bill->order,
2021-07-18 07:51:30 -05:00
'notes' => $notes,
2022-12-29 12:42:26 -06:00
'object_group_id' => $objectGroupId ? (string)$objectGroupId : null,
2021-07-18 07:51:30 -05:00
'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle,
// these fields need work:
'next_expected_match' => $nextExpectedMatch,
'next_expected_match_diff' => $nextExpectedMatchDiff,
'pay_dates' => $payDatesFormatted,
'paid_dates' => $paidDataFormatted,
'links' => [
2018-02-06 00:49:56 -06:00
[
'rel' => 'self',
2022-12-29 12:42:26 -06:00
'uri' => '/bills/'.$bill->id,
2018-02-06 00:49:56 -06:00
],
],
];
}
2023-05-29 06:56:55 -05:00
/**
* 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;
}
$latest = $dates->first()->date;
/** @var TransactionJournal $journal */
foreach ($dates as $journal) {
if ($journal->date->gte($latest)) {
$latest = $journal->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
*/
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
{
//Log::debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d')));
$start = clone $bill->date;
//Log::debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
while ($start < $date) {
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
//Log::debug(sprintf('End of loop, bill start date is now %s', $start->format('Y-m-d')));
return $start;
}
2018-02-06 00:49:56 -06:00
/**
2018-02-17 03:47:06 -06:00
* Get the data the bill was paid and predict the next expected match.
*
2022-12-29 12:42:26 -06:00
* @param Bill $bill
2018-02-06 00:49:56 -06:00
*
* @return array
*/
protected function paidData(Bill $bill): array
{
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Now in paidData for bill #%d', $bill->id));
2018-04-02 07:50:17 -05:00
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
2021-03-21 03:15:40 -05:00
// Log::debug('parameters are NULL, return empty array');
2018-02-17 03:47:06 -06:00
return [
'paid_dates' => [],
'next_expected_match' => null,
];
}
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Parameters are start:%s end:%s', $this->parameters->get('start')->format('Y-m-d'), $this->parameters->get('end')->format('Y-m-d')));
2020-01-09 12:59:53 -06:00
/*
* Get from database when bill was paid.
*/
2018-12-18 23:06:01 -06:00
$set = $this->repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
2018-02-06 00:49:56 -06:00
2020-01-09 12:59:53 -06: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.
*/
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Grab last paid date from function, return %s if it comes up with nothing.', $this->parameters->get('start')->format('Y-m-d')));
$lastPaidDate = $this->lastPaidDate($set, $this->parameters->get('start'));
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d')));
2020-01-09 12:59:53 -06:00
/*
* The next expected match (nextMatch) is, initially, the bill's date.
*/
2020-01-09 12:52:47 -06:00
$nextMatch = clone $bill->date;
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Next match is %s (bill->date)', $nextMatch->format('Y-m-d')));
2018-02-06 00:49:56 -06:00
while ($nextMatch < $lastPaidDate) {
2020-01-09 12:59:53 -06:00
/*
* As long as this date is smaller than the last time the bill was paid, keep jumping ahead.
* For example: 1 jan, 1 feb, etc.
*/
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('next match %s < last paid date %s, so add one period.', $nextMatch->format('Y-m-d'), $lastPaidDate->format('Y-m-d')));
2018-02-06 00:49:56 -06:00
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Next match is now %s.', $nextMatch->format('Y-m-d')));
2018-02-06 00:49:56 -06:00
}
2021-03-21 03:15:40 -05:00
if ($nextMatch->isSameDay($lastPaidDate)) {
2020-01-09 13:04:42 -06:00
/*
* Add another period because its the same day as the last paid date.
*/
2021-02-22 11:40:46 -06:00
//Log::debug('Because the last paid date was on the same day as our next expected match, add another day.');
2020-01-09 13:04:42 -06:00
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
2020-01-09 12:59:53 -06:00
/*
* At this point the "next match" is exactly after the last time the bill was paid.
*/
$result = [];
foreach ($set as $entry) {
$result[] = [
2022-12-29 12:42:26 -06:00
'transaction_group_id' => (int)$entry->transaction_group_id,
'transaction_journal_id' => (int)$entry->id,
'date' => $entry->date->format('Y-m-d'),
];
}
2021-03-21 03:15:40 -05:00
2021-02-22 11:50:12 -06:00
//Log::debug('Result', $result);
2020-01-09 12:52:47 -06:00
2021-05-24 01:50:17 -05:00
return [
'paid_dates' => $result,
'next_expected_match' => $nextMatch->format('Y-m-d'),
];
2018-02-06 00:49:56 -06:00
}
/**
2022-12-29 12:42:26 -06:00
* @param Bill $bill
2018-02-06 00:49:56 -06:00
*
* @return array
*/
protected function payDates(Bill $bill): array
{
2021-02-22 11:40:46 -06:00
//Log::debug(sprintf('Now in payDates() for bill #%d', $bill->id));
2018-04-02 07:50:17 -05:00
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
2021-02-22 11:40:46 -06:00
//Log::debug('No start or end date, give empty array.');
2019-12-19 22:38:26 -06:00
return [];
}
2022-10-30 08:24:37 -05:00
$set = new Collection();
$currentStart = clone $this->parameters->get('start');
2019-12-19 22:38:26 -06:00
$loop = 0;
while ($currentStart <= $this->parameters->get('end')) {
2018-02-06 00:49:56 -06:00
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
// If nextExpectedMatch is after end, we continue:
if ($nextExpectedMatch > $this->parameters->get('end')) {
2018-02-06 00:49:56 -06:00
break;
}
// add to set
$set->push(clone $nextExpectedMatch);
$nextExpectedMatch->addDay();
$currentStart = clone $nextExpectedMatch;
2019-12-19 22:38:26 -06:00
$loop++;
2022-03-29 08:00:29 -05:00
if ($loop > 4) {
2022-02-05 01:53:45 -06:00
break;
}
2018-02-06 00:49:56 -06:00
}
$simple = $set->map(
2019-09-05 23:30:01 -05:00
static function (Carbon $date) {
2018-02-06 00:49:56 -06:00
return $date->format('Y-m-d');
}
);
2021-07-18 07:51:30 -05:00
2021-05-24 01:06:56 -05:00
return $simple->toArray();
2018-02-06 00:49:56 -06:00
}
2018-03-05 12:35:58 -06:00
}