ERPNext version 15 when create a sales order discount and margin percentage applied when create the sales invoice line item rate is not match with the sales order item rate because of discount and margin.
Thanks for the reply NCP. I have used the custom script value we got the sales order items value but not set in sales invoice items per row section discount_percentage and discount_rate value
frappe.ui.form.on(“Sales Invoice”, {
onload: function(frm) {
console.log(“Sales Invoice loaded”, frm.doc);
if (frm.doc.items && frm.doc.items.length > 0) {
$.each(frm.doc.items || [], function(i, d) {
if (d.item_code && frm.doc.custom_sales_order) {
console.log('Fetching Sales Order for', d.item_code);
frappe.call({
method: "frappe.client.get",
args: {
doctype: "Sales Order",
name: frm.doc.custom_sales_order
},
callback: function(response) {
if (response.message) {
let custom_sales_order = response.message;
console.log('Sales Order fetched', custom_sales_order);
// Search for the matching item_code in Sales Order items
let matching_item = (custom_sales_order.items || []).find(item => item.item_code === d.item_code);
console.log('Matching item found?', matching_item);
if (matching_item) {
// Set the discount fields if found
console.log('Setting discount values');
frappe.model.set_value(d.doctype, d.name, "discount_percentage", matching_item.discount_percentage || 0);
frappe.model.set_value(d.doctype, d.name, "discount_amount", matching_item.discount_amount || 0);
} else {
console.log('Item not matching, setting default values');
// Fallback if item is not found
frappe.model.set_value(d.doctype, d.name, "discount_percentage", 0);
frappe.model.set_value(d.doctype, d.name, "discount_amount", 0);
}
} else {
console.log('No response from Sales Order');
}
},
error: function(err) {
console.log('Error fetching Sales Order', err);
}
});
}
});
} else {
console.log('No items found in Sales Invoice');
}
}
});
please provide solution how can i set the value in sales invoice items ??