|
2 | 2 | import logging
|
3 | 3 | import environ
|
4 | 4 | import datetime, calendar
|
5 |
| -from django.utils import timezone |
| 5 | +import holviapi |
6 | 6 | from django.core.mail import EmailMessage
|
7 | 7 | from members.handlers import BaseApplicationHandler, BaseMemberHandler
|
8 | 8 | from creditor.handlers import BaseTransactionHandler, BaseRecurringTransactionsHandler
|
9 |
| -from creditor.models import Transaction, TransactionTag |
| 9 | +from creditor.models import Transaction, TransactionTag, RecurringTransaction |
10 | 10 | from django.utils.translation import ugettext_lazy as _
|
11 |
| -import environ |
| 11 | +from holviapp.utils import api_configured, get_invoiceapi |
12 | 12 |
|
13 | 13 | logger = logging.getLogger('example.handlers')
|
14 |
| -env = environ.Env() |
15 | 14 |
|
16 | 15 |
|
17 | 16 | class ExampleBaseHandler(BaseMemberHandler):
|
@@ -160,16 +159,46 @@ def on_creating(self, rt, t, *args, **kwargs):
|
160 | 159 | msg = "on_creating called for %s (from %s)" % (t, rt)
|
161 | 160 | logger.info(msg)
|
162 | 161 | print(msg)
|
163 |
| - holvi_pool = env('HOLVI_POOL', default=None) |
164 |
| - holvi_key = env('HOLVI_APIKEY', default=None) |
165 |
| - |
166 |
| - # QnD reference generation example |
167 |
| - import time |
168 |
| - t.reference = holviapi.utils.int2iso_reference(int(time.time())) |
169 |
| - |
170 |
| - if not holvi_pool or not holvi_key: |
| 162 | + # Only care about negative amounts |
| 163 | + if t.amount >= 0.0: |
171 | 164 | return True
|
172 |
| - # TODO: Create invoice and set t.reference to the holvi reference |
| 165 | + # If holvi is configured, make invoice |
| 166 | + if api_configured(): |
| 167 | + return self.create_holvi_invoice(rt, t) |
| 168 | + # otherwise make reference number that matches the tmatch logic above |
| 169 | + t.reference = holviapi.utils.int2fin_reference(int("1%03d%s" % (rt.owner.member_id, rt.tag.tmatch))) |
| 170 | + return True |
| 171 | + |
| 172 | + def create_holvi_invoice(self, rt, t): |
| 173 | + if t.stamp: |
| 174 | + year = t.stamp.year |
| 175 | + month = t.stamp.month |
| 176 | + else: |
| 177 | + now = datetime.datetime.now() |
| 178 | + year = now.year |
| 179 | + month = now.month |
| 180 | + |
| 181 | + invoice = holviapi.Invoice(get_invoiceapi()) |
| 182 | + invoice.receiver = holviapi.InvoiceContact({ |
| 183 | + 'email': t.owner.email, |
| 184 | + 'name': t.owner.name, |
| 185 | + }) |
| 186 | + invoice.items.append(holviapi.InvoiceItem(invoice)) |
| 187 | + |
| 188 | + if rt.rtype == RecurringTransaction.YEARLY: |
| 189 | + invoice.items[0].description = "%s %d" % (t.tag.label, year) |
| 190 | + else: |
| 191 | + invoice.items[0].description = "%s %02d/%d" % (t.tag.label, month, year) |
| 192 | + |
| 193 | + invoice.items[0].net = -t.amount # Negative amount transaction -> positive amount invoice |
| 194 | + if t.tag.holvi_code: |
| 195 | + invoice.items[0].category = holviapi.IncomeCategory(invoice.api.categories_api, { 'code': t.tag.holvi_code }) # Lazy-loading category, avoids a GET |
| 196 | + invoice.subject = "%s / %s" % (invoice.items[0].description, invoice.receiver.name) |
| 197 | + |
| 198 | + invoice = invoice.save() |
| 199 | + invoice.send() |
| 200 | + print("Created (and sent) Holvi invoice %s" % invoice.code) |
| 201 | + t.reference = invoice.rf_reference |
173 | 202 | return True
|
174 | 203 |
|
175 | 204 | def on_created(self, rt, t, *args, **kwargs):
|
|
0 commit comments