From 3c7e7a76f0c89b6bedf0b27062895f05f68c1fa5 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:23:32 +0100 Subject: [PATCH 1/3] refactor: split batch --- erpnext/stock/doctype/batch/batch.py | 62 ++++++++++++++-------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 7b23f9ec01..e8e94fda31 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -9,7 +9,7 @@ from frappe import _ from frappe.model.document import Document from frappe.model.naming import make_autoname, revert_series_if_last from frappe.query_builder.functions import CurDate, Sum -from frappe.utils import cint, flt, get_link_to_form, nowtime, today +from frappe.utils import cint, flt, get_link_to_form from frappe.utils.data import add_days from frappe.utils.jinja import render_template @@ -248,8 +248,9 @@ def get_batches_by_oldest(item_code, warehouse): @frappe.whitelist() -def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None): - +def split_batch( + batch_no: str, item_code: str, warehouse: str, qty: float, new_batch_id: str | None = None +): """Split the batch into a new batch""" batch = frappe.get_doc(dict(doctype="Batch", item=item_code, batch_id=new_batch_id)).insert() qty = flt(qty) @@ -257,29 +258,21 @@ def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None): company = frappe.db.get_value("Warehouse", warehouse, "company") from_bundle_id = make_batch_bundle( - frappe._dict( - { - "item_code": item_code, - "warehouse": warehouse, - "batches": frappe._dict({batch_no: qty}), - "company": company, - "type_of_transaction": "Outward", - "qty": qty, - } - ) + item_code=item_code, + warehouse=warehouse, + batches=frappe._dict({batch_no: qty}), + company=company, + type_of_transaction="Outward", + qty=qty, ) to_bundle_id = make_batch_bundle( - frappe._dict( - { - "item_code": item_code, - "warehouse": warehouse, - "batches": frappe._dict({batch.name: qty}), - "company": company, - "type_of_transaction": "Inward", - "qty": qty, - } - ) + item_code=item_code, + warehouse=warehouse, + batches=frappe._dict({batch.name: qty}), + company=company, + type_of_transaction="Inward", + qty=qty, ) stock_entry = frappe.get_doc( @@ -304,21 +297,30 @@ def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None): return batch.name -def make_batch_bundle(kwargs): +def make_batch_bundle( + item_code: str, + warehouse: str, + batches: dict[str, float], + company: str, + type_of_transaction: str, + qty: float, +): + from frappe.utils import nowtime, today + from erpnext.stock.serial_batch_bundle import SerialBatchCreation return ( SerialBatchCreation( { - "item_code": kwargs.item_code, - "warehouse": kwargs.warehouse, + "item_code": item_code, + "warehouse": warehouse, "posting_date": today(), "posting_time": nowtime(), "voucher_type": "Stock Entry", - "qty": flt(kwargs.qty), - "type_of_transaction": kwargs.type_of_transaction, - "company": kwargs.company, - "batches": kwargs.batches, + "qty": qty, + "type_of_transaction": type_of_transaction, + "company": company, + "batches": batches, "do_not_submit": True, } ) From 34ec2f8a2b805e63c531ad293f3adb2061c219eb Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:57:23 +0100 Subject: [PATCH 2/3] fix(Batch): reload doc after splitting to show updated qty --- erpnext/stock/doctype/batch/batch.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.js b/erpnext/stock/doctype/batch/batch.js index 7bf7a1f65d..2d12a27d60 100644 --- a/erpnext/stock/doctype/batch/batch.js +++ b/erpnext/stock/doctype/batch/batch.js @@ -128,19 +128,16 @@ frappe.ui.form.on('Batch', { fieldtype: 'Data', }], (data) => { - frappe.call({ - method: 'erpnext.stock.doctype.batch.batch.split_batch', - args: { + frappe.xcall( + 'erpnext.stock.doctype.batch.batch.split_batch', + { item_code: frm.doc.item, batch_no: frm.doc.name, qty: data.qty, warehouse: $btn.attr('data-warehouse'), new_batch_id: data.new_batch_id - }, - callback: (r) => { - frm.refresh(); - }, - }); + } + ).then(() => frm.reload_doc()); }, __('Split Batch'), __('Split') From 7a7a2132855e496a0b1d51698cdb7d675c527650 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:02:42 +0100 Subject: [PATCH 3/3] refactor(Batch): use const instead of var --- erpnext/stock/doctype/batch/batch.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.js b/erpnext/stock/doctype/batch/batch.js index 2d12a27d60..f4a935aa90 100644 --- a/erpnext/stock/doctype/batch/batch.js +++ b/erpnext/stock/doctype/batch/batch.js @@ -52,7 +52,7 @@ frappe.ui.form.on('Batch', { // sort by qty r.message.sort(function(a, b) { a.qty > b.qty ? 1 : -1 }); - var rows = $('
').appendTo(section); + const rows = $('
').appendTo(section); // show (r.message || []).forEach(function(d) { @@ -76,7 +76,7 @@ frappe.ui.form.on('Batch', { // move - ask for target warehouse and make stock entry rows.find('.btn-move').on('click', function() { - var $btn = $(this); + const $btn = $(this); const fields = [ { fieldname: 'to_warehouse', @@ -115,7 +115,7 @@ frappe.ui.form.on('Batch', { // split - ask for new qty and batch ID (optional) // and make stock entry via batch.batch_split rows.find('.btn-split').on('click', function() { - var $btn = $(this); + const $btn = $(this); frappe.prompt([{ fieldname: 'qty', label: __('New Batch Qty'),