/*

	REQUERIDO: FTR 4.4.0.31
	FORÇA DE VENDA: 6.0.17

*/

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SEL_PRODUTO (
    ID_REP INTEGER,
    SINCR SMALLINT)
RETURNS (
    ID_PRO INTEGER,
    ID_SBR INTEGER,
    DESCRICAO VARCHAR(100),
    PRECO_UNIT NUMERIC(10,3),
    PRECO_UNIT2 NUMERIC(10,3),
    PRECO_UNIT_MIN NUMERIC(10,3),
    PRECO_CUSTO NUMERIC(10,3),
    DESCONTO NUMERIC(10,2),
    PESO_BRT NUMERIC(7,3),
    VALID_DCT DATE,
    APL_FINANC SMALLINT,
    APL_CLASSIF SMALLINT,
    INATIVO SMALLINT,
    STATUS_EST SMALLINT,
    STATUS_DISP SMALLINT,
    ID_LIN INTEGER,
    ID_FOR INTEGER,
    PRECO_MIN_MKT NUMERIC(10,2),
    USE_WEB_IMAGE SMALLINT,
    BAR_CODE VARCHAR(14),
    DETACH SMALLINT,
    OPER_TYPE SMALLINT)
AS
DECLARE TRUE       SMALLINT = 1;
DECLARE ID         INTEGER;
DECLARE TABLE_NAME VARCHAR(20)  = 'PRODUTO';
BEGIN
  
  SELECT ID FROM SP_MIN_ID_REPL(:ID_REP, :TABLE_NAME) INTO :ID;

  IF (SINCR = TRUE) THEN BEGIN
    DELETE FROM FV_REPLICATE WHERE ID_REP = :ID_REP AND TABLE_NAME = :TABLE_NAME AND DATE_REPL IS NULL;

    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE, REPLICATING)
    SELECT :ID_REP, :TABLE_NAME, PRODUTO.ID_PRO, 0, 0 FROM PRODUTO;

    EXECUTE PROCEDURE SET_REPLICANDO(:ID_REP, :TABLE_NAME);

    SELECT ID FROM SP_MIN_ID_REPL(:ID_REP, :TABLE_NAME) INTO :ID;
  END

  FOR SELECT FV_REPLICATE.FIELD_KEY AS ID_PRO,
             PRODUTO.ID_SBR,
             PRODUTO.DESCRICAO,
             PRODUTO.PRECO_UNIT,
             PRODUTO.PRECO_UNIT2,
             PRODUTO.PRECO_UNIT_MIN,
             PRODUTO.PRECO_CUSTO,
             PRODUTO.DESCONTO,
             PRODUTO.PESO_BRT,
             PRODUTO.VALID_DCT,
             PRODUTO.APL_FINANC,
             PRODUTO.APL_CLASSIF,
             PRODUTO.INATIVO,
             PRODUTO.STATUS_EST,
             PRODUTO.STATUS_DISP,
             PRODUTO.ID_LIN,
             PRODUTO.ID_FOR,
             PRODUTO.PRECO_MIN_MKT,
             PRODUTO.USE_WEB_IMAGE,
             PRODUTO.BAR_CODE,
             PRODUTO.DETACH,
             FV_REPLICATE.OPER_TYPE
      FROM   FV_REPLICATE
      LEFT JOIN PRODUTO ON (PRODUTO.ID_PRO = FV_REPLICATE.FIELD_KEY)
      WHERE  FV_REPLICATE.ID_REP      = :ID_REP
      AND    FV_REPLICATE.TABLE_NAME  = :TABLE_NAME
      AND    FV_REPLICATE.DATE_REPL   IS NULL
      AND   (FV_REPLICATE.REPLICATING = :ID AND FV_REPLICATE.REPLICATING > 0)
      ORDER BY FV_REPLICATE.ID_ITEM
      INTO  :ID_PRO,
            :ID_SBR,
            :DESCRICAO,
            :PRECO_UNIT,
            :PRECO_UNIT2,
            :PRECO_UNIT_MIN,
            :PRECO_CUSTO,
            :DESCONTO,
            :PESO_BRT,
            :VALID_DCT,
            :APL_FINANC,
            :APL_CLASSIF,
            :INATIVO,
            :STATUS_EST,
            :STATUS_DISP,
            :ID_LIN,
            :ID_FOR,
            :PRECO_MIN_MKT,
            :USE_WEB_IMAGE,
            :BAR_CODE,
            :DETACH,
            :OPER_TYPE
  DO BEGIN
    SUSPEND;
  END
  EXECUTE PROCEDURE SP_UPDATE_REPLICADO(:ID, :ID_REP, :TABLE_NAME);
