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
+11 -4
View File
@@ -244,10 +244,17 @@ class GroupsPriceCalculator
$shortTermSurcharge = (int) round(($basePrice + $additionalPersonsPrice) * self::SHORT_TERM_FACTORS[$nights] / 100);
}
// Rule 4: undersubscription surcharge (only when board is selected)
// Board is only billable — and only triggers the catering surcharge when it costs
// something. A free "Selbstversorgung" board is stored as 0 (BoardService::$price is
// non-nullable), so a plain null check would charge the surcharge for it.
$paidBoardPricePerPersonNight = ($boardServicePricePerPersonNight ?? 0) > 0
? $boardServicePricePerPersonNight
: null;
// Rule 4: undersubscription surcharge (only when a paid board is selected)
$undersubscriptionSurcharge = 0;
$undersubscriptionThreshold = null;
if ($boardServicePricePerPersonNight !== null) {
if ($paidBoardPricePerPersonNight !== null) {
$surcharge30 = (int) round(('CHF' === $currency ? $this->config['undersubscription30Chf'] : $this->config['undersubscription30Eur']) * 100);
$surcharge40 = (int) round(('CHF' === $currency ? $this->config['undersubscription40Chf'] : $this->config['undersubscription40Eur']) * 100);
@@ -262,8 +269,8 @@ class GroupsPriceCalculator
// Rule 5: board price
$boardPrice = 0;
if ($boardServicePricePerPersonNight !== null) {
$boardPrice = $boardServicePricePerPersonNight * $effectivePax * $nights;
if ($paidBoardPricePerPersonNight !== null) {
$boardPrice = $paidBoardPricePerPersonNight * $effectivePax * $nights;
}
// Rule 6: additional services
@@ -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',
);
}
}