#84 refactor(SalesForReconciliation): move to apiview.
This commit is contained in:
parent
5dff7565f4
commit
8b7c2efcb3
@ -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
|
||||
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer
|
||||
|
||||
from decimal import Decimal
|
||||
import json
|
||||
@ -106,3 +106,17 @@ class PaymentMethodView(APIView):
|
||||
def get(self, request):
|
||||
serializer = PaymentMethodSerializer(PaymentMethods.choices, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class SalesForReconciliationView(APIView):
|
||||
def get(self, request):
|
||||
sales = Sale.objects.filter(reconciliation=None)
|
||||
grouped_sales = {}
|
||||
|
||||
for sale in sales:
|
||||
if sale.payment_method not in grouped_sales.keys():
|
||||
grouped_sales[sale.payment_method] = []
|
||||
serializer = SaleForRenconciliationSerializer(sale)
|
||||
grouped_sales[sale.payment_method].append(serializer.data)
|
||||
|
||||
return Response(grouped_sales)
|
||||
|
@ -48,3 +48,19 @@ class PaymentMethodSerializer(serializers.Serializer):
|
||||
'text': instance[1],
|
||||
'value': instance[0],
|
||||
}
|
||||
|
||||
class SaleForRenconciliationSerializer(serializers.Serializer):
|
||||
id = serializers.IntegerField()
|
||||
date = serializers.DateTimeField()
|
||||
payment_method = serializers.CharField()
|
||||
customer = serializers.SerializerMethodField()
|
||||
total = serializers.SerializerMethodField()
|
||||
|
||||
def get_customer(self, sale):
|
||||
return {
|
||||
'id': sale.customer.id,
|
||||
'name': sale.customer.name,
|
||||
}
|
||||
|
||||
def get_total(self, sale):
|
||||
return sale.get_total()
|
||||
|
@ -26,7 +26,7 @@ urlpatterns = [
|
||||
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("payment_methods/all/select_format", api_views.PaymentMethodView.as_view(), name="payment_methods_to_select"),
|
||||
path('purchases/for_reconciliation', views.sales_for_reconciliation, name='sales_for_reconciliation'),
|
||||
path('purchases/for_reconciliation', api_views.SalesForReconciliationView.as_view(), name='sales_for_reconciliation'),
|
||||
path('reconciliate_jar', api_views.ReconciliateJarView.as_view()),
|
||||
path('api/', include(router.urls)),
|
||||
]
|
||||
|
@ -152,24 +152,6 @@ def purchase_json_summary(request, id):
|
||||
return JsonResponse(to_response, safe=False)
|
||||
|
||||
|
||||
def sales_for_reconciliation(request):
|
||||
sales = Sale.objects.filter(reconciliation=None)
|
||||
grouped_sales = {}
|
||||
for sale in sales:
|
||||
if sale.payment_method not in grouped_sales.keys():
|
||||
grouped_sales[sale.payment_method] = []
|
||||
grouped_sales[sale.payment_method].append({
|
||||
'id': sale.id,
|
||||
'date': sale.date,
|
||||
'payment_method': sale.payment_method,
|
||||
'customer': {
|
||||
'id': sale.customer.id,
|
||||
'name': sale.customer.name,
|
||||
},
|
||||
'total': sale.get_total(),
|
||||
})
|
||||
return JsonResponse(grouped_sales, safe=False)
|
||||
|
||||
def _mask_phone(phone):
|
||||
digits = str(phone)[-3:] if phone else " " * 3
|
||||
return "X" * 7 + digits
|
||||
|
Loading…
Reference in New Issue
Block a user