models.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import uuid
  5. import re
  6. import logging
  7. from decimal import Decimal
  8. from django.conf import settings
  9. from django.db import models, transaction
  10. from django.utils import timezone
  11. from django.utils.encoding import python_2_unicode_compatible
  12. from django.dispatch import receiver
  13. from django.db.models.signals import post_save
  14. from coin.offers.models import OfferSubscription
  15. from coin.members.models import Member
  16. from coin.html2pdf import render_as_pdf
  17. from coin.utils import private_files_storage, start_of_month, end_of_month, \
  18. postgresql_regexp, send_templated_email
  19. from coin.isp_database.context_processors import branding
  20. accounting_log = logging.getLogger("coin.billing")
  21. def invoice_pdf_filename(instance, filename):
  22. """Nom et chemin du fichier pdf à stocker pour les factures"""
  23. member_id = instance.member.id if instance.member else 0
  24. return 'invoices/%d_%s_%s.pdf' % (member_id,
  25. instance.number,
  26. uuid.uuid4())
  27. @python_2_unicode_compatible
  28. class InvoiceNumber:
  29. """ Logic and validation of invoice numbers
  30. Defines invoice numbers serie in a way that is legal in france.
  31. https://www.service-public.fr/professionnels-entreprises/vosdroits/F23208#fiche-item-3
  32. Our format is YYYY-MM-XXXXXX
  33. - YYYY the year of the bill
  34. - MM month of the bill
  35. - XXXXXX a per-month sequence
  36. """
  37. RE_INVOICE_NUMBER = re.compile(
  38. r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<index>\d{6})')
  39. def __init__(self, date, index):
  40. self.date = date
  41. self.index = index
  42. def get_next(self):
  43. return InvoiceNumber(self.date, self.index + 1)
  44. def __str__(self):
  45. return '{:%Y-%m}-{:0>6}'.format(self.date, self.index)
  46. @classmethod
  47. def parse(cls, string):
  48. m = cls.RE_INVOICE_NUMBER.match(string)
  49. if not m:
  50. raise ValueError('Not a valid invoice number: "{}"'.format(string))
  51. return cls(
  52. datetime.date(
  53. year=int(m.group('year')),
  54. month=int(m.group('month')),
  55. day=1),
  56. int(m.group('index')))
  57. @staticmethod
  58. def time_sequence_filter(date, field_name='date'):
  59. """ Build queryset filter to be used to get the invoices from the
  60. numbering sequence of a given date.
  61. :param field_name: the invoice field name to filter on.
  62. :type date: datetime
  63. :rtype: dict
  64. """
  65. return {'{}__month'.format(field_name): date.month}
  66. class InvoiceQuerySet(models.QuerySet):
  67. def get_next_invoice_number(self, date):
  68. last_invoice_number_str = self._get_last_invoice_number(date)
  69. if last_invoice_number_str is None:
  70. # It's the first bill of the month
  71. invoice_number = InvoiceNumber(date, 1)
  72. else:
  73. invoice_number = InvoiceNumber.parse(last_invoice_number_str).get_next()
  74. return str(invoice_number)
  75. def _get_last_invoice_number(self, date):
  76. same_seq_filter = InvoiceNumber.time_sequence_filter(date)
  77. return self.filter(**same_seq_filter).with_valid_number().aggregate(
  78. models.Max('number'))['number__max']
  79. def with_valid_number(self):
  80. """ Excludes previous numbering schemes or draft invoices
  81. """
  82. return self.filter(number__regex=postgresql_regexp(
  83. InvoiceNumber.RE_INVOICE_NUMBER))
  84. class Invoice(models.Model):
  85. INVOICES_STATUS_CHOICES = (
  86. ('open', 'À payer'),
  87. ('closed', 'Réglée'),
  88. ('trouble', 'Litige')
  89. )
  90. validated = models.BooleanField(default=False, verbose_name='validée',
  91. help_text='Once validated, a PDF is generated'
  92. ' and the invoice cannot be modified')
  93. number = models.CharField(max_length=25,
  94. unique=True,
  95. verbose_name='numéro')
  96. status = models.CharField(max_length=50, choices=INVOICES_STATUS_CHOICES,
  97. default='open',
  98. verbose_name='statut')
  99. date = models.DateField(
  100. default=datetime.date.today, null=True, verbose_name='date',
  101. help_text='Cette date sera définie à la date de validation dans la facture finale')
  102. date_due = models.DateField(
  103. null=True, blank=True,
  104. verbose_name="date d'échéance de paiement",
  105. help_text='Le délai de paiement sera fixé à {} jours à la validation si laissé vide'.format(settings.PAYMENT_DELAY))
  106. member = models.ForeignKey(Member, null=True, blank=True, default=None,
  107. related_name='invoices',
  108. verbose_name='membre',
  109. on_delete=models.SET_NULL)
  110. pdf = models.FileField(storage=private_files_storage,
  111. upload_to=invoice_pdf_filename,
  112. null=True, blank=True,
  113. verbose_name='PDF')
  114. amount_paid = models.DecimalField(max_digits=5,
  115. decimal_places=2,
  116. default=0,
  117. verbose_name='montant payé')
  118. date_last_reminder_email = models.DateTimeField(null=True, blank=True,
  119. verbose_name="Date du dernier email de relance envoyé")
  120. def save(self, *args, **kwargs):
  121. # First save to get a PK
  122. super(Invoice, self).save(*args, **kwargs)
  123. # Then use that pk to build draft invoice number
  124. if not self.validated and self.pk and not self.number:
  125. self.number = 'DRAFT-{}'.format(self.pk)
  126. self.save()
  127. def amount(self):
  128. """
  129. Calcul le montant de la facture
  130. en fonction des éléments de détails
  131. """
  132. total = Decimal('0.0')
  133. for detail in self.details.all():
  134. total += detail.total()
  135. return total.quantize(Decimal('0.01'))
  136. amount.short_description = 'Montant'
  137. def amount_before_tax(self):
  138. total = Decimal('0.0')
  139. for detail in self.details.all():
  140. total += detail.amount
  141. return total.quantize(Decimal('0.01'))
  142. amount_before_tax.short_description = 'Montant HT'
  143. def amount_paid(self):
  144. """
  145. Calcul le montant payé de la facture en fonction des éléments
  146. de paiements
  147. """
  148. total = Decimal('0.0')
  149. for payment in self.payments.all():
  150. total += payment.amount
  151. return total.quantize(Decimal('0.01'))
  152. amount_paid.short_description = 'Montant payé'
  153. def amount_remaining_to_pay(self):
  154. """
  155. Calcul le montant restant à payer
  156. """
  157. return self.amount() - self.amount_paid
  158. amount_remaining_to_pay.short_description = 'Reste à payer'
  159. def has_owner(self, username):
  160. """
  161. Check if passed username (ex gmajax) is owner of the invoice
  162. """
  163. return (self.member and self.member.username == username)
  164. def generate_pdf(self):
  165. """
  166. Make and store a pdf file for the invoice
  167. """
  168. context = {"invoice": self}
  169. context.update(branding(None))
  170. pdf_file = render_as_pdf('billing/invoice_pdf.html', context)
  171. self.pdf.save('%s.pdf' % self.number, pdf_file)
  172. @transaction.atomic
  173. def validate(self):
  174. """
  175. Switch invoice to validate mode. This set to False the draft field
  176. and generate the pdf
  177. """
  178. self.date = datetime.date.today()
  179. if not self.date_due:
  180. self.date_due = self.date + datetime.timedelta(days=settings.PAYMENT_DELAY)
  181. old_number = self.number
  182. self.number = Invoice.objects.get_next_invoice_number(self.date)
  183. self.validated = True
  184. self.save()
  185. self.generate_pdf()
  186. accounting_log.info("Draft invoice %s validated as invoice %s. "
  187. "(Total amount : %f ; Member : %s)"
  188. % (old_number, self.number, self.amount(), self.member))
  189. assert self.pdf_exists()
  190. if self.member is not None:
  191. update_accounting_for_member(self.member)
  192. def pdf_exists(self):
  193. return (self.validated
  194. and bool(self.pdf)
  195. and private_files_storage.exists(self.pdf.name))
  196. def get_absolute_url(self):
  197. from django.core.urlresolvers import reverse
  198. return reverse('billing:invoice', args=[self.number])
  199. def __unicode__(self):
  200. return '#%s %0.2f€ %s' % (self.number, self.amount(), self.date_due)
  201. def reminder_needed(self):
  202. # If there's no member, there's nobody to be reminded
  203. if self.member is None:
  204. return False
  205. # If bill is close or not validated yet, nope
  206. if self.status != 'open' or not self.validated:
  207. return False
  208. from dateutil.relativedelta import relativedelta
  209. # If bill is not at least one month old, nope
  210. if self.date_due >= timezone.now()+relativedelta(weeks=-4):
  211. return False
  212. # If a reminder has been recently sent, nope
  213. if (self.date_last_reminder_email
  214. and (self.date_last_reminder_email
  215. >= timezone.now() + relativedelta(weeks=-3))):
  216. return False
  217. return True
  218. def send_reminder(self, auto=False):
  219. """ Envoie un courrier pour rappeler à un abonné qu'une facture est
  220. en attente de paiement
  221. :param bill: id of the bill to remind
  222. :param auto: is it an auto email? (changes slightly template content)
  223. """
  224. if not self.reminder_needed():
  225. return False
  226. accounting_log.info("Sending reminder email to %s to pay invoice %s"
  227. % (str(self.member), str(self.number)))
  228. from coin.isp_database.models import ISPInfo
  229. isp_info = ISPInfo.objects.first()
  230. kwargs = {}
  231. # Il peut ne pas y avir d'ISPInfo, ou bien pas d'administrative_email
  232. if isp_info and isp_info.administrative_email:
  233. kwargs['from_email'] = isp_info.administrative_email
  234. # Si le dernier courriel de relance a été envoyé il y a moins de trois
  235. # semaines, n'envoi pas un nouveau courriel
  236. send_templated_email(
  237. to=self.member.email,
  238. subject_template='billing/emails/reminder_for_unpaid_bill.txt',
  239. body_template='billing/emails/reminder_for_unpaid_bill.html',
  240. context={'member': self.member, 'branding': isp_info,
  241. 'membership_info_url': settings.MEMBER_MEMBERSHIP_INFO_URL,
  242. 'today': datetime.date.today,
  243. 'auto_sent': auto},
  244. **kwargs)
  245. # Sauvegarde en base la date du dernier envoi de mail de relance
  246. self.date_last_call_for_membership_fees_email = timezone.now()
  247. self.save()
  248. return True
  249. class Meta:
  250. verbose_name = 'facture'
  251. objects = InvoiceQuerySet().as_manager()
  252. class InvoiceDetail(models.Model):
  253. label = models.CharField(max_length=100)
  254. amount = models.DecimalField(max_digits=5, decimal_places=2,
  255. verbose_name='montant')
  256. quantity = models.DecimalField(null=True, verbose_name='quantité',
  257. default=1.0, decimal_places=2, max_digits=4)
  258. tax = models.DecimalField(null=True, default=0.0, decimal_places=2,
  259. max_digits=4, verbose_name='TVA',
  260. help_text='en %')
  261. invoice = models.ForeignKey(Invoice, verbose_name='facture',
  262. related_name='details')
  263. offersubscription = models.ForeignKey(OfferSubscription, null=True,
  264. blank=True, default=None,
  265. verbose_name='abonnement')
  266. period_from = models.DateField(
  267. default=start_of_month,
  268. null=True,
  269. blank=True,
  270. verbose_name='début de période',
  271. help_text='Date de début de période sur laquelle est facturé cet item')
  272. period_to = models.DateField(
  273. default=end_of_month,
  274. null=True,
  275. blank=True,
  276. verbose_name='fin de période',
  277. help_text='Date de fin de période sur laquelle est facturé cet item')
  278. def __unicode__(self):
  279. return self.label
  280. def total(self):
  281. """Calcul le total"""
  282. return (self.amount * (self.tax / Decimal('100.0') +
  283. Decimal('1.0')) *
  284. self.quantity).quantize(Decimal('0.01'))
  285. class Meta:
  286. verbose_name = 'détail de facture'
  287. class Payment(models.Model):
  288. PAYMENT_MEAN_CHOICES = (
  289. ('cash', 'Espèces'),
  290. ('check', 'Chèque'),
  291. ('transfer', 'Virement'),
  292. ('other', 'Autre')
  293. )
  294. member = models.ForeignKey(Member, null=True, blank=True, default=None,
  295. related_name='payments',
  296. verbose_name='membre',
  297. on_delete=models.SET_NULL)
  298. payment_mean = models.CharField(max_length=100, null=True,
  299. default='transfer',
  300. choices=PAYMENT_MEAN_CHOICES,
  301. verbose_name='moyen de paiement')
  302. amount = models.DecimalField(max_digits=5, decimal_places=2, null=True,
  303. verbose_name='montant')
  304. date = models.DateField(default=datetime.date.today)
  305. invoice = models.ForeignKey(Invoice, verbose_name='facture associée', null=True,
  306. blank=True, related_name='payments')
  307. amount_already_allocated = models.DecimalField(max_digits=5,
  308. decimal_places=2,
  309. null=True, blank=True, default=0.0,
  310. verbose_name='montant déjà alloué')
  311. label = models.CharField(max_length=500,
  312. null=True, blank=True, default="",
  313. verbose_name='libellé')
  314. def save(self, *args, **kwargs):
  315. super(Payment, self).save(*args, **kwargs)
  316. # If this payment is related to a member, update the accounting for
  317. # this member
  318. if self.member is not None:
  319. update_accounting_for_member(self.member)
  320. def amount_not_allocated(self):
  321. return self.amount - self.amount_already_allocated
  322. @transaction.atomic
  323. def allocate_to_invoice(self, invoice):
  324. amount_can_pay = self.amount_not_allocated()
  325. amount_to_pay = invoice.amount_remaining_to_pay()
  326. amount_to_allocate = min(amount_can_pay, amount_to_pay)
  327. accounting_log.info("Allocating %f from payment %s to invoice %s"
  328. % (float(amount_to_allocate), str(self.date),
  329. invoice.number))
  330. self.amount_already_allocated += amount_to_allocate
  331. invoice.amount_paid += amount_to_allocate
  332. # Close invoice if relevant
  333. if (invoice.amount_remaining_to_pay() <= 0) and (invoice.status == "open"):
  334. accounting_log.info("Invoice %s has been paid and is now closed"
  335. % invoice.number)
  336. invoice.status = "closed"
  337. invoice.save()
  338. self.save()
  339. @property
  340. def name(self):
  341. return str(self)
  342. def __unicode__(self):
  343. if self.member is not None:
  344. return 'Paiment de %0.2f€ le %s par %s' \
  345. % (self.amount, str(self.date), self.member)
  346. else:
  347. return 'Paiment de %0.2f€ le %s' \
  348. % (self.amount, str(self.date))
  349. class Meta:
  350. verbose_name = 'paiement'
  351. def get_active_payment_and_invoices(member):
  352. # Fetch relevant and active payments / invoices
  353. # and sort then by chronological order : olders first, newers last.
  354. this_member_invoices = [i for i in member.invoices
  355. .filter(validated=True)
  356. .order_by("date")]
  357. this_member_payments = [p for p in member.payments
  358. .order_by("date")]
  359. # TODO / FIXME ^^^ maybe also consider only 'opened' invoices (i.e. not
  360. # conflict / trouble invoices)
  361. active_payments = [p for p in this_member_payments if p.amount_not_allocated() > 0]
  362. active_invoices = [p for p in this_member_invoices if p.amount_remaining_to_pay() > 0]
  363. return active_payments, active_invoices
  364. def update_accounting_for_member(member):
  365. """
  366. Met à jour le status des factures, des paiements et le solde du compte
  367. d'un utilisateur
  368. """
  369. accounting_log.info("Updating accounting for member %s ..."
  370. % str(member))
  371. accounting_log.info("Member %s current balance is %f ..."
  372. % (str(member), float(member.balance)))
  373. reconcile_invoices_and_payments(member)
  374. this_member_invoices = [i for i in member.invoices
  375. .filter(validated=True)
  376. .order_by("date")]
  377. this_member_payments = [p for p in member.payments
  378. .order_by("date")]
  379. member.balance = compute_balance(this_member_invoices,
  380. this_member_payments)
  381. member.save()
  382. accounting_log.info("Member %s new balance is %f"
  383. % (str(member), float(member.balance)))
  384. def reconcile_invoices_and_payments(member):
  385. """
  386. Rapproche des factures et des paiements qui sont actifs (paiement non alloué
  387. ou factures non entièrement payées) automatiquement.
  388. """
  389. active_payments, active_invoices = get_active_payment_and_invoices(member)
  390. if active_payments == []:
  391. accounting_log.info("(No active payment for %s. No invoice/payment "
  392. "reconciliation needed.)."
  393. % str(member))
  394. return
  395. elif active_invoices == []:
  396. accounting_log.info("(No active invoice for %s. No invoice/payment "
  397. "reconciliation needed.)."
  398. % str(member))
  399. return
  400. accounting_log.info("Initiating reconciliation between "
  401. "invoice and payments for %s" % str(member))
  402. while active_payments != [] and active_invoices != []:
  403. # Only consider the oldest active payment and the oldest active invoice
  404. p = active_payments[0]
  405. i = active_invoices[0]
  406. # TODO : should add an assert that the ammount not allocated / remaining to
  407. # pay is lower before and after calling the allocate_to_invoice
  408. p.allocate_to_invoice(i)
  409. active_payments, active_invoices = get_active_payment_and_invoices(member)
  410. if active_payments == []:
  411. accounting_log.info("No more active payment. Nothing to reconcile anymore.")
  412. elif active_invoices == []:
  413. accounting_log.info("No more active invoice. Nothing to reconcile anymore.")
  414. return
  415. def compute_balance(invoices, payments):
  416. active_payments = [p for p in payments if p.amount_not_allocated() > 0]
  417. active_invoices = [i for i in invoices if i.amount_remaining_to_pay() > 0]
  418. s = 0
  419. s -= sum([i.amount_remaining_to_pay() for i in active_invoices])
  420. s += sum([p.amount_not_allocated() for p in active_payments])
  421. return s
  422. def test_accounting_update():
  423. Member.objects.all().delete()
  424. Payment.objects.all().delete()
  425. Invoice.objects.all().delete()
  426. johndoe = Member.objects.create(username="johndoe",
  427. first_name="John",
  428. last_name="Doe",
  429. email="johndoe@yolo.test")
  430. johndoe.set_password("trololo")
  431. # First facture
  432. invoice = Invoice.objects.create(number="1337",
  433. member=johndoe)
  434. InvoiceDetail.objects.create(label="superservice",
  435. amount="15.0",
  436. invoice=invoice)
  437. invoice.validate()
  438. # Second facture
  439. invoice2 = Invoice.objects.create(number="42",
  440. member=johndoe)
  441. InvoiceDetail.objects.create(label="superservice",
  442. amount="42",
  443. invoice=invoice2)
  444. invoice2.validate()
  445. # Payment
  446. payment = Payment.objects.create(amount=20,
  447. member=johndoe)
  448. Member.objects.all().delete()
  449. Payment.objects.all().delete()
  450. Invoice.objects.all().delete()
  451. @receiver(post_save, sender=Payment)
  452. def payment_changed(sender, instance, created, **kwargs):
  453. if created:
  454. accounting_log.info("Adding payment %s (Date: %s, Member: %s, Amount: %s, Label: %s)."
  455. % (instance.pk, instance.date, instance.member,
  456. instance.amount, instance.label))
  457. else:
  458. accounting_log.info("Updating payment %s (Date: %s, Member: %s, Amount: %s, Label: %s, Allocated : )%s."
  459. % (instance.pk, instance.date, instance.member,
  460. instance.amount, instance.label,
  461. instance.amount_already_allocated))
  462. @receiver(post_save, sender=Invoice)
  463. def invoice_changed(sender, instance, created, **kwargs):
  464. if created:
  465. accounting_log.info("Creating draft invoice %s (Member: %s)."
  466. % ('DRAFT-{}'.format(instance.pk), instance.member))
  467. else:
  468. if not instance.validated:
  469. accounting_log.info("Updating draft invoice %s (Member: %s)."
  470. % (instance.number, instance.member))
  471. else:
  472. accounting_log.info("Updating invoice %s (Member: %s, Total amount: %s, Amount paid: %s)."
  473. % (instance.number, instance.member, instance.amount(), instance.amount_paid ))