Deploy FeWo mit PipelineManager / deploy (push) Successful in 4m34s
- Vorgang: Die Kopfaktionen werden nach jeder Aktion neu aufgebaut, damit der große Schalter gleich den nächsten Schritt zeigt (Filament baut sie vor der Aktion auf). - In Folgejahr kopieren: Reicht eine bestehende Saison (z. B. Neujahr über den Jahreswechsel) nur über Beginn oder Ende der Kopie, wird die Kopie dort gekürzt statt ganz übersprungen; sonst blieb z. B. Januar bis März ohne Preis. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
3.9 KiB
PHP
104 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Models\Property;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/**
|
|
* Copies all seasons starting in a given year to the following year, so a new price year only needs adjusting
|
|
* instead of re-entering. Optionally every season start is moved to the nearest given weekday (e.g. Saturday
|
|
* as changeover day); seasons that were adjacent stay adjacent.
|
|
*/
|
|
class CopySeasonsToNextYear
|
|
{
|
|
/**
|
|
* @param int|null $alignToWeekday ISO weekday (1 = Monday … 7 = Sunday) or null for the same calendar dates
|
|
* @return array{created: int, skipped: int, trimmed: int}
|
|
*/
|
|
public function handle(Property $property, int $year, ?int $alignToWeekday = null): array
|
|
{
|
|
$created = 0;
|
|
$skipped = 0;
|
|
$trimmed = 0;
|
|
|
|
$seasons = $property->seasons()->whereYear('starts_on', $year)->orderBy('starts_on')->get();
|
|
|
|
// New start per old start date; an end that equals another season's start follows that start.
|
|
$moved = [];
|
|
foreach ($seasons as $season) {
|
|
$moved[$season->starts_on->toDateString()] = $this->shift($season->starts_on, $alignToWeekday);
|
|
}
|
|
|
|
foreach ($seasons as $season) {
|
|
$startsOn = $moved[$season->starts_on->toDateString()];
|
|
$endsOn = $moved[$season->ends_on->toDateString()] ?? $this->shift($season->ends_on, $alignToWeekday);
|
|
if ($endsOn->lessThanOrEqualTo($startsOn)) {
|
|
$skipped++;
|
|
|
|
continue;
|
|
}
|
|
|
|
[$startsOn, $endsOn, $wasTrimmed] = $this->fitBetweenExisting($property, $startsOn, $endsOn);
|
|
if ($startsOn === null || $endsOn === null) {
|
|
$skipped++;
|
|
|
|
continue;
|
|
}
|
|
|
|
$property->seasons()->create([
|
|
...$season->only(['name', 'nightly_rate', 'min_nights']),
|
|
'starts_on' => $startsOn,
|
|
'ends_on' => $endsOn,
|
|
]);
|
|
$created++;
|
|
$trimmed += $wasTrimmed ? 1 : 0;
|
|
}
|
|
|
|
return ['created' => $created, 'skipped' => $skipped, 'trimmed' => $trimmed];
|
|
}
|
|
|
|
/**
|
|
* Existing seasons win. One that only reaches over the start or the end of the copy shortens the copy there (e.g.
|
|
* a season over the turn of the year that ends a day after the moved start), so no nights stay without a price;
|
|
* the copy is left out only when an existing season lies within it or covers it completely.
|
|
*
|
|
* @return array{0: ?CarbonImmutable, 1: ?CarbonImmutable, 2: bool}
|
|
*/
|
|
private function fitBetweenExisting(Property $property, CarbonImmutable $startsOn, CarbonImmutable $endsOn): array
|
|
{
|
|
$originalStart = $startsOn;
|
|
$originalEnd = $endsOn;
|
|
|
|
foreach ($property->seasons()->overlapping($startsOn, $endsOn)->get() as $existing) {
|
|
if ($existing->starts_on->lessThanOrEqualTo($startsOn) && $existing->ends_on->greaterThan($startsOn)) {
|
|
$startsOn = $existing->ends_on;
|
|
}
|
|
if ($existing->ends_on->greaterThanOrEqualTo($endsOn) && $existing->starts_on->lessThan($endsOn)) {
|
|
$endsOn = $existing->starts_on;
|
|
}
|
|
}
|
|
|
|
if ($endsOn->lessThanOrEqualTo($startsOn) || $property->seasons()->overlapping($startsOn, $endsOn)->exists()) {
|
|
return [null, null, false];
|
|
}
|
|
|
|
return [$startsOn, $endsOn, ! $startsOn->equalTo($originalStart) || ! $endsOn->equalTo($originalEnd)];
|
|
}
|
|
|
|
/**
|
|
* One year later; with a weekday, moved to the nearest such day (at most 3 days earlier or later).
|
|
*/
|
|
private function shift(CarbonImmutable $date, ?int $weekday): CarbonImmutable
|
|
{
|
|
$next = $date->addYearNoOverflow();
|
|
if ($weekday === null) {
|
|
return $next;
|
|
}
|
|
|
|
$difference = ($weekday - $next->isoWeekday() + 7) % 7;
|
|
|
|
return $difference <= 3 ? $next->addDays($difference) : $next->subDays(7 - $difference);
|
|
}
|
|
}
|