feat: create a correct reconciliation jar.
This commit is contained in:
		| @@ -82,7 +82,7 @@ class ReconciliationJar(models.Model): | |||||||
|     cash_taken = models.DecimalField(max_digits=9, decimal_places=2) |     cash_taken = models.DecimalField(max_digits=9, decimal_places=2) | ||||||
|     cash_discrepancy = models.DecimalField(max_digits=9, decimal_places=2) |     cash_discrepancy = models.DecimalField(max_digits=9, decimal_places=2) | ||||||
|  |  | ||||||
|     def clean(self): |     def manual_clean(self): | ||||||
|         payments_amount = sum([p.amount for p in self.payment_set.all()]) |         payments_amount = sum([p.amount for p in self.payment_set.all()]) | ||||||
|         reconciliation_ammount = sum([ |         reconciliation_ammount = sum([ | ||||||
|             self.cash_taken, |             self.cash_taken, | ||||||
|   | |||||||
| @@ -53,7 +53,7 @@ class TestBilling(TestCase): | |||||||
|         for payment in jar_summary.payments: |         for payment in jar_summary.payments: | ||||||
|             reconciliation_jar.payment_set.add(payment) |             reconciliation_jar.payment_set.add(payment) | ||||||
|         with self.assertRaises(ValidationError): |         with self.assertRaises(ValidationError): | ||||||
|             reconciliation_jar.clean() |             reconciliation_jar.manual_clean() | ||||||
|  |  | ||||||
|     def test_validate_reconciliation_jar_with_cash_float(self): |     def test_validate_reconciliation_jar_with_cash_float(self): | ||||||
|         cash_payment1, cash_payment2 = self._create_two_cash_payments() |         cash_payment1, cash_payment2 = self._create_two_cash_payments() | ||||||
| @@ -72,6 +72,7 @@ class TestBilling(TestCase): | |||||||
|             reconciliation_jar.payment_set.add(payment) |             reconciliation_jar.payment_set.add(payment) | ||||||
|         reconciliation_jar.clean() |         reconciliation_jar.clean() | ||||||
|         reconciliation_jar.save() |         reconciliation_jar.save() | ||||||
|  |         reconciliation_jar.manual_clean() | ||||||
|         self.assertTrue(reconciliation_jar.is_valid) |         self.assertTrue(reconciliation_jar.is_valid) | ||||||
|  |  | ||||||
|     def _create_two_cash_payments(self): |     def _create_two_cash_payments(self): | ||||||
|   | |||||||
| @@ -14,10 +14,23 @@ class TestReconciliationJarClient(TestCase): | |||||||
|         self._generate_two_cash_payments() |         self._generate_two_cash_payments() | ||||||
|         response = self.client.get("/don_confiao/cuadrar_tarro") |         response = self.client.get("/don_confiao/cuadrar_tarro") | ||||||
|         self.assertEqual(response.status_code, 200) |         self.assertEqual(response.status_code, 200) | ||||||
|         # raise Exception(response.content.decode('utf-8')) |  | ||||||
|         self.assertEqual(response.context["summary"].total, 160000) |         self.assertEqual(response.context["summary"].total, 160000) | ||||||
|         self.assertIn('160000', response.content.decode('utf-8')) |         self.assertIn('160000', response.content.decode('utf-8')) | ||||||
|  |  | ||||||
|  |     def test_create_reconciliation_jar(self): | ||||||
|  |         self._generate_two_cash_payments() | ||||||
|  |         response = self.client.post( | ||||||
|  |             "/don_confiao/cuadrar_tarro", | ||||||
|  |             { | ||||||
|  |                 "date_time": "2024-07-20T00:00", | ||||||
|  |                 "description": "Cuadre de prueba", | ||||||
|  |                 "reconcilier": "Jorge", | ||||||
|  |                 "cash_taken": "100000", | ||||||
|  |                 "cash_discrepancy": "60000", | ||||||
|  |             } | ||||||
|  |         ) | ||||||
|  |         self.assertRedirects(response, '/don_confiao/cuadres') | ||||||
|  |  | ||||||
|     def _generate_two_cash_payments(self): |     def _generate_two_cash_payments(self): | ||||||
|         cash_payment1 = Payment() |         cash_payment1 = Payment() | ||||||
|         cash_payment1.date_time = '2024-07-07 12:00:00' |         cash_payment1.date_time = '2024-07-07 12:00:00' | ||||||
| @@ -25,7 +38,6 @@ class TestReconciliationJarClient(TestCase): | |||||||
|         cash_payment1.amount = 130000 |         cash_payment1.amount = 130000 | ||||||
|         cash_payment1.description = 'Saldo en compra' |         cash_payment1.description = 'Saldo en compra' | ||||||
|         cash_payment1.save() |         cash_payment1.save() | ||||||
|         # raise Exception (cash_payment1.id) |  | ||||||
|  |  | ||||||
|         cash_payment2 = Payment() |         cash_payment2 = Payment() | ||||||
|         cash_payment2.date_time = '2024-07-07 13:05:00' |         cash_payment2.date_time = '2024-07-07 13:05:00' | ||||||
|   | |||||||
| @@ -10,4 +10,5 @@ urlpatterns = [ | |||||||
|     path("productos", views.products, name="products"), |     path("productos", views.products, name="products"), | ||||||
|     path("importar_productos", views.import_products, name="import_products"), |     path("importar_productos", views.import_products, name="import_products"), | ||||||
|     path("cuadrar_tarro", views.reconciliate_jar, name="reconciliate_jar"), |     path("cuadrar_tarro", views.reconciliate_jar, name="reconciliate_jar"), | ||||||
|  |     path("cuadres", views.reconciliate_jar, name="reconciliations"), | ||||||
| ] | ] | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| from django.shortcuts import render | from django.shortcuts import render | ||||||
| from django.http import HttpResponse, HttpResponseRedirect, JsonResponse | from django.http import HttpResponse, HttpResponseRedirect, JsonResponse | ||||||
| from django.template import loader | from django.template import loader | ||||||
|  | from django.core.exceptions import ValidationError | ||||||
|  |  | ||||||
| from .models import Sale, Product, ProductCategory, Payment | from .models import Sale, Product, ProductCategory, Payment | ||||||
| from .forms import ImportProductsForm, PurchaseForm, LineaFormSet, ReconciliationJarForm | from .forms import ImportProductsForm, PurchaseForm, LineaFormSet, ReconciliationJarForm | ||||||
| @@ -71,17 +72,31 @@ def import_products(request): | |||||||
|  |  | ||||||
|  |  | ||||||
| def reconciliate_jar(request): | def reconciliate_jar(request): | ||||||
|     if request.method == 'POST': |  | ||||||
|         return HttpResponseRedirect("cuadres") |  | ||||||
|  |  | ||||||
|     form = ReconciliationJarForm() |  | ||||||
|     summary = Payment.get_reconciliation_jar_summary() |     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 | ||||||
|  |     else: | ||||||
|  |         form = ReconciliationJarForm() | ||||||
|     return render( |     return render( | ||||||
|         request, |         request, | ||||||
|         "don_confiao/reconciliate_jar.html", |         "don_confiao/reconciliate_jar.html", | ||||||
|         {'summary': summary, 'form': form} |         {'summary': summary, 'form': form} | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  | def reconciliations(request): | ||||||
|  |     return HttpResponse('<h1>Reconciliaciones</h1>') | ||||||
|  |  | ||||||
|  |  | ||||||
| def _categories_from_csv_string(categories_string, separator="&"): | def _categories_from_csv_string(categories_string, separator="&"): | ||||||
|     categories = categories_string.split(separator) |     categories = categories_string.split(separator) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user