Implement Xero export for reimbursements

This commit is contained in:
Yingtong Li 2020-02-12 01:03:02 +11:00
parent a170d1ea89
commit e5bbcaabbb
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 59 additions and 2 deletions

View File

@ -24,8 +24,9 @@
<h1>Claims processing</h1> <h1>Claims processing</h1>
<form class="ui form" method="POST"> <form class="ui form" method="POST">
<button class="ui small primary labeled icon button" type="submit" name="action" value="Export"><i class="download icon"></i>Export selected claims to ABA</button> <button class="ui small primary labeled icon button" type="submit" name="action" value="Export"><i class="download icon"></i>Export selected to ABA</button>
<button class="ui small basic primary labeled icon button" type="submit" name="action" value="Pay"><i class="check icon"></i>Mark selected claims as paid</button> <button class="ui small basic primary labeled icon button" type="submit" name="action" value="ExportXero"><i class="download icon"></i>Export selected for Xero</button>
<button class="ui small basic primary labeled icon button" type="submit" name="action" value="Pay"><i class="check icon"></i>Mark selected as paid</button>
<table class="ui celled table"> <table class="ui celled table">
<thead> <thead>

View File

@ -29,6 +29,7 @@ from django.views import generic
from . import aba from . import aba
from . import models from . import models
from . import xero
from ssmain.email import Emailer from ssmain.email import Emailer
import functools import functools
@ -600,6 +601,20 @@ def claim_processing(request):
response['Content-Disposition'] = 'attachment; filename="claims.aba"' response['Content-Disposition'] = 'attachment; filename="claims.aba"'
return response return response
if request.POST['action'] == 'ExportXero':
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)
for claim in claims:
xero.write_claim(csv_writer, claim)
response = HttpResponse(csv_file.getvalue(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="claims.csv"'
return response
if request.POST['action'] == 'Pay': if request.POST['action'] == 'Pay':
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)]

41
sstreasury/xero.py Normal file
View File

@ -0,0 +1,41 @@
# Society Self-Service
# Copyright © 2018–2020 Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from django.utils import timezone
import csv
def new_writer(f):
writer = csv.DictWriter(f, ['*ContactName', 'EmailAddress', 'POAddressLine1', 'POAddressLine2', 'POAddressLine3', 'POAddressLine4', 'POCity', 'PORegion', 'POPostalCode', 'POCountry', '*InvoiceNumber', '*InvoiceDate', '*DueDate', 'InventoryItemCode', 'Description', '*Quantity', '*UnitAmount', '*AccountCode', '*TaxType', 'TrackingName1', 'TrackingOption1', 'TrackingName2', 'TrackingOption2', 'Currency'])
writer.writeheader()
return writer
def write_row(writer, d):
writer.writerow(d)
def write_claim(writer, claim):
for item in claim.items:
write_row(writer, {
'*ContactName': claim.payee_name,
'*InvoiceNumber': 'RE-{}'.format(claim.id),
'*InvoiceDate': timezone.now().strftime('%d/%m/%Y'),
'*DueDate': timezone.now().strftime('%d/%m/%Y'),
'Description': item['Description'],
'*Quantity': str(item['Units']),
'*UnitAmount': str(item['Unit price']),
'*AccountCode': '850', # Suspense
'*TaxType': 'GST Free Expenses' if item['GST-free'] else 'GST on Expenses',
})