Late fee custom function

Late fee custom function

I am currently using the following custom function to execute a 1.5% late fee on invoices that are past due.

  1. //create a variable to get invoice total and add a 1.5% fee
  2. latefee = invoice.get("total").toDecimal() * 0.015;
  3. orgID = organization.get("organization_id").toString();
  4. invoiceID = invoice.get("invoice_id").toLong();
  5. //display the value stored in the latefee variable
  6. info latefee;
  7. //add late fee and adjustment and store the total amount in the variable 'adjustment'  
  8. adjustment = invoice.get("adjustment").toDecimal() + latefee;
  9. jsonMap = Map();
  10. jsonMap.put("adjustment",adjustment);
  11. jsonMap.put("reason","Late fee");
  12. // To update the current record
  13. result = zoho.invoice.update("invoices",orgID,invoiceID,jsonMap);
  14. info result.toMap().get("message");


Currently it is taking the total invoice amount and adding 1.5% of that total. Is there a way I can have it calculate 1.5% of the unpaid balance?

For example: If an invoice is $10,000 but the customer makes a payment of $2,000, the 1.5% should only be calculated on $8,000.