Browse code

Allow to hide base unit

Benjamin Roth authored on20/08/2025 13:39:30
Showing3 changed files
... ...
@@ -53,10 +53,12 @@
53 53
                     <div class="grid-c-6 mb-2 mb-0-md">
54 54
                         <fieldset>
55 55
                             <label for="res-behaeltereinheit"><strong>Liefernde Botticheinheit<sup class="text-danger">*</sup></strong></label>
56
-                            <select id="res-behaeltereinheit" name="unit" hx-get="/_ajax/vr_wa/v1/slot?do=availableBookingUnitAmount&id={{ id }}" hx-target="#wa-booking-{{ id }} #res-behaelter">
57
-                                <option value="">{{ 'tl_vr_wa_units.containers.0'|trans([], 'contao_tl_vr_wa_units') }}</option>
56
+                            <select id="res-behaeltereinheit" name="unit" hx-get="/_ajax/vr_wa/v1/slot?do=availableBookingUnitAmount&id={{ id }}" hx-target="#wa-booking-{{ id }} #res-behaelter" hx-trigger="change, load">
57
+                                {% if not buchen.disable_base_unit %}
58
+                                    <option value=""{% if buchen.default_unit == '' %} selected{% endif %}>{{ 'tl_vr_wa_units.containers.0'|trans([], 'contao_tl_vr_wa_units') }}</option>
59
+                                {% endif %}
58 60
                                 {% for value, option in buchen.units %}
59
-                                    <option value="{{ value }}"{{ buchung.unit == value ? ' selected' : '' }}>{{ option }}</option>
61
+                                    <option value="{{ value }}"{% if buchen.default_unit == value %} selected{% endif %}>{{ option }}</option>
60 62
                                 {% endfor %}
61 63
                             </select>
62 64
                         </fieldset>
... ...
@@ -87,10 +87,12 @@
87 87
                             <div class="grid-c-6 mb-2 mb-0-md">
88 88
                                 <fieldset>
89 89
                                     <label for="res-behaeltereinheit"><strong>Liefernde Botticheinheit<sup class="text-danger">*</sup></strong></label>
90
-                                    <select id="res-behaeltereinheit" name="unit" hx-get="/_ajax/vr_wa/v1/slot?do=availableUnitAmount&id={{ id }}" hx-target="#wa-slot-{{ id }} #res-behaelter">
91
-                                        <option value="">{{ 'tl_vr_wa_units.containers.0'|trans([], 'contao_tl_vr_wa_units') }}</option>
90
+                                    <select id="res-behaeltereinheit" name="unit" hx-get="/_ajax/vr_wa/v1/slot?do=availableUnitAmount&id={{ id }}" hx-target="#wa-slot-{{ id }} #res-behaelter" hx-trigger="change, load">
91
+                                        {% if not buchen.disable_base_unit %}
92
+                                            <option value=""{% if buchen.default_unit == '' %} selected{% endif %}>{{ 'tl_vr_wa_units.containers.0'|trans([], 'contao_tl_vr_wa_units') }}</option>
93
+                                        {% endif %}
92 94
                                         {% for value, option in buchen.units %}
93
-                                            <option value="{{ value }}">{{ option }}</option>
95
+                                            <option value="{{ value }}"{% if buchen.default_unit == value %} selected{% endif %}>{{ option }}</option>
94 96
                                         {% endfor %}
95 97
                                     </select>
96 98
                                 </fieldset>
... ...
@@ -214,6 +214,25 @@ class SlotAjaxController extends AbstractController
214 214
             }
215 215
         }
216 216
 
217
+        $standort = $Slot->getRelated('pid');
218
+        $disable_base_unit = !empty($standort->disable_base_unit);
219
+
220
+        // Determine default selected unit
221
+        $defaultUnitId = '';
222
+        if ($disable_base_unit && !empty($arrUnits)) {
223
+            // If base unit is disabled, select the first available unit
224
+            reset($arrUnits);
225
+            $defaultUnitId = key($arrUnits);
226
+        }
227
+
228
+        // Adjust quantity options based on default unit
229
+        $behaelterOptions = range(min($intBookableBehaelter,1), $intBookableBehaelter);
230
+        if ($defaultUnitId && ($Unit = WeinanlieferungUnitsModel::findByPk($defaultUnitId)) !== null) {
231
+            $intDefault = floor($intAvailableBehaelter / max(1, $Unit->containers));
232
+            $intAmount = floor($intBookableBehaelter / max(1, $Unit->containers));
233
+            $behaelterOptions = $intAmount ? range(1, $intAmount) : [];
234
+        }
235
+
217 236
         $arrData = [
218 237
             'modal' => $blnModal,
219 238
             'id'       => $Slot->id,
... ...
@@ -221,11 +240,13 @@ class SlotAjaxController extends AbstractController
221 240
                 'anmerkungen' => $insertTagService->replace($Slot->anmerkungen ?? ''),
222 241
                 'behaelterAvailable' => $intAvailableBehaelter
223 242
             ]),
224
-            'standort' => $Slot->getRelated('pid'),
243
+            'standort' => $standort,
225 244
             'buchen' => [
226 245
                 'buchbar' => (boolean) $intAvailableBehaelter,
227
-                'behaelter' => range(min($intBookableBehaelter,1),$intBookableBehaelter),
246
+                'behaelter' => $behaelterOptions,
228 247
                 'units' => $arrUnits,
248
+                'disable_base_unit' => $disable_base_unit,
249
+                'default_unit' => $defaultUnitId
229 250
             ],
230 251
             'reservations' => $arrReservations,
231 252
             'attribute_groups' => $arrAttributeGroups
... ...
@@ -466,6 +487,20 @@ class SlotAjaxController extends AbstractController
466 487
             }
467 488
         }
468 489
 
490
+        $standort = $Slot->getRelated('pid');
491
+        $disable_base_unit = !empty($standort->disable_base_unit);
492
+
493
+        // Determine default selected unit
494
+        $defaultUnitId = $Booking->unit ?: '';
495
+        if ($defaultUnitId === '' && $disable_base_unit && !empty($arrUnits)) {
496
+            // If base unit is disabled and no unit is selected, select the first available unit
497
+            reset($arrUnits);
498
+            $defaultUnitId = key($arrUnits);
499
+        }
500
+
501
+        // Adjust quantity options based on default unit if needed
502
+        $behaelterOptions = $intUnitAmount ? range(1, $intUnitAmount) : [];
503
+
469 504
         $arrData = array_merge($arrData,[
470 505
             'modal' => $blnModal,
471 506
             'id'       => $Booking->id,
... ...
@@ -474,12 +509,14 @@ class SlotAjaxController extends AbstractController
474 509
                 'behaelterAvailable' => $intAvailableBehaelter + $Booking->behaelter,
475 510
             ]),
476 511
             'buchung' => $Booking->row(),
477
-            'standort' => $Slot->getRelated('pid'),
512
+            'standort' => $standort,
478 513
             'buchen' => [
479 514
                 'buchbar' => (boolean) $intBookableBehaelter,
480 515
                 'default' => $intDefaultAmount,
481
-                'behaelter' => $intUnitAmount ? range(1,$intUnitAmount) : [],
516
+                'behaelter' => $behaelterOptions,
482 517
                 'units' => $arrUnits,
518
+                'disable_base_unit' => $disable_base_unit,
519
+                'default_unit' => $defaultUnitId
483 520
             ],
484 521
             'attribute_groups' => $arrAttributeGroups
485 522
         ]);