Skip to content

Commit 9a207e6

Browse files
committed
Use new holviapp helpers when creating Holvi invoice
Also handle monthly/yearly differently and add support for holvi categories
1 parent eda0a0a commit 9a207e6

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

project/examples/handlers.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
import logging
33
import environ
44
import datetime, calendar
5-
from django.utils import timezone
5+
import holviapi
66
from django.core.mail import EmailMessage
77
from members.handlers import BaseApplicationHandler, BaseMemberHandler
88
from creditor.handlers import BaseTransactionHandler, BaseRecurringTransactionsHandler
9-
from creditor.models import Transaction, TransactionTag
9+
from creditor.models import Transaction, TransactionTag, RecurringTransaction
1010
from django.utils.translation import ugettext_lazy as _
11-
import environ
11+
from holviapp.utils import api_configured, get_invoiceapi
1212

1313
logger = logging.getLogger('example.handlers')
14-
env = environ.Env()
1514

1615

1716
class ExampleBaseHandler(BaseMemberHandler):
@@ -160,16 +159,46 @@ def on_creating(self, rt, t, *args, **kwargs):
160159
msg = "on_creating called for %s (from %s)" % (t, rt)
161160
logger.info(msg)
162161
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:
171164
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
173202
return True
174203

175204
def on_created(self, rt, t, *args, **kwargs):

project/examples/utils.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)