unit U_RSFormPas;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,DBGrids, DB, cxDBData,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, ADODB,StrUtils,
  Midas,cxGridCustomTableView, cxGridTableView, cxGridDBTableView,cxTimeEdit,
  cxTreeView,  cxGrid,cxDBLookupComboBox,cxCalendar, cxCurrencyEdit,cxExportGrid4Link,
  ExtCtrls, Buttons,DBClient,FTComboBox,cxDropDownEdit,RM_GridReport;
type
    TA = class(TComponent)
    public
      S:string;
end;
procedure InitTree(ADOQueryTemp:TADOQuery;TreeView1:TcxTreeView);
procedure InitChildTree(ADOQueryTemp:TADOQuery;TreeView1:TcxTreeView);
function GetRoot(node:TTreeNode):TTreeNode;
function TreeFiltered(ADOQueryTemp:TADOQuery;TreeStr:string):string;
function TreeSelect(TreeStr:string;ADOQueryTemp:TADOQuery):string;
procedure Dofilter(ADOQueryMain:TADOQuery;TreeFilter:string);
procedure InitCombox(ADOQueryTemp: TADOQuery;FState:Integer;CB1:TComboBox;FFlag:string);
function  DelData(ADOQueryMain,ADOQueryCmd:TADOQuery;FStr:String):Boolean;
//procedure InitData(ADOQueryMain:TADOQuery);
//procedure KeyPress(Sender: TObject;Key: Char);
procedure InitData(ADOQueryMain:TADOQuery;mParent:TWinControl;FTag:Integer);
procedure Initcomponents(mParent:TWinControl);
function GetDate(FDate:TDateTime):TDateTime;
procedure GetDate10(FPanel:TWinControl);
procedure SetDataNull(mParent:TWinControl);
procedure Setsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
procedure SelMember(Selmem:TBtnEditA);
procedure Seldept(Selmem:TBtnEditA);
function GetTime(FTime:TDateTime):TTime;
procedure InitCDSData30(fromADO:TADOQuery;toCDS:TClientDataSet;
                         ProgressBar1:TProgressBar);
procedure InitFtComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           isClearOld:boolean
                           );
procedure InitComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure PrintRM(ADOQueryMain:TADOQuery;RMname:string;RM1:TRMGridReport);

