--7.1 to 7.2.2  ---2024/0820

if exists (select * from sys.procedures where name='sp_AlisStokKarti')
drop proc sp_AlisStokKarti
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create 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 ,@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)    
 set @No=0
select @No= isnull(StokNo,0),@Kodu=Kodu,@Adi= StokAdi from STOKKARTI where Kodu=@Kodu 
set @StokFytAd=convert(nvarchar(10),@Adi)
if(@Tip=1)
select @BrmFiyat=PriceAmount from InvoiceLineType where Id=@InvoiceItemId   
else 
select @BrmFiyat =PriceAmount from SendBoxInvoiceLineType 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,SatisKdv,KULNO,TP, AktifPasif)        
 VALUES(@Kodu,@UreticiKodu,@Adi,@Adi2,@Birim,@Barkod,@Kdv,@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 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
      				   
if exists (select * from sys.procedures where name='spStokHarKaydet')
drop proc spStokHarKaydet
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spStokHarKaydet]             
   @IslemNo int,            
   @IslemTuru nvarchar(10),            
   @CariNo int,            
   @CariKodu nvarchar(20),            
   @CariAdi nvarchar(50),            
   @StokNo int,            
   @StokKodu nvarchar(30),            
   @StokAdi nvarchar(500),            
   @Miktar  float,            
   @Birim nvarchar(10),            
   @BirimMiktar float,            
   @BirimFiyat float,            
   @Iskonto1 float,            
   @Iskonto2 float,            
   @Iskonto3 float,            
   @Iskonto4 float,            
   @Tutar float,            
   @SiparisNo int,            
   @SiparisTarihi datetime,            
   @Kdv float,            
   @KdvTutari float,            
   @IndirimTutari float,            
   @SatirIskTut float,            
   @FisTuru nvarchar(1),            
   @GC nvarchar(1),            
   @EkIskTut1 float,            
   @EkIskTut2 float,            
   @EkIskTut3 float,            
   @EkIskTutTop float,            
   @FaturaNo int,            
   @FaturaTarihi datetime,            
   @Satici nvarchar(10),            
   @TopTutar float,            
   @KDVHD nvarchar(1),            
   @DovizTut float,            
   @DovizCinsi smallint,            
   @DovizKodu nvarchar(2),            
   @DovizTutB float,            
   @Depo nvarchar(30),            
   @AnaBirimFiyat float,            
   @FaturaMiktari float,            
   @IslemNo2 int,            
   @OTVOrani float,            
   @OTVTutari float,            
   @KULNO int,            
   @KULADI nvarchar(20),            
   @SaticiAdi nvarchar(25),            
   @AltIskYuzde1 float,            
   @AltIskYuzde2 float,            
   @AltIskYuzde3 float,            
   @AcikKapali nvarchar(3),            
   @DovizBorc float,            
   @DovizAlacak float,            
   @IslemNo3 int,            
   @IslemNo4 int,            
   @TankKodu nvarchar(30),            
   @TankAdi nvarchar(30),            
   @FisNo nvarchar(9),            
   @Plaka nvarchar(8),            
   @TP nvarchar(2),            
   @VardiyaKodu nvarchar(15),            
   @TahsilatEH nvarchar(1),            
   @km float,            
   @Departman nvarchar(8),            
   @SonTutar float,            
   @SonTutarDov float,            
   @SatMasrafTutari float,            
   @SatIskTut1 float,            
   @SatIskTut2 float,            
   @SatIskTut3 float,            
   @SatIskTut4 float,            
   @AnaBirim nvarchar(10),            
   @OtvBirimTutari float,            
   @IlkBirimFiyat float,            
   @VadeFarkliBirimFiyat float,            
   @FifoFiyat Float,            
   @LifoFiyat float,            
   @NFiyat float,            
   @NFiyatDovBr float,            
    @StokTipi nvarchar(15),            
   @VardiyaNo nvarchar(8),            
   @Markasi nvarchar(30),            
   @SiraNo int,            
   @Iskonto5 float,            
   @Iskonto6 float,            
   @Iskonto7 float,            
   @Iskonto8 float,            
   @Iskonto9 float,            
   @Iskonto10 float,            
   @SatIskTut5 float,            
   @SatIskTut6 float,            
   @SatIskTut7 float,            
   @SatIskTut8 float,            
   @SatIskTut9 float,            
   @SatIskTut10 float,               
   @RBIslemNo int,            
   @StokAgirligi Float,            
    @KontrolBarkodu nvarchar(30),            
   @HatKodu nvarchar(15),            
   @LotNo int,            
   @StokBolge nvarchar(30),            
   @StokRaf nvarchar(30),            
    @StokYer nvarchar(30),            
   @BurutMiktar float,            
   @FireOrani float,            
   @FireMiktari float,    
   @DovizKuru float = 1,    
   @TevkifatOran float=0,    
   @TevkifatTutar float=0,    
   @TevkifatKodu nvarchar(50)='',    
   @TevkifatAdi nvarchar(200)=''    
AS             
          
if @StokNo = 0           
  Select Top 1 @StokNo=StokNo from STOKKARTI where Kodu=@StokKodu          
          
INSERT STOKHAR ( IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, StokNo, StokKodu, StokAdi, Miktar, Birim, BirimMiktar, BirimFiyat, Iskonto1, Iskonto2, Iskonto3, Iskonto4, Tutar, SiparisNo, SiparisTarihi, Kdv, KdvTutari, IndirimTutari,   
                         SatirIskTut, FisTuru, GC, EkIskTut1, EkIskTut2, EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, TopTutar, KDVHD, DovizTut, DovizCinsi, DovizKodu, DovizTutB, Depo, AnaBirimFiyat, FaturaMiktari, IslemNo2, OTVOrani,   
                         OTVTutari, KULNO, KULADI, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AcikKapali, DovizBorc, DovizAlacak, IslemNo3, IslemNo4, TankKodu, TankAdi, FisNo, Plaka, TP, VardiyaKodu, TahsilatEH, km, Departman,   
                         SonTutar, SonTutarDov, SatMasrafTutari, SatIskTut1, SatIskTut2, SatIskTut3, SatIskTut4, AnaBirim, OtvBirimTutari, IlkBirimFiyat, VadeFarkliBirimFiyat, LifoFiyat, FifoFiyat, NFiyat, NFiyatDovBr, StokTipi, VardiyaNo, Markasi,   
                         SiraNo, Iskonto5, Iskonto6, Iskonto7, Iskonto8, Iskonto9, Iskonto10, SatIskTut5, SatIskTut6, SatIskTut7, SatIskTut8, SatIskTut9, SatIskTut10, RBIslemNo, StokAgirligi, KontrolBarkodu, HatKodu, LotNo, StokBolge, StokRaf, StokYer,   
                         BurutMiktar, FireOrani, FireMiktari, DovizKuru  
) VALUES (@IslemNo, @IslemTuru, @CariNo, @CariKodu,@CariAdi, @StokNo, @StokKodu, @StokAdi, @Miktar,            
    @Birim, @BirimMiktar, @BirimFiyat, @Iskonto1,@Iskonto2, @Iskonto3, @Iskonto4, @Tutar, @SiparisNo,            
    @SiparisTarihi, @Kdv, @KdvTutari, @IndirimTutari, @SatirIskTut, @FisTuru, @GC, @EkIskTut1, @EkIskTut2,            
    @EkIskTut3, @EkIskTutTop, @FaturaNo, @FaturaTarihi,@Satici, @TopTutar, @KDVHD, @DovizTut,            
    @DovizCinsi, @DovizKodu, @DovizTutB, @Depo,@AnaBirimFiyat, @FaturaMiktari, @IslemNo2, @OTVOrani,            
    @OTVTutari, @KULNO, @KULADI, @SaticiAdi, @AltIskYuzde1,@AltIskYuzde2, @AltIskYuzde3, @AcikKapali, @DovizBorc,@DovizAlacak,@IslemNo3,            
    @IslemNo4, @TankKodu, @TankAdi, @FisNo, @Plaka,@TP, @VardiyaKodu, @TahsilatEH, @km, @Departman,            
    @SonTutar, @SonTutarDov, @SatMasrafTutari, @SatIskTut1,@SatIskTut2, @SatIskTut3, @SatIskTut4, @AnaBirim,            
 @OtvBirimTutari, @IlkBirimFiyat, @VadeFarkliBirimFiyat, @LifoFiyat, @FifoFiyat, @NFiyat,@NFiyatDovBr,@StokTipi,@VardiyaNo,@Markasi,@SiraNo,@Iskonto5,            
                                                        @Iskonto6,@Iskonto7,@Iskonto8,@Iskonto9,@Iskonto10,@SatIskTut5,@SatIskTut6,@SatIskTut7,@SatIskTut8,@SatIskTut9,@SatIskTut10,@RBIslemNo,@StokAgirligi,            
                                                        @KontrolBarkodu,@HatKodu,@LotNo,            
    CASE WHEN @StokBolge = '' THEN 'YOK' ELSE ISNULL(@StokBolge,'YOK') END,            
    CASE WHEN @StokRaf = '' THEN 'YOK' ELSE ISNULL(@StokRaf,'YOK') END,            
    CASE WHEN @StokYer = '' THEN 'YOK' ELSE ISNULL(@StokYer,'YOK') END,            
    @BurutMiktar,@FireOrani,@FireMiktari,@DovizKuru) 

	go--
-------------------------------------------------------------------------------------------------------------------------------------------
    	   
     

		 
      				   
   				 
  
 				   
     	  
      
       				   
   	 
         				
 	 
         			   
       	   
				  
 	 
    
  	  
  
		  
  				   
				  
				 
 			 
			 
			   
				   
 			  
  			 
 		
    
      
   	 
    
      
 			   
    
 	  
 				
    
  	
	
		  
		   
	 
				
 	  
 		  
     		   
     		   
 	   
        
      				 
			
				 
    				
    				
  
      	 
    	 
    		
       
      
 
    	
    
    	  
    	  
     	   
	
 
   				 
  				  
  	  
   
		
			 
				 
				
			   
			
			  
  
 	 
  
			 

		 
      				   
    	
  
		 
      				   
   	   
 			  
				
			
			
 	  
   
  		   
     
  	 
    
  		 
 		   
  		
 			
 		   
   
 				
 			  
  	 
 				  
  	  
     
 				
 			  
  		  
  	
     		   
     	 
   			 
 			  
  
       				   
  	   
 
			  
  		  
     				   
	   
 			 
  		
   	 
		   
    			
       	
	   
	   
    
  			  
				 
      				 
      				 
        	  
   			  
       	  
      		
 			
         
   
 				 
 				 
 		 
 				 
        		 
            	   
   		
      		  
     	 
  	  
    			 
  		
       		  
       
 	
   
  	
   	
  				 
   	 
 		
     
   			
  			 
   		
   
   			  
     
		 
      				   
    	   
     
		 
      				   
   				   
	
   
     		
        
         
   	   
         				  
 	   
         				 
       		 
 
 	   
      
  		
	
			
    
 
				   
 			   
			   
				 
  
 				
  			   
 		  
      
   	 
   	   
  	
   	 
 				 
  	
 		
 				  
      
  	  
	  
			
			 
	   
				  
 		
 			
     			 
     			 
 		 
          
      				   
			  
				   
    				  
    				  
    
      	   
    	   
    		  
         
        
   
    			
      
    		
    		
     		 
	  
   
   				   
   
  		
 	
		  
			   
				   
				  
				 
			  
				
    
 	   
    
			 
		 
      				   
    	
  
		 
      				   

   	   
 			  
				
			
			
 	  
   
  		   
     
  	 
    
  		 
 		   
  		
 			
 		   
   
 				
 			  
  	 
 				  
  	  
     
 				
 			  
  		  
  	
     		   
     	 
   			 
 				
  	 
       				   
  	   
 
			  
  		  
     				   
	   
 			 
  		
   	 
		   
    			
       	
	   
	   
    
  			  
				 
      				 
      				 
        	  
   			  
       	  
      		
 			
         
   
 				 
 				 
 		 
 				 
        		 
            	   
   		
      		  
     	 
  	  
    			 
  		
       		  
       
 	
   
  	
   	
  				 
   	 
 		
     
   			
  			 
   		
   
   			  
     
		 
      				   
   		  
 	  
		 
      				   
    
  
  
  	   
  	 
  	
  	 
     
  			
  	
 				 

     				 
		 
      				   
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'EF_ISTISNA')
begin
ALTER TABLE STOKHARDETBAG ADD EF_ISTISNA INT
end
else begin
ALTER TABLE STOKHARDETBAG alter column EF_ISTISNA int
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Buton') and name=N'StokNo')
begin
ALTER TABLE Buton ADD StokNo INT
end
 
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ACIKLAMA') and name=N'ACIKLAMA1')
begin
alter table ACIKLAMA add ACIKLAMA1 nvarchar(200)
end
 else begin
ALTER TABLE ACIKLAMA alter column ACIKLAMA1 nvarchar(200)
end
 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ACIKLAMA') and name=N'ACIKLAMA2')
begin
alter table ACIKLAMA add ACIKLAMA2 nvarchar(200)
end
 else begin
ALTER TABLE ACIKLAMA alter column ACIKLAMA2 nvarchar(200)
end
 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ACIKLAMA') and name=N'ACIKLAMA3')
begin
alter table ACIKLAMA add ACIKLAMA3 nvarchar(200)
end
 else begin
ALTER TABLE ACIKLAMA alter column ACIKLAMA3 nvarchar(200)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ACARI') and name=N'Aciklama2')
begin
alter table ACARI add Aciklama2 nvarchar(200)
end
 else begin 
 alter table ACARI alter column  Aciklama2 nvarchar(200)
 end
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'Ack1')
begin
alter table STOKHARDETBAG add Ack1 nvarchar(200)
end
  else begin
ALTER TABLE STOKHARDETBAG alter column Ack1 nvarchar(200)
end
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'Ack2')
begin
alter table STOKHARDETBAG add Ack2 nvarchar(200)
end
  else begin
ALTER TABLE STOKHARDETBAG alter column Ack2 nvarchar(200)
end
 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'Ack3')
begin
alter table STOKHARDETBAG add Ack3 nvarchar(200)
end
   else begin
ALTER TABLE STOKHARDETBAG alter column Ack3 nvarchar(200)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'Ack4')
begin
alter table STOKHARDETBAG add  Ack4 nvarchar(200)
end
   else begin
ALTER TABLE STOKHARDETBAG alter column  Ack4 nvarchar(200)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like 'DF__STOKHARDE__Tevki_%'
SET @SQL='ALTER TABLE [dbo].[STOKHARDETBAG] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC(@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHAR') and name=N'TevkifatAdi')
begin
alter table STOKHAR DROP COLUMN TevkifatAdi
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHAR') and name=N'TevkifatKod')
begin
alter table STOKHAR DROP COLUMN TevkifatKod
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHAR') and name=N'TevkifatOrani')
begin
alter table STOKHAR DROP COLUMN TevkifatOrani
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHAR') and name=N'TevkifatTutari')
begin
alter table STOKHAR DROP COLUMN TevkifatTutari
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'TevkifatAdi')
begin
alter table STOKHARDETBAG DROP COLUMN TevkifatAdi
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'TevkifatKod')
begin
alter table STOKHARDETBAG DROP COLUMN TevkifatKod
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'TevkifatOrani')
begin
alter table STOKHARDETBAG DROP COLUMN TevkifatOrani
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'tblTevkifat') and name=N'Oran')
begin
ALTER TABLE tblTevkifat ADD Oran Float
end
else begin
ALTER TABLE tblTevkifat alter column Oran Float
end
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='Tevkifat')
 BEGIN 
create Table Tevkifat (
  NoId int identity(1,1),
  IslemNo int not null,
  StokNo int not null,
  Kod nvarchar(50) not null,
  TevkifatOrani decimal(18,2) not null,
  TevkifatTutari float not null,
  constraint PK_Tevkifat primary key (NoId)
)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'UlkeKodu')
begin
 ALTER TABLE STOKKARTI ADD UlkeKodu nvarchar(24)
end
else begin
ALTER TABLE STOKKARTI alter column UlkeKodu nvarchar(24)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Rapor') and name=N'RaporSecimEkraniOnIzleme')
begin
alter table Rapor  add RaporSecimEkraniOnIzleme varchar(250)
end
else begin
ALTER TABLE Rapor alter column RaporSecimEkraniOnIzleme varchar(250)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Rapor') and name=N'YeniRapormu')
begin
alter table Rapor add YeniRapormu bit
end
else begin
ALTER TABLE Rapor alter column YeniRapormu bit
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Rapor') and name=N'VarsayilanRaporKullNo')
begin
alter table Rapor add VarsayilanRaporKullNo varchar(250)
end
else begin
ALTER TABLE Rapor alter column VarsayilanRaporKullNo varchar(250)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Rapor') and name=N'RaporSecimEkraniOnIzleme')
begin
alter table Rapor add RaporSecimEkraniOnIzleme varchar(250)
end
else begin
ALTER TABLE Rapor alter column RaporSecimEkraniOnIzleme varchar(250)
end

go-- 

  				
	   
			   
	
		 
      				   
 	   
	
         			  
   
    				 
   			
 			   
		 
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_KasaIslemleriListesi')
drop proc sp_KasaIslemleriListesi
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   PROCEDURE [dbo].[sp_KasaIslemleriListesi]    
  @tarih1 varchar(20),     
  @tarih2 varchar(20),    
  @dep varchar(5000),    
  @orderby int,    
  @arama varchar(100),    
  @GosterilecekKayit int,    
  @IslemTuru varchar(15),    
  @KasaTuru varchar(1),    
  @KasaKodu varchar(10),
  @IslemNoLike bit = 0  with encryption   
AS  
declare @str nvarchar(max)    
declare @rc int     
declare @tmp table (trc int)    
    
if @IslemTuru <> 'Tümü'    
begin    
 if @IslemTuru = 'Senet Tah.'    
  set @str='select count(*) from (select ISLEMNO from KASA where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';    
 else    
  set @str='select count(*) from (select ISLEMNO from KASA where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';    
end     
else begin    
 set @str='select count(*) from (select ISLEMNO from KASA where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';    
end    
   
if @KasaTuru = 'N'    
 set @str = @str + 'and NakitVisa = ''N''';    
else if @KasaTuru = 'V'    
 set @str = @str + 'and NakitVisa = ''V''';    
else if @KasaTuru = 'a'    
 set @str = @str + 'and NakitVisa <> ''a''';    
    
if @KasaKodu <> ''    
 set @str = @str + 'AND KASAKODU = '''+@KasaKodu+'''';    
    
if @dep <> ''    
 set @str=@str+' AND Departman in ('+@Dep+')'    
    
if @IslemTuru <> 'Tümü'    
BEGIN    
 IF @IslemTuru = 'Senet Tah.'    
  set @str = @str + ' AND (BAACIKLA = '''+@IslemTuru+''') OR (BAACIKLA = ''Senet Tahsil'')'    
 ELSE    
  set @str = @str + ' AND BAACIKLA = '''+@IslemTuru+'''';    
END    
ELSE    
 set @str = @str + ' AND (BAACIKLA <> ''Açılış'') AND (BAACIKLA <> ''Pos Çıkış'') ';    
    
if @arama <> '' 
begin      
	if @orderby=0 
	begin
		if @IslemNoLike = 0
			set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' ' 
		else
			set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' ' 
	end;  
	if @orderby=1     
	 set @str=@str+'and upper(MUSADI) like upper(''%'+@arama+'%'') '     
	if @orderby=2     
	 set @str=@str+'and upper(MUSKOD) like upper('''+@arama+'%'') '    
	if @orderby=3     
	 set @str=@str+'and upper(IsIstasyonKodu) like upper(''%'+@arama+'%'') '     
	if @orderby=4    
	 set @str=@str+'and upper(IsIstasyonAdi) like upper(''%'+@arama+'%'') '    
	if @orderby=5    
	 set @str=@str+'and upper(KASAKODU) like upper('''+@arama+'%'') '    
	if @orderby=6    
	 set @str=@str+'and upper(KASAADI) like upper('''+@arama+'%'') '    
	if @orderby=7    
	 set @str=@str+'and upper(BelgeNo) like upper('''+@arama+'%'') '    
 end;
set @str=@str+')Y';    
   
insert @tmp exec(@str)    
select @rc = trc from @tmp;    
  
declare @tstr varchar(100);  
  
if @orderby=0    
  set @tstr='ISLEMNO'    
if @orderby=1     
 set @tstr='MUSADI'     
if @orderby=2     
 set @tstr='MUSKOD'    
if @orderby=3     
 set @tstr='IsIstasyonKodu'     
if @orderby=4    
 set @tstr='IsIstasyonAdi'    
if @orderby=5    
 set @tstr='KASAKODU'    
if @orderby=6    
 set @tstr='KASAADI'    
if @orderby=7    
 set @tstr='BelgeNo'   
  
    
set @str='select * from KASA  '+    
   'where ISLEMNO in (select X.ISLEMNO '+    
   'from (select ROW_NUMBER() over (order by '+@tstr+') as sira, ISLEMNO '+    
   'from KASA where TARIH >='''+@Tarih1+''' and TARIH <= '''+@Tarih2+'''';    
    
if @KasaTuru = 'N'    
 set @str = @str + 'and NakitVisa = ''N''';    
else if @KasaTuru = 'V'    
 set @str = @str + 'and NakitVisa = ''V''';    
else if @KasaTuru = 'a'    
 set @str = @str + 'and NakitVisa <> ''a''';    
    
if @KasaKodu <> ''    
 set @str = @str + 'and KASAKODU = '''+@KasaKodu+'''';    
    
if @dep <> ''    
 set @str=@str+' and Departman in ('+@Dep+')'    
if @IslemTuru <> 'Tümü'    
BEGIN    
 IF @IslemTuru = 'Senet Tah.'    
  set @str = @str + ' AND (BAACIKLA = '''+@IslemTuru+''') OR (BAACIKLA = ''Senet Tahsil'')'    
 ELSE    
  set @str = @str + ' AND BAACIKLA = '''+@IslemTuru+'''';    
END    
ELSE    
 set @str = @str + ' AND ((BAACIKLA <> ''Açılış'') AND (BAACIKLA <> ''Pos Çıkış'')) ';    
if @arama <> '' 
begin      
	if @orderby=0 
	begin
		if @IslemNoLike = 0
			set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' ' 
		else
			set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' ' 
	end;   
	if @orderby=1     
	 set @str=@str+'and upper(MUSADI) like upper(''%'+@arama+'%'') '     
	if @orderby=2     
	 set @str=@str+'and upper(MUSKOD) like upper('''+@arama+'%'') '    
	if @orderby=3     
	 set @str=@str+'and upper(IsIstasyonKodu) like upper(''%'+@arama+'%'') '     
	if @orderby=4    
	 set @str=@str+'and upper(IsIstasyonAdi) like upper(''%'+@arama+'%'') '    
	if @orderby=5    
	 set @str=@str+'and upper(KASAKODU) like upper('''+@arama+'%'') '    
	if @orderby=6    
	 set @str=@str+'and upper(KASAADI) like upper('''+@arama+'%'') '    
	if @orderby=7    
	 set @str=@str+'and upper(BelgeNo) like upper('''+@arama+'%'') '    
end;    
set @str=@str+')X where X.sira >'+convert(varchar,@rc - @GosterilecekKayit) +') order by ';    
    
if @orderby=0    
  set @str=@str+'ISLEMNO'    
if @orderby=1     
 set @str=@str+'MUSADI'     
if @orderby=2     
 set @str=@str+'MUSKOD'    
if @orderby=3     
 set @str=@str+'IsIstasyonKodu'     
if @orderby=4    
 set @str=@str+'IsIstasyonAdi'    
if @orderby=5    
 set @str=@str+'KASAKODU'    
if @orderby=6    
 set @str=@str+'KASAADI'    
if @orderby=7    
 set @str=@str+'BelgeNo'    

exec(@str)    
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_GELGIDH')
drop proc sp_GELGIDH
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  proc [dbo].[sp_GELGIDH] 
(@tarih1 varchar(20),
@tarih2 varchar(20), 
@dep varchar(500), 
@orderby int, 
@arama varchar(100),
@GosterilecekKayit int,
@IslemTuru varchar(5),
@KarsiHesap varchar(50),
@IslemNoLike bit = 0
)
as      
begin   
      
declare @str varchar(1000)      
declare @rc int       
declare @tmp table (trc int)      
      
set @str='select count(*) from (select ISLEMNO from GELGID where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';      
      
if @dep <> ''      
 set @str=@str+' and Departman in ('+@Dep+')'      
if @IslemTuru <> 'Tümü'      
 set @str=@str+' and BAACIKLA = '''+@IslemTuru+''''      
if @KarsiHesap <> 'Tümü'      
 set @str=@str+' and KarsiHesp = '''+@KarsiHesap+''''    

if @arama <> '' 
begin      
if @orderby=0 
begin
	 if @IslemNoLike = 0
		set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' ' 
	 else
		set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' ' 
	end;

	if @orderby=1       
	 set @str=@str+'and upper(GELGIDKODU) like upper('''+@arama+'%'') '       
	if @orderby=2       
	 set @str=@str+'and upper(GELGIDADI) like upper(''%'+@arama+'%'') '      
	if @orderby=3       
	 set @str=@str+'and upper(ACIKLAMA) like upper(''%'+@arama+'%'') '       
  
end;

set @str=@str+')Y';      
      
 
insert @tmp exec(@str)      
select @rc = trc from @tmp;       
    
      
set @str='select * from GELGID '+      
   'where ISLEMNO in (select X.ISLEMNO '+      
   'from (select ROW_NUMBER() over (order by ISLEMNO) as sira, ISLEMNO '+      
   'from GELGID where TARIH >='''+@Tarih1+''' and TARIH <= '''+@Tarih2+'''';      
if @dep <> ''      
 set @str=@str+' and Departman in ('+@Dep+')'            
if @IslemTuru <> 'Tümü'      
 set @str=@str+' and BAACIKLA = '''+@IslemTuru+''''  
if @KarsiHesap <> 'Tümü'      
 set @str=@str+' and KarsiHesp = '''+@KarsiHesap+'''' 
 
if @arama <> '' 
begin      
if @orderby=0 
begin
	 if @IslemNoLike = 0
		set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' ' 
	 else
		set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' ' 
	end;      
if @orderby=1       
 set @str=@str+'and upper(GELGIDKODU) like upper('''+@arama+'%'') '       
if @orderby=2       
 set @str=@str+'and upper(GELGIDADI) like upper(''%'+@arama+'%'') '      
if @orderby=3       
 set @str=@str+'and upper(ACIKLAMA) like upper(''%'+@arama+'%'') '       
     
end;

set @str=@str+')X where X.sira>'+convert(varchar,@rc - @GosterilecekKayit) +') order by ';      
        
if @orderby=0      
  set @str=@str+'ISLEMNO'      
if @orderby=1        
  set @str=@str+'GELGIDKODU'      
if @orderby=2      
  set @str=@str+'GELGIDADI'      
if @orderby=3      
  set @str=@str+'ACIKLAMA'      

if @orderby=4      
  set @str=@str+'TARIH'      
      
      
exec(@str)      
end;

      				   
   			  
 		  
		 
      				   
  		 
 		   
 		   
 		  
 		  
 	
 		   
 				
 	
   
			
			   
  
 		 
  
		  
      				 
		  
	   
    			   
		
 				 
   			 
  
				
		   
			   
	  
  
   				
	  
  				   
	 
		 
 	   
    		 
 	   
    		 
 	   
    		  
 	   
    
	
  
   
		  
 				   
 				  
		  
  		
     				   
    		   
		  
	   
    			   
		  
 				 
   			 
		  
				
		   
			   
	  
  
   				
	  
  				   
	  
 
 	   
    		 
 	   
    		 
 	   
    		  
 	   
   				   
	  
     	
		  
  				
    		 
	
  
	
	
 	  
  	   
 	  
  	   
 	  
  	   
 	  
  		
 	  
  	 
		   
 	
		  
	 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME= name  FROM sys.objects where type_desc like '%FUNCT%' and name like '%fnBankaPosBilgisiGetir%'
   			
SET @SQL='DROP FUNCTION [dbo].['+CONVERT(VARCHAR(1000),@CNTNAME)+']'
EXEC(@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE function [dbo].[fnBankaPosBilgisiGetir](@Bankasi varchar(150),  
@HesapNo varchar(50), @Gun Float, @KomisyonTutari Float, @KasaPos varchar(100), @Subesi  varchar(100),  
@IslemTuru int)  
returns varchar(500)  
as begin  
declare @Poslar varchar(500)  
declare @posSecilen varchar(100)  
set @Poslar=''  
declare posliste cursor for SELECT case @IslemTuru when 1 then ( KASAKODU +' '+  KASAADI) when 2 then KASAKODU end  
    FROM KASAT as a where a.KasaPos='V' and  
     ( a.Bankasi= @Bankasi and a.HesapNo= @HesapNo and  a.Gun= @Gun and  a.KomisyonTutari= @KomisyonTutari  
  and a.KasaPos= @KasaPos and a.Subesi= @Subesi)  
open posliste  
fetch next from posliste into @posSecilen  
WHILE @@FETCH_STATUS = 0   
BEGIN  
  if @IslemTuru=1  
   begin  
   if @Poslar=''   
       SET @Poslar = @posSecilen   
   else  
    SET @Poslar = @Poslar+ ',' + @posSecilen   
  end else begin  
   if @Poslar=''   
       SET @Poslar =''''+ @posSecilen+''''   
   else  
    SET @Poslar = @Poslar + ','''+ @posSecilen+''''  
  end  
  
  FETCH NEXT FROM posliste INTO @posSecilen  
END  
  
CLOSE posliste  
  
DEALLOCATE posliste  
   
RETURN @Poslar  
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
IF  EXISTS (SELECT *  FROM sys.views WHERE  name='VRUT')
 BEGIN 
 drop view VRUT
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  CREATE   VIEW [dbo].[VRUT]  
AS  
SELECT        dbo.Rut.NoId, dbo.Rut.CariKodu,dbo.AMUSTERI.MUSNO CariNo ,dbo.AMUSTERI.MUSADI AS CariAdi, dbo.Rut.RutAdi, dbo.GRUP.Deger, dbo.Rut.RutSirasi, dbo.Rut.SaticiKodu, dbo.Rut.SaticiAdi  
FROM            dbo.Rut LEFT OUTER JOIN  
                         dbo.AMUSTERI ON dbo.Rut.CariKodu = dbo.AMUSTERI.MUSKOD LEFT OUTER JOIN  
                         dbo.GRUP ON dbo.Rut.RutAdi = dbo.GRUP.GrupAdi  
WHERE        (dbo.GRUP.Turu = N'R')  
 
      				   
   				
 			
		 
       
     		 
	  
    
	 
    			
        
  		 
	 
  
  
 			
          
  
  			
       
		  
   		  
			   
 	  
			   
 		
	   
  
    	
 			   
	
  				 
			  
				
				   
				 
				  
				
				   
				   
		  
 			
 			
			 
 		   
	
 
 		   
		 
			 
				
			  
			   
			 
				 
	
  			 
		  
 		 
			
 				  
  	  
 	 
		  
		   
			
 				   
 			  
 				  
		  
	
 	  
		
		   
			  
			
			 
		   
   
			   
			 
			
			   
		
			 
		
			 
			   
			 
			  
			
    
    
  
 		 
				
		
	
 			 
		   
			
			   
			 
			  
			
				
		
	  
  
 	   
  
   
		
				
 	
  
		  
		  
		   
 	 
 	   
 	   
 			   
 		 
 		  
  				  
  			   
  			  
			 
  
			  
			   
 			  
 		
				  
  		  
    
  				 
  	 
		 
      				   
   		  
    
		 
      				   
  			
    	
      			
				  
		   
	  
 				   
 		 
	   
	   
      				 
     		  
      	  
    		 
   
     			
     	  
      				 
   	 
    				   
 		   
 			
 	
		
 	   
		   
        	 
	  
 	
       				
  			  
    			 
  	 
 		  
  				
     			  
  			  
     	 
     		
      
    				 
				  
   	   
				
       
 		   
	  
	   
		   
		
			
          
 	  
          
  			  
    			   
  	 
 		  
  				
     			  
  			  
     	 
     		
      
    				 
				  
   	   
				
       
 		  
	  
		  
	  
		   
  
 		
 				  
	  
 			 
   			 
     
    
 		
			
	  
 		
		   
    		  
   			  
	  
			   
		   
	  
  
 			   
	  
 			   
			  
     				  
   				 
			
			  
			  
 				  
     				  
   				 
			
		 
	  
     
		 
 	
 		
	  
 				   
  	   
		  
       
   		  
		 
      				   
    
 			 

		 
      				   
   		  
      
      	
      				  
     				 
      
     				 
     			 
         
      			  
       
       
      	 
        
     	  
     			
      		
       	
 
		   
		
  
  	
     
 			  
    
  		 
			  
  	
 			   
  
 			
  
     	  
			  
 		  
 			
 
 			
     				   
     	
   				
			 
				   
 
 		  
       	   
      			 
   				
 		
		
    
 
     
  		  
  			  
				  
			
			   
          
		  
   				  
				 
			   
		
         		 
		
   
     			   
     
      	  
  				 
    
     			  
         
       	
   			  
    				  
       			
   				  
    		
      			  
      	  
            			   
   
       
  		   
        
   			   
  				
 			
		
 		   
		
   				
   		 
  
   		   
		
 		  
			 
  				   
				 
	
		 
      				   
   				  
 		   
		 
      				   
  				   
   
 			 
  
 			
 		   
   
 			 
 	  
 		  
 	 
 	  
 	  
 	  
 		
    
 			
 		 
 	 
 		  
 			
 	  
 		   
 		   
 		   
 		   
 			 
 		 
 		 
 		 
 		 
 		 
 
 		  
 		  
 			 
 		 
 		 
 		 
 	 
 		  
 		
 		
 	  
 			 
 	  
 		
 	   
 		  
    
 	 
 		
 			   
 				
 			
 		  
  
    
 	  
 	  
 				  
 		
 				  
    
 		   
 			
 				 
 		
 	  
 	 
 		  
 			 
 		
   
   
   
 	  
		 
			
			   
 		  
 				   
     
		 
		  
 	   
			   
     		   
     				 
				  
			   
      			 
      				  
  
			 
		 
		 
 				 
			   
  		  
  				 
		 
			 
        				 
		 
         
  
 	
	   

   
	 
  
            			   
           	 
          				  
      			   
  		 
   	 
    		
           		  
       		   
       			 
      			  
     			 
         
    			
		 
		
   
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   if exists (select * from sys.procedures where name='spTevkifat_Aciklama_Sil')
drop proc spTevkifat_Aciklama_Sil
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROC spTevkifat_Aciklama_Sil (@Tip varchar(10),@IslemNo int)  
AS  
BEGIN 
    IF @Tip = 'F'
	BEGIN
		DELETE Tevkifat WHERE IslemNo IN (select IslemNo from STOKHAR where @IslemNo in (IslemNo,IslemNo3));
		DELETE ACIKLAMA WHERE FATURANO IN (select IslemNo from STOKHAR where @IslemNo in (IslemNo,IslemNo3));
		DELETE FatAciklama WHERE IslemNo2  = @IslemNo
    END
	ELSE
	BEGIN
		DELETE Tevkifat WHERE IslemNo IN (select IslemNo from STOKHAR where @IslemNo in (IslemNo,IslemNo3));
		DELETE ACIKLAMA WHERE FATURANO  IN (select IslemNo from STOKHAR where @IslemNo in (IslemNo,IslemNo3));
		DELETE FatAciklama WHERE IslemNo2 IN (select IslemNo2 from STOKHAR where @IslemNo in (IslemNo,IslemNo3));
	END
END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   if exists (select * from sys.procedures where name='spTevkifatSil')
drop proc spTevkifatSil
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  proc spTevkifatSil (@IslemNo int)
AS
BEGIN
	delete Tevkifat WHERE IslemNo IN (select IslemNo from STOKHAR where @IslemNo in (IslemNo,IslemNo3))
END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   if exists (select * from sys.procedures where name='sp_ButonResmiGetir')
drop proc sp_ButonResmiGetir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[sp_ButonResmiGetir] @StokNo nvarchar(10)    
AS    
BEGIN    
select top 1 DBImage from Resimler where ResimTipi=7 and No=@StokNo    
END  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   if exists (select * from sys.procedures where name='RaporGetir')
drop proc RaporGetir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   PROCEDURE [dbo].[RaporGetir]         
 @TipNo int,        
 @KulNo int,        
 @CariNo int = 0,        
 @Onizleme bit = 1 OUTPUT,        
 @YeniRapormu bit = 0        
AS        
 DECLARE @CariTipi nvarchar(30),        
  @OzelKod1 nvarchar(40), @OzelKod2 nvarchar(40),         
  @OzelKod3 nvarchar(40), @OzelKod4 nvarchar(40)        
 DECLARE @RaporNo int        
        
 select @CariTipi = ISNULL(Tipi, ''),        
  @OzelKod1 = ISNULL(OZELKOD1, ''),        
  @OzelKod2 = ISNULL(OZELKOD2, ''),        
  @OzelKod3 = ISNULL(OZELKOD3, ''),        
  @OzelKod4 = ISNULL(OZELKOD4, '')        
 from AMUSTERI        
 where MUSNO = @CariNo        
   
   
 select @RaporNo = RaporNo        
 from CariVarsayilanRaporlari --Null ise kriteri dikkate alma(1=1 gibi olacak)        
 where ISNULL(CariTipi, @CariTipi) = @CariTipi AND        
  ISNULL(OzelKod1, @OzelKod1) = @OzelKod1 AND         
  ISNULL(OzelKod2, @OzelKod2) = @OzelKod2 AND         
  ISNULL(OzelKod3, @OzelKod3) = @OzelKod3 AND        
  ISNULL(OzelKod4, @OzelKod4) = @OzelKod4 AND        
  TipNo = @TipNo        
   
   
  
 IF @RaporNo IS NOT NULL BEGIN--Carinin raporu varsa        
  SELECT RaporIsmi, Rapor from Rapor where RaporNo = @RaporNo        
  SELECT @Onizleme = ISNULL(Onizleme, 1) from Rapor where RaporNo = @RaporNo        
  RETURN        
 END        
 ELSE BEGIN--Kullanicinin Varsayılan raporundan yazdır    
  
  SELECT RaporIsmi, Rapor, RaporNo,(select GirisAdi from Kullanicilar where No=@KulNo ) as Kullanici from Rapor where RaporNo = (select RaporNo from KullaniciVarsayilanRaporlari         
         where KullaniciNo=@KulNo and TipNo=@TipNo) and YeniRapormu=@YeniRapormu        
  SELECT @Onizleme = ISNULL(Onizleme, 1) from Rapor where RaporNo = (select RaporNo from KullaniciVarsayilanRaporlari         
         where KullaniciNo=@KulNo and TipNo=@TipNo)     
     
  
  
  RETURN        
 END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='ILCE')
 BEGIN 
 
CREATE TABLE [dbo].[ILCE](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[IL_ID] [int] NOT NULL,
	[ILCE_ID] [int] NULL,
	[ILCE_ADI] [nvarchar](50) NULL,
	[AKTIF] [nvarchar](1) NULL,
	[ISLEM_YAPAN] [int] NULL,
	[ISLEM_TARIHI] [datetime] NULL,
 CONSTRAINT [PK_ILCE] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='IL')
 BEGIN 
 
CREATE TABLE [dbo].[IL](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[IL_ID] [int] NOT NULL,
	[IL_ADI] [nvarchar](50) NOT NULL,
	[AKTIF] [nvarchar](1) NULL,
	[ISLEM_YAPAN] [int] NULL,
	[ISLEM_TARIHI] [datetime] NULL,
 CONSTRAINT [PK_IL] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='YazarKasalar')
 BEGIN 
CREATE TABLE [dbo].[YazarKasalar](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[YazarKasaTanimi] [varchar](150) NULL,
	[YazarKasaIp] [varchar](50) NULL,
	[YazarKasaPort] [int] NULL,
	[YazarKasaSeriNo] [varchar](20) NULL,
	[ServerIP] [varchar](50) NULL,
	[ServerPort] [int] NULL,
	[KasiyerNo] [int] NULL,
	[KasiyerSifre] [varchar](50) NULL,
	[KulNo] [int] NULL,
 CONSTRAINT [PK_YazarKasalar] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='YazarKasalarMapKullanicilar')
 BEGIN 
CREATE TABLE [dbo].[YazarKasalarMapKullanicilar](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[YazarKasaID] [int] NOT NULL,
	[YazarKasaKulNo] [int] NOT NULL,
	[KulNo] [int] NULL,
 CONSTRAINT [PK_YazarKasalarMapKullanicilar] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   		   
 	   
		 
      				   
  			  
	  
   			   
   
     	   
	   
			   
			  
  	
   	  
   
	  
	  
	  
  	  
 	  
 	
  
 			
 				   
 	
 	   
 			  
 			
    
 			  
 				 
 		
 			
 				   
 				
   
 			 
 			  
 		  
    
 	
 	
 			
 			 
  	   
  
 			
 	
 	   
 		  
 	  
 		 
 	 
 			   
 			 
 				
 				
 			   
 		   
 			
  	   
			   
     
   				 
 				  
    	  
				
  		 
				 
 		   
 			
 			
  
			
	   
    				 
     			   
 			 
   			  
  
 				 
			   
			
 				 
	   
     	   
         				
 			 
   			 
  		
	  
				
			 
    				  
      		
 			 
   			 
 				
			 
		 
				  
	 
		
   				  
			
 			   
        	   
   
     	 
  			  
  
   	  
      	
     
    	   
 		
    				  
 				
 			  
	  
  
 		
        	  
      
     	 
  			  
  
   	  
      	
     
    	   
 		
    				  
 		  
	   
		 
  		
		
				
			 
  
   
 			   
       			 
      
     	 
  			  
  
   	  
      	
     
    	   
 		
      			  
  
 			 
			   
		   
 			   
       			 
      
     	 
  			  
  
   	  
      	
     
    	   
 		
     	  
  
 			
		 
		   
		
   		

 		 
 			
 	 
  
	  
    
	   
      			 
	  
     	   
         
 		   
   		   
      		  
  	 
		
  				   
	  
     				 
   				 
 			   
 		  
			
 	   
			  
 				  
     				   
   				 
 				  
    	 
  			 
	  
  	  
			  
      				 
     
	  
				 
			  
 				  
      
   				 
 				  
    	 
  	
	  
			
	  
   	
  				 
  	  
	  
		  
	  
	  
		
 		
 			
	  
	  
        		
 			   
	  
 		  
	  
				
	  
 		
			   
    			   
   			   
		  
 	
 	  
			 
    		
    		
				
			 
 	  
 				   
	  
   
  
      		   
		 
     
		
	  
 	
			  
  	
      		   
		 
    
			  
			
		   
   	 
		   
  			
		
		
		
    	   
 			  
				 
 
				 
 
 
				   
 
  
  	  
    
  			   
   	  
   		
   			
  	
   	 
  			 
  		
  				   
  			 
  				
 			
  	 
  				  
  			 
      
			
 		  
   	   
    			  
   
 		 
     
			   
   				 
   				 
				
 		   
   		
    			   
  			  
 		   
  	 
				  
			  
        			   
		
			
		
	 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 
  if exists (select * from sys.tables where name='AcariTakas')
drop table AcariTakas
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='AcariTakas')
 BEGIN 
CREATE TABLE [dbo].[AcariTakas](
	[NoID] [int] IDENTITY(1,1) NOT NULL,
	[TESSIRNO] [int] NULL,
	[MUSNO] [int] NOT NULL,
	[MUSADI] [nvarchar](50) NULL,
	[TARIH] [smalldatetime] NOT NULL,
	[ACIKLAMA] [nvarchar](60) NULL,
	[BORC] [float] NOT NULL,
	[ALACAK] [float] NOT NULL,
	[BA] [nvarchar](1) NULL,
	[BAACIKLA] [nvarchar](14) NULL,
	[BAKIYE] [float] NOT NULL,
	[ACIKLABAKIYE] [nvarchar](1) NULL,
	[BORDRONO] [int] NOT NULL,
	[LISTNO] [nvarchar](5) NULL,
	[ISLEMNO] [int] NOT NULL,
	[TDATE] [datetime] NULL,
	[DOVIZTUT] [float] NULL,
	[DOVIZCINSI] [smallint] NOT NULL,
	[MUSKOD] [nvarchar](20) NOT NULL,
	[DOVIZKODU] [nvarchar](2) NULL,
	[OZELKOD] [nvarchar](3) NULL,
	[ISLEMTIPI] [nvarchar](30) NULL,
	[BELGENO] [nvarchar](10) NULL,
	[KULNO] [int] NOT NULL,
	[KULADI] [nvarchar](20) NULL,
	[SaticiKodu] [nvarchar](15) NULL,
	[SaticiAdi] [nvarchar](25) NULL,
	[CariOPlanGunu] [int] NOT NULL,
	[GunAy] [nvarchar](3) NULL,
	[OpsiyonGunu] [int] NOT NULL,
	[AcikKapali] [nvarchar](5) NULL,
	[DovizBorc] [float] NOT NULL,
	[DovizAlacak] [float] NOT NULL,
	[TP] [nvarchar](2) NULL,
	[VardiyaKodu] [nvarchar](15) NULL,
	[VardiyaDurumu] [nvarchar](9) NULL,
	[Departman] [nvarchar](8) NOT NULL,
	[IslemNo2] [int] NOT NULL,
	[KayitDuzeyi] [int] NOT NULL,
	[KayitDuzeyiNo] [int] NOT NULL,
	[IslemNo3] [int] NOT NULL,
	[Aciklama2] [nvarchar](30) NULL,
	[OnOdeme] [nvarchar](1) NOT NULL,
	[VadeTarihi] [smalldatetime] NULL,
	[IsIstasyonKodu] [nvarchar](30) NULL,
	[IsIstasyonAdi] [nvarchar](30) NULL,
	[SiraNo] [int] NOT NULL,
	[VardiyaNo] [nvarchar](8) NOT NULL,
	[PosSablonKodu] [nvarchar](15) NULL,
	[Update_Date] [datetime] NULL,
	[StokKodu] [nvarchar](30) NULL,
	[TTarihi] [smalldatetime] NULL,
	[TeslimEden] [nvarchar](80) NULL,
	[TeslimAlan] [nvarchar](60) NULL,
 CONSTRAINT [PK_AcariTakas] PRIMARY KEY CLUSTERED 
(
	[NoID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [BORC]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [ALACAK]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [BAKIYE]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [BORDRONO]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [DOVIZCINSI]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((1)) FOR [KULNO]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [CariOPlanGunu]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [OpsiyonGunu]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [DovizBorc]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [DovizAlacak]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ('01000000') FOR [Departman]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [IslemNo2]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((1)) FOR [KayitDuzeyi]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((1)) FOR [KayitDuzeyiNo]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [IslemNo3]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ('H') FOR [OnOdeme]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ((0)) FOR [SiraNo]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT ('20000000') FOR [VardiyaNo]

ALTER TABLE [dbo].[AcariTakas] ADD  DEFAULT (getdate()) FOR [Update_Date]
end


go--
-------------------------------------------------------------------------------------------------------------------------------------------
IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='Resimler')
 BEGIN 
CREATE TABLE [dbo].[Resimler](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[No] [int] NOT NULL,
	[ResimTipi] [int] NOT NULL,
	[SiraNo] [int] NULL,
	[WebYayini] [nvarchar](1) NULL,
	[DBImage] [image] NOT NULL,
	[KULNO] [int] NOT NULL,
	[Tarih] [datetime] NULL,
	[Varsayilan] [bit] NULL,
	[Aciklama] [nvarchar](200) NULL,
 CONSTRAINT [PK_Resimler] PRIMARY KEY CLUSTERED 
(
	[NoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
 

ALTER TABLE [dbo].[Resimler] ADD  CONSTRAINT [DF__Resimler__WebYay__39444182]  DEFAULT ('E') FOR [WebYayini]
 

ALTER TABLE [dbo].[Resimler] ADD  DEFAULT (getdate()) FOR [Tarih]
 

ALTER TABLE [dbo].[Resimler] ADD  DEFAULT ((0)) FOR [Varsayilan]
 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  exists (select * from sys.foreign_keys where name like 'FK_YazarKasalarMapKullanicilar_YazarKasa%')
begin
ALTER TABLE [dbo].[YazarKasalarMapKullanicilar] DROP CONSTRAINT FK_YazarKasalarMapKullanicilar_YazarKasalar
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE [dbo].[YazarKasalarMapKullanicilar]  WITH CHECK ADD  CONSTRAINT [FK_YazarKasalarMapKullanicilar_YazarKasalar] FOREIGN KEY([YazarKasaID])
REFERENCES [dbo].[YazarKasalar] ([Id])
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF   EXISTS (SELECT *  FROM sys.views WHERE  name='KullanicilarVarsayilan_TipRapor')
 BEGIN 
 drop view KullanicilarVarsayilan_TipRapor
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create    view [dbo].[KullanicilarVarsayilan_TipRapor]   
AS  
 select Kul.[No] as [KulNo],  
  Tip.Ad  as [Tip],  
  Rap.RaporIsmi as [Rapor],  
  Rap.Onizleme,Rap.RaporNo as RaporNo,Tip.TipNo as TipNo,  
  convert(bit,(select case COUNT(*) when 0 then 0 else 1 end from dbo.splitstring(Rap.RaporSecimEkraniOnIzleme,',') where Name=Kul.No )) as RaporSecimEkraniOnIzleme,  
  Rap.Yazici  
 from Kullanicilar as Kul  
  cross join RaporTipTanimlari as Tip  
  left outer join KullaniciVarsayilanRaporlari as K on Tip.TipNo=K.TipNo and Kul.[No] = K.KullaniciNo  
  left outer join Rapor as Rap on K.RaporNo = Rap.RaporNo  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME= name  FROM sys.objects where type_desc like '%FUNCT%' and name like '%splitstring%'
--name from sys.default_constraints where name like 'DF_STOKHARDETevki%'
SET @SQL='DROP FUNCTION [dbo].['+CONVERT(VARCHAR(1000),@CNTNAME)+']'
EXEC(@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[splitstring] ( @stringToSplit VARCHAR(MAX), @splitdet varchar(1)) 
RETURNS 
 @returnList TABLE ([Name] [nvarchar] (500)) 
AS 
BEGIN 
 
 DECLARE @name NVARCHAR(255) 
 DECLARE @pos INT 
 
 WHILE CHARINDEX(@splitdet, @stringToSplit) > 0 
 BEGIN 
  SELECT @pos  = CHARINDEX(@splitdet, @stringToSplit)   
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1) 
 
  INSERT INTO @returnList  
  SELECT @name 
 
  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos) 
 END 
 
 INSERT INTO @returnList 
 SELECT @stringToSplit 
 
 RETURN 
END 

go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spCariHareketYaz')
drop proc spCariHareketYaz
 go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spCariHareketYaz]    
   @TESSIRNO int,        
   @MUSNO int,        
   @MUSADI nvarchar(50),        
   @TARIH datetime,        
   @ACIKLAMA nvarchar(60),        
   @BORC float,        
   @ALACAK float,        
   @BA nvarchar(1),        
   @BAACIKLA nvarchar(14),        
   @ACIKLABAKIYE nvarchar(1),        
   @BORDRONO int,        
   @LISTNO nvarchar(5),        
   @ISLEMNO int,        
   @TDATE datetime,        
   @DOVIZTUT float,        
   @DOVIZCINSI int,        
   @MUSKOD nvarchar(20),        
   @DOVIZKODU nvarchar(2),        
   @OZELKOD nvarchar(3),        
   @ISLEMTIPI nvarchar(30),        
   @BELGENO nvarchar(10),        
   @KULNO int,        
   @KULADI nvarchar(20),        
   @SaticiKodu nvarchar(15),        
   @SaticiAdi nvarchar(25),        
   @CariOPlanGunu int,        
   @GunAy nvarchar(3),        
   @OpsiyonGunu int,        
   @AcikKapali nvarchar(3),        
   @DovizBorc float,        
   @DovizAlacak float,        
   @TP nvarchar(2),        
   @VardiyaKodu nvarchar(15),        
   @VardiyaDurumu nvarchar(9),        
   @Departman nvarchar(8),        
   @IslemNo2 int,        
   @KayitDuzeyi int,         
   @KayitDuzeyiNo int,        
   @IslemNo3 int,        
   @Aciklama2 nvarchar(30),        
   @OnOdeme nvarchar(1),        
   @VadeTarihi datetime,        
   @IsIstasyonKodu nvarchar(30),        
   @IsIstasyonAdi nvarchar(30),         
   @SiraNo int,        
   @VardiyaNo nvarchar(8),         
   @PosSablonKodu nvarchar(15),        
   @Update_Date DateTime,        
   @StokKodu nvarchar(30),        
   @FisNo int,
   @DovizKuru float = 1
AS        
BEGIN        
    
 INSERT ACARI (TESSIRNO, MUSNO, MUSADI, TARIH, ACIKLAMA, BORC, ALACAK, BA, BAACIKLA, ACIKLABAKIYE, BORDRONO, LISTNO, ISLEMNO, TDATE, DOVIZTUT, DOVIZCINSI, MUSKOD, DOVIZKODU, OZELKOD, 
ISLEMTIPI, BELGENO, KULNO, KULADI, SaticiKodu, SaticiAdi, CariOPlanGunu, GunAy, OpsiyonGunu, AcikKapali, DovizBorc, DovizAlacak, TP, VardiyaKodu, VardiyaDurumu, Departman, IslemNo2, KayitDuzeyi, KayitDuzeyiNo, 
 IslemNo3, Aciklama2, OnOdeme, VadeTarihi, IsIstasyonKodu, IsIstasyonAdi, SiraNo, VardiyaNo, PosSablonKodu, Update_Date, StokKodu, FisNo, DovizKur)

 VALUES ( @TESSIRNO, @MUSNO, @MUSADI, @TARIH,        
    @ACIKLAMA, @BORC, @ALACAK, @BA,        
    @BAACIKLA, @ACIKLABAKIYE,        
    @BORDRONO, @LISTNO, @ISLEMNO,        
    GETDATE(), @DOVIZTUT, @DOVIZCINSI,        
    @MUSKOD, @DOVIZKODU, @OZELKOD,        
    @ISLEMTIPI, @BELGENO, @KULNO,        
    @KULADI, @SaticiKodu, @SaticiAdi,        
    @CariOPlanGunu, @GunAy, @OpsiyonGunu,        
    @AcikKapali, @DovizBorc, @DovizAlacak,        
    @TP, @VardiyaKodu, @VardiyaDurumu,         
    @Departman, @IslemNo2, @KayitDuzeyi,        
    @KayitDuzeyiNo, @IslemNo3, @Aciklama2,        
    @OnOdeme, @VadeTarihi, @IsIstasyonKodu,        
    @IsIstasyonAdi,@SiraNo,@VardiyaNo,@PosSablonKodu,        
    GETDATE(),@StokKodu, @FisNo,@DovizKuru )     
    
    IF @BAACIKLA = 'Prk.Satış'  
 BEGIN  
  DECLARE @CariSayisi int,@SD_Fark float  
  
  SET @CariSayisi = ( SELECT COUNT(*)   
                        FROM STOKHAR S,STOKHARDETBAG B  
        WHERE (S.IslemNo = B.IslemNo or S.IslemNo3 = B.IslemNo)  
          AND B.IslemNo = @ISLEMNO  
          AND (S.CariNo <> B.CariNo or S.CariKodu <> B.CariKodu))  
  
  SET @SD_Fark = @BORC - ( SELECT Tutar FROM STOKHARDETBAG B WHERE B.IslemNo = @ISLEMNO )   
  
  IF  (@CariSayisi > 0) or (ABS(@SD_Fark) > 0.01)  
  BEGIN  
      UPDATE STOKHARDETBAG SET CariNo = @MUSNO,CariKodu = @MUSKOD, CariAdi = @MUSADI,Tutar = @BORC,Ack4 = 'ACari Tutar Güncelleme' where IslemNo = @ISLEMNO and IslemTuru = 'Prk.Satış'  
   RAISERROR ('Cari veya tutar hatası. Kontrol ediniz..!',10,1)  
   return 0  
  END    
 END  
  
END;
go--
      				   
    	
  
		 
      				   
   
 	
	  
  
		 
   
  				   
  	
    
       
  	  
 		
	   
		  
				 
      			   
       	   
       			  
     			   
    				  
    	  
    			
	   
    
  				
				 
 		  
		 
      				   
    	  
   
		 
      				   
   		   
 	  
		 
		   
  				   
				  
				 
				   
			 
			   
				   
				  
  			 
 		
  			
  			  
  				
 				
  			  
 	  
 			 
 	
 			   
  			  
 				 
		  
		   
	 
				
 	  
 			  
   		  
 	   
      
    				 
			
  			   
  
   			
       
   
  	 
    
     				  
     			 
        	  
   				 
         				  
 	   
         				 
        	 
    	
    
    	  
    	  
     	
				
       
	
				   
   	 
  				
     
     			  
		
			 
				 
				
				
			
			  
				  
				 
 	 
				
		 
      				   
    		
     
		 
      				   
     
	 
   
 			 
 	  
		 
		   
  				   
				  
				 
 			 
			 
			   
				   
				  
  			 
 		
    
      
   	 
    
      
 			   
    
 	  
 				
    
  	
	
		  
		   
	 
				
 	  
 		  
     		   
     		   
 	   
        
      				 
			
				 
    				
    				
  
      	 
    	 
    		
       
     	  
      
       				   
   	 
             
 	 
            
        			 
    		  
    
    	  
    	  
     	   
	
 
   	   
  				  
  	  
   
		
			 
				 
				
			   
			
			  
				  
				 
 	 
  
			 
		 
      				   
    	   
    
		 
      				   
    
 		   
   
 	  
	 
  

    

   
 				  
 		   
				  

			 
  	   
 	  
		 
      				   
    	 
  
		 
      				   
   		  
 				
				  
			  
 		
 		
     
  			 
  	 
  	   
  	
  		   
 			 
  		  
 			  
 			 
     
 				  
 				
  	   
  
  		
  	 
 				  
 				
  			
  	  
     			 
     	   
   			   
 				  
  	
         
  		 
   
				
  			
       
		 
 			   
  		  
   	   
			 
    			  
       	  
		 
		 
  	
  				
				   
      				   
      				   
        		
   				
       		
      		  
 			  
       	
 	
 				   
 				   
 		   
 				   
        		   
        
        
   		  
      			
     	   
  		
    			   
  		  
       			
    	 
 	  
 	
  	  
   	  
  				   
   	   
 		  
   	
   			  
  			   
   		  
     
   				
     
		 
      				   
    	 
  
		 
      				   
   
 	
	  
  
		 
   
  				   
  	
    
       
  	  
 		
	   
		  
				 
      			   
       	   
       			  
     			   
    				  
    	  
    			
	   
    
  				
				 
 		  
		 
 
-------------------------------------------------------------------------------------------------------------------------------------------
IF NOT EXISTS (SELECT *  FROM sys.columns WHERE  object_id = OBJECT_ID(N'[dbo].[AcariTakas]') AND name = 'ISLEMTIPI') 
 BEGIN 
  ALTER TABLE AcariTakas ADD ISLEMTIPI NVARCHAR (30)
 END 
ELSE 
 BEGIN 
alter table AcariTakas ALTER COLUMN ISLEMTIPI NVARCHAR (30)
 END
 go--
-------------------------------------------------------------------------------------------------------------------------------------------
     
 				
		 
      				   
  		   
   
   
	  
  			   
			  
			 
			   
		 
		   
			   
			  
   
 	
  		
  		  
  			
 			
  		  
   
 		 
  
	  
	   
 
			
   
 	  
  			   
    
    	 
       
		
  
				 
   	  
 		   
 				 
   		  
      		  
    
   				
      
      
      

   				
  		 
			 
	
		 
			 
			
			
		
		  
			  
			 
  
				
		 
      				   

declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME= name  FROM sys.objects where type_desc like '%FUNCT%' and name like '%fnFaturaTipiGetir%'
--name from sys.default_constraints where name like 'DF_STOKHARDETevki%'
SET @SQL='DROP FUNCTION [dbo].['+CONVERT(VARCHAR(1000),@CNTNAME)+']'
EXEC(@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[fnFaturaTipiGetir]   
(  
 @FaturaTipi int  
)  
RETURNS nvarchar(20)  
AS  
BEGIN   
 declare @Aciklama nvarchar(20) = '';  
  
 IF @FaturaTipi = 1  
   SET @Aciklama  = 'TEVKIFAT'  
 ELSE IF @FaturaTipi = 2  
   SET @Aciklama  = 'IADE'  
 ELSE IF @FaturaTipi = 3  
   SET @Aciklama  = 'IHRACKAYITLI'  
 ELSE IF @FaturaTipi = 4  
   SET @Aciklama  = 'ISTISNA'  
 ELSE IF @FaturaTipi = 5   
   SET @Aciklama  = 'MOBIL'  
 ELSE IF @FaturaTipi = 6  
   SET @Aciklama  = 'KAMU'  
 ELSE   
   SET @Aciklama  = 'SATIS'  
    
  
 RETURN @Aciklama  
END;  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spOdemeYaz')
drop proc spOdemeYaz
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE   PROCEDURE [dbo].[spOdemeYaz]   
 @IslemNo int,@Tarih datetime,@Tutar float,  
 @Aciklama nvarchar(15), @Durum nvarchar(1),  
 @CariNo int,@CariKodu nvarchar(20),  
 @Borc float,@Alacak float,@NoNo int,  
 @KULNO int,@BAAcikla nvarchar (15) ,@Departman nvarchar(8),  
 @KayitDuzeyi int,@SiraNo int,   
 @Modul nvarchar(3)  
AS  
INSERT Odeme (IslemNo, Tarih, Tutar, Aciklama, Durum, CariNo,  
 CariKodu,  Borc, Alacak,  NoNo, KULNO,BAAcikla, Departman,  
 KayitDuzeyi, SiraNo, Modul) 
VALUES (@IslemNo, @Tarih, @Tutar, @Aciklama, @Durum, @CariNo,  
 @CariKodu,  @Borc, @Alacak,  @NoNo, @KULNO,@BAAcikla, @Departman,  
 @KayitDuzeyi, @SiraNo, @Modul)  
go--
      				   
   		   
 	   
		 
      				   
  		
				 
 	
 				
 	   
 	   
 	   
 		
 	   
 		   
 		   
 				  
 		   
 		   
 				  
 			 
 			 
  
    
 	
    
   
 	 
 	 
 	 
 	 
 
 
 
 
 	
 	
 	
 	
 	   
				  
   
 			
 			
 				   
 			
 			
 				   
    
   
  
				   
 
 	   
  
 			
				
 	  
  
  
 		
 		
 		 
 		  
   
 			
 		  
 		  
 		  
 		  
 			
 		 
 		
 	 
 		   
 	   
 	 
 		
 			   
 		 
    
    
 		  
 		  
 		  
 		   
 		   
 		   
    
    
 		 
 		  
   
 			
 				   
    
 		  
				
 			
 			
 			
 			
 			
 		
 		 
 		   
 	
 	   
 	   
 			
 		   
    
 	
  
 		 
 	   
				
 				
 		  
 		  
   
 		 
 				   
 		   
  
				  
   
 
	   
 		  
		   
    
			  
			   
			   
			   
			  
				  
 	 
 	 
				  
 	 
 	 
 
 	   
 	   
			  
			   
			  
				
			
			
			
			
			  
			  
			  
			  
			
			
			
			
			  
			
				
				   
 	  
 	  
				   
 	  
 	  
				 
				
			   
			 
			  
  
			   
				   
			
			 
		 
			   
			   
				
    
 	
				
 
				  
				  
				  
				  
				  
				 
				
			
				  
			  
				   
			   
   
				 
				 
		   
 	
 	
 	
 	 
 	 
 	 
				   
				 
    
 	
				
				   
 	   
		   
				 
			
				   
				   
				   
				   
				   
			   
				
				  
				  
  
  
				 
				  
				   
		   
				
			   
			   
			
 			
				 
				 
				
 	 
 			   
				  
				 
			 
			 



 
		   
    
			  
			   
			   
			   
			  
				  
 	 
 	 
				  
 	 
 	 
 
 	   
 	   
			  
			   
			  
				
			
			
			
			
			  
			  
			  
			  
			
			
			
			
			  
			
				
				   
 	  
 	  
				   
 	  
 	  
				 
				
			   
			 
			  
  
			   
				   
			
			 
		 
			   
			   
				
    
 	
				
 
				  
				  
				  
				  
				  
				 
				
			
				  
			  
				   
			   
   
				 
				 
		   
 	
 	
 	
 	 
 	 
 	 
				   
				 
    
 	
				
				   
 	   
		   
				 
			
				   
				   
				   
				   
				   
			   
				
				  
				  
  
  
				 
				  
				   
		   
				
			   
			   
			
 			
				 
				 
				
 	 
 			   
				  
				 
			 
		  
		 
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'TevkifatKodu')
begin
ALTER TABLE STOKKARTI ADD TevkifatKodu nvarchar(5) NULL
end
else begin
ALTER TABLE STOKKARTI alter column TevkifatKodu nvarchar(5) NULL
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------

 IF EXISTS (SELECT *  FROM sys.views WHERE  name='VCr_CariHareketlerFisNo')
 BEGIN 
 drop view VCr_CariHareketlerFisNo
 end
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE VIEW dbo.VCr_CariHareketlerFisNo   
AS   
SELECT    C.MUSKOD AS CariKodu,C.MUSADI AS CariUnvani,C.BAACIKLA AS IslemTuru,C.TARIH AS Tarih,   
                     C.BORC AS Borc,C.ALACAK AS Alacak,C.IsIstasyonKodu AS IsIstasyonKodu,C.IsIstasyonAdi AS IsIstasyonAdi,   
                     S.FisNo AS FisNo, S.FaturaNo AS FaturaNo, S.FatSerNo AS FatSeriNo,   
                     C.VardiyaKodu AS VardiyaKodu,C.VardiyaNo AS VardiyaNo,C.ACIKLAMA AS Aciklama,C.BELGENO AS BelgeNo,   
                     C.SaticiKodu AS SaticiKodu,C.SaticiAdi AS SaticiAdi,C.VardiyaDurumu AS VardiyaDurumu,   
                     C.Departman AS Departman,C.KULNO AS KulNo,C.KULADI AS KulAdi,C.ISLEMNO AS IslemNo,	
  M.SaticiKodu CariSaticiKodu,M.SaticiAdi CariSaticiAdi
FROM         dbo.ACARI C
inner join  AMUSTERI M on M.MUSNO = C.MUSNO
LEFT OUTER JOIN  dbo.STOKHARDETBAG S ON C.ISLEMNO = S.IslemNo
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokBarkodlari') and name=N'Birim')
begin
ALTER TABLE StokBarkodlari ADD Birim nvarchar(20) 
end
else begin
ALTER TABLE StokBarkodlari alter column Birim nvarchar(20)  
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokBarkodlari') and name=N'AnaBirim')
begin
ALTER TABLE StokBarkodlari ADD AnaBirim bit
end
else begin
ALTER TABLE StokBarkodlari alter column AnaBirim bit 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'Barkodu')
begin
ALTER TABLE STOKKARTI add Barkodu nvarchar (40) null
end
else begin
ALTER TABLE STOKKARTI alter column Barkodu nvarchar (40) null
end
go-- 
-------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokBarkodlari') and name=N'Barkodu')
begin
ALTER TABLE StokBarkodlari add Barkodu nvarchar (40) null
end
else begin
ALTER TABLE StokBarkodlari ALTER COLUMN Barkodu nvarchar (40) null
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if not exists (select * from  STOKKARTI where StokNo is null)
update ADIGSABITLER SET STOKNO =(SELECT MAX(StokNo) from STOKKARTI )+1
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='VergiMuafiyetleri')
 BEGIN 
 CREATE TABLE [dbo].VergiMuafiyetleri(
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[Kod] [nvarchar](5) NOT NULL,
	[Aciklama] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_VergiMuafiyetleri] PRIMARY KEY CLUSTERED 
(
	[NoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_StokBarkodlari')
 BEGIN 
 drop view V_StokBarkodlari
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   view V_StokBarkodlari as 
select   * from (
SELECT 1 BarkodNo,StokNo,Kodu,Barkodu ,1 Miktar FROM STOKKARTI   UNION ALL
SELECT 2,StokNo,Kodu,IkinciBirimBarkodu Barkodu,IkinciBirimMiktari Miktar FROM STOKKARTI where AktifPasif='A' and IkinciBirimMiktari>0 AND IkinciBirimMiktari<>1 UNION ALL
SELECT 3,StokNo,Kodu,UcuncuBirimBarkodu,UcuncuBirimMiktari FROM STOKKARTI where AktifPasif='A' and UcuncuBirimMiktari>0 AND UcuncuBirimMiktari <>1 UNION ALL
SELECT 4,StokNo,Kodu,DorduncuBirimBarkodu,DorduncuBirimMiktari FROM STOKKARTI where AktifPasif='A' and DorduncuBirimMiktari>0 AND DorduncuBirimMiktari<>1 UNION ALL
SELECT 5,StokNo,Kodu,BesinciBirimBarkodu,BesinciBirimMiktari FROM STOKKARTI where AktifPasif='A' and BesinciBirimMiktari>0 AND BesinciBirimMiktari<>1 UNION ALL
SELECT 6,StokNo,Kodu,AltinciBirimBarkodu,AltinciBirimMiktari FROM STOKKARTI where AktifPasif='A' and AltinciBirimMiktari>0 AND AltinciBirimMiktari<>1 ) t 
where  Barkodu <>''	
  
      				   
   			 
 		
		 
      				   
  		  
	  
    
	 
    			
        
  		 
	 
	
	
 			
      
	
    		   
     				  
	
 			  
	
  				
			 
			   
				  
				
				 
			   
				  
				  
		 
 		   
 		   
			
 		  
	
				   
 		  
		
			
			   
			 
			  
			
				
	
  			
		 
 		
		   
  
  	 
 	
		 
		  
		   
   
 			 
 				 
		 
	
 	 
	   
			 
				
			  
			   
			 
  
			  
			
		   
			  
	   
			
	   
			
			  
			   
				
			  
   
   
 
 		
			   
	   
	
 			
		  
			  
				 
			   
				
			  
			   
	   
	  
 
 	 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_StokFiyatDegistirStokListesiGetir')
drop proc sp_StokFiyatDegistirStokListesiGetir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  proc sp_StokFiyatDegistirStokListesiGetir(@EkParametreler nvarchar(max)='')    
as     
declare  @SqlText nvarchar(max)    
    
set @SqlText=' select T.*  from (    
select SK.Barkodu, SK.Kodu, SK.UreticiFirmaKodu, SK.StokAdi, SK.OzelKod1, SK.OzelKod2,     
 SK.GrupAdi, SK.OTVOrani,SK.StokAdedi,SK.Update_Date,SK.AnaBirim,SK.AktifPasif,    
    
 SF.F1Adi,SF.F2Adi,SF.F3Adi,SF.F4Adi,SF.F5Adi,SF.F6Adi,SF.F7Adi,SF.F8Adi,SF.F9Adi,SF.F10Adi,SF.F11Adi,SF.F12Adi,SF.F13Adi,SF.F14Adi,SF.F15Adi,SF.F16Adi,    
 SF.F17Adi,SF.F18Adi,SF.F19Adi,SF.F20Adi,SF.F1Fiyat,SF.F2Fiyat,SF.F3Fiyat,SF.F4Fiyat,SF.F5Fiyat,SF.F6Fiyat,SF.F7Fiyat,SF.F8Fiyat,SF.F9Fiyat,SF.F10Fiyat,    
 SF.F11Fiyat,SF.F12Fiyat,SF.F13Fiyat,SF.F14Fiyat,SF.F15Fiyat,SF.F16Fiyat,SF.F17Fiyat,SF.F18Fiyat,SF.F19Fiyat,SF.F20Fiyat,SF.F1Kdv,SF.F2Kdv,SF.F3Kdv,    
 SF.F4Kdv,SF.F5Kdv,SF.F6Kdv,SF.F7Kdv,SF.F8Kdv,SF.F9Kdv,SF.F10Kdv,SF.F11Kdv,SF.F12Kdv,SF.F13Kdv,SF.F14Kdv,SF.F15Kdv,SF.F16Kdv,SF.F17Kdv,SF.F18Kdv,SF.F19Kdv,    
 SF.F20Kdv,SF.F1DovizKodu,SF.F2DovizKodu,SF.F3DovizKodu,SF.F4DovizKodu,SF.F5DovizKodu,SF.F6DovizKodu,SF.F7DovizKodu,    
 SF.F8DovizKodu,SF.F9DovizKodu,SF.F10DovizKodu,SF.F11DovizKodu,SF.F12DovizKodu,SF.F13DovizKodu,SF.F14DovizKodu,SF.F15DovizKodu,SF.F16DovizKodu,    
 SF.F17DovizKodu,SF.F18DovizKodu,SF.F19DovizKodu,SF.F20DovizKodu,SF.F1FiyatH,SF.F2FiyatH,SF.F3FiyatH,SF.F4FiyatH,SF.F5FiyatH,SF.F6FiyatH,SF.F7FiyatH,    
 SF.F8FiyatH,SF.F9FiyatH,SF.F10FiyatH,SF.F11FiyatH,SF.F12FiyatH,SF.F13FiyatH,SF.F14FiyatH,SF.F15FiyatH,SF.F16FiyatH,SF.F17FiyatH,SF.F18FiyatH,SF.F19FiyatH,    
 SF.F20FiyatH,SF.SatisKdv,SF.IslemNo,SF.DF,SF.StokNo     
from STOKKARTI as SK    
inner join StokFiyat as SF on SF.StokNo=SK.StokNo)T    
Where AktifPasif=''A'' '    
    
if @EkParametreler<>''    
set @SqlText=@SqlText+' and '+@EkParametreler    
    
    
--select @SqlText    
exec(@SqlText) 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_StokFiyatDegistir_V1')
drop proc sp_StokFiyatDegistir_V1
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  proc sp_StokFiyatDegistir_V1 (@EkParametreler nvarchar(max)='')      
as       
declare  @SqlText nvarchar(max)      
      
set @SqlText='  SELECT SK.Barkodu, SK.Kodu, SK.UreticiFirmaKodu, SK.StokAdi, SK.OzelKod1, SK.OzelKod2,OzelKod3,OzelKod4,       
    SK.GrupAdi, SK.OTVOrani,SK.StokAdedi,SK.Update_Date,SK.AnaBirim,SK.AktifPasif,SK.StokNo       ,
    SF.F1Fiyat,SF.F2Fiyat,SF.F3Fiyat,SF.F4Fiyat,SF.F5Fiyat,SF.F6Fiyat,SF.F7Fiyat,SF.F8Fiyat,SF.F9Fiyat,SF.F10Fiyat,
    SF.F11Fiyat,SF.F12Fiyat,SF.F13Fiyat,SF.F14Fiyat,SF.F15Fiyat,SF.F16Fiyat,SF.F17Fiyat,SF.F18Fiyat,SF.F19Fiyat,SF.F20Fiyat
				  FROM STOKKARTI SK,
    StokFiyat SF
				 WHERE SK.StokNo = SF.StokNo 
				   AND SK.AktifPasif=''A''';      
if @EkParametreler<>''      
set @SqlText=@SqlText+' and '+@EkParametreler      
      
      
--select @SqlText      
exec(@SqlText)   
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_Stok_Birim_Barkod')
 BEGIN 
 drop view V_Stok_Birim_Barkod
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  VIEW V_Stok_Birim_Barkod as
select StokNo,Kodu,Barkodu,Miktar, Birim, AnaBirim FROM (
SELECT StokNo,Kodu,Barkodu,1 Miktar,AnaBirim Birim,1 AnaBirim FROM STOKKARTI WHERE Barkodu is not null or Barkodu <> '' UNION
SELECT StokNo,Kodu,IkinciBirimBarkodu Barkodu,IkinciBirimMiktari Miktar,IkinciBirim,0 FROM STOKKARTI wHERE IkinciBirimMiktari>0  UNION
SELECT StokNo,Kodu,UcuncuBirimBarkodu,UcuncuBirimMiktari,UcuncuBirim,0 FROM STOKKARTI wHERE UcuncuBirimMiktari>0  UNION
SELECT StokNo,Kodu,DorduncuBirimBarkodu,DorduncuBirimMiktari,DorduncuBirim,0 FROM STOKKARTI wHERE DorduncuBirimMiktari>0  UNION
SELECT StokNo,Kodu,BesinciBirimBarkodu,BesinciBirimMiktari,BesinciBirim,0 FROM STOKKARTI wHERE BesinciBirimMiktari>0  UNION
SELECT StokNo,Kodu,AltinciBirimBarkodu,AltinciBirimMiktari,AltinciBirim,0 FROM STOKKARTI wHERE AltinciBirimMiktari>0  
) t WHERE Barkodu is not null and Barkodu <> '' 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_Arac_Gun_Takip')
 BEGIN 
 drop view V_Arac_Gun_Takip
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 create   view V_Arac_Gun_Takip as    
SELECT İŞLEM,TARIH,Markasi, Modeli, ModelYili, Plaka, TescilTarihi   FROM (    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'AracMuayeneGecerlilik' İŞLEM,  AracMuayeneGecerlilik TARIH  FROM  OAracKarti UNION    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'SigortaBitisTarihi', SigortaBitisTarihi  FROM  OAracKarti UNION    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'KaskoBitisTarihi', KaskoBitisTarihi  FROM  OAracKarti UNION    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'EgzosMBitisTarihi', EgzosMBitisTarihi  FROM  OAracKarti UNION    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'SonAkuDegisimTarihi' İŞLEM,SonAkuDegisimTarihi  FROM  OAracKarti UNION    
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'SonLastikDegisimTarihi',SonLastikDegisimTarihi  FROM  OAracKarti    
) T
 go--
-------------------------------------------------------------------------------------------------------------------------------------------
   IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_Arac_KM_Takip')
 BEGIN 
 drop view V_Arac_KM_Takip
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  CREATE     VIEW V_Arac_KM_Takip AS      
SELECT * FROM (       
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'HavaFilitresiKm' Islem,HavaFilitresiKm DegistirilmesiGerekenKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'YagFilitresiKm',YagFilitresiKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'MazotFilitreKm', MazotFilitreKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'BujiKm',BujiKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'PlatinKm',PlatinKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'SanzumanKm',SanzumanKm,AracinKm  FROM  OAracKarti UNION      
SELECT  Markasi, Modeli, ModelYili, Plaka, TescilTarihi,'DiferansiyelKm',DiferansiyelKm,AracinKm  FROM  OAracKarti) ARC
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spStkDINAMIKMalSayimFarki')
drop proc spStkDINAMIKMalSayimFarki
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   proc spStkDINAMIKMalSayimFarki @Tarih1 DateTime, @Tarih2 DateTime    
as    
select StokKodu, Birim, Depo,IslemTuru, sum  (Miktar) Miktar from (   
select StokKodu,GC,Birim,Depo,IslemTuru,  
case  GC WHEN 'G' THEN Miktar   when 'C' THEN Miktar*(-1) ELSE 0 END Miktar  
from STOKHAR WHERE  FaturaTarihi between @Tarih1 and @Tarih2 ) t , STOKKARTI k WHERE  t.StokKodu=k.Kodu  
GROUP BY StokKodu ,Birim, Depo,IslemTuru  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_prim_kaydet')
drop proc sp_prim_kaydet
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  proc sp_prim_kaydet(    
    @IslemNo int,    
 @GELGIDKODU nvarchar(30),      
    @GELGIDADI nvarchar(150),     
 @TARIH datetime,      
    @KULNO int,          
    @KULADI nvarchar(20),    
 @Departman nvarchar(8),         
    @KODU nvarchar(5),    
 @VardiyaKodu nvarchar(15),      
    @VardiyaDurumu nvarchar(9),     
    @BelgeNo nvarchar(30),    
    @IsIstasyonKodu nvarchar(30),      
    @IsIstasyonAdi nvarchar(30),      
    @VardiyaNo nvarchar(8),    
    
 @MUSNO1 int,          
    @MUSADI1 nvarchar(50),    
    @MUSKOD1 nvarchar(20),    
    @BORC1 nvarchar(50),    
    @ALACAK1 nvarchar(50),     
    @DOVIZTUT1 nvarchar(50),      
    @DOVIZCINSI1 int,      
    @DOVIZKODU1 nvarchar(2),      
    @DOVIZKURU1 nvarchar(50),    
    @SaticiKodu1 nvarchar(10),      
    @SaticiAdi1 nvarchar(25),      
    
 @MUSNO2 int,          
    @MUSADI2 nvarchar(50),     
    @MUSKOD2 nvarchar(20),    
    @BORC2 nvarchar(50),    
    @ALACAK2 nvarchar(50),     
    @DOVIZTUT2 nvarchar(50),      
    @DOVIZCINSI2 int,      
    @DOVIZKODU2 nvarchar(2),      
    @DOVIZKURU2 nvarchar(50),    
    @SaticiKodu2 nvarchar(10),      
    @SaticiAdi2 nvarchar(25),      
    
 @MUSNO3 int,          
    @MUSADI3 nvarchar(50),     
    @MUSKOD3 nvarchar(20),    
    @BORC3 nvarchar(50),   
    @ALACAK3 nvarchar(50),     
    @DOVIZTUT3 nvarchar(50),      
    @DOVIZCINSI3 int,      
    @DOVIZKODU3 nvarchar(2),      
    @DOVIZKURU3 nvarchar(50),    
    @SaticiKodu3 nvarchar(10),      
    @SaticiAdi3 nvarchar(25),      
    
    @MUSNO4 int,          
    @MUSADI4 nvarchar(50),     
    @MUSKOD4 nvarchar(20),    
    @BORC4 nvarchar(50),    
    @ALACAK4 nvarchar(50),     
    @DOVIZTUT4 nvarchar(50),      
    @DOVIZCINSI4 int,      
    @DOVIZKODU4 nvarchar(2),    
    @DOVIZKURU4 nvarchar(50),    
    @SaticiKodu4 nvarchar(10),      
    @SaticiAdi4 nvarchar(25)      
    
)    
    
as     
    
begin    
declare @ACIKLAMA varchar(50)    
declare @BA  nvarchar(1)     
declare @BAACIKLA nvarchar(20)     
  
declare @TESSIRNO int     
declare @AcikKapali nvarchar(1)    
declare @DovizBorc float    
Declare @DovizAlacak float    
declare @HesapAdi nvarchar(50)    
declare @KarsiHesp nvarchar(5)    
declare @KayitDuzeyi int    
declare @OzelKod1 nvarchar(10)      
declare @OzelKod2 nvarchar(10)      
declare @BordroNosu int    
declare @BordroAciklamasi nvarchar(100)    
declare @Modul nvarchar(3)    
declare @Tutar float      
declare @KdvTutari float      
declare @Kdv float     
declare @StopajTutari float      
declare @Stopaj float      
declare @FisNo int    
declare @ACIKLABAKIYE nvarchar(1)          
declare @BORDRONO int          
declare @LISTNO nvarchar(5)      
declare @OZELKOD nvarchar(3)          
declare @ISLEMTIPI nvarchar(30)     
declare @TDATE datetime    
declare @CariOPlanGunu int          
declare @GunAy nvarchar(3)          
declare @OpsiyonGunu int    
declare @IslemNo2 int          
declare @KayitDuzeyiNo int          
declare @IslemNo3 int        
declare @Aciklama2 nvarchar(30)          
declare @OnOdeme nvarchar(1)          
declare @VadeTarihi datetime       
declare @TP nvarchar(2)    
declare @Update_Date DateTime         
declare @StokKodu nvarchar(30)        
declare @SiraNo int          
declare @PosSablonKodu nvarchar(15)    
declare @NoNo int    
declare @Durum nvarchar(1)    
declare @KASAADI nvarchar(10)      
declare @KASAKODU nvarchar(6)      
declare @HesapKodu nvarchar(25)      
  
declare @BORC float    
declare @ALACAK float     
declare @DOVIZTUT float      
declare @DOVIZKURU float    
    
begin transaction    
    
set @ACIKLAMA='Komisyon Tutarı'    
set @BA='B';    
set @BAACIKLA='Gider';    
set @AcikKapali='A'    
set @DovizAlacak=0    
set @HesapAdi=''    
set @KarsiHesp='Cari'    
set @KayitDuzeyi=0    
set @OzelKod1=''    
set @OzelKod2=''    
set @BordroNosu=0    
set @BordroAciklamasi=''    
set @Modul='001'    
set @StopajTutari=0      
set @Stopaj=0    
set @FisNo=0    
set @ACIKLABAKIYE=''          
set @BORDRONO=0         
set @LISTNO=''    
set @OZELKOD=''    
set @ISLEMTIPI=''    
set @TDATE=getdate()    
set @CariOPlanGunu=0          
set @GunAy='Gün'          
set @OpsiyonGunu=0    
set @IslemNo2=0          
set @KayitDuzeyiNo=0          
set @IslemNo3=0       
set @Aciklama2=''        
set @OnOdeme='H'          
set @VadeTarihi=getdate()    
set @TP=''    
set @Update_Date=getdate()         
set @StokKodu=''     
set @PosSablonKodu=''    
set @NoNo=0    
set @Durum='G'    
set @SiraNo=0;    
set @KASAADI=''      
set @KASAKODU=''      
      
--select @MUSKOD1 as MUS1,@MUSKOD2 as MUS2, @MUSKOD3 as  MUS3,@MUSKOD4 as MUS4    
   
  
if @MUSKOD1<>''     
begin    
/*1. satıcı için prim kayıt girişi*/    
   
 set @BORC=convert(float,isnull(@BORC1,0))    
 set @ALACAK=convert(float,isnull(@ALACAK1,0))  
 set @DOVIZTUT=convert(float,isnull(@DOVIZTUT1,0))  
 set @DOVIZKURU=convert(float,isnull(@DOVIZKURU1,0))  
   
 set @HesapKodu=@MUSKOD1    
 set @DovizBorc=@BORC/@DOVIZKURU    
 set @Tutar=((@BORC/118)*100)    
 set @KdvTutari=((@BORC/118)*18)    
 set @Kdv=18    
    set @SiraNo=@SiraNo+1      	
    
 INSERT GELGID ( BORC, ALACAK, ACIKLAMA,GELGIDKODU, GELGIDADI, MUSNO,MUSADI,TARIH, BA,BAACIKLA,KODU, ISLEMNO,MUSKOD,DOVIZTUT,      
  DOVIZCINSI,DOVIZKODU, KASAADI,KASAKODU,KULNO,KULADI,AcikKapali,DovizBorc,DovizAlacak,VardiyaKodu,VardiyaDurumu, Departman,HesapKodu,      
  HesapAdi,KarsiHesp,KayitDuzeyi,BelgeNo,OzelKod1,OzelKod2,IsIstasyonKodu,IsIstasyonAdi,VardiyaNo,BordroNosu, BordroAciklamasi,      
  Modul,SaticiKodu,SaticiAdi,Tutar,KdvTutari,Kdv,StopajTutari,Stopaj, FisNo)  
 
 VALUES ( @BORC, @ALACAK, @ACIKLAMA,@GELGIDKODU, @GELGIDADI, @MUSNO1,@MUSADI1,@TARIH, @BA,@BAACIKLA,@KODU, @ISLEMNO,@MUSKOD1,@DOVIZTUT,      
  @DOVIZCINSI1,@DOVIZKODU1, @KASAADI,@KASAKODU,@KULNO,@KULADI,@AcikKapali,@DovizBorc,@DovizAlacak,@VardiyaKodu,@VardiyaDurumu, @Departman,@HesapKodu,      
  @HesapAdi,@KarsiHesp,@KayitDuzeyi,@BelgeNo,@OzelKod1,@OzelKod2,@IsIstasyonKodu,@IsIstasyonAdi,@VardiyaNo,@BordroNosu, @BordroAciklamasi,      
  @Modul,@SaticiKodu1,@SaticiAdi1,@Tutar,@KdvTutari,@Kdv,@StopajTutari,@Stopaj, @FisNo)     
    
 set @ACIKLAMA='Komisyon'     
 set @BA='A';    
 set @BAACIKLA='G.Virman';    
 set @TESSIRNO=0    
 /*Acari tablosunda alacak ve borç yer değiştirir */    
    
 INSERT ACARI ( TESSIRNO, MUSNO, MUSADI, TARIH,ACIKLAMA, ALACAK,BORC, BA,BAACIKLA, ACIKLABAKIYE,BORDRONO, LISTNO, ISLEMNO,          
    TDATE, DOVIZTUT, DOVIZCINSI,MUSKOD, DOVIZKODU, OZELKOD,ISLEMTIPI, BELGENO, KULNO,KULADI, SaticiKodu, SaticiAdi,    
    CariOPlanGunu, GunAy,    OpsiyonGunu,AcikKapali, DovizBorc, DovizAlacak,TP, VardiyaKodu, VardiyaDurumu,Departman, IslemNo2,    
    KayitDuzeyi,KayitDuzeyiNo, IslemNo3,Aciklama2,OnOdeme, VadeTarihi, IsIstasyonKodu,IsIstasyonAdi,SiraNo,VardiyaNo,PosSablonKodu,    
    Update_Date,StokKodu, FisNo,DovizKur )  

 VALUES ( @TESSIRNO, @MUSNO1, @MUSADI1, @TARIH,@ACIKLAMA, @ALACAK,@BORC, @BA,@BAACIKLA, @ACIKLABAKIYE,@BORDRONO, @LISTNO, @ISLEMNO,          
    @TDATE, @DOVIZTUT, @DOVIZCINSI1,@MUSKOD1, @DOVIZKODU1, @OZELKOD,@ISLEMTIPI, @BELGENO, @KULNO,@KULADI, @SaticiKodu1, @SaticiAdi1,    
    @CariOPlanGunu, @GunAy,    @OpsiyonGunu,@AcikKapali, @DovizBorc, @DovizAlacak,@TP, @VardiyaKodu, @VardiyaDurumu,@Departman, @IslemNo2,    
    @KayitDuzeyi,@KayitDuzeyiNo, @IslemNo3,@Aciklama2,@OnOdeme, @VadeTarihi, @IsIstasyonKodu,@IsIstasyonAdi,@SiraNo,@VardiyaNo,@PosSablonKodu,    
    @Update_Date,@StokKodu, @FisNo,@DOVIZKURU )     
    
 set @ACIKLAMA='G.Virman'      
 INSERT Odeme (IslemNo, Tarih , Tutar, Aciklama , Durum, CariNo,CariKodu,  Borc , Alacak ,  NoNo, KULNO, BAAcikla ,     
 Departman,KayitDuzeyi, SiraNo, Modul)    
 VALUES (@IslemNo, @TARIH, @Tutar, @ACIKLAMA, @Durum, @MUSNO1,@MUSKOD1,  @BORC, @ALACAK,  @NoNo, @KULNO, @BAACIKLA,     
 @Departman,@KayitDuzeyi, @SiraNo, @Modul)    
     
end    
    
    
if @MUSKOD2<>''     
begin    
/*2. satıcı için prim kayıt girişi*/    
   
    
 set @BORC=convert(float,isnull(@BORC2,0))    
 set @ALACAK=convert(float,isnull(@ALACAK2,0))  
 set @DOVIZTUT=convert(float,isnull(@DOVIZTUT2,0))  
 set @DOVIZKURU=convert(float,isnull(@DOVIZKURU2,0))  
   
 set @HesapKodu=@MUSKOD2     
 set @DovizBorc=@BORC/@DOVIZKURU    
 set @Tutar=((@BORC/118)*100)    
 set @KdvTutari=((@BORC/118)*18)    
 set @Kdv=18    
    set @SiraNo=@SiraNo+1         
  
        
    
 INSERT GELGID ( BORC, ALACAK, ACIKLAMA,GELGIDKODU, GELGIDADI, MUSNO,MUSADI,TARIH, BA,BAACIKLA,KODU, ISLEMNO,MUSKOD,DOVIZTUT,      
  DOVIZCINSI,DOVIZKODU, KASAADI,KASAKODU,KULNO,KULADI,AcikKapali,DovizBorc,DovizAlacak,VardiyaKodu,VardiyaDurumu, Departman,HesapKodu,      
  HesapAdi,KarsiHesp,KayitDuzeyi,BelgeNo,OzelKod1,OzelKod2,IsIstasyonKodu,IsIstasyonAdi,VardiyaNo,BordroNosu, BordroAciklamasi,      
  Modul,SaticiKodu,SaticiAdi,Tutar,KdvTutari,Kdv,StopajTutari,Stopaj, FisNo)

 VALUES ( @BORC, @ALACAK, @ACIKLAMA,@GELGIDKODU, @GELGIDADI, @MUSNO2,@MUSADI2,@TARIH, @BA,@BAACIKLA,@KODU, @ISLEMNO,@MUSKOD2,@DOVIZTUT,      
  @DOVIZCINSI2,@DOVIZKODU2, @KASAADI,@KASAKODU,@KULNO,@KULADI,@AcikKapali,@DovizBorc,@DovizAlacak,@VardiyaKodu,@VardiyaDurumu, @Departman,@HesapKodu,      
  @HesapAdi,@KarsiHesp,@KayitDuzeyi,@BelgeNo,@OzelKod1,@OzelKod2,@IsIstasyonKodu,@IsIstasyonAdi,@VardiyaNo,@BordroNosu, @BordroAciklamasi,      
  @Modul,@SaticiKodu2,@SaticiAdi2,@Tutar,@KdvTutari,@Kdv,@StopajTutari,@Stopaj, @FisNo)     
    
 set @ACIKLAMA='Komisyon'     
 set @BA='A';    
 set @BAACIKLA='G.Virman';    
 set @TESSIRNO=0    
 /*Acari tablosunda alacak ve borç yer değiştirir */    
    
 INSERT ACARI ( TESSIRNO, MUSNO, MUSADI, TARIH,ACIKLAMA, ALACAK,BORC, BA,BAACIKLA, ACIKLABAKIYE,BORDRONO, LISTNO, ISLEMNO,          
    TDATE, DOVIZTUT, DOVIZCINSI,MUSKOD, DOVIZKODU, OZELKOD,ISLEMTIPI, BELGENO, KULNO,KULADI, SaticiKodu, SaticiAdi,    
    CariOPlanGunu, GunAy,    OpsiyonGunu,AcikKapali, DovizBorc, DovizAlacak,TP, VardiyaKodu, VardiyaDurumu,Departman, IslemNo2,    
    KayitDuzeyi,KayitDuzeyiNo, IslemNo3,Aciklama2,OnOdeme, VadeTarihi, IsIstasyonKodu,IsIstasyonAdi,SiraNo,VardiyaNo,PosSablonKodu,    
    Update_Date,StokKodu, FisNo,DovizKur ) 
	VALUES ( @TESSIRNO, @MUSNO2, @MUSADI2, @TARIH,@ACIKLAMA, @ALACAK,@BORC, @BA,@BAACIKLA, @ACIKLABAKIYE,@BORDRONO, @LISTNO, @ISLEMNO,          
    @TDATE, @DOVIZTUT, @DOVIZCINSI2,@MUSKOD2, @DOVIZKODU2, @OZELKOD,@ISLEMTIPI, @BELGENO, @KULNO,@KULADI, @SaticiKodu2, @SaticiAdi2,    
    @CariOPlanGunu, @GunAy,    @OpsiyonGunu,@AcikKapali, @DovizBorc, @DovizAlacak,@TP, @VardiyaKodu, @VardiyaDurumu,@Departman, @IslemNo2,    
    @KayitDuzeyi,@KayitDuzeyiNo, @IslemNo3,@Aciklama2,@OnOdeme, @VadeTarihi, @IsIstasyonKodu,@IsIstasyonAdi,@SiraNo,@VardiyaNo,@PosSablonKodu,    
    @Update_Date,@StokKodu, @FisNo,@DOVIZKURU )     
    
 set @ACIKLAMA='G.Virman'      
 INSERT Odeme (IslemNo, Tarih, Tutar, Aciklama, Durum, CariNo,CariKodu,  Borc, Alacak,  NoNo, KULNO, BAAcikla,     
 Departman,KayitDuzeyi, SiraNo, Modul)   
 VALUES (@IslemNo, @TARIH, @Tutar, @ACIKLAMA, @Durum, @MUSNO2,@MUSKOD2,  @BORC, @ALACAK,  @NoNo, @KULNO, @BAACIKLA,     
 @Departman,@KayitDuzeyi, @SiraNo, @Modul)     
end    
    
if @MUSKOD3<>''     
begin    
/*3. satıcı için prim kayıt girişi*/    
  
    
 set @BORC=convert(float,isnull(@BORC3,0))    
 set @ALACAK=convert(float,isnull(@ALACAK3,0))  
 set @DOVIZTUT=convert(float,isnull(@DOVIZTUT3,0))  
 set @DOVIZKURU=convert(float,isnull(@DOVIZKURU3,0))  
  
 set @HesapKodu=@MUSKOD3    
 set @DovizBorc=@BORC/@DOVIZKURU    
 set @Tutar=((@BORC/118)*100)    
 set @KdvTutari=((@BORC/118)*18)    
 set @Kdv=18    
    set @SiraNo=@SiraNo+1         
       
    
 INSERT GELGID ( BORC, ALACAK, ACIKLAMA,GELGIDKODU, GELGIDADI, MUSNO,MUSADI,TARIH, BA,BAACIKLA,KODU, ISLEMNO,MUSKOD,DOVIZTUT,      
  DOVIZCINSI,DOVIZKODU, KASAADI,KASAKODU,KULNO,KULADI,AcikKapali,DovizBorc,DovizAlacak,VardiyaKodu,VardiyaDurumu, Departman,HesapKodu,      
  HesapAdi,KarsiHesp,KayitDuzeyi,BelgeNo,OzelKod1,OzelKod2,IsIstasyonKodu,IsIstasyonAdi,VardiyaNo,BordroNosu, BordroAciklamasi,      
  Modul,SaticiKodu,SaticiAdi,Tutar,KdvTutari,Kdv,StopajTutari,Stopaj, FisNo) 
  VALUES ( @BORC, @ALACAK, @ACIKLAMA,@GELGIDKODU, @GELGIDADI, @MUSNO3,@MUSADI3,@TARIH, @BA,@BAACIKLA,@KODU, @ISLEMNO,@MUSKOD3,@DOVIZTUT,      
  @DOVIZCINSI3,@DOVIZKODU3, @KASAADI,@KASAKODU,@KULNO,@KULADI,@AcikKapali,@DovizBorc,@DovizAlacak,@VardiyaKodu,@VardiyaDurumu, @Departman,@HesapKodu,      
  @HesapAdi,@KarsiHesp,@KayitDuzeyi,@BelgeNo,@OzelKod1,@OzelKod2,@IsIstasyonKodu,@IsIstasyonAdi,@VardiyaNo,@BordroNosu, @BordroAciklamasi,      
  @Modul,@SaticiKodu3,@SaticiAdi3,@Tutar,@KdvTutari,@Kdv,@StopajTutari,@Stopaj, @FisNo)     
    
 set @ACIKLAMA='Komisyon'     
 set @BA='A';    
 set @BAACIKLA='G.Virman';    
 set @TESSIRNO=0    
 /*Acari tablosunda alacak ve borç yer değiştirir */    
    
 INSERT ACARI ( TESSIRNO, MUSNO, MUSADI, TARIH,ACIKLAMA, ALACAK,BORC, BA,BAACIKLA, ACIKLABAKIYE,BORDRONO, LISTNO, ISLEMNO,          
    TDATE, DOVIZTUT, DOVIZCINSI,MUSKOD, DOVIZKODU, OZELKOD,ISLEMTIPI, BELGENO, KULNO,KULADI, SaticiKodu, SaticiAdi,    
    CariOPlanGunu, GunAy,    OpsiyonGunu,AcikKapali, DovizBorc, DovizAlacak,TP, VardiyaKodu, VardiyaDurumu,Departman, IslemNo2,    
    KayitDuzeyi,KayitDuzeyiNo, IslemNo3,Aciklama2,OnOdeme, VadeTarihi, IsIstasyonKodu,IsIstasyonAdi,SiraNo,VardiyaNo,PosSablonKodu,    
    Update_Date,StokKodu, FisNo,DovizKur ) 
	VALUES ( @TESSIRNO, @MUSNO3, @MUSADI3, @TARIH,@ACIKLAMA, @ALACAK,@BORC, @BA,@BAACIKLA, @ACIKLABAKIYE,@BORDRONO, @LISTNO, @ISLEMNO,          
    @TDATE, @DOVIZTUT, @DOVIZCINSI3,@MUSKOD3, @DOVIZKODU3, @OZELKOD,@ISLEMTIPI, @BELGENO, @KULNO,@KULADI, @SaticiKodu3, @SaticiAdi3,    
    @CariOPlanGunu, @GunAy,    @OpsiyonGunu,@AcikKapali, @DovizBorc, @DovizAlacak,@TP, @VardiyaKodu, @VardiyaDurumu,@Departman, @IslemNo2,    
    @KayitDuzeyi,@KayitDuzeyiNo, @IslemNo3,@Aciklama2,@OnOdeme, @VadeTarihi, @IsIstasyonKodu,@IsIstasyonAdi,@SiraNo,@VardiyaNo,@PosSablonKodu,    
    @Update_Date,@StokKodu, @FisNo,@DOVIZKURU )     
    
 set @ACIKLAMA='G.Virman'      
 INSERT Odeme (IslemNo, Tarih , Tutar, Aciklama , Durum, CariNo,CariKodu,  Borc , Alacak ,  NoNo, KULNO, BAAcikla ,     
 Departman,KayitDuzeyi, SiraNo, Modul) 
 VALUES (@IslemNo, @TARIH, @Tutar, @ACIKLAMA, @Durum, @MUSNO3,@MUSKOD3,  @BORC, @ALACAK,  @NoNo, @KULNO, @BAACIKLA,     
 @Departman,@KayitDuzeyi, @SiraNo, @Modul)     
end    
    
    
if @MUSKOD4<>''     
begin    
/*4. satıcı için prim kayıt girişi*/    
 set @BORC=convert(float,isnull(@BORC4,0))    
 set @ALACAK=convert(float,isnull(@ALACAK4,0))  
 set @DOVIZTUT=convert(float,isnull(@DOVIZTUT4,0))  
 set @DOVIZKURU=convert(float,isnull(@DOVIZKURU4,0))  
  
 set @HesapKodu=@MUSKOD4    
 set @DovizBorc=@BORC/@DOVIZKURU    
 set @Tutar=((@BORC/118)*100)    
 set @KdvTutari=((@BORC/118)*18)    
 set @Kdv=18    
    set @SiraNo=@SiraNo+1         
        
    
 INSERT GELGID ( BORC, ALACAK, ACIKLAMA,GELGIDKODU, GELGIDADI, MUSNO,MUSADI,TARIH, BA,BAACIKLA,KODU, ISLEMNO,MUSKOD,DOVIZTUT,      
  DOVIZCINSI,DOVIZKODU, KASAADI,KASAKODU,KULNO,KULADI,AcikKapali,DovizBorc,DovizAlacak,VardiyaKodu,VardiyaDurumu, Departman,HesapKodu,      
  HesapAdi,KarsiHesp,KayitDuzeyi,BelgeNo,OzelKod1,OzelKod2,IsIstasyonKodu,IsIstasyonAdi,VardiyaNo,BordroNosu, BordroAciklamasi,      
  Modul,SaticiKodu,SaticiAdi,Tutar,KdvTutari,Kdv,StopajTutari,Stopaj, FisNo) 
  VALUES ( @BORC, @ALACAK, @ACIKLAMA,@GELGIDKODU, @GELGIDADI, @MUSNO4,@MUSADI4,@TARIH, @BA,@BAACIKLA,@KODU, @ISLEMNO,@MUSKOD4,@DOVIZTUT,      
  @DOVIZCINSI4,@DOVIZKODU4, @KASAADI,@KASAKODU,@KULNO,@KULADI,@AcikKapali,@DovizBorc,@DovizAlacak,@VardiyaKodu,@VardiyaDurumu, @Departman,@HesapKodu,      
  @HesapAdi,@KarsiHesp,@KayitDuzeyi,@BelgeNo,@OzelKod1,@OzelKod2,@IsIstasyonKodu,@IsIstasyonAdi,@VardiyaNo,@BordroNosu, @BordroAciklamasi,      
  @Modul,@SaticiKodu4,@SaticiAdi4,@Tutar,@KdvTutari,@Kdv,@StopajTutari,@Stopaj, @FisNo)     
    
 set @ACIKLAMA='Komisyon'     
 set @BA='A';    
 set @BAACIKLA='G.Virman';    
 set @TESSIRNO=0    
 /*Acari tablosunda alacak ve borç yer değiştirir */    
    
 INSERT ACARI ( TESSIRNO, MUSNO, MUSADI, TARIH,ACIKLAMA, ALACAK,BORC, BA,BAACIKLA, ACIKLABAKIYE,BORDRONO, LISTNO, ISLEMNO,          
    TDATE, DOVIZTUT, DOVIZCINSI,MUSKOD, DOVIZKODU, OZELKOD,ISLEMTIPI, BELGENO, KULNO,KULADI, SaticiKodu, SaticiAdi,    
    CariOPlanGunu, GunAy,    OpsiyonGunu,AcikKapali, DovizBorc, DovizAlacak,TP, VardiyaKodu, VardiyaDurumu,Departman, IslemNo2,    
    KayitDuzeyi,KayitDuzeyiNo, IslemNo3,Aciklama2,OnOdeme, VadeTarihi, IsIstasyonKodu,IsIstasyonAdi,SiraNo,VardiyaNo,PosSablonKodu,    
    Update_Date,StokKodu, FisNo,DovizKur ) 
	VALUES ( @TESSIRNO, @MUSNO4, @MUSADI4, @TARIH,@ACIKLAMA, @ALACAK,@BORC, @BA,@BAACIKLA, @ACIKLABAKIYE,@BORDRONO, @LISTNO, @ISLEMNO,          
    @TDATE, @DOVIZTUT, @DOVIZCINSI4,@MUSKOD4, @DOVIZKODU4, @OZELKOD,@ISLEMTIPI, @BELGENO, @KULNO,@KULADI, @SaticiKodu4, @SaticiAdi4,    
    @CariOPlanGunu, @GunAy,    @OpsiyonGunu,@AcikKapali, @DovizBorc, @DovizAlacak,@TP, @VardiyaKodu, @VardiyaDurumu,@Departman, @IslemNo2,    
    @KayitDuzeyi,@KayitDuzeyiNo, @IslemNo3,@Aciklama2,@OnOdeme, @VadeTarihi, @IsIstasyonKodu,@IsIstasyonAdi,@SiraNo,@VardiyaNo,@PosSablonKodu,    
    @Update_Date,@StokKodu, @FisNo,@DOVIZKURU )     
    
 set @ACIKLAMA='G.Virman'      
 INSERT Odeme (IslemNo, Tarih , Tutar, Aciklama , Durum, CariNo,CariKodu,  Borc , Alacak ,  NoNo, KULNO, BAAcikla ,     
 Departman,KayitDuzeyi, SiraNo, Modul) 
 VALUES (@IslemNo, @TARIH, @Tutar, @ACIKLAMA, @Durum, @MUSNO4,@MUSKOD4,  @BORC, @ALACAK,  @NoNo, @KULNO, @BAACIKLA,     
 @Departman,@KayitDuzeyi, @SiraNo, @Modul)     
end    
    
if @@ERROR<>0          
 begin          
  ROLLBACK           
  Select 'İşlem sırasında bir hata oluştu, işlem tamamlanamadı!'    
 end          
 ELSE begin           
COMMIT     
  Select 'İşlem başarıyla tamamlandı';      
end     
    
end  
go--
      				   
if exists (select * from sys.procedures where name='sp_Fatura_Aktar')
drop proc sp_Fatura_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE   PROCEDURE [dbo].[sp_Fatura_Aktar]  
(  
    @Hedef   SYSNAME,  
	@Kaynak  SYSNAME,  
	@IslemNo nvarchar(max)  
)  
  
AS   
BEGIN  
ALTER TABLE STOKHAR NOCHECK CONSTRAINT ALL  
ALTER TABLE ACARI NOCHECK CONSTRAINT ALL  

 DECLARE @Query nvarchar(MAX); 
	 
 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.STOKHAR NOCHECK CONSTRAINT ALL' + Char(13);
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.STOKHARDETBAG NOCHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.ACARI NOCHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.KASA NOCHECK CONSTRAINT ALL'+ Char(13); 
 EXEC sp_executesql @Query;  
  
 exec sp_STOKHARDETBAG_Aktar @Hedef,@Kaynak,@IslemNo;  
 exec sp_STOKHAR_Aktar @Hedef,@Kaynak,@IslemNo;  
 exec sp_ACARI_Aktar @Hedef,@Kaynak,@IslemNo;  
 exec sp_KASA_Aktar @Hedef,@Kaynak,@IslemNo;  
 exec sp_Odeme_Aktar @Hedef,@Kaynak,@IslemNo;  
  
 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.STOKHAR CHECK CONSTRAINT ALL' + Char(13);
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.STOKHARDETBAG CHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.ACARI CHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.KASA CHECK CONSTRAINT ALL'+ Char(13); 
 EXEC sp_executesql @Query;  
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_STOKHARDETBAG_Aktar')
drop proc sp_STOKHARDETBAG_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  CREATE  PROCEDURE [dbo].[sp_STOKHARDETBAG_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@IslemNo    nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.STOKHARDETBAG WHERE IslemNo in (Select IslemNo from '+@Kaynak+'.dbo.STOKHARDETBAG where IslemNo in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[STOKHARDETBAG] 
 			([IslemNo], [IslemTuru], [CariNo], [CariKodu], [CariAdi], [Tutar], [SiparisTarihi], [KdvTutari], [FisTuru], [GC], [EkIskTut1], [EkIskTut2], [EkIskTut3], [EkIskTutTop], [FaturaNo], [FaturaTarihi], [Satici], [DovizTut], [DovizCinsi], [DovizKodu], [DovizTutB], [BelgeNo], [OzelKod1], [OzelKod2], [OzelKod3], [IslemTipi], [IrsNo], [Ack1], [Ack2], [Ack3], [Ack4], [KULNO], [KULADI], [FatSerNo], [SaticiAdi], [AltIskYuzde1], [AltIskYuzde2], [AltIskYuzde3], [AK], [FaturaOPlanGunu], [Depo], [GunAy], [OpsiyonGunu], [AcikKapali], [DovizBorc], [DovizAlacak], [FYaz], [SecimEH], [IslemNo3], [TP], [FisNo], [VardiyaKodu], [VardiyaDurumu], [Departman], [MasrafToplami], [KDVHD], [IslemNo4], [KayitDuzeyi], [YazarKasaNo], [YazarKasaFisNo], [YaziciCount], [IsIstasyonKodu], [IsIstasyonAdi], [TevkifatTutari], [VardiyaNo], [OdemeBilgisi], [FaturaAdresNo], [SevkAdresNo], [FI], [SevkTarihi], [eFaturaNo], [UUID], [eFaturaGonderimTarihi], [eFaturaDurumu], [EF_ISTISNA], [FaturaTipi], [Entegrator], [Update_Date], [DovizKur])	
 	SELECT [IslemNo], [IslemTuru], [CariNo], [CariKodu], [CariAdi], [Tutar], [SiparisTarihi], [KdvTutari], [FisTuru], [GC], [EkIskTut1], [EkIskTut2], [EkIskTut3], [EkIskTutTop], [FaturaNo], [FaturaTarihi], [Satici], [DovizTut], [DovizCinsi], [DovizKodu], [DovizTutB], [BelgeNo], [OzelKod1], [OzelKod2], [OzelKod3], [IslemTipi], [IrsNo], [Ack1], [Ack2], [Ack3], [Ack4], [KULNO], [KULADI], [FatSerNo], [SaticiAdi], [AltIskYuzde1], [AltIskYuzde2], [AltIskYuzde3], [AK], [FaturaOPlanGunu], [Depo], [GunAy], [OpsiyonGunu], [AcikKapali], [DovizBorc], [DovizAlacak], [FYaz], [SecimEH], [IslemNo3], [TP], [FisNo], [VardiyaKodu], [VardiyaDurumu], [Departman], [MasrafToplami], [KDVHD], [IslemNo4], [KayitDuzeyi], [YazarKasaNo], [YazarKasaFisNo], [YaziciCount], [IsIstasyonKodu], [IsIstasyonAdi], [TevkifatTutari], [VardiyaNo], [OdemeBilgisi], [FaturaAdresNo], [SevkAdresNo], [FI], [SevkTarihi], [eFaturaNo], [UUID], [eFaturaGonderimTarihi], [eFaturaDurumu], [EF_ISTISNA], [FaturaTipi], [Entegrator], [Update_Date], [DovizKur]
 	FROM ' + @Kaynak + '.dbo.[STOKHARDETBAG] S WHERE S.IslemNo in ('+@IslemNo+')';
 	
   EXEC sp_executesql @Query;
END;

go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_STOKHAR_Aktar')
drop proc sp_STOKHAR_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 
CREATE PROCEDURE [dbo].[sp_STOKHAR_Aktar]
(
    @Hedef	SYSNAME,
	@Kaynak	SYSNAME,
	@IslemNo    nvarchar(max)
)
AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query NVARCHAR(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.STOKHAR WHERE IslemNo in (Select IslemNo from '+@Kaynak+'.dbo.STOKHAR where IslemNo in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[STOKHAR] (IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, StokNo, StokKodu, StokAdi, Miktar, Birim, BirimMiktar, BirimFiyat, 
 Iskonto1, Iskonto2, Iskonto3, Iskonto4, Tutar, SiparisNo, SiparisTarihi, Kdv, KdvTutari, IndirimTutari, SatirIskTut, FisTuru, GC, EkIskTut1, EkIskTut2, 
 EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, TopTutar, KDVHD, DovizTut, DovizCinsi, DovizKodu, DovizTutB, Depo, AnaBirimFiyat, FaturaMiktari, 
 IslemNo2, OTVOrani, OTVTutari, KULNO, KULADI, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AcikKapali, DovizBorc, DovizAlacak, IslemNo3, IslemNo4, 
 TankKodu, TankAdi, FisNo, Plaka, TP, VardiyaKodu, TahsilatEH, km, Departman, SonTutar, SonTutarDov, SatMasrafTutari, SatIskTut1, SatIskTut2, SatIskTut3, 
 SatIskTut4, AnaBirim, OtvBirimTutari, IlkBirimFiyat, VadeFarkliBirimFiyat, LifoFiyat, FifoFiyat, NFiyat, NFiyatDovBr, StokTipi, VardiyaNo, Markasi, SiraNo, 
 Iskonto5, Iskonto6, Iskonto7, Iskonto8, Iskonto9, Iskonto10, SatIskTut5, SatIskTut6, SatIskTut7, SatIskTut8, SatIskTut9, SatIskTut10, RBIslemNo, 
 StokAgirligi, KontrolBarkodu, HatKodu, LotNo, StokBolge, StokRaf, StokYer, BurutMiktar, FireOrani, FireMiktari, DovizKuru)
		SELECT IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, StokNo, StokKodu, StokAdi, Miktar, Birim, BirimMiktar, BirimFiyat, Iskonto1, Iskonto2, Iskonto3, Iskonto4, Tutar, SiparisNo, SiparisTarihi, Kdv, KdvTutari, IndirimTutari, SatirIskTut, FisTuru, GC, EkIskTut1, EkIskTut2, EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, TopTutar, KDVHD, DovizTut, DovizCinsi, DovizKodu, DovizTutB, Depo, AnaBirimFiyat, FaturaMiktari, IslemNo2, OTVOrani, OTVTutari, KULNO, KULADI, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AcikKapali, DovizBorc, DovizAlacak, IslemNo3, IslemNo4, TankKodu, TankAdi, FisNo, Plaka, TP, VardiyaKodu, TahsilatEH, km, Departman, SonTutar, SonTutarDov, SatMasrafTutari, SatIskTut1, SatIskTut2, SatIskTut3, SatIskTut4, AnaBirim, OtvBirimTutari, IlkBirimFiyat, VadeFarkliBirimFiyat, LifoFiyat, FifoFiyat, NFiyat, NFiyatDovBr, StokTipi, VardiyaNo, Markasi, SiraNo, Iskonto5, Iskonto6, Iskonto7, Iskonto8, Iskonto9, Iskonto10, SatIskTut5, SatIskTut6, SatIskTut7, SatIskTut8, SatIskTut9, SatIskTut10, RBIslemNo, StokAgirligi, KontrolBarkodu, HatKodu, LotNo, StokBolge, StokRaf, StokYer, BurutMiktar, FireOrani, FireMiktari, DovizKuru 
 FROM ' + @Kaynak + '.dbo.[STOKHAR] S WHERE S.IslemNo in ('+@IslemNo+')';

	--SELECT @Query Sorgu; 	
	--PRINT @Query;
	EXEC sp_executesql @Query;
	--EXEC ( @Query )

END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_ACARI_Aktar')
drop proc sp_ACARI_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE  PROCEDURE [dbo].[sp_ACARI_Aktar]
(
    @Hedef	SYSNAME,
	@Kaynak	SYSNAME,
	@IslemNo    nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query NVARCHAR(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.ACARI WHERE ISLEMNO in (Select ISLEMNO from '+@Kaynak+'.dbo.ACARI where ISLEMNO in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[ACARI] 
 			(
      [TESSIRNO]
      ,[MUSNO]
      ,[MUSADI]
      ,[TARIH]
      ,[ACIKLAMA]
      ,[BORC]
      ,[ALACAK]
      ,[BA]
      ,[BAACIKLA]
      ,[ACIKLABAKIYE]
      ,[BORDRONO]
      ,[LISTNO]
      ,[ISLEMNO]
      ,[TDATE]
      ,[DOVIZTUT]
      ,[DOVIZCINSI]
      ,[MUSKOD]
      ,[DOVIZKODU]
      ,[OZELKOD]
      ,[ISLEMTIPI]
      ,[BELGENO]
      ,[KULNO]
      ,[KULADI]
      ,[SaticiKodu]
      ,[SaticiAdi]
      ,[CariOPlanGunu]
      ,[GunAy]
      ,[OpsiyonGunu]
      ,[AcikKapali]
      ,[DovizBorc]
      ,[DovizAlacak]
      ,[TP]
      ,[VardiyaKodu]
      ,[VardiyaDurumu]
      ,[Departman]
      ,[IslemNo2]
      ,[KayitDuzeyi]
      ,[KayitDuzeyiNo]
      ,[IslemNo3]
      ,[Aciklama2]
      ,[OnOdeme]
      ,[VadeTarihi]
      ,[IsIstasyonKodu]
      ,[IsIstasyonAdi]
      ,[SiraNo]
      ,[VardiyaNo]
      ,[PosSablonKodu]
      ,[Update_Date]
      ,[StokKodu]
      ,[FisNo])	
 	SELECT [TESSIRNO]
      ,[MUSNO]
      ,[MUSADI]
      ,[TARIH]
      ,[ACIKLAMA]
      ,[BORC]
      ,[ALACAK]
      ,[BA]
      ,[BAACIKLA]
      ,[ACIKLABAKIYE]
      ,[BORDRONO]
      ,[LISTNO]
      ,[ISLEMNO]
      ,[TDATE]
      ,[DOVIZTUT]
      ,[DOVIZCINSI]
      ,[MUSKOD]
      ,[DOVIZKODU]
      ,[OZELKOD]
      ,[ISLEMTIPI]
      ,[BELGENO]
      ,[KULNO]
      ,[KULADI]
      ,[SaticiKodu]
      ,[SaticiAdi]
      ,[CariOPlanGunu]
      ,[GunAy]
      ,[OpsiyonGunu]
      ,[AcikKapali]
      ,[DovizBorc]
      ,[DovizAlacak]
      ,[TP]
      ,[VardiyaKodu]
      ,[VardiyaDurumu]
      ,[Departman]
      ,[IslemNo2]
      ,[KayitDuzeyi]
      ,[KayitDuzeyiNo]
      ,[IslemNo3]
      ,[Aciklama2]
      ,[OnOdeme]
      ,[VadeTarihi]
      ,[IsIstasyonKodu]
      ,[IsIstasyonAdi]
      ,[SiraNo]
      ,[VardiyaNo]
      ,[PosSablonKodu]
      ,[Update_Date]
      ,[StokKodu]
      ,[FisNo] 
 	FROM ' + @Kaynak + '.dbo.[ACARI] S WHERE S.ISLEMNO in ('+@IslemNo+')';

	--SELECT @Query Sorgu
	--PRINT @Query;
	EXEC sp_executesql @Query;
	--EXEC ( @Query )

END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_KASA_Aktar')
drop proc sp_KASA_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE    PROCEDURE [dbo].[sp_KASA_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak	SYSNAME,
	@IslemNo    nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query NVARCHAR(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.KASA WHERE ISLEMNO in (Select ISLEMNO from '+@Kaynak+'.dbo.KASA where ISLEMNO in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[KASA] 
 			([BORC]
      ,[ALACAK]
      ,[ACIKLAMA]
      ,[KASAKODU]
      ,[KASAADI]
      ,[MUSNO]
      ,[MUSADI]
      ,[TARIH]
      ,[BA]
      ,[BAACIKLA]
      ,[ISLEMNO]
      ,[MUSKOD]
      ,[DovizTut]
      ,[DovizCinsi]
      ,[DovizKodu]
      ,[AcikKapali]
      ,[IslemNo2]
      ,[AktarimTarihi]
      ,[BelgeNo]
      ,[KULNO]
      ,[KULADI]
      ,[NakitVisa]
      ,[SaticiKodu]
      ,[SaticiAdi]
      ,[DovizBorc]
      ,[DovizAlacak]
      ,[VardiyaKodu]
      ,[VardiyaDurumu]
      ,[Departman]
      ,[KayitDuzeyi]
      ,[IsIstasyonKodu]
      ,[IsIstasyonAdi]
      ,[VardiyaNo]
      ,[FisNo])	
 	SELECT [BORC]
      ,[ALACAK]
      ,[ACIKLAMA]
      ,[KASAKODU]
      ,[KASAADI]
      ,[MUSNO]
      ,[MUSADI]
      ,[TARIH]
      ,[BA]
      ,[BAACIKLA]
      ,[ISLEMNO]
      ,[MUSKOD]
      ,[DovizTut]
      ,[DovizCinsi]
      ,[DovizKodu]
      ,[AcikKapali]
      ,[IslemNo2]
      ,[AktarimTarihi]
      ,[BelgeNo]
      ,[KULNO]
      ,[KULADI]
      ,[NakitVisa]
      ,[SaticiKodu]
      ,[SaticiAdi]
      ,[DovizBorc]
      ,[DovizAlacak]
      ,[VardiyaKodu]
      ,[VardiyaDurumu]
      ,[Departman]
      ,[KayitDuzeyi]
      ,[IsIstasyonKodu]
      ,[IsIstasyonAdi]
      ,[VardiyaNo]
      ,[FisNo]
 	FROM ' + @Kaynak + '.dbo.[KASA] S WHERE S.ISLEMNO in ('+@IslemNo+')';
	--SELECT @Query Sorgu
	--PRNT @Query;
	EXEC sp_executesql @Query;
	--EXEC ( @Query )

END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_Odeme_Aktar')
drop proc sp_Odeme_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 
CREATE     PROCEDURE [dbo].[sp_Odeme_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak	SYSNAME,
	@IslemNo    nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query NVARCHAR(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.Odeme WHERE IslemNo in (Select IslemNo from '+@Kaynak+'.dbo.Odeme where IslemNo in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[Odeme] 
				 (IslemNo, Tarih, Tutar, Aciklama, Durum, CariNo, CariKodu, Borc, Alacak, NoNo, KULNO, BAAcikla, Departman, KayitDuzeyi, SiraNo, Modul)
			 SELECT IslemNo, Tarih, Tutar, Aciklama, Durum, CariNo, CariKodu, Borc, Alacak, NoNo, KULNO, BAAcikla, Departman, KayitDuzeyi, SiraNo, Modul
                 FROM ' + @Kaynak + '.dbo.[Odeme] S WHERE S.IslemNo in ('+@IslemNo+')';
	--SELECT @Query Sorgu
	--PRNT @Query;
	EXEC sp_executesql @Query;
	--EXEC ( @Query )

END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_Prk_Satis_Cevirme')
drop proc sp_Prk_Satis_Cevirme
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 create  procedure sp_Prk_Satis_Cevirme(@CariKodu nvarchar(50),@IslemNo int,@YeniIslemNo int,@IslemTuruSatis nvarchar(50),                    
@IslemTuruPrkSatis nvarchar(50),                    
@FaturaTarihi datetime,@FaturaNo integer,@FatSerNo nvarchar(50),@SeciliIslemNolar nvarchar(max),@GeldigiYer nvarchar(50)='',@YazarKasaFisNo int = 0,@FaturaTipi int= 0)                    
as           
        
        
declare @sqlText nvarchar(max)                    
declare @sqlTextHepsi nvarchar(max)                    
                    
declare @i integer                    
declare @toplam integer;                    
declare @ParamIslemNo integer                                 
declare @ExecuteCalistir int;              
 BEGIN TRANSACTION               
  set @ExecuteCalistir=1;              
                
 UPDATE STOKHAR SET AcikKapali='K',IslemNo3= @YeniIslemNo,FisTuru = 'I',IslemTuru= @IslemTuruSatis WHERE IslemNo in (SELECT convert(int,Name) FROM splitstring(@SeciliIslemNolar, ','))    
 UPDATE STOKHARDETBAG SET AcikKapali='K',SecimEH= 'H',FisTuru = 'I',IslemTuru = @IslemTuruSatis,FaturaTarihi = @FaturaTarihi  WHERE IslemNo in (SELECT convert(int,Name) FROM splitstring(@SeciliIslemNolar, ','))    
                            
   print('/*En Büyük İşlem No Parametre - Seçili İşlem Numaraları */'+convert(varchar,@IslemNo)+'-'+@SeciliIslemNolar);    
     
  SET @sqlText=' insert into STOKHARDETBAG (IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, Tutar,  KdvTutari, FisTuru, GC, '+          
    
    ' FaturaNo,  Satici, DovizTut, DovizCinsi, DovizKodu, DovizTutB, '+                
                ' BelgeNo, OzelKod1, OzelKod2, OzelKod3, IslemTipi, IrsNo, Ack1, Ack2, Ack3, Ack4, KULNO, KULADI, FatSerNo, SaticiAdi, AK, FaturaOPlanGunu, Depo, GunAy, OpsiyonGunu, AcikKapali, '+                 
                ' DovizBorc, DovizAlacak,  IslemNo3, TP,  VardiyaKodu, VardiyaDurumu, Departman, MasrafToplami, KDVHD, IslemNo4, KayitDuzeyi, YazarKasaNo, YazarKasaFisNo, YaziciCount, IsIstasyonKodu, '+                 
                ' IsIstasyonAdi, TevkifatTutari, VardiyaNo, OdemeBilgisi, FaturaAdresNo, SevkAdresNo, FI,  eFaturaNo, UUID, eFaturaGonderimTarihi, eFaturaDurumu, EF_ISTISNA,  FaturaTipi,  '+                
                ' Entegrator,DovizKur)  '+                
    ' select top 1 '+CONVERT(VARCHAR,@YeniIslemNo)+' as IslemNo, IslemTuru, CariNo, CariKodu, CariAdi,'+                
    ' 0 as Tutar, '+              
    ' 0 as KdvTutari, '+                
    ' ''F'' as FisTuru, GC, '+                 
    /*' EkIskTut1, EkIskTut2, EkIskTut3, EkIskTutTop, '+*/                
    ' '+convert(varchar,@FaturaNo)+' as FaturaNo, Satici, '+                
    ' 0 as DovizTut, '+                
    ' DovizCinsi, DovizKodu,  0 as DovizTutB, '+                
                ' BelgeNo, OzelKod1, OzelKod2, OzelKod3, IslemTipi, IrsNo, Ack1, Ack2, Ack3, Ack4, KULNO, KULADI, '+@FatSerNo+', SaticiAdi, AK, FaturaOPlanGunu, Depo, GunAy, OpsiyonGunu, AcikKapali, '+                 
                ' 0 as DovizBorc, '+                
    ' 0 as DovizAlacak, 0 as IslemNo3, '+           
    ' TP,  VardiyaKodu, VardiyaDurumu, Departman, 0 as MasrafToplami, KDVHD, IslemNo4, KayitDuzeyi, YazarKasaNo, YazarKasaFisNo, YaziciCount, IsIstasyonKodu, '+                 
                ' IsIstasyonAdi,0 as TevkifatTutari, VardiyaNo, OdemeBilgisi, FaturaAdresNo, SevkAdresNo, FI, eFaturaNo, UUID, eFaturaGonderimTarihi, eFaturaDurumu, EF_ISTISNA,   FaturaTipi,  '+          
        
                ' Entegrator,DovizKur from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo='+convert(varchar,@IslemNo)+' ';                    
                 
     
      
  print('/*2*/'+@sqlText);                    
 if @ExecuteCalistir>0              
  exec(@sqlText)        
      
  UPDATE STOKHARDETBAG SET IslemTuru =@IslemTuruSatis,FaturaTarihi = @FaturaTarihi, SevkTarihi = @FaturaTarihi, SiparisTarihi = @FaturaTarihi,YazarKasaFisNo = @YazarKasaFisNo,FaturaTipi = @FaturaTipi WHERE IslemNo = @YeniIslemNo;    
                
  set @sqlText='update STOKHARDETBAG set '+                
   ' Tutar=(select sum(Tutar) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ), '+                
   ' KdvTutari=(select sum(KdvTutari) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ), '+                
   ' DovizTut=(select sum(Tutar) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ), '+                
   ' DovizBorc=(select sum(DovizBorc) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ), '+                
   ' DovizAlacak=(select sum(DovizAlacak) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ), '+                
   ' DovizTutB=(select sum(Tutar) from STOKHARDETBAG where CariKodu = '+@CariKodu+' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ) '+                
   ' where CariKodu = '+@CariKodu+' and IslemNo='+convert(varchar,@YeniIslemNo)+' ';                   
                
  print('/*2.1*/'+@sqlText);                    
 if @ExecuteCalistir>0              
  exec(@sqlText)                    
 if @IslemTuruSatis = 'Alış'    
 SET @sqlText=' delete from ACARI where MUSKOD = '+@CariKodu+' and  ISLEMNO in ('+@SeciliIslemNolar+')  and ISLEMNO <> '+convert(varchar,@IslemNo)+'  and BAACIKLA = ''Alış'' '    
 else     
 SET @sqlText=' delete from ACARI where MUSKOD = '+@CariKodu+' and  ISLEMNO in ('+@SeciliIslemNolar+')  and ISLEMNO <> '+convert(varchar,@IslemNo)+'  and BAACIKLA in (''Prk.Satış'',''Satış'') '    
  
  print('/*3*/'+@sqlText);              
 if @ExecuteCalistir>0              
 exec(@sqlText)     
 if @IslemTuruSatis = 'Alış'    
 SET @sqlText=' update Odeme set IslemNo = '+convert(varchar,@YeniIslemNo)+' where CariKodu = '+@CariKodu+' and IslemNo in ('+@SeciliIslemNolar+')  and BAAcikla = ''Alış'' ';    
 else    
 SET @sqlText=' update Odeme set IslemNo = '+convert(varchar,@YeniIslemNo)+',BAAcikla = ''Satış''  where CariKodu = '+@CariKodu+' and IslemNo in ('+@SeciliIslemNolar+')  and BAAcikla = ''Prk.Satış'',''Satış'' ';    
 if @ExecuteCalistir>0              
 exec(@sqlText)     
  
if @IslemTuruSatis = 'Satış'     
 set @sqlText='update dbo.ACARI '+                    
                     ' SET ACIKLAMA='''+convert(varchar,@FaturaNo)+' no.lu Fatura'',BAACIKLA = ''Satış'', '+                    
                     ' dbo.ACARI.BORC = (select sum(ISNULL(dbo.STOKHARDETBAG.Tutar,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+                    
                     ' dbo.ACARI.DovizBorc = (select sum(ISNULL(dbo.STOKHARDETBAG.DovizBorc,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+                    
                     ' dbo.ACARI.DovizAlacak = (select sum(ISNULL(dbo.STOKHARDETBAG.DovizAlacak,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+                 
      ' dbo.ACARI.ISLEMNO = '+CONVERT(varchar,@YeniIslemNo)+                                  
                     ' where dbo.ACARI.ISLEMNO = '+convert(varchar,@IslemNo)+' and BAACIKLA in (''Prk.Satış'',''Satış'') '    
else         
 set @sqlText='update dbo.ACARI '+                    
                     ' SET ACIKLAMA='''+convert(varchar,@FaturaNo)+' no.lu Fatura'', '+                    
                     ' dbo.ACARI.BORC = (select sum(ISNULL(dbo.STOKHARDETBAG.Tutar,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+                    
                     ' dbo.ACARI.DovizBorc = (select sum(ISNULL(dbo.STOKHARDETBAG.DovizBorc,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+       
                     ' dbo.ACARI.DovizAlacak = (select sum(ISNULL(dbo.STOKHARDETBAG.DovizAlacak,0)) from dbo.STOKHARDETBAG where dbo.STOKHARDETBAG.IslemNo = '+CONVERT(varchar,@YeniIslemNo)+'), '+                 
      ' dbo.ACARI.ISLEMNO = '+CONVERT(varchar,@YeniIslemNo)+                                  
                     ' where dbo.ACARI.ISLEMNO = '+convert(varchar,@IslemNo)+' and BAACIKLA ''Alış'' ';          
print('/*4*/'+@sqlText);                    
 if @ExecuteCalistir>0              
 exec(@sqlText)                
    
if @IslemTuruSatis = 'Alış'     
 UPDATE dbo.ACARI SET TARIH = @FaturaTarihi where ISLEMNO = @YeniIslemNo and BAACIKLA = 'Alış'    
else    
 UPDATE dbo.ACARI SET TARIH = @FaturaTarihi where ISLEMNO = @YeniIslemNo and BAACIKLA = 'Satış';      
    
       
 if @geldigiYer <>'cariperakende'       
 begin      
 print('/*5*/'+@sqlText);       
 set @sqlText='Delete from STOKHARDETBAG  where CariKodu = '+@CariKodu+' and FisTuru = ''I'' and IslemNo in (SELECT Name FROM splitstring('''+@SeciliIslemNolar+''', '','')) ';      
 exec(@sqlText)       
 end      
                     
if @@ERROR<>0                    
begin                    
ROLLBACK                     
 if @ExecuteCalistir>0              
Select 'İşlem sırasında bir hata oluştu, işlem tamamlanamadı!'                    
else              
print('İşlem sırasında bir hata oluştu, işlem tamamlanamadı!' )              
end                    
ELSE begin                     
COMMIT               
 if @ExecuteCalistir>0              
Select 'İşlem başarıyla tamamlandı'                
else               
print('İşlem başarıyla tamamlandı')              
end    
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokKart_Aktar')
drop proc sp_StokKart_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE  PROCEDURE [dbo].[sp_StokKart_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@StokNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.STOKKARTI WHERE StokNo in (Select StokNo from '+@Kaynak+'.dbo.STOKKARTI where StokNo in ('+@StokNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[STOKKARTI] 
				 (	StokNo, Kodu, UreticiFirmaKodu, StokAdi, OzelKod1, OzelKod2, AnaBirim, Barkodu,AlisKdv, SatisKdv, Indirim, StokAdedi,F1Adi, F2Adi, F3Adi, F4Adi, F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat, F1Kdv, F2Kdv, F3Kdv, F4Kdv, 
 GrupAdi, UrtNo, UrtMiktar,AsgariAdet, AzamiAdet, OTVOrani, StokEn, StokBoy, StokYuksekligi, StokKilo, StokRenkKodu, KULNO, KULADI, TP, UygFiyat, 
 Aciklama, Departman, AlisIndirimYuzde, SatisIndirimYuzde, OTVTutari, DepartmanKodu, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu, Update_Date, TeraziTusu, YKasaTusu, Secim, AnaStokKodu, Ambalaj, 
 StokKiloBrut, StokTipi, StokOzellikKodu, AktifPasif, OzelBarkod, Olcu, AlisIndirimYuzde2, AlisIndirimYuzde3, AlisIndirimYuzde4, SatisIndirimYuzde2, SatisIndirimYuzde3, SatisIndirimYuzde4, MuadilStokNo, BonusPuani, 
 BonusPuaniSatici, OlmasiGerekenAdet, PrimYuzde, StokBedenNo, StokRenkBedenCalisir, WASG, WebGrupAdi, SKFIK, GrupOzelKod1, GrupOzelKod2, GrupOzelKod3, GrupOzelKod4, OrjinalKodu, StokAdi2, Aciklama2, 
 WebGrupAdi2, StokAgirlik, StokMaxAgirlik, StokMinAgirlik, AltGrupAdi, AltGrupAdi2, SeriNoTakibi, IRKI, SuniTohum, OzelKod3, OzelKod4, Icmal, SatisFiyatiDegistirilemez, A_Muh_Kodu, S_Muh_Kodu, FireOrani, 
 WebdeGosterilsinmi, TarimElTerminliGosterilsinmi, AltGrupAdi3, AdetTakibi, LotTakibi, UlkeKodu, TevkifatKodu)	
				SELECT	StokNo, Kodu, UreticiFirmaKodu, StokAdi, OzelKod1, OzelKod2, AnaBirim, Barkodu,AlisKdv, SatisKdv, Indirim, StokAdedi,F1Adi, F2Adi, F3Adi, F4Adi, F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat, F1Kdv, F2Kdv, F3Kdv, F4Kdv, 
 	GrupAdi, UrtNo, UrtMiktar,AsgariAdet, AzamiAdet, OTVOrani, StokEn, StokBoy, StokYuksekligi, StokKilo, StokRenkKodu, KULNO, KULADI, TP, UygFiyat, 
 	Aciklama, Departman, AlisIndirimYuzde, SatisIndirimYuzde, OTVTutari, DepartmanKodu, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu, Update_Date, TeraziTusu, YKasaTusu, Secim, AnaStokKodu, Ambalaj, 
 	StokKiloBrut, StokTipi, StokOzellikKodu, AktifPasif, OzelBarkod, Olcu, AlisIndirimYuzde2, AlisIndirimYuzde3, AlisIndirimYuzde4, SatisIndirimYuzde2, SatisIndirimYuzde3, SatisIndirimYuzde4, MuadilStokNo, BonusPuani, 
 	BonusPuaniSatici, OlmasiGerekenAdet, PrimYuzde, StokBedenNo, StokRenkBedenCalisir, WASG, WebGrupAdi, SKFIK, GrupOzelKod1, GrupOzelKod2, GrupOzelKod3, GrupOzelKod4, OrjinalKodu, StokAdi2, Aciklama2, 
 	WebGrupAdi2, StokAgirlik, StokMaxAgirlik, StokMinAgirlik, AltGrupAdi, AltGrupAdi2, SeriNoTakibi, IRKI, SuniTohum, OzelKod3, OzelKod4, Icmal, SatisFiyatiDegistirilemez, A_Muh_Kodu, S_Muh_Kodu, FireOrani, 
 	WebdeGosterilsinmi, TarimElTerminliGosterilsinmi, AltGrupAdi3, AdetTakibi, LotTakibi, UlkeKodu, TevkifatKodu
				FROM ' + @Kaynak + '.dbo.[STOKKARTI] S WHERE S.StokNo in ('+@StokNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------

if exists (select * from sys.procedures where name='sp_StokFiyat_Aktar')
drop proc sp_StokFiyat_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE   PROCEDURE [dbo].[sp_StokFiyat_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@StokNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.StokFiyat WHERE StokNo in (Select StokNo from '+@Kaynak+'.dbo.StokFiyat where StokNo in ('+@StokNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[StokFiyat] 
				 (	 Kodu, F1Adi, F2Adi, F3Adi, F4Adi, F5Adi, F6Adi, F7Adi, F8Adi, F9Adi, F10Adi, F11Adi, F12Adi, F13Adi, F14Adi, F15Adi, F16Adi, F17Adi, F18Adi, F19Adi, F20Adi, F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat, F5Fiyat, F6Fiyat, F7Fiyat, F8Fiyat, 
                         F9Fiyat, F10Fiyat, F11Fiyat, F12Fiyat, F13Fiyat, F14Fiyat, F15Fiyat, F16Fiyat, F17Fiyat, F18Fiyat, F19Fiyat, F20Fiyat, F1Kdv, F2Kdv, F3Kdv, F4Kdv, F5Kdv, F6Kdv, F7Kdv, F8Kdv, F9Kdv, F10Kdv, F11Kdv, F12Kdv, F13Kdv, 
                         F14Kdv, F15Kdv, F16Kdv, F17Kdv, F18Kdv, F19Kdv, F20Kdv, KULNO, KULADI, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu, F5DovizKodu, F6DovizKodu, F7DovizKodu, F8DovizKodu, F9DovizKodu, F10DovizKodu, 
                         F11DovizKodu, F12DovizKodu, F13DovizKodu, F14DovizKodu, F15DovizKodu, F16DovizKodu, F17DovizKodu, F18DovizKodu, F19DovizKodu, F20DovizKodu, F1FiyatH, F2FiyatH, F3FiyatH, F4FiyatH, F5FiyatH, F6FiyatH, 
                         F7FiyatH, F8FiyatH, F9FiyatH, F10FiyatH, F11FiyatH, F12FiyatH, F13FiyatH, F14FiyatH, F15FiyatH, F16FiyatH, F17FiyatH, F18FiyatH, F19FiyatH, F20FiyatH, SatisKdv, IslemNo, DF, Update_Date, StokNo)	
				SELECT	 Kodu, F1Adi, F2Adi, F3Adi, F4Adi, F5Adi, F6Adi, F7Adi, F8Adi, F9Adi, F10Adi, F11Adi, F12Adi, F13Adi, F14Adi, F15Adi, F16Adi, F17Adi, F18Adi, F19Adi, F20Adi, F1Fiyat, F2Fiyat, F3Fiyat, F4Fiyat, F5Fiyat, F6Fiyat, F7Fiyat, F8Fiyat, 
                         F9Fiyat, F10Fiyat, F11Fiyat, F12Fiyat, F13Fiyat, F14Fiyat, F15Fiyat, F16Fiyat, F17Fiyat, F18Fiyat, F19Fiyat, F20Fiyat, F1Kdv, F2Kdv, F3Kdv, F4Kdv, F5Kdv, F6Kdv, F7Kdv, F8Kdv, F9Kdv, F10Kdv, F11Kdv, F12Kdv, F13Kdv, 
                         F14Kdv, F15Kdv, F16Kdv, F17Kdv, F18Kdv, F19Kdv, F20Kdv, KULNO, KULADI, F1DovizKodu, F2DovizKodu, F3DovizKodu, F4DovizKodu, F5DovizKodu, F6DovizKodu, F7DovizKodu, F8DovizKodu, F9DovizKodu, F10DovizKodu, 
                         F11DovizKodu, F12DovizKodu, F13DovizKodu, F14DovizKodu, F15DovizKodu, F16DovizKodu, F17DovizKodu, F18DovizKodu, F19DovizKodu, F20DovizKodu, F1FiyatH, F2FiyatH, F3FiyatH, F4FiyatH, F5FiyatH, F6FiyatH, 
                         F7FiyatH, F8FiyatH, F9FiyatH, F10FiyatH, F11FiyatH, F12FiyatH, F13FiyatH, F14FiyatH, F15FiyatH, F16FiyatH, F17FiyatH, F18FiyatH, F19FiyatH, F20FiyatH, SatisKdv, IslemNo, DF, Update_Date, StokNo
				FROM ' + @Kaynak + '.dbo.[StokFiyat] S WHERE S.StokNo in ('+@StokNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokBarkodlari_Aktar')
drop proc sp_StokBarkodlari_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------

CREATE   PROCEDURE [dbo].[sp_StokBarkodlari_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@StokNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.StokBarkodlari WHERE StokNo in (Select StokNo from '+@Kaynak+'.dbo.StokBarkodlari where StokNo in ('+@StokNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[StokBarkodlari] 
				 (StokKodu, Barkodu, KulNo, Carpani, UpdateDate, StokNo,AnaBirim,Birim)			
				 SELECT	 StokKodu, Barkodu, KulNo, Carpani, UpdateDate, StokNo,AnaBirim,Birim
				 FROM ' + @Kaynak + '.dbo.[StokBarkodlari] S WHERE S.StokNo in ('+@StokNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   	   
    
		 
      				   

 				   
 
 
				   
 	 
 

   
	 
  	   
  		
  			 
	
		 
      				   

if exists (select * from sys.procedures where name='sp_AMUSTERI_Aktar')
drop proc sp_AMUSTERI_Aktar

go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[sp_AMUSTERI_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@MusNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.AMUSTERI WHERE MUSNO in (Select MUSNO from '+@Kaynak+'.dbo.AMUSTERI where MUSNO in ('+@MusNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[AMUSTERI] 
				 (	MUSNO,MUSKOD, MUSADI, YETKILI, OZELKOD1, OZELKOD2, OZELKOD3, OZELKOD4, MUSVERGI, MUSVERNO, MUSADRES1, MUSADRES2, MUSTEL, MUSTEL2, MUSTEL3, MUSFAX, MUSCEP, MUSACIKLA1, MUSACIKLA2, 
 MUSACIKLA3, MUSACIKLA4, MUSBORC, MUSALACAK, MUSBAKIYE, SIGORTA, PLAKA, MUSCIN, MUSDURUM, YUZDE, MUSIL, MUSILCE, ISKONTO, RISK, SONEXTTAR, KULNO, KULADI, Tipi, FaturaOPlanGunu, GunAy, 
 OpsiyonGunu, ParaLitre, Departman, Secim, FiyatGrupNo, MUSCEP2, FiyatGrupAdi, Iskonto1, Iskonto2, Iskonto3, StokKodu, StokAdi, FatKes, TaksitTutari, SaticiKodu, SaticiAdi, Kilitli, Odeme, Depo, Bagkur, BagkurNo, VergiTipi, 
 VergiMukellefi, KomisyonYuzdesi, BagkurKesenegi, SSKNo, BorsaSicilNo, BelgeNo, UstCariNo, FaturaAdresNo, SevkAdresNo, web_kullanici_adi, web_kullanici_sifre, SektorGrubu, CariGrubu, BolgeGrubu, PuanCarpani, 
 AnaRutAdi, Gorunum, AcilisTarihi, SMSTelefonu, SMSGonder, UserType, Cinsiyeti, Uyrugu, AcentaAnlasmaNo, AcentaAnlasmaDeger, TCKimlikNo, Rengi, PasaportSeriNo, PasaportNo, Meslek, SKFIK, KimlikTuru, UpdateDate, 
 CGTipi, Mah_Koyu, A_Muh_Kodu, S_Muh_Kodu, TaksitCekSenetBakiye, DSYBTalepteFaturaKesilmesin, cbKampanyaCariAlan)	
				SELECT	MUSNO,MUSKOD, MUSADI, YETKILI, OZELKOD1, OZELKOD2, OZELKOD3, OZELKOD4, MUSVERGI, MUSVERNO, MUSADRES1, MUSADRES2, MUSTEL, MUSTEL2, MUSTEL3, MUSFAX, MUSCEP, MUSACIKLA1, MUSACIKLA2, 
 	MUSACIKLA3, MUSACIKLA4, MUSBORC, MUSALACAK, MUSBAKIYE, SIGORTA, PLAKA, MUSCIN, MUSDURUM, YUZDE, MUSIL, MUSILCE, ISKONTO, RISK, SONEXTTAR, KULNO, KULADI, Tipi, FaturaOPlanGunu, GunAy, 
 	OpsiyonGunu, ParaLitre, Departman, Secim, FiyatGrupNo, MUSCEP2, FiyatGrupAdi, Iskonto1, Iskonto2, Iskonto3, StokKodu, StokAdi, FatKes, TaksitTutari, SaticiKodu, SaticiAdi, Kilitli, Odeme, Depo, Bagkur, BagkurNo, VergiTipi, 
 	VergiMukellefi, KomisyonYuzdesi, BagkurKesenegi, SSKNo, BorsaSicilNo, BelgeNo, UstCariNo, FaturaAdresNo, SevkAdresNo, web_kullanici_adi, web_kullanici_sifre, SektorGrubu, CariGrubu, BolgeGrubu, PuanCarpani, 
 	AnaRutAdi, Gorunum, AcilisTarihi, SMSTelefonu, SMSGonder, UserType, Cinsiyeti, Uyrugu, AcentaAnlasmaNo, AcentaAnlasmaDeger, TCKimlikNo, Rengi, PasaportSeriNo, PasaportNo, Meslek, SKFIK, KimlikTuru, UpdateDate, 
 	CGTipi, Mah_Koyu, A_Muh_Kodu, S_Muh_Kodu, TaksitCekSenetBakiye, DSYBTalepteFaturaKesilmesin, cbKampanyaCariAlan
				FROM ' + @Kaynak + '.dbo.[AMUSTERI] S WHERE S.MUSNO in ('+@MusNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------

if exists (select * from sys.procedures where name='sp_AMusteri1_Aktar')
drop proc sp_AMusteri1_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[sp_AMusteri1_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@CariNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.AMusteri1 WHERE CariNo in (Select CariNo from '+@Kaynak+'.dbo.AMusteri1 where CariNo in ('+@CariNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[AMusteri1] 
 	 (	CariNo,Adres_1, Adres2_1, AdresIl_1, AdresIlce_1, Adres_2, Adres2_2, AdresIl_2, AdresIlce_2, WebAdresi_1, WebAdresi_2, email_1, email_2, email_3, Banka_1, BankaSube_1, BankaHesapNo_1, Banka_2, BankaSube_2, 
 		BankaHesapNo_2, Banka_3, BankaSube_3, BankaHesapNo_3, Banka_4, BankaSube_4, BankaHesapNo_4, Istihbarat, KulNo, KulAdi, KrediKartiSahibi, KrediKartiBankasi, KrediKartiTipi, KrediKartiNo, SonKullanimTarihi, 
 		GuvenlikKodu, Aciklama, Istihbarat1, Istihbarat2, Istihbarat3, Istihbarat4, Istihbarat5, Istihbarat6, Istihbarat7, Istihbarah8, Istihbarat9, Istihbarat10, OzelTarih1, OzelTarih2, OzelTarih3, OzelTarih4, Bilgi1, Bilgi2, Bilgi3, Bilgi4, 
 		Bilgi5, Bilgi6, Bilgi7, Bilgi8, Bilgi9, Bilgi10, Bilgi11, Bilgi12, Bilgi13, Bilgi14, Bilgi15, Bilgi16, Bilgi17, Bilgi18, Bilgi19, Bilgi20, KrediKartiSahibi2, KrediKartiBankasi2, KrediKArtiTipi2, KrediKartiNo2, SonKullanimTarihi2, 
 		GuvenlikKodu2, Aciklama2, TCKimlikNo, EhliyetNo, EhliyetinVerildigiYer, EhliyetinVerildigiTarih, EhliyetSinifi, KimlikSeriNo, KimlikNo, KimlikAdi, KimlikSoyAdi, KimlikBabaAdi, KimlikAnaAdi, KimlikDogumYeri, 
 		KimlikDogumTarihi, KimlikMedeniHali, KimlikDini, KimlikKanGrubu, KimlikIli, KimlikIlce, KimlikMahalleKoy, KimlikCiltNo, KimlikAileSiraNo, KimlikSiraNo, KimlikVerildigiYer, KimlikVerilisNedeni, KimlikKayitNo, KimlikVerilisTarihi,KimlikOncekiSoyAdi)
				SELECT	CariNo,Adres_1, Adres2_1, AdresIl_1, AdresIlce_1, Adres_2, Adres2_2, AdresIl_2, AdresIlce_2, WebAdresi_1, WebAdresi_2, email_1, email_2, email_3, Banka_1, BankaSube_1, BankaHesapNo_1, Banka_2, BankaSube_2, 
 	BankaHesapNo_2, Banka_3, BankaSube_3, BankaHesapNo_3, Banka_4, BankaSube_4, BankaHesapNo_4, Istihbarat, KulNo, KulAdi, KrediKartiSahibi, KrediKartiBankasi, KrediKartiTipi, KrediKartiNo, SonKullanimTarihi, 
 	GuvenlikKodu, Aciklama, Istihbarat1, Istihbarat2, Istihbarat3, Istihbarat4, Istihbarat5, Istihbarat6, Istihbarat7, Istihbarah8, Istihbarat9, Istihbarat10, OzelTarih1, OzelTarih2, OzelTarih3, OzelTarih4, Bilgi1, Bilgi2, Bilgi3, Bilgi4, 
 	Bilgi5, Bilgi6, Bilgi7, Bilgi8, Bilgi9, Bilgi10, Bilgi11, Bilgi12, Bilgi13, Bilgi14, Bilgi15, Bilgi16, Bilgi17, Bilgi18, Bilgi19, Bilgi20, KrediKartiSahibi2, KrediKartiBankasi2, KrediKArtiTipi2, KrediKartiNo2, SonKullanimTarihi2, 
 	GuvenlikKodu2, Aciklama2, TCKimlikNo, EhliyetNo, EhliyetinVerildigiYer, EhliyetinVerildigiTarih, EhliyetSinifi, KimlikSeriNo, KimlikNo, KimlikAdi, KimlikSoyAdi, KimlikBabaAdi, KimlikAnaAdi, KimlikDogumYeri, 
 	KimlikDogumTarihi, KimlikMedeniHali, KimlikDini, KimlikKanGrubu, KimlikIli, KimlikIlce, KimlikMahalleKoy, KimlikCiltNo, KimlikAileSiraNo, KimlikSiraNo, KimlikVerildigiYer, KimlikVerilisNedeni, KimlikKayitNo, KimlikVerilisTarihi,KimlikOncekiSoyAdi				
 	FROM ' + @Kaynak + '.dbo.[AMusteri1] S WHERE S.CariNo in ('+@CariNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_Adres_Aktar')
drop proc sp_Adres_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[sp_Adres_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@CariNo     nvarchar(max)
)

AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query nvarchar(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.Adres WHERE CariNo in (Select CariNo from '+@Kaynak+'.dbo.Adres where CariNo in ('+@CariNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[Adres] 
 	(CariNo, CariKodu, CariAdi, AdresNo, Adres1, Adres2, PostaKodu, Ilce, Il, Ulke, TelUlkeKodu, TelBolgeKodu, Tel1, Tel2, Tel3, Faks, Aciklama, KulNo, KulAdi, VergiDairesi, VergiNo, Enlem, Boylam, EDerece, EDakika, ESaniye, 
 	 ESalise, BDerece, BDakika, BSaniye, BSalise, Barkodu, AnaRutAdi, BolgeGrubu, Mahalle, SokakAdi, BinaAdi, BinaNo, KapiNo, UlkeKodu, eFaturaMukellefimi, eFaturaGuncellemeTarihi, Alias, SMSTelefonu)			
				 SELECT	CariNo, CariKodu, CariAdi, AdresNo, Adres1, Adres2, PostaKodu, Ilce, Il, Ulke, TelUlkeKodu, TelBolgeKodu, Tel1, Tel2, Tel3, Faks, Aciklama, KulNo, KulAdi, VergiDairesi, VergiNo, Enlem, Boylam, EDerece, EDakika, ESaniye, 
 	ESalise, BDerece, BDakika, BSaniye, BSalise, Barkodu, AnaRutAdi, BolgeGrubu, Mahalle, SokakAdi, BinaAdi, BinaNo, KapiNo, UlkeKodu, eFaturaMukellefimi, eFaturaGuncellemeTarihi, Alias, SMSTelefonu
				 FROM ' + @Kaynak + '.dbo.[Adres] S WHERE S.CariNo in ('+@CariNo+')';
 	
   EXEC sp_executesql @Query;
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------

if exists (select * from sys.procedures where name='sp_Cari_Aktar')
drop proc sp_Cari_Aktar
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[sp_Cari_Aktar]
(
    @Hedef		SYSNAME,
	@Kaynak  	SYSNAME,
	@MusNo    nvarchar(max)
)

AS 
BEGIN
ALTER TABLE STOKKARTI NOCHECK CONSTRAINT ALL


	exec sp_AMUSTERI_Aktar @Hedef,@Kaynak,@MusNo;
	exec sp_AMusteri1_Aktar @Hedef,@Kaynak,@MusNo;
	exec sp_Adres_Aktar @Hedef,@Kaynak,@MusNo;

ALTER TABLE STOKKARTI WITH CHECK CHECK CONSTRAINT ALL
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   IF EXISTS (SELECT *  FROM sys.views WHERE  name='StokBarkodlari_Unique')
 BEGIN 
 drop view StokBarkodlari_Unique
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   create  view StokBarkodlari_Unique as
 select sb.*,s.SAYI from StokBarkodlari sb, ( SELECT Barkodu ,COUNT(*) SAYI,StokKodu  FROM StokBarkodlari 
 GROUP BY Barkodu,StokKodu HAVING COUNT(*)>1) s 
 where sb.Barkodu=s.Barkodu and 
 sb.StokBarkodlariNoId  not in  (SELECT max(s3.StokBarkodlariNoId) FROM StokBarkodlari s3   group by s3.Barkodu)
 and sb.StokBarkodlariNoId not in (SELECT StokBarkodlariNoId FROM StokBarkodlari where AnaBirim=1)
 --order by SAYI desc , sb.Barkodu
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  INSERT INTO  StokBarkodlari (StokKodu,Barkodu,KulNo,Carpani,UpdateDate,StokNo,AnaBirim,Birim)
Select Kodu,Barkodu,1,1,GETDATE(),StokNo,1,sk.AnaBirim from STOKKARTI sk
where NOT EXISTS (Select 1 from  StokBarkodlari sb where sk.Kodu = sb.StokKodu)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  UPDATE sb SET sb.StokNo = sk.StokNo from STOKKARTI sk,StokBarkodlari sb
where sk.Kodu = sb.StokKodu and sk.StokNo <> sb.StokNo
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   delete from StokBarkodlari where StokBarkodlariNoId in (
  select sb.StokBarkodlariNoId from StokBarkodlari sb, ( SELECT Barkodu ,COUNT(*) SAYI,StokKodu  FROM StokBarkodlari GROUP BY Barkodu,StokKodu HAVING COUNT(*)>1) s 
 where sb.Barkodu=s.Barkodu and 
 sb.StokBarkodlariNoId  not in  (SELECT max(s3.StokBarkodlariNoId) FROM StokBarkodlari s3   group by s3.Barkodu)
 and sb.StokBarkodlariNoId not in (SELECT StokBarkodlariNoId FROM StokBarkodlari where AnaBirim=1))
go--
-------------------------------------------------------------------------------------------------------------------------------------------
	
 		  
		 
      				   
   
       		 
      				  
		  
		
 		 
  
 				
 				
 				
		
  				   
   	  
		  
   
 	  
		
  
		  
    		 
 				 
			
   		  
				 
		  
			  
				 
			
	 
				 
		
         	   
	  
	   
  			   
			
      			   
		
     
   		  
		
     
   		  
		
		
 		
     		 
		  
		   
			
      	 
		
     
   		  
		
     
   		  
		
		
 		
       				 
		   
	
                
        	
	   
			  
 	 
 		
				  
  		
   			
 	   
		   
  			
      			
    			 
		
				  
 			   
		  
  
			 
      				
		  
	  
   	
    	   
    	 
    	 
    	
   
 			 
  				  
	  
 	 
 	  
 	
		 
		   
 		  
     	 
			  
    
 		  
		   
		
		 
		
 			   
   
 			
				 
		 
      				   


   				 
 	   
		 
      				   

  	  
 		  
 		   
 		  
 	  
 		
 		
 		
 			 
 			 
 			   
 		  
 			
				   
 		 
 			
 		  
			 
  
			  

	 
	 
  
 		   

				   
	   
 	 
	  
   		   
	
	 
	  
   		
	 

  
				
			   
				  
 
 
 
			 
				 
				 
   
				  
			 
			  
				  
				
  
				 
	  

	
		 
      				   
    	   
     
		 
      				   
 
     
	
   
     		
        
         
   	   
         				  
 	   
         				 
       		 
 
 	   
      
  		
	
			
    
 
				   
 			   
			   
				 
  
 				
  			   
 		  
      
   	 
   	   
  	
   	 
 				 
  	
 		
 				  
      
  	  
	  
			
			 
	   
				  
 		
 			
   	 
   	 
 		 
    			   
    		  
			  
				   
  		   
  		   
    
    
 				  
   
   				  
        
   
    	  
      
    		
    		
     		 
	  
   
   				   
   
  		
 	
		  
			   
				   
				  
				 
			  
				
    
 	   
    
			   
		 
      				   

    		
  	
		 
      				   
   	   
 				   
 		   
 
  
 			   
 	  
 		  
  
  
		 
				
		  
 
  			  
 				   
   		   
    	 
        
     		   
 	  
 	 
  
    
		 
      				   
   		   
 	   
		 
      				   
 			   
  
 
     				  
    		   
     	
    		
		 
      				   
   		   
 	   
		 
      				   
  	
 		
 		 
 			
 		 
		
 	  
    			   
     				   
   		 
   			  
   			
    
     
     	  
      
    				  
     
    		
    		 
    			 
     		  
     		  
    		   
    	   
    			
    	   
   			
    	 
    		   
   			  
     				  
       
    	  
    			
     	  
     	
    	  
    		
     	 
       
     	   
    		 
    		   
    				   
       
    	 
     	  
     	
    				  
    	
    	   
       
    				
    				 
    	  
   		   
    	 
    		
     		
     	 
   				 
    


		
		 
      				   
   			  
 		  
		 
      				   
    
	  
		 
 				 
    
   				  
	 
				
		  
   		  
  
 		   
		   
   	
		
  
     	 
	 
 	
		   
    			
			 
     		 
		   
			
			 
    				
   		  
			
		   
		
  
		  
				
 				  
 			   
 	  
 		  
 			  
 		
 				 
		 
		
		  
         

   				 
	   
 			   
	
		 
         
  
     			 
    				   
        
     				
     				  
     				  
     				   
   
  
 	 
 	
		 
         
     	 
   		 
   			   
    
      	   
         
       	   
       				
       		   
       	  
	  
   
   				  
   	   
	
			  
		  
       
   declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME= name  FROM sys.objects where type_desc like '%FUNCT%' and name like '%fnFaturaTipiGetir%'
--name from sys.default_constraints where name like 'DF_STOKHARDETevki%'
SET @SQL='DROP FUNCTION [dbo].['+CONVERT(VARCHAR(1000),@CNTNAME)+']'
EXEC(@SQL)
go--
------------------------------------------------------------------------------------------------------------------------------------------- 
CREATE FUNCTION [dbo].[fnFaturaTipiGetir]   
(  
 @FaturaTipi int  
)  
RETURNS nvarchar(20)  
AS  
BEGIN   
 declare @Aciklama nvarchar(20) 
set @Aciklama= '';  
  
 IF @FaturaTipi = 1  
   SET @Aciklama  = 'TEVKIFAT'  
 ELSE IF @FaturaTipi = 2  
   SET @Aciklama  = 'IADE'  
 ELSE IF @FaturaTipi = 3  
   SET @Aciklama  = 'IHRACKAYITLI'  
 ELSE IF @FaturaTipi = 4  
   SET @Aciklama  = 'ISTISNA'  
 ELSE IF @FaturaTipi = 5   
   SET @Aciklama  = 'MOBIL'  
 ELSE IF @FaturaTipi = 6  
   SET @Aciklama  = 'KAMU'  
 ELSE   
   SET @Aciklama  = 'SATIS'  
    
  
 RETURN @Aciklama  
END;  

		  
       
   		  
go--
------------------------------------------------------------------------------------------------------------------------------------------- 
if exists (select * from sys.procedures where name='sp_StokKart_Bakim')
drop proc sp_StokKart_Bakim
go--
------------------------------------------------------------------------------------------------------------------------------------------- 
CREATE  PROCEDURE sp_StokKart_Bakim
AS
 
DELETE FROM StokVergiMuafiyetleri WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokVergiMuafiyetleri.StokKodu)
DELETE FROM StokFiyat  WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokFiyat.Kodu)
DELETE FROM StokBarkodlari WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokBarkodlari.StokKodu)
DELETE FROM StokDB WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokDB.StokKodu)
UPDATE STOKKARTI SET Barkodu=Kodu where Barkodu='' or Barkodu is null 
		 
      				   
   		   
 	   
		 
      				   
  	
 		
 		 
 			
 		 
		
 	  
    			   
     				   
   		 
   			  
   			
    
     
     	  
      
    				  
     
    		
    		 
    			 
     		  
     		  
    		   
    	   
    			
    	   
   			
    	 
    		   
   			  
     				  
       
    	  
    			
     	  
     	
    	  
    		
     	 
       
     	   
    		 
    		   
    				   
       
    	 
     	  
     	
    				  
    	
    	   
       
    				
    				 
    	  
   		   
    	 
    		
     		
     	 
   				 
    


		
		 
      				   

   			  
 		  
		 
      				   
    
	  
		 

   				  
	 
				
		  
   		  
  
 		   
		   
   	
		
  
     	 
	 
 	
		   
    			
			 
     		 
		   
			
			 
    				
   		  
			
		   
		
  
		  
				
 				  
 			   
 	  
 		  
 			  
 		
 				 
		 
  				  
   		
 
		
		 
      				   

   				   
 			   
		 
      				   
    		  
    
   
  				  
  			 
   
      
      
  
  
 				  
  
 	   
 	  
  
		  
   	
  			  
   	  
 				   
 				 
    
    
 		 
 		 
		
       
   				   
 			 
  		   
       
      
    
    	 
    	 
  
				  
	   
				
	   
   
   		   
	   
  			  
  
  			
     	
  			
        
  			
     	
  		   
       
  		   
    			  
  			
    				  
  		   
    				
  		   
   				   
  		   
            
     				 
        
      		
 				  
   			   

   
  
			  
      
 				
  				  
  				   
  			
   
  			
  				  
  			
   
  		   
  				 
  		   
  		  
  		   
   
  			
 				   
  			
  
			   
  			   
   			 
  			
           	
      
      
   				   
     			 
        		 
 			
    	 
			  
 		  
    				
			  
  		   
       
      
      
    	
   				 
  
				  
 	
				
	   
   
   		   
	   
  			  
    
  			
     	
  			
        
  			
     	
  		   
       
  		   
    			  
  			
    				  
  		   
    				
  		   
   		  
  		   
            
     				 
        
      		 
 		  
				 
      		 
 				  
  				  
     
  			
   	
  			
     
  			
  		 
  		   
    
  		   
   	
  		   
  	   
  		
  	 
  		   
  		   
			 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
UPDATE sb SET sb.StokNo = sk.StokNo from STOKKARTI sk,StokBarkodlari sb where sk.Kodu = sb.StokKodu 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
UPDATE sb SET sb.StokNo = sk.StokNo from STOKKARTI sk,StokFiyat sb where sk.Kodu = sb.Kodu 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
INSERT INTO  StokBarkodlari (StokKodu,Barkodu,KulNo,Carpani,UpdateDate,StokNo,AnaBirim,Birim)
Select Kodu,Barkodu,1,1,GETDATE(),StokNo,1,sk.AnaBirim from STOKKARTI sk
where NOT EXISTS (Select 1 from  StokBarkodlari sb where sk.Kodu = sb.StokKodu or sk.Barkodu = sb.Barkodu)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
IF  EXISTS (SELECT *  FROM sys.triggers WHERE  name='TR_ST_HAREKET_KONTROL')
 BEGIN 
 drop trigger TR_ST_HAREKET_KONTROL
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  TRIGGER [dbo].[TR_ST_HAREKET_KONTROL] ON [dbo].[STOKKARTI] AFTER DELETE    
AS    
BEGIN    
	DECLARE @Kodu varchar (50)    
	SELECT @Kodu= Kodu FROM DELETED    

	--Print 'Ürün Stok Hareketlede mevcuttur, silinenmez'    
	--ROLLBACK TRANSACTION    

	IF EXISTS(SELECT * FROM STOKHAR WHERE StokKodu=@Kodu)  
	BEGIN    
		Print 'Ürün Stok Hareketlede mevcuttur, silinenmez'    
		ROLLBACK TRANSACTION    
		RETURN    
		END    
		else IF EXISTS(SELECT * FROM SIPARISHAR WHERE StokKodu=@Kodu)  
		BEGIN    
		Print 'Ürün Sipari? Hareketerinde mevcuttur, silinenmez'    
		ROLLBACK TRANSACTION    
		RETURN    
		END else IF EXISTS(SELECT * FROM TEKLIFHAR WHERE StokKodu=@Kodu)  
		BEGIN    
		Print 'Ürün Teklif Hareketerinde mevcuttur, silinenmez'    
		ROLLBACK TRANSACTION    
		RETURN    
		END else IF EXISTS(SELECT * FROM RenkBedenHareket WHERE StokKodu=@Kodu)  
		BEGIN    
		Print 'Ürün Renk Beden Hareketerinde mevcuttur, silinenmez'    
		ROLLBACK TRANSACTION    
		RETURN     
		END else IF EXISTS(SELECT * FROM UrtRecetesi WHERE StkMsrfKodu=@Kodu)  
		BEGIN    
		Print 'Ürün Üretim Reçetelerinde mevcuttur, silinenmez'    
		ROLLBACK TRANSACTION    
		RETURN    
	END      
	ELSE 
	BEGIN   
		DELETE FROM StokFiyat where Kodu=@Kodu    
		DELETE from StokDB where StokKodu=@Kodu   
		DELETE from RenkBedenTablosu where StokKodu = @Kodu  
		DELETE from StokFiyatLog where Kodu = @Kodu  
		DELETE from StokPaketleri where StokKodu = @Kodu  
		DELETE from MuadilStoklar where MuadilStokKodu = @Kodu  
		DELETE from StokBarkodlari where StokKodu = @Kodu  
	END  
END
go--
-------------------------------------------------------------------------------------------------------------------------------------------
declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.key_constraints where name='UK_Barkodu'
SET @SQL='ALTER TABLE [dbo].[StokBarkodlari] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC (@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
    IF not EXISTS (SELECT *  FROM sys.key_constraints WHERE  name='UQ_Barkodu')
 BEGIN 
ALTER TABLE StokBarkodlari ADD CONSTRAINT UQ_Barkodu UNIQUE(Barkodu)
 end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.key_constraints where name='UQ_VergiMuaf_Kodu'
SET @SQL='ALTER TABLE [dbo].[VergiMuafiyetleri] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC (@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   IF not EXISTS (SELECT *  FROM sys.key_constraints WHERE  name='UQ_VergiMuaf_Kodu')
 BEGIN 
ALTER TABLE VergiMuafiyetleri ADD constraint UQ_VergiMuaf_Kodu UNIQUE (Kod)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like 'DF__ACARI__Update_Da%'
SET @SQL='ALTER TABLE [dbo].[ACARI] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC(@SQL)
		 
      				   
   				 
 			 
		 
      				   
  				   
 		  
 		  
 		
    
 		  
 			 
 			 
   
 	  
 	
 				   
 	
 	   
		  
 			 
 	
 			 
   
 				   
 	 
  	
 		   
				
		 
        				   

 			  
   			

 			
    	

   
  			  
 			  
    
  			   
   			   
	
    
             	   
  
		 
    
            
    
   			 
		
				  
			 
				
	   
    
   			
		
  			   
	   
  
    
   			   
    
   			   
    
     
    
   			
    
   			  
			 
 		
 
 		   
 				
		 
 				 
		 
   
 		 
   
 		 
   
 		  
   
 			   
   
 	   
   
 		 
		 
  		 
  		
      
      			 

 			  
   			

 			
    	

   
  			  
 			  
    
  			   
    
		
    
             	   
  
	
    
           
    
   			
		
				  
	   
				 
		
    
   			
		
  			   
		  
    
   			   
    
   			   
      
    
   			
    
   			  
			 
     
		 
   
 			 
   
 			 
   
 			  
   
 				   
   
 		   
   
 			 
   
 			   
			
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_FaturaIslemleriListesi')
drop proc sp_FaturaIslemleriListesi
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE   PROCEDURE [dbo].[sp_FaturaIslemleriListesi]                                        
  @tarih1 varchar(20),                                         
  @tarih2 varchar(20),                                        
  @dep varchar(500),                                        
  @orderby int,                                        
  @arama varchar(100),                                        
  @GosterilecekKayit int,                                        
  @IslemTuru varchar(20),                                        
  @Durumu int,                            
  @IslemTipi  varchar(30),                
  @FatSerNo  varchar(3),                
  @eFaturaNo  varchar(30),                
  @FaturaTuru varchar(1),    
  @IslemNoLike bit = 0      
AS                                        
            
declare @str nvarchar(max)                                        
declare @rc int                                         
declare @tmp table (trc int)                                        
declare @ayrac varchar(2)                
declare @peFaturaNo varchar(50)        
if @eFaturaNo<>''        
select @peFaturaNo=UPPER(@eFaturaNo)        
else set @peFaturaNo=''        
set @ayrac='"';                
          
set @str='select count(*) from (select IslemNo from STOKHARDETBAG where FisTuru = ''F'' AND FaturaTarihi >='''+ @Tarih1+                      
''' and FaturaTarihi <= '''+@Tarih2+'''';     

if @peFaturaNo<>''                
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '   

if @FatSerNo<>''                
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '  
                                   
if @dep <> ''                                        
 set @str=@str+' and Departman in ('+@Dep+')'                                        
if @IslemTuru <> 'Tümü'                                        
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                              
if @IslemTipi <> 'Tümü' and   @IslemTipi <> ''                                       
 set @str=@str+' and IslemTipi like ''%'+@IslemTipi+'%'''                              
    
if @arama <> ''     
begin    
if @orderby=0     
begin    
 if @IslemNoLike = 0    
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '     
 else    
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '     
end;                                      
if @orderby=1                                         
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                         
if @orderby=2                                         
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                        
if @orderby=3                                         
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                         
if @orderby=4                                        
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                        
if @orderby=5                                        
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                             
 if @orderby=6                                        
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                             
if @orderby=7                                        
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                             
if @orderby=8                                        
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '           
if @orderby=9                                        
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))   
      or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))  
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))  
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '     
end;                                    
set @str=@str+')Y';                                                          
  
insert @tmp exec(@str)                                        
select @rc = trc from @tmp;               
                
declare @tstr varchar(100);                                      
                                      
if @orderby in (0,9)                                        
  set @tstr='IslemNo'                                        
if @orderby=1                                         
 set @tstr='FaturaNo'                                         
if @orderby=2                                         
 set @tstr='CariAdi'                                        
if @orderby=3                                         
 set @tstr='CariKodu'                                         
if @orderby=4                                        
 set @tstr='KULADI'                                        
if @orderby=5                                        
 set @tstr='SaticiAdi'                              
if @orderby=6                                        
 set @tstr='eFaturaDurumu'                 
 if @orderby=7                                        
 set @tstr='YaziciCount'                 
 if @orderby=8                                        
 set @tstr='FaturaTarihi'                 
                 
set @str='select '''' as plaka, *, '+                    
   'CASE YazarKasaFisNo when 0 then ''TICARIFATURA'' '+                    
   'when 1 then ''TEMELFATURA'' '+                    
   'when 2 then ''IHRACAT'' else ''TICARIFATURA'' end + '' / ''+ case FaturaTipi  when 0 then ''SATIS'' when 1 then ''TEVKIFAT'' when 2 then ''IADE'' when 3 then ''IHRACKAYITLI'' when 4 then ''ISTISNA'' '+                     
   'when 5 then ''MOBIL'' else ''SATIS'' end FaturaSenaryoTip '+                    
   'from STOKHARDETBAG '+                                        
   'where IslemNo in (select X.IslemNo '+                                        
   'from (select ROW_NUMBER() over (order by '+@tstr+') as sira, IslemNo '+                                        
   'from STOKHARDETBAG where FisTuru = '''+@FaturaTuru+''' and FaturaTarihi >='''+@Tarih1+''' and FaturaTarihi <= '''+@Tarih2+'''';                                        
if @peFaturaNo<>''                
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '                
                
if @FatSerNo<>''                
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '                
                
if @dep <> ''                                        
 set @str=@str+' and Departman in ('+@Dep+')'                                        
if @IslemTuru <> 'Tümü'                                        
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                                
if @IslemTipi <> 'Tümü' and @IslemTipi <> ''                                        
 set @str=@str+' and IslemTipi = '''+@IslemTipi+''''                           
    
if @arama <> ''     
begin                     
if @orderby=0     
begin    
 if @IslemNoLike = 0    
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '     
 else    
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '     
end;                                        
if @orderby=1                                         
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                         
if @orderby=2                                         
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                        
if @orderby=3                                         
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                         
if @orderby=4                                        
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                        
if @orderby=5     
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                             
 if @orderby=6                                        
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                             
if @orderby=7                                        
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                             
if @orderby=8                                        
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '  
if @orderby=9                                        
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))   
      or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))  
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))  
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '      
end;                            
                   
set @str=@str+')X where X.sira >'+convert(varchar,@rc - @GosterilecekKayit) +') order by ';                                        
                                        
if @orderby in (0,9)                                        
  set @str=@str+'IslemNo'                    
if @orderby=1                                         
 set @str=@str+'FaturaNo'                                         
if @orderby=2                                         
 set @str=@str+'CariAdi'                                        
if @orderby=3                                         
 set @str=@str+'CariKodu'                          
if @orderby=4                                        
 set @str=@str+'KULADI'                                        
if @orderby=5                                        
 set @str=@str+'SaticiAdi'                                        
if @orderby=6                                        
 set @str=@str+'eFaturaDurumu'                   
if @orderby=7                                     
 set @str=@str+'YaziciCount'                   
if @orderby=8                                        
 set @str=@str+'FaturaTarihi'                        
exec(@str) 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
UPDATE StokVergiMuafiyetleri SET MuafiyetKodu = '0',MuafiyetAciklamasi = 'YOK' where ISNULL(MuafiyetKodu,'') = '' 

		 
      				   
   			  
 		 
		 
      				   

   
 				   
 			  
   
 	  
 	
 	
 	
   
		  
		 
 		 
  		  
  	
	
 	  
   
			 
    
	
   
		  
     		  
 			
  	 
  		   
  
		 
	
     
 				  
	
             	   
 		  
      			
 			 
 				 
		  

    				 
    	   
    				
    			   
       
        
     	
       
    				  
		 
	 
go--
-------------------------------------------------------------------------------------------------------------------------------------------

declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like 'DF__ACARI__Update_Da%'
SET @SQL='ALTER TABLE [dbo].[ACARI] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC(@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if    exists (select * from dbo.syscolumns where id = object_id(N'ACARI') and name=N'Update_Date')
begin
alter table ACARI ALTER COLUMN Update_Date DATETIME end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ASABIT') and name=N'T1')
begin
ALTER TABLE ASABIT ADD T1 int 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ASABIT') and name=N'T2')
begin
ALTER TABLE ASABIT ADD  T2 int
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ASABIT') and name=N'B1')
begin
ALTER TABLE ASABIT ADD B1 int 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ASABIT') and name=N'B2')
begin
ALTER TABLE ASABIT ADD B2 int 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
update ASABIT set T1 = 20, T2=60, B1 = (CASE WHEN ISNULL(B1,0) = 0 THEN 3000 ELSE B1 END), B2=(CASE WHEN ISNULL(B2,0) = 0 THEN 7000 ELSE B2 END)
		 
      				   
   	   
    
		 
      				   
 		  
	
    			 
   			   

  			
     		 
   				
     		 
  
   	   
       
     
   				
      
      
   		  
         
      	  
 
  				
  			
      
    			 
     	  
    		  
  
    			 
 				
       
     
		 
      				   

   			
 		 
		 
      				   
 			  
	
       	 
      				 
      
     
        		
       	  
      				   
       	   
       
        		
      				 
        
     				   
      	 
      				  
         
       	  
       		
		 
      				   
   	   
 	
		 
      				   
 		   
	
	   
  	   
   	 
   	
    		
  
 	
  	   
  				  
  	  
    		
  
   
  	   
  			 
  
    	
  
  
  	   
  				  
  		
    		 
  
  
  	   
  				  
  		
    		 
  
  
  	   
  				  
  		
    		 
  
  
  	   
  				  
  		
    		 
  
	
go--
-------------------------------------------------------------------------------------------------------------------------------------------

if exists (select * from sys.procedures where name='sp_StokFiyat_Bakim')
drop proc sp_StokFiyat_Bakim 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create PROCEDURE sp_StokFiyat_Bakim    
AS    
begin    
 declare @i int,@sorgu nvarchar(max),@cnt nvarchar(2)    
 set @i = 1;    
    while @i <= 20     
 begin    
   SET @cnt = CAST(@i as varchar(2));    
   SET @sorgu = ' UPDATE StokFiyat SET  F'+@cnt+'Fiyat = CASE WHEN F'+@cnt+'Kdv = ''H'' THEN F'+@cnt+'FiyatH ELSE (F'+@cnt+'FiyatH*(100 + SatisKdv)/100) END';    
   -- print @sorgu    
   EXEC sp_executesql @sorgu;    
   SET @i += 1;    
    end;    
  
   UPDATE sk SET sk.F1Fiyat  = sf.F1Fiyat, sk.F1DovizKodu = sf.F1DovizKodu,  sk.F1Kdv = sf.F1Kdv,  
			   sk.F2Fiyat  = sf.F2Fiyat, sk.F2DovizKodu = sf.F2DovizKodu,  sk.F2Kdv = sf.F2Kdv, 
               sk.F3Fiyat  = sf.F3Fiyat, sk.F3DovizKodu = sf.F3DovizKodu,  sk.F3Kdv = sf.F3Kdv, 
               sk.F4Fiyat  = sf.F4Fiyat, sk.F4DovizKodu = sf.F4DovizKodu,  sk.F4Kdv = sf.F4Kdv 
          FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu  
 
end;  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spDevirBakim')
drop proc spDevirBakim 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROC spDevirBakim @DBE nvarchar(30)  
as  
--exec('use '+@DBE)  
-- spDevirBakim 'Ent_MLTSBS'  
exec('update '+@DBE+'.dbo.STOKHAR set StokKodu = (select top 1 Kodu from '+@DBE+'.dbo.STOKKARTI where STOKKARTI.StokNo = STOKHAR.StokNo or STOKKARTI.StokAdi = STOKHAR.StokAdi) where StokKodu in ('''','' '',null)')  
  
delete from STOKHAR where StokKodu in ('',' ',null)  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   		   
 		
		 
      				   
 			 
	
  				  
 	   
 	   
				 
				 
		   
		   
				
	   
     			 
		   
				 
	 
	 

   	  
     
   	  
     

  			
  
		   
		  
				
	   
     			
     				  
		   
		   
				 
	 
   			
		   
		   
				
	   
      	 
		   
				 
	 
		 
         
    	   
     
		 
      				   

       
	  
     
     		  
     	 
           
   		 
          
 		 
         				   
       		   
   
 		 
   	 
  		  
	  
			  
      
   
  
 				 
				 
				   
    
 				  
  				 
 			
   	 
   	   
   		 
  	  
   	   
 				   
  	  
 		  
  
   	 
  		
		
			  
			   
		 
 
 		  
 			  
   	   
   	   
 		   
    				 
    			
				
  
  			 
  			 
 	 
      
  
     
    
      	
 	
    		
    	
    		  
    		  
     		   
		
 		
     
  			
			
				 
  
 
				   
				
				  
 	 
 		 
 	 
				 
		 
      				   

if exists (select * from sys.procedures where name='sp_AsabitUyari')
drop proc sp_AsabitUyari
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   proc sp_AsabitUyari  
with encryption
as

Declare @Byt int
Declare @tt1 int
Declare @tt2 int
Declare @ayy int
Declare @bb1 int
Declare @bb2 int
Declare @sonuc int 

select @ayy=AA.AY,@tt1=T1,@tt2=T2,@Byt=Boyut, @bb1=B1,@bb2=B2 from
(SELECT MAX (AY) - MIN (AY) AY, T1, T2,B1,B2
               FROM (  SELECT (SELECT T1 FROM ASABIT)    T1, 
                             (SELECT T2 FROM ASABIT)     T2, 
 		 (SELECT B1 FROM ASABIT)     B1,
 		 (SELECT B2 FROM ASABIT)     B2,
                            Month (TARIH)                AY,
 		
                             COUNT (*)                   CNT 
                        FROM ACARI 
                    GROUP BY Month (TARIH) 
                      HAVING COUNT (*) > 50) X 
              GROUP BY T1, T2,B1,B2)AA,(SELECT SUM(SizeMB) as Boyut
 			FROM (
 				SELECT DB_NAME(database_id) AS DatabaseName,
     Name AS Logical_Name,
     Physical_Name,
     (size * 8) / 1024  SizeMB
 				FROM sys.master_files
 				WHERE DB_NAME(database_id) = (SELECT DB_NAME() AS [Current Database])
 			)Boyut)BB

if (@bb1=null or @bb1 <100 )
  Set @bb1=100
if (@bb2=null or @bb2 <500 )
  Set @bb2=500

if (@tt1=null or @tt1 <12 )
  Set @tt1=12
if (@tt2=null or @tt2 <100 )
  Set @tt2=100

if (@Byt>@bb2 or @ayy>@tt2)
  Set @sonuc=2
else
if (@Byt>@bb1 or @ayy>@tt1)
  Set @sonuc=1
else
  Set @sonuc=0

select @sonuc as sonuc, @ayy as ay, @Byt as boyut
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   			 
 		 
		 
      				   
   	   
    
		 
      				   
 			
	  
  				 
   	  
    			   
   				 
  
  			  
     		   
   				  
     		   
	
   		 
     	
       
   				  
        
    	
   			
      	 
      		
   
  				  
  			  
        
    			   
     		
    			
		 
      				   
DECLARE 
    @StokBarkodlariNoId int, 
    @stokKodu   nvarchar(40),
	@Birim nvarchar(10),
	@Kod nvarchar(40)

DECLARE Stoklar CURSOR FOR (select DISTINCT  s1.StokKodu from StokBarkodlari s1,StokBarkodlari s2 where s1.StokKodu = s2.StokKodu and s1.Birim = s2.Birim and s1.Carpani <> s2.Carpani and s1.Carpani <> 1) 
  OPEN Stoklar;
 FETCH NEXT FROM Stoklar INTO   @Kod
WHILE @@FETCH_STATUS = 0
BEGIN
		DECLARE stokbarkod CURSOR
			FOR (select DISTINCT  s1.StokBarkodlariNoId,s1.StokKodu from StokBarkodlari s1,StokBarkodlari s2 where s1.StokKodu = s2.StokKodu and s1.Birim = s2.Birim and s1.Carpani <> s2.Carpani and s1.Carpani <> 1 
 and s1.StokKodu = @Kod)
		OPEN stokbarkod;
		FETCH NEXT FROM stokbarkod INTO   @StokBarkodlariNoId,@StokKodu;  
		
		WHILE @@FETCH_STATUS = 0
			BEGIN		
	        	SET @Birim = (Select TOP 1 BirimAdi from BIRIM B where NOT EXISTS (Select BirimAdi from StokBarkodlari sb where sb.StokKodu =@StokKodu and B.BirimAdi = sb.Birim)) 			
				UPDATE StokBarkodlari SET Birim =@Birim where StokBarkodlariNoId = @StokBarkodlariNoId
				FETCH NEXT FROM stokbarkod INTO     @StokBarkodlariNoId,@StokKodu;
			END;

		CLOSE stokbarkod;

		DEALLOCATE stokbarkod;
FETCH NEXT FROM Stoklar INTO     @Kod
END;

CLOSE Stoklar;

DEALLOCATE Stoklar;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'Barkodu')
begin
ALTER TABLE STOKKARTI ALTER COLUMN Barkodu  nvarchar(40)
end 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
UPDATE STOKKARTI SET Barkodu = Kodu where ISNULL(Barkodu,'') = ''
go--
-------------------------------------------------------------------------------------------------------------------------------------------
UPDATE STOKKARTI SET Barkodu = Kodu where ISNULL(Barkodu,'') = ''
go--
      				 

update StokBarkodlari SET Barkodu =REPLACE (Barkodu,'_2_' ,'--2')  
update StokBarkodlari SET Barkodu =REPLACE (Barkodu,'_3_' ,'--3')  
update StokBarkodlari SET Barkodu =REPLACE (Barkodu,'_4_' ,'--4')  
update StokBarkodlari SET Barkodu =REPLACE (Barkodu,'_5_' ,'--5')  
update StokBarkodlari SET Barkodu =REPLACE (Barkodu,'_6_' ,'--6')  


go--
      				 
       
   		
   			   
    
      	   
               
              			
               		
                
              			  
	 
   				   
 	 
	
			  
		 
      				 
  			   
 				   
 			  
 		  
 	  
 	
 	
 	
    
 	 
		  
		 
 		 
  		  
  	
	
 	  
   
			 
    
	
   
		  
     		  
 			
  	 
  		   
  
		 
	
     
 				  
	
             	   
 		  
      			
 			 
 				 
		 
    				  
    		
    		
    	   
     		  
     		   
     			
     		  
    			 
		 
	 
		 
-------------------------------------------------------------------------------------------------------------------------------------------
DELETE FROM  GridView where  KayitIsmi = 'FrmBankaicxGrid1cxGrid1DBTableView1'
		 
      				   
   			  
 		  
		 
      				   
  		   
 			 
 			 
 			
 			
 	  
 			 
 				  
 	  
   
    
			  
				 
    
 		   
    
			
      				   
			
		 
    				 
		  
 				   
   		  

 			 
   		 
	
				  
			 
				 
		
    
   				  
		
    
	   
		   
 		 
    		   
 		 
    		   
 		 
    			
 		 
      
	  
	
     
			
   
  
			
  		  
       
    			 
			
		 
    				 
			
 				   
   		
  
 			 
   		 
			
				  
			 
				 
		
    
   				  
		
    
		
   
 		 
    		   
 		 
    		   
 		 
    			
 		 
     
		
     	  
			
  				  
    		   
	  
    
	  
	  
 		
  		 
 		
  		 
 		
  		 
 		
  		  
 		
  	   
			 
 	  
			
	 
		 
      				   
   				   
 			   
		 
      				   
  	 
	  
		 
  				 
				
    
		  
   
         
   
 			 
				   
			
	
     
 			  
 			  
 			 
     
	
		 
      				   
   		   
 	   
		 
      				   
 			   
	  
   
 		 
 		 
				   
				   
			 
			 
				  
		 

     			   
			 
				   
	   
	   
   		
   	
   		
   	
  
  			  
	
			 
			
				  
		 
       				
        	 
			 
			 
				   
	   

  				   
	 
       
  		 
   
   			  
			 
			 
				  
		 
             			
			 
				   
	


   			  
			 
			 
				  
		 
             			
			 
				   
	
		 
      				   
   				
 		
		 
      				   
  		  
	  
    
	 
    			
        
  		 
	 
	
	
 			
      
	
    		   
     				  
	
 			  
	
  				
			 
			   
				  
				
				 
			   
				  
				  
		 
 		   
 		   
			
 		  
	
				   
 		  
		
			
			   
			 
			  
			
				
	
  			
		 
 		
		   
  
  	 
 	
		 
		  
		   
   
 			 
 				 
		 
	
 	 
	   
			 
				
			  
			   
			 
  
			  
			
		   
			  
	   
			
	   
			
			  
			   
				
			  
   
   
 
 		
			   
	   
	
 			
		  
			  
				 
			   
				
			  
			   
	   
	  
 
 	 
	
	 
 		 
  				  
  	
    	   
      		
 	   
	 
      
  	
    	   
      		
 	   
	
		  
 	 
 	   
 	   
 			   
 		 
 		  
 			
 		
  			  
			  
  
			   
 			  
 		
				  
  		  
    
  				 
 				 
		 
      				   
     
 			 
		 
      				   
   
 			
 			
 		  
 	 
 			
 			   
 			   
 	
 		
 	  
   
 	  
 		 
			
 			   
 	  
 			   
     
   
 	   
  	  
 			 
				  
		   
          
  
 				
   			  
  
 			  
    	  
  
 	
  				
 				
      
  				 
   				 
	  
  	
             	   
  
	
		   
  	
         	 
  	
   			   
		  
 
			   
				  
		 
 	 
   			  
		  
  				 
		 
	
 	 
   				 
 	 
   				 
 	 
       
 	 
      	  
 	 
   				
			   
 		  
   
 			 
 				  
		   
 				   
		   
 	
 		   
 	
 		   
 	
 			
 	
 				 
 	
 		 
 	
 		   
		   
  		   
  		  
    	
      			   
  
 				
   			  
  
 			  
    	  
  
 	
  				
 				
      
  				 
      
		  
  	
             	   
  
	
	  
  	
         	
  	
   			  
		  
 
		 
				   
		  
 	 
   			  
		  
  				 
			
 	 
   				 
 	 
   				 
        
 	 
      	  
 	 
   				
			   
       
		   
 	
 			   
 	
 			   
 	
 				
 	
   
 	
 			 
 	
 			   
	 
 				 
		   
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokBarkodlari') and name=N'SiraNo')
begin
ALTER TABLE StokBarkodlari ADD SiraNo int
end
else begin
ALTER TABLE StokBarkodlari alter column SiraNo int
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   			  
 		  
		 
      				   
     
		
		   
  
    
	   
				  
			
   			
	
 			 
			 
   	  
		  
	
     	   
	   
 	  
			 
    			  
			   
     		   
			 
			  
			   
     	 
   		   
			  
			 
		  
    
			
				  
  
 				 
 		
 			
 				
 		  
 				   
		   
   
   		  
   
	
		 
      				   
update s1 SET  s1.SiraNo = s2.Rank from StokBarkodlari s1, (select *,DENSE_RANK() OVER  (PARTITION BY StokKodu ORDER BY AnaBirim DESC,Barkodu ) AS Rank  from StokBarkodlari) s2
where s1.StokBarkodlariNoId = s2.StokBarkodlariNoId
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 delete GridView where GirdAdi = 'Stok Barkodları Gridi'
	   
 				
	
		 
      				   
  
	   
		  
     		 
     		 
     			
     		   
     		  
   
				 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  				
		 
      				   
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'EIrsaliyeNo')
begin
ALTER TABLE STOKHARDETBAG add  EIrsaliyeNo varchar(50) 
end
else begin 
ALTER TABLE STOKHARDETBAG alter column  EIrsaliyeNo varchar(50) 
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
delete GridView where GirdAdi = 'Stok İşlemleri Gridi'
 go--
-------------------------------------------------------------------------------------------------------------------------------------------
    		
  	
		 

      				   
   	   
 				   
 		   
 
  
 			   
 	  
 		  
  
  
		 
				
		  
 
  			  
 				   
   		   
    				
   				
        
     		   
 	  
 	 
  
    
		 
      				   
    	
  
		 
      				   
   		  
 				
				  
			  
 		
 		
     
  			 
  	 
  	   
  	
  		   
 			 
  		  
 			  
 			 
     
 				  
 				
  	   
  
  		
  	 
 				  
 				
  			
  	  
     			 
     	   
   			   
 				  
  	
         
  		 
   
				
  			
       
		 
 			   
  		  
   	   
			 
    			  
       	  
		 
		 
  	
  				
				   
      				   
      				   
        		
   				
       		
      		  
 			  
       	
 	
 				   
 				   
 		   
 				   
        		   
        
             
   		  
      			
     	   
  		
    			   
  		  
       			
    	 
 	  
 	
  	  
   	  
  				   
   	   
 		  
   	
   			  
  			   
   		  
     
   				
     

		 
      				   
    	   
     
		 
          
     
	 
   
 			 
 	  
		 
		   
  				   
				  
				 
 			 
			 
			   
				   
				  
  			 
 		
    
      
   	 
    
      
 			   
    
 	  
 				
    
  	
	
		  
		   
	 
				
 	  
 		  
     		   
     		   
 	   
        
      				 
			
				 
    				
    				
  
      	 
    	 
    		
       
     	  
      
       				   
   	 
             
 	 
            
        			 
    		  
    
    	  
    	  
     	   
	
 
   	   
  				  
  	  
   
		
			 
				 
				
			   
			
			  
				  
				 
 	 
  
			 

		 
      				   

  if exists (select * from sys.tables where name='FifoLifoLog')
drop table FifoLifoLog
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE FifoLifoLog 
(Id int IDENTITY (1,1),
 NoId int,
 StokKodu nvarchar(100),
 Durum bit,
 KayitSayisi int,
 BaslamaTarihi datetime,
 BitisTarihi datetime,
 KulNo int)
		 
      				   
   				 
 			
		 
      				   
    		  
			  
  	
    		   
    			   
  			 
	   
			   
       				
 
      			 
 		
      
     				
     
 	 
	
   
			  
				  
  		 
	
       
   		 
	
 		   
 		 
 	   
 		  
 			  
 			 
 			
 		  
 		   
			
    
  
			
   		  
		 
 		   
			
   				 
     		
			
 	 
   				
				   
   
 	   
 			   
 				
 				  
 				
			
   	
   	  
  			
		
 			 
 
    			  
 			   
			
 	   
    
 		   
     	
 
  
 		 
       
 			  
			
     
 			 
				
  		 
   
 			  
 	  
      
				  
   		   
 
				
 				  
   
	
				   
   	   
    
	   
 
 	  
		  
      
       
	
      
		 
      				   
   			   
 		   
		 
      				   
 			  
  
  		  
   				   
    		  
		   

    		  
    			   
		   

  	  
  			
    
  		   
  				 
   	  
   	
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like '%VTG_TarihSaat%'
SET @SQL='ALTER TABLE [dbo].[VTGuncellemeleri] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC (@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like '%VTG_Prg%'
SET @SQL='ALTER TABLE [dbo].[VTGuncellemeleri] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC (@SQL)
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_Hareket_Bakim')
drop proc sp_Hareket_Bakim
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 CREATE     PROCEDURE sp_Hareket_Bakim      
AS      
       
DELETE FROM STOKHAR WHERE Miktar in (NULL,'',' ')     
DELETE FROM SIPARISHAR WHERE Miktar   in (NULL,'',' ')     
DELETE FROM TEKLIFHAR WHERE Miktar in (NULL,'',' ')       
 ---DEPO VİRMANDAN GELEN İŞLEMNO OLMAYAN KAYITLAR İÇİN      
UPDATE STOKHAR SET IslemNo=IslemNo-1 where IslemNo in( select IslemNo from STOKHAR SH WHERE NOT EXISTS     
(SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo) AND IslemTuru='Depo Vir.')      
--STOKHAR OLUP STOKHARDETBAG OLMAYAN KAYITLARI GÖRMEK İÇİN      
select IslemNo from STOKHAR SH WHERE NOT EXISTS (SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo or SD.IslemNo=SH.IslemNo3)      
--STOKHAR OLUP STOKHARDETBAG OLMAYAN KAYITLARI SİLMEK İÇİN      
DELETE FROM STOKHAR WHERE IslemNo IN  (select IslemNo from STOKHAR SH WHERE NOT EXISTS     
(SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo or SD.IslemNo=SH.IslemNo3) AND IslemTuru<>'Depo Vir.' ) 

-- Faturalaşmış Açık Kalan İrsaliyeleri Kapatan Sorgu
UPDATE sb  SET AcikKapali = 'K' FROM STOKHARDETBAG sb
 WHERE FisTuru = 'I' 
  and AcikKapali = 'A' 
  and FaturaNo > 0 
  and IslemTuru in ('Alış','Alış İade','Satış','Satış İade')
  and EXISTS(select * from STOKHAR sh where sh.IslemNo = sb.IslemNo )
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_SaticiAdiDegistir')
drop proc sp_SaticiAdiDegistir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 create   proc sp_SaticiAdiDegistir @EskiSaticiKodu nvarchar(10), @YeniSaticiKodu nvarchar(50), @EskiSaticiAdi nvarchar(50), @YeniSaticiAdi nvarchar(50), @EskiHareketlereUygula  int = 0    
as    
begin          
 if (@EskiSaticiKodu <> @YeniSaticiKodu) or (@EskiSaticiAdi <>@YeniSaticiAdi)   
 begin    
   update Satici set SaticiKodu = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu         
   if (@EskiHareketlereUygula <> 0 )    
    begin     
	 BEGIN transaction    
     begin try    
		 update STOKHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu    
		 update STOKHARDETBAG SET  Satici = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu  
		 update ACARI SET SaticiKodu = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update KASA SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu    
		 update GELGID SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu 
		 update AMUSTERI SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu   
		 update BORDRO SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu    
		 update DSYBSuniTohumlama SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu   
		 update DSYB_IsEmirleri SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu
		 update TEKLIFHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu  
		 update TEKLIFHARDETBAG SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update Ziyaret SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu 
		 update SipHar SET  Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu    
		 update SipHarDetBag SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu   
		 update Rut SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update OdemeSozu SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update SIPARISHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu
		 update SIPARISHARDETBAG SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu
     COMMIT    
     END try    
	 begin catch    
		 rollback    
	 end catch    
   end     
 end    
end 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'IRSALIYEUUID')
begin
ALTER TABLE STOKHARDETBAG ADD IRSALIYEUUID NVARCHAR(50) NULL
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'EIRSALYETARIHI')
 begin
ALTER TABLE STOKHARDETBAG ADD EIRSALYETARIHI DATETIME NOT NULL DEFAULT(GETDATE())
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
  if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'EIRSALIYEDURUM')
 begin
ALTER TABLE STOKHARDETBAG ADD EIRSALIYEDURUM NVARCHAR(100) NULL
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_mb_XML_StokHarDurumUpdateDelphi')
drop proc sp_mb_XML_StokHarDurumUpdateDelphi
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[sp_mb_XML_StokHarDurumUpdateDelphi]       
/* 2020 01 16 a.t güncelleme fatno*/  
/*2021 07 17 a.t entegrator*/  
 @IslemNo int,      
 @eFaturaGonderimTarihi DateTime,       
 @eFaturaDurumu nvarchar(20),      
 @UUID nvarchar(60),      
 @Efaturano nvarchar(60)='',  
 @Entegrator int=0,  
 @ProcessType int=1  
AS       
update STK      
 set      
 
    IRSALIYEUUID= CASE WHEN  @ProcessType=3  THEN   @UUID ELSE IRSALIYEUUID END  ,      
   UUID= CASE WHEN  @ProcessType<>3  THEN   @UUID ELSE UUID END  ,  
  eFaturaGonderimTarihi=@eFaturaGonderimTarihi,       
  EIRSALIYEDURUM= CASE WHEN  @ProcessType=3  THEN   @eFaturaDurumu ELSE EIRSALIYEDURUM END  ,      
   eFaturaDurumu= CASE WHEN  @ProcessType<>3  THEN   @eFaturaDurumu ELSE eFaturaDurumu END  ,  
  eFaturaNo=case when @ProcessType<>3 then  @Efaturano   else '' end,  
 IrsNo= case when @ProcessType=3 and @Efaturano<>'' then right (@eFaturaNo,9)  else IrsNo  end  
 ,EIrsaliyeNo=case when @ProcessType=3 then  @Efaturano else EIrsaliyeNo end
 ,FatSerNo=case when @ProcessType<>3 and @Efaturano<>'' then  convert(varchar(3),left(@Efaturano,3)) else FatSerNo   end  
 ,STK.FaturaNo=case when @ProcessType<>3 and  @Efaturano<>'' then  right (@eFaturaNo,9) else STK.FaturaNo end  
 ,Entegrator=@Entegrator  
 from STOKHARDETBAG STK  
  
  where   (STK.IslemNo=@IslemNo OR STK.IslemNo3=@IslemNo);  
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokBarkodlari') and name=N'SiraNo')
begin
ALTER TABLE StokBarkodlari ADD SiraNo int
end
else begin
ALTER TABLE StokBarkodlari alter column SiraNo int
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKHARDETBAG') and name=N'EIrsaliyeNo')
begin
alter table STOKHARDETBAG ADD EIrsaliyeNo nvarchar(50) null
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
delete GridView where GirdAdi = 'Stok İşlemleri Gridi'

go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'HESAPLAR') and name=N'Aciklama2')
begin
ALTER TABLE HESAPLAR ADD Aciklama2 nvarchar(250)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'KASAT') and name=N'Aciklama2')
begin
ALTER TABLE KASAT ADD Aciklama2 nvarchar(250)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'GELGIDT') and name=N'Aciklama')
begin
ALTER TABLE GELGIDT ADD Aciklama nvarchar(250)
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_stokbarkod_kaydet')
drop proc sp_stokbarkod_kaydet
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create    proc  sp_stokbarkod_kaydet @Tip int,@StokNo int      
AS      
BEGIN      
  
 DECLARE  @Kodu nvarchar(100), @Barkodu nvarchar(100),@AnaBirim nvarchar(20)    
       
 IF @Tip = 0      
 BEGIN      
  Select @Barkodu = Barkodu from STOKKARTI where StokNo = @StokNo;      
    
  IF ISNULL(@Barkodu,'') = ''    
  BEGIN      
	UPDATE STOKKARTI SET Barkodu = Kodu where StokNo = @StokNo;      
  END;    
    
  Select  @Kodu = Kodu,@Barkodu = Barkodu,@AnaBirim = AnaBirim from STOKKARTI where StokNo = @StokNo;      
       
  IF @Barkodu <> ''       
  BEGIN      
    IF (Select count(*) from StokBarkodlari Where StokNo = @StokNo and AnaBirim = 1) > 0      
    BEGIN      
		UPDATE StokBarkodlari SET Barkodu = @Barkodu,Birim = @AnaBirim where StokNo = @StokNo and AnaBirim = 1;      
    END      
    ELSE      
    BEGIN      
		insert into StokBarkodlari (StokKodu,Barkodu,KulNo,Carpani,UpdateDate,StokNo,Birim,AnaBirim,SiraNo)    
			 values (@Kodu,@Barkodu,1,1,GETDATE(),@StokNo,@AnaBirim,1,1)        
    END;      
  END;       
 END      
 ELSE IF @Tip = 1      
 BEGIN    
 
  UPDATE sk       
     SET sk.Barkodu = sb.Barkodu,       
         sk.AnaBirim = sb.Birim      
    from STOKKARTI sk,      
         StokBarkodlari sb      
   where sk.StokNo = sb.StokNo      
     and sb.AnaBirim = 1      
     and sk.StokNo = @StokNo           
 END;      
 -- update StokFiyat set StokNo =@StokNo  where Kodu=@Kodu  
 -- update StokBarkodlari set StokNo =@StokNo   where StokKodu=@Kodu  
   
END; 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
update s1 SET  s1.SiraNo = s2.Rank from StokBarkodlari s1, (select *,DENSE_RANK() OVER  (PARTITION BY StokKodu ORDER BY AnaBirim DESC,Barkodu ) AS Rank  from StokBarkodlari) s2
where s1.StokBarkodlariNoId = s2.StokBarkodlariNoId

go--
-------------------------------------------------------------------------------------------------------------------------------------------
   				 
	   
 				
	
		 
      				   
   
	   
		  
     		 
     		 
     			
     		   
     		  
   
				 
		 
      				   
if exists (select * from sys.procedures where name='sp_SaticiAdiDegistir')
drop proc sp_SaticiAdiDegistir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  proc sp_SaticiAdiDegistir @EskiSaticiKodu nvarchar(10), @YeniSaticiKodu nvarchar(50), @EskiSaticiAdi nvarchar(50), @YeniSaticiAdi nvarchar(50), @EskiHareketlereUygula  int = 0    
as    
begin          
 if (@EskiSaticiKodu <> @YeniSaticiKodu) or (@EskiSaticiAdi <>@YeniSaticiAdi)   
 begin    
   update Satici set SaticiKodu = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu         
   if (@EskiHareketlereUygula <> 0 )    
    begin     
	 BEGIN transaction    
     begin try    
		 update STOKHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu    
		 update STOKHARDETBAG SET  Satici = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu  
		 update ACARI SET SaticiKodu = @YeniSaticiKodu, SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update KASA SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu    
		 update GELGID SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu 
		 update AMUSTERI SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu   
		 update BORDRO SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu    
		 update DSYBSuniTohumlama SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu   
		 update DSYB_IsEmirleri SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu
		 update TEKLIFHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu  
		 update TEKLIFHARDETBAG SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update Ziyaret SET  SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu 
		 update SipHar SET  Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu    
		 update SipHarDetBag SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu   
		 update Rut SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update OdemeSozu SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu  
		 update SIPARISHAR SET Satici = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where Satici = @EskiSaticiKodu
		 update SIPARISHARDETBAG SET SaticiKodu = @YeniSaticiKodu,SaticiAdi = @YeniSaticiAdi where SaticiKodu = @EskiSaticiKodu
     COMMIT    
     END try    
	 begin catch    
		 rollback    
	 end catch    
   end     
 end    
end 
		 
      				   
   			
 		
		 
      				   
  		  
	  
    
	 
    			
        
  		 
	 
	
	
 			
      
	
    		   
     				  
	
 			  
	
  				
			 
			   
				  
				
				 
			   
				  
				  
		 
 		   
 		   
			
 		  
	
				   
 		  
		
			
			   
			 
			  
			
				
	
  			
		 
 		
		   
  
  	 
 	
		 
		  
		   
   
 			 
 				 
		 
	
 	 
	   
			 
				
			  
			   
			 
  
			  
			
		   
			  
	   
			
	   
			
			  
			   
				
			  
   
   
 
 		
			   
	   
	
 			
		  
			  
				 
			   
				
			  
			   
	   
	  
 
 	 
	
	 
 		 
  				  
  	
    	   
      		
 	   
	 
      
  	
    	   
      		
 	   
	
		  
 	 
 	   
 	   
 			   
 		 
 		  
 			
 		
  			  
			  
  
			   
 			  
 		
				  
  		  
    
  				 
 				 

go--
-------------------------------------------------------------------------------------------------------------------------------------------
   				 
 			 
		 
      				   
     
 			
 			
 		  
 	 
 			
 			   
 			   
 	
 		
 	  
   
 	  
 		 
			
 			   
 	  
 			   
     
   
 	   
  	  
 			 
				  
		   
          
  
 				
   			  
  
 			  
    	  
  
 	
  				
 				
      
  				 
   				 
	  
  	
             	   
  
	
		   
  	
         	 
  	
   			   
		  
 
			   
				  
		 
 	 
   			  
		  
  				 
		 
	
 	 
   				 
 	 
   				 
 	 
       
 	 
      	  
 	 
   				
			   
 		  
   
 			 
 				  
		   
 				   
		   
 	
 		   
 	
 		   
 	
 			
 	
 				 
 	
 		 
 	
 		   
		   
  		   
  		  
    	
      			   
  
 				
   			  
  
 			  
    	  
  
 	
  				
 				
      
  				 
      
		  
  	
             	   
  
	
	  
  	
         	
  	
   			  
		  
 
		 
				   
		  
 	 
   			  
		  
  				 
			
 	 
   				 
 	 
   				 
        
 	 
      	  
 	 
   				
			   
       
		   
 	
 			   
 	
 			   
 	
 				
 	
   
 	
 			 
 	
 			   
	 
 				 
		   
		 
      				   
if exists (select * from sys.procedures where name='sp_Grup_Bakim')
drop proc sp_Grup_Bakim
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE sp_Grup_Bakim    
AS      
BEGIN      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT UreticiFirmaKodu,'U',1,'Admin' from STOKKARTI       
 where UreticiFirmaKodu <> '' and UreticiFirmaKodu is not null      
   and NOT EXISTS (Select 1 from GRUP where GrupAdi = UreticiFirmaKodu and Turu = 'U')      
      
   -- Grup Adı insert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT GrupAdi,'G',1,'Admin' from STOKKARTI sk      
 where GrupAdi <> '' and GrupAdi is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = sk.GrupAdi and g.Turu = 'G')      
      
 -- Cari Tip İnsert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT Tipi,'T',1,'Admin' from AMUSTERI m      
 where Tipi <> '' and Tipi is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.Tipi and g.Turu = 'T')      
      
 -- Cari O1 İnsert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT OZELKOD1,'O1',1,'Admin' from AMUSTERI m      
 where OZELKOD1 <> '' and OZELKOD1 is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD1 and g.Turu = 'O1')      
      
 -- Cari O2 İnsert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT OZELKOD2,'O2',1,'Admin' from AMUSTERI m      
 where OZELKOD2 <> '' and OZELKOD2 is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD2 and g.Turu = 'O2')      
      
 -- Cari O3 İnsert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT OZELKOD3,'O3',1,'Admin' from AMUSTERI m      
 where OZELKOD3 <> '' and OZELKOD3 is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD3 and g.Turu = 'O3')      
      
 -- Cari O4 İnsert      
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)      
 select DISTINCT OZELKOD4,'O4',1,'Admin' from AMUSTERI m      
 where OZELKOD4 <> '' and OZELKOD4 is not null      
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD4 and g.Turu = 'O4')      
      
END;
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokDoviz_Bakim')
drop proc sp_StokDoviz_Bakim
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create   proc sp_StokDoviz_Bakim     
as    
UPDATE STOKHAR SET DovizKuru = (CASE WHEN DovizKodu in ('YP', 'TL') or SonTutar = 0 or SonTutarDov = 0 THEN 1 ELSE SonTutar / SonTutarDov END)     
UPDATE STOKHARDETBAG SET DovizKur = (CASE WHEN DovizKodu in ('YP', 'TL') or Tutar = 0 or DovizTut = 0 THEN 1 ELSE Tutar / DovizTut END)    
UPDATE ACARI SET DovizKur = 1 where DOVIZKODU in ('YP', 'TL')    
UPDATE KASA SET DovizKur = 1 where DovizKodu in ('YP', 'TL')    
UPDATE ACARI SET DovizKur = (CASE WHEN BORC >0 AND DovizBorc > 0   THEN BORC / DovizBorc    WHEN ALACAK > 0 and DovizAlacak > 0 THEN ALACAK / DovizAlacak  ELSE 1 END)    
UPDATE TEKLIFHAR SET DovizKur = (CASE WHEN DovizKodu in ('YP', 'TL') or SonTutar = 0 or SonTutarDov = 0 THEN 1 ELSE SonTutar / SonTutarDov END)     
UPDATE TEKLIFHARDETBAG SET DovizKur = (CASE WHEN DovizKodu in ('YP', 'TL') or Tutar = 0 or DovizTut = 0 THEN 1 ELSE Tutar / DovizTut END)    
UPDATE SIPARISHAR SET DovizKur = (CASE WHEN DovizKodu in ('YP', 'TL') or SonTutar = 0 or SonTutarDov = 0 THEN 1 ELSE SonTutar / SonTutarDov END)     
UPDATE SIPARISHARDETBAG SET DovizKur = (CASE WHEN DovizKodu in ('YP', 'TL') or Tutar = 0 or DovizTut = 0 THEN 1 ELSE Tutar / DovizTut END)    
UPDATE KASA SET DovizKur = (CASE WHEN BORC > 0 and DovizBorc > 0   THEN BORC / DovizBorc    WHEN ALACAK > 0 and DovizAlacak > 0 THEN ALACAK / DovizAlacak  ELSE 1 END)    
select  'STH', DovizKodu,DovizKuru,max(FaturaTarihi) mxTarih, min(FaturaTarihi) mnTrh from STOKHAR group by DovizKodu,DovizKuru     UNION  
select   'STHDB',DovizKodu,DovizKur,max(FaturaTarihi) mxTarih, min(FaturaTarihi) mnTrh from STOKHARDETBAG group by DovizKodu,DovizKur   UNION  
select   'ACARI',DOVIZKODU,DovizKur,max(TARIH) mxTarih, min(TARIH) mnTrh from ACARI group by DOVIZKODU,DovizKur  UNION   
select   'KASA',DovizKodu,DovizKur,max(TARIH) mxTarih, min(TARIH) mnTrh from KASA group by DovizKodu,DovizKur ORDER BY 1,2,3   
 select   'Kasa_DovizKodu_olmayanlar',DovizKodu,DovizKur,ISLEMNO   from KASA where DovizKodu not in (select DovizKodu from DOVIZT )  UNION  
 select   'Cari_DovizKodu_olmayanlar',DOVIZKODU ,DovizKur,ISLEMNO   from ACARI  where DOVIZKODU not in (select DovizKodu from DOVIZT ) UNION    
 select   'STOKHAR_DovizKodu_olmayanlar',DovizKodu,DovizKuru,IslemNo   from STOKHAR where DovizKodu not in (select DovizKodu from DOVIZT )  UNION   
 select   'STOKHARDETBAG_DovizKodu_olmayanlar',DovizKodu,DovizKuru,IslemNo   from STOKHAR where DovizKodu not in (select DovizKodu from DOVIZT ) 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   		   
 	   
		 
      				   
 				  
		
     
 		   
 		   
  
  
			   
			   
 
		   
  
     				 
			   
  
		 
		 
   		  
   	  
   		  
   	  
	
  				
	  
			   
			  
 
		   
       				  
        	   
			   
			   
  
		 
  
    
	   
     	
  		   
	 
   				
			   
			   
 
		   
            			   
				   
			   
  
	  
  
  
   				
			   
			   
 
		   
            			   
				   
			   
  
	
		 
      				   
   	   
    
		 
      				   
 			 
		
  				   
   		
    				 
   				   
	  
  				
     			 
    
     			 
	  
   		   
     	  
    	 
    
     	 
    	  
   			  
      	   
      		  
	 
   
  				
      	
    				 
     		  
    		 

		 
      				   
if exists (select * from sys.procedures where name='sp_BAKIM_KOLON_UPDATE')
drop proc sp_BAKIM_KOLON_UPDATE
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROC sp_BAKIM_KOLON_UPDATE  
AS  
declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)  
select @CNTNAME=name from sys.default_constraints where name like '%Update_Da%'  
SET @SQL='ALTER TABLE [dbo].[ACARI] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'  
EXEC (@SQL)  
  
select @CNTNAME=name from sys.default_constraints where name like '%DF__AcariTaka__Updat%'  
SET @SQL='ALTER TABLE [dbo].[AcariTakas] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'  
EXEC (@SQL)  
  
 ALTER TABLE ACARI ALTER COLUMN TDATE DATETIME  
 ALTER TABLE ACARI ALTER COLUMN Update_Date DATETIME  
 ALTER TABLE ACARI ADD  DEFAULT (getdate()) FOR [Update_Date]  
 ALTER TABLE AcariTakas ALTER COLUMN TDATE DATETIME  
 ALTER TABLE AcariTakas ALTER COLUMN Update_Date DATETIME  
 ALTER TABLE AcariTakas ADD  DEFAULT (getdate()) FOR [Update_Date]  
 delete from Parametre where Param='Stokkartı Fiili Stok Adedi'  

go--
-------------------------------------------------------------------------------------------------------------------------------------------
   		  
 	  
		 
      				   
  	 
		  
		 
  				
    
   
   		
     		 
    			   
   		  
         
   		  
    			 
     				  
  
    
  			   
    
 	 
  
   			
   		 

		 
      				   
   	 
  

		 
      				   
DECLARE 
    @StokBarkodlariNoId int, 
    @stokKodu   nvarchar(40),
	@Birim nvarchar(10),
	@Kod nvarchar(40)

DECLARE Stoklar CURSOR FOR (select DISTINCT  s1.StokKodu from StokBarkodlari s1,StokBarkodlari s2 where s1.StokKodu = s2.StokKodu and s1.Birim = s2.Birim and s1.Carpani <> s2.Carpani and s1.Carpani <> 1) 
  OPEN Stoklar;
 FETCH NEXT FROM Stoklar INTO   @Kod
WHILE @@FETCH_STATUS = 0
BEGIN
		DECLARE stokbarkod CURSOR
			FOR (select DISTINCT  s1.StokBarkodlariNoId,s1.StokKodu from StokBarkodlari s1,StokBarkodlari s2 where s1.StokKodu = s2.StokKodu and s1.Birim = s2.Birim and s1.Carpani <> s2.Carpani and s1.Carpani <> 1 
 and s1.StokKodu = @Kod)
		OPEN stokbarkod;
		FETCH NEXT FROM stokbarkod INTO   @StokBarkodlariNoId,@StokKodu;  
		
		WHILE @@FETCH_STATUS = 0
			BEGIN		
	        	SET @Birim = (Select TOP 1 BirimAdi from BIRIM B where NOT EXISTS (Select BirimAdi from StokBarkodlari sb where sb.StokKodu =@StokKodu and B.BirimAdi = sb.Birim)) 			
				UPDATE StokBarkodlari SET Birim =@Birim where StokBarkodlariNoId = @StokBarkodlariNoId
				FETCH NEXT FROM stokbarkod INTO     @StokBarkodlariNoId,@StokKodu;
			END;

		CLOSE stokbarkod;

		DEALLOCATE stokbarkod;
FETCH NEXT FROM Stoklar INTO     @Kod
END;

CLOSE Stoklar;

DEALLOCATE Stoklar;
go--

-------------------------------------------------------------------------------------------------------------------------------------------
      
	 
  				
   
		 
      				   
UPDATE STOKKARTI SET Barkodu = Kodu where ISNULL(Barkodu,'') = ''

go--
-------------------------------------------------------------------------------------------------------------------------------------------
insert into StokBarkodlari ( StokKodu, Barkodu, KulNo, Carpani, UpdateDate, StokNo,Birim,AnaBirim)    
SELECT Kodu,Barkodu,1 Kulno,Miktar,Tarih,StokNo,Birim,AnaBirim FROM 
	(select Kodu,Barkodu,1 Kulno,Miktar,GETDATE() Tarih,StokNo,Birim,AnaBirim,
		DENSE_RANK() OVER  (PARTITION BY Barkodu ORDER BY StokNo DESC) AS Rank  from (
		SELECT StokNo,Kodu,Barkodu,1 Miktar,AnaBirim Birim,1 AnaBirim FROM STOKKARTI WHERE Barkodu is not null or Barkodu <> '' UNION
		SELECT StokNo,Kodu,CASE WHEN ISNULL(IkinciBirimBarkodu,'') = '' THEN Barkodu + '--2' + IkinciBirim ELSE IkinciBirimBarkodu END,IkinciBirimMiktari Miktar,CASE WHEN ISNULL(IkinciBirim,'') = '' THEN AnaBirim ELSE IkinciBirim END,0 FROM STOKKARTI wHERE IkinciBirimMiktari>0 AND IkinciBirim  <> '' UNION
		SELECT StokNo,Kodu,CASE WHEN ISNULL(UcuncuBirimBarkodu,'') = '' THEN Barkodu + '--3' + UcuncuBirim ELSE UcuncuBirimBarkodu END,UcuncuBirimMiktari,CASE WHEN ISNULL(UcuncuBirim,'') = '' THEN AnaBirim ELSE UcuncuBirim END,0 FROM STOKKARTI wHERE UcuncuBirimMiktari>0 AND UcuncuBirim <> '' UNION
		SELECT StokNo,Kodu,CASE WHEN ISNULL(DorduncuBirimBarkodu,'') = '' THEN Barkodu + '--4' + DorduncuBirim ELSE DorduncuBirimBarkodu END,DorduncuBirimMiktari,CASE WHEN ISNULL(DorduncuBirim,'') = '' THEN AnaBirim ELSE DorduncuBirim END,0 FROM STOKKARTI wHERE DorduncuBirimMiktari>0 AND DorduncuBirim <> '' UNION
		SELECT StokNo,Kodu,CASE WHEN ISNULL(BesinciBirimBarkodu,'') = '' THEN Barkodu + '--5' + BesinciBirim ELSE BesinciBirimBarkodu END,BesinciBirimMiktari,CASE WHEN ISNULL(BesinciBirim,'') = '' THEN AnaBirim ELSE BesinciBirim END,0 FROM STOKKARTI wHERE BesinciBirimMiktari>0 AND BesinciBirim <> ''  UNION
		SELECT StokNo,Kodu,CASE WHEN ISNULL(AltinciBirimBarkodu,'') = '' THEN Barkodu + '--6' + AltinciBirim ELSE AltinciBirimBarkodu END,AltinciBirimMiktari,CASE WHEN ISNULL(AltinciBirim,'') = '' THEN AnaBirim ELSE AltinciBirim END,0 FROM STOKKARTI wHERE AltinciBirimMiktari>0 AND AltinciBirim <> ''
		) t
		where NOT EXISTS (Select 1 from StokBarkodlari a where a.Barkodu = t.Barkodu)
		AND Barkodu is NOT NULL
) t2
where Rank = 1
go--

-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spStokFiyatDegistir')
drop proc spStokFiyatDegistir
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spStokFiyatDegistir]          
 @GelenStokKodu nvarchar(30),          
 @FiyatAdi nvarchar(30),          
 @Fiyat decimal(18,8),        
 @DovizKodu nvarchar(3),  
 @Iskonto1 float = 0,   
 @Iskonto2 float = 0,   
 @Iskonto3 float = 0,   
 @Iskonto4 float = 0  ,
 @IskontoGuncelle bit = 0
as        
begin    
 DECLARE @DovizKuru float    
 DECLARE @Query nvarchar(max),@Fyt nvarchar(3)    
    DECLARE @ResultDovizKodu nvarchar(3)    
    
 IF LEN(@FiyatAdi) = 7    
  SET @Fyt = SUBSTRING(@FiyatAdi,1,2)    
    ELSE     
  SET @Fyt = SUBSTRING(@FiyatAdi,1,3);    
    
 if @DovizKodu =''    
 BEGIN    
  SET @Query = 'select @DovizKod = '+@Fyt+'DovizKodu from StokFiyat where Kodu = '''+@GelenStokKodu+'''';     
  EXEC sp_executesql @Query,    
         N'@DovizKod nvarchar(3) OUTPUT',    
          @DovizKod = @ResultDovizKodu OUTPUT ;    
 SET @DovizKodu =  @ResultDovizKodu;    
 END;    
    
 SET @DovizKuru = (Select GunFiyati from DOVIZT where DovizKodu = @DovizKodu)    
 SET @Fiyat = @Fiyat / @DovizKuru;    
    
 SET @Query = 'UPDATE StokFiyat SET '+@Fyt+'Fiyat = (CASE WHEN '+@Fyt+'Kdv = ''H'' THEN '+CAST(@Fiyat as nvarchar(50))+' ELSE ('+CAST(@Fiyat as nvarchar(50))+'*(100+SatisKdv))/100 END), '+@Fyt+'DovizKodu='''+@DovizKodu+''' where Kodu =''' + @GelenStokKodu + '''';    
 EXEC sp_executesql @Query;   
 SET @Query = 'UPDATE StokFiyat SET  '+@Fyt+'FiyatH =  '+CAST(@Fiyat as nvarchar(50))+' where Kodu = '''+@GelenStokKodu+'''';       
  EXEC sp_executesql @Query;     
 IF @Fyt in ('F1','F2','F3','F4')    
 BEGIN   
	 UPDATE sk SET sk.F1Fiyat  = sf.F1Fiyat, sk.F1DovizKodu = sf.F1DovizKodu,  sk.F1Kdv = sf.F1Kdv,  
				   sk.F2Fiyat  = sf.F2Fiyat, sk.F2DovizKodu = sf.F2DovizKodu,  sk.F2Kdv = sf.F2Kdv, 
				   sk.F3Fiyat  = sf.F3Fiyat, sk.F3DovizKodu = sf.F3DovizKodu,  sk.F3Kdv = sf.F3Kdv, 
				   sk.F4Fiyat  = sf.F4Fiyat, sk.F4DovizKodu = sf.F4DovizKodu,  sk.F4Kdv = sf.F4Kdv,
				   sk.AlisIndirimYuzde  =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto1 ELSE sk.AlisIndirimYuzde END),  
				   sk.AlisIndirimYuzde2 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto2 ELSE sk.AlisIndirimYuzde2 END),  
				   sk.AlisIndirimYuzde3 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto3 ELSE sk.AlisIndirimYuzde3 END),   
				   sk.AlisIndirimYuzde4 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto4 ELSE sk.AlisIndirimYuzde4 END)  
			  FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu and sk.Kodu = @GelenStokKodu    
 END;    
end; 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
DELETE FROM  GridView where  KayitIsmi = 'FrmBankaicxGrid1cxGrid1DBTableView1'

go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_BankaIslemListesi')
drop proc sp_BankaIslemListesi
go--
-------------------------------------------------------------------------------------------------------------------------------------------
create  PROCEDURE sp_BankaIslemListesi             
 @Tarih1 varchar(30),            
 @Tarih2 varchar(30),            
 @Dep1 varchar(500),            
 @Dep2 varchar(500),            
 @OrderBy int,            
 @Arama varchar(100),            
 @IslemTuru varchar(25),              
 @GosterilecekKayit int,  
 @IslemNoLike bit = 0,
 @Departman varchar(20)
AS            
BEGIN            
 DECLARE @SQLSTR VARCHAR(2000)            
 declare @rc int               
 declare @tmp table (trc int)             
            
 SET @SQLSTR = 'SELECT count(*) from (SELECT ISLEMNO FROM BANKA WHERE TARIH >='''+ @Tarih1+''' AND TARIH <= '''+@Tarih2+'''';              
            
         
 SET @SQLSTR = @SQLSTR + ' AND (Departman >= '+@dep1+' AND Departman <= '+@dep2+') ';            
          
 IF @IslemTuru <> 'Tümü'             
  SET @SQLSTR = @SQLSTR + ' AND BAACIKLA = '''+@IslemTuru+'''';       

 IF @Departman <> ''             
  SET @SQLSTR = @SQLSTR + ' AND Departman = '''+@Departman+'''';     
    
if @arama <> ''   
begin        
 if @orderby=0   
 begin  
  if @IslemNoLike = 0  
  set @SQLSTR=@SQLSTR+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' '   
  else  
  set @SQLSTR=@SQLSTR+'and ISLEMNO like ''%'+@arama+'%'' '   
 end;  
           
  IF @orderby = 1            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(BANKADI) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 2            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(HESAPNO) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 3            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(ACIKLAMA) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 4            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(TARIH) LIKE UPPER(''%'+@arama+'%'') ';     
end;  
    
 SET @SQLSTR = @SQLSTR + ')Y';             
            
  INSERT @tmp exec(@SQLSTR)              
 select @rc = trc from @tmp;            
            
 SET @SQLSTR = 'SELECT * FROM BANKA' +            
      ' WHERE ISLEMNO IN (SELECT X.ISLEMNO FROM (SELECT ROW_NUMBER() OVER (ORDER BY TARIH) AS SIRA,ISLEMNO '+            
      ' FROM BANKA WHERE TARIH >='''+@Tarih1+''' and TARIH <= '''+@Tarih2+'''';              
            
         
 SET @SQLSTR = @SQLSTR + ' AND (Departman >= '+@dep1+' AND Departman <= '+@dep2+') ';            
            
 IF @IslemTuru <> 'Tümü'             
  SET @SQLSTR = @SQLSTR + ' and BAACIKLA = '''+@IslemTuru+'''';     
  
 IF @Departman <> ''             
  SET @SQLSTR = @SQLSTR + ' AND Departman = '''+@Departman+'''';     
            
if @arama <> ''   
begin        
 if @orderby=0   
 begin  
  if @IslemNoLike = 0  
  set @SQLSTR=@SQLSTR+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' '   
  else  
  set @SQLSTR=@SQLSTR+'and ISLEMNO like ''%'+@arama+'%'' '   
 end;   
   
  IF @orderby = 1            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(BANKADI) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 2            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(HESAPNO) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 3            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(ACIKLAMA) LIKE UPPER(''%'+@arama+'%'') ';            
  IF @orderby = 4            
   SET @SQLSTR = @SQLSTR + ' AND UPPER(TARIH) LIKE UPPER(''%'+@arama+'%'') ';    
end;    
 SET @SQLSTR = @SQLSTR + ')X WHERE X.SIRA>'+convert(varchar,@rc - @GosterilecekKayit) +') ';              
            
SET @SQLSTR = @SQLSTR + ' and BORC+ALACAK > 0.1 ';        
SET @SQLSTR = @SQLSTR + ' and TARIH >='''+@Tarih1+''' and TARIH <= '''+@Tarih2+'''';       
      
SET @SQLSTR = @SQLSTR + 'ORDER BY ';      
      
      
 IF @orderby = 0            
  SET @SQLSTR = @SQLSTR + ' ISLEMNO';            
 IF @orderby = 1            
  SET @SQLSTR = @SQLSTR + ' BANKADI';            
 IF @orderby = 2            
  SET @SQLSTR = @SQLSTR + ' HESAPNO';            
 IF @orderby = 3            
  SET @SQLSTR = @SQLSTR + ' ACIKLAMA';            
 IF @orderby = 4            
  SET @SQLSTR = @SQLSTR + ' TARIH';            
             
 EXEC(@SQLSTR)            
            
END; 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokFiyat_FiyatH_Fiyat')
drop proc sp_StokFiyat_FiyatH_Fiyat
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE   PROCEDURE sp_StokFiyat_FiyatH_Fiyat
AS    
begin    
 declare @i int,@sorgu nvarchar(max),@cnt nvarchar(2)    
 set @i = 1;    
    while @i <= 20     
 begin    
   SET @cnt = CAST(@i as varchar(2));    
   SET @sorgu = ' UPDATE StokFiyat SET  F'+@cnt+'Fiyat = CASE WHEN F'+@cnt+'Kdv = ''H'' THEN F'+@cnt+'FiyatH ELSE (F'+@cnt+'FiyatH*(100 + SatisKdv)/100) END';    
   -- print @sorgu    
   EXEC sp_executesql @sorgu;    
   SET @i += 1;    
    end;    
    
 update sk SET sk.F1Fiyat = sf.F1Fiyat,    
      sk.F2Fiyat = sf.F2Fiyat,    
      sk.F3Fiyat = sf.F3Fiyat,    
      sk.F4Fiyat = sf.F4Fiyat    
    from STOKKARTI sk,StokFiyat sf where sk.Kodu = sf.Kodu    
end;

      				   
   				   
 			   
		 
      				   
  	
	  
		 
  				 
				
    
		  
   
         
   
 			 
				   
			
	
     
 			  
 			  
 			 
     
	
go--
-------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokFiyatBakim')
drop proc sp_StokFiyatBakim
go--
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROC sp_StokFiyatBakim     
as    
--Update STOKHAR SET Iskonto1 = 0 where Iskonto1 IS NULL    
declare @1 nvarchar(max)     
declare @2 nvarchar (max)    
declare @i int     
declare @s int     
set @i=1     
set @s=10    
WHILE @i <= @s    
begin    

set @1 ='Update STOKHAR SET Iskonto' + convert(varchar,@i)+'= 0 where Iskonto' + convert(varchar,@i)+' IS NULL'    
exec (@1)    
SET @i = @i + 1    
end    
---    
Update STOKHAR SET IndirimTutari = 0 where IndirimTutari IS NULL    
Update STOKHAR SET EkIskTutTop = 0 where EkIskTutTop IS NULL    
Update STOKHAR SET IndirimTutari = 0 where IndirimTutari IS NULL    
Update STOKHAR SET SatirIskTut = 0 where SatirIskTut IS NULL    
  
---EkIskTut1-3 - AltIskYuzde1-3 alanlarına update    
    
set @i=1     
set @s=3    
WHILE @i <= @s    
begin    
set @1 ='Update STOKHAR SET EkIskTut'+convert(varchar,@i)+' = 0 where EkIskTut'+convert(varchar,@i)+' IS NULL or EkIskTut'+convert(varchar,@i)+' = '''''    
set @2 ='Update STOKHAR SET AltIskYuzde'+convert(varchar,@i)+' = 0 where AltIskYuzde'+convert(varchar,@i)+' IS NULL or AltIskYuzde'+convert(varchar,@i)+' = '''''    
exec (@1)    
exec (@2)    
SET @i = @i + 1    
end    

IF( Select count(*) From DOVIZT where DovizKodu = 'YP') = 0
BEGIN
	INSERT INTO DOVIZT (DovizKodu, DovizAdi, GunFiyati,KULNO,KULADI,PerakendeGorunsunmu,AltBirim,IsoKodu)
 VALUES('YP','YP',1,1,'Admin','H',NULL,'TRY')
END
--- Update StokFiyat SET F1DovizKodu = 'YP' where F1DovizKodu IS NULL     
set @i=1     
set @s=20    
WHILE @i <= @s    
begin    
set @1 ='Update StokFiyat SET F'+ convert(varchar,@i)+'DovizKodu = ''YP'' where F'+ convert(varchar,@i)+'DovizKodu IS NULL or  F'+ convert(varchar,@i)+'DovizKodu = '''' or NOT EXISTS (select DovizKodu from DOVIZT where DovizKodu = F'+ convert(varchar,@i)+'DovizKodu) '    
exec (@1)    
SET @i = @i + 1    
end 


--- Update StokFiyat SET F1DovizKodu = 'YP' where F1DovizKodu IS NULL     
set @i=1     
set @s=20    
WHILE @i <= @s    
begin    
set @1 ='Update StokFiyat SET F'+ convert(varchar,@i)+'DovizKodu = ''YP'' where F'+ convert(varchar,@i)+'DovizKodu IS NULL or  F'+ convert(varchar,@i)+'DovizKodu = '''' or NOT EXISTS (select DovizKodu from DOVIZT where DovizKodu = F'+ convert(varchar,@i)+'DovizKodu) '    
exec (@1)    
SET @i = @i + 1    
end 
go--
-------------------------------------------------------------------------------------------------------------------------------------------
   			  
 		  
		 
      				   
         		
	  
			   
    
		  
       
  
			  
    
				  
     		
     			  
     			 
     			  
     			 
     				 
     				
      	 
      
     		
         
     			  
     		
     			
     		  
     				
     	   
       
			   
				
				 
			   
			   
		   
		
	
		  
      				   
if exists (select * from sys.procedures where name='sp_STOKHARDETBAG_Update')
drop proc sp_STOKHARDETBAG_Update
go--
      				   
create   procedure [dbo].[sp_STOKHARDETBAG_Update](                    
           @IslemNo int,@IslemTuru nvarchar(10),@CariNo int,@CariKodu nvarchar(20),                    
           @CariAdi nvarchar(50),@Tutar nvarchar(50),@SiparisTarihi smalldatetime,@KdvTutari nvarchar(50),                    
           @FisTuru nvarchar(1),@GC nvarchar(1),@EkIskTut1 nvarchar(50),@EkIskTut2 nvarchar(50),@EkIskTut3 nvarchar(50),                    
           @EkIskTutTop nvarchar(50),@FaturaNo int,@FaturaTarihi smalldatetime,@Satici nvarchar(10),                    
           @DovizTut nvarchar(50),@DovizCinsi smallint,@DovizKodu nvarchar(2),@DovizTutB nvarchar(50),                    
           @BelgeNo nvarchar(10),@OzelKod1 nvarchar(3),@OzelKod2 nvarchar(3),@OzelKod3 nvarchar(3),                    
           @IslemTipi nvarchar(30),@IrsNo nvarchar(13),@Ack1 nvarchar(200),@Ack2 nvarchar(200),                    
           @Ack3 nvarchar(200),@Ack4 nvarchar(200),@KULNO int,@KULADI nvarchar(20),@FatSerNo nvarchar(3),                    
           @SaticiAdi nvarchar(25),@AltIskYuzde1 nvarchar(50),@AltIskYuzde2 nvarchar(50),@AltIskYuzde3 nvarchar(50),                    
           @AK nvarchar(3),@FaturaOPlanGunu int,@Depo nvarchar(30),@GunAy nvarchar(3),@OpsiyonGunu int,                    
           @AcikKapali nvarchar(3),@DovizBorc nvarchar(50),@DovizAlacak nvarchar(50),@FYaz nvarchar(1),                    
           @SecimEH nvarchar(1),@IslemNo3 int,@TP nvarchar(2),@FisNo nvarchar(9),@VardiyaKodu nvarchar(15),                    
           @VardiyaDurumu nvarchar(9),@Departman nvarchar(8),@MasrafToplami nvarchar(50),@KDVHD char(1),                    
           @IslemNo4 int,@KayitDuzeyi int,@YazarKasaNo int,@YazarKasaFisNo nvarchar(10),                    
           @YaziciCount nvarchar(50),@IsIstasyonKodu nvarchar(30),@IsIstasyonAdi nvarchar(30),                    
           @TevkifatTutari nvarchar(50),@VardiyaNo nvarchar(8),@OdemeBilgisi nvarchar(100),@FaturaAdresNo int,                    
           @SevkAdresNo int,@FI nvarchar(1),@EF_ISTISNA int,@FaturaTipi int,@DovizKuru float = 1,@eFaturaNo nvarchar(25) = '')                    
AS                    
             
          
declare @TutarYeni float                  
declare @KdvTutariYeni float                  
declare @DovizTutYeni float                  
declare @MUSNO int                  
declare @MUSKOD nvarchar(20)                
declare @MUSADI nvarchar(50)                       
declare @DovizBorcYeni float                  
declare @DovizAlacakYeni float       
    
declare @YeniTevkifatTut float    
    
SET @YeniTevkifatTut = ISNULL((Select SUM(TevkifatTutari) from Tevkifat t where t.IslemNo = @IslemNo),0)    
                
 print(@FisTuru)                
 if (@FisTuru='I')                
 begin                
 print('Irsaliye')                
 set @TutarYeni=(select sum(SonTutar) - @YeniTevkifatTut from STOKHAR where STOKHAR.IslemNo=@IslemNo  )                  
 set @KdvTutariYeni=(select sum(KdvTutari) from STOKHAR where STOKHAR.IslemNo=@IslemNo )                  
 set @DovizTutYeni=( @TutarYeni/ convert(float,@DovizKuru) )                  
 end           
 else                
 begin                
 print('diğer')                
 set @TutarYeni=(select sum(SonTutar) - @YeniTevkifatTut from STOKHAR where STOKHAR.IslemNo=@IslemNo  or STOKHAR.IslemNo3=@IslemNo)                  
 set @KdvTutariYeni=(select sum(KdvTutari) from STOKHAR where STOKHAR.IslemNo=@IslemNo  or STOKHAR.IslemNo3=@IslemNo)                  
 set @DovizTutYeni=( @TutarYeni/ convert(float,@DovizKuru) )                  
 end                          
          
 if(@IslemTuru='DepoVirman')                
 begin                
 set @TutarYeni =@TutarYeni/2                
 set @KdvTutariYeni=@KdvTutariYeni/2                
 set @DovizTutYeni=@DovizTutYeni/2                      
 end                
              
                 
IF (select COUNT(*) from (select Count(CariKodu) c from STOKHAR where STOKHAR.IslemNo=@IslemNo  or STOKHAR.IslemNo3=@IslemNo GROUP BY CariKodu) t) > 1              
BEGIN       
 RAISERROR ('Kayıtlarda cari farkı vardır. Kontrol ediniz..!',10,1)          
 RETURN 0          
END              
          
select @MUSNO=MUSNO,@MUSKOD=MUSKOD,@MUSADI=MUSADI from AMUSTERI where MUSKOD in (select CariKodu from STOKHAR where STOKHAR.IslemNo=@IslemNo  or STOKHAR.IslemNo3=@IslemNo)                    
          
UPDATE    STOKHARDETBAG                    
SET              IslemTuru = @IslemTuru, CariNo = @MUSNO, CariKodu = @MUSKOD, CariAdi = @MUSADI,                     
Tutar =  @TutarYeni , SiparisTarihi = @SiparisTarihi, KdvTutari = @KdvTutariYeni,                     
FisTuru = @FisTuru, GC = @GC, EkIskTut1 = convert(float,@EkIskTut1), EkIskTut2 = convert(float,@EkIskTut2),                     
EkIskTut3 = convert(float,@EkIskTut3),                     
EkIskTutTop = convert(float,@EkIskTutTop),                     
FaturaNo = @FaturaNo, FaturaTarihi = @FaturaTarihi, Satici = @Satici, DovizTut = @DovizTutYeni,                     
DovizCinsi = @DovizCinsi, DovizKodu = @DovizKodu, DovizTutB =@DovizTutYeni/* convert(float,@DovizTutB)*/,                    
BelgeNo = @BelgeNo, OzelKod1 = @OzelKod1, OzelKod2 = @OzelKod2, OzelKod3 = @OzelKod3, IslemTipi = @IslemTipi, IrsNo = @IrsNo,                     
Ack1 = @Ack1, Ack2 = @Ack2, Ack3 = @Ack3, Ack4 = @Ack4,                     
KULNO = @KULNO, KULADI = @KULADI, FatSerNo = @FatSerNo, SaticiAdi = @SaticiAdi,                     
AltIskYuzde1 = convert(float,@AltIskYuzde1), AltIskYuzde2 = convert(float,@AltIskYuzde2), AltIskYuzde3 = convert(float,@AltIskYuzde3),                    
AK = @AK, FaturaOPlanGunu = @FaturaOPlanGunu, Depo = @Depo,                     
GunAy = @GunAy, OpsiyonGunu = @OpsiyonGunu, AcikKapali = @AcikKapali,                     
DovizBorc = convert(float,@DovizBorc), DovizAlacak = convert(float,@DovizAlacak), FYaz = @FYaz, SecimEH = @SecimEH,                     
IslemNo3 = @IslemNo3, TP = @TP, FisNo = @FisNo, VardiyaKodu = @VardiyaKodu, VardiyaDurumu = @VardiyaDurumu,                     
Departman = @Departman,MasrafToplami = convert(float,@MasrafToplami), KDVHD = @KDVHD, IslemNo4 = @IslemNo4, KayitDuzeyi = @KayitDuzeyi, YazarKasaNo = @YazarKasaNo, YazarKasaFisNo = @YazarKasaFisNo, YaziciCount = convert(float,@YaziciCount),               
  
     
IsIstasyonKodu = @IsIstasyonKodu, IsIstasyonAdi = @IsIstasyonAdi,                    
TevkifatTutari =convert(FLOAT,@TevkifatTutari ),     
VardiyaNo = @VardiyaNo, OdemeBilgisi = @OdemeBilgisi, FaturaAdresNo = @FaturaAdresNo, SevkAdresNo = @SevkAdresNo, FI = @FI,                    
EF_ISTISNA=@EF_IsTISNA,FaturaTipi=@FaturaTipi,Update_Date = GETDATE() ,      
DovizKur = @DovizKuru ,eFaturaNo =  @eFaturaNo            
where IslemNo =@IslemNo           
          
DECLARE @CariSayisi int          
          
SET @CariSayisi = ( SELECT   COUNT(*) FROM STOKHAR S,STOKHARDETBAG B          
      WHERE (S.IslemNo = B.IslemNo or S.IslemNo3 = B.IslemNo)          
        AND B.IslemNo = @IslemNo          
        AND (S.CariNo <> B.CariNo or S.CariKodu <> B.CariKodu))          
          
IF @CariSayisi > 0              
BEGIN          
 RAISERROR ('Cari hatası. Kontrol ediniz..!',10,1)          
 RETURN 0          
END 

go--

if exists (select * from sys.procedures where name='sp_STOKHAR_Aktar')
drop proc sp_STOKHAR_Aktar
go--

CREATE PROCEDURE [dbo].[sp_STOKHAR_Aktar]
(
    @Hedef	SYSNAME,
	@Kaynak	SYSNAME,
	@IslemNo    nvarchar(max)
)
AS 
BEGIN
	SET NOCOUNT ON;

	-- Gerçekten bu veritabanları sistemde var mıdır, buna bakıyoruz. sqlinjection da engellenmiş oluyor.
	SELECT @Hedef = name
	FROM sys.databases
	WHERE name = @Hedef;

	SELECT @Kaynak = name
	FROM sys.databases
	WHERE name = @Kaynak;

	DECLARE @Query NVARCHAR(MAX);

	SET @Query = 'DELETE FROM '+@Hedef+'.dbo.STOKHAR WHERE IslemNo in (Select IslemNo from '+@Kaynak+'.dbo.STOKHAR where IslemNo in ('+@IslemNo+'))';
    EXEC sp_executesql @Query;

	SET @Query = 'INSERT INTO ' + @Hedef + '.dbo.[STOKHAR] (IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, StokNo, StokKodu, StokAdi, Miktar, Birim, BirimMiktar, BirimFiyat, 
 Iskonto1, Iskonto2, Iskonto3, Iskonto4, Tutar, SiparisNo, SiparisTarihi, Kdv, KdvTutari, IndirimTutari, SatirIskTut, FisTuru, GC, EkIskTut1, EkIskTut2, 
 EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, TopTutar, KDVHD, DovizTut, DovizCinsi, DovizKodu, DovizTutB, Depo, AnaBirimFiyat, FaturaMiktari, 
 IslemNo2, OTVOrani, OTVTutari, KULNO, KULADI, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AcikKapali, DovizBorc, DovizAlacak, IslemNo3, IslemNo4, 
 TankKodu, TankAdi, FisNo, Plaka, TP, VardiyaKodu, TahsilatEH, km, Departman, SonTutar, SonTutarDov, SatMasrafTutari, SatIskTut1, SatIskTut2, SatIskTut3, 
 SatIskTut4, AnaBirim, OtvBirimTutari, IlkBirimFiyat, VadeFarkliBirimFiyat, LifoFiyat, FifoFiyat, NFiyat, NFiyatDovBr, StokTipi, VardiyaNo, Markasi, SiraNo, 
 Iskonto5, Iskonto6, Iskonto7, Iskonto8, Iskonto9, Iskonto10, SatIskTut5, SatIskTut6, SatIskTut7, SatIskTut8, SatIskTut9, SatIskTut10, RBIslemNo, 
 StokAgirligi, KontrolBarkodu, HatKodu, LotNo, StokBolge, StokRaf, StokYer, BurutMiktar, FireOrani, FireMiktari, DovizKuru)
		SELECT IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, StokNo, StokKodu, StokAdi, Miktar, Birim, BirimMiktar, BirimFiyat, Iskonto1, Iskonto2, Iskonto3, Iskonto4, Tutar, SiparisNo, SiparisTarihi, Kdv, KdvTutari, IndirimTutari, SatirIskTut, FisTuru, GC, EkIskTut1, EkIskTut2, EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, TopTutar, KDVHD, DovizTut, DovizCinsi, DovizKodu, DovizTutB, Depo, AnaBirimFiyat, FaturaMiktari, IslemNo2, OTVOrani, OTVTutari, KULNO, KULADI, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AcikKapali, DovizBorc, DovizAlacak, IslemNo3, IslemNo4, TankKodu, TankAdi, FisNo, Plaka, TP, VardiyaKodu, TahsilatEH, km, Departman, SonTutar, SonTutarDov, SatMasrafTutari, SatIskTut1, SatIskTut2, SatIskTut3, SatIskTut4, AnaBirim, OtvBirimTutari, IlkBirimFiyat, VadeFarkliBirimFiyat, LifoFiyat, FifoFiyat, NFiyat, NFiyatDovBr, StokTipi, VardiyaNo, Markasi, SiraNo, Iskonto5, Iskonto6, Iskonto7, Iskonto8, Iskonto9, Iskonto10, SatIskTut5, SatIskTut6, SatIskTut7, SatIskTut8, SatIskTut9, SatIskTut10, RBIslemNo, StokAgirligi, KontrolBarkodu, HatKodu, LotNo, StokBolge, StokRaf, StokYer, BurutMiktar, FireOrani, FireMiktari, DovizKuru 
 FROM ' + @Kaynak + '.dbo.[STOKHAR] S WHERE S.IslemNo in ('+@IslemNo+')';

	--SELECT @Query Sorgu; 	
	--PRINT @Query;
	EXEC sp_executesql @Query;
	--EXEC ( @Query )

END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spStokHarDetBagKaydet')
drop proc spStokHarDetBagKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create    PROCEDURE [dbo].[spStokHarDetBagKaydet]            
@IslemNo int,           
@IslemTuru nvarchar(10),           
@CariNo int,           
@CariKodu nvarchar(20),           
@CariAdi nvarchar(50),           
@Tutar float,           
@SiparisTarihi datetime,           
@KdvTutari float,           
@FisTuru nvarchar(1),           
@GC nvarchar(1),           
@EkIskTut1 float,           
@EkIskTut2 float,           
@EkIskTut3 float,           
@EkIskTutTop float,           
@FaturaNo int,           
@FaturaTarihi datetime,           
@Satici nvarchar(10),           
@DovizTut float,           
@DovizCinsi smallint,           
@DovizKodu nvarchar(2),           
@DovizTutB float,           
@BelgeNo nvarchar(10),           
@OzelKod1 nvarchar(3),           
@OzelKod2 nvarchar(3),           
@OzelKod3 nvarchar(3),           
@IslemTipi nvarchar(30),           
@IrsNo nvarchar(13),           
@Ack1 nvarchar(200),           
@Ack2 nvarchar(200),           
@Ack3 nvarchar(200),           
@Ack4 nvarchar(200),           
@KULNO int,           
@KULADI nvarchar(20),           
@FatSerNo varchar(3),           
@SaticiAdi nvarchar(25),           
@AltIskYuzde1 float,           
@AltIskYuzde2 float,           
@AltIskYuzde3 float,           
@AK nvarchar(3),           
@FaturaOPlanGunu int,           
@Depo nvarchar(30),           
@GunAy nvarchar(3),           
@OpsiyonGunu int,           
@AcikKapali nvarchar(3),           
@DovizBorc float,           
@DovizAlacak float,           
@FYaz nvarchar(1),           
@SecimEH nvarchar(1),           
@IslemNo3 int,           
@TP nvarchar(2),           
@FisNo nvarchar(9),           
@VardiyaKodu nvarchar(15),           
@VardiyaDurumu nvarchar(9),           
@Departman nvarchar(8),           
@MasrafToplami float,           
@KDVDH char,           
@IslemNo4 int,           
@KayitDuzeyi int,           
@YazarKasaNo int,           
@YazarKasaFisNo nvarchar(10),           
@YaziciCount float,           
@IsIstasyonKodu nvarchar(30),           
@IsIstasyonAdi nvarchar(30),                
@TevkifatTutari float,           
@VardiyaNo nvarchar(8),           
@OdemeBilgisi nvarchar(100),           
@FaturaAdresNo int,           
@SevkAdresNo int,           
@FI nvarchar(1),           
@SevkTarihi dateTime,           
@eFaturaNo nvarchar(30),           
@UUID nVarChar(50),           
@eFaturaGonderimTarihi DateTime,           
@eFaturaDurumu nVarChar(20) ,              
@FaturaTipi int,        
@DovizKuru float = 1        
           
 AS           
 begin           
 declare @Istisna int;          
 declare @HesaplananTutar float          
 declare @HesaplananKdvTutari float          
           
            
 if (@FisTuru='I')           
 begin           
 set @HesaplananTutar = (select sum(SonTutar) from STOKHAR where CariNo=@CariNo and (IslemNo=@IslemNo))          
 set @HesaplananKdvTutari = (select sum(KdvTutari) from STOKHAR where CariNo=@CariNo and (IslemNo=@IslemNo) )          
 end else           
 begin           
 set @HesaplananTutar = (select sum(SonTutar) from STOKHAR where CariNo=@CariNo and (IslemNo3=@IslemNo or IslemNo=@IslemNo) )          
 set @HesaplananKdvTutari = (select sum(KdvTutari) from STOKHAR where CariNo=@CariNo and (IslemNo3=@IslemNo or IslemNo=@IslemNo) )          
    
 end           
           
           
 if(@IslemTuru='DepoVirman')           
 begin           
 set @HesaplananTutar =@HesaplananTutar/2           
 set @HesaplananKdvTutari=@HesaplananKdvTutari/2           
           
 end           
 set @Istisna=(select convert(int,(SELECT top 1 v.MuafiyetKodu FROM STOKHAR sh join StokVergiMuafiyetleri v on v.StokKodu=sh.StokKodu WHERE IslemNo=@IslemNo and Kdv=0)))          
           
select @TevkifatTutari =     sum(t.TevkifatTutari) from Tevkifat t where t.IslemNo in(select distinct IslemNo from STOKHAR where  @IslemNo in(IslemNo,IslemNo3))   
    
if @TevkifatTutari > 0    
begin    
  
 set @HesaplananTutar = @HesaplananTutar - @TevkifatTutari    
end    
    
 INSERT STOKHARDETBAG (IslemNo, IslemTuru, CariNo, CariKodu, CariAdi, Tutar, SiparisTarihi, KdvTutari, FisTuru, GC, EkIskTut1, EkIskTut2, EkIskTut3, EkIskTutTop, FaturaNo, FaturaTarihi, Satici, DovizTut, DovizCinsi, DovizKodu, DovizTutB, BelgeNo,         
  
        OzelKod1, OzelKod2, OzelKod3, IslemTipi, IrsNo, Ack1, Ack2, Ack3, Ack4, KULNO, KULADI, FatSerNo, SaticiAdi, AltIskYuzde1, AltIskYuzde2, AltIskYuzde3, AK, FaturaOPlanGunu, Depo, GunAy, OpsiyonGunu, AcikKapali,           
        DovizBorc, DovizAlacak, FYaz, SecimEH, IslemNo3, TP, FisNo, VardiyaKodu, VardiyaDurumu, Departman, MasrafToplami, KDVHD, IslemNo4, KayitDuzeyi, YazarKasaNo, YazarKasaFisNo, YaziciCount, IsIstasyonKodu,           
        IsIstasyonAdi, VardiyaNo, OdemeBilgisi, FaturaAdresNo, SevkAdresNo, FI, SevkTarihi, eFaturaNo, UUID, eFaturaGonderimTarihi,      
  eFaturaDurumu, EF_ISTISNA, FaturaTipi,           
        Entegrator, Update_Date,DovizKur,TevkifatTutari)           
 VALUES     (@IslemNo, @IslemTuru,@CariNo,@CariKodu,@CariAdi,@HesaplananTutar ,           
       @SiparisTarihi,@HesaplananKdvTutari ,@FisTuru, @GC, @EkIskTut1, @EkIskTut2, @EkIskTut3,@EkIskTutTop, @FaturaNo, @FaturaTarihi,  @Satici,  @DovizTut,  @DovizCinsi,  @DovizKodu,  @DovizTutB,  @BelgeNo,    @OzelKod1,            
       @OzelKod2,  @OzelKod3,  @IslemTipi,  @IrsNo,  @Ack1,  @Ack2,  @Ack3,  @Ack4,  @KULNO,  @KULADI,  @FatSerNo,  @SaticiAdi, @AltIskYuzde1,           
       @AltIskYuzde2,  @AltIskYuzde3,  @AK,  @FaturaOPlanGunu,  @Depo, @GunAy,  @OpsiyonGunu,  @AcikKapali,   @DovizBorc,  @DovizAlacak,  @FYaz,           
       @SecimEH,  @IslemNo3,  @TP,  @FisNo,  @VardiyaKodu,  @VardiyaDurumu, @Departman,  @MasrafToplami,  @KDVDH,  @IslemNo4,           
       @KayitDuzeyi,  @YazarKasaNo,  @YazarKasaFisNo,  @YaziciCount, @IsIstasyonKodu,   @IsIstasyonAdi,            
       @VardiyaNo,  @OdemeBilgisi,  @FaturaAdresNo,  @SevkAdresNo,  @FI,  @SevkTarihi,  @eFaturaNo,  @UUID,  @eFaturaGonderimTarihi,            
       @eFaturaDurumu ,  @Istisna,  @FaturaTipi,  0,GETDATE(),@DovizKuru, @TevkifatTutari     
)          
          
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spTeklifHarKaydet')
drop proc spTeklifHarKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  CREATE  PROCEDURE [dbo].[spTeklifHarKaydet]       
     @IslemNo int,      
     @IslemTuru nvarchar(10),      
     @CariNo int,      
     @CariKodu nvarchar(20),      
     @CariAdi nvarchar(50),      
     @StokNo int,      
     @StokKodu nvarchar(30),      
     @StokAdi nvarchar(500),      
     @Miktar float,      
     @FaturaMiktari float,      
     @Birim nvarchar(10),      
     @BirimFiyat float,      
     @BirimMiktar float,      
     @AnaBirim nvarchar(10),      
     @AnaBirimFiyat float,      
     @Iskonto1 float,      
     @Iskonto2 float,      
     @Iskonto3 float,      
     @Iskonto4 float,      
     @SatIskTut1 float,      
     @SatIskTut2 float,      
     @SatIskTut3 float,      
     @SatIskTut4 float,      
     @SatIskTut float,      
     @IndirimTutari float,      
     @AltIskYuzde1 float,      
     @AltIskYuzde2 float,      
     @AltIskYuzde3 float,      
     @EkIskTut1 float,      
     @EkIskTut2 float,      
     @EkIskTut3 float,      
     @EkIskTutTop float,      
     @Tutar float,      
     @TeklifNo int,      
     @TeklifTarihi smalldatetime,      
     @Kdv float,      
     @KdvTutari float,      
     @FisTuru nvarchar(1),      
     @GC nvarchar(1),      
     @Satici nvarchar(10),      
     @SaticiAdi nvarchar(25),      
     @TopTutar Float,      
     @KDVHD nvarchar(1),      
     @DovizTut float,      
     @DovizCinsi int,      
     @DovizKodu nvarchar(2),      
     @DovizTutB float,      
     @Depo nvarchar(30),      
     @OTVOrani float,      
     @OTVTutari float,      
     @IslemNo2 int,      
     @IslemNo3 int,      
     @IslemNo4 int,      
     @VardiyaNo nvarchar(8),      
     @VardiyaKodu nvarchar(15),      
     @Departman nvarchar(8),      
     @SonTutar float,      
     @SonTutarDov float,      
     @SatMasrafTutari float,      
     @OtvBirimTutari float,      
     @LifoFiyat float,      
     @FifoFiyat float,      
     @NFiyat float,      
     @NFiyatDovBr float,      
     @StokTipi nvarchar(15),      
     @Markasi nvarchar(30),      
     @SiraNo int,      
     @KULNO int,      
     @KULADI nvarchar(20),      
     @TedarikGun int,  
  @DovizKuru float = 1  
AS       
INSERT TEKLIFHAR VALUES (@IslemNo,      
     @IslemTuru,      
     @CariNo,      
     @CariKodu,      
     @CariAdi,      
     @StokNo,      
     @StokKodu,      
     @StokAdi,      
     @Miktar,      
     @FaturaMiktari,      
     @Birim,      
     @BirimFiyat,      
     @BirimMiktar,      
     @AnaBirim,      
     @AnaBirimFiyat,      
     @Iskonto1,      
     @Iskonto2,      
     @Iskonto3,      
     @Iskonto4,      
     @SatIskTut1,      
     @SatIskTut2,      
     @SatIskTut3,      
     @SatIskTut4,      
     @SatIskTut,      
     @IndirimTutari,      
     @AltIskYuzde1,      
     @AltIskYuzde2,      
     @AltIskYuzde3,      
     @EkIskTut1,      
     @EkIskTut2,      
     @EkIskTut3,      
     @EkIskTutTop,      
     @Tutar,      
     @TeklifNo,      
     @TeklifTarihi,      
     @Kdv,      
     @KdvTutari,      
     @FisTuru,      
     @GC,      
     @Satici,      
     @SaticiAdi,      
     @TopTutar,      
     @KDVHD,      
     @DovizTut,      
     @DovizCinsi,      
     @DovizKodu,      
     @DovizTutB,      
     @Depo,      
     @OTVOrani,      
     @OTVTutari,      
     @IslemNo2,      
     @IslemNo3,      
     @IslemNo4,      
     @VardiyaNo,      
     @VardiyaKodu,      
     @Departman,      
     @SonTutar,      
     @SonTutarDov,      
     @SatMasrafTutari,      
     @OtvBirimTutari,      
     @LifoFiyat,      
     @FifoFiyat,      
     @NFiyat,      
     @NFiyatDovBr,      
     @StokTipi,      
     @Markasi,      
     @SiraNo,      
     @KULNO,      
     @KULADI,      
     @TedarikGun,  
  @DovizKuru) 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spTeklifHarDetBagKaydet')
drop proc spTeklifHarDetBagKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spTeklifHarDetBagKaydet]       
     @IslemNo int,      
     @TeklifNo int,      
     @IslemTuru nvarchar(10),      
     @CariNo int,      
     @CariKodu nvarchar(20),      
     @CariAdi nvarchar(50),      
     @KdvTutari float,      
     @Tutar float,      
     @TeklifTarihi smalldatetime,      
     @TeklifGecerlilikSuresi smalldatetime,      
     @SipariseAktarimTarihi smalldatetime,      
     @FisTuru nvarchar(1),      
     @GC nvarchar(1),      
     @EkIskTut1 float,      
     @EkIskTut2 float,      
     @EkIskTut3 float,      
     @EkIskTutTop float,      
     @AltIskYuzde1 float,      
     @AltIskYuzde2 float,      
     @AltIskYuzde3 float,      
     @MasrafToplami float,      
     @KDVHD char(1),      
     @SaticiKodu nvarchar(10),      
     @SaticiAdi nvarchar(25),      
     @DovizTut float,      
     @DovizCinsi smallint,      
     @DovizKodu nvarchar(2),      
     @BelgeNo nvarchar(10),      
     @OzelKod1 nvarchar(15),      
     @OzelKod2 nvarchar(15),      
     @OzelKod3 nvarchar(15),      
     @IslemTipi nvarchar(30),      
     @Ack1 nvarchar(100),      
     @Ack2 nvarchar(100),      
     @Ack3 nvarchar(100),      
     @Ack4 nvarchar(100),      
     @FaturaOPlanGunu int,      
     @Depo nvarchar(30),      
     @GunAy nvarchar(3),      
     @OpsiyonGunu int,      
     @AcikKapali nvarchar(3),      
     @FYaz nvarchar(1),      
     @YaziciCount float,      
     @SecimEH nvarchar(1),      
     @IslemNo2 int,      
     @IslemNo3 int,      
     @IslemNo4 int,      
     @VardiyaNo nvarchar(8),      
     @VardiyaKodu nvarchar(15),      
     @VardiyaDurumu nvarchar(8),      
     @Departman nvarchar(8),      
     @IsIstasyonKodu nvarchar(30),      
     @IsIstasyonAdi nvarchar(30),      
     @OdemeBilgisi nvarchar(100),      
     @FaturaAdresNo int,      
     @SevkAdresNo int,      
     @TeklifDurumu nvarchar(30),      
     @Durumu nvarchar(30),      
     @UstAlt nvarchar(1),      
     @TeklifYazisi nvarchar(2000),      
     @KULNO int,      
     @KULADI nvarchar(20),  
  @DovizKuru float  = 1  
AS       
INSERT TEKLIFHARDETBAG VALUES (@IslemNo,      
     @TeklifNo,      
     @IslemTuru,      
     @CariNo,      
     @CariKodu,      
     @CariAdi,      
     @KdvTutari,      
     @Tutar,      
     @TeklifTarihi,      
     @TeklifGecerlilikSuresi,      
     @SipariseAktarimTarihi,      
     @FisTuru,      
     @GC,      
     @EkIskTut1,      
     @EkIskTut2,      
     @EkIskTut3,      
     @EkIskTutTop,      
     @AltIskYuzde1,      
     @AltIskYuzde2,      
     @AltIskYuzde3,      
     @MasrafToplami,      
     @KDVHD,      
     @SaticiKodu,      
     @SaticiAdi,      
     @DovizTut,      
     @DovizCinsi,      
     @DovizKodu,      
     @BelgeNo,      
     @OzelKod1,      
     @OzelKod2,      
     @OzelKod3,      
     @IslemTipi,      
     @Ack1,      
     @Ack2,      
     @Ack3,      
     @Ack4,      
     @FaturaOPlanGunu,      
     @Depo,      
     @GunAy,      
     @OpsiyonGunu,      
     @AcikKapali,      
     @FYaz,      
     @YaziciCount,      
     @SecimEH,      
     @IslemNo2,      
     @IslemNo3,      
     @IslemNo4,      
     @VardiyaNo,      
     @VardiyaKodu,      
     @VardiyaDurumu,      
     @Departman,      
     @IsIstasyonKodu,      
     @IsIstasyonAdi,      
     @OdemeBilgisi,      
     @FaturaAdresNo,      
     @SevkAdresNo,      
     @TeklifDurumu,      
     @Durumu,      
     @UstAlt,      
     @TeklifYazisi,      
     @KULNO,      
     @KULADI,  
  @DovizKuru)
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spSiparisHarDetBagKaydet')
drop proc spSiparisHarDetBagKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 create    PROCEDURE [dbo].[spSiparisHarDetBagKaydet]       
     @IslemNo int,      
     @SiparisNo int,      
     @SiparisNumarasi int,      
     @IslemTuru nvarchar(10),      
     @CariNo int,      
     @CariKodu nvarchar(20),      
     @CariAdi nvarchar(50),      
     @KdvTutari float,      
     @Tutar float,      
     @SiparisTarihi smalldatetime,      
     @SiparisGecerlilikSuresi smalldatetime,      
     @TeslimTarihi smalldatetime,      
     @FisTuru nvarchar(1),      
     @GC nvarchar(1),      
     @EkIskTut1 float,      
     @EkIskTut2 float,      
     @EkIskTut3 float,      
     @EkIskTutTop float,      
     @AltIskYuzde1 float,      
     @AltIskYuzde2 float,      
     @AltIskYuzde3 float,      
     @MasrafToplami float,      
     @KDVHD char(1),      
     @SaticiKodu nvarchar(10),      
     @SaticiAdi nvarchar(25),      
     @DovizTut float,      
     @DovizCinsi smallint,      
     @DovizKodu nvarchar(2),      
     @BelgeNo nvarchar(10),      
     @OzelKod1 nvarchar(15),      
     @OzelKod2 nvarchar(15),      
     @OzelKod3 nvarchar(15),      
     @IslemTipi nvarchar(30),      
     @Ack1 nvarchar(100),      
     @Ack2 nvarchar(100),      
     @Ack3 nvarchar(100),      
     @Ack4 nvarchar(100),      
     @FaturaOPlanGunu int,      
     @Depo nvarchar(30),      
     @GunAy nvarchar(3),      
     @OpsiyonGunu int,      
     @AcikKapali nvarchar(3),      
     @FYaz nvarchar(1),      
     @YaziciCount float,      
     @SecimEH nvarchar(1),      
     @IslemNo2 int,      
     @IslemNo3 int,      
     @IslemNo4 int,      
     @VardiyaNo nvarchar(8),      
     @VardiyaKodu nvarchar(15),      
     @VardiyaDurumu nvarchar(8),      
     @Departman nvarchar(8),      
     @IsIstasyonKodu nvarchar(30),      
     @IsIstasyonAdi nvarchar(30),      
     @OdemeBilgisi nvarchar(100),      
     @FaturaAdresNo int,      
     @SevkAdresNo int,      
     @SiparisDurumu nvarchar(30),      
     @Durumu nvarchar(30),      
     @UstAlt nvarchar(1),      
     @SiparisYazisi nvarchar(2000),      
     @Kalan float,      
     @KULNO int,      
     @KULADI nvarchar(20),      
     @Update_Date smalldateTime,      
     @ToplamMiktar float,  
  @DovizKuru float = 1  
AS       
INSERT SIPARISHARDETBAG VALUES (@IslemNo,      
     @SiparisNo,      
     @SiparisNumarasi,      
     @IslemTuru,      
     @CariNo,      
     @CariKodu,      
     @CariAdi,      
     @KdvTutari,      
     @Tutar,      
     @SiparisTarihi,      
     @SiparisGecerlilikSuresi,      
     @TeslimTarihi,      
     @FisTuru,      
     @GC,      
     @EkIskTut1,      
     @EkIskTut2,      
     @EkIskTut3,      
     @EkIskTutTop,      
     @AltIskYuzde1,      
     @AltIskYuzde2,      
     @AltIskYuzde3,      
     @MasrafToplami,      
     @KDVHD,      
     @SaticiKodu,      
     @SaticiAdi,      
     @DovizTut,      
     @DovizCinsi,      
     @DovizKodu,      
     @BelgeNo,      
     @OzelKod1,      
     @OzelKod2,      
     @OzelKod3,      
     @IslemTipi,      
     @Ack1,      
     @Ack2,      
     @Ack3,      
     @Ack4,      
     @FaturaOPlanGunu,      
     @Depo,      
     @GunAy,      
     @OpsiyonGunu,      
     @AcikKapali,      
     @FYaz,      
     @YaziciCount,      
     @SecimEH,      
     @IslemNo2,      
     @IslemNo3,      
     @IslemNo4,      
     @VardiyaNo,      
     @VardiyaKodu,      
     @VardiyaDurumu,      
     @Departman,      
     @IsIstasyonKodu,      
     @IsIstasyonAdi,      
     @OdemeBilgisi,      
     @FaturaAdresNo,      
     @SevkAdresNo,      
     @SiparisDurumu,      
     @Durumu,      
     @UstAlt,      
     @SiparisYazisi,      
     @Kalan,      
     @KULNO,      
     @KULADI,      
     @Update_Date,      
     @ToplamMiktar,  
  @DovizKuru) 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spSiparisHarKaydet')
drop proc spSiparisHarKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spSiparisHarKaydet]       
     @IslemNo int,      
     @IslemTuru nvarchar(10),      
     @CariNo int,      
     @CariKodu nvarchar(20),      
     @CariAdi nvarchar(50),      
     @StokNo int,      
     @StokKodu nvarchar(30),      
     @StokAdi nvarchar(500),      
     @Miktar float,      
     @FaturaMiktari float,      
     @Birim nvarchar(10),      
     @BirimFiyat float,      
     @BirimMiktar float,      
     @AnaBirim nvarchar(10),      
     @AnaBirimFiyat float,      
     @Iskonto1 float,      
     @Iskonto2 float,      
     @Iskonto3 float,      
     @Iskonto4 float,      
     @SatIskTut1 float,      
     @SatIskTut2 float,      
     @SatIskTut3 float,      
     @SatIskTut4 float,      
     @SatIskTut float,      
     @IndirimTutari float,      
     @AltIskYuzde1 float,      
     @AltIskYuzde2 float,      
     @AltIskYuzde3 float,      
     @EkIskTut1 float,      
     @EkIskTut2 float,      
     @EkIskTut3 float,      
     @EkIskTutTop float,      
     @Tutar float,      
     @SiparisNo int,      
     @SiparisDetayNumarasi nvarchar(3),      
     @SiparisTarihi smalldatetime,      
     @Kdv float,      
     @KdvTutari float,      
     @FisTuru nvarchar(1),      
     @GC nvarchar(1),      
     @Satici nvarchar(10),      
     @SaticiAdi nvarchar(25),      
     @TopTutar Float,      
     @KDVHD nvarchar(1),      
     @DovizTut float,      
     @DovizCinsi int,      
     @DovizKodu nvarchar(2),      
     @DovizTutB float,      
     @Depo nvarchar(30),      
     @OTVOrani float,      
     @OTVTutari float,      
     @IslemNo2 int,      
     @IslemNo3 int,      
     @IslemNo4 int,      
     @VardiyaNo nvarchar(8),      
     @VardiyaKodu nvarchar(15),      
     @Departman nvarchar(8),      
     @SonTutar float,      
     @SonTutarDov float,      
     @SatMasrafTutari float,      
     @OtvBirimTutari float,      
     @LifoFiyat float,      
     @FifoFiyat float,      
     @NFiyat float,      
     @NFiyatDovBr float,      
     @StokTipi nvarchar(15),      
     @Markasi nvarchar(30),      
     @SiraNo int,      
     @KalanMiktar float,      
     @KULNO int,      
     @KULADI nvarchar(20),      
     @Durumu nvarchar(8),      
     @UpdateKulNo int,      
     @UpdateKulAdi nvarchar(20),  
  @DovizKuru float = 1   
AS       
INSERT SIPARISHAR VALUES(@IslemNo,      
      @IslemTuru,      
      @CariNo,      
      @CariKodu,      
      @CariAdi,      
      @StokNo,      
      @StokKodu,      
      @StokAdi,      
      @Miktar,      
      @FaturaMiktari,      
      @Birim,      
      @BirimFiyat,      
      @BirimMiktar,      
      @AnaBirim,      
      @AnaBirimFiyat,      
      @Iskonto1,      
      @Iskonto2,      
      @Iskonto3,      
      @Iskonto4,      
      @SatIskTut1,      
      @SatIskTut2,      
      @SatIskTut3,      
      @SatIskTut4,      
      @SatIskTut,      
      @IndirimTutari,      
      @AltIskYuzde1,      
      @AltIskYuzde2,      
      @AltIskYuzde3,      
      @EkIskTut1,      
      @EkIskTut2,      
      @EkIskTut3,      
      @EkIskTutTop,      
      @Tutar,      
      @SiparisNo,      
      @SiparisDetayNumarasi,      
      @SiparisTarihi,      
      @Kdv,      
      @KdvTutari,      
      @FisTuru,      
      @GC,      
      @Satici,      
      @SaticiAdi,      
      @TopTutar,      
      @KDVHD,      
      @DovizTut,      
      @DovizCinsi,      
      @DovizKodu,      
      @DovizTutB,      
      @Depo,      
      @OTVOrani,      
      @OTVTutari,      
      @IslemNo2,      
      @IslemNo3,      
      @IslemNo4,      
      @VardiyaNo,      
      @VardiyaKodu,      
      @Departman,      
      @SonTutar,      
      @SonTutarDov,      
      @SatMasrafTutari,      
      @OtvBirimTutari,      
      @LifoFiyat,      
      @FifoFiyat,      
      @NFiyat,      
      @NFiyatDovBr,      
      @StokTipi,      
      @Markasi,      
      @SiraNo,      
      @KalanMiktar,      
      @KULNO,      
      @KULADI,      
     @Durumu,      
     @UpdateKulNo,      
     @UpdateKulAdi,  
  @DovizKuru)
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spOdemeSozuKaydet')
drop proc spOdemeSozuKaydet
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spOdemeSozuKaydet]     
      @AramaTarihi datetime,    
      @CariKodu nvarchar(20),    
      @CariAdi nvarchar(50),    
      @ToplamBorc float,    
      @ToplamAlacak float,    
      @ToplamBakiye float,    
      @TaksitBakiye float,    
      @OdeyecegiTarih datetime,    
      @Aciklama1 nvarchar(200),    
      @Aciklama2 nvarchar(200),      
      @OdeyecegiTutar float,    
      @AktifPasif nvarchar(1),    
      @KulNo int,    
      @KulAdi nvarchar(20),    
      @SaticiKodu nvarchar(10),    
      @SaticiAdi nvarchar(25),  
   @Adet int,  
   @IslemAraligi int,  
   @Aralik int  
  
AS     
BEGIN  
  declare @i int = 1;  
  declare @Tarih smalldatetime;  
  
  WHILE @i <= @Adet  
  BEGIN  
    IF @IslemAraligi = 0   
 BEGIN  
  SET @Tarih = DATEADD(month, ((@i - 1) * @Aralik) ,@OdeyecegiTarih)     
 END  
 ELSE  
 BEGIN  
  SET @Tarih = DATEADD(day,((@i - 1) * @Aralik) ,@OdeyecegiTarih)     
 END;  
  
 INSERT OdemeSozu VALUES(@AramaTarihi,    
    @CariKodu,    
    @CariAdi,    
    @ToplamBorc,    
    @ToplamAlacak,    
    @ToplamBakiye,    
    @TaksitBakiye,    
    @Tarih,    
    @Aciklama1,    
    @Aciklama2,    
    @OdeyecegiTutar,    
    @AktifPasif,    
    @KulNo,    
    @KulAdi,    
    @SaticiKodu,    
    @SaticiAdi    
 )  
 SET @i = @i + 1;  
  END;  
  
END;  
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spCariKartYaz')
drop proc spCariKartYaz
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spCariKartYaz]       
     @MUSNO int,      
     @MUSKOD nvarchar(20),      
     @MUSADI nvarchar(50),      
     @YETKILI nvarchar(30),      
     @OZELKOD1 nvarchar(20),      
     @OZELKOD2 nvarchar(20),      
     @OZELKOD3 nvarchar(20),      
     @OZELKOD4 nvarchar(20),      
     @MUSVERGI nvarchar(50),      
     @MUSVERNO nvarchar(20),      
     @MUSADRES1 nvarchar(50),      
     @MUSADRES2 nvarchar(50),      
     @MUSTEL nvarchar(20),      
     @MUSTEL2 nvarchar(20),      
     @MUSTEL3 nvarchar(20),      
     @MUSFAX nvarchar(20),      
     @MUSCEP nvarchar(20),      
     @MUSACIKLA1 nvarchar(100),      
     @MUSACIKLA2 nvarchar(100),      
     @MUSACIKLA3 nvarchar(100),      
     @MUSACIKLA4 nvarchar(100),      
     @MUSBORC float,      
     @MUSALACAK float,      
     @MUSBAKIYE float,      
     @SIGORTA float,      
     @PLAKA nvarchar(20),      
     @MUSCIN nvarchar(1),      
     @MUSDURUM nvarchar(1),      
     @YUZDE float,      
     @MUSIL nvarchar(25),      
     @MUSILCE nvarchar(25),      
     @ISKONTO float,      
     @RISK float,      
     @SONEXTTAR smalldatetime,      
     @KULNO int,      
     @KULADI nvarchar(20),      
     @Tipi nvarchar(15),      
     @FaturaOPlanGunu int,      
     @GunAy nvarchar(3),      
     @OpsiyonGunu int,      
     @ParaLitre nvarchar(1),      
     @Departman nvarchar(8),      
     @Secim nvarchar(10),      
     @FiyatGrupNo int,      
     @MUSCEP2 nvarchar(20),      
     @FiyatGrupAdi nvarchar(15),      
     @Iskonto1 float,      
     @Iskonto2 float,      
     @Iskonto3 float,      
     @StokKodu nvarchar(30),      
     @StokAdi nvarchar(40),      
     @FatKes nvarchar(1),      
     @TaksitTutari float,      
     @SaticiKodu nvarchar(10),      
     @SaticiAdi nvarchar(25),      
     @Kilitli nvarchar(15),      
     @Odeme nvarchar(1),      
     @Depo nvarchar(30),      
     @Bagkur nvarchar(1),      
     @BagkurNo nvarchar(15),      
     @VergiTipi nvarchar(6),      
     @VergiMukellefi nvarchar(5),      
     @KomisyonYuzdesi float,      
     @BagkurKesenegi float,      
     @SSKNo float,      
     @BorsaSicilNo nvarchar(20),      
     @BelgeNo nvarchar(19),      
     @UstCariNo int,      
     @FaturaAdresNo int,      
     @SevkAdresNo int,      
     @web_kullanici_adi nvarchar(50),      
     @web_kullanici_sifre nvarchar(50),      
     @SektorGrubu nvarchar(30),      
     @CariGrubu nvarchar(30),      
     @BolgeGrubu nvarchar(30),      
     @PuanCarpani float,      
     @AnaRutAdi nvarchar(30),      
     @Gorunum int,      
     @AcilisTarihi smalldatetime,      
     @SMSTelefonu nvarchar(11),      
     @SMSGonder nvarchar(1),      
     @UserType nvarchar(5),      
     @Cinsiyeti nvarchar(5),      
     @Uyrugu nvarchar(30),      
     @AcentaAnlasmaNo int,      
     @AcentaAnlasmaDeger float,      
     @TCKimlikNo nvarchar(11),      
     @Rengi nvarchar(15),      
     @PasaportSeriNo nvarchar(15),      
     @PasaportNo nvarchar(15),      
     @Meslek nvarchar(30),      
     @SKFIK int,      
     @KimlikTuru nvarchar(1),      
     @UpdateDate smallDateTime,      
     @CGTipi nvarchar(2), @Mah_Koyu nvarchar(30),      
     @A_Muh_Kodu nvarchar(20),      
     @S_Muh_Kodu nvarchar(20),      
     @TaksitCekSenetBakiye int,      
     @DSYBTalepteFaturaKesilmesin bit          
AS          
if exists (select * from AMUSTERI where MUSNO=@MUSNO)     
begin    
update AMUSTERI set     
     MUSKOD=@MUSKOD,      
     MUSADI=@MUSADI,      
     YETKILI=@YETKILI,      
     OZELKOD1=@OZELKOD1,      
     OZELKOD2=@OZELKOD2,      
     OZELKOD3=@OZELKOD3,      
     OZELKOD4=@OZELKOD4,      
     MUSVERGI=@MUSVERGI,      
     MUSVERNO=@MUSVERNO,      
     MUSADRES1=@MUSADRES1,      
     MUSADRES2=@MUSADRES2,      
     MUSTEL=@MUSTEL,      
     MUSTEL2=@MUSTEL2,      
     MUSTEL3=@MUSTEL3,      
     MUSFAX=@MUSFAX,      
     MUSCEP=@MUSCEP,      
     MUSACIKLA1=@MUSACIKLA1,      
     MUSACIKLA2=@MUSACIKLA2,      
     MUSACIKLA3=@MUSACIKLA3,      
     MUSACIKLA4=@MUSACIKLA4,      
     MUSBORC=@MUSBORC,      
     MUSALACAK=@MUSALACAK,      
     MUSBAKIYE=@MUSBAKIYE,      
     SIGORTA=@SIGORTA,      
     PLAKA=@PLAKA,      
     MUSCIN=@MUSCIN,      
     MUSDURUM=@MUSDURUM,      
     YUZDE=@YUZDE,      
     MUSIL=@MUSIL,      
     MUSILCE=@MUSILCE,     
     ISKONTO=@ISKONTO,      
     RISK=@RISK,      
     SONEXTTAR=@SONEXTTAR,      
     KULNO=@KULNO,      
     KULADI=@KULADI,      
     Tipi=@Tipi,      
     FaturaOPlanGunu=@FaturaOPlanGunu,      
     GunAy=@GunAy,      
     OpsiyonGunu=@OpsiyonGunu,      
     ParaLitre=@ParaLitre,      
     Departman=@Departman,      
     Secim=@Secim,      
     FiyatGrupNo=@FiyatGrupNo,      
     MUSCEP2=@MUSCEP2,      
     FiyatGrupAdi=@FiyatGrupAdi,      
     Iskonto1=@Iskonto1,      
     Iskonto2=@Iskonto2,      
     Iskonto3=@Iskonto3,      
     StokKodu=@StokKodu,      
     StokAdi=@StokAdi,      
     FatKes=@FatKes,      
     TaksitTutari=@TaksitTutari,      
     SaticiKodu=@SaticiKodu,      
     SaticiAdi=@SaticiAdi,      
     Kilitli=@Kilitli,      
     Odeme=@Odeme,      
     Depo=@Depo,      
     Bagkur=@Bagkur,      
     BagkurNo=@BagkurNo,      
     VergiTipi=@VergiTipi,      
     VergiMukellefi=@VergiMukellefi,      
     KomisyonYuzdesi=@KomisyonYuzdesi,      
     BagkurKesenegi=@BagkurKesenegi,      
     SSKNo=@SSKNo,      
     BorsaSicilNo=@BorsaSicilNo,      
     BelgeNo=@BelgeNo,      
     UstCariNo=@UstCariNo,      
     FaturaAdresNo=@FaturaAdresNo,      
     SevkAdresNo=@SevkAdresNo,      
     web_kullanici_adi=@web_kullanici_adi,      
     web_kullanici_sifre=@web_kullanici_sifre,      
     SektorGrubu=@SektorGrubu,      
     CariGrubu=@CariGrubu,      
     BolgeGrubu=@BolgeGrubu,      
     PuanCarpani=@PuanCarpani,      
     AnaRutAdi=@AnaRutAdi,      
     Gorunum=@Gorunum,      
     AcilisTarihi=@AcilisTarihi,      
     SMSTelefonu=@SMSTelefonu,      
     SMSGonder=@SMSGonder,      
     UserType=@UserType,      
     Cinsiyeti=@Cinsiyeti,      
     Uyrugu=@Uyrugu,      
     AcentaAnlasmaNo=@AcentaAnlasmaNo,      
     AcentaAnlasmaDeger=@AcentaAnlasmaDeger,      
     TCKimlikNo=@TCKimlikNo,      
     Rengi=@Rengi,      
     PasaportSeriNo=@PasaportSeriNo,      
     PasaportNo=@PasaportNo,      
     Meslek=@Meslek,      
     SKFIK=@SKFIK,      
     KimlikTuru=@KimlikTuru,      
     UpdateDate=@UpdateDate,      
     CGTipi=@CGTipi,       
     Mah_Koyu=@Mah_Koyu,      
     A_Muh_Kodu=@A_Muh_Kodu,      
     S_Muh_Kodu=@S_Muh_Kodu,      
     TaksitCekSenetBakiye=@TaksitCekSenetBakiye,      
     DSYBTalepteFaturaKesilmesin=@DSYBTalepteFaturaKesilmesin      
  where MUSNO=@MUSNO    
end else     
begin     
INSERT AMUSTERI (  MUSNO, MUSKOD, MUSADI, YETKILI, OZELKOD1, OZELKOD2, OZELKOD3, OZELKOD4, MUSVERGI, MUSVERNO, MUSADRES1, MUSADRES2, MUSTEL, MUSTEL2, MUSTEL3, MUSFAX, MUSCEP, MUSACIKLA1, MUSACIKLA2,   
                         MUSACIKLA3, MUSACIKLA4, MUSBORC, MUSALACAK, MUSBAKIYE, SIGORTA, PLAKA, MUSCIN, MUSDURUM, YUZDE, MUSIL, MUSILCE, ISKONTO, RISK, SONEXTTAR, KULNO, KULADI, Tipi, FaturaOPlanGunu, GunAy,   
                         OpsiyonGunu, ParaLitre, Departman, Secim, FiyatGrupNo, MUSCEP2, FiyatGrupAdi, Iskonto1, Iskonto2, Iskonto3, StokKodu, StokAdi, FatKes, TaksitTutari, SaticiKodu, SaticiAdi, Kilitli, Odeme, Depo, Bagkur, BagkurNo, VergiTipi,   
                         VergiMukellefi, KomisyonYuzdesi, BagkurKesenegi, SSKNo, BorsaSicilNo, BelgeNo, UstCariNo, FaturaAdresNo, SevkAdresNo, web_kullanici_adi, web_kullanici_sifre, SektorGrubu, CariGrubu, BolgeGrubu, PuanCarpani,   
                         AnaRutAdi, Gorunum, AcilisTarihi, SMSTelefonu, SMSGonder, UserType, Cinsiyeti, Uyrugu, AcentaAnlasmaNo, AcentaAnlasmaDeger, TCKimlikNo, Rengi, PasaportSeriNo, PasaportNo, Meslek, SKFIK, KimlikTuru, UpdateDate,   
                         CGTipi, Mah_Koyu, A_Muh_Kodu, S_Muh_Kodu, TaksitCekSenetBakiye, DSYBTalepteFaturaKesilmesin  
) VALUES(@MUSNO,      
      @MUSKOD,      
     @MUSADI,      
     @YETKILI,      
     @OZELKOD1,      
     @OZELKOD2,      
     @OZELKOD3,      
     @OZELKOD4,      
     @MUSVERGI,      
     @MUSVERNO,      
     @MUSADRES1,      
     @MUSADRES2,      
     @MUSTEL,      
     @MUSTEL2,      
     @MUSTEL3,      
     @MUSFAX,      
     @MUSCEP,      
     @MUSACIKLA1,      
     @MUSACIKLA2,      
     @MUSACIKLA3,      
     @MUSACIKLA4,      
     @MUSBORC,      
     @MUSALACAK,      
     @MUSBAKIYE,      
     @SIGORTA,      
     @PLAKA,      
     @MUSCIN,      
     @MUSDURUM,      
     @YUZDE,      
     @MUSIL,      
     @MUSILCE,      
     @ISKONTO,      
     @RISK,      
     @SONEXTTAR,      
     @KULNO,      
     @KULADI,      
     @Tipi,      
     @FaturaOPlanGunu,      
     @GunAy,      
     @OpsiyonGunu,      
     @ParaLitre,      
     @Departman,      
     @Secim,      
     @FiyatGrupNo,      
     @MUSCEP2,      
     @FiyatGrupAdi,      
     @Iskonto1,      
     @Iskonto2,      
     @Iskonto3,      
     @StokKodu,      
     @StokAdi,      
     @FatKes,      
     @TaksitTutari,      
     @SaticiKodu,      
     @SaticiAdi,      
     @Kilitli,      
     @Odeme,      
     @Depo,      
     @Bagkur,      
     @BagkurNo,      
     @VergiTipi,      
     @VergiMukellefi,      
     @KomisyonYuzdesi,      
     @BagkurKesenegi,      
     @SSKNo,      
     @BorsaSicilNo,      
     @BelgeNo,      
     @UstCariNo,      
     @FaturaAdresNo,      
     @SevkAdresNo,      
     @web_kullanici_adi,      
     @web_kullanici_sifre,      
     @SektorGrubu,      
     @CariGrubu,      
     @BolgeGrubu,      
     @PuanCarpani,      
     @AnaRutAdi,      
     @Gorunum,      
     @AcilisTarihi,      
     @SMSTelefonu,      
     @SMSGonder,      
     @UserType,      
     @Cinsiyeti,      
     @Uyrugu,      
     @AcentaAnlasmaNo,      
     @AcentaAnlasmaDeger,      
     @TCKimlikNo,      
     @Rengi,      
     @PasaportSeriNo,      
     @PasaportNo,      
 @Meslek,      
     @SKFIK,      
     @KimlikTuru,      
     @UpdateDate,      
     @CGTipi,       
     @Mah_Koyu,      
     @A_Muh_Kodu,      
     @S_Muh_Kodu,      
     @TaksitCekSenetBakiye,      
     @DSYBTalepteFaturaKesilmesin)          
end
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spKasaHareketYaz')
drop proc spKasaHareketYaz
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spKasaHareketYaz]     
   @BORC Float,    
   @ALACAK Float,    
   @ACIKLAMA nvarchar(40),     
   @KASAKODU nvarchar(6),    
   @KASAADI nvarchar(10),    
   @MUSNO int,    
   @MUSADI nvarchar(50),    
   @TARIH datetime,    
   @BA nvarchar(1),    
   @BAACIKLA nvarchar(12),    
   @ISLEMNO int,    
   @MUSKOD nvarchar(20),    
   @DovizTut float,    
   @DovizCinsi int,    
   @DovizKodu nvarchar(2),    
   @AcikKapali nvarchar(1),    
   @IslemNo2 int,    
   @AktarimTarihi datetime,    
   @BelgeNo nvarchar(10),    
   @KULNO int,    
   @KULADI nvarchar(20),    
   @NakitVisa nvarchar(1),    
   @SaticiKodu nvarchar(10),    
   @SaticiAdi nvarchar(25),    
   @DovizBorc Float,    
   @DovizAlacak Float,    
   @VardiyaKodu nvarchar(15),    
   @VardiyaDurumu nvarchar(9),    
   @Departman nvarchar(8),    
   @KayitDuzeyi int,    
    @IsIstasyonKodu nvarchar(30),    
   @IsIstasyonAdi nvarchar(30),    
   @VardiyaNo nvarchar(8),    
   @FisNo int,  
   @DovizKuru float = 1  
AS     
    
INSERT KASA VALUES ( @BORC,    
   @ALACAK,    
   @ACIKLAMA,    
   @KASAKODU,    
   @KASAADI,    
   @MUSNO,    
   @MUSADI,    
   @TARIH,    
   @BA,    
   @BAACIKLA,    
   @ISLEMNO,    
   @MUSKOD,    
   @DovizTut,    
   @DovizCinsi,    
   @DovizKodu,    
   @AcikKapali,    
   @IslemNo2,    
   @AktarimTarihi,    
   @BelgeNo,    
   @KULNO,    
   @KULADI,    
   @NakitVisa,    
   @SaticiKodu,    
   @SaticiAdi,    
   @DovizBorc,    
   @DovizAlacak,    
   @VardiyaKodu,    
   @VardiyaDurumu,    
   @Departman,    
   @KayitDuzeyi,    
   @IsIstasyonKodu,    
   @IsIstasyonAdi,    
   @VardiyaNo,    
   @FisNo,  
   @DovizKuru) 
	
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_Stok_Aktar')
drop proc sp_Stok_Aktar
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE   PROCEDURE [dbo].[sp_Stok_Aktar]  
(  
   @Hedef  SYSNAME,  
   @Kaynak SYSNAME,  
   @StokNo nvarchar(max)  
)  
  
AS   
BEGIN  
 DECLARE @Query nvarchar(MAX); 
 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.STOKKARTI NOCHECK CONSTRAINT ALL' + Char(13);
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.StokFiyat NOCHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.StokBarkodlari NOCHECK CONSTRAINT ALL'+ Char(13); 
 EXEC sp_executesql @Query;  

 exec sp_StokKart_Aktar @Hedef,@Kaynak,@StokNo;  
 exec sp_StokFiyat_Aktar @Hedef,@Kaynak,@StokNo;  
 exec sp_StokBarkodlari_Aktar @Hedef,@Kaynak,@StokNo;  

 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.STOKKARTI CHECK CONSTRAINT ALL'+ Char(13);  
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.StokFiyat CHECK CONSTRAINT ALL'+ Char(13); 
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.StokBarkodlari CHECK CONSTRAINT ALL'+ Char(13); 
 EXEC sp_executesql @Query; 
END;

go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_Cari_Aktar')
drop proc sp_Cari_Aktar
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE   PROCEDURE [dbo].[sp_Cari_Aktar]    
(    
    @Hedef  SYSNAME,    
 @Kaynak   SYSNAME,    
 @MusNo    nvarchar(max)    
)    
    
AS     
BEGIN    
 DECLARE @Query nvarchar(MAX);   
 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.AMUSTERI NOCHECK CONSTRAINT ALL'+ Char(13);  
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.AMusteri1 NOCHECK CONSTRAINT ALL'+ Char(13);   
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.Adres NOCHECK CONSTRAINT ALL'+ Char(13);   
 EXEC sp_executesql @Query;    
    
 exec sp_AMUSTERI_Aktar @Hedef,@Kaynak,@MusNo;    
 exec sp_AMusteri1_Aktar @Hedef,@Kaynak,@MusNo;    
 exec sp_Adres_Aktar @Hedef,@Kaynak,@MusNo;    
    
 SET @Query = 'ALTER TABLE  '+@Hedef+'.dbo.AMUSTERI NOCHECK CONSTRAINT ALL'+ Char(13);  
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.AMusteri1 NOCHECK CONSTRAINT ALL'+ Char(13);   
 SET @Query = @Query +'ALTER TABLE  '+@Hedef+'.dbo.Adres NOCHECK CONSTRAINT ALL'+ Char(13);   
 EXEC sp_executesql @Query;    
END;  
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_AMUSTERI')
 BEGIN 
 drop view V_AMUSTERI
 end
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  VIEW V_AMUSTERI   
AS   
SELECT     dbo.AMUSTERI.MUSNO, dbo.AMUSTERI.MUSKOD, dbo.AMUSTERI.MUSADI, dbo.AMUSTERI.YETKILI, dbo.AMUSTERI.OZELKOD1, dbo.AMUSTERI.OZELKOD2,   
                      dbo.AMUSTERI.OZELKOD3, dbo.AMUSTERI.OZELKOD4, dbo.AMUSTERI.MUSVERGI, dbo.AMUSTERI.MUSVERNO, dbo.AMUSTERI.MUSADRES1,   
                      dbo.AMUSTERI.MUSADRES2, dbo.AMUSTERI.MUSTEL, dbo.AMUSTERI.MUSTEL2, dbo.AMUSTERI.MUSTEL3, dbo.AMUSTERI.MUSFAX, dbo.AMUSTERI.MUSCEP,   
                      dbo.AMUSTERI.MUSACIKLA1, dbo.AMUSTERI.MUSACIKLA2, dbo.AMUSTERI.MUSACIKLA3, dbo.AMUSTERI.MUSACIKLA4,   
                      dbo.VCr_CariKartlarBABTR.BorcToplami AS MUSBORC, dbo.VCr_CariKartlarBABTR.AlacakToplami AS MUSALACAK, dbo.VCr_CariKartlarBABTR.Bakiye AS MUSBAKIYE,   
                      dbo.AMUSTERI.SIGORTA, dbo.AMUSTERI.PLAKA, dbo.AMUSTERI.MUSCIN, dbo.AMUSTERI.MUSDURUM, dbo.AMUSTERI.YUZDE, dbo.AMUSTERI.MUSIL,   
                      dbo.AMUSTERI.MUSILCE, dbo.AMUSTERI.ISKONTO, dbo.AMUSTERI.RISK, dbo.AMUSTERI.SONEXTTAR, dbo.AMUSTERI.KULNO, dbo.AMUSTERI.KULADI,   
                      dbo.AMUSTERI.Tipi, dbo.AMUSTERI.FaturaOPlanGunu, dbo.AMUSTERI.GunAy, dbo.AMUSTERI.OpsiyonGunu, dbo.AMUSTERI.ParaLitre, dbo.AMUSTERI.Departman,   
                      dbo.AMUSTERI.Secim, dbo.AMUSTERI.FiyatGrupNo, dbo.AMUSTERI.MUSCEP2, dbo.AMUSTERI.FiyatGrupAdi, dbo.AMUSTERI.Iskonto1, dbo.AMUSTERI.Iskonto2,   
                      dbo.AMUSTERI.Iskonto3, dbo.AMUSTERI.StokKodu, dbo.AMUSTERI.StokAdi, dbo.AMUSTERI.FatKes, dbo.VCr_CariKartlarBABTR.TaksitTutari, dbo.AMUSTERI.SaticiKodu,   
                      S.SaticiAdi, dbo.AMUSTERI.Kilitli, dbo.AMUSTERI.Odeme, dbo.AMUSTERI.Depo, dbo.AMUSTERI.Bagkur, dbo.AMUSTERI.BagkurNo,   
                      dbo.AMUSTERI.VergiTipi, dbo.AMUSTERI.VergiMukellefi, dbo.AMUSTERI.KomisyonYuzdesi, dbo.AMUSTERI.BagkurKesenegi, dbo.AMUSTERI.SSKNo,   
                      dbo.AMUSTERI.BorsaSicilNo, dbo.AMUSTERI.BelgeNo, dbo.AMUSTERI.UstCariNo, dbo.AMUSTERI.FaturaAdresNo, dbo.AMUSTERI.SevkAdresNo,   
                      dbo.AMUSTERI.web_kullanici_adi, dbo.AMUSTERI.web_kullanici_sifre, dbo.AMUSTERI.SektorGrubu, dbo.AMUSTERI.CariGrubu, dbo.AMUSTERI.BolgeGrubu,   
                      dbo.AMUSTERI.PuanCarpani, dbo.AMUSTERI.AnaRutAdi, dbo.AMUSTERI.Gorunum, dbo.AMUSTERI.AcilisTarihi, dbo.AMUSTERI.SMSTelefonu,   
                      dbo.AMUSTERI.SMSGonder, dbo.AMUSTERI.UserType, dbo.AMUSTERI.Cinsiyeti, dbo.AMUSTERI.Uyrugu, dbo.AMUSTERI.AcentaAnlasmaNo,   
                      dbo.AMUSTERI.AcentaAnlasmaDeger, dbo.AMUSTERI.TCKimlikNo, dbo.AMUSTERI.Rengi, dbo.AMUSTERI.PasaportSeriNo, dbo.AMUSTERI.PasaportNo,   
                      dbo.AMUSTERI.Meslek, dbo.AMUSTERI.SKFIK, dbo.AMUSTERI.KimlikTuru, dbo.AMUSTERI.UpdateDate, dbo.AMUSTERI.CGTipi,dbo.AMUSTERI.Mah_Koyu   
FROM         dbo.AMUSTERI LEFT OUTER JOIN  Satici S on AMUSTERI.SaticiKodu=S.SaticiKodu 
                      left join dbo.VCr_CariKartlarBABTR ON dbo.AMUSTERI.MUSNO = dbo.VCr_CariKartlarBABTR.CariNo
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spCariDINAMIK_PivotKolonlar')
drop proc spCariDINAMIK_PivotKolonlar
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 create   proc spCariDINAMIK_PivotKolonlar( @Tarih1 datetime='20000101', @Tarih2 datetime='20250101'  )    
as    
DECLARE @PivotKolonlar VARCHAR(MAX)    
SELECT @PivotKolonlar=      
COALESCE (@PivotKolonlar +',['+CAST(BAACIKLA AS VARCHAR) +']',    
'['+CAST(BAACIKLA AS VARCHAR)+']')    
FROM (SELECT DISTINCT(BAACIKLA) FROM ACARI where convert(varchar(10),TARIH,121)>=convert(varchar(10),@Tarih1,121) and convert(varchar(10),TARIH,121)<=convert(varchar(10),@Tarih2,121) ) AS DISTBAACIKLA    
DECLARE @PVT nvarchar(max)    
set @PVT=N'    
SELECT * FROM (SELECT A.MUSKOD,M.MUSADI,M.SaticiAdi ,ROUND(M.MUSBAKIYE,0) BAKIYE, BAACIKLA, (A.BORC-A.ALACAK) TUTAR FROM ACARI A ,V_AMUSTERI M  where A.MUSNO=M.MUSNO AND
convert(varchar(10),TARIH,121)>='''+convert(varchar(10),@Tarih1,121)+''' and 
convert(varchar(10),TARIH,121)<='''+convert(varchar(10),@Tarih2,121)+''' ) AS PIVOTDATA    
PIVOT ( SUM(TUTAR)   FOR BAACIKLA IN ('+ @PivotKolonlar+')    
) AS PIVOTTABLO    ORDER BY MUSADI
'    
EXECUTE  (@PVT)
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='KdvOraniDegistir')
drop proc KdvOraniDegistir
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  proc [dbo].[KdvOraniDegistir] @Kod nvarchar(max), @FiyatDegistir bit , @Skdv float,@Akdv float
as    
   -------  exec KdvOraniDegistir   '''1001'',''10101'',''101010'',''1020125'',''11'',''121''', 0,'1','8' 
IF (@Skdv  is not null) or (@Akdv is not null)
BEGIN	
	BEGIN TRANSACTION    
	BEGIN TRY    
	declare @i int    
	declare @FKdvAdi nvarchar(5)    
	declare @SqlTextSF varchar(max)  -- nvarchar karakter boyutu kısıtlı gelebilir.  
	declare @SqlTextD  varchar(max)
	 SET @SqlTextSF =  'UPDATE sf SET sf.SatisKdv=sk.SatisKdv from StokFiyat sf,STOKKARTI sk where sf.Kodu = sk.Kodu AND sk.Kodu in ('+@Kod+')';
	 exec(@SqlTextSF); 

	 IF (@Skdv  is not null)
	 BEGIN
		 set @i=1  
		 set @SqlTextSF=''  
		 while @i<21
		 begin  
			set @FKdvAdi='F'+convert(varchar,@i)+'Kdv'    
			set @SqlTextSF =@SqlTextSF+Char(13)+ ' update StokFiyat SET  F'+convert(varchar,@i)+'FiyatH=(case when '+convert(varchar,@FiyatDegistir)+' = 1 then ' +    
			'F'+convert(varchar,@i)+'FiyatH  else (F'+convert(varchar,@i)+'FiyatH*(100 +SatisKdv)/(100+'''+convert(varchar,@Skdv)+    
			''')) end  ), F'+convert(varchar,@i)+'Fiyat = (case when F'+convert(varchar,@i)+'Kdv=''H'' then F'+convert(varchar,@i)+'FiyatH ELSE ( F'+convert(varchar,@i)+'FiyatH*(100+'''+convert(varchar,@Skdv)+''')/100) END) WHERE Kodu in ('+@Kod+'); '    

			set @i=@i+1  
		 end 
	 END
  
	 --update STOKKARTI SET AlisKdv=@Skdv ,SatisKdv=@Skdv WHERE Kodu in (@Kod)
	 --update StokFiyat SET SatisKdv = @Skdv WHERE Kodu in (@Kod);
	SET @SqlTextD = '';
    IF (@Akdv is not null)
	BEGIN
		set @SqlTextD =' update STOKKARTI SET AlisKdv='''+convert(varchar,@Akdv)+'''  WHERE Kodu in ('+@Kod+'); '+Char(13);  
    END;

    IF (@Skdv  is not null) 
	BEGIN
	    set @SqlTextD = @SqlTextD + ' update STOKKARTI SET SatisKdv='''+convert(varchar,@Skdv)+''' WHERE Kodu in ('+@Kod+'); '+Char(13);  
 		set @SqlTextD = @SqlTextD + ' update StokFiyat SET SatisKdv='''+convert(varchar,@Skdv)+'''  WHERE Kodu in ('+@Kod+');';      
    END
	--select  @SqlText   
	exec(@SqlTextSF) 
	exec(@SqlTextD) 

	 update s       
	   set s.F1Fiyat = f.F1Fiyat,s.F1Kdv = f.F1Kdv,s.F1DovizKodu = f.F1DovizKodu,      
		 s.F2Fiyat = f.F2Fiyat,s.F2Kdv = f.F2Kdv,s.F2DovizKodu = f.F2DovizKodu,      
		 s.F3Fiyat = f.F3Fiyat,s.F3Kdv = f.F3Kdv,s.F3DovizKodu = f.F3DovizKodu,      
		 s.F4Fiyat = f.F4Fiyat,s.F4Kdv = f.F4Kdv,s.F4DovizKodu = f.F4DovizKodu      
	 from  STOKKARTI s,StokFiyat f       
	 where  s.Kodu = f.Kodu;

	COMMIT    
	END TRY    
	BEGIN CATCH    
	ROLLBACK    
	END CATCH    
END    
				
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 IF EXISTS (SELECT *  FROM sys.views WHERE  name='V_TopluStokFiyatDegistir')
 BEGIN 
 drop view V_TopluStokFiyatDegistir
 end
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  VIEW V_TopluStokFiyatDegistir AS  
SELECT SK.Barkodu, SK.Kodu, SK.UreticiFirmaKodu, SK.StokAdi, SK.OzelKod1, SK.OzelKod2,OzelKod3,OzelKod4,           
        SK.GrupAdi, SK.OTVOrani,SK.StokAdedi,SK.Update_Date,SK.AnaBirim,SK.AktifPasif,SK.StokNo ,    
  ROUND(SF.F1Fiyat,2) F1Fiyat ,ROUND(SF.F2Fiyat,2)F2Fiyat,  ROUND(SF.F3Fiyat,2)F3Fiyat,  ROUND(SF.F4Fiyat,2)F4Fiyat,  
  ROUND(SF.F5Fiyat,2) F5Fiyat, ROUND(SF.F6Fiyat,2)F6Fiyat,  ROUND(SF.F7Fiyat,2)F7Fiyat,  ROUND(SF.F8Fiyat,2)F8Fiyat,  
  ROUND(SF.F9Fiyat,2) F9Fiyat, ROUND(SF.F10Fiyat,2)F10Fiyat,ROUND(SF.F11Fiyat,2)F11Fiyat,ROUND(SF.F12Fiyat,2)F12Fiyat,  
  ROUND(SF.F13Fiyat,2)F13Fiyat,ROUND(SF.F14Fiyat,2)F14Fiyat,ROUND(SF.F15Fiyat,2)F15Fiyat,ROUND(SF.F16Fiyat,2)F16Fiyat,  
  ROUND(SF.F17Fiyat,2)F17Fiyat,ROUND(SF.F18Fiyat,2)F18Fiyat,ROUND(SF.F19Fiyat,2)F19Fiyat,ROUND(SF.F20Fiyat,2)F20Fiyat,
  SF.F1Kdv,SF.F2Kdv,SF.F3Kdv,SF.F4Kdv,SF.F5Kdv,SF.F6Kdv,SF.F7Kdv,SF.F8Kdv,SF.F9Kdv,SF.F10Kdv,SF.F11Kdv,SF.F12Kdv,
  SF.F13Kdv,SF.F14Kdv,SF.F15Kdv,SF.F16Kdv,SF.F17Kdv,SF.F18Kdv,SF.F19Kdv,SF.F20Kdv,
  SF.F1DovizKodu,SF.F2DovizKodu,SF.F3DovizKodu,SF.F4DovizKodu,SF.F5DovizKodu,SF.F6DovizKodu,SF.F7DovizKodu,SF.F8DovizKodu,SF.F9DovizKodu,SF.F10DovizKodu,
  SF.F11DovizKodu,SF.F12DovizKodu,SF.F13DovizKodu,SF.F14DovizKodu,SF.F15DovizKodu,SF.F16DovizKodu,SF.F17DovizKodu,SF.F18DovizKodu,SF.F19DovizKodu,SF.F20DovizKodu,
  SF.SatisKdv
  FROM STOKKARTI SK,    
       StokFiyat SF    
 WHERE SK.Kodu =SF.Kodu    
   AND SK.AktifPasif='A'
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
             				  
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='ISOBIRIM')
 BEGIN 
 CREATE TABLE [dbo].[ISOBIRIM](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[eFaturaTip] int NULL,
	[IsoBirimKodu] [nvarchar](20) NULL,
	[IsoBirimAdi] [nvarchar](50) NULL
 CONSTRAINT [PK_ISOBIRIM] PRIMARY KEY CLUSTERED 
(
	Id ASC
)
)
end
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
             				  
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'Varsayilan')
begin
ALTER TABLE AnaMenuAyar ADD Varsayilan bit
end
else begin
ALTER TABLE AnaMenuAyar alter column Varsayilan bit
end
go--
-------------------------------------------------------------------------------------------------------------------------------------------
             				  
if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'IkinciBirimBarkodu')
begin
	ALTER TABLE STOKKARTI ADD IkinciBirimBarkodu nvarchar(40)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN IkinciBirimBarkodu nvarchar(40)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'UcuncuBirimBarkodu')
begin
	ALTER TABLE STOKKARTI ADD UcuncuBirimBarkodu nvarchar(40)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN UcuncuBirimBarkodu nvarchar(40)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'DorduncuBirimBarkodu')
begin
	ALTER TABLE STOKKARTI ADD DorduncuBirimBarkodu nvarchar(40)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN DorduncuBirimBarkodu nvarchar(40)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'BesinciBirimBarkodu')
begin
	ALTER TABLE STOKKARTI ADD BesinciBirimBarkodu nvarchar(40)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN BesinciBirimBarkodu nvarchar(40)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'AltinciBirimBarkodu')
begin
	ALTER TABLE STOKKARTI ADD AltinciBirimBarkodu nvarchar(40)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN AltinciBirimBarkodu nvarchar(40)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
             				  
if exists (select * from sys.views where name='V_StokBarkodlari_Birimler')
drop VIEW V_StokBarkodlari_Birimler
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  VIEW V_StokBarkodlari_Birimler AS  
select   
 StokKodu,  
 (Select Barkodu from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 2) IkinciBirimBarkodu,  
 (Select Carpani from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 2) IkinciBirimCarpani,  
 (Select Birim from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 2) IkinciBirim,  
 (Select Barkodu from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 3) UcuncuBirimBarkodu, 
 (Select Carpani from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 3) UcuncuBirimCarpani,  
 (Select Birim from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 3) UcuncuBirim,   
 (Select Barkodu from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 4) DorduncuBirimBarkodu, 
 (Select Carpani from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 4) DorduncuBirimCarpani,  
 (Select Birim from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 4) DorduncuBirim,   
 (Select Barkodu from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 5) BesinciBirimBarkodu, 
 (Select Carpani from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 5) BesinciBirimCarpani,  
 (Select Birim from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 5) BesinciBirim,   
 (Select Barkodu from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 6) AltinciBirimBarkodu,   
  (Select Carpani from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 6) AltinciBirimCarpani,  
 (Select Birim from StokBarkodlari sb1 where sb1.StokKodu  =sb.StokKodu and SiraNo = 6) AltinciBirim
from StokBarkodlari sb  
Group By StokKodu 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_BirimBarkod_Aktarim')
drop proc sp_BirimBarkod_Aktarim
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE sp_BirimBarkod_Aktarim AS
UPDATE sk SET sk.IkinciBirim = ISNULL(Brm.IkinciBirim,sk.IkinciBirim),
			  sk.IkinciBirimBarkodu = ISNULL(Brm.IkinciBirimBarkodu,sk.IkinciBirimBarkodu),
			  sk.IkinciBirimMiktari = ISNULL(Brm.IkinciBirimCarpani,sk.IkinciBirimMiktari),
			  sk.UcuncuBirim = ISNULL(Brm.UcuncuBirim,sk.UcuncuBirim),
			  sk.UcuncuBirimBarkodu = ISNULL(Brm.UcuncuBirimBarkodu,sk.UcuncuBirimBarkodu),
			  sk.UcuncuBirimMiktari = ISNULL(Brm.UcuncuBirimCarpani,sk.UcuncuBirimMiktari),
			  sk.DorduncuBirim = ISNULL(Brm.DorduncuBirim,sk.DorduncuBirim),
			  sk.DorduncuBirimBarkodu = ISNULL(Brm.DorduncuBirimBarkodu,sk.DorduncuBirimBarkodu),
			  sk.DorduncuBirimMiktari = ISNULL(Brm.DorduncuBirimCarpani,sk.DorduncuBirimMiktari),
			  sk.BesinciBirim = ISNULL(Brm.BesinciBirim,sk.BesinciBirim),
			  sk.BesinciBirimBarkodu = ISNULL(Brm.BesinciBirimBarkodu,sk.BesinciBirimBarkodu),
			  sk.BesinciBirimMiktari = ISNULL(Brm.BesinciBirimCarpani,sk.BesinciBirimMiktari),
			  sk.AltinciBirim = ISNULL(Brm.AltinciBirim,sk.AltinciBirim),
			  sk.AltinciBirimBarkodu = ISNULL(Brm.AltinciBirimBarkodu,sk.AltinciBirimBarkodu),
			  sk.AltinciBirimMiktari = ISNULL(Brm.AltinciBirimCarpani,sk.AltinciBirimMiktari)
  from STOKKARTI sk,
       V_StokBarkodlari_Birimler Brm
 where sk.Kodu = Brm.StokKodu 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DELETE FROM GridView where KayitIsmi = 'FrmHizliPerakendePCTSPanel9pcHizliSatistsSatiscxHareketlertvHareketler'
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if NOT  exists (select * from sys.tables where name='ISOBIRIM')
BEGIN
CREATE TABLE [dbo].[ISOBIRIM](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[eFaturaTip] int NULL,
	[IsoBirimKodu] [nvarchar](20) NULL,
	[IsoBirimAdi] [nvarchar](50) NULL
 CONSTRAINT [PK_ISOBIRIM] PRIMARY KEY CLUSTERED 
(
	Id ASC
)
)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'ISOBIRIM') and name=N'eFaturaTip')
begin
	ALTER TABLE ISOBIRIM ADD eFaturaTip int
end ELSE BEGIN
	ALTER TABLE ISOBIRIM ALTER COLUMN eFaturaTip int
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spTahsilVadeRaporu_Rut')
drop proc spTahsilVadeRaporu_Rut
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

create proc spTahsilVadeRaporu_Rut (@RutAdi nvarchar(max),@RutGunu nvarchar(max),@RutSatici nvarchar(max))    
as      
create table #odeme      
(      
  RecID integer Identity (1, 1) not null, NoId integer, IslemNo integer, Tarih datetime,      
  CariNo integer,  IslemTuru nvarchar(20) COLLATE DATABASE_DEFAULT,  Bakiye float,  Borc_Kullanilan float ,  Devreden float,  Borc float,      
  Devir float  PRIMARY KEY CLUSTERED  (RecID)      
)      
    
DECLARE @CariNo int    
Declare @DagitimTutari float      
Declare @NoId integer,  @IslemNo integer,  @Tarih datetime,  @Borc decimal (28, 14), @Borc_Kullanilan decimal (28, 14),  @Son_Kalan_Tutar float,  @IslemTuru nvarchar(20), @TmpTutar decimal(28, 14)      
    
declare cr_Rut cursor FAST_FORWARD READ_ONLY for      
select  CariNo from [VRUT] where @RutAdi is not null and RutAdi =@RutAdi and Deger = @RutGunu and SaticiAdi = @RutSatici   
union ALL   
select MUSNO from AMUSTERI where MUSBAKIYE > 0.01 and  @RutAdi is null  
open cr_Rut      
fetch next from cr_Rut      
into  @CariNo    
while (@@FETCH_STATUS = 0)    
begin    
    
 set @DagitimTutari = (select SUM (BORC-ALACAK) from ACARI WHERE MUSNO=@CariNo)       
 set @TmpTutar = @DagitimTutari      
      
 declare cr_Prepare cursor FAST_FORWARD READ_ONLY for      
 select         
   odm.NoId,      
   odm.IslemNo,      
   odm.Tarih,      
   odm.CariNo,      
   odm.Borc,      
   odm.BAAcikla      
 from Odeme odm      
 where      
    odm.CariNo = @CariNo and      
    IsNull (odm.Borc, 0) > 0      
 order by      
   odm.Tarih desc,NoId desc      
      
 open cr_Prepare      
 fetch next from cr_Prepare      
 into      
   @NoId,      
   @IslemNo,      
   @Tarih,      
   @CariNo,      
   @Borc,      
   @IslemTuru      
      
 while (@@FETCH_STATUS = 0) and (@TmpTutar > 0.0)      
 begin      
   if @Borc >= @TmpTutar       
   begin      
  set @Borc_Kullanilan = @TmpTutar      
  set @Son_Kalan_Tutar = @TmpTutar - @Borc      
  set @TmpTutar = 0.0      
   end      
   else      
   begin      
  set @TmpTutar = @TmpTutar - @Borc      
  set @Borc_Kullanilan = @Borc      
  set @Son_Kalan_Tutar = @TmpTutar      
   end      
      
   insert into #odeme       
   (      
  NoId,      
  IslemNo,      
  Tarih,      
  CariNo,      
  Borc,      
  Borc_Kullanilan,      
  Devreden,      
  Bakiye,      
  Devir,      
  IslemTuru      
   )      
   values      
   (      
  @NoId,       
  @IslemNo,      
  @Tarih,      
  @CariNo,      
  @Borc,      
  @Borc_Kullanilan,      
  @Son_Kalan_Tutar,      
  @DagitimTutari,      
  @Son_Kalan_Tutar+@Borc,      
  @IslemTuru      
   )      
      
   fetch next from cr_Prepare      
   into      
  @NoId,      
  @IslemNo,      
  @Tarih,      
  @CariNo,      
  @Borc,      
  @IslemTuru      
 end      
        
 close cr_Prepare      
 deallocate cr_Prepare       
    
   FETCH NEXT FROM cr_Rut  INTO @CariNo    
end       
close cr_Rut      
deallocate cr_Rut         
    
select      
  RecID,    
  o.NoId,    
  a.MUSNO  [Cari No],      
  a.MUSKOD [Cari Kodu],      
  a.MUSADI [Cari Adı],      
  o.IslemTuru [İşlem Türü],      
  o.IslemNo [İşlem No],      
  c.ACIKLAMA [Açıklama],      
  CONVERT(nvarchar(10),c.TARIH,104)  [İşlem Tarihi],      
  CONVERT(nvarchar(10),o.Tarih,104)  [Vade Tarihi],      
  CAST(GETDATE() - o.Tarih as int) [Geciken Gün],      
  Devreden,    
  o.Borc [Borç],      
  o.Devir,      
  o.Bakiye,      
  d.FatSerNo [Fatura Seri No],      
  d.FaturaNo [Fatura No]      
 from #odeme o      
 inner join  AMUSTERI a on  o.CariNo = a.MUSNO      
 inner join  ACARI    c on c.ISLEMNO = o.IslemNo and c.MUSNO = o.CariNo and c.BAACIKLA = o.IslemTuru     
 left join   STOKHARDETBAG d on o.IslemNo = d.IslemNo      
 Order by a.MUSNO,o.Tarih desc, o.NoId desc 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spTahsilVadeRaporu')
drop proc spTahsilVadeRaporu
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create   proc spTahsilVadeRaporu @CariNo int       
as      
create table #odeme      
(      
  RecID integer Identity (1, 1) not null, NoId integer, IslemNo integer, Tarih datetime,      
  CariNo integer,  IslemTuru nvarchar(20) COLLATE DATABASE_DEFAULT,  Bakiye float,  Borc_Kullanilan float ,  Devreden float,  Borc float,      
  Devir float  PRIMARY KEY CLUSTERED  (RecID)      
)      
      
      
Declare @DagitimTutari float      
set @DagitimTutari = (select SUM (BORC-ALACAK) from ACARI WHERE MUSNO=@CariNo)      
      
Declare  @NoId integer,  @IslemNo integer,  @Tarih datetime,  @Borc decimal (28, 14),        
   @Borc_Kullanilan decimal (28, 14),  @Son_Kalan_Tutar float,  @IslemTuru nvarchar(20), @TmpTutar decimal(28, 14)      
      
set @TmpTutar = @DagitimTutari      
      
declare cr_Prepare cursor FAST_FORWARD READ_ONLY for      
select         
  odm.NoId,      
  odm.IslemNo,      
  odm.Tarih,      
  odm.CariNo,      
  odm.Borc,      
  odm.BAAcikla      
from Odeme odm      
where      
   odm.CariNo = @CariNo and      
   IsNull (odm.Borc, 0) > 0      
order by      
  odm.Tarih desc,NoId desc      
      
open cr_Prepare      
fetch next from cr_Prepare      
into      
  @NoId,      
  @IslemNo,      
  @Tarih,      
  @CariNo,      
  @Borc,      
  @IslemTuru      
      
while (@@FETCH_STATUS = 0) and (@TmpTutar > 0.0)      
begin      
  if @Borc >= @TmpTutar       
  begin      
    set @Borc_Kullanilan = @TmpTutar      
 set @Son_Kalan_Tutar = @TmpTutar - @Borc      
 set @TmpTutar = 0.0      
  end      
  else      
  begin      
    set @TmpTutar = @TmpTutar - @Borc      
 set @Borc_Kullanilan = @Borc      
 set @Son_Kalan_Tutar = @TmpTutar      
  end      
      
  insert into #odeme       
  (      
    NoId,      
    IslemNo,      
    Tarih,      
    CariNo,      
    Borc,      
 Borc_Kullanilan,      
 Devreden,      
 Bakiye,      
 Devir,      
 IslemTuru      
  )      
  values      
  (      
 @NoId,       
 @IslemNo,      
    @Tarih,      
    @CariNo,      
    @Borc,      
 @Borc_Kullanilan,      
 @Son_Kalan_Tutar,      
 @DagitimTutari,      
 @Son_Kalan_Tutar+@Borc,      
 @IslemTuru      
  )      
      
  fetch next from cr_Prepare      
  into      
    @NoId,      
    @IslemNo,      
    @Tarih,      
    @CariNo,      
    @Borc,      
 @IslemTuru      
end      
        
close cr_Prepare      
deallocate cr_Prepare      
      
       
--  spTahsilVadeRaporu 42      
update AMUSTERI SET Kilitli='Kilitli' WHERE MUSNO IN (      
select  distinct o.CariNo  from #odeme o      
where o.Devir > (select Deger from Parametre  where Param = 'Vadesi Geçen Toplam')      
      and CAST(GETDATE() - o.Tarih as int) > (select Deger from Parametre  where Param = 'Vadesi Geçen Kilitlenecek Gün')       
 ) AND Kilitli='Açık'      
       
 update AMUSTERI SET  Kilitli='Açık' WHERE MUSNO NOT IN (      
select  distinct o.CariNo  from #odeme o      
where o.Devir > (select Deger from Parametre  where Param = 'Vadesi Geçen Toplam')      
      and CAST(GETDATE() - o.Tarih as int) > (select Deger from Parametre  where Param = 'Vadesi Geçen Kilitlenecek Gün')       
) AND Kilitli='Kilitli'      
      
select      
  a.MUSNO  [Cari No],      
  a.MUSKOD [Cari Kodu],      
  a.MUSADI [Cari Adı],      
  o.IslemTuru [İşlem Türü],      
  o.IslemNo [İşlem No],      
  c.ACIKLAMA [Açıklama],      
  c.TARIH  [İşlem Tarihi],      
  o.Tarih [Vade Tarihi],      
  CAST(GETDATE() - o.Tarih as int) [Geciken Gün],      
  o.Devir,      
  o.Borc [Borç],      
  o.Bakiye,      
  d.FatSerNo [Fatura Seri No],      
  d.FaturaNo [Fatura No]      
 from #odeme o      
 inner join  AMUSTERI a on  o.CariNo = a.MUSNO      
 inner join  ACARI    c on c.ISLEMNO = o.IslemNo and  c.MUSNO = o.CariNo and c.BAACIKLA = o.IslemTuru     
 left join   STOKHARDETBAG d on o.IslemNo = d.IslemNo      
 Order by o.Tarih desc, o.NoId desc   
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.views where name='VStk_RenkBedenGirisCikisDepo')
drop VIEW VStk_RenkBedenGirisCikisDepo
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE VIEW VStk_RenkBedenGirisCikisDepo   
AS  
SELECT StokKodu,  
  Beden,  
  Renk,  
        (SELECT ISNULL(SUM(Miktar), 0) AS Expr1 FROM dbo.RenkBedenHareket   
        WHERE (StokKodu = AnaTablo.StokKodu) AND   
     (GC = 'G') AND   
     (Beden = AnaTablo.Beden) and   
     (Renk = AnaTablo.Renk) AND   
     (Depo = AnaTablo.Depo) and  
     (HT = AnaTablo.HT)) AS Giris,  
  (SELECT ISNULL(SUM(Miktar), 0) AS Expr1 FROM dbo.RenkBedenHareket  
   WHERE (StokKodu = AnaTablo.StokKodu) AND   
      (GC = 'C') AND   
      (Beden = AnaTablo.Beden) and   
     (Renk = AnaTablo.Renk) AND   
     (Depo = AnaTablo.Depo) and  
     (HT = AnaTablo.HT)) AS Cikis,  
  (SELECT ISNULL(SUM(Miktar), 0) AS Expr1 FROM dbo.RenkBedenHareket   
        WHERE (StokKodu = AnaTablo.StokKodu) AND   
     (GC = 'G') AND   
     (Beden = AnaTablo.Beden) and   
     (Renk = AnaTablo.Renk) AND   
     (Depo = AnaTablo.Depo)and  
     (HT = AnaTablo.HT))-  
  (SELECT ISNULL(SUM(Miktar), 0) AS Expr1 FROM dbo.RenkBedenHareket  
   WHERE (StokKodu = AnaTablo.StokKodu) AND   
      (GC = 'C') AND   
      (Beden = AnaTablo.Beden) and   
     (Renk = AnaTablo.Renk) AND   
     (Depo = AnaTablo.Depo) and  
     (HT = AnaTablo.HT)) AS Fark,  
     Depo, HT,
	 (select rb.Barkodu from RenkBedenTablosu rb where rb.StokKodu = AnaTablo.StokKodu and rb.Beden = AnaTablo.Beden and rb.Renk = AnaTablo.Renk) Barkodu
FROM         dbo.RenkBedenHareket AS AnaTablo  
group by StokKodu,Beden,Renk,Depo,HT
go--
 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE Name='uidx_IsoBirim_01')
BEGIN
CREATE UNIQUE INDEX uidx_IsoBirim_01 ON [ISOBIRIM] ([IsoBirimKodu],[IsoBirimAdi]);
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   				  
 				  
		 
             				  
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'AnaMenu')
begin
	ALTER TABLE AnaMenuAyar ADD AnaMenu BIT
end ELSE BEGIN
	ALTER TABLE AnaMenuAyar ALTER COLUMN AnaMenu BIT
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.1.1')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.1.1',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END

go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AcariTakas') and name=N'DovizKur')
begin
	ALTER TABLE AcariTakas ADD DovizKur float
end ELSE BEGIN
	ALTER TABLE AcariTakas ALTER COLUMN DovizKur float
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'Kart')
begin
	ALTER TABLE AnaMenuAyar ADD Kart BIT
end ELSE BEGIN
	ALTER TABLE AnaMenuAyar alter column Kart BIT
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'Rapor')
begin
	ALTER TABLE AnaMenuAyar ADD Rapor BIT
end ELSE BEGIN
	ALTER TABLE AnaMenuAyar alter column Rapor BIT
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'IslemGiris')
begin
	ALTER TABLE AnaMenuAyar ADD IslemGiris BIT 
end ELSE BEGIN
	ALTER TABLE AnaMenuAyar ALTER COLUMN IslemGiris BIT 
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
delete GridView where KayitIsmi in ('FrmStokIslemlericxGrid1cxGrid1DBTableView1',
 				'FrmFaturaIslemlericxGrid1cxGrid1DBTableView1',
 				'frmCaricxCariHareketlertvACARI',
 				'FrmCariDetRapcxGrid1tvHareketler',
 				'FrmFCariDovizlicxGrid1tvACARI',
 				'FrmCariIslemlericxGrid1cxGrid1DBTableView1')
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spMalSayimiDuzenle')
drop proc spMalSayimiDuzenle
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create  proc spMalSayimiDuzenle 
as
---Malsayım detbag kaydı olmayanları düzenler
declare @IslemNo int declare @GC nvarchar(1) declare @KullaniciAdi nvarchar(20) declare @ParamKullaniciNo int
declare @DovizKodu nvarchar(2) declare @DovizId smallint declare @Depo nvarchar(30) declare @KullaniciDepartmani nvarchar(8)
declare @Tutar money declare @KdvTutari money declare @DovizliTutar money declare @SevkTarihi datetime declare @DovizKuru money=1

declare @FaturaTarihi datetime 

declare StoklarCursor cursor            
for            
	Select IslemNo,SUM(Tutar) Tutar,SUM(KdvTutari)KdvTutari,SUM(DovizliTutar) DovizliTutar,SevkTarihi
	from (
		select	IslemNo,
				CASE WHEN GC = 'G' THEN SonTutar ELSE SonTutar * (-1) end Tutar, 
				CASE WHEN GC = 'G' THEN KdvTutari ELSE KdvTutari * (-1) end KdvTutari, 
				CASE WHEN GC = 'G' THEN DovizTut ELSE DovizTut * (-1) end DovizliTutar, 
			   SiparisTarihi as SevkTarihi  
		from STOKHAR where IslemTuru='Mal Sayımı' and not exists (select * from STOKHARDETBAG where IslemNo=STOKHAR.IslemNo and IslemTuru=STOKHAR.IslemTuru)  
	) tbl
	GROUP BY IslemNo,SevkTarihi

         
 open StoklarCursor            
 fetch next from StoklarCursor into @IslemNo,@Tutar,@KdvTutari,@DovizliTutar,@SevkTarihi           
            
while @@FETCH_STATUS =0             
begin

select top 1 @FaturaTarihi=FaturaTarihi,@KullaniciAdi=KULADI,@ParamKullaniciNo=KULNO,
@DovizKodu=DovizKodu,@DovizId=DovizCinsi,@Depo=Depo,@KullaniciDepartmani=Departman,@DovizKuru=DovizKuru from STOKHAR where IslemNo=@IslemNo and IslemTuru='Mal Sayımı' 


IF @Tutar >= 0
	SET @GC = 'G'
ELSE
	SET @GC = 'C';

exec spStokHarDetBagKaydet /*@IslemNo*/ @IslemNo, /*@IslemTuru*/ 'Mal Sayımı',/*:CariNo*/0,/*:CariKodu*/'Mal Sayımı',/*:CariAdi*/'Mal Sayımı',/*@Tutar*/ @Tutar,                       
   /*@SiparisTarihi*/ @FaturaTarihi,/*@KdvTutari*/ @KdvTutari, /*@FisTuru*/'M',/*:GC*/@GC,/*:EkIskTut1*/0 ,/*:EkIskTut2*/0 ,/*:EkIskTut3*/0 ,/*:EkIskTutTop*/0 ,                    
   /*:FaturaNo*/0,/*:FaturaTarihi*/ @FaturaTarihi, /*:Satici*/'',/*:DovizTut*/@DovizliTutar ,/*:DovizCinsi*/@DovizId ,/*:DovizKodu*/@DovizKodu ,/*  @DovizTutB*/@DovizliTutar,                    
   /*@BelgeNo*/'', /*@OzelKod1*/'',/*@OzelKod2*/'',/*@OzelKod3*/'',/*@IslemTipi*/'',/*@IrsNo*/'',/*@Ack1*/'',/*@Ack2*/'',/*@Ack3*/'',/*@Ack4*/'',/*:KULNO*/@ParamKullaniciNo ,
   /*:KULADI*/@KullaniciAdi ,                    
   /*@FatSerNo*/'',/*@SaticiAdi*/'',/*@AltIskYuzde1*/0,/*@AltIskYuzde2*/0,/*@AltIskYuzde3*/0,/*@AK*/'',/*@FaturaOPlanGunu*/0,/*@Depo*/@Depo,/*@GunAy*/0,/*@OpsiyonGunu*/0,                    
   /*@AcikKapali*/'A',/*@DovizBorc*/0,/*@DovizAlacak*/0,/*@FYaz*/'',/*@SecimEH*/'',/*@IslemNo3*/0,/*@TP*/'TF',/*@FisNo*/'',/*@VardiyaKodu*/'',/*@VardiyaDurumu*/'',/*@Departman*/@KullaniciDepartmani,                    
   /*@MasrafToplami*/0,/*@KDVDH*/'H',/*@IslemNo4*/0,/*@KayitDuzeyi*/0,/*@YazarKasaNo*/0,/*@YazarKasaFisNo*/'',/*@YaziciCount*/0,/*@IsIstasyonKodu*/'',/*@IsIstasyonAdi*/'',                    
   /*@TevkifatTutari*/0,/*@VardiyaNo*/'',/*@OdemeBilgisi*/'',/*@FaturaAdresNo*/ 0,/*@SevkAdresNo*/ 0,/*@FI*/ 'E',/*@SevkTarihi*/@SevkTarihi,/*@eFaturaNo*/'',                    
   /*@UUID*/ '',/*@eFaturaGonderimTarihi*/ null,/*@eFaturaDurumu*/'',/*@FaturaTipi*/0,@DovizKuru

  fetch next from StoklarCursor into @IslemNo,@Tutar,@KdvTutari,@DovizliTutar,@SevkTarihi              
end            
            
close StoklarCursor            
deallocate StoklarCursor 
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'Tevkifat') and name=N'IslemNo2')
begin
	ALTER TABLE Tevkifat ADD IslemNo2 int
end ELSE BEGIN
	ALTER TABLE Tevkifat ALTER COLUMN IslemNo2 int
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE t SET t.IslemNo2 = h.IslemNo2 from Tevkifat t,STOKHAR h where t.StokNo = h.StokNo and t.IslemNo in (h.IslemNo,h.IslemNo3) 
go-- 
		 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_DepoSifirla')
drop proc sp_DepoSifirla
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create   procedure sp_DepoSifirla(@ParamDepo varchar(250),@ParamGrupAdi varchar(250),@ParamKullaniciNo int,            
@ParamReferansFiyat varchar(100),@ParamTarih nvarchar(8),@BelgeNo varchar(20) = '',@BelgeAciklama varchar(100) = '' )            
as                     
create table #Stoklar(StokNo int ,Kodu varchar(50),StokKodu varchar(50),StokAdi varchar(500),GrupAdi varchar(500),UreticiFirmaKodu varchar(250),            
OzelKod1 varchar(250), OzelKod2 varchar(250), AnaBirim varchar(250),            
AktifPasif varchar(5), Giris float, Cikis float, StokAdedi float, Depo varchar(500),Fiyat money)            
            
declare @SiraNo int             
declare @KacKayitVar int            
declare @FaturaTarihi datetime            
declare @FaturaMiktari money declare @Miktar  money declare @BirimMiktar money declare @BirimFiyat money declare @NFiyat money             
declare @Tutar money declare @TopTutar money declare @DovizTutB money declare @DovizTut money declare @GenelToplamTutar money             
declare @DovizGunFiyati money             
declare @DovizliTutar money             
declare @NFiyatDovBr money            
            
declare @GC varchar(50) declare @DovizId varchar(50)              
declare @DovizKodu varchar(50)  declare @IslemNo  varchar(50)             
declare @KullaniciAdi varchar(250)            
declare @KullaniciVardiya varchar(250)            
declare @KullaniciVardiyaNo varchar(250)            
            
declare @KullaniciDepartmani varchar(50)            
declare @SqlText nvarchar(max)            
     
    
    
set @KullaniciVardiya='99';            
set @KullaniciVardiyaNo='20090101';            
            
select top 1 @KullaniciDepartmani=Departman,@KullaniciAdi=GirisAdi from Kullanicilar where No=@ParamKullaniciNo            
            
set @SqlText='UPDATE STOKKARTI ' +            
                       ' SET StokAdedi = (SELECT ISNULL(SUM(Miktar), 0) from STOKHAR ' +            
                       ' WHERE StokKodu = STOKKARTI.Kodu and GC=''G'' and FisTuru<>''İ'')-' +            
                       ' (SELECT ISNULL(SUM(Miktar), 0) from STOKHAR '+            
                       ' WHERE StokKodu = STOKKARTI.Kodu and GC=''C'' and FisTuru<>''İ'')';            
            
exec(@SqlText)            
            
 select top 1 @DovizKodu=Deger from KullaniciAyarlari where Ayar='VarsayilanDovizKodu'            
 select TOP 1 @DovizId=NoId,@DovizGunFiyati=GunFiyati from DOVIZT where DovizKodu=@DovizKodu            
             
  SELECT @IslemNo = ISLEMNO FROM ADIGSABITLER              
  Update ADIGSABITLER SET ISLEMNO=ISLEMNO+2              
  set @SiraNo = 1            
  set @GenelToplamTutar = 0            
    
  insert into #Stoklar            
   exec sp_DepoDurumlari @ParamTarih    
     
  delete from #Stoklar where @ParamDepo != Depo  
      
  if @ParamGrupAdi<>''    
  delete from #Stoklar where GrupAdi<>@ParamGrupAdi    
      
declare @StokNo int             
declare @Kodu varchar(50)            
declare @StokAdi varchar(500)            
declare @GrupAdi varchar(500)            
declare @UreticiFirmaKodu varchar(250)            
declare @OzelKod1 varchar(250)            
declare @OzelKod2 varchar(250)            
declare @AnaBirim varchar(250)            
declare @AktifPasif varchar(5)            
declare @Giris float            
declare @Cikis float            
declare @StokAdedi float            
declare @Depo varchar(500)            
declare @Fiyat money            
            
declare StoklarCursor cursor            
for            
 select StokNo,Kodu ,StokAdi ,GrupAdi ,UreticiFirmaKodu ,OzelKod1 , OzelKod2 , AnaBirim ,            
  AktifPasif , Giris , Cikis , StokAdedi , Depo,Fiyat  from #Stoklar            
 open StoklarCursor            
 fetch next from StoklarCursor into @StokNo,@Kodu,@StokAdi,@GrupAdi,@UreticiFirmaKodu,@OzelKod1,@OzelKod2,@AnaBirim,            
  @AktifPasif,@Giris,@Cikis,@StokAdedi,@Depo,@Fiyat            
            
while @@FETCH_STATUS =0             
begin            
            
set @Miktar = 0;            
            
    if @StokAdedi> 0 begin   
      set @FaturaMiktari = @StokAdedi;            
      set @Miktar    = @StokAdedi;            
      set @BirimMiktar   = 1;   
      set @BirimFiyat    = @Fiyat;            
      set @NFiyat        = @Fiyat;            
      set @Tutar         = @Fiyat * @StokAdedi;            
      set @TopTutar      = @Fiyat*@StokAdedi;            
      set @GC            = 'C';            
      set @DovizTutB     = @Fiyat/ @DovizGunFiyati;            
      set @DovizTut      = @StokAdedi* @DovizTutB;            
    end else            
    if @StokAdedi< 0 begin            
      set @FaturaMiktari = -(@StokAdedi);            
      set @Miktar        = -(@StokAdedi);            
      set @BirimMiktar   = 1;            
      set @BirimFiyat    = @Fiyat;            
      set @NFiyat        = @Fiyat;            
      set @Tutar         = @Fiyat * (-@StokAdedi);            
      set @TopTutar      = @Fiyat * (-@StokAdedi);            
      set @GC            = 'G';            
      set @DovizTutB     = @Fiyat / @DovizGunFiyati;            
      set @DovizTut      = (-(@StokAdedi))* @DovizTutB;            
    end;            
  set @GenelToplamTutar = @GenelToplamTutar + @TopTutar;            
set @FaturaTarihi=convert(datetime,@ParamTarih);  
set @DovizliTutar=@TopTutar/@DovizGunFiyati            
set @NFiyatDovBr=@Fiyat/@DovizGunFiyati            
  
 exec spStokHarKaydet  @IslemNo,'Mal Sayımı',0,'Mal Sayımı','Mal Sayımı',            
   @StokNo ,@Kodu ,@StokAdi,@Miktar,@AnaBirim,@BirimMiktar,        
   @BirimFiyat,0,0,0,0,@Tutar,@SiraNo,    @FaturaTarihi,0,0,0 ,0 ,'M',            
   @GC ,0 ,0 ,0 ,0 ,0 , @FaturaTarihi,               
   '' ,@TopTutar ,'H' ,@DovizliTutar ,@DovizId ,@DovizKodu ,            
            
   @DovizTutB ,@Depo ,@Fiyat ,@FaturaMiktari ,0 ,            
   0 ,0 ,@ParamKullaniciNo ,@KullaniciAdi ,'' ,0 ,            
   0 ,0 ,'A' ,0 ,0 ,            
               
   0 ,0 ,'' ,'' ,'' ,'' ,            
   'TF' ,@KullaniciVardiya ,'H' ,0 ,@KullaniciDepartmani ,@TopTutar ,            
   @DovizliTutar ,0 ,0 ,0 ,0 ,            
        
   0 ,@AnaBirim ,0 ,@Fiyat ,0 ,            
   0 ,0 ,@Fiyat ,@NFiyatDovBr ,'+ Ürün' ,@KullaniciVardiya ,            
   'Genel' ,@SiraNo ,0 ,0 ,0 ,0 , 0,            
            
   0 ,0,0 ,0 ,0 ,0 ,            
   0 ,0 ,0 ,'' ,'' , 0,            
   'YOK' , 'YOK','YOK' ,@FaturaMiktari ,0,0            
set @SiraNo = @SiraNo + 1;            
 fetch next from StoklarCursor into @StokNo,@Kodu,@StokAdi,@GrupAdi,@UreticiFirmaKodu,@OzelKod1,@OzelKod2,@AnaBirim,            
  @AktifPasif,@Giris,@Cikis,@StokAdedi,@Depo,@Fiyat            
end    
            
close StoklarCursor            
deallocate StoklarCursor    
EXEC spMalSayimiDuzenle
delete from STOKHAR where Miktar=0
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_StokFiyatDegistir')
drop proc sp_StokFiyatDegistir
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create    proc sp_StokFiyatDegistir(@ParamSeciliStokKodlari nvarchar(max),@ParamDegisecekFiyatIndex int=0,@ParamReferansAlanAdi nvarchar(50),              
@ParamIslemTur1 varchar(1)='',@ParamIslemDeger1 nvarchar(50)='1',@ParamIslemTur2 varchar(1)='',@ParamIslemDeger2 nvarchar(50)='1',@HaneSayisi nvarchar(10) = '8')              
as                           
  declare @i integer               
  declare @Sqlexec nvarchar(max)              
  declare @IslemDeger1 float              
  declare @IslemDeger2 float              
  declare @IslemDeger3 float              
              
  set @IslemDeger1=CONVERT(float,@ParamIslemDeger1)              
  set @IslemDeger2=CONVERT(float,@ParamIslemDeger2)                     
                
  declare @HesapSql nvarchar(max)              
  set @HesapSql=''              
              
  if @ParamDegisecekFiyatIndex>0              
  begin         
    -- Toplu fiyat güncelleme ekranında sayısal değer girilidğinde yapılacak işlem      
 declare @sonuc int = 0,@Deger float;      
 BEGIN TRY        
	SET @Deger =  CAST( REPLACE(@ParamReferansAlanAdi,',','.') as float) 
	SET @sonuc = 0;      
 END TRY        
 BEGIN CATCH        
	SET @sonuc = 1;      
 END CATCH        
   --      
   IF @sonuc = 0       
   BEGIN      
		SET @HesapSql=	' update StokFiyat SET F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Fiyat= (case F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Kdv when ''D'' then  '+REPLACE(@ParamReferansAlanAdi,',','.')+' '+                
 	' when ''H'' then  ('+REPLACE(@ParamReferansAlanAdi,',','.')+'/(100 + SatisKdv)) * 100 end )  where StokNo in ( '+@ParamSeciliStokKodlari+')  ';            
    
 --SET @HesapSql=' update StokFiyat SET F'+convert(varchar,@ParamDegisecekFiyatIndex)+'FiyatH='+REPLACE(@ParamReferansAlanAdi,',','.')+' where StokNo in ( '+@ParamSeciliStokKodlari+')';         
   END      
   ELSE      
   IF CHARINDEX('Fiyat',@ParamReferansAlanAdi) > 0           
   BEGIN      
		set @HesapSql=' update StokFiyat SET F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Fiyat=ROUND(((SF.'+@ParamReferansAlanAdi;              
              
		if( @ParamIslemTur1!='' and @ParamIslemDeger1!='')              
			set @HesapSql=@HesapSql+@ParamIslemTur1+@ParamIslemDeger1+')'    
		else     
			set @HesapSql =  @HesapSql + ')';    
              
		if( @ParamIslemTur2!='' and @ParamIslemDeger2!='')              
			set @HesapSql=@HesapSql+@ParamIslemTur2+@ParamIslemDeger2;      
       
		if @HesapSql!=''              
			set @HesapSql=@HesapSql+'),'+@HaneSayisi+')  from StokFiyat as SF where StokNo in ( '+@ParamSeciliStokKodlari+')';              
   END          
   ELSE          
   BEGIN         
		IF @ParamReferansAlanAdi = 'Fifo'
			SET @ParamReferansAlanAdi = 'FifoFiyat'

		IF @ParamReferansAlanAdi = 'Lifo'
			SET @ParamReferansAlanAdi = 'LifoFiyat'

			set @HesapSql=' update SF SET F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Fiyat=ROUND(((SF2.'+@ParamReferansAlanAdi;              
              
		if( @ParamIslemTur1!='' and @ParamIslemDeger1!='')              
			set @HesapSql=@HesapSql+@ParamIslemTur1+@ParamIslemDeger1+')'    
		else 
			set @HesapSql =  @HesapSql + ')';      
              
		if( @ParamIslemTur2!='' and @ParamIslemDeger2!='')              
			set @HesapSql=@HesapSql+@ParamIslemTur2+@ParamIslemDeger2;               
              
		if @HesapSql!=''              
			set @HesapSql=@HesapSql+'),'+@HaneSayisi+')   from StokFiyat as SF,StokFiyatlari SF2 where SF.StokNo = SF2.StokNo and  SF.StokNo in ( '+@ParamSeciliStokKodlari+')';             
   END;          
          
   set @Sqlexec=@HesapSql+CHAR(13)+CHAR(10)+' update StokFiyat SET F'+convert(varchar,@ParamDegisecekFiyatIndex)+'FiyatH  = '+     
                             ' (case F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Kdv when ''H'' then  F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Fiyat '+                
           ' when ''D'' then  (F'+convert(varchar,@ParamDegisecekFiyatIndex)+'Fiyat/(100 + SatisKdv)) * 100 end )  where StokNo in ( '+@ParamSeciliStokKodlari+')  ';            
             
  end               
 --  print(@Sqlexec)             
            
  set @Sqlexec=@Sqlexec+CHAR(13)+CHAR(10)+ ' update s                 
   set s.F1Fiyat = f.F1Fiyat,s.F1Kdv = f.F1Kdv,s.F1DovizKodu = f.F1DovizKodu,                
     s.F2Fiyat = f.F2Fiyat,s.F2Kdv = f.F2Kdv,s.F2DovizKodu = f.F2DovizKodu,              
     s.F3Fiyat = f.F3Fiyat,s.F3Kdv = f.F3Kdv,s.F3DovizKodu = f.F3DovizKodu,                
     s.F4Fiyat = f.F4Fiyat,s.F4Kdv = f.F4Kdv,s.F4DovizKodu = f.F4DovizKodu                
 from  STOKKARTI s,StokFiyat f                 
where  s.Kodu = f.Kodu                 
  and  s.StokNo in ( '+@ParamSeciliStokKodlari+') ';            
            
-- print(@Sqlexec)            
-- select @Sqlexec              
 exec(@Sqlexec)   
 go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_StokIslemleriListesi')
drop proc sp_StokIslemleriListesi
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE    PROCEDURE [dbo].[sp_StokIslemleriListesi]             
 @tarih1 varchar(20),             
 @tarih2 varchar(20),             
 @dep varchar(500),             
 @orderby int,             
 @arama varchar(100),             
 @GosterilecekKayit int,             
 @IslemTuru varchar(20),             
 @Durumu int,             
 @IslemTipi varchar(30),      
 @IslemNoLike bit = 0  ,    
 @FatSerNo  varchar(3),                    
 @eFaturaNo  varchar(30)    
 --with encryption             
AS            
declare @str varchar(max)            
declare @rc int             
declare @tmp table (trc int)         
declare @ayrac varchar(2)                    
declare @peFaturaNo varchar(50)            
if @eFaturaNo<>''            
select @peFaturaNo=UPPER(@eFaturaNo)            
else set @peFaturaNo=''            
set @ayrac='"';     
             
set @str='select count(*) from (select IslemNo from STOKHARDETBAG where FI = ''E'' and TP=''TF'' AND FaturaTarihi >='''+ @Tarih1+''' and FaturaTarihi <= '''+@Tarih2+'''';             
    
if @peFaturaNo<>''                    
set @str=@str+'and (upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') OR upper(EIrsaliyeNo) like upper(''%'+@peFaturaNo+'%'')) '       
    
if @FatSerNo<>''                    
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '      
    
if @dep <> ''             
 set @str=@str+' and Departman in ('+@Dep+')'             
if @IslemTuru <> 'Tümü'             
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''             
if @IslemTipi <> 'Tümü' and @IslemTipi <> ''             
 set @str=@str+' and IslemTipi like ''%'+@IslemTipi+'%'''                      
        
if @Durumu = 1                                
 set @str=@str+' and IslemTuru in (''Alış'',''Alış İade'',''Satış'',''Satış İade'') and FisTuru = ''I''  and AcikKapali=''A'' ' --Açıklar   /* and not exists (select IslemNo from STOKHAR where STOKHAR.IslemNo3=STOKHARDETBAG.IslemNo)*/                      
             
if @Durumu = 2                                
 set @str=@str+' and not(IslemTuru in (''Alış'',''Alış İade'',''Satış'',''Satış İade'') and FisTuru = ''I''  and AcikKapali=''A'') ' --Kapalılar                                
if @Durumu = 3                                
 set @str=@str+' and FisTuru > ''2''' --Stokİşlemi değilse                
            
if @arama <> ''       
begin            
if @orderby=0       
begin      
  if @IslemNoLike = 0      
  set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '       
  else      
  set @str=@str+'and IslemNo like ''%'+@arama+'%'' '       
 end;      
      
 if @orderby=1             
  set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '             
 if @orderby=2             
  set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '             
 if @orderby=3             
  set @str=@str+'and upper(SiparisTarihi) like upper(''%'+@arama+'%'') '             
 if @orderby=4             
  set @str=@str+'and (upper(IrsNo) like upper('''+@arama+'%'')  or upper(EIrsaliyeNo) like upper('''+@arama+'%''))'             
 if @orderby=5             
  set @str=@str+'and upper(IslemNo) like upper('''+@arama+'%'') '             
end;             
set @str=@str+')Y';             
     
insert @tmp exec(@str)             
select @rc = trc from @tmp;             
             
declare @tstr varchar(1000);             
             
if @orderby=0             
 set @tstr='IslemNo'             
if @orderby=1             
 set @tstr='CariAdi'             
if @orderby=2             
 set @tstr='FaturaNo'             
if @orderby=3             
 set @tstr='SiparisTarihi'             
if @orderby=4             
 set @tstr='IrsNo'             
if @orderby=5             
 set @tstr='IslemNo'             
             
set @str='select * from STOKHARDETBAG '+             
 'where IslemNo in (select X.IslemNo '+             
 'from (select ROW_NUMBER() over (order by '+@tstr+') as sira, IslemNo '+             
 'from STOKHARDETBAG where FI = ''E'' and TP=''TF'' and FaturaTarihi >='''+@Tarih1+''' and FaturaTarihi <= '''+@Tarih2+'''';             
    
if @peFaturaNo<>''                    
set @str=@str+'and (upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') OR upper(EIrsaliyeNo) like upper(''%'+@peFaturaNo+'%''))'       
    
if @FatSerNo<>''                    
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '      
    
if @dep <> ''             
 set @str=@str+' and Departman in ('+@Dep+')'             
if @IslemTuru <> 'Tümü'             
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''             
if @IslemTipi <> 'Tümü' and @IslemTipi <> ''             
 set @str=@str+' and IslemTipi = '''+@IslemTipi+''''             
            
if @Durumu = 1                                
 set @str=@str+' and IslemTuru in (''Alış'',''Alış İade'',''Satış'',''Satış İade'') and FisTuru = ''I''  and AcikKapali=''A'' ' --Açıklar     /* and not exists (select IslemNo from STOKHAR where STOKHAR.IslemNo3=STOKHARDETBAG.IslemNo)*/                   
  
if @Durumu = 2                                
 set @str=@str+' and not(IslemTuru in (''Alış'',''Alış İade'',''Satış'',''Satış İade'') and FisTuru = ''I'' and AcikKapali=''A'') ' --Kapalılar                                
if @Durumu = 3                                
 set @str=@str+' and FisTuru > ''2''' --Stokİşlemi değilse               
            
if @arama <> ''       
begin      
 if @orderby=0       
 begin      
  if @IslemNoLike = 0      
  set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '       
  else      
  set @str=@str+'and IslemNo like ''%'+@arama+'%'' '       
 end;         
 if @orderby=1             
  set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '             
 if @orderby=2             
  set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '             
 if @orderby=3        set @str=@str+'and upper(SiparisTarihi) like upper(''%'+@arama+'%'') '             
 if @orderby=4             
  set @str=@str+'and (upper(IrsNo) like upper('''+@arama+'%'')  or upper(EIrsaliyeNo) like upper('''+@arama+'%''))'             
 if @orderby=5             
  set @str=@str+'and upper(IslemNo) like upper('''+@arama+'%'') '             
end;             
set @str=@str+')X where X.sira >'+convert(varchar,@rc - @GosterilecekKayit) +') order by ';             
             
if @orderby=0             
 set @str=@str+'IslemNo'             
if @orderby=1             
 set @str=@str+'CariAdi'             
if @orderby=2             
 set @str=@str+'FaturaNo'             
if @orderby=3             
 set @str=@str+'SiparisTarihi'             
if @orderby=4             
 set @str=@str+'IrsNo'             
if @orderby=5             
 set @str=@str+'IslemNo'             
       
--select @str as strislemsql           
exec(@str)   
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='sp_CARIEKSTRE')
drop proc sp_CARIEKSTRE
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[sp_CARIEKSTRE]                 
@MUSNO nvarchar(30), @ILKTARIH nvarchar(10),@SONTARIH nvarchar(10),                     
 @KAPALIHAREKET nvarchar(1),@ORTVADESEC nvarchar(1),@ISLEMTIPINEGORE SMALLINT=0,@ISLEMTIPINEGOREBAKIYEGRUPLA SMALLINT=0                 
 as    
 --  2024-05-18  ACIKLAMA 400 OLDU
BEGIN          
          
declare @pIslemTipi nvarchar(250)          
declare @pBAKIYE1 money          
           
           
DECLARE @ACARI TABLE (ROWNUMBER INT,TARIH DATETIME, TESSIRNO INT, MUSNO INT, BAACIKLA nvarchar(14),ACIKLAMA nvarchar(400),                    
  ISLEMNO INT,DOVIZTUT float,DOVIZKODU nvarchar(2),OZELKOD nvarchar(3),ISLEMTIPI nvarchar(30),                    
  BELGENO nvarchar(10), ACIKLABAKIYE nvarchar(1), TP nvarchar(2), [Ort Vade] DATETIME,SaticiKodu nvarchar(15),                    
  SaticiAdi nvarchar(25), KULNO INT,KULADI nvarchar(20), DovizBorc float,                    
  DovizAlacak float,Aciklama2 nvarchar(200),                    
  SiraNo INT, CARINO INT, MUSKOD nvarchar(20),MUSADI nvarchar(50), IsIstasyonKodu nvarchar(30),                     
  IsIstasyonAdi nvarchar(30), TESLIM nvarchar(200),BORC money, ALACAK money,  FARK money,                     
  BAKIYE money,eFaturaNo varchar(30),eFaturaDurumu varchar(20),eFaturaGonderimTarihi datetime,OdemeBilgisi nvarchar(100),DovizKur float)                    
DECLARE @BAKIYE money, @IBKY MONEY,@BAKIYE2 money                    
SELECT  @IBKY= SUM(BORC-ALACAK)  FROM ACARI WHERE MUSNO=@MUSNO AND TARIH<=@ILKTARIH                    
SET @BAKIYE = 0                    
IF @IBKY IS NULL                    
     SET @IBKY=0            
            
IF @ISLEMTIPINEGORE=1          
BEGIN          
  INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO) VALUES (0,@ILKTARIH,@MUSNO,@IBKY,0,'Devir','Hesap Ekstresi Devir Tutarı',1);          
          
INSERT INTO @ACARI          
SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA,  
(ISNULL(ACIKLAMA, '''')+ ' - - ' +ISNULL((select TOP 1 STDB.Ack1 FROM STOKHARDETBAG as STDB where STDB.IslemNo=ACARI.ISLEMNO),'')) as ACIKLAMA,          
  
 ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,           
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as Ort_Vade,SaticiKodu,          
  SaticiAdi, KULNO, KULADI, DovizBorc,           
  DovizAlacak,Aciklama2,          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,          
  BORC money, ALACAK money,(BORC-ALACAK), NULL,           
  (select top 1 A.eFaturaNo  from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaNo,               
 (select top 1 A.eFaturaDurumu  from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaDurumu,           
 (select top 1 A.eFaturaGonderimTarihi from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaGonderimTarihi,        
 (select top 1 OdemeBilgisi from STOKHARDETBAG where IslemNo=ACARI.ISLEMNO ) as OdemeBilgisi,DovizKur         
 FROM ACARI           
 WHERE MUSNO=@MUSNO AND TARIH >@ILKTARIH AND TARIH<@SONTARIH           
 and 1=             
 case  when @KAPALIHAREKET is not null then  case  when AcikKapali <> @KAPALIHAREKET then 1 else 0 end else 1 end            
 ORDER BY ROWNUMBER1 ASC           
          
           
 END           
 ELSE       
 BEGIN          
INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO) VALUES (0,@ILKTARIH,@MUSNO,@IBKY,0,'Devir','Hesap Ekstresi Devir Tutarı',1)          
 INSERT INTO @ACARI           
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH,CARINO  ,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA,  
   
(ISNULL(ACIKLAMA, '''')+ ' - - ' +ISNULL((select TOP 1 STDB.Ack1 FROM STOKHARDETBAG as STDB where STDB.IslemNo=ACARI.ISLEMNO),'')) as ACIKLAMA,       
  
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,           
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,          
  SaticiAdi, KULNO, KULADI, DovizBorc,           
  DovizAlacak,Aciklama2,          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,          
  BORC money, ALACAK money,(BORC-ALACAK), NULL,           
  (select top 1 A.eFaturaNo  from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaNo,               
 (select top 1 A.eFaturaDurumu  from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaDurumu,           
 (select top 1 A.eFaturaGonderimTarihi from STOKHARDETBAG as A where A.IslemNo=ACARI.ISLEMNO) eFaturaGonderimTarihi,        
 (select top 1 OdemeBilgisi from STOKHARDETBAG where IslemNo=ACARI.ISLEMNO ) as OdemeBilgisi,DovizKur        
 FROM ACARI           
 WHERE MUSNO=@MUSNO AND TARIH >@ILKTARIH AND TARIH<@SONTARIH           
 and 1=             
 case  when @KAPALIHAREKET is not null then  case  when AcikKapali <> @KAPALIHAREKET then 1 else 0 end else 1 end            
 ORDER BY ROWNUMBER ASC           
          
 END          
          
END            
      
declare @ROWNUMBER INT          
declare @IslemTipi1 varchar(250)          
          
declare AcariCursor cursor           
for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ROWNUMBER ASC           
FOR UPDATE OF BAKIYE OPEN AcariCursor          
FETCH NEXT FROM AcariCursor INTO @ROWNUMBER,@IslemTipi1          
WHILE @@FETCH_STATUS=0          
BEGIN           
          
IF @ISLEMTIPINEGORE=0           
BEGIN          
 SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER )          
 UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER          
          
end  else          
begin          
          
if @ROWNUMBER=0          
 set @pIslemTipi=@IslemTipi1;          
          
   if @pIslemTipi=@IslemTipi1          
   begin          
    SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)          
    UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER          
   end          
   else           
   begin          
    set @pIslemTipi=@IslemTipi1;          
    SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)          
    UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER          
   end          
end          
          
 FETCH NEXT FROM AcariCursor INTO @ROWNUMBER,@IslemTipi1          
END          
CLOSE AcariCursor           
DEALLOCATE AcariCursor          
          
if @ISLEMTIPINEGOREBAKIYEGRUPLA=0          
SELECT  * FROM @ACARI  ORDER BY ROWNUMBER          
else          
SELECT  ISLEMTIPI,sum(BORC) as BORC,sum(ALACAK) as ALACAK,sum(BORC-ALACAK) as BAKIYE FROM @ACARI          
where ISLEMTIPI is not null and MUSNO=@MUSNO  GROUP BY ISLEMTIPI     
 go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 if exists (select * from sys.procedures where name='spStkDINAMIKInvoiceIhracatGtip')
drop proc spStkDINAMIKInvoiceIhracatGtip
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE    proc [dbo].[spStkDINAMIKInvoiceIhracatGtip] @Tarih1 datetime ,@Tarih2 datetime      as  
 select K.Kodu,K.StokAdi, H.Birim , H.Miktar , (H.BirimFiyat/H.DovizKuru) DovizBirimFiyatı,H.DovizKodu,(H.BirimFiyat/H.DovizKuru*H.Miktar)      TOPLAM ,
 H.DovizKodu,      H.IslemNo,H.IslemNo3,K.Aciklama2   , K.StokAdi2 ,K.UreticiFirmaKodu Uretici , K.GrupAdi   from STOKHAR H LEFT JOIN STOKKARTI K ON H.StokKodu=K.Kodu    
 where H.FaturaTarihi>=@Tarih1 and H.FaturaTarihi <=@Tarih2 
 go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='spOdemeHareketSil')
drop proc spOdemeHareketSil
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE  PROCEDURE [dbo].[spOdemeHareketSil]   
   @IslemNo int,  
   @CariNo int,
   @BAAcikla nvarchar(50) = 'X'
AS  
	IF ISNULL(@BAAcikla,'X') = 'X'
		DELETE FROM Odeme WHERE (IslemNo = @IslemNo) and (CariNo = @CariNo)  
	ELSE
		DELETE FROM Odeme WHERE (IslemNo = @IslemNo) and (CariNo = @CariNo)  and BAAcikla = @BAAcikla;
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.1.2')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.1.2',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go--

if exists (select * from sys.procedures where name='sp_StokHar_Masraf_Dagit')
drop proc sp_StokHar_Masraf_Dagit
go--
create  proc sp_StokHar_Masraf_Dagit(@IslemNo int,@Tip int,@MasrafTutar float)
as
begin
	if @Tip = 0 --Masraf tutarını fiyata göre dağıt
	begin
		update har SET har.SatMasrafTutari =  (@MasrafTutar*har.SonTutar) / (select Tutar from STOKHARDETBAG det where det.IslemNo in (har.IslemNo,har.IslemNo3)) from STOKHAR har where IslemNo = @IslemNo
	end;
	if @Tip = 1   --Masraf tutarını eşit dağıt
	begin
		update har SET har.SatMasrafTutari = @MasrafTutar /(select count(*) from STOKHAR where  IslemNo = @IslemNo or IslemNo3 = @IslemNo) from STOKHAR har where IslemNo = @IslemNo or IslemNo3 = @IslemNo
	end;

	UPDATE STOKHARDETBAG SET MasrafToplami = @MasrafTutar where IslemNo = @IslemNo;
end;
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'GELGID') and name=N'DovizKuru')
begin
ALTER TABLE GELGID ADD DovizKuru Float
end
else begin
ALTER TABLE GELGID alter column DovizKuru float
end
go--
if exists (select * from sys.procedures where name='spGelirGiderYaz')
drop proc spGelirGiderYaz
go--
CREATE  PROCEDURE [dbo].[spGelirGiderYaz]   
   @BORC float,  
   @ALACAK float,  
   @ACIKLAMA nvarchar(50),  
   @GELGIDKODU nvarchar(30),  
   @GELGIDADI nvarchar(150),  
   @MUSNO int,  
   @MUSADI nvarchar(50),  
   @TARIH datetime,  
   @BA nvarchar(1),  
   @BAACIKLA nvarchar(12),  
   @KODU nvarchar(5),  
   @ISLEMNO int,  
   @MUSKOD nvarchar(20),  
   @DOVIZTUT float,  
   @DOVIZCINSI int,  
   @DOVIZKODU nvarchar(2),  
   @KASAADI nvarchar(10),  
   @KASAKODU nvarchar(6),  
   @KULNO int,  
   @KULADI nvarchar(20),  
   @AcikKapali nvarchar(3),  
   @DovizBorc float,  
   @DovizAlacak float,  
   @VardiyaKodu nvarchar(15),  
   @VardiyaDurumu nvarchar(9),  
   @Departman nvarchar(8),  
   @HesapKodu nvarchar(25),  
   @HesapAdi nvarchar(50),  
   @KarsiHesp nvarchar(5),  
   @KayitDuzeyi int,  
   @BelgeNo nvarchar(30),  
   @OzelKod1 nvarchar(10),  
   @OzelKod2 nvarchar(10),  
   @IsIstasyonKodu nvarchar(30),  
   @IsIstasyonAdi nvarchar(30),  
   @VardiyaNo nvarchar(8),  
   @BordroNosu int,  
   @BordroAciklamasi nvarchar(100),  
   @Modul nvarchar(3),      
   @SaticiKodu nvarchar(10),  
   @SaticiAdi nvarchar(25),  
   @Tutar float,  
   @KdvTutari float,  
   @Kdv float,  
   @StopajTutari float,  
   @Stopaj float,  
   @FisNo int,
   @DovizKuru float
AS  
  
 INSERT GELGID VALUES ( @BORC, @ALACAK, @ACIKLAMA,  
    @GELGIDKODU, @GELGIDADI, @MUSNO,  
    @MUSADI,@TARIH, @BA,@BAACIKLA,  
    @KODU, @ISLEMNO,@MUSKOD,@DOVIZTUT,  
    @DOVIZCINSI,@DOVIZKODU, @KASAADI,  
    @KASAKODU,@KULNO,@KULADI,@AcikKapali,  
    @DovizBorc,@DovizAlacak,@VardiyaKodu,  
    @VardiyaDurumu, @Departman,@HesapKodu,  
    @HesapAdi,@KarsiHesp,@KayitDuzeyi,  
    @BelgeNo,@OzelKod1,@OzelKod2,@IsIstasyonKodu,  
    @IsIstasyonAdi,@VardiyaNo,@BordroNosu, @BordroAciklamasi,  
    @Modul,@SaticiKodu,@SaticiAdi,@Tutar,@KdvTutari,@Kdv,@StopajTutari,@Stopaj, @FisNo,@DovizKuru)  
go--
if exists (select * from sys.procedures where name='StokKodAdDegistir')
drop proc StokKodAdDegistir
go--
create     procedure StokKodAdDegistir           
@STOKADI nvarchar(500),          
@STOKKODU nvarchar(40),          
@EskiStokKodu nvarchar(40),         
@STOKADI2 nvarchar(40) = ''      
as          
begin Transaction             
 UPDATE STOKHAR SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE MuadilStoklar SET MuadilStokAdi = @STOKADI, MuadilStokKodu= @STOKKODU where MuadilStokKodu= @EskiStokKodu          
 UPDATE StokFiyat SET Kodu= @STOKKODU where Kodu= @EskiStokKodu          
 UPDATE StokDB SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE StokFiyatLog SET Kodu= @STOKKODU where Kodu= @EskiStokKodu          
 UPDATE StokMarkalari SET StokKodu= @STOKKODU where StokKodu=@EskiStokKodu          
 UPDATE Tedarikciler SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE PYazarKasaStoklari SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE PYazarKasaHar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE PPetrolHar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE PDegFiyatlar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE DonemselFiyatListesi SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE TekstilHareketTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE TekstilHataHareketTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE TekstilHataliStokVirmanSablonu SET HataliStokKodu= @STOKKODU where HataliStokKodu= @EskiStokKodu          
 UPDATE TekstilHataliStokVirmanSablonu SET VirmanStokKodu= @STOKKODU where VirmanStokKodu= @EskiStokKodu          
 UPDATE TekstilIsEmirleriDetayi SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE TekstilKesimTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE TekstilStokRenkTanimlari SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE CariUretimFiyatlari SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE Seri SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE Tarim_AmbalajStok SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 UPDATE Tarim_AmbalajStok SET AmbalajKodu= @STOKKODU where AmbalajKodu= @EskiStokKodu          
 UPDATE StokDB SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu          
 update MH_Connect SET Degeri =@STOKKODU where Degeri=@EskiStokKodu and TabloAdi='STOKKARTI' and FieldAdi='Kodu'          
 Update StokTablolari   Set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu            
 Update Tarim_AmbalajStok  Set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu            
 Update Tarim_AmbalajStok  Set AmbalajKodu=@STOKKODU Where AmbalajKodu=@EskiStokKodu            
 Update Tarim_SatisEmirleri  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update Tarim_UretimDetay  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update Tarim_UretimDetay  Set KasaKodu=@STOKKODU Where KasaKodu=@EskiStokKodu            
 Update Tarim_UretimDetay  Set PaletKodu=@STOKKODU Where PaletKodu=@EskiStokKodu            
 Update Tarim_UretimDetay2  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update Tarim_UrunAlim   Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update Tarim_UrunAlimAmbalaj Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update Tarim_UrunAlimHareket Set KasaKodu=@STOKKODU Where KasaKodu=@EskiStokKodu            
 Update Tarim_UrunAlimHareket Set PaletKodu=@STOKKODU Where PaletKodu=@EskiStokKodu            
 Update StokTartim    Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu            
 Update StokTartimPalet   Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu           
 Update CariDonemFiyatlari set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update ElTerminaliDepoSayim set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu           
 Update ElTerminaliYukleme set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu           
 Update EtiketBarkod set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu           
 Update MiktarsalHedefler set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update RenkBedenSeriTablosu set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update RenkBedenTablosu set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update SIPARISHAR set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu           
 Update TEKLIFHAR set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu             
 Update SeriNoKaliteKontrol set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update SipHar set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu         
 Update StokDepoMiktarlari set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update StokVergiMuafiyetleri set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 UPDATE InvoiceItemMap SET OurCode = @STOKKODU,SenderItemName = @STOKADI where OurCode = @EskiStokKodu          
 UPDATE STOKKARTI SET Kodu= @STOKKODU, StokAdi=@STOKADI,StokAdi2 = @STOKADI2 where Kodu= @EskiStokKodu       
 Update StokBarkodlari set StokKodu=@STOKKODU  Where StokKodu=@EskiStokKodu      
 Update StokBarkodlari set Barkodu=@STOKKODU  Where Barkodu=@EskiStokKodu         
        
commit; 
go--
IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='FaturaFiyatGuncellemeFarki')
 BEGIN 
CREATE TABLE [dbo].[FaturaFiyatGuncellemeFarki](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[IslemNo] [int] NULL,
	[Tarih] [datetime] NULL,
	[EskiTutar] [float] NULL,
	[YeniTutar] [float] NULL,
	[StokNo] [int] NULL,
 CONSTRAINT [PK_FaturaFiyatGuncellemeFarki] PRIMARY KEY CLUSTERED 
(
	[NoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
end
go--
IF  EXISTS (SELECT *  FROM sys.views WHERE  name='V_FaturaFiyatGuncelleme')
 BEGIN 
 drop view V_FaturaFiyatGuncelleme
 end
go--
CREATE    VIEW V_FaturaFiyatGuncelleme 
AS
	select F.Tarih,F.IslemNo ,round(F.EskiTutar*(100+SF.SatisKdv )/100*S.FaturaMiktari,2)EskiTutar ,
	round (F.YeniTutar*(100+SF.SatisKdv )/100*S.FaturaMiktari,2) YeniTutar,   
	round((YeniTutar-EskiTutar)*(100+SF.SatisKdv )/100*S.FaturaMiktari,2) Fark,
	S.StokKodu,
	S.StokAdi,
	S.CariKodu,
	A.MUSADI Cari_Adi,
	S.FaturaTarihi
	from FaturaFiyatGuncellemeFarki    F 
	LEFT JOIN STOKHAR S ON S.IslemNo=F.IslemNo 
	LEFT JOIN AMUSTERI A ON A.MUSKOD=S.CariKodu 
	LEFT JOIN StokFiyat SF ON SF.Kodu=S.StokKodu WHERE S.IslemNo=F.IslemNo AND S.CariNo = A.MUSNO and S.StokNo=F.StokNo
go--
if exists (select * from sys.procedures where name='sp_Prk_Satis_Cevirme_Tekli')
drop proc sp_Prk_Satis_Cevirme_Tekli
go--
create  procedure sp_Prk_Satis_Cevirme_Tekli  
(@IslemNo int,            
 @FaturaTarihi smalldatetime,  
 @FaturaNo integer,  
 @FatSerNo nvarchar(50),
 @YazarKasaFisNo integer,
 @FaturaTipi integer
)                  
as  
begin
	update STOKHAR SET  IslemTuru= 'Satış',FaturaTarihi = @FaturaTarihi where IslemNo = @IslemNo;
	update STOKHARDETBAG SET	
		   IslemTuru='Satış',
		   FaturaTarihi= @FaturaTarihi ,
		   FatSerNo=@FatSerNo ,
		   FaturaNo= @FaturaNo,
		   YazarKasaFisNo = @YazarKasaFisNo,
		   FaturaTipi =  @FaturaTipi
		   where IslemNo = @IslemNo;                                                     
	update dbo.ACARI  SET ACIKLAMA=  @FaturaNo + ' no.lu Fatura',BAACIKLA = 'Satış', TARIH = @FaturaTarihi where ISLEMNO = @IslemNo;        
    update Odeme SET BAAcikla='Satış', Tarih =  @FaturaTarihi where IslemNo = @IslemNo;                      
end;
go--
if exists (select * from sys.procedures where name='sp_AdresStokHareketRap')
drop proc sp_AdresStokHareketRap
go--
      				   
CREATE  PROCEDURE sp_AdresStokHareketRap
(@CariKodu nvarchar(25),
 @SevkTarihi1 smalldatetime,
 @SevkTarihi2 smalldatetime,
 @AdresNo int
)
AS
SELECT IslemNo,
	   IslemTuru,
	   FaturaNo,
	   eFaturaNo,
	   FaturaTarihi,
	   SevkTarihi,
	   CAST(AdresNo as nvarchar(10))+ ' - ' + Adres1 + ' ' + Adres2 + ' İl - İlçe : ' + Il + ' ' + Ilce Adres,
	   Tutar,
	   Aciklama,
	   DovizKodu,
	   DovizKur
  FROM STOKHARDETBAG db,Adres adr
 WHERE db.CariNo = adr.CariNo
   AND db.SevkAdresNo = adr.AdresNo
   AND db.CariKodu = @CariKodu
   AND ((@SevkTarihi1 IS NULL) OR (db.SevkTarihi >= @SevkTarihi1))
   AND ((@SevkTarihi2 IS NULL) OR (db.SevkTarihi <= @SevkTarihi2))
   AND ((@AdresNo IS NULL) OR (AdresNo = @AdresNo))
ORDER BY FaturaTarihi DESC
go--
      				   
if exists (select * from sys.procedures where name='StokNo_Update')
drop proc StokNo_Update
go--
      				   
CREATE  PROC StokNo_Update(@Degistir bit = 0)
AS 
BEGIN
	IF @Degistir = 1
	BEGIN
		UPDATE n  SET StokNo = k2.SIRA  FROM STOKKARTI  n , (select Kodu,StokNo,NoId,RANK () OVER (order  BY   Kodu DESC) SIRA FROM STOKKARTI) k2 where n.Kodu=k2.Kodu 
	END;

	UPDATE n  SET StokNo = k2.StokNo  FROM STOKHAR  n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM SIPARISHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM StokBarkodlari   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM StokFiyat   n ,  STOKKARTI k2 where n.Kodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM StokDB   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM StokHarLog   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM MuadilStoklar   n ,  STOKKARTI k2 where n.MuadilStokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM Buton   n ,  STOKKARTI k2 where n.ButonDegeri =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM PDegFiyatlar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM SipHar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM TEKLIFHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu 
	UPDATE n  SET StokNo = k2.StokNo  FROM Tevkifat   n ,  STOKKARTI k2 where n.Kod =k2.Kodu 	
--- FaturaFiyatGuncellemeFarki -StokHarLog -Tedarikciler TABLOLARI KONTROL ET
END;
go--
      				   
if exists (select * from sys.procedures where name='sp_StokFiyat_Fiyat_FiyatH_Kodlu')
drop proc sp_StokFiyat_Fiyat_FiyatH_Kodlu
go--
      				   
CREATE PROCEDURE sp_StokFiyat_Fiyat_FiyatH_Kodlu
 @GelenStokKodu nvarchar(30),            
 @FiyatAdi nvarchar(30),            
 @Fiyat decimal(18,8),          
 @DovizKodu nvarchar(3),
 @KdvDHGuncelle bit = 0,
 @KdvDH char(1)
 /*@Iskonto1 float = 0,     
 @Iskonto2 float = 0,     
 @Iskonto3 float = 0,     
 @Iskonto4 float = 0  ,  
 @IskontoGuncelle bit = 0,*/
as          
begin      
 DECLARE @DovizKuru float      
 DECLARE @Query nvarchar(max),@Fyt nvarchar(3)      
    DECLARE @ResultDovizKodu nvarchar(3)      
      
 IF LEN(@FiyatAdi) = 7      
  SET @Fyt = SUBSTRING(@FiyatAdi,1,2)      
    ELSE       
  SET @Fyt = SUBSTRING(@FiyatAdi,1,3);      
      
 if @DovizKodu =''      
 BEGIN      
  SET @Query = 'select @DovizKod = '+@Fyt+'DovizKodu from StokFiyat where Kodu = '''+@GelenStokKodu+'''';       
  EXEC sp_executesql @Query,      
         N'@DovizKod nvarchar(3) OUTPUT',      
          @DovizKod = @ResultDovizKodu OUTPUT ;      
 SET @DovizKodu =  @ResultDovizKodu;      
 END;      
      
 SET @DovizKuru = (Select GunFiyati from DOVIZT where DovizKodu = @DovizKodu)      
 SET @Fiyat = @Fiyat / @DovizKuru;      

 IF @KdvDHGuncelle = 1 
 BEGIN
	SET @Query = 'UPDATE StokFiyat SET  '+@Fyt+'Kdv =  '''+@KdvDH+''' where Kodu = '''+@GelenStokKodu+''''; 
	EXEC sp_executesql @Query;	
 END

 SET @Query = 'UPDATE StokFiyat SET  '+@Fyt+'Fiyat =  '+CAST(@Fiyat as nvarchar(50))+' where Kodu = '''+@GelenStokKodu+'''';   
 EXEC sp_executesql @Query;
 SET @Query = 'UPDATE StokFiyat SET '+@Fyt+'FiyatH = (CASE WHEN '+@Fyt+'Kdv = ''H'' THEN '+CAST(@Fiyat as nvarchar(50))+' ELSE ('+CAST(@Fiyat as nvarchar(50))+'/(100+SatisKdv))*100 END), '+@Fyt+'DovizKodu='''+@DovizKodu+''' where Kodu =''' + @GelenStokKodu
 + '''';      
 EXEC sp_executesql @Query;          
 IF @Fyt in ('F1','F2','F3','F4')      
 BEGIN     
  UPDATE sk SET sk.F1Fiyat  = sf.F1Fiyat, sk.F1DovizKodu = sf.F1DovizKodu,  sk.F1Kdv = sf.F1Kdv,    
       sk.F2Fiyat  = sf.F2Fiyat, sk.F2DovizKodu = sf.F2DovizKodu,  sk.F2Kdv = sf.F2Kdv,   
       sk.F3Fiyat  = sf.F3Fiyat, sk.F3DovizKodu = sf.F3DovizKodu,  sk.F3Kdv = sf.F3Kdv,   
       sk.F4Fiyat  = sf.F4Fiyat, sk.F4DovizKodu = sf.F4DovizKodu,  sk.F4Kdv = sf.F4Kdv
       /*sk.AlisIndirimYuzde  =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto1 ELSE sk.AlisIndirimYuzde END),    
       sk.AlisIndirimYuzde2 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto2 ELSE sk.AlisIndirimYuzde2 END),    
       sk.AlisIndirimYuzde3 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto3 ELSE sk.AlisIndirimYuzde3 END),     
       sk.AlisIndirimYuzde4 =  (CASE WHEN @IskontoGuncelle = 1 THEN @Iskonto4 ELSE sk.AlisIndirimYuzde4 END) */   
     FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu and sk.Kodu = @GelenStokKodu      
 END; 
END;
go--
      				   
if exists (select * from sys.procedures where name='sp_StokFiyat_Fiyat_FiyatH')
drop proc sp_StokFiyat_Fiyat_FiyatH
go--
      				   
CREATE  PROCEDURE sp_StokFiyat_Fiyat_FiyatH   
AS      
begin      
 declare @i int,@sorgu nvarchar(max),@cnt nvarchar(2)      
 set @i = 1;      
    while @i <= 20       
 begin      
   SET @cnt = CAST(@i as varchar(2));      
   SET @sorgu = ' UPDATE StokFiyat SET  F'+@cnt+'FiyatH = CASE WHEN F'+@cnt+'Kdv = ''H'' THEN F'+@cnt+'Fiyat ELSE (F'+@cnt+'Fiyat/(100 + SatisKdv)*100) END';      
   -- print @sorgu      
   EXEC sp_executesql @sorgu;      
   SET @i += 1;      
    end;      
      
 UPDATE sk SET sk.F1Fiyat  = sf.F1Fiyat, sk.F1DovizKodu = sf.F1DovizKodu,  sk.F1Kdv = sf.F1Kdv,    
       sk.F2Fiyat  = sf.F2Fiyat, sk.F2DovizKodu = sf.F2DovizKodu,  sk.F2Kdv = sf.F2Kdv,   
       sk.F3Fiyat  = sf.F3Fiyat, sk.F3DovizKodu = sf.F3DovizKodu,  sk.F3Kdv = sf.F3Kdv,   
       sk.F4Fiyat  = sf.F4Fiyat, sk.F4DovizKodu = sf.F4DovizKodu,  sk.F4Kdv = sf.F4Kdv  
     FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu       
end;  
go--

if  not  exists (select * from dbo.syscolumns where id = object_id(N'AnaMenuAyar') and name=N'ComputerName')
begin
ALTER TABLE AnaMenuAyar ADD ComputerName nvarchar(50)
end
else begin
ALTER TABLE AnaMenuAyar alter column ComputerName nvarchar(50)
end
go--

if  not  exists (select * from dbo.syscolumns where id = object_id(N'AcariTakas') and name=N'TeslimEden')
begin
ALTER TABLE AcariTakas ADD TeslimEden nvarchar(150)
end
else begin
ALTER TABLE AcariTakas alter column TeslimEden nvarchar(150)
end
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AcariTakas') and name=N'TeslimAlan')
begin
ALTER TABLE AcariTakas ADD TeslimAlan nvarchar(150)
end
else begin
ALTER TABLE AcariTakas ALTER COLUMN TeslimAlan nvarchar(150)
end
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'AcariTakas') and name=N'Aciklama2')
begin
ALTER TABLE AcariTakas ADD Aciklama2 nvarchar(150)
end
else begin
ALTER TABLE AcariTakas ALTER COLUMN Aciklama2 nvarchar(150)
end
go--


if exists (select * from sys.procedures where name='FaturaFisFiyatGuncelle')
drop proc FaturaFisFiyatGuncelle
go--
CREATE  procedure FaturaFisFiyatGuncelle @muskod varchar(20),@Tar1 varchar(8),@Tar2 varchar(8)      
as    
--declare @muskod varchar(10)    
--declare @Tar1 varchar(8)    
--declare @Tar2 varchar(8)    
    
--set @muskod='M0002'    
--set @Tar1='20210301'    
--set @Tar2='20220301'    
    
begin        
declare @TopTutar float         
declare @tutar float        
declare @ttutar float        
declare @noids varchar(max)        
declare @noid int        
        
select @TopTutar=MUSBAKIYE from V_AMUSTERI where MUSKOD = @muskod        
        
set @TopTutar=@TopTutar        
    
--print 'Toptutar='    
--print @TopTutar    
    
update STOKHARDETBAG set SecimEH = 'H' where CariKodu=@muskod        
        
declare FaturaFisCursor2 Cursor         
for        
select NoId,Tutar from STOKHARDETBAG         
where CariKodu = @muskod and         
IslemTuru in( 'Prk.Satış','Satış') and         
FaturaTarihi>=@Tar1 and     
FaturaTarihi<=@Tar2 and    
FisTuru='F' and IslemNo in(        
select ISLEMNO from ACARI where not exists (select * from KASA where KASA.ISLEMNO=ACARI.ISLEMNO)        
and MUSNO=STOKHARDETBAG.CariNo)        
order by FaturaTarihi desc         
        
open FaturaFisCursor2;        
        
fetch next from FaturaFisCursor2 into @noid,@tutar        
        
set @ttutar = 0;        
set @noids ='0';        
        
declare @dur int        
set @dur=0        
        
WHILE (@@FETCH_STATUS = 0)and(@dur=0)        
  Begin        
   set @ttutar = @ttutar + @tutar        
   print @ttutar        
        
   if (@TopTutar>=@ttutar)        
     begin        
    set @noids = @noids+',<'+cast(@noid as varchar)+'>'           
    fetch next from FaturaFisCursor2 into @noid,@tutar        
  end        
    else        
     Set @dur = 1        
  end        
        
print 'noids='+@noids        
        
close FaturaFisCursor2         
        
deallocate FaturaFisCursor2         
        
update STOKHARDETBAG set SecimEH ='E' where        
charindex('<'+cast(NoId as varchar)+'>',@noids)>0        
        
select sum(Tutar)as toplam     
 from STOKHARDETBAG         
where CariKodu = @muskod and         
IslemTuru in( 'Prk.Satış','Satış') and         
FaturaTarihi>=@Tar1 and          
FaturaTarihi<=@Tar2 and        
FisTuru='F' and IslemNo in(        
select ISLEMNO from ACARI where not exists (select * from KASA where KASA.ISLEMNO=ACARI.ISLEMNO)        
and MUSNO=STOKHARDETBAG.CariNo)       
and SecimEH='E'      
end    
go--
IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='FaturaFiyatGuncellemeKontrol')
 BEGIN 
CREATE TABLE [dbo].[FaturaFiyatGuncellemeKontrol](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[StokKodu] [nvarchar](25) NULL,
	[StokAdi] [nvarchar](500) NULL,
	[EskiFiyat] [float] NULL,
	[YeniFiyat] [float] NULL,
	[Tarih] [smalldatetime] NULL,
	[KullaniciNo] [int]
) ON [PRIMARY]
end
go--
IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='BakiyeKapatmaParametre')
 BEGIN 
CREATE TABLE [dbo].[BakiyeKapatmaParametre](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[Gun1] [int] NULL,
	[Gun2] [int] NULL,
	[KolonAdi] [nvarchar](50) NULL,
	[Tarih] [smalldatetime] NULL,
	[KullaniciNo] [int] NULL
) ON [PRIMARY]
end

go--
UPDATE Adres SET AdresNo = 10 where AdresNo = 0 
go--
UPDATE a  SET a.AdresNo = tbl.SiraNo from 
	(select CariNo,AdresNo,NoId,RANK () OVER (PARTITION BY CariNo order  BY  AdresNo,NoId) SiraNo from Adres) tbl,Adres a
where a.NoId = tbl.NoId
go--
if exists (select * from sys.procedures where name='StokKartiBirlestir')
drop proc StokKartiBirlestir
go--
create       procedure StokKartiBirlestir(  
@EskiStokKodu nvarchar(40),  
@EskiStokAdi nvarchar(500),           
@EskiStokNo int,  
@YeniStokKodu nvarchar(40),  
@YeniStokAdi nvarchar(500),           
@YeniStokNo int)      
as              
begin                 
 UPDATE STOKHAR SET StokAdi = @YeniStokAdi, StokKodu= @YeniStokKodu,StokNo = @YeniStokNo where StokKodu= @EskiStokKodu      
 UPDATE StokFiyatLog SET Kodu= @YeniStokKodu where Kodu= @EskiStokKodu              
 UPDATE PDegFiyatlar SET StokAdi = @YeniStokAdi, StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu  
 UPDATE PPetrolHar SET StokAdi = @YeniStokAdi, StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu      
 UPDATE PYazarKasaStoklari SET StokAdi = @YeniStokAdi, StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu              
 UPDATE PYazarKasaHar SET StokAdi = @YeniStokAdi, StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu    
 UPDATE DonemselFiyatListesi SET StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu    
 --DELETE MuadilStoklar WHERE MuadilStokKodu = @EskiStokKodu  
 --UPDATE MuadilStoklar SET MuadilStokAdi = @STOKADI, MuadilStokKodu= @STOKKODU where MuadilStokKodu= @EskiStokKodu                         
 UPDATE CariUretimFiyatlari SET StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu    
 UPDATE Seri SET StokKodu= @YeniStokKodu where StokKodu= @EskiStokKodu     
 Update CariDonemFiyatlari set StokKodu=@YeniStokKodu Where StokKodu=@EskiStokKodu       
 Update ElTerminaliDepoSayim set StokKodu=@YeniStokKodu, StokAdi=@YeniStokAdi  Where StokKodu=@EskiStokKodu     
 Update ElTerminaliYukleme set StokKodu=@YeniStokKodu, StokAdi=@YeniStokAdi  Where StokKodu=@EskiStokKodu       
 Update EtiketBarkod set StokKodu=@YeniStokKodu, StokAdi=@YeniStokAdi  Where StokKodu=@EskiStokKodu             
 Update MiktarsalHedefler set StokKodu=@YeniStokKodu Where StokKodu=@EskiStokKodu         
 Update RenkBedenSeriTablosu set StokKodu=@YeniStokKodu Where StokKodu=@EskiStokKodu            
 Update RenkBedenTablosu set StokKodu=@YeniStokKodu Where StokKodu=@EskiStokKodu               
 Update SIPARISHAR set StokKodu=@YeniStokKodu, StokAdi=@YeniStokAdi,StokNo = @YeniStokNo  Where StokKodu=@EskiStokKodu              
 Update TEKLIFHAR set StokKodu=@YeniStokKodu, StokAdi=@YeniStokAdi,StokNo = @YeniStokNo  Where StokKodu=@EskiStokKodu       
 Update StokDepoMiktarlari set StokKodu=@YeniStokKodu Where StokKodu=@EskiStokKodu      
 UPDATE InvoiceItemMap SET OurCode = @YeniStokKodu,SenderItemName = @YeniStokAdi where OurCode = @EskiStokKodu       
 DELETE StokBarkodlari Where StokKodu=@EskiStokKodu          
 DELETE StokDB where StokKodu= @EskiStokKodu     
 DELETE StokTablolari Where StokKodu=@EskiStokKodu     
 DELETE StokVergiMuafiyetleri Where StokKodu=@EskiStokKodu    
 DELETE StokDB where StokKodu= @EskiStokKodu   
 DELETE StokMarkalari where StokKodu=@EskiStokKodu              
 DELETE Tedarikciler where StokKodu= @EskiStokKodu        
 DELETE StokFiyat where Kodu= @EskiStokKodu   
 DELETE STOKKARTI where Kodu= @EskiStokKodu   
 DELETE MH_Connect where Degeri=@EskiStokKodu and TabloAdi='STOKKARTI' and FieldAdi='Kodu'          
END; 
go--
if exists (select * from sys.procedures where name='CariKartiBirlestir')
drop proc CariKartiBirlestir
go--
create      procedure CariKartiBirlestir(
@EskiCariKodu nvarchar(40),
@EskiCariAdi nvarchar(500),         
@EskiCariNo int,
@YeniCariKodu nvarchar(40),
@YeniCariAdi nvarchar(500),         
@YeniCariNo int)    
AS            
BEGIN
	 UPDATE ACARI SET MUSADI = @YeniCariAdi, MUSKOD= @YeniCariKodu,MUSNO = @YeniCariNo where MUSNO = @EskiCariNo   
	 UPDATE Odeme SET CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where CariNo = @EskiCariNo  
	 UPDATE STOKHAR SET CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where CariNo = @EskiCariNo    
	 UPDATE STOKHARDETBAG SET CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where CariNo = @EskiCariNo    
	 UPDATE BANKA SET MUSADI = @YeniCariAdi, MusKod= @YeniCariKodu,MUSNO = @YeniCariNo where MUSNO = @EskiCariNo    
	 UPDATE KASA SET MUSADI = @YeniCariAdi, MUSKOD= @YeniCariKodu,MUSNO = @YeniCariNo where MUSNO = @EskiCariNo    
	 UPDATE CEK SET MUSADI = @YeniCariAdi, MUSKODU= @YeniCariKodu,MUSNO = @YeniCariNo where MUSNO = @EskiCariNo    
	 UPDATE BORDRO SET MUSADI = @YeniCariAdi, MUSKODU= @YeniCariKodu,MUSNO = @YeniCariNo where MUSNO = @EskiCariNo      
	 UPDATE DonemselFiyatListesiCari SET CariKodu =  @YeniCariKodu where CariKodu = @EskiCariKodu      
	 UPDATE TEKLIFHAR SET CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where  CariNo=@EskiCariNo    
	 UPDATE TEKLIFHARDETBAG SET CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where  CariNo=@EskiCariNo   
	 UPDATE CariNotlar set CariKodu=@YeniCariKodu where CariKodu = @EskiCariKodu  
	 UPDATE FaturaDetay set CariKodu=@YeniCariKodu where CariKodu = @EskiCariKodu    
	 UPDATE OdemeSozu set CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu where CariKodu = @EskiCariKodu    
	 UPDATE PPlaka set CariKodu=@YeniCariKodu where CariKodu = @EskiCariKodu  
	 UPDATE SIPARISHAR set CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where CariNo = @EskiCariNo    
	 UPDATE SIPARISHARDETBAG set CariAdi = @YeniCariAdi, CariKodu= @YeniCariKodu,CariNo = @YeniCariNo where CariNo = @EskiCariNo    
	 UPDATE TelAramaLog set CariKodu=@YeniCariKodu where CariKodu = @EskiCariKodu    
	 UPDATE Ziyaret set CariKodu=@YeniCariKodu where CariKodu = @EskiCariKodu     
	 DELETE FirinFiyatListesi WHERE CariKodu = @EskiCariKodu   
	 DELETE CariUretimFiyatlari WHERE CariKodu = @EskiCariKodu   
	 DELETE CariSozlesme WHERE CariKodu = @EskiCariKodu   
	 DELETE CariReferanslar WHERE CariKodu = @EskiCariKodu   
	 DELETE CariBarkodlari WHERE CariKodu = @EskiCariKodu 
	 DELETE Bonus WHERE CariKodu = @EskiCariKodu 
	 DELETE Rut WHERE CariKodu = @EskiCariKodu  
	 DELETE Satici WHERE CariKodu = @EskiCariKodu
	 DELETE Adres WHERE CariNo = @EskiCariNo
	 DELETE AMUSTERI WHERE MUSNO=@EskiCariNo    
END;         
go--
if exists (select * from sys.procedures where name='sp_Bakim_Doviz_KdvDH_Update')
drop proc sp_Bakim_Doviz_KdvDH_Update
go--
CREATE PROCEDURE sp_Bakim_Doviz_KdvDH_Update
AS
BEGIN
	declare @DovizKod nvarchar(5),@KdvDH nvarchar(1)
	declare @i int,@sorgu nvarchar(max),@cnt nvarchar(2) 

	IF (select count(*) from DOVIZT where DovizKodu = 'YP') > 0 
		SET @DovizKod = 'YP'
	ELSE IF  (select count(*) from DOVIZT where DovizKodu = 'TL') > 0
		SET @DovizKod = 'TL'
	ELSE 
		select TOP 1  @DovizKod = DovizKodu from DOVIZT ORDER BY NoId 
       
	SET @i = 1;        
	WHILE @i <= 20         
	BEGIN        
	   SET @cnt = CAST(@i as varchar(2));        
	   SET @sorgu = ' UPDATE StokFiyat SET  F'+@cnt+'DovizKodu = ''' + @DovizKod + ''' WHERE  ISNULL(F'+@cnt+'DovizKodu,'''') = '''''               
	   EXEC sp_executesql @sorgu;   
	   SET @i += 1;        
	END;        

	UPDATE StokFiyat SET F4Kdv = 'D' WHERE ISNULL(F4Kdv,'') = '' 
	UPDATE StokFiyat SET F2Kdv = 'H' WHERE ISNULL(F2Kdv,'') = '' 
 
	UPDATE sk SET sk.F1DovizKodu = sf.F1DovizKodu,   
				sk.F2DovizKodu = sf.F2DovizKodu,  
				sk.F3DovizKodu = sf.F3DovizKodu,  
				sk.F4DovizKodu = sf.F4DovizKodu,
				sk.F2Kdv		  = sf.F2Kdv,
				sk.F4Kdv		  = sf.F4Kdv
		FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu      
END;
go--
if exists (select * from sys.procedures where name='sp_SN_Bakim')
drop proc sp_SN_Bakim
go--
CREATE   PROC sp_SN_Bakim      
AS  
BEGIN       
 EXEC sp_Grup_Bakim        
 EXEC sp_StokDoviz_Bakim        
 EXEC  sp_StokFiyat_Bakim        
 EXEC sp_StokFiyatBakim         
 EXEC sp_Kart_Bakim      
 EXEC sp_BAKIM_KOLON_UPDATE    
 EXEC sp_Hareket_Bakim  
 EXEC sp_BirimBarkod_Aktarim  
 EXEC sp_Bakim_Doviz_KdvDH_Update
 exec sp_OdemePlaniKontrol 
END  
go--
IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='StokDepoRafTanimlari')
 BEGIN 
CREATE TABLE [dbo].[StokDepoRafTanimlari](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[StokNo] [int] NULL,
	[DepoNoId] int NULL,
	[RafNo] nvarchar(50) NULL,
	[KulNo] [int] NOT NULL,
	[UpdateDate] [datetime] NOT NULL,	
 CONSTRAINT [PK_StokDepoRafTanimlari] PRIMARY KEY CLUSTERED 
(
	[NoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[StokDepoRafTanimlari] ADD  DEFAULT ((1)) FOR [KulNo]

ALTER TABLE [dbo].[StokDepoRafTanimlari] ADD  DEFAULT (getdate()) FOR [UpdateDate]

end
go--
      				   
if exists (select * from sys.procedures where name='sp_shrink')
drop proc sp_shrink
go--
CREATE  PROCEDURE sp_shrink (@dbname nvarchar(50))
AS
BEGIN
	DECLARE @sorgu nvarchar(max),@filename nvarchar(50)
   
	SET @sorgu = 'USE ' + @dbname
	EXEC sp_executesql @sorgu; 

	SET @sorgu = 'DBCC SHRINKDATABASE(N'''+@dbname+''')'; 
	EXEC sp_executesql @sorgu;   

	select @filename = name from sys.database_files where name like '%Data'

	SET @sorgu = 'DBCC SHRINKFILE (N'''+@filename+''' , 0, TRUNCATEONLY)'; 
	EXEC sp_executesql @sorgu;   

	select  @filename = name from sys.database_files where name like '%Log'
	SET @sorgu = 'DBCC SHRINKFILE (N'''+@filename+''' , 0, TRUNCATEONLY)'; 
	EXEC sp_executesql @sorgu;   
END;
go--
if exists (select * from sys.procedures where name='sp_Kart_Bakim')
drop proc sp_Kart_Bakim
go--
  create   proc sp_Kart_Bakim          
as          
 ALTER TABLE STOKKARTI ALTER COLUMN Barkodu  nvarchar(40)      
 UPDATE STOKKARTI SET Barkodu = Kodu where ISNULL(Barkodu,'') = ''      
  update StokBarkodlari set AnaBirim=0 where AnaBirim is null or AnaBirim='' or AnaBirim<>1          
  UPDATE StokBarkodlari SET Barkodu=StokKodu WHERE Barkodu IN ('',' ',NULL)        
          
----ÇİFT KAYIT STOKKARTLARI LİSTESİNİ VERİR.          
--select * from STOKKARTI SK1 WHERE Kodu in (SELECT Kodu FROM STOKKARTI group by Kodu having count(Kodu)>1)          
-- AND NoId =(SELECT MIN(NoId) FROM STOKKARTI SK3 where SK3.Kodu=SK1.Kodu)          
--select * from STOKKARTI SK1 WHERE Kodu in (SELECT Kodu FROM STOKKARTI group by Kodu having count(Kodu)>1)          
          
--ÇİFT KAYITLARDAN İLK AÇILANLARI SİLER. EN SON KAYIT KALIR.          
DELETE from STOKKARTI  WHERE Kodu in (SELECT Kodu FROM STOKKARTI group by Kodu having count(Kodu)>1)          
 AND NoId <>(SELECT max(NoId) FROM STOKKARTI SK3 where SK3.Kodu=STOKKARTI.Kodu)          
    --Stokfiyat Kodu  1 den fazla olan kayıtlardan son kayıt hariç siler         
DELETE from StokFiyat WHERE Kodu in (SELECT Kodu FROM StokFiyat group by Kodu having count(Kodu)>1)          
 AND NoId <>(SELECT max(NoId) FROM StokFiyat SK3 where SK3.Kodu= StokFiyat.Kodu)          
  --StokBarkodu 1 den fazla olan kayıtlardan son kayıt hariç siler         
DELETE from StokBarkodlari WHERE Barkodu in (SELECT Barkodu FROM StokBarkodlari group by Barkodu having count(Barkodu)>1)          
 AND StokBarkodlariNoId <>(SELECT max(StokBarkodlariNoId) FROM StokBarkodlari SK3 where SK3.Barkodu= StokBarkodlari.Barkodu)          
         
 --stokkartı olmayan stokfiyat'-Stokbarkodlari  silme          
 ---Tabloda karşılılğı olmayanları silme !!!          
DELETE FROM StokVergiMuafiyetleri WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokVergiMuafiyetleri.StokKodu)          
DELETE FROM StokFiyat  WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokFiyat.Kodu)          
DELETE FROM StokBarkodlari WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokBarkodlari.StokKodu)          
DELETE FROM StokDB WHERE NOT EXISTS (SELECT * FROM STOKKARTI WHERE Kodu=StokDB.StokKodu)     
  
UPDATE STOKKARTI SET IkinciBirimMiktari = 1 WHERE ISNULL(IkinciBirimMiktari,0) = 0 or IkinciBirimMiktari = ''  
UPDATE STOKKARTI SET UcuncuBirimMiktari = 1 WHERE ISNULL(UcuncuBirimMiktari,0) = 0 or UcuncuBirimMiktari = ''  
UPDATE STOKKARTI SET DorduncuBirimMiktari = 1 WHERE ISNULL(DorduncuBirimMiktari,0) = 0 or DorduncuBirimMiktari = ''  
UPDATE STOKKARTI SET BesinciBirimMiktari = 1 WHERE ISNULL(BesinciBirimMiktari,0) = 0 or BesinciBirimMiktari = ''  
UPDATE STOKKARTI SET AltinciBirimMiktari = 1 WHERE ISNULL(AltinciBirimMiktari,0) = 0 or AltinciBirimMiktari = ''  
UPDATE Adres SET AdresNo = 10 where AdresNo = 0 
UPDATE a  SET a.AdresNo = tbl.SiraNo from 
	(select CariNo,AdresNo,NoId,RANK () OVER (PARTITION BY CariNo order  BY  AdresNo,NoId) SiraNo from Adres) tbl,Adres a where a.NoId = tbl.NoId
	
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
if exists (select * from sys.procedures where name='sp_CARIEKSTRE_UST')
drop proc sp_CARIEKSTRE_UST
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE      PROCEDURE [dbo].[sp_CARIEKSTRE_UST]                             
 @MUSNO int, @ILKTARIH nvarchar(10),@SONTARIH nvarchar(10),                         
 @KAPALIHAREKET nvarchar(1),@ORTVADESEC nvarchar(1),                
 @ISLEMTIPINEGORE SMALLINT=0,@ISLEMTIPINEGOREBAKIYEGRUPLA SMALLINT=0,@P1IslemTipi nvarchar(250)='',@AdresNo int = NULL                              
AS                     
BEGIN                 
 declare @ParamIslemTipi nvarchar(250)              
 declare @pIslemTipi nvarchar(250) declare @pBAKIYE1 money                
 set @ParamIslemTipi= @P1IslemTipi;              
      
DECLARE @ACARI TABLE (ROWNUMBER INT,                  
  TARIH DATETIME,                   
  TESSIRNO INT,                   
  MUSNO INT,                   
  BAACIKLA nvarchar(14),                  
  ACIKLAMA nvarchar(900),                        
  ISLEMNO INT,                  
  DOVIZTUT float,                  
  DOVIZKODU nvarchar(2),                  
  OZELKOD nvarchar(5),                  
  ISLEMTIPI nvarchar(250),                        
  BELGENO nvarchar(10),                   
  ACIKLABAKIYE nvarchar(1),                  
  TP nvarchar(2),                   
  [Ort Vade] DATETIME,                  
  SaticiKodu nvarchar(30),                     
  SaticiAdi nvarchar(25),                   
  KULNO INT,                  
  KULADI nvarchar(20),                   
  DovizBorc float,                        
  DovizAlacak float,                  
  Aciklama2 nvarchar(200),                        
  SiraNo INT,                   
  CARINO INT,                   
  MUSKOD nvarchar(20),                  
  MUSADI nvarchar(50),                   
  IsIstasyonKodu nvarchar(30),                         
  IsIstasyonAdi nvarchar(30),                   
  TESLIM nvarchar(90),                  
  BORC money,                   
  ALACAK money,                    
  FARK money,                         
  BAKIYE money ,                  
  TTarihi DATETIME,                  
  ISLEMNO3 int,                  
  NoID int identity(1,1),                  
  BA nvarchar(5),                        
  AcikKapali nvarchar(20),                  
  VardiyaKodu nvarchar(15),                 
  Departman nvarchar(15),                  
  IslemNo2 int,                        
  VadeTarihi datetime,                  
  DOVIZCINSI int,NEWROWNUMBER INT,DovizKur float)                     
                       
  declare @Musteri table (MusteriNo decimal,UstMusteriNo decimal)                        
  update ACARI SET AcikKapali='A' WHERE AcikKapali <>'K' and MUSNO = @MUSNO          
  insert into @Musteri                        
  select distinct MUSNO,UstCariNo from AMUSTERI where UstCariNo=@MUSNO                        
                        
DECLARE @BAKIYE money, @IBKY MONEY                       
                         
SET @BAKIYE = 0                        
IF @IBKY IS NULL                        
     SET @IBKY=0                        
 if isnull(@ParamIslemTipi,'')<>''              
 begin              
               
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO)                        
 select 0,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1                        
  from @Musteri M                        
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                
   and A.ISLEMTIPI=@ParamIslemTipi   
    group by M.UstMusteriNo                  
 end else              
 begin              
  IF @ISLEMTIPINEGORE=1 BEGIN                
               
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI)                        
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(isnull(BORC,0)-isnull(ALACAK,0)),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,convert(varchar(250),A.ISLEMTIPI) as ISLEMTIPI                         
  from @Musteri M                        
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH        
    group by M.UstMusteriNo,A.ISLEMTIPI                 
              
  end else              
  begin              
  INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO)                        
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1                        
from @Musteri M                        
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH               
    group by M.UstMusteriNo                 
end                  
end              
                          
             
                
 IF @ISLEMTIPINEGORE=1 BEGIN         if isnull(@ParamIslemTipi,'')<>''              
 begin              
 INSERT INTO @ACARI                        
 SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                        
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                     
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                        
  SaticiAdi, KULNO, KULADI, DovizBorc,                        
  DovizAlacak,Aciklama2,                        
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                         
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                        
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                        
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                         
 FROM ACARI                         
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                
 and ISLEMTIPI=@ParamIslemTipi              
  ORDER BY ROWNUMBER1 ASC                 
              
 end else begin              
  INSERT INTO @ACARI                
SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                        
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                        
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                        
  SaticiAdi, KULNO, KULADI, DovizBorc,                        
  DovizAlacak,Aciklama2,                        
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                         
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                        
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                        
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                        
 FROM ACARI                         
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                
 ORDER BY ROWNUMBER1 ASC              
               
end              
 /*ORDER BY TARIH,ACARI.CARINO*/                        
                
end else                
begin                
 if isnull(@ParamIslemTipi,'')<>''              
 begin                        
 INSERT INTO @ACARI                        
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                        
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                        
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                        
  SaticiAdi, KULNO, KULADI, DovizBorc,                        
  DovizAlacak,Aciklama2,                        
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                         
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                        
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                        
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                         
 FROM ACARI                         
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH and ISLEMTIPI=@ParamIslemTipi                      
 /*ORDER BY TARIH,ACARI.CARINO*/                
  ORDER BY ROWNUMBER ASC                 
end else               
begin              
 INSERT INTO @ACARI                        
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                        
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                        
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                        
  SaticiAdi, KULNO, KULADI, DovizBorc,                        
  DovizAlacak,Aciklama2,                        
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                         
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                        
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                        
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                         
 FROM ACARI                         
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                        
 /*ORDER BY TARIH,ACARI.CARINO*/                
  ORDER BY ROWNUMBER ASC                
end              
end                
                
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0                        
        
declare @ROWNUMBER INT               
declare @NEWROWNUMBER INT               
set @NEWROWNUMBER=0              
declare @IslemTipi1 varchar(250)                
              
IF (@ISLEMTIPINEGORE=1) and (@ParamIslemTipi='')  BEGIN              
               
delete from @ACARI where BAACIKLA='Devir' and ISLEMTIPI not in (select distinct ISLEMTIPI from @ACARI where  BAACIKLA<>'Devir')              
              
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI)                        
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,0,0,'Devir','Hesap Ekstresi Devir Tutary',1, A.ISLEMTIPI                           
 from @Musteri M                       
 left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH>@ILKTARIH               
 where (select count(B.ISLEMNO) from ACARI B where M.MusteriNo=B.MUSNO AND B.TARIH<=@ILKTARIH and A.ISLEMTIPI=B.ISLEMTIPI)=0              
 group by M.UstMusteriNo,A.ISLEMTIPI                 
                
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0               
              
declare AcariCursor1 cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ISLEMTIPI ASC,ROWNUMBER ASC                
FOR UPDATE OF ROWNUMBER OPEN AcariCursor1 FETCH NEXT FROM AcariCursor1               
INTO @ROWNUMBER,@IslemTipi1                
WHILE @@FETCH_STATUS=0                
BEGIN               
   if @NEWROWNUMBER=0              
   begin              
    set @pIslemTipi=@IslemTipi1;              
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M                        
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                
     and A.ISLEMTIPI=@pIslemTipi              
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'              
             set @NEWROWNUMBER=@NEWROWNUMBER+1;              
              
   end else if (@pIslemTipi=@IslemTipi1)              
   begin              
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER where ISLEMTIPI=@IslemTipi1 and ROWNUMBER=@ROWNUMBER and  BAACIKLA<>'Devir'              
   set @NEWROWNUMBER=@NEWROWNUMBER+1;              
              
   end else              
   begin     
    set @pIslemTipi=@IslemTipi1;              
     update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M                        
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                
     and A.ISLEMTIPI=@pIslemTipi              
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'              
    set @NEWROWNUMBER=@NEWROWNUMBER+1;              
              
   end              
              
FETCH NEXT FROM AcariCursor1 INTO @ROWNUMBER,@IslemTipi1                
END  CLOSE AcariCursor1  DEALLOCATE AcariCursor1                 
update @ACARI set ROWNUMBER=NEWROWNUMBER              
              
 end              
              
              
                
DECLARE @BAKIYE2 money              
DECLARE @DevirBakiye money              
              
              
declare AcariCursor cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ROWNUMBER ASC  FOR UPDATE OF BAKIYE OPEN AcariCursor FETCH NEXT FROM AcariCursor               
INTO @ROWNUMBER,@IslemTipi1                
              
WHILE @@FETCH_STATUS=0                
              
BEGIN                   
              
IF @ISLEMTIPINEGORE=0               
BEGIN                  
  SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER )                  
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER              
 end              
  else   begin                  
  if @ROWNUMBER=0                 
  begin             
   UPDATE TOP(1) @ACARI SET BAKIYE =isnull(FARK,0)  FROM @ACARI where ROWNUMBER =0              
   SET @DevirBakiye=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER =0)                
  end else              
  begin              
  if @ROWNUMBER=1                 
     set @pIslemTipi=@IslemTipi1;              
              
   if @pIslemTipi=@IslemTipi1                    
   begin                     
  SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)                
                 
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                    
                
              
   end else                     
   begin              
   set @pIslemTipi=@IslemTipi1;                     
   SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)               
                 
   UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                  
   end                
 end                
  end              
  FETCH NEXT FROM AcariCursor INTO @ROWNUMBER,@IslemTipi1                
END                
  CLOSE AcariCursor  DEALLOCATE AcariCursor                             
                
DELETE FROM AcariTakas WHERE MUSNO  in(select MusteriNo from @Musteri)                         
 insert into AcariTakas(                  
 TARIH,                  
 TESSIRNO,                  
 MUSNO,                  
 BAACIKLA,                  
 ACIKLAMA,                  
 ISLEMNO,                  
 DOVIZTUT,                  
 DOVIZKODU,                  
 OZELKOD,ISLEMTIPI,BELGENO,TP,                        
VadeTarihi,SaticiKodu,SaticiAdi,                  
KULNO,KULADI,DovizBorc,DovizAlacak,Aciklama2,                  
SiraNo,MUSKOD,MUSADI,IsIstasyonKodu,IsIstasyonAdi,                        
TeslimAlan,TeslimEden,BORC,ALACAK,BAKIYE,TTarihi,IslemNo3,                  
BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,DOVIZCINSI,DovizKur)       
    
select A.TARIH,A.TESSIRNO,A.MUSNO,                  
A.BAACIKLA,A.ACIKLAMA,A.ISLEMNO,A.DOVIZTUT,A.DOVIZKODU,                  
A.OZELKOD,A.ISLEMTIPI,A.BELGENO,A.TP,                        
A.[Ort Vade],A.SaticiKodu,A.SaticiAdi,                  
isnull(A.KULNO,0),A.KULADI,isnull(A.DovizBorc,0),                  
isnull(A.DovizAlacak,0),A.Aciklama2,A.NoID,                  
isnull(A.MUSKOD,''),A.MUSADI,A.IsIstasyonKodu,    
A.IsIstasyonAdi,                        
A.TESLIM,A.TESLIM,isnull(A.BORC,0),                  
isnull(A.ALACAK,0),isnull(A.BAKIYE,0),A.TTarihi,                  
A.ISLEMNO3,A.BA,A.AcikKapali,A.VardiyaKodu,                  
isnull(A.Departman,''),isnull(A.IslemNo2,0),isnull(A.DOVIZCINSI,0),ISNULL(A.DovizKur,1)    
                    
 FROM @ACARI A                        
 Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                        
 Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                        
 Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                    
order by  A.ROWNUMBER       
      
  if @ISLEMTIPINEGOREBAKIYEGRUPLA=0                
  begin                
 select A.*, ams.Tipi As CariTipi, ams.CariGrubu, ad.AdresNo,                        
  ad.Adres1, dbo.fn_FaturaSerinolari(A.ISLEMNO) As SeriNolar                         
                        
  FROM @ACARI A                        
  Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                        
  Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                        
  Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                 
  order by  A.ROWNUMBER                
  /*order by  A.MUSNO,TARIH,CARINO */             
 end        
 else                
 begin                
   SELECT  ISLEMTIPI,sum(BORC) as BORC,sum(ALACAK) as ALACAK,sum(BORC-ALACAK) as BAKIYE FROM @ACARI    where ISLEMTIPI is not null and MUSNO=@MUSNO  GROUP BY ISLEMTIPI                
                
 end                
                
 end 
 
go--
 if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.1.5')
 	     		  
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.1.5',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go--


if  not  exists (select * from dbo.syscolumns where id = object_id(N'STOKKARTI') and name=N'StokAdi2')
begin
	ALTER TABLE STOKKARTI ADD StokAdi2 nvarchar(100)
end ELSE BEGIN
	ALTER TABLE STOKKARTI ALTER COLUMN StokAdi2 nvarchar(100)
END
go--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='StokKodAdDegistir')
drop proc StokKodAdDegistir
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
go--

create      procedure StokKodAdDegistir             
@STOKADI nvarchar(500),            
@STOKKODU nvarchar(40),            
@EskiStokKodu nvarchar(40),           
@STOKADI2 nvarchar(100) = ''        
as            
begin Transaction               
 UPDATE STOKHAR SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE MuadilStoklar SET MuadilStokAdi = @STOKADI, MuadilStokKodu= @STOKKODU where MuadilStokKodu= @EskiStokKodu            
 UPDATE StokFiyat SET Kodu= @STOKKODU where Kodu= @EskiStokKodu            
 UPDATE StokDB SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE StokFiyatLog SET Kodu= @STOKKODU where Kodu= @EskiStokKodu            
 UPDATE StokMarkalari SET StokKodu= @STOKKODU where StokKodu=@EskiStokKodu            
 UPDATE Tedarikciler SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE PYazarKasaStoklari SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE PYazarKasaHar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE PPetrolHar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE PDegFiyatlar SET StokAdi = @STOKADI, StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE DonemselFiyatListesi SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE TekstilHareketTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE TekstilHataHareketTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE TekstilHataliStokVirmanSablonu SET HataliStokKodu= @STOKKODU where HataliStokKodu= @EskiStokKodu            
 UPDATE TekstilHataliStokVirmanSablonu SET VirmanStokKodu= @STOKKODU where VirmanStokKodu= @EskiStokKodu            
 UPDATE TekstilIsEmirleriDetayi SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE TekstilKesimTablosu SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE TekstilStokRenkTanimlari SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE CariUretimFiyatlari SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE Seri SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE Tarim_AmbalajStok SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 UPDATE Tarim_AmbalajStok SET AmbalajKodu= @STOKKODU where AmbalajKodu= @EskiStokKodu            
 UPDATE StokDB SET StokKodu= @STOKKODU where StokKodu= @EskiStokKodu            
 update MH_Connect SET Degeri =@STOKKODU where Degeri=@EskiStokKodu and TabloAdi='STOKKARTI' and FieldAdi='Kodu'            
 Update StokTablolari   Set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu              
 Update Tarim_AmbalajStok  Set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu              
 Update Tarim_AmbalajStok  Set AmbalajKodu=@STOKKODU Where AmbalajKodu=@EskiStokKodu              
 Update Tarim_SatisEmirleri  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu              
 Update Tarim_UretimDetay  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu              
 Update Tarim_UretimDetay  Set KasaKodu=@STOKKODU Where KasaKodu=@EskiStokKodu              
 Update Tarim_UretimDetay  Set PaletKodu=@STOKKODU Where PaletKodu=@EskiStokKodu              
 Update Tarim_UretimDetay2  Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu              
 Update Tarim_UrunAlim   Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu              
 Update Tarim_UrunAlimAmbalaj Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu              
 Update Tarim_UrunAlimHareket Set KasaKodu=@STOKKODU Where KasaKodu=@EskiStokKodu              
 Update Tarim_UrunAlimHareket Set PaletKodu=@STOKKODU Where PaletKodu=@EskiStokKodu              
 Update StokTartim    Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu         
 Update StokTartimPalet   Set StokKodu=@STOKKODU, StokAdi=@STOKADI Where StokKodu=@EskiStokKodu             
 Update CariDonemFiyatlari set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update ElTerminaliDepoSayim set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu             
 Update ElTerminaliYukleme set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu             
 Update EtiketBarkod set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu             
 Update MiktarsalHedefler set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update RenkBedenSeriTablosu set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update RenkBedenTablosu set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update SIPARISHAR set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu             
 Update TEKLIFHAR set StokKodu=@STOKKODU, StokAdi=@STOKADI  Where StokKodu=@EskiStokKodu               
 Update SeriNoKaliteKontrol set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update SipHar set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu           
 Update StokDepoMiktarlari set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 Update StokVergiMuafiyetleri set StokKodu=@STOKKODU Where StokKodu=@EskiStokKodu             
 UPDATE InvoiceItemMap SET OurCode = @STOKKODU,SenderItemName = @STOKADI where OurCode = @EskiStokKodu            
 UPDATE STOKKARTI SET Kodu= @STOKKODU, StokAdi=@STOKADI,StokAdi2 = @STOKADI2 where Kodu= @EskiStokKodu         
 Update StokBarkodlari set StokKodu=@STOKKODU  Where StokKodu=@EskiStokKodu        
 Update StokBarkodlari set Barkodu=@STOKKODU  Where Barkodu=@EskiStokKodu           
          
commit; 

go--

if exists (select * from sys.procedures where name='spStokKartiKaydet')
drop proc spStokKartiKaydet
go--
CREATE     PROCEDURE [dbo].[spStokKartiKaydet]       
 @StokNo int,      
 @Kodu nvarchar(30),      
 @UreticiFirmaKodu nvarchar(30),      
 @StokAdi nvarchar(500),      
 @OzelKod1 nvarchar(8),      
 @OzelKod2 nvarchar(8),      
 @AnaBirim nvarchar(10),      
 @Barkodu nvarchar(40),      
 @IkinciBirim nvarchar(10),      
 @IkinciBirimMiktari float,      
 @IkinciBirimBarkodu nvarchar(40),      
 @UcuncuBirim nvarchar(10),      
 @UcuncuBirimMiktari float,      
 @UcuncuBirimBarkodu nvarchar(40),      
 @DorduncuBirim nvarchar(10),      
 @DorduncuBirimMiktari float,      
 @DorduncuBirimBarkodu nvarchar(40),      
 @AlisKdv smallint,      
 @SatisKdv smallint,      
 @Indirim smallint,      
 @StokAdedi float,      
 @F1Adi nvarchar(10),      
 @F2Adi nvarchar(10),      
 @F3Adi nvarchar(10),      
 @F4Adi nvarchar(10),      
 @F1Fiyat Float,      
 @F2Fiyat Float,      
 @F3Fiyat Float,      
 @F4Fiyat Float,      
 @F1Kdv nvarchar(1),      
 @F2Kdv nvarchar(1),      
 @F3Kdv nvarchar(1),      
 @F4Kdv nvarchar(1),      
 @GrupAdi nvarchar(30),      
 @UrtNo Float,      
 @UrtMiktar Float,      
 @BesinciBirim nvarchar(10),      
 @BesinciBirimMiktari Float,      
 @BesinciBirimBarkodu nvarchar(40),      
 @AltinciBirim nvarchar(10),      
 @AltinciBirimMiktari Float,      
 @AltinciBirimBarkodu nvarchar(40),      
 @AsgariAdet Float,      
 @AzamiAdet Float,      
 @OTVOrani Float,      
 @StokEn Float,      
 @StokBoy Float,      
 @StokYuksekligi Float,      
 @StokKilo Float,      
 @StokRenkKodu nvarchar(15),      
 @KULNO int,      
 @KULADI nvarchar(20),      
 @TP nvarchar(2),      
 @UygFiyat Float,      
 @Aciklama nvarchar(50),      
 @Departman nvarchar(8),      
 @AlisIndirimYuzde Float,      
 @SatisIndirimYuzde Float,      
 @OTVTutari Float,      
 @DepartmanKodu nvarchar(2),      
 @F1DovizKodu nvarchar(2),      
 @F2DovizKodu nvarchar(2),      
 @F3DovizKodu nvarchar(2),      
 @F4DovizKodu nvarchar(2),      
 @Update_Date smalldatetime,      
 @TeraziTusu nvarchar(2),      
 @YKasaTusu nvarchar(2),      
 @Secim nvarchar(10),      
 @AnaStokKodu nvarchar(30),      
 @Ambalaj nvarchar(20),      
 @StokKiloBrut Float,      
 @StokTipi nvarchar(15),      
 @StokOzellikKodu nvarchar(15),      
 @AktifPasif nvarchar(1),      
 @OzelBarkod Float,      
 @Olcu nvarchar(8),      
 @AlisIndirimYuzde2 Float,      
 @AlisIndirimYuzde3 Float,      
 @AlisIndirimYuzde4 Float,      
 @SatisIndirimYuzde2 Float,      
 @SatisIndirimYuzde3 Float,      
 @SatisIndirimYuzde4 Float,      
 @MuadilStokNo int,      
 @BonusPuani Float,      
 @BonusPuaniSatici Float,      
 @OlmasiGerekenAdet float,      
 @PrimYuzde float,      
  @StokBedenNo nvarchar(15),      
 @StokRenkBedenCalisir nvarchar(1),      
 @WASG nvarchar(1),      
 @WebGrupAdi nvarchar(30),      
 @SKFIK int,      
 @GrupOzelKod1 nvarchar(15),      
 @GrupOzelKod2 nvarchar(15),      
 @GrupOzelKod3 nvarchar(15),      
 @GrupOzelKod4 nvarchar(15),      
  @OrjinalKodu nvarchar(30),      
 @StokAdi2 nvarchar(100),      
 @Aciklama2 nvarchar(50),      
 @WebGrupAdi2 nvarchar(30),      
 @StokAgirlik Float,      
 @StokMaxAgirlik Float,      
 @StokMinAgirlik Float,      
   @AltGrupAdi nvarchar(30),      
 @AltGrupAdi2 nvarchar(30),      
 @SeriNoTakibi bit,      
 @IRKI nvarchar(30),      
 @SuniTohum bit,       
 @OzelKod3 nvarchar(8),        
 @OzelKod4 nvarchar(8),      
 @Icmal bit,      
 @SatisFiyatiDegistirilemez bit,      
 @A_Muh_Kodu nvarchar(20),      
 @S_Muh_Kodu nvarchar(20),      
 @FireOrani float,      
 @WebdeGosterilsinmi bit,      
 @TarimElTerminliGosterilsinmi bit,      
 @AltGrupAdi3 nvarchar(30),      
 @AdetTakibi bit,      
 @LotTakibi bit,    
 @UlkeKodu nvarchar(3)  
   
AS       
INSERT STOKKARTI  (StokNo,      
  Kodu,      
  UreticiFirmaKodu,      
  StokAdi,      
  OzelKod1,      
  OzelKod2,      
  AnaBirim,      
  Barkodu,      
  IkinciBirim,      
  IkinciBirimMiktari,      
  IkinciBirimBarkodu,      
  UcuncuBirim,      
  UcuncuBirimMiktari,      
  UcuncuBirimBarkodu,      
  DorduncuBirim,      
  DorduncuBirimMiktari,      
  DorduncuBirimBarkodu,      
  AlisKdv,      
  SatisKdv,      
  Indirim,      
  StokAdedi,      
  F1Adi,      
  F2Adi,      
  F3Adi,      
  F4Adi,      
  F1Fiyat,      
  F2Fiyat,      
  F3Fiyat,      
  F4Fiyat,      
  F1Kdv,      
  F2Kdv,      
  F3Kdv,      
  F4Kdv,      
  GrupAdi,      
  UrtNo,      
  UrtMiktar,      
  BesinciBirim,      
  BesinciBirimMiktari,      
  BesinciBirimBarkodu,      
  AltinciBirim,      
  AltinciBirimMiktari,      
  AltinciBirimBarkodu,      
  AsgariAdet,      
  AzamiAdet,      
  OTVOrani,      
  StokEn,      
  StokBoy,      
  StokYuksekligi,      
  StokKilo,      
  StokRenkKodu,      
  KULNO,      
  KULADI,      
  TP,      
  UygFiyat,      
  Aciklama,      
  Departman,      
  AlisIndirimYuzde,      
  SatisIndirimYuzde,      
  OTVTutari,      
  DepartmanKodu,      
  F1DovizKodu,      
  F2DovizKodu,      
  F3DovizKodu,      
  F4DovizKodu,      
  Update_Date,      
  TeraziTusu,      
  YKasaTusu,      
  Secim,      
  AnaStokKodu,      
  Ambalaj,      
  StokKiloBrut,      
  StokTipi,      
  StokOzellikKodu,      
  AktifPasif,      
  OzelBarkod,      
  Olcu,      
  AlisIndirimYuzde2,      
  AlisIndirimYuzde3,      
  AlisIndirimYuzde4,      
  SatisIndirimYuzde2,      
  SatisIndirimYuzde3,      
  SatisIndirimYuzde4,      
  MuadilStokNo,      
  BonusPuani,      
  BonusPuaniSatici,      
  OlmasiGerekenAdet,      
  PrimYuzde,      
   StokBedenNo,      
  StokRenkBedenCalisir,      
  WASG,      
  WebGrupAdi,      
  SKFIK,      
  GrupOzelKod1,      
  GrupOzelKod2,      
  GrupOzelKod3,      
  GrupOzelKod4,      
   OrjinalKodu,      
  StokAdi2,      
  Aciklama2,      
  WebGrupAdi2,      
  StokAgirlik,      
  StokMaxAgirlik,      
  StokMinAgirlik,      
  AltGrupAdi,      
  AltGrupAdi2,      
  SeriNoTakibi,      
  IRKI,      
  SuniTohum,      
  OzelKod3,      
  OzelKod4,      
  Icmal,      
  SatisFiyatiDegistirilemez,      
  A_Muh_Kodu,      
  S_Muh_Kodu,      
  FireOrani,      
  WebdeGosterilsinmi,      
  TarimElTerminliGosterilsinmi,      
  AltGrupAdi3,      
  AdetTakibi,      
 LotTakibi,    
 UlkeKodu )    
  
  
  
VALUES (@StokNo,      
 @Kodu,      
 @UreticiFirmaKodu,      
 @StokAdi,      
 @OzelKod1,      
 @OzelKod2,      
 @AnaBirim,      
 @Barkodu,      
 @IkinciBirim,      
 @IkinciBirimMiktari,      
 @IkinciBirimBarkodu,      
 @UcuncuBirim,      
 @UcuncuBirimMiktari,      
 @UcuncuBirimBarkodu,      
 @DorduncuBirim,      
 @DorduncuBirimMiktari,      
 @DorduncuBirimBarkodu,      
 @AlisKdv,      
 @SatisKdv,      
 @Indirim,      
 @StokAdedi,      
 @F1Adi,      
 @F2Adi,      
 @F3Adi,      
 @F4Adi,      
 @F1Fiyat,      
 @F2Fiyat,      
 @F3Fiyat,      
 @F4Fiyat,      
 @F1Kdv,      
 @F2Kdv,      
 @F3Kdv,      
 @F4Kdv,      
 @GrupAdi,      
 @UrtNo,      
 @UrtMiktar,      
 @BesinciBirim,      
 @BesinciBirimMiktari,      
 @BesinciBirimBarkodu,      
 @AltinciBirim,      
 @AltinciBirimMiktari,      
 @AltinciBirimBarkodu,      
 @AsgariAdet,      
 @AzamiAdet,      
 @OTVOrani,      
 @StokEn,      
 @StokBoy,      
 @StokYuksekligi,      
 @StokKilo,      
 @StokRenkKodu,      
 @KULNO,      
 @KULADI,      
 @TP,      
 @UygFiyat,      
 @Aciklama,      
 @Departman,      
 @AlisIndirimYuzde,      
 @SatisIndirimYuzde,      
 @OTVTutari,      
 @DepartmanKodu,      
 @F1DovizKodu,      
 @F2DovizKodu,      
 @F3DovizKodu,      
 @F4DovizKodu,      
 @Update_Date,      
 @TeraziTusu,      
 @YKasaTusu,      
 @Secim,      
 @AnaStokKodu,      
 @Ambalaj,      
 @StokKiloBrut,      
 @StokTipi,      
 @StokOzellikKodu,      
 @AktifPasif,      
 @OzelBarkod,      
 @Olcu,      
 @AlisIndirimYuzde2,      
 @AlisIndirimYuzde3,      
 @AlisIndirimYuzde4,      
 @SatisIndirimYuzde2,      
 @SatisIndirimYuzde3,      
 @SatisIndirimYuzde4,      
 @MuadilStokNo,      
 @BonusPuani,      
 @BonusPuaniSatici,      
 @OlmasiGerekenAdet,      
 @PrimYuzde,      
  @StokBedenNo,      
 @StokRenkBedenCalisir,      
 @WASG,      
 @WebGrupAdi,      
 @SKFIK,      
 @GrupOzelKod1,      
 @GrupOzelKod2,      
 @GrupOzelKod3,      
 @GrupOzelKod4,      
  @OrjinalKodu,      
 @StokAdi2,      
 @Aciklama2,      
 @WebGrupAdi2,      
 @StokAgirlik,      
 @StokMaxAgirlik,      
 @StokMinAgirlik,      
 @AltGrupAdi,      
 @AltGrupAdi2,      
 @SeriNoTakibi,      
 @IRKI,      
 @SuniTohum,      
 @OzelKod3,      
 @OzelKod4,      
 @Icmal,      
 @SatisFiyatiDegistirilemez,      
 @A_Muh_Kodu,      
 @S_Muh_Kodu,      
 @FireOrani,      
 @WebdeGosterilsinmi,      
 @TarimElTerminliGosterilsinmi,      
 @AltGrupAdi3,      
 @AdetTakibi,      
@LotTakibi,    
@UlkeKodu)  


go--
if  (select  count(*) from  STOKKARTI where StokNo is null) = 0 and not exists (select  count(*) from  STOKKARTI GROUP BY StokNo HAVING count(*) > 1)
begin
	update ADIGSABITLER SET STOKNO =ISNULL((SELECT MAX(StokNo) from STOKKARTI ),1)+1
end;
go--
DELETE FROM GridView where KayitIsmi  = 'frmOncekiveSonrakiOdemePlanlaricxGridtvSorgu' and GirdAdi  ='Genel Rapor Gridi'
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'StokVergiMuafiyetleri') and name=N'StokKodu')
begin
	ALTER TABLE StokVergiMuafiyetleri ADD StokKodu nvarchar(30)
end ELSE BEGIN
	ALTER TABLE StokVergiMuafiyetleri ALTER COLUMN StokKodu nvarchar(30)
END
go--
if exists (select * from sys.procedures where name='spStkDINAMIKInvoiceIhracat')
drop proc spStkDINAMIKInvoiceIhracat
go--

CREATE   proc [dbo].[spStkDINAMIKInvoiceIhracat] @Tarih1 datetime ,@Tarih2 datetime  
as  
select (K.StokAdi + '-- '  + K.StokAdi2 ) AS STOKADI , H.Birim , H.Miktar , (H.BirimFiyat/H.DovizKuru) DovizBirimFiyatı,H.DovizKodu,(H.BirimFiyat/H.DovizKuru*H.Miktar)  
TOPLAM ,H.DovizKodu,  
H.IslemNo,H.IslemNo3,K.Aciklama2  
from STOKHAR H LEFT JOIN STOKKARTI K ON H.StokKodu=K.Kodu  
where H.FaturaTarihi>=@Tarih1 and H.FaturaTarihi <=@Tarih2  
ORDER BY H.IslemNo3,H.IslemNo,H.NoId

go--
if exists (select * from sys.procedures where name='spTahsilVadeRaporuCari')
drop proc spTahsilVadeRaporuCari
 go--
-------------------------------------------------------------------------------------------------------------------------------------------

create    proc [dbo].[spTahsilVadeRaporuCari] (@CariNo int = 0)        
as   

create table #odeme          
(          
  RecID integer Identity (1, 1) not null, NoId integer, IslemNo integer, Tarih datetime,          
  CariNo integer,  IslemTuru nvarchar(20) COLLATE DATABASE_DEFAULT,  Bakiye float,  Borc_Kullanilan float ,  Devreden float,  Borc float,          
  Devir float  PRIMARY KEY CLUSTERED  (RecID)          
)          
        
Declare @DagitimTutari float          
Declare @NoId integer,  @IslemNo integer,  @Tarih datetime,  @Borc decimal (28, 14), @Borc_Kullanilan decimal (28, 14),  @Son_Kalan_Tutar float,  @IslemTuru nvarchar(20), @TmpTutar decimal(28, 14)               
    
IF @CariNo = 0       
 declare cr_cari cursor  FAST_FORWARD READ_ONLY  for Select MUSNO from V_AMUSTERI where MUSDURUM='A' and MUSBAKIYE > 0.01      
ELSE       
 declare cr_cari cursor  FAST_FORWARD READ_ONLY  for Select MUSNO from V_AMUSTERI where MUSDURUM='A' and MUSBAKIYE > 0.01 and MUSNO = @CariNo      
    
open cr_cari          
fetch next from cr_cari       
into @CariNo      
      
while (@@FETCH_STATUS = 0)    
begin    
  set @DagitimTutari = (select SUM (BORC-ALACAK) from ACARI WHERE MUSNO=@CariNo)           
  set @TmpTutar = @DagitimTutari        
  declare cr_Prepare cursor FAST_FORWARD READ_ONLY for          
  select             
    odm.NoId,          
    odm.IslemNo,          
    odm.Tarih,          
    odm.CariNo,          
    odm.Borc,          
    odm.BAAcikla          
  from Odeme odm          
  where          
  odm.CariNo = @CariNo and          
  IsNull (odm.Borc, 0) > 0          
  order by          
    odm.Tarih desc,NoId desc          
          
  open cr_Prepare          
  fetch next from cr_Prepare          
  into          
    @NoId,          
    @IslemNo,          
    @Tarih,          
    @CariNo,          
    @Borc,          
    @IslemTuru          
          
  while (@@FETCH_STATUS = 0) and (@TmpTutar > 0.0)          
  begin      
    if @Borc >= @TmpTutar           
    begin          
   set @Borc_Kullanilan = @TmpTutar          
   set @Son_Kalan_Tutar = @TmpTutar - @Borc          
   set @TmpTutar = 0.0          
    end          
    else          
    begin          
   set @TmpTutar = @TmpTutar - @Borc          
   set @Borc_Kullanilan = @Borc          
   set @Son_Kalan_Tutar = @TmpTutar          
    end          
          
    insert into #odeme           
    (          
   NoId,          
   IslemNo,          
   Tarih,          
   CariNo,          
   Borc,          
   Borc_Kullanilan,          
   Devreden,          
   Bakiye,          
   Devir,          
   IslemTuru          
    )          
    values          
    (          
   @NoId,           
   @IslemNo,          
   @Tarih,          
   @CariNo,          
   @Borc,          
   @Borc_Kullanilan,          
   @Son_Kalan_Tutar,          
   @DagitimTutari,          
   @Son_Kalan_Tutar+@Borc,          
   @IslemTuru          
    )          
          
    fetch next from cr_Prepare          
    into          
   @NoId,          
   @IslemNo,          
   @Tarih,          
   @CariNo,          
   @Borc,          
   @IslemTuru          
  end             
  close cr_Prepare          
  deallocate cr_Prepare           
      
  update #odeme SET Borc = Devir where RecID = @@IDENTITY      
    
FETCH NEXT FROM cr_Cari INTO @CariNo    
end    
close cr_Cari         
deallocate cr_Cari          
    
declare @paramkolon nvarchar(max),@paramkolonsorgu nvarchar(max)  
declare @Type nvarchar(2),@Id int,@Gun1 int,@Gun2 int, @KolonAdi nvarchar(25)  
declare @kolonsira int,@par_cnt int,@bk_isaret nvarchar(1)  
  
select @par_cnt = count(*) from BakiyeKapatmaParametre  
  
set @kolonsira = 0  
set @paramkolon = '';  
set @paramkolonsorgu = '';  
declare cr_Parametre cursor  FAST_FORWARD READ_ONLY  for   
select 'GO' Tur,NoId,Gun1,Gun2,'GO'+KolonAdi KolonAdi from BakiyeKapatmaParametre   
union all   
select 'EO',NoId,Gun1*-1,Gun2*-1,'EO'+KolonAdi KolonAdi from BakiyeKapatmaParametre   order by Tur desc,NoId   
   
open cr_Parametre 
fetch next from cr_Parametre        			 
into @Type,@Id,@Gun1,@Gun2,@KolonAdi   
 while (@@FETCH_STATUS = 0)         
 begin    
  if @kolonsira = 0   
  begin   
   if @Type = 'GO'  
   begin  
    SET @paramkolon = '['+@KolonAdi+']';  
    SET @paramkolonsorgu = 'CASE WHEN [Geciken Gün]  >= ' +CAST(@Gun1 as nvarchar(5))+ ' and [Geciken Gün] <='+CAST(@Gun2 as nvarchar(5))+' THEN  '''+@KolonAdi+'''';    
   end  
   else  
   begin  
    SET @paramkolon = '['+@KolonAdi+']';  
    SET @paramkolonsorgu = 'CASE WHEN [Geciken Gün]  <= ' +CAST(@Gun1 as nvarchar(5))+ ' and [Geciken Gün] >='+CAST(@Gun2 as nvarchar(5))+' THEN  '''+@KolonAdi+'''';  
   end;  
  end  
  else  
  begin  
   if @Type = 'GO'  
   begin  
    SET @paramkolon = @paramkolon + ',['+@KolonAdi+']'  
    IF @Id = @par_cnt  
     SET @paramkolonsorgu = @paramkolonsorgu + ' WHEN [Geciken Gün]  > ' +CAST(@Gun1 as nvarchar(5))+ ' THEN  '''+@KolonAdi+'''';;   
    ELSE  
     SET @paramkolonsorgu = @paramkolonsorgu + ' WHEN [Geciken Gün]  > ' +CAST(@Gun1 as nvarchar(5))+ ' and [Geciken Gün] <='+CAST(@Gun2 as nvarchar(5))+' THEN  '''+@KolonAdi+'''';   
   end  
   else  
   begin  
    SET @paramkolon = @paramkolon + ',['+@KolonAdi+']'  
    IF @Id = @par_cnt  
     SET @paramkolonsorgu = @paramkolonsorgu + ' WHEN [Geciken Gün]  < ' +CAST(@Gun1 as nvarchar(5))+ ' THEN  '''+@KolonAdi+'''';  
    ELSE  
     SET @paramkolonsorgu = @paramkolonsorgu + ' WHEN [Geciken Gün]  < ' +CAST(@Gun1 as nvarchar(5))+ ' and [Geciken Gün] >='+CAST(@Gun2 as nvarchar(5))+' THEN  '''+@KolonAdi+'''';  
   end;  
  end;       
  SET @kolonsira = @kolonsira + 1;  
 FETCH NEXT FROM cr_Parametre INTO @Type,@Id,@Gun1,@Gun2,@KolonAdi     
 end  
close cr_Parametre         
deallocate cr_Parametre     
SET @paramkolonsorgu = @paramkolonsorgu + ' END GUN'  
  
declare @sorgu nvarchar(max);  
SET @sorgu = '   
Select * from (      
Select     
 [Cari No],    
 [Cari Kodu],    
 [Cari Adı],    
    [Özelkod 1],    
    [Özelkod 2] ,    
    [Özelkod 3],    
    [Özelkod 4],    
    [Telefon],    
    [Cep Telefon],    
    [İl],    
    [İlçe],    
    [Tipi],    
    [Departman],    
    [Satıcı Kodu],    
    [Satıcı Adı],    
    Kilitli,     
 ROUND([Cari Bakiye],0) [Cari Bakiye],ROUND(Borç,0) Borç,      
    '+@paramkolonsorgu+'  
from (        
select          
  RecID,        
  o.NoId,        
  a.MUSNO  [Cari No],          
  a.MUSKOD [Cari Kodu],          
  a.MUSADI [Cari Adı],          
  a.MUSBAKIYE [Cari Bakiye],      
  o.IslemTuru [İşlem Türü],          
  o.IslemNo [İşlem No],          
  c.ACIKLAMA [Açıklama],          
  CONVERT(nvarchar(10),c.TARIH,104)  [İşlem Tarihi],          
  CONVERT(nvarchar(10),o.Tarih,104)  [Vade Tarihi],        
  o.Tarih orderbytarih,      
  CAST(GETDATE() - o.Tarih as int) [Geciken Gün],        
  CASE WHEN CAST(GETDATE() - o.Tarih as int) >=  0 THEN ''GO'' ELSE ''EO'' END Tip,      
  Devreden,        
  o.Borc [Borç],          
  o.Devir,          
  o.Bakiye,          
  a.OZELKOD1 [Özelkod 1],    
  a.OZELKOD2 [Özelkod 2] ,    
  a.OZELKOD3 [Özelkod 3],    
  a.OZELKOD4 [Özelkod 4],    
  a.MUSTEL [Telefon],    
  a.MUSCEP [Cep Telefon],    
  a.MUSIL [İl],    
  a.MUSILCE [İlçe],    
  a.Tipi [Tipi],    
  a.Departman [Departman],    
  a.SaticiKodu [Satıcı Kodu],    
  a.SaticiAdi [Satıcı Adı],    
  a.Kilitli     
 from #odeme o          
 left join  V_AMUSTERI a on  o.CariNo = a.MUSNO          
 left join  ACARI    c on c.ISLEMNO = o.IslemNo and c.BAACIKLA = o.IslemTuru and c.BORC=o.Borc       
 ) t      
 )table1      
 PIVOT        
(        
  SUM([Borç])        
  FOR GUN IN ('+@paramkolon+')        
) AS PivotTable';        
--print @sorgu  
 EXEC sp_executesql @sorgu; 
go--

 if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.1.7')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.1.7',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
     			
go--

select top 5 * from VTGuncellemeleri order by VTVersiyon desc

go--
if exists (select * from sys.procedures where name='sp_FaturaIslemleriListesi')
drop proc sp_FaturaIslemleriListesi
go--
CREATE   PROCEDURE [dbo].[sp_FaturaIslemleriListesi]                                          
  @tarih1 varchar(20),                                           
  @tarih2 varchar(20),                                          
  @dep varchar(500),                                          
  @orderby int,                                          
  @arama varchar(100),                                          
  @GosterilecekKayit int,                                          
  @IslemTuru varchar(20),                                          
  @Durumu int,                              
  @IslemTipi  varchar(30),                  
  @FatSerNo  varchar(3),                  
  @eFaturaNo  varchar(30),                  
  @FaturaTuru varchar(1),      
  @IslemNoLike bit = 0        
AS                                          
              
declare @str nvarchar(max)                                          
declare @rc int                                           
declare @tmp table (trc int)                                          
declare @ayrac varchar(2)                  
declare @peFaturaNo varchar(50)          
if @eFaturaNo<>''          
select @peFaturaNo=UPPER(@eFaturaNo)          
else set @peFaturaNo=''          
set @ayrac='"';                  
            
set @str='select count(*) from (select IslemNo from STOKHARDETBAG where FisTuru = ''F'' AND FaturaTarihi >='''+ @Tarih1+                        
''' and FaturaTarihi <= '''+@Tarih2+'''';       
  
if @peFaturaNo<>''                  
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '     
  
if @FatSerNo<>''                  
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '    
                                     
if @dep <> ''                                          
 set @str=@str+' and Departman in ('+@Dep+')'                                          
if @IslemTuru <> 'Tümü'                                          
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                                
if @IslemTipi <> 'Tümü' and   @IslemTipi <> ''                                         
 set @str=@str+' and IslemTipi like ''%'+@IslemTipi+'%'''                                
      
if @arama <> ''       
begin      
if @orderby=0       
begin      
 if @IslemNoLike = 0      
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '       
 else      
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '       
end;                                        
if @orderby=1                                           
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                           
if @orderby=2                                           
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                          
if @orderby=3                                           
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                           
if @orderby=4                                          
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                          
if @orderby=5                                          
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                               
 if @orderby=6                                          
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                               
if @orderby=7                                          
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                               
if @orderby=8                                          
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '             
if @orderby=9                                          
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))     
   or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))    
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))    
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '       
end;                                      
set @str=@str+')Y';                                                            
    
insert @tmp exec(@str)                                          
select @rc = trc from @tmp;                 
                  
declare @tstr varchar(100);                                        
                                        
if @orderby in (0,9)                                          
  set @tstr='IslemNo'                                          
if @orderby=1                                           
 set @tstr='FaturaNo'                                           
if @orderby=2                                           
 set @tstr='CariAdi'                                          
if @orderby=3                                           
 set @tstr='CariKodu'                                           
if @orderby=4                                          
 set @tstr='KULADI'                                          
if @orderby=5                                          
 set @tstr='SaticiAdi'                                
if @orderby=6                                          
 set @tstr='eFaturaDurumu'                   
 if @orderby=7                                          
 set @tstr='YaziciCount'                   
 if @orderby=8                                          
 set @tstr='FaturaTarihi'                   
                   
set @str='select '''' as plaka, *, '+                      
   'CASE YazarKasaFisNo when 0 then ''TICARIFATURA'' '+                      
   'when 1 then ''TEMELFATURA'' '+                      
   'when 2 then ''IHRACAT'' else ''TICARIFATURA'' end + '' / ''+ case FaturaTipi  when 0 then ''SATIS'' when 1 then ''TEVKIFAT'' when 2 then ''IADE'' when 3 then ''IHRACKAYITLI'' when 4 then ''ISTISNA'' '+                       
   'when 5 then ''MOBIL'' else ''SATIS'' end FaturaSenaryoTip '+                      
   'from STOKHARDETBAG '+                                          
   'where IslemNo in (select X.IslemNo '+                                          
   'from (select ROW_NUMBER() over (order by '+@tstr+') as sira, IslemNo '+                                          
   'from STOKHARDETBAG where FisTuru = '''+@FaturaTuru+''' and FaturaTarihi >='''+@Tarih1+''' and FaturaTarihi <= '''+@Tarih2+'''';                                          
if @peFaturaNo<>''                  
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '                  
                  
if @FatSerNo<>''                  
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '                  
                  
if @dep <> ''                                          
 set @str=@str+' and Departman in ('+@Dep+')'                                          
if @IslemTuru <> 'Tümü'                                          
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                                  
if @IslemTipi <> 'Tümü' and @IslemTipi <> ''                                          
 set @str=@str+' and IslemTipi = '''+@IslemTipi+''''                             
      
if @arama <> ''       
begin                       
if @orderby=0       
begin      
 if @IslemNoLike = 0      
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '       
 else      
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '       
end;                                          
if @orderby=1                                           
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                           
if @orderby=2                                           
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                          
if @orderby=3                                           
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                           
if @orderby=4                                          
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                          
if @orderby=5       
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                               
 if @orderby=6                                          
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                               
if @orderby=7                                          
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                               
if @orderby=8                                          
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '    
if @orderby=9                                          
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))     
      or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))    
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))    
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '        
end;                              
                     
set @str=@str+')X where X.sira >'+convert(varchar,@rc - @GosterilecekKayit) +') order by ';                                          
                                          
if @orderby in (0,9)                                          
  set @str=@str+'IslemNo'                      
if @orderby=1                                           
 set @str=@str+'FaturaNo'                                           
if @orderby=2                                           
 set @str=@str+'CariAdi'                                          
if @orderby=3                                           
 set @str=@str+'CariKodu'                            
if @orderby=4                                          
 set @str=@str+'KULADI'                                          
if @orderby=5                                          
 set @str=@str+'SaticiAdi'                                          
if @orderby=6                                          
 set @str=@str+'eFaturaDurumu'                     
if @orderby=7                                       
 set @str=@str+'YaziciCount'                     
if @orderby=8                                          
 set @str=@str+'FaturaTarihi'                          
exec(@str)   
go--
alter table AMUSTERI alter column MUSIL varchar(100) null
alter table AMUSTERI alter column MUSILCE varchar(100) null
go--
if exists (select * from sys.procedures where name='sp_GELGIDH')
drop proc [sp_GELGIDH]
go--

CREATE  PROC [dbo].[sp_GELGIDH]   
(@tarih1 varchar(20),  
@tarih2 varchar(20),   
@dep varchar(500),   
@orderby int,   
@arama varchar(100),  
@GosterilecekKayit int,  
@IslemTuru varchar(5),  
@KarsiHesap varchar(50),  
@IslemNoLike bit = 0,
@KarsiHesapAdi varchar(150)
)  
as        
begin     
declare @l_KarsiHesapAdi nvarchar(150)
SET @l_KarsiHesapAdi = UPPER(@KarsiHesapAdi)
declare @str varchar(max)              
        
set @str='select * from (select TOP ' + CAST(@GosterilecekKayit as nvarchar(10)) + ' * from GELGID where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';        
        
if @dep <> ''        
 set @str=@str+' and Departman in ('+@Dep+')'        
if @IslemTuru <> 'Tümü'        
 set @str=@str+' and BAACIKLA = '''+@IslemTuru+''''        
if @KarsiHesap <> 'Tümü'        
 set @str=@str+' and KarsiHesp = '''+@KarsiHesap+''''      
   
if @KarsiHesapAdi <> ''        
 set @str=@str+' and (UPPER(HesapAdi) like ''%'+@l_KarsiHesapAdi+'%'' or UPPER(KASAADI) like ''%'+@l_KarsiHesapAdi+'%'' or UPPER(MUSADI) like ''%'+@l_KarsiHesapAdi+'%'')'
 
if @arama <> ''   
begin        
if @orderby=0   
begin  
  if @IslemNoLike = 0  
  set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' '   
  else  
  set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' '   
 end;  
  
 if @orderby=1         
  set @str=@str+'and upper(GELGIDKODU) like upper('''+@arama+'%'') '         
 if @orderby=2         
  set @str=@str+'and upper(GELGIDADI) like upper(''%'+@arama+'%'') '        
 if @orderby=3         
  set @str=@str+'and upper(ACIKLAMA) like upper(''%'+@arama+'%'') '         
 
end;  
  
set @str=@str+' ORDER BY ISLEMNO DESC) y order by ISLEMNO';            
EXEC    (@str)       
PRINT (@str)        
end;  

go--
IF  EXISTS (SELECT *  FROM sys.views WHERE  name='V_Odeme')
 BEGIN 
 drop view V_Odeme
 end
go--
CREATE  VIEW [dbo].[V_Odeme] 
AS 
SELECT     dbo.Odeme.NoId, dbo.Odeme.IslemNo, dbo.Odeme.Tarih, dbo.Odeme.Tutar, dbo.Odeme.Aciklama, dbo.Odeme.Durum, dbo.Odeme.CariNo, dbo.Odeme.CariKodu, 
           dbo.ACARI.MUSADI, dbo.Odeme.Borc, dbo.Odeme.Alacak, dbo.Odeme.NoNo, dbo.Odeme.KULNO, 
                      dbo.Odeme.BAAcikla, dbo.Odeme.Departman, dbo.Odeme.KayitDuzeyi, dbo.Odeme.SiraNo, dbo.Odeme.Modul, ISNULL(dbo.ACARI.DOVIZCINSI, 1) 
                      AS DOVIZCINSI, ISNULL(dbo.ACARI.DOVIZKODU, 'YP') AS DOVIZKODU, 
                      dbo.ACARI.DovizKur
FROM         dbo.ACARI  --RIGHT OUTER JOIN
             INNER JOIN  dbo.Odeme ON dbo.ACARI.BAACIKLA = dbo.Odeme.BAAcikla AND dbo.ACARI.MUSNO = dbo.Odeme.CariNo AND dbo.ACARI.ISLEMNO = dbo.Odeme.IslemNo
go--
IF  EXISTS (SELECT *  FROM sys.views WHERE  name='V_Odeme_DvzBA')
 BEGIN 
 drop view V_Odeme_DvzBA
 end
go--

CREATE  VIEW [dbo].[V_Odeme_DvzBA] 
AS 
SELECT     NoId, IslemNo, Tarih, Tutar, Aciklama, Durum, CariNo, CariKodu, Borc, Alacak, NoNo, KULNO, BAAcikla, Departman, 
                      KayitDuzeyi, SiraNo, Modul, DOVIZCINSI, DOVIZKODU, DovizKur,
                      CASE WHEN Borc = 0 THEN 0 ELSE Borc / DovizKur END AS DovizBorc, CASE WHEN Alacak = 0 THEN 0 ELSE Alacak / DovizKur
                      END AS DovizAlacak
FROM         dbo.V_Odeme

go--
delete from GridView where GirdAdi in ('Günlük Durum Raporu Gridi','Fatura İşlemleri İşlem Gridi')
go--
INSERT INTO VergiMuafiyetleri (Kod,Aciklama) 
Select '0','YOK'  WHERE NOT EXISTS (select 1 from VergiMuafiyetleri where Kod = '0')

go--

IF (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu')  = 0  
BEGIN
	Insert into Parametre Select'Vergi Muafiyet Kodu','0'
END
ELSE IF  (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu' and (Deger = '' or Deger is null))  > 0  
BEGIN
	update Parametre SET Deger = '0' where Param = 'Vergi Muafiyet Kodu'	
END;
go--
        
go--
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.1.8')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.1.8',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'DOVIZT') and name=N'HPS_Gorunsun')
begin
ALTER TABLE DOVIZT ADD HPS_Gorunsun bit default 1
end
go--
--------------------------------------------------------------
update KullaniciAyarlari set Deger='YOK'  where Ayar  ='Kullanılacak Modem Adı' AND Deger='Default'
go--
----------------------------------------------------------------

INSERT INTO VergiMuafiyetleri (Kod,Aciklama) 
Select '0','YOK'  WHERE NOT EXISTS (select 1 from VergiMuafiyetleri where Kod = '0')

go--
----------------------------------------------------------------

IF (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu')  = 0  
BEGIN
	Insert into Parametre Select'Vergi Muafiyet Kodu','0'
END
ELSE IF  (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu' and (Deger = '' or Deger is null))  > 0  
BEGIN
	update Parametre SET Deger = '0' where Param = 'Vergi Muafiyet Kodu'	
END;
go--
--------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_CARIEKSTRE_UST')
drop proc sp_CARIEKSTRE_UST
go--
--------------------------------------------------------------------

CREATE    PROCEDURE [dbo].[sp_CARIEKSTRE_UST]                               
 @MUSNO int, @ILKTARIH nvarchar(10),@SONTARIH nvarchar(10),                           
 @KAPALIHAREKET nvarchar(1),@ORTVADESEC nvarchar(1),                  
 @ISLEMTIPINEGORE SMALLINT=0,@ISLEMTIPINEGOREBAKIYEGRUPLA SMALLINT=0,@P1IslemTipi nvarchar(250)='',@AdresNo int = NULL                                
AS                       
BEGIN                   
 declare @ParamIslemTipi nvarchar(250)                
 declare @pIslemTipi nvarchar(250) declare @pBAKIYE1 money                  
 set @ParamIslemTipi= @P1IslemTipi;                
        
DECLARE @ACARI TABLE (ROWNUMBER INT,                    
  TARIH DATETIME,                     
  TESSIRNO INT,                     
  MUSNO INT,                     
  BAACIKLA nvarchar(14),                    
  ACIKLAMA nvarchar(900),                          
  ISLEMNO INT,                    
  DOVIZTUT float,                    
  DOVIZKODU nvarchar(2),                    
  OZELKOD nvarchar(5),                    
  ISLEMTIPI nvarchar(250),                          
  BELGENO nvarchar(10),                     
  ACIKLABAKIYE nvarchar(1),                    
  TP nvarchar(2),                     
  [Ort Vade] DATETIME,                    
  SaticiKodu nvarchar(30),                       
  SaticiAdi nvarchar(25),                     
  KULNO INT,                    
  KULADI nvarchar(20),                     
  DovizBorc float,                          
  DovizAlacak float,                    
  Aciklama2 nvarchar(200),                          
  SiraNo INT,                     
  CARINO INT,                     
  MUSKOD nvarchar(20),                    
  MUSADI nvarchar(50),                     
  IsIstasyonKodu nvarchar(30),                           
  IsIstasyonAdi nvarchar(30),                     
  TESLIM nvarchar(90),                    
  BORC money,                     
  ALACAK money,                      
  FARK money,                           
  BAKIYE money ,                    
  TTarihi DATETIME,                    
  ISLEMNO3 int,                    
  NoID int identity(1,1),                    
  BA nvarchar(5),                          
  AcikKapali nvarchar(20),                    
  VardiyaKodu nvarchar(15),                   
  Departman nvarchar(15),                    
  IslemNo2 int,                          
  VadeTarihi datetime,                    
  DOVIZCINSI int,NEWROWNUMBER INT,DovizKur float)                       
                         
  declare @Musteri table (MusteriNo decimal,UstMusteriNo decimal)                          
  update ACARI SET AcikKapali='A' WHERE AcikKapali <>'K' and MUSNO = @MUSNO            
  insert into @Musteri                          
  select distinct MUSNO,UstCariNo from AMUSTERI where UstCariNo=@MUSNO                          
                          
DECLARE @BAKIYE money, @IBKY MONEY                         
                           
SET @BAKIYE = 0                          
IF @IBKY IS NULL                          
     SET @IBKY=0                          
 if isnull(@ParamIslemTipi,'')<>''                
 begin                
                 
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,MUSKOD)                          
 select 0,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,A.MUSKOD                       
  from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
   and A.ISLEMTIPI=@ParamIslemTipi     
    group by M.UstMusteriNo,A.MUSKOD                     
 end else                
 begin                
  IF @ISLEMTIPINEGORE=1 BEGIN                  
                 
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(isnull(BORC,0)-isnull(ALACAK,0)),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,convert(varchar(250),A.ISLEMTIPI) as ISLEMTIPI,A.MUSKOD                           
  from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH          
    group by M.UstMusteriNo,A.ISLEMTIPI,A.MUSKOD                    
                
  end else                
  begin                
  INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,A.MUSKOD
from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                 
    group by M.UstMusteriNo,A.MUSKOD                    
end                    
end                
                            
               
                  
 IF @ISLEMTIPINEGORE=1 BEGIN         if isnull(@ParamIslemTipi,'')<>''                
 begin                
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                       
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                  
 and ISLEMTIPI=@ParamIslemTipi                
  ORDER BY ROWNUMBER1 ASC                   
                
 end else begin                
  INSERT INTO @ACARI                  
SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                          
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                  
 ORDER BY ROWNUMBER1 ASC                
                 
end                
 /*ORDER BY TARIH,ACARI.CARINO*/                          
                  
end else                  
begin                  
 if isnull(@ParamIslemTipi,'')<>''                
 begin                          
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH and ISLEMTIPI=@ParamIslemTipi                        
 /*ORDER BY TARIH,ACARI.CARINO*/                  
  ORDER BY ROWNUMBER ASC                   
end else                 
begin                
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                          
 /*ORDER BY TARIH,ACARI.CARINO*/                  
  ORDER BY ROWNUMBER ASC                  
end                
end                  
                  
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0                          
          
declare @ROWNUMBER INT                 
declare @NEWROWNUMBER INT                 
set @NEWROWNUMBER=0                
declare @IslemTipi1 varchar(250)                  
                
IF (@ISLEMTIPINEGORE=1) and (@ParamIslemTipi='')  BEGIN                
                 
delete from @ACARI where BAACIKLA='Devir' and ISLEMTIPI not in (select distinct ISLEMTIPI from @ACARI where  BAACIKLA<>'Devir')                
                
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,0,0,'Devir','Hesap Ekstresi Devir Tutarı',1, A.ISLEMTIPI,A.MUSKOD                             
 from @Musteri M                         
 left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH>@ILKTARIH                 
 where (select count(B.ISLEMNO) from ACARI B where M.MusteriNo=B.MUSNO AND B.TARIH<=@ILKTARIH and A.ISLEMTIPI=B.ISLEMTIPI)=0                
 group by M.UstMusteriNo,A.ISLEMTIPI,A.MUSKOD                   
                  
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0                 
                
declare AcariCursor1 cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ISLEMTIPI ASC,ROWNUMBER ASC                  
FOR UPDATE OF ROWNUMBER OPEN AcariCursor1 FETCH NEXT FROM AcariCursor1                 
INTO @ROWNUMBER,@IslemTipi1                  
WHILE @@FETCH_STATUS=0                  
BEGIN                 
   if @NEWROWNUMBER=0                
   begin                
    set @pIslemTipi=@IslemTipi1;                
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M              
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
     and A.ISLEMTIPI=@pIslemTipi                
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'                
             set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end else if (@pIslemTipi=@IslemTipi1)                
   begin                
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER where ISLEMTIPI=@IslemTipi1 and ROWNUMBER=@ROWNUMBER and  BAACIKLA<>'Devir'                
   set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end else                
   begin       
    set @pIslemTipi=@IslemTipi1;                
     update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M                          
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
     and A.ISLEMTIPI=@pIslemTipi                
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'                
    set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end                
                
FETCH NEXT FROM AcariCursor1 INTO @ROWNUMBER,@IslemTipi1                  
END  CLOSE AcariCursor1  DEALLOCATE AcariCursor1                   
update @ACARI set ROWNUMBER=NEWROWNUMBER                
                
 end                
                
                
                  
DECLARE @BAKIYE2 money                
DECLARE @DevirBakiye money                
                
                
declare AcariCursor cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ROWNUMBER ASC  FOR UPDATE OF BAKIYE OPEN AcariCursor FETCH NEXT FROM AcariCursor                 
INTO @ROWNUMBER,@IslemTipi1                  
                
WHILE @@FETCH_STATUS=0                  
                
BEGIN                     
                
IF @ISLEMTIPINEGORE=0                 
BEGIN                    
  SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER )                    
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                
 end                
  else   begin                    
  if @ROWNUMBER=0                   
  begin               
   UPDATE TOP(1) @ACARI SET BAKIYE =isnull(FARK,0)  FROM @ACARI where ROWNUMBER =0                
   SET @DevirBakiye=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER =0)                  
  end else                
  begin                
  if @ROWNUMBER=1                   
     set @pIslemTipi=@IslemTipi1;                
                
   if @pIslemTipi=@IslemTipi1                      
   begin                       
  SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)                  
                   
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                      
                  
                
   end else                       
   begin                
   set @pIslemTipi=@IslemTipi1;                       
   SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)                 
                   
   UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                    
   end                  
 end                  
  end                
  FETCH NEXT FROM AcariCursor INTO @ROWNUMBER,@IslemTipi1                  
END                  
  CLOSE AcariCursor  DEALLOCATE AcariCursor                               
                  
DELETE FROM AcariTakas WHERE MUSNO  in(select MusteriNo from @Musteri)                           
 insert into AcariTakas(                    
 TARIH,                    
 TESSIRNO,                    
 MUSNO,                    
 BAACIKLA,                    
 ACIKLAMA,                    
 ISLEMNO,                    
 DOVIZTUT,                    
 DOVIZKODU,                    
 OZELKOD,ISLEMTIPI,BELGENO,TP,                          
VadeTarihi,SaticiKodu,SaticiAdi,                    
KULNO,KULADI,DovizBorc,DovizAlacak,Aciklama2,                    
SiraNo,MUSKOD,MUSADI,IsIstasyonKodu,IsIstasyonAdi,                          
TeslimAlan,TeslimEden,BORC,ALACAK,BAKIYE,TTarihi,IslemNo3,                    
BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,DOVIZCINSI,DovizKur)         
      
select A.TARIH,A.TESSIRNO,A.MUSNO,                    
A.BAACIKLA,A.ACIKLAMA,A.ISLEMNO,A.DOVIZTUT,A.DOVIZKODU,                    
A.OZELKOD,A.ISLEMTIPI,A.BELGENO,A.TP,                          
A.[Ort Vade],A.SaticiKodu,A.SaticiAdi,                    
isnull(A.KULNO,0),A.KULADI,isnull(A.DovizBorc,0),                    
isnull(A.DovizAlacak,0),A.Aciklama2,A.NoID,                    
isnull(A.MUSKOD,''),A.MUSADI,A.IsIstasyonKodu,      
A.IsIstasyonAdi,                          
A.TESLIM,A.TESLIM,isnull(A.BORC,0),                    
isnull(A.ALACAK,0),isnull(A.BAKIYE,0),A.TTarihi,                    
A.ISLEMNO3,A.BA,A.AcikKapali,A.VardiyaKodu,                    
isnull(A.Departman,''),isnull(A.IslemNo2,0),isnull(A.DOVIZCINSI,0),ISNULL(A.DovizKur,1)      
                      
 FROM @ACARI A                          
 Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                          
 Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                          
 Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                      
order by  A.ROWNUMBER         
        
  if @ISLEMTIPINEGOREBAKIYEGRUPLA=0                  
  begin                  
 select A.*, ams.Tipi As CariTipi, ams.CariGrubu, ad.AdresNo,                          
  ad.Adres1, dbo.fn_FaturaSerinolari(A.ISLEMNO) As SeriNolar                           
                          
  FROM @ACARI A                          
  Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                          
  Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                          
  Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                   
  order by  A.ROWNUMBER                  
  /*order by  A.MUSNO,TARIH,CARINO */               
 end          
 else                  
 begin                  
   SELECT  ISLEMTIPI,sum(BORC) as BORC,sum(ALACAK) as ALACAK,sum(BORC-ALACAK) as BAKIYE FROM @ACARI    where ISLEMTIPI is not null and MUSNO=@MUSNO  GROUP BY ISLEMTIPI                  
                  
 end                  
                  
 end   
go-- 
-------------------------------------------------------------------
   IF  EXISTS (SELECT *  FROM sys.key_constraints WHERE  name='UQ_Doviz_IsoKodu')
 BEGIN 

 ALTER TABLE Doviz_Iso_Birim  DROP CONSTRAINT UQ_Doviz_IsoKodu
end
else begin
ALTER TABLE Doviz_Iso_Birim ADD CONSTRAINT UQ_Doviz_IsoKodu UNIQUE(IsoKodu)
 end
go--

---------------------------------------------------------------------------
  IF  EXISTS (SELECT *  FROM sys.key_constraints WHERE  name='UQ_StokFiyat_FiyatNo')
 BEGIN 
--select 'drop' 
ALTER TABLE [StokFiyatParametre]  DROP CONSTRAINT UQ_StokFiyat_FiyatNo
end
else begin
--select 'add'
ALTER TABLE [StokFiyatParametre] ADD CONSTRAINT UQ_StokFiyat_FiyatNo UNIQUE(FiyatNo)
end;
go--

---------------------------------------------------------------------------
 CREATE TABLE [dbo].[#StokFiyatParametre](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[FiyatNo] [int] NOT NULL UNIQUE,
	[FiyatAdi] [nvarchar](30) NULL,
	[KDV_DH] [nvarchar](1) NULL,
	[DovizKodu] [nvarchar](3) NULL
) ON [PRIMARY]

go--
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='StokFiyatParametre')
 BEGIN 
 CREATE TABLE [dbo].[StokFiyatParametre](
	[NoId] [int] IDENTITY(1,1) NOT NULL,
	[FiyatNo] [int] NOT NULL UNIQUE,
	[FiyatAdi] [nvarchar](30) NULL,
	[KDV_DH] [nvarchar](1) NULL,
	[DovizKodu] [nvarchar](3) NULL
) ON [PRIMARY]
 end
go--
---------------------------------------------------------------------------
 SET IDENTITY_INSERT [dbo].[#StokFiyatParametre] ON 
 ---------------------------------------------------------------------------
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (1, 1, N'ALIŞ', N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (2, 2, N'SATIŞ', N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (3, 3, N'', N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (4, 4, N'PRK.SATIŞ', N'D', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (5, 5, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (6, 6, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (7, 7, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (8, 8, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (9, 9, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (10, 10, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (11, 11, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (12, 12, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (13, 13, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (14, 14, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (15, 15, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (16, 16, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (17, 17, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (18, 18, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (19, 19, NULL, N'H', N'YP')
INSERT [dbo].[#StokFiyatParametre] ( [NoId],[FiyatNo], [FiyatAdi], [KDV_DH], [DovizKodu]) VALUES (20, 20, NULL, N'H', N'YP')

SET IDENTITY_INSERT [dbo].[#StokFiyatParametre] OFF
go--

insert into StokFiyatParametre (FiyatNo, FiyatAdi, KDV_DH, DovizKodu)
SELECT FiyatNo, FiyatAdi, KDV_DH, DovizKodu FROM  #StokFiyatParametre where FiyatNo not in (select FiyatNo from StokFiyatParametre)
go--
 --select * from [StokFiyatParametre]
 --select * from [#StokFiyatParametre]

go--
 ---------------------------------------------------------------------------
  IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='Doviz_Iso_Birim')
 BEGIN 
CREATE TABLE Doviz_Iso_Birim (IsoKodu nvarchar(5)  NOT NULL ,IsoAciklama nvarchar(250))
 end
go--
-----------------------------------------------------------------

if exists (select * from sys.procedures where name='sp_FiyatGetir')
drop proc sp_FiyatGetir
go--
-----------------------------------------------------------------
CREATE    proc sp_FiyatGetir                               
@IslemTuru varchar(50),  --'AlisFiyati'  , 'AlisIadeFiyati' , 'PerakendeFiyati'                              
@CariKodu varchar(20),                              
@StokKodu varchar(30),                               
@Tarih varchar(8),                        
@IslemNo int                        
as                              
                              
Declare @KDVHaricFiyat float;      
Declare @KDVHaric float;    
Declare @Iskonto1 float;                              
Declare @Iskonto2 float;                              
Declare @Iskonto3 float;                              
Declare @Iskonto4 float;                              
Declare @AlismiSatismi varchar(9);                              
begin                              
  Declare @AlisFiyatiAlan varchar(10);                              
  Declare @SatisFiyatiAlan varchar(10);                              
  Declare @PerakendeFiyatiAlan varchar(10);                            
  Declare @KDV float;                           
                              
  Declare @AlisFiyati float;                              
  Declare @SatisFiyati float;                              
  Declare @PerakendeFiyati float;                              
  Declare @Sql nvarchar(500);                              
  Declare @DonemselFiyatUygulansin varchar(2);                              
  Declare @CariFiyatGrubuKullanilsin varchar(2);                           
  Declare @DovizKodu varchar(20);                            
  Declare @DovizKoduAlisAlan varchar(20);                            
  Declare @DovizKoduSatisAlan varchar(20);         
        
    if (@IslemTuru='AlisFiyati' )                              
      select @Iskonto1=AlisIndirimYuzde, @Iskonto2=AlisIndirimYuzde2,@Iskonto3=AlisIndirimYuzde3,@Iskonto4=AlisIndirimYuzde4,@KDV = AlisKdv from STOKKARTI where Kodu=@StokKodu                              
    if (@IslemTuru='SatisFiyati' or @IslemTuru='PerakendeFiyati')                              
      select @Iskonto1=SatisIndirimYuzde, @Iskonto2=SatisIndirimYuzde2,@Iskonto3=SatisIndirimYuzde3,@Iskonto4=SatisIndirimYuzde4,@KDV = SatisKdv from STOKKARTI where Kodu=@StokKodu                              
          
      
                              
  if @IslemTuru='AlisFiyati'                               
    Set @AlismiSatismi='Alış'                              
  if @IslemTuru='SatisFiyati'                            
    Set @AlismiSatismi='Satış';                    
                               
  if @IslemTuru='AlisFiyati' or @IslemTuru='SatisFiyati' or @IslemTuru='PerakendeFiyati'                    
    begin                            
   --0 ise hayır, -1 ise evet                              
   select @DonemselFiyatUygulansin=Deger from Parametre                               
  where Param ='Kampanya Fiyatları Kullanılsın'                              
   select @CariFiyatGrubuKullanilsin=Deger from Parametre                               
  where Param ='Faturalarda Cari Fiyat Grubu Uygulansın'            
        
      
  declare @KDVDahilFiyat float                        
  declare @KDVDahil float 
 set @KDVDahilFiyat=@KDVHaricFiyat*(@KDV+100)/100        
        
   if (@DonemselFiyatUygulansin='-1')           
      
  begin                              
    select TOP (1) @KDVHaricFiyat = KdvHaricFiyat,      
   @KDVDahilFiyat = KdvDahilFiyat,      
   @Iskonto1=Iskonto1,                              
   @Iskonto2=Iskonto2,                              
   @Iskonto3=Iskonto3,                              
   @Iskonto4=Iskonto4                               
   from Vstk_CariDonemselFiyatlar where CariKodu = @CariKodu                              
   and StokKodu = @StokKodu                                        
   and FiyatTipi = @AlismiSatismi and                              
   @Tarih between BaslangicTarihi and BitisTarihi                               
   order by SiraNo DESC                              
  end;                               
                                 if (@KDVHaricFiyat is null)                              
  begin                              
    if (@CariFiyatGrubuKullanilsin='-1')                              
   begin                              
  select @AlisFiyatiAlan = AlisFiyatiH, @SatisFiyatiAlan=SatisFiyatiH, @PerakendeFiyatiAlan=PerakendeFiyati                               
    from CariFiyatlari                               
    where FiyatGrupAdi = (select ISNULL(FiyatGrupAdi,'F.G.A.YOK') as FiyatGrupAdi from                               
    AMUSTERI where MUSKOD = @CariKodu)                              
   end;                            
                                        
   if (@AlisFiyatiAlan is null)                              
   begin                              
   Set @AlisFiyatiAlan = 'F1FiyatH';                              
   Set @SatisFiyatiAlan = 'F2FiyatH';                              
   Set @PerakendeFiyatiAlan = 'F4FiyatH';                              
   end;                            
                            
   Set @DovizKoduAlisAlan = LEFT(@AlisFiyatiAlan,2)+'DovizKodu';                    
                               
   Set @DovizKoduSatisAlan = LEFT(@SatisFiyatiAlan,2)+'DovizKodu';                              
                     
   if (@IslemTuru='PerakendeFiyati')                  
     Set @DovizKoduSatisAlan = LEFT(@PerakendeFiyatiAlan,2)+'DovizKodu';                              
                               
   if (@IslemTuru='AlisFiyati' or @IslemTuru='AlisIadeFiyati')                              
     Set @Sql='select @KdvHaricFiyat='+ @AlisFiyatiAlan+',@DovizKodu='+@DovizKoduAlisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                              
   if (@IslemTuru='SatisFiyati' or @IslemTuru='SatisIadeFiyati')                              
     Set @Sql='select @KdvHaricFiyat='+ @SatisFiyatiAlan+',@DovizKodu='+@DovizKoduSatisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                    
   if (@IslemTuru='PerakendeFiyati')                  
     Set @Sql='select @KdvHaricFiyat='+ @PerakendeFiyatiAlan+',@DovizKodu='+@DovizKoduSatisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                                                 
                                         
   execute sp_executesql @Sql,N'@KdvHaricFiyat float Output, @DovizKodu varchar(20) Output ', @KdvHaricFiyat=@KDVHaricFiyat Output, @DovizKodu=@DovizKodu Output;                            
   select @KDVHaric = @KDVHaricFiyat                       
   Select @KDVHaricFiyat = @KDVHaricFiyat * (select GunFiyati from DOVIZT where DovizKodu=@DovizKodu)  
    
     
   declare @F1FifoFiyat   float  
   SET @F1FifoFiyat = (select   (F1FiyatH * (100  + SatisKdv) / 100) * (select GunFiyati from DOVIZT where DovizKodu=@DovizKodu)  from StokFiyat where Kodu = @StokKodu)  
              
              
   --print 'DovizKodu='+@DovizKodu+' Sql='+@sql                          
    end;                              
                              
    end;                      
  if (@IslemTuru='AlisIadeFiyati')                    
    begin                    
      select top 1 @KDVHaricFiyat=BirimFiyat, @Iskonto1=Iskonto1, @Iskonto2=Iskonto2,@Iskonto3=Iskonto3,@Iskonto4=Iskonto4, @KDV=Kdv  from STOKHAR where CariKodu = @CariKodu and                     
  IslemTuru = 'Alış' and StokKodu=@StokKodu                    
  order by NoId desc                    
 end;                    
  if (@IslemTuru='SatisIadeFiyati')                    
    begin                    
      select top 1 @KDVHaricFiyat=BirimFiyat, @Iskonto1=Iskonto1, @Iskonto2=Iskonto2,@Iskonto3=Iskonto3,@Iskonto4=Iskonto4, @KDV=Kdv from STOKHAR where CariKodu = @CariKodu and                     
   IslemTuru in  ('Satış','Prk.Satış')  and StokKodu=@StokKodu                    
   order by NoId desc                    
 end;                  
                
declare @strIslemNo varchar                        
set @strIslemNo=convert(varchar,@IslemNo)                        
--declare @KDVDahilFiyat float       
set @KDVDahil = @KDVHaric*(@KDV+100)/100                          
set @KDVDahilFiyat=@KDVHaricFiyat*(@KDV+100)/100                         
                  
select  @KdvHaricFiyat as KDVHaricFiyat,@KDVDahilFiyat as KDVDahilFiyat,
	   @Iskonto1 as Iskonto1,@Iskonto2 as Iskonto2, @Iskonto3 as Iskonto3,@Iskonto4 as Iskonto4,
	   @KDV as KDV , @DonemselFiyatUygulansin AS KampanyaFyt, @CariFiyatGrubuKullanilsin FiyatGrp,@F1FifoFiyat F1FifoFiyat,      
       @DovizKodu as DovizKodu,@KDVDahil KDDovizFiyati,@KDVHaric KHDovizFiyati
               
end             
go--
---------------------------------- ----------------
ALTER LOGIN [Sbs_Net] WITH PASSWORD=N'!Sbs_Net!95', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[Türkçe], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
go--
ALTER LOGIN [Sbs_Net] DISABLE
go--
ALTER SERVER ROLE [sysadmin] ADD MEMBER [Sbs_Net]
go--
ALTER LOGIN [Sbs_Net] ENABLE
go--

IF not  EXISTS (SELECT *  FROM sys.tables WHERE  name='CekmeceHareket')
 BEGIN 
CREATE TABLE CekmeceHareket (
	Id int NOT NULL,
	Kayit_Tarihi smalldatetime NULL,
	Kullanici_Id int NULL,
	Aciklama nvarchar(100) NULL,
	Onay bit NULL,
	Kaynak char(1) NULL
)
end
go--
 IF   EXISTS (SELECT *  FROM sys.tables WHERE  name='BAAciklamaTanim')
 BEGIN 
	drop table [BAAciklamaTanim]
	end
go--

CREATE TABLE [dbo].[BAAciklamaTanim](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Tanim] [nvarchar](30) not NULL 
) ON [PRIMARY]
go--
ALTER TABLE [BAAciklamaTanim] ADD CONSTRAINT UQ_BAAciklama_Tanim UNIQUE(Tanim)
go--
SET IDENTITY_INSERT [dbo].[BAAciklamaTanim] ON 
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (1, N'Alış')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (2, N'Alış İade')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (3, N'Alacak')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (4, N'Borç')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (5, N'G.Virman')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (6, N'Gel.Havale')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (7, N'Gön.Havale')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (8, N'KK.Tahsilat')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (9, N'M.Mahsup')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (10, N'Mutabakat')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (11, N'Prk.Satış')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (12, N'Satış')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (13, N'Satış İade')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (14, N'Senet Girişi')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (15, N'Senet Çıkışı')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (16, N'Tahsilat')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (17, N'Virman')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (18, N'Çek İade')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (19, N'Çek Girişi')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (20, N'Çek Çıkışı')
go--
INSERT [dbo].[BAAciklamaTanim] ([Id], [Tanim]) VALUES (21, N'Ödeme')
go--
SET IDENTITY_INSERT [dbo].[BAAciklamaTanim] OFF
go--
SET ANSI_PADDING ON
go--
if  not  exists (select * from dbo.syscolumns where id = object_id(N'DOVIZT') and name=N'HPS_Gorunsun')
begin
ALTER TABLE DOVIZT ADD HPS_Gorunsun bit default 1
end
go--
--------------------------------------------------------------
update KullaniciAyarlari set Deger='YOK'  where Ayar  ='Kullanılacak Modem Adı' AND Deger='Default'
go--
----------------------------------------------------------------

INSERT INTO VergiMuafiyetleri (Kod,Aciklama) 
Select '0','YOK'  WHERE NOT EXISTS (select 1 from VergiMuafiyetleri where Kod = '0')

go--
----------------------------------------------------------------

IF (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu')  = 0  
BEGIN
	Insert into Parametre Select'Vergi Muafiyet Kodu','0'
END
ELSE IF  (Select COUNT(*) from Parametre where Param = 'Vergi Muafiyet Kodu' and (Deger = '' or Deger is null))  > 0  
BEGIN
	update Parametre SET Deger = '0' where Param = 'Vergi Muafiyet Kodu'	
END;
go--
--------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_CARIEKSTRE_UST')
drop proc sp_CARIEKSTRE_UST
go--
--------------------------------------------------------------------

CREATE    PROCEDURE [dbo].[sp_CARIEKSTRE_UST]                               
 @MUSNO int, @ILKTARIH nvarchar(10),@SONTARIH nvarchar(10),                           
 @KAPALIHAREKET nvarchar(1),@ORTVADESEC nvarchar(1),                  
 @ISLEMTIPINEGORE SMALLINT=0,@ISLEMTIPINEGOREBAKIYEGRUPLA SMALLINT=0,@P1IslemTipi nvarchar(250)='',@AdresNo int = NULL                                
AS                       
BEGIN                   
 declare @ParamIslemTipi nvarchar(250)                
 declare @pIslemTipi nvarchar(250) declare @pBAKIYE1 money                  
 set @ParamIslemTipi= @P1IslemTipi;                
        
DECLARE @ACARI TABLE (ROWNUMBER INT,                    
  TARIH DATETIME,                     
  TESSIRNO INT,                     
  MUSNO INT,                     
  BAACIKLA nvarchar(14),                    
  ACIKLAMA nvarchar(900),                          
  ISLEMNO INT,                    
  DOVIZTUT float,                    
  DOVIZKODU nvarchar(2),                    
  OZELKOD nvarchar(5),                    
  ISLEMTIPI nvarchar(250),                          
  BELGENO nvarchar(10),                     
  ACIKLABAKIYE nvarchar(1),                    
  TP nvarchar(2),                     
  [Ort Vade] DATETIME,                    
  SaticiKodu nvarchar(30),                       
  SaticiAdi nvarchar(25),                     
  KULNO INT,                    
  KULADI nvarchar(20),                     
  DovizBorc float,                          
  DovizAlacak float,                    
  Aciklama2 nvarchar(200),                          
  SiraNo INT,                     
  CARINO INT,                     
  MUSKOD nvarchar(20),                    
  MUSADI nvarchar(50),                     
  IsIstasyonKodu nvarchar(30),                           
  IsIstasyonAdi nvarchar(30),                     
  TESLIM nvarchar(90),                    
  BORC money,                     
  ALACAK money,                      
  FARK money,                           
  BAKIYE money ,                    
  TTarihi DATETIME,                    
  ISLEMNO3 int,                    
  NoID int identity(1,1),                    
  BA nvarchar(5),                          
  AcikKapali nvarchar(20),                    
  VardiyaKodu nvarchar(15),                   
  Departman nvarchar(15),                    
  IslemNo2 int,                          
  VadeTarihi datetime,                    
  DOVIZCINSI int,NEWROWNUMBER INT,DovizKur float)                       
                         
  declare @Musteri table (MusteriNo decimal,UstMusteriNo decimal)                          
  update ACARI SET AcikKapali='A' WHERE AcikKapali <>'K' and MUSNO = @MUSNO            
  insert into @Musteri                          
  select distinct MUSNO,UstCariNo from AMUSTERI where UstCariNo=@MUSNO                          
                          
DECLARE @BAKIYE money, @IBKY MONEY                         
                           
SET @BAKIYE = 0                          
IF @IBKY IS NULL                          
     SET @IBKY=0                          
 if isnull(@ParamIslemTipi,'')<>''                
 begin                
                 
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,MUSKOD)                          
 select 0,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,A.MUSKOD                       
  from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
   and A.ISLEMTIPI=@ParamIslemTipi     
    group by M.UstMusteriNo,A.MUSKOD                     
 end else                
 begin                
  IF @ISLEMTIPINEGORE=1 BEGIN                  
                 
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(isnull(BORC,0)-isnull(ALACAK,0)),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,convert(varchar(250),A.ISLEMTIPI) as ISLEMTIPI,A.MUSKOD                           
  from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH          
    group by M.UstMusteriNo,A.ISLEMTIPI,A.MUSKOD                    
                
  end else                
  begin                
  INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,SUM(BORC-ALACAK),@BAKIYE,'Devir','Hesap Ekstresi Devir Tutarı',1,A.MUSKOD
from @Musteri M                          
  left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                 
    group by M.UstMusteriNo,A.MUSKOD                    
end                    
end                
                            
               
                  
 IF @ISLEMTIPINEGORE=1 BEGIN         if isnull(@ParamIslemTipi,'')<>''                
 begin                
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                       
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                  
 and ISLEMTIPI=@ParamIslemTipi                
  ORDER BY ROWNUMBER1 ASC                   
                
 end else begin                
  INSERT INTO @ACARI                  
SELECT ROW_NUMBER() OVER(ORDER BY ISLEMTIPI ASC,TARIH ASC,CARINO ASC) AS ROWNUMBER1,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                          
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                  
 ORDER BY ROWNUMBER1 ASC                
                 
end                
 /*ORDER BY TARIH,ACARI.CARINO*/                          
                  
end else                  
begin                  
 if isnull(@ParamIslemTipi,'')<>''                
 begin                          
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH and ISLEMTIPI=@ParamIslemTipi                        
 /*ORDER BY TARIH,ACARI.CARINO*/                  
  ORDER BY ROWNUMBER ASC                   
end else                 
begin                
 INSERT INTO @ACARI                          
 SELECT ROW_NUMBER() OVER(ORDER BY  TARIH ASC,CARINO ASC) AS ROWNUMBER,TARIH,TESSIRNO,MUSNO,BAACIKLA, ISNULL(ACIKLAMA, '''') as ACIKLAMA,                          
  ISLEMNO,DOVIZTUT,DOVIZKODU,OZELKOD,ISLEMTIPI,                          
  ISNULL(BELGENO, '''') as [BELGENO],ACIKLABAKIYE,TP,VadeTarihi as [Ort Vade],SaticiKodu,                          
  SaticiAdi, KULNO, KULADI, DovizBorc,                          
  DovizAlacak,Aciklama2,                          
  SiraNo, CARINO, MUSKOD, MUSADI, IsIstasyonKodu,                           
  IsIstasyonAdi,(select TeslimEden + ' - - ' + TeslimAlan from ACIKLAMA where FATURANO = ISLEMNO) as Teslim,                          
  BORC money, ALACAK money,(BORC-ALACAK), @BAKIYE,TARIH TTarihi,ISLEMNO as ISLEMNO3,                          
  BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,VadeTarihi,DOVIZCINSI,0,DovizKur                           
 FROM ACARI                           
 WHERE MUSNO in(select MusteriNo from @Musteri)  AND TARIH >@ILKTARIH AND TARIH<=@SONTARIH                          
 /*ORDER BY TARIH,ACARI.CARINO*/                  
  ORDER BY ROWNUMBER ASC                  
end                
end                  
                  
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0                          
          
declare @ROWNUMBER INT                 
declare @NEWROWNUMBER INT                 
set @NEWROWNUMBER=0                
declare @IslemTipi1 varchar(250)                  
                
IF (@ISLEMTIPINEGORE=1) and (@ParamIslemTipi='')  BEGIN                
                 
delete from @ACARI where BAACIKLA='Devir' and ISLEMTIPI not in (select distinct ISLEMTIPI from @ACARI where  BAACIKLA<>'Devir')                
                
 INSERT INTO @ACARI (ROWNUMBER,TARIH,MUSNO,FARK,BAKIYE,BAACIKLA,ACIKLAMA,ISLEMNO,ISLEMTIPI,MUSKOD)                          
 select 0 as ROWNUMBER,@ILKTARIH,M.UstMusteriNo,0,0,'Devir','Hesap Ekstresi Devir Tutarı',1, A.ISLEMTIPI,A.MUSKOD                             
 from @Musteri M                         
 left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH>@ILKTARIH                 
 where (select count(B.ISLEMNO) from ACARI B where M.MusteriNo=B.MUSNO AND B.TARIH<=@ILKTARIH and A.ISLEMTIPI=B.ISLEMTIPI)=0                
 group by M.UstMusteriNo,A.ISLEMTIPI,A.MUSKOD                   
                  
update @ACARI SET IslemNo3 = ISLEMNO where ISLEMNO>0                 
                
declare AcariCursor1 cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ISLEMTIPI ASC,ROWNUMBER ASC                  
FOR UPDATE OF ROWNUMBER OPEN AcariCursor1 FETCH NEXT FROM AcariCursor1                 
INTO @ROWNUMBER,@IslemTipi1                  
WHILE @@FETCH_STATUS=0                  
BEGIN                 
   if @NEWROWNUMBER=0                
   begin                
    set @pIslemTipi=@IslemTipi1;                
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M              
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
     and A.ISLEMTIPI=@pIslemTipi                
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'                
             set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end else if (@pIslemTipi=@IslemTipi1)                
   begin                
    update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER where ISLEMTIPI=@IslemTipi1 and ROWNUMBER=@ROWNUMBER and  BAACIKLA<>'Devir'                
   set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end else                
   begin       
    set @pIslemTipi=@IslemTipi1;                
     update TOP(1) @ACARI SET NEWROWNUMBER=@NEWROWNUMBER,BAKIYE=(select SUM(BORC-ALACAK) from @Musteri M                          
     left JOIN  ACARI A ON M.MusteriNo=A.MUSNO AND A.TARIH<=@ILKTARIH                  
     and A.ISLEMTIPI=@pIslemTipi                
      group by M.UstMusteriNo) where ISLEMTIPI=@IslemTipi1 and BAACIKLA='Devir'                
    set @NEWROWNUMBER=@NEWROWNUMBER+1;                
                
   end                
                
FETCH NEXT FROM AcariCursor1 INTO @ROWNUMBER,@IslemTipi1                  
END  CLOSE AcariCursor1  DEALLOCATE AcariCursor1                   
update @ACARI set ROWNUMBER=NEWROWNUMBER                
                
 end                
                
                
                  
DECLARE @BAKIYE2 money                
DECLARE @DevirBakiye money                
                
                
declare AcariCursor cursor  for Select ROWNUMBER,ISLEMTIPI FROM  @ACARI ORDER BY ROWNUMBER ASC  FOR UPDATE OF BAKIYE OPEN AcariCursor FETCH NEXT FROM AcariCursor                 
INTO @ROWNUMBER,@IslemTipi1                  
                
WHILE @@FETCH_STATUS=0                  
                
BEGIN                     
                
IF @ISLEMTIPINEGORE=0                 
BEGIN                    
  SET @BAKIYE2=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER )                    
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                
 end                
  else   begin                    
  if @ROWNUMBER=0                   
  begin               
   UPDATE TOP(1) @ACARI SET BAKIYE =isnull(FARK,0)  FROM @ACARI where ROWNUMBER =0                
   SET @DevirBakiye=(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER =0)                  
  end else                
  begin                
  if @ROWNUMBER=1                   
     set @pIslemTipi=@IslemTipi1;                
                
   if @pIslemTipi=@IslemTipi1                      
   begin                       
  SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)                  
                   
  UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                      
                  
                
   end else                       
   begin                
   set @pIslemTipi=@IslemTipi1;                       
   SET @BAKIYE2=@DevirBakiye+(SELECT SUM(ISNULL(FARK,0)) FROM @ACARI  WHERE ROWNUMBER<=@ROWNUMBER and ISLEMTIPI=@pIslemTipi)                 
                   
   UPDATE TOP(1) @ACARI SET BAKIYE=@BAKIYE2 WHERE ROWNUMBER=@ROWNUMBER                    
   end                  
 end                  
  end                
  FETCH NEXT FROM AcariCursor INTO @ROWNUMBER,@IslemTipi1                  
END                  
  CLOSE AcariCursor  DEALLOCATE AcariCursor                               
                  
DELETE FROM AcariTakas WHERE MUSNO  in(select MusteriNo from @Musteri)                           
 insert into AcariTakas(                    
 TARIH,                    
 TESSIRNO,                    
 MUSNO,                    
 BAACIKLA,                    
 ACIKLAMA,                    
 ISLEMNO,                    
 DOVIZTUT,                    
 DOVIZKODU,                    
 OZELKOD,ISLEMTIPI,BELGENO,TP,                          
VadeTarihi,SaticiKodu,SaticiAdi,                    
KULNO,KULADI,DovizBorc,DovizAlacak,Aciklama2,                    
SiraNo,MUSKOD,MUSADI,IsIstasyonKodu,IsIstasyonAdi,                          
TeslimAlan,TeslimEden,BORC,ALACAK,BAKIYE,TTarihi,IslemNo3,                    
BA,AcikKapali,VardiyaKodu,Departman,IslemNo2,DOVIZCINSI,DovizKur)         
      
select A.TARIH,A.TESSIRNO,A.MUSNO,                    
A.BAACIKLA,A.ACIKLAMA,A.ISLEMNO,A.DOVIZTUT,A.DOVIZKODU,                    
A.OZELKOD,A.ISLEMTIPI,A.BELGENO,A.TP,                          
A.[Ort Vade],A.SaticiKodu,A.SaticiAdi,                    
isnull(A.KULNO,0),A.KULADI,isnull(A.DovizBorc,0),                    
isnull(A.DovizAlacak,0),A.Aciklama2,A.NoID,                    
isnull(A.MUSKOD,''),A.MUSADI,A.IsIstasyonKodu,      
A.IsIstasyonAdi,                          
A.TESLIM,A.TESLIM,isnull(A.BORC,0),                    
isnull(A.ALACAK,0),isnull(A.BAKIYE,0),A.TTarihi,                    
A.ISLEMNO3,A.BA,A.AcikKapali,A.VardiyaKodu,                    
isnull(A.Departman,''),isnull(A.IslemNo2,0),isnull(A.DOVIZCINSI,0),ISNULL(A.DovizKur,1)      
                      
 FROM @ACARI A                          
 Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                          
 Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                          
 Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                      
order by  A.ROWNUMBER         
        
  if @ISLEMTIPINEGOREBAKIYEGRUPLA=0                  
  begin                  
 select A.*, ams.Tipi As CariTipi, ams.CariGrubu, ad.AdresNo,                          
  ad.Adres1, dbo.fn_FaturaSerinolari(A.ISLEMNO) As SeriNolar                           
                          
  FROM @ACARI A                          
  Left Join STOKHARDETBAG sb on sb.IslemNo=A.ISLEMNO                          
  Left Join Adres ad on ad.CariKodu=sb.CariKodu and ad.AdresNo=sb.FaturaAdresNo                          
  Left Join AMUSTERI ams on ams.MUSNO=A.MUSNO                   
  order by  A.ROWNUMBER                  
  /*order by  A.MUSNO,TARIH,CARINO */               
 end          
 else                  
 begin                  
   SELECT  ISLEMTIPI,sum(BORC) as BORC,sum(ALACAK) as ALACAK,sum(BORC-ALACAK) as BAKIYE FROM @ACARI    where ISLEMTIPI is not null and MUSNO=@MUSNO  GROUP BY ISLEMTIPI                  
                  
 end                  
                  
 end   
 go--
 ---------------------------------------------------------------------------
  IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='Doviz_Iso_Birim')
 BEGIN 
CREATE TABLE Doviz_Iso_Birim (IsoKodu nvarchar(5) UNIQUE NOT NULL ,IsoAciklama nvarchar(250))
 end
go--
-----------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_FiyatGetir')
drop proc sp_FiyatGetir
go--
-----------------------------------------------------------------
CREATE    proc sp_FiyatGetir                               
@IslemTuru varchar(50),  --'AlisFiyati'  , 'AlisIadeFiyati' , 'PerakendeFiyati'                              
@CariKodu varchar(20),                              
@StokKodu varchar(30),                               
@Tarih varchar(8),                        
@IslemNo int                        
as                              
                              
Declare @KDVHaricFiyat float;      
Declare @KDVHaric float;    
Declare @Iskonto1 float;                              
Declare @Iskonto2 float;                              
Declare @Iskonto3 float;                              
Declare @Iskonto4 float;                              
Declare @AlismiSatismi varchar(9);                              
begin                              
  Declare @AlisFiyatiAlan varchar(10);                              
  Declare @SatisFiyatiAlan varchar(10);                              
  Declare @PerakendeFiyatiAlan varchar(10);                            
  Declare @KDV float;                           
                              
  Declare @AlisFiyati float;                              
  Declare @SatisFiyati float;                              
  Declare @PerakendeFiyati float;                              
  Declare @Sql nvarchar(500);                              
  Declare @DonemselFiyatUygulansin varchar(2);                              
  Declare @CariFiyatGrubuKullanilsin varchar(2);                           
  Declare @DovizKodu varchar(20);                            
  Declare @DovizKoduAlisAlan varchar(20);                            
  Declare @DovizKoduSatisAlan varchar(20);         
        
    if (@IslemTuru='AlisFiyati' )                              
      select @Iskonto1=AlisIndirimYuzde, @Iskonto2=AlisIndirimYuzde2,@Iskonto3=AlisIndirimYuzde3,@Iskonto4=AlisIndirimYuzde4,@KDV = AlisKdv from STOKKARTI where Kodu=@StokKodu                              
    if (@IslemTuru='SatisFiyati' or @IslemTuru='PerakendeFiyati')                              
      select @Iskonto1=SatisIndirimYuzde, @Iskonto2=SatisIndirimYuzde2,@Iskonto3=SatisIndirimYuzde3,@Iskonto4=SatisIndirimYuzde4,@KDV = SatisKdv from STOKKARTI where Kodu=@StokKodu                              
          
      
                              
  if @IslemTuru='AlisFiyati'                               
    Set @AlismiSatismi='Alış'                              
  if @IslemTuru='SatisFiyati'                            
    Set @AlismiSatismi='Satış';                    
                               
  if @IslemTuru='AlisFiyati' or @IslemTuru='SatisFiyati' or @IslemTuru='PerakendeFiyati'                    
    begin                            
   --0 ise hayır, -1 ise evet                              
   select @DonemselFiyatUygulansin=Deger from Parametre                               
  where Param ='Kampanya Fiyatları Kullanılsın'                              
   select @CariFiyatGrubuKullanilsin=Deger from Parametre                               
  where Param ='Faturalarda Cari Fiyat Grubu Uygulansın'            
        
      
  declare @KDVDahilFiyat float                        
  declare @KDVDahil float 
 set @KDVDahilFiyat=@KDVHaricFiyat*(@KDV+100)/100        
        
   if (@DonemselFiyatUygulansin='-1')           
      
  begin                              
    select TOP (1) @KDVHaricFiyat = KdvHaricFiyat,      
   @KDVDahilFiyat = KdvDahilFiyat,      
   @Iskonto1=Iskonto1,                              
   @Iskonto2=Iskonto2,                              
   @Iskonto3=Iskonto3,                              
   @Iskonto4=Iskonto4                               
   from Vstk_CariDonemselFiyatlar where CariKodu = @CariKodu                              
   and StokKodu = @StokKodu                                        
   and FiyatTipi = @AlismiSatismi and                              
   @Tarih between BaslangicTarihi and BitisTarihi                               
   order by SiraNo DESC                              
  end;                               
                                 if (@KDVHaricFiyat is null)                              
  begin                              
    if (@CariFiyatGrubuKullanilsin='-1')                              
   begin                              
  select @AlisFiyatiAlan = AlisFiyatiH, @SatisFiyatiAlan=SatisFiyatiH, @PerakendeFiyatiAlan=PerakendeFiyati                               
    from CariFiyatlari                               
    where FiyatGrupAdi = (select ISNULL(FiyatGrupAdi,'F.G.A.YOK') as FiyatGrupAdi from                               
    AMUSTERI where MUSKOD = @CariKodu)                              
   end;                            
                                        
   if (@AlisFiyatiAlan is null)                              
   begin                              
   Set @AlisFiyatiAlan = 'F1FiyatH';                              
   Set @SatisFiyatiAlan = 'F2FiyatH';                              
   Set @PerakendeFiyatiAlan = 'F4FiyatH';                              
   end;                            
                            
   Set @DovizKoduAlisAlan = LEFT(@AlisFiyatiAlan,2)+'DovizKodu';                    
                               
   Set @DovizKoduSatisAlan = LEFT(@SatisFiyatiAlan,2)+'DovizKodu';                              
                     
   if (@IslemTuru='PerakendeFiyati')                  
     Set @DovizKoduSatisAlan = LEFT(@PerakendeFiyatiAlan,2)+'DovizKodu';                              
                               
   if (@IslemTuru='AlisFiyati' or @IslemTuru='AlisIadeFiyati')                              
     Set @Sql='select @KdvHaricFiyat='+ @AlisFiyatiAlan+',@DovizKodu='+@DovizKoduAlisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                              
   if (@IslemTuru='SatisFiyati' or @IslemTuru='SatisIadeFiyati')                              
     Set @Sql='select @KdvHaricFiyat='+ @SatisFiyatiAlan+',@DovizKodu='+@DovizKoduSatisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                    
   if (@IslemTuru='PerakendeFiyati')                  
     Set @Sql='select @KdvHaricFiyat='+ @PerakendeFiyatiAlan+',@DovizKodu='+@DovizKoduSatisAlan+' from StokFiyat where Kodu='''+@StokKodu+'''';                                                 
                                         
   execute sp_executesql @Sql,N'@KdvHaricFiyat float Output, @DovizKodu varchar(20) Output ', @KdvHaricFiyat=@KDVHaricFiyat Output, @DovizKodu=@DovizKodu Output;                            
   select @KDVHaric = @KDVHaricFiyat                       
   Select @KDVHaricFiyat = @KDVHaricFiyat * (select GunFiyati from DOVIZT where DovizKodu=@DovizKodu)  
    
     
   declare @F1FifoFiyat   float  
   SET @F1FifoFiyat = (select   (F1FiyatH * (100  + SatisKdv) / 100) * (select GunFiyati from DOVIZT where DovizKodu=@DovizKodu)  from StokFiyat where Kodu = @StokKodu)  
              
              
   --print 'DovizKodu='+@DovizKodu+' Sql='+@sql                          
    end;                              
                              
    end;                      
  if (@IslemTuru='AlisIadeFiyati')                    
    begin                    
      select top 1 @KDVHaricFiyat=BirimFiyat, @Iskonto1=Iskonto1, @Iskonto2=Iskonto2,@Iskonto3=Iskonto3,@Iskonto4=Iskonto4, @KDV=Kdv  from STOKHAR where CariKodu = @CariKodu and                     
  IslemTuru = 'Alış' and StokKodu=@StokKodu                    
  order by NoId desc                    
 end;                    
  if (@IslemTuru='SatisIadeFiyati')                    
    begin                    
      select top 1 @KDVHaricFiyat=BirimFiyat, @Iskonto1=Iskonto1, @Iskonto2=Iskonto2,@Iskonto3=Iskonto3,@Iskonto4=Iskonto4, @KDV=Kdv from STOKHAR where CariKodu = @CariKodu and                     
   IslemTuru in  ('Satış','Prk.Satış')  and StokKodu=@StokKodu                    
   order by NoId desc                    
 end;                  
                
declare @strIslemNo varchar                        
set @strIslemNo=convert(varchar,@IslemNo)                        
--declare @KDVDahilFiyat float       
set @KDVDahil = @KDVHaric*(@KDV+100)/100                          
set @KDVDahilFiyat=@KDVHaricFiyat*(@KDV+100)/100                         
                  
select  @KdvHaricFiyat as KDVHaricFiyat,@KDVDahilFiyat as KDVDahilFiyat,
	   @Iskonto1 as Iskonto1,@Iskonto2 as Iskonto2, @Iskonto3 as Iskonto3,@Iskonto4 as Iskonto4,
	   @KDV as KDV , @DonemselFiyatUygulansin AS KampanyaFyt, @CariFiyatGrubuKullanilsin FiyatGrp,@F1FifoFiyat F1FifoFiyat,      
       @DovizKodu as DovizKodu,@KDVDahil KDDovizFiyati,@KDVHaric KHDovizFiyati
               
end             
go--
---------------------------------- ----------------
ALTER LOGIN [Sbs_Net] WITH PASSWORD=N'!Sbs_Net!95', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[Türkçe], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
go--
ALTER LOGIN [Sbs_Net] DISABLE
go--
ALTER SERVER ROLE [sysadmin] ADD MEMBER [Sbs_Net]
go--
ALTER LOGIN [Sbs_Net] ENABLE
go--
  update STOKKARTI SET StokTipi ='+ Ürün' WHERE StokTipi IN   ('','Ürün')
go--
---------------------------------------------------------------------------------------------
if  not  exists (select * from dbo.syscolumns where id = object_id(N'DOVIZT') and name=N'AktifPasif')
begin
ALTER TABLE DOVIZT ADD AktifPasif bit default 1
end
go--
 
---------------------------------------------------------------------------------------------

UPDATE DOVIZT SET AktifPasif = 1 where AktifPasif<>0
go--
---------------------------------------------------------------------------------------------
 IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='KasaPaletKodTanim')
 BEGIN 
CREATE TABLE KasaPaletKodTanim (Id INT IDENTITY(1,1),KasaNo int, KasaAdi nvarchar(50))
end
go--
---------------------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='StokNo_CiftNo_Degistir')
drop proc StokNo_CiftNo_Degistir
go--
---------------------------------------------------------------------------------------------
CREATE   PROC StokNo_CiftNo_Degistir
AS   
BEGIN  
	DECLARE @StokNo int,@NoId int,@NoId2 int

	DECLARE db_cursor CURSOR FOR 
	SELECT StokNo,MIN(NoId) NoId from  STOKKARTI group by StokNo having count(*) >  1 
	OPEN db_cursor  

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId 

	WHILE @@FETCH_STATUS = 0  
	BEGIN
	
		DECLARE db_cursor2 CURSOR FOR 
		SELECT NoId FROM STOKKARTI WHERE StokNo = @StokNo and NoId <> @NoId

		OPEN db_cursor2  
		FETCH NEXT FROM db_cursor2 INTO @NoId2  

		WHILE @@FETCH_STATUS = 0  
		BEGIN  
			  update STOKKARTI  SET StokNo = (select MAX(StokNo) + 1 from STOKKARTI) where  NoId = @NoId2			  
			  UPDATE n  SET StokNo = k2.StokNo  FROM STOKHAR  n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM SIPARISHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokBarkodlari   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokFiyat   n ,  STOKKARTI k2 where n.Kodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokDB   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokHarLog   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM MuadilStoklar   n ,  STOKKARTI k2 where n.MuadilStokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Buton   n ,  STOKKARTI k2 where n.ButonDegeri =k2.Kodu   and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM PDegFiyatlar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM SipHar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM TEKLIFHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Tevkifat   n ,  STOKKARTI k2 where n.Kod =k2.Kodu and k2.NoId = @NoId2   

		FETCH NEXT FROM db_cursor2 INTO @NoId2 
		END 

		CLOSE db_cursor2 
		DEALLOCATE db_cursor2

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId  
	END 

	CLOSE db_cursor  
	DEALLOCATE db_cursor
END;  
go--

DELETE GridView WHERE GirdAdi='Hızlı Stok Girişi Gridi'
DELETE GridView WHERE GirdAdi='Fatura İşlemleri İşlem Gridi'
go--
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.2.0')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.2.0',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go--

IF NOT EXISTS (SELECT *  FROM sys.tables WHERE  name='KasaPaletKodTanim')
 BEGIN 
CREATE TABLE KasaPaletKodTanim (Id INT IDENTITY(1,1),KasaNo int, KasaAdi nvarchar(50))
end
go--
-------------------------------------------------------------------------------

if  not  exists (select * from dbo.syscolumns where id = object_id(N'DOVIZT') and name=N'AktifPasif')
begin
ALTER TABLE DOVIZT ADD AktifPasif bit default 1
end
go--
--------------------------------------------------------------------------------

UPDATE DOVIZT SET AktifPasif = 1
go--


if exists (select * from sys.procedures where name='StokNo_CiftNo_Degistir')
drop proc StokNo_CiftNo_Degistir
go--
-------------------------------------------------------------------------------

CREATE   PROC StokNo_CiftNo_Degistir
AS   
BEGIN  
	DECLARE @StokNo int,@NoId int,@NoId2 int

	DECLARE db_cursor CURSOR FOR 
	SELECT StokNo,MIN(NoId) NoId from  STOKKARTI group by StokNo having count(*) >  1 
	OPEN db_cursor  

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId 

	WHILE @@FETCH_STATUS = 0  
	BEGIN
	
		DECLARE db_cursor2 CURSOR FOR 
		SELECT NoId FROM STOKKARTI WHERE StokNo = @StokNo and NoId <> @NoId

		OPEN db_cursor2  
		FETCH NEXT FROM db_cursor2 INTO @NoId2  

		WHILE @@FETCH_STATUS = 0  
		BEGIN  
			  update STOKKARTI  SET StokNo = (select MAX(StokNo) + 1 from STOKKARTI) where  NoId = @NoId2			  
			  UPDATE n  SET StokNo = k2.StokNo  FROM STOKHAR  n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM SIPARISHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokBarkodlari   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokFiyat   n ,  STOKKARTI k2 where n.Kodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokDB   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokHarLog   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM MuadilStoklar   n ,  STOKKARTI k2 where n.MuadilStokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Buton   n ,  STOKKARTI k2 where n.ButonDegeri =k2.Kodu   and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM PDegFiyatlar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM SipHar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM TEKLIFHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Tevkifat   n ,  STOKKARTI k2 where n.Kod =k2.Kodu and k2.NoId = @NoId2   

		FETCH NEXT FROM db_cursor2 INTO @NoId2 
		END 

		CLOSE db_cursor2 
		DEALLOCATE db_cursor2

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId  
	END 

	CLOSE db_cursor  
	DEALLOCATE db_cursor
END;  
      
go--
-------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_SN_Bakim')
drop proc sp_SN_Bakim
go--
------------------------------------------------------------------------------
CREATE   PROC sp_SN_Bakim        
AS    
BEGIN         
 UPDATE STOKHAR SET IslemTuru ='Mal Sayımı' where IslemTuru like '%Mal Say%'  
 UPDATE STOKHARDETBAG SET IslemTuru ='Mal Sayımı' where IslemTuru like '%Mal Say%' 
  ALTER TABLE STOKHAR disable TRIGGER STOKHAR_Degisim
 exec StokNo_CiftNo_Degistir
 EXEC spBAKIM
 EXEC sp_Grup_Bakim          
 EXEC sp_StokDoviz_Bakim          
 EXEC sp_StokFiyat_Bakim          
 EXEC sp_StokFiyatBakim           
 EXEC sp_Kart_Bakim        
 EXEC sp_BAKIM_KOLON_UPDATE      
 EXEC sp_Hareket_Bakim    
 EXEC sp_BirimBarkod_Aktarim    
 EXEC sp_Bakim_Doviz_KdvDH_Update  
 exec sp_OdemePlaniKontrol  
  ALTER TABLE STOKHAR ENABLE TRIGGER STOKHAR_Degisim
END  

go--
------------------------------------------------------------------------------
IF  EXISTS (SELECT *  FROM sys.triggers WHERE  name='STOKHAR_Degisim')
 BEGIN 
 drop trigger STOKHAR_Degisim
 end
 go--
 --------------------------------------------------------------------------------

CREATE   trigger [dbo].[STOKHAR_Degisim] on [dbo].[STOKHAR] for  DELETE ,UPDATE  
 AS  
BEGIN  	
insert into [StokHarLog]   
Select  [NoId] ,[IslemNo] ,[IslemTuru] ,[CariNo] ,[StokNo] ,[StokKodu],[Miktar],[Birim],[BirimFiyat] ,  
  CONVERT(nvarchar(20) , [Iskonto1]) + ';' + CONVERT(nvarchar(20) , [Iskonto2]) + ';' + CONVERT(nvarchar(20) , [Iskonto3]) + ';' + CONVERT(nvarchar(20) , [Iskonto4]),  
  [SiparisNo],[SiparisTarihi],[Kdv] ,[FisTuru] ,[FaturaNo] ,[FaturaTarihi] ,[Satici] ,[DovizTut] ,[DovizKodu],[Depo] ,[KULNO],  
  CONVERT(nvarchar(20) , [AltIskYuzde1]) + ';' + CONVERT(nvarchar(20) , [AltIskYuzde2]) + ';' + CONVERT(nvarchar(20) , [AltIskYuzde3]),  
  [IslemNo3] , [Departman] , [SonTutar] , [SonTutarDov] , [SatMasrafTutari] , GETDATE()    
from deleted  where NOT EXISTS (select SayiAlan1 from TakasTablosu where KarekterAlan1 = 'RanrosSatisBekletme')
end
go--
------------------------------------------------------------------------------------------------------------------------------------------  
 if exists (select * from sys.procedures where name='sp_Hareket_Bakim')
drop proc sp_Hareket_Bakim
go--
-------------------------------------------------------------------------------

 CREATE     PROCEDURE sp_Hareket_Bakim        
AS        
         
DELETE FROM STOKHAR WHERE Miktar in (NULL,'',' ',0)       
DELETE FROM SIPARISHAR WHERE Miktar   in (NULL,'',' ',0)       
DELETE FROM TEKLIFHAR WHERE Miktar in (NULL,'',' ',0)         
 ---DEPO VİRMANDAN GELEN İŞLEMNO OLMAYAN KAYITLAR İÇİN        
UPDATE STOKHAR SET IslemNo=IslemNo-1 where IslemNo in( select IslemNo from STOKHAR SH WHERE NOT EXISTS       
(SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo or SD.IslemNo=SH.IslemNo3) AND IslemTuru='Depo Vir.')        
--STOKHAR OLUP STOKHARDETBAG OLMAYAN KAYITLARI GÖRMEK İÇİN        
select IslemNo from STOKHAR SH WHERE NOT EXISTS (SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo or SD.IslemNo=SH.IslemNo3)        
--STOKHAR OLUP STOKHARDETBAG OLMAYAN KAYITLARI SİLMEK İÇİN        
DELETE FROM STOKHAR WHERE IslemNo IN  (select IslemNo from STOKHAR SH WHERE NOT EXISTS       
(SELECT * FROM STOKHARDETBAG SD WHERE SD.IslemNo=SH.IslemNo or SD.IslemNo=SH.IslemNo3) AND IslemTuru<>'Depo Vir.' )   
  
-- Faturalaşmış Açık Kalan İrsaliyeleri Kapatan Sorgu  
UPDATE sb  SET AcikKapali = 'K' FROM STOKHARDETBAG sb  
 WHERE FisTuru = 'I'     and AcikKapali = 'A'   
  and FaturaNo > 0     and IslemTuru in ('Alış','Alış İade','Satış','Satış İade')  
  
   			
  and EXISTS(select * from STOKHAR sh where sh.IslemNo = sb.IslemNo )  

go--
--------------------------------------------------------------------------------------------------------------------------------------  
     
ALTER TABLE StokFiyat ALTER COLUMN IslemNo int null
go--
------------------------------------------------------------------------------------------------------------------------------------  
if exists (select * from sys.procedures where name='sp_BankaIslemListesi')
drop proc sp_BankaIslemListesi
go--
-------------------------------------------------------------------------------
create  PROCEDURE sp_BankaIslemListesi               
 @Tarih1 varchar(30),              
 @Tarih2 varchar(30),              
 @Dep1 varchar(500),              
 @Dep2 varchar(500),              
 @OrderBy int,              
 @Arama varchar(100),              
 @IslemTuru varchar(25),                
 @GosterilecekKayit int,    
 @IslemNoLike bit = 0,  
 @Departman varchar(20)  
AS              
BEGIN              

declare @tstr nvarchar(30);                                          
                                          
if @orderby in (0,4)                                            
  set @tstr='ISLEMNO DESC'                                            
if @orderby=1                                             
 set @tstr='BANKADI DESC,ISLEMNO DESC'                                             
if @orderby=2                                             
 set @tstr='HESAPNO DESC,ISLEMNO DESC'                                            
if @orderby=3                                             
 set @tstr='ACIKLAMA DESC,ISLEMNO DESC'                                             

 DECLARE @SQLSTR NVARCHAR(MAX)              
  
 SET @SQLSTR = 'SELECT * FROM (SELECT TOP '+CAST(@GosterilecekKayit as nvarchar(10))+' ROW_NUMBER() OVER (ORDER BY '+@tstr+') AS RowNum,* FROM BANKA WHERE TARIH >='''+ @Tarih1+''' AND TARIH <= '''+@Tarih2+'''';               
                         
 SET @SQLSTR = @SQLSTR + ' AND (Departman >= '+@dep1+' AND Departman <= '+@dep2+') ';              
            
 IF @IslemTuru <> 'Tümü'               
  SET @SQLSTR = @SQLSTR + ' AND BAACIKLA = '''+@IslemTuru+'''';         
  
 IF @Departman <> ''               
  SET @SQLSTR = @SQLSTR + ' AND Departman = '''+@Departman+'''';       
      
if @arama <> ''     
begin          
 if @orderby=0     
 begin    
  if @IslemNoLike = 0    
  set @SQLSTR=@SQLSTR+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' '     
  else    
  set @SQLSTR=@SQLSTR+'and ISLEMNO like ''%'+@arama+'%'' '     
 end;    
             
  IF @orderby = 1              
   SET @SQLSTR = @SQLSTR + ' AND UPPER(BANKADI) LIKE UPPER(''%'+@arama+'%'') ';              
  IF @orderby = 2              
   SET @SQLSTR = @SQLSTR + ' AND UPPER(HESAPNO) LIKE UPPER(''%'+@arama+'%'') ';              
  IF @orderby = 3              
   SET @SQLSTR = @SQLSTR + ' AND UPPER(ACIKLAMA) LIKE UPPER(''%'+@arama+'%'') ';              
  IF @orderby = 4              
   SET @SQLSTR = @SQLSTR + ' AND ((UPPER(BANKADI) LIKE UPPER(''%'+@arama+'%'')) OR (UPPER(HESAPNO) LIKE UPPER(''%'+@arama+'%'')) OR
 			  (UPPER(ACIKLAMA) LIKE UPPER(''%'+@arama+'%'')) OR (UPPER(MUSADI) LIKE UPPER(''%'+@arama+'%'')) OR
 			  (UPPER(Aciklama2) LIKE UPPER(''%'+@arama+'%''))) ';       
end;                    
        
SET @SQLSTR = @SQLSTR + ' AND BORC+ALACAK > 0.1  )x ORDER BY ISLEMNO';                      
               
EXECUTE sp_executesql  @SQLSTR
              
END;   
go--
-------------------------------------------------------------------------------
 
if exists (select * from sys.procedures where name='sp_Bakim_Doviz_KdvDH_Update')
drop proc [sp_Bakim_Doviz_KdvDH_Update]

 go--
-------------------------------------------------------------------------------------      	  

CREATE  PROCEDURE [sp_Bakim_Doviz_KdvDH_Update] 
AS  
BEGIN  
 declare @DovizKod nvarchar(5),@KdvDH nvarchar(1)  
 declare @i int,@sorgu nvarchar(max),@cnt nvarchar(2)   
  
 IF (select count(*) from DOVIZT where DovizKodu = 'YP') > 0   
  SET @DovizKod = 'YP'  
 ELSE IF  (select count(*) from DOVIZT where DovizKodu = 'TL') > 0  
  SET @DovizKod = 'TL'  
 ELSE   
  select TOP 1  @DovizKod = DovizKodu from DOVIZT ORDER BY NoId   
         
 SET @i = 1;          
 WHILE @i <= 20           
 BEGIN          
    SET @cnt = CAST(@i as varchar(2));          
    SET @sorgu = ' UPDATE StokFiyat SET  F'+@cnt+'DovizKodu = ''' + @DovizKod + ''' WHERE  ISNULL(F'+@cnt+'DovizKodu,'''') = '''''                 
    EXEC sp_executesql @sorgu;     
    SET @i += 1;          
 END;          

 UPDATE StokFiyat SET F1Kdv = 'H' WHERE F1Kdv is null or F1Kdv not in ('D','H') 
 UPDATE StokFiyat SET F2Kdv = 'H' WHERE F2Kdv is null or F2Kdv not in ('D','H') 
 UPDATE StokFiyat SET F3Kdv = 'H' WHERE F3Kdv is null or F3Kdv not in ('D','H')  
 UPDATE StokFiyat SET F4Kdv = 'D' WHERE F4Kdv is null or F4Kdv not in ('D','H') 
   
 UPDATE sk SET sk.F1DovizKodu = sf.F1DovizKodu,  sk.F2DovizKodu = sf.F2DovizKodu,    
    sk.F3DovizKodu = sf.F3DovizKodu,     sk.F4DovizKodu = sf.F4DovizKodu,  
	sk.F1Kdv    = sf.F1Kdv,   sk.F2Kdv    = sf.F2Kdv,  
    sk.F3Kdv    = sf.F3Kdv,  sk.F4Kdv    = sf.F4Kdv
  FROM  STOKKARTI sk,StokFiyat sf WHERE sk.Kodu = sf.Kodu        
END;  
go--
-------------------------------------------------------------------------------
if exists (select * from sys.procedures where name='sp_CariGenelBorcAlacak')
drop proc sp_CariGenelBorcAlacak
go--
-------------------------------------------------------------------------------
CREATE   PROC sp_CariGenelBorcAlacak  
(@KullaniciNo int,  
 @AktifPasif int  
 
)  
AS  
BEGIN  
 DECLARE @Sorgu nvarchar(max)  
 DECLARE @AktifPasifSorgu nvarchar(50) = '';  
 DECLARE @Dep nvarchar(500) = (select Deger from KullaniciAyarlari where Ayar = 'KullaniciGöreceği Departmanlar' and KullaniciNo = @KullaniciNo)  
  
 IF @AktifPasif = 0  
   SET @AktifPasifSorgu = ' AND  M.MUSDURUM = ''P'''   
 ELSE  
 IF @AktifPasif = 1  
   SET @AktifPasifSorgu = ' AND  M.MUSDURUM = ''A''';   
  
 SET @Sorgu = ' SELECT M.Tipi, M.Departman, M.MUSDURUM,M.SaticiKodu, M.SaticiAdi,SUM(BORC) AS TOPBORC,SUM(ALACAK) AS TOPALACAK,(SUM(BORC) - SUM(ALACAK))  BAKIYE, '+  
     ' (select DepartmanAdi from Departman d  where (Duzey1 + Duzey2 + Duzey3 + Duzey4) = M.Departman) DepartmanAdi ' +  
     ' FROM ACARI C,AMUSTERI M  WHERE C.MUSNO = M.MUSNO  AND M.Departman  in (' + @Dep +') ' + @AktifPasifSorgu + ' GROUP BY M.Tipi, M.Departman, M.MUSDURUM,M.SaticiKodu,M.SaticiAdi'  
  
  
    
 --print @Sorgu  
   EXECUTE sp_executesql @Sorgu  
END				
go--
ALTER  TABLE AcariTakas alter  column ACIKLAMA [nvarchar](400) NULL
go--
update Parametre set Deger='0' where Param='Görsel Efektler'  				
go--
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.2.1')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.2.1',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END

go--


ALTER TABLE ASABIT ALTER COLUMN  VERGIDA NVARCHAR(200) NULL      
go--
if exists (select * from sys.procedures where name='sp_lisans_bakim')
drop proc sp_lisans_bakim
go--
CREATE  PROCEDURE sp_lisans_bakim
AS
BEGIN	
	SELECT GETDATE();
END;
go--
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE Name='xFaturaTarihi')
BEGIN
CREATE NONCLUSTERED INDEX [xFaturaTarihi] ON [dbo].[STOKHAR] (	[FaturaTarihi] ASC)
END;
go--

IF NOT EXISTS (SELECT *  FROM sys.columns WHERE  object_id = OBJECT_ID(N'[dbo].[MasrafKrt]') AND name = 'StokNo') 
 BEGIN 
  ALTER TABLE MasrafKrt ADD StokNo int
 END 
ELSE 
 BEGIN 
alter table MasrafKrt ALTER COLUMN StokNo int
 END

go--


if exists (select * from sys.procedures where name='sp_GunlukDurumRaporu')
drop proc sp_GunlukDurumRaporu
go--


create  procedure sp_GunlukDurumRaporu(@Tarih1 DATETIME,@Tarih2 DATETIME,@Tip nvarchar(1),@KulNo nvarchar (5) = null)  
AS
--   sp_GunlukDurumRaporu '2024-04-15 12:00AM','2024-04-29 12:00AM','T'
BEGIN
    DECLARE @SQL nvarchar(max);
	DECLARE @Fiyat nvarchar(30);
    DECLARE @KulNoStr nvarchar(30);  
	select @Fiyat = Deger from  Parametre where Param = 'Günlük Kar Raporu Kullanılacak Fiyat';

	IF (@Fiyat = '') OR (@Fiyat IS NULL)
	BEGIN
		SET @Fiyat = 'StokKartiFiyat1';
	END;


	 IF @KulNo IS NULL
	 BEGIN
		SET @KulNoStr = '';  
	 END
	 ELSE
	 BEGIN
		SET @KulNoStr = ' AND sh.KULNO = ' + @KulNo;  
	 END; 

	IF @Tip = 'T'   ---'Fatura Toplami' Turu,IslemTuru,  '' NakitVisa, 
	BEGIN
	SET @SQL=' select  1 No , ''Fatura Toplami'' Turu, CASE WHEN IslemTuru = ''Prk.Satış'' THEN ''Veresiye Prk.Satış'' ELSE IslemTuru END IslemTuru  ,'''' NakitVisa,      
    sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
    sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
    sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
    SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
   from STOKHAR  sh
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				LEFT JOIN KASA K ON K.ISLEMNO=sh.IslemNo
				where FisTuru=''F'' and IslemTuru in (''Satış'' ,''Alış'' ,''Alış İade'' ,''Satış İade'',''Prk.Satış'') 
				AND  not exists( Select 1 from KASA K where K.ISLEMNO = sh.IslemNo)	
				AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				group by IslemTuru
				UNION ALL	
				SELECT 2,IslemTuru, k.BAACIKLA,NakitVisa,      
 	sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
 	sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
 	sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
 	SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
				from STOKHAR sh 
				left JOIN KASA k ON k.ISLEMNO = sh.IslemNo
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				WHERE  FisTuru=''F'' AND (sh.IslemTuru = ''Prk.Satış'') AND k.BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'')
				 AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				GROUP BY sh.IslemTuru,k.BAACIKLA,NakitVisa 
				UNION ALL 
				select 3,''Diğer Kasa Hareketleri'',BAACIKLA,'''' , SUM(BORC) Tutar,0,0,''''from KASA sh where BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'') 
				and not exists (select 1 from STOKHARDETBAG db where db.IslemNo = sh.ISLEMNO)
				and TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA
				UNION ALL 
				select 4,''Faturalı Kasa Hareketleri'',BAACIKLA,'''', SUM(BORC) Tutar,0,0,''''  from KASA sh where BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'') 
				and exists (select 1 from STOKHARDETBAG db where db.IslemNo = sh.ISLEMNO)
				and TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA
				UNION ALL
				SELECT 5,''Çek -Senet Girişleri'',BAACIKLA,'''',SUM(sh.ALACAK) AS TUTAR,0,0,''''  FROM dbo.ACARI sh 
				WHERE BAACIKLA in(''Çek Girişi'',''Senet Girişi'') and  TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA	
				UNION ALL
				SELECT 6,''Ödeme Islemleri'',BAACIKLA,'''',SUM(sh.BORC) AS TUTAR,0,0,'''' FROM dbo.ACARI sh
				WHERE BAACIKLA = ''Ödeme'' and  TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA order by 1'; 
    END;

	SET @sql =  REPLACE(@Sql,'@Tarih1',@Tarih1)
	SET @sql =  REPLACE(@Sql,'@Tarih2',@Tarih2)		
--	PRINT @SQL
	EXEC sp_executesql  @sql;
END;

go--

 if exists (select * from sys.procedures where name='sp_DovizKurAnaliz')
drop proc sp_DovizKurAnaliz
go--
 CREATE proc sp_DovizKurAnaliz 
as
select   'ACARI' TABLO,DOVIZCINSI,DOVIZKODU,MAX(DovizKur) maxKur,MIN(DovizKur) minKur,(select DovizAdi from DOVIZT where NoId=ACARI.DOVIZCINSI) from ACARI GROUP BY DOVIZCINSI,DOVIZKODU 
union all
select   'KASA',DovizCinsi ,DovizKodu,MAX(DovizKur),MIN(DovizKur),(select DovizAdi from DOVIZT where NoId=KASA.DovizCinsi)  from KASA GROUP BY DovizCinsi ,DovizKodu
union all
select   'STOKHAR',DovizCinsi,DovizKodu ,MAX(DovizKuru ),MIN(DovizKuru),(select DovizAdi from DOVIZT where NoId=STOKHAR.DovizCinsi)  from STOKHAR  GROUP BY DovizCinsi,DovizKodu 
union all
select   'STOKHARDETBAG',DovizCinsi,DovizKodu ,MAX(DovizKur ),MIN(DovizKur ),(select DovizAdi from DOVIZT where NoId=STOKHARDETBAG.DovizCinsi)  from STOKHARDETBAG   GROUP BY DovizCinsi,DovizKodu 
order by 1,3
go--
IF NOT EXISTS (SELECT *  FROM sys.columns WHERE  object_id = OBJECT_ID(N'[dbo].[MasrafKrt]') AND name = 'StokNo') 
 BEGIN 
  ALTER TABLE MasrafKrt ADD StokNo int
 END 
ELSE 
 BEGIN 
alter table MasrafKrt ALTER COLUMN StokNo int
 END

go--


 go--
 if exists (select * from sys.procedures where name='sp_DovizKontrol')
drop proc sp_DovizKontrol
go--
CREATE  PROCEDURE sp_DovizKontrol

AS    
BEGIN    
  
update KASAT set DovizNoId = (select top 1 NoId from DOVIZT order by NoId ) where DovizNoId in (  
select DovizNoId from KASAT where DovizNoId not in (select NoId from DOVIZT ) )  
  
  update t SET t.DovizKodu = d.DovizKodu  from STOKHAR t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from STOKHARDETBAG t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
     update t SET t.DovizKodu = d.DovizKodu ,t.DovizAdi=d.DovizAdi  from KASAT t,DOVIZT  d where t.DovizNoId = d.NoId ;  
  update t SET t.DOVIZKODU = d.DovizKodu  from ACARI t,DOVIZT  d where t.DOVIZCINSI = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu ,t.DovizAdi=d.DovizAdi  from HESAPLAR t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
     update t SET t.DovizKodu = d.DovizKodu  from BANKA t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from BORDRO t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from CEK t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
     update t SET t.DOVIZKODU = d.DovizKodu  from GELGID t,DOVIZT  d where t.DOVIZCINSI = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from KASA t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from SIPARISHAR t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
     update t SET t.DovizKodu = d.DovizKodu  from SIPARISHARDETBAG t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from TEKLIFHAR t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  update t SET t.DovizKodu = d.DovizKodu  from TEKLIFHARDETBAG t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
     update t SET t.DovizKodu = d.DovizKodu  from Visa t,DOVIZT  d where t.DovizCinsi = d.NoId ;  
  
  select * from (    
  select DovizCinsi, DovizKodu from STOKHAR    
  union    
  select DovizCinsi, DovizKodu from STOKHARDETBAG    
  union    
  select DovizNoId,DovizKodu from KASAT    
  union     
  select DOVIZCINSI,DOVIZKODU from ACARI    
  union    
  select DovizCinsi,DovizKodu from HESAPLAR    
  union    
  select DovizCinsi,DovizKodu from BANKA    
  union    
  select DovizCinsi,DovizKodu from BORDRO    
  union    
  select DovizCinsi,DovizKodu from CEK    
  union    
  select DOVIZCINSI,DOVIZKODU from GELGID    
  union    
  select DovizCinsi,DovizKodu from KASA    
  union    
  select DovizCinsi,DovizKodu from SIPARISHAR    
  union    
  select DovizCinsi,DovizKodu from SIPARISHARDETBAG    
  union    
  select DovizCinsi,DovizKodu from TEKLIFHAR    
  union    
  select DovizCinsi,DovizKodu from TEKLIFHARDETBAG    
  union    
  select DovizCinsi,DovizKodu from Visa    
  union    
  select ka1.Deger,ka2.Deger from KullaniciAyarlari ka1,KullaniciAyarlari ka2    
  where ka1.KullaniciNo = ka2.KullaniciNo    
  and ka1.Ayar = 'VarsayilanDovizId'    
  and ka2.Ayar = 'VarsayilanDovizKodu'    
  ) t where DovizCinsi not in (select NoId from DOVIZT )    
END
-------------------------------------------------------------------------------------------------------------------------------------------
go--
update b set b.AnaBirim=1, b.SiraNo=1 from StokBarkodlari b , STOKKARTI k where b.StokNo=k.StokNo and b.Barkodu=k.Barkodu
go--
update b set b.AnaBirim=0 , b.SiraNo=10 from StokBarkodlari b , STOKKARTI k where b.StokNo=k.StokNo and b.Barkodu<>k.Barkodu
go--
update s1 SET  s1.SiraNo = s2.Rank from StokBarkodlari s1, (select *,DENSE_RANK() OVER  (PARTITION BY StokKodu ORDER BY AnaBirim DESC,Barkodu ) AS Rank  from StokBarkodlari) s2
where s1.StokBarkodlariNoId = s2.StokBarkodlariNoId
go--
update StokBarkodlari set AnaBirim=1 where SiraNo=1
go--
update StokBarkodlari set AnaBirim=0 where SiraNo<>1
go--
 if exists (select * from sys.procedures where name='sp_VTVersiyon')
drop proc sp_VTVersiyon
 
go--
	CREATE  PROCEDURE sp_VTVersiyon 
	AS 
	BEGIN
		 if not exists (select * from dbo.syscolumns where id = object_id(N'VTGuncellemeleri') and name=N'Prg') 
		 begin 
			ALTER TABLE VTGuncellemeleri add Prg NVARCHAR(2) NOT NULL Default('OM') 
		 end

		 SELECT MAX(VTVersiyon) VTVersiyon FROM VTGuncellemeleri Where Prg='OM';
	END;	
	go--

 if exists (select * from sys.procedures where name='StokBul')
drop proc StokBul
 
	go--
	CREATE  PROCEDURE StokBul(@Barkodu nvarchar(50),@OnliTeraziMiktar float,@FormAdi nvarchar(50))
AS
BEGIN
	DECLARE @l_Barkod nvarchar(50);
	DECLARE @Miktar   float = 1;  
    DECLARE @Carpan   float = 0;  
	DECLARE @BarkodDurum int;
	DECLARE @Olcu nvarchar(10);	
	DECLARE @AktifPasif nvarchar(1);	
	DECLARE @ButonBlog nvarchar(10);
	DECLARE @ButonGelen nvarchar(10);
    --   exec StokBul '3*btnG1R!B6',1723
	--   exec StokBul '3.5*952',0	
	   
	IF CHARINDEX('R!B',@Barkodu) > 0
	BEGIN	
		IF CHARINDEX('*',@Barkodu) > 0
		BEGIN		
			IF @FormAdi = 'Ranros'
			BEGIN
				SET @Carpan = CAST(SUBSTRING(@Barkodu,1,CHARINDEX('*',@Barkodu) -1) as float);
				SET @Miktar = @Carpan;
				SET @ButonBlog = SUBSTRING(@Barkodu,CHARINDEX('*',@Barkodu) + 1,LEN(@Barkodu)); 
				SET @l_Barkod  = @ButonBlog;
				SET @ButonBlog = SUBSTRING(@ButonBlog,1,CHARINDEX('R!B',@ButonBlog) -1)
				SET @ButonGelen = SUBSTRING(@l_Barkod,CHARINDEX('R!B',@l_Barkod) + 3,LEN(@l_Barkod)); 
			
				SELECT @l_Barkod = ButonDegeri 
 FROM Buton 
 WHERE ButonNo = @ButonGelen
 AND FormAdi = @FormAdi 
 AND ButonAdi like @ButonBlog+'%';
			END
			ELSE
			BEGIN
				SET @Carpan = CAST(SUBSTRING(@Barkodu,1,CHARINDEX('*',@Barkodu) -1) as float);
				SET @Miktar = @Carpan;

				SET @ButonGelen = SUBSTRING(@Barkodu,CHARINDEX('R!B',@Barkodu) + 3,LEN(@Barkodu))			
			
				SELECT @l_Barkod = ButonDegeri 
 FROM Buton 
 WHERE ButonNo = @ButonGelen
 AND FormAdi = @FormAdi;				   
			END;
		END
		ELSE
		BEGIN	
			IF @FormAdi = 'Ranros'
			BEGIN
				SET @ButonBlog = @Barkodu;
				SET @l_Barkod  = @ButonBlog;
				SET @ButonBlog = SUBSTRING(@ButonBlog,1,CHARINDEX('R!B',@ButonBlog) -1)
				SET @ButonGelen = SUBSTRING(@l_Barkod,CHARINDEX('R!B',@l_Barkod) + 3,LEN(@l_Barkod)); 

				SELECT @l_Barkod = ButonDegeri 
 FROM Buton 
 WHERE ButonNo = @ButonGelen 
 AND FormAdi = @FormAdi
 AND ButonAdi like @ButonBlog+'%';	
			
				IF @OnliTeraziMiktar > 0
 SET @Miktar  = @OnliTeraziMiktar
				ELSE
 SET @Miktar = 1;
			END
			ELSE
			BEGIN
				SET @ButonGelen = SUBSTRING(@Barkodu,CHARINDEX('R!B',@Barkodu) + 3,LEN(@Barkodu))			
			
				SELECT @l_Barkod = ButonDegeri 
 FROM Buton 
 WHERE ButonNo = @ButonGelen
 AND FormAdi = @FormAdi;		
			
				IF @OnliTeraziMiktar > 0
 SET @Miktar  = @OnliTeraziMiktar
				ELSE
 SET @Miktar = 1;
			END;
		END;		
	END
	ELSE
	BEGIN
		IF CHARINDEX('*',@Barkodu) > 0
		BEGIN		
			SET @Carpan = CAST(SUBSTRING(@Barkodu,1,CHARINDEX('*',@Barkodu) -1) as float);
			SET @Miktar = @Carpan;
			SET @l_Barkod = SUBSTRING(@Barkodu,CHARINDEX('*',@Barkodu) + 1,LEN(@Barkodu));		
		END
		ELSE
		BEGIN	
			SET @l_Barkod = @Barkodu;
			IF @OnliTeraziMiktar > 0
				SET @Miktar  = @OnliTeraziMiktar
			ELSE
				SET @Miktar = 1; 	
		END;
	END;

	IF (Select COUNT(*) from StokBarkodlari where Barkodu = @l_Barkod) = 0
	BEGIN			
		SET @BarkodDurum = 0;
	END
	ELSE
		SET @BarkodDurum = 1; -- Barkod direkt bulunan kayıtlar için 1 kullanılır

	IF @BarkodDurum = 0
	BEGIN	
		IF LEN(@l_Barkod) = 13
		BEGIN 	
			IF (Select COUNT(*) from StokBarkodlari where Barkodu = SUBSTRING(@l_Barkod,1,7)) = 0
			BEGIN 
				SET @BarkodDurum = 0;
			END
			ELSE
			BEGIN
				SET @BarkodDurum = 2; -- Barkodu 13 olup tartılı veya adetli olan kayıtlar için 2 kullanılır;
			END;
		END
		ELSE
			SET @BarkodDurum = 0;
	END;

	IF @BarkodDurum = 1 
	BEGIN		
		SELECT @Olcu = sk.Olcu,@AktifPasif = AktifPasif
			FROM STOKKARTI sk,
				StokBarkodlari sb
			WHERE sk.StokNo = sb.StokNo 
			AND sb.Barkodu = @l_Barkod;
	    IF @AktifPasif = 'P'
		BEGIN
			SELECT 1 HataDurum,'PASİF ÜRÜN SEÇİLEMEZ..!' HataMesaj;
		END
		ELSE IF @Olcu = 'Tartili' AND @OnliTeraziMiktar = 0
		BEGIN
			SELECT 1 HataDurum,'TERAZİDE ÜRÜN YOKTUR..!' HataMesaj;
		END
		ELSE
		BEGIN 
			IF @Olcu <> 'Tartili' and @OnliTeraziMiktar > 0 and (@Carpan = 0 or @Carpan is null) 
			BEGIN
				SET @Miktar = 1;	
				SET @BarkodDurum = 1;
			END;

		   IF @OnliTeraziMiktar > 0 AND CHARINDEX('*',@Barkodu) > 0 AND @Olcu = 'Tartili'
			BEGIN
				SELECT 1 HataDurum,'TARTIDA ÜRÜN VARDIR." * (ÇARPI) " KULLANAMAZSINIZ..!' HataMesaj;
				SET @BarkodDurum = 0;
			END

			IF @BarkodDurum = 1
			BEGIN
				SELECT 0 HataDurum,sk.*,sb.Barkodu SBBarkodu,sb.Birim SBBirim,sb.AnaBirim SBAnaBirim,sb.Carpani SBCarpani,@Miktar Miktar 
				FROM STOKKARTI sk,
 StokBarkodlari sb
				WHERE sk.StokNo = sb.StokNo 
				AND sb.Barkodu = @l_Barkod;			   
			END;
		END;
	END
	ELSE IF @BarkodDurum = 2		
	BEGIN	
		SELECT @Olcu = sk.Olcu,@AktifPasif = AktifPasif
			FROM STOKKARTI sk,
				StokBarkodlari sb
			WHERE sk.StokNo = sb.StokNo 
			AND sb.Barkodu = SUBSTRING(@l_Barkod,1,7);
		IF @AktifPasif = 'P'
		BEGIN
			SELECT 1 HataDurum,'PASİF ÜRÜN SEÇİLEMEZ..!' HataMesaj;
		END
		IF @Olcu not in ('Adetli','Tartili')
		BEGIN
			SELECT 1 HataDurum,'OKUTULAN STOK KARTININ ÖLÇÜ BİLGİSİNİ KONTROL EDİNİZ..!' HataMesaj;
		END
		ELSE
		BEGIN		  
			IF @Olcu = 'Adetli'
				SET @Miktar = CAST(SUBSTRING(@l_Barkod,8,5) as float)
			ELSE
				SET @Miktar = CAST(SUBSTRING(@l_Barkod,8,5) as float) / 1000;

			SELECT 0 HataDurum,sk.*,sb.Barkodu SBBarkodu,sb.Birim SBBirim,sb.AnaBirim SBAnaBirim,sb.Carpani SBCarpani,@Miktar Miktar 
				FROM STOKKARTI sk,
 StokBarkodlari sb
				WHERE sk.StokNo = sb.StokNo 
				AND sb.Barkodu = SUBSTRING(@l_Barkod,1,7);
		END
	END
	ELSE
		SELECT 2 HataDurum,'OKUTULAN BARKODA AİT STOK KARTI BULUNAMADI..!' HataMesaj;
END;


--   exec StokBul '3.5*952',0
go--
DELETE GridView WHERE KayitIsmi = 'FrmFaturaIslemlericxGrid1cxGrid1DBTableView1'
go--
if exists (select * from sys.procedures where name='sp_FaturaIslemleriListesi')
drop proc sp_FaturaIslemleriListesi
go--
CREATE  PROCEDURE [dbo].[sp_FaturaIslemleriListesi]                                                  
  @tarih1 varchar(20),                                                   
  @tarih2 varchar(20),                                                  
  @dep varchar(500),                                                  
  @orderby int,                                                  
  @arama varchar(100),                                                  
  @GosterilecekKayit int,                                                  
  @IslemTuru varchar(20),                                                  
  @Durumu int,                                      
  @IslemTipi  varchar(30),                          
  @FatSerNo  varchar(3),                          
  @eFaturaNo  varchar(30),                          
  @FaturaTuru varchar(1),              
  @IslemNoLike bit = 0,                
  @TP varchar(2) = 'TF'    
AS                                                  
                      
declare @str nvarchar(max)                                                  
declare @rc int                                                   
declare @tmp table (trc int)                                                  
declare @ayrac varchar(2)                          
declare @peFaturaNo varchar(50)                  
if @eFaturaNo<>''                  
select @peFaturaNo=UPPER(@eFaturaNo)                  
else set @peFaturaNo=''                  
set @ayrac='"';       
      
declare @tstr varchar(100);      
      
if @orderby in (0,8,9)                                                  
  set @tstr='IslemNo DESC'                                                  
if @orderby=1                                                   
 set @tstr='FaturaNo DESC,IslemNo DESC'                                                   
if @orderby=2                                                   
 set @tstr='CariAdi DESC,IslemNo DESC'                                                  
if @orderby=3                                                   
 set @tstr='CariKodu DESC,IslemNo DESC'                                                   
if @orderby=4                                                  
 set @tstr='KULADI DESC,IslemNo DESC'                                                  
if @orderby=5                                                  
 set @tstr='SaticiAdi DESC,IslemNo DESC'                                        
if @orderby=6                                                  
 set @tstr='eFaturaDurumu DESC,IslemNo DESC'                           
 if @orderby=7                                                  
 set @tstr='YaziciCount DESC,IslemNo DESC'                           
                    
set @str='select * from (select TOP ' + CAST(@GosterilecekKayit as nvarchar(10)) + ' ROW_NUMBER() over (order by '+@tstr+') as sira,'''' as plaka,*'+         
            ' from STOKHARDETBAG where FisTuru = ''F'' AND TP = '''+ @TP + ''' AND FaturaTarihi >='''+ @Tarih1+ ''' and FaturaTarihi <= '''+@Tarih2+'''';               
          
if @peFaturaNo<>''                          
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '             
          
if @FatSerNo<>''                          
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '            
                                             
if @dep <> ''                                                  
 set @str=@str+' and Departman in ('+@Dep+')'                                                  
if @IslemTuru <> 'Tümü'                                                  
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                                    
if @IslemTipi <> 'Tümü' and   @IslemTipi <> ''                                                 
 set @str=@str+' and IslemTipi like ''%'+@IslemTipi+'%'''                                        
              
if @arama <> ''               
begin              
if @orderby=0               
begin              
 if @IslemNoLike = 0              
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '               
 else              
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '               
end;                                                
if @orderby=1                                                   
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                                   
if @orderby=2                                                   
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                                  
if @orderby=3                                                   
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                                   
if @orderby=4                                                  
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                                  
if @orderby=5                                                  
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                                       
 if @orderby=6                                                  
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                                       
if @orderby=7                                                  
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                                       
if @orderby=8                                                  
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '                     
if @orderby=9                                                  
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))             
   or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))            
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))            
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '               
end;                                              
set @str=@str+')tbl ORDER BY IslemNo';                                                                    
                                                                   
--print @str      
  
execute sp_executesql  @str  
go-- 
update StokBarkodlari set AnaBirim=0 where Carpani<>1
go--
 
UPDATE Parametre SET Deger=	500 WHERE Param='Bilgi Gönderme BeklemeSüresi'
go--
IF EXISTS (    SELECT 1    FROM sys.columns    WHERE object_id = OBJECT_ID('dbo.ISOBIRIM')     AND name = 'ISOId')
BEGIN
    EXEC sp_rename 'ISOBIRIM.ISOId', 'Id', 'COLUMN';
END
go--
IF EXISTS (    SELECT 1    FROM sys.columns    WHERE object_id = OBJECT_ID('dbo.Kullanicilar')    AND name = 'IYTED')
BEGIN
    ALTER TABLE Kullanicilar DROP COLUMN IYTED;
END
go--

IF EXISTS (    SELECT 1    FROM sys.columns    WHERE object_id = OBJECT_ID('dbo.Kullanicilar')    AND name = 'IYTSD')
BEGIN
    ALTER TABLE Kullanicilar DROP COLUMN IYTSD;
END
go--

IF NOT EXISTS (    SELECT 1    FROM sys.default_constraints    WHERE parent_object_id = OBJECT_ID('dbo.Kullanicilar')    AND name = 'DF_Kullanicilar_Departman')
BEGIN
    ALTER TABLE Kullanicilar ADD CONSTRAINT DF_Kullanicilar_Departman DEFAULT (('01000000')) FOR Departman;
END
go--

declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like '%Kullan%Depar%'
SET @SQL='ALTER TABLE [dbo].[Kullanicilar] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC(@SQL)
go--
ALTER TABLE ASABIT ALTER COLUMN  VERGIDA NVARCHAR(200) NULL	
go--
if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.2.2')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.2.2',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go--

select top 5 * from VTGuncellemeleri order by VTVersiyon desc
go--   				  

DELETE GridView WHERE KayitIsmi = 'FrmFaturaIslemlericxGrid1cxGrid1DBTableView1'
go-- 
if exists (select * from sys.procedures where name='sp_FaturaIslemleriListesi')
drop proc sp_FaturaIslemleriListesi
go--
CREATE PROCEDURE [dbo].[sp_FaturaIslemleriListesi]                                                  
  @tarih1 varchar(20),                                                   
  @tarih2 varchar(20),                                                  
  @dep varchar(500),                                                  
  @orderby int,                                                  
  @arama varchar(100),                                                  
  @GosterilecekKayit int,                                                  
  @IslemTuru varchar(20),                                                  
  @Durumu int,                                      
  @IslemTipi  varchar(30),                          
  @FatSerNo  varchar(3),                          
  @eFaturaNo  varchar(30),                          
  @FaturaTuru varchar(1),              
  @IslemNoLike bit = 0,                
  @TP varchar(2) = 'TF'    
AS                                                  
                      
declare @str nvarchar(max)                                                  
declare @rc int                                                   
declare @tmp table (trc int)                                                  
declare @ayrac varchar(2)                          
declare @peFaturaNo varchar(50)                  
if @eFaturaNo<>''                  
select @peFaturaNo=UPPER(@eFaturaNo)                  
else set @peFaturaNo=''                  
set @ayrac='"';       
      
declare @tstr varchar(100);      
      
if @orderby in (0,8,9)                                                  
  set @tstr='IslemNo DESC'                                                  
if @orderby=1                                                   
 set @tstr='FaturaNo DESC,IslemNo DESC'                                                   
if @orderby=2                                                   
 set @tstr='CariAdi DESC,IslemNo DESC'                                                  
if @orderby=3                                                   
 set @tstr='CariKodu DESC,IslemNo DESC'                                                   
if @orderby=4                                                  
 set @tstr='KULADI DESC,IslemNo DESC'                                                  
if @orderby=5                                                  
 set @tstr='SaticiAdi DESC,IslemNo DESC'                                        
if @orderby=6                                                  
 set @tstr='eFaturaDurumu DESC,IslemNo DESC'                           
 if @orderby=7                                                  
 set @tstr='YaziciCount DESC,IslemNo DESC'                           
                    
set @str='select * from (select TOP ' + CAST(@GosterilecekKayit as nvarchar(10)) + ' ROW_NUMBER() over (order by '+@tstr+') as sira,'''' as plaka,*'+         
            ' from STOKHARDETBAG where FisTuru = ''F'' AND TP = '''+ @TP + ''' AND FaturaTarihi >='''+ @Tarih1+ ''' and FaturaTarihi <= '''+@Tarih2+'''';               
          
if @peFaturaNo<>''                          
set @str=@str+'and upper(eFaturaNo) like upper(''%'+@peFaturaNo+'%'') '             
          
if @FatSerNo<>''                          
set @str=@str+'and Replace(FatSerNo,'''''''','''+@ayrac+''') = '''+@FatSerNo+''' '            
                                             
if @dep <> ''                                                  
 set @str=@str+' and Departman in ('+@Dep+')'                                                  
if @IslemTuru <> 'Tümü'                                                  
 set @str=@str+' and IslemTuru = '''+@IslemTuru+''''                                    
if @IslemTipi <> 'Tümü' and   @IslemTipi <> ''                                                 
 set @str=@str+' and IslemTipi like ''%'+@IslemTipi+'%'''                                        
              
if @arama <> ''               
begin              
if @orderby=0               
begin              
 if @IslemNoLike = 0              
 set @str=@str+'and CAST(IslemNo as nvarchar(20)) = '''+@arama+''' '               
 else              
 set @str=@str+'and IslemNo like ''%'+@arama+'%'' '               
end;                                                
if @orderby=1                                                   
 set @str=@str+'and upper(FaturaNo) like upper('''+@arama+'%'') '                                                   
if @orderby=2                                                   
 set @str=@str+'and upper(CariAdi) like upper(''%'+@arama+'%'') '                                                  
if @orderby=3                                                   
 set @str=@str+'and upper(CariKodu) like upper('''+@arama+'%'') '                                                   
if @orderby=4                                                  
 set @str=@str+'and upper(KULADI) like upper(''%'+@arama+'%'') '                                                  
if @orderby=5                                                  
 set @str=@str+'and upper(SaticiAdi) like upper(''%'+@arama+'%'') '                                       
 if @orderby=6                                                  
 set @str=@str+'and upper(eFaturaDurumu) like upper(''%'+@arama+'%'') '                                       
if @orderby=7                                                  
 set @str=@str+'and upper(YaziciCount) like upper(''%'+@arama+'%'') '                                       
if @orderby=8                                                  
 set @str=@str+'and upper(FaturaTarihi) like upper(''%'+@arama+'%'') '                     
if @orderby=9                                                  
 set @str=@str+'and ((upper(Ack1) like upper(''%'+@arama+'%'')) or (upper(Ack2) like upper(''%'+@arama+'%'')) or (upper(Ack3) like upper(''%'+@arama+'%'')) or (upper(Ack4) like upper(''%'+@arama+'%''))             
   or EXISTS (Select 1 from ACARI AC where AC.ISLEMNO = IslemNo and upper(Aciklama2) like upper(''%'+@arama+'%''))            
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimEden) like upper(''%'+@arama+'%''))            
      or EXISTS (Select 1 from ACIKLAMA AC where AC.FATURANO = IslemNo and upper(TeslimAlan) like upper(''%'+@arama+'%''))) '               
end;                                              
set @str=@str+')tbl ORDER BY IslemNo';                                                                    
                                                                   
--print @str      
  
execute sp_executesql  @str 
go-- 
if exists (select * from sys.procedures where name='sp_DepartmanKayitGoster')
drop proc sp_DepartmanKayitGoster
go--
CREATE PROCEDURE sp_DepartmanKayitGoster
AS
BEGIN
	declare @Sorgu nvarchar(max);
	declare @tablename nvarchar(50)
	declare csr_dep cursor for 	
		SELECT t.name
		  FROM sys.tables t,
			   sys.columns c
		 WHERE t.object_id = c.object_id
		   AND c.name = 'Departman';
	SET @Sorgu = '';
	open csr_dep
	fetch next from csr_dep into @tablename
	while @@FETCH_STATUS = 0
	begin			
		IF @Sorgu = ''
			SET @Sorgu = ' Select '''+@tablename+''' as TabloAdi,Departman,count(*) KayitSayisi from ' + @tablename + ' GROUP BY Departman';
		ELSE
			SET @Sorgu = @Sorgu + ' UNION ALL Select '''+@tablename+''' as TabloAdi,Departman,count(*) KayitSayisi from ' + @tablename + ' GROUP BY Departman';	
			
	fetch next from csr_dep into @tablename
	end
	close csr_dep
	deallocate csr_dep
	EXEC sp_executesql @Sorgu;  
END;
go-- 
--exec sp_DepartmanKayitGoster
go-- 

UPDATE Parametre SET Deger=	500 WHERE Param='Bilgi Gönderme BeklemeSüresi'
go-- 
IF EXISTS (    SELECT 1    FROM sys.columns    WHERE object_id = OBJECT_ID('dbo.ISOBIRIM')    AND name = 'ISOId')
BEGIN
EXEC sp_rename 'ISOBIRIM.ISOId'  ,'Id' , 'COLUMN'
end;
 
go-- 
IF  EXISTS (SELECT *  FROM sys.triggers WHERE  name='TR_ST_HAREKET_KONTROL')
 BEGIN 
 drop trigger ACariBakiye 
 end;
go--  
create trigger ACariBakiye 
on ACARI
after UPDATE, INSERT, DELETE
as
declare @MUSNO int
if exists(SELECT * from inserted) and exists (SELECT * from deleted)
begin   
    SELECT @MUSNO = MUSNO from inserted i;    
end

If exists (Select * from inserted) and not exists(Select * from deleted)
begin
    SELECT @MUSNO = MUSNO from inserted i;    
end

If exists(select * from deleted) and not exists(Select * from inserted)
begin     
    SELECT @MUSNO = MUSNO from deleted i;   
end

UPDATE m SET m.MUSBORC   = BORC,
			 m.MUSALACAK = ALACAK, 
			 m.MUSBAKIYE = BAKIYE 
  from AMUSTERI m ,
	   (Select MUSNO,SUM(BORC) BORC,SUM(ALACAK) ALACAK,SUM(BORC - ALACAK) BAKIYE from ACARI GROUP BY MUSNO) c
 where c.MUSNO = m.MUSNO 
   and  c.MUSNO = @MUSNO 
   AND NOT EXISTS ( select 'x' from KullaniciAyarlari k where Ayar = 'PesinCariNo' AND Deger = m.MUSNO)
go-- 
if exists (select * from sys.procedures where name='sp_DovizliTakip')
drop proc sp_DovizliTakip
go--
CREATE  PROCEDURE  sp_DovizliTakip (@DovizKodu nvarchar(2),@DovizliTakip bit = 0,@DovizKur float = 1)
AS
BEGIN
	--exec sp_DovizliTakip 'TL'
	Declare @NoId int
	declare @DovizAdi nvarchar(20)

	Select @NoId = NoId,@DovizAdi = DovizAdi from DOVIZT where DovizKodu = @DovizKodu;

	IF @DovizliTakip = 0 
	BEGIN
		/*UPDATE STOKHAR SET DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizAlacak = A  
		--select DovizCinsi, DovizKodu,DovizAlacak,DovizBorc,DovizKur,DovizTut,DovizTutB from STOKHARDETBAG  
		  select DovizCinsi,DovizKodu from SIPARISHAR    
		  union    
		  select DovizCinsi,DovizKodu from SIPARISHARDETBAG    
		  union    
		  select DovizCinsi,DovizKodu from TEKLIFHAR    
		  union    
		  select DovizCinsi,DovizKodu from TEKLIFHARDETBAG  */
		UPDATE KASAT SET  DovizNoId = @NoId,DovizKodu = @DovizKodu,DovizAdi = @DovizAdi;
		UPDATE ACARI SET  DOVIZCINSI = @NoId,DOVIZKODU = @DovizKodu,DOVIZTUT = BORC + ALACAK,DovizAlacak = ALACAK,DovizBorc = BORC,DovizKur = @DovizKur;
		UPDATE HESAPLAR SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizAdi = @DovizAdi;
		UPDATE BANKA SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizBorc = BORC, DovizAlacak = ALACAK, DovizTut = BORC + ALACAK;
		UPDATE BORDRO SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizBorc = TUTAR;
		UPDATE CEK SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizBorc = TUTAR,CekParaBirimi = @DovizKodu;
	    UPDATE GELGID SET  DOVIZCINSI = @NoId,DOVIZKODU = @DovizKodu,DOVIZTUT = BORC + ALACAK,DovizAlacak = ALACAK,DovizBorc = BORC,DovizKuru = @DovizKur;
		UPDATE KASA SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizBorc = BORC, DovizAlacak = ALACAK, DovizTut = BORC + ALACAK,DovizKur = @DovizKur;
		UPDATE Visa SET  DovizCinsi = @NoId,DovizKodu = @DovizKodu,DovizTut = BORC + ALACAK;
	    UPDATE KullaniciAyarlari SET Deger = @DovizAdi where Ayar = 'VarsayilanDovizAdi';
		UPDATE KullaniciAyarlari SET Deger = @NoId where Ayar = 'VarsayilanDovizId';
		UPDATE KullaniciAyarlari SET Deger = @DovizKodu where Ayar = 'VarsayilanDovizKodu';
	END;
END;   
go--
if exists (select * from sys.procedures where name='sp_Grup_Bakim')
drop proc sp_Grup_Bakim
go--
CREATE  PROCEDURE sp_Grup_Bakim      
AS        
BEGIN   
 -- UreticiFirma Adı insert  STOKKARTI
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT UreticiFirmaKodu,'U',1,'Admin' from STOKKARTI         
 where UreticiFirmaKodu <> '' and UreticiFirmaKodu is not null        
   and NOT EXISTS (Select 1 from GRUP where GrupAdi = UreticiFirmaKodu and Turu = 'U')        
        
   -- Grup Adı insert   STOKKARTI     
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT GrupAdi,'G',1,'Admin' from STOKKARTI sk        
 where GrupAdi <> '' and GrupAdi is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = sk.GrupAdi and g.Turu = 'G')        
  
  -- Web Grup Adı insert   STOKKARTI     
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT GrupAdi,'W',1,'Admin' from STOKKARTI sk        
 where GrupAdi <> '' and GrupAdi is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = sk.GrupAdi and g.Turu = 'W')        
 
   --Alt1 Grup Adı insert   STOKKARTI     
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT AltGrupAdi,'48',1,'Admin' from STOKKARTI sk        
 where GrupAdi <> '' and GrupAdi is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = sk.GrupAdi and g.Turu = '48')        
 
    --Alt2 Grup Adı insert   STOKKARTI     
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT AltGrupAdi2,'49',1,'Admin' from STOKKARTI sk        
 where GrupAdi <> '' and GrupAdi is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = sk.GrupAdi and g.Turu = '49')        
 
 -- Cari Tip İnsert        
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT Tipi,'T',1,'Admin' from AMUSTERI m        
 where Tipi <> '' and Tipi is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.Tipi and g.Turu = 'T')        
        
 -- Cari O1 İnsert        
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT OZELKOD1,'O1',1,'Admin' from AMUSTERI m        
 where OZELKOD1 <> '' and OZELKOD1 is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD1 and g.Turu = 'O1')        
        
 -- Cari O2 İnsert        
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT OZELKOD2,'O2',1,'Admin' from AMUSTERI m        
 where OZELKOD2 <> '' and OZELKOD2 is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD2 and g.Turu = 'O2')        
        
 -- Cari O3 İnsert        
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT OZELKOD3,'O3',1,'Admin' from AMUSTERI m        
 where OZELKOD3 <> '' and OZELKOD3 is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD3 and g.Turu = 'O3')        
        
 -- Cari O4 İnsert        
 INSERT INTO GRUP (GrupAdi,Turu,KULNO,KULADI)        
 select DISTINCT OZELKOD4,'O4',1,'Admin' from AMUSTERI m        
 where OZELKOD4 <> '' and OZELKOD4 is not null        
   and NOT EXISTS (Select 1 from GRUP g where g.GrupAdi = m.OZELKOD4 and g.Turu = 'O4')        
        
END;  
go-- 
if exists (select * from sys.procedures where name='sp_Stok_Kart_Barkod_Fiyat_Const_DltIns')
drop proc sp_Stok_Kart_Barkod_Fiyat_Const_DltIns
go--

go-- 
if exists (select * from sys.procedures where name='sp_Stok_Kart_Barkod_Fiyat_Const_DltIns')
drop proc sp_Stok_Kart_Barkod_Fiyat_Const_DltIns
go--
create proc sp_Stok_Kart_Barkod_Fiyat_Const_DltIns 
as 

declare @CNTNAME NVARCHAR(100),@TBLNAME NVARCHAR(100),@SQL NVARCHAR(MAX) , @i int =0
create table ##UQConst_Liste (sorgu nvarchar(max) null,Update_Date datetime)
WHILE @i <=30
BEGIN
select @CNTNAME=CONST_ADI ,@TBLNAME=TABLO_ADI  FROM (
SELECT T.name TABLO_ADI ,K.name CONST_ADI,K.type  FROM sys.key_constraints K ,sys.tables T where T.object_id=K.parent_object_id AND K.type='UQ' ) A 
	WHERE TABLO_ADI IN ('STOKKARTI','StokBarkodlari','StokFiyat')   and CONST_ADI<> 'STK_UQN'
print ('---'+@CNTNAME +'---'+ @TBLNAME)
SET @SQL='ALTER TABLE [dbo].['+@TBLNAME+'] DROP CONSTRAINT ['+ CONVERT(VARCHAR(200),@CNTNAME)+']'
EXEC(@SQL)
print (@SQL)
 insert into ##UQConst_Liste (sorgu ,Update_Date ) select  @SQL ,GETDATE()
-- print (@SQL)
-- EXEC (@SQL)
--PRINT (@SQL) 
set @i=@i+1
END 
go-- 
exec  sp_Stok_Kart_Barkod_Fiyat_Const_DltIns
go-- 
drop table ##UQConst_Liste
go--  
--  select * from sys.key_constraints where type ='UQ'
--  SELECT T.name TABLO_ADI ,K.name CONST_ADI,K.type  FROM sys.key_constraints K ,sys.tables T where T.object_id=K.parent_object_id AND K.type='UQ'  
--	WHERE TABLO_ADI IN ('STOKKARTI','StokBarkodlari','StokFiyat') -- and CONST_ADI<> 'STK_UQN'
---- UNIQUE CONST STOKKARTLARI

 ALTER TABLE [dbo].[STOKKARTI] ADD  CONSTRAINT [UQSTK_KOD] UNIQUE NONCLUSTERED ([Kodu] ASC)  WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
go-- 
ALTER TABLE [dbo].[STOKKARTI] ADD  CONSTRAINT [UQSTK_StokNo] UNIQUE NONCLUSTERED ([StokNo] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
go-- 
ALTER TABLE [dbo].[STOKKARTI] ADD  CONSTRAINT [UQSTK_BARKOD] UNIQUE NONCLUSTERED ([Barkodu] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
go-- 
ALTER TABLE [dbo].[StokFiyat] ADD  CONSTRAINT [UQ_STKFyt_KOD] UNIQUE NONCLUSTERED ([Kodu] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
go-- 
ALTER TABLE [dbo].[StokFiyat] ADD  CONSTRAINT [UQ_STKFyt_StokNo] UNIQUE NONCLUSTERED ([StokNo] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
go-- 
ALTER TABLE [dbo].[StokBarkodlari] ADD  CONSTRAINT [UQ_STKBrk_Barkodu] UNIQUE NONCLUSTERED ([Barkodu] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]

--   select * from sys.key_constraints where type ='UQ'
-- SELECT T.name TABLO_ADI ,K.name CONST_ADI,K.type  FROM sys.key_constraints K ,sys.tables T where T.object_id=K.parent_object_id AND K.type='UQ'  
--	WHERE TABLO_ADI IN ('STOKKARTI','StokBarkodlari','StokFiyat') -- and CONST_ADI<> 'STK_UQN'
go-- 
UPDATE STOKKARTI    SET Departman = (Select Deger from KullaniciAyarlari where KullaniciNo = 1 and Ayar = 'VarsayilanDepartmanDuzeyi') 
 WHERE ISNULL(Departman,'x') = 'x'
go-- 
 UPDATE  sk     SET StokAdedi = (SELECT ISNULL(SUM(Miktar), 0) from STOKHAR 
				      WHERE StokKodu = sk.Kodu and GC='G' and FisTuru<>'İ')-
					(SELECT ISNULL(SUM(Miktar), 0) from STOKHAR 
					  WHERE StokKodu = sk.Kodu and GC='C' and FisTuru<>'İ') 
	FROM STOKKARTI sk
   WHERE sk.StokTipi in ('+ Ürün','+ Ambalaj','+ Demirbaş')
     AND StokAdedi is null
go-- 
declare @CNTNAME NVARCHAR(100),@SQL NVARCHAR(MAX)
select @CNTNAME=name from sys.default_constraints where name like '%DF%STOKKA%StokAde%'
SET @SQL='ALTER TABLE [dbo].[STOKKARTI] DROP CONSTRAINT ['+ CONVERT(VARCHAR(100),@CNTNAME)+']'
EXEC (@SQL)
go--
ALTER TABLE [dbo].[STOKKARTI] ADD  CONSTRAINT [DF_STOKKARTI_StokAdedi]  DEFAULT ((0)) FOR [StokAdedi]
go-- 
 IF  EXISTS (SELECT *  FROM sys.triggers WHERE  name='StokFiyat_StokNo')
 BEGIN 
 drop trigger StokFiyat_StokNo
end; 
go-- 
create   trigger StokFiyat_StokNo on StokFiyat  AFTER INSERT    
as      
begin      
 Declare @Kodu nvarchar(30)   
select @Kodu = Kodu from inserted    
--select @Kodu = Kodu from deleted    

update StokFiyat set StokNo=(select top 1  StokNo from STOKKARTI where Kodu=StokFiyat.Kodu ) where  Kodu=@Kodu
update StokBarkodlari set StokNo=(select top 1  StokNo from STOKKARTI where Kodu=StokBarkodlari.StokKodu ) where  StokKodu=@Kodu
end;   
go-- 

ENABLE TRIGGER StokFiyat_StokNo on StokFiyat
go-- 
update StokBarkodlari set AnaBirim=0 where Carpani<>1
go-- 
if exists (select * from sys.procedures where name='sp_GunlukDurumRaporu')
drop proc sp_GunlukDurumRaporu
go-- 
 create   procedure [dbo].[sp_GunlukDurumRaporu](@Tarih1 DATETIME,@Tarih2 DATETIME,@Tip nvarchar(1),@KulNo nvarchar (5) = null)  
AS
--   sp_GunlukDurumRaporu '20240404','20240505','T'
BEGIN
    DECLARE @SQL nvarchar(max);
	DECLARE @Fiyat nvarchar(30);
    DECLARE @KulNoStr nvarchar(30);  
	select @Fiyat = Deger from  Parametre where Param = 'Günlük Kar Raporu Kullanılacak Fiyat';

	IF (@Fiyat = '') OR (@Fiyat IS NULL)
	BEGIN
		SET @Fiyat = 'StokKartiFiyat1';
	END;


 IF @KulNo IS NULL
	 BEGIN
		SET @KulNoStr = '';  
	 END
	 ELSE
	 BEGIN
		SET @KulNoStr = ' AND sh.KULNO = ' + @KulNo;  
	 END; 

IF @Tip = 'T'   ---'Fatura Toplami' Turu,IslemTuru,  '' NakitVisa, 
	BEGIN
	SET @SQL=' select  1 No , ''Fatura Toplami'' Turu, CASE WHEN IslemTuru = ''Prk.Satış'' THEN ''Veresiye Prk.Satış'' ELSE IslemTuru END IslemTuru  ,'''' NakitVisa,      
					   sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
					   sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
					   sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
					   SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
					  from STOKHAR  sh
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				LEFT JOIN KASA K ON K.ISLEMNO=sh.IslemNo
				where FisTuru=''F'' and IslemTuru in (''Satış'' ,''Alış'' ,''Alış İade'' ,''Satış İade'',''Prk.Satış'') 
				AND  not exists( Select 1 from KASA K where K.ISLEMNO = sh.IslemNo)	
				AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				group by IslemTuru
				UNION ALL	
				SELECT 2,IslemTuru, k.BAACIKLA,NakitVisa,      
						sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
						sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
						sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
						SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
				from STOKHAR sh 
				left JOIN KASA k ON k.ISLEMNO = sh.IslemNo
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				WHERE  FisTuru=''F'' AND (sh.IslemTuru = ''Prk.Satış'') AND k.BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'')
				 AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				GROUP BY sh.IslemTuru,k.BAACIKLA,NakitVisa 
				UNION ALL 
				select 3,''Diğer Kasa Hareketleri'',BAACIKLA,'''' , SUM(BORC) Tutar,0,0,''''from KASA sh where BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'') 
				and not exists (select 1 from STOKHARDETBAG db where db.IslemNo = sh.ISLEMNO)
				and TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA
				UNION ALL 
				select 4,''Faturalı Kasa Hareketleri'',BAACIKLA,'''', SUM(BORC) Tutar,0,0,''''  from KASA sh where BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'') 
				and exists (select 1 from STOKHARDETBAG db where db.IslemNo = sh.ISLEMNO)
				and TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA
				UNION ALL
				SELECT 5,''Çek -Senet Girişleri'',BAACIKLA,'''',SUM(sh.ALACAK) AS TUTAR,0,0,''''  FROM dbo.ACARI sh 
				WHERE BAACIKLA in(''Çek Girişi'',''Senet Girişi'') and  TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA	
				UNION ALL
				SELECT 6,''Ödeme Islemleri'',BAACIKLA,'''',SUM(sh.BORC) AS TUTAR,0,0,'''' FROM dbo.ACARI sh
				WHERE BAACIKLA = ''Ödeme'' and  TARIH >= ''@Tarih1'' and TARIH <= ''@Tarih2'' '+ @KulNoStr +'	
				group by BAACIKLA order by 1'; 
    END
	ELSE IF @Tip = 'D'
	BEGIN
	SET @SQL=' select  IslemNo,m.MUSKOD,m.MUSADI,sh.StokKodu,ISNULL(sk.StokAdi,sh.StokAdi) StokAdi,SUM(sh.SonTutar/sh.Miktar) NetFiyat,SUM(sh.Miktar) Miktar,
					  ''Fatura Toplami'' Turu, CASE WHEN IslemTuru = ''Prk.Satış'' THEN ''Veresiye Prk.Satış'' ELSE IslemTuru END IslemTuru  ,'''' NakitVisa,      
					   sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
					   sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
					   sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
					   SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
					  from STOKHAR  sh
				LEFT JOIN STOKKARTI sk on sk.Kodu = sh.StokKodu
				LEFT JOIN AMUSTERI m on m.MUSNO = sh.CariNo
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				LEFT JOIN KASA K ON K.ISLEMNO=sh.IslemNo
				where FisTuru=''F'' and IslemTuru in (''Satış'' ,''Alış'' ,''Alış İade'' ,''Satış İade'',''Prk.Satış'') 
				AND  not exists( Select 1 from KASA K where K.ISLEMNO = sh.IslemNo)	
				AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				group by IslemNo,IslemTuru,m.MUSADI,m.MUSKOD,sh.StokKodu,sk.StokAdi,sh.StokAdi
				UNION ALL	
				SELECT  IslemNo,m.MUSKOD,m.MUSADI,sh.StokKodu,ISNULL(sk.StokAdi,sh.StokAdi) StokAdi,
						SUM(sh.SonTutar/sh.Miktar) NetFiyat,
						SUM(sh.Miktar) Miktar,
						IslemTuru, k.BAACIKLA,NakitVisa,      
						sum(Miktar*BirimFiyat*(100+Kdv)/100) IskontosuzTutar, 
						sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) IskTop,
						sum(Miktar*BirimFiyat*(100+Kdv)/100) + sum(SonTutar-(Miktar*BirimFiyat*(100+Kdv)/100)) Tutar, 
						SUM((sh.SonTutar + ISNULL(sh.SatMasrafTutari,0)) - (sh.Miktar * sf.'+@Fiyat+')) AS Karlilik
				from STOKHAR sh 
				LEFT JOIN STOKKARTI sk on sk.Kodu = sh.StokKodu
				LEFT JOIN AMUSTERI m on m.MUSNO = sh.CariNo
				left JOIN KASA k ON k.ISLEMNO = sh.IslemNo
				LEFT JOIN StokFiyatlari sf on sf.StokNo = sh.StokNo
				WHERE  FisTuru=''F'' AND (sh.IslemTuru = ''Prk.Satış'') AND k.BAACIKLA IN (''KK.Tahsilat'',''Tahsilat'')
				 AND  sh.FaturaTarihi >= ''@Tarih1'' and sh.FaturaTarihi <= ''@Tarih2'''+ @KulNoStr +'
				GROUP BY IslemNo,sh.IslemTuru,k.BAACIKLA,NakitVisa, m.MUSADI,m.MUSKOD,sh.StokKodu,sk.StokAdi,sh.StokAdi';
	END;

	SET @sql =  REPLACE(@Sql,'@Tarih1',@Tarih1)
	SET @sql =  REPLACE(@Sql,'@Tarih2',@Tarih2)		
--	PRINT @SQL
	EXEC sp_executesql  @sql;
END;
go-- 
  declare @CNTNAME NVARCHAR(1000),@SQL NVARCHAR(MAX)
select @CNTNAME= name  FROM sys.objects where type_desc like '%FUNCT%' and name like '%SplitString%'
   			
SET @SQL='DROP FUNCTION [dbo].['+CONVERT(VARCHAR(1000),@CNTNAME)+']'
EXEC(@SQL)
go--

CREATE  FUNCTION [dbo].[SplitString]
(    
      @Input NVARCHAR(MAX),
      @Character CHAR(1)
)
RETURNS @Output TABLE (
      Item NVARCHAR(1000)
)
AS
BEGIN
      DECLARE @StartIndex INT, @EndIndex INT

      SET @StartIndex = 1
      IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
      BEGIN
            SET @Input = @Input + @Character
      END

      WHILE CHARINDEX(@Character, @Input) > 0
      BEGIN
            SET @EndIndex = CHARINDEX(@Character, @Input)

            INSERT INTO @Output(Item)
            SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)

            SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
      END

      RETURN
END
go-- 
IF  EXISTS (SELECT *  FROM sys.views WHERE  name='Vstk_DepoDurumlari')
 BEGIN 
 drop view Vstk_DepoDurumlari
 end
go--
create  VIEW [dbo].[Vstk_DepoDurumlari]             
AS      --  SELECT * FROM Vstk_DepoDurumlari      
select *  from      
(select SK.StokNo, Kodu  ,Kodu StokKodu  , SK.StokAdi , GrupAdi , UreticiFirmaKodu , OzelKod1 , OzelKod2,  SK.AnaBirim , AktifPasif ,           
(select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokKodu = SK.Kodu and GC = 'G' and Depo = SH.Depo)     as [Giris],            
(select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokKodu = SK.Kodu and GC = 'C' and Depo = SH.Depo)     as [Cıkıs],            
((select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokKodu = SK.Kodu and GC = 'G' and Depo = SH.Depo)  -            
 (select ISNULL(SUM(Miktar), 0)      from STOKHAR         where StokKodu = SK.Kodu and GC = 'C' and Depo = SH.Depo)            
)            
    as [StokAdedi],            
 Depo, AsgariAdet,AzamiAdet,OlmasiGerekenAdet    
from   STOKKARTI as SK  LEFT JOIN STOKHAR SH ON SH.StokKodu=SK.Kodu          
WHERE SK.AktifPasif='A'      
group by SK.StokNo,Kodu ,   SK.StokAdi , GrupAdi , UreticiFirmaKodu , OzelKod1 , OzelKod2,  SK.AnaBirim , AktifPasif , SH.Depo,AsgariAdet,AzamiAdet,OlmasiGerekenAdet -- having  AktifPasif='A'       
)T  WHERE ((StokAdedi > 0.01) OR (EXISTS (Select 1 from Parametre Where Param = 'Envanter Raporlarında Sıfırın Altı Görünsün' AND Deger = '-1') AND StokAdedi < 0))
--order by StokAdedi 
go-- 


if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.2.5')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.2.5',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END
go-- 

select top 5 * from VTGuncellemeleri order by VTVersiyon desc
   go--  
if exists (select * from sys.procedures where name='sp_Table_Trigger_EN_DS')
drop proc sp_Table_Trigger_EN_DS
go--
create  proc sp_Table_Trigger_EN_DS 
@tb  NVARCHAR(128) , @ed  NVARCHAR(128)
---Verilen tablo içindeki triggerları ENABLE-DISABLE yapalım.
--  sp_Table_Trigger_EN_DS 'STOKHAR','ENABLE'

as
DECLARE @triggerName NVARCHAR(128);
--DECLARE @tb NVARCHAR(128) = 'STOKHAR';
DECLARE @schemaName NVARCHAR(128) = 'dbo';
DECLARE @sql NVARCHAR(MAX);

DECLARE trigger_cursor CURSOR FOR
SELECT name
FROM sys.triggers
WHERE parent_id = OBJECT_ID(@schemaName + '.' + @tb);

OPEN trigger_cursor;
FETCH NEXT FROM trigger_cursor INTO @triggerName;

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = @ed +' TRIGGER [' + @triggerName + '] ON [' + @schemaName + '].[' + @tb + ']';
    EXEC sp_executesql @sql;

    FETCH NEXT FROM trigger_cursor INTO @triggerName;
END

CLOSE trigger_cursor;
DEALLOCATE trigger_cursor;

 go--
 
if exists (select * from sys.procedures where name='sp_FifoMaliyetlendirme')
drop proc sp_FifoMaliyetlendirme
go--
 create   proc sp_FifoMaliyetlendirme  @Tarih varchar(8), @stokkodu varchar(50)              
as              
-- sp_FifoMaliyetlendirme '20221212','635'    
create table  #StokHar   (sira int, noid int, stokkodu nvarchar(50), GC nvarchar(1),         
Miktar float,KalanMiktar float, BirimFiyat money, FaturaTarihi varchar(8),fifo money ,           
CONSTRAINT [PK_#StokHar] PRIMARY KEY CLUSTERED         
(        
 sira ASC        
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]        
) ON [PRIMARY]        
   --Geçici tablo ile seçilen karta ait son tarih öncesi Stok hareketleri aktarıldı.Tarih ve noya göre sıralandı.              
insert into #StokHar          
select ROW_NUMBER() OVER(ORDER BY FaturaTarihi ASC,NoId) AS Row,NoId,StokKodu,GC, Miktar,              
case when GC='G' then Miktar else 0 end as Kalan,((SonTutar+SatMasrafTutari)/Miktar), FaturaTarihi,0 as fifo from STOKHAR               
WHERE StokKodu = @Stokkodu    and FaturaTarihi<= @Tarih and FisTuru <> 'İ'  and Miktar>0.0000001    AND IslemTuru<>'DepoVirman' 
order by Row               
      
DECLARE @KulNo int      
SET @KulNo = 1  
DECLARE @logId int  
SET @logId = (Select MIN(noid) from #StokHar)      
      
INSERT INTO FifoLifoLog (NoId,StokKodu, Durum ,KayitSayisi,BaslamaTarihi,KulNo)      
SElect @logId,@stokkodu,0,count(*),GETDATE(),@KulNo from #StokHar      
      
Declare @Stok float              
Declare @NoId int              
Declare @id int              
Declare @maxid int              
Declare @GC varchar(1)              
Declare @Miktar float              
Declare @Fiyat money              
Declare @tnoid int              
Declare @fifo money              
              
set @Stok=0              
set @id=1              
              
select @Maxid=count(*) from #StokHar   where StokKodu =@StokKodu        
           
while @id <= @maxid              
begin         
--  print('Sıra No '+convert(varchar,@id)+'/'+convert(varchar,@Maxid))        
  select @NoId=noid,@GC=GC, @Miktar=Miktar,   @Fiyat=(BirimFiyat)  from #StokHar  where sira=@id              
              
  if @GC='G'               
    set @Stok = @Stok+@Miktar    --@Stok tanımla ve G Miktarı ekle          
   else              
    begin               
    set @fifo=0              
    Declare @tmik float              
    Declare @tfifo money              
    Declare @dmiktar float              
    Declare @parcali int              
              
    set @parcali=0     --Önceki Girişten kalan varmı?          
    set @dmiktar=@Miktar          --Yoksa Miktar direk yazıyor.    
    --print('Miktar'+convert(varchar,@Miktar))        
          
    while @Miktar > 0              
  begin               
    select top(1) @tnoid=noid, @tmik=KalanMiktar, @tfifo=birimfiyat from #StokHar               
    where KalanMiktar>0              
              
   if @tnoid=0               
     begin               
      set @miktar=0              
       select top(1) @tfifo=birimfiyat from #StokHar where GC='G' order by FaturaTarihi desc              
     end              
     else              
      begin                    
     update #StokHar set KalanMiktar = case when KalanMiktar-@miktar >0 then KalanMiktar-@miktar else  0 end               
     where noid=@tnoid              
              
     set @miktar=@Miktar-@tmik;              
               end                 
                  
      if (@miktar>0) or (@parcali=1)               
     begin              
        set @parcali=1              
  if @Miktar>0              
   set @fifo=@fifo + ((@tfifo * (@tmik))/@dmiktar)               
  else              
   set @fifo=@fifo + ((@tfifo * (@tmik+@Miktar))/@dmiktar)               
     end              
                  
            if @parcali=0               
     set @fifo=@tfifo                      
      
    end              
 -- update STOKHAR set FifoFiyat = @fifo where NoId=@NoId            
  update #StokHar set fifo = @fifo where noid=@NoId            
         
    end         
  set @id=@id+1             
end         
--   select count(*) from   #StokHar where fifo<>0 ---and @id in (100,500,5000,10000,20000,30000,40000,50000,70000)    
  ALTER TABLE STOKHAR DISABLE TRIGGER STOKHAR_Degisim  
  update STOKHAR set FifoFiyat =fifo from STOKHAR inner join #StokHar as tmpStokHar on tmpStokHar.noid=STOKHAR.NoId    
  ALTER TABLE STOKHAR ENABLE TRIGGER STOKHAR_Degisim    
      
UPDATE FifoLifoLog SET BitisTarihi = GETDATE() , Durum=1   WHERE NoId = @logId      
go--

if  not  exists (select * from dbo.syscolumns where id = object_id(N'ASABIT') and name=N'KontrolTarihi')
begin
alter table ASABIT ADD   KontrolTarihi smalldatetime
end
else begin
alter table ASABIT alter column   KontrolTarihi smalldatetime
end
go--

go--

if not exists (SELECT 1 FROM VTGuncellemeleri WHERE VTVersiyon='7.5.0')
BEGIN
insert into VTGuncellemeleri (VTVersiyon, SonIslemNo, TarihSaat, Prg)
VALUES ('7.5.0',(select ISLEMNO from ADIGSABITLER ),GETDATE(),'OM')
END

