CREATE DEFINER=`root`@`localhost` PROCEDURE `SPRegistrar_BalanceDetailLibroLiq-IVA`()
BEGIN
	declare done int default 0;
	declare balance_libro_id,person_id, periodo_id, period_liquidation_id, plan_cuenta_id, saldo_id, comprobante_id,retention_type_id, zone_id int;
    declare fecha date;
    declare control,total_debit,total_credit,previous_technical,technical_balance,previous_free_availability,retention_amount,perception_amount,compensation_amount,
	negative_amount,positive_amount,total_free_availability,monto decimal(20,2) default 0.00;
    
    DECLARE liq_iva CURSOR FOR 
	select bl.id as balance_libro_id,pl.person_id,pl.period_id,pl.total_debit,pl.total_credit,pl.previous_technical,
	pl.technical_balance,pl.previous_free_availability,pl.retention_amount,pl.perception_amount,
	pl.compensation_amount,pl.negative_amount,pl.positive_amount,pl.total_free_availability 
	from period_liquidation_iva pl
	join balance_libro bl on pl.period_liquidation_id = bl.period_liquidation_id
	join period_liquidations pli on pl.period_liquidation_id = pli.id
	where pli.status = 2
	order by pl.period_liquidation_id;


	DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

	OPEN liq_iva;
	REPEAT

	FETCH liq_iva INTO balance_libro_id,person_id, periodo_id, total_debit,total_credit,previous_technical,
	technical_balance,previous_free_availability,retention_amount,perception_amount,compensation_amount,
	negative_amount,positive_amount,total_free_availability;
	-- este if tiene que estar sino agrega un registro mas
	IF NOT done THEN
		
   
		if total_debit <>0 then
			insert into balance_libro_details 
				(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
					VALUES
	            (balance_libro_id, person_id, periodo_id, 15, 67,1, total_debit );
	    end if;

	    if total_credit <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 68,2, total_credit );
        end if;

       	if previous_technical <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 353,2, previous_technical );
        end if;

       	if previous_free_availability <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 354,2, previous_free_availability );
        end if;

       	if retention_amount <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 25,2, retention_amount );
        end if;

       	if perception_amount <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 72,2, perception_amount );
        end if;

       	if compensation_amount <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 352,2, compensation_amount );
        end if;

       	if negative_amount <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 74,2, negative_amount );
        end if;

       	if positive_amount <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 353,1, positive_amount );
        end if;

       	if total_free_availability <>0 then
		insert into balance_libro_details 
			(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
				VALUES
            (balance_libro_id, person_id, periodo_id, 15, 354,1, total_free_availability );
        end if;


        -- Me fijo que cierre el asiento, sino cierra, registro la diferencia en la cuenta Ajustes!
        	set control :=  total_debit + positive_amount + total_free_availability - 
        	total_credit - previous_technical - previous_free_availability - retention_amount -
        	perception_amount - compensation_amount - negative_amount;

            if control <> 0 then
            	
            	if control > 0 then
            		set saldo_id := 2; 
            	end if;
            	if control < 0 then
            		set saldo_id := 1; 
            	end if;
            	set control := ABS(control);
            	insert into balance_libro_details 
				(balance_libro_id, person_id, period_id, conta_tipo_asiento_id, plan_cuenta_id, saldo_id, monto )
					VALUES
	            (balance_libro_id, person_id, periodo_id, 15, 136,saldo_id, control );

            end if;



    END IF; -- Fin If Done

	UNTIL done END REPEAT; -- fin del loop

  	CLOSE liq_iva;
  	
  
  END