#69 test(ReconciliationJar): fix tests.
This commit is contained in:
		| @@ -51,14 +51,14 @@ class CustomerView(viewsets.ModelViewSet): | ||||
|     queryset = Customer.objects.all() | ||||
|     serializer_class = CustomerSerializer | ||||
|  | ||||
|  | ||||
| class ReconciliateJarView(APIView): | ||||
|     def post(self, request): | ||||
|         data = request.data | ||||
|         cash_purchases_id = json.loads(data.get('cash_purchases')) | ||||
|         cash_purchases_id = data.get('cash_purchases') | ||||
|         serializer = ReconciliationJarSerializer(data=data) | ||||
|         if serializer.is_valid(): | ||||
|             purchases = Sale.objects.filter(pk__in=cash_purchases_id) | ||||
|  | ||||
|             total_cash_purchases = sum(p.get_total() for p in purchases) | ||||
|             if total_cash_purchases != Decimal(data.get('total_cash_purchases')): | ||||
|                 return Response( | ||||
|   | ||||
| @@ -100,7 +100,6 @@ | ||||
|         selectedPurchaseId: null, | ||||
|         selectedTab: 'CASH', | ||||
|         reconciliation: { | ||||
|           csrfmiddlewaretoken: null, | ||||
|           date_time: '', | ||||
|           total_cash_purchases: 0, | ||||
|           cash_taken: 0, | ||||
| @@ -157,13 +156,6 @@ | ||||
|         this.selectedPurchaseId = id; | ||||
|         this.$refs.summaryModal.dialog = true; | ||||
|       }, | ||||
|       getCsrfToken() { | ||||
|         const cookies = document.cookie.split(';'); | ||||
|         const csrfToken = cookies.find(cookie => cookie.startsWith('csrftoken=')); | ||||
|         if (csrfToken) { | ||||
|           this.reconciliation.csrfmiddlewaretoken = csrfToken.split('=')[1]; | ||||
|         } | ||||
|       }, | ||||
|       fetchPurchases() { | ||||
|         const endpoint = '/don_confiao/purchases/for_reconciliation'; | ||||
|         fetch(endpoint) | ||||
| @@ -172,7 +164,6 @@ | ||||
|             this.summary.purchases = data; | ||||
|             this.reconciliation.cash_purchases = this.summary.purchases['CASH'].map(purchase => purchase.id); | ||||
|             this.reconciliation.total_cash_purchases = this.totalByMethod('CASH'); | ||||
|             this.getCsrfToken(); | ||||
|           }) | ||||
|           .catch(error => { | ||||
|             console.error(error); | ||||
|   | ||||
| @@ -114,22 +114,23 @@ class TestJarReconcliation(TestCase): | ||||
|             reconciliation.save() | ||||
|  | ||||
|     def test_fail_create_reconciliation_with_wrong_total_purchases_purchases(self): | ||||
|         url = '/don_confiao/reconciliation_jar/create' | ||||
|         url = '/don_confiao/reconciliate_jar' | ||||
|         total_purchases = (11 * 72500) + (27 * 72500) | ||||
|         bad_total_purchases = total_purchases + 2 | ||||
|         data = { | ||||
|             'date_time': '2024-12-02T21:07', | ||||
|             'cashman': 'carlos', | ||||
|             'reconcilier': 'carlos', | ||||
|             'total_cash_purchases': bad_total_purchases, | ||||
|             'cash_taken': total_purchases, | ||||
|             'cash_discrepancy': 0, | ||||
|             'purchases': json.dumps([#machete por error al codificar el json en el request | ||||
|             'cash_purchases': [ | ||||
|                 self.purchase.id, | ||||
|                 self.purchase2.id, | ||||
|                 self.purchase.id, | ||||
|             ]), | ||||
|             ], | ||||
|         } | ||||
|         response = self.client.post(url, data, format='json') | ||||
|         response = self.client.post(url, data=json.dumps(data).encode('utf-8'), | ||||
|                                     content_type='application/json') | ||||
|         rawContent = response.content.decode('utf-8') | ||||
|         content = json.loads(rawContent) | ||||
|  | ||||
| @@ -146,13 +147,15 @@ class TestJarReconcliation(TestCase): | ||||
|             'total_cash_purchases': total_purchases, | ||||
|             'cash_taken': total_purchases, | ||||
|             'cash_discrepancy': 0, | ||||
|             'cash_purchases': json.dumps([#machete por error al codificar el json en el request | ||||
|             'cash_purchases': [ | ||||
|                 self.purchase.id, | ||||
|                 self.purchase2.id, | ||||
|                 self.purchase.id, | ||||
|             ]), | ||||
|             ], | ||||
|         } | ||||
|         response = self.client.post(url, data, format='json') | ||||
|         response = self.client.post(url, data=json.dumps(data).encode('utf-8'), | ||||
|                                     content_type='application/json') | ||||
|  | ||||
|         rawContent = response.content.decode('utf-8') | ||||
|         content = json.loads(rawContent) | ||||
|  | ||||
|   | ||||
| @@ -27,7 +27,6 @@ urlpatterns = [ | ||||
|     path("resumen_compra_json/<int:id>", views.purchase_json_summary, name="purchase_json_summary"), | ||||
|     path("payment_methods/all/select_format", views.payment_methods_to_select, name="payment_methods_to_select"), | ||||
|     path('purchases/for_reconciliation', views.sales_for_reconciliation, name='sales_for_reconciliation'), | ||||
|     path('reconciliation_jar/create', views.reconciliate_jar, name='reconciliate_jar'), | ||||
|     path('reconciliate_jar', api_views.ReconciliateJarView.as_view()), | ||||
|     path('api/', include(router.urls)), | ||||
| ] | ||||
|   | ||||
| @@ -2,7 +2,6 @@ from django.shortcuts import render | ||||
| from django.http import HttpResponse, HttpResponseRedirect, JsonResponse | ||||
| from django.views.generic import ListView | ||||
| from django.db import transaction | ||||
| from django.middleware.csrf import get_token | ||||
|  | ||||
| from .models import ( | ||||
|     Sale, SaleLine, Product, Customer, ProductCategory, Payment, PaymentMethods, ReconciliationJar) | ||||
| @@ -17,7 +16,6 @@ import csv | ||||
| import io | ||||
| import json | ||||
| from decimal import Decimal | ||||
| from datetime import datetime | ||||
|  | ||||
|  | ||||
| class DecimalEncoder(json.JSONEncoder): | ||||
| @@ -96,6 +94,7 @@ def import_products(request): | ||||
|         {'form': form} | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def import_customers(request): | ||||
|     if request.method == "POST": | ||||
|         form = ImportCustomersForm(request.POST, request.FILES) | ||||
| @@ -110,34 +109,6 @@ def import_customers(request): | ||||
|         {'form': form} | ||||
|     ) | ||||
|  | ||||
| def reconciliate_jar(request): | ||||
|     date_format = '%Y-%m-%dT%H:%M' | ||||
|     if request.method == 'POST': | ||||
|         content = request.POST.dict() | ||||
|         content['purchases'] = json.loads(content.get('purchases'))#machete por error al codificar el json en el test | ||||
|         reconciliation = ReconciliationJar() | ||||
|         reconciliation.date_time = content.get('date_time') | ||||
|         reconciliation.cashman = content.get('cashman') | ||||
|         reconciliation.total_cash_purchases = float(content.get('total_cash_purchases')) | ||||
|         reconciliation.cash_taken = float(content.get('cash_taken')) | ||||
|         reconciliation.cash_discrepancy = float(content.get('cash_discrepancy')) | ||||
|         purchases = Sale.objects.filter(pk__in=content.get('purchases')) | ||||
|         if reconciliation.total_cash_purchases != sum(p.get_total() for p in purchases): | ||||
|             return JsonResponse( | ||||
|                 {'error': 'total_cash_purchases not equal to sum of all purchases.'}, | ||||
|                 status=400 | ||||
|             ) | ||||
|         reconciliation.clean() | ||||
|         reconciliation.save() | ||||
|         for purchase in purchases: | ||||
|             purchase.reconciliation = reconciliation | ||||
|             purchase.clean() | ||||
|             purchase.save() | ||||
|         return JsonResponse( | ||||
|             {'id': reconciliation.id}, | ||||
|             safe=False | ||||
|         ) | ||||
|  | ||||
|  | ||||
| def reconciliations(request): | ||||
|     return HttpResponse('<h1>Reconciliaciones</h1>') | ||||
| @@ -205,12 +176,7 @@ def sales_for_reconciliation(request): | ||||
|             }, | ||||
|             'total': sale.get_total(), | ||||
|         }) | ||||
|     response = JsonResponse(grouped_sales, safe=False) | ||||
|     csrf_token = get_token(request) | ||||
|     response['X-CSRFToken'] = csrf_token | ||||
|     response['Access-Control-Allow-Headers'] = 'X-CSRFToken' | ||||
|     response.set_cookie('csrftoken', csrf_token) | ||||
|     return response | ||||
|     return JsonResponse(grouped_sales, safe=False) | ||||
|  | ||||
| def _mask_phone(phone): | ||||
|     digits = str(phone)[-3:] if phone else " " * 3 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user