fix: correctly calculate undersubscription fees

This commit is contained in:
Björn Fromme
2026-08-06 12:05:03 +02:00
parent 3a5895e356
commit 577f7445ab
2 changed files with 99 additions and 4 deletions
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\Entity\Groups\AccommodationPrice;
use App\Service\GroupsPriceCalculator;
use App\Service\PriceTimelineBuilder;
use PHPUnit\Framework\TestCase;
class GroupsPriceCalculatorTest extends TestCase
{
private const int NIGHTS = 3;
public function testFreeBoardDoesNotTriggerUndersubscriptionSurcharge(): void
{
$result = $this->calculate(30, 0);
self::assertSame(0, $result['undersubscriptionSurcharge']);
self::assertNull($result['undersubscriptionThreshold']);
self::assertSame(0, $result['boardPrice']);
}
public function testNoBoardDoesNotTriggerUndersubscriptionSurcharge(): void
{
$result = $this->calculate(30, null);
self::assertSame(0, $result['undersubscriptionSurcharge']);
self::assertNull($result['undersubscriptionThreshold']);
self::assertSame(0, $result['boardPrice']);
}
public function testPaidBoardBelow40TriggersTheLowerThresholdSurcharge(): void
{
$result = $this->calculate(30, 1500);
self::assertSame(30 * 500 * self::NIGHTS, $result['undersubscriptionSurcharge']);
self::assertSame(40, $result['undersubscriptionThreshold']);
self::assertSame(1500 * 30 * self::NIGHTS, $result['boardPrice']);
}
public function testPaidBoardBelow50TriggersTheUpperThresholdSurcharge(): void
{
$result = $this->calculate(45, 1500);
self::assertSame(45 * 250 * self::NIGHTS, $result['undersubscriptionSurcharge']);
self::assertSame(50, $result['undersubscriptionThreshold']);
}
/**
* @return array<string, mixed>
*/
private function calculate(int $paxCount, ?int $boardServicePrice): array
{
$dateFrom = new \DateTimeImmutable('2026-07-01');
$dateTo = $dateFrom->modify(sprintf('+%d days', self::NIGHTS));
$price = (new AccommodationPrice())
->setDateFrom($dateFrom->modify('-1 month'))
->setDateTo($dateTo->modify('+1 month'))
->setIncludedPax(20)
->setPricePerNight(100_00)
->setPriceAdditionalPerson(10_00)
->setMinNights(1);
$calculator = new GroupsPriceCalculator(new PriceTimelineBuilder(), [
'runningCostsEur' => 0,
'runningCostsChf' => 0,
'undersubscription30Eur' => 5.0,
'undersubscription30Chf' => 6.0,
'undersubscription40Eur' => 2.5,
'undersubscription40Chf' => 3.0,
]);
return $calculator->calculateFromSnapshots(
$paxCount,
0,
self::NIGHTS,
$dateFrom,
$dateTo,
[$price],
$boardServicePrice,
[],
'EUR',
);
}
}