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 itertools
import json
import zipfile
# HELPER DECORATORS
@ -663,14 +664,24 @@ def claim_processing(request):
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)]
csv_file = io.StringIO()
csv_writer = xero.new_writer(csv_file)
# Export CSV
with io.StringIO() as csv_file:
csv_writer = xero.new_writer(csv_file)
for claim in claims:
xero.write_claim(csv_writer, claim)
for claim in claims:
xero.write_claim(csv_writer, claim)
# Export resources to ZIP
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())
response = HttpResponse(csv_file.getvalue(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="claims.csv"'
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
if request.POST['action'] == 'Pay':

View File

@ -36,6 +36,7 @@ def write_claim(writer, claim):
'Description': item['Description'],
'*Quantity': str(item['Units']),
'*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',
})