Merge pull request 'refactor_endpoints_to_api_views #84' (#85) from refactor_endpoints_to_api_views_#84 into main
Reviewed-on: OneTeam/don_confiao#85
This commit is contained in:
commit
a4048473ae
@ -3,8 +3,8 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.status import HTTP_400_BAD_REQUEST
|
from rest_framework.status import HTTP_400_BAD_REQUEST
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from .models import Sale, SaleLine, Customer, Product, ReconciliationJar
|
from .models import Sale, SaleLine, Customer, Product, ReconciliationJar, PaymentMethods
|
||||||
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer
|
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer, SaleSummarySerializer
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
import json
|
import json
|
||||||
@ -100,3 +100,29 @@ class ReconciliateJarView(APIView):
|
|||||||
purchase.reconciliation = reconciliation
|
purchase.reconciliation = reconciliation
|
||||||
purchase.clean()
|
purchase.clean()
|
||||||
purchase.save()
|
purchase.save()
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
class SaleSummary(APIView):
|
||||||
|
def get(self, request, id):
|
||||||
|
sale = Sale.objects.get(pk=id)
|
||||||
|
serializer = SaleSummarySerializer(sale)
|
||||||
|
return Response(serializer.data)
|
||||||
|
@ -12,33 +12,25 @@
|
|||||||
</v-toolbar>
|
</v-toolbar>
|
||||||
<v-list>
|
<v-list>
|
||||||
<v-list-item>
|
<v-list-item>
|
||||||
<v-list-item-content>
|
|
||||||
<v-list-item-title>Fecha:</v-list-item-title>
|
<v-list-item-title>Fecha:</v-list-item-title>
|
||||||
<v-list-item-subtitle>{{ purchase.date }}</v-list-item-subtitle>
|
<v-list-item-subtitle>{{ purchase.date }}</v-list-item-subtitle>
|
||||||
</v-list-item-content>
|
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
<v-list-item>
|
<v-list-item>
|
||||||
<v-list-item-content>
|
|
||||||
<v-list-item-title>Cliente:</v-list-item-title>
|
<v-list-item-title>Cliente:</v-list-item-title>
|
||||||
<v-list-item-subtitle v-if="purchase.customer">{{ purchase.customer.name }}</v-list-item-subtitle>
|
<v-list-item-subtitle v-if="purchase.customer">{{ purchase.customer.name }}</v-list-item-subtitle>
|
||||||
</v-list-item-content>
|
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
<v-list-item>
|
<v-list-item>
|
||||||
<v-list-item-content>
|
|
||||||
<v-list-item-title>Pagado en:</v-list-item-title>
|
<v-list-item-title>Pagado en:</v-list-item-title>
|
||||||
<v-list-item-subtitle v-if="purchase.payment_method">{{ purchase.payment_method }}</v-list-item-subtitle>
|
<v-list-item-subtitle v-if="purchase.payment_method">{{ purchase.payment_method }}</v-list-item-subtitle>
|
||||||
</v-list-item-content>
|
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
<v-list-item>
|
<v-list-item>
|
||||||
<v-list-item-content>
|
|
||||||
<v-list-item-title>Total:</v-list-item-title>
|
<v-list-item-title>Total:</v-list-item-title>
|
||||||
<v-list-item-subtitle v-if="purchase.set_lines">{{ currencyFormat(calculateTotal(purchase.set_lines)) }}</v-list-item-subtitle>
|
<v-list-item-subtitle v-if="purchase.lines">{{ currencyFormat(calculateTotal(purchase.lines)) }}</v-list-item-subtitle>
|
||||||
</v-list-item-content>
|
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
</v-list>
|
</v-list>
|
||||||
<v-data-table-virtual
|
<v-data-table-virtual
|
||||||
:headers="headers"
|
:headers="headers"
|
||||||
:items="purchase.set_lines"
|
:items="purchase.lines"
|
||||||
>
|
>
|
||||||
<template v-slot:item.unit_price="{ item }">
|
<template v-slot:item.unit_price="{ item }">
|
||||||
{{ currencyFormat(item.unit_price) }}
|
{{ currencyFormat(item.unit_price) }}
|
||||||
|
@ -38,3 +38,58 @@ class ReconciliationJarSerializer(serializers.ModelSerializer):
|
|||||||
'cash_discrepancy',
|
'cash_discrepancy',
|
||||||
'total_cash_purchases',
|
'total_cash_purchases',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
class PaymentMethodSerializer(serializers.Serializer):
|
||||||
|
text = serializers.CharField()
|
||||||
|
value = serializers.CharField()
|
||||||
|
|
||||||
|
def to_representation(self, instance):
|
||||||
|
return {
|
||||||
|
'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()
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
lines = SummarySaleLineSerializer(many=True, source='saleline_set')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Sale
|
||||||
|
fields = ['id', 'date', 'customer', 'payment_method', 'lines']
|
||||||
|
@ -41,11 +41,13 @@ class TestSummaryViewPurchase(TestCase):
|
|||||||
def test_json_summary(self):
|
def test_json_summary(self):
|
||||||
url = f"/don_confiao/resumen_compra_json/{self.purchase.id}"
|
url = f"/don_confiao/resumen_compra_json/{self.purchase.id}"
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
|
content = response.content.decode('utf-8')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertIn('Alejo Mono', response.content.decode('utf-8'))
|
self.assertIn('Alejo Mono', content)
|
||||||
self.assertIn('cafe', response.content.decode('utf-8'))
|
self.assertIn('cafe', content)
|
||||||
self.assertIn('72500', response.content.decode('utf-8'))
|
self.assertIn('72500', content)
|
||||||
self.assertIn('quantity', response.content.decode('utf-8'))
|
self.assertIn('quantity', content)
|
||||||
self.assertIn('11', response.content.decode('utf-8'))
|
self.assertIn('11', content)
|
||||||
self.assertIn('date', response.content.decode('utf-8'))
|
self.assertIn('date', content)
|
||||||
self.assertIn(self.purchase.date, response.content.decode('utf-8'))
|
self.assertIn(self.purchase.date, content)
|
||||||
|
self.assertIn('lines', content)
|
||||||
|
@ -24,9 +24,9 @@ urlpatterns = [
|
|||||||
views.exportar_ventas_para_tryton,
|
views.exportar_ventas_para_tryton,
|
||||||
name="exportar_ventas_para_tryton"),
|
name="exportar_ventas_para_tryton"),
|
||||||
path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"),
|
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", views.payment_methods_to_select, name="payment_methods_to_select"),
|
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('reconciliate_jar', api_views.ReconciliateJarView.as_view()),
|
||||||
path('api/', include(router.urls)),
|
path('api/', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
@ -125,64 +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 payment_methods_to_select(request):
|
|
||||||
methods = [
|
|
||||||
{'text': choice[1], 'value': choice[0]}
|
|
||||||
for choice in PaymentMethods.choices
|
|
||||||
]
|
|
||||||
return JsonResponse(methods, 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
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
clean_categories = [c.strip() for c in categories]
|
clean_categories = [c.strip() for c in categories]
|
||||||
|
Loading…
Reference in New Issue
Block a user