implementation
uses U_global,U_StuffHelp,U_frameHelp;
///////////////////////////////////////////////////////
///// 函数功能：初始化一级树
//////////////////////////////////////////////////////
procedure InitTree(ADOQueryTemp:TADOQuery;TreeView1:TcxTreeView);
var
  aNode:TTreeNode;
  j:integer;
  kind:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Xc_CustCode where Flag=''PStatus''');
    Open;
  end;
  ADOQueryTemp.First;
  for j:=0 to ADOQueryTemp.RecordCount-1   do
  begin
    kind:='所有'+Trim(ADOQueryTemp.FieldByName('name').AsString)+'员工';
    aNode:=TreeView1.Items.AddChild(nil,kind) ;
    ADOQueryTemp.Next;
  end;
end;
///////////////////////////////////////////////
///函数功能：初始化子树
//////////////////////////////////////////////
procedure InitChildTree(ADOQueryTemp:TADOQuery;TreeView1:TcxTreeView);
var
  kind:string;
  Node:TTreeNode;
  i:integer;
begin
  if TreeView1.Selected.HasChildren then
  begin
    Exit;
  end;
  Node:=TreeView1.Selected;
  i:=0;
  while((Node<>nil)and(Node.Parent<>nil)) do
  begin
    Node:=Node.Parent;
    i:=i+1;
  end;
  if TreeView1.Selected.Parent<>nil then
  begin
    kind:=Trim(TreeView1.Selected.Text);
    with ADOQueryTemp do
    begin
      close;
      SQL.Clear;
      SQL.Add('select * from Yc_Frame where framename='''+kind+'''');
      Open;
      kind:=Trim(fieldbyname('frameno').AsString);
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Yc_Frame where frameLevel=:i');
    Parameters.ParamByName('i').Value:=i+1;
    if TreeView1.Selected.Parent<>nil then
      SQL.Add('and frameno like '+QuotedStr('%'+kind+'%'));
    Open;
  end;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      kind:='';
      kind:=fieldbyname('framename').AsString;
      node:=TreeView1.Items.AddChild(TreeView1.Selected,kind);
      Next;
    end;
  end;
end;
////////////////////////////////
//// 函数功能：获得根节点
///////////////////////////////
function GetRoot(node:TTreeNode):TTreeNode;
begin
  Result:=node;
  while((Result   <>   nil)   and   (Result.Parent   <>   nil))   do
    Result:=   Result.Parent;
end;
//////////////////////////////////////
/// 函数功能：汇总过滤条件进行过滤
/////////////////////////////////////
function TreeFiltered(ADOQueryTemp:TADOQuery;TreeStr:string):string;
var
  TreeFilter:string;
begin
  result:='';
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Yc_Frame where framename='''+TreeStr+'''');
    Open;
    TreeStr:=Trim(fieldbyname('frameNo').AsString);
  end;
  TreeFilter:=' and Dept like'+' '+'%'+TreeStr+'%';
  //TreeFilter:=' and Dept like'''+'%'+TreeStr+'%'+'''';
  result:=TreeFilter;
end;
function GetDate(FDate:TDateTime):TDateTime;
begin
  result:=StrToDate(FormatDateTime('yyyy-MM-dd',FDate));
end;
function GetTime(FTime:TDateTime):TTime;
begin
  result:=StrTotime(FormatDateTime('HH:mm:ss',FTime));
end;
/////////////////////////////
//函数功能：获得根节点
///////////////////////////
function TreeSelect(TreeStr:string;ADOQueryTemp:TADOQuery):string;
var
  TreeFilter:string;
begin
  result:='';
  TreeStr:=Copy(TreeStr,5,4);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Xc_CustCode where name='''+TreeStr+''' and flag=''PStatus''');
    Open;
    TreeStr:=Trim(fieldbyname('code').AsString);
  end;
  TreeFilter:=' PStatus='''+TreeStr+'''';
  result:=TreeFilter;
end;
////////////////////////////
/// 函数功能：过滤
///////////////////////////
procedure Dofilter(ADOQueryMain:TADOQuery;TreeFilter:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      if Trim(TreeFilter)='' then
      begin
        Filtered:=False;
        EnableControls;
      end else
      begin
        Filtered:=False;
        Filter:=TreeFilter;
        Filtered:=True;
      end;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;
/////////////////////////////////////////////
///函数功能：初始化combobox  【XC_CustCode】
////////////////////////////////////////////
procedure InitCombox(ADOQueryTemp: TADOQuery;FState:Integer;CB1:TComboBox;FFlag:string);
var
  A:TA;
begin
  CB1.Items.Clear;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' select * from XC_CustCode');
    SQL.Add(' where Flag='''+trim(FFlag)+''' ');
    SQL.Add(' And Valid=''Y''');
    SQL.Add(' order by orderno ');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox(PChar('定义XC_CustCode Flag='+FFlag),'提示',0);
      Exit;
    end;
    while not Eof do
    begin
      A:=TA.Create(nil);
      A.S:=trim(fieldByName('Code').AsString);
      if FState=0 then
      begin
        CB1.Items.AddObject((Trim(fieldbyname('name').AsString)),TObject(a));
        Next;
      end else
      begin
        CB1.Items.AddObject((Trim(fieldbyname('code').AsString)),TObject(a));
        Next;
      end;
    end;
  end;

end;

//////////////////////////////////////////////
//////函数功能：根据SQL语句删除数据
//////////////////////////////////////////////
function DelData(ADOQueryMain,ADOQueryCmd:TADOQuery;FStr:String):Boolean;
begin
  try
    result:=False;
    if ADOQueryMain.IsEmpty then Exit;
    if Application.MessageBox('确定要删除数据吗？','提示',MB_YESNO+MB_ICONSTOP)<>IDYES then
    begin
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add(FStr);
      ExecSQL;
    end;
    result:=True;
  except
    result:=False;
    Application.MessageBox('数据删除失败！','提示',0);
  end;
end;
//////////////////////////////////
///函数功能：初始化数据
//////////////////////////////////
procedure InitData(ADOQueryMain:TADOQuery;mParent:TWinControl;FTag:Integer);
var
  i:Integer;
