USE [portalfornecedores]
GO
/****** Object: StoredProcedure [dbo].[portal_concurso_getArtigos] Script Date: 07/10/2015 16:14:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ricardo Antunes
-- Create date: 07/10/2015
-- Description: data for concurso reply by fornecedor
-- =============================================
ALTER PROCEDURE [dbo].[portal_concurso_getForReply]
-- Add the parameters for the stored procedure here
@id_concurso int = 0,
@reg_start int = 0,
@reg_end int = 0
AS
BEGIN
--header select (concurso)
SELECT * FROM (SELECT * FROM portal_concurso AS c
WHERE c.id_concurso = @id_concurso) AS header;
-- select artigos
SELECT * FROM
(
SELECT ROW_NUMBER() OVER ( ORDER BY a.id_artigo ) AS RowNum,
a.[id_artigo], a.[id_artigo_sicaf], a.[quantidade], sa.id_cartigo, sa.descricao,
sb.[ref_forn], sc.[data_ini], sc.[data_fim], sc.[quantidade] as qtdComercial,
sc.[moeda], sc.[preco_base_moeda], sc.[cambio], sc.[preco_base_euros], sc.[unidade_base],
sc.[desconto1], sc.[desconto2], sc.[desconto3], sc.[lead_time]
FROM portal_concurso_artigo as a
INNER JOIN sicaf_artigo AS sa ON a.id_artigo_sicaf = sa.id_cartigo
LEFT JOIN sicaf_fornecedor_artigo AS sb ON a.id_artigo_sicaf = sb.id_cartigo
--LEFT JOIN sicaf_condcomerciais_artigo AS sc ON sb.id = sc.id_artforn
OUTER APPLY (select top 1 * from sicaf_condcomerciais_artigo sc
where sc.id_artforn = sb.id order by data_fim ) sc
WHERE a.id_concurso = @id_concurso
) AS result
WHERE RowNum >= @reg_start AND RowNum <= @reg_end
ORDER BY RowNum;
-- select count
SELECT COUNT(*) as num_rows
FROM portal_concurso_artigo as a
WHERE a.id_concurso = @id_concurso ;
END