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

60 lines
1.2 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_next`(
IN vFiscalYear INT,
2024-05-28 12:50:58 +00:00
IN vIsManageTransaction BOOLEAN,
2024-05-21 15:31:29 +00:00
OUT vLastBookEntry INT
)
BEGIN
2024-05-21 15:31:29 +00:00
DECLARE vHasStartTransaction BOOLEAN;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
IF vHasStartTransaction THEN
ROLLBACK TO sp;
RESIGNAL;
ELSE
ROLLBACK;
CALL util.throw ('It has not been possible to generate a new ledger');
END IF;
END;
IF vFiscalYear IS NULL THEN
CALL util.throw('Fiscal year is required');
END IF;
2024-05-28 12:50:58 +00:00
IF NOT vIsManageTransaction THEN
SELECT @@in_transaction INTO vHasStartTransaction;
2024-05-21 15:31:29 +00:00
2024-05-28 12:50:58 +00:00
IF NOT vHasStartTransaction THEN
START TRANSACTION;
ELSE
SAVEPOINT sp;
END IF;
END IF;
2024-05-21 15:31:29 +00:00
SELECT bookEntry + 1 INTO vLastBookEntry
FROM ledgerCompany
WHERE fiscalYear = vFiscalYear
FOR UPDATE;
IF vLastBookEntry IS NULL THEN
INSERT INTO ledgerCompany
SET fiscalYear = vFiscalYear,
bookEntry = 1;
SET vLastBookEntry = 1;
END IF;
UPDATE ledgerCompany
SET bookEntry = vLastBookEntry
WHERE fiscalYear = vFiscalYear;
2024-05-28 12:50:58 +00:00
IF NOT vIsManageTransaction THEN
IF vHasStartTransaction THEN
RELEASE SAVEPOINT sp;
ELSE
COMMIT;
END IF;
END IF;
END$$
2024-05-21 15:31:29 +00:00
DELIMITER ;