Update inventory
This commit is contained in:
parent
61a8937eb3
commit
bc53171969
|
@ -4058,7 +4058,10 @@ class WareHouseProduct(BaseModel):
|
|||
product = args['product'],
|
||||
)
|
||||
obj, created = WareHouseProduct.get_or_create(**values)
|
||||
obj.exists += args['cant']
|
||||
cant = args['cant']
|
||||
if created:
|
||||
cant = float(cant)
|
||||
obj.exists += cant
|
||||
obj.save()
|
||||
return
|
||||
|
||||
|
@ -5120,6 +5123,9 @@ class Facturas(BaseModel):
|
|||
values['forma_pago'] = ''
|
||||
|
||||
with database_proxy.atomic() as txn:
|
||||
|
||||
print(values)
|
||||
|
||||
obj = Facturas.create(**values)
|
||||
totals = cls._calculate_totals(cls, obj, productos, tipo_comprobante)
|
||||
cls._guardar_relacionados(cls, obj, relacionados)
|
||||
|
@ -5547,7 +5553,7 @@ class Facturas(BaseModel):
|
|||
anticipo = True
|
||||
cls._actualizar_saldo_cliente(cls, obj)
|
||||
if update:
|
||||
cls._update_inventory(cls, obj)
|
||||
cls._update_inventory(cls, obj, user=user)
|
||||
cls._sync(cls, id, rfc)
|
||||
|
||||
m = 'T {}'.format(obj.id)
|
||||
|
@ -5823,18 +5829,38 @@ class Facturas(BaseModel):
|
|||
|
||||
# ~ v2
|
||||
@utils.run_in_thread
|
||||
def _update_inventory(self, invoice, cancel=False):
|
||||
def _update_inventory(self, invoice, cancel=False, user=None):
|
||||
if invoice.tipo_comprobante != 'I':
|
||||
return
|
||||
|
||||
warehouse = None
|
||||
try:
|
||||
warehouse = user.sucursal.warehouse
|
||||
except:
|
||||
pass
|
||||
|
||||
products = FacturasDetalle.get_by_invoice(invoice.id)
|
||||
for p in products:
|
||||
if p.producto.inventario:
|
||||
cant = Decimal(p.cantidad)
|
||||
if cancel:
|
||||
p.producto.existencia += Decimal(p.cantidad)
|
||||
else:
|
||||
p.producto.existencia -= Decimal(p.cantidad)
|
||||
cant *= -1
|
||||
# ~ p.producto.existencia += Decimal(p.cantidad)
|
||||
# ~ else:
|
||||
# ~ p.producto.existencia -= Decimal(p.cantidad)
|
||||
p.producto.existencia -= cant
|
||||
p.producto.save()
|
||||
|
||||
if warehouse is None:
|
||||
continue
|
||||
|
||||
fields = (
|
||||
WareHouseProduct.warehouse==warehouse,
|
||||
WareHouseProduct.product==p,
|
||||
)
|
||||
obj = WareHouseProduct.get(*fields)
|
||||
obj.exists -= cant
|
||||
obj.save()
|
||||
return
|
||||
|
||||
@utils.run_in_thread
|
||||
|
|
Loading…
Reference in New Issue