IF  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].UFD_EST_PROD_TRIB') AND TYPE IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [DBO].UFD_EST_PROD_TRIB
GO 

--   SELECT  * FROM UFD_EST_PROD_TRIB (1,203,4, 90, 6251, 0, 0, 0,1,125.86,0,0,0,0,0,0)

CREATE FUNCTION [dbo].[UFD_EST_PROD_TRIB](  @CD_EMP                 INT,
                                            @CD_FILIAL              INT,
                                            @CD_MOV_OP              INT,
                                            @CD_DEST_ORIG           INT,
                                            @CD_PROD                VARCHAR(MAX),
                                            @GERA_DARE              INT,
                                            @TP_MOV                 INT,
                                            @TP_MOV_OP_TRANSA       INT,
                                            @QT_PROD                MONEY,
                                            @VLR_IT                 MONEY,
                                            @PERC_DESC_IT           MONEY,
                                            @PERC_IPI               MONEY,
                                            @VLR_FRETE              MONEY,
                                            @VLR_OUTRAS             MONEY,
                                            @TIPO_OPERACAO          INT,
                                            @ALIQ_ICMS_ENTRADA      MONEY)
/*
DECLARE @CD_EMP INT,
@CD_FILIAL INT,
@CD_MOV_OP INT,
@CD_DEST_ORIG INT,
@CD_PROD VARCHAR(MAX),
@GERA_DARE INT,
@TP_MOV INT,
@TP_MOV_OP_TRANSA INT,
@QT_PROD MONEY,
@VLR_IT MONEY,
@PERC_DESC_IT MONEY,
@PERC_IPI MONEY,
@VLR_FRETE MONEY,
@VLR_OUTRAS MONEY
SET @CD_EMP=1
SET @CD_FILIAL=2
SET @CD_MOV_OP=0
SET @CD_DEST_ORIG=0
SET @CD_PROD=38878
SET @GERA_DARE=0
SET @TP_MOV=0
SET @TP_MOV_OP_TRANSA=1
SET @QT_PROD=0
SET @VLR_IT=0
SET @PERC_DESC_IT=0
SET @PERC_IPI=0
SET @VLR_FRETE=0
SET @VLR_OUTRAS=0
*/
RETURNS @RS_RETURN TABLE(CD_ID                                  INT,    
                            CD_EMP                              INT,
                            CD_PROD                             INT,    
                            CD_CTR                              INT,    
                            CD_TRIB_CST                         INT,
                            UF                                  VARCHAR(2),
                            UF_FILIAL                           VARCHAR(2),
                            TP_MOV                              INT,
                            TP_TRANSA                           INT,
                            TP_LISTA                            INT,
                            DS_TP_LISTA                         VARCHAR(20),
                            NR_NCM                              VARCHAR(50) ,
                            NR_CEST                             VARCHAR(20),
                            CD_CLASS_FISC                       INT,
                            DS_CLASS_FISC                       VARCHAR(200),
                            PERC_RED                            MONEY ,
                            PERC_ICMS                           MONEY,
                            TP_RED_BASE_IMP                     MONEY ,
                            PERC_RED_SUBS_TRIB                  MONEY ,
                            PERC_RED_ESTORNO                    MONEY ,
                            PERC_REPASSE                        MONEY ,
                            PERC_IBPT                           MONEY,
                            TP_GERACAO_DARE                     INT,
                            FLAG_TRIB_DARE                      INT,
                            VLR_PAUTA_IVA                       MONEY,
                            TP_SUBS                             INT,
                            DS_TP_SUBS                          VARCHAR(100),    
                            PERC_ICMS_EST                       MONEY,
                            PERC_ICMS_ORIGEM                    MONEY,
                            HIST                                VARCHAR(500),
                            CD_TRIB                             INT,
                            CD_TRIB_FC                          VARCHAR(5),
                            ICMS                                INT,
                            SUBS_TRIB                           INT,
                            ICMS_RD                             INT,
                            SUBS_TRIB_ANT                       INT,
                            ISENTO                              INT,
                            TIPO_REGIME_EMPRESA                 INT,
                            FLAG_VLR_SUBST_TOTAL_PROD           INT, 
                            FLAG_GERAR_ICMS_SUBS_DARE           INT, 
                            FLAG_OUTROS                         INT,
                            ORIGEM_PRODUTO                      INT,
                            DS_ORIGEM_PRODUTO                   VARCHAR(500),
                            CD_TBL_MVA                          INT,
                            CD_REGIAO_FISCAL                    INT,
                            NM_REGIAO_FISCAL                    VARCHAR(200),
                            VLR_PAUTA_IVA_ORIGINAL              MONEY,
                            MVA_AJUSTADO                        INT,
                            TIPO_TRIBUTACAO_PIS_COFINS          INT,
                            SIGLA_EXCECAO_PIS_COFINS            VARCHAR(10),
                            FLAG_EXCECAO_PIS_COFINS             INT,
                            PIS_COFINS                          INT,
                            FLAG_NAO_TRIBUTAR_PIS_COFINS_MOV_OP INT,
                            FLAG_TRIB_PIS_COFINS_PROD           INT,
                            CTR_PMC                             INT,
                            FLAG_GERAR_ICMS_RETIDO              INT,
                            PERC_RED_IMPORTADO                  MONEY,
                            PERC_RED_SUBS_TRIB_IMPORTADO        MONEY,
                            TIPO_REGIME_EMPRESA_GRUPO_TRIB      INT,
                            DS_TIPO_REGIME_EMPRESA_GRUPO_TRIB   VARCHAR(100),
                            TIPO_REGIME_DESTINATARIO_ORIGEM     INT,
                            DS_TIPO_REGIME_DESTINATARIO_ORIGEM  VARCHAR(100),
                            OBS_GRUPO_TRIB_NF                   VARCHAR(200),
                            EXCECAO_MVA_PRODUTO                 INT,
                            MODALI_BASE_ICMS_SUBST              INT,
                            DS_MODALI_BASE_ICMS_SUBST           VARCHAR(200),
                            PRECO_BASE_SUBS                     INT,
                            PERC_REDU_PMC_FORN                  MONEY,
                            TIPO_REGIME_FILIAL                  INT,
                            DS_TIPO_REGIME_FILIAL               VARCHAR(200),
                            TRIBUTACAO_FORNECEDOR               VARCHAR(3),
                            VLR_PMC                             MONEY,
                            VLR_ICMS_SUBS_GARANTIR              MONEY,
                            PERC_ICMS_DESTINO_DIFAL             MONEY,
                            PERC_ICMS_ORIGEM_DIFAL              MONEY, 
                            GERAR_DIFAL                         INT,
                            GERAR_DAR                           INT,
                            CONTRIBUINTE                        INT,
                            FLAG_GERAR_MOV_DARE                 INT,
                            GERAR_SUBS_TRIB                     INT,
                            TP_SUBS_TRIB                        INT,
                            PERC_ALIQ_FECOP                     MONEY,
                            FLAG_FRETE_BASE_ICMS                MONEY,
                            FCOP                                INT,
                            ICMS_DESONERADO                     INT,
                            MOTIVO_DESONERADO_ICMS              INT,
                            VLR_ICMS_DESONERADO                 MONEY,
                            PERC_ICMS_DESONERADO                MONEY,
                            CD_BENEF                            VARCHAR(10) ,
                            QT_PROD                             INT, -- RETORNO APARTIR DOS APPLY
                            VLR_IT                              MONEY,
                            VLR_DESC_IT                         MONEY,
                            VLR_FRETE                           MONEY,
                            VLR_OUTROS                          MONEY,
                            VLR_IPI                             MONEY,
                            VLR_BASE_ICMS                       MONEY,
                            VLR_ICMS                            MONEY,    
                            VLR_BASE_SUBS                       MONEY,    
                            VLR_ICMS_SUBS                       MONEY,
                            VLR_BASE_ICMS_OP                    MONEY,
                            VLR_ICMS_OP                         MONEY,
                            VLR_BASE_RET                        MONEY,
                            VLR_ICMS_RET                        MONEY,
                            VLR_CUSTO                           MONEY,
                            VLR_REA                             MONEY,
                            VLR_BASE_FECOP                      MONEY,
                            VLR_BASE_FECOP_SUBS                 MONEY,
                            VLR_BASE_FECOP_RET                  MONEY,
                            VLR_FECOP                           MONEY,
                            VLR_FECOP_ST                        MONEY,
                            VLR_FECOP_RET                       MONEY    ,
                            CD_NT_OP                            INT,
                            CD_NT_OP_FC                         VARCHAR(10),
                            CST_PIS                             INT,
                            PERC_PIS                            MONEY,
                            VLR_BASE_PIS                        MONEY,
                            VLR_PIS                             MONEY,
                            CST_COFINS                          INT,
                            PERC_COFINS                         MONEY,
                            VLR_BASE_COFINS                     MONEY,
                            VLR_COFINS                          MONEY,
                            MOV_FLAG_NAO_TRIBUTAR_PIS_COFINS    INT,
                            PROD_PIS_COFINS                     INT,
                            PROD_TIPO_TRIBUTACAO_PIS_COFINS     INT,
                            PROD_FLAG_TRIB_PIS_COFINS_PROD      INT,
                            PROD_LISTA_PROD                     INT,
                            NT_OP_CST                           INT, 
                            CST_TP_TRIB_PIS                     INT,
                            CST_TP_TRIB_COFINS                  INT,
                            PERC_DIFERIMENTO                    MONEY,
							VLR_DIFERIMENTO                     MONEY,
							CD_PROC_FISCAL_REFERENCIADO			INT)
AS
BEGIN
        
	INSERT INTO
		@RS_RETURN
	SELECT
		*
	FROM
		UFD_EST_PROD_TRIB_UF (@CD_EMP
							,@CD_FILIAL
							,@CD_MOV_OP
							,@CD_DEST_ORIG
							,@CD_PROD
							,@GERA_DARE
							,@TP_MOV
							,@TP_MOV_OP_TRANSA
							,@QT_PROD
							,@VLR_IT
							,@PERC_DESC_IT
							,@PERC_IPI
							,@VLR_FRETE
							,@VLR_OUTRAS
							,@TIPO_OPERACAO
							,@ALIQ_ICMS_ENTRADA
							,'')
    RETURN     
END
GO


