Try ERPNext Buy Support Partners Foundation

How to use array in Custom Script (javascript) for Sales Incentive calculation?

Hi everyone, I wanted to calculate Sales Incentive on top of Sales Commissions. Our staff, if their total Sales Commissions reaches a specific tier, they will earn an extra income (Sales Incentive). To calculate Sales Incentive, we have to refer to our Sales Incentive table, named ‘EEDCA’ as shown below.

image
How it works? We will calculate the total Sales Commission (salary component) (if Calculate Sales Incentive is checked) as shown below. The total will be measured against the table; we will then reward our sales staff with the ‘Sales Incentive’
image

Below is my code, but it isn’t returning the value to the custom field ‘Sales Incentive’ in Salary Slip doctype.

frappe.ui.form.on('Salary Detail', 'sales_incentive', function(frm, cdt, cdn) {
    set_sales_incentive(frm);
});

const eedca= 
    {from: 1000.01, to: 1100.00, sales_incentive: 0.00},
    {from: 1100.01, to: 1200.00, sales_incentive: 30.00},
    {from: 1200.01, to: 1400.00, sales_incentive: 50.00},
    {from: 1400.01, to: 1600.00, sales_incentive: 70.00},
    {from: 1600.01, to: 1800.00, sales_incentive: 90.00},
    {from: 1800.01, to: 2000.00, sales_incentive: 110.00},
    {from: 2000.01, to: 2200.00, sales_incentive: 140.00},
    {from: 2200.01, to: 2400.00, sales_incentive: 160.00},
    {from: 2400.01, to 2600.00, sales_incentive: 180.00},
    {from: 2600.01, to: 2800.00, sales_incentive: 200.00}
]

var set_sales_incentive = function(frm) {
    var sales_incentive = 0.0;
    $.each(frm.doc.earnings, function(i, row) {
        if (row.calculate_sales_incentive == 1) {
            sales_incentive += flt(row.amount);
        }
    });
    frm.set_value('sales_incentive', sales_incentive);
};

Can anyone help me? What I want to achieve is to return only ‘sales_incentive’ from the array to my custom field ‘sales_incentive’ in Salary Slip doctype.

The reason I am using this method because the table is actually much longer than this; hence, I don’t think ‘putting’ the whole table into the existing formula docfield in Salary Component doctype is a viable & feasible way of doing it. On the other hand, I am not familiar with python, so the best option for me is via Custom Script using javascript.

You can try by adding frm.get_field("sales_incentive").refresh() after frm.set_value('sales_incentive', sales_incentive);. Another thing that you should add is [ after eedca=.
And i don’t know if this is correct, but maybe you want this:

frappe.ui.form.on('Salary Detail', 'calculate_sales_incentive', function(frm, cdt, cdn) {
    set_sales_incentive(frm);
});

Instead of:

frappe.ui.form.on('Salary Detail', 'sales_incentive', function(frm, cdt, cdn) {
    set_sales_incentive(frm);
});
1 Like