procedure  OnSrcChange(Sender: TObject);
var
  i: Integer;
begin
  DocumentClass_Id := 27;
  FileGuid := '{A18DCFA6-3842-4CFC-902D-C55D08322CA4}';
  edtDirection := TEdit(FmSrc.FindComponent('edtDirection'));
  edtOperType := TEdit(FmSrc.FindComponent('edtOperType'));
  WriteLog('Контрагенты: ' + IfThen(edtCounteragent.Text = '', 'Не указаны', edtCounteragent.Text));
  WriteLog('Гл. администраторы: ' + IfThen(edtKadmi.Text = '', 'Не указаны', edtKadmi.Text));
  WriteLog('Расходные обязательтсва: ' + IfThen(edtDirection.Text = '', 'Не указаны', edtDirection.Text));
  WriteLog('Типы операций: ' + IfThen(edtOperType.Text = '', 'Не указаны', edtOperType.Text));
  Sql := 'Select S.Id, D.Document_Id, D.Doc_Number, D.Doc_Date' +
    ', D.OperType_Id, P.Caption OperType_Name' +
    ', S.Counteragent_Id, C.Caption Counteragent_Caption' +
    ', S.Direction_Id Pl_Direction_Id, Pd.Name Pl_Direction_Name, O.Name Pl_Direction_Org_Name' +
    ', S.Fsi_Id, S.Kadmi_Code, S.Kdi_Code, S.Kesi_Code, S.Ki_Code'

    // exchange #2485 - добавлен код цели в выгрузку УиБН
   +', S.PURPOSEFULGRANT_ID '

    // exchange #3017 - экспорт поля "Источник изменения БН"
   +', D.SOURCE_SRCDOC_ID'

   +', S.Y1_Amt, S.Y2_Amt, S.Y3_Amt' +
    ', D.Remark, D.Description'

    // exchange #3110 - экспорт получателя
   +' , D.REC_ID, D.REC_KPP, D.REC_NAME, D.REC_ACCOUNT_ID'
   +' From SrcDoc D' +
    ' Join SourceChange S On S.SrcDoc_Id  = D.Id' +
    ' Left Outer Join OperType P On P.Id = D.OperType_Id' +
    ' Left Outer Join Org C On C.Id = S.Counteragent_Id' +
    ' Left Outer Join Pl_Direction Pd On Pd.Id = S.Direction_Id' +
    ' Left Outer Join Org O On O.Id = Pd.Org_Id' +
    ' Where D.Budget_Id = ? And D.DocumentClass_Id = 27 And D.DispStatus_Id = 10' +
    ' And D.Doc_Date >= ? And D.Doc_Date <= ?' +
    ' And (S.Y1_Amt + S.Y2_Amt + S.Y3_Amt) <> 0' +
    IfThen(chkRepeat.Checked, '', ' And S.ExtFlag = 0') +
    IfThen(edtCounteragent.Hint = '', '', ' And S.Counteragent_Id In (' + edtCounteragent.Hint + ')') +
    IfThen(edtKadmi.Hint = '', '', ' And S.Kadmi_Code In (' + edtKadmi.Hint + ')') +
    IfThen(edtDirection.Hint = '', '', ' And S.Direction_Id In (' + edtDirection.Hint + ')') +
    IfThen(edtOperType.Hint = '', '', ' And D.OperType_Id In (' + edtOperType.Hint + ')') +
    ' Order By D.Document_Id';
  Form.SimpleText := 'Запрос данныз из АЦК ...';
  Xml := QueryXml(Sql, [Budget_Id, StrToDate(DateToStr(dtmBeginDate.Date)),
    StrToDate(DateToStr(dtmEndDate.Date))]);
  Form.SimpleText := 'Формирование данных экспорта ...';
  Xml := Xml.documentElement.childNodes;
  ProgressBar.Max := Xml.length;
  Data := CreateOleObject('Microsoft.XmlDom');
  Data := Data.appendChild(Data.createElement('ROOT'));
  FillHeadFile;
  Head := Null;
  for i := 0 to ProgressBar.Max - 1 do
  begin
    Xml := Xml.item[i];
    Xml.setAttribute('KEY', Xml.getAttribute('DOCUMENT_ID'));
    if Key <> Xml.getAttribute('KEY') then
    begin
      Key := Xml.getAttribute('KEY');
      if not VarIsNull(Head) and (Head.childNodes.length = 0) then
        Data.removeChild(Head);
      Head := Data.appendChild(Data.ownerDocument.createElement('ROW'));
      Head.setAttribute('DOCUMENT_ID', Xml.getAttribute('DOCUMENT_ID'));
      Head.setAttribute('DOC_NUMBER', Trim(Xml.getAttribute('DOC_NUMBER')));
      Head.setAttribute('DOC_DATE', Xml.getAttribute('DOC_DATE'));
      Head.setAttribute('OPERTYPE_ID', Xml.getAttribute('OPERTYPE_ID'));
      Head.setAttribute('OPERTYPE_NAME', Xml.getAttribute('OPERTYPE_NAME'));
      Head.setAttribute('REMARK', Xml.getAttribute('REMARK'));
      Head.setAttribute('DESCRIPTION', Xml.getAttribute('DESCRIPTION'));
      // exchange #3017 - экспорт поля "Источник изменения БН"
      Head.setAttribute('SOURCE_SRCDOC_ID', Xml.getAttribute('SOURCE_SRCDOC_ID'));
      // exchange #3110 - экспорт получателя
      Head.setAttribute( 'REC_ID', Xml.getAttribute( 'REC_ID'));
      Head.setAttribute( 'REC_KPP', Xml.getAttribute( 'REC_KPP'));
      Head.setAttribute( 'REC_NAME', Xml.getAttribute( 'REC_NAME'));
      Head.setAttribute( 'REC_ACCOUNT_ID', Xml.getAttribute( 'REC_ACCOUNT_ID'));
    end;
    Line := Head.appendChild(Data.ownerDocument.createElement('LINE'));
    Line.setAttribute('ID', Xml.getAttribute('ID'));
    Line.setAttribute('LINE_NUMBER', IntToStr(Head.childNodes.length));
    Line.setAttribute('AMOUNT1YEAR', Xml.getAttribute('Y1_AMT'));
    Line.setAttribute('AMOUNT2YEAR', Xml.getAttribute('Y2_AMT'));
    Line.setAttribute('AMOUNT3YEAR', Xml.getAttribute('Y3_AMT'));
    Line.setAttribute('COUNTERAGENT_ID', Xml.getAttribute('COUNTERAGENT_ID'));
    Line.setAttribute('COUNTERAGENT_CAPTION', Xml.getAttribute('COUNTERAGENT_CAPTION'));
    Line.setAttribute('FSI_ID', Xml.getAttribute('FSI_ID'));
    Line.setAttribute('KADMI_CODE', Xml.getAttribute('KADMI_CODE'));
    Line.setAttribute('KI_CODE', Xml.getAttribute('KI_CODE'));
    Line.setAttribute('KESI_CODE', Xml.getAttribute('KESI_CODE'));
    Line.setAttribute('KDI_CODE', Xml.getAttribute('KDI_CODE'));
    Line.setAttribute('PL_DIRECTION_ID', Xml.getAttribute('PL_DIRECTION_ID'));
    Line.setAttribute('PL_DIRECTION_NAME', Xml.getAttribute('PL_DIRECTION_NAME'));
    Line.setAttribute('PL_DIRECTION_ORG_NAME', Xml.getAttribute('PL_DIRECTION_ORG_NAME'));

    // exchange #2485 - добавлен код цели в выгрузку УиБН
    // добавлена выгрузка в два поля - не понятно какое должно быть
    Line.setAttribute('PURPOSEFULGRANT_ID', Xml.getAttribute('PURPOSEFULGRANT_ID'));
    Line.setAttribute('PURPOSEFULGRANTI_ID', Xml.getAttribute('PURPOSEFULGRANT_ID'));

    ProgressBar.Position := ProgressBar.Position + 1;
    Xml := Xml.ParentNode.ChildNodes;
  end;
  Form.SimpleText := '';
  ProgressBar.Position := 0;
  if not VarIsNull(Head) and (Head.childNodes.length = 0) then
    Data.removeChild(Head);
  if Data.childNodes.length = 0 then
  begin
    MessageDlg('Нет данных по соответстующему запросу', mtInformation, mbOk, 0);
    Exit;
  end;
  UpdateExtFlag('SRCDOC', Data.GetElementsByTagName('LINE'));
  Form.SimpleText := 'Сохранение данных в файл ...';
  Data := Data.ownerDocument;
  Data.documentElement.setAttribute('SP_VER_CAPTION', 'Версия 1');
  Data.documentElement.setAttribute('ONDATE', FormatDateTime('yyyy-mm-dd', Now));
  FileOut := GetUniqueFileName(edtDir.Text + 'SrcChange.xml');
  Data.loadXml('<?xml version="1.0" encoding="Windows-1251"?>' + FormatXmlData(Data.xml));
  Data.save(FileOut);
  WriteLog(Format('Выгружено документов: %d', [Data.documentElement.childNodes.length]));
  WriteLog('Данный сохранены в файл: ' + FileOut);
  Form.SimpleText := '';
  MessageDlg('Данный сохранены в файл: ' + FileOut, mtInformation, mbOk, 0);end; {End OnSrcChange}

begin
end.