DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`fustControlDetail`(vFromDated DATE, vToDated DATE)
BEGIN
	
	DECLARE vSijsnerClientFk INT DEFAULT 19752;

	DECLARE vDateStart DATETIME;
	DECLARE vDateEnd DATETIME;

	SET vDateStart = vFromDated;
	SET vDateEnd = util.Dayend(vToDated);
	
	SELECT a.nickname shopName,
			a.city , 
			IFNULL(pe.equivalentFk ,tp.packagingFk) FustCode, 
			tp.quantity,
			tp.ticketFk,
			CONCAT('From ', vFromDated,' to ', vToDated) AS dateRange
				FROM vn.ticketPackaging tp 
					JOIN vn.ticket t ON t.id = tp.ticketFk 
					JOIN vn.warehouse w ON w.id = t.warehouseFk 
					JOIN vn.warehouseAlias wa ON wa.id = w.aliasFk 
					JOIN vn.packaging p ON p.id = tp.packagingFk 
					LEFT JOIN vn.packageEquivalent pe ON pe.packagingFk = p.id
					JOIN vn.address a ON a.id = t.addressFk 
					JOIN vn.province p2 ON p2.id = a.provinceFk 
					JOIN vn.country c ON c.id = p2.countryFk 
				WHERE t.shipped BETWEEN vFromDated AND util.dayend(vToDated)
					AND wa.name = 'VNH'
					AND p.isPackageReturnable 
					AND c.country = 'FRANCIA'
					AND t.clientFk != vSijsnerClientFk
					AND tp.quantity > 0;
				
END$$
DELIMITER ;
