Debug bill transformer

This commit is contained in:
James Cole
2023-10-27 17:47:12 +02:00
parent 106471877f
commit 902cc21ff0
2 changed files with 107 additions and 57 deletions

View File

@@ -58,6 +58,7 @@ class Navigation
*/ */
public function addPeriod(Carbon $theDate, string $repeatFreq, int $skip = 0): Carbon public function addPeriod(Carbon $theDate, string $repeatFreq, int $skip = 0): Carbon
{ {
$date = clone $theDate;
$functionMap = [ $functionMap = [
'1D' => Periodicity::Daily, '1D' => Periodicity::Daily,
'daily' => Periodicity::Daily, 'daily' => Periodicity::Daily,
@@ -95,7 +96,7 @@ class Navigation
return $theDate; return $theDate;
} }
return $this->nextDateByInterval($theDate, $functionMap[$repeatFreq], $skip); return $this->nextDateByInterval($date, $functionMap[$repeatFreq], $skip);
} }
/** /**
@@ -348,25 +349,36 @@ class Navigation
* *
* @return int * @return int
*/ */
public function diffInPeriods(string $period, Carbon $beginning, Carbon $end): int public function diffInPeriods(string $period, int $skip, Carbon $beginning, Carbon $end): int
{ {
app('log')->debug(sprintf('diffInPeriods: %s (skip: %d), between %s and %s.',
$period, $skip, $beginning->format('Y-m-d'), $end->format('Y-m-d')));
$map = [ $map = [
'daily' => 'diffInDays', 'daily' => 'floatDiffInDays',
'weekly' => 'diffInWeeks', 'weekly' => 'floatDiffInWeeks',
'monthly' => 'diffInMonths', 'monthly' => 'floatDiffInMonths',
'quarterly' => 'diffInQuarters', //'quarterly' => 'floatDiffInMonths',
'half-year' => 'diffInQuarters', //'half-year' => 'floatDiffInQuarters',
'yearly' => 'diffInYears', 'yearly' => 'floatDiffInYears',
]; ];
if (!array_key_exists($period, $map)) { if (!array_key_exists($period, $map)) {
app('log')->warning(sprintf('No diffInPeriods for period "%s"', $period)); app('log')->warning(sprintf('No diffInPeriods for period "%s"', $period));
return 1; return 1;
} }
$func = $map[$period]; $func = $map[$period];
$diff = $beginning->$func($end); $diff = ceil($beginning->$func($end));
app('log')->debug(sprintf('Diff is %f (%d)', $beginning->$func($end), $diff));
if ('half-year' === $period) { if ('half-year' === $period) {
$diff = ceil($diff / 2); $diff = ceil($diff / 2);
} }
if($skip > 0) {
$parameter = $skip + 1;
$diff = ceil($diff / $parameter) * $parameter;
app('log')->debug(sprintf('diffInPeriods: skip is %d, so param is %d, and diff becomes %d',
$skip, $parameter, $diff));
}
return (int)$diff; return (int)$diff;
} }

View File

@@ -189,7 +189,7 @@ class BillTransformer extends AbstractTransformer
/* /*
* Diff in months (or other period) between bill start and last paid date or $start. * Diff in months (or other period) between bill start and last paid date or $start.
*/ */
$steps = app('navigation')->diffInPeriods($bill->repeat_freq, $start, $nextMatch); $steps = app('navigation')->diffInPeriods($bill->repeat_freq, $bill->skip, $start, $nextMatch);
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $steps); $nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $steps);
if ($nextMatch->lt($lastPaidDate)) { if ($nextMatch->lt($lastPaidDate)) {
@@ -264,31 +264,60 @@ class BillTransformer extends AbstractTransformer
return []; return [];
} }
app('log')->debug(sprintf('Start: %s, end: %s', $this->parameters->get('start')->toIso8601String(), $this->parameters->get('end')->toIso8601String())); 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(); $set = new Collection();
$currentStart = clone $this->parameters->get('start'); $currentStart = clone $this->parameters->get('start');
// 2023-06-23 subDay to fix 7655 // 2023-06-23 subDay to fix 7655
$currentStart->subDay(); $currentStart->subDay();
$loop = 0; $loop = 0;
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')) { while ($currentStart <= $this->parameters->get('end')) {
app('log')->debug(sprintf('Current start is %s', $currentStart->toIso8601String())); app('log')->debug(sprintf('Current start is %s', $currentStart->format('Y-m-d')));
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart); $nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
// If nextExpectedMatch is after end, we continue: // If nextExpectedMatch is after end, we continue:
if ($nextExpectedMatch > $this->parameters->get('end')) { if ($nextExpectedMatch > $this->parameters->get('end')) {
app('log')->debug('Next expected match is after END, so stop looking'); app('log')->debug('Next expected match is after END, so stop looking');
break; break;
} }
app('log')->debug(sprintf('Next expected match is %s', $nextExpectedMatch->toIso8601String())); app('log')->debug(sprintf('Next expected match is %s', $nextExpectedMatch->format('Y-m-d')));
// add to set // add to set
$set->push(clone $nextExpectedMatch); $set->push(clone $nextExpectedMatch);
// 2023-10
// for the next loop, go to end of period, THEN add day.
//$nextExpectedMatch = app('navigation')->endOfPeriod($nextExpectedMatch, $bill->repeat_freq);
$nextExpectedMatch->addDay(); $nextExpectedMatch->addDay();
$currentStart = clone $nextExpectedMatch; $currentStart = clone $nextExpectedMatch;
$loop++; $loop++;
if ($loop > 4) { if ($loop > 4) {
break; break;
} }
} }
app('log')->debug('end of loop');
$simple = $set->map( $simple = $set->map(
static function (Carbon $date) { static function (Carbon $date) {
return $date->format('Y-m-d'); return $date->format('Y-m-d');
@@ -301,7 +330,7 @@ class BillTransformer extends AbstractTransformer
/** /**
* Given a bill and a date, this method will tell you at which moment this bill expects its next * Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether it is there already, is not relevant. * transaction. That date must be AFTER $date as a sanity check.
* *
* @param Bill $bill * @param Bill $bill
* @param Carbon $date * @param Carbon $date
@@ -310,13 +339,22 @@ class BillTransformer extends AbstractTransformer
*/ */
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
{ {
app('log')->debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d'))); app('log')->debug(sprintf('Now in nextDateMatch(#%d, %s)', $bill->id, $date->format('Y-m-d')));
$start = clone $bill->date; $start = clone $bill->date;
app('log')->debug(sprintf('Bill start date is %s', $start->format('Y-m-d'))); app('log')->debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
if ($start->gt($date)) {
app('log')->debug('Start is after bill start, just return bill start date.');
return clone $start;
}
$steps = app('navigation')->diffInPeriods($bill->repeat_freq, $start, $date); $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); $result = app('navigation')->addPeriod($start, $bill->repeat_freq, $steps);
app('log')->debug(sprintf('Number of steps is %d, result is %s', $steps, $start->format('Y-m-d'))); }
app('log')->debug(sprintf('Number of steps is %d, result is %s', $steps, $result->format('Y-m-d')));
return $result; return $result;
} }
} }