fix: conexão com api de pagamento de multiplas comandas ok

This commit is contained in:
2025-06-22 17:19:18 -03:00
parent 0e7c7d7c68
commit 179342ff80
14 changed files with 281 additions and 38 deletions

View File

@@ -51,16 +51,7 @@ Clientes
<input type="hidden" id="name-{{client.id}}" value="{{ client.name }}">
<input type="hidden" id="contact-{{client.id}}" value="{{ client.contact }}">
<form style="max-width: 50px;" hx-post="{% url 'payDebt' %}" hx-trigger="click" hx-target="#client-list">
{% csrf_token %}
<input type="hidden" name="id-client" id="id-{{client.id}}" value="{{ client.id }}">
<button style="background-color: rgba(255, 0, 0, 0); padding: 0px; border: none;">
<img
src="{% static 'midia/icons/pay.svg' %}"
style="width: 35px; height: 35px; cursor: pointer;">
</img>
</button>
</form>
</div>
</td>

View File

@@ -16,7 +16,12 @@ Comandas
<body>
<div style="justify-self: center;">
<h4>{{client.name}}</h4>
<h4>R$ {{client.id | totalFiado}}</h4>
<h4>R$ {{client.id | totalFiado}}</h4><br>
<h4 id="total-selecionado">R$</h4>
<button id="btn-fechar-comandas" class="btn-fechar" onclick="enviarComandasSelecionadas()">
<span class="icon"></span>
Fechar Comandas Selecionadas
</button>
</div>
<div class=" ">
@@ -26,6 +31,7 @@ Comandas
<th style="text-align: left;"><b>Atendente</b></th>
<th style="text-align: left;"><b>Data abertura</b></th>
<th style="text-align: left;"><b>Data fechamento</b></th>
<th style="text-align: left;"><b><input id="selectAll" name="selectAll" type="checkbox"></b></th>
<th style="text-align: left;"><b>Detalhes</b></th>
<th style="text-align: left;"><b>Valor</b></th>
</tr>
@@ -35,6 +41,7 @@ Comandas
<td>{{comanda.user.first_name}} {{comanda.user.last_name}}</td>
<td>{{comanda.dt_open}}</td>
<td>{{comanda.dt_close}}</td>
<td><input id="{{comanda.id}}" name="{{comanda.id}}" type="checkbox"></td>
<td>
<span data-tooltip="Visualizar Comanda" data-flow="top">
<a href="{% url 'viewcomanda' %}?parametro={{ comanda.id }}">
@@ -57,6 +64,7 @@ Comandas
</body>
<script src="{% static 'comandas/js/comandas.js' %}"></script>
<script src="{% static 'clients/js/clients.js' %}"></script>
{% endblock %}

View File

@@ -1,11 +1,19 @@
from decimal import Decimal
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
import json
from comandas.models import Comanda, ProductComanda
from gestaoRaul.decorators import group_required
from clients.models import Client
from payments.models import Payments
from payments.models import Payments, somar
from typePay.models import TypePay
# Create your views here.
@@ -56,10 +64,48 @@ def editClient(request):
client.save()
return redirect('/clients')
@csrf_exempt
@require_POST
def payDebt(request):
# id = request.POST.get('id-client')
# client_id = int(id)
# client = Client.objects.get(id=client_id)
# client.debt = client.debt - 1
# client.save()
return redirect('/clients')
try:
# Verifica se é uma requisição AJAX
if not request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return JsonResponse({'error': 'Requisição inválida'}, status=400)
# Obter os IDs do corpo da requisição (não mais da URL)
try:
data = json.loads(request.body)
comanda_ids = data.get('ids', [])
except json.JSONDecodeError:
return JsonResponse({'error': 'JSON inválido'}, status=400)
for comanda_id in comanda_ids:
try:
comanda = Comanda.objects.get(id=comanda_id)
comanda.status = 'CLOSED'
comanda.save()
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,
# 'message': f'{len(comanda_ids)} comandas processadas',
# 'ids': comanda_ids
# }, status=200)
except Exception as e:
return JsonResponse({
'success': False,
'error': str(e)
}, status=500)