53 lines
1.4 KiB
SQL
53 lines
1.4 KiB
SQL
DROP procedure IF EXISTS `vn`.`ticket_priceDifference`;
|
|
|
|
DELIMITER $$
|
|
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticket_priceDifference`(
|
|
vTicketFk INT,
|
|
vLanded DATE,
|
|
vAddressFk INT,
|
|
vZoneFk INT,
|
|
vWarehouseFk INT)
|
|
BEGIN
|
|
/**
|
|
* Devuelve las diferencias de precio
|
|
* de los movimientos de un ticket.
|
|
*
|
|
* @param vTicketFk Id del ticket
|
|
* @param vLanded Fecha de recepcion
|
|
* @param vAddressFk Id del consignatario
|
|
* @param vZoneFk Id de la zona
|
|
* @param vWarehouseFk Id del almacén
|
|
*/
|
|
CALL vn.ticket_componentPreview(vTicketFk, vLanded, vAddressFk, vZoneFk, vWarehouseFk);
|
|
|
|
SELECT s.itemFk,
|
|
i.name,
|
|
i.size,
|
|
i.category,
|
|
IFNULL(s.quantity, 0) AS quantity,
|
|
IFNULL(s.price, 0) AS price,
|
|
ROUND(SUM(tc.cost), 2) AS newPrice,
|
|
s.quantity * (s.price - ROUND(SUM(tc.cost), 2)) difference,
|
|
s.id AS saleFk
|
|
FROM sale s
|
|
JOIN item i ON i.id = s.itemFk
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
LEFT JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk
|
|
AND tc.warehouseFk = t.warehouseFk
|
|
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
|
|
AND sc.componentFk = tc.componentFk
|
|
LEFT JOIN componentRate cr ON cr.id = tc.componentFk
|
|
WHERE
|
|
t.id = vTicketFk
|
|
AND IF(sc.componentFk IS NULL
|
|
AND cr.classRate IS NOT NULL, FALSE, TRUE)
|
|
GROUP BY s.id ORDER BY s.id;
|
|
|
|
DROP TEMPORARY TABLE
|
|
tmp.ticketComponent,
|
|
tmp.ticketComponentPrice;
|
|
END$$
|
|
|
|
DELIMITER ;
|
|
|