feat: notificação de pedidos na cozinha

This commit is contained in:
2025-01-24 17:00:55 -03:00
parent 2d0cbae050
commit 1747305c05
7 changed files with 187 additions and 15 deletions

Binary file not shown.

View File

@@ -7,6 +7,7 @@ urlpatterns = [
path('preparing/<int:order_id>/', views.preparing, name='preparing'), path('preparing/<int:order_id>/', views.preparing, name='preparing'),
path('finished/<int:order_id>/', views.finished, name='finished'), path('finished/<int:order_id>/', views.finished, name='finished'),
path('delivered/<int:order_id>/', views.delivered, name='delivered'), path('delivered/<int:order_id>/', views.delivered, name='delivered'),
path('notificacao/', views.notificacao, name='notificacao'),
] ]

View File

@@ -1,6 +1,8 @@
# from datetime import timezone # from datetime import timezone
from django.utils import timezone from django.utils import timezone
from django.shortcuts import render from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from orders.models import Order from orders.models import Order
from django.db.models import Q from django.db.models import Q
@@ -39,3 +41,79 @@ def delivered(request, order_id):
fifteen_hours_ago = timezone.now() - timezone.timedelta(hours=15) fifteen_hours_ago = timezone.now() - timezone.timedelta(hours=15)
orders = Order.objects.filter(queue__gte=fifteen_hours_ago ) orders = Order.objects.filter(queue__gte=fifteen_hours_ago )
return render(request, 'htmx_components/orders/htmx_list_orders_fila.html',{'orders': orders}) return render(request, 'htmx_components/orders/htmx_list_orders_fila.html',{'orders': orders})
def notificacao(request):
fifteen_hours_ago = timezone.now() - timezone.timedelta(hours=15)
ordersFila = Order.objects.filter(queue__gte=fifteen_hours_ago)
ordersPronto = Order.objects.filter(queue__gte=fifteen_hours_ago, finished__isnull=False)
print(len(ordersFila))
print(len(ordersPronto))
grupoCozinha = request.user.groups.filter(name='Cozinha').exists()
grupoGarcom = request.user.groups.filter(name='Garçom').exists()
grupoGerente = request.user.groups.filter(name='Gerente').exists()
if grupoCozinha == True:
if 'fila' in request.COOKIES:
cookiesFila = int(request.COOKIES['fila'])
if len(ordersFila) > cookiesFila:
return JsonResponse({
'notificacao': 'true',
'fila': len(ordersFila),
'pronto':len(ordersPronto),
'titulo': 'Pedido para: '+ ordersFila[len(ordersFila)-1].id_comanda.name,
'corpo': ordersFila[len(ordersFila)-1].id_product.name,
})
else:
return JsonResponse({
'notificacao': 'false',
'fila': len(ordersFila),
'pronto':len(ordersPronto),
})
else:
return JsonResponse({
'notificacao': 'true',
'fila': len(ordersFila),
'pronto':len(ordersPronto),
'titulo': 'Pedido para: '+ ordersFila[len(ordersFila)-1].id_comanda.name,
'corpo': ordersFila[len(ordersFila)-1].id_product.name,
})
elif grupoGarcom == True and grupoGerente == False:
if 'pronto' in request.COOKIES:
cookiesPronto = int(request.COOKIES['pronto'])
if len(ordersPronto) > cookiesPronto:
return JsonResponse({
'notificacao': 'true',
'fila': len(ordersPronto),
'pronto':len(ordersPronto),
'titulo': ordersPronto[len(ordersPronto)-1].id_comanda.name,
'corpo': ordersPronto[len(ordersPronto)-1].id_product.name,
})
else:
return JsonResponse({
'notificacao': 'false',
'fila': len(ordersPronto),
})
else:
return JsonResponse({
'notificacao': 'false',
'fila': len(ordersPronto),
'pronto':len(ordersPronto),
'titulo': ordersPronto[len(ordersPronto)-1].id_comanda.name,
'corpo': ordersPronto[len(ordersPronto)-1].id_product.name,
})
else:
return JsonResponse({
'notificacao': 'false',
'fila': len(ordersPronto),
'pronto':len(ordersPronto),
'titulo': ordersPronto[len(ordersPronto)-1].id_comanda.name,
'corpo': ordersPronto[len(ordersPronto)-1].id_product.name,
})

