Settlement discounts

Settlement discounts

Hey Zoho guys,

A lot of your users, I'm pretty sure, get settlement discounts for early payment or whatever reason and there is no feature to automate this!
It would be nice if you kind people at Zoho could provide us with a "custom button" to accomplish this using deluge ?

I have done it for myself using JavaScript but it would be really great to be able to do this within Zoho ;-)
I've set up a custom field in contacts to enter the item_id of the discount used....
Here's the code I wrote, which hopefully you would be good enough to convert to a deluge script.....

function getPayables(){
  var discounts = "";
  // if there was a filter for vendors with open balances - even better !!
  var response = UrlFetchApp.fetch(url);
  var data = JSON.parse(response.getContentText());
  var vendors = data.contacts;
  for (var i = 0; i <= vendors.length-1; i++)
    if (vendors[i].outstanding_payable_amount > 0) 
    {
      for (var j = 0; j <= vendors[i].custom_fields.length-1; j++)
      if (vendors[i].custom_fields[j].label == "Settlements") discounts = vendors[i].custom_fields[j].value;
      if ( discounts > 0)
      { var CN = {
        "id" : vendors[i].contact_id,
        "due" : vendors[i].outstanding_payable_amount,
        "item" : discounts}
        createCredits(CN);
      }
    }
return;
}

function createCredits(CN){
  var MyString=
      {
        "vendor_id": CN.id,
        "reference_number" : 'Settlement discounts',
        "line_items":
        [{
          "item_id": CN.item,
          "quantity": CN.due,
          "tax_id": "###################",    // maybe get this from the item used ?
        }
        ]}
  var options = {
    'method' : 'post',
    'contentType' : 'application/json',
    'muteHttpExceptions' : true
  }; 
  var myStuff = encodeURIComponent(JSON.stringify(MyString));
  try{
  var response = UrlFetchApp.fetch(url,options);
  } catch(f) {log_('Credit creation response = ' + f)};
  return;
}