END^

CREATE OR ALTER PROCEDURE SP_INS_PEDIDO (
    ID_PED INTEGER,
    ID_PES INTEGER,
    ID_REP INTEGER,
    ID_COP SMALLINT,
    EMISSAO DATE,
    FATURA DATE,
    STATUS SMALLINT,
    TOTAL_NEG NUMERIC(10,2),
    TOTAL_SUG NUMERIC(10,2),
    OBSERV_1 VARCHAR(80),
    OBSERV_2 VARCHAR(80),
    OBSERV_3 VARCHAR(80),
    NUM_PED VARCHAR(20),
    HASHING VARCHAR(500),
    IN_STORE SMALLINT)
AS
DECLARE ID_EMP DOM_ID_EMPRESA;
DECLARE EMISSOR INTEGER = 2;
BEGIN

  STATUS = 99;
  SELECT FIRST 1 EMPRESA.ID_EMP FROM EMPRESA ORDER BY EMPRESA.EMP_DEF DESC INTO :ID_EMP;

  INSERT INTO PEDIDO (
  ID_PED,
  ID_PES,
  ID_REP,
  ID_EMP,
  ID_COP,
  EMISSAO,
  FATURA,
  STATUS,
  TOTAL_NEG,
  TOTAL_SUG,
  TOTAL_FID,
  TOTAL_MIN,
  PESO_LIQ,
  PESO_BRT,
  EMISSOR,
  OBSERV_1,
  OBSERV_2,
  OBSERV_3,
  TRANS_EMISSOR,
  PICKING_STATUS,
  COD_COMPRA,
  HASHING,
  IN_STORE)
  VALUES (:ID_PED,
          :ID_PES,
          :ID_REP,
          :ID_EMP,
          :ID_COP,
          :EMISSAO,
          :FATURA,
          :STATUS,
          :TOTAL_NEG,
          :TOTAL_SUG,
          0,
          0,
          0,
          0,
          :EMISSOR,
          :OBSERV_1,
          :OBSERV_2,
          :OBSERV_3,
          CURRENT_TIMESTAMP,
          0,
          :NUM_PED,
          :HASHING,
          :IN_STORE);

END^

CREATE OR ALTER TRIGGER AU_PRODUTO_REPL_FV FOR PRODUTO
ACTIVE AFTER UPDATE POSITION 1
AS

