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

66 lines
1.8 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`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-04-09 13:57:34 +00:00
IF(ar.debitCredit = 'credit' AND a.currencyFk = arc.currencyFk, ar.amount, NULL),
IF(ar.debitCredit = 'debit' AND a.currencyFk = arc.currencyFk, ar.amount, NULL),
2024-03-28 14:03:33 +00:00
a.id,
sa.supplierFk,
2024-03-26 12:41:53 +00:00
arc.warehouseFk,
ar.supplierAccountFk,
ar.calculatedCode,
2024-04-09 13:57:34 +00:00
IF(ar.debitCredit = 'credit' AND NOT a.currencyFk = arc.currencyFk, ar.amount, NULL),
IF(ar.debitCredit = 'debit' AND NOT a.currencyFk = arc.currencyFk, ar.amount, NULL),
2024-03-28 14:03:33 +00:00
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 ;