$1   PROCEDURE [dbo].[FastSell_v2_CHAR_EKLE]
@cha_create_user smallint,
@cha_lastup_user smallint,
@cha_firmano int, 
@cha_subeno int, 
@cha_tarihi datetime, 
@cha_tip tinyint, 
@cha_cinsi tinyint, 
@cha_normal_Iade tinyint, 
@cha_evrak_tip int,
@cha_evrakno_seri varchar(20), 
@cha_evrakno_sira int, 
@cha_satir_no int,
@cha_belge_no varchar(20), 
@cha_belge_tarih datetime, 
@cha_cari_cins tinyint, 
@cha_kod varchar(25),
@cha_meblag float, 
@cha_ft_iskonto1 float, 
@cha_ft_iskonto2 float, 
@cha_ft_iskonto3 float, 
@cha_ft_iskonto4 float, 
@cha_ft_iskonto5 float, 
@cha_ft_iskonto6 float, 
@cha_ft_masraf1 float,
@cha_vergi1 float, 
@cha_vergi2 float, 
@cha_vergi3 float, 
@cha_vergi4 float, 
@cha_vergi5 float, 
@cha_vergi6 float, 
@cha_vergi7 float, 
@cha_vergi8 float, 
@cha_vergi9 float, 
@cha_vergi10 float, 
@cha_tpoz tinyint, 
@cha_aciklama varchar(40), 
@cha_srmrkkodu varchar(25)='', 
@cha_aratoplam float, 
@cha_satici_kodu varchar(25), 
@cha_ciro_cari_kodu varchar(25),
@cha_projekodu varchar(25)='',
@fatura_belge_turu int
as 
declare 
@Record_id int ,
@evraktip int,
@ticaretTuru int


DECLARE @cha_vergi11 float,@cha_vergi12 float,@cha_vergi13 float,@cha_vergi14 float,@cha_vergi15 float,@cha_vergi16 float,@cha_vergi17 float,@cha_vergi18 float,@cha_vergi19 float,@cha_vergi20 float,@vergi_toplam float
DECLARE @birim_fiyat float=0

set @evraktip=4
	if @cha_normal_Iade = 1
		begin
			set @evraktip=3
		end

set @ticaretTuru=case when @cha_cinsi=6 then 0 else 1 end

select @cha_vergi1 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira  and sth_vergi_pntr=1
select @cha_vergi2 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=2
select @cha_vergi3 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=3
select @cha_vergi4 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=4
select @cha_vergi5 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=5
select @cha_vergi6 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=6
select @cha_vergi7 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=7
select @cha_vergi8 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=8
select @cha_vergi9 =ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=9
select @cha_vergi10=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=10

select @cha_vergi11=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=11
select @cha_vergi12=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=12
select @cha_vergi13=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=13
select @cha_vergi14=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=14
select @cha_vergi15=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=15
select @cha_vergi16=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=16
select @cha_vergi17=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=17
select @cha_vergi18=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=18
select @cha_vergi19=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=19
select @cha_vergi20=ISNULL(sum(sth_vergi),0) FROM STOK_HAREKETLERI WHERE (sth_evraktip = @evraktip) AND sth_evrakno_seri=@cha_evrakno_seri and sth_evrakno_sira=@cha_evrakno_sira and sth_vergi_pntr=20

--set @vergi_toplam=(@cha_vergi1+@cha_vergi2+@cha_vergi3+@cha_vergi4+@cha_vergi5+@cha_vergi6+@cha_vergi7+@cha_vergi8+@cha_vergi9+@cha_vergi10+@cha_vergi11+@cha_vergi12+@cha_vergi13+@cha_vergi14+@cha_vergi15+@cha_vergi16+@cha_vergi17+@cha_vergi18+@cha_vergi19+@cha_vergi20)


IF NOT EXISTS(SELECT cha_Guid FROM CARI_HESAP_HAREKETLERI WHERE cha_evrakno_seri=@cha_evrakno_seri and cha_evrakno_sira=@cha_evrakno_sira and cha_evrak_tip=@cha_evrak_tip)
BEGIN 