DECLARE SINCRON  SMALLINT = 0;
DECLARE OPERAC   SMALLINT;
DECLARE FALSE    SMALLINT = 0;
DECLARE TRUE     SMALLINT = 1;
DECLARE OPER_INS SMALLINT = 0;
DECLARE OPER_UPD SMALLINT = 1;
DECLARE OPER_DEL SMALLINT = 2;
DECLARE ID_PRO   INTEGER;
BEGIN
  IF ((INSERTING) OR (UPDATING)) THEN ID_PRO = NEW.ID_PRO;
  ELSE ID_PRO = OLD.ID_PRO;
  /* --- */
  IF (INSERTING) THEN BEGIN
    SINCRON = TRUE;
    OPERAC  = OPER_INS;
  END IF (UPDATING) THEN BEGIN
    IF ((NEW.ID_PRO         <> OLD.ID_PRO)         OR
        (NEW.DESCRICAO      <> OLD.DESCRICAO)      OR
        (NEW.ID_SBR         <> OLD.ID_SBR)         OR
        (NEW.PRECO_UNIT     <> OLD.PRECO_UNIT)     OR
        (NEW.PRECO_UNIT2    <> OLD.PRECO_UNIT2)    OR
        (NEW.PRECO_UNIT_MIN <> OLD.PRECO_UNIT_MIN) OR
        (NEW.PRECO_CUSTO    <> OLD.PRECO_CUSTO)    OR
        (NEW.DESCONTO       <> OLD.DESCONTO)       OR
        (NEW.VALID_DCT      <> OLD.VALID_DCT)      OR
        (NEW.PESO_BRT       <> OLD.PESO_BRT)       OR
        (NEW.APL_FINANC     <> OLD.APL_FINANC)     OR
        (NEW.APL_CLASSIF    <> OLD.APL_CLASSIF)    OR
        (NEW.STATUS_EST     <> OLD.STATUS_EST)     OR
        (NEW.INATIVO        <> OLD.INATIVO)        OR
        (NEW.STATUS_DISP    <> OLD.STATUS_DISP)    OR
        (NEW.PRECO_MIN_MKT  <> OLD.PRECO_MIN_MKT)  OR
        (NEW.USE_WEB_IMAGE  <> OLD.USE_WEB_IMAGE)  OR
        (NEW.BAR_CODE       <> OLD.BAR_CODE)       OR
        (NEW.DETACH         <> OLD.DETACH)         OR
        (NEW.ID_LIN         <> OLD.ID_LIN))        THEN BEGIN
      SINCRON = TRUE;
      OPERAC  = OPER_UPD;
    END
  END ELSE IF (DELETING) THEN BEGIN
    SINCRON = TRUE;
    OPERAC  = OPER_DEL;
  END
  
  IF (SINCRON = TRUE) THEN BEGIN
    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
    SELECT PERFIL_PRODUTO_REPRES.ID_REP, 'PRODUTO', :ID_PRO, :OPERAC FROM PERFIL_PRODUTO_REPRES JOIN PESSOA ON (PESSOA.ID_PES=PERFIL_PRODUTO_REPRES.ID_REP)
    JOIN REPRES ON (REPRES.ID_PES = PESSOA.ID_PES) AND PESSOA.INATIVO = :FALSE AND PERFIL_PRODUTO_REPRES.ID_PRO = :ID_PRO
    AND REPRES.FORC_VEND = :TRUE;
  END
END^

CREATE OR ALTER TRIGGER AIUD_PESSOA_REPL_FV FOR PESSOA
ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 0
AS
DECLARE SINCRON   SMALLINT = 0;
DECLARE OPERAC    SMALLINT;
DECLARE FALSE     SMALLINT = 0;
DECLARE TRUE      SMALLINT = 1;
DECLARE OPER_UPD  SMALLINT = 1;
BEGIN
  IF (UPDATING) THEN BEGIN
    IF ((OLD.ID_PES      <> NEW.ID_PES)      OR
        (OLD.INATIVO     <> NEW.INATIVO)     OR
        (OLD.E_MAIL_NFE  <> NEW.E_MAIL_NFE)  OR
        (OLD.IBGE        <> NEW.IBGE)        OR
        (OLD.ID_REG      <> NEW.ID_REG)      OR
        (OLD.FANTASIA    <> NEW.FANTASIA)    OR
        (OLD.ENDERECO    <> NEW.ENDERECO)    OR
        (OLD.BAIRRO      <> NEW.BAIRRO)      OR
        (OLD.CEP         <> NEW.CEP)         OR
        (OLD.COMPLEMENTO <> NEW.COMPLEMENTO) OR
        (OLD.ID_CID      <> NEW.ID_CID)      OR
        (OLD.RAZ_SOC     <> NEW.RAZ_SOC)) THEN BEGIN
      SINCRON = TRUE;
      OPERAC  = OPER_UPD;
    END
  END

  IF (SINCRON = TRUE) THEN BEGIN
    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
      SELECT PERFIL_CLIENTE_REPRES.ID_REP, 'PESSOA', PERFIL_CLIENTE_REPRES.ID_CLI, :OPERAC
        FROM PERFIL_CLIENTE_REPRES JOIN REPRES ON (PERFIL_CLIENTE_REPRES.ID_REP = REPRES.ID_PES)
         AND PERFIL_CLIENTE_REPRES.ID_CLI = NEW.ID_PES AND REPRES.FORC_VEND = :TRUE;

    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
      SELECT REPRES.ID_PES, 'FORNECEDOR', PESSOA.ID_PES, :OPERAC
        FROM PESSOA, REPRES
        JOIN FORNEC ON (PESSOA.ID_PES = FORNEC.ID_PES)
         AND REPRES.FORC_VEND = :TRUE AND FORNEC.ID_PES = NEW.ID_PES AND PESSOA.INATIVO = :FALSE;
  END
