salix/db/routines/vn/procedures/invoiceExpenseMake.sql

31 lines
1002 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceExpenseMake`(IN vInvoice INT)
BEGIN
/* Inserta las partidas de gasto correspondientes a la factura
* REQUIERE tabla tmp.ticketToInvoice
* @param vInvoice Numero de factura
*/
DELETE FROM invoiceOutExpense
WHERE invoiceOutFk = vInvoice;
INSERT INTO invoiceOutExpense(invoiceOutFk, expenseFk, amount)
SELECT vInvoice,
expenseFk,
SUM(ROUND(quantity * price * (100 - discount)/100,2)) amount
FROM tmp.ticketToInvoice t
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
GROUP BY i.expenseFk
HAVING amount != 0;
INSERT INTO invoiceOutExpense(invoiceOutFk, expenseFk, amount)
SELECT vInvoice,
tst.expenseFk,
SUM(ROUND(ts.quantity * ts.price ,2)) amount
FROM tmp.ticketToInvoice t
JOIN ticketService ts ON ts.ticketFk = t.id
JOIN ticketServiceType tst ON tst.id = ts.ticketServiceTypeFk
HAVING amount != 0;
END$$
DELIMITER ;