From cd358849991fec27d970ca906d129f1fef76fc14 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Mon, 4 Oct 2021 22:21:49 -0500 Subject: [PATCH] Add warehouse in detail tickets --- source/app/models/main.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/source/app/models/main.py b/source/app/models/main.py index bae0a25..b333597 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -7482,12 +7482,18 @@ class Tickets(BaseModel): base = round(importe / tasa, DECIMALES) return base - def _calcular_totales(self, ticket, productos): + def _calcular_totales(self, ticket, productos, user=None): subtotal = 0 descuento_cfdi = 0 totals_tax = {} total_trasladados = None + warehouse = None + try: + warehouse = user.sucursal.warehouse + except: + pass + for producto in productos: id_producto = producto.pop('id_product') p = Productos.get(Productos.id==id_producto) @@ -7507,6 +7513,8 @@ class Tickets(BaseModel): producto['precio_final'] = precio_final producto['importe'] = importe + producto['warehouse'] = warehouse + descuento_cfdi += descuento subtotal += importe @@ -7593,12 +7601,12 @@ class Tickets(BaseModel): if warehouse is None: continue - fields = ( + where = ( WareHouseProduct.warehouse==warehouse, WareHouseProduct.product==p.producto, ) - obj = WareHouseProduct.get(*fields) + obj = WareHouseProduct.get(*where) obj.exists -= cant obj.save() return @@ -7611,7 +7619,7 @@ class Tickets(BaseModel): with database_proxy.atomic() as txn: obj = Tickets.create(**values) - totals = cls._calcular_totales(cls, obj, productos) + totals = cls._calcular_totales(cls, obj, productos, user) obj.subtotal = totals['subtotal'] obj.descuento = totals['descuento'] obj.total_trasladados = totals['total_trasladados'] @@ -7898,6 +7906,10 @@ class Tickets(BaseModel): if p.producto.inventario: p.producto.existencia += p.cantidad p.producto.save() + + + + return @classmethod @@ -8154,6 +8166,7 @@ class TicketsDetalle(BaseModel): auto_round=True) importe = DecimalField(default=0.0, max_digits=20, decimal_places=6, auto_round=True) + warehouse = ForeignKeyField(Almacenes, null=True) class Meta: order_by = ('ticket',) @@ -10308,6 +10321,13 @@ def _migrate_tables(rfc=''): warehouse = ForeignKeyField(Almacenes, null=True, to_field=Almacenes.id) migrations.append(migrator.add_column(table, 'warehouse_id', warehouse)) + table = 'ticketsdetalle' + field = 'warehouse_id' + columns = [c.name for c in database_proxy.get_columns(table)] + if not field in columns: + warehouse = ForeignKeyField(Almacenes, null=True, to_field=Almacenes.id) + migrations.append(migrator.add_column(table, field, warehouse)) + if migrations: with database_proxy.atomic() as txn: migrate(*migrations)