unit U_custHelpPS;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxPC, cxControls, ImgList, ComCtrls, ToolWin, StdCtrls, ExtCtrls,
  U_LeftHelptree, DB, ADODB, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses,
  cxGridCustomView, cxGrid, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,
  cxTLData;
type
PMyRec = ^TMyRec;
 TMyRec = record
    wbcode: string;
    Tvtem_str: string;
end;
type
  TfrmCustHelpPS = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ImageList24: TImageList;
    ADOQueryTmp: TADOQuery;
    ADOConnection1: TADOConnection;
    ADOQueryHelp: TADOQuery;
    DS_Help: TDataSource;
    tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    tv1customNo: TcxGridDBColumn;
    tv1shortName: TcxGridDBColumn;
    ToolBar1: TToolBar;
    BtnOk: TToolButton;
    BtnCancel: TToolButton;
    tv1customName: TcxGridDBColumn;
    DataSource1: TDataSource;
    ADOQueryChild: TADOQuery;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
  private
    LoadFlag:Boolean;
    fSqlStr:string;
    procedure InitTree();
    procedure InitGrid(mSql:string);
  public
    fValidCustFlag:Boolean;   //是否显示无效客户
    fDispCustomName:Boolean;  //是否显示全称
  end;

var
  frmCustHelpPS: TfrmCustHelpPS;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmCustHelpPS.FormCreate(Sender: TObject);
begin
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;

  fSqlStr:='select a.CustomNo,a.ShortName,a.CustomName,a.CustomNameEn,'+
           'a.Contactor,ywyCode from V_BC_customer10 A where 1=1 ';
  InitTree();
  cxGrid1.Align :=alClient;
  InitGrid(fSqlStr);
end;


procedure TfrmCustHelpPS.BtnCancelClick(Sender: TObject);
begin
  close;
end;
procedure TfrmCustHelpPS.InitTree();
var
  fsj:String;
begin
  fsj:='select frameNo,frameName,TopFrameNo='''''+
       ' from YC_Frame A '+
       ' where exists(select Code from YC_Stuff X where X.dept=A.frameNo and X.ywyFlag=''Y'' and valid=''Y'')'+
       ' union all '+
       ' select frameNo=Code,frameName=name,TopFrameNo=dept '+
       ' from YC_Stuff A  '+
       ' where A.YWYFlag=''Y'' and A.Valid=''Y'' ';

  with ADOQueryChild do
  begin
    Close;
    sql.Clear;
    sql.Add(fsj);
    Open;
  end;
  cxDBTreeList1.FullExpand;
end;

procedure TfrmCustHelpPS.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;
procedure TfrmCustHelpPS.InitGrid(mSql:string);
begin
  try
    with ADOQueryHelp do
    begin
      close;
      sql.Clear ;
      sql.Add(mSql);
      if gIsYwy then
        sql.Add('and A.contactor='+quotedStr(guserName));
      if fValidCustFlag then
         sql.Add('and A.valid=''Y''');
      sql.Add('order by CustomNoId');
      Open;
    end;
  finally
  end;
end;
procedure TfrmCustHelpPS.Edit1Change(Sender: TObject);
var
  TmpSql:string;
begin
   TmpSql := 'SELECT CustomNo,ShortName,CustomName,a.Contactor,ywyCode  FROM V_BC_Customer10 A'
              + ' where (A.CustomName Like '+quotedStr('%'+trim(edit1.Text)+'%')+
                ' or A.ShortName Like '+quotedStr('%'+trim(edit1.Text)+'%')+
                ' or A.CustomNo Like '+quotedStr('%'+trim(edit1.Text)+'%')+     ')';
    InitGrid(TmpSql);
end;

procedure TfrmCustHelpPS.tv1DblClick(Sender: TObject);
begin
  BtnOk.Click ;
end;

procedure TfrmCustHelpPS.FormShow(Sender: TObject);
begin
  tv1customName.Visible := fDispCustomName;
end;

procedure TfrmCustHelpPS.cxDBTreeList1Click(Sender: TObject);
var
  fSqlStr:string;
begin
   if ADOQueryHelp.Active then
   begin
     if Trim(ADOQueryChild.fieldbyname('frameno').AsString)<>'' then
     begin
       fSqlStr:=' ywyCode='+quotedStr(Trim(ADOQueryChild.fieldbyname('frameno').AsString));
       try
         ADOQueryHelp.DisableControls;
         with ADOQueryHelp do
         begin
           if Trim(fSqlStr)='' then
           begin
             Filtered:=False;
           end else
           begin
             Filtered:=False;
             Filter:=fSqlStr;
             Filtered:=True;
           end;
         end;
       finally
         ADOQueryHelp.EnableControls;
       end;
     end;
   end;
end;

end.
