Creating Offsets in Abila MIP via API

As we created invoices via API in Abila MIP, we wanted to create an offset as well. This kind of functionality is very clear when using the MIP UI;  it is just a button that you hit to create offsets for the current session and document. However, we encountered initial confusion when trying to create the offsets via the API. This post documents how to go about creating the offsets for an existing invoice. (Note we will use the terms invoice and document interchangeably below).

It turns out the process to create offsets for an existing invoices in a session consists of 3 steps, the details of which will follow.

1.  GET the invoice

This first step is mainly to get the existing document via the API

2. POST the offset to calculate

Now we have an invoice’s data in hand; how should this invoice be offset ? The API will be happy to calculate the offsets for you !

3.  PUT the invoice with calculated offset data to insert

This final step will update the existing document with the calculated offset information.

A SIMPLIFIED PYTHON FUNCTION TO CALCULATE OFFSETS VIA API

Consider the following function.  For simplicity sake , it is stripped of any sort of error checking, but the guts show how the 3 step process would work.  We pass a URL which if you are running locally can just be localhost on port 9001 (https://localhost:9001). The second parameter is the access token for the MIP API, which we will assume you have in hand. Then the next two parameter are the session number and document number of the invoice we want to offset.

def offset_the_doc(p_url,p_token,p_sess_id,p_docnum):

   # Get current data ; build the UR with the session number and document number

   mip_inv_url = p_url + '/api/te/APInvoices/sessions/' + p_sess_id + '/documents/' + p_docnum

   # This first step grabs the existing document via API; the payload of this will be passed to the next step

   try:
      mip_inv_req = requests.get(mip_inv_url, headers={'Authorization-Token': p_token})
   except:
        DO SOMETHING IN CASE OF ERROR

   # Calculate and return offset - We build the offset URL and pass the payload from the previous step above

   mip_offset_pay = json.loads(mip_inv_req.text)
   mip_offset_url = p_url + '/api/te/APInvoices/sessions/' + p_sess_id + '/documents/offsets'

   try:
      mip_offset_req = requests.post(mip_offset_url, headers={'Authorization-Token': p_token}, 
         json=mip_offset_pay)
   except:
        DO SOMETHING IN CASE OF ERROR

   # The previous step returned a payload which has the original invoice data and the newly calculated offset data; 
   # update the existing document with this payload

   mip_upd_json = json.loads(mip_offset_req.text)
   mip_upd_url = p_url + '/api/te/APInvoices/sessions/' + p_sess_id + '/documents/' + p_docnum

   try:
      mip_upd_req = requests.put(mip_upd_url, headers={'Authorization-Token': p_token},json=mip_upd_json)
   except:
        DO SOMETHING IN CASE OF ERROR