diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 3e6edf07d..b8094471a 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -2549,18 +2549,18 @@ INSERT INTO `vn`.`duaEntry` (`duaFk`, `entryFk`, `value`, `customsValue`, `euroV (7, 7, 1.00, 1.00, 1.00), (8, 8, 1.00, 1.00, 1.00); -REPLACE INTO `vn`.`invoiceIn`(`id`, `serialNumber`,`serial`, `supplierFk`, `issued`, `created`, `supplierRef`, `isBooked`, `companyFk`, `docFk`) +REPLACE INTO `vn`.`invoiceIn`(`id`, `serialNumber`,`serial`, `supplierFk`, `issued`, `created`, `supplierRef`, `isBooked`, `companyFk`, `docFk`, `bookEntried`) VALUES - (1, 1001, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1234, 0, 442, 1), - (2, 1002, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1235, 0, 442, 1), - (3, 1003, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1236, 0, 442, 1), - (4, 1004, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1237, 0, 442, 1), - (5, 1005, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1238, 0, 442, 1), - (6, 1006, 'R', 2, util.VN_CURDATE(), util.VN_CURDATE(), 1239, 0, 442, 1), - (7, 1007, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1240, 0, 442, 1), - (8, 1008, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1241, 0, 442, 1), - (9, 1009, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1242, 0, 442, 1), - (10, 1010, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1243, 0, 442, 1); + (1, 1001, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1234, 0, 442, 1,util.VN_CURDATE()), + (2, 1002, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1235, 0, 442, 1,util.VN_CURDATE()), + (3, 1003, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1236, 0, 442, 1,util.VN_CURDATE()), + (4, 1004, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1237, 0, 442, 1,util.VN_CURDATE()), + (5, 1005, 'R', 1, util.VN_CURDATE(), util.VN_CURDATE(), 1238, 0, 442, 1,util.VN_CURDATE()), + (6, 1006, 'R', 2, util.VN_CURDATE(), util.VN_CURDATE(), 1239, 0, 442, 1,util.VN_CURDATE()), + (7, 1007, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1240, 0, 442, 1,util.VN_CURDATE()), + (8, 1008, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1241, 0, 442, 1,util.VN_CURDATE()), + (9, 1009, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1242, 0, 442, 1,util.VN_CURDATE()), + (10, 1010, 'R', 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1243, 0, 442, 1,util.VN_CURDATE()); INSERT INTO `vn`.`invoiceInConfig` (`id`, `retentionRate`, `retentionName`, `sageFarmerWithholdingFk`, `daysAgo`) VALUES @@ -3791,4 +3791,11 @@ INSERT INTO vn.workerTeam(id, team, workerFk) INSERT INTO vn.workCenter (id, name, payrollCenterFk, counter, warehouseFk, street, geoFk, deliveryManAdjustment) VALUES(100, 'workCenterOne', 1, NULL, 1, 'gotham', NULL, NULL); -UPDATE vn.locker SET workerFk = 1110 WHERE id = 147; \ No newline at end of file +UPDATE vn.locker SET workerFk = 1110 WHERE id = 147; + +INSERT INTO `vn`.`ledgerCompany` SET + fiscalYear = YEAR(util.VN_CURDATE()), + bookEntry = 2; + +INSERT INTO `vn`.`ledgerConfig` SET + maxTolerance = 0.01; diff --git a/db/routines/hedera/procedures/tpvTransaction_confirm.sql b/db/routines/hedera/procedures/tpvTransaction_confirm.sql index e4a8c932f..60a6d8452 100644 --- a/db/routines/hedera/procedures/tpvTransaction_confirm.sql +++ b/db/routines/hedera/procedures/tpvTransaction_confirm.sql @@ -81,7 +81,7 @@ BEGIN -- Código redundante - DO vn.till_new( + CALL vn.till_new( vCustomer ,vBank ,vAmount / 100 diff --git a/db/routines/vn/functions/till_new.sql b/db/routines/vn/functions/till_new.sql index 24f4f2b79..b93072596 100644 --- a/db/routines/vn/functions/till_new.sql +++ b/db/routines/vn/functions/till_new.sql @@ -1,79 +1,73 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`till_new`(vClient INT - ,vBank INT - ,vAmount DOUBLE - ,vConcept VARCHAR(25) - ,vDated DATE - ,vSerie CHAR(1) - ,vBatch TINYINT - ,vNumber INT - ,vCompany SMALLINT - ,vWorker INT +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`till_new`( + vClient INT, + vBank INT, + vAmount DOUBLE, + vConcept VARCHAR(25), + vDated DATE, + vSerie CHAR(1), + vBatch TINYINT, + vNumber INT, + vCompany SMALLINT, + vWorker INT ) - RETURNS int(11) - DETERMINISTIC BEGIN DECLARE vAccount VARCHAR(12); DECLARE vSubaccount VARCHAR(12); DECLARE vAsiento INT DEFAULT NULL; -- Inserta el registro en cajas - INSERT INTO till SET - workerFk = vWorker - ,bankFk = vBank - ,`in` = vAmount - ,concept = vConcept - ,dated = vDated - ,serie = vSerie - ,isAccountable = vBatch - ,`number` = vNumber - ,companyFk = vCompany; + workerFk = vWorker, + bankFk = vBank, + `in` = vAmount, + concept = vConcept, + dated = vDated, + serie = vSerie, + isAccountable = vBatch, + `number` = vNumber, + companyFk = vCompany; -- Inserta los asientos contables - SELECT account INTO vAccount FROM accounting WHERE id = vBank; SELECT accountingAccount INTO vSubaccount FROM `client` WHERE id = vClient; - SET vAsiento = xdiario_new - ( - vAsiento - ,vDated - ,vAccount - ,vSubaccount - ,vConcept - ,vAmount - ,0 - ,0 - ,NULL -- Serie - ,NULL -- Factura - ,NULL -- IVA - ,NULL -- Recargo - ,FALSE -- Auxiliar - ,vCompany - ); - DO xdiario_new - ( - vAsiento - ,vDated - ,vSubaccount - ,vAccount - ,vConcept - ,0 - ,vAmount - ,0 - ,NULL -- Serie - ,NULL -- Factura - ,NULL -- IVA - ,NULL -- Recargo - ,FALSE -- Auxiliar - ,vCompany - ); + CALL xdiario_new( + vAsiento, + vDated, + vAccount, + vSubaccount, + vConcept, + vAmount, + 0, + 0, + NULL, -- Serie + NULL, -- Factura + NULL, -- IVA + NULL, -- Recargo + FALSE, -- Auxiliar + vCompany, + vAsiento); - RETURN NULL; + CALL xdiario_new( + vAsiento, + vDated, + vSubaccount, + vAccount, + vConcept, + 0, + vAmount, + 0, + NULL, -- Serie + NULL, -- Factura + NULL, -- IVA + NULL, -- Recargo + FALSE, -- Auxiliar + vCompany, + vAsiento); END$$ DELIMITER ; diff --git a/db/routines/vn/functions/xdiario_new.sql b/db/routines/vn/functions/xdiario_new.sql deleted file mode 100644 index 4f4b3f3fd..000000000 --- a/db/routines/vn/functions/xdiario_new.sql +++ /dev/null @@ -1,45 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`xdiario_new`( - vBookNumber INT, - vDated DATE, - vSubaccount VARCHAR(12), - vAccount VARCHAR(12), - vConcept VARCHAR(25), - vDebit DOUBLE, - vCredit DOUBLE, - vEuro DOUBLE, - vSerie CHAR(1), - vInvoice VARCHAR(8), - vVat DOUBLE, - vRe DOUBLE, - vAux TINYINT, - vCompanyFk INT -) - RETURNS int(11) - NOT DETERMINISTIC - NO SQL -BEGIN - IF vBookNumber IS NULL THEN - CALL ledger_next(YEAR(vDated), vBookNumber); - END IF; - - INSERT INTO XDiario - SET ASIEN = vBookNumber, - FECHA = vDated, - SUBCTA = vSubaccount, - CONTRA = vAccount, - CONCEPTO = vConcept, - EURODEBE = vDebit, - EUROHABER = vCredit, - BASEEURO = vEuro, - SERIE = vSerie, - FACTURA = vInvoice, - IVA = vVat, - RECEQUIV = vRe, - AUXILIAR = IF(vAux = FALSE, NULL, '*'), - MONEDAUSO = 2, - empresa_id = vCompanyFk; - - RETURN vBookNumber; -END$$ -DELIMITER ; \ No newline at end of file diff --git a/db/routines/vn/procedures/ledger_next.sql b/db/routines/vn/procedures/ledger_next.sql index 5cde90def..dccce3a76 100644 --- a/db/routines/vn/procedures/ledger_next.sql +++ b/db/routines/vn/procedures/ledger_next.sql @@ -1,13 +1,55 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_next`( IN vFiscalYear INT, - OUT vNewBookEntry INT + OUT vLastBookEntry INT ) BEGIN - UPDATE ledgerCompany - SET bookEntry = LAST_INSERT_ID(bookEntry + 1) + 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 + 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; - SET vNewBookEntry = LAST_INSERT_ID(); + IF vHasStartTransaction THEN + RELEASE SAVEPOINT sp; + ELSE + COMMIT; + END IF; END$$ -DELIMITER ; +DELIMITER ; \ No newline at end of file diff --git a/db/routines/vn/procedures/xdiario_new.sql b/db/routines/vn/procedures/xdiario_new.sql new file mode 100644 index 000000000..8204f4652 --- /dev/null +++ b/db/routines/vn/procedures/xdiario_new.sql @@ -0,0 +1,64 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`xdiario_new`( + vBookNumber INT, + vDated DATE, + vSubaccount VARCHAR(12), + vAccount VARCHAR(12), + vConcept VARCHAR(25), + vDebit DOUBLE, + vCredit DOUBLE, + vEuro DOUBLE, + vSerie CHAR(1), + vInvoice VARCHAR(8), + vVat DOUBLE, + vRe DOUBLE, + vAux TINYINT, + vCompanyFk INT, + OUT vNewBookNumber INT +) +/** + * Este procedimiento se encarga de la inserción de registros en la tabla XDiario. + * Si el número de asiento (vBookNumber) no está definido, se genera uno nuevo utilizando + * vn.ledger_next. + * + * @param vBookNumber Número de asiento. Si es NULL, se generará uno nuevo. + * @param vDated Fecha utilizada para generar un nuevo número de libro si vBookNumber es NULL. + * @param vSubaccount Subcuenta para la transacción. + * @param vAccount Cuenta para la transacción. + * @param vConcept Concepto de la transacción. + * @param vDebit Monto del débito para la transacción. + * @param vCredit Monto del crédito para la transacción. + * @param vEuro Monto en euros para la transacción. + * @param vSerie Serie para la transacción. + * @param vInvoice Número de factura para la transacción. + * @param vVat Monto del IVA para la transacción. + * @param vRe Monto del RE para la transacción. + * @param vAux Variable auxiliar para la transacción. + * @param vCompanyFk Clave foránea de la compañía para la transacción. + * @return No retorna un valor, pero realiza una inserción en la tabla XDiario. + */ +BEGIN + IF vBookNumber IS NULL THEN + CALL ledger_next(YEAR(vDated), vBookNumber); + END IF; + + INSERT INTO XDiario + SET ASIEN = vBookNumber, + FECHA = vDated, + SUBCTA = vSubaccount, + CONTRA = vAccount, + CONCEPTO = vConcept, + EURODEBE = vDebit, + EUROHABER = vCredit, + BASEEURO = vEuro, + SERIE = vSerie, + FACTURA = vInvoice, + IVA = vVat, + RECEQUIV = vRe, + AUXILIAR = IF(vAux = FALSE, NULL, '*'), + MONEDAUSO = 2, + empresa_id = vCompanyFk; + + SET vNewBookNumber = vBookNumber; +END$$ +DELIMITER ; \ No newline at end of file diff --git a/modules/client/back/methods/client/createReceipt.js b/modules/client/back/methods/client/createReceipt.js index e2a57272b..23570baf2 100644 --- a/modules/client/back/methods/client/createReceipt.js +++ b/modules/client/back/methods/client/createReceipt.js @@ -95,9 +95,11 @@ module.exports = function(Self) { myOptions ); } else if (accountingType.isAutoConciliated == true) { - const description = `${originalClient.id} : ${originalClient.socialName} - ${accountingType.receiptDescription}`; - const [xdiarioNew] = await Self.rawSql( - `SELECT xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ledger;`, + const description = + `${originalClient.id} : ${originalClient.socialName} - ${accountingType.receiptDescription}`; + const [, [xdiarioNew]] = await Self.rawSql( + `CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew); + SELECT @xdiarioNew ledger;`, [ null, date, @@ -118,7 +120,7 @@ module.exports = function(Self) { ); await Self.rawSql( - `SELECT xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, + `CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew);`, [ xdiarioNew.ledger, date,