feat(Payments): Generate cash payment with purchase.
This commit is contained in:
		@@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _
 | 
				
			|||||||
from django.core.exceptions import ValidationError
 | 
					from django.core.exceptions import ValidationError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from decimal import Decimal
 | 
					from decimal import Decimal
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Customer(models.Model):
 | 
					class Customer(models.Model):
 | 
				
			||||||
@@ -144,3 +145,23 @@ class Payment(models.Model):
 | 
				
			|||||||
                reconciliation_jar=None
 | 
					                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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,7 +19,7 @@ class TestPurchaseWithPayment(TestCase):
 | 
				
			|||||||
        quantity = 2
 | 
					        quantity = 2
 | 
				
			||||||
        unit_price = 2500
 | 
					        unit_price = 2500
 | 
				
			||||||
        total = 5000
 | 
					        total = 5000
 | 
				
			||||||
        response = self.client.post(
 | 
					        self.client.post(
 | 
				
			||||||
            '/don_confiao/comprar',
 | 
					            '/don_confiao/comprar',
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                "customer": str(self.customer.id),
 | 
					                "customer": str(self.customer.id),
 | 
				
			||||||
@@ -42,6 +42,10 @@ class TestPurchaseWithPayment(TestCase):
 | 
				
			|||||||
                "payment_method": "CASH",
 | 
					                "payment_method": "CASH",
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        purchases = Sale.objects.all()
 | 
					        purchases = Sale.objects.all()
 | 
				
			||||||
        self.assertEqual(1, len(purchases))
 | 
					        self.assertEqual(1, len(purchases))
 | 
				
			||||||
        payments = Payment.objects.all()
 | 
					        payments = Payment.objects.all()
 | 
				
			||||||
 | 
					        self.assertEqual(1, len(payments))
 | 
				
			||||||
 | 
					        self.assertEqual(total, payments[0].amount)
 | 
				
			||||||
 | 
					        self.assertEqual('CASH', payments[0].type_payment)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
 | 
				
			|||||||
from django.views.generic import ListView
 | 
					from django.views.generic import ListView
 | 
				
			||||||
from django.db import transaction
 | 
					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
 | 
					from .forms import ImportProductsForm, PurchaseForm, SaleLineFormSet, ReconciliationJarForm, PurchaseSummaryForm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
@@ -13,7 +13,6 @@ import io
 | 
				
			|||||||
def index(request):
 | 
					def index(request):
 | 
				
			||||||
    return render(request, 'don_confiao/index.html')
 | 
					    return render(request, 'don_confiao/index.html')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def buy(request):
 | 
					def buy(request):
 | 
				
			||||||
    if request.method == "POST":
 | 
					    if request.method == "POST":
 | 
				
			||||||
        sale_form = PurchaseForm(request.POST)
 | 
					        sale_form = PurchaseForm(request.POST)
 | 
				
			||||||
@@ -24,11 +23,18 @@ def buy(request):
 | 
				
			|||||||
            line_formset.is_valid(),
 | 
					            line_formset.is_valid(),
 | 
				
			||||||
            sale_summary_form.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:
 | 
					        if forms_are_valid:
 | 
				
			||||||
            with transaction.atomic():
 | 
					            with transaction.atomic():
 | 
				
			||||||
                sale = sale_form.save()
 | 
					                sale = sale_form.save()
 | 
				
			||||||
                line_formset.instance = sale
 | 
					                line_formset.instance = sale
 | 
				
			||||||
                line_formset.save()
 | 
					                line_formset.save()
 | 
				
			||||||
 | 
					                Payment.total_payment_from_sale(
 | 
				
			||||||
 | 
					                    payment_method,
 | 
				
			||||||
 | 
					                    sale
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
            return HttpResponseRedirect("compras")
 | 
					            return HttpResponseRedirect("compras")
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        sale_form = PurchaseForm()
 | 
					        sale_form = PurchaseForm()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user