2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`invoiceExpenseMake`(IN vInvoice INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
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 ;
|