ALTER PROCEDURE [dbo].[pos4_ArtikliDan]
	(
		@Datum datetime,
		@Placanje smallint = 2,
		@Objekat varchar(6)
	)
AS
-- IZMENJENO 7.7.23 zbirno artikli za karticu i kredit odnosno biznis karticu
BEGIN
	SET NOCOUNT ON
	declare @strDat0 as nvarchar(10)
	declare @strDat1 as nvarchar(10)
	declare @Dat0 as datetime
	declare @Dat1 as datetime
	
	set @strDat0 = convert(nvarchar(10), @Datum, 126)
	set @strDat1 = convert(nvarchar(10), DateAdd(d, 1, @Datum), 126)
	set @Dat0 = convert(datetime, @strDat0 + ' 00:00:01', 102)
	set @Dat1 = convert(datetime, @strDat0 + ' 23:59:59', 102)
	DECLARE @Dat as nvarchar(10)
	SET @Dat = convert(nvarchar(10), @Datum, 102)
	declare @Artikli table (Datum datetime, Dokument varchar(50), Broj int, Placanje int, Partner varchar(8), Sifra int, Cena money, Stopa smallmoney, TarifaA varchar(1), Tarifa varchar(4), Kolicina money, Iznos money, Popust money, Rabat smallmoney)
	if @Placanje = 1 BEGIN
	-- RACUN FAKTURE POJEDINAČNO
		insert into @Artikli (Datum , Dokument , Broj , Placanje , Partner , Sifra, Cena , Stopa , TarifaA , Kolicina, Iznos, Popust , Rabat )
		SELECT     dbo.DateValue(dbo.KasaFP.Datum) AS Datum, dbo.KasaFP.Dokument, dbo.KasaFP.Broj, dbo.KasaFP.Placanje, dbo.KasaFP.Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA,  SUM(dbo.KasaFPSt.kolicina) AS kolicina,
							ROUND(SUM(dbo.KasaFPSt.Kolicina * dbo.KasaFPSt.Cena),4) AS iznos, ROUND(SUM((dbo.KasaFPSt.kolicina * dbo.KasaFPSt.cena) * (cast(dbo.KasaFPSt.Rabat as Money) / 100)), 4) AS popust, cast(dbo.KasaFPSt.Rabat as smallmoney) as Rabat
		FROM         dbo.KasaFPSt INNER JOIN
							dbo.KasaFP ON dbo.KasaFPSt.Broj = dbo.KasaFP.Broj
		WHERE    (dbo.KasaFP.Objekat = @Objekat) and ((dbo.KasaFP.ZakljucenDan) <> 0 ) and (dbo.KasaFP.Placanje = @Placanje) AND (dbo.KasaFP.Datum BETWEEN @Dat0 AND @Dat1)
		GROUP BY dbo.DateValue(dbo.KasaFP.Datum), dbo.KasaFP.Dokument, dbo.KasaFP.Broj, dbo.KasaFP.Placanje, dbo.KasaFP.Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, dbo.KasaFPSt.Rabat
		ORDER BY dbo.KasaFP.Broj, dbo.KasaFPSt.Sifra
	END
	if @Placanje = 3 BEGIN
	-- KARTICE PO VRSTI
		insert into @Artikli (Datum , Dokument , Broj , Placanje , Partner , Sifra, Cena , Stopa , TarifaA , Kolicina, Iznos, Popust , Rabat )
		SELECT     dbo.DateValue(dbo.KasaFP.Datum) AS Datum, CAST(N'C0000' AS nvarchar(10)) as Dokument, CAST(0 AS INT) AS Broj, dbo.KasaFP.Placanje, '0000' as Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, SUM(dbo.KasaFPSt.kolicina) AS kolicina,
							ROUND(SUM(dbo.KasaFPSt.Kolicina * dbo.KasaFPSt.Cena),4) AS iznos, ROUND(SUM((dbo.KasaFPSt.kolicina * dbo.KasaFPSt.cena) * (cast(dbo.KasaFPSt.Rabat as Money) / 100)), 4) AS popust, cast(dbo.KasaFPSt.Rabat as smallmoney) as Rabat
		FROM         dbo.KasaFPSt INNER JOIN
							dbo.KasaFP ON dbo.KasaFPSt.Broj = dbo.KasaFP.Broj
		WHERE   (dbo.KasaFP.Objekat = @Objekat) and ((dbo.KasaFP.ZakljucenDan) <> 0  ) AND  (dbo.KasaFP.Placanje = @Placanje) AND (dbo.KasaFP.Datum BETWEEN @Dat0 AND @Dat1)
		GROUP BY dbo.DateValue(dbo.KasaFP.Datum), dbo.KasaFP.Placanje, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, dbo.KasaFPSt.Rabat	
		ORDER BY dbo.KasaFPSt.Sifra
	END
	if @Placanje = 4 BEGIN
	-- biznis kartica zbirno
		insert into @Artikli (Datum , Dokument , Broj , Placanje , Partner , Sifra, Cena , Stopa , TarifaA , Kolicina, Iznos, Popust , Rabat )
		SELECT     dbo.DateValue(dbo.KasaFP.Datum) AS Datum, CAST(N'K0000' AS nvarchar(10)) as Dokument, CAST(0 AS INT) AS Broj, dbo.KasaFP.Placanje, '0000' as Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, SUM(dbo.KasaFPSt.kolicina) AS kolicina,
							ROUND(SUM(dbo.KasaFPSt.Kolicina * dbo.KasaFPSt.Cena),4) AS iznos, ROUND(SUM((dbo.KasaFPSt.kolicina * dbo.KasaFPSt.cena) * (cast(dbo.KasaFPSt.Rabat as Money) / 100)), 4) AS popust, cast(dbo.KasaFPSt.Rabat as smallmoney) as Rabat
		FROM         dbo.KasaFPSt INNER JOIN
							dbo.KasaFP ON dbo.KasaFPSt.Broj = dbo.KasaFP.Broj
		WHERE    (dbo.KasaFP.Objekat = @Objekat) and ((dbo.KasaFP.ZakljucenDan) <> 0  ) AND (dbo.KasaFP.Placanje = @Placanje) AND (dbo.KasaFP.Datum BETWEEN @Dat0 AND @Dat1)
		GROUP BY dbo.DateValue(dbo.KasaFP.Datum), dbo.KasaFP.Placanje, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, dbo.KasaFPSt.Rabat	
		ORDER BY dbo.KasaFPSt.Sifra
	END
	if @Placanje = 2 BEGIN
	-- GOTOVINA SAMO ZBIRNO
		insert into @Artikli (Datum , Dokument , Broj , Placanje , Partner , Sifra, Cena , Stopa , TarifaA , Kolicina, Iznos, Popust , Rabat )
		SELECT     dbo.DateValue(dbo.KasaFP.Datum) AS Datum, CAST(N'00000' AS nvarchar(10)) as Dokument, CAST(0 AS INT) AS Broj, dbo.KasaFP.Placanje, dbo.KasaFP.Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, SUM(dbo.KasaFPSt.kolicina) AS kolicina,
							ROUND(SUM(dbo.KasaFPSt.Kolicina * dbo.KasaFPSt.Cena),4) AS iznos, ROUND(SUM((dbo.KasaFPSt.kolicina * dbo.KasaFPSt.cena) * (cast(dbo.KasaFPSt.Rabat as Money) / 100)), 4) AS popust, cast(dbo.KasaFPSt.Rabat as smallmoney) as Rabat
		FROM         dbo.KasaFPSt INNER JOIN
							dbo.KasaFP ON dbo.KasaFPSt.Broj = dbo.KasaFP.Broj
		WHERE    (dbo.KasaFP.Objekat = @Objekat) and ((dbo.KasaFP.ZakljucenDan) <> 0  ) AND (dbo.KasaFP.Placanje = @Placanje) AND (dbo.KasaFP.Datum BETWEEN @Dat0 AND @Dat1)
		GROUP BY dbo.DateValue(dbo.KasaFP.Datum), dbo.KasaFP.Placanje, dbo.KasaFP.Partner, dbo.KasaFPSt.Sifra, dbo.KasaFPSt.Cena, dbo.KasaFPSt.Stopa, dbo.KasaFPSt.TarifaA, dbo.KasaFPSt.Rabat	
		ORDER BY dbo.KasaFPSt.Sifra
	END
	--update art set art.Tarifa = a.Tarifa from @Artikli art inner join Artikli a on art.Sifra = a.Sifra
	update art set art.Tarifa = 'P2' from @Artikli art where stopa < 15 and stopa > 0
	update art set art.Tarifa = 'P1' from @Artikli art where stopa >= 15
	update art set art.Tarifa = '0' from @Artikli art where stopa = 0
	
	select Datum , Dokument , Broj , Placanje , Partner , Sifra, Cena , Stopa , TarifaA , Tarifa , Kolicina, Iznos, Popust , Rabat
	from @Artikli
	RETURN
END