This commit is contained in:
James Cole
2023-07-01 12:18:07 +02:00
parent ad0a1b9a24
commit b055a5d6af
4 changed files with 28 additions and 21 deletions

View File

@@ -30,14 +30,14 @@ use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
/**
* Class BillTransformer
*/
class BillTransformer extends AbstractTransformer
{
/** @var BillRepositoryInterface */
private $repository;
private BillRepositoryInterface $repository;
/**
* BillTransformer constructor.
@@ -60,7 +60,6 @@ class BillTransformer extends AbstractTransformer
{
$paidData = $this->paidData($bill);
$payDates = $this->payDates($bill);
$currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
@@ -87,11 +86,15 @@ class BillTransformer extends AbstractTransformer
foreach ($payDates as $string) {
$payDatesFormatted[] = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'))->toAtomString();
}
if (1 === $bill->id) {
var_dump($payDates);
var_dump($payDatesFormatted);
var_dump($paidData['next_expected_match']);
}
$nextExpectedMatch = null;
if (null !== $paidData['next_expected_match']) {
$nextExpectedMatch = Carbon::createFromFormat('!Y-m-d', $paidData['next_expected_match'], config('app.timezone'))->toAtomString();
}
$nextExpectedMatchDiff = trans('firefly.not_expected_period');
// converting back and forth is bad code but OK.
$temp = new Carbon($nextExpectedMatch);
@@ -102,7 +105,7 @@ class BillTransformer extends AbstractTransformer
$current = $payDatesFormatted[0] ?? null;
if (null !== $current && !$temp->isToday()) {
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
$nextExpectedMatchDiff = $temp2->diffForHumans(today(), CarbonInterface::DIFF_RELATIVE_TO_NOW);
$nextExpectedMatchDiff = $temp2->diffForHumans(today(config('app.timezone')), CarbonInterface::DIFF_RELATIVE_TO_NOW);
}
unset($temp, $temp2);
@@ -152,28 +155,32 @@ class BillTransformer extends AbstractTransformer
*/
protected function paidData(Bill $bill): array
{
//Log::debug(sprintf('Now in paidData for bill #%d', $bill->id));
Log::debug(sprintf('Now in paidData for bill #%d', $bill->id));
if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) {
// Log::debug('parameters are NULL, return empty array');
//Log::debug('parameters are NULL, return empty array');
return [
'paid_dates' => [],
'next_expected_match' => null,
];
}
//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')));
// 2023-07-1 sub one day from the start date to fix a possible bug (see #7704)
/** @var Carbon $start */
$start = clone $this->parameters->get('start');
$start->subDay();
//Log::debug(sprintf('Parameters are start:%s end:%s', $start->format('Y-m-d'), $this->parameters->get('end')->format('Y-m-d')));
/*
* Get from database when bill was paid.
*/
$set = $this->repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
$set = $this->repository->getPaidDatesInRange($bill, $start, $this->parameters->get('end'));
//Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
/*
* Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
*/
//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'));
//Log::debug(sprintf('Grab last paid date from function, return %s if it comes up with nothing.', $start->format('Y-m-d')));
$lastPaidDate = $this->lastPaidDate($set, $start);
//Log::debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d')));
/*
@@ -192,7 +199,7 @@ class BillTransformer extends AbstractTransformer
}
if ($nextMatch->isSameDay($lastPaidDate)) {
/*
* Add another period because its the same day as the last paid date.
* Add another period because it's the same day as the last paid date.
*/
//Log::debug('Because the last paid date was on the same day as our next expected match, add another day.');
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
@@ -258,7 +265,7 @@ class BillTransformer extends AbstractTransformer
$currentStart = clone $this->parameters->get('start');
// 2023-06-23 subDay to fix 7655
$currentStart->subDay();
$loop = 0;
$loop = 0;
while ($currentStart <= $this->parameters->get('end')) {
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
// If nextExpectedMatch is after end, we continue: