var
  cbxWoCommit: TComboBox;

procedure OnExpChange(Sender: TObject);
var
  i: Integer;
begin

  DocumentClass_Id := 128;
  FileGuid := '{3F1EF7CA-01D1-471F-A898-F59F4FE0E6AF}';
  edtDirection := TEdit(FmExp.FindComponent('edtDirection'));
  cbxWoCommit := TComboBox(FmExp.FindComponent('cbxWoCommit'));
  edtOperType := TEdit(FmExp.FindComponent('edtOperType'));
  WriteLog('Бланки расходов: ' + IfThen(edtEstimate.Text = '', 'Не указаны', edtEstimate.Text));
  WriteLog('Бюджетополучатели: ' + IfThen(edtRecipient.Text = '', 'Не указаны', edtRecipient.Text));
  WriteLog('Расходные обязательтсва: ' + IfThen(edtDirection.Text = '', 'Не указаны', edtDirection.Text));
  WriteLog('Типы операций: ' + IfThen(edtOperType.Text = '', 'Не указаны', edtOperType.Text));
  WriteLog('Источники изменения ассигнований и лимитов: ' + IfThen(edtAssignSource.Text = '', 'Не указаны', edtAssignSource.Text));
  WriteLog('Без внесения изменений в закон о бюджете: ' + IfThen(cbxWoCommit.Text = '', 'Все', cbxWoCommit.Text));
  Sql := 'Select D.Id, D.Title_Id, D.Document_Id, D.Doc_Number, D.Doc_Date' +
    ', D.OperType_Id, P.Caption OperType_Name' +
    ', D.Fsr_Id, D.Estimate_Id Est_Id, E.Caption Est_Caption, E.FinSource_Id Est_Fsr_Id' +
    ', E.Org_Id Est_Org_Id, E.EstKind_Id Est_Kind_Id' +
    ', O.TaxCode Est_Inn, O.Kpp Est_Kpp, A.Code Assign_Source_Code' +
    ', D.Recipient_Id, R.TaxCode Inn, R.Kpp, R.Caption Recipient_Caption' +
    ', D.Direction_Id Pl_Direction_Id, Pd.Name Pl_Direction_Name, Od.Name Pl_Direction_Org_Name' +
    ', D.Kadmr_Code, D.Kcsr_Code, D.Kde_Code, D.Kdf_Code, D.Kdr_Code, D.Kesr_Code, D.Kfsr_Code, D.Kvr_Code' +
    ', D.Assign_Amt1, D.Assign_Amt2, D.Assign_Amt3' +
    ', D.Limit_Amt1, D.Limit_Amt2, D.Limit_Amt3' +
    ', D.Q1_Amt, D.Q2_Amt, D.Q3_Amt, D.Q4_Amt' +
    ', D.Remark, D.Description, D.PurposeFulGrant_Id' +
    ' From ExpAssignment D' +
    ' Left Outer Join OperType P On P.Id = D.OperType_Id' +
    ' Left Outer Join Estimate E On E.Id = D.Estimate_Id' +
    ' Left Outer Join Org O On O.Id = E.Org_Id' +
    ' Left Outer Join Org R On R.Id = D.Recipient_Id' +
    ' Left Outer Join Pl_Direction Pd On Pd.Id = D.Direction_Id' +
    ' Left Outer Join Org Od On Od.Id = Pd.Org_Id' +
    ' Left Outer Join Assign_Source A On A.Id = D.Assign_Source_Id' +
    ' Where D.Budget_Id = ? And D.DocumentClass_Id = 128 And D.DispStatus_Id = 10' +
    ' And D.Doc_Date >= ? And D.Doc_Date <= ?' +
    ' And (D.Title_Id Is Null Or D.Title_Id <> D.Id)' +
    IfThen(chkRepeat.Checked, '', ' And D.ExtFlag = 0') +
    IfThen(edtEstimate.Hint = '', '', ' And D.Estimate_Id In (' + edtEstimate.Hint + ')') +
    IfThen(edtRecipient.Hint = '', '', ' And D.Recipient_Id In (' + edtRecipient.Hint + ')') +
    IfThen(edtDirection.Hint = '', '', ' And D.Direction_Id In (' + edtDirection.Hint + ')') +
    IfThen(edtOperType.Hint = '', '', ' And D.OperType_Id In (' + edtOperType.Hint + ')') +
    IfThen(edtAssignSource.Hint = '', '', ' And D.Assign_Source_Id In (' + edtAssignSource.Hint + ')') +
    IfThen(cbxWoCommit.Text = 'Да', ' And D.WoCommitChanges = 1',
      IfThen(cbxWoCommit.Text = 'Нет', ' And D.WoCommitChanges = 0', )) +
    ' Order By D.Document_Id, D.Estimate_Id, D.Recipient_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') + Xml.getAttribute('EST_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('REMARK', Xml.getAttribute('REMARK'));
      Head.setAttribute('DESCRIPTION', Xml.getAttribute('DESCRIPTION'));
      Head.setAttribute('EST_ID', Xml.getAttribute('EST_ID'));
      Head.setAttribute('EST_CAPTION', Xml.getAttribute('EST_CAPTION'));
      Head.setAttribute('EST_FSR_ID', Xml.getAttribute('EST_FSR_ID'));
      Head.setAttribute('EST_ORG_ID', Xml.getAttribute('EST_ORG_ID'));
      Head.setAttribute('EST_KIND_ID', Xml.getAttribute('EST_KIND_ID'));
      Head.setAttribute('EST_KPP', Xml.getAttribute('EST_KPP'));
      Head.setAttribute('EST_INN', Xml.getAttribute('EST_INN'));
      Head.setAttribute('ASSIGN_SOURCE_CODE', Xml.getAttribute('ASSIGN_SOURCE_CODE'));
    end;
    Line := Head.appendChild(Data.ownerDocument.createElement('LINE'));
    Line.setAttribute('ID', Xml.getAttribute('ID'));
    Line.setAttribute('LINE_NUMBER', IntToStr(Head.childNodes.length));
    Line.setAttribute('EST_CAPTION', Xml.getAttribute('EST_CAPTION'));
    Line.setAttribute('FSR_ID', Xml.getAttribute('FSR_ID'));
    Line.setAttribute('INN', Xml.getAttribute('INN'));
    Line.setAttribute('KADMR_CODE', Xml.getAttribute('KADMR_CODE'));
    Line.setAttribute('KCSR_CODE', Xml.getAttribute('KCSR_CODE'));
    Line.setAttribute('KDE_CODE', Xml.getAttribute('KDE_CODE'));
    Line.setAttribute('KDF_CODE', Xml.getAttribute('KDF_CODE'));
    Line.setAttribute('KDR_CODE', Xml.getAttribute('KDR_CODE'));
    Line.setAttribute('KESR_CODE', Xml.getAttribute('KESR_CODE'));
    Line.setAttribute('KFSR_CODE', Xml.getAttribute('KFSR_CODE'));
    Line.setAttribute('KPP', Xml.getAttribute('KPP'));
    Line.setAttribute('KVR_CODE', Xml.getAttribute('KVR_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'));
    Line.setAttribute('Q1_AMT', Xml.getAttribute('Q1_AMT'));
    Line.setAttribute('Q2_AMT', Xml.getAttribute('Q2_AMT'));
    Line.setAttribute('Q3_AMT', Xml.getAttribute('Q3_AMT'));
    Line.setAttribute('Q4_AMT', Xml.getAttribute('Q4_AMT'));
    Line.setAttribute('RECIPIENT_CAPTION', Xml.getAttribute('RECIPIENT_CAPTION'));
    Line.setAttribute('RECIPIENT_ID', Xml.getAttribute('RECIPIENT_ID'));
    Line.setAttribute('PURPOSEFULGRANT_ID', Xml.getAttribute('PURPOSEFULGRANT_ID'));
    getFlagAssLim(Xml);
    if FlagLim and not FlagAss then
    begin
      Line.setAttribute('YEAR_AMT', Xml.getAttribute('LIMIT_AMT1'));
      Line.setAttribute('AMOUNT2YEAR', Xml.getAttribute('LIMIT_AMT2'));
      Line.setAttribute('AMOUNT3YEAR', Xml.getAttribute('LIMIT_AMT3'));
    end
    else
    begin
      Line.setAttribute('YEAR_AMT', Xml.getAttribute('ASSIGN_AMT1'));
      Line.setAttribute('AMOUNT2YEAR', Xml.getAttribute('ASSIGN_AMT2'));
      Line.setAttribute('AMOUNT3YEAR', Xml.getAttribute('ASSIGN_AMT3'));
    end;
    if (StrToCurrDef(Line.getAttribute('YEAR_AMT'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('AMOUNT2YEAR'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('AMOUNT3YEAR'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('Q1_AMT'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('Q2_AMT'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('Q3_AMT'), 0) = 0) and
      (StrToCurrDef(Line.getAttribute('Q4_AMT'), 0) = 0) then
      Head.removeChild(Line);
    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('EXPCHANGEASSIGNMENT', Data.GetElementsByTagName('LINE'));
  Form.SimpleText := 'Сохранение данных в файл ...';
  Data := Data.ownerDocument;
  Data.documentElement.setAttribute('SP_VER_CAPTION', 'Версия межбюджета (тест)');
  Data.documentElement.setAttribute('ONDATE', FormatDateTime('yyyy-mm-dd', Now));
  FileOut := GetUniqueFileName(edtDir.Text + 'ExpChange.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 OnExpChange}

begin
end.