begin
  with mParent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then Continue;
      if Controls[i].Tag=FTag then
      begin
        if (Controls[i] is TEdit) then
        begin
          if Controls[i].Name='name1' then
            TEdit(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname('name').AsString)
          else
          TEdit(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString);
        end else
        if (Controls[i] is TRichEdit) then
        begin
          TRichEdit(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString);
        end else
        if (Controls[i] is TFTComboBox) then
        begin
          TFTComboBox(Controls[i]).ItemIndex:=TFTComboBox(Controls[i]).IndexOfItem2(Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString));
        end else
        if (Controls[i] is TComboBox) then
        begin
          TComboBox(Controls[i]).ItemIndex:=TComboBox(Controls[i]).Items.IndexOf(Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString));
        end else
        if (Controls[i] is TDateTimePicker) then
        begin
          if Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString)<>'' then
          begin
            if TDateTimePicker(Controls[i]).ShowCheckbox then
              TDateTimePicker(Controls[i]).Checked:=True;
            TDateTimePicker(Controls[i]).DateTime:=ADOQueryMain.fieldbyname(Controls[i].Name).AsDateTime;
          end else
          begin
            if TDateTimePicker(Controls[i]).ShowCheckbox then
              TDateTimePicker(Controls[i]).Checked:=False;
          end;
        end else
        if (Controls[i] is TBtnEditA) then
        begin
          if Controls[i].Name='name1' then
            TBtnEditA(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname('name').AsString)
          else
          if Controls[i].Name='code' then
          begin
            TBtnEditA(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname('code').AsString);
            TBtnEditA(Controls[i]).TxtCode:=Trim(ADOQueryMain.fieldbyname('code').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode:=Trim(ADOQueryMain.fieldbyname(Controls[i].Name).AsString);
            TBtnEditA(Controls[i]).Text:=Trim(ADOQueryMain.fieldbyname(Controls[i].Name+'1').AsString);
          end;
        end;
      end;
    end;
  end;
end;
procedure SetDataNull(mParent:TWinControl);
var
  i:integer;
begin
  with mParent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      //if Controls[i].Tag=1 then
      //begin
        if Controls[i] is TLabel then Continue;
        if (Controls[i] is TEdit) then
        begin
          TEdit(Controls[i]).Text:='';
        end else
        if (Controls[i] is TRichEdit) then
        begin
          TRichEdit(Controls[i]).Text:='';
        end else
        if (Controls[i] is TComboBox) then
        begin
          TComboBox(Controls[i]).ItemIndex:=-1;
        end else
        if (Controls[i] is TDateTimePicker) then
        begin
          if TDateTimePicker(Controls[i]).Kind=dtkdate then
            TDateTimePicker(Controls[i]).DateTime:=GetDate(Now)
          else
            TDateTimePicker(Controls[i]).Time:=GetTime(Now);
        end else
        if (Controls[i] is TBtnEditA) then
        begin
          TBtnEditA(Controls[i]).Text:='';
          TBtnEditA(Controls[i]).TxtCode:='';
        end;
      //end;
    end;
  end;
end;
////////////////////////////////////
///函数功能：按回车跳到下一个控件
///////////////////////////////////
{procedure KeyPress(Sender: TObject;Key: Char);
begin
  if Key=#13 then
  begin
    FindNextControl(TWinControl(sender),True,True,False).SetFocus;
  end;
end;}
/////////////////////////////////////
/// 函数功能：初始化控件状态
////////////////////////////////////
procedure Initcomponents(mParent:TWinControl);
var
  i:Integer;
begin
  with mParent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then Continue;
      if (Controls[i] is TEdit) then
      begin
        TEdit(Controls[i]).Enabled:=False;
      end else
      if (Controls[i] is TRichEdit) then
      begin
        TRichEdit(Controls[i]).Enabled:=False;
      end else
      if (Controls[i] is TComboBox) then
      begin
        TComboBox(Controls[i]).Enabled:=False;
      end else
      if (Controls[i] is TDateTimePicker) then
      begin
        TDateTimePicker(Controls[i]).Enabled:=False;
      end else
      if (Controls[i] is TButton) then
      begin
        TButton(Controls[i]).Enabled:=False;
      end else
      if (Controls[i] is TToolButton) then
      begin
        TToolButton(Controls[i]).Visible:=False;
      end else
      if (Controls[i] is TBtnEditA) then
      begin
        TBtnEditA(Controls[i]).Enabled:=False;
      end;
    end;
  end;
end;
procedure GetDate10(FPanel:TWinControl);
var
  i:Integer;
begin
  with FPanel do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if not(Controls[i] is TLabel) then Continue;
      if (Controls[i] is TDateTimePicker) then
      begin
        if TDateTimePicker(Controls[i]).Kind=dtkdate then
        begin
          TDateTimePicker(Controls[i]).DateTime:=StrToDate(FormatDateTime('yyyy-MM-dd',now));
          if TDateTimePicker(Controls[i]).ShowCheckbox then
            TDateTimePicker(Controls[i]).Checked:=False;
        end
        else
          TDateTimePicker(Controls[i]).Time:=StrToTime(FormatDateTime('HH:mm:ss',now));
      end;
    end;
  end;
end;
 ///////////////////////////////////
 /////设置保存数据时字段赋值代码
 //////////////////////////////////
procedure Setsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
var
  i:Integer;
begin
  with Myparent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i].Tag=MyTag then
      begin
        if Controls[i] is TEdit then
        begin
          if Controls[i].Name='name1' then
            ADOQueryCmd.FieldByName('name').Value:=Trim(TEdit(Controls[i]).Text)
          else
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TEdit(Controls[i]).Text);
        end else
        if Controls[i] is TRichEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TFTComboBox then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TFTComboBox(Controls[i]).Item2);
        end else
        if Controls[i] is TComboBox then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TComboBox(Controls[i]).Text);
        end else
        if Controls[i] is TDateTimePicker then
        begin
          if TDateTimePicker(Controls[i]).ShowCheckbox then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              ADOQueryCmd.FieldByName(Controls[i].Name).Value:=GetDate(TDateTimePicker(Controls[i]).DateTime);
          end else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=GetDate(TDateTimePicker(Controls[i]).DateTime);
        end else
        if Controls[i] is TBtnEditA then
        begin
          if Controls[i].Name='name1' then
            ADOQueryCmd.FieldByName('name').Value:=Trim(TBtnEditA(Controls[i]).Text)
          else
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditA(Controls[i]).TxtCode);
        end;
      end;
    end;
  end;
