CREATE OR REPLACE DEFINER=`vn`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`saleVolume`
AS SELECT `s`.`ticketFk` AS `ticketFk`,
	`s`.`id` AS `saleFk`,
	round(`ic`.`cm3delivery` * `s`.`quantity` / 1000, 0) AS `litros`,
	`t`.`routeFk` AS `routeFk`,
	`t`.`shipped` AS `shipped`,
	`t`.`landed` AS `landed`,
	`s`.`quantity` * `ic`.`cm3delivery` / 1000000 AS `volume`,
	`s`.`quantity` * `ic`.`grams` / 1000 AS `physicalWeight`,
	`s`.`quantity` * `ic`.`cm3delivery` * greatest(
		`ic`.`grams` / `ic`.`cm3delivery`,
		`vc`.`aerealVolumetricDensity`
	) / 1000000 AS `weight`,
	`s`.`quantity` * `ic`.`cm3delivery` / 1000000 AS `physicalVolume`,
	`s`.`quantity` * `ic`.`cm3delivery` * IFNULL(
		`t`.`zonePrice` - IFNULL(`t`.`zoneBonus`, 0),
		`z`.`price` - `z`.`bonus`
	) / (`vc`.`standardFlowerBox` * 1000) * `z`.`inflation` AS `freight`,
	`t`.`zoneFk` AS `zoneFk`,
	`t`.`clientFk` AS `clientFk`,
	`s`.`isPicked` AS `isPicked`,
	`s`.`quantity` * `s`.`price` * (100 - `s`.`discount`) / 100 AS `eurosValue`,
	`i`.`itemPackingTypeFk` AS `itemPackingTypeFk`
FROM (
		(
			(
				(
					(
						`vn`.`sale` `s`
						JOIN `vn`.`item` `i` ON(`i`.`id` = `s`.`itemFk`)
					)
					JOIN `vn`.`ticket` `t` ON(`t`.`id` = `s`.`ticketFk`)
				)
				JOIN `vn`.`zone` `z` ON(`z`.`id` = `t`.`zoneFk`)
			)
			JOIN `vn`.`volumeConfig` `vc`
		)
		JOIN `vn`.`itemCost` `ic` FORCE INDEX (`PRIMARY`) ON(
			`ic`.`itemFk` = `s`.`itemFk`
			AND `ic`.`warehouseFk` = `t`.`warehouseFk`
		)
	)
WHERE `s`.`quantity` > 0
