refactor: packing_slip.js

This commit is contained in:
s-aga-r
2023-04-27 19:43:37 +05:30
parent 380dd73065
commit b62bf78814

View File

@@ -1,113 +1,122 @@
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors // Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
// License: GNU General Public License v3. See license.txt // For license information, please see license.txt
cur_frm.fields_dict['delivery_note'].get_query = function(doc, cdt, cdn) { frappe.ui.form.on("Packing Slip", {
return{ setup: (frm) => {
filters:{ 'docstatus': 0} frm.set_query('delivery_note', () => {
} return {
} filters: {
docstatus: 0,
}
}
});
frm.set_query('item_code', 'items', (doc, cdt, cdn) => {
if (!doc.delivery_note) {
frappe.throw(__("Please select a Delivery Note"));
} else {
let d = locals[cdt][cdn];
return {
query: 'erpnext.stock.doctype.packing_slip.packing_slip.item_details',
filters: {
delivery_note: doc.delivery_note,
}
}
}
});
},
cur_frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) { refresh: (frm) => {
if(!doc.delivery_note) { frm.toggle_display("misc_details", frm.doc.amended_from);
frappe.throw(__("Please select a Delivery Note")); },
} else {
return { validate: (frm) => {
query: "erpnext.stock.doctype.packing_slip.packing_slip.item_details", frm.trigger("validate_case_nos");
filters:{ 'delivery_note': doc.delivery_note} frm.trigger("validate_calculate_item_details");
},
onload_post_render: (frm) => {
if(frm.doc.delivery_note && frm.doc.__islocal) {
frm.trigger("get_items");
} }
} },
}
cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) { get_items: (frm) => {
if(doc.delivery_note && doc.__islocal) { return frm.call({
cur_frm.cscript.get_items(doc, cdt, cdn); doc: frm.doc,
} method: "get_items",
} callback: function(r) {
if(!r.exc) {
frm.refresh();
}
}
});
},
cur_frm.cscript.get_items = function(doc, cdt, cdn) { // To Case No. cannot be less than From Case No.
return this.frm.call({ validate_case_nos: (frm) => {
doc: this.frm.doc, doc = locals[frm.doc.doctype][frm.doc.name];
method: "get_items",
callback: function(r) { if(cint(doc.from_case_no) == 0) {
if(!r.exc) cur_frm.refresh(); frappe.msgprint(__("The 'From Package No.' field must neither be empty nor it's value less than 1."));
frappe.validated = false;
} else if(!cint(doc.to_case_no)) {
doc.to_case_no = doc.from_case_no;
refresh_field('to_case_no');
} else if(cint(doc.to_case_no) < cint(doc.from_case_no)) {
frappe.msgprint(__("'To Case No.' cannot be less than 'From Case No.'"));
frappe.validated = false;
} }
}); },
}
cur_frm.cscript.refresh = function(doc, dt, dn) { validate_calculate_item_details: (frm) => {
cur_frm.toggle_display("misc_details", doc.amended_from); doc = locals[frm.doc.doctype][frm.doc.name];
} var ps_detail = doc.items || [];
cur_frm.cscript.validate = function(doc, cdt, cdn) { frm.events.validate_duplicate_items(doc, ps_detail);
cur_frm.cscript.validate_case_nos(doc); frm.events.calc_net_total_pkg(doc, ps_detail);
cur_frm.cscript.validate_calculate_item_details(doc); },
}
// To Case No. cannot be less than From Case No. // Do not allow duplicate items i.e. items with same item_code
cur_frm.cscript.validate_case_nos = function(doc) { // Also check for 0 qty
doc = locals[doc.doctype][doc.name]; validate_duplicate_items: (doc, ps_detail) => {
if(cint(doc.from_case_no)==0) { for(var i=0; i<ps_detail.length; i++) {
frappe.msgprint(__("The 'From Package No.' field must neither be empty nor it's value less than 1.")); for(var j=0; j<ps_detail.length; j++) {
frappe.validated = false; if(i!=j && ps_detail[i].item_code && ps_detail[i].item_code == ps_detail[j].item_code) {
} else if(!cint(doc.to_case_no)) { frappe.msgprint(__("You have entered duplicate items. Please rectify and try again."));
doc.to_case_no = doc.from_case_no; frappe.validated = false;
refresh_field('to_case_no'); return;
} else if(cint(doc.to_case_no) < cint(doc.from_case_no)) { }
frappe.msgprint(__("'To Case No.' cannot be less than 'From Case No.'")); }
frappe.validated = false;
}
}
if(flt(ps_detail[i].qty) <= 0) {
cur_frm.cscript.validate_calculate_item_details = function(doc) { frappe.msgprint(__("Invalid quantity specified for item {0}. Quantity should be greater than 0.", [ps_detail[i].item_code]));
doc = locals[doc.doctype][doc.name];
var ps_detail = doc.items || [];
cur_frm.cscript.validate_duplicate_items(doc, ps_detail);
cur_frm.cscript.calc_net_total_pkg(doc, ps_detail);
}
// Do not allow duplicate items i.e. items with same item_code
// Also check for 0 qty
cur_frm.cscript.validate_duplicate_items = function(doc, ps_detail) {
for(var i=0; i<ps_detail.length; i++) {
for(var j=0; j<ps_detail.length; j++) {
if(i!=j && ps_detail[i].item_code && ps_detail[i].item_code==ps_detail[j].item_code) {
frappe.msgprint(__("You have entered duplicate items. Please rectify and try again."));
frappe.validated = false; frappe.validated = false;
return;
} }
} }
if(flt(ps_detail[i].qty)<=0) { },
frappe.msgprint(__("Invalid quantity specified for item {0}. Quantity should be greater than 0.", [ps_detail[i].item_code]));
frappe.validated = false; // Calculate Net Weight of Package
calc_net_total_pkg: (doc, ps_detail) => {
var net_weight_pkg = 0;
doc.net_weight_uom = (ps_detail && ps_detail.length) ? ps_detail[0].weight_uom : '';
doc.gross_weight_uom = doc.net_weight_uom;
for(var i=0; i<ps_detail.length; i++) {
var item = ps_detail[i];
if(item.weight_uom != doc.net_weight_uom) {
frappe.msgprint(__("Different UOM for items will lead to incorrect (Total) Net Weight value. Make sure that Net Weight of each item is in the same UOM."));
frappe.validated = false;
}
net_weight_pkg += flt(item.net_weight) * flt(item.qty);
} }
}
}
doc.net_weight_pkg = roundNumber(net_weight_pkg, 2);
// Calculate Net Weight of Package if(!flt(doc.gross_weight_pkg)) {
cur_frm.cscript.calc_net_total_pkg = function(doc, ps_detail) { doc.gross_weight_pkg = doc.net_weight_pkg;
var net_weight_pkg = 0;
doc.net_weight_uom = (ps_detail && ps_detail.length) ? ps_detail[0].weight_uom : '';
doc.gross_weight_uom = doc.net_weight_uom;
for(var i=0; i<ps_detail.length; i++) {
var item = ps_detail[i];
if(item.weight_uom != doc.net_weight_uom) {
frappe.msgprint(__("Different UOM for items will lead to incorrect (Total) Net Weight value. Make sure that Net Weight of each item is in the same UOM."));
frappe.validated = false;
} }
net_weight_pkg += flt(item.net_weight) * flt(item.qty);
}
doc.net_weight_pkg = roundNumber(net_weight_pkg, 2); refresh_many(['net_weight_pkg', 'net_weight_uom', 'gross_weight_uom', 'gross_weight_pkg']);
if(!flt(doc.gross_weight_pkg)) {
doc.gross_weight_pkg = doc.net_weight_pkg;
} }
refresh_many(['net_weight_pkg', 'net_weight_uom', 'gross_weight_uom', 'gross_weight_pkg']); });
}
// TODO: validate gross weight field