Merge pull request 'feat: #7117 company_getSuppliersDebt devolver el pais del banco' (!2211) from 7117 into dev
Reviewed-on: #2211
This commit is contained in:
commit
585dd70a60
|
@ -7,81 +7,81 @@ 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,
|
||||||
|
@ -100,80 +100,80 @@ BEGIN
|
||||||
-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.*
|
|
||||||
FROM tPendingDuedates vp
|
|
||||||
JOIN tRowsToDelete rd ON (
|
|
||||||
vp.dated < rd.dated
|
|
||||||
OR (vp.dated = rd.dated AND vp.expirationId <= rd.expirationId)
|
|
||||||
)
|
|
||||||
AND vp.supplierFk = rd.supplierFk
|
|
||||||
AND vp.companyFk = rd.companyFk
|
|
||||||
AND vp.currencyFk = rd.currencyFk
|
|
||||||
WHERE vp.isPayment = FALSE;
|
|
||||||
|
|
||||||
SELECT vp.expirationId,
|
DELETE vp.*
|
||||||
vp.dated,
|
FROM tPendingDuedates vp
|
||||||
vp.supplierFk,
|
JOIN tRowsToDelete rd ON (
|
||||||
|
vp.dated < rd.dated
|
||||||
|
OR (vp.dated = rd.dated AND vp.expirationId <= rd.expirationId)
|
||||||
|
)
|
||||||
|
AND vp.supplierFk = rd.supplierFk
|
||||||
|
AND vp.companyFk = rd.companyFk
|
||||||
|
AND vp.currencyFk = rd.currencyFk
|
||||||
|
WHERE NOT vp.isPayment;
|
||||||
|
|
||||||
|
SELECT vp.expirationId,
|
||||||
|
vp.dated,
|
||||||
|
vp.supplierFk,
|
||||||
vp.companyFk,
|
vp.companyFk,
|
||||||
vp.currencyFk,
|
vp.currencyFk,
|
||||||
vp.amount,
|
vp.amount,
|
||||||
|
@ -183,15 +183,19 @@ BEGIN
|
||||||
vp.isPayment,
|
vp.isPayment,
|
||||||
vp.isReconciled,
|
vp.isReconciled,
|
||||||
vp.endingBalance,
|
vp.endingBalance,
|
||||||
cr.amount clientRiskAmount
|
cr.amount clientRiskAmount,
|
||||||
FROM tPendingDuedates vp
|
co.CEE
|
||||||
LEFT JOIN supplier s ON s.id = vp.supplierFk
|
FROM tPendingDuedates vp
|
||||||
LEFT JOIN client c ON c.fi = s.nif
|
LEFT JOIN supplier s ON s.id = vp.supplierFk
|
||||||
LEFT JOIN clientRisk cr ON cr.clientFk = c.id
|
LEFT JOIN client c ON c.fi = s.nif
|
||||||
AND cr.companyFk = vp.companyFk;
|
LEFT JOIN clientRisk cr ON cr.clientFk = c.id
|
||||||
|
LEFT JOIN supplierAccount sa ON sa.supplierFk = s.id
|
||||||
DROP TEMPORARY TABLE tOpeningBalances;
|
LEFT JOIN bankEntity be ON be.id = sa.bankEntityFk
|
||||||
DROP TEMPORARY TABLE tPendingDuedates;
|
LEFT JOIN country co ON co.id = be.countryFk
|
||||||
DROP TEMPORARY TABLE tRowsToDelete;
|
AND cr.companyFk = vp.companyFk;
|
||||||
|
|
||||||
|
DROP TEMPORARY TABLE tOpeningBalances;
|
||||||
|
DROP TEMPORARY TABLE tPendingDuedates;
|
||||||
|
DROP TEMPORARY TABLE tRowsToDelete;
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
||||||
|
|
Loading…
Reference in New Issue