feat: company_getSuppliersDebtHotFix #2314

Merged
robert merged 1 commits from company_getSuppliersDebtHotFix into master 2024-04-17 10:08:01 +00:00
1 changed files with 180 additions and 180 deletions
Showing only changes of commit 0a4cd4d4d0 - Show all commits

View File

@ -7,195 +7,195 @@ BEGIN
* @param vSelf company id * @param vSelf company id
* @param vMonthAgo time interval to be consulted * @param vMonthAgo time interval to be consulted
*/ */
DECLARE vStartingDate DATETIME DEFAULT TIMESTAMPADD (MONTH,- vMonthsAgo,util.VN_CURDATE()); DECLARE vStartingDate DATETIME DEFAULT TIMESTAMPADD (MONTH,- vMonthsAgo,util.VN_CURDATE());
DECLARE vCurrencyEuroFk INT; DECLARE vCurrencyEuroFk INT;
DECLARE vStartDate DATE; DECLARE vStartDate DATE;
DECLARE vInvalidBalances DOUBLE; DECLARE vInvalidBalances DOUBLE;
SELECT dated, invalidBalances INTO vStartDate, vInvalidBalances FROM supplierDebtConfig; SELECT dated, invalidBalances INTO vStartDate, vInvalidBalances FROM supplierDebtConfig;
SELECT id INTO vCurrencyEuroFk FROM currency WHERE code = 'EUR'; SELECT id INTO vCurrencyEuroFk FROM currency WHERE code = 'EUR';
DROP TEMPORARY TABLE IF EXISTS tOpeningBalances; DROP TEMPORARY TABLE IF EXISTS tOpeningBalances;
CREATE TEMPORARY TABLE tOpeningBalances ( CREATE TEMPORARY TABLE tOpeningBalances (
supplierFk INT NOT NULL, supplierFk INT NOT NULL,
companyFk INT NOT NULL, companyFk INT NOT NULL,
openingBalances DOUBLE NOT NULL, openingBalances DOUBLE NOT NULL,
closingBalances DOUBLE NOT NULL, closingBalances DOUBLE NOT NULL,
currencyFk INT NOT NULL, currencyFk INT NOT NULL,
PRIMARY KEY (supplierFk, companyFk, currencyFk) PRIMARY KEY (supplierFk, companyFk, currencyFk)
) ENGINE = MEMORY; ) ENGINE = MEMORY;
-- Calculates the opening and closing balance for each supplier -- Calculates the opening and closing balance for each supplier
INSERT INTO tOpeningBalances INSERT INTO tOpeningBalances
SELECT supplierFk, SELECT supplierFk,
companyFk, companyFk,
SUM(amount * isBeforeStarting) AS openingBalances, SUM(amount * isBeforeStarting) AS openingBalances,
SUM(amount) closingBalances, SUM(amount) closingBalances,
currencyFk currencyFk
FROM ( FROM (
SELECT p.supplierFk, SELECT p.supplierFk,
p.companyFk, p.companyFk,
IF (p.currencyFk = vCurrencyEuroFk, p.amount, p.divisa) AS amount, IF (p.currencyFk = vCurrencyEuroFk, p.amount, p.divisa) AS amount,
p.dueDated < vStartingDate isBeforeStarting, p.dueDated < vStartingDate isBeforeStarting,
p.currencyFk p.currencyFk
FROM payment p FROM payment p
WHERE p.received > vStartDate WHERE p.received > vStartDate
AND p.companyFk = vSelf AND p.companyFk = vSelf
UNION ALL UNION ALL
SELECT r.supplierFk, SELECT r.supplierFk,
r.companyFk, r.companyFk,
- IF (r.currencyFk = vCurrencyEuroFk, rv.amount, rv.foreignValue) AS Total, - IF (r.currencyFk = vCurrencyEuroFk, rv.amount, rv.foreignValue) AS Total,
rv.dueDated < vStartingDate isBeforeStarting, rv.dueDated < vStartingDate isBeforeStarting,
r.currencyFk r.currencyFk
FROM invoiceIn r FROM invoiceIn r
INNER JOIN invoiceInDueDay rv ON r.id = rv.invoiceInFk INNER JOIN invoiceInDueDay rv ON r.id = rv.invoiceInFk
WHERE r.issued > vStartDate WHERE r.issued > vStartDate
AND r.isBooked AND r.isBooked
AND r.companyFk = vSelf AND r.companyFk = vSelf
) sub GROUP BY companyFk, supplierFk, currencyFk; ) sub GROUP BY companyFk, supplierFk, currencyFk;
DROP TEMPORARY TABLE IF EXISTS tPendingDuedates; DROP TEMPORARY TABLE IF EXISTS tPendingDuedates;
CREATE TEMPORARY TABLE tPendingDuedates ( CREATE TEMPORARY TABLE tPendingDuedates (
id INT auto_increment, id INT auto_increment,
expirationId INT, expirationId INT,
dated DATE, dated DATE,
supplierFk INT NOT NULL, supplierFk INT NOT NULL,
companyFk INT NOT NULL, companyFk INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL, amount DECIMAL(10, 2) NOT NULL,
currencyFk INT NOT NULL, currencyFk INT NOT NULL,
pending DECIMAL(10, 2) DEFAULT 0, pending DECIMAL(10, 2) DEFAULT 0,
balance DECIMAL(10, 2) DEFAULT 0, balance DECIMAL(10, 2) DEFAULT 0,
endingBalance DECIMAL(10, 2) DEFAULT 0, endingBalance DECIMAL(10, 2) DEFAULT 0,
isPayment BOOLEAN, isPayment BOOLEAN,
isReconciled BOOLEAN, isReconciled BOOLEAN,
PRIMARY KEY (id), PRIMARY KEY (id),
INDEX (supplierFk, companyFk, currencyFk) INDEX (supplierFk, companyFk, currencyFk)
) ENGINE = MEMORY; ) ENGINE = MEMORY;
INSERT INTO tPendingDuedates ( INSERT INTO tPendingDuedates (
expirationId, expirationId,
dated, dated,
supplierFk, supplierFk,
companyFk, companyFk,
amount, amount,
currencyFk, currencyFk,
isPayment, isPayment,
isReconciled isReconciled
)SELECT p.id, )SELECT p.id,
p.dueDated, p.dueDated,
p.supplierFk, p.supplierFk,
p.companyFk, p.companyFk,
IF (p.currencyFk = vCurrencyEuroFk, p.amount, p.divisa), IF (p.currencyFk = vCurrencyEuroFk, p.amount, p.divisa),
p.currencyFk, p.currencyFk,
TRUE isPayment, TRUE isPayment,
p.isConciliated p.isConciliated
FROM payment p FROM payment p
WHERE p.dueDated >= vStartingDate WHERE p.dueDated >= vStartingDate
AND p.companyFk = vSelf AND p.companyFk = vSelf
UNION ALL UNION ALL
SELECT r.id, SELECT r.id,
rv.dueDated, rv.dueDated,
r.supplierFk, r.supplierFk,
r.companyFk, r.companyFk,
-IF (r.currencyFk = vCurrencyEuroFk, rv.amount, rv.foreignValue), -IF (r.currencyFk = vCurrencyEuroFk, rv.amount, rv.foreignValue),
r.currencyFk, r.currencyFk,
FALSE isPayment, FALSE isPayment,
TRUE TRUE
FROM invoiceIn r FROM invoiceIn r
LEFT JOIN tOpeningBalances si ON r.companyFk = si.companyFk LEFT JOIN tOpeningBalances si ON r.companyFk = si.companyFk
AND r.supplierFk = si.supplierFk AND r.supplierFk = si.supplierFk
AND r.currencyFk = si.currencyFk AND r.currencyFk = si.currencyFk
JOIN invoiceInDueDay rv ON r.id = rv.invoiceInFk JOIN invoiceInDueDay rv ON r.id = rv.invoiceInFk
WHERE rv.dueDated >= vStartingDate WHERE rv.dueDated >= vStartingDate
AND (si.closingBalances IS NULL OR si.closingBalances <> 0) AND (si.closingBalances IS NULL OR si.closingBalances <> 0)
AND r.isBooked AND r.isBooked
AND r.companyFk = vSelf AND r.companyFk = vSelf
ORDER BY supplierFk, companyFk, companyFk, dueDated, isPayment DESC, id; ORDER BY supplierFk, companyFk, companyFk, dueDated, isPayment DESC, id;
-- Now, we calculate the outstanding amount for each receipt in descending order -- Now, we calculate the outstanding amount for each receipt in descending order
SET @risk := 0.0; SET @risk := 0.0;
SET @supplier := 0.0; SET @supplier := 0.0;
SET @company := 0.0; SET @company := 0.0;
SET @moneda := 0.0; SET @moneda := 0.0;
SET @pending := 0.0; SET @pending := 0.0;
SET @day := util.VN_CURDATE(); SET @day := util.VN_CURDATE();
UPDATE tPendingDuedates vp UPDATE tPendingDuedates vp
LEFT JOIN tOpeningBalances si ON vp.companyFk = si.companyFk LEFT JOIN tOpeningBalances si ON vp.companyFk = si.companyFk
AND vp.supplierFk = si.supplierFk AND vp.supplierFk = si.supplierFk
AND vp.currencyFk = si.currencyFk AND vp.currencyFk = si.currencyFk
SET vp.balance = @risk := ( SET vp.balance = @risk := (
IF ( IF (
@company <> vp.companyFk @company <> vp.companyFk
OR @supplier <> vp.supplierFk OR @supplier <> vp.supplierFk
OR @moneda <> vp.currencyFk, OR @moneda <> vp.currencyFk,
IFNULL(si.openingBalances, 0), IFNULL(si.openingBalances, 0),
@risk @risk
) + ) +
vp.amount vp.amount
), ),
-- if there is a change of company or supplier or currency, the balance is reset -- if there is a change of company or supplier or currency, the balance is reset
vp.pending = @pending := IF ( vp.pending = @pending := IF (
@company <> vp.companyFk @company <> vp.companyFk
OR @supplier <> vp.supplierFk OR @supplier <> vp.supplierFk
OR @moneda <> vp.currencyFk OR @moneda <> vp.currencyFk
OR @day <> vp.dated, OR @day <> vp.dated,
vp.amount * (NOT vp.isPayment), vp.amount * (NOT vp.isPayment),
@pending + vp.amount @pending + vp.amount
), ),
vp.companyFk = @company := vp.companyFk, vp.companyFk = @company := vp.companyFk,
vp.supplierFk = @supplier := vp.supplierFk, vp.supplierFk = @supplier := vp.supplierFk,
vp.currencyFk = @moneda := vp.currencyFk, vp.currencyFk = @moneda := vp.currencyFk,
vp.dated = @day := vp.dated, vp.dated = @day := vp.dated,
vp.balance = @risk, vp.balance = @risk,
vp.pending = @pending; vp.pending = @pending;
CREATE OR REPLACE TEMPORARY TABLE tRowsToDelete ENGINE = MEMORY CREATE OR REPLACE TEMPORARY TABLE tRowsToDelete ENGINE = MEMORY
SELECT expirationId, SELECT expirationId,
dated, dated,
supplierFk, supplierFk,
companyFk, companyFk,
currencyFk, currencyFk,
balance balance
FROM tPendingDuedates FROM tPendingDuedates
WHERE balance < vInvalidBalances WHERE balance < vInvalidBalances
AND balance > - vInvalidBalances; AND balance > - vInvalidBalances;
DELETE vp.* DELETE vp.*
FROM tPendingDuedates vp FROM tPendingDuedates vp
JOIN tRowsToDelete rd ON ( JOIN tRowsToDelete rd ON (
vp.dated < rd.dated vp.dated < rd.dated
OR (vp.dated = rd.dated AND vp.expirationId <= rd.expirationId) OR (vp.dated = rd.dated AND vp.expirationId <= rd.expirationId)
) )
AND vp.supplierFk = rd.supplierFk AND vp.supplierFk = rd.supplierFk
AND vp.companyFk = rd.companyFk AND vp.companyFk = rd.companyFk
AND vp.currencyFk = rd.currencyFk AND vp.currencyFk = rd.currencyFk
WHERE NOT vp.isPayment; WHERE NOT vp.isPayment;
SELECT vp.expirationId, SELECT vp.expirationId,
vp.dated, vp.dated,
vp.supplierFk, vp.supplierFk,
vp.companyFk, vp.companyFk,
vp.currencyFk, vp.currencyFk,
vp.amount, vp.amount,
vp.pending, vp.pending,
vp.balance, vp.balance,
s.payMethodFk, s.payMethodFk,
vp.isPayment, vp.isPayment,
vp.isReconciled, vp.isReconciled,
vp.endingBalance, vp.endingBalance,
cr.amount clientRiskAmount, cr.amount clientRiskAmount,
co.CEE co.CEE
FROM tPendingDuedates vp FROM tPendingDuedates vp
LEFT JOIN supplier s ON s.id = vp.supplierFk LEFT JOIN supplier s ON s.id = vp.supplierFk
LEFT JOIN client c ON c.fi = s.nif LEFT JOIN client c ON c.fi = s.nif
LEFT JOIN clientRisk cr ON cr.clientFk = c.id JOIN clientRisk cr ON cr.clientFk = c.id
LEFT JOIN supplierAccount sa ON sa.supplierFk = s.id AND cr.companyFk = vp.companyFk
LEFT JOIN bankEntity be ON be.id = sa.bankEntityFk LEFT JOIN supplierAccount sa ON sa.supplierFk = s.id
LEFT JOIN country co ON co.id = be.countryFk LEFT JOIN bankEntity be ON be.id = sa.bankEntityFk
AND cr.companyFk = vp.companyFk; LEFT JOIN country co ON co.id = be.countryFk;
DROP TEMPORARY TABLE tOpeningBalances; DROP TEMPORARY TABLE tOpeningBalances;
DROP TEMPORARY TABLE tPendingDuedates; DROP TEMPORARY TABLE tPendingDuedates;
DROP TEMPORARY TABLE tRowsToDelete; DROP TEMPORARY TABLE tRowsToDelete;
END$$ END$$
DELIMITER ; DELIMITER ;