From b7c4cf5d44ebc2090565f094916e0066d3a16208 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 9 Nov 2024 13:49:24 -0500 Subject: [PATCH] refactoring(Purchase): simplify payment_method #47 --- .../migrations/0033_sale_payment_method.py | 18 +++++++++++++++ tienda_ilusion/don_confiao/models.py | 23 ++++++++++++++----- tienda_ilusion/don_confiao/tests/tests.py | 13 +++++++++++ .../don_confiao/tests/tests_purchase_form.py | 1 + 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 tienda_ilusion/don_confiao/migrations/0033_sale_payment_method.py diff --git a/tienda_ilusion/don_confiao/migrations/0033_sale_payment_method.py b/tienda_ilusion/don_confiao/migrations/0033_sale_payment_method.py new file mode 100644 index 0000000..8717add --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0033_sale_payment_method.py @@ -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), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index b82f9dd..54a96f1 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -6,6 +6,12 @@ from decimal import Decimal from datetime import datetime +class PaymentMethods(models.TextChoices): + CASH = 'CASH', _('Cash') + CONFIAR = 'CONFIAR', _('Confiar') + BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia') + + class Customer(models.Model): name = models.CharField(max_length=100, default=None, null=False, blank=False) address = models.CharField(max_length=100, null=True, blank=True) @@ -61,6 +67,13 @@ class Sale(models.Model): date = models.DateField("Date") phone = models.CharField(max_length=13, 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): return f"{self.date} {self.customer}" @@ -69,6 +82,10 @@ class Sale(models.Model): lines = self.saleline_set.all() 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 def sale_header_csv(cls): 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}" -class PaymentMethods(models.TextChoices): - CASH = 'CASH', _('Cash') - CONFIAR = 'CONFIAR', _('Confiar') - BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia') - - class ReconciliationJarSummary(): def __init__(self, payments): self._validate_payments(payments) diff --git a/tienda_ilusion/don_confiao/tests/tests.py b/tienda_ilusion/don_confiao/tests/tests.py index f50839d..672d3b4 100644 --- a/tienda_ilusion/don_confiao/tests/tests.py +++ b/tienda_ilusion/don_confiao/tests/tests.py @@ -1,4 +1,6 @@ from django.test import TestCase +from django.core.exceptions import ValidationError + from ..models import Customer, Product, Sale, SaleLine @@ -24,6 +26,17 @@ class ConfiaoTest(TestCase): 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): sale = Sale() sale.customer = self.customer diff --git a/tienda_ilusion/don_confiao/tests/tests_purchase_form.py b/tienda_ilusion/don_confiao/tests/tests_purchase_form.py index 656810e..7cfd236 100644 --- a/tienda_ilusion/don_confiao/tests/tests_purchase_form.py +++ b/tienda_ilusion/don_confiao/tests/tests_purchase_form.py @@ -19,6 +19,7 @@ class PurchaseFormTest(TestCase): "csrfmiddlewaretoken": _csrf_token, "customer": self.customer.id, "date": "2024-08-03", + "payment_method": "CASH", "phone": "sfasfd", "description": "dasdadad", "saleline_set-TOTAL_FORMS": "1",