2023-06-23 11:55:52 +00:00
import requests
2023-06-24 12:43:35 +00:00
from requests_oauthlib import OAuth2Session
2023-06-23 11:55:52 +00:00
import json
import sys
2023-06-24 12:43:35 +00:00
import webbrowser
2023-06-23 11:55:52 +00:00
from config import *
2023-06-24 12:43:35 +00:00
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
2023-06-24 16:43:35 +00:00
#print(response.text)
2023-06-23 11:55:52 +00:00
invoice = json . loads ( response . text )
2023-06-24 16:43:35 +00:00
#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
2023-06-24 12:43:35 +00:00
api = OpenbravoToBanqupAPI ( bq_client_id , bq_client_secret )
authUrl = api . authHandler . getAuthURL ( bq_redirect_uri )
webbrowser . open ( authUrl )
2023-06-24 16:43:35 +00:00
response = input ( ' Paste response: ' )
2023-06-24 12:43:35 +00:00
token = api . authHandler . retrieveToken ( response , redirectUri = bq_redirect_uri )
2023-06-24 16:43:35 +00:00
#print(token)
2023-06-24 12:43:35 +00:00
businessPartner = invoice [ ' response ' ] [ ' data ' ] [ 0 ] [ ' businessPartner ' ]
debtor_list = api . get ( ' debtors?client_id= ' + banqup_client_id + ' &client_debtor_number= ' + businessPartner , None , None )
2023-06-24 16:43:35 +00:00
#print(debtor_list)
2023-06-24 17:57:20 +00:00
if not debtor_list [ 2 ] [ ' results ' ] :
print ( ' *********************** ' )
print ( ' The business partner ' + businessPartner + ' is not registered in the Banqup portal for the customer ' + invoice [ ' response ' ] [ ' data ' ] [ 0 ] [ ' businessPartner$_identifier ' ] )
print ( ' Please complete the registration in Banqup and try again. ' )
print ( ' *********************** ' )
return
2023-06-24 12:43:35 +00:00
debtor_id = debtor_list [ 2 ] [ ' results ' ] [ 0 ] [ ' id ' ]
preferred_channel = debtor_list [ 2 ] [ ' results ' ] [ 0 ] [ ' preferred_channel ' ]
2023-07-28 18:37:39 +00:00
url = ob_api_url + " InvoiceLine?_where=invoice= ' " + invoice [ ' response ' ] [ ' data ' ] [ 0 ] [ ' id ' ] + " ' &_noActiveFilter=false&_sortBy=lineNo "
2023-06-23 11:55:52 +00:00
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
2023-06-29 07:59:16 +00:00
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 ' ]
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 + ' } '
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 ,
2023-06-24 12:43:35 +00:00
" 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 ,
2023-06-24 14:15:11 +00:00
" delivery_channel " : preferred_channel ,
2023-06-25 04:00:58 +00:00
" invoice_lines " : json . loads ( lines_output ) ,
" po_number " : " NA " ,
" buyer_reference " : " NA " ,
" customer_reference " : " NA " ,
" supplier_reference " : " NA " ,
" contract_number " : " NA "
2023-06-23 11:55:52 +00:00
} )
2023-06-24 16:43:35 +00:00
#print(payload)
2023-06-23 11:55:52 +00:00
2023-06-24 14:15:11 +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 ] ) )