DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `edi`.`ekt_add`(vPutOrderFk INT)
BEGIN
	
/**
 *  Añade ekt para las ordenes de compra de proveedores que no envian el ekt por email
 * 
 * @param vPutOrderFk PutOrderId de donde coger los datos
 **/
	
	INSERT INTO edi.ekt(entryYear, 
						deliveryNumber, 
						fec,
						hor,
						item,
						pac,
						qty,
						ori,
						cat,
						pro,
						pri,
						package,
						s1,
						s2,
						s3,
						s4,
						s5,
						s6,
						putOrderFk,
						`ref`,
						kop)
		SELECT year(util.VN_CURDATE()) entryYear,
				p.id deliveryNumber ,
				DATE(di.LatestDeliveryDateTime) fec,
				HOUR(di.LatestDeliveryDateTime) hor,
				i.longName item,
				sr.NumberOfItemsPerCask pac,
				p.quantity qty,
				o.code ori,
				i.category cat,
				mp.supplierFk pro,
				sr.Price pri,
				sr.EmbalageCode package,
				i.value5 s1,
				i.value6 s2,
				i.value7 s3,
				i.value8 s4,
				i.value9 s5,
				i.value10 s6,
				p.id putOrderFk,
				sr.Item_ArticleCode,
				sr.vmpID 
			FROM edi.putOrder p
				JOIN edi.supplyResponse sr ON sr.ID = p.supplyResponseID 
				JOIN edi.deliveryInformation di ON di.id = p.deliveryInformationID 
				JOIN edi.marketPlace mp ON mp.id = sr.MarketPlaceID 
				JOIN vn.item i ON i.supplyResponseFk = sr.ID 
				JOIN vn.origin o ON o.id = i.originFk 
			WHERE p.id = vPutOrderFk;
				
	CALL edi.ekt_load(LAST_INSERT_ID());
	
END$$
DELIMITER ;
