org.openbravo.kikobar.banqup/extract_invoice.py

84 lines
3.1 KiB
Python
Raw Normal View History

2023-06-23 11:55:52 +00:00
import requests
from requests_oauthlib import OAuth2Session
2023-06-23 11:55:52 +00:00
import json
import sys
import webbrowser
2023-06-23 11:55:52 +00:00
from config import *
from cachehandler import CacheHandler
from authhandler import AuthHandler
from api import OpenbravoToBanqupAPI
2023-06-24 16:23:57 +00:00
from datetime import timedelta, date
2023-06-23 11:55:52 +00:00
def extract_invoice(document):
url = ob_api_url+"Invoice?_where=documentNo='"+document+"'&_noActiveFilter=false"
payload = {}
headers = {
'Authorization': 'Basic '+userpass_b64
}
response = requests.request("GET", url, headers=headers, data=payload) #extracts invoice header
#print(response.text)
2023-06-23 11:55:52 +00:00
invoice = json.loads(response.text)
#print (invoice['response']['data'][0]['businessPartner'])
2023-06-24 16:23:57 +00:00
lastCalculatedOnDate = invoice['response']['data'][0]['lastCalculatedOnDate']
daysTillDue = invoice['response']['data'][0]['daysTillDue']
dueDate = date(int(lastCalculatedOnDate[0:4]), int(lastCalculatedOnDate[5:7]), int(lastCalculatedOnDate[8:10])) + timedelta(days=daysTillDue)
2023-06-23 11:55:52 +00:00
api = OpenbravoToBanqupAPI(bq_client_id,bq_client_secret)
authUrl = api.authHandler.getAuthURL(bq_redirect_uri)
webbrowser.open(authUrl)
response = input('Paste response: ')
token = api.authHandler.retrieveToken(response, redirectUri=bq_redirect_uri)
#print(token)
businessPartner=invoice['response']['data'][0]['businessPartner']
debtor_list = api.get('debtors?client_id='+banqup_client_id+'&client_debtor_number='+businessPartner,None,None)
#print(debtor_list)
debtor_id = debtor_list[2]['results'][0]['id']
preferred_channel = debtor_list[2]['results'][0]['preferred_channel']
2023-06-23 11:55:52 +00:00
url = ob_api_url+"InvoiceLine?_where=invoice='"+invoice['response']['data'][0]['id']+"'&_noActiveFilter=false"
response = requests.request("GET", url, headers=headers, data=payload) #extracts invoice lines
2023-06-23 14:02:27 +00:00
#print(response.text)
lines = json.loads(response.text)['response']['data']
lines_json = json.dumps(lines)
#print(lines_json)
lines = json.loads(lines_json)
lines_output = '['
first_line = True
for key in lines:
#print (key)
linetemp_json = json.dumps(key)
linetemp = json.loads(linetemp_json)
if linetemp['product'] != None:
if not first_line:
lines_output = lines_output+','
first_line = False
lines_output = lines_output + '{"service_name": "","service_description": "'+linetemp['product$_identifier']+'","service_quantity": '+str(linetemp['invoicedQuantity'])+',"service_price": '+str(linetemp['unitPrice'])+',"service_vat": '+'8'+'}'
2023-06-23 14:02:27 +00:00
lines_output = lines_output+']'
2023-06-23 14:13:55 +00:00
#print(lines_output)
2023-06-23 11:55:52 +00:00
payload = json.dumps({
"sales_invoice_number": invoice['response']['data'][0]['documentNo'],
"sales_invoice_date": invoice['response']['data'][0]['invoiceDate']+"T00:00:00Z",
2023-06-24 16:23:57 +00:00
"sales_invoice_due_date": str(dueDate)+"T00:00:00Z",
2023-06-23 11:55:52 +00:00
"platform_id": banqup_platform_id,
"debtor_id": debtor_id,
2023-06-23 11:55:52 +00:00
"currency_code": invoice['response']['data'][0]['currency$_identifier'],
"client_id": banqup_client_id,
"delivery_channel": preferred_channel,
2023-06-23 14:13:55 +00:00
"invoice_lines": json.loads(lines_output)
2023-06-23 11:55:52 +00:00
})
#print(payload)
2023-06-23 11:55:52 +00:00
new_invoice = api.post('sales-invoices',json.loads(payload),None,None)
print(new_invoice)
2023-06-23 11:55:52 +00:00
if __name__ == '__main__':
extract_invoice(str(sys.argv[1]))