Tip #5 Applying preset discounts to sales orders items based on total quantity sold per item group

Tip #5 Applying preset discounts to sales orders items based on total quantity sold per item group

Greetings everyone!


Hope you all are having a good week so far.
Last time, we talked about automating package and shipment creation for your sales orders. Today, we will discuss how we can automate the process of applying discounts to your sales orders based on the total number of items from an item group sold. 

How does this work? 

E.g.: Let's suppose your condition is something like this: 

If total qty of items from a group is (>/= 100 but < 200), then discount given = 5%,
and, if total qty of items from a group is (>/= 200), then discount given = 10%, 
else, discount given = 0%. 

Now, one of your sales order has 4 items - items a1 (125pcs), a2 (75pcs), b (112pcs) and c (40pcs), where a1 and a2 are from the same item group and both b and c are from different item groups. 

For such a case, then discounts applied will be as follows:
  • a1 price = 10% discount.

  • a2 price = 10% discount.

  • b price = 5% discount.

  • c price = 0% discount.


Custom function set up:

Log in to your Zoho account.
  • Open your Zoho Inventory organization.

  • Navigate to Settings using the gear icon from the top-right corner.

  • Select Automation from the sidebar. Now click on the button adjacent to +New Workflow Rule.

  • Out of the options from the drop-down, select Custom Functions.

  • Now let's add a name for this workflow rule and make sure that there are no spaces between words.

  • Select Sales Orders against module.

  • Now, the workflow type is going to be Event Based.

  • The condition to set shall be, "when a Sales Order is created".

  • Now copy and paste this code snippet inside the deluge pane:

salesorderID = salesorder.get("salesorder_id");

salesorderdate = salesorder.get("date").toDate();
organizationID = organization.get("organization_id");
lineitems = salesorder.get("line_items").toList();
idlist = List();
newlist = List();
for each line in lineitems
{
if(line.get("item_id") != line.get("product_id"))
{
gmap = Map();
gmap.put("itemid",line.get("item_id"));
gmap.put("id",line.get("product_id"));
gmap.put("quantity",line.get("quantity"));
if(!idlist.contains(line.get("product_id")))
{
idlist.add(line.get("product_id"));
}
newlist.add(gmap);
}
}
info newlist;
info idlist;
i = 0;
quanlist = List();
for each index i in idlist
{
val = 0;
nmap = Map();
for each it in newlist
{
if(it.get("id") == idlist.get(i))
{
val = val + it.get("quantity");
}
}
nmap.put("added_id",idlist.get(i));
nmap.put("tot",val);
quanlist.add(nmap);
}
info quanlist;
// applying discount with line items
json = Map();
newLineItems = List();
for each rec in lineitems
{
bson = Map();
id = rec.get("item_id");
bson.put("item_id",id);
quantity = rec.get("quantity");
bson.put("quantity",quantity);
init_rate = rec.get("rate").toDecimal();
bson.put("rate",init_rate);
if(rec.get("item_id") != rec.get("product_id"))
{
for each quant in quanlist
{
if(quant.get("added_id") == rec.get("product_id"))
{
if(quant.get("tot").toDecimal() >= 100 && quant.get("tot").toDecimal() < 199)
{
bson.put("discount","2%");
}
else if(quant.get("tot").toDecimal() >= 200 && quant.get("tot").toDecimal() < 500)
{
bson.put("discount","5%");
}
else if(quant.get("tot").toDecimal() > 500 )
{
bson.put("discount","7%");
}
}
}
}
else
{
bson.put("discount","0%");
}
cfs = rec.get("item_custom_fields").toList();
bson.put("item_custom_fields",cfs);
newLineItems.add(bson);
}
info newLineItems;
json.put("line_items",newLineItems);
json.put("salesorder_number",salesorder.get("salesorder_number"));
json.put("customer_id",salesorder.get("customer_id"));
res = zoho.inventory.updateRecord("SalesOrders",organizationID,salesorderID,json);
info res.toMap().get("message");
  • Now within the code, you can configure your own discount conditions by editing the quantities and discount percentage within these lines:

    • if(quant.get("tot").toDecimal() >= 100 && quant.get("tot").toDecimal() < 199)

    • {

    • bson.put("discount","2%");

    • }

    • else if(quant.get("tot").toDecimal() >= 200 && quant.get("tot").toDecimal() < 500)

    • {

    • bson.put("discount","5%");

    • }

    • else if(quant.get("tot").toDecimal() > 500 )

    • {

    • bson.put("discount","7%");

    • ​}

  • You can also add more cases by adding more 'else if' conditions to the code.

  • Once everything is ready, hit Save.

Henceforth, discounts will be applied to the items on your sales orders at item level based on the total quantity of items in the order from every item group. 


Hope this helps with your sales endeavours. 

Until next time. 

Your everyday end user,
Ajay Aadhithya Chandrasekaran
Zoho Inventory