From 02122df01609989b83cf1b28d880dbc9674fa100 Mon Sep 17 00:00:00 2001 From: Enrique Barcelli Date: Sun, 23 Jul 2023 21:25:20 +0800 Subject: [PATCH] Add funtionality to SEND invoice to customer --- README.md | 24 +++++++++++++++--------- send_invoice.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 send_invoice.py diff --git a/README.md b/README.md index 4d6a2a5..fdafa5e 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,21 @@ to the Banqup API of Unifiedpost. * Copy the file `config-sample.py` to `config.py`. * Edit `config.py` with your credentials for Openbravo and Banqup. -* Run `python3 extract_invoice.py `, where `` is the human readable invoice number in Openbravo. -* The application will do the following: - * Fetch the invoice `` from Openbravo. - * Authenticate with Banqup using OAuth2, for which it will launch a webbrowser to complete the authentication. The browser will receive the response from the Banqup server including the authentication token. The user will need to 'Copy' and 'Paste' this response at the corresponding prompt in the terminal. - * Fetch the corresponding `businessPartner` from Banqup to verify its existence and correct setup. - * Build a JSON record for the requested invoice ``, which is compatible with the Banqup API format. - * Post the invoice to the Banqup API for the creation of the corresponding invoice in Banqup. - * Display the response from the Banqup API, confirming the success or failure of the request. - * If the requests is successful, a new invoice will be created with status 'Draft' in the Banqup portal, so that it could be further processed on that system. +* Run `python3 extract_invoice.py `, where `` is the human readable invoice number in Openbravo, to create an invoice in the Banqup portal via the API. + * The application will do the following: + * Fetch the invoice `` from Openbravo. + * Authenticate with Banqup using OAuth2, for which it will launch a webbrowser to complete the authentication. The browser will receive the response from the Banqup server including the authentication token. The user will need to 'Copy' and 'Paste' this response at the corresponding prompt in the terminal. + * Fetch the corresponding `businessPartner` from Banqup to verify its existence and correct setup. + * Build a JSON record for the requested invoice ``, which is compatible with the Banqup API format. + * Post the invoice to the Banqup API for the creation of the corresponding invoice in Banqup. + * Display the response from the Banqup API, confirming the success or failure of the request. + * If the requests is successful, a new invoice will be created with status 'Draft' in the Banqup portal, so that it could be further processed on that system. +* Run `python3 send_invoice.py `, where `` is the human readable invoice number in Openbravo, to confirm an invoice in Banqup portal and send it to the customer via the API. + * The application will do the following: + * Authenticate with Banqup using OAuth2, for which it will launch a webbrowser to complete the authentication. The browser will receive the response from the Banqup server including the authentication token. The user will need to 'Copy' and 'Paste' this response at the corresponding prompt in the terminal. + * Fetch the invoice in Banqup to verify its existence in the portal and retrieve its id in Banqup. + * Post a request to the Banqup API to confirm the invoice and send it to the customer. + * Display the response from the Banqup API, confirming the success or failure of the request. **Credits** diff --git a/send_invoice.py b/send_invoice.py new file mode 100644 index 0000000..a4c6982 --- /dev/null +++ b/send_invoice.py @@ -0,0 +1,37 @@ +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 send_invoice(document): + + 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) + invoice_list = api.get('sales-invoices?client_id='+banqup_client_id+'&sales_invoice_number='+document,None,None) + if not invoice_list[2]['results']: + print('***********************') + print('The invoice number provided does not exist in the Banqup portal.') + print('Please verify and try again.') + print('***********************') + return + invoice_id = str(invoice_list[2]['results'][0]['id']) + #print(invoice_id) + payload = json.dumps({ + "type": "SEND" + }) + invoice_action = api.post('sales-invoices/'+invoice_id+'/action',json.loads(payload),None,None) + print(invoice_action) + + +if __name__ == '__main__': + send_invoice(str(sys.argv[1]))