From f70c057da055a892f58f2ec77cb3be508efc4594 Mon Sep 17 00:00:00 2001 From: Sam Lau Date: Fri, 23 Aug 2024 17:51:53 +0000 Subject: [PATCH] Bug 34922: Auto populate amount for an invoice with replacement_price associated with the barcode This patch allows for the "amount" field to be automatically populated with the replacement_price or the purchase_price after entering a barcode. If no barcode is enter (or an invalid barcode is entered), the "amount" field defaults to the default amount for that invoice type. To test: 1) Make an item with a barcode of '10' and a replacement price of 4 2) Make another item with a barcode of '101' and a purchase price of 6 3) Make a third item with a barcode of '1010' and leave prices blank. 4) Apply patch, restart_all 5) Visit a patron's accounting tab. 6) Click on 'Create manual invoice' 7) Type in the barcode '10', notice the "Amount" automatically fills to 4 8) Now add another 1 to the barcode, note the amount changes to 6 (no replacement price so default to purchase price) 9) Add another 0, now the amount goes to the defualt value that it was before. Signed-off-by: Roman Dolny --- .../prog/en/modules/members/maninvoice.tt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/maninvoice.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/maninvoice.tt index b64dfb26ad..c2bd2a670d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/maninvoice.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/maninvoice.tt @@ -136,6 +136,42 @@ type_fees['[% debit_type.code | html %]'] = "[% IF debit_type.default_amount %][% debit_type.default_amount | $Price on_editing => 1 %][% END %]"; [% END %] $(document).ready(function(){ + + document.getElementById('barcode').addEventListener('input', function () { + var barcode = this.value; + if (barcode.length > 0) { + fetchReplacementPrice(barcode); + } + }); + + function fetchReplacementPrice(barcode) { + $.ajax({ + url: `/api/v1/items?external_id=${barcode}`, + method: 'GET', + dataType: 'json', + success: function (data) { + // Check if data is an array and has at least one item + if (Array.isArray(data) && data.length > 0) { + // Filter the data to find the item with an exact barcode match + var matchedItem = data.find(item => item.external_id === barcode); + + if (matchedItem) { + var replacementPrice = matchedItem.replacement_price || matchedItem.purchase_price; + if (replacementPrice !== null && replacementPrice !== undefined) { + $('#amount').val(replacementPrice.toFixed(2)); + return; + } + } + } + var selectedType = $('#type').val(); + $('#amount').val(type_fees[selectedType] || ''); + }, + error: function() { + alert( _("An error occurred. Check the logs for details.") ); + } + }); + } + $('#maninvoice').preventDoubleFormSubmit(); $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit"); $("#invoice_type").on("change",function(){ -- 2.39.2