// çäåñü íóæíî âïèñàòü èìÿ è ïóòü ê ðåàëüíîìó ñïðàâî÷íèêó âìåñòî òîãî, ÷òî ñåé÷àñ íàïèñàíî

const XLSFile = 'c:\temp\2\Ôðàãìåíò ñïðàâî÷íèêà òîâàðîâ.xls';




var CDS :TClientDataset;
    str :string;
    i :integer;

    Table,TableGR :TTable;
    Query :Tquery;
begin

  CDS := TClientDataset.Create(nil);
  Table := TTable.create(nil);
  TableGr := TTable.create(nil);
  Query := TQuery.create(nil);
  with TMyWait.create('Èäåò ÷òåíèå ôàéëà xls') do
  try
    if not readxls(CDS,XLSFILE) then exit
    else begin
      table.databasename := 'dbkassa';
      table.tablename := 'tovar.db';

      query.databasename := 'dbkassa';
      Query.sql.text := 'delete from tovar';
      Query.execsql;

      Query.sql.text := 'delete from nn_group';
      Query.execsql;

      tablegr.databasename := 'dbkassa';
      tablegr.tablename := 'nn_group';
      tablegr.open;

      table.open;

      Text := 'Ïåðåíîñ äàííûõ â áàçó';

      progressbarmax := cds.recordcount;

      cds.first;
      while not cds.eof do
      begin

        // Ãðóïïû

        if not tablegr.findkey([cds.fields[1].asstring])
           or (tablegr.fieldbyname('name').asstring <> cds.fields[2].asstring)
        then begin
          if tablegr.fieldbyname('nn').asstring <> cds.fields[1].asstring
          then begin
            tablegr.append;
            tablegr.fieldbyname('nn').asstring := cds.fields[1].asstring;
          end
          else
            tablegr.edit;

          tablegr.fieldbyname('name').asstring := cds.fields[2].asstring;
          tablegr.post;
        end;

        // Ñîáñòâåííî, òîâàðû

        table.append;
        table.fieldbyname('nn').asstring := cds.fields[1].asstring+ cds.fields[0].asstring;
        table.fieldbyname('maxprocent').asstring := cds.fields[3].asstring;
        table.fieldbyname('name').asstring := cds.fields[4].asstring;
        table.fieldbyname('ed').asstring := cds.fields[5].asstring;
        table.fieldbyname('smallname').asstring := cds.fields[6].asstring;
        table.post;

        incprogress;
        cds.next;
      end;


    end


  finally
    free;
    Query.free;
    TableGR.free;
    table.free;
    CDS.FREE
  end;
end.


