129 lines
4.5 KiB
Python
129 lines
4.5 KiB
Python
from rest_framework import viewsets
|
|
from rest_framework.response import Response
|
|
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, SaleSummarySerializer
|
|
|
|
from decimal import Decimal
|
|
import json
|
|
|
|
class SaleView(viewsets.ModelViewSet):
|
|
queryset = Sale.objects.all()
|
|
serializer_class = SaleSerializer
|
|
|
|
def create(self, request):
|
|
data = request.data
|
|
customer = Customer.objects.get(pk=data['customer'])
|
|
date = data['date']
|
|
lines = data['saleline_set']
|
|
payment_method = data['payment_method']
|
|
sale = Sale.objects.create(
|
|
customer=customer,
|
|
date=date,
|
|
payment_method=payment_method
|
|
)
|
|
|
|
for line in lines:
|
|
product = Product.objects.get(pk=line['product'])
|
|
quantity = line['quantity']
|
|
unit_price = line['unit_price']
|
|
SaleLine.objects.create(
|
|
sale=sale,
|
|
product=product,
|
|
quantity=quantity,
|
|
unit_price=unit_price
|
|
)
|
|
|
|
return Response(
|
|
{'id': sale.id, 'message': 'Venta creada con exito'},
|
|
status=201
|
|
)
|
|
|
|
|
|
class ProductView(viewsets.ModelViewSet):
|
|
queryset = Product.objects.all()
|
|
serializer_class = ProductSerializer
|
|
|
|
|
|
class CustomerView(viewsets.ModelViewSet):
|
|
queryset = Customer.objects.all()
|
|
serializer_class = CustomerSerializer
|
|
|
|
|
|
class ReconciliateJarView(APIView):
|
|
def post(self, request):
|
|
data = request.data
|
|
cash_purchases_id = data.get('cash_purchases')
|
|
serializer = ReconciliationJarSerializer(data=data)
|
|
if serializer.is_valid():
|
|
cash_purchases = Sale.objects.filter(pk__in=cash_purchases_id)
|
|
if not self._is_valid_total(cash_purchases, data.get('total_cash_purchases')):
|
|
return Response(
|
|
{'error': 'total_cash_purchases not equal to sum of all purchases.'},
|
|
status=HTTP_400_BAD_REQUEST
|
|
)
|
|
reconciliation = serializer.save()
|
|
other_purchases = self._get_other_purchases(data.get('other_totals'))
|
|
|
|
self._link_purchases(reconciliation, cash_purchases, other_purchases)
|
|
return Response({'id': reconciliation.id})
|
|
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
|
|
|
|
def get(self, request):
|
|
reconciliations = ReconciliationJar.objects.all()
|
|
serializer = ReconciliationJarSerializer(reconciliations, many=True)
|
|
return Response(serializer.data)
|
|
|
|
def _is_valid_total(self, purchases, total):
|
|
calculated_total = sum(p.get_total() for p in purchases)
|
|
return calculated_total == Decimal(total)
|
|
|
|
def _get_other_purchases(self, other_totals):
|
|
if not other_totals:
|
|
return []
|
|
purchases = []
|
|
for method in other_totals:
|
|
purchases.extend(other_totals[method]['purchases'])
|
|
if purchases:
|
|
return Sale.objects.filter(pk__in=purchases)
|
|
return []
|
|
|
|
def _link_purchases(self, reconciliation, cash_purchases, other_purchases):
|
|
for purchase in cash_purchases:
|
|
purchase.reconciliation = reconciliation
|
|
purchase.clean()
|
|
purchase.save()
|
|
|
|
for purchase in other_purchases:
|
|
purchase.reconciliation = reconciliation
|
|
purchase.clean()
|
|
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)
|