DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`item_getPackage`(vItemFk INT)
	RETURNS varchar(50) CHARSET utf8mb3 COLLATE utf8mb3_unicode_ci
	DETERMINISTIC
BEGIN
	
		DECLARE vDiameter INT;
		DECLARE vPackageFk VARCHAR(50);
		DECLARE vCoeficient DOUBLE DEFAULT 1.08;
	
		SELECT MAX(LEFT(value,3)) INTO vDiameter
			FROM vn.itemTag it 
				JOIN vn.tag t ON t.id = it.tagFk
			WHERE it.itemFk = vItemFk
				AND t.overwrite = 'diameter';
	
		SELECT 8000 + FLOOR(vc.ccLength * vc.ccWidth / (vCoeficient * PI() * (vDiameter / 2) * (vDiameter / 2)))
			INTO vPackageFk
			FROM vn.volumeConfig vc;
		
		INSERT IGNORE INTO vn.packaging(id, width, `depth`)
			SELECT vPackageFk, 
					FLOOR(vc.ccWidth / FLOOR(vc.ccWidth / vDiameter)), 
					FLOOR(vc.ccLength / FLOOR(vc.ccLength / vDiameter))
			FROM vn.volumeConfig vc;
			
		RETURN vPackageFk;
	
END$$
DELIMITER ;
