DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`supplier_disablePayMethodChecked`()
BEGIN

/*
 * Deshabilita la comprobación de los datos bancarios de todos aquellos proveedores que tengan
 * un número que no sea 0 en el campo graceMonthsDisableChecked de la tabla vn.payMethod
 * y que no hayan recibido un pago desde el mes indicado
 */

	UPDATE vn.supplier s
		JOIN (	SELECT p.supplierFk
					FROM supplier s
						JOIN payMethod pm ON pm.id = s.payMethodFk
						JOIN (SELECT supplierFk,MAX(created) created 
								FROM payment 
								GROUP BY supplierFk) p ON p.supplierFk = s.id
					WHERE p.created < (util.VN_CURDATE() - INTERVAL pm.graceMonthsDisableChecked MONTH)
						AND pm.graceMonthsDisableChecked
						AND s.isPayMethodChecked
					GROUP BY p.supplierFk) sdpmc ON sdpmc.supplierFk = s.id
		SET s.isPayMethodChecked = FALSE;

END$$
DELIMITER ;
