Browse Source

Add PaymentMethod model in billing to store means of payments dynamically.
Data migration for invoice payments

Fabs 10 years ago
parent
commit
7b705d40a0

+ 1 - 1
coin/billing/admin.py

@@ -67,7 +67,7 @@ class InvoiceDetailInlineReadOnly(admin.StackedInline):
 class PaymentInline(admin.StackedInline):
     model = Payment
     extra = 0
-    fields = (('date', 'payment_mean', 'amount'),)
+    fields = (('date', 'method', 'amount'),)
 
 
 class InvoiceAdmin(admin.ModelAdmin):

+ 32 - 0
coin/billing/migrations/0003_auto_20141101_2337.py

@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('billing', '0002_auto_20141002_0204'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='PaymentMethod',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('name', models.CharField(max_length=100, verbose_name='nom')),
+            ],
+            options={
+                'verbose_name': 'moyen de paiment',
+                'verbose_name_plural': 'moyens de paiment',
+            },
+            bases=(models.Model,),
+        ),
+        migrations.AddField(
+            model_name='payment',
+            name='method',
+            field=models.ForeignKey(verbose_name='moyen de paiment', to='billing.PaymentMethod', null=True),
+            preserve_default=True,
+        ),
+    ]

+ 53 - 0
coin/billing/migrations/0004_auto_20141101_2347.py

@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+PAYMENT_MEAN_MAPPING = {
+    'cash': (1, 'Espèces'),
+    'check': (2, 'Chèque'),
+    'transfer': (3, 'Virement'),
+    'other': (4, 'Autre')
+}
+
+def forward(apps, schema_editor):
+    PaymentMethod = apps.get_model("billing", "PaymentMethod")
+
+    # Add default payment means:
+    for key, payment_mean_values in PAYMENT_MEAN_MAPPING.iteritems():
+        payment_method = PaymentMethod(
+            id=payment_mean_values[0],
+            name=payment_mean_values[1])
+        payment_method.save()
+        # PAYMENT_MEAN_MAPPING[key] += (payment_method,)
+
+    # Change invoices payment mean choice field to foreign key
+    Payment = apps.get_model("billing", "Payment")
+
+    for payment in Payment.objects.all():
+        if payment.payment_mean  and payment.payment_mean in PAYMENT_MEAN_MAPPING:
+            payment.method_id = PAYMENT_MEAN_MAPPING[payment.payment_mean][0]
+        payment.save()
+
+def backward(apps, schema_editor):
+    # Change invoices payment mean foreign key field to choice
+    Payment = apps.get_model("billing", "Payment")
+
+    for payment in Payment.objects.all():
+        if payment.method_id:
+            for k,v in PAYMENT_MEAN_MAPPING.iteritems():
+                if v[0]==payment.method_id:
+                    payment.payment_mean = k
+        payment.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('billing', '0003_auto_20141101_2337'),
+    ]
+
+    operations = [
+        migrations.RunPython(forward, backward),
+    ]

+ 18 - 0
coin/billing/migrations/0005_remove_payment_payment_mean.py

@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('billing', '0004_auto_20141101_2347'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='payment',
+            name='payment_mean',
+        ),
+    ]

+ 24 - 11
coin/billing/models.py

@@ -173,19 +173,32 @@ class InvoiceDetail(models.Model):
         verbose_name = 'détail de facture'
 
 
-class Payment(models.Model):
+class PaymentMethod(models.Model):
+    name = models.CharField(max_length=100, verbose_name='nom')
 
-    PAYMENT_MEAN_CHOICES = (
-        ('cash', 'Espèces'),
-        ('check', 'Chèque'),
-        ('transfer', 'Virement'),
-        ('other', 'Autre')
-    )
+    def __unicode__(self):
+        return self.name
+
+    class Meta:
+        verbose_name = 'moyen de paiment'
+        verbose_name_plural = 'moyens de paiment'
+
+
+class Payment(models.Model):
 
-    payment_mean = models.CharField(max_length=100, null=True,
-                                    default='transfer',
-                                    choices=PAYMENT_MEAN_CHOICES,
-                                    verbose_name='moyen de paiement')
+    # PAYMENT_MEAN_CHOICES = (
+    #     ('cash', 'Espèces'),
+    #     ('check', 'Chèque'),
+    #     ('transfer', 'Virement'),
+    #     ('other', 'Autre')
+    # )
+
+    # payment_mean = models.CharField(max_length=100, null=True,
+    #                                 default='transfer',
+    #                                 choices=PAYMENT_MEAN_CHOICES,
+    #                                 verbose_name='moyen de paiement')
+    method = models.ForeignKey(PaymentMethod, null=True,
+                                         verbose_name='moyen de paiment')
     amount = models.DecimalField(max_digits=5, decimal_places=2, null=True,
                                  verbose_name='montant')
     date = models.DateField(default=datetime.date.today)