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>
Rebased-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
(cherry picked from commit 6b1b371888)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Nick Clemens 2024-03-05 14:52:16 +00:00 committed by Fridolin Somers
parent da258a3195
commit bb5fd20c68
2 changed files with 65 additions and 40 deletions

View file

@ -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 '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;

View file

@ -316,14 +316,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>
@ -424,6 +419,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() {
@ -526,6 +522,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 %]