END^

CREATE OR ALTER TRIGGER AIUD_CONFIG_SYS_REPL_FV FOR CONFIG_SYS
ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 1
AS
DECLARE SINCRON   SMALLINT = 0;
DECLARE OPERAC    SMALLINT;
DECLARE TRUE      SMALLINT = 1;
DECLARE OPER_INS  SMALLINT = 0;
DECLARE OPER_UPD  SMALLINT = 1;
DECLARE OPER_DEL  SMALLINT = 2;
DECLARE ID_CFG    SMALLINT;
BEGIN

  IF ((INSERTING) OR (UPDATING)) THEN
    ID_CFG = NEW.ID_CFG;
  ELSE --DELETING
    ID_CFG = OLD.ID_CFG;

  /* --- */
  IF (INSERTING) THEN BEGIN
    SINCRON = TRUE;
    OPERAC  = OPER_INS;
  END IF (UPDATING) THEN BEGIN
    IF ((OLD.ID_CFG                   <> NEW.ID_CFG)          OR
        (OLD.PRAZO_MAX_COB            <> NEW.PRAZO_MAX_COB)   OR
        (OLD.LIB_CAMP_DCT_FV          <> NEW.LIB_CAMP_DCT_FV) OR
        (OLD.LIB_CAMP_PRC_FV          <> NEW.LIB_CAMP_PRC_FV) OR
        (OLD.RECALC_ITEM              <> NEW.RECALC_ITEM)     OR
        (OLD.AVISOS_FV                <> NEW.AVISOS_FV)       OR
        (OLD.FORM_CALC_MKT            <> NEW.FORM_CALC_MKT)   OR
        (OLD.APL_SUG_NEG_PED          <> NEW.APL_SUG_NEG_PED) OR
        (OLD.MAX_DIAS_FAT_FV          <> NEW.MAX_DIAS_FAT_FV) OR
        (OLD.MAX_DIAS_ABE_FV          <> NEW.MAX_DIAS_ABE_FV) OR
        (OLD.QUANT_PED_AB             <> NEW.QUANT_PED_AB)    OR
        (OLD.ORDER_PROD_FV            <> NEW.ORDER_PROD_FV)   OR
        (OLD.QUANT_ITEM_PED           <> NEW.QUANT_ITEM_PED)  OR
        (OLD.BLOQ_PED_MIN_FV          <> NEW.BLOQ_PED_MIN_FV) OR
        (OLD.MGR_PRC_MIN_MKT          <> NEW.MGR_PRC_MIN_MKT) OR
        (OLD.WEB_IMAGE                <> NEW.WEB_IMAGE)       OR
        (OLD.ACCESS_LOCATION_ADDRESS  <> NEW.ACCESS_LOCATION_ADDRESS) OR
        (OLD.PERC_SUG_NEG             <> NEW.PERC_SUG_NEG))   THEN BEGIN
      SINCRON = TRUE;
      OPERAC  = OPER_UPD;
    END
  END ELSE IF (DELETING) THEN BEGIN
    SINCRON = TRUE;
    OPERAC  = OPER_DEL;
  END
  
  IF (SINCRON = TRUE) THEN BEGIN
    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
      SELECT REPRES.ID_PES, 'CONFIG_SYS', :ID_CFG, :OPERAC
      FROM REPRES WHERE FORC_VEND = :TRUE;
  END
END^

CREATE OR ALTER PROCEDURE SEL_CONFIG_SYS (
    ID_REP INTEGER)
