Try ERPNext Buy Support Partners Foundation

Calculate each product total discount amount in sales order item

I wrote the following script to calculate the total discount amount in sales order item

custom script:
doctype: Sales Order Item
script:
frappe.ui.form.on(“Sales Order Item”, {
validate: function(frm) {
disprice = 0
disprice += flt(frm.doc.price_list_rate) * flt(frm.doc.discount_percentage) / 100 * flt(frm.doc.qty);
frm.doc.disprice = disprice;
}
})

custom field:
document: Sales Order Item
Label: disprice
Fieldname: disprice
Insert After: Rate
Field Type: Currency

The disprice field apper in sales order item table but always empty.
(Test Order apply product with price rule discount)

1 Like

Calculate discount price for each Salary Order Item.

frappe.ui.form.on("Sales Order", {
	validate: function(frm) {    		
		$.each(frm.doc.items || [], function(i, d) {
                    disprice = 0;
                    disprice = flt(d.price_list_rate) * flt(d.discount_percentage) / 100 * flt(d.qty);
                    frappe.model.set_value(d.doctype, d.name, "disprice", disprice);
		});
});

custom script:
doctype: Sales Order Item
script:
frappe.ui.form.on(“Sales Order”, {
validate: function(frm) {
$.each(frm.doc.items || [], function(i, d) {
disprice = 0;
disprice = flt(d.price_list_rate) * flt(d.discount_percentage) / 100 * flt(d.qty);
frappe.model.set_value(d.doctype, d.name, “disprice”, disprice);
});
});

custom field:
document: Sales Order Item
Label: disprice
Fieldname: disprice
Insert After: Rate
Field Type: Currency

No value in “disprice” field.
Anything wrong?

Any error in browser console?

No Error.

Please create custom script in Sales Order doctype. Not Sales Order Item doctype.

Show error in custom script when I reload the page @sales order

SyntaxError: Unexpected token )
at Class.setup (http://[DOMAIN]/assets/js/form.min.js:2624:18)
at _f.Frm.setup (http://[DOMAIN]/assets/js/form.min.js:172:22)
at _f.Frm.refresh (http://[DOMAIN]/assets/js/form.min.js:439:9)
at Class.load (http://[DOMAIN]/assets/js/form.min.js:87:33)
at http://[DOMAIN]/assets/js/form.min.js:82:7
at Object.callback (http://[DOMAIN]/assets/js/desk.min.js:6635:6)
at Object.callback [as success_callback] (http://[DOMAIN]/assets/js/desk.min.js:1228:16)
at _ (http://[DOMAIN]/assets/js/desk.min.js:1252:34)
at Object. (http://[DOMAIN]/assets/js/desk.min.js:1349:5)
at i (http://[DOMAIN]/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

When you are Sales Order form, try to run code below in console and let me know if there’s any error.
Please make sure you have correct double quote (""), sometime it has wrong double by copy & paste code from the forum.

$.each(cur_frm.doc.items || [], function(i, d) {
        var disprice = 0;
        disprice = flt(d.price_list_rate) * flt(d.discount_percentage) / 100 * flt(d.qty);
        frappe.model.set_value(d.doctype, d.name, "disprice", disprice);
});

Edit: make sure you have item in Sales Order Item.

It works fine if I del the custom script and type the above cmd in brower console.
The disprice field show crroect value.

Did you create custom script under Sales Order or Sales Order Item doctype?

customscript doctype Sales Order will get error in sales order page.
SyntaxError: Unexpected token )
at Class.setup (http://[DOMAIN]/assets/js/form.min.js:2624:18)
at _f.Frm.setup (http://[DOMAIN]/assets/js/form.min.js:172:22)
at _f.Frm.refresh (http://[DOMAIN]/assets/js/form.min.js:439:9)
at Class.load (http://[DOMAIN]/assets/js/form.min.js:87:33)
at http://[DOMAIN]/assets/js/form.min.js:82:7
at Object.callback (http://[DOMAIN]/assets/js/desk.min.js:6635:6)
at Object.callback [as success_callback] (http://[DOMAIN]/assets/js/desk.min.js:1228:16)
at _ (http://[DOMAIN]/assets/js/desk.min.js:1252:34)
at Object. (http://[DOMAIN]/assets/js/desk.min.js:1349:5)
at i (http://[DOMAIN]/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

customscript doctype Sales Order Item will get “disprice field” blank in sales order item page.

There was a missing closed curly bracket. Please try code below.

frappe.ui.form.on("Sales Order", {
	validate: function(frm) {    		
		$.each(frm.doc.items || [], function(i, d) {
                    disprice = 0;
                    disprice = flt(d.price_list_rate) * flt(d.discount_percentage) / 100 * flt(d.qty);
                    frappe.model.set_value(d.doctype, d.name, "disprice", disprice);
		});
     }
});
2 Likes

Tried,no error show when reload in sales order page but the “disprice” field blank(0.00)

After the Sales Order form saved, you will see the disprice value.

It works.
How can I show the currency synbol when I print it out? I select currency in custom field, but it show the disprice amount w/o currency symbol.

Thanks.

Edit: Currency symbol apper in sales order item but disapper when print.
2nd edit: Problem solve after add “currency” in custom field -> options

1 Like

I tried but the disprice is 0.00 only

I submitted the SO also but still the disprice is showing 0.00 only