USE `vn`;
CREATE 
     OR REPLACE ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `saleVolume` AS
    SELECT 
        `s`.`ticketFk` AS `ticketFk`,
        `s`.`id` AS `saleFk`,
        IFNULL(ROUND(((((`i`.`compression` * (GREATEST(`i`.`density`, 167) / 167)) * `ic`.`cm3`) * `s`.`quantity`) / 1000),
                        2),
                0) AS `litros`,
        `t`.`routeFk` AS `routeFk`,
        `t`.`shipped` AS `shipped`,
        (((`s`.`quantity` * `ic`.`cm3`) * `i`.`compression`) / 1000000) AS `volume`,
        ((((`s`.`quantity` * `ic`.`cm3`) * `i`.`compression`) * (GREATEST(`i`.`density`, 167) / 167)) / 1000000) AS `physicalWeight`,
        (((`s`.`quantity` * `ic`.`cm3`) * `i`.`density`) / 1000000) AS `weight`,
        (((`s`.`quantity` * `ic`.`cm3`) * `i`.`compression`) / 1000000) AS `physicalVolume`,
        ((((`s`.`quantity` * `ic`.`cm3`) * `t`.`zonePrice`) * `i`.`compression`) / `cb`.`volume`) AS `freight`
    FROM
        ((((`sale` `s`
        JOIN `item` `i` ON ((`i`.`id` = `s`.`itemFk`)))
        JOIN `ticket` `t` ON ((`t`.`id` = `s`.`ticketFk`)))
        JOIN `packaging` `cb` ON ((`cb`.`id` = '94')))
        JOIN `itemCost` `ic` ON (((`ic`.`itemFk` = `s`.`itemFk`)
				AND (`ic`.`warehouseFk` = `t`.`warehouseFk`))));
