Update inventory in cancel tickets

This commit is contained in:
Mauricio Baeza 2021-10-05 15:11:27 -05:00
parent cd35884999
commit c64de2bbb9
1 changed files with 14 additions and 17 deletions

View File

@ -7579,7 +7579,7 @@ class Tickets(BaseModel):
return '\n'.join([r.notas for r in rows])
@utils.run_in_thread
def _update_inventory(cls, ticket, cancel=False, user=None):
def _update_inventory(cls, id_ticket, cancel=False, user=None):
warehouse = None
try:
warehouse = user.sucursal.warehouse
@ -7588,13 +7588,14 @@ class Tickets(BaseModel):
products = (TicketsDetalle
.select()
.where(TicketsDetalle.ticket==ticket.id)
.where(TicketsDetalle.ticket==id_ticket)
)
for p in products:
if p.producto.inventario:
cant = Decimal(p.cantidad)
if cancel:
cant *= -1
warehouse = p.warehouse
p.producto.existencia -= cant
p.producto.save()
@ -7626,7 +7627,7 @@ class Tickets(BaseModel):
obj.total = totals['total']
obj.save()
cls._update_inventory(cls, obj, user=user)
cls._update_inventory(cls, obj.id, user=user)
row = {
'id': obj.id,
@ -7900,17 +7901,13 @@ class Tickets(BaseModel):
)
return query
def _update_inventory_if_cancel(self, id):
products = TicketsDetalle.select().where(TicketsDetalle.ticket==id)
for p in products:
if p.producto.inventario:
p.producto.existencia += p.cantidad
p.producto.save()
return
# ~ def _update_inventory_if_cancel(self, id):
# ~ products = TicketsDetalle.select().where(TicketsDetalle.ticket==id)
# ~ for p in products:
# ~ if p.producto.inventario:
# ~ p.producto.existencia += p.cantidad
# ~ p.producto.save()
# ~ return
@classmethod
def cancel(cls, values, user):
@ -7922,12 +7919,12 @@ class Tickets(BaseModel):
id = int(values['id'])
msg = 'Ticket cancelado correctamente'
u = {'cancelado': True, 'estatus': 'Cancelado'}
fields = {'cancelado': True, 'estatus': 'Cancelado'}
with database_proxy.atomic() as txn:
obj = Tickets.update(**u).where(Tickets.id==id)
obj = Tickets.update(**fields).where(Tickets.id==id)
result = bool(obj.execute())
if result:
cls._update_inventory_if_cancel(cls, id)
cls._update_inventory(cls, id, True, user)
row = {'estatus': 'Cancelado'}
return {'ok': result, 'row': row, 'msg': msg}