DROP PROCEDURE IF EXISTS `bs`.`manaCustomerUpdate`;

DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `bs`.`manaCustomerUpdate`()
BEGIN
	DECLARE vToDated DATE;
	DECLARE vFromDated DATE;
	DECLARE vForDeleteDated DATE;
	DECLARE vManaId INT;
    DECLARE vManaAutoId INT;
    DECLARE vClaimManaId INT;
    DECLARE vManaBankId INT;
    DECLARE vManaGreugeTypeId INT;

	SELECT id INTO vManaId 
			FROM `component` WHERE code = 'mana';
		
	SELECT id INTO vManaAutoId 
		FROM `component` WHERE code = 'autoMana';
	
	SELECT id INTO vClaimManaId
		FROM `component` WHERE code = 'mana claim';
	
	SELECT id INTO vManaBankId 
		FROM `bank` WHERE code = 'mana';
	
	SELECT id INTO vManaGreugeTypeId 
		FROM `greugeType` WHERE code = 'mana';

	SELECT IFNULL(max(dated), '2016-01-01')
            INTO vFromDated 
        FROM bs.manaCustomer;
	
    DELETE 
		FROM bs.manaCustomer 
		WHERE dated = vFromDated;

	SELECT IFNULL(max(dated), '2016-01-01')
            INTO vFromDated 
        FROM bs.manaCustomer;

    WHILE timestampadd(DAY,30,vFromDated) < CURDATE() DO

        SELECT
                timestampadd(DAY,30,vFromDated),
                timestampadd(DAY,-90,vFromDated) 
            INTO 
                vToDated,
                vForDeleteDated;
            
        DELETE FROM bs.manaCustomer 
            WHERE dated <= vForDeleteDated;


        INSERT INTO bs.manaCustomer(Id_Cliente, Mana, dated)

            SELECT 
                    Id_Cliente,
                    cast(sum(mana) as decimal(10,2)) as mana,
                    vToDated as dated 
                FROM 

                (
                    SELECT cs.Id_Cliente, Cantidad * Valor as mana
                        FROM vn2008.Tickets t
                            JOIN vn2008.Consignatarios cs using(Id_Consigna)
                            JOIN vn2008.Movimientos m on m.Id_Ticket = t.Id_Ticket
                            JOIN vn2008.Movimientos_componentes mc on mc.Id_Movimiento = m.Id_Movimiento
                        WHERE Id_Componente IN (vManaAutoId, vManaId, vClaimManaId)
                            AND t.Fecha > vFromDated 
                            AND date(t.Fecha) <= vToDated
                    

                    UNION ALL

                    SELECT r.Id_Cliente, - Entregado
                        FROM vn2008.Recibos r
                        WHERE Id_Banco = vManaBankId
                            AND Fechacobro > vFromDated 
                            AND Fechacobro <= vToDated
                    
                    UNION ALL
                    
                    SELECT g.Id_Cliente, g.Importe
                        FROM vn2008.Greuges g
                        WHERE Greuges_type_id = vManaGreugeTypeId
                            AND Fecha > vFromDated 
                            AND Fecha <= vToDated
                    
                    UNION ALL
                    
                    SELECT Id_Cliente, mana
                        FROM bs.manaCustomer
                        WHERE dated = vFromDated    
                ) sub

                GROUP BY Id_Cliente
                HAVING Id_Cliente;

        SET vFromDated = vToDated;
               
    END WHILE;

END$$
DELIMITER ;
