Cosmetics, Pallets Discount in a Sales order.

Jira Code: MT-38

Whenever a sales order is created, discounts are added to the sales order. Different discount amounts are applied to cosmetics and pallets based on the total amount of cosmetics and the total number of pallets.

A workflow triggers on save of a sales order and then the following workflow action script starts to execute.

MT-38 Partner Discount

/**
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 */
define(['N/record', 'N/runtime', 'N/search'],

    function(record, runtime, search) {

        /**
         * Definition of the Suitelet script trigger point.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @Since 2016.1
         */
        function onAction(scriptContext) {
            try {
                var total_cosmetics = 0; //total amount of cosmetics items
                var total_pallets = 0; //total amount of pallet items
                var item_number = 0; //number of items in pallet category
                var objrecord = scriptContext.newRecord;
                log.debug("objrecord", objrecord);

                var numLines = objrecord.getLineCount({
                    sublistId: 'item'
                });
                log.debug("Number of Line items", numLines);
                log.debug('Line Index', numLines);

                // objrecord.setFieldValue({
                //         fieldId: 'location',
                //         value: 5
                //     });

                /*record.submitFields({
                    type: 'salesorder',
                    id: objrecord.id,
                    values: {
                        'location': 5,
                        'custbody_salesstatus': 1
                    }
                });*/


                for (i = 0; i < numLines; i++) {

                    var sublistFieldValue = objrecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        line: i
                    });
                    var itemtype = objrecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'itemtype',
                        line: i
                    });
                    if (itemtype == 'InvtPart') { //inventory items considered
                        var discount_category = search.lookupFields({
                            type: search.Type.INVENTORY_ITEM,
                            id: sublistFieldValue,
                            columns: ['custitem_mt_35_discount_rule']
                        });
                        log.debug('discount_category', discount_category);

                        if (discount_category.custitem_mt_35_discount_rule ==
                            '' || discount_category.custitem_mt_35_discount_rule ==
                            null || discount_category.custitem_mt_35_discount_rule == undefined) {
                            continue;
                        }
                        discount_category_value = discount_category.custitem_mt_35_discount_rule[0].value;
                        log.debug('discount_category_value', discount_category_value);



                        var item_amount = objrecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'amount',
                            line: i
                        });
                        log.debug('item_amount', item_amount);
                        var item_quantity = objrecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i
                        });
                        log.debug('item_quantity', item_quantity);

                        if (discount_category_value == '1') {
                            total_cosmetics = total_cosmetics + item_amount;
                            log.debug('total_cosmetics', total_cosmetics);
                        } else if (discount_category_value == '2') {
                            total_pallets = total_pallets + item_amount;
                            item_number = item_number + item_quantity;
                            log.debug('total_pallets', total_pallets);
                            log.debug('item_number', item_number);
                        } else {
                            log.debug('Sorry');;
                        }
                    }

                }
                var total_cosmetics_discount = 0;

                if ((total_cosmetics >= 10000) && (total_cosmetics <= 24999)) {
                    // 5 percent discount
                    total_cosmetics_discount = total_cosmetics * (1 / 20);
                    log.debug('total_cosmetics_discount', total_cosmetics_discount);
                } else if (total_cosmetics >= 25000) {
                    // 10 percent discount
                    total_cosmetics_discount = total_cosmetics * (1 / 10);
                    log.debug('total_cosmetics_discount', total_cosmetics_discount);
                } else {
                    log.debug('No Discount for Cosemtics');
                    log.debug('total_cosmetics_discount', total_cosmetics_discount);
                }
                if (total_cosmetics_discount != 0) {
                    /*objrecord.insertLine({                                  //Partner discount for Cosmetics
                        sublistId: 'item',
                        line: numLines
                    });*/
                    var lineNum = objrecord.selectNewLine({
                        sublistId: 'item'
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        value: -6
                    });
                    total_cosmetics_discount_negative = total_cosmetics_discount * -1;
                    log.debug('total_cosmetics_discount_negative', total_cosmetics_discount_negative);
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'rate',
                        value: total_cosmetics_discount_negative
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'description',
                        value: "Discount via webstore"
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'istaxable',
                        value: true
                    });
                    log.debug('success 1');

                    objrecord.commitLine({
                        sublistId: 'item'
                    });
                }


                var total_pallets_discount = 0;
                if ((item_number >= 4) && (item_number <= 9)) {
                    total_pallets_discount = total_pallets * (1 / 20);
                } else if ((item_number >= 10) && (item_number <= 19)) {
                    total_pallets_discount = total_pallets * (1 / 10);
                } else if ((item_number >= 20) && (item_number <= 39)) {
                    total_pallets_discount = total_pallets * (3 / 20);
                } else if (item_number >= 40) {
                    total_pallets_discount = total_pallets * (1 / 5);
                } else {
                    log.debug('No Discount for Pallets');
                    log.debug('total_pallets_discount', total_pallets_discount);
                }
                if (total_pallets_discount != 0) {
                    /*objrecord.insertLine({ //Partner discount for Pallets
                        sublistId: 'item',
                        line: numLines
                    });*/
                    var lineNum = objrecord.selectNewLine({
                        sublistId: 'item'
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        value: -6
                    });
                    total_pallets_discount = total_pallets_discount * -1;
                    log.debug('total_pallets_discount', total_pallets_discount);
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'rate',
                        value: total_pallets_discount
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'description',
                        value: "Discount via webstore"
                    });
                    objrecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'istaxable',
                        value: true
                    });
                    log.debug('success 2');
                    objrecord.commitLine({
                        sublistId: 'item'
                    });
                }

            } catch (er) {
                log.debug('er', er);
            }
        }
        return {
            onAction: onAction
        };
    });

Leave a comment

Your email address will not be published. Required fields are marked *