View | Details | Raw Unified | Return to bug 37618
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js (-40 / +77 lines)
Lines 8-28 let bookable_items, Link Here
8
    booking_patron,
8
    booking_patron,
9
    booking_itemtype_id;
9
    booking_itemtype_id;
10
10
11
/**
12
 * @param {number[]} integers1 - The first array of integers to check.
13
 * @param {number[]} integers2 - The second array of integers to be checked against.
14
 * @returns {boolean} - Returns true if any element from integers1 is found in integers2, otherwise false.
15
 */
11
function containsAny(integers1, integers2) {
16
function containsAny(integers1, integers2) {
12
    // Create a hash set to store integers from the second array
17
    return integers1.some(integer => new Set(integers2).has(integer));
13
    let integerSet = {};
14
    for (let i = 0; i < integers2.length; i++) {
15
        integerSet[integers2[i]] = true;
16
    }
17
18
    // Check if any integer from the first array exists in the hash set
19
    for (let i = 0; i < integers1.length; i++) {
20
        if (integerSet[integers1[i]]) {
21
            return true; // Found a match, return true
22
        }
23
    }
24
25
    return false; // No match found
26
}
18
}
27
19
28
$("#placeBookingModal").on("show.bs.modal", function (e) {
20
$("#placeBookingModal").on("show.bs.modal", function (e) {
Lines 173-213 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
173
        allowClear: false,
165
        allowClear: false,
174
        placeholder: __("Pickup location"),
166
        placeholder: __("Pickup location"),
175
    });
167
    });
176
    function setLocationsPicker(response) {
168
    function setLocationsPicker(response, booking_patron) {
177
        let $pickupSelect = $("#pickup_library_id");
169
        let $pickupSelect = $("#pickup_library_id");
178
        let bookableItemnumbers = bookable_items.map(function (object) {
170
        let bookableItemnumbers = bookable_items.map(function (object) {
179
            return object.item_id;
171
            return object.item_id;
180
        });
172
        });
181
        $pickupSelect.empty();
173
        $pickupSelect.empty();
182
174
183
        $.each(response, function (index, pickup_location) {
175
        const filtered_pickup_locations = response.filter(({ pickup_items }) =>
184
            if (
176
            containsAny(pickup_items, bookableItemnumbers)
185
                containsAny(pickup_location.pickup_items, bookableItemnumbers)
177
        );
186
            ) {
178
        $.each(filtered_pickup_locations, function (index, pickup_location) {
187
                let option = $(
179
            let option = $(
188
                    '<option value="' +
180
                '<option value="' +
189
                        pickup_location.library_id +
181
                    pickup_location.library_id +
190
                        '">' +
182
                    '">' +
191
                        pickup_location.name +
183
                    pickup_location.name +
192
                        "</option>"
184
                    "</option>"
193
                );
185
            );
194
186
195
                option.attr(
187
            option.attr("data-needs_override", pickup_location.needs_override);
196
                    "data-needs_override",
188
            option.attr(
197
                    pickup_location.needs_override
189
                "data-pickup_items",
198
                );
190
                pickup_location.pickup_items.join(",")
199
                option.attr(
191
            );
200
                    "data-pickup_items",
201
                    pickup_location.pickup_items.join(",")
202
                );
203
192
204
                $pickupSelect.append(option);
193
            $pickupSelect.append(option);
205
            }
206
        });
194
        });
207
195
208
        $pickupSelect.prop("disabled", false);
196
        $pickupSelect.prop("disabled", false);
209
197
210
        // If pickup_library already exists, pre-select
198
        // If filtered_pickup_locations contain the booking_patron's home library, use it as the default for pickup_library_id
199
        pickup_library_id ??= filtered_pickup_locations.find(
200
            pickup_location =>
201
                pickup_location.library_id ===
202
                booking_patron?.library.library_id
203
        )?.library_id;
204
211
        if (pickup_library_id) {
205
        if (pickup_library_id) {
212
            $pickupSelect.val(pickup_library_id).trigger("change");
206
            $pickupSelect.val(pickup_library_id).trigger("change");
213
        } else {
207
        } else {
Lines 250-261 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
250
            },
244
            },
251
            success: function (response) {
245
            success: function (response) {
252
                if (dataFetched === true) {
246
                if (dataFetched === true) {
253
                    setLocationsPicker(response);
247
                    setLocationsPicker(response, booking_patron);
254
                } else {
248
                } else {
255
                    var interval = setInterval(function () {
249
                    var interval = setInterval(function () {
256
                        if (dataFetched === true) {
250
                        if (dataFetched === true) {
257
                            // Data is fetched, execute the callback and stop the interval
251
                            // Data is fetched, execute the callback and stop the interval
258
                            setLocationsPicker(response);
252
                            setLocationsPicker(response, booking_patron);
259
                            clearInterval(interval);
253
                            clearInterval(interval);
260
                        }
254
                        }
261
                    }, 100);
255
                    }, 100);
Lines 617-622 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
617
                    });
611
                    });
618
                    $("#pickup_library_id").trigger("change.select2");
612
                    $("#pickup_library_id").trigger("change.select2");
619
613
614
                    // Set item's home library as default if patron's home library hasn't already populated pickup_library_id
615
                    let pickup_library_id = $("#pickup_library_id").val();
616
                    const booking_patron_id = $("#booking_patron_id").val();
617
                    if (!pickup_library_id && booking_patron_id) {
618
                        $.ajax({
619
                            url:
620
                                "/api/v1/patrons?patron_id=" +
621
                                booking_patron_id,
622
                            dataType: "json",
623
                            type: "GET",
624
                        }).then(response => {
625
                            const [booking_patron] = response;
626
                            let is_home;
627
                            pickup_library_id = bookable_items.find(
628
                                ({ home_library_id, holding_library_id }) => {
629
                                    is_home =
630
                                        holding_library_id === home_library_id;
631
                                    if (is_home) {
632
                                        return (
633
                                            home_library_id ===
634
                                            booking_patron.library_id
635
                                        );
636
                                    }
637
638
                                    return (
639
                                        holding_library_id ===
640
                                        booking_patron.library_id
641
                                    );
642
                                }
643
                            )?.[
644
                                is_home
645
                                    ? "home_library_id"
646
                                    : "holding_library_id"
647
                            ];
648
                            if (!pickup_library_id) {
649
                                return;
650
                            }
651
652
                            $("#pickup_library_id")
653
                                .val(pickup_library_id)
654
                                .trigger("change.select2");
655
                        });
656
                    }
657
620
                    // Disable patron selection change
658
                    // Disable patron selection change
621
                    $("#booking_patron_id").prop("disabled", true);
659
                    $("#booking_patron_id").prop("disabled", true);
622
660
623
- 

Return to bug 37618