RETURNS (
    ID_CFG SMALLINT,
    LIB_CAMP_DCT SMALLINT,
    LIB_CAMP_PRC SMALLINT,
    RECALC_ITEM SMALLINT,
    CALC_SUG_NEG SMALLINT,
    PRAZO_MAX_COB SMALLINT,
    PERC_SUG_NEG NUMERIC(10,2),
    APL_SUG_NEG_PED SMALLINT,
    MAX_DIAS_FAT SMALLINT,
    MAX_DIAS_AB SMALLINT,
    AVISO VARCHAR(2000),
    QUANT_PED_AB SMALLINT,
    QUANT_ITEM_PED SMALLINT,
    ORDER_PROD_FV SMALLINT,
    BLOQ_PED_MIN_FV SMALLINT,
    ACCESS_LOCATION_ADDRESS SMALLINT,
    MGR_PRC_MIN_MKT SMALLINT,
    WEB_IMAGE VARCHAR(10),
    OPER_TYPE SMALLINT)
AS
DECLARE ID INTEGER;
DECLARE TABLE_NAME VARCHAR(20) = 'CONFIG_SYS';
BEGIN
  
  SELECT ID FROM SP_MIN_ID_REPL(:ID_REP, :TABLE_NAME) INTO :ID;

  FOR SELECT CONFIG_SYS.ID_CFG,
             CONFIG_SYS.LIB_CAMP_DCT_FV,
             CONFIG_SYS.LIB_CAMP_PRC_FV,
             CONFIG_SYS.RECALC_ITEM,
             CONFIG_SYS.FORM_CALC_MKT,
             CONFIG_SYS.PRAZO_MAX_COB,
             CONFIG_SYS.PERC_SUG_NEG,
             CONFIG_SYS.APL_SUG_NEG_PED,
             CONFIG_SYS.MAX_DIAS_FAT_FV,
             CONFIG_SYS.MAX_DIAS_ABE_FV,
             CONFIG_SYS.AVISOS_FV,
             CONFIG_SYS.QUANT_PED_AB,
             CONFIG_SYS.QUANT_ITEM_PED,
             CONFIG_SYS.ORDER_PROD_FV,
             CONFIG_SYS.BLOQ_PED_MIN_FV,
             CONFIG_SYS.ACCESS_LOCATION_ADDRESS,
             CONFIG_SYS.MGR_PRC_MIN_MKT,
             CONFIG_SYS.WEB_IMAGE,
             FV_REPLICATE.OPER_TYPE
       FROM  CONFIG_SYS
       JOIN  FV_REPLICATE ON (CONFIG_SYS.ID_CFG = FV_REPLICATE.FIELD_KEY)
       WHERE FV_REPLICATE.ID_REP      = :ID_REP
       AND   FV_REPLICATE.TABLE_NAME  = :TABLE_NAME
       AND   FV_REPLICATE.DATE_REPL   IS NULL
       AND  (FV_REPLICATE.REPLICATING = :ID AND FV_REPLICATE.REPLICATING > 0)
       ORDER BY FV_REPLICATE.ID_ITEM
       INTO :ID_CFG,
            :LIB_CAMP_DCT,
            :LIB_CAMP_PRC,
            :RECALC_ITEM,
            :CALC_SUG_NEG,
            :PRAZO_MAX_COB,
            :PERC_SUG_NEG,
            :APL_SUG_NEG_PED,
            :MAX_DIAS_FAT,
            :MAX_DIAS_AB,
            :AVISO,
            :QUANT_PED_AB,
            :QUANT_ITEM_PED,
            :ORDER_PROD_FV,
            :BLOQ_PED_MIN_FV,
            :ACCESS_LOCATION_ADDRESS,
            :MGR_PRC_MIN_MKT,
            :WEB_IMAGE,
            :OPER_TYPE
  DO BEGIN
    SUSPEND;
  END
  EXECUTE PROCEDURE SP_UPDATE_REPLICADO(:ID, :ID_REP, :TABLE_NAME);
END^

SET TERM ; ^

