salix/db/changes/231001/00-invoiceOut_getWeight.sql

33 lines
811 B
MySQL
Raw Normal View History

2023-02-09 10:03:22 +00:00
DROP FUNCTION IF EXISTS `vn`.`invoiceOut_getWeight`;
DELIMITER $$
$$
2023-03-14 08:34:41 +00:00
CREATE DEFINER=`root`@`localhost` FUNCTION `vn`.`invoiceOut_getWeight`(
vInvoiceRef VARCHAR(15)
)RETURNS decimal(10,2)
2023-02-09 10:03:22 +00:00
READS SQL DATA
BEGIN
/**
* Calcula el peso de una factura emitida
*
2023-03-14 08:34:41 +00:00
* @param vInvoiceRef referencia de la factura
2023-02-09 10:03:22 +00:00
* @return vTotalWeight peso de la factura
*/
DECLARE vTotalWeight DECIMAL(10,2);
SELECT SUM(CAST(IFNULL(i.stems, 1)
* s.quantity
* IF(ic.grams, ic.grams, IFNULL(i.weightByPiece, 0)) / 1000 AS DECIMAL(10,2)))
INTO vTotalWeight
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
JOIN itemCost ic ON ic.itemFk = i.id
AND ic.warehouseFk = t.warehouseFk
2023-03-14 08:34:41 +00:00
WHERE t.refFk = vInvoiceRef
2023-02-09 10:03:22 +00:00
AND i.intrastatFk;
RETURN vTotalWeight;
END$$
DELIMITER ;