declare @cha_guid varchar(50)
set @cha_guid=NEWID()
INSERT INTO CARI_HESAP_HAREKETLERI
             (cha_Guid,cha_MainProgramNo,cha_VersionNo,cha_MenuNo,cha_MikroSpecial1,cha_MikroSpecial2,cha_MikroSpecial3,cha_ExternalProgramType,cha_ExternalProgramId,cha_Hash, cha_DBCno, cha_SpecRecNo, cha_iptal, cha_fileid, cha_hidden, cha_kilitli, cha_degisti, cha_CheckSum, cha_create_user, cha_create_date, cha_lastup_user, cha_lastup_date, cha_special1, cha_special2, cha_special3, cha_firmano, cha_subeno, cha_tarihi, cha_tip, 
             cha_cinsi, cha_normal_Iade, cha_evrak_tip, cha_satir_no, cha_belge_no, cha_belge_tarih, cha_cari_cins, cha_kod, cha_kasa_hizmet, cha_kasa_hizkod, cha_d_cins, cha_d_kur, cha_altd_kur, cha_grupno, cha_meblag, cha_vade, cha_fis_tarih, cha_fis_sirano, cha_ft_iskonto1, 
             cha_ft_iskonto2, cha_ft_iskonto3, cha_ft_iskonto4, cha_ft_iskonto5, cha_ft_iskonto6, cha_ft_masraf1, cha_ft_masraf2, cha_ft_masraf3, cha_ft_masraf4, cha_vergi1, cha_vergi2, cha_vergi3, cha_vergi4, cha_vergi5, cha_vergi6, cha_vergi7, cha_vergi8, cha_vergi9, cha_vergi10, 
			 cha_vergi11, cha_vergi12, cha_vergi13, cha_vergi14, cha_vergi15, cha_vergi16, cha_vergi17, cha_vergi18, cha_vergi19, cha_vergi20, 
             cha_yuvarlama, cha_tpoz, cha_aciklama, cha_trefno, cha_sntck_poz, cha_karsidcinsi, cha_karsid_kur, cha_karsidgrupno, cha_srmrkkodu, cha_reftarihi, cha_miktari, cha_aratoplam, cha_vergipntr, cha_istisnakodu, cha_stopaj, cha_savsandesfonu, cha_vergisiz_fl, cha_satici_kodu, 
             cha_StFonPntr, cha_pos_hareketi, cha_vardiya_tarihi, cha_vardiya_no, cha_vardiya_evrak_ti, cha_Vade_Farki_Yuz, cha_karsisrmrkkodu, cha_EXIMkodu, cha_ticaret_turu, cha_otvtutari, cha_otvvergisiz_fl, cha_projekodu, cha_yat_tes_kodu, cha_ciro_cari_kodu, cha_oivergisiz_fl, 
             cha_meblag_ana_doviz_icin_gecersiz_fl, cha_meblag_alt_doviz_icin_gecersiz_fl, cha_meblag_orj_doviz_icin_gecersiz_fl, cha_evrakno_seri, cha_evrakno_sira, cha_isk_mas1, cha_isk_mas2, cha_isk_mas3, cha_isk_mas4, cha_isk_mas5, cha_isk_mas6, cha_isk_mas7, cha_isk_mas8, 
             cha_isk_mas9, cha_isk_mas10, cha_sat_iskmas1, cha_sat_iskmas2, cha_sat_iskmas3, cha_sat_iskmas4, cha_sat_iskmas5, cha_sat_iskmas6, cha_sat_iskmas7, cha_sat_iskmas8, cha_sat_iskmas9, cha_sat_iskmas10, cha_avansmak_damgapul, cha_oiv_pntr, cha_oivtutari, 
             cha_oiv_vergi, cha_sip_uid, cha_kirahar_uid, cha_tevkifat_toplam, cha_ebelge_turu, 
			 cha_ilave_edilecek_kdv1, cha_ilave_edilecek_kdv2, cha_ilave_edilecek_kdv3, cha_ilave_edilecek_kdv4, cha_ilave_edilecek_kdv5, cha_ilave_edilecek_kdv6, cha_ilave_edilecek_kdv7, cha_ilave_edilecek_kdv8, cha_ilave_edilecek_kdv9, cha_ilave_edilecek_kdv10,
			 cha_ilave_edilecek_kdv11, cha_ilave_edilecek_kdv12, cha_ilave_edilecek_kdv13, cha_ilave_edilecek_kdv14, cha_ilave_edilecek_kdv15, cha_ilave_edilecek_kdv16, cha_ilave_edilecek_kdv17, cha_ilave_edilecek_kdv18, cha_ilave_edilecek_kdv19, cha_ilave_edilecek_kdv20,
			 cha_e_islem_turu, cha_fatura_belge_turu, cha_diger_belge_adi, cha_uuid, cha_adres_no, cha_vergifon_toplam, cha_ilk_belge_tarihi, cha_ilk_belge_doviz_kuru, cha_HareketGrupKodu1, 
             cha_HareketGrupKodu2, cha_HareketGrupKodu3, cha_ebelgeno_seri, cha_ebelgeno_sira, cha_bsba_e_belge_mi, cha_ebelge_Islemturu,cha_birimfiyat,cha_hubid,cha_hubglbid,cha_eticaret_kanal_kodu,cha_hizli_satis_kasa_no,cha_tevkifat_sifirlandi_fl)
