Agregar indice para las cancelaciones en depositos de facturas
This commit is contained in:
parent
f1d47d1119
commit
5cda707d59
|
@ -3225,25 +3225,23 @@ class FacturasPagos(BaseModel):
|
|||
class Meta:
|
||||
order_by = ('factura',)
|
||||
indexes = (
|
||||
(('factura', 'numero'), True),
|
||||
(('movimiento', 'factura', 'numero'), True),
|
||||
)
|
||||
|
||||
def _movimiento_anterior(self, mov, id):
|
||||
try:
|
||||
query = (FacturasPagos
|
||||
.select()
|
||||
.where(
|
||||
(FacturasPagos.movimiento==mov) &
|
||||
(FacturasPagos.factura==id) &
|
||||
(FacturasPagos.cancelado==False))[-1]
|
||||
)
|
||||
return query
|
||||
except IndexError:
|
||||
return None
|
||||
query = (FacturasPagos
|
||||
.select()
|
||||
.where(FacturasPagos.factura==id,
|
||||
FacturasPagos.cancelado==False)
|
||||
)
|
||||
if len(query):
|
||||
return query[-1], len(query) + 1
|
||||
else:
|
||||
return None, 1
|
||||
|
||||
def _actualizar_saldo_cliente(self, cliente, importe):
|
||||
q = (Socios
|
||||
.update(saldo_cliente=Socios.saldo_cliente - importe)
|
||||
.update(saldo_cliente=Socios.saldo_cliente + importe)
|
||||
.where(Socios.id==cliente.id)
|
||||
)
|
||||
return bool(q.execute())
|
||||
|
@ -3251,15 +3249,18 @@ class FacturasPagos(BaseModel):
|
|||
|
||||
@classmethod
|
||||
def add(cls, mov, ids):
|
||||
print (ids)
|
||||
for i, importe in ids.items():
|
||||
fac = Facturas.get(Facturas.id==int(i))
|
||||
mov_ant = cls._movimiento_anterior(cls, mov, fac)
|
||||
nuevo = {'movimiento': mov, 'factura': fac, 'importe': importe}
|
||||
mov_ant, numero = cls._movimiento_anterior(cls, mov, fac)
|
||||
nuevo = {
|
||||
'movimiento': mov,
|
||||
'factura': fac,
|
||||
'numero': numero,
|
||||
'importe': importe,
|
||||
}
|
||||
if mov_ant is None:
|
||||
nuevo['saldo_anterior'] = float(fac.saldo)
|
||||
else:
|
||||
nuevo['numero'] = mov_ant.numero + 1
|
||||
nuevo['saldo_anterior'] = float(mov_ant.saldo)
|
||||
nuevo['saldo'] = nuevo['saldo_anterior'] - importe
|
||||
FacturasPagos.create(**nuevo)
|
||||
|
@ -3267,7 +3268,7 @@ class FacturasPagos(BaseModel):
|
|||
if nuevo['saldo'] == 0:
|
||||
fac.pagada = True
|
||||
fac.save()
|
||||
cls._actualizar_saldo_cliente(cls, fac.cliente, importe)
|
||||
cls._actualizar_saldo_cliente(cls, fac.cliente, importe * -1)
|
||||
return
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue