2023-02-09 10:03:22 +00:00
|
|
|
DELIMITER $$
|
2024-01-15 11:31:03 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`invoiceOut_getWeight`(vInvoiceRef VARCHAR(15)
|
|
|
|
)
|
|
|
|
RETURNS decimal(10,2)
|
|
|
|
NOT DETERMINISTIC
|
|
|
|
READS SQL DATA
|
2023-02-09 10:03:22 +00:00
|
|
|
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 ;
|