feat: criado funcções de movimentação de estoque

This commit is contained in:
2025-07-22 18:22:41 -03:00
parent a9289cb28d
commit 37428e8cc7
15 changed files with 164 additions and 23 deletions

View File

@@ -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)

View File

@@ -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,15 +39,27 @@ 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()
Product.addStock(Product.objects.get(id=product_comanda.product.id), 1)
msg = JsonResponse({
'type': 'broadcast',
@@ -60,8 +74,15 @@ 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()
Product.addStock(Product.objects.get(id=product_comanda.product.id), 1)
consumo = ProductComanda.objects.filter(comanda=comanda)
valores = somar(consumo,comanda)

View File

@@ -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
@@ -77,5 +77,75 @@ class StockMovement(models.Model):
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']

View File

@@ -3,15 +3,16 @@ from django.urls import reverse
from django.utils import timezone
from django.http import JsonResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from django.db.models import Count, F
from comandas.models import Comanda, ProductComanda
from comandas.models import Comanda, ProductComanda, StockMovement, StockMovementType
from clients.models import Client
from payments.models import Payments, somar
from orders.models import Order
from products.models import Product
from products.models import Product, ProductComponent
from mesas.models import Mesa
from gestaoRaul.decorators import group_required
@@ -147,9 +148,21 @@ def addProduct(request, product_id, comanda_id):
obs = request.GET.get("obs")
product_comanda = ProductComanda(comanda_id=comanda_id, product_id=product_id)
product_comanda.save()
Product.subStock(Product.objects.get(id=product_id), 1)
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 - Comanda")
StockMovement.subTransactionStock(
product=product,
movement_type=typeMovement,
comanda=comanda,
user=user,
qtd=1,
obs= "Adicionado na comanda"
)
parcial = Payments.objects.filter(comanda=comanda)
if product.cuisine == True:
order = Order(id_comanda=comanda, id_product=product, productComanda=product_comanda, obs='')