From c5d108c954eee2841593eb4b0516ec7ba58a885e Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 23 Apr 2020 00:38:19 +0530 Subject: [PATCH 1/2] chore: Commonify autofilling warehouses in child tables --- .../doctype/work_order/work_order.js | 5 +++ erpnext/public/js/controllers/buying.js | 7 ++++ erpnext/public/js/controllers/transaction.js | 15 +++------ erpnext/selling/sales_common.js | 2 +- .../doctype/delivery_note/delivery_note.js | 8 +++++ .../stock/doctype/stock_entry/stock_entry.js | 32 +++---------------- 6 files changed, 31 insertions(+), 38 deletions(-) diff --git a/erpnext/manufacturing/doctype/work_order/work_order.js b/erpnext/manufacturing/doctype/work_order/work_order.js index 2d8d75d1f94..d14c8d82f11 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.js +++ b/erpnext/manufacturing/doctype/work_order/work_order.js @@ -122,6 +122,11 @@ frappe.ui.form.on("Work Order", { } }, + source_warehouse: function(frm) { + let transaction_controller = new erpnext.TransactionController(); + transaction_controller.autofill_warehouse(frm.doc.required_items, "source_warehouse", frm.doc.source_warehouse); + }, + refresh: function(frm) { erpnext.toggle_naming_series(); erpnext.work_order.set_custom_buttons(frm); diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js index afbdbc661d3..802cc056c6a 100644 --- a/erpnext/public/js/controllers/buying.js +++ b/erpnext/public/js/controllers/buying.js @@ -253,6 +253,13 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({ } }, + rejected_warehouse: function(doc, cdt) { + // trigger autofill_warehouse only if parent rejected_warehouse field is triggered + if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) { + this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse); + } + }, + category: function(doc, cdt, cdn) { // should be the category field of tax table if(cdt != doc.doctype) { diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 85d9799ce56..215282161d1 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1843,21 +1843,16 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ }, set_reserve_warehouse: function() { - this.autofill_warehouse("reserve_warehouse"); + this.autofill_warehouse(this.frm.doc.supplied_items, "reserve_warehouse", this.frm.doc.set_reserve_warehouse); }, set_warehouse: function() { - this.autofill_warehouse("warehouse"); + this.autofill_warehouse(this.frm.doc.items, "warehouse", this.frm.doc.set_warehouse); }, - autofill_warehouse : function (warehouse_field) { - // set warehouse in all child table rows - var me = this; - let warehouse = (warehouse_field === "warehouse") ? me.frm.doc.set_warehouse : me.frm.doc.set_reserve_warehouse; - let child_table = (warehouse_field === "warehouse") ? me.frm.doc.items : me.frm.doc.supplied_items; - let doctype = (warehouse_field === "warehouse") ? (me.frm.doctype + " Item") : (me.frm.doctype + " Item Supplied"); - - if(warehouse) { + autofill_warehouse : function (child_table, warehouse_field, warehouse) { + let doctype = child_table[0].doctype; + if (warehouse) { $.each(child_table || [], function(i, item) { frappe.model.set_value(doctype, item.name, warehouse_field, warehouse); }); diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index 095b7c3dffa..4a7dd5ad9b4 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -429,7 +429,7 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({ if (doc.has_serial_no && doc.serial_no) { args['serial_no'] = doc.serial_no } - + return frappe.call({ method: 'erpnext.stock.doctype.batch.batch.get_batch_no', args: args, diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js index 2ee68723789..62aebbaf504 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.js +++ b/erpnext/stock/doctype/delivery_note/delivery_note.js @@ -267,6 +267,14 @@ erpnext.stock.DeliveryNoteController = erpnext.selling.SellingController.extend( frappe.ui.form.is_saving = false; } }) + }, + + to_warehouse: function() { + let packed_items_table = this.frm.doc["packed_items"]; + this.autofill_warehouse(this.frm.doc["items"], "target_warehouse", this.frm.doc.to_warehouse); + if (packed_items_table && packed_items_table.length) { + this.autofill_warehouse(packed_items_table, "target_warehouse", this.frm.doc.to_warehouse); + } } }); diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index c54e3cdfb87..b3923bc4437 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -799,39 +799,17 @@ erpnext.stock.StockEntry = erpnext.stock.StockController.extend({ if(!row.t_warehouse) row.t_warehouse = this.frm.doc.to_warehouse; }, - source_mandatory: ["Material Issue", "Material Transfer", "Send to Subcontractor", - "Material Transfer for Manufacture", "Send to Warehouse", "Receive at Warehouse"], - target_mandatory: ["Material Receipt", "Material Transfer", "Send to Subcontractor", - "Material Transfer for Manufacture", "Send to Warehouse", "Receive at Warehouse"], - from_warehouse: function(doc) { - var me = this; - this.set_warehouse_if_different("s_warehouse", doc.from_warehouse, function(row) { - return me.source_mandatory.indexOf(me.frm.doc.purpose)!==-1; - }); + this.set_warehouse_in_children(doc.items, "s_warehouse", doc.from_warehouse); }, to_warehouse: function(doc) { - var me = this; - this.set_warehouse_if_different("t_warehouse", doc.to_warehouse, function(row) { - return me.target_mandatory.indexOf(me.frm.doc.purpose)!==-1; - }); + this.set_warehouse_in_children(doc.items, "t_warehouse", doc.to_warehouse); }, - set_warehouse_if_different: function(fieldname, value, condition) { - var changed = false; - for (var i=0, l=(this.frm.doc.items || []).length; i Date: Tue, 28 Apr 2020 19:18:45 +0530 Subject: [PATCH 2/2] fix: Handle empty child table --- erpnext/public/js/controllers/transaction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 215282161d1..7b817f66afa 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1851,8 +1851,8 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ }, autofill_warehouse : function (child_table, warehouse_field, warehouse) { - let doctype = child_table[0].doctype; - if (warehouse) { + if (warehouse && child_table && child_table.length) { + let doctype = child_table[0].doctype; $.each(child_table || [], function(i, item) { frappe.model.set_value(doctype, item.name, warehouse_field, warehouse); });