hotfix: ledger_next transacciones refs #7523 #2540

Merged
carlosap merged 7 commits from 7523-ledger_next-transacciones into master 2024-06-04 17:14:52 +00:00
15 changed files with 77 additions and 73 deletions

View File

@ -1,12 +1,12 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(isTx BOOL) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(vIsTx BOOL)
BEGIN BEGIN
/** /**
* Confirma los cambios asociados a una transacción. * Confirma los cambios asociados a una transacción.
* *
* @param isTx es true si existe transacción asociada * @param vIsTx es true si existe transacción asociada
*/ */
IF isTx THEN IF vIsTx THEN
COMMIT; COMMIT;
END IF; END IF;
END$$ END$$

View File

@ -1,12 +1,12 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(isTx BOOL) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(vIsTx BOOL)
BEGIN BEGIN
/** /**
* Deshace los cambios asociados a una transacción. * Deshace los cambios asociados a una transacción.
* *
* @param isTx es true si existe transacción asociada * @param vIsTx es true si existe transacción asociada
*/ */
IF isTx THEN IF vIsTx THEN
ROLLBACK; ROLLBACK;
END IF; END IF;
END$$ END$$

View File

@ -1,12 +1,12 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(isTx BOOL) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(vIsTx BOOL)
BEGIN BEGIN
/** /**
* Inicia una transacción. * Inicia una transacción.
* *
* @param isTx es true si existe transacción asociada * @param vIsTx es true si existe transacción asociada
*/ */
IF isTx THEN IF vIsTx THEN
START TRANSACTION; START TRANSACTION;
END IF; END IF;
END$$ END$$

View File

@ -44,7 +44,7 @@ BEGIN
JOIN dua d ON d.id = de.duaFk JOIN dua d ON d.id = de.duaFk
WHERE d.id = vDuaFk WHERE d.id = vDuaFk
LIMIT 1; LIMIT 1;
CALL ledger_next(vFiscalYear, FALSE, vBookEntry); CALL ledger_nextTx(vFiscalYear, vBookEntry);
END IF; END IF;
OPEN vInvoicesIn; OPEN vInvoicesIn;

View File

@ -12,7 +12,7 @@ BEGIN
WHERE id = vDuaFk; WHERE id = vDuaFk;
IF vBookNumber IS NULL OR NOT vBookNumber THEN IF vBookNumber IS NULL OR NOT vBookNumber THEN
CALL ledger_next(YEAR(vBookDated), FALSE, vBookNumber); CALL ledger_nextTx(YEAR(vBookDated), vBookNumber);
END IF; END IF;
-- Apunte de la aduana -- Apunte de la aduana

View File

@ -70,7 +70,7 @@ BEGIN
SELECT YEAR(bookEntried) INTO vFiscalYear FROM tInvoiceIn LIMIT 1; SELECT YEAR(bookEntried) INTO vFiscalYear FROM tInvoiceIn LIMIT 1;
IF vBookNumber IS NULL THEN IF vBookNumber IS NULL THEN
CALL ledger_next(vFiscalYear, FALSE, vBookNumber); CALL ledger_nextTx(vFiscalYear, vBookNumber);
END IF; END IF;
-- Apunte del proveedor -- Apunte del proveedor

View File

