- Extract invoice from Openbravo
- Add Peppol mandatory fields
This commit is contained in:
parent
e9f9c67fd7
commit
06ab27a128
|
|
@ -17,3 +17,15 @@ userpass_b64 = base64.b64encode((ob_user+':'+ob_pass).encode('ascii')).decode('a
|
||||||
# the line below to match the 'Search Key' of that specific product:
|
# the line below to match the 'Search Key' of that specific product:
|
||||||
# comment_product = 'search-key-of-your-comment-product'
|
# comment_product = 'search-key-of-your-comment-product'
|
||||||
comment_product = 'Comment/Note'
|
comment_product = 'Comment/Note'
|
||||||
|
gst_rate = "9"
|
||||||
|
|
||||||
|
indentation = " "
|
||||||
|
|
||||||
|
xml_header = '<?xml version="1.0" encoding="UTF-8"?>'
|
||||||
|
|
||||||
|
xmlns_header = 'xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
|
||||||
|
|
||||||
|
CustomizationID = '<cbc:CustomizationID>urn:peppol:pint:billing-1@sg-1</cbc:CustomizationID>'
|
||||||
|
ProfileID = '<cbc:ProfileID>urn:peppol:pint:billing-1@sg-1</cbc:ProfileID>'
|
||||||
|
InvoiceTypeCode = '<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>'
|
||||||
|
PeppolID = 'SGUEN200212345Z'
|
||||||
|
|
|
||||||
104
extract_invoice.py
Normal file
104
extract_invoice.py
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
import requests
|
||||||
|
from requests_oauthlib import OAuth2Session
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import webbrowser
|
||||||
|
from config import *
|
||||||
|
#from cachehandler import CacheHandler
|
||||||
|
#from authhandler import AuthHandler
|
||||||
|
#from api import OpenbravoToBanqupAPI
|
||||||
|
from datetime import timedelta, date
|
||||||
|
|
||||||
|
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)
|
||||||
|
invoice = json.loads(response.text)
|
||||||
|
#print (invoice['response']['data'][0]['businessPartner'])
|
||||||
|
uuid = invoice['response']['data'][0]['id']
|
||||||
|
uuid = uuid[:8]+'-'+uuid[8:12]+'-'+uuid[12:16]+'-'+uuid[16:20]+'-'+uuid[20:32]
|
||||||
|
print(uuid)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
businessPartner=invoice['response']['data'][0]['businessPartner']
|
||||||
|
|
||||||
|
|
||||||
|
url = ob_api_url+"InvoiceLine?_where=invoice='"+invoice['response']['data'][0]['id']+"'&_noActiveFilter=false&_sortBy=lineNo"
|
||||||
|
|
||||||
|
response = requests.request("GET", url, headers=headers, data=payload) #extracts invoice lines
|
||||||
|
#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
|
||||||
|
url = ob_api_url+"Product?_where=id='"+linetemp['product']+"'&_noActiveFilter=false"
|
||||||
|
response = requests.request("GET", url, headers=headers, data=payload) #extracts product from master
|
||||||
|
product = json.loads(response.text)['response']['data'][0]['searchKey']
|
||||||
|
if product != comment_product:
|
||||||
|
lines_output = lines_output + '{"service_name": "'+product+'","service_description": "'+linetemp['product$_identifier']+'","service_quantity": '+str(linetemp['invoicedQuantity'])+',"service_price": '+str(linetemp['unitPrice'])+',"service_vat": '+gst_rate+'}'
|
||||||
|
else:
|
||||||
|
lines_output = lines_output + '{"service_name": "'+product+'","service_description": "'+linetemp['description']+'","service_quantity": '+str(linetemp['invoicedQuantity'])+',"service_price": '+str(linetemp['unitPrice'])+',"service_vat": '+gst_rate+'}'
|
||||||
|
lines_output = lines_output+']'
|
||||||
|
print(lines_output)
|
||||||
|
|
||||||
|
ubloutput = xml_header+'\n'+'<Invoice '+xmlns_header+'>\n'
|
||||||
|
ubloutput = ubloutput+indentation+CustomizationID+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+ProfileID+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+InvoiceTypeCode+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+'<cbc:ID>'+invoice['response']['data'][0]['documentNo']+'</cbc:ID>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+'<cbc:IssueDate>'+invoice['response']['data'][0]['invoiceDate']+'</cbc:IssueDate>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+'<cbc:DocumentCurrencyCode>'+invoice['response']['data'][0]['currency$_identifier']+'</cbc:DocumentCurrencyCode>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+'<cac:AccountingSupplierParty>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+indentation+'<cac:Party>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+indentation+indentation+'<cbc:EndpointID schemeID="0195">'+PeppolID+'</cbc:EndpointID>'+'\n'
|
||||||
|
|
||||||
|
|
||||||
|
ubloutput = ubloutput+indentation+indentation+'</cac:Party>'+'\n'
|
||||||
|
ubloutput = ubloutput+indentation+'</cac:AccountingSupplierParty>'+'\n'
|
||||||
|
|
||||||
|
print(ubloutput)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
payload = json.dumps({
|
||||||
|
"sales_invoice_number": invoice['response']['data'][0]['documentNo'],
|
||||||
|
"sales_invoice_date": invoice['response']['data'][0]['invoiceDate']+"T00:00:00Z",
|
||||||
|
"sales_invoice_due_date": str(dueDate)+"T00:00:00Z",
|
||||||
|
"currency_code": invoice['response']['data'][0]['currency$_identifier'],
|
||||||
|
"invoice_lines": json.loads(lines_output, strict=False),
|
||||||
|
"po_number": "NA",
|
||||||
|
"buyer_reference": "NA",
|
||||||
|
"customer_reference": "NA",
|
||||||
|
"supplier_reference": "NA",
|
||||||
|
"contract_number": "NA"
|
||||||
|
})
|
||||||
|
print(payload)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
extract_invoice(str(sys.argv[1]))
|
||||||
Loading…
Reference in a new issue