From 1a947dbf6a3e8814176b0af2caa8f9a8935fff89 Mon Sep 17 00:00:00 2001 From: tundebabzy Date: Tue, 29 Aug 2017 13:48:27 +0100 Subject: [PATCH] Item Name not loaded in Material Request via BOM (#10535) (#10555) * adds item_name to child table * adds two new functions to utils: - first_row_is_empty: to check if first row in child table is empty - remove_empty_first_row: to remove the empty first row in a child table * removes empty first row after getting from BOM * ui test --- erpnext/public/js/utils.js | 28 ++++++++++++++++++- .../material_request/material_request.js | 2 ++ .../tests/test_material_request_from_bom.js | 28 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index eaf064b26c..a333ca82d6 100644 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -125,7 +125,33 @@ $.extend(erpnext.utils, { } }); } - } + }, + + /** + * Checks if the first row of a given child table is empty + * @param child_table - Child table Doctype + * @return {Boolean} + **/ + first_row_is_empty: function(child_table){ + if($.isArray(child_table) && child_table.length > 0) { + return !child_table[0].item_code; + } + return false; + }, + + /** + * Removes the first row of a child table if it is empty + * @param {_Frm} frm - The current form + * @param {String} child_table_name - The child table field name + * @return {Boolean} + **/ + remove_empty_first_row: function(frm, child_table_name){ + const rows = frm['doc'][child_table_name]; + if (this.first_row_is_empty(rows)){ + frm['doc'][child_table_name] = rows.splice(1); + } + return rows; + }, }); erpnext.utils.map_current_doc = function(opts) { diff --git a/erpnext/stock/doctype/material_request/material_request.js b/erpnext/stock/doctype/material_request/material_request.js index fc0b9b2106..7043fb7ba8 100644 --- a/erpnext/stock/doctype/material_request/material_request.js +++ b/erpnext/stock/doctype/material_request/material_request.js @@ -153,9 +153,11 @@ erpnext.buying.MaterialRequestController = erpnext.buying.BuyingController.exten if(!r.message) { frappe.throw(__("BOM does not contain any stock item")) } else { + erpnext.utils.remove_empty_first_row(cur_frm, "items"); $.each(r.message, function(i, item) { var d = frappe.model.add_child(cur_frm.doc, "Material Request Item", "items"); d.item_code = item.item_code; + d.item_name = item.item_name; d.description = item.description; d.warehouse = values.warehouse; d.uom = item.stock_uom; diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js b/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js new file mode 100644 index 0000000000..d8b39fe5aa --- /dev/null +++ b/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js @@ -0,0 +1,28 @@ +QUnit.module('manufacturing'); + +QUnit.test("test material request get items from BOM", function(assert) { + assert.expect(4); + let done = assert.async(); + frappe.run_serially([ + () => frappe.set_route('Form', 'BOM'), + () => frappe.timeout(3), + () => frappe.click_button('Get Items from BOM'), + () => frappe.timeout(3), + () => { + assert.ok(cur_dialog, 'dialog appeared'); + }, + () => cur_dialog.set_value('bom', 'Laptop'), + () => cur_dialog.set_value('warehouse', 'Laptop Scrap Warehouse'), + () => frappe.click_button('Get Items from BOM'), + () => frappe.timeout(3), + () => { + assert.ok(cur_frm.doc.items[0].item_code, "First row is not empty"); + assert.ok(cur_frm.doc.items[0].item_name, "Item name is not empty"); + assert.equal(cur_frm.doc.items[0].item_name, "Laptop", cur_frm.doc.items[0].item_name); + }, + () => cur_frm.doc.items[0].schedule_date = '2017-12-12', + () => cur_frm.save(), + () => done() + ]); +}); +