#84 refactor(SaleSummary): move to apiview.
This commit is contained in:
		| @@ -4,7 +4,7 @@ from rest_framework.status import HTTP_400_BAD_REQUEST | ||||
| from rest_framework.views import APIView | ||||
|  | ||||
| from .models import Sale, SaleLine, Customer, Product, ReconciliationJar, PaymentMethods | ||||
| from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer | ||||
| from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer, SaleSummarySerializer | ||||
|  | ||||
| from decimal import Decimal | ||||
| import json | ||||
| @@ -120,3 +120,9 @@ class SalesForReconciliationView(APIView): | ||||
|             grouped_sales[sale.payment_method].append(serializer.data) | ||||
|  | ||||
|         return Response(grouped_sales) | ||||
|  | ||||
| class SaleSummary(APIView): | ||||
|     def get(self, request, id): | ||||
|         sale = Sale.objects.get(pk=id) | ||||
|         serializer = SaleSummarySerializer(sale) | ||||
|         return Response(serializer.data) | ||||
|   | ||||
| @@ -64,3 +64,32 @@ class SaleForRenconciliationSerializer(serializers.Serializer): | ||||
|  | ||||
|     def get_total(self, sale): | ||||
|         return sale.get_total() | ||||
|  | ||||
|  | ||||
| class ListCustomerSerializer(serializers.ModelSerializer): | ||||
|     class Meta: | ||||
|         model = Customer | ||||
|         fields = ['id', 'name'] | ||||
|  | ||||
|  | ||||
| class ListProductSerializer(serializers.ModelSerializer): | ||||
|     class Meta: | ||||
|         model = Product | ||||
|         fields = ['id', 'name'] | ||||
|  | ||||
|  | ||||
| class SummarySaleLineSerializer(serializers.ModelSerializer): | ||||
|     product = ListProductSerializer() | ||||
|  | ||||
|     class Meta: | ||||
|         model = SaleLine | ||||
|         fields = ['product', 'quantity', 'unit_price', 'description'] | ||||
|  | ||||
|  | ||||
| class SaleSummarySerializer(serializers.ModelSerializer): | ||||
|     customer = ListCustomerSerializer() | ||||
|     saleline_set = SummarySaleLineSerializer(many=True) | ||||
|  | ||||
|     class Meta: | ||||
|         model = Sale | ||||
|         fields = ['id', 'date', 'customer', 'payment_method', 'saleline_set'] | ||||
|   | ||||
| @@ -24,7 +24,7 @@ urlpatterns = [ | ||||
|          views.exportar_ventas_para_tryton, | ||||
|          name="exportar_ventas_para_tryton"), | ||||
|     path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"), | ||||
|     path("resumen_compra_json/<int:id>", views.purchase_json_summary, name="purchase_json_summary"), | ||||
|     path("resumen_compra_json/<int:id>", api_views.SaleSummary.as_view(), name="purchase_json_summary"), | ||||
|     path("payment_methods/all/select_format", api_views.PaymentMethodView.as_view(), name="payment_methods_to_select"), | ||||
|     path('purchases/for_reconciliation', api_views.SalesForReconciliationView.as_view(), name='sales_for_reconciliation'), | ||||
|     path('reconciliate_jar', api_views.ReconciliateJarView.as_view()), | ||||
|   | ||||
| @@ -125,38 +125,6 @@ def purchase_summary(request, id): | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def purchase_json_summary(request, id): | ||||
|     purchase = Sale.objects.get(pk=id) | ||||
|     lines = [] | ||||
|     for line in purchase.saleline_set.all(): | ||||
|         lines.append({ | ||||
|             'product': { | ||||
|                 'id': line.product.id, | ||||
|                 'name': line.product.name, | ||||
|             }, | ||||
|             'quantity': line.quantity, | ||||
|             'unit_price': line.unit_price, | ||||
|             'description': line.description, | ||||
|         }) | ||||
|     to_response = { | ||||
|         'id': purchase.id, | ||||
|         'date': purchase.date, | ||||
|         'customer': { | ||||
|             'id': purchase.customer.id, | ||||
|             'name': purchase.customer.name, | ||||
|             # 'phone': _mask_phone(purchase.customer.phone) | ||||
|         }, | ||||
|         'payment_method': purchase.payment_method, | ||||
|         'set_lines': lines, | ||||
|     } | ||||
|     return JsonResponse(to_response, safe=False) | ||||
|  | ||||
|  | ||||
| def _mask_phone(phone): | ||||
|     digits = str(phone)[-3:] if phone else " " * 3 | ||||
|     return "X" * 7 + digits | ||||
|  | ||||
|  | ||||
| def _categories_from_csv_string(categories_string, separator="&"): | ||||
|     categories = categories_string.split(separator) | ||||
|     clean_categories = [c.strip() for c in categories] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user