UPDATE REPLICATE_TABLE SET
    SQL_STATEMENT = 'INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE) SELECT :ID_REP, ''FORNECEDOR'', PESSOA.ID_PES, 0 FROM PESSOA, REPRES JOIN FORNEC ON (PESSOA.ID_PES = FORNEC.ID_PES) AND PESSOA.INATIVO = 0  AND REPRES.ID_PES = :ID_REP;'
WHERE (TABLE_NAME = 'FORNECEDOR');

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SEL_PESSOA (
    ID_REP INTEGER,
    SINCR SMALLINT)
RETURNS (
    ID_PES INTEGER,
    RAZ_SOC VARCHAR(60),
    FANTASIA VARCHAR(60),
    E_MAIL VARCHAR(160),
    INATIVO SMALLINT,
    TIPO_PESSOA SMALLINT,
    REG_ENTR VARCHAR(50),
    ENDERECO VARCHAR(300),
    LAST_ORDER_DAYS SMALLINT,
    OPER_TYPE SMALLINT)
AS
DECLARE ID         INTEGER;
DECLARE TABLE_NAME VARCHAR(20) = 'PESSOA';
DECLARE TP_CLIENTE SMALLINT = 0;
BEGIN

  SELECT ID FROM SP_MIN_ID_REPL(:ID_REP, :TABLE_NAME) INTO :ID;

  FOR SELECT FV_REPLICATE.FIELD_KEY AS ID_PES,
             PESSOA.RAZ_SOC,
             PESSOA.FANTASIA,
             PESSOA.INATIVO,
             PESSOA.E_MAIL_NFE,
             PESSOA.ENDERECO || ', ' || PESSOA.NUMERO || ' - ' || PESSOA.BAIRRO || ', ' || CIDADE.DESCRICAO || ' - ' || CIDADE.UF,
             REGIAO_ENTR.DESCRICAO,
             PESSOA.LAST_ORDER_DAYS,
             FV_REPLICATE.OPER_TYPE
       FROM  FV_REPLICATE 
       LEFT JOIN PESSOA      ON (PESSOA.ID_PES = FV_REPLICATE.FIELD_KEY)
       LEFT JOIN CIDADE      ON (PESSOA.ID_CID = CIDADE.ID_CID)
       LEFT JOIN REGIAO_ENTR ON (PESSOA.ID_REG = REGIAO_ENTR.ID_REG)
       WHERE FV_REPLICATE.ID_REP      = :ID_REP
       AND   FV_REPLICATE.TABLE_NAME  = :TABLE_NAME
       AND   FV_REPLICATE.DATE_REPL   IS NULL
       AND  (FV_REPLICATE.REPLICATING = :ID AND FV_REPLICATE.REPLICATING > 0)
       ORDER BY FV_REPLICATE.ID_ITEM
       INTO :ID_PES,
            :RAZ_SOC,
            :FANTASIA,
            :INATIVO,
            :E_MAIL,
            :ENDERECO,
            :REG_ENTR,
            :LAST_ORDER_DAYS,
            :OPER_TYPE
  DO BEGIN
    TIPO_PESSOA = :TP_CLIENTE;
    SUSPEND;
  END
  EXECUTE PROCEDURE SP_UPDATE_REPLICADO(:ID, :ID_REP, :TABLE_NAME);
END^

