DROP procedure IF EXISTS `vn`.`buy_notifyPassport`;

DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`buy_notifyPassport`(
	IN vBuyFk INT,
	IN vItemFk INT,
    IN vStickers SMALLINT,
    IN vPacking SMALLINT
)
BEGIN
	INSERT INTO vn.mail(`subject`,`body`,`sender`) 
		SELECT 'Solicitar pasaporte',
				CONCAT(
					'Etiquetas: ', IFNULL(vStickers, 0),
                    ', Packing: ', IFNULL(vPacking, 0),
                    ', Nombre: ', IFNULL(i.`name`, 0),
                    ', buy_edi: ', IFNULL(e.id, 0),
                    ', Nombre botánico: ', IFNULL(g.latin_genus_name, ''), ' ', IFNULL(s.latin_species_name, ''),
                    ', Productor: ',IFNULL(es.company_name, IFNULL(p.`name`, ''))
				)
				,'ekt@verdnatura.es' 
			FROM item i
				LEFT JOIN itemBotanical ib ON ib.itemFk = i.id
				LEFT JOIN edi.genus g ON g.genus_id = ib.genusFk
				LEFT JOIN edi.specie s ON IFNULL(s.specie_id, ib.specieFk) = ib.specieFk
				LEFT JOIN producer p ON p.id = i.producerFk
				LEFT JOIN buy b ON b.id = vBuyFk
				LEFT JOIN edi.ekt e ON b.ektFk = e.id
				LEFT JOIN edi.supplier es ON es.supplier_id = e.pro
			WHERE i.id = vItemFk; 
END$$

DELIMITER ;

