update balance_libro_details as t1
set t1.saldo_id = 2

where  t1.plan_cuenta_id != 351
and t1.balance_libro_id in
(
	select balance_libro_id
    from 
		(
			SELECT bl.id,bl.total,bld.balance_libro_id,bld.plan_cuenta_id,bld.saldo_id,bld.monto 
            FROM db1.balance_libro_details bld
            inner join balance_libro bl on bld.balance_libro_id = bl.id
            where total < 0 and bld.conta_tipo_asiento_id =8
            and bld.plan_cuenta_id !=351
            
				
		) as t2
);


-- con esto paso a positivo lo negativo:
update db1.balance_libro as t1, 
(
SELECT * FROM db1.balance_libro
where total < 0
and conta_tipo_asiento_id = 8
) as t2

set t1.total = t2.total*-1
where t1.id 
in (
	select id from(
		SELECT id FROM db1.balance_libro
		where total < 0
		and conta_tipo_asiento_id = 8) as t3
)

-- Cambio el saldo a las liquidaciones de iibb
START TRANSACTION;

update balance_libro_details as t1
set t1.saldo_id = 1
where t1.balance_libro_id in
(
	select balance_libro_id
    from 
		(
			SELECT bl.id,bl.total,bld.balance_libro_id,bld.plan_cuenta_id,bld.saldo_id,bld.monto 
            FROM db1.balance_libro_details bld
            inner join balance_libro bl on bld.balance_libro_id = bl.id
            where monto < 0 and bld.conta_tipo_asiento_id =16            
				
		) as t2
)
and t1.plan_cuenta_id in

(select plan_cuenta_id from
	(
	SELECT distinct(bld.plan_cuenta_id) 
	FROM db1.balance_libro_details bld
	inner join balance_libro bl on bld.balance_libro_id = bl.id
	where monto < 0 and bld.conta_tipo_asiento_id =16    
	) t3
)

and t1.monto <0

-- rollback
-- commit

-- Cambio las liquidaciones de IIBB al saldo 1
START TRANSACTION;

update balance_libro_details as t1
set t1.saldo_id = 1
where t1.balance_libro_id in
(
	select balance_libro_id
    from 
		(
			SELECT bl.id,bl.total,bld.balance_libro_id,bld.plan_cuenta_id,bld.saldo_id,bld.monto 
            FROM db1.balance_libro_details bld
            inner join balance_libro bl on bld.balance_libro_id = bl.id
            where monto < 0 and bld.conta_tipo_asiento_id =16            
				
		) as t2
)
and t1.plan_cuenta_id in

(select plan_cuenta_id from
	(
	SELECT distinct(bld.plan_cuenta_id) 
	FROM db1.balance_libro_details bld
	inner join balance_libro bl on bld.balance_libro_id = bl.id
	where monto < 0 and bld.conta_tipo_asiento_id =16    
	) t3
)

and t1.monto <0

-- le cambio el signo negativo
START TRANSACTION;

update db1.balance_libro_details as t1, 
(
SELECT * FROM db1.balance_libro_details
where monto < 0
and conta_tipo_asiento_id = 16
) as t2

set t1.monto = t2.monto*-1
where t1.id 
in (
	select id from(
		SELECT id FROM db1.balance_libro_details
		where monto < 0
		and conta_tipo_asiento_id = 16) as t3
)
and t1.monto <0

-- rollback
-- commit