Hi Community,
i had custom doctype “Job Delivery” in this childtable “Job Delivery Item” in that “description” field is having it should fetch in sales invoice item chilldtable but it is fetching from item master ; For “Get Items from Job Delivery” button in sales invoice my code is below
frappe.ui.form.on('Sales Invoice', {
refresh: function(frm) {
frm.add_custom_button(__("Get items from Job Delivery"), function() {
show_sinv_dialog(frm);
});
},
onload_post_render: function(frm) {
frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
return {
query: "frappe.client.get_list",
filters: [["Item", "disabled", "=", 0]]
};
};
frm.fields_dict['items'].grid.fields_map['description'].reqd = 1;
},
validate: function(frm) {
frm.fields_dict.items.grid.grid_rows.forEach(function(row) {
let item = row.doc;
// Check if the item description, rate, and amount have been set from Job Delivery
if (item.from_job_delivery) {
// Prevent standard code from overriding these values
row.fields_dict.description.df.read_only = 1;
row.fields_dict.rate.df.read_only = 1;
row.fields_dict.amount.df.read_only = 1;
}
});
}
});
function show_sinv_dialog(frm) {
frappe.prompt([
{
'fieldname': 'job_delivery',
'fieldtype': 'Link',
'label': 'Job Delivery',
'reqd': 1,
'options': 'Job Delivery'
}
], function(job_delivery) {
get_items_from_sinv(frm, job_delivery.job_delivery);
}, 'Get items from job delivery', 'Get items');
}
function get_items_from_sinv(frm, job_delivery) {
frappe.call({
method: "frappe.client.get",
args: {
doctype: "Job Delivery",
name: job_delivery
},
callback: function(response) {
var sinv = response.message;
if (!sinv) return;
frm.doc.customer = sinv.customer;
frm.refresh_field('customer');
frm.clear_table("items");
sinv.items.forEach(function(item) {
var child = frm.add_child('items');
frappe.model.set_value(child.doctype, child.name, 'item_code', item.item_code);
frappe.model.set_value(child.doctype, child.name, 'item_name', item.item_name);
frappe.model.set_value(child.doctype, child.name, 'description', item.description);
frappe.model.set_value(child.doctype, child.name, 'qty', item.qty);
frappe.model.set_value(child.doctype, child.name, 'rate', item.rate);
frappe.model.set_value(child.doctype, child.name, 'amount', item.amount);
// Custom flag to indicate these fields should not be overwritten
frappe.model.set_value(child.doctype, child.name, 'from_job_delivery', 1);
});
frm.refresh_field('items');
}
});
}
Thanks in Advance