Merge branch 'master' into 195937-icoterms
gitea/salix/pipeline/pr-master This commit looks good Details

This commit is contained in:
Robert Ferrús 2024-06-21 08:52:14 +00:00
commit fb58e8226d
8 changed files with 200 additions and 72 deletions

View File

@ -16,36 +16,51 @@ module.exports = Self => {
accessScopes: ['DEFAULT', 'read:multimedia']}); accessScopes: ['DEFAULT', 'read:multimedia']});
Self.renewToken = async function(ctx) { Self.renewToken = async function(ctx) {
const {accessToken: token} = ctx.req;
const {courtesyTime} = await models.AccessTokenConfig.findOne({
fields: ['courtesyTime']
});
const isNotExceeded = await Self.validateToken(ctx);
if (isNotExceeded)
return token;
// Schedule to remove current token
setTimeout(async() => {
try {
const exists = await models.AccessToken.findById(token.id);
exists && await Self.logout(token.id);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
}, courtesyTime * 1000);
// Get scopes
let createTokenOptions = {}; let createTokenOptions = {};
const {scopes} = token; let token; let isNotExceeded;
if (scopes) try {
createTokenOptions = {scopes: [scopes[0]]}; token = ctx.req.accessToken;
// Create new accessToken
const user = await Self.findById(token.userId);
const accessToken = await user.accessTokens.create(createTokenOptions);
return {id: accessToken.id, ttl: accessToken.ttl}; const {courtesyTime} = await models.AccessTokenConfig.findOne({
fields: ['courtesyTime']
});
isNotExceeded = await Self.validateToken(ctx);
if (isNotExceeded)
return token;
// Schedule to remove current token
setTimeout(async() => {
let exists;
try {
exists = await models.AccessToken.findById(token.id);
exists && await Self.logout(token.id);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
const body = {error: error.message, now: Date.now(), userId: token?.userId ?? null, exists};
await handleError(body);
throw new Error(error);
}
}, courtesyTime * 1000);
// Get scopes
const {scopes} = token;
if (scopes)
createTokenOptions = {scopes: [scopes[0]]};
// Create new accessToken
const user = await Self.findById(token.userId);
const accessToken = await user.accessTokens.create(createTokenOptions);
return {id: accessToken.id, ttl: accessToken.ttl};
} catch (error) {
const body = {error: error.message, now: Date.now(), userId: token?.userId ?? null, createTokenOptions, isNotExceeded};
await handleError(body);
throw new Error(error);
}
}; };
}; };
async function handleError(body, tag = 'renewToken') {
body = JSON.stringify(body);
await models.Application.rawSql('CALL util.debugAdd(?,?);', [tag, body]);
}

View File

@ -61,4 +61,21 @@ describe('Renew Token', () => {
expect(error).toBeUndefined(); expect(error).toBeUndefined();
expect(response.id).toEqual(ctx.req.accessToken.id); expect(response.id).toEqual(ctx.req.accessToken.id);
}); });
it('throw error', async() => {
let error;
try {
await models.VnUser.renewToken({req: {token: null}});
} catch (e) {
error = e;
}
expect(error).toBeDefined();
const query = 'SELECT * FROM util.debug';
const debugLog = await models.Application.rawSql(query, null);
expect(debugLog.length).toEqual(1);
});
}); });

View File

