Browse Source

Adding migrations for new billing system

Alexandre Aubin 7 years ago
parent
commit
fd33615e6d

+ 54 - 0
coin/billing/migrations/0005_new_billing_system_schema.py

@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+from django.conf import settings
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('billing', '0004_auto_20161230_1803'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='PaymentAllocation',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('amount', models.DecimalField(null=True, verbose_name='montant', max_digits=5, decimal_places=2)),
+            ],
+        ),
+        migrations.AddField(
+            model_name='invoice',
+            name='date_last_reminder_email',
+            field=models.DateTimeField(null=True, verbose_name='Date du dernier email de relance envoy\xe9', blank=True),
+        ),
+        migrations.AddField(
+            model_name='payment',
+            name='label',
+            field=models.CharField(default='', max_length=500, null=True, verbose_name='libell\xe9', blank=True),
+        ),
+        migrations.AddField(
+            model_name='payment',
+            name='member',
+            field=models.ForeignKey(related_name='payments', on_delete=django.db.models.deletion.SET_NULL, default=None, blank=True, to=settings.AUTH_USER_MODEL, null=True, verbose_name='membre'),
+        ),
+        migrations.AlterField(
+            model_name='payment',
+            name='invoice',
+            field=models.ForeignKey(related_name='payments', verbose_name='facture associ\xe9e', blank=True, to='billing.Invoice', null=True),
+        ),
+        migrations.AddField(
+            model_name='paymentallocation',
+            name='invoice',
+            field=models.ForeignKey(related_name='allocations', verbose_name='facture associ\xe9e', to='billing.Invoice'),
+        ),
+        migrations.AddField(
+            model_name='paymentallocation',
+            name='payment',
+            field=models.ForeignKey(related_name='allocations', verbose_name='facture associ\xe9e', to='billing.Payment'),
+        ),
+    ]

+ 73 - 0
coin/billing/migrations/0006_new_billing_system_data.py

@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+import sys
+
+from django.db import migrations
+
+from coin.members.models import Member
+from coin.billing.models import Invoice, InvoiceDetail, Payment
+
+
+def check_current_state(apps, schema_editor):
+
+    for invoice in Invoice.objects.all():
+
+        invoice_name = invoice.__unicode__()
+
+        related_payments = invoice.payments.all()
+
+        total_related_payments = sum([p.amount for p in related_payments])
+
+        if total_related_payments > invoice.amount:
+            error = "For invoice, current sum of payment is higher than total of invoice. Please fix this before running this migration" % invoice_name
+            raise AssertionError(error.encode('utf-8'))
+
+        if total_related_payments != 0 and not invoice.validated:
+            error = "Invoice %s is not validated but already has allocated payments. Please remove them before running this migration" % invoice_name
+            raise AssertionError(error.encode('utf-8'))
+
+
+def forwards(apps, schema_editor):
+
+    # Create allocation for all payment to their respective invoice
+    for payment in Payment.objects.all():
+        payment.member = payment.invoice.member
+        payment.allocate_to_invoice(payment.invoice)
+
+    # Update balance for all members
+    for member in Member.objects.all():
+
+        this_member_invoices = [i for i in member.invoices.filter(validated=True).order_by("date")]
+        this_member_payments = [p for p in member.payments.order_by("date")]
+
+        member.balance = compute_balance(this_member_invoices,
+                                         this_member_payments)
+        member.save()
+
+
+def compute_balance(invoices, payments):
+
+    active_payments = [p for p in payments if p.amount_not_allocated()    > 0]
+    active_invoices = [i for i in invoices if i.amount_remaining_to_pay() > 0]
+
+    s = 0
+    s -= sum([i.amount_remaining_to_pay() for i in active_invoices])
+    s += sum([p.amount_not_allocated()    for p in active_payments])
+
+    return s
+
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('billing', '0005_new_billing_system_schema'),
+        ('members', '0014_member_balance'),
+    ]
+
+    operations = [
+        migrations.RunPython(check_current_state),
+        migrations.RunPython(forwards),
+    ]
+

+ 19 - 0
coin/members/migrations/0014_member_balance.py

@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('members', '0013_auto_20161015_1837'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='member',
+            name='balance',
+            field=models.DecimalField(default=0, verbose_name='account balance', max_digits=5, decimal_places=2),
+        ),
+    ]