2024-03-12 11:57:34 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`addAccountReconciliation`()
|
2024-03-12 11:57:34 +00:00
|
|
|
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,
|
2024-03-12 11:57:34 +00:00
|
|
|
CONCAT(
|
2024-03-14 10:49:21 +00:00
|
|
|
calculatedCode,
|
2024-03-12 11:57:34 +00:00
|
|
|
'(',
|
2024-03-14 10:49:21 +00:00
|
|
|
ROW_NUMBER() OVER (PARTITION BY calculatedCode ORDER BY id),
|
2024-03-12 11:57:34 +00:00
|
|
|
')'
|
|
|
|
) newId
|
2024-03-14 10:49:21 +00:00
|
|
|
FROM accountReconciliation ar
|
|
|
|
WHERE calculatedCode IN (
|
|
|
|
SELECT calculatedCode
|
2024-03-12 11:57:34 +00:00
|
|
|
FROM accountReconciliation
|
2024-03-14 10:49:21 +00:00
|
|
|
GROUP BY calculatedCode
|
2024-03-12 11:57:34 +00:00
|
|
|
HAVING COUNT(*) > 1
|
|
|
|
)
|
2024-03-14 10:49:21 +00:00
|
|
|
ORDER BY calculatedCode, id
|
2024-03-12 11:57:34 +00:00
|
|
|
) 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;
|
2024-03-12 11:57:34 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|