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