VALUES (@cha_guid,1,'',0, '','','',0,'',0,0, 0, 0, 51, 0, 0, 0, 0,@cha_create_user, GETDATE(),@cha_lastup_user, GETDATE(), '', '', 
             N'FS',@cha_firmano,@cha_subeno,@cha_tarihi,@cha_tip,@cha_cinsi,@cha_normal_Iade,@cha_evrak_tip,@cha_satir_no,@cha_belge_no,@cha_belge_tarih,@cha_cari_cins,@cha_kod, 0, '', 0, 1, 1, 0,@cha_meblag, 0, GETDATE(), 
             0,@cha_ft_iskonto1,@cha_ft_iskonto2,@cha_ft_iskonto3,@cha_ft_iskonto4,@cha_ft_iskonto5,@cha_ft_iskonto6,@cha_ft_masraf1, 0, 0, 0,@cha_vergi1,@cha_vergi2,@cha_vergi3,@cha_vergi4,@cha_vergi5,@cha_vergi6,@cha_vergi7,@cha_vergi8,@cha_vergi9,@cha_vergi10, 
			 @cha_vergi11,@cha_vergi12,@cha_vergi13,@cha_vergi14,@cha_vergi15,@cha_vergi16,@cha_vergi17,@cha_vergi18,@cha_vergi19,@cha_vergi20, 
             0,@cha_tpoz,@cha_aciklama, '', 0, 0, 1, 0,@cha_srmrkkodu, GETDATE(), 0,@cha_aratoplam, 0, 0, 0, 0, 0,@cha_satici_kodu, 0, 0, GETDATE(), 0, 0, 0, '', '',@ticaretTuru, 0, 0,@cha_projekodu, '',@cha_ciro_cari_kodu, 0, 0, 0, 0,@cha_evrakno_seri,@cha_evrakno_sira, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,@fatura_belge_turu, N'', N'', 1, 0, 0, 0, N'', N'', N'', '', 0, 0, 0,@birim_fiyat,'','','',0,0)

IF(@cha_evrak_tip=0 OR @cha_evrak_tip=63)
BEGIN
	UPDATE STOK_HAREKETLERI  
			SET   sth_fat_uid=@cha_guid 
			WHERE    (sth_evrakno_seri = @cha_evrakno_seri) AND (sth_evrakno_sira = @cha_evrakno_sira)
			AND ((@cha_evrak_tip=0 AND sth_evraktip=3) OR (@cha_evrak_tip=63 AND sth_evraktip=4))
			AND ((@cha_cinsi=6 AND sth_cins=0) OR (@cha_cinsi=7 AND sth_cins=1))
END
END