DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`emailYesterdayPurchasesByConsigna`(IN v_Date DATE, IN v_Client_Id INT) BEGIN DECLARE MyIdTicket BIGINT; DECLARE MyAlias VARCHAR(50); DECLARE MyDomicilio VARCHAR(255); DECLARE MyPoblacion VARCHAR(25); DECLARE MyImporte DOUBLE; DECLARE MyMailTo VARCHAR(250); DECLARE MyMailReplyTo VARCHAR(250); DECLARE done INT DEFAULT FALSE; DECLARE emptyList INT DEFAULT 0; DECLARE txt TEXT; DECLARE rs CURSOR FOR SELECT t.Id_Ticket, Alias, cast(amount as decimal(10,2)) Importe, Domicilio, POBLACION FROM Tickets t JOIN Consignatarios cs ON t.Id_Consigna = cs.Id_Consigna JOIN v_ticket_amount v ON v.Id_Ticket = t.Id_Ticket WHERE t.Fecha BETWEEN v_Date AND util.dayEnd(v_Date) AND t.Id_Cliente = v_Client_Id; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET v_Date = IFNULL(v_Date, util.yesterday()); OPEN rs; FETCH rs INTO MyIdTicket, MyAlias, MyImporte, MyDomicilio, MyPoblacion; SET emptyList = done; SET txt = CONCAT('

', '

Relación de envíos.

', '

Dia: ', v_Date, '

'); WHILE NOT done DO SET txt = CONCAT(txt, '

', '
Ticket ', MyIdTicket,' ', MyImporte, ' €
' , ' ', MyAlias, '
' , ' ', MyDomicilio, '(', MyPoblacion, ')'); FETCH rs INTO MyIdTicket, MyAlias, MyImporte, MyDomicilio, MyPoblacion; END WHILE; SET txt = CONCAT( txt, '', '', '
', '

Puede acceder al detalle de los albaranes haciendo click sobre el número de Ticket', '

Muchas gracias por su confianza

', '

'); -- Envío del email IF emptyList = 0 THEN SELECT CONCAT(`e-mail`,',pako@verdnatura.es') INTO MyMailTo FROM Clientes WHERE Id_Cliente = v_Client_Id AND `e-mail`>''; IF v_Client_Id = 7818 THEN -- LOEWE SET MyMailTo = 'isabel@elisabethblumen.com,emunozca@loewe.es,pako@verdnatura.es'; END IF; CALL vn.mail_insert( IFNULL(MyMailTo,'pako.natek@gmail.com'), 'pako@verdnatura.es', 'Resumen de pedidos preparados', txt ); END IF; END$$ DELIMITER ;