From 686a09620db6874729fcc59dbfea749398d290e6 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 2 Apr 2020 13:51:25 +0530 Subject: [PATCH] fix: Commonified code and added server side validation --- erpnext/stock/doctype/pick_list/pick_list.js | 36 +++++++++----------- erpnext/stock/doctype/pick_list/pick_list.py | 7 ++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/erpnext/stock/doctype/pick_list/pick_list.js b/erpnext/stock/doctype/pick_list/pick_list.js index f4fd0a202f..d46b98b461 100644 --- a/erpnext/stock/doctype/pick_list/pick_list.js +++ b/erpnext/stock/doctype/pick_list/pick_list.js @@ -38,13 +38,17 @@ frappe.ui.form.on('Pick List', { }; }); }, - get_item_locations: (frm, save=false) => { + set_item_locations:(frm, save) => { if (!(frm.doc.locations && frm.doc.locations.length)) { frappe.msgprint(__('Add items in the Item Locations table')); } else { frm.call('set_item_locations', {save: save}); } }, + get_item_locations: (frm) => { + // Button on the form + frm.events.set_item_locations(frm, false); + }, refresh: (frm) => { frm.trigger('add_get_items_button'); if (frm.doc.docstatus === 1) { @@ -106,30 +110,22 @@ frappe.ui.form.on('Pick List', { frm.trigger('add_get_items_button'); }, create_delivery_note: (frm) => { - if (!(frm.doc.locations && frm.doc.locations.length)) { - frappe.msgprint(__('Add items in the Item Locations table')); - } else { - frappe.model.open_mapped_doc({ - method: 'erpnext.stock.doctype.pick_list.pick_list.create_delivery_note', - frm: frm - }); - } + frappe.model.open_mapped_doc({ + method: 'erpnext.stock.doctype.pick_list.pick_list.create_delivery_note', + frm: frm + }); }, create_stock_entry: (frm) => { - if (!(frm.doc.locations && frm.doc.locations.length)) { - frappe.msgprint(__('Add items in the Item Locations table')); - } else { - frappe.xcall('erpnext.stock.doctype.pick_list.pick_list.create_stock_entry', { - 'pick_list': frm.doc, - }).then(stock_entry => { - frappe.model.sync(stock_entry); - frappe.set_route("Form", 'Stock Entry', stock_entry.name); - }); - } + frappe.xcall('erpnext.stock.doctype.pick_list.pick_list.create_stock_entry', { + 'pick_list': frm.doc, + }).then(stock_entry => { + frappe.model.sync(stock_entry); + frappe.set_route("Form", 'Stock Entry', stock_entry.name); + }); }, update_pick_list_stock: (frm) => { - frm.events.get_item_locations(frm, true); + frm.events.set_item_locations(frm, true); }, add_get_items_button: (frm) => { let purpose = frm.doc.purpose; diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py index d7afaf3d68..616de5e00a 100644 --- a/erpnext/stock/doctype/pick_list/pick_list.py +++ b/erpnext/stock/doctype/pick_list/pick_list.py @@ -90,6 +90,10 @@ class PickList(Document): return item_map.values() +def validate_item_locations(pick_list): + if not pick_list.locations: + frappe.throw(_("Add items in the Item Locations table")) + def get_items_with_location_and_quantity(item_doc, item_location_map): available_locations = item_location_map.get(item_doc.item_code) locations = [] @@ -241,6 +245,8 @@ def get_available_item_locations_for_other_item(item_code, from_warehouses, requ @frappe.whitelist() def create_delivery_note(source_name, target_doc=None): pick_list = frappe.get_doc('Pick List', source_name) + validate_item_locations(pick_list) + sales_orders = [d.sales_order for d in pick_list.locations if d.sales_order] sales_orders = set(sales_orders) @@ -300,6 +306,7 @@ def create_delivery_note(source_name, target_doc=None): @frappe.whitelist() def create_stock_entry(pick_list): pick_list = frappe.get_doc(json.loads(pick_list)) + validate_item_locations(pick_list) if stock_entry_exists(pick_list.get('name')): return frappe.msgprint(_('Stock Entry has been already created against this Pick List'))