From 615a3fc689b6fde8a93475494b7d4384974a3d0f Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 18 Nov 2024 22:28:55 +0100 Subject: [PATCH] invoice created, email sending fails --- rowers/idoklad.py | 89 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/rowers/idoklad.py b/rowers/idoklad.py index d2199c06..1da50022 100644 --- a/rowers/idoklad.py +++ b/rowers/idoklad.py @@ -11,6 +11,8 @@ from rowsandall_app.settings import ( ) contacts_url = 'https://api.idoklad.cz/v3/Contacts' +invoice_url = 'https://api.idoklad.cz/v3/IssuedInvoices' +email_url = 'https://api.idoklad.cz/v3/Mails/IssuedInvoice/Send' from rowers.models import iDokladToken @@ -51,21 +53,12 @@ def idoklad_token(): token = response.json() token['updated_at'] = timezone.now() token = iDokladToken.objects.filter(id=1).update(**token) - return token + return iDokladToken.objects.get(id=1) else: return None return token -def prepare_invoice(): - token = idoklad_token() - if token is None: - return None - - data = { - 'templateId': 1, - } - def get_contacts(rower): token = idoklad_token() if token is None: @@ -85,8 +78,6 @@ def get_contacts(rower): return None - dologging('idoklad.log','Status Code '+json.dumps(res.json())+'\n') - data = res.json()['Data']['Items'] if len(data) >= 1: @@ -134,4 +125,78 @@ def create_contact(rower): return id +def create_invoice(rower, amount, braintreeid, dosend=True, contact_id=None, name=None): + t = idoklad_token() + if t is None: + return None + + if not contact_id: + contact_id = get_contacts(rower) + if not name: + name = 'Rowsandall Subscription '+str(braintreeid) + + if not contact_id: + return 0 + + token = idoklad_token() + if token is None: + return None + + dologging('idoklad.log','Creating idoklad invoice for '+str(rower.user.email)+'\n') + + headers = { + 'Authorization': 'Bearer {t}'.format(t=token.access_token), + } + + res = requests.get(invoice_url+'/Default', headers=headers) + + post_data = res.json()['Data'] + post_data['DateOfPayment'] = timezone.now().strftime('%Y-%m-%d') + post_data['Description'] = name + post_data['Items'][0]['Name'] = name + post_data['Items'][0]['UnitPrice'] = amount + post_data['PartnerId'] = contact_id + post_data['ItemsTextPrefix'] = 'We invoice you for '+str(name)+' in the amount of '+str(amount)+' EUR.' + post_data['ItemsTextSuffix'] = 'This invoice was already paid. Please do not pay it again.' + post_data['Items'][0]['VatRate'] = 0.0 + post_data['Items'][0]['VatRateType'] = 2 + post_data['Items'][0]['VatCodeId'] = 3 + post_data['CurrencyId'] = 2 + post_data['ReportLanguage'] = 3 + post_data.pop('ExchangeRate', None) + + + res = requests.post(invoice_url, json=post_data, headers=headers) + dologging('idoklad.log','Invoice Created - status code '+str(res.status_code)+'\n') + + if res.status_code not in [200, 201]: + dologging('idoklad.log','Invoice Created - reason '+str(res.reason)+'\n') + return 0 + + id = res.json()['Data']['Id'] + + if dosend: + url = invoice_url+'/'+str(id)+'/Send' + print(url) + data = { + 'AttachmentIds': [id], + 'DocumentId': braintreeid, + 'EmailBody': 'Dear customer, we are sending you the invoice for your subscription. Please do not hesitate to contact us if you have any questions. Best regards, Rowsandall Team', + 'EmailSubject': 'Rowsandall Subscription Invoice', + 'Method': 1, + 'ReportLanguage': 3, + 'SendToSelf': True, + } + + print(data) + print(email_url) + + res = requests.post(email_url, json=data, headers=headers) + + dologging('idoklad.log','Invoice Sent - status code '+str(res.status_code)+'\n') + if res.status_code not in [200, 201]: + dologging('idoklad.log','Invoice Sent - reason '+str(res.reason)+'\n') + + return id +