CREATE OR ALTER TRIGGER AIUD_PESSOA_REPL_FV FOR PESSOA
ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 0
AS
DECLARE SINCRON   SMALLINT = 0;
DECLARE OPERAC    SMALLINT;
DECLARE FALSE     SMALLINT = 0;
DECLARE TRUE      SMALLINT = 1;
DECLARE OPER_UPD  SMALLINT = 1;
BEGIN
  IF (UPDATING) THEN BEGIN
    IF ((OLD.ID_PES          <> NEW.ID_PES)      OR
        (OLD.INATIVO         <> NEW.INATIVO)     OR
        (OLD.E_MAIL_NFE      <> NEW.E_MAIL_NFE)  OR
        (OLD.IBGE            <> NEW.IBGE)        OR
        (OLD.ID_REG          <> NEW.ID_REG)      OR
        (OLD.FANTASIA        <> NEW.FANTASIA)    OR
        (OLD.ENDERECO        <> NEW.ENDERECO)    OR
        (OLD.BAIRRO          <> NEW.BAIRRO)      OR
        (OLD.CEP             <> NEW.CEP)         OR
        (OLD.COMPLEMENTO     <> NEW.COMPLEMENTO) OR
        (OLD.ID_CID          <> NEW.ID_CID)      OR
        (OLD.LAST_ORDER_DAYS <> NEW.LAST_ORDER_DAYS)OR
        (OLD.RAZ_SOC         <> NEW.RAZ_SOC)) THEN BEGIN
      SINCRON = TRUE;
      OPERAC  = OPER_UPD;
    END
  END

  IF (SINCRON = TRUE) THEN BEGIN
    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
      SELECT PERFIL_CLIENTE_REPRES.ID_REP, 'PESSOA', PERFIL_CLIENTE_REPRES.ID_CLI, :OPERAC
        FROM PERFIL_CLIENTE_REPRES JOIN REPRES ON (PERFIL_CLIENTE_REPRES.ID_REP = REPRES.ID_PES)
         AND PERFIL_CLIENTE_REPRES.ID_CLI = NEW.ID_PES AND REPRES.FORC_VEND = :TRUE;

    INSERT INTO FV_REPLICATE (ID_REP, TABLE_NAME, FIELD_KEY, OPER_TYPE)
      SELECT REPRES.ID_PES, 'FORNECEDOR', PESSOA.ID_PES, :OPERAC
        FROM PESSOA, REPRES
        JOIN FORNEC ON (PESSOA.ID_PES = FORNEC.ID_PES)
         AND REPRES.FORC_VEND = :TRUE AND FORNEC.ID_PES = NEW.ID_PES AND PESSOA.INATIVO = :FALSE;
  END
END^

CREATE OR ALTER PROCEDURE SEL_FORNECEDOR (
    ID_REP INTEGER)
RETURNS (
    ID_PES INTEGER,
    RAZ_SOC VARCHAR(60),
    FANTASIA VARCHAR(60),
    E_MAIL VARCHAR(160),
    INATIVO SMALLINT,
    TIPO_PESSOA SMALLINT,
    LAST_ORDER_DAYS SMALLINT,
    OPER_TYPE SMALLINT)
AS
DECLARE TP_FORNECEDOR SMALLINT = 4;
DECLARE ID            INTEGER;
DECLARE TABLE_NAME    VARCHAR(20) = 'FORNECEDOR';
BEGIN

  SELECT ID FROM SP_MIN_ID_REPL(:ID_REP, :TABLE_NAME) INTO :ID;

  FOR SELECT FV_REPLICATE.FIELD_KEY AS ID_PES,
             PESSOA.RAZ_SOC,
             PESSOA.FANTASIA,
             PESSOA.INATIVO,
             PESSOA.E_MAIL_NFE,
             PESSOA.LAST_ORDER_DAYS,
             FV_REPLICATE.OPER_TYPE
       FROM  FV_REPLICATE 
       LEFT JOIN PESSOA      ON (PESSOA.ID_PES = FV_REPLICATE.FIELD_KEY)
       WHERE FV_REPLICATE.ID_REP      = :ID_REP
       AND   FV_REPLICATE.TABLE_NAME  = :TABLE_NAME
       AND   FV_REPLICATE.DATE_REPL   IS NULL
       AND  (FV_REPLICATE.REPLICATING = :ID AND FV_REPLICATE.REPLICATING > 0)
       ORDER BY FV_REPLICATE.ID_ITEM
       INTO :ID_PES,
            :RAZ_SOC,
            :FANTASIA,
            :INATIVO,
            :E_MAIL,
            :LAST_ORDER_DAYS,
            :OPER_TYPE
  DO BEGIN
    TIPO_PESSOA = :TP_FORNECEDOR;
    SUSPEND;
  END
  EXECUTE PROCEDURE SP_UPDATE_REPLICADO(:ID, :ID_REP, :TABLE_NAME);
END^

SET TERM ; ^



