ALTER TABLE ITEMPRODUTO ADD BRS_INTEGRAR VARCHAR2(01)
/
ALTER TABLE ITEMPRODUTO ADD BRS_INTEGRADO VARCHAR2(01)
/
ALTER TABLE ITEMPRODUTO ADD BRS_OBS_INTEGRACAO VARCHAR2(4000)
/

CREATE OR REPLACE VIEW USR_BRSEN_NOTAFISCAL AS
   SELECT NF.NRACESSONFE  AS CHAVE_NFE,
          NF.NRNOTAFISC   AS NRO_NOTA,
          LPAD(NF.CDSERIEENTR,3,'0') AS SERIE_NOTA,
          TO_CHAR(NF.DTEMISSAO,'YYYY/MM/DD') DATA_EMISSAO_DA_NOTA,
          NF.DTEMISSAO    AS Dt_Emissao,
          USR_DESC_DT_VENCIMENTO(NF.CDFILIAL, NF.NRLANCTONF) AS VENC1,
          '' AS VENC2,
          '' AS VENC3,
          '' AS VENC4,
          '' AS VENC5,
          '' AS VENC6,
          NF.DTENTRSAID   AS Dt_Digitacao,
          F.NRINSJURFORN AS CNPJ_FORNECEDOR,
          A.NRINSJURFILI AS Cnpj_Filial,
          I.NRAUTOFORN AS Nr_AF,
          P.CDPRODINTE AS CODIGO_PROTHEUS,
          REPLACE(I.QTITEMPROD,',','.') AS NRO_EMBALAGENS,
          REPLACE(I.VRUNITITEM,',','.') AS PRECO_UNITARIO,
          I.VRTOTAITEM AS Vr_Total_Item,
          I.BRS_INTEGRAR AS INTEGRAR,
          I.BRS_INTEGRADO AS INTEGRADO,
          I.BRS_OBS_INTEGRACAO AS OBS_INTEGRACAO,
          NF.NRNOTAFISC || NF.CDSERIEENTR || F.NRINSJURFORN AS QUEBRA,
          NF.NRLANCTONF AS ID,
          CASE
            when A.NRINSJURFILI = '17070693000272' then '01002'
            when A.NRINSJURFILI = '17070693000515' then '01005'
            when A.NRINSJURFILI = '17070693000604' then '01006'
            when A.NRINSJURFILI = '17070693000868' then '01008'
            when A.NRINSJURFILI = '17070693001082' then '01010'
            else '0'
          END AS UNIDADENEGOCIO,
          CASE
            when A.NRINSJURFILI = '17070693000272' then '112030010002'
            when A.NRINSJURFILI = '17070693000515' then '112030010005'
            when A.NRINSJURFILI = '17070693000604' then '112030010006'
            when A.NRINSJURFILI = '17070693000868' then '112030010008'
            when A.NRINSJURFILI = '17070693001082' then '112030010010'
            else '0'
          END AS CENTRO_CUSTO,
          '1' AS BSL_DANFE
     FROM ITEMPRODUTO I INNER JOIN NOTAFISCAL NF ON I.CDFILIAL    = NF.CDFILIAL
                                                 AND I.NRLANCTONF = NF.NRLANCTONF
                        INNER JOIN PRODUTO P ON P.CDPRODUTO = I.CDPRODUTO,
          FORNECEDOR F, FILIAL A
    WHERE NF.IDENTRSAIDOP = 'E'
      AND NF.CDTIPOOPER IN ('01','11')
      AND F.CDFORNECED = NF.CDFORNECED
      AND A.CDFILIAL   = NF.CDFILIRECE
      AND I.BRS_INTEGRAR = '1'
      AND (I.BRS_INTEGRADO IS NULL OR I.BRS_INTEGRADO= '0')
/


-- VERSAO --
BEGIN ATUVEROBJ('USR_BRSEN_NOTAFISCAL','6.39.021'); END;
/
