feat: sincro client supplier sage refs #7595 #2618
|
@ -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,44 @@ 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
|
||||||
|
)SELECT idClientSupplier, `type`
|
||||||
|
FROM sage.clientSupplier cs
|
||||||
|
WHERE NOT isSync
|
||||||
|
UNION
|
||||||
|
SELECT id, 'C'
|
||||||
|
FROM client
|
||||||
|
UNION
|
||||||
|
SELECT id, 'P'
|
||||||
|
FROM supplier;
|
||||||
|
|
||||||
|
CALL clientSupplier_add(vCompanyFk);
|
||||||
|
|
||||||
|
INSERT IGNORE INTO sage.clientSupplier (companyFk, `type`, idClientSupplier, isSync)
|
||||||
|
SELECT vCompanyCode, `type`, idClientSupplier, FALSE
|
||||||
|
FROM tmp.clientSupplier;
|
||||||
|
|
||||||
jgallego marked this conversation as resolved
Outdated
|
|||||||
|
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 ;
|
||||||
|
|
|
@ -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 ;
|
||||||
jgallego marked this conversation as resolved
Outdated
jgallego
commented
quitar quitar
|
|||||||
|
|
|
@ -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 ;
|
||||||
|
|
|
@ -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$$
|
||||||
|
|
|
@ -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 ;
|
|
@ -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';
|
Loading…
Reference in New Issue
DROP TEMPORARY TABLE tmp.clientSupplier;