salix/db/changes/10300-newFacility/00-saleValue.sql

27 lines
2.2 KiB
SQL

CREATE OR REPLACE DEFINER = root@`%` VIEW `vn`.`saleValue` AS
SELECT `wh`.`name` AS `warehouse`,
`c`.`name` AS `client`,
`c`.`typeFk` AS `clientTypeFk`,
`u`.`name` AS `buyer`,
`it`.`id` AS `itemTypeFk`,
`it`.`name` AS `family`,
`s`.`itemFk` AS `itemFk`,
`s`.`concept` AS `concept`,
`s`.`quantity` AS `quantity`,
`b`.`buyingValue` + `b`.`freightValue` + `b`.`comissionValue` + `b`.`packageValue` AS `cost`,
(`b`.`buyingValue` + `b`.`freightValue` + `b`.`comissionValue` + `b`.`packageValue`) * `s`.`quantity` AS `value`,
`tm`.`year` AS `year`,
`tm`.`week` AS `week`
FROM `vn`.`sale` `s`
JOIN `vn`.`item` `i` ON `i`.`id` = `s`.`itemFk`
JOIN `vn`.`itemType` `it` ON `it`.`id` = `i`.`typeFk`
JOIN `account`.`user` `u` ON `u`.`id` = `it`.`workerFk`
JOIN `vn`.`ticket` `t` ON `t`.`id` = `s`.`ticketFk`
JOIN `vn`.`client` `c` ON `c`.`id` = `t`.`clientFk`
JOIN `vn`.`warehouse` `wh` ON `wh`.`id` = `t`.`warehouseFk`
JOIN `vn`.`time` `tm` ON `tm`.`dated` = CAST(`t`.`shipped` AS DATE)
JOIN `cache`.`last_buy` `lb` ON `lb`.`item_id` = `i`.`id` AND `lb`.`warehouse_id` = `wh`.`id`
JOIN `vn`.`buy` `b` ON `b`.`id` = `lb`.`buy_id`
WHERE `wh`.`isManaged` <> 0;