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`.`invoiceInDueDay_calculate`(
|
2024-07-29 05:18:23 +00:00
|
|
|
vInvoiceInFk INT
|
2024-07-15 15:53:23 +00:00
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Calcula los vctos. de una factura recibida
|
|
|
|
* En caso de haberlos informado previamente o que el proveedor sea español, no los recalculará
|
|
|
|
*
|
|
|
|
* @param vInvoiceInFk Factura recibida
|
|
|
|
*/
|
|
|
|
DECLARE vCompanyCountryCode VARCHAR(3);
|
|
|
|
DECLARE vHasDueDayTax BOOL;
|
|
|
|
DECLARE vHasDueDay BOOL;
|
|
|
|
|
|
|
|
SELECT c.code, IFNULL(a.isUeeMember, TRUE AND co.code ='ES')
|
|
|
|
INTO vCompanyCountryCode, vHasDueDayTax
|
|
|
|
FROM invoiceIn ii
|
|
|
|
JOIN supplier su ON su.id = ii.companyFk
|
|
|
|
LEFT JOIN country c ON c.id = su.countryFk
|
|
|
|
LEFT JOIN supplier s ON s.id = ii.supplierFk
|
|
|
|
LEFT JOIN country co ON co.id = s.countryFk
|
|
|
|
LEFT JOIN province p ON p.id = s.provinceFk
|
|
|
|
LEFT JOIN autonomy a ON a.id = p.autonomyFk
|
|
|
|
WHERE ii.id = vInvoiceInFk;
|
|
|
|
|
|
|
|
SELECT COUNT(*) INTO vHasDueDay
|
|
|
|
FROM invoiceInDueDay iid
|
|
|
|
WHERE iid.invoiceInFk = vInvoiceInFk;
|
|
|
|
|
|
|
|
IF NOT vHasDueDay AND vCompanyCountryCode = 'ES' THEN
|
|
|
|
|
|
|
|
SET @cont := 0;
|
|
|
|
INSERT INTO invoiceInDueDay (
|
|
|
|
invoiceInFk,
|
|
|
|
dueDated,
|
|
|
|
amount,
|
|
|
|
foreignValue
|
|
|
|
)
|
|
|
|
SELECT vInvoiceInFk,
|
|
|
|
IF(payDay,
|
|
|
|
IF(getNextDueDate(issued, pdd.detail, payDay) < created,
|
|
|
|
created,
|
|
|
|
getNextDueDate(issued, pdd.detail, payDay)),
|
|
|
|
GREATEST(created, issued + INTERVAL pdd.detail DAY)),
|
|
|
|
IF((@cont := @cont + 1) < cont,
|
|
|
|
TRUNCATE(venc / cont, 2),
|
|
|
|
venc - (TRUNCATE(venc / cont, 2) * (cont - 1))),
|
|
|
|
IF(@cont < cont,
|
|
|
|
TRUNCATE(foreignValue / cont, 2),
|
|
|
|
foreignValue - (TRUNCATE(foreignValue / cont, 2) * (cont - 1)))
|
|
|
|
FROM (
|
|
|
|
SELECT SUM((1 + (IFNULL(ti.PorcentajeIva, 0) / 100) * vHasDueDayTax)
|
|
|
|
* iit.taxableBase) / COUNT(DISTINCT(pdd.detail)) venc,
|
|
|
|
SUM(iit.foreignValue) / COUNT(DISTINCT(pdd.detail)) foreignValue,
|
|
|
|
s.payDemFk,
|
|
|
|
ii.companyFk,
|
|
|
|
COUNT(DISTINCT(pdd.detail)) cont,
|
|
|
|
s.payDay,
|
|
|
|
ii.issued,
|
2024-07-15 15:53:23 +00:00
|
|
|
DATE(ii.created) + INTERVAL iic.dueDateMarginDays DAY created
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM invoiceIn ii
|
|
|
|
JOIN invoiceInTax iit ON iit.invoiceInFk = ii.id
|
|
|
|
LEFT JOIN sage.TiposIva ti ON ti.CodigoIva= iit.taxTypeSageFk
|
|
|
|
JOIN supplier s ON s.id = ii.supplierFk
|
2024-07-15 15:53:23 +00:00
|
|
|
JOIN payDemDetail pdd ON pdd.id = s.payDemFk
|
|
|
|
JOIN invoiceInConfig iic
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE ii.id = vInvoiceInFk
|
|
|
|
GROUP BY ii.id
|
|
|
|
)sub
|
|
|
|
JOIN payDemDetail pdd ON pdd.id = sub.payDemFk
|
|
|
|
GROUP BY pdd.detail;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|