Export reimbursement claim receipts

This commit is contained in:
Yingtong Li 2020-02-14 15:14:51 +11:00
parent dbe1eb988c
commit ecfa4bdc2b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 21 additions and 9 deletions

View File

@ -37,6 +37,7 @@ import functools
import io import io
import itertools import itertools
import json import json
import zipfile
# HELPER DECORATORS # HELPER DECORATORS
@ -663,14 +664,24 @@ def claim_processing(request):
claims = models.ReimbursementClaim.objects.filter(state=models.ClaimState.APPROVED.value).all() claims = models.ReimbursementClaim.objects.filter(state=models.ClaimState.APPROVED.value).all()
claims = [c for c in claims if request.POST.get('claim_{}'.format(c.id), False)] claims = [c for c in claims if request.POST.get('claim_{}'.format(c.id), False)]
csv_file = io.StringIO() # Export CSV
csv_writer = xero.new_writer(csv_file) with io.StringIO() as csv_file:
csv_writer = xero.new_writer(csv_file)
for claim in claims: for claim in claims:
xero.write_claim(csv_writer, claim) xero.write_claim(csv_writer, claim)
response = HttpResponse(csv_file.getvalue(), content_type='text/csv') # Export resources to ZIP
response['Content-Disposition'] = 'attachment; filename="claims.csv"' with io.BytesIO() as zip_file_bytes:
with zipfile.ZipFile(zip_file_bytes, 'w') as zip_file:
zip_file.writestr('claims.csv', csv_file.getvalue())
for claim in claims:
for claim_receipt in claim.claimreceipt_set.all():
with claim_receipt.uploaded_file.open() as f:
zip_file.writestr('RE-{}/{}'.format(claim.id, claim_receipt.uploaded_file.name.split('/')[-1]), f.read())
response = HttpResponse(zip_file_bytes.getvalue(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="claims.zip"'
return response return response
if request.POST['action'] == 'Pay': if request.POST['action'] == 'Pay':

View File

@ -36,6 +36,7 @@ def write_claim(writer, claim):
'Description': item['Description'], 'Description': item['Description'],
'*Quantity': str(item['Units']), '*Quantity': str(item['Units']),
'*UnitAmount': str(item['Unit price']), '*UnitAmount': str(item['Unit price']),
'*AccountCode': '850', # Suspense #'*AccountCode': '850', # Suspense
'*AccountCode': 'EVT-E',
'*TaxType': 'GST Free Expenses' if item['GST-free'] else 'GST on Expenses', '*TaxType': 'GST Free Expenses' if item['GST-free'] else 'GST on Expenses',
}) })