
ALTER proc [dbo].[sp_AlisStokKarti](@Kodu nvarchar(30),@UreticiKodu nvarchar(30),@Adi nvarchar(40),        
@Adi2 nvarchar(40),@Birim nvarchar(5),@Kdv decimal,@Deger nvarchar(max),
@VergiNo nvarchar(50),@Barkod nvarchar(16),@InvoiceItemId int,@DovizKodu nvarchar(10),@Tip int)        
as        
begin        
declare @No int =0,@error nvarchar(50),@message nvarchar(500)  ,@BrmFiyat float ,@StokFytAd nvarchar(10) 
   Declare @F1F float;  
 Declare @F2F float;  
 Declare @F3F float;  
 Declare @F4F float;  
 declare @kdvD nvarchar(1)=(select case when @Kdv>0 then 'H' else 'D' end)    
select @No= isnull(StokNo,0),@Kodu=Kodu,@Adi= StokAdi from STOKKARTI where Kodu=@Kodu 
set @StokFytAd=convert(nvarchar(10),@Adi)
select @BrmFiyat=PriceAmount from InvoiceLineType where Id=@InvoiceItemId   
if(@No>0)  
begin  
  if(@Tip=1)
    update STOKKARTI set StokNo=@No,F1Fiyat=@BrmFiyat,F1DovizKodu=@DovizKodu,AlisKdv=@Kdv where NoId=@No  
  else
    update STOKKARTI set StokNo=@No,F2Fiyat=@BrmFiyat,F2DovizKodu=@DovizKodu,SatisKdv=@Kdv where NoId=@No  
  
 Set @F1F = @BrmFiyat  
 Set @F2F = @BrmFiyat  
 Set @F3F = @BrmFiyat  
 Set @F4F = @BrmFiyat  
  
 if (@kdvD='D')  
   Set @F1F = @F1F * (@Kdv +100)/100  
 if (@kdvD='D')  
   Set @F2F = @F2F * (@Kdv+100)/100  
 if (@kdvD='D')  
   Set @F3F = @F3F * (@Kdv+100)/100  
 if (@kdvD='D')  
   Set @F4F = @F4F * (@Kdv+100)/100  
if(not exists (select 1 from StokFiyat where Kodu=@Kodu))  
begin  
 INSERT into StokFiyat (Kodu, F1Adi, F2Adi, F3Adi, F4Adi,   
  F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat,   
     
  KULNO, KULADI, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu,  
  F1FiyatH, F2FiyatH, F3FiyatH, F4FiyatH,    
  SatisKdv, StokNo)  
 values(   
 @Kodu,@StokFytAd,@StokFytAd,@StokFytAd,@StokFytAd,  
 @BrmFiyat,@BrmFiyat,@BrmFiyat,@BrmFiyat,   
   
 1,'Admin',@DovizKodu,@DovizKodu,@DovizKodu,@DovizKodu,  
 @F1F,@F2F,@F3F,@F4F,  
 @Kdv,@No) 
   
end   
end  
if(@No<=0)    
begin       
 begin try        
 set @Birim=(select case when isnull(@Birim,'') ='' then 'ADET' else @Birim end)    
 insert into STOKKARTI(Kodu,UreticiFirmaKodu,StokAdi,StokAdi2,AnaBirim,Barkodu,AlisKdv,KULNO,TP, AktifPasif)        
 VALUES(@Kodu,@UreticiKodu,@Adi,@Adi2,@Birim,@Barkod,@Kdv,1,'TS','A')        
 set @No=@@IDENTITY        
   if(@Tip=1)
    update STOKKARTI set StokNo=@No,F1Fiyat=@BrmFiyat,F1DovizKodu=@DovizKodu,AlisKdv=@Kdv where NoId=@No  
  else
    update STOKKARTI set StokNo=@No,F2Fiyat=@BrmFiyat,F2DovizKodu=@DovizKodu,SatisKdv=@Kdv where NoId=@No       
 end try         
 begin catch        
   set @No=-1        
    select @error = ERROR_NUMBER(),        
      @message = ERROR_MESSAGE()        
 end catch        
 if(@No>0)        
 begin        
 update InvoiceItemMap set OurCode=@Kodu  where SenderIndentifier= @VergiNo and        
    ( SenderItemValue =case SenderItemKey        
   when 'B' then @Deger        
   when 'S' then @Deger        
   when 'M' then @Deger        
   when 'D' then @Deger        
   when 'N' then @Deger        
   end )        
  
    
 Set @F1F = @BrmFiyat  
 Set @F2F = @BrmFiyat  
 Set @F3F = @BrmFiyat  
 Set @F4F = @BrmFiyat  
  
 if (@kdvD='D')  
   Set @F1F = @F1F * (@Kdv +100)/100  
 if (@kdvD='D')  
   Set @F2F = @F2F * (@Kdv+100)/100  
 if (@kdvD='D')  
   Set @F3F = @F3F * (@Kdv+100)/100  
 if (@kdvD='D')  
   Set @F4F = @F4F * (@Kdv+100)/100  
if(not exists (select 1 from StokFiyat where Kodu=@Kodu))  
begin  
 INSERT into StokFiyat (Kodu, F1Adi, F2Adi, F3Adi, F4Adi,   
  F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat,   
   
  KULNO, KULADI, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu,  
  F1FiyatH, F2FiyatH, F3FiyatH, F4FiyatH,    
  SatisKdv, StokNo)  
 values (  
 @Kodu,@StokFytAd,@StokFytAd,@StokFytAd,@StokFytAd,  
 @BrmFiyat,@BrmFiyat,@BrmFiyat,@BrmFiyat,   
 
 1,'Admin',@DovizKodu,@DovizKodu,@DovizKodu,@DovizKodu,  
 @F1F,@F2F,@F3F,@F4F,  
 @Kdv,@No  )
   
end   
 select StokNo,Kodu,StokAdi from STOKKARTI where StokNo=@No        
 end        
 else         
   select @No StokNo,@error Kodu ,@message StokAdi     
 end    
    
else    
 update InvoiceItemMap set OurCode=@Kodu  where SenderIndentifier= @VergiNo and        
    ( SenderItemValue =case SenderItemKey        
   when 'B' then @Deger        
   when 'S' then @Deger        
   when 'M' then @Deger        
   when 'D' then @Deger        
   when 'N' then @Deger        
   end )      
   
  select @No StokNo,@Kodu Kodu ,@Adi  StokAdi    
end 