Draft bills

Draft bills

When we get billed for a PO that is not yet received they are saved as "Draft".
I'd like to be notified when a billed PO is received....
Easy enough to code externally but could one of you Deluge experts kindly convert the code below....

function draftBills()
{
  var POr = UrlFetchApp.fetch("https://books.zoho.com/api/v3/purchaseorders?organization_id=" + orgID + "&authtoken=" + token + "&status=issued");
  var POd = JSON.parse(POr.getContentText());
  var POs = POd.purchaseorders;
  for (var i = 0; i <= POs.length-1; i++)
  {
    if (POs[i].billed_status == "billed" && POs[i].received_status == "received")

Ooopsy - What a silly mistake LOL ;-)

Updated .....

function draftBills()
{
  try
  {
    var result = UrlFetchApp.fetch("https://books.zoho.com/api/v3/bills?authtoken=" + token + "&status=draft");
    var data = JSON.parse(result.getContentText());
    var bills = data.bills;
    var body = "", count = 0, words = "", bill = [], poNums = [];
    for (var i = 0; i <= bills.length-1; i++)
    {
      result = UrlFetchApp.fetch("https://books.zoho.com/api/v3/bills/" + bills[i].bill_id + "?authtoken=" + token);
      data = JSON.parse(result.getContentText());
      bill = data.bill;
      poNums = bill.purchaseorder_ids;
      if (poNums.length < 1) continue;
      for (var p = 0; p <= poNums.length-1; p++)
      {
        result = UrlFetchApp.fetch('https://books.zoho.com/api/v3/purchaseorders/' + poNums[p] + '?authtoken=' + token);
        data = JSON.parse(result.getContentText());
        if (data.purchaseorder.received_status != "received") continue
        count++;
        result = openDraftBills(bill.bill_id);
        if (result === true) {words = ", <b>has been converted to open</b>"}
        else words = result;
        body = body + "<b>" + poNums[p] + "</b> is now received from " + bills[i].vendor_name + " - " + bills[i].bill_number + words + "<br>";  
      }
    }
    if (count > 0)
    {
      MailApp.sendEmail(
        {
          to: "delugeguys@zoho.com",
          subject: count + " PO receives for draft bills",
          htmlBody: body
        })
    }
  } catch (e) {log_("An error occured in draftBills: " + e.message + " : line number = " + e.lineNumber)};
  return;
}


function openDraftBills(iD)
{
  try
  {
    var result = UrlFetchApp.fetch("https://books.zoho.com/api/v3/bills/" + iD + "?authtoken=" + token);
    var data = JSON.parse(result.getContentText());
    var bill = data.bill;
    if (bill.status != "draft") return " - Was already open!";
    var vendor = bill.vendor_id;
    var billArray =
        {
          "vendor_id": vendor,
          "bill_number": iD,
          "status": "open"
        }    
    var newArray = encodeURIComponent(JSON.stringify(billArray));
    var options = {
      "method":"put",
      "contentType":"application/json"
      //'muteHttpExceptions' : true
    }
    var url = 'https://books.zoho.com/api/v3/bills/' + iD + '?authtoken=' + token + '&JSONString=' + newArray; 
    var response = UrlFetchApp.fetch(url,options);
    //var x = JSON.parse(response.getContentText());
    return true;
  } catch (e) {return " - Error converting bill to open: " + e.message + " : line number = " + e.lineNumber}
}

 
One day I'll get around to learning deluge script ;-)