refactoring(Purchase): simplify payment_method #47

This commit is contained in:
Mono Mono 2024-11-09 13:49:24 -05:00
parent d5b7c99b79
commit b7c4cf5d44
4 changed files with 49 additions and 6 deletions

View File

@ -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),
),
]

View File

@ -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)

View File

@ -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

View File

@ -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",