
/****** Object:  View [dbo].[VRESTHFAL]    Script Date: 05/21/2013 14:20:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




IF EXISTS (SELECT * FROM sys.objects 
   WHERE object_id = OBJECT_ID(N'DSS_VSTHistFal') )
DROP VIEW DSS_VSTHistFal
GO


create       VIEW [dbo].[DSS_VSTHistFal]
As
SELECT 
	   hfal.id_fali,
    --   mdau.id_mdau,
    --   arau.id_arau,
       hfal.id_fact
       --,
   --    hfal.id_arti,
   --    cod_arti  = LTRIM(RTRIM(arti.cod_arti)),
   --    desc_arti = LTRIM(RTRIM(arti.desc_arti)), 
   --    arti.cod_alte,
   --    hfal.precio_unit,
   --    hfal.precio_neto,
   --    precio_costo = ROUND( CONVERT(money, CASE WHEN arco.id_arti is NULL     
			--   		  THEN arti.precio_costo / arti.factor_div  
			--   		  ELSE arco.p_costo / arti.factor_div 
		 --      		     END
			--     ) , 2),
   --    cantidad        = dbo.Get_Cant_Art_Post_Hist(hfal.id_fali),
   --    cant_devol      = dbo.Get_Cant_Art_Devol_Hist(hfal.id_fali), 
   --    cant_anul       = dbo.Get_Cant_Art_Anul_Hist(hfal.id_fali), 
   --    cant_nomarchada = dbo.Get_Cant_Art_NoMarchados_Hist(hfal.id_fali),
   --    modi.cod_modi, 
   --    modi.desc_modi, 
   --    modi.tipo_modi, 
   --    importe_mod   = COALESCE(hfal.importe_mod, 0),
   --    hfal.importe_mod_neto,
   --    hfal.importe_neto,
   --    hfal.dcto_neto,
   --    hfal.recargo_neto,
   --    dcto_noccasa,
   --    recargo_noccasa,
   --    importe_neto_cont,
   --    importe_bruto    = dbo.Get_Importe_Bruto_Hist(hfal.id_fali),
   --    id_impr          = COALESCE(arau.id_impr,enfa.id_impr),
   --    impr_cocina      = CASE WHEN arau.id_impr is  null 
			--       THEN impr.desc_impr 
	 	--	       ELSE impr1.desc_impr 
			--  END,
   --    id_fami = COALESCE (arfa.id_fami, arti.id_fami), 
   --    desc_fami = CASE WHEN arfa.id_fami IS NOT NULL 
			--THEN fami1.desc_fami 
			--ELSE fami.desc_fami 
		 -- END,
   --    id_sufa = COALESCE (arfa.id_sufa, suar.id_sufa),
   --    mdau.id_modi, 
   --    mdau.automatico, 
   --    enfa.id_enfa,
   --    hfal.inc_cont,
   --    hfac.id_exen,
   --    moau.id_pvta,
   --    id_oper_modi,
   --    nom_oper_modi     = COALESCE(oper_modi.nombre,''),
   --    desc_oper_modi     = COALESCE(oper_modi.descripcion,'')	
FROM  dbo.RESTHFAL hfal 
   INNER JOIN dbo.RESTHFAC hfac on hfal.id_fact = hfac.id_fact 
   --Inner Join RESTMOAU  moau    on hfac.id_moau = moau.id_moau
   --INNER JOIN dbo.RESTARTI  arti on hfal.id_arti = arti.id_arti 
   --LEFT JOIN  dbo.RESTARAU  arau on hfal.id_arti = arau.id_arti and
			--	    arau.id_moau = hfac.id_moau 
   --INNER JOIN dbo.RESTFAMI fami  ON arti.id_fami = fami.id_fami 
   --LEFT JOIN  dbo.RESTARFA arfa  ON arau.id_arau = arfa.id_arau 
   --LEFT JOIN  dbo.RESTSUAR suar  ON arti.id_arti = suar.id_arti 
   --LEFT JOIN  dbo.RESTFAMI fami1 ON arfa.id_fami = fami1.id_fami 
   --LEFT JOIN  dbo.RESTENFA enfa  ON hfac.id_moau = enfa.id_moau AND 
   --                        	    enfa.id_fami = COALESCE (arfa.id_fami, arti.id_fami) 
   --LEFT JOIN  dbo.RESTMODI modi  on hfal.id_modi = modi.id_modi 
   --LEFT JOIN  dbo.RESTMDAU mdau  on hfal.id_modi = mdau.id_modi and
			--	    mdau.id_moau = hfac.id_moau	
   --Left Join  dbo.RESTARCO arco  ON hfal.id_arti = arco.id_arti and
			--	    moau.id_pvta = arco.id_pvta
   --LEFT JOIN  dbo.RESTIMPR impr1 ON arau.id_impr = impr1.id_impr 
   --LEFT JOIN  dbo.RESTIMPR impr  ON enfa.id_impr = impr.id_impr 
   --LEFT  Join OPERADOR oper_modi on hfal.id_oper_modi = oper_modi.id_oper  




































GO