@ -21,7 +21,8 @@ BEGIN
DECLARE vTransactionExportTaxFreeFk INT; DECLARE vTransactionExportTaxFreeFk INT;
DECLARE vSerialDua VARCHAR(1) DEFAULT 'D'; DECLARE vSerialDua VARCHAR(1) DEFAULT 'D';
DECLARE vInvoiceTypeInformativeCode VARCHAR(1); DECLARE vInvoiceTypeInformativeCode VARCHAR(1);
DECLARE vCountryCanariasCode, vCountryCeutaMelillaCode VARCHAR(2) ; DECLARE vCountryCanariasCode, vCountryCeutaMelillaCode VARCHAR(2);
DECLARE vCompanyCode INT;
SELECT SiglaNacion INTO vCountryCanariasCode SELECT SiglaNacion INTO vCountryCanariasCode
FROM Naciones FROM Naciones
@ -31,9 +32,6 @@ BEGIN
FROM Naciones FROM Naciones
WHERE Nacion ='CEUTA Y MELILLA'; WHERE Nacion ='CEUTA Y MELILLA';
SELECT pendingServiceTransactionTypeFk INTO vDuaTransactionFk
FROM config;
SELECT id INTO vTaxImportFk SELECT id INTO vTaxImportFk
FROM taxType FROM taxType
WHERE code = 'import21'; WHERE code = 'import21';
@ -46,10 +44,14 @@ BEGIN
FROM taxType FROM taxType
WHERE code = 'import4'; WHERE code = 'import4';
SELECT definitiveExportTransactionTypeFk INTO vTransactionExportFk SELECT shipmentTransactionTypeFk,
FROM config; definitiveExportTransactionTypeFk,
pendingServiceTransactionTypeFk,
SELECT shipmentTransactionTypeFk INTO vTransactionExportTaxFreeFk company_getCode(vCompanyFk)
INTO vTransactionExportTaxFreeFk,
vTransactionExportFk,
vDuaTransactionFk,
vCompanyCode
FROM config; FROM config;
SELECT codeSage INTO vInvoiceTypeInformativeCode SELECT codeSage INTO vInvoiceTypeInformativeCode
@ -64,8 +66,6 @@ BEGIN
WHERE enlazadoSage = FALSE WHERE enlazadoSage = FALSE
AND Asiento <> 1 ; AND Asiento <> 1 ;
CALL clientSupplier_add(vCompanyFk);
CALL pgc_add(vCompanyFk);
CALL invoiceOut_manager(vYear, vCompanyFk); CALL invoiceOut_manager(vYear, vCompanyFk);
CALL invoiceIn_manager(vYear, vCompanyFk); CALL invoiceIn_manager(vYear, vCompanyFk);
@ -158,7 +158,7 @@ BEGIN
) )
SELECT 'EN' TipoEntrada, SELECT 'EN' TipoEntrada,
YEAR(x.FECHA) Ejercicio, YEAR(x.FECHA) Ejercicio,
company_getCode(vCompanyFk) AS CodigoEmpresa, vCompanyCode CodigoEmpresa,
x.ASIEN Asiento, x.ASIEN Asiento,
IF(EURODEBE <> 0 OR (EURODEBE = 0 AND EUROHABER IS NULL), IF(EURODEBE <> 0 OR (EURODEBE = 0 AND EUROHABER IS NULL),
'D', 'H') CargoAbono, 'D', 'H') CargoAbono,
@ -291,20 +291,6 @@ BEGIN
WHERE m.CargoAbono = 'D' WHERE m.CargoAbono = 'D'
AND m.enlazadoSage = FALSE; AND m.enlazadoSage = FALSE;
-- Elimina cuentas de cliente/proveedor que no se utilizarán en la importación
DELETE cp
FROM clientesProveedores cp
LEFT JOIN movConta mc ON mc.codigoCuenta = cp.codigoCuenta
AND mc.enlazadoSage = FALSE
WHERE mc.codigoCuenta IS NULL;
-- Elimina cuentas contables que no se utilizarán en la importación
DELETE pc
FROM planCuentasPGC pc
LEFT JOIN movConta mc ON mc.codigoCuenta = pc.codigoCuenta
AND mc.enlazadoSage = FALSE
WHERE mc.codigoCuenta IS NULL;
-- DUAS -- DUAS
UPDATE movConta mci UPDATE movConta mci
JOIN vn.XDiario x ON x.ASIEN = mci.Asiento JOIN vn.XDiario x ON x.ASIEN = mci.Asiento
@ -411,5 +397,55 @@ BEGIN
AND importeDivisa > 0 AND importeDivisa > 0
AND ImporteAsiento < 0; AND ImporteAsiento < 0;
CREATE OR REPLACE TEMPORARY TABLE tmp.clientSupplier
(INDEX(idClientSupplier, `type`))
ENGINE = MEMORY
WITH client AS(
SELECT DISTINCT c.id
FROM sage.movConta mc
JOIN vn.client c ON c.accountingAccount = mc.CodigoCuenta
WHERE NOT enlazadoSage
),supplier AS(
SELECT DISTINCT s.id
FROM sage.movConta mc
JOIN vn.supplier s ON s.account = mc.CodigoCuenta
WHERE NOT enlazadoSage
),clientSupplierSync AS(
SELECT idClientSupplier, `type`
FROM sage.clientSupplier cs
WHERE isSync
)
SELECT idClientSupplier, `type`
FROM sage.clientSupplier cs
WHERE NOT isSync
UNION
SELECT id, 'C'
FROM client c
LEFT JOIN clientSupplierSync cs ON cs.idClientSupplier = c.id
AND cs.Type ='C'
WHERE cs.idClientSupplier IS NULL
UNION
SELECT id, 'P'
FROM supplier s
LEFT JOIN clientSupplierSync cs ON cs.idClientSupplier = s.id
AND cs.Type ='P'
WHERE cs.idClientSupplier IS NULL;
CALL clientSupplier_add(vCompanyFk);
INSERT IGNORE INTO sage.clientSupplier (companyFk, `type`, idClientSupplier, isSync)
SELECT vCompanyCode, `type`, idClientSupplier, FALSE
FROM tmp.clientSupplier;
DROP TEMPORARY TABLE tmp.clientSupplier;
CALL pgc_add(vCompanyFk);
-- Elimina cuentas contables que no se utilizarán en la importación
DELETE pc
FROM planCuentasPGC pc
LEFT JOIN movConta mc ON mc.codigoCuenta = pc.codigoCuenta
AND mc.enlazadoSage = FALSE
WHERE mc.codigoCuenta IS NULL;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -1,11 +1,16 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `sage`.`clientSupplier_add`(vCompanyFk INT) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `sage`.`clientSupplier_add`(
vCompanyFk INT
)
BEGIN BEGIN
/** /**
* Prepara los datos de clientes y proveedores para exportarlos a Sage * Inserta en la tabla sage.clientesProveedores los datos de clientes y proveedores
* @vCompanyFk Empresa dela que se quiere trasladar datos * que se actualizaran o se daran de alta en Sage
* @vCompanyFk Id de empresa
* @table tmp.clientSupplier(idClientSupplier, `type`)
*/ */
DECLARE vCountryCeutaMelillaFk INT; DECLARE vCountryCeutaMelillaFk INT;
DECLARE vCompanyCode INT DEFAULT company_getCode(vCompanyFk);
DECLARE vCountryCanariasCode, vCountryCeutaMelillaCode VARCHAR(2); DECLARE vCountryCanariasCode, vCountryCeutaMelillaCode VARCHAR(2);
SELECT SiglaNacion INTO vCountryCanariasCode SELECT SiglaNacion INTO vCountryCanariasCode
@ -45,7 +50,7 @@ BEGIN
Email1, Email1,
iban) iban)
SELECT SELECT
company_getCode(vCompanyFk), vCompanyCode,
'C', 'C',
c.id, c.id,
c.socialName, c.socialName,
@ -53,7 +58,7 @@ BEGIN
IFNULL(c.street, ''), IFNULL(c.street, ''),
c.accountingAccount, c.accountingAccount,
@fi := IF(cu.code = LEFT(TRIM(c.fi), 2) AND c.isVies, MID(TRIM(c.fi), 3, LENGTH(TRIM(c.fi))-1), TRIM(c.fi)), @fi := IF(cu.code = LEFT(TRIM(c.fi), 2) AND c.isVies, MID(TRIM(c.fi), 3, LENGTH(TRIM(c.fi))-1), TRIM(c.fi)),
IF(c.isVies, CONCAT(IFNULL(cu.viesCode,cu.code), @fi ), TRIM(c.fi)), IF(c.isVies, CONCAT(IFNULL(cu.viesCode,cu.code), @fi ), TRIM(c.fi)),
IFNULL(c.postcode, ''), IFNULL(c.postcode, ''),
IFNULL(c.city, ''), IFNULL(c.city, ''),
IFNULL(pr.CodigoProvincia, ''), IFNULL(pr.CodigoProvincia, ''),
@ -75,15 +80,14 @@ BEGIN
IFNULL(SUBSTR(c.email, 1, LOCATE(',', CONCAT(c.email, ','))-1), ''), IFNULL(SUBSTR(c.email, 1, LOCATE(',', CONCAT(c.email, ','))-1), ''),
IFNULL(c.iban, '') IFNULL(c.iban, '')
FROM vn.`client` c FROM vn.`client` c
JOIN clientLastTwoMonths clm ON clm.clientFk = c.id JOIN tmp.clientSupplier cs ON cs.idClientSupplier = c.id
LEFT JOIN vn.country cu ON cu.id = c.countryFk LEFT JOIN vn.country cu ON cu.id = c.countryFk
LEFT JOIN Naciones n ON n.countryFk = cu.id LEFT JOIN Naciones n ON n.countryFk = cu.id
LEFT JOIN vn.province p ON p.id = c.provinceFk LEFT JOIN vn.province p ON p.id = c.provinceFk
LEFT JOIN Provincias pr ON pr.provinceFk = p.id LEFT JOIN Provincias pr ON pr.provinceFk = p.id
WHERE c.isRelevant WHERE cs.type = 'C'
AND clm.companyFk = vCompanyFk
UNION ALL UNION ALL
SELECT company_getCode(vCompanyFk), SELECT vCompanyCode,
'P', 'P',
s.id, s.id,
s.name, s.name,
@ -107,18 +111,16 @@ BEGIN
IFNULL(s.transactionTypeSageFk, 0), IFNULL(s.transactionTypeSageFk, 0),
IFNULL(s.withholdingSageFk, '0'), IFNULL(s.withholdingSageFk, '0'),
IFNULL(SUBSTR(sc.email, 1, (COALESCE(NULLIF(LOCATE(',', sc.email), 0), 99) - 1)), ''), IFNULL(SUBSTR(sc.email, 1, (COALESCE(NULLIF(LOCATE(',', sc.email), 0), 99) - 1)), ''),
IFNULL(iban, '') IFNULL(sa.iban, '')
FROM vn.supplier s FROM vn.supplier s
JOIN supplierLastThreeMonths pl ON pl.supplierFk = s.id JOIN tmp.clientSupplier cs ON cs.idClientSupplier = s.id
LEFT JOIN vn.country co ON co.id = s.countryFk LEFT JOIN vn.country co ON co.id = s.countryFk
LEFT JOIN Naciones n ON n.countryFk = co.id LEFT JOIN Naciones n ON n.countryFk = co.id
LEFT JOIN vn.province p ON p.id = s.provinceFk LEFT JOIN vn.province p ON p.id = s.provinceFk
LEFT JOIN Provincias pr ON pr.provinceFk = p.id LEFT JOIN Provincias pr ON pr.provinceFk = p.id
LEFT JOIN vn.supplierContact sc ON sc.supplierFk = s.id LEFT JOIN vn.supplierContact sc ON sc.supplierFk = s.id
LEFT JOIN vn.supplierAccount sa ON sa.supplierFk = s.id LEFT JOIN vn.supplierAccount sa ON sa.supplierFk = s.id
WHERE pl.companyFk = vCompanyFk AND WHERE cs.type = 'P'
s.isActive AND GROUP BY s.id;
s.nif <> ''
GROUP BY pl.supplierFk, pl.companyFk;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -3,12 +3,35 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`client_afterUpdate`
AFTER UPDATE ON `client` AFTER UPDATE ON `client`
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
IF !(NEW.defaultAddressFk <=> OLD.defaultAddressFk) THEN IF NOT (NEW.defaultAddressFk <=> OLD.defaultAddressFk) THEN
UPDATE `address` SET isDefaultAddress = FALSE UPDATE `address` SET isDefaultAddress = FALSE
WHERE clientFk = NEW.id; WHERE clientFk = NEW.id;
UPDATE `address` SET isDefaultAddress = TRUE UPDATE `address` SET isDefaultAddress = TRUE
WHERE id = NEW.defaultAddressFk; WHERE id = NEW.defaultAddressFk;
END IF; END IF;
IF NEW.id <> OLD.id
OR NOT (NEW.provinceFk <=> OLD.provinceFk)
OR NOT (NEW.socialName <=> OLD.socialName)
OR NOT (NEW.street <=> OLD.street)
OR NOT (NEW.accountingAccount <=> OLD.accountingAccount)
OR NOT (NEW.isVies <=> OLD.isVies)
OR NOT (NEW.fi <=> OLD.fi)
OR NOT (NEW.postcode <=> OLD.postcode)
OR NOT (NEW.city <=> OLD.city)
OR NOT (NEW.countryFk <=> OLD.countryFk)
OR NOT (NEW.taxTypeSageFk <=> OLD.taxTypeSageFk)
OR NOT (NEW.transactionTypeSageFk <=> OLD.transactionTypeSageFk)
OR NOT (NEW.email <=> OLD.email)
OR NOT (NEW.iban <=> OLD.iban)
OR NOT (NEW.phone <=> OLD.phone)
OR NOT (NEW.mobile <=> OLD.mobile) THEN
UPDATE sage.clientSupplier
SET isSync = FALSE
WHERE idClientSupplier IN (NEW.id, OLD.id)
AND `type` = 'C';
END IF;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -65,11 +65,11 @@ BEGIN
END IF; END IF;
END IF; END IF;
IF !(NEW.salesPersonFk <=> OLD.salesPersonFk) THEN IF NOT (NEW.salesPersonFk <=> OLD.salesPersonFk) THEN
SET NEW.lastSalesPersonFk = IFNULL(NEW.salesPersonFk, OLD.salesPersonFk); SET NEW.lastSalesPersonFk = IFNULL(NEW.salesPersonFk, OLD.salesPersonFk);
END IF; END IF;
IF !(NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN IF NOT (NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN
SET NEW.isTaxDataChecked = 0; SET NEW.isTaxDataChecked = 0;
END IF; END IF;
END$$ END$$

View File

@ -0,0 +1,26 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`supplier_afterUpdate`
BEFORE UPDATE ON `supplier`
FOR EACH ROW
BEGIN
IF NEW.id <> OLD.id
OR NOT (NEW.name <=> OLD.name)
OR NOT (NEW.street <=> OLD.street)
OR NOT (NEW.account <=> OLD.account)
OR NOT (NEW.nif <=> OLD.nif)
OR NOT (NEW.isVies <=> OLD.isVies)
OR NOT (NEW.provinceFk <=> OLD.provinceFk)
OR NOT (NEW.countryFk <=> OLD.countryFk)
OR NOT (NEW.postCode <=> OLD.postCode)
OR NOT (NEW.city <=> OLD.city)
OR NOT (NEW.taxTypeSageFk <=> OLD.taxTypeSageFk)
OR NOT (NEW.transactionTypeSageFk <=> OLD.transactionTypeSageFk)
OR NOT (NEW.withholdingSageFk <=> OLD.withholdingSageFk) THEN
UPDATE sage.clientSupplier
SET isSync = FALSE
WHERE idClientSupplier IN (NEW.id, OLD.id)
AND `type` = 'P';
END IF;
END$$
DELIMITER ;

View File

@ -0,0 +1,9 @@
CREATE OR REPLACE TABLE sage.clientSupplier (
`companyFk` smallint(6) NOT NULL,
`type` ENUM('C','P') NOT NULL,
`idClientSupplier` INT NOT NULL,
`isSync` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`companyFk`,`idClientSupplier`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci
COMMENT='Clients and suppliers present in Sage and their synchronization status';