
CREATE procedure [dbo].[_AdicionalesDespacho]	
@p_idalquiler as INT = 0

as begin
select itemalq.id,itemalq.itemdocumento as adicional, itemalq.id_catalogo,itemalq.cantidadplaneado as cantalq,sum(despa.cantidadPlaneado) as despachado from doc_itemDocumento as itemalq 
 inner join doc_itemdocumento as despa  on despa.id_itemDocumento  = itemalq.id 
 inner join doc_documento as salidas on salidas.id = despa.id_documento
 where  
 (itemalq.id_catalogo in (select id from [cat_catalogo.otro] where active=1 and periodoalquiler<1 ) or  
 itemalq.id_catalogo in (select id from [cat_catalogo.repuesto]where active= 1) )
  and   itemalq.active=1 and itemalq.id_documento in(@p_idalquiler)
  and itemalq.id_catalogo not in (select id_catalogo from equ_detallealquiler where active=1 and [id_documento.alquiler] in (@p_idalquiler) and id_Catalogo>0 ) 
  and itemalq.id_catalogo not in (select  i.id_catalogo from doc_itemdocumento i
  inner join alq_tarifa f on f.id_itemdocumento=i.id 
  where f.active=1 
  and i.active=1 
  and  i.id_documento in(@p_idalquiler)) 
  and despa.id_catalogo in (select id from [cat_catalogo.otro] where active=1 and periodoalquiler<1  )
  and salidas.id in (select id from [doc_documento.salida] where active =  1)  
  group by itemalq.id_catalogo,itemalq.id, itemalq.itemdocumento,itemalq.cantidadplaneado

  end