
GO

/****** Object:  StoredProcedure [dbo].[TITEMSONALISGUNCELLE]    Script Date: 03.10.2017 11:40:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[TITEMSONALISGUNCELLE]  
@STOKID int,
@ZORLAGUNCELLE bit = 0
AS BEGIN
IF ISNULL(@STOKID,0)=0  
BEGIN
   IF  @ZORLAGUNCELLE = 1 
   BEGIN
     RAISERROR('Toplu stok maliyeti güncellemede zorla güncelleme yapılamaz!', 16, 1)
     RETURN 1
   END
   
   BEGIN TRANSACTION 

	-- ROUND 4 OLSUN
	-- NULL DEĞERLER OLABİLİR 
	UPDATE TITEM SET MALIYET = 0 WHERE MALIYET IS NULL AND MALIYETGUNCELLE = 1
	-- BİRİMİ KUTU OLANLARI AYNEN ALALIM
	UPDATE TITEM 
	SET TITEM.MALIYET = ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100), 4)
	FROM TITEM INNER JOIN 
		STOK ON (STOK.STOKID=TITEM.STOKID) LEFT OUTER JOIN 
		STOKGRUP ON STOK.GRUP = STOKGRUP.KODU
	WHERE STOK.SALISFIYAT IS NOT NULL AND ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100), 4) <> TITEM.MALIYET AND 
		 TITEM.STOKBIRIMIKUTU=1 AND IIPTAL=0 AND MALIYETGUNCELLE = 1

	-- BİRİMİ ADET OLANLARI ÇARPIP ALALIM
	UPDATE TITEM SET TITEM.MALIYET = ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100) * CASE WHEN TITEM.KUTUICIADET = 0 THEN 1 ELSE TITEM.KUTUICIADET END, 4) 
	FROM TITEM INNER JOIN 
	    STOK ON (STOK.STOKID=TITEM.STOKID) LEFT OUTER JOIN
		STOKGRUP ON STOK.GRUP = STOKGRUP.KODU
	WHERE STOK.SALISFIYAT IS NOT NULL AND ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100) * CASE WHEN TITEM.KUTUICIADET = 0 THEN 1 ELSE TITEM.KUTUICIADET END, 4) <> TITEM.MALIYET AND 
		 TITEM.STOKBIRIMIKUTU=0 AND IIPTAL=0 AND MALIYETGUNCELLE = 1
	COMMIT
END
ELSE
BEGIN
   BEGIN TRANSACTION 
	-- ROUND 4 OLSUN
	-- NULL DEĞERLER OLABİLİR 
	UPDATE TITEM SET MALIYET = 0 WHERE MALIYET IS NULL AND STOKID=@STOKID AND (MALIYETGUNCELLE = 1 OR @ZORLAGUNCELLE = 1)
	-- BİRİMİ KUTU OLANLARI AYNEN ALALIM
	UPDATE TITEM SET TITEM.MALIYET = ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100), 4) 
	FROM TITEM INNER JOIN 
		STOK ON (STOK.STOKID=TITEM.STOKID) LEFT OUTER JOIN
		STOKGRUP ON STOK.GRUP = STOKGRUP.KODU
	WHERE STOK.SALISFIYAT IS NOT NULL AND ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100)* (CONVERT (FLOAT,MALIYETYUZDE) / 100), 4) <> TITEM.MALIYET AND 
		 TITEM.STOKBIRIMIKUTU=1 AND IIPTAL=0 AND TITEM.STOKID=@STOKID  AND (MALIYETGUNCELLE = 1 OR @ZORLAGUNCELLE = 1)

	-- BİRİMİ ADET OLANLARI OLANLARI ÇARPIP ALALIM
	UPDATE TITEM SET TITEM.MALIYET=ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100) * CASE WHEN TITEM.KUTUICIADET = 0 THEN 1 ELSE TITEM.KUTUICIADET END, 4) 
	FROM TITEM INNER JOIN 
	    STOK ON (STOK.STOKID=TITEM.STOKID) LEFT OUTER JOIN
		STOKGRUP ON STOK.GRUP = STOKGRUP.KODU
	WHERE STOK.SALISFIYAT IS NOT NULL AND ROUND(STOK.SALISFIYAT * (1 + STOK.ALISKDV/100) * (CONVERT (FLOAT,MALIYETYUZDE) / 100) * CASE WHEN TITEM.KUTUICIADET = 0 THEN 1 ELSE TITEM.KUTUICIADET END, 4) <> TITEM.MALIYET AND 
		 TITEM.STOKBIRIMIKUTU=0 AND IIPTAL=0 AND TITEM.STOKID=@STOKID AND (MALIYETGUNCELLE = 1 OR @ZORLAGUNCELLE = 1)
	COMMIT
END
END


GO