View File

@@ -1,3 +1,8 @@
document.cookie = `pronto=0`;
function openModal() { function openModal() {
document.getElementById('Modal-create-comanda').style.display = 'block'; document.getElementById('Modal-create-comanda').style.display = 'block';
// HTMLDialogElement.show() // HTMLDialogElement.show()
@@ -21,3 +26,60 @@ document.getElementById('openModal').addEventListener('click', openModal);
// } // }
// ); // );
function mostrarNotificacao(titulo,corpo,grupo) {
if (Notification.permission != 'granted') {
Notification.requestPermission().then(function(permission) {
if (permission == 'granted') {
var notification = new Notification(titulo, {
body: corpo,
icon: 'https://example.com/icon.png'
});
}
});
} else {
var notification = new Notification(titulo, {
body: corpo,
icon: 'https://imagecolorpicker.com/imagecolorpicker-preview_b.avif',
image: 'https://imagecolorpicker.com/imagecolorpicker-preview_b.avif',
});
}
}
function notificacao(){
var resposta = fetch(`/pedidos/notificacao/`, {method: 'GET',
headers: {'Content-Type': 'application/json',
},})
.then(response => response.json())
.then(data => {
if (data['notificacao'] == 'true'){
console.log('verdadeiro')
document.cookie = `pronto=${data['pronto']}`;
// navigator.vibrate(200);
// navigator.vibrate([200, 100, 200]);
mostrarNotificacao(data['titulo'], data['corpo'],'Cozinha')
console.log(data['notificacao'])
}else{
document.cookie = `pronto=${data['pronto']}`;
console.log('falso')
console.log(data['notificacao'])
console.log('notificação foi false')
}
// var produtos_mais_vendidos = data.produtos_mais_vendidos
})
.catch(error => {
alert('Erro verificar notificação:', error)
console.error('Erro verificar notificação:', error);
});
}
setInterval(()=> {
notificacao()
}, 10000)

View File

@@ -1,4 +1,4 @@
document.cookie = `fila=0`;
function openTab(evt, etapa) { function openTab(evt, etapa) {
@@ -48,33 +48,64 @@ function openTab(evt, etapa) {
function mostrarNotificacao() { function mostrarNotificacao(titulo,corpo,grupo) {
if (Notification.permission != 'granted') { if (Notification.permission != 'granted') {
Notification.requestPermission().then(function(permission) { Notification.requestPermission().then(function(permission) {
if (permission == 'granted') { if (permission == 'granted') {
var notification = new Notification('Nova Mensagem', { var notification = new Notification(titulo, {
body: 'Você tem uma nova mensagem!', body: corpo,
icon: 'https://example.com/icon.png' icon: 'https://example.com/icon.png'
}); });
} }
}); });
} else { } else {
var notification = new Notification('Nova Mensagem', { var notification = new Notification(titulo, {
body: 'Você tem uma nova mensagem!', body: corpo,
icon: 'https://example.com/icon.png' icon: 'https://imagecolorpicker.com/imagecolorpicker-preview_b.avif',
image: 'https://imagecolorpicker.com/imagecolorpicker-preview_b.avif',
}); });
} }
} }
function notificacao(){
var resposta = fetch(`/pedidos/notificacao/`, {method: 'GET',
headers: {'Content-Type': 'application/json',
},})
.then(response => response.json())
.then(data => {
if (data['notificacao'] == 'true'){
document.cookie = `fila=${data['fila']}`;
// navigator.vibrate(200);
// navigator.vibrate([200, 100, 200]);
mostrarNotificacao(data['titulo'], data['corpo'],'Cozinha')
}else{
console.log(data['notificacao'])
console.log('notificação foi false')
}
// var produtos_mais_vendidos = data.produtos_mais_vendidos
})
.catch(error => {
alert('Erro verificar notificação:', error)
console.error('Erro verificar notificação:', error);
});
}
let i = 0; setInterval(()=> {
while (i < 10) { notificacao()
setTimeout(function() { }, 10000)
mostrarNotificacao();}, 3000);
}
setTimeout(function() {
mostrarNotificacao();}, 3000); // setTimeout(function() {
// mostrarNotificacao();}, 2000);
// mostrarNotificacao()
// notificacao()