salix/db/routines/vn/procedures/addAccountReconciliation.sql

66 lines
1.8 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`addAccountReconciliation`()
BEGIN
/**
* Updates duplicate records in the accountReconciliation table,
* by assigning them a new identifier and then inserts a new entry in the till table.
*/
UPDATE accountReconciliation ar
JOIN (
SELECT id,
2024-03-14 10:49:21 +00:00
calculatedCode,
CONCAT(
2024-03-14 10:49:21 +00:00
calculatedCode,
'(',
2024-03-14 10:49:21 +00:00
ROW_NUMBER() OVER (PARTITION BY calculatedCode ORDER BY id),
')'
) newId
2024-03-14 10:49:21 +00:00
FROM accountReconciliation ar
WHERE calculatedCode IN (
SELECT calculatedCode
FROM accountReconciliation
2024-03-14 10:49:21 +00:00
GROUP BY calculatedCode
HAVING COUNT(*) > 1
)
2024-03-14 10:49:21 +00:00
ORDER BY calculatedCode, id
) sub2 ON ar.id = sub2.id
2024-03-14 10:49:21 +00:00
SET ar.calculatedCode = sub2.newId;
2024-03-26 12:41:53 +00:00
INSERT INTO till(
dated,
isAccountable,
serie,
concept,
`in`,
`out`,
bankFk,
companyFk,
warehouseFk,
supplierAccountFk,
calculatedCode,
InForeignValue,
OutForeignValue,
workerFk
)
2024-03-28 14:03:33 +00:00
SELECT ar.operationDated,
TRUE,
'MB',
2024-03-26 12:41:53 +00:00
ar.concept,
2024-03-28 14:03:33 +00:00
IF(ar.debitCredit = arc.debitCredit2 AND a.currencyFk = arc.currencyFk, ar.amount, NULL),
IF(ar.debitCredit = arc.debitCredit AND a.currencyFk = arc.currencyFk, ar.amount, NULL),
a.id,
sa.supplierFk,
2024-03-26 12:41:53 +00:00
arc.warehouseFk,
ar.supplierAccountFk,
ar.calculatedCode,
2024-03-28 14:03:33 +00:00
IF(ar.debitCredit = arc.debitCredit2 AND NOT a.currencyFk = arc.currencyFk, ar.amount, NULL),
IF(ar.debitCredit = arc.debitCredit AND NOT a.currencyFk = arc.currencyFk, ar.amount, NULL),
account.myUser_getId()
2024-03-26 12:41:53 +00:00
FROM accountReconciliation ar
JOIN supplierAccount sa ON sa.id = ar.supplierAccountFk
JOIN accounting a ON a.id = sa.accountingFk
LEFT JOIN till t ON t.calculatedCode = ar.calculatedCode
JOIN accountReconciliationConfig arc
WHERE t.id IS NULL;
END$$
DELIMITER ;