1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.contrib.auth.models import User
from django.conf import settings
from datetime import datetime
from decimal import Decimal
from payment import PaymentMethodWrapper
from postgresqleu.util.validators import ListOfEmailAddressValidator
from postgresqleu.accounting.models import Account
class InvoiceProcessor(models.Model):
# The processor name is purely cosmetic
processorname = models.CharField(max_length=50, null=False, blank=False, unique=True)
# Python class name (full path) to the class that should be
# notified when an invoice has been processed.
classname = models.CharField(max_length=200, null=False, blank=False)
def __unicode__(self):
return self.processorname
class InvoicePaymentMethod(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
active = models.BooleanField(null=False, blank=False, default=True)
sortkey = models.IntegerField(null=False, blank=False, default=100)
internaldescription = models.CharField(max_length=100, null=False, blank=True)
# Python class name (full path) to the class that implements
# this payment method.
classname = models.CharField(max_length=200, null=False, blank=False, unique=True)
auto = models.BooleanField(null=False, blank=False, default=True, verbose_name="Used by automatically generated invoices")
def __unicode__(self):
return self.name
class Meta:
ordering = ['sortkey', ]
class InvoiceRefund(models.Model):
reason = models.CharField(max_length=500, null=False, blank=True, default='', help_text="Reason for refunding of invoice")
amount = models.DecimalField(max_digits=10, decimal_places=2, null=False)
vatamount = models.DecimalField(max_digits=10, decimal_places=2, null=False)
vatrate = models.ForeignKey('VatRate', null=True, on_delete=models.CASCADE)
registered = models.DateTimeField(null=False, auto_now_add=True)
issued = models.DateTimeField(null=True, blank=True)
completed = models.DateTimeField(null=True, blank=True)
payment_reference = models.CharField(max_length=100, null=False, blank=True, help_text="Reference in payment system, depending on system used for invoice.")
refund_pdf = models.TextField(blank=True, null=False)
@property
def fullamount(self):
return self.amount + self.vatamount
class Invoice(models.Model):
# pk = invoice number, which is fully exposed.
# The recipient. We set the user if we have matched it to an
# account, but support invoices that are just listed
# by name. If email is set, we can retro-match it up, but once
# a recipient is matched, the recipient_user field "owns" the
# recipient information.
recipient_user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
recipient_email = models.EmailField(blank=True, null=False)
recipient_name = models.CharField(max_length=100, blank=False, null=False)
recipient_address = models.TextField(blank=False, null=False)
recipient_secret = models.CharField(max_length=64, blank=True, null=True)
extra_bcc_list = models.CharField(max_length=500, blank=True, null=False, validators=[ListOfEmailAddressValidator, ])
# Global invoice info
title = models.CharField(max_length=100, blank=False, null=False, verbose_name="Invoice title")
invoicedate = models.DateTimeField(null=False, blank=False)
duedate = models.DateTimeField(null=False, blank=False)
canceltime = models.DateTimeField(null=True, blank=True, help_text="Invoice will automatically be canceled at this time")
# Amount information is calculated when the invoice is finalized
total_amount = models.DecimalField(decimal_places=2, max_digits=10, null=False)
total_vat = models.DecimalField(decimal_places=2, max_digits=10, null=False, default=0)
reverse_vat = models.BooleanField(null=False, blank=False, default=False, help_text="Invoice is subject to EU reverse VAT")
finalized = models.BooleanField(null=False, blank=True, default=False, help_text="Invoice is finalized, should not ever be changed again")
deleted = models.BooleanField(null=False, blank=False, default=False, help_text="This invoice has been deleted")
deletion_reason = models.CharField(max_length=500, null=False, blank=True, default='', help_text="Reason for deletion of invoice")
refund = models.OneToOneField(InvoiceRefund, null=True, blank=True, on_delete=models.SET_NULL)
# base64 encoded version of the PDF invoice
pdf_invoice = models.TextField(blank=True, null=False)
# Which class, if any, is responsible for processing the payment
# of this invoice. This can typically be to flag a conference
# payment as done once the payment is in. processorid is an arbitrary
# id value that the processor can use for whatever it wants.
processor = models.ForeignKey(InvoiceProcessor, null=True, blank=True, on_delete=models.CASCADE)
processorid = models.IntegerField(null=True, blank=True)
# Allowed payment methods
allowedmethods = models.ManyToManyField(InvoicePaymentMethod, blank=True, verbose_name="Allowed payment methods")
bankinfo = models.BooleanField(null=False, blank=False, default=True, verbose_name="Include bank details on invoice")
# Payment status of this invoice. Once it's paid, the payment system
# writes the details of the transaction to the paymentdetails field.
paidat = models.DateTimeField(null=True, blank=True)
paymentdetails = models.CharField(max_length=100, null=False, blank=True)
paidusing = models.ForeignKey(InvoicePaymentMethod, null=True, blank=True, related_name="paidusing", verbose_name="Payment method actually used", on_delete=models.CASCADE)
# Reminder (if any) sent when?
remindersent = models.DateTimeField(null=True, blank=True, verbose_name="Automatic reminder sent at")
# Once an invoice is paid, a recipient is generated. PDF base64
pdf_receipt = models.TextField(blank=True, null=False)
# Information for accounting of this invoice. This is intentionally not
# foreign keys - we'll just drop some such information into the system
# manually in the forms.
accounting_account = models.IntegerField(null=True, blank=True, verbose_name="Accounting account")
accounting_object = models.CharField(null=True, blank=True, max_length=30, verbose_name="Accounting object")
@property
def has_recipient_user(self):
return self.recipientuser and True or False
@property
def ispaid(self):
return self.paidat is not None
@property
def isexpired(self):
return (self.paidat is None) and self.duedate and (self.duedate < datetime.now())
@property
def allowedmethodwrappers(self):
return [PaymentMethodWrapper(m, self) for m in self.allowedmethods.all()]
@property
def invoicestr(self):
return "%s #%s - %s" % (settings.INVOICE_TITLE_PREFIX, self.pk, self.title)
@property
def payment_fees(self):
if self.paidusing:
return PaymentMethodWrapper(self.paidusing, self).payment_fees
else:
return "unknown"
@property
def amount_without_fees(self):
f = self.payment_fees
if type(f) == str:
return "Unknown"
else:
return self.total_amount - f
@property
def amount_without_vat(self):
return self.total_amount - self.total_vat
def used_vatrates(self):
return ", ".join([unicode(r.vatrate) for r in self.invoicerow_set.all() if r.vatrate])
@property
def can_autorefund(self):
return PaymentMethodWrapper(self.paidusing, self).can_autorefund
def autorefund(self):
return PaymentMethodWrapper(self.paidusing, self).autorefund()
@property
def payment_method_description(self):
if not self.paidat:
return "not paid"
if self.paidusing:
return "paid using {0}.".format(self.paidusing.name)
return "manually flagged as paid."
@property
def statusstring(self):
if self.deleted:
return "canceled"
elif self.refund:
return "refunded"
elif self.paidat:
return "paid"
if self.finalized:
return "finalized"
else:
return "pending"
def __unicode__(self):
return "Invoice #%s" % self.pk
class Meta:
ordering = ('-id', )
class VatRate(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
shortname = models.CharField(max_length=16, blank=False, null=False)
vatpercent = models.IntegerField(null=False, default=0, verbose_name="VAT percentage",
validators=[MaxValueValidator(100), MinValueValidator(0)])
vataccount = models.ForeignKey(Account, null=False, blank=False, on_delete=models.CASCADE)
_safe_attributes = ('vatpercent', 'shortstr', 'shortname', 'name', 'org_name', 'treasurer_email')
def __unicode__(self):
return u"{0} ({1}%)".format(self.name, self.vatpercent)
@property
def shortstr(self):
return "%s%% (%s)" % (self.vatpercent, self.shortname)
class InvoiceRow(models.Model):
# Invoice rows are only used up until the invoice is finished,
# but allows us to save a half-finished invoice.
invoice = models.ForeignKey(Invoice, null=False, on_delete=models.CASCADE)
rowtext = models.CharField(max_length=100, blank=False, null=False, verbose_name="Text")
rowcount = models.IntegerField(null=False, default=1, verbose_name="Count")
rowamount = models.DecimalField(decimal_places=2, max_digits=10, null=False, default=0, verbose_name="Amount per item (ex VAT)")
vatrate = models.ForeignKey(VatRate, null=True, on_delete=models.CASCADE)
def __unicode__(self):
return self.rowtext
@property
def totalvat(self):
if self.vatrate:
return (self.rowamount * self.rowcount * self.vatrate.vatpercent / Decimal(100)).quantize(Decimal('.01'))
else:
return 0
@property
def totalrow(self):
return self.rowamount * self.rowcount
@property
def totalwithvat(self):
return self.totalrow + self.totalvat
class InvoiceHistory(models.Model):
invoice = models.ForeignKey(Invoice, null=False, on_delete=models.CASCADE)
time = models.DateTimeField(null=False, blank=False, auto_now_add=True)
txt = models.CharField(max_length=100, null=False, blank=False)
class Meta:
ordering = ['time', ]
def __unicode__(self):
return self.txt
class InvoiceLog(models.Model):
timestamp = models.DateTimeField(null=False, blank=False, auto_now_add=True)
message = models.TextField(null=False, blank=False)
sent = models.BooleanField(null=False, blank=False, default=False)
@property
def message_trunc(self):
return self.message[:150]
class Meta:
ordering = ['-timestamp', ]
|