diff --git a/gestaoRaul/balcao/__pycache__/htmx_views.cpython-312.pyc b/gestaoRaul/balcao/__pycache__/htmx_views.cpython-312.pyc index 2762430..47e2733 100644 Binary files a/gestaoRaul/balcao/__pycache__/htmx_views.cpython-312.pyc and b/gestaoRaul/balcao/__pycache__/htmx_views.cpython-312.pyc differ diff --git a/gestaoRaul/balcao/__pycache__/views.cpython-312.pyc b/gestaoRaul/balcao/__pycache__/views.cpython-312.pyc index e5fe050..718e315 100644 Binary files a/gestaoRaul/balcao/__pycache__/views.cpython-312.pyc and b/gestaoRaul/balcao/__pycache__/views.cpython-312.pyc differ diff --git a/gestaoRaul/balcao/htmx_views.py b/gestaoRaul/balcao/htmx_views.py index 9b608e0..5ab531a 100644 --- a/gestaoRaul/balcao/htmx_views.py +++ b/gestaoRaul/balcao/htmx_views.py @@ -6,7 +6,7 @@ from django.contrib.auth.models import User -from comandas.models import Comanda, ProductComanda +from comandas.models import Comanda, ProductComanda, StockMovementType, StockMovement from mesas.models import Mesa from products.models import Product from payments.models import Payments @@ -30,6 +30,20 @@ def addProductBalcao(request, product_id, comanda_id, qtd): for i in range(qtd): product_comanda = ProductComanda(comanda_id=comanda_id, product_id=product_id) product_comanda.save() + product = Product.objects.get(id=product_id) + comanda = Comanda.objects.get(id=comanda_id) + user = User.objects.get(id=request.user.id) + typeMovement = StockMovementType.objects.get(name="Venda - Balcao") + + StockMovement.subTransactionStock( + product=product, + movement_type=typeMovement, + comanda=comanda, + user=user, + qtd=1, + obs= "Vendido no balcão" + ) + consumo = ProductComanda.objects.filter(comanda=comanda_id) total = 0 for produto in consumo: @@ -42,6 +56,20 @@ def addProductBalcaoTeclado(request, product_id, comanda_id, qtd): for i in range(qtd): product_comanda = ProductComanda(comanda_id=comanda_id, product_id=product_id) product_comanda.save() + product = Product.objects.get(id=product_id) + comanda = Comanda.objects.get(id=comanda_id) + user = User.objects.get(id=request.user.id) + typeMovement = StockMovementType.objects.get(name="Venda - Balcao") + + StockMovement.subTransactionStock( + product=product, + movement_type=typeMovement, + comanda=comanda, + user=user, + qtd=1, + obs= "Vendido no balcão" + ) + consumo = ProductComanda.objects.filter(comanda=comanda_id) total = 0 for produto in consumo: @@ -51,8 +79,24 @@ def addProductBalcaoTeclado(request, product_id, comanda_id, qtd): @group_required(groupName='Garçom') def removeProductBalcao(request, productComanda_id): product_comanda = ProductComanda.objects.get(id=productComanda_id) + comanda = product_comanda.comanda + product = product_comanda.product + user = User.objects.get(id=request.user.id) + typeMovement = StockMovementType.objects.get(name="Estorno") + consumo = ProductComanda.objects.filter(comanda=product_comanda.comanda) + StockMovement.sumTransactionStock( + product=product, + movement_type=typeMovement, + comanda=comanda, + user=user, + qtd=1, + obs= "Excluido do balcão" + ) + + product_comanda.delete() + total = 0 for produto in consumo: total += produto.product.price diff --git a/gestaoRaul/comandas/__pycache__/admin.cpython-312.pyc b/gestaoRaul/comandas/__pycache__/admin.cpython-312.pyc index ac25a4d..30aaef4 100644 Binary files a/gestaoRaul/comandas/__pycache__/admin.cpython-312.pyc and b/gestaoRaul/comandas/__pycache__/admin.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/__pycache__/htmx_views.cpython-312.pyc b/gestaoRaul/comandas/__pycache__/htmx_views.cpython-312.pyc index d3dc32f..c57a7db 100644 Binary files a/gestaoRaul/comandas/__pycache__/htmx_views.cpython-312.pyc and b/gestaoRaul/comandas/__pycache__/htmx_views.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/__pycache__/models.cpython-312.pyc b/gestaoRaul/comandas/__pycache__/models.cpython-312.pyc index f02c04a..793c12e 100644 Binary files a/gestaoRaul/comandas/__pycache__/models.cpython-312.pyc and b/gestaoRaul/comandas/__pycache__/models.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/__pycache__/urls.cpython-312.pyc b/gestaoRaul/comandas/__pycache__/urls.cpython-312.pyc index 60191b4..706f463 100644 Binary files a/gestaoRaul/comandas/__pycache__/urls.cpython-312.pyc and b/gestaoRaul/comandas/__pycache__/urls.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/__pycache__/views.cpython-312.pyc b/gestaoRaul/comandas/__pycache__/views.cpython-312.pyc index b055aa7..53182cc 100644 Binary files a/gestaoRaul/comandas/__pycache__/views.cpython-312.pyc and b/gestaoRaul/comandas/__pycache__/views.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/admin.py b/gestaoRaul/comandas/admin.py index c0dd1c6..d27670a 100644 --- a/gestaoRaul/comandas/admin.py +++ b/gestaoRaul/comandas/admin.py @@ -1,7 +1,9 @@ from django.contrib import admin -from comandas.models import Comanda, ProductComanda +from comandas.models import Comanda, ProductComanda, StockMovement, StockMovementType admin.site.register(Comanda) admin.site.register(ProductComanda) +admin.site.register(StockMovementType) +admin.site.register(StockMovement) diff --git a/gestaoRaul/comandas/htmx_views.py b/gestaoRaul/comandas/htmx_views.py index 1787fee..392b63c 100644 --- a/gestaoRaul/comandas/htmx_views.py +++ b/gestaoRaul/comandas/htmx_views.py @@ -2,8 +2,10 @@ from datetime import date from decimal import Decimal from django.http import JsonResponse from django.shortcuts import render, redirect +from django.contrib.auth.models import User -from comandas.models import Comanda, ProductComanda + +from comandas.models import Comanda, ProductComanda, StockMovementType, StockMovement from orders.models import Order from products.models import Product from payments.models import Payments, somar @@ -37,14 +39,28 @@ def removeProductComanda(request, productComanda_id): 'taxa': False } product_comanda = ProductComanda.objects.get(id=productComanda_id) - comanda = Comanda.objects.get(id= product_comanda.comanda.id) + comanda = product_comanda.comanda + product = product_comanda.product + user = User.objects.get(id=request.user.id) + typeMovement = StockMovementType.objects.get(name="Estorno") + if comanda.status == 'OPEN': parcial = Payments.objects.filter(comanda=comanda) consumo = ProductComanda.objects.filter(comanda=comanda) valores = somar(consumo,comanda) if product_comanda.product.cuisine == True: order = Order.objects.get(productComanda=product_comanda) + + StockMovement.sumTransactionStock( + product=product, + movement_type=typeMovement, + comanda=comanda, + user=user, + qtd=1, + obs= "Excluido da comanda" + ) product_comanda.delete() + msg = JsonResponse({ 'type': 'broadcast', 'message': 'Atenção! Pedido cancelado', @@ -58,6 +74,14 @@ def removeProductComanda(request, productComanda_id): consumo = ProductComanda.objects.filter(comanda=comanda) valores = somar(consumo,comanda) else: + StockMovement.sumTransactionStock( + product=product, + movement_type=typeMovement, + comanda=comanda, + user=user, + qtd=1, + obs= "Excluido da comanda" + ) product_comanda.delete() consumo = ProductComanda.objects.filter(comanda=comanda) valores = somar(consumo,comanda) diff --git a/gestaoRaul/comandas/migrations/0005_stockmovementtype_stockmovement.py b/gestaoRaul/comandas/migrations/0005_stockmovementtype_stockmovement.py new file mode 100644 index 0000000..1dd6d62 --- /dev/null +++ b/gestaoRaul/comandas/migrations/0005_stockmovementtype_stockmovement.py @@ -0,0 +1,42 @@ +# Generated by Django 5.1.4 on 2025-07-22 18:36 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('comandas', '0004_comanda_user'), + ('products', '0004_unitofmeasure_productcomponent_product_components_and_more'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='StockMovementType', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('name', models.CharField(help_text="Ex: 'Entrada por Compra', 'Saída por Venda', 'Ajuste de Estoque'", max_length=100, unique=True)), + ('observation', models.TextField(blank=True, null=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ], + ), + migrations.CreateModel( + name='StockMovement', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('quantity', models.IntegerField(help_text='Quantidade movimentada. Use valores negativos para saídas.')), + ('observation', models.TextField(blank=True, null=True)), + ('movement_date', models.DateTimeField(auto_now_add=True)), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='stock_movements', to='products.product')), + ('related_comanda', models.ForeignKey(blank=True, help_text='Comanda relacionada à movimentação (opcional).', null=True, on_delete=django.db.models.deletion.SET_NULL, to='comandas.comanda')), + ('user', models.ForeignKey(blank=True, help_text='Usuário que realizou a movimentação.', null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), + ('movement_type', models.ForeignKey(help_text='Tipo de movimentação (entrada, saída, ajuste).', on_delete=django.db.models.deletion.PROTECT, to='comandas.stockmovementtype')), + ], + options={ + 'ordering': ['-movement_date'], + }, + ), + ] diff --git a/gestaoRaul/comandas/migrations/__pycache__/0005_stockmovementtype_stockmovement.cpython-312.pyc b/gestaoRaul/comandas/migrations/__pycache__/0005_stockmovementtype_stockmovement.cpython-312.pyc new file mode 100644 index 0000000..a1c63ed Binary files /dev/null and b/gestaoRaul/comandas/migrations/__pycache__/0005_stockmovementtype_stockmovement.cpython-312.pyc differ diff --git a/gestaoRaul/comandas/models.py b/gestaoRaul/comandas/models.py index 6e6bf59..b1919c7 100644 --- a/gestaoRaul/comandas/models.py +++ b/gestaoRaul/comandas/models.py @@ -5,7 +5,7 @@ from django.db.models import Count, F from clients.models import Client -from products.models import Product +from products.models import Product, ProductComponent from mesas.models import Mesa from typePay.models import TypePay @@ -43,4 +43,109 @@ class ProductComanda(models.Model): for p in products: if p.name == produto['nome'] and p.active == True: products_ordenados.append(p) - return products_ordenados[:15] \ No newline at end of file + return products_ordenados[:15] + + +class StockMovementType(models.Model): + id = models.AutoField(primary_key=True) + name = models.CharField(max_length=100, unique=True, help_text="Ex: 'Entrada por Compra', 'Saída por Venda', 'Ajuste de Estoque'") + observation = models.TextField(null=True, blank=True) + created_at = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return self.name + +class StockMovement(models.Model): + id = models.AutoField(primary_key=True) + product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='stock_movements') + user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, help_text="Usuário que realizou a movimentação.") + related_comanda = models.ForeignKey( + Comanda, + on_delete=models.SET_NULL, + null=True, + blank=True, + help_text="Comanda relacionada à movimentação (opcional)." + ) + movement_type = models.ForeignKey(StockMovementType, on_delete=models.PROTECT, help_text="Tipo de movimentação (entrada, saída, ajuste).") + quantity = models.IntegerField(help_text="Quantidade movimentada. Use valores negativos para saídas.") + observation = models.TextField(null=True, blank=True) + movement_date = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return ( + f"Movimentação de {self.quantity} de {self.product.name} " + f"({self.movement_type.name}) em {self.movement_date.strftime('%d/%m/%Y %H:%M')}" + ) + + + def subTransactionStock(product:Product, + movement_type:StockMovementType, + comanda:Comanda, + obs:str, + user:User=None, + qtd:int=1): + + components = ProductComponent.objects.filter(composite_product=product) + if components.exists(): + for component in components: + movi = StockMovement.objects.create( + product=component.component_product , + related_comanda=comanda, + user=user, + movement_type=movement_type, + quantity=-component.quantity_required, + observation=obs + ) + movi.save() + component.component_product.quantity -= component.quantity_required + component.component_product.save() + + movi = StockMovement.objects.create( + product=product , + related_comanda=comanda, + user=user, + movement_type=movement_type, + quantity=-qtd, + observation=obs + ) + movi.save() + product.quantity -= qtd + product.save() + + + def sumTransactionStock(product:Product, + movement_type:StockMovementType, + comanda:Comanda, + obs:str, + user:User=None, + qtd:int=1): + + components = ProductComponent.objects.filter(composite_product=product) + if components.exists(): + for component in components: + movi = StockMovement.objects.create( + product=component.component_product , + related_comanda=comanda, + user=user, + movement_type=movement_type, + quantity=component.quantity_required, + observation=obs + ) + movi.save() + component.component_product.quantity += component.quantity_required + component.component_product.save() + + movi = StockMovement.objects.create( + product=product , + related_comanda=comanda, + user=user, + movement_type=movement_type, + quantity=qtd, + observation=obs + ) + movi.save() + product.quantity += qtd + product.save() + + class Meta: + ordering = ['-movement_date'] \ No newline at end of file diff --git a/gestaoRaul/comandas/templates/viewcomanda.html b/gestaoRaul/comandas/templates/viewcomanda.html index 8d6ff2b..920a66a 100644 --- a/gestaoRaul/comandas/templates/viewcomanda.html +++ b/gestaoRaul/comandas/templates/viewcomanda.html @@ -50,7 +50,7 @@ Detalhes {{comanda.name}}