// Ñêðèïò èìïîðòà íàêëàäíîé èç Êàòðåí (Ñàðàòîâ)

const UseBarcode = true; // îïðåäåëÿåò, áóäåò ëè èñïîëüçîâàòüñÿ øòðèõêîä, ïîñòàâëÿåìûé ïîñòàâùèêîì


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

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

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

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

    function IFSTR(BoolVal :boolean; const TrueVal :string; const FAlseVal :string = '') :string;
    begin
      if BoolVal then Result := TrueVal
      else Result := FalseVal;
    end;

 begin
     //FileName := '';
     if filename = ''
     then begin
       OpenDialog := TOpenDialog.create(nil);
       try
         OpenDialog.Title := 'Óêàæèòå ôàéë';
         OpenDialog.FileName  := AppIniFile.readstring('plugins_katren','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_katren','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 + extractfilename(FileName);
        filedelete(Table.tablename);
        Table.tabletype := ttfoxpro;
        copyfile(FileName,Table.TableName,False);
        //SetTableLangDriver(Table,'db866ru0');
       // CheckDBFCP(Table.TableName,38); //866DOS

        Table.Open;
        Table.First;

        if table.findfield('tfact') <> nil
        then begin
           Header.Values[ExportNakladNom] := table.fieldbyname('tfact').asstring;
           Header.Values[ExportNakladData] := table.fieldbyname('tdata').asstring;
        end
        else begin
          raiseexception('Òàáëèöà èìååò ôîðìàò, íå ñîãëàñîâàííûé ñ ïîñòàâùèêîì. Îáðàòèòåñü â òåõïîääåðæêó');
        end;

        data.Clear;


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

        FPrice_reg := table.findfield('reestr');

		if UseBarcode then
		  FBarcode := Table.findfield('barcode')
		else
		  FBarcode := nil;


        while not Table.eof do begin

          try
            str1 := table.fieldbyname('docend').asstring;

            (* óñòàðåëî
                formatdatetime('dd.mm.yyyy',
                EncodeDate(
                  strtoint(Copy( Table.FieldByName('docend').AsString,1,4)),
                  strToInt(Copy( Table.FieldByName('docend').AsString,5,2)),
                  strToInt(Copy( Table.FieldByName('docend').AsString,7,2))
                ))  *);


          except
            str1  := '';
          end;

          str :=
             Table.FieldByName('dataendtov').AsString+#9+
             Str1 +#9+
             Table.FieldByName('centrsert').AsString+#9+
             Table.FieldByName('amount').AsString+#9+
             Table.FieldByName('nds').AsString+#9+
             Table.FieldByName('name').AsString+#9+
             Table.FieldByName('price1').asstring+#9+
             Table.FieldByName('seria').AsString+#9+
             Table.FieldByName('numdoc').AsString+#9+
             Table.FieldByName('manufac').AsString+#9+
             Table.FieldByName('docd').AsString+#9+
             Table.FieldByName('gtd').AsString+#9+
             Table.FieldByName('country').AsString+#9+
             Table.FieldByName('regn').AsString+#9;

          if (FBarcode<>nil)
          then Str := Str + FBarcode.asstring;
          Str := Str + #9;

          Str := Str + Table.FieldByName('SUMM').AsString;

          Str := Str + #9;

          if (FPrice_Reg<>nil) and (FPrice_Reg.asFloat <>0)
          then Str := Str +FPrice_Reg.asstring;
          // else îñòàâëÿåì ïóñòûì

          data.Add(str);
          Table.Next;
        end;

       finally
          Table.free;
       end;

     finally
       free
     end;

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