@ -61,7 +61,7 @@ BEGIN
WHERE io.id = vInvoice; WHERE io.id = vInvoice;
SELECT YEAR(FECHA) INTO vFiscalYear FROM rs LIMIT 1; SELECT YEAR(FECHA) INTO vFiscalYear FROM rs LIMIT 1;
CALL ledger_next(vFiscalYear, FALSE, vBookNumber); CALL ledger_nextTx(vFiscalYear, vBookNumber);
-- Linea del cliente -- Linea del cliente
INSERT INTO XDiario( INSERT INTO XDiario(
ASIEN, ASIEN,

View File

@ -28,7 +28,7 @@ BEGIN
DECLARE vIsOriginalAClient BOOL; DECLARE vIsOriginalAClient BOOL;
DECLARE vPayMethodCompensation INT; DECLARE vPayMethodCompensation INT;
CALL ledger_next(YEAR(vDated), FALSE, vNewBookEntry); CALL ledger_nextTx(YEAR(vDated), vNewBookEntry);
SELECT COUNT(id) INTO vIsOriginalAClient SELECT COUNT(id) INTO vIsOriginalAClient
FROM client FROM client

View File

@ -1,38 +1,21 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_next`( CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_next`(
IN vFiscalYear INT, IN vFiscalYear INT,
IN vIsManageTransaction BOOLEAN,
OUT vLastBookEntry INT OUT vLastBookEntry INT
) )
/**
* Devuelve un número de asiento válido, según el contador de asientos
* tabla vn.ledgerConfig, si no existe lo inicializa a 1
* No inicia transacción, para transaccionar usar vn.ledger_nextTx
*
* @param vFiscalYear Id del año contable
* @return vLastBookEntry Id del asiento
*/
BEGIN BEGIN
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 IF vFiscalYear IS NULL THEN
CALL util.throw('Fiscal year is required'); CALL util.throw('Fiscal year is required');
END IF; END IF;
IF NOT vIsManageTransaction THEN
SELECT @@in_transaction INTO vHasStartTransaction;
IF NOT vHasStartTransaction THEN
START TRANSACTION;
ELSE
SAVEPOINT sp;
END IF;
END IF;
SELECT bookEntry + 1 INTO vLastBookEntry SELECT bookEntry + 1 INTO vLastBookEntry
FROM ledgerCompany FROM ledgerCompany
WHERE fiscalYear = vFiscalYear WHERE fiscalYear = vFiscalYear
@ -48,13 +31,5 @@ BEGIN
UPDATE ledgerCompany UPDATE ledgerCompany
SET bookEntry = vLastBookEntry SET bookEntry = vLastBookEntry
WHERE fiscalYear = vFiscalYear; WHERE fiscalYear = vFiscalYear;
IF NOT vIsManageTransaction THEN
IF vHasStartTransaction THEN
RELEASE SAVEPOINT sp;
ELSE
COMMIT;
END IF;
END IF;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -0,0 +1,30 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_nextTx`(
IN vFiscalYear INT,
OUT vLastBookEntry INT
)
/**
* Devuelve un número de asiento válido, según el contador de asientos
* tabla vn.ledgerConfig, si no existe lo inicializa a 1
* Lo hace transaccionando el proceso
*
* @param vFiscalYear Id del año contable
* @return vLastBookEntry Id del asiento
*/
BEGIN
DECLARE vIsRequiredTx BOOL DEFAULT NOT @@in_transaction;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
CALL util.tx_rollback(vIsRequiredTx);
RESIGNAL;
END;
IF vFiscalYear IS NULL THEN
CALL util.throw('Fiscal year is required');
END IF;
CALL util.tx_start(vIsRequiredTx);
CALL ledger_next(vFiscalYear, vLastBookEntry);
CALL util.tx_commit(vIsRequiredTx);
END$$
DELIMITER ;

View File

@ -24,7 +24,7 @@ BEGIN
DECLARE vEvaNotes VARCHAR(255); DECLARE vEvaNotes VARCHAR(255);
DECLARE vDone BOOL; DECLARE vDone BOOL;
DECLARE vAuxEntryFk INT; DECLARE vAuxEntryFk INT;
DECLARE vTx BOOLEAN DEFAULT @@in_transaction; DECLARE vIsRequiredTx BOOLEAN DEFAULT NOT @@in_transaction;
DECLARE vRsEntry CURSOR FOR DECLARE vRsEntry CURSOR FOR
SELECT e.id SELECT e.id
FROM entry e FROM entry e
@ -35,11 +35,11 @@ BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN
CALL util.tx_rollback(vTx); CALL util.tx_rollback(vIsRequiredTx);
RESIGNAL; RESIGNAL;
END; END;
CALL util.tx_start(vTx); CALL util.tx_start(vIsRequiredTx);
INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom) INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom)
SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk
@ -76,6 +76,6 @@ BEGIN
SET @isModeInventory = FALSE; SET @isModeInventory = FALSE;
CLOSE vRsEntry; CLOSE vRsEntry;
CALL util.tx_commit(vTx); CALL util.tx_commit(vIsRequiredTx);
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -39,7 +39,7 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`xdiario_new`(
*/ */
BEGIN BEGIN
IF vBookNumber IS NULL THEN IF vBookNumber IS NULL THEN
CALL ledger_next(YEAR(vDated), FALSE, vBookNumber); CALL ledger_nextTx(YEAR(vDated), vBookNumber);
END IF; END IF;
INSERT INTO XDiario INSERT INTO XDiario

View File

@ -23,7 +23,7 @@ BEGIN
FROM supplier FROM supplier
WHERE id = NEW.supplierFk; WHERE id = NEW.supplierFk;
CALL ledger_next(YEAR(NEW.received), TRUE, vNewBookEntry); CALL ledger_next(YEAR(NEW.received), vNewBookEntry);
INSERT INTO XDiario ( INSERT INTO XDiario (
ASIEN, ASIEN,

View File

@ -26,10 +26,9 @@ module.exports = Self => {
Self.cloneWithEntries = async(ctx, id, options) => { Self.cloneWithEntries = async(ctx, id, options) => {
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
let tx;
const myOptions = {}; const myOptions = {};
let tx = options?.transaction;
try {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -37,7 +36,7 @@ module.exports = Self => {
tx = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
myOptions.transaction = tx; myOptions.transaction = tx;
} }
try {
const travel = await Self.findById(id, { const travel = await Self.findById(id, {
fields: [ fields: [
'id', 'id',
@ -89,6 +88,8 @@ module.exports = Self => {
'ref' 'ref'
] ]
}, myOptions); }, myOptions);
if (tx) await tx.commit();
return newTravel.id; return newTravel.id;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -7,19 +7,22 @@ describe('Travel cloneWithEntries()', () => {
const ctx = {req: {accessToken: {userId: currentUserId}}}; const ctx = {req: {accessToken: {userId: currentUserId}}};
let newTravelId; let newTravelId;
it(`should clone the travel and the containing entries`, async() => { it(`should clone the travel and the containing entries`, async() => {
const tx = await models.Travel.beginTransaction({ const tx = await models.Travel.beginTransaction({});
});
const warehouseThree = 3; const warehouseThree = 3;
const agencyModeOne = 1; const agencyModeOne = 1;
let travelRemoved;
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options); newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options);
const travelEntries = await models.Entry.find({ const travelEntries = await models.Entry.find({
where: { where: {
travelFk: newTravelId travelFk: newTravelId
} }
}, options); }, options);
const newTravel = await models.Travel.findById(travelId); const newTravel = await models.Travel.findById(travelId, null, options);
travelRemoved = await models.Travel.findById(newTravelId);
expect(newTravelId).not.toEqual(travelId); expect(newTravelId).not.toEqual(travelId);
expect(newTravel.ref).toEqual('fifth travel'); expect(newTravel.ref).toEqual('fifth travel');
@ -27,14 +30,9 @@ describe('Travel cloneWithEntries()', () => {
expect(newTravel.warehouseOutFk).toEqual(warehouseThree); expect(newTravel.warehouseOutFk).toEqual(warehouseThree);
expect(newTravel.agencyModeFk).toEqual(agencyModeOne); expect(newTravel.agencyModeFk).toEqual(agencyModeOne);
expect(travelEntries.length).toBeGreaterThan(0); expect(travelEntries.length).toBeGreaterThan(0);
await models.Entry.destroyAll({
travelFk: newTravelId
}, options);
await models.Travel.destroyById(newTravelId, options);
await tx.rollback();
const travelRemoved = await models.Travel.findById(newTravelId, options);
expect(travelRemoved).toBeNull(); expect(travelRemoved).toBeNull();
await tx.rollback();
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;