Merge pull request #39512 from barredterra/refactor-split-batch

This commit is contained in:
Raffael Meyer 2024-01-23 17:54:28 +01:00 committed by GitHub
commit 7a6a789199
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 41 deletions

View File

@ -52,7 +52,7 @@ frappe.ui.form.on('Batch', {
// sort by qty // sort by qty
r.message.sort(function(a, b) { a.qty > b.qty ? 1 : -1 }); r.message.sort(function(a, b) { a.qty > b.qty ? 1 : -1 });
var rows = $('<div></div>').appendTo(section); const rows = $('<div></div>').appendTo(section);
// show // show
(r.message || []).forEach(function(d) { (r.message || []).forEach(function(d) {
@ -76,7 +76,7 @@ frappe.ui.form.on('Batch', {
// move - ask for target warehouse and make stock entry // move - ask for target warehouse and make stock entry
rows.find('.btn-move').on('click', function() { rows.find('.btn-move').on('click', function() {
var $btn = $(this); const $btn = $(this);
const fields = [ const fields = [
{ {
fieldname: 'to_warehouse', fieldname: 'to_warehouse',
@ -115,7 +115,7 @@ frappe.ui.form.on('Batch', {
// split - ask for new qty and batch ID (optional) // split - ask for new qty and batch ID (optional)
// and make stock entry via batch.batch_split // and make stock entry via batch.batch_split
rows.find('.btn-split').on('click', function() { rows.find('.btn-split').on('click', function() {
var $btn = $(this); const $btn = $(this);
frappe.prompt([{ frappe.prompt([{
fieldname: 'qty', fieldname: 'qty',
label: __('New Batch Qty'), label: __('New Batch Qty'),
@ -128,19 +128,16 @@ frappe.ui.form.on('Batch', {
fieldtype: 'Data', fieldtype: 'Data',
}], }],
(data) => { (data) => {
frappe.call({ frappe.xcall(
method: 'erpnext.stock.doctype.batch.batch.split_batch', 'erpnext.stock.doctype.batch.batch.split_batch',
args: { {
item_code: frm.doc.item, item_code: frm.doc.item,
batch_no: frm.doc.name, batch_no: frm.doc.name,
qty: data.qty, qty: data.qty,
warehouse: $btn.attr('data-warehouse'), warehouse: $btn.attr('data-warehouse'),
new_batch_id: data.new_batch_id new_batch_id: data.new_batch_id
}, }
callback: (r) => { ).then(() => frm.reload_doc());
frm.refresh();
},
});
}, },
__('Split Batch'), __('Split Batch'),
__('Split') __('Split')

View File

@ -9,7 +9,7 @@ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.model.naming import make_autoname, revert_series_if_last from frappe.model.naming import make_autoname, revert_series_if_last
from frappe.query_builder.functions import CurDate, Sum 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.data import add_days
from frappe.utils.jinja import render_template from frappe.utils.jinja import render_template
@ -248,8 +248,9 @@ def get_batches_by_oldest(item_code, warehouse):
@frappe.whitelist() @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""" """Split the batch into a new batch"""
batch = frappe.get_doc(dict(doctype="Batch", item=item_code, batch_id=new_batch_id)).insert() batch = frappe.get_doc(dict(doctype="Batch", item=item_code, batch_id=new_batch_id)).insert()
qty = flt(qty) 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") company = frappe.db.get_value("Warehouse", warehouse, "company")
from_bundle_id = make_batch_bundle( from_bundle_id = make_batch_bundle(
frappe._dict( item_code=item_code,
{ warehouse=warehouse,
"item_code": item_code, batches=frappe._dict({batch_no: qty}),
"warehouse": warehouse, company=company,
"batches": frappe._dict({batch_no: qty}), type_of_transaction="Outward",
"company": company, qty=qty,
"type_of_transaction": "Outward",
"qty": qty,
}
)
) )
to_bundle_id = make_batch_bundle( to_bundle_id = make_batch_bundle(
frappe._dict( item_code=item_code,
{ warehouse=warehouse,
"item_code": item_code, batches=frappe._dict({batch.name: qty}),
"warehouse": warehouse, company=company,
"batches": frappe._dict({batch.name: qty}), type_of_transaction="Inward",
"company": company, qty=qty,
"type_of_transaction": "Inward",
"qty": qty,
}
)
) )
stock_entry = frappe.get_doc( 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 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 from erpnext.stock.serial_batch_bundle import SerialBatchCreation
return ( return (
SerialBatchCreation( SerialBatchCreation(
{ {
"item_code": kwargs.item_code, "item_code": item_code,
"warehouse": kwargs.warehouse, "warehouse": warehouse,
"posting_date": today(), "posting_date": today(),
"posting_time": nowtime(), "posting_time": nowtime(),
"voucher_type": "Stock Entry", "voucher_type": "Stock Entry",
"qty": flt(kwargs.qty), "qty": qty,
"type_of_transaction": kwargs.type_of_transaction, "type_of_transaction": type_of_transaction,
"company": kwargs.company, "company": company,
"batches": kwargs.batches, "batches": batches,
"do_not_submit": True, "do_not_submit": True,
} }
) )