salix/db/.archive/231001/02-invoiceOut_new.sql

259 lines
7.4 KiB
MySQL
Raw Normal View History

2023-02-23 08:25:23 +00:00
DROP PROCEDURE IF EXISTS `vn`.`invoiceOut_new`;
DELIMITER $$
$$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceOut_new`(
vSerial VARCHAR(255),
vInvoiceDate DATE,
vTaxArea VARCHAR(25),
OUT vNewInvoiceId INT)
BEGIN
/**
* Creación de facturas emitidas.
2023-04-17 06:44:24 +00:00
* requiere previamente tabla tmp.ticketToInvoice(id).
2023-02-23 08:25:23 +00:00
*
* @param vSerial serie a la cual se hace la factura
* @param vInvoiceDate fecha de la factura
* @param vTaxArea tipo de iva en relacion a la empresa y al cliente
* @param vNewInvoiceId id de la factura que se acaba de generar
* @return vNewInvoiceId
2023-04-17 06:44:24 +00:00
*/
2023-02-23 08:25:23 +00:00
DECLARE vIsAnySaleToInvoice BOOL;
DECLARE vIsAnyServiceToInvoice BOOL;
DECLARE vNewRef VARCHAR(255);
DECLARE vWorker INT DEFAULT account.myUser_getId();
DECLARE vCompanyFk INT;
DECLARE vInterCompanyFk INT;
DECLARE vClientFk INT;
DECLARE vCplusStandardInvoiceTypeFk INT DEFAULT 1;
DECLARE vCplusCorrectingInvoiceTypeFk INT DEFAULT 6;
DECLARE vCplusSimplifiedInvoiceTypeFk INT DEFAULT 2;
DECLARE vCorrectingSerial VARCHAR(1) DEFAULT 'R';
DECLARE vSimplifiedSerial VARCHAR(1) DEFAULT 'S';
DECLARE vNewInvoiceInFk INT;
DECLARE vIsInterCompany BOOL DEFAULT FALSE;
DECLARE vIsCEESerial BOOL DEFAULT FALSE;
DECLARE vIsCorrectInvoiceDate BOOL;
DECLARE vMaxShipped DATE;
SET vInvoiceDate = IFNULL(vInvoiceDate, util.CURDATE());
2023-04-17 06:44:24 +00:00
SELECT t.clientFk,
t.companyFk,
2023-02-23 08:25:23 +00:00
MAX(DATE(t.shipped)),
DATE(vInvoiceDate) >= invoiceOut_getMaxIssued(
2023-04-17 06:44:24 +00:00
vSerial,
t.companyFk,
2023-02-23 08:25:23 +00:00
YEAR(vInvoiceDate))
2023-04-17 06:44:24 +00:00
INTO vClientFk,
2023-02-23 08:25:23 +00:00
vCompanyFk,
vMaxShipped,
vIsCorrectInvoiceDate
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice tt
2023-02-23 08:25:23 +00:00
JOIN ticket t ON t.id = tt.id;
2023-04-17 06:44:24 +00:00
IF(vMaxShipped > vInvoiceDate) THEN
2023-02-23 08:25:23 +00:00
CALL util.throw("Invoice date can't be less than max date");
END IF;
2023-04-17 06:44:24 +00:00
2023-02-23 08:25:23 +00:00
IF NOT vIsCorrectInvoiceDate THEN
CALL util.throw('Exists an invoice with a previous date');
END IF;
2023-04-17 06:44:24 +00:00
-- Eliminem de tmp.ticketToInvoice els tickets que no han de ser facturats
2023-02-23 08:25:23 +00:00
DELETE ti.*
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice ti
2023-02-23 08:25:23 +00:00
JOIN ticket t ON t.id = ti.id
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
JOIN supplier su ON su.id = t.companyFk
JOIN client c ON c.id = t.clientFk
LEFT JOIN itemTaxCountry itc ON itc.itemFk = i.id AND itc.countryFk = su.countryFk
2023-03-01 09:36:38 +00:00
WHERE (YEAR(t.shipped) < 2001 AND t.isDeleted)
2023-02-23 08:25:23 +00:00
OR c.isTaxDataChecked = FALSE
OR t.isDeleted
OR c.hasToInvoice = FALSE
OR itc.id IS NULL;
SELECT SUM(s.quantity * s.price * (100 - s.discount)/100) <> 0
INTO vIsAnySaleToInvoice
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice t
2023-02-23 08:25:23 +00:00
JOIN sale s ON s.ticketFk = t.id;
SELECT COUNT(*) > 0 INTO vIsAnyServiceToInvoice
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice t
2023-02-23 08:25:23 +00:00
JOIN ticketService ts ON ts.ticketFk = t.id;
IF (vIsAnySaleToInvoice OR vIsAnyServiceToInvoice)
AND (vCorrectingSerial = vSerial OR NOT hasAnyNegativeBase())
THEN
-- el trigger añade el siguiente Id_Factura correspondiente a la vSerial
INSERT INTO invoiceOut(
ref,
serial,
issued,
clientFk,
dued,
companyFk,
2023-11-09 13:33:46 +00:00
siiTypeInvoiceOutFk
2023-02-23 08:25:23 +00:00
)
SELECT
1,
vSerial,
vInvoiceDate,
vClientFk,
getDueDate(vInvoiceDate, dueDay),
vCompanyFk,
IF(vSerial = vCorrectingSerial,
vCplusCorrectingInvoiceTypeFk,
IF(vSerial = vSimplifiedSerial,
vCplusSimplifiedInvoiceTypeFk,
vCplusStandardInvoiceTypeFk))
FROM client
WHERE id = vClientFk;
SET vNewInvoiceId = LAST_INSERT_ID();
SELECT `ref`
INTO vNewRef
FROM invoiceOut
WHERE id = vNewInvoiceId;
UPDATE ticket t
2023-04-17 06:44:24 +00:00
JOIN tmp.ticketToInvoice ti ON ti.id = t.id
2023-02-23 08:25:23 +00:00
SET t.refFk = vNewRef;
DROP TEMPORARY TABLE IF EXISTS tmp.updateInter;
CREATE TEMPORARY TABLE tmp.updateInter ENGINE = MEMORY
SELECT s.id,ti.id ticket_id,vWorker Id_Trabajador
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice ti
2023-02-23 08:25:23 +00:00
LEFT JOIN ticketState ts ON ti.id = ts.ticket
JOIN state s
WHERE IFNULL(ts.alertLevel,0) < 3 and s.`code` = getAlert3State(ti.id);
INSERT INTO ticketTracking(stateFk,ticketFk,workerFk)
SELECT * FROM tmp.updateInter;
INSERT INTO ticketLog (action, userFk, originFk, description)
SELECT 'UPDATE', account.myUser_getId(), ti.id, CONCAT('Crea factura ', vNewRef)
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice ti;
2023-02-23 08:25:23 +00:00
CALL invoiceExpenceMake(vNewInvoiceId);
CALL invoiceTaxMake(vNewInvoiceId,vTaxArea);
UPDATE invoiceOut io
JOIN (
SELECT SUM(amount) total
FROM invoiceOutExpence
WHERE invoiceOutFk = vNewInvoiceId
) base
JOIN (
SELECT SUM(vat) total
FROM invoiceOutTax
WHERE invoiceOutFk = vNewInvoiceId
) vat
SET io.amount = base.total + vat.total
WHERE io.id = vNewInvoiceId;
DROP TEMPORARY TABLE tmp.updateInter;
2023-04-17 06:44:24 +00:00
SELECT COUNT(*), id
2023-02-23 08:25:23 +00:00
INTO vIsInterCompany, vInterCompanyFk
2023-04-17 06:44:24 +00:00
FROM company
2023-02-23 08:25:23 +00:00
WHERE clientFk = vClientFk;
2023-04-17 06:44:24 +00:00
2023-02-23 08:25:23 +00:00
IF (vIsInterCompany) THEN
INSERT INTO invoiceIn(supplierFk, supplierRef, issued, companyFk)
SELECT vCompanyFk, vNewRef, vInvoiceDate, vInterCompanyFk;
SET vNewInvoiceInFk = LAST_INSERT_ID();
DROP TEMPORARY TABLE IF EXISTS tmp.ticket;
CREATE TEMPORARY TABLE tmp.ticket
(KEY (ticketFk))
ENGINE = MEMORY
SELECT id ticketFk
2023-04-17 06:44:24 +00:00
FROM tmp.ticketToInvoice;
2023-02-23 08:25:23 +00:00
CALL `ticket_getTax`('NATIONAL');
SET @vTaxableBaseServices := 0.00;
SET @vTaxCodeGeneral := NULL;
INSERT INTO invoiceInTax(invoiceInFk, taxableBase, expenceFk, taxTypeSageFk, transactionTypeSageFk)
SELECT vNewInvoiceInFk,
@vTaxableBaseServices,
sub.expenceFk,
sub.taxTypeSageFk,
sub.transactionTypeSageFk
FROM (
SELECT @vTaxableBaseServices := SUM(tst.taxableBase) taxableBase,
i.expenceFk,
i.taxTypeSageFk,
i.transactionTypeSageFk,
@vTaxCodeGeneral := i.taxClassCodeFk
FROM tmp.ticketServiceTax tst
JOIN invoiceOutTaxConfig i ON i.taxClassCodeFk = tst.code
WHERE i.isService
HAVING taxableBase
) sub;
INSERT INTO invoiceInTax(invoiceInFk, taxableBase, expenceFk, taxTypeSageFk, transactionTypeSageFk)
2023-04-17 06:44:24 +00:00
SELECT vNewInvoiceInFk,
2023-02-23 08:25:23 +00:00
SUM(tt.taxableBase) - IF(tt.code = @vTaxCodeGeneral,
@vTaxableBaseServices, 0) taxableBase,
i.expenceFk,
i.taxTypeSageFk ,
i.transactionTypeSageFk
FROM tmp.ticketTax tt
JOIN invoiceOutTaxConfig i ON i.taxClassCodeFk = tt.code
WHERE !i.isService
GROUP BY tt.pgcFk
HAVING taxableBase
ORDER BY tt.priority;
CALL invoiceInDueDay_calculate(vNewInvoiceInFk);
2023-04-17 06:44:24 +00:00
SELECT COUNT(*) INTO vIsCEESerial
2023-02-23 08:25:23 +00:00
FROM invoiceOutSerial
WHERE code = vSerial;
IF vIsCEESerial THEN
2023-04-17 06:44:24 +00:00
2023-02-23 08:25:23 +00:00
INSERT INTO invoiceInIntrastat (
invoiceInFk,
intrastatFk,
amount,
stems,
countryFk,
net)
SELECT
vNewInvoiceInFk,
i.intrastatFk,
SUM(CAST((s.quantity * s.price * (100 - s.discount) / 100 ) AS DECIMAL(10, 2))),
SUM(CAST(IFNULL(i.stems, 1) * s.quantity AS DECIMAL(10, 2))),
su.countryFk,
CAST(SUM(IFNULL(i.stems, 1)
* s.quantity
* IF(ic.grams, ic.grams, IFNULL(i.weightByPiece, 0)) / 1000) AS DECIMAL(10, 2))
FROM sale s
JOIN ticket t ON s.ticketFk = t.id
JOIN supplier su ON su.id = t.companyFk
JOIN item i ON i.id = s.itemFk
LEFT JOIN itemCost ic ON ic.itemFk = i.id AND ic.warehouseFk = t.warehouseFk
WHERE t.refFk = vNewRef
GROUP BY i.intrastatFk;
END IF;
DROP TEMPORARY TABLE tmp.ticket;
DROP TEMPORARY TABLE tmp.ticketAmount;
DROP TEMPORARY TABLE tmp.ticketTax;
DROP TEMPORARY TABLE tmp.ticketServiceTax;
END IF;
END IF;
2023-04-17 06:44:24 +00:00
DROP TEMPORARY TABLE tmp.`ticketToInvoice`;
2023-02-23 08:25:23 +00:00
END$$
2023-04-17 06:44:24 +00:00
DELIMITER ;