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

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;
    EXPORTNAKLADSROK :string;

var Table :TTable;
    num_doc :string;
    Item :variant;
    i,k :integer;
    OpenDialog :TOpenDialog;
    ThF :TField;
    str,str1 :string;
    Field :TField;
    FPrice_Reg : Tfield;
    FBarcode :TField;
    FMNN :TField = nil;
    FAdvPriceIn1 :TField;
    FAdvPricesIn  :TField;

    List :TStringList;
    List1 :TSTringList;
    List2 :Tstringlist;
    tmpdir :string = '';

    cds :tclientdataset;

    function StrToDTStr( str :string) :string;
    begin
      if length(str)<>8
      then begin
        result := str;
      end
      else begin
        result := copy(str,7,2)+'.'+copy(str,5,2)+'.'+ copy(str,1,4);
      end;
    end;

    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;

    procedure AddStr(var Destination :string; FieldNameList :tstringLIst; const FieldName :string; FieldValueList :TStringlist);
    var j :integer;
    begin
      j := FieldNameList.indexof(FieldName);
      //createhint('i='+inttostr(i)+',Name =' +FieldName+',NC ='+inttostr(FieldNamelist.count)+', VC='+inttostr(fieldvaluelist.count));
      if j>=0
      then Destination := Destination + FieldValueList[j];
      Destination := Destination + #9;
    end;

    function GetFieldDelta :string;
    begin
     result := '';
     if table.findfield('delta')=nil then exit;

     if (Table.FieldByName('delta').AsString<>'0')
        and (Table.FieldByName('delta').AsString<>'')
     then
       result :=  Table.FieldByName('delta').AsString;

    end;



    procedure AddStr1(var Destination :string; const thField :tField);
    begin
      if thField<>nil
      then Destination := Destination + thField.asstring;
      Destination := Destination + #9;
    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,*.txt,*.zip)|*.dbf;*.txt;*.zip|Âñå ôàéëû(*.*)|*.*';
         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 uppercase(extractfileext(filename))='.ZIP'
     then begin
        tmpdir := createuniquedir('','',ntNumber);
        filename := tmpdir + ZipExtractAll(FileName,tmpdir);
       // showmessage(filename);

     end;

     if FileName = '' then exit;


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

       if uppercase(extractfileext(filename))='.DBF'
       then begin

         Table := TTable.Create(selfscript);
         try

          table.tabletype := ttFoxpro;

          if tmpdir=''
          then begin
            tmpdir := createuniquedir('','',ntNumber);
            Table.TableName := tmpdir + extractfilename(FileName);
            filedelete(Table.tablename);
            copyfile(FileName,Table.TableName,False);
          end
          else
            table.tablename := filename;

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

          Table.Open;
          Table.First;

          data.Clear;

          if table.findfield('tfact') <> nil
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('tfact').asstring;
             Header.Values[ExportNakladData] := table.fieldbyname('tdata').asstring;



              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'
                'action_lab'+#9+
                'barcode'+#9+
                'summa'+#9+
                'price_reg'+#9+
                'MNN'+#9+
                'AdvPricesIn'); //ra****

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

              if UseBarcode then
                FBarcode := Table.findfield('barcode')
              else
                FBarcode := nil;
                
              FMNN := table.findfield('mnn');
              
              FAdvPricesIn := table.findfield('PRICEINCOME');
              if FAdvPricesIn = nil then FAdvPricesIn := table.findfield('PRICEINCOM');
              
              if FAdvPricesIn=nil then CreateHintW('Íå íàéäåíî ïîëå ñ äîïîëíèòåëüíûìè öåíàìè ïîñòàâùèêîâ');
                  
              while not Table.eof do begin

                try
                  str1 := table.fieldbyname('docend').asstring;
                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+
                   GetFieldDelta+#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 îñòàâëÿåì ïóñòûì
                
                Str := Str + #9;
                if (FMNN<>nil) and (FMNN.asstring>'')
                then Str := Str + FMNN.asstring;
                
                Str := Str + #9;
                if FAdvPricesIn<>nil  //ra****
                then Str := Str +FAdvPricesIn.asstring; //ra****
                
                data.Add(str);
                Table.Next;
              end;

          end
          else
          if table.findfield('docname') <> nil
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('docname').asstring;
             Header.Values[ExportNakladData] := StrTodtStr( table.fieldbyname('datedoc').asstring);
             Header.Values[ExportNakladSROK] := strtodtstr( table.fieldbyname('datepay').asstring);

              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+
                'action_lab'+#9+
                'regn'+#9+ //+'cenanotnds'
                'barcode'+#9+'cenanotnds'+#9+'price_reg'+#9+'MNN');

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

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


              while not Table.eof do begin


                str :=
                   Table.FieldByName('dateb').AsString+#9+
                   table.fieldbyname('dateb').asstring +#9+
                   Table.FieldByName('sertwho').AsString+#9+
                   Table.FieldByName('quant').AsString+#9+
                   Table.FieldByName('nds').AsString+#9+
                   Table.FieldByName('good').AsString+#9+
                   Table.FieldByName('priceent').asstring+#9+
                   Table.FieldByName('serial').AsString+#9+
                   Table.FieldByName('sert').AsString+#9+
                   Table.FieldByName('enterp').AsString+#9+
                   Table.FieldByName('dates').AsString+#9+
                   {Table.FieldByName('gtd').AsString+}#9+
                   Table.FieldByName('country').AsString+#9+
                   GetFieldDelta+#9+
                   {Table.FieldByName('regn').AsString+}#9;

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

                Str := Str + Table.FieldByName('pricewonds').AsString;
                Str := Str + #9;

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

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

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

          end
          else
          if ((table.findfield('dcode') <> nil) and (table.findfield('srok_s')<> nil))
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('dcode').asstring;
             Header.Values[ExportNakladData] := StrTodtStr( table.fieldbyname('date_doc').asstring);
             //Header.Values[ExportNakladSROK] := strtodtstr( table.fieldbyname('datepay').asstring);

              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+
                'action_lab'+#9+
                'regn'+#9+ //+'cenanotnds'
                'barcode'+#9+'summa'+#9+'price_reg'+#9+'MNN');

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

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

              FMNN := table.findfield('MNN');

              while not Table.eof do begin


                str :=
                   Table.FieldByName('srok_s').AsString+#9+
                   table.fieldbyname('srok_s').asstring +#9+
                   {Table.FieldByName('sertwho').AsString+}#9+
                   Table.FieldByName('kolvo').AsString+#9+
                   Table.FieldByName('nds_pr').AsString+#9+
                   Table.FieldByName('product').AsString+#9+
                   Table.FieldByName('price_base').asstring+#9+
                   Table.FieldByName('series').AsString+#9+
                   Table.FieldByName('sert_n').AsString+#9+
                   Table.FieldByName('producer').AsString+#9+
                   {Table.FieldByName('dates').AsString+}#9+
                   {Table.FieldByName('gtd').AsString+}#9+
                   {Table.FieldByName('country').AsString+}#9+
                   GetFieldDelta+#9+
                   {Table.FieldByName('regn').AsString+}#9;

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

                Str := Str + Table.FieldByName('sum_opl').AsString;
                Str := Str + #9;

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

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

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

          end
          
          else
          if (table.findfield('NDOC') <> nil)   and  (table.findfield('GOD_SERT') <> nil)
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('NDOC').asstring;
             Header.Values[ExportNakladData] := StrTodtStr( table.fieldbyname('DATEDOC').asstring);
             //Header.Values[ExportNakladSROK] := strtodtstr( table.fieldbyname('datepay').asstring);

              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+
                'action_lab'+#9+
                'regn'+#9+ //+'cenanotnds'
                'barcode'+#9+'summa'+#9+'price_reg'+#9+'MNN');

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

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

              FMNN := table.findfield('MNN');

              while not Table.eof do begin


                str :=
                   Table.FieldByName('GDATE').AsString+#9+
                   table.fieldbyname('GOD_SERT').asstring +#9+
                   Table.FieldByName('SERTORG').AsString+#9+
                   Table.FieldByName('QNT').AsString+#9+
                   Table.FieldByName('NDS').AsString+#9+
                   Table.FieldByName('NAME').AsString+#9+
                   Table.FieldByName('PRICEMAN').asstring+#9+
                   Table.FieldByName('SER').AsString+#9+
                   Table.FieldByName('SERTIF').AsString+#9+
                   Table.FieldByName('FIRM').AsString+#9+
                   Table.FieldByName('SERTDATE').AsString+#9+
                   Table.FieldByName('NUMGTD').AsString+#9+
                   Table.FieldByName('CNTRMADE').AsString+#9+
                   GetFieldDelta+#9+
                   {Table.FieldByName('regn').AsString+}#9;

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

                Str := Str + Table.FieldByName('SUMS0').AsString;
                Str := Str + #9;

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

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

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

          end
          
          else
          if ((table.findfield('dcode') <> nil) and (table.findfield('PREP_NAME')<> nil))   //Âàðèàíò Àðõàíãåëüñêà   
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('dcode').asstring;
             Header.Values[ExportNakladData] := StrTodtStr( table.fieldbyname('date_doc').asstring);
             //Header.Values[ExportNakladSROK] := strtodtstr( table.fieldbyname('datepay').asstring);

              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+
                'action_lab'+#9+
                'regn'+#9+ //+'cenanotnds'
                'barcode'+#9+'summa'+#9+'price_reg'+#9+'MNN');

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

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

              FMNN := table.findfield('MNN');

              while not Table.eof do begin


                str :=
                   Table.FieldByName('SER_SROK').AsString+#9+
                   table.fieldbyname('SERT_END').asstring +#9+
                   {Table.FieldByName('sertwho').AsString+}#9+
                   Table.FieldByName('kolvo').AsString+#9+
                   Table.FieldByName('nds_pr').AsString+#9+
                   Table.FieldByName('PREP_NAME').AsString+#9+
                   Table.FieldByName('price_base').asstring+#9+
                   Table.FieldByName('series').AsString+#9+
                   Table.FieldByName('sert_n').AsString+#9+
                   Table.FieldByName('MANUF_N').AsString+#9+
                   {Table.FieldByName('dates').AsString+}#9+
                   {Table.FieldByName('gtd').AsString+}#9+
                   Table.FieldByName('COUNTRY_N').AsString+#9+
                   GetFieldDelta+#9+
                   {Table.FieldByName('regn').AsString+}#9;

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

                Str := Str + Table.FieldByName('sum_opl').AsString;
                Str := Str + #9;

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

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

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

          end
          
          
          else
          if (table.findfield('NDOC') <> nil)   and  (table.findfield('CNTR') <> nil)
          then begin
             Header.Values[ExportNakladNom] := table.fieldbyname('NDOC').asstring;
             Header.Values[ExportNakladData] := StrTodtStr( table.fieldbyname('DATEDOC').asstring);
             //Header.Values[ExportNakladSROK] := strtodtstr( table.fieldbyname('datepay').asstring);

              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+
                'action_lab'+#9+
                'regn'+#9+ //+'cenanotnds'
                'barcode'+#9+'summa'+#9+'price_reg'+#9+
                'MNN');

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

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

              FMNN := table.findfield('MNN');

              while not Table.eof do begin


                str :=
                   Table.FieldByName('GDATE').AsString+#9+
                   //table.fieldbyname('GOD_SERT').asstring +#9+
                   Table.FieldByName('SERTORG').AsString+#9+
                   Table.FieldByName('QNT').AsString+#9+
                   Table.FieldByName('NDS').AsString+#9+
                   Table.FieldByName('NAME').AsString+#9+
                   Table.FieldByName('PRICE1').asstring+#9+  //öåíà ïðîèçâîäèòåëÿ
                   Table.FieldByName('SER').AsString+#9+
                   Table.FieldByName('SERTIF').AsString+#9+
                   Table.FieldByName('FIRM').AsString+#9+
                   Table.FieldByName('SERTDATE').AsString+#9+
                   Table.FieldByName('NUMGTD').AsString+#9+
                   Table.FieldByName('CNTR').AsString+#9+
                   GetFieldDelta+#9+
                   {Table.FieldByName('regn').AsString+}#9;

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

                Str := Str + Table.FieldByName('SUMITEM').AsString;
                Str := Str + #9;

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

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

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

          end
          
          else begin
            raiseexception('Òàáëèöà èìååò ôîðìàò, íå ñîãëàñîâàííûé ñ ïîñòàâùèêîì. Îáðàòèòåñü â òåõïîääåðæêó');
          end;

         finally
            Table.free;
         end;
       end
       else if uppercase(extractfileext(filename))='.TXT'
       then begin


         //DATE_ORDER;NUM_ORDER;DATE_TO_PAID;ORDER_SUM;ORDER_NDS_SUM


         List := Tstringlist.create;
         List1 := Tstringlist.create;
         LIst2 := Tstringlist.create;
         try

          List.loadfromfile(filename);



          if pos('Tdata',list[0])=1
          then begin
          //////////////////////// âàðèàíò txt1

             cds := tclientdataset.create(selfscript);

             ReadTXTList(cds, list );

             Header.Values[ExportNakladNom] := cds.fieldbyname('tfact').asstring;
             Header.Values[ExportNakladData] := cds.fieldbyname('tdata').asstring;



              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'
                'action_lab'+#9+ //+'cenanotnds'
                'barcode'+#9+'summa'+#9+'price_reg'+#9+'MNN');

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

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

              FMNN := cds.findfield('mnn');

              cds.first;
              while not cds.eof do
              begin


                str := '';



                   addstr1(str,cds.findfield('dataendtov'));


                  try
                    addstr1(str,cds.findfield('docend'));

                    (* óñòàðåëî
                        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
                    addstr1(str,nil);
                  end;

                   addstr1(str,cds.findfield('centrsert'));
                   addstr1(str,cds.findfield('number'));
                   addstr1(str,cds.findfield('nds'));
                   addstr1(str,cds.findfield('name'));
                   addstr1(str,cds.findfield('price1'));
                   addstr1(str,cds.findfield('seria'));
                   addstr1(str,cds.findfield('numdoc'));
                   addstr1(str,cds.findfield('manufac'));
                   addstr1(str,cds.findfield('docd'));
                   addstr1(str,cds.findfield('gtd'));
                   addstr1(str,cds.findfield('country'));
                   addstr1(str,cds.findfield('regn'));

                   if cds.findfield('delta')<>nil
                   then begin
                      if (cds.fieldbyname('delta').asstring<>'')
                         and
                         (cds.fieldbyname('delta').asstring<>'0')
                      then
                        addstr1(str,cds.findfield('delta'))
                      else
                         addstr1(str,nil);

                   end
                   else
                     addstr1(str,nil);


                   addstr1(str,FBarcode);

                   addstr1(str,cds.findfield('SUM'));


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

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


          /////////////////////////
          end
          else  // âàðèàíò txt2
          if (list.count<3) or (pos('DATE_ORDER;NUM_ORDER;DATE_TO_PAID;ORDER_SUM;ORDER_NDS_SUM',list[0])=0)
          then
            raiseexception('Òàáëèöà èìååò ôîðìàò, íå ñîãëàñîâàííûé ñ ïîñòàâùèêîì. Îáðàòèòåñü â òåõïîääåðæêó')
          else begin

            StringtoList( list[1],List2,';');

            if list2.count<5
            then
              raiseexception('Òàáëèöà èìååò ôîðìàò, íå ñîãëàñîâàííûé ñ ïîñòàâùèêîì (êîëè÷åñòâî ïîëåé â çàãîëîâêå = '+inttostr(list2.count)+'). Îáðàòèòåñü â òåõïîääåðæêó');


            Header.Values[ExportNakladNom] := List2[1];
            Header.Values[ExportNakladData] := List2[0];
            Header.Values[ExportNakladSROK] := List2[2];

            StringtoList( list[2],List1,';');

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


            for i := 3 to list.count-1 do
            begin
              StringtoList( list[i],List2,';');

              if list2.count=List1.count
              then begin

              //GOODS;ID_PROVIDER_GOODS;QUANT;
              //PRODUCER_PRICE;PROVIDER_MARKUP;PERCENT_MARKUP;
              //PERCENT_NDS;SUM_NDS;SUM;PRICE;PRODUCER;
              //CERTIFICATE_SERIES;CERTIFICATE_DATE;CERTIFICATE_SROK;
              //CERTIFICATE_ORG;COUNTRY;GTD;SERIES;PRODUCTION_DATE;BEST_BEFORE

                Str := '';

                AddStr(Str,List1,'BEST_BEFORE',List2);
                AddStr(Str,List1,'CERTIFICATE_SROK',List2);
                AddStr(Str,List1,'CERTIFICATE_ORG',List2);
                AddStr(Str,List1,'QUANT',List2);
                AddStr(Str,List1,'PERCENT_NDS',List2);
                AddStr(Str,List1,'GOODS',List2);
                AddStr(Str,List1,'PRICE',List2);
                AddStr(Str,List1,'SERIES',List2);
                AddStr(Str,List1,'CERTIFICATE_SERIES',List2);
                AddStr(Str,List1,'PRODUCER',List2);
                AddStr(Str,List1,'CERTIFICATE_DATE',List2);
                AddStr(Str,List1,'GTD',List2);
                AddStr(Str,List1,'COUNTRY',List2);
                AddStr(Str,List1,'REGN',List2); // òàêîãî ïîëÿ íåò ó íèõ ïîêà
                AddStr(Str,List1,'BARCODE',List2); // ïîëÿ íåò ïîêà
                AddStr(Str,List1,'SUM',List2);
                AddStr(Str,List1,'PRICE_REGISTRY',List2); // ïîëÿ íåò ïîêà
                AddStr(Str,List1,'DELTA',List2); // ïîëÿ íåò ïîêà
                data.Add(str);
              end;
            end;
          end;

         finally

            LIst2.free;
            LIst1.free;
            List.free;
         end;
       end
       else CreateHintE('Äàííûé ôîðìàò íàêëàäíûõ îò Êàòðåí íå èññëåäîâàí, îáðàòèòåñü â òåõïîääåðæêó');

     finally
       free;
       if tmpdir<>'' then deletedir(tmpdir);
     end;

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