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

(-)a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js (-39 / +77 lines)
Lines 7-27 let bookable_items, Link Here
7
    booking_item_id,
7
    booking_item_id,
8
    booking_patron;
8
    booking_patron;
9
9
10
/**
11
 * @param {number[]} integers1 - The first array of integers to check.
12
 * @param {number[]} integers2 - The second array of integers to be checked against.
13
 * @returns {boolean} - Returns true if any element from integers1 is found in integers2, otherwise false.
14
 */
10
function containsAny(integers1, integers2) {
15
function containsAny(integers1, integers2) {
11
    // Create a hash set to store integers from the second array
16
    return integers1.some(integer => new Set(integers2).has(integer));
12
    let integerSet = {};
13
    for (let i = 0; i < integers2.length; i++) {
14
        integerSet[integers2[i]] = true;
15
    }
16
17
    // Check if any integer from the first array exists in the hash set
18
    for (let i = 0; i < integers1.length; i++) {
19
        if (integerSet[integers1[i]]) {
20
            return true; // Found a match, return true
21
        }
22
    }
23
24
    return false; // No match found
25
}
17
}
26
18
27
$("#placeBookingModal").on("show.bs.modal", function (e) {
19
$("#placeBookingModal").on("show.bs.modal", function (e) {
Lines 138-177 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
138
        allowClear: false,
130
        allowClear: false,
139
        placeholder: __("Pickup location"),
131
        placeholder: __("Pickup location"),
140
    });
132
    });
141
    function setLocationsPicker(response) {
133
    function setLocationsPicker(response, booking_patron) {
142
        let $pickupSelect = $("#pickup_library_id");
134
        let $pickupSelect = $("#pickup_library_id");
143
        let bookableItemnumbers = bookable_items.map(function (object) {
135
        let bookableItemnumbers = bookable_items.map(function (object) {
144
            return object.item_id;
136
            return object.item_id;
145
        });
137
        });
146
        $pickupSelect.empty();
138
        $pickupSelect.empty();
147
139
148
        $.each(response, function (index, pickup_location) {
140
        const filtered_pickup_locations = response.filter(({ pickup_items }) =>
149
            if (
141
            containsAny(pickup_items, bookableItemnumbers)
150
                containsAny(pickup_location.pickup_items, bookableItemnumbers)
142
        );
151
            ) {
143
        $.each(filtered_pickup_locations, function (index, pickup_location) {
152
                let option = $(
144
            let option = $(
153
                    '<option value="' +
145
                '<option value="' +
154
                        pickup_location.library_id +
146
                    pickup_location.library_id +
155
                        '">' +
147
                    '">' +
156
                        pickup_location.name +
148
                    pickup_location.name +
157
                        "</option>"
149
                    "</option>"
158
                );
150
            );
159
151
160
                option.attr(
152
            option.attr("data-needs_override", pickup_location.needs_override);
161
                    "data-needs_override",
153
            option.attr(
162
                    pickup_location.needs_override
154
                "data-pickup_items",
163
                );
155
                pickup_location.pickup_items.join(",")
164
                option.attr(
156
            );
165
                    "data-pickup_items",
166
                    pickup_location.pickup_items.join(",")
167
                );
168
157
169
                $pickupSelect.append(option);
158
            $pickupSelect.append(option);
170
            }
171
        });
159
        });
172
160
173
        $pickupSelect.prop("disabled", false);
161
        $pickupSelect.prop("disabled", false);
174
162
163
        // If filtered_pickup_locations contain the booking_patron's home library, use it as the default for pickup_library_id
164
        pickup_library_id ??= filtered_pickup_locations.find(
165
            pickup_location =>
166
                pickup_location.library_id ===
167
                booking_patron?.library.library_id
168
        )?.library_id;
169
175
        // If pickup_library alread exists, pre-select
170
        // If pickup_library alread exists, pre-select
176
        if (pickup_library_id) {
171
        if (pickup_library_id) {
177
            $pickupSelect.val(pickup_library_id).trigger("change");
172
            $pickupSelect.val(pickup_library_id).trigger("change");
Lines 205-216 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
205
            },
200
            },
206
            success: function (response) {
201
            success: function (response) {
207
                if (dataFetched === true) {
202
                if (dataFetched === true) {
208
                    setLocationsPicker(response);
203
                    setLocationsPicker(response, booking_patron);
209
                } else {
204
                } else {
210
                    var interval = setInterval(function () {
205
                    var interval = setInterval(function () {
211
                        if (dataFetched === true) {
206
                        if (dataFetched === true) {
212
                            // Data is fetched, execute the callback and stop the interval
207
                            // Data is fetched, execute the callback and stop the interval
213
                            setLocationsPicker(response);
208
                            setLocationsPicker(response, booking_patron);
214
                            clearInterval(interval);
209
                            clearInterval(interval);
215
                        }
210
                        }
216
                    }, 100);
211
                    }, 100);
Lines 515-520 $("#placeBookingModal").on("show.bs.modal", function (e) { Link Here
515
                    });
510
                    });
516
                    $("#pickup_library_id").trigger("change.select2");
511
                    $("#pickup_library_id").trigger("change.select2");
517
512
513
                    // Set item's home library as default if patron's home library hasn't already populated pickup_library_id
514
                    let pickup_library_id = $("#pickup_library_id").val();
515
                    const booking_patron_id = $("#booking_patron_id").val();
516
                    if (!pickup_library_id && booking_patron_id) {
517
                        $.ajax({
518
                            url:
519
                                "/api/v1/patrons?patron_id=" +
520
                                booking_patron_id,
521
                            dataType: "json",
522
                            type: "GET",
523
                        }).then(response => {
524
                            const [booking_patron] = response;
525
                            let is_home;
526
                            pickup_library_id = bookable_items.find(
527
                                ({ home_library_id, holding_library_id }) => {
528
                                    is_home =
529
                                        holding_library_id === home_library_id;
530
                                    if (is_home) {
531
                                        return (
532
                                            home_library_id ===
533
                                            booking_patron.library_id
534
                                        );
535
                                    }
536
537
                                    return (
538
                                        holding_library_id ===
539
                                        booking_patron.library_id
540
                                    );
541
                                }
542
                            )?.[
543
                                is_home
544
                                    ? "home_library_id"
545
                                    : "holding_library_id"
546
                            ];
547
                            if (!pickup_library_id) {
548
                                return;
549
                            }
550
551
                            $("#pickup_library_id")
552
                                .val(pickup_library_id)
553
                                .trigger("change.select2");
554
                        });
555
                    }
556
518
                    // Disable patron selection change
557
                    // Disable patron selection change
519
                    $("#booking_patron_id").prop("disabled", true);
558
                    $("#booking_patron_id").prop("disabled", true);
520
559
521
- 

Return to bug 37618