// Èìïîðò íàêëàäíîé èç ÈÍÏÐÎ (Ïÿòèãîðñê)

var
    Header :TStringList; // ïðåäîïðåäåëåíà ïðèëîæåíèåì
    Data :TStringList; // ïðåäîïðåäåëåíà ïðèëîæåíèåì
    ExportAgent :string; // âñå îïðåäåëåíû ïðèëîæåíèåì
    Filename :string;

    ExportAgentName :string;
    ExportNakladNom :string;
    ExportNakladID_DOC :string;
    ExportNakladData :string;
    ExportNakladSklad :string;
    ExportSkladName :string;
    ExportNakladSrok :string;

var Table :TTable;
    num_doc :string;
    Item :variant;
    i :integer;
    OpenDialog :TOpenDialog;
    ThF :TField;
    str,str1 :string;
    Field :TField;

    function GetItemValue(Item :Variant; Name :String) :string;
    begin
       try
         result := vartostr(Item.attributes.getNamedItem(Name).value);
       except
         result := ''
       end;
    end;

 begin
     if filename = ''
     then begin
       OpenDialog := TOpenDialog.create(nil);
       try
         OpenDialog.Title := 'Óêàæèòå ôàéë';
         OpenDialog.FileName  := AppIniFile.readstring('plugins_inpro','filename','');
         opendialog.initialdir := extractfilepath(opendialog.filename);
         OpenDialog.Filter := 'Ôàéëû-íàêëàäíûå(*.dbf)|*.dbf|Âñå ôàéëû(*.*)|*.*';
         while  OpenDialog.execute do begin
           if not fileexists(OpenDialog.FileName) then CreateHint('Óêàæèòå ïðàâèëüíî ôàéë ')
           else begin
             Filename := OpenDialog.FileName;
             AppIniFile.writestring('plugins_inpro','filename',OpenDialog.filename);
             break;
           end;
         end;
       finally
         OpenDialog.free;
       end;
     end;

     if FileName = '' then exit;

     with TMyWait.create('Èäåò èìïîðò íàêëàäíîé') do
     try

       Table := TTable.Create(nil);
       try

        table.tabletype := ttFoxpro;

        Table.TableName := gettempdir + inttostr(round(random * 100000))+'.dbf';
        filedelete(Table.tablename);
        Table.tabletype := ttFoxpro;
        copyfile(FileName,Table.TableName,False);

        try
          //SetTableLangDriver(Table,'db866ru0');
         CheckDBFCP(Table.TableName,38); //866DOS

          Table.Open;
          Table.First;

          Header.Values[ExportNakladNom] := table.fieldbyname('doc_num').asstring;
          Header.Values[ExportNakladData] := table.fieldbyname('doc_date').asstring;
          Header.Values[ExportNakladSrok] := table.fieldbyname('pay_date').asstring;

          data.Clear;


          data.Add(
            'srok'+#9+
            'sroksertif'+#9+
            'cenanotnds'+#9+
            // 'sertifname'+#9+
            'kol'+#9+
            //'nds'+#9+
            'summands'+#9+
            'name'+#9+'firstcena'+#9+'seria'+#9+'sertif'+#9+
            'producername'+#9+
            //'datesertif'+#9+
            'tamog'+#9+
            'countryname'+#9+
            //'regn'+#9+ //+'cenanotnds'
            'barcode');


          while not Table.eof do begin

            str :=
               Table.FieldByName('godn').AsString+#9+
               Table.FieldByName('sert_godn').AsString+#9+
               Table.FieldByName('cena_firm').AsString+#9+
               //Table.FieldByName('centrsert').AsString+#9+
               Table.FieldByName('kolvo').AsString+#9+
               Table.FieldByName('nds_firm').AsString+#9+
               Table.FieldByName('tovar').AsString+#9+
               Table.FieldByName('cena_maker').asstring+#9+
               Table.FieldByName('seria').AsString+#9+
               Table.FieldByName('sert_num').AsString+#9+
               Table.FieldByName('maker').AsString+#9+
               //Table.FieldByName('docd').AsString+#9+
               Table.FieldByName('gtd_num').AsString+#9+
               Table.FieldByName('gtd_str').AsString+#9+
               //Table.FieldByName('regn').AsString+#9+
               //Table.FieldByName('price').AsString+#9+
               Table.FieldByName('barcode').AsString;

            data.Add(str);
            Table.Next;
          end;
        finally
           filedelete(Table.tablename);
        end;

       finally
          Table.free;
       end;

     finally
       free
     end;

     createhint('Îïåðàöèÿ çàâåðøåíà');
 end.
