unit U_SelPrint;

interface
uses
  Windows,Messages,SysUtils,Variants,Classes,Controls,Forms,DBGrids,
  Menus,DB,Dialogs,ComCtrls,StrUtils,DBClient,ComObj, OleServer, Excel2000,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, ADODB,
  cxDataStorage, cxEdit, cxDBData, cxTextEdit, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxControls, cxGridCustomView,
  cxClasses, cxGridLevel, cxGrid;
  procedure SelPrintData10(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;
                         FTitle:string;FLTitle:string;FRTile:string;
                         footerZ:string;footerL:string;footerR:string);

  procedure CreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
  procedure InitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
implementation
uses
  U_SelPrintField;
procedure CreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  mClientDataset.FieldDefs.Add('flag11',ftString,1);
  mClientDataset.FieldDefs.Add('index11',ftInteger,0);
  mClientDataset.FieldDefs.Add('sel11',ftBoolean,0);
  mClientDataset.FieldDefs.Add('DefNote11',ftString,10);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
///////////////////////////////////////////////////////////
  //ΣΠΡ‘ΤρΑΠ
//////////////////////////////////////////////////////////
procedure InitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.DisableControls;
  fromADO.first;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;

    while not fromADO.Eof do
    begin
      with toCDS do
      begin
        Append;
        for i:=0 to fromADO.FieldCount-1 do
        begin
          fields[i].value:=fromADO.Fields[i].Value ;
        end;
        fieldByName('flag11').AsString :='1';
        fieldByName('index11').value :=k;
        fieldByName('sel11').value :=false;
        inc(k);
        Post;
      end;
      fromADO.Next;
    end;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
    fromADO.EnableControls;
  end;
end;
procedure SelPrintData10(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;
                         FTitle:string;FLTitle:string;FRTile:string;
                         footerZ:string;footerL:string;footerR:string);
var
   i,j,k:Integer;
   fsj:string;
   FOrder,fcolumnName:string;
begin
 if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintField:=TfrmSelPrintField.Create(Application);
    with frmSelPrintField do
    begin
      with frmSelPrintField.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintField.IniName:=FTitle;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
            if FTv.Columns[i].SortOrder=soAscending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+'  ';
            end else
            if FTv.Columns[i].SortOrder=soDescending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+' DESC';
            end;
          end;
        end;
      end;
     // PrintDataSource.DataSet:=FAdoQry;
      FAdoQry.Open;
      FAdoQry.Sort:=FOrder;
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.LeftMemo.Add(FLTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.RightMemo.Add(FRTile);
      //frmSelPrintField.RMPrintDBGrid1.PageHeaderMsg.CenterMemo.Add(FTile);
      frmSelPrintField.RMPrintDBGrid1.PageFooterMsg.CenterMemo.Add(footerZ);
      frmSelPrintField.RMPrintDBGrid1.PageFooterMsg.LeftMemo.Add(footerL);
      frmSelPrintField.RMPrintDBGrid1.PageFooterMsg.RightMemo.Add(footerR);
      CreateCDS20(FAdoQry,frmSelPrintField.ClientDataSet1);
      InitCDSData20(FAdoQry,frmSelPrintField.ClientDataSet1);
      if FTv.OptionsView.Footer=True then
      begin
        if FTv.DataController.Summary.FooterSummaryItems.Count>0 then
        begin
          frmSelPrintField.ClientDataSet1.Append;
          frmSelPrintField.ClientDataSet1.FieldByName('index11').Value:=frmSelPrintField.ClientDataSet1.RecNo+1;
          frmSelPrintField.ClientDataSet1.FieldByName('DefNote11').Value:='***ALL***';
          frmSelPrintField.ClientDataSet1.Post;
          for i:=0 to FTv.ColumnCount-1 do
          begin
            if FTv.Columns[i].Visible=False then  Continue;
            fcolumnName:=FTv.Columns[i].Name;
            if FTv.Columns[i].Summary.FooterKind<>sknone then
            begin

              for k:=0 to FTv.DataController.Summary.FooterSummaryItems.Count-1 do
              begin
                if TcxGridDBTableSummaryItem(FTv.DataController.Summary.FooterSummaryItems.Items[k]).Column.Name=fcolumnName then
                begin
                  frmSelPrintField.ClientDataSet1.edit;
                  frmSelPrintField.ClientDataSet1.FieldByName(FTv.Columns[i].DataBinding.FieldName).Value:=
                                                             FTv.DataController.Summary.FooterSummaryValues[k];
                  frmSelPrintField.ClientDataSet1.FieldByName('flag11').Value:='Y';
                  frmSelPrintField.ClientDataSet1.Post;
                end;
              end;  
            end;
          end;
        end;
      end;
      if ShowModal=1 then
      begin

      end;
    end;
    //frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTile);
    frmSelPrintField.Free;
  except

  end;

end;
end.
