diff --git a/beam/beam/overrides/bin.py b/beam/beam/overrides/bin.py new file mode 100644 index 00000000..7d2cd8e8 --- /dev/null +++ b/beam/beam/overrides/bin.py @@ -0,0 +1,22 @@ +# Copyright (c) 2024, AgriTheory and contributors +# For license information, please see license.txt + +import frappe + + +class Bin: + pass + + +@frappe.whitelist() +@frappe.read_only() +def get_actual_qty(warehouse): + actual_qty = frappe.db.get_all( + "Bin", + filters=[ + ["warehouse", "=", warehouse], + ["actual_qty", ">", 0], + ], + fields=["item_code", "actual_qty", "valuation_rate"], + ) + return actual_qty diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index 6ac24e42..9fb8f042 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -208,6 +208,38 @@ class ScanHandler { frappe.model.set_value(row.doctype, row.name, 's_warehouse', barcode_context.target) frappe.model.set_value(row.doctype, row.name, 't_warehouse', barcode_context.target) } + } else if (barcode_context.doctype == 'Stock Reconciliation Item') { + frappe + .xcall('beam.beam.overrides.bin.get_actual_qty', { warehouse: barcode_context.context.doc.name }) + .then(r => { + if (r.length > 0) { + const items = new Map(cur_frm.doc.items.map(item => [item.item_code, item])); + for (let row of r) { + if (items.has(row.item_code)) { + items.get(row.item_code).qty += row.actual_qty; + } else { + items.set(row.item_code, { + ...row, + warehouse: barcode_context.context.doc.name, + qty: row.actual_qty, + barcode: barcode_context.context.barcode, + }); + } + } + + const filteredItems = Array.from(items.values()).filter(items => items.item_code); + cur_frm.set_value('items', filteredItems); + + /* + Avoid using `cur_frm.set_value('set_warehouse', barcode_context.context.doc.name)` + because it triggers `set_valuation_rate_and_qty` from `erpnext/stock_reconciliation.js`, + which resets the quantity to 0. + */ + + cur_frm.doc.set_warehouse = barcode_context.context.doc.name + cur_frm.refresh_field('set_warehouse') + } + }); } } add_or_increment(barcode_context) {