fix: ReconciliationJar.
This commit is contained in:
		@@ -2,6 +2,7 @@ from django.db import models
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
from django.core.exceptions import ValidationError
 | 
			
		||||
 | 
			
		||||
from decimal import Decimal
 | 
			
		||||
 | 
			
		||||
class Sale(models.Model):
 | 
			
		||||
 | 
			
		||||
@@ -82,16 +83,27 @@ class ReconciliationJar(models.Model):
 | 
			
		||||
    cash_taken = models.DecimalField(max_digits=9, decimal_places=2)
 | 
			
		||||
    cash_discrepancy = models.DecimalField(max_digits=9, decimal_places=2)
 | 
			
		||||
 | 
			
		||||
    def manual_clean(self):
 | 
			
		||||
        payments_amount = sum([p.amount for p in self.payment_set.all()])
 | 
			
		||||
        reconciliation_ammount = sum([
 | 
			
		||||
    def clean(self):
 | 
			
		||||
        if not self.is_valid:
 | 
			
		||||
            payments = Payment.get_reconciliation_jar_summary().payments
 | 
			
		||||
        else:
 | 
			
		||||
            payments = self.payment_set.all()
 | 
			
		||||
 | 
			
		||||
        payments_amount = Decimal(sum([p.amount for p in payments]))
 | 
			
		||||
        reconciliation_ammount = Decimal(sum([
 | 
			
		||||
            self.cash_taken,
 | 
			
		||||
            self.cash_discrepancy,
 | 
			
		||||
        ])
 | 
			
		||||
        if reconciliation_ammount != payments_amount:
 | 
			
		||||
        ]))
 | 
			
		||||
 | 
			
		||||
        equal_ammounts = reconciliation_ammount.compare(payments_amount) == Decimal('0')
 | 
			
		||||
        if not equal_ammounts:
 | 
			
		||||
            raise ValidationError(
 | 
			
		||||
                {"cash_take": _("The taken ammount has discrepancy.")}
 | 
			
		||||
                {"cash_taken": _("The taken ammount has discrepancy.")}
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    def add_payments(self, payments):
 | 
			
		||||
        for payment in payments:
 | 
			
		||||
            self.payment_set.add(payment)
 | 
			
		||||
        self.is_valid = True
 | 
			
		||||
 | 
			
		||||
class Payment(models.Model):
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
from django.test import Client, TestCase
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
from django.core.exceptions import ValidationError
 | 
			
		||||
from .models import Payment, ReconciliationJar
 | 
			
		||||
 | 
			
		||||
@@ -50,10 +50,10 @@ class TestBilling(TestCase):
 | 
			
		||||
        reconciliation_jar.cash_discrepancy = 0
 | 
			
		||||
        reconciliation_jar.save()
 | 
			
		||||
 | 
			
		||||
        for payment in jar_summary.payments:
 | 
			
		||||
            reconciliation_jar.payment_set.add(payment)
 | 
			
		||||
        reconciliation_jar.add_payments(jar_summary.payments)
 | 
			
		||||
 | 
			
		||||
        with self.assertRaises(ValidationError):
 | 
			
		||||
            reconciliation_jar.manual_clean()
 | 
			
		||||
            reconciliation_jar.clean()
 | 
			
		||||
 | 
			
		||||
    def test_validate_reconciliation_jar_with_cash_float(self):
 | 
			
		||||
        cash_payment1, cash_payment2 = self._create_two_cash_payments()
 | 
			
		||||
@@ -63,16 +63,13 @@ class TestBilling(TestCase):
 | 
			
		||||
        reconciliation_jar.date_time = '2024-07-13 13:02:00'
 | 
			
		||||
        reconciliation_jar.description = "test reconcialiation jar"
 | 
			
		||||
        reconciliation_jar.reconcilier = 'Jorge'
 | 
			
		||||
        reconciliation_jar.cash_float = 10000
 | 
			
		||||
        reconciliation_jar.cash_taken = jar_summary.total
 | 
			
		||||
        reconciliation_jar.cash_discrepancy = 0
 | 
			
		||||
        reconciliation_jar.save()
 | 
			
		||||
 | 
			
		||||
        for payment in jar_summary.payments:
 | 
			
		||||
            reconciliation_jar.payment_set.add(payment)
 | 
			
		||||
        reconciliation_jar.add_payments(jar_summary.payments)
 | 
			
		||||
        reconciliation_jar.clean()
 | 
			
		||||
        reconciliation_jar.save()
 | 
			
		||||
        reconciliation_jar.manual_clean()
 | 
			
		||||
        self.assertTrue(reconciliation_jar.is_valid)
 | 
			
		||||
 | 
			
		||||
    def _create_two_cash_payments(self):
 | 
			
		||||
 
 | 
			
		||||
@@ -75,17 +75,12 @@ def reconciliate_jar(request):
 | 
			
		||||
    summary = Payment.get_reconciliation_jar_summary()
 | 
			
		||||
    if request.method == 'POST':
 | 
			
		||||
        form = ReconciliationJarForm(request.POST)
 | 
			
		||||
        reconciliation = form.save()
 | 
			
		||||
        try:
 | 
			
		||||
            if form.is_valid():
 | 
			
		||||
                reconciliation.payment_set.set(summary.payments)
 | 
			
		||||
                reconciliation.manual_clean()
 | 
			
		||||
                form.save()
 | 
			
		||||
                return HttpResponseRedirect('cuadres')
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            reconciliation.payment_set.set({})
 | 
			
		||||
            reconciliation.delete()
 | 
			
		||||
            raise e
 | 
			
		||||
        if form.is_valid():
 | 
			
		||||
            reconciliation = form.save()
 | 
			
		||||
            reconciliation.add_payments(summary.payments)
 | 
			
		||||
            reconciliation.clean()
 | 
			
		||||
            reconciliation.save()
 | 
			
		||||
            return HttpResponseRedirect('cuadres')
 | 
			
		||||
    else:
 | 
			
		||||
        form = ReconciliationJarForm()
 | 
			
		||||
    return render(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user