fix: ReconciliationJar.

This commit is contained in:
2024-07-27 12:37:31 -05:00
parent a3c8189d8c
commit 5879296132
3 changed files with 29 additions and 25 deletions

View File

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