From d0412a2168c7618a5305ebb962eb0f668e1a4fc8 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 27 Feb 2025 18:07:50 +0000 Subject: [PATCH 01/11] feat: scanning on Stock Reconciliation --- beam/beam/overrides/bin.py | 21 +++++++++++++++++++++ beam/public/js/scan/scan.js | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 beam/beam/overrides/bin.py diff --git a/beam/beam/overrides/bin.py b/beam/beam/overrides/bin.py new file mode 100644 index 00000000..9b4586f1 --- /dev/null +++ b/beam/beam/overrides/bin.py @@ -0,0 +1,21 @@ +# 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 f6acf877..e29488b3 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -208,6 +208,24 @@ 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.model.set_value(cur_frm.doc.doctype, cur_frm.doc.name, 'set_warehouse', barcode_context.context.doc.name) + frappe + .xcall('beam.beam.overrides.bin.get_actual_qty', { warehouse: barcode_context.context.doc.name }) + .then(r => { + if (r.length > 0) { + cur_frm.clear_table('items') + for (let row of r) { + const child = cur_frm.add_child('items', { + ...row, + warehouse: barcode_context.context.doc.name, + qty: row.actual_qty, + barcode: barcode_context.context.barcode, + }) + cur_frm.refresh_field('items') + } + } + }) } } add_or_increment(barcode_context) { From d55abc9feaa80a7cf85662ccdebdcfacb2c9ae55 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 27 Feb 2025 18:26:14 +0000 Subject: [PATCH 02/11] fix: refresh table after clear --- beam/public/js/scan/scan.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index e29488b3..c6f8f36a 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -215,15 +215,16 @@ class ScanHandler { .then(r => { if (r.length > 0) { cur_frm.clear_table('items') + cur_frm.refresh_field('items') for (let row of r) { - const child = cur_frm.add_child('items', { + cur_frm.add_child('items', { ...row, warehouse: barcode_context.context.doc.name, qty: row.actual_qty, barcode: barcode_context.context.barcode, }) - cur_frm.refresh_field('items') } + cur_frm.refresh_field('items') } }) } From 623c47ee5cc9e39b63ffe659924e2e699411f3d7 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 27 Feb 2025 18:36:12 +0000 Subject: [PATCH 03/11] fix: clear items --- beam/public/js/scan/scan.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index c6f8f36a..e9969d67 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -214,8 +214,7 @@ class ScanHandler { .xcall('beam.beam.overrides.bin.get_actual_qty', { warehouse: barcode_context.context.doc.name }) .then(r => { if (r.length > 0) { - cur_frm.clear_table('items') - cur_frm.refresh_field('items') + cur_frm.doc.items = [] for (let row of r) { cur_frm.add_child('items', { ...row, @@ -224,8 +223,8 @@ class ScanHandler { barcode: barcode_context.context.barcode, }) } - cur_frm.refresh_field('items') } + cur_frm.refresh_field('items') }) } } From bc9e914a657a91ebac002e48a050c58bfad973bd Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 27 Feb 2025 18:42:03 +0000 Subject: [PATCH 04/11] prettier --- beam/beam/overrides/bin.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/beam/beam/overrides/bin.py b/beam/beam/overrides/bin.py index 9b4586f1..7d2cd8e8 100644 --- a/beam/beam/overrides/bin.py +++ b/beam/beam/overrides/bin.py @@ -4,18 +4,19 @@ import frappe -class Bin(): - pass +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 + actual_qty = frappe.db.get_all( + "Bin", + filters=[ + ["warehouse", "=", warehouse], + ["actual_qty", ">", 0], + ], + fields=["item_code", "actual_qty", "valuation_rate"], + ) + return actual_qty From b3a01ced06d35d3ca35d5926d764394ded2c50d7 Mon Sep 17 00:00:00 2001 From: Lauty <38353324+lauty95@users.noreply.github.com> Date: Wed, 5 Mar 2025 10:22:51 -0300 Subject: [PATCH 05/11] Update beam/public/js/scan/scan.js Co-authored-by: Rohan --- beam/public/js/scan/scan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index e9969d67..4084e00e 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -209,7 +209,7 @@ class ScanHandler { frappe.model.set_value(row.doctype, row.name, 't_warehouse', barcode_context.target) } } else if (barcode_context.doctype == 'Stock Reconciliation Item') { - frappe.model.set_value(cur_frm.doc.doctype, cur_frm.doc.name, 'set_warehouse', barcode_context.context.doc.name) + cur_frm.set_value('set_warehouse', barcode_context.context.doc.name) frappe .xcall('beam.beam.overrides.bin.get_actual_qty', { warehouse: barcode_context.context.doc.name }) .then(r => { From 72063f0f81b6e982b31bc23c29ec94fd56377c58 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Wed, 5 Mar 2025 17:44:52 +0000 Subject: [PATCH 06/11] feat: reconciliation items incrementing --- beam/public/js/scan/scan.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index 4084e00e..20b420a2 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -214,18 +214,24 @@ class ScanHandler { .xcall('beam.beam.overrides.bin.get_actual_qty', { warehouse: barcode_context.context.doc.name }) .then(r => { if (r.length > 0) { - cur_frm.doc.items = [] + const items = new Map(cur_frm.doc.items.map(item => [item.item_code, item])); for (let row of r) { - cur_frm.add_child('items', { - ...row, - warehouse: barcode_context.context.doc.name, - qty: row.actual_qty, - barcode: barcode_context.context.barcode, - }) + 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(item => item.item_code || item.qty); + cur_frm.set_value('items', filteredItems); } - cur_frm.refresh_field('items') - }) + }); + cur_frm.refresh_field('items') } } add_or_increment(barcode_context) { From c59f6878d1534084f77915cc89f6925bcf4015ec Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 6 Mar 2025 13:09:15 +0000 Subject: [PATCH 07/11] fix: update warehouse at the end --- beam/public/js/scan/scan.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index 20b420a2..3de2980d 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -209,29 +209,32 @@ class ScanHandler { frappe.model.set_value(row.doctype, row.name, 't_warehouse', barcode_context.target) } } else if (barcode_context.doctype == 'Stock Reconciliation Item') { - cur_frm.set_value('set_warehouse', barcode_context.context.doc.name) + const items = new Map(cur_frm.doc.items.map(item => [item.item_code, 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; + const item = items.get(row.item_code); + if (row.actual_qty !== undefined && item) { + item.qty += row.actual_qty; + } } else { items.set(row.item_code, { ...row, warehouse: barcode_context.context.doc.name, - qty: row.actual_qty, + qty: row.actual_qty || 0, barcode: barcode_context.context.barcode, }); } } const filteredItems = Array.from(items.values()).filter(item => item.item_code || item.qty); cur_frm.set_value('items', filteredItems); + cur_frm.set_value('set_warehouse', barcode_context.context.doc.name) + cur_frm.refresh_field('items'); } }); - cur_frm.refresh_field('items') } } add_or_increment(barcode_context) { From e4b25a20e998371c672c416cb4cfcbed2d32c79b Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 6 Mar 2025 14:02:12 +0000 Subject: [PATCH 08/11] fix: set value to assign warehouse to avoid handle changes --- beam/public/js/scan/scan.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index 3de2980d..d8103db6 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -209,30 +209,29 @@ class ScanHandler { frappe.model.set_value(row.doctype, row.name, 't_warehouse', barcode_context.target) } } else if (barcode_context.doctype == 'Stock Reconciliation Item') { - const items = new Map(cur_frm.doc.items.map(item => [item.item_code, 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)) { - const item = items.get(row.item_code); - if (row.actual_qty !== undefined && item) { - item.qty += row.actual_qty; - } + 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 || 0, + qty: row.actual_qty, barcode: barcode_context.context.barcode, }); } } - const filteredItems = Array.from(items.values()).filter(item => item.item_code || item.qty); + + const filteredItems = Array.from(items.values()) cur_frm.set_value('items', filteredItems); - cur_frm.set_value('set_warehouse', barcode_context.context.doc.name) - cur_frm.refresh_field('items'); + + cur_frm.doc.set_warehouse = barcode_context.context.doc.name + cur_frm.refresh_field('set_warehouse') } }); } From 00aeccf397c9f5249c602c3fa5b9704530b43a41 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 6 Mar 2025 14:18:01 +0000 Subject: [PATCH 09/11] fix: filter empty item_code --- beam/public/js/scan/scan.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index d8103db6..0b73a19a 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -87,6 +87,7 @@ class ScanHandler { } else { const context = this.reduceContext() frappe.xcall('beam.beam.scan.scan', { barcode: sCode, context: context, current_qty: iQty }).then(r => { + console.log(r) if (r && r.length) { if (Object.keys(frappe.boot.beam.client).includes(r[0].action)) { let path = frappe.boot.beam.client[r[0].action][0] @@ -227,7 +228,7 @@ class ScanHandler { } } - const filteredItems = Array.from(items.values()) + const filteredItems = Array.from(items.values()).filter(items => items.item_code); cur_frm.set_value('items', filteredItems); cur_frm.doc.set_warehouse = barcode_context.context.doc.name From 1f34799f05490d4429754fb1a657ddcebea685e8 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 6 Mar 2025 14:21:38 +0000 Subject: [PATCH 10/11] fix: console --- beam/public/js/scan/scan.js | 1 - 1 file changed, 1 deletion(-) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index 0b73a19a..a965600d 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -87,7 +87,6 @@ class ScanHandler { } else { const context = this.reduceContext() frappe.xcall('beam.beam.scan.scan', { barcode: sCode, context: context, current_qty: iQty }).then(r => { - console.log(r) if (r && r.length) { if (Object.keys(frappe.boot.beam.client).includes(r[0].action)) { let path = frappe.boot.beam.client[r[0].action][0] From 42ac08975b7e33a8a6be522d39e1e24877b47355 Mon Sep 17 00:00:00 2001 From: lauty95 Date: Thu, 6 Mar 2025 15:05:31 +0000 Subject: [PATCH 11/11] doc: comments --- beam/public/js/scan/scan.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/beam/public/js/scan/scan.js b/beam/public/js/scan/scan.js index a965600d..85467877 100644 --- a/beam/public/js/scan/scan.js +++ b/beam/public/js/scan/scan.js @@ -230,6 +230,12 @@ class ScanHandler { 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') }