refactor: Update project settings, URL configurations, client views, and remove requirements.txt.

This commit is contained in:
2026-02-23 18:44:06 -03:00
parent 2160998c23
commit 7ddaa2d1f9
129 changed files with 888 additions and 40 deletions

View File

@@ -0,0 +1,74 @@
from rest_framework import viewsets, permissions, status
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import Client
from .serializers import ClientSerializer
from comandas.models import Comanda, ProductComanda
from comandas.serializers import ComandaSerializer
from payments.models import Payments, somar
from typePay.models import TypePay
class ClientViewSet(viewsets.ModelViewSet):
queryset = Client.objects.all()
serializer_class = ClientSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return Client.objects.all()
@action(detail=True, methods=['get'])
def fiados(self, request, pk=None):
client = self.get_object()
comandas = Comanda.objects.filter(client=client, status='FIADO')
serializer = ComandaSerializer(comandas, many=True)
return Response(serializer.data)
@action(detail=False, methods=['post'])
def pagar_fiados(self, request):
comanda_ids = request.data.get('ids', [])
if not comanda_ids:
return Response({'error': 'Nenhum ID de comanda fornecido.'}, status=status.HTTP_400_BAD_REQUEST)
results = []
for comanda_id in comanda_ids:
try:
comanda = Comanda.objects.get(id=comanda_id)
# Apenas processar se estiver como FIADO
if comanda.status != 'FIADO':
results.append({'id': comanda_id, 'status': 'erro', 'message': 'Status da comanda não é FIADO.'})
continue
# 1. Fechar a comanda
comanda.status = 'CLOSED'
comanda.save()
# 2. Gerar o pagamento (Inspirado no payDebt original)
try:
type_pay = TypePay.objects.get(id=1) # Assume ID 1 como padrão para recebimento de fiado
except TypePay.DoesNotExist:
type_pay, _ = TypePay.objects.get_or_create(id=1, defaults={'name': 'Dinheiro'})
consumo = ProductComanda.objects.filter(comanda=comanda)
valores = somar(consumo, comanda)
payment = Payments.objects.create(
value=valores['totalSemTaxa'], # Valor que falta pagar
type_pay=type_pay,
comanda=comanda,
client=comanda.client,
description='PAGAMENTO DE FIADO (via API)'
)
results.append({'id': comanda_id, 'status': 'sucesso', 'payment_id': payment.id})
except Comanda.DoesNotExist:
results.append({'id': comanda_id, 'status': 'erro', 'message': 'Comanda não encontrada.'})
except Exception as e:
results.append({'id': comanda_id, 'status': 'erro', 'message': str(e)})
return Response({
'success': True,
'message': f'{len(comanda_ids)} comandas processadas',
'results': results
}, status=status.HTTP_200_OK)

View File

@@ -0,0 +1,23 @@
from rest_framework import serializers
from .models import Client
from comandas.models import Comanda, ProductComanda
from payments.models import Payments, somar
from decimal import Decimal
class ClientSerializer(serializers.ModelSerializer):
debt = serializers.SerializerMethodField()
class Meta:
model = Client
fields = ['id', 'name', 'created_at', 'active', 'contact', 'debt']
def get_debt(self, obj):
comandas = Comanda.objects.filter(client=obj, status='FIADO')
total_debt = Decimal(0)
for comanda in comandas:
consumo = ProductComanda.objects.filter(comanda=comanda)
valores = somar(consumo, comanda)
total_debt += valores['totalSemTaxa']
return total_debt

View File

@@ -89,14 +89,12 @@ def payDebt(request):
typePayment = TypePay.objects.get(id=1)
consumo = ProductComanda.objects.filter(comanda=comanda_id)
value = somar(consumo,comanda)
print(value["totalSemTaxa"])
description = 'PAGAMENTO DE FIADO'
pagamento = Payments(value=value["totalSemTaxa"], comanda=comanda, type_pay=typePayment,description=description,client=comanda.client)
pagamento.save()
except Comanda.DoesNotExist:
return JsonResponse({'error': f'Comanda com ID {comanda_id} não encontrada'}, status=404)
# return redirect(f'/clients/viewClient/{comanda.client.id}')
return JsonResponse({
'success': True,