diff --git a/db/routines/vn/procedures/invoiceIn_booking.sql b/db/routines/vn/procedures/invoiceIn_booking.sql index 4d6056bc4..4b015750f 100644 --- a/db/routines/vn/procedures/invoiceIn_booking.sql +++ b/db/routines/vn/procedures/invoiceIn_booking.sql @@ -57,7 +57,7 @@ BEGIN LEFT JOIN sage.taxType tt ON tt.id = ti.CodigoIva WHERE ii.id = vSelf; - SELECT YEAR(bookEntried) INTO vFiscalYear FROM tInvoiceIn; + SELECT YEAR(bookEntried) INTO vFiscalYear FROM tInvoiceIn LIMIT 1; CALL ledger_next(vFiscalYear, vBookNumber); -- Apunte del proveedor diff --git a/db/routines/vn/procedures/invoiceOutBooking.sql b/db/routines/vn/procedures/invoiceOutBooking.sql index 9883d56fb..913035576 100644 --- a/db/routines/vn/procedures/invoiceOutBooking.sql +++ b/db/routines/vn/procedures/invoiceOutBooking.sql @@ -60,7 +60,7 @@ BEGIN LEFT JOIN invoiceOut ior ON ior.id = ic.correctedFk WHERE io.id = vInvoice; - SELECT YEAR(FECHA) INTO vFiscalYear FROM rs; + SELECT YEAR(FECHA) INTO vFiscalYear FROM rs LIMIT 1; CALL ledger_next(vFiscalYear, vBookNumber); -- Linea del cliente INSERT INTO XDiario( diff --git a/db/routines/vn/procedures/ledger_next.sql b/db/routines/vn/procedures/ledger_next.sql index e2eb1bcee..36afccb33 100644 --- a/db/routines/vn/procedures/ledger_next.sql +++ b/db/routines/vn/procedures/ledger_next.sql @@ -4,54 +4,11 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_next`( OUT vNewBookEntry INT ) BEGIN - DECLARE vLastBookEntry INT; - 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; - - SELECT @@in_transaction INTO vHasStartTransaction; - - IF NOT vHasStartTransaction THEN - START TRANSACTION; - ELSE - SAVEPOINT sp; - END IF; - - SELECT bookEntry INTO vLastBookEntry - FROM ledgerCompany - WHERE fiscalYear = vFiscalYear - FOR UPDATE; - - IF vLastBookEntry IS NULL THEN - INSERT INTO ledgerCompany - SET fiscalYear = vFiscalYear, - bookEntry = 1; - END IF; - - SET vNewBookEntry = IFNULL(vLastBookEntry, 0) + 1; - - UPDATE ledgerCompany - SET bookEntry = vNewBookEntry + UPDATE ledgerCompany + SET bookEntry = LAST_INSERT_ID(bookEntry + 1) WHERE fiscalYear = vFiscalYear; - IF vHasStartTransaction THEN - RELEASE SAVEPOINT sp; - ELSE - COMMIT; - END IF; + SET vNewBookEntry = LAST_INSERT_ID(); END$$ DELIMITER ; + diff --git a/db/versions/11052-grayHydrangea/00-firstScript.sql b/db/versions/11052-grayHydrangea/00-firstScript.sql index 8d1219d7e..76bdc7b10 100644 --- a/db/versions/11052-grayHydrangea/00-firstScript.sql +++ b/db/versions/11052-grayHydrangea/00-firstScript.sql @@ -1,8 +1,10 @@ -CREATE OR REPLACE TABLE vn.ledgerCompany ( + +CREATE TABLE OR REPLACE `ledgerCompany` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `fiscalYear` int(10) unsigned NOT NULL COMMENT 'Año del ejercicio contable', `bookEntry` int(10) unsigned NOT NULL DEFAULT 1 COMMENT 'Contador asiento contable', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY `ledgerCompany_unique` (`fiscalYear`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='Contador para asientos contables'; \ No newline at end of file