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

48 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticketToInvoiceByRef`(IN vInvoiceRef VARCHAR(15))
BEGIN
/* Para tickets ya facturados, vuelve a repetir el proceso de facturación.
*
* @param vInvoiceRef Factura
*/
DECLARE vInvoice INT;
DECLARE vCountry INT;
DECLARE vTaxArea VARCHAR(15);
DECLARE vSpainCountryCode INT DEFAULT 1;
SELECT id
INTO vInvoice
FROM vn.invoiceOut
WHERE ref = vInvoiceRef;
SELECT s.countryFk
INTO vCountry
FROM vn.supplier s
JOIN vn.invoiceOut io ON io.companyFk = s.id
WHERE io.id = vInvoice;
SELECT IF(
c.isEqualizated
AND c.countryFk = vSpainCountryCode
AND i.taxAreaFk = 'NATIONAL',
'EQU',
i.taxAreaFk
)
INTO vTaxArea
FROM vn.invoiceOutSerial i
JOIN vn.invoiceOut io ON io.serial = i.code
JOIN vn.client c ON c.id = io.clientFk
WHERE io.id = vInvoice;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketToInvoice;
CREATE TEMPORARY TABLE tmp.ticketToInvoice
SELECT id
FROM vn.ticket
WHERE refFk = vInvoiceRef;
END$$
DELIMITER ;