
// Ñêðèïò ýêñïîðòà ñïèñêà íàêëàäíûõ â TXT-ôàéë. Ñêðèïò òðåáóåò íàëè÷èÿ âåðñèè ïðîãðàììû 5.8.0.8
// Ñêðèïò èñïîëüçóåòñÿ ôîðìîé "Îò÷åòû/Ýêñïîðò â 1Ñ ñïèñêîâ íàêëàäíûõ". Ïðîïèñûâàåòñÿ â ïàðàìåòðàõ
// (âûçûâàåìûõ ïî êíîïêå "Ïàðàìåòðû" â ñàìîé ôîðìå ýêñïîðòà), â ïàðàìåòðå "âûïîëíèòü ñêðèïò". Áîëüøå íèêàêèå
// íàñòðîéêè â ïàðàìåòðàõ ýêñïîðòà íå íóæíû.

// Ñêðèïò ðàçðàáîòàí ïî çàêàçó ÎÎÎ "Àâåñòà-Ì".

// Â ñàìîé ôîðìå íóæíî âûáðàòü ïåðèîä íàêëàäíûõ, òèïû íàêëàäíûõ (ïðèõîäû è ðàñõîäû), ñêëàäû, îñíîâàíèÿ íàêëàäíûõ,
// êîòîðûå ó÷àñòâóþò â îò÷åòå.



var // ïðåäîïðåäåëåííûå ïðîãðàììîé ïåðåìåííûå

  DATASET :TTable; // òàáëèöà ñ øàïêàìè íàêëàäíûõ ,êîòîðóþ ñîáèðàåò âûçûâàþùàÿ ôîðìà. Èç íåå ôîðìèðóåì âûõîäíîé ôàéë
  MinDate :tDateTime; // Ïåðâàÿ èç äàò â äèàïàçîíå
  MaxDate :tDateTime; // Âòîðàÿ (ìàêñèìàëüíàÿ) äàòà â äèàïàçîíå
  CheckOutput :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ðàñõîäíûì äîêóìåíòàì
  CheckInput  :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ïðèõîäíûì äîêóìåíòàì
  CheckInputMoney :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî âûïëàòàì îò ïîêóïàòåëåé


  function ifstr(boolval :boolean; truestr :string; falsestr :string) :string;
  begin
    if boolval then result := truestr
    else result := falsestr;
  end;


  function FindField(table :tTable; KeyVAlue :Variant; SearchFieldName :string ) :string;
  begin
    if table.findkey([KeyValue])
    then result := Table.fieldbyname(searchfieldname).asstring
    else Result := '';
  end;

function coalesce_str2(str1 :string; str2 :string; str3 :string = ''; str4 :string = ''; str5 :string = '') :string;
begin
  result := str1;
  if result >'' then exit;
  result := str2;
  if result  >'' then exit;
  result := str3;
  if result  >'' then exit;
  result := str4;
  if result  >'' then exit;
  result := str5;
end;

function DeleteLastPunkt(str :string) :string;
begin
  result := str;
  if (length(result)>0) and (result[length(result)] ='.')
  then delete(result,length(result),1);
end;



var
  Doc :Variant;
  DocTXT :TStringList;
  NodeIn,NodeOut,NodeMoneyIn :Variant;
  NodeInTXT,NodeOutTXT,NodeOutPERTXT,NodeMoneyInTXT :TStringLIst;
  SaveDialog :TSaveDialog;
  FileName :string;

  Node,RootNode,NodeDetail :Variant;
  QueryPR,QueryRS :tQuery;

  TaPR_Tovar,TaRS_Tovar,TaTovar,TaPRoducer,TaSertif,TaCountry,TaAgents :TTAble;

  strval :string;

  ed,code_ed,name_ed :string;

begin
  Filename := '';

  // çàïðàøèâàåì ôàéë, êóäà áóäåì êëàñòü äàííûå. Ó÷èòûâàþòñÿ äàííûå ïðåäûäóùåãî ñåàíñà
  SaveDialog := TSaveDialog.create(nil);
  try
     SaveDialog.Title := 'Óêàæèòå ôàéë';
     SaveDialog.FileName  := AppIniFile.readstring('plugin_export1c_avesta','filename','');
     SaveDialog.defaultext := 'TXT';

     SaveDialog.Filter := 'TXT-ôàéëû(*.txt)|*.txt';

     SaveDialog.filter := SaveDialog.Filter + '|Âñå ôàéëû(*.*)|*.*';

     if  SaveDialog.execute then
     begin
         Filename := SaveDialog.FileName;
         AppIniFile.writestring('plugin_export1c_avesta','filename',SaveDialog.filename);
     end;
  finally
    SaveDialog.free;
  end;


  // Ñóäÿ ïî âñåìó, íàì äàëè îòáîé, óõîäèì, ïîíóðèâ ãîëîâó...
  if filename = '' then exit;


  QueryPR := TQuery.create(nil);
  with TMyWait.create('Èäåò ýêñïîðò íàêëàäíûõ') do
  try
      QueryRS := TQuery.create(QueryPR);
      TaPR_Tovar := TTable.create(QueryPR);
      TaRS_Tovar := Ttable.create(QueryPR);
      TaTovar := Ttable.create(QueryPR);
      TaPRoducer := Ttable.create(QueryPR);
      TaSertif := Ttable.create(QueryPR);
      TaCountry := Ttable.create(QueryPR);
      TaAgents := Ttable.create(QueryPR);

      QueryPR.databasename := 'dbkassa';
      QueryRS.databasename := 'dbkassa';

      TaPR_tovar.databasename := 'dbkassa';
      TaRS_tovar.databasename := 'dbkassa';
      TaTovar.databasename := 'dbkassa';
      TaProducer.databasename := 'dbkassa';
      TaSertif.databasename := 'dbkassa';
      TaCountry.databasename := 'dbkassa';
      TaAgents.databasename := 'dbkassa';

      TaPR_tovar.TAblename :='pr_tovar';
      tapr_tovar.open;
      TaRS_tovar.TAblename :='rs_tovar';
      tars_tovar.open;
      TaTovar.TAblename :='tovar';
      tatovar.open;
      TaProducer.TAblename :='producer';
      taproducer.open;
      TaSertif.TAblename :='sertif';
      tasertif.open;
      TaCountry.TAblename :='country';
      taCountry.open;
      TaAgents.TAblename :='agents';
      taAgents.open;

      (*
      QueryPR.sql.text := 'select a.id,a.nn,b.name,b.ed,a.kol,a.cena,a.rs_cena,a.nds,a.ndsout,a.seria,a.sertif,a.sroksertif,a.producer,c.name as name_producer,a.idsertif,d.name as name_sertif '+
                          ' from pr_tovar a join tovar b on a.nn = b.nn '+
                          ' left join producer c on c.id = a.producer '+
                          ' left join sertif d on d.id = a.idsertif '+
                          ' where a.id_nakl  = :id_n';

      QueryRS.sql.text := 'select a.id,a.id_pr,a.nn,b.name,b.ed,a.kol,a.cena,a.nds,pr.seria,pr.sertif,pr.sroksertif,pr.producer,c.name as name_producer,pr.idsertif,d.name as name_sertif  '+
                          ' from rs_tovar a join tovar b on a.nn = b.nn '+
                          ' left join  pr_tovar pr on a.id_pr = pr.id '+
                          ' left join producer c on c.id = pr.producer '+
                          ' left join sertif d on d.id = pr.idsertif '+
                          ' where a.id_nakl  = :id_n';
      *)

      QueryPR.sql.text := 'select a.id,a.nn,a.kol,a.cena,a.rs_cena,a.nds,a.ndsout,a.seria'+
                          ',a.sertif,a.sroksertif,a.producer,a.idsertif'+
                          ' from pr_tovar a '+
                          ' where a.id_nakl  = :id_n'+
                          ' order by a.ID';

      QueryRS.sql.text := 'select a.id,a.id_pr,a.nn,a.kol,a.cena,a.nds'+
                          ' from rs_tovar a'+
                          ' where a.id_nakl  = :id_n'+
                          ' order by a.id';

      DocTXT := TStringlIst.create;


      // çàäàåì ãëîáàëüíûå ñâîéñòâà îò÷åòà
      (*
      begin
        DocTXT.add('[root]');
        DocTXT.add('MinDate='+formatdatetime('dd/mm/yyyy', Mindate));
        DocTXt.add('MaxDate='+formatdatetime('dd/mm/yyyy', Maxdate));
      end;
      *)



      if CheckInput
      then begin
          NodeInTXT :=TStringlist.create;
      end;

      if CheckOutput then begin
          NodeOutTXT := tstringlist.create;
          NodeOutPerTXT := tstringlist.create;
      end;

      (*
      if CheckInputMoney
      then begin
          NodeMOneyInTXT := Tstringlist.create;
      end;
      *)

      progressbarpos := 0;
      progressbarmax := dataset.recordcount;
      advancedtext := 'çàïèñåé: '+inttostr(dataset.recordcount);
      dataset.first;
      while not dataset.eof do
      begin


        if (dataset.fieldbyname('po').asstring = '+')
           and CheckInput
           and (pos('Ï',dataset.fieldbyname('osnov').asstring)<>1) // âñå íå ïåðåáðîñû
        then begin
          begin
              NodeInTXT.add('ZAGOLOVOK');

              strval := dataset.fieldbyname('nomer').asstring +'***';
              strval := strval +formatdatetime('dd/mm/yyyy',dataset.fieldbyname('data').asdatetime) + '***';
              strval := strval +dataset.fieldbyname('id_doc').asstring + '***';
              strval := strval +ifstr(dataset.fieldbyname('datepost').asdatetime=0,
                                     formatdatetime('dd/mm/yyyy',dataset.fieldbyname('data').asdatetime),
                                     formatdatetime('dd/mm/yyyy',dataset.fieldbyname('datepost').asdatetime)) + '***';

              strval := strval +dataset.fieldbyname('sklad').asstring + '***';
              strval := strval +dataset.fieldbyname('skladname').asstring + '***';

              strval := strval +dataset.fieldbyname('agent').asstring + '***';
              strval := strval +dataset.fieldbyname('agentname').asstring + '***';


              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('summaz').asfloat) + '***';
              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('nds10z').asfloat + dataset.fieldbyname('nds18z').asfloat) + '***';

              nodeintxt.add(strval);

              //NodeInTXT.add('SummDoc='+ dataset.fieldbyname('summaz').asstring);
              //NodeInTXT.add('SummRetail='+ dataset.fieldbyname('summa').asstring);
              //NodeInTXT.add('SummNDS='+ FormatFloat('0.00##', dataset.fieldbyname('nds10z').asfloat + dataset.fieldbyname('nds18z').asfloat));
              //NodeInTXT.add('SummNDSRetail='+ FormatFloat('0.00##',dataset.fieldbyname('nds10').asfloat + dataset.fieldbyname('nds18').asfloat));
              //NodeInTXT.add('Doc_Reason='+ dataset.fieldbyname('osnov').asstring);
              //NodeInTXT.add('Doc_Reason_Name='+ dataset.fieldbyname('osnovname').asstring);
          end;

          begin
            QueryPR.close;
            QueryPR.parambyname('id_n').asInteger := dataset.fieldbyname('id_nakl').asinteger;
            QueryPR.open;
            QueryPR.first;
            //CreateHintI('id_n='+inttostr(dataset.fieldbyname('id_nakl').asinteger)+', recordcount = '+inttostr(Querypr.recordcount));

            NodeInTXT.add('STROKA');


            while not QueryPR.eof do begin
              //CreateHintI('Record='+inttostr(Querypr.recno));
              
              tatovar.open;

              ed := FindField(taTovar,QueryPR.fieldbyname('nn').asstring,'ed');
              code_ed :=  asstring(BDEqueryvalue('select code from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or  upper(name) = :ed',[deletelastpunkt(uppercase(ed))]));
              name_ed :=  coalesce_str2(asstring(BDEqueryvalue('select smallname_ru from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                            asstring(BDEqueryvalue('select smallname from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                            asstring(BDEqueryvalue('select name from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                          ed);


              begin
                  NodeInTXT.add(
                                QueryPR.Fieldbyname('nn').asstring+'***'+
                                FindField(taTovar,QueryPR.fieldbyname('nn').asstring,'name')+'***'+

                                code_ed+'***'+
                                ed+'***'+
                                name_ed+'***'+

                                QueryPR.Fieldbyname('ndsout').asstring+'***'+
                                QueryPR.Fieldbyname('kol').asstring+'***'+

                                formatfloat('0.00##', QueryPR.Fieldbyname('cena').ascurrency)+'***'+
                                formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat *( 1- QueryPR.fieldbyname('NDS').asinteger/(100 + QueryPR.fieldbyname('nds').asinteger)))+'***'+
                                QueryPR.Fieldbyname('nds').asstring+'***'+
                                formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat * QueryPR.fieldbyname('NDS').asinteger/(100 + QueryPR.fieldbyname('nds').asinteger))+'***'+
                                formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat )+'***'+

                                );
              end;
              QueryPR.next;
            end;
          end;
        end
        else
        if (dataset.fieldbyname('po').asstring = '-')
           and CheckOutput
           and (pos('Ï',dataset.fieldbyname('osnov').asstring)<>1) // âñå íå ïåðåáðîñû
           and (dataset.fieldbyname('osnov').asstring<>'ÂÐ') // ýòè îñíîâàíèÿ òîæå ê ïåðåáðîñàì áóäóò çàêèíóòû
        then begin
          begin

              NodeOutTXT.add('ZAGOLOVOK');

              strval := dataset.fieldbyname('nomer').asstring +'***';
              strval := strval +formatdatetime('dd/mm/yyyy',dataset.fieldbyname('data').asdatetime) + '***';

              strval := strval +dataset.fieldbyname('sklad').asstring + '***';
              strval := strval +dataset.fieldbyname('skladname').asstring + '***';

              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('summa').asfloat) + '***';
              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('nds10').asfloat+ dataset.fieldbyname('nds18').asfloat) + '***';

              nodeouttxt.add(strval);

          end;

          begin
            QueryRS.close;
            QueryRS.parambyname('id_n').asInteger := dataset.fieldbyname('id_nakl').asinteger;
            QueryRS.open;
            QueryRS.first;

            NodeOutTXT.add('STROKA');

            while not QueryRS.eof do begin
              begin
              
                tatovar.open;


                ed := FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'ed');
                code_ed :=  asstring(BDEqueryvalue('select code from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or  upper(name) = :ed',[deletelastpunkt(uppercase(ed))]));
                name_ed :=  coalesce_str2(asstring(BDEqueryvalue('select smallname_ru from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                              asstring(BDEqueryvalue('select smallname from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                              asstring(BDEqueryvalue('select name from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                            ed);


                  NodeoutTXT.add(
                                Queryrs.Fieldbyname('nn').asstring+'***'+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'name')+'***'+

                                code_ed+'***'+
                                ed+'***'+
                                name_ed+'***'+

                                Queryrs.Fieldbyname('nds').asstring+'***'+
                                Queryrs.Fieldbyname('kol').asstring+'***'+

                                formatfloat('0.00##', Queryrs.Fieldbyname('cena').ascurrency)+'***'+
                                formatfloat('0.00##', Queryrs.Fieldbyname('kol').asfloat *Queryrs.fieldbyname('cena').asfloat *( 1- Queryrs.fieldbyname('NDS').asinteger/(100 + Queryrs.fieldbyname('nds').asinteger)))+'***'+
                                Queryrs.Fieldbyname('nds').asstring+'***'+
                                formatfloat('0.00##', Queryrs.Fieldbyname('kol').asfloat *Queryrs.fieldbyname('cena').asfloat * Queryrs.fieldbyname('NDS').asinteger/(100 + Queryrs.fieldbyname('nds').asinteger))+'***'+
                                formatfloat('0.00##', Queryrs.Fieldbyname('kol').asfloat *Queryrs.fieldbyname('cena').asfloat )+'***'
                                );

                  (*
                  NodeOutTXT.add(Queryrs.Fieldbyname('id').asstring+#9+
                                Queryrs.Fieldbyname('nn').asstring+#9+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'name')+#9+
                                //Queryrs.Fieldbyname('name').asstring+#9+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'ed')+#9+
                                //Queryrs.Fieldbyname('ed').asstring+#9+
                                Queryrs.Fieldbyname('kol').asstring+#9+
                                formatfloat('0.00##', Queryrs.Fieldbyname('cena').ascurrency)+#9+
                                Queryrs.Fieldbyname('nds').asstring+#9+
                                Queryrs.Fieldbyname('id_pr').asstring+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'seria')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'sertif')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'sroksertif')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'producer')+#9+
                                FindField(taproducer,FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'producer'),'name')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'idsertif')+#9+
                                FindField(tasertif,FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'idsertif'),'name')+#9+
                                formatfloat('0.00##', QueryRS.Fieldbyname('kol').asfloat *QueryRS.fieldbyname('cena').asfloat)+#9+
                                formatfloat('0.00##', QueryRS.Fieldbyname('kol').asfloat *Queryrs.fieldbyname('cena').asfloat * QueryRS.fieldbyname('NDS').asinteger/(100 + QueryRS.fieldbyname('nds').asinteger))


                                );
                  *)
                    //QueryRS.Fieldbyname('name_sertif').asstring;
              end;

              QueryRS.next;
            end;
          end;
        {
        end
        else
        if (dataset.fieldbyname('po').asstring = '1') and CheckInputMoney
        then begin
          // do nothing çà íåíàäîáíîñòüþ
        }
        end
        else
        if (dataset.fieldbyname('po').asstring = '-')
           and CheckOutput
           and ((pos('Ï',dataset.fieldbyname('osnov').asstring)=1) // ïåðåáðîñû
                 or  (dataset.fieldbyname('osnov').asstring='ÂÐ') // âñå íå ïåðåáðîñû
               )
        then begin
          begin

              NodeOUTPERTXT.add('ZAGOLOVOK');

              strval := dataset.fieldbyname('nomer').asstring +'***';
              strval := strval +formatdatetime('dd/mm/yyyy',dataset.fieldbyname('data').asdatetime) + '***';

              strval := strval +dataset.fieldbyname('sklad').asstring + '***';
              strval := strval +dataset.fieldbyname('skladname').asstring + '***';

              strval := strval +dataset.fieldbyname('agent').asstring + '***';
              strval := strval +dataset.fieldbyname('agentname').asstring + '***';

              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('summa').asfloat) + '***';
              strval := strval +FormatFloat('0.00##', dataset.fieldbyname('nds10').asfloat+ dataset.fieldbyname('nds18').asfloat) + '***';

              NodeOutPerTXT.add(strval);

          end;

          begin
            QueryRS.close;
            QueryRS.parambyname('id_n').asInteger := dataset.fieldbyname('id_nakl').asinteger;
            QueryRS.open;
            QueryRS.first;

            NodeOutPerTXT.add('STROKA');

            while not QueryRS.eof do begin
              begin

                  tatovar.open;

                  ed := FindField(taTovar,QueryRS.fieldbyname('nn').asstring,'ed');
                  code_ed :=  asstring(BDEqueryvalue('select code from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or  upper(name) = :ed',[deletelastpunkt(uppercase(ed))]));
                  name_ed :=  coalesce_str2(asstring(BDEqueryvalue('select smallname_ru from ed where upper(codename)=:ed or upper(codename_ru) = :ed or upper(smallname) = :ed or upper(smallname_ru) = :ed or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                                asstring(BDEqueryvalue('select smallname from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                                asstring(BDEqueryvalue('select name from ed where upper(codename)=:ed or upper(codename_ru) = :ed  or upper(smallname) = :ed or upper(smallname_ru) = :ed  or upper(name) = :ed',[deletelastpunkt(uppercase(ed))])),
                              ed);


                  NodeOutPerTXT.add(
                                Queryrs.Fieldbyname('nn').asstring+'***'+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'name')+'***'+

                                code_ed+'***'+
                                ed+'***'+
                                name_ed+'***'+

                                Queryrs.Fieldbyname('nds').asstring+'***'+
                                Queryrs.Fieldbyname('kol').asstring+'***'

                                //+
                                //formatfloat('0.00##', QueryPR.Fieldbyname('cena').ascurrency)+'***'+
                                //formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat *( 1- QueryPR.fieldbyname('NDS').asinteger/(100 + QueryPR.fieldbyname('nds').asinteger)))+'***'+
                                //QueryPR.Fieldbyname('nds').asstring+'***'+
                                //formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat * QueryPR.fieldbyname('NDS').asinteger/(100 + QueryPR.fieldbyname('nds').asinteger))+'***'+
                                //formatfloat('0.00##', QueryPR.Fieldbyname('kol').asfloat *QueryPR.fieldbyname('cena').asfloat )+'***'+

                                );

                  (*
                  NodeOutTXT.add(Queryrs.Fieldbyname('id').asstring+#9+
                                Queryrs.Fieldbyname('nn').asstring+#9+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'name')+#9+
                                //Queryrs.Fieldbyname('name').asstring+#9+
                                FindField(taTovar,Queryrs.fieldbyname('nn').asstring,'ed')+#9+
                                //Queryrs.Fieldbyname('ed').asstring+#9+
                                Queryrs.Fieldbyname('kol').asstring+#9+
                                formatfloat('0.00##', Queryrs.Fieldbyname('cena').ascurrency)+#9+
                                Queryrs.Fieldbyname('nds').asstring+#9+
                                Queryrs.Fieldbyname('id_pr').asstring+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'seria')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'sertif')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'sroksertif')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'producer')+#9+
                                FindField(taproducer,FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'producer'),'name')+#9+
                                FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'idsertif')+#9+
                                FindField(tasertif,FindField(tapr_tovar,Queryrs.fieldbyname('id_pr').asstring,'idsertif'),'name')+#9+
                                formatfloat('0.00##', QueryRS.Fieldbyname('kol').asfloat *QueryRS.fieldbyname('cena').asfloat)+#9+
                                formatfloat('0.00##', QueryRS.Fieldbyname('kol').asfloat *Queryrs.fieldbyname('cena').asfloat * QueryRS.fieldbyname('NDS').asinteger/(100 + QueryRS.fieldbyname('nds').asinteger))


                                );
                  *)
                    //QueryRS.Fieldbyname('name_sertif').asstring;
              end;

              QueryRS.next;
            end;
          end;
        {
        end
        else
        if (dataset.fieldbyname('po').asstring = '1') and CheckInputMoney
        then begin
          // do nothing çà íåíàäîáíîñòüþ
        }
        end;

        dataset.next;
        IncProgress;
      end;

      begin
        If checkInput then begin
          DocTXT.add('VID:ÏîñòóïëåíèåÒîâàðîâ');
          DocTXT.text := Doctxt.text + NodeInTXT.text;
          NodeInTXT.free
        end;
        If checkOutput then begin
          DocTXT.add('VID:Ðîçíè÷íàÿÏðîäàæà');
          DocTXT.text := Doctxt.text + NodeoutTXT.text;
          NodeOutTXT.free;
          DocTXT.add('VID:ÏåðåìåùåíèåÒîâàðîâ');
          DocTXT.text := Doctxt.text + NodeoutperTXT.text;
          NodeOutPerTXT.free;
        end;
        (*
        If checkinputmoney then begin
          DocTXT.add('[InputMoney]');
          DocTXT.add(NodeMoneyInTXT.text);
          NodeMoneyInTXT.Free
        end;
        *)

        DocTXT.SaveToFile(FileName);
        DocTXT.free;

      end;

      CreateHintI('Îïåðàöèÿ ýêñïîðòà äàííûõ çàâåðøåíà');
  finally
    free;
    QueryPR.free;
  end;

end.


