DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`clientDebtSpray`(vClientFk INT)
BEGIN

	/* Reparte el saldo de un cliente en greuge en la cartera que corresponde, y desasigna el comercial
    *
    *
    */

	DECLARE vClientDebt DOUBLE;
    
    SELECT vn.client_getDebt(vClientFk, util.VN_CURDATE()) INTO vClientDebt;
    
    INSERT INTO vn.greuge(clientFk, description, amount, shipped, greugeTypeFk)
		VALUES (vClientFk, 'Saldo pendiente', vClientDebt, util.VN_CURDATE(), 5);
    
    CALL vn.clientGreugeSpray(vClientFk, TRUE, '', TRUE);
    
    INSERT INTO vn.greuge(clientFk, description, amount, shipped, greugeTypeFk)
			SELECT vClientFk, 'Liquidación cliente', sum(amount), util.VN_CURDATE(), 5
				FROM vn.greuge
				WHERE clientFk = vClientFk;
                
	UPDATE vn.client 
		SET salesPersonFk = NULL 
		WHERE id = vClientFk;

END$$
DELIMITER ;
