﻿Const
  FFileName = 'c:\CARİ.xlsx';
  BlockSize = 16;
  
Var
  ADOConn: TADOCOnnection;
  FSQLS: TStringList;
  
procedure Start;
Begin
  AddLog('Bağlantı Oluşturuluyor...');
  ADOConn:= TADOCOnnection.Create(nil);
  ADOConn.LoginPrompt:= False;
  ADOCOnn.ConnectionString:= ConnectionString;
  ADOCOnn.Connected:= True;
  
  FSQLS:= TStringList.Create;  
End;

Procedure Finish;
Begin
  AddLog('Bağlantı Kesiliyor...');
  ADOCOnn.Connected:= False;
  ADOConn.Free;
  
  FSQLS.Free;
  AddLog('Tamamlandı...');
End;

Function CreateQuery: TADOQuery;
Begin
  Result:= TADOQuery.Create(ADOCOnn);
  Result.Connection:= ADOConn;
End;

procedure Execute;
Var
  Ex: TPXExcelIO;
  O, I, Y: Integer;
  FK, FA, FU, Adr, Il, Ilce, Gr, YT, T1, T2, CT, VD, VN, Bk, OK1: String;  
  Bakiye: Single;
Begin
  Ex:= TPXExcelIO.Create;
  
  Ex.Open(FFileName);
  
  Y:= 2;
  O:= 0;
  AddLog('Cariler Okunuyor...');
  While True Do
  Begin
    FK:= Ex.GetText(1, Y);
	AddLog('Firma Adı: ' + FK);
	if FK = '' Then Break;
	
	if Length(FK) > 50 Then
	  FK:= Copy(FK, 1, 50);
		
	FU   := Ex.GetText( 2, Y);
	FA   := Ex.GetText( 3, Y);
	Adr  := Ex.GetText( 4, Y);
	Il   := Ex.GetText( 5, Y);
	Ilce := Ex.GetText( 6, Y);  
	T2   := Ex.GetText( 7, Y); // Tel. 1
	T1   := Ex.GetText( 8, Y); // Tel. 1
	
	VD   := Ex.GetText( 9, Y); // Ex.GetText(10, Y); // V. Dairesi
	VN   := Ex.GetText(10, Y); // Ex.GetText(11, Y); // Vergi Nosu
	
	OK1  := Ex.GetText(11, Y); // ÖZel Kod

	CT   := ''; // Ex.GetText( 9, Y); // Cep Tel
	Gr   := ''; // Grup Adı
	YT   := ''; // Ex.GetText( 7, Y); // Yetkili
		
	Bk   := '0'; // Ex.GetText( 7, Y);
	if Bk = '' Then Bk:= '0';
	Bakiye:= StrToFloat(Bk);
	
	AddLog('Firma Adı: ' + FA);
	
    FSQLS.Add('INSERT INTO TBLCARI( ' +
      ' USTTEMSILCI,SORUMLU,POZISYON,BANKA,SUBE,SUBEKODU,HESAPNO,IBANNO, ' +
      ' GRUPADI,ANAHESAP,ISLEMUYARISI,UYARIACIKLAMA,BARKOD,SEHIR,ILCE, ' +
      ' SATISYAPILMASIN,REFERANS,KEFIL,REFERANSTEL,KEFILTEL,RESIM, ' +
      ' FIRMATIPI,KAYITTARIHI,DELETED,TLBAKIYE,KARTNO,BAKIYE, ' +
      ' FIYATGRUBU, FIRMAKODU,FIRMAADI,FIRMAUNVANI,YETKILI, ' +
      ' VERGIDAIRESI,VERGINO,KOD1,KOD2,KOD3,KOD4,KOD5,ISTIHBARAT,EMAIL,URL, ' +
      ' TELEFON1,TELEFON2,TELEFON3,FAKS,ADRES,RISKLIMITI,DOVIZID,SATISISKONTO, ' +
      ' VADEFARKI,OPSIYONGUN) ');
	FSQLS.Add('SELECT ' +
      ' 0,0,0,'''','''','''','''','''', ' +
      QuotedStr(Gr) + ', 0,'''','''','''',' + QuotedStr(Il) + ',' + QuotedStr(Ilce) + ',' +
      ' 0,0,0,'''','''',NULL, ' +
      ' 2, GETDATE(), 0,0, ISNULL((Select MAX(KARTNO) FROM TBLCARI), 0) + 1, 0, ' + 
	  '0, ' + QuotedStr(FK) + ',' + QuotedStr(FA) + ', ' + QuotedStr(FU) + ', ' + QuotedStr(YT) + ',' +
	  QuotedStr(VD) + ',' + QuotedStr(VN) + ', ' + QuotedStr(OK1) + ','''','''','''','''','''','''','''',' +
	  QuotedStr(T1) + ',' + QuotedStr(T2) + ', '''','''',' + QuotedStr(Adr) + ', 0, 0, '''',' + 
	  '0, 0');

	// if Bakiye > 0 Then
	//  FSQLS.Add('');
	  
	O:= O + 1;
	Begin
	  // FSQLS.SaveToFile('D:\Cari.SQL');
	  
	  ExecSQL(FSQLS.Text);
	  FSQLS.Clear;
	  O:= 0;
	End;
	  
	
	Y:= Y + 1;
  End;
  
  if O > 0 Then
    ExecSQL(FSQLS.Text);
End;

Begin
End.