refactoring(Purchase): simplify payment_method #47
This commit is contained in:
		@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					# Generated by Django 5.0.6 on 2024-11-09 17:55
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from django.db import migrations, models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('don_confiao', '0032_customer_address'),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    operations = [
 | 
				
			||||||
 | 
					        migrations.AddField(
 | 
				
			||||||
 | 
					            model_name='sale',
 | 
				
			||||||
 | 
					            name='payment_method',
 | 
				
			||||||
 | 
					            field=models.CharField(choices=[('CASH', 'Cash'), ('CONFIAR', 'Confiar'), ('BANCOLOMBIA', 'Bancolombia')], default='CASH', max_length=30),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
@@ -6,6 +6,12 @@ from decimal import Decimal
 | 
				
			|||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class PaymentMethods(models.TextChoices):
 | 
				
			||||||
 | 
					    CASH = 'CASH', _('Cash')
 | 
				
			||||||
 | 
					    CONFIAR = 'CONFIAR', _('Confiar')
 | 
				
			||||||
 | 
					    BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Customer(models.Model):
 | 
					class Customer(models.Model):
 | 
				
			||||||
    name = models.CharField(max_length=100, default=None, null=False, blank=False)
 | 
					    name = models.CharField(max_length=100, default=None, null=False, blank=False)
 | 
				
			||||||
    address = models.CharField(max_length=100, null=True, blank=True)
 | 
					    address = models.CharField(max_length=100, null=True, blank=True)
 | 
				
			||||||
@@ -61,6 +67,13 @@ class Sale(models.Model):
 | 
				
			|||||||
    date = models.DateField("Date")
 | 
					    date = models.DateField("Date")
 | 
				
			||||||
    phone = models.CharField(max_length=13, null=True, blank=True)
 | 
					    phone = models.CharField(max_length=13, null=True, blank=True)
 | 
				
			||||||
    description = models.CharField(max_length=255, null=True, blank=True)
 | 
					    description = models.CharField(max_length=255, null=True, blank=True)
 | 
				
			||||||
 | 
					    payment_method = models.CharField(
 | 
				
			||||||
 | 
					        max_length=30,
 | 
				
			||||||
 | 
					        choices=PaymentMethods.choices,
 | 
				
			||||||
 | 
					        default=PaymentMethods.CASH,
 | 
				
			||||||
 | 
					        blank=False,
 | 
				
			||||||
 | 
					        null=False
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return f"{self.date} {self.customer}"
 | 
					        return f"{self.date} {self.customer}"
 | 
				
			||||||
@@ -69,6 +82,10 @@ class Sale(models.Model):
 | 
				
			|||||||
        lines = self.saleline_set.all()
 | 
					        lines = self.saleline_set.all()
 | 
				
			||||||
        return sum([l.quantity * l.unit_price for l in lines])
 | 
					        return sum([l.quantity * l.unit_price for l in lines])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def clean(self):
 | 
				
			||||||
 | 
					        if self.payment_method not in [choice[0] for choice in PaymentMethods.choices]:
 | 
				
			||||||
 | 
					            raise ValidationError({'payment_method': f"Null or invalid payment method ({self.payment_method}, {PaymentMethods.choices})"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def sale_header_csv(cls):
 | 
					    def sale_header_csv(cls):
 | 
				
			||||||
        sale_header_csv = [field.name for field in cls._meta.fields]
 | 
					        sale_header_csv = [field.name for field in cls._meta.fields]
 | 
				
			||||||
@@ -88,12 +105,6 @@ class SaleLine(models.Model):
 | 
				
			|||||||
        return f"{self.sale} - {self.product}"
 | 
					        return f"{self.sale} - {self.product}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PaymentMethods(models.TextChoices):
 | 
					 | 
				
			||||||
    CASH = 'CASH', _('Cash')
 | 
					 | 
				
			||||||
    CONFIAR = 'CONFIAR', _('Confiar')
 | 
					 | 
				
			||||||
    BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class ReconciliationJarSummary():
 | 
					class ReconciliationJarSummary():
 | 
				
			||||||
    def __init__(self, payments):
 | 
					    def __init__(self, payments):
 | 
				
			||||||
        self._validate_payments(payments)
 | 
					        self._validate_payments(payments)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,6 @@
 | 
				
			|||||||
from django.test import TestCase
 | 
					from django.test import TestCase
 | 
				
			||||||
 | 
					from django.core.exceptions import ValidationError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..models import Customer, Product, Sale, SaleLine
 | 
					from ..models import Customer, Product, Sale, SaleLine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -24,6 +26,17 @@ class ConfiaoTest(TestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.assertIsInstance(sale, Sale)
 | 
					        self.assertIsInstance(sale, Sale)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_can_create_sale_without_payment_method(self):
 | 
				
			||||||
 | 
					        sale = Sale()
 | 
				
			||||||
 | 
					        sale.customer = self.customer
 | 
				
			||||||
 | 
					        sale.date = "2024-06-22"
 | 
				
			||||||
 | 
					        sale.phone = '666666666'
 | 
				
			||||||
 | 
					        sale.description = "Description"
 | 
				
			||||||
 | 
					        sale.payment_method = ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        with self.assertRaises(ValidationError):
 | 
				
			||||||
 | 
					            sale.full_clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_create_sale_line(self):
 | 
					    def test_create_sale_line(self):
 | 
				
			||||||
        sale = Sale()
 | 
					        sale = Sale()
 | 
				
			||||||
        sale.customer = self.customer
 | 
					        sale.customer = self.customer
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ class PurchaseFormTest(TestCase):
 | 
				
			|||||||
            "csrfmiddlewaretoken": _csrf_token,
 | 
					            "csrfmiddlewaretoken": _csrf_token,
 | 
				
			||||||
            "customer": self.customer.id,
 | 
					            "customer": self.customer.id,
 | 
				
			||||||
            "date": "2024-08-03",
 | 
					            "date": "2024-08-03",
 | 
				
			||||||
 | 
					            "payment_method": "CASH",
 | 
				
			||||||
            "phone": "sfasfd",
 | 
					            "phone": "sfasfd",
 | 
				
			||||||
            "description": "dasdadad",
 | 
					            "description": "dasdadad",
 | 
				
			||||||
            "saleline_set-TOTAL_FORMS": "1",
 | 
					            "saleline_set-TOTAL_FORMS": "1",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user