2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`invoiceOutBooking`(IN vInvoice INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
2024-05-16 16:48:59 +00:00
|
|
|
/**
|
|
|
|
* Asienta una factura emitida
|
|
|
|
*
|
|
|
|
* @param vInvoice Id invoiceOut
|
|
|
|
*/
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vBookNumber INT;
|
2024-05-16 16:48:59 +00:00
|
|
|
DECLARE vExpenseConcept VARCHAR(50);
|
|
|
|
DECLARE vSpainCountryFk INT;
|
|
|
|
DECLARE vOldBookNumber INT;
|
|
|
|
DECLARE vFiscalYear INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT id INTO vSpainCountryFk FROM country WHERE `code` = 'ES';
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT ASIEN
|
2024-01-15 11:31:03 +00:00
|
|
|
INTO vOldBookNumber
|
|
|
|
FROM XDiario x
|
|
|
|
JOIN invoiceOut io ON io.id = vInvoice
|
2024-05-16 16:48:59 +00:00
|
|
|
WHERE x.SERIE = io.serial
|
2024-01-15 11:31:03 +00:00
|
|
|
AND x.FACTURA = RIGHT(io.ref, LENGTH(io.ref) - 1)
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
DELETE
|
|
|
|
FROM XDiario
|
|
|
|
WHERE ASIEN = vOldBookNumber;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS rs;
|
|
|
|
CREATE TEMPORARY TABLE rs
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT c.accountingAccount clientBookingAccount,
|
|
|
|
io.amount totalAmount,
|
|
|
|
CONCAT('n/fra ', io.ref) simpleConcept,
|
|
|
|
CONCAT('n/fra ', io.ref, ' ', c.name) Concept,
|
|
|
|
io.serial SERIE,
|
|
|
|
io.issued FECHA_EX,
|
|
|
|
io.issued FECHA_OP,
|
|
|
|
io.issued FECHA,
|
|
|
|
1 NFACTICK,
|
|
|
|
IF(ic.correctingFk,'D','') TIPOOPE,
|
|
|
|
io.siiTrascendencyInvoiceOutFk TIPOCLAVE,
|
|
|
|
io.cplusTaxBreakFk TIPOEXENCI,
|
|
|
|
io.cplusSubjectOpFk TIPONOSUJE,
|
|
|
|
io.siiTypeInvoiceOutFk TIPOFACT,
|
|
|
|
ic.cplusRectificationTypeFk TIPORECTIF,
|
2024-01-15 11:31:03 +00:00
|
|
|
io.companyFk,
|
2024-05-16 16:48:59 +00:00
|
|
|
RIGHT(io.ref, LENGTH(io.ref) - 1) invoiceNum,
|
|
|
|
IF(c.countryFk = vSpainCountryFk, vSpainCountryFk, IF(ct.isUeeMember,2,4)) TERIDNIF,
|
|
|
|
CONCAT(IF(ct.isUeeMember AND c.countryFk <> vSpainCountryFk,ct.code,''),c.fi) TERNIF,
|
|
|
|
c.socialName TERNOM,
|
|
|
|
ior.serial SERIE_RT,
|
|
|
|
RIGHT(ior.ref, LENGTH(ior.ref) - 1) FACTU_RT,
|
|
|
|
ior.issued FECHA_RT,
|
|
|
|
IF(ior.id,TRUE,FALSE) RECTIFICA
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM invoiceOut io
|
|
|
|
JOIN invoiceOutSerial ios ON ios.code = io.serial
|
|
|
|
JOIN client c ON c.id = io.clientFk
|
|
|
|
JOIN country ct ON ct.id = c.countryFk
|
|
|
|
LEFT JOIN invoiceCorrection ic ON ic.correctingFk = io.id
|
2024-05-16 16:48:59 +00:00
|
|
|
LEFT JOIN invoiceOut ior ON ior.id = ic.correctedFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE io.id = vInvoice;
|
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT YEAR(FECHA) INTO vFiscalYear FROM rs LIMIT 1;
|
2024-06-03 18:16:27 +00:00
|
|
|
CALL ledger_nextTx(vFiscalYear, vBookNumber);
|
2024-01-15 11:31:03 +00:00
|
|
|
-- Linea del cliente
|
|
|
|
INSERT INTO XDiario(
|
2024-05-16 16:48:59 +00:00
|
|
|
ASIEN,
|
|
|
|
FECHA,
|
|
|
|
SUBCTA,
|
|
|
|
EURODEBE,
|
|
|
|
CONCEPTO,
|
|
|
|
FECHA_EX,
|
|
|
|
FECHA_OP,
|
|
|
|
empresa_id)
|
|
|
|
SELECT vBookNumber,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.FECHA,
|
2024-05-16 16:48:59 +00:00
|
|
|
rs.clientBookingAccount,
|
|
|
|
rs.totalAmount,
|
|
|
|
rs.simpleConcept,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.FECHA_EX,
|
|
|
|
rs.FECHA_OP,
|
2024-05-16 16:48:59 +00:00
|
|
|
rs.companyFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM rs;
|
|
|
|
|
|
|
|
-- Lineas de gasto
|
|
|
|
INSERT INTO XDiario(
|
2024-05-16 16:48:59 +00:00
|
|
|
ASIEN,
|
|
|
|
FECHA,
|
|
|
|
SUBCTA,
|
|
|
|
CONTRA,
|
|
|
|
EUROHABER,
|
|
|
|
CONCEPTO,
|
|
|
|
FECHA_EX,
|
|
|
|
FECHA_OP,
|
|
|
|
empresa_id)
|
|
|
|
SELECT vBookNumber,
|
|
|
|
rs.FECHA,
|
|
|
|
ioe.expenseFk,
|
|
|
|
rs.clientBookingAccount,
|
|
|
|
ioe.amount,
|
|
|
|
rs.Concept,
|
|
|
|
rs.FECHA_EX,
|
|
|
|
rs.FECHA_OP,
|
|
|
|
rs.companyFk
|
|
|
|
FROM rs
|
|
|
|
JOIN invoiceOutExpense ioe
|
|
|
|
WHERE ioe.invoiceOutFk = vInvoice;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT GROUP_CONCAT(`name` SEPARATOR ',')
|
|
|
|
INTO vExpenseConcept
|
|
|
|
FROM expense e
|
|
|
|
JOIN invoiceOutExpense ioe ON ioe.expenseFk = e.id
|
|
|
|
WHERE ioe.invoiceOutFk = vInvoice;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
-- Lineas de IVA
|
2024-01-15 11:31:03 +00:00
|
|
|
INSERT INTO XDiario(
|
2024-05-16 16:48:59 +00:00
|
|
|
ASIEN,
|
|
|
|
FECHA,
|
|
|
|
SUBCTA,
|
|
|
|
CONTRA,
|
|
|
|
EUROHABER,
|
|
|
|
BASEEURO,
|
|
|
|
CONCEPTO,
|
|
|
|
FACTURA,
|
|
|
|
IVA,
|
|
|
|
RECEQUIV,
|
|
|
|
AUXILIAR,
|
|
|
|
SERIE,
|
|
|
|
SERIE_RT,
|
|
|
|
FACTU_RT,
|
|
|
|
RECTIFICA,
|
|
|
|
FECHA_RT,
|
|
|
|
FECHA_OP,
|
|
|
|
FECHA_EX,
|
|
|
|
TIPOOPE,
|
|
|
|
NFACTICK,
|
|
|
|
TERIDNIF,
|
|
|
|
TERNIF,
|
|
|
|
TERNOM,
|
|
|
|
L340,
|
|
|
|
TIPOCLAVE,
|
|
|
|
TIPOEXENCI,
|
|
|
|
TIPONOSUJE,
|
|
|
|
TIPOFACT,
|
|
|
|
TIPORECTIF,
|
|
|
|
empresa_id)
|
|
|
|
SELECT vBookNumber ASIEN,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.FECHA,
|
2024-05-16 16:48:59 +00:00
|
|
|
iot.pgcFk SUBCTA,
|
|
|
|
rs.clientBookingAccount CONTRA,
|
|
|
|
iot.vat EUROHABER,
|
|
|
|
iot.taxableBase BASEEURO,
|
|
|
|
CONCAT(vExpenseConcept,' : ',rs.Concept) CONCEPTO,
|
|
|
|
rs.invoiceNum FACTURA,
|
|
|
|
IF(pe2.equFk,0,pgc.rate) IVA,
|
|
|
|
IF(pe2.equFk,0,pgce.rate) RECEQUIV,
|
|
|
|
IF(pgc.mod347,'','*') AUXILIAR,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.SERIE,
|
|
|
|
rs.SERIE_RT,
|
|
|
|
rs.FACTU_RT,
|
|
|
|
rs.RECTIFICA,
|
|
|
|
rs.FECHA_RT,
|
|
|
|
rs.FECHA_OP,
|
|
|
|
rs.FECHA_EX,
|
|
|
|
rs.TIPOOPE,
|
2024-05-16 16:48:59 +00:00
|
|
|
rs.NFACTICK,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.TERIDNIF,
|
|
|
|
rs.TERNIF,
|
|
|
|
rs.TERNOM,
|
2024-05-16 16:48:59 +00:00
|
|
|
pgc.mod340 L340,
|
|
|
|
pgc.siiTrascendencyInvoiceOutFk TIPOCLAVE,
|
|
|
|
pgc.cplusTaxBreakFk TIPOEXENCI,
|
2024-01-15 11:31:03 +00:00
|
|
|
rs.TIPONOSUJE,
|
|
|
|
rs.TIPOFACT,
|
|
|
|
rs.TIPORECTIF,
|
2024-05-16 16:48:59 +00:00
|
|
|
rs.companyFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM rs
|
|
|
|
JOIN invoiceOutTax iot
|
|
|
|
JOIN pgc ON pgc.code = iot.pgcFk
|
|
|
|
LEFT JOIN pgcEqu pe ON pe.vatFk = iot.pgcFk -- --------------- Comprueba si la linea es de iva con rec.equiv. asociado
|
|
|
|
LEFT JOIN pgc pgce ON pgce.code = pe.equFk
|
2024-05-16 16:48:59 +00:00
|
|
|
LEFT JOIN pgcEqu pe2 ON pe2.equFk = iot.pgcFk -- --------------- Comprueba si la linea es de rec.equiv.
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE iot.invoiceOutFk = vInvoice;
|
2024-05-16 16:48:59 +00:00
|
|
|
|
|
|
|
UPDATE invoiceOut
|
|
|
|
SET booked = util.VN_CURDATE()
|
|
|
|
WHERE id = vInvoice;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|