привет, оптимизируй процедуру procedure TFrameCustomDoc.Create_702(id_md: string);
var
  xml_root, xml_root1: IXMLNode;
  result: TXMLDocument;
  MyGUID: TGUID;
  s: string;
  tq, tq2: TIBQuery;
  f: double;
begin
  CreateGUID(MyGUID);
  result := TXMLDocument.Create(frmManagerXP2);
  result.Active := True;
  result.Version := '1.0';
  result.Options := result.Options + [doNodeAutoIndent];
  xml_root := result.AddChild('documents');
  xml_root.SetAttributeNS('xmlns:xsi', '',
    'http://www.w3.org/2001/XMLSchema-instance');
  xml_root.SetAttributeNS('version', '', dm.MARK_VERSION);
  s := GUIDToString(MyGUID);
  s := AnsiLowerCase(Copy(s, 2, Length(s) - 2));
  xml_root.SetAttributeNS('session_ui', '', s);
  xml_root1 := xml_root.AddChild('posting');
  xml_root1.SetAttributeNS('action_id', '', '702');

  with xml_root1 do
    begin
      AddChild('subject_id').Text := dm.MngrWindows.frmMarkInfo.GetCurrentMD;
      AddChild('shipper_id').Text := id_md;

      tq2 := dm.TempQuery;
      tq2.SQL.Text :=
        'select d.docnum,d.docdate,a.inn from docs d left join agents a on a.id=d.agent_id where d.id=:doc_id';
      tq2.ParamByName('doc_id').AsInteger := DocMainInfo.doc_id;
      tq2.Active := true;
      AddChild('operation_date').Text :=
        FormatDateTime('yyyy-mm-dd"T"hh:nn:ss' + GetTimeZone, Now);
      AddChild('doc_num').Text := tq2.fieldByNAme('docnum').AsString;
      AddChild('doc_date').Text := FormatDateTime('dd.mm.yyyy',
        tq2.fieldByName('docdate').AsVariant);
      AddChild('receive_type').Text := '1';
      AddChild('contract_type').Text := '1';
      AddChild('source').Text := '1';

      with AddChild('order_details') do
        begin
          tq2.Active := False;
          tq2.SQL.text :=
            'select DD.PRICE_O, MD.MARK_DATA, DD.SUM_NDSO, DD.QUANT,0 ISSSCC  from DOC_DETAIL_ACTIVE DD join MARK_DETAIL MD on MD.DDA_ID = DD.ID where MD.DOC_ID = :DOC_ID and MD.SSCC is null and mdlp_status in(1,2)' +
            ' union all select DD.PRICE_O, MD.sscc, DD.SUM_NDSO, DD.QUANT,1 ISSSCC  from DOC_DETAIL_ACTIVE DD join MARK_DETAIL MD on MD.DDA_ID = DD.ID where MD.DOC_ID = :DOC_ID and MD.SSCC ' +
            ' is not null and mdlp_status in(1,2) group by DD.PRICE_O, MD.sscc, DD.SUM_NDSO, DD.QUANT';
          tq2.ParamByName('doc_id').AsInteger := DocMainInfo.doc_id;
          tq2.Active := True;
          //Для работы с SGTIN
          while not tq2.Eof do
            with addchild('union') do
              begin
                if tq2.fieldByName('issscc').AsInteger = 0 then
                  begin
                    addchild('sgtin').Text :=
                      tq2.fieldByname('mark_data').AsString;
                    f := RoundTo(tq2.fieldByname('price_o').asFloat, -2);

                    addchild('cost').Text := StringReplace(floattostr(f), ',',
                      '.',
                      [rfReplaceAll]);
                    //параметр налогообложения
                    f := RoundTo(tq2.fieldByname('sum_ndso').asFloat /
                      tq2.fieldByname('quant').asFloat, -2);
                    addchild('vat_value').Text := StringReplace(floattostr(f),
                      ',',
                      '.', [rfReplaceAll]);
                  end;
                if tq2.fieldByName('issscc').AsInteger = 1 then
                  begin
                    with addchild('sscc_detail') do
                      begin
                        addchild('sscc').Text :=
                          tq2.fieldByname('mark_data').AsString;
                      end;

                    f := RoundTo(tq2.fieldByname('price_o').asFloat, -2);
                    addchild('cost').Text := StringReplace(floattostr(f), ',',
                      '.', [rfReplaceAll]);
                    //параметр налогообложения
                    f := RoundTo(tq2.fieldByname('sum_ndso').asFloat /
                      tq2.fieldByname('quant').asFloat, -2);
                    addchild('vat_value').Text := StringReplace(floattostr(f),
                      ',',
                      '.', [rfReplaceAll]);
                  end;
                tq2.Next;
              end;
        end;
    end;
  result.SaveToFile('702.xml');
end;