end;

procedure SelMember(Selmem:TBtnEditA);
begin
  try
    frmStuffHelp:=TfrmStuffHelp.Create(Application);
    with frmStuffHelp do
    begin
      frmStuffHelp.Position:=poScreenCenter;
      if ShowModal=1 then
      begin
        Selmem.TxtCode:=Trim(ADOQueryHelp.fieldbyname('code').AsString);
        Selmem.Text:=Trim(ADOQueryHelp.fieldbyname('name').AsString);
      end;
    end;
  finally
    frmStuffHelp.Free;
  end;
end;
procedure Seldept(Selmem:TBtnEditA);
begin
  try
    frmFrameHelp:=TfrmFrameHelp.Create(Application);
    with frmFrameHelp do
    begin
      frmFrameHelp.Position:=poScreenCenter;
      if ShowModal=1 then
      begin
        Selmem.TxtCode:=Trim(ADOQueryHelp.fieldbyname('frameno').AsString);
        Selmem.Text:=Trim(ADOQueryHelp.fieldbyname('framename').AsString);
      end;
    end;
  finally
    frmFrameHelp.Free;
  end;
end;
////////////////////////
//增加进度条
///////////////////////
procedure InitCDSData30(fromADO:TADOQuery;toCDS:TClientDataSet;
                         ProgressBar1:TProgressBar);
var
  i:integer;
  k:integer;
  j:Integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.first;
  j:=1;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;
    ProgressBar1.Min:=0;

    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;
        ProgressBar1.Visible:=True;
        ProgressBar1.Max:=fromADO.FieldCount*10;
        ProgressBar1.Position:=j;
        fieldByName('flag').AsString :='1';
        fieldByName('index').value :=k;
        fieldByName('sel').value :=false;
        //Application.ProcessMessages;
        sleep(1);
        inc(k);
        Post;
      end;
      fromADO.Next;
      j:=j+1;
    end;
    ProgressBar1.Visible:=False;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
  end;
end;

procedure PrintRM(ADOQueryMain:TADOQuery;RMname:string;RM1:TRMGridReport);
var
  fPrintFile:string;
begin
    if ADOQueryMain.IsEmpty then Exit;
    fPrintFile := ExtractFilePath(Application.ExeName)+RMname ;
    begin
      if FileExists(fPrintFile) then
      begin
        RM1.Clear;
        RM1.LoadFromFile(fPrintFile);
        RM1.ShowReport;
      end
      else
      begin
        Application.MessageBox(PChar('没有定义打印格式文件！不能完成打印' + #13+ fPrintFile),'提示',0);
        Exit ;
      end ;
    end;
end;

//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_CustCode表中取定义数据
  //Boxtype:0; 带编号：1;
  //emptyFlag：是否默认为空
  //isClearOld:清除原有的项目
//////////////////////////////////////////////
procedure InitFtComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           isClearOld:boolean
                           );
var
  A:TA;
begin
  if isClearOld then
    cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Custcode');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('基础设置中未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('name').AsString));
      end
      else
      begin
        cb.AddItem2(trim(fieldByName('name').AsString),nil,trim(fieldByName('code').AsString));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;

//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_CustCode表中取定义数据
  //Boxtype:0; 带编号：1;
//////////////////////////////////////////////
procedure InitComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Custcode');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('name').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('code').AsString);
        cb.Items.AddObject(trim(fieldByName('name').AsString),TObject(a));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
end.

