Determine the Commission proportion and calculate the Commission according to the performance

Posted by fleabel on Sat, 05 Mar 2022 03:39:36 +0100

describe

Calculate the total performance of the salesman in the current month according to the trading volume of orders in the current month, determine the Commission grade of the salesman in the current month, obtain the Commission proportion, and automatically calculate all the commissions of the salesman's orders in the current month at the end of the month.

preparation in advance

In white code Low code development Create business representative table, commission class table, order table and performance commission table on the platform; In the business representative table set Commission class table, the field of commission class table: "performance left interval (yuan)" means greater than or equal to this value. For example, filling 50000 in "performance left interval (yuan)" means greater than or equal to 50000 yuan; "Performance right range" means less than this value. For example, if "performance right range (yuan)" is filled with 100000, it means less than 100000 yuan. The order table is simply represented.

 

Implementation steps:

1. Create a new "new operator" function to enter the basic information of the operator and the most important "Commission class".

Example effect:

 

 

2. Create a new "new order" function and enter the corresponding order data; When a new order data is created, a corresponding operator's performance commission information will be created synchronously.

Example effect:

 

 

3. Create a new "single operator commission calculation" function to count the monthly Cumulative Performance of a single operator, so as to obtain the Commission proportion corresponding to the total performance in the Commission class of the operator, calculate all commissions of the current month and update them to the performance commission table.

Add a new step of "interaction", select "operation" as "input", and "add attribute" in the attribute tab, with the alias set to "current time"

 

 

The performance calculation step is of "programming" type, and the effect is as follows:

 

code:

async function runProcess($model = model, $plugin = plugin, $params) {
    let commission_list = $params.commission_list;//Get salesman Commission class
    let salesman = $params.salesman; //Get salesman
    let date = new Date();
    date.setMonth(date.getMonth()-1);//last month
    date.setDate(1);//On the 1st of last month
    let params = await $model.getValue("6077ed6363070c5016868574");
    let now_str = params["field_1618472308212"];//Current date YYYY-MM-DD
    let YY = date.getFullYear();
    let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    let DD = (date.getDate() + 1 <= 10 ? '0' + date.getDate() : date.getDate());
    let time = YY + "-" + MM + "-" + DD;//1st of last month date str
    await $model.log("now:" + now_str);
    await $model.log("time:" + time);
    let achievement_list = await $plugin.data.queryData("607656e51221966e7625ae4c", {
        "$and": [{
            "6076578923ebd46e75f74344": salesman._id,     //Salesman id
            "607657951221966e7625ae4d": { "$gte": time }  //Greater than or equal to the 1st day of last month
        }, {
            "6076578923ebd46e75f74344": salesman._id,     //Salesman id
            "607657951221966e7625ae4d": { "$lt": now_str } //Less than current date (No. 1)
        }
        ]
    }, {
        all: true
    });//Query all the performance of the salesman in last month
    await $model.log(achievement_list.length);//Amount of Commission data obtained from salesman
    let total_amount = await $plugin.data.summaryData("607656e51221966e7625ae4c", "6076573e23ebd46e75f74341","6076578923ebd46e75f74344",{
        "$and": [{
            "6076578923ebd46e75f74344": salesman._id,     //Salesman id
            "607657951221966e7625ae4d": { "$gte": time }  //Greater than or equal to the 1st day of last month
        }, {
            "6076578923ebd46e75f74344": salesman._id,     //Salesman id
            "607657951221966e7625ae4d": { "$lt": now_str } //Less than current date (No. 1)
        }
        ]
    });//Statistics of sales performance of the current month
    total_amount = total_amount[0]["sum"];//Total sales performance
    await $model.log("total:" + total_amount);//Print total performance
    for (let i = 0; i < commission_list.length; i++) {
        if (total_amount >= commission_list[i]["6075506114b72e6e88545005"] && total_amount < commission_list[i]["60755074cbbb586e7aa6b5ec"]) {//Find the corresponding Commission interval, which is greater than or equal to the left interval and less than the right interval
            let rate = commission_list[i]["60755081e609b06e81432714"];//Proportion corresponding to royalty class
            for (let j = 0; j < achievement_list.length; j++) {
                await $plugin.data.updateData("607656e51221966e7625ae4c", achievement_list[j]._id, {
                    "6076575823ebd46e75f74342": new Number(rate),
                    "6076576723ebd46e75f74343": achievement_list[j]["6076573e23ebd46e75f74341"] * new Number(rate) * 0.01 //Performance commission = performance amount * Commission proportion * 0.01
                });//The performance commission is calculated circularly
            }
        }
    }
 
}

 

4. Create a new "automatic commission calculation function" to obtain all operators, and call the "single operator commission calculation" function in step 3 with programming code to calculate all performance commissions of all operators in a circular manner.

 

The "programming" type is used to cycle the calculation of salesman's Commission, and the effect is as follows:

 

 

code:

async function runProcess($model = model, $plugin = plugin, $params) {
    let list = $params.list;//Get all salesmen
    for (let i = 0; i < list.length; i++) {
        await $plugin.program.exec("6076d0f41221966e7625b4ae", {//Call calculation Commission function id
            "6077eb7263070c5016868568": { //For the called function, select the step id of the operator step
                "_id": list[i]._id
            }
        });
    }
}

The id of the called function can be obtained in its function canvas

 

Get the id of the step of selecting business representative

 

 

5. Create scheduled tasks and set the functions to be executed and the execution time

So far, the implementation steps of the function are all completed.

 

Function effect: