
// Ñêðèïò ýêñïîðòà ïðàéñ-ëèñòà (ìèíèìàëüíûå öåíû) â XML-ôàéë.

const
  ExportFileName = 'c:\temp\price.xml';


var
  Doc :Variant;
  SaveDialog :TSaveDialog;
  FileName :string;

  Node,RootNode,NodeDetail :Variant;
  Query :tQuery;


begin
  Filename := '';

  if exportfilename > '' then FileName := ExportFilename
  else begin
    // çàïðàøèâàåì ôàéë, êóäà áóäåì êëàñòü äàííûå. Ó÷èòûâàþòñÿ äàííûå ïðåäûäóùåãî ñåàíñà
    SaveDialog := TSaveDialog.create(nil);
    try
       SaveDialog.Title := 'Óêàæèòå ôàéë';
       SaveDialog.FileName  := AppIniFile.readstring('plugin_export1c','filename','');
       SaveDialog.defaultext := 'XML';
       if true //UseFormat = 0
       then
         SaveDialog.Filter := 'XML-ôàéëû(*.xml)|*.xml'
       ;//else
       //  SaveDialog.Filter := 'TXT-ôàéëû(*.txt)|*.txt';

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

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

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


  Query := TQuery.create(nil);
  with TMyWait.create('Èäåò ýêñïîðò ïðàéñ-ëèñòà') do
  try
      Query.databasename := 'dbkassa';

      Query.sql.text := 'select b.name, min(a.rs_cena) as price '+
                          ' from pr_tovar a join tovar b on a.nn = b.nn '+
                          ' where a.ostat>0 ' +
                          ' group by b.name ' +
                          ' order by b.name ';

      Query.open;

      if true //UseFormat = 0
      then begin
        Doc := CreateOleObject('MSXML2.DOMDocument');
        Doc.Async := false;
        Doc.setProperty('SelectionLanguage', 'XPath');

        // òàê âñå äåëàþò çà÷åì-òî :)
        Doc.AppendChild(Doc.CreateProcessingInstruction('xml', 'version=''1.0'' encoding=''utf-8'''));

      {end
      else begin
        DocTXT := TStringlIst.create;}
      end;



      // çàäàåì ãëîáàëüíûå ñâîéñòâà îò÷åòà

      if true //UseFormat = 0
      then begin
        RootNode := Doc.CreateElement('root');
        Doc.appendchild(RootNode);
        RootNode.Attributes.SetNamedItem(Doc.CreateAttribute('Date')).Value := formatdatetime('dd/mm/yyyy', date());

        //NodeDocAttributes.AppendChild(Doc.CreateElement('MinDate')).Text := '222';
      {end
      else begin
        DocTXT.add('[root]');
        DocTXT.add('MinDate='+formatdatetime('dd/mm/yyyy', Mindate));
        DocTXt.add('MaxDate='+formatdatetime('dd/mm/yyyy', Maxdate));}
      end;



      if true //UseFormat = 0
      then begin
        RootNode.AppendChild(Doc.CreateComment('Ïðàéñ-ëèñò'));
        Node := RootNode.AppendChild(Doc.CreateElement('Price'));
      {end
      else begin
        NodeInTXT :=TStringlist.create;
      }
      end;


      progressbarpos := 0;
      progressbarmax := Query.recordcount;
      advancedtext := 'çàïèñåé: '+inttostr(Query.recordcount);
      Query.first;
      while not Query.eof do
      begin
          if true //useformat = 0
          then begin
              NodeDetail := Node.AppendChild(Doc.createElement('Row'));
              NodeDetail.Attributes.SetNamedItem(Doc.createAttribute('NAME')).Value := Query.fieldbyname('Name').asstring;
              NodeDetail.Attributes.SetNamedItem(Doc.createAttribute('Price')).Value := Query.fieldbyname('Price').asstring;
          {end
          else begin
              NodeInTXT.add('[Invoice]');
              NodeInTXT.add('ID='+ dataset.fieldbyname('ID_NAKL').asstring);
              NodeInTXT.add('DateDoc='+ dataset.fieldbyname('data').asstring);
              NodeInTXT.add('NumberDoc='+ dataset.fieldbyname('nomer').asstring);
              NodeInTXT.add('FacturaDoc=' +
                ifstr(dataset.fieldbyname('factura').asinteger=0,'', dataset.fieldbyname('factura').asstring));
              NodeInTXT.add('ID_DOC='+ dataset.fieldbyname('id_doc').asstring);
              NodeInTXT.add('Storage='+ dataset.fieldbyname('sklad').asstring);
              NodeInTXT.add('Storage_Name='+ dataset.fieldbyname('skladname').asstring);
              NodeInTXT.add('Agent='+ dataset.fieldbyname('agent').asstring);

              NodeInTXT.add('AgentINN='+
                FindField(TaAgents,dataset.fieldbyname('agent').asinteger,'inn'));
                //vartostr( BDEQueryValue(' select inn from agents where id = :id', [dataset.fieldbyname('agent').asinteger])));
              NodeInTXT.add('AgentDogovor='+
                FindField(TaAgents,dataset.fieldbyname('agent').asinteger,'dogovor'));

                //vartostr( BDEQueryValue(' select dogovor from agents where id = :id', [dataset.fieldbyname('agent').asinteger])));

              NodeInTXT.add('Agent_Name='+ dataset.fieldbyname('agentname').asstring);
              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;
          query.next;
          IncProgress;
      end;

      if true //UseFormat=0
      then begin
        Doc.Save(FileName);
      {end
      else begin
        If checkInput then begin
          DocTXT.add('[InputInvoices]');
          DocTXT.add(NodeInTXT.text);
          NodeInTXT.free
        end;
        If checkOutput then begin
          DocTXT.add('[OutputInvoices]');
          DocTXT.add(NodeOutTXT.text);
          NodeOutTXT.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;
    Query.free;
  end;

end.

