From bfcc71f818b4925575d95855ea71d76a39228e48 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 5 Mar 2024 14:52:16 +0000
Subject: [PATCH] Bug 36233: Use select2 to load vendors on invoice search

This patch moves the dropdown to use select2 and avoids loading all
vendors at page load.

To test:
 1 - Create some extra vendors in your system, ideally over 20
 2 - Search for a vendor in acquisitions
 3 - Click 'Invoices'
 4 - Note the dropdown of all vendors 'Vendor:' in search bar on left
 5 - The vendor you came from shoudl eb selected
 6 - Apply patch
 7 - Repeat
 8 - Note only a partial list of vendors is loaded, confirm current vendor still selected
 9 - Search in the dropdown and confirm vendors are returned
10 - Select a vendor and search
11 - confirm selection is retained

Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com>
---
 acqui/invoices.pl                             | 49 ++++++----------
 .../prog/en/modules/acqui/invoices.tt         | 56 ++++++++++++++++---
 2 files changed, 65 insertions(+), 40 deletions(-)

diff --git a/acqui/invoices.pl b/acqui/invoices.pl
index dce1a4d668..317d383561 100755
--- a/acqui/invoices.pl
+++ b/acqui/invoices.pl
@@ -105,22 +105,8 @@ $template->param(
 );
 
 # Build suppliers list
-my @suppliers      = Koha::Acquisition::Booksellers->search( undef, { order_by => { -asc => 'name' } } )->as_list;
-my $suppliers_loop = [];
-my $suppliername;
-foreach (@suppliers) {
-    my $selected = 0;
-    if ($supplierid && $supplierid == $_->id ) {
-        $selected = 1;
-        $suppliername = $_->name;
-    }
-    push @{$suppliers_loop},
-      {
-        suppliername => $_->name,
-        booksellerid   => $_->id,
-        selected     => $selected,
-      };
-}
+my $supplier;
+$supplier = Koha::Acquisition::Booksellers->find($supplierid) if $supplierid;
 
 my $budgets = GetBudgets();
 my @budgets_loop;
@@ -140,24 +126,23 @@ for my $sub ( @{$invoices} ) {
 $template->{'VARS'}->{'budgets_loop'} = \@budgets_loop;
 
 $template->param(
-    openedinvoices => \@openedinvoices,
-    closedinvoices => \@closedinvoices,
-    do_search => ( $op and $op eq 'cud-do_search' ) ? 1 : 0,
-    invoices => $invoices,
-    invoicenumber   => $invoicenumber,
-    booksellerid    => $supplierid,
-    suppliername    => $suppliername,
+    openedinvoices   => \@openedinvoices,
+    closedinvoices   => \@closedinvoices,
+    do_search        => ( $op and $op eq 'do_search' ) ? 1 : 0,
+    invoices         => $invoices,
+    invoicenumber    => $invoicenumber,
+    booksellerid     => $supplierid,
+    supplier         => $supplier,
     shipmentdatefrom => $shipmentdatefrom,
     shipmentdateto   => $shipmentdateto,
-    billingdatefrom => $billingdatefrom,
-    billingdateto   => $billingdateto,
-    isbneanissn     => $isbneanissn,
-    title           => $title,
-    author          => $author,
-    publisher       => $publisher,
-    publicationyear => $publicationyear,
-    branch          => $branch,
-    suppliers_loop  => $suppliers_loop,
+    billingdatefrom  => $billingdatefrom,
+    billingdateto    => $billingdateto,
+    isbneanissn      => $isbneanissn,
+    title            => $title,
+    author           => $author,
+    publisher        => $publisher,
+    publicationyear  => $publicationyear,
+    branch           => $branch,
 );
 
 output_html_with_http_headers $input, $cookie, $template->output;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt
index b045a90264..7973d64217 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt
@@ -317,14 +317,9 @@
                             </li>
                             <li>
                                 <label for="supplier">Vendor:</label>
-                                <select id="supplier" name="supplierid">
-                                    <option value="">All</option>
-                                    [% FOREACH supplier IN suppliers_loop %]
-                                        [% IF ( supplier.selected ) %]
-                                            <option selected="selected" value="[% supplier.booksellerid | html %]">[% supplier.suppliername | html %]</option>
-                                        [% ELSE %]
-                                            <option value="[% supplier.booksellerid | html %]">[% supplier.suppliername | html %]</option>
-                                        [% END %]
+                                <select id="supplierid" name="supplierid">
+                                    [% IF ( supplier ) %]
+                                        <option selected="selected" value="[% supplier.id | html %]">[% supplier.name | html %]</option>
                                     [% END %]
                                 </select>
                             </li>
@@ -425,6 +420,7 @@
     [% Asset.js("js/acquisitions-menu.js") | $raw %]
     [% INCLUDE 'datatables.inc' %]
     [% INCLUDE 'calendar.inc' %]
+    [% INCLUDE 'select2.inc' %]
     <script>
         $(document).ready(function() {
             $('[id^="CheckAll"]').click( function() {
@@ -527,6 +523,50 @@
                     $('#merge_invoices').show();
                 }
             });
+
+            function display_vendor(vendor) {
+                var $text;
+                $text = $('<span>'+vendor.text+'</span>');
+                return $text;
+            }
+
+            $("#supplierid").kohaSelect({
+                width: '50%',
+                allowClear: false,
+                ajax: {
+                    url: '/api/v1/acquisitions/vendors',
+                    delay: 300, // wait 300 milliseconds before triggering the request
+                    cache: true,
+                    dataType: 'json',
+                    data: function (params) {
+                        var search_term = (params.term === undefined) ? '' : params.term;
+                        var query = {
+                            "q": JSON.stringify({"name":{"-like":'%'+search_term+'%'}}),
+                            "_order_by": "name",
+                            "_page": params.page
+                        };
+
+                        return query;
+                    },
+                    processResults: function (data) {
+                        var results = [];
+                        data.results.forEach( function ( vendor ) {
+                            results.push(
+                                {
+                                    "id": vendor.id,
+                                    "text": vendor.name.escapeHtml()
+                                }
+                            );
+                        });
+                        return { "results": results, "pagination": { "more": data.pagination.more } };
+                    }
+                },
+                templateResult: display_vendor,
+                templateSelection: display_vendor
+            });
+
+
+
         });
     </script>
 [% END %]
-- 
2.30.2