--USE [TouchScreen]
--GO

/****** Object:  View [dbo].[VRESTFALI]    Script Date: 08/19/2016 14:21:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DROP VIEW [dbo].[WRESTFALI]
GO

CREATE VIEW [dbo].[WRESTFALI]
AS
Select fali.id_fali, 
       mdau.id_mdau, 
       arau.id_arau, 
       fali.id_fact, 
       fali.id_arti,
       cod_arti  = LTRIM(RTRIM(arti.cod_arti)),
       desc_arti = LTRIM(RTRIM(arti.desc_arti)),
       arti.cod_alte,
       fali.precio_unit, 
       fali.precio_neto,
       precio_costo = 
          ROUND( CONVERT(money, 
                         CASE WHEN arco.id_arti is NULL     -- PRECIO DE COSTO UNITARIO
			   		          THEN arti.precio_costo / arti.factor_div  
			   		          ELSE 
			   		          CASE WHEN COALESCE(arco.p_costo,0) = 0 
						           THEN arti.precio_costo / arti.factor_div 
						           ELSE arco.p_costo / arti.factor_div 
					          END 
		       		     END
			     ), 2 ),
       cantidad         = dbo.Get_Cant_Art_Post(fali.id_fali),
       cant_devol       = dbo.Get_Cant_Art_Devol(fali.id_fali),
       cant_anul        = dbo.Get_Cant_Art_Anul(fali.id_fali),
       cant_nomarchada  = dbo.Get_Cant_Art_NoMarchados(fali.id_fali),
       cod_modi  	= COALESCE(modi.cod_modi ,''),
       desc_modi 	= COALESCE(modi.desc_modi,''),
       modi.tipo_modi,	
       fali.importe_mod,
       fali.importe_mod_neto,
       fali.importe_neto,
       fali.dcto_neto,
       fali.recargo_neto,
       dcto_noccasa,
       recargo_noccasa,
       importe_neto_cont,
       importe_bruto    = dbo.Get_Importe_Bruto(fali.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,
       fali.id_modi,
       mdau.automatico,
       enfa.id_enfa,
       id_sufa = COALESCE(arfa.id_sufa, suar.id_sufa),
       fali.inc_cont,
       fact.id_exen,
       moau.id_pvta,
       id_oper_modi,
       nom_oper_modi = COALESCE(oper_modi.nombre,''),
       desc_oper_modi= COALESCE(oper_modi.descripcion,''),
       PorcImpuesto = dbo.Get_ImpFamilia(fami.id_fami,enfa.id_enfa),
       0 tasa_cos_inv,
       0 tasa_fact	
  From dbo.RESTFALI fali
	  Inner Join RESTFACT  fact   on fali.id_fact = fact.id_fact
	  Inner Join RESTMOAU  moau   on fact.id_moau = moau.id_moau
	  Inner Join RESTARTI  arti   on fali.id_arti = arti.id_arti
	  Inner Join RESTFAMI  fami   on arti.id_fami = fami.id_fami
	  Left  Join RESTARAU  arau   on fali.id_arti = arau.id_arti 
	   and  arau.id_moau = fact.id_moau 
	  LEFT  Join RESTARFA arfa   on arau.id_arau = arfa.id_arau 
	  LEFT  Join RESTFAMI fami1  on arfa.id_fami = fami1.id_fami
	  LEFT  Join RESTENFA enfa   on fact.id_moau = enfa.id_moau 
	   and  enfa.id_fami = COALESCE(arfa.id_fami,arti.id_fami)
	  Left  Join RESTSUAR suar   on fali.id_arti = suar.id_arti
	  Left  Join RESTMODI modi   on fali.id_modi = modi.id_modi
	  Left  Join RESTMDAU mdau   on fali.id_modi = mdau.id_modi 
	   and  mdau.id_moau = fact.id_moau	
	  Left  Join RESTARCO arco   on fali.id_arti = arco.id_arti 
	   and	moau.id_pvta = arco.id_pvta 
	  LEFT  Join RESTIMPR impr1  on arau.id_impr = impr1.id_impr
	  LEFT  Join RESTIMPR impr   on enfa.id_impr = impr.id_impr
	  LEFT  Join OPERADOR oper_modi on fali.id_oper_modi = oper_modi.id_oper


GO


