diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 5e48b3d..71772d1 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _ from django.core.exceptions import ValidationError from decimal import Decimal +from datetime import datetime class Customer(models.Model): @@ -118,7 +119,7 @@ class ReconciliationJar(models.Model): self.payment_set.add(payment) self.is_valid = True - + class Payment(models.Model): date_time = models.DateTimeField() type_payment = models.CharField( @@ -144,3 +145,23 @@ class Payment(models.Model): reconciliation_jar=None ) ) + + @classmethod + def total_payment_from_sale(cls, payment_method, sale): + payment = cls() + payment.date_time = datetime.today() + payment.type_payment = payment_method + payment.amount = sale.get_total() + payment.clean() + payment.save() + + payment_sale = PaymentSale() + payment_sale.payment = payment + payment_sale.sale = sale + payment_sale.clean() + payment_sale.save() + + +class PaymentSale(models.Model): + payment = models.ForeignKey(Payment, on_delete=models.CASCADE) + sale = models.ForeignKey(Sale, on_delete=models.CASCADE) diff --git a/tienda_ilusion/don_confiao/tests/test_purchase_with_payment.py b/tienda_ilusion/don_confiao/tests/test_purchase_with_payment.py index bee9de2..962aaa2 100644 --- a/tienda_ilusion/don_confiao/tests/test_purchase_with_payment.py +++ b/tienda_ilusion/don_confiao/tests/test_purchase_with_payment.py @@ -19,7 +19,7 @@ class TestPurchaseWithPayment(TestCase): quantity = 2 unit_price = 2500 total = 5000 - response = self.client.post( + self.client.post( '/don_confiao/comprar', { "customer": str(self.customer.id), @@ -42,6 +42,10 @@ class TestPurchaseWithPayment(TestCase): "payment_method": "CASH", } ) + purchases = Sale.objects.all() self.assertEqual(1, len(purchases)) payments = Payment.objects.all() + self.assertEqual(1, len(payments)) + self.assertEqual(total, payments[0].amount) + self.assertEqual('CASH', payments[0].type_payment) diff --git a/tienda_ilusion/don_confiao/views.py b/tienda_ilusion/don_confiao/views.py index 7c837bc..63169c4 100644 --- a/tienda_ilusion/don_confiao/views.py +++ b/tienda_ilusion/don_confiao/views.py @@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.views.generic import ListView from django.db import transaction -from .models import Sale, Product, ProductCategory, Payment +from .models import Sale, Product, ProductCategory, Payment, PaymentMethods from .forms import ImportProductsForm, PurchaseForm, SaleLineFormSet, ReconciliationJarForm, PurchaseSummaryForm import csv @@ -13,7 +13,6 @@ import io def index(request): return render(request, 'don_confiao/index.html') - def buy(request): if request.method == "POST": sale_form = PurchaseForm(request.POST) @@ -24,11 +23,18 @@ def buy(request): line_formset.is_valid(), sale_summary_form.is_valid() ]) + payment_method = request.POST.get('payment_method') + valid_payment_methods = [PaymentMethods.CASH] + valid_payment_method = payment_method in valid_payment_methods if forms_are_valid: with transaction.atomic(): sale = sale_form.save() line_formset.instance = sale line_formset.save() + Payment.total_payment_from_sale( + payment_method, + sale + ) return HttpResponseRedirect("compras") else: sale_form = PurchaseForm()