procedure OnInChange(Sender: TObject);
var
  i: Integer;
begin
  DocumentClass_Id := 26;
  FileGuid := '{A88349DD-FAC4-43F9-ABC5-39B11639EEBC}';
  edtOperType := TEdit(FmIn.FindComponent('edtOperType'));
  WriteLog('Территории: ' + IfThen(edtTerritory.Text = '', 'Не указаны', edtTerritory.Text));
  WriteLog('Получатели: ' + IfThen(edtReceiver.Text = '', 'Не указаны', edtReceiver.Text));
  WriteLog('Гл. администраторы: ' + IfThen(edtKadmd.Text = '', 'Не указаны', edtKadmd.Text));
  WriteLog('Типы операций: ' + IfThen(edtOperType.Text = '', 'Не указаны', edtOperType.Text));
  Sql := 'Select I.Id, D.Document_Id, D.Doc_Number, D.Doc_Date' +
    ', D.OperType_Id, P.Caption OperType_Name' +
    ', I.Receiver_Id, R.Caption Receiver_Caption' +
    ', Ro.TaxCode Receiver_Inn, Ro.Kpp Receiver_Kpp, Ro.Okato Receiver_Okato' +
    ', I.Payer_Id, Po.Caption Payer_Caption, Po.TaxCode Payer_Inn' +
    ', Po.Kpp Payer_Kpp, Po.Okato Payer_Okato' +
    ', I.Territory_Id, T.Caption Territory_Caption, T.Code Territory_Code, T.Okato Territory_Okato' +
    ', I.Y1_Amt, I.Y2_Amt, I.Y3_Amt' +
    ', D.Kdt_Code, I.Fsd_Id, I.Kadmd_Code, I.Kd_Code, I.Kesd_Code, I.Kdd_Code'

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

   +', D.Remark, D.Description, I.PurposeFulGrant_Id' +
    ' From InDoc D' +
    ' Join InChange I On I.InDoc_Id  = D.Id' +
    ' Left Outer Join OperType P On P.Id = D.OperType_Id' +
    ' Left Outer Join Receiver R On R.Id = I.Receiver_Id' +
    ' Left Outer Join Org Ro On Ro.Id = R.Org_Id' +
    ' Left Outer Join Org Po On Po.Id = I.Payer_Id' +
    ' Left Outer Join Territory T On T.Id = I.Territory_Id' +
    ' Where D.Budget_Id = ? And D.DocumentClass_Id = 26 And D.DispStatus_Id = 10' +
    ' And D.Doc_Date >= ? And D.Doc_Date <= ?' +
    ' And (I.Y1_Amt + I.Y2_Amt + I.Y3_Amt) <> 0' +
    IfThen(chkRepeat.Checked, '', ' And I.ExtFlag = 0') +
    IfThen(edtTerritory.Hint = '', '', ' And I.Territory_Id In (' + edtTerritory.Hint + ')') +
    IfThen(edtReceiver.Hint = '', '', ' And I.Receiver_Id In (' + edtReceiver.Hint + ')') +
    IfThen(edtKadmd.Hint = '', '', ' And I.Kadmd_Code In (' + edtKadmd.Hint + ')') +
    IfThen(edtOperType.Hint = '', '', ' And D.OperType_Id In (' + edtOperType.Hint + ')') +
    ' Order By D.Document_Id, D.Kdt_Code, I.Fsd_Id, I.Kadmd_Code, I.Kd_Code, I.Kesd_Code, I.Kdd_Code';
  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')
                    + Xml.getAttribute('FSD_ID')
                    + Xml.getAttribute('KADMD_CODE')
                    + Xml.getAttribute('KD_CODE')
                    + Xml.getAttribute('KESD_CODE')
                    + Xml.getAttribute('KDT_CODE')
                    // exchange #2647
                    // для разделения на ЭД добавлен код цели
                    + Xml.getAttribute('PURPOSEFULGRANT_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('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('FSD_ID', Xml.getAttribute('FSD_ID'));
      Head.setAttribute('KADMD_CODE', Xml.getAttribute('KADMD_CODE'));
      Head.setAttribute('KDT_CODE', Xml.getAttribute('KDT_CODE'));
      Head.setAttribute('KD_CODE', Xml.getAttribute('KD_CODE'));
      Head.setAttribute('KESD_CODE', Xml.getAttribute('KESD_CODE'));
      Head.setAttribute('REMARK', Xml.getAttribute('REMARK'));
      Head.setAttribute('DESCRIPTION', Xml.getAttribute('DESCRIPTION'));
      // exchange #2647
      // выгрузка кода цели в шапке ЭД
      Head.setAttribute('PURPOSEFULGRANT_ID', Xml.getAttribute('PURPOSEFULGRANT_ID'));
      // exchange #3017 - экспорт поля "Источник изменения БН"
      Head.setAttribute('SOURCE_INDOC_ID', Xml.getAttribute('SOURCE_INDOC_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('KDD_CODE', Xml.getAttribute('KDD_CODE'));
    Line.setAttribute('Y1_AMT', Xml.getAttribute('Y1_AMT'));
    Line.setAttribute('Y2_AMT', Xml.getAttribute('Y2_AMT'));
    Line.setAttribute('Y3_AMT', Xml.getAttribute('Y3_AMT'));
    Line.setAttribute('PURPOSEFULGRANT_ID', Xml.getAttribute('PURPOSEFULGRANT_ID'));
    Line := Line.appendChild(Data.ownerDocument.createElement('RECEIVER'));
    Line.setAttribute('ID', Xml.getAttribute('RECEIVER_ID'));
    Line.setAttribute('CAPTION', Xml.getAttribute('RECEIVER_CAPTION'));
    Line.setAttribute('INN', Xml.getAttribute('RECEIVER_INN'));
    Line.setAttribute('KPP', Xml.getAttribute('RECEIVER_KPP'));
    Line.setAttribute('OKATO', Xml.getAttribute('RECEIVER_OKATO'));
    Line := Line.parentNode;
    Line := Line.appendChild(Data.ownerDocument.createElement('PAYER'));
    Line.setAttribute('ID', Xml.getAttribute('PAYER_ID'));
    Line.setAttribute('CAPTION', Xml.getAttribute('PAYER_CAPTION'));
    Line.setAttribute('INN', Xml.getAttribute('PAYER_INN'));
    Line.setAttribute('KPP', Xml.getAttribute('PAYER_KPP'));
    Line.setAttribute('OKATO', Xml.getAttribute('PAYER_OKATO'));
    Line := Line.parentNode;
    Line := Line.appendChild(Data.ownerDocument.createElement('TERRITORY'));
    Line.setAttribute('ID', Xml.getAttribute('TERRITORY_ID'));
    Line.setAttribute('CAPTION', Xml.getAttribute('TERRITORY_CAPTION'));
    Line.setAttribute('CODE', Xml.getAttribute('TERRITORY_CODE'));
    Line.setAttribute('OKATO', Xml.getAttribute('TERRITORY_OKATO'));
    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('INDOC', Data.GetElementsByTagName('LINE'));
  Form.SimpleText := 'Сохранение данных в файл ...';
  Data := Data.ownerDocument;
  Data.documentElement.setAttribute('SP_VER_CAPTION', 'Версия 13');
  Data.documentElement.setAttribute('SP_VER_ID', '1201000000153');
  FileOut := GetUniqueFileName(edtDir.Text + 'InChange.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 OnIncChange}

begin
end.