fix: Commonified code and added server side validation

This commit is contained in:
marination 2020-04-02 13:51:25 +05:30
parent 3251dcf672
commit 686a09620d
2 changed files with 23 additions and 20 deletions

View File

@ -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)) { if (!(frm.doc.locations && frm.doc.locations.length)) {
frappe.msgprint(__('Add items in the Item Locations table')); frappe.msgprint(__('Add items in the Item Locations table'));
} else { } else {
frm.call('set_item_locations', {save: save}); frm.call('set_item_locations', {save: save});
} }
}, },
get_item_locations: (frm) => {
// Button on the form
frm.events.set_item_locations(frm, false);
},
refresh: (frm) => { refresh: (frm) => {
frm.trigger('add_get_items_button'); frm.trigger('add_get_items_button');
if (frm.doc.docstatus === 1) { if (frm.doc.docstatus === 1) {
@ -106,30 +110,22 @@ frappe.ui.form.on('Pick List', {
frm.trigger('add_get_items_button'); frm.trigger('add_get_items_button');
}, },
create_delivery_note: (frm) => { create_delivery_note: (frm) => {
if (!(frm.doc.locations && frm.doc.locations.length)) { frappe.model.open_mapped_doc({
frappe.msgprint(__('Add items in the Item Locations table')); method: 'erpnext.stock.doctype.pick_list.pick_list.create_delivery_note',
} else { frm: frm
frappe.model.open_mapped_doc({ });
method: 'erpnext.stock.doctype.pick_list.pick_list.create_delivery_note',
frm: frm
});
}
}, },
create_stock_entry: (frm) => { create_stock_entry: (frm) => {
if (!(frm.doc.locations && frm.doc.locations.length)) { frappe.xcall('erpnext.stock.doctype.pick_list.pick_list.create_stock_entry', {
frappe.msgprint(__('Add items in the Item Locations table')); 'pick_list': frm.doc,
} else { }).then(stock_entry => {
frappe.xcall('erpnext.stock.doctype.pick_list.pick_list.create_stock_entry', { frappe.model.sync(stock_entry);
'pick_list': frm.doc, frappe.set_route("Form", 'Stock Entry', stock_entry.name);
}).then(stock_entry => { });
frappe.model.sync(stock_entry);
frappe.set_route("Form", 'Stock Entry', stock_entry.name);
});
}
}, },
update_pick_list_stock: (frm) => { update_pick_list_stock: (frm) => {
frm.events.get_item_locations(frm, true); frm.events.set_item_locations(frm, true);
}, },
add_get_items_button: (frm) => { add_get_items_button: (frm) => {
let purpose = frm.doc.purpose; let purpose = frm.doc.purpose;

View File

@ -90,6 +90,10 @@ class PickList(Document):
return item_map.values() 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): def get_items_with_location_and_quantity(item_doc, item_location_map):
available_locations = item_location_map.get(item_doc.item_code) available_locations = item_location_map.get(item_doc.item_code)
locations = [] locations = []
@ -241,6 +245,8 @@ def get_available_item_locations_for_other_item(item_code, from_warehouses, requ
@frappe.whitelist() @frappe.whitelist()
def create_delivery_note(source_name, target_doc=None): def create_delivery_note(source_name, target_doc=None):
pick_list = frappe.get_doc('Pick List', source_name) 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 = [d.sales_order for d in pick_list.locations if d.sales_order]
sales_orders = set(sales_orders) sales_orders = set(sales_orders)
@ -300,6 +306,7 @@ def create_delivery_note(source_name, target_doc=None):
@frappe.whitelist() @frappe.whitelist()
def create_stock_entry(pick_list): def create_stock_entry(pick_list):
pick_list = frappe.get_doc(json.loads(pick_list)) pick_list = frappe.get_doc(json.loads(pick_list))
validate_item_locations(pick_list)
if stock_entry_exists(pick_list.get('name')): if stock_entry_exists(pick_list.get('name')):
return frappe.msgprint(_('Stock Entry has been already created against this Pick List')) return frappe.msgprint(_('Stock Entry has been already created against this Pick List'))