MR : rohit_w
function my_before_submit(doc,cdt,cdn) {
$.each(cur_frm.doc.items || [], function(i, d) {
if(d.t_warehouse && (!d.target_locator || d.target_locator==“”)) {
validated = false;
msgprint(“Locator is not defined for target warehouse in row”+d.idx);
}
if(d.s_warehouse && (!d.locator || d.locator==“”)) {
validated = false;
msgprint(“Locator is not defined for source warehouse in row”+d.idx);
}
});
}
cur_frm.cscript.after_save = function(doc,cdt,cdn) {
validated = true;
cur_frm.doc.flg = 0;
$.each(cur_frm.doc.items || [], function(i, d) {
if(d.t_warehouse && (!d.target_locator || d.target_locator==“”)) {
cur_frm.call({
method: “frappe.client.get_value”,
args: {
doctype: “Locator”,
fieldname: “name”,
filters: { parent: d.t_warehouse },
},
callback: function(r, rt) {
if(r.message) {
validated = false;
cur_frm.doc.flg = -1;
msgprint(“Locator is not defined for target warehouse in row”+d.idx);
}
}
});
}
});
}
cur_frm.cscript.custom_s_warehouse = function(doc,cdt,cdn) {
d = locals[cdt][cdn];
hhh = frappe.call({
method:“frappe.direction.customizations.get_default_locator”,
args: {warehouse: d.s_warehouse},
callback: function(r,rt) {
if(!r.exc && r.message[0].loc>=1) {
d.locator = r.message[0].locator;
refresh_field(‘locator’,d.name,‘items’);
} else {
d.locator = “”;
refresh_field(“locator”,d.name,“items”);
}
}
});
}
cur_frm.cscript.custom_t_warehouse = function(doc,cdt,cdn) {
d = locals[cdt][cdn];
hhh = frappe.call({
method:“frappe.direction.customizations.get_default_locator”,
args: {warehouse: d.t_warehouse},
callback: function(r,rt) {
if(!r.exc && r.message[0].loc>=1) {
d.target_locator = r.message[0].locator;
refresh_field(‘target_locator’,d.name,‘items’);
} else {
d.target_locator = “”;
refresh_field(“target_locator”,d.name,“items”);
}
}
});
}
cur_frm.cscript.before_submit = function(doc,cdt,cdn) {
my_before_submit();
if (cur_frm.doc.flg<2 && cur_frm.doc.flg>=0) {
cur_frm.doc.flg = 0;
validated = false;
}
if (cur_frm.doc.flg==-1) {
validated = false;
msgprint(“Did Not Save \nUndefined locator !!!”);
} else {
$.each(cur_frm.doc.items || [], function(i, d) {
if(d.s_warehouse) {
hhh = frappe.call({
method:“frappe.direction.customizations.check_locator_bal”,
args: {item_code: d.item_code, warehouse: d.s_warehouse, locator: d.locator || “xxx”, qty: d.qty},
callback: function(r,rt) {
if(!r.exc && r.message[0].flg<0) {
cur_frm.doc.flg = 1;
validated = false;
th_msg = “item: ”+r.message[0].item_code+“ | “;
th_msg += “warehouse: ”+r.message[0].warehouse+” | “;
th_msg += “locator: ”+r.message[0].locator+”\n Have Quantity: ”+r.message[0].qty;
th_msg += “ less than quantity entered in Row# ”+d.idx.toString()+“”;
//cur_frm.disable_save();
frappe.throw(th_msg);
//msgprint(th_msg);
}
if((!r.exc) && cur_frm.doc.flg!=1 && r.message[0].flg>=0) {
cur_frm.doc.flg = 2;
if(d.length==d.idx) {
validated = true;
cur_frm.save(“Submit”);
}
}
}
});
}
else {
if(cur_frm.doc.flg!=-1) {
cur_frm.doc.flg = 2;
validated = true;
cur_frm.save(“Submit”);
}
}
});
}
}
cur_frm.fields_dict[‘items’].grid.get_field(‘locator’).get_query = function(doc, cdt, cdn) {
var item = locals[cdt][cdn];
if(!item.item_code) {
frappe.throw(__(“Please enter Item Code to get Warehouses”));
}
else {
var filters = {
‘item_code’: item.item_code,
‘warehouse’: item.s_warehouse
}
return {
query : "frappe.direction.customizations.locator_query",
filters: filters
}
}
}
cur_frm.fields_dict[‘items’].grid.get_field(‘target_locator’).get_query = function(doc, cdt, cdn) {
var item = locals[cdt][cdn];
return {
filters: {‘parent’: item.t_warehouse}
}
}
cur_frm.cscript.custom_onload = function(doc,cdt,cdn) {
cur_frm.get_field(‘items’).grid.editable_fields = [
{fieldname: ‘item_code’, columns: 2},
{fieldname: ‘item_name’, columns: 2},
{fieldname: ‘qty’, columns: 2},
{fieldname: ‘s_warehouse’, columns: 2},
{fieldname: ‘t_warehouse’, columns: 2}
];
}
// Fix empty locator after selecting an item
frappe.ui.form.on(“Stock Entry Detail”, {
“item_code”: function(doc, cdt, cdn) {
var d = locals[cdt][cdn];
var isDone=false;
$(document).ajaxStop(function() {
if(isDone)return;
isDone=true;
if (d.s_warehouse) {
//alert(d.warehouse);
cur_frm.cscript.custom_s_warehouse(doc, cdt, cdn);
}
if (d.t_warehouse) {
//alert(d.warehouse);
cur_frm.cscript.custom_t_warehouse(doc, cdt, cdn);
}
frappe.ui.form.on(“Material Request”, “validate”, function(frm) {
if(user=="test1@gmail.com" && frm.doc.purpose!=“Material Receipt”) {
msgprint(“You are only allowed Material Receipt”);
throw “Not allowed”;
}
}
});
}
});