diff --git a/db/changes/240401/02-invoiceOutConfig_refLen.sql b/db/changes/240401/02-invoiceOutConfig_refLen.sql new file mode 100644 index 000000000..8b91889f2 --- /dev/null +++ b/db/changes/240401/02-invoiceOutConfig_refLen.sql @@ -0,0 +1,2 @@ +ALTER TABLE vn.invoiceOutConfig + ADD IF NOT EXISTS refLen TINYINT UNSIGNED DEFAULT 5 NOT NULL COMMENT 'Invoice reference identifier length'; diff --git a/db/changes/240401/00-fixInvoiceOutBeforeInsert.sql b/db/changes/240401/03-fixInvoiceOutBeforeInsert.sql similarity index 51% rename from db/changes/240401/00-fixInvoiceOutBeforeInsert.sql rename to db/changes/240401/03-fixInvoiceOutBeforeInsert.sql index 10e8d6356..0081c8803 100644 --- a/db/changes/240401/00-fixInvoiceOutBeforeInsert.sql +++ b/db/changes/240401/03-fixInvoiceOutBeforeInsert.sql @@ -4,17 +4,20 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`invoiceOut_beforeInse FOR EACH ROW BEGIN /** + * Generates the next reference for the invoice serial. There cannot be gaps + * between identifiers of the same serial! + * * Reference format: - * - 0: Serial [A-Z] - * - 1: Sage company id - * - 2-3: Last two digits of issued year - * - 4-8: Autoincrement identifier - **/ - DECLARE vNewRef INT DEFAULT 0; - DECLARE vCompanyCode INT; + * {0} Invoice serial + * {1} The company code + * {2-3} Last two digits of issue year + * {4-$} Autoincrement identifier + */ + DECLARE vRef INT DEFAULT 0; + DECLARE vRefLen INT; + DECLARE vRefPrefix VARCHAR(255); DECLARE vLastRef VARCHAR(255); - DECLARE vRefStr VARCHAR(255); - DECLARE vRefLen INT DEFAULT 5; + DECLARE vCompanyCode INT; DECLARE vYearLen INT DEFAULT 2; DECLARE vPrefixLen INT; @@ -23,36 +26,34 @@ BEGIN WHERE id = NEW.companyFk; IF vCompanyCode IS NULL THEN - CALL util.throw('sageCompanyNotDefined'); + CALL util.throw('companyCodeNotDefined'); END IF; SELECT MAX(i.ref) INTO vLastRef FROM invoiceOut i WHERE i.serial = NEW.serial - AND i.issued BETWEEN util.firstDayOfYear(NEW.issued) AND util.dayEnd(util.lastDayOfYear(NEW.issued)) + AND i.issued BETWEEN util.firstDayOfYear(NEW.issued) AND util.lastDayOfYear(NEW.issued) AND i.companyFk = NEW.companyFk; IF vLastRef IS NOT NULL THEN SET vPrefixLen = LENGTH(NEW.serial) + LENGTH(vCompanyCode) + vYearLen; SET vRefLen = LENGTH(vLastRef) - vPrefixLen; - SET vRefStr = SUBSTRING(vLastRef, vPrefixLen + 1); - SET vNewRef = vRefStr + 1; - - IF LENGTH(vNewRef) > vRefLen THEN - CALL util.throw('refLenExceeded'); - END IF; - - SET NEW.ref = CONCAT( - SUBSTRING(vLastRef, 1, vPrefixLen), - LPAD(vNewRef, LENGTH(vRefStr), '0') - ); + SET vRefPrefix = LEFT(vLastRef, vPrefixLen); + SET vRef = RIGHT(vLastRef, vRefLen); ELSE - SET NEW.ref = CONCAT( + SELECT refLen INTO vRefLen FROM invoiceOutConfig; + SET vRefPrefix = CONCAT( NEW.serial, vCompanyCode, - RIGHT(YEAR(NEW.issued), vYearLen), - LPAD(1, vRefLen, '0') + RIGHT(YEAR(NEW.issued), vYearLen) ); END IF; + + SET vRef = vRef + 1; + IF LENGTH(vRef) > vRefLen THEN + CALL util.throw('refIdLenExceeded'); + END IF; + + SET NEW.ref = CONCAT(vRefPrefix, LPAD(vRef, vRefLen, '0')); END$$ DELIMITER ; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 923872f8c..b243692bb 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -600,6 +600,9 @@ INSERT INTO `vn`.`taxArea` (`code`, `claveOperacionFactura`, `CodigoTransaccion` ('NATIONAL', 0, 1), ('WORLD', 2, 15); +INSERT INTO vn.invoiceOutConfig + SET parallelism = 8; + INSERT INTO `vn`.`invoiceOutSerial` (`code`, `description`, `isTaxed`, `taxAreaFk`, `isCEE`, `type`) VALUES ('A', 'Global nacional', 1, 'NATIONAL', 0, 'global'), @@ -623,9 +626,6 @@ UPDATE `vn`.`invoiceOut` SET ref = 'T3333333' WHERE id = 3; UPDATE `vn`.`invoiceOut` SET ref = 'T4444444' WHERE id = 4; UPDATE `vn`.`invoiceOut` SET ref = 'A1111111' WHERE id = 5; -INSERT INTO vn.invoiceOutConfig - SET parallelism = 8; - INSERT INTO `vn`.`invoiceOutTax` (`invoiceOutFk`, `taxableBase`, `vat`, `pgcFk`) VALUES (1, 895.76, 89.58, 4722000010),