feat: sort groups price services by price first, then alphabetically

This commit is contained in:
Björn Fromme
2026-08-03 16:23:47 +02:00
parent 9d2aa11fdb
commit 0b21bb00cb
4 changed files with 55 additions and 5 deletions
@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace App\Controller\Admin\Accommodation;
use App\Entity\Groups\Accommodation;
use App\Entity\Groups\AdditionalService;
use App\Entity\Groups\BoardService;
use App\Form\Admin\Groups\AccommodationType;
use App\Service\CmsDataProvider;
use Doctrine\ORM\EntityManagerInterface;
@@ -52,10 +54,31 @@ class EditController extends AbstractController
return $this->redirectToRoute('app_admin_accommodation_edit', ['id' => $accommodation->getId()]);
}
$boardServices = $accommodation->getBoardServices()->toArray();
usort($boardServices, $this->compareServices(...));
$additionalServices = $accommodation->getAdditionalServices()->toArray();
usort($additionalServices, $this->compareServices(...));
return $this->render('admin/accommodation/edit.html.twig', [
'accommodation' => $accommodation,
'form' => $form,
'cmsData' => $cmsData,
'boardServices' => $boardServices,
'additionalServices' => $additionalServices,
]);
}
/**
* DateFrom ascending, then free (price 0) before paid, then alphabetically by label.
*/
private function compareServices(BoardService|AdditionalService $a, BoardService|AdditionalService $b): int
{
$aFree = 0 === $a->getPrice() ? 0 : 1;
$bFree = 0 === $b->getPrice() ? 0 : 1;
return $a->getDateFrom() <=> $b->getDateFrom()
?: $aFree <=> $bFree
?: strnatcasecmp($a->getLabel(), $b->getLabel());
}
}
@@ -156,6 +156,12 @@ class AccommodationBookingService
}
}
$boardServices = $this->sortServicesByPriceThenLabel($boardServices);
$ungrouped = $this->sortServicesByPriceThenLabel($ungrouped);
foreach ($grouped as $groupName => $groupServices) {
$grouped[$groupName] = $this->sortServicesByPriceThenLabel($groupServices);
}
return [
'boardServices' => $boardServices,
'additionalServices' => $additionalServices,
@@ -164,6 +170,27 @@ class AccommodationBookingService
];
}
/**
* Free services (price 0) first, then alphabetically; each group sorted alphabetically.
*
* @template T of BoardService|AdditionalService
*
* @param T[] $services
*
* @return T[]
*/
private function sortServicesByPriceThenLabel(array $services): array
{
usort($services, static function (BoardService|AdditionalService $a, BoardService|AdditionalService $b): int {
$aFree = 0 === $a->getPrice() ? 0 : 1;
$bFree = 0 === $b->getPrice() ? 0 : 1;
return $aFree <=> $bFree ?: strnatcasecmp($a->getLabel(), $b->getLabel());
});
return $services;
}
/**
* Determines whether the booking must be treated as non-binding and why.
*
@@ -188,7 +188,7 @@
</tr>
</thead>
<tbody>
{% for service in accommodation.boardServices %}
{% for service in boardServices %}
<tr>
<td>
{{ service.dateFrom | date('d.m.Y') }} - {{ service.dateTo | date('d.m.Y') }}
@@ -258,7 +258,7 @@
</tr>
</thead>
<tbody>
{% for service in accommodation.additionalServices %}
{% for service in additionalServices %}
<tr>
<td>
{{ service.dateFrom | date('d.m.Y') }} - {{ service.dateTo | date('d.m.Y') }}
+3 -3
View File
@@ -109,7 +109,7 @@
{% endif %}
</td>
<td class="border border-primary-bg p-2 align-top w-32 text-right">
<div>{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}</div>
<div>{% if service.price == 0 %}inkl.{% else %}{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}{% endif %}</div>
<div class="text-xs text-gray-500">pro Person und Nacht</div>
</td>
<td class="border border-primary-bg p-2 align-top w-12 text-center">
@@ -155,7 +155,7 @@
{% endif %}
</td>
<td class="border border-primary-bg p-2 align-top w-32 text-right">
<div>{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}</div>
<div>{% if service.price == 0 %}inkl.{% else %}{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}{% endif %}</div>
<div class="text-xs text-gray-500">{{ service.type.label | trans }}</div>
</td>
<td class="border border-primary-bg p-2 align-top w-12 text-center">
@@ -193,7 +193,7 @@
{% endif %}
</td>
<td class="border border-primary-bg p-2 align-top w-32 text-right">
<div>{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}</div>
<div>{% if service.price == 0 %}inkl.{% else %}{{ (service.price / 100)|format_currency(ctx.accommodation.currency) }}{% endif %}</div>
<div class="text-xs text-gray-500">{{ service.type.label | trans }}</div>
</td>
<td class="border border-primary-bg p-2 align-top w-12 text-center">