unit U_custHelp;

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;
type
PMyRec = ^TMyRec;
 TMyRec = record
    wbcode: string;
    Tvtem_str: string;
end;
type
  TfrmCustHelp = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ImageList24: TImageList;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    TreeView1: TTreeView;
    frameLeftHelpTree1: TframeLeftHelpTree;
    ADOQueryTmp: TADOQuery;
    ADOConnection1: TADOConnection;
    ADOQueryHelp: TADOQuery;
    DS_Help: TDataSource;
    tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    tv1customNo: TcxGridDBColumn;
    tv1shortName: TcxGridDBColumn;
    ADOQueryChild: TADOQuery;
    ToolBar1: TToolBar;
    BtnOk: TToolButton;
    BtnCancel: TToolButton;
    tv1customName: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure frameLeftHelpTree1tt1CellClick(
      Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TreeView1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure frameLeftHelpTree1tt1FocusedRecordChanged(
      Sender: TcxCustomGridTableView; APrevFocusedRecord,
      AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure FormShow(Sender: TObject);
  private
    LoadFlag:Boolean;
    fSqlStr:string;
    procedure inti_nodeA();
    procedure InitGrid(mSql:string);
    procedure AddChildNode(parentNode:TtreeNode;mNo:string);
  public
    fValidCustFlag:Boolean;   //是否显示无效客户
    fDispCustomName:Boolean;  //是否显示全称
  end;

var
  frmCustHelp: TfrmCustHelp;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmCustHelp.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_customer A where 1=1 ';
  try
    LoadFlag:=false;
    frameLeftHelpTree1.InitLTree(ADOQueryTmp,'','USER','YWY','',false);
  finally
    LoadFlag:=true;
  end;
  if gIsYwy then
    cxPageControl1.Tabs[0].Visible:=false;
  inti_nodeA();
  cxGrid1.Align :=alClient;
  InitGrid(fSqlStr);
end;

procedure TfrmCustHelp.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TfrmCustHelp.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;
//////////////////////////////////////////////////////////////
  //函数功能：初始化客户分类树
  // CusType：分类编号；  CusTypeName：分类名称
//////////////////////////////////////////////////////////////
procedure TfrmCustHelp.inti_nodeA();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
   NodeXX,CNode :TTreeNode;
   array_Node:array of TTreeNOde;
   i:integer;
begin
   try
  	   New(MyRecPtr);
  	   MyRecPtr^.wbcode := 'ALL';
  	   MyRecPtr^.Tvtem_str := '全部客户';
       //treetop := 'ALL';
       with TreeView1 do
  	   begin
         Items.Clear;
         CNode := Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
         CNode.ImageIndex := 1;
         CNode.SelectedIndex := 2;
  	   end;
  	   with TreeView1,ADOQueryTmp do
  	   begin
         close;
         sql.Add('select code,name from xc_code');
         sql.Add('where flag = ''CUSTYPE'' AND valid = ''Y''');
         //sql.Add('and Name not like ''%业务员%''');
         sql.Add('order by code');
         Open;
         
         if recordcount = 0 then
         begin
           application.MessageBox('对不起,基础表里未定义产品基础分类:CUSTYPE!','提示信息',0);
           exit;
         end;

         setLength(array_Node, RecordCount);
         i:=0;
         while not Eof do
         begin
  			   New(MyRecPtr);
  			   MyRecPtr^.wbcode := trim(fieldbyname('code').Asstring);
           MyRecPtr^.Tvtem_str := trim(fieldbyname('name').Asstring);
           NodeXX := Items.AddChildObject(TreeView1.Items[0], MyRecPtr^.Tvtem_str, MyRecPtr);
           NodeXX.ImageIndex := 1;
           NodeXX.SelectedIndex := 2;
           array_Node[i]:=NodeXX;
           INC(i);
           next;
         end;
         Close;
      end;
     ///////////////////////////////////////
     with ADOQueryChild do
     begin
       close;
       filtered:=false;
       sql.Clear ;
       sql.Add('select * from xc_code');
       sql.Add('where Note=''CUST'' and valid=''Y''');
       sql.Add('and Name not like ''%业务员%''');
       Open;  
     end;
     for i:=0 to Length(array_Node)-1 do
     begin
       //创建子配方
       AddChildNode(array_Node[i],PMyRec(array_Node[i].Data)^.wbcode);
     end;
     TreeView1.Items[0].Selected := True;
     TreeView1.Items[0].Expanded:=true;
   except
       ;
   end;

end;
//////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////
procedure TfrmCustHelp.InitGrid(mSql:string);
begin
  try
    with ADOQueryHelp do
    begin
      close;
      sql.Clear ;
      sql.Add(mSql);
      if cxPageControl1.ActivePageIndex=0 then
      begin
        if trim(frameLeftHelpTree1.fLeftSelCaption)<>'' then
          sql.Add('and A.contactor='+quotedStr(frameLeftHelpTree1.fLeftSelCaption));
      end;
      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 TfrmCustHelp.frameLeftHelpTree1tt1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin

end;
//////////////////////////////////////////////////////////////////
  //函数功能：增加子节点
//////////////////////////////////////////////////////////////////
procedure TfrmCustHelp.AddChildNode(parentNode:TtreeNode;mNo:string);
var
  NodeInfo: PMyRec;
  mNode:TtreeNode;
  i:integer;
  array_Node:array of TTreeNode;
begin
  with ADOQueryChild do
  begin
    filtered:=false;
    filter:='flag ='''+PMyRec(parentNode.Data)^.wbcode+'''';
    filtered:=true;
    Open;

    if RecordCount<=0 then exit;
    //////////////////////
    setLength(array_Node, RecordCount);
    i:=0;
    while not Eof do
    begin
      with treeView1 do
      begin
        New(NodeInfo);

        //编号
        NodeInfo^.wbcode:=trim(fieldByName('code').AsString);
        //名称
        NodeInfo^.Tvtem_str :=fieldByName('Name').AsString
                              +'('+trim(fieldByName('code').AsString)+')';
        mNode:=Items.AddChildObject(parentNode, NodeInfo^.Tvtem_str, NodeInfo);
        array_Node[i]:=mNode;
        INC(i);
      end;
      Next;
    end;

     for i:=0 to Length(array_Node)-1 do
     begin
       //创建子
       AddChildNode(array_Node[i],PMyRec(array_Node[i].Data)^.wbcode);
     end;
  end;

end;

procedure TfrmCustHelp.TreeView1Click(Sender: TObject);
var
   NodeXX :TTreeNode;
   TmpDept:string;
begin
   //if not LoadFlag then exit;
   NodeXX := TreeView1.Selected;
   TmpDept := trim(PMyRec(NodeXX.Data).wbcode);
   //treetop := TmpDept;
   fSqlStr   := 'select a.CustomNo,a.ShortName,a.CustomName,a.CustomNameEn,a.Contactor,ywyCode ' ;
   if (TmpDept <> 'ALL') then  //不是全部客户
     begin
       ADOQueryTmp.Close;
       ADOQueryTmp.SQL.Clear;
       ADOQueryTmp.SQL.Add('select flag from xc_code where code = '''+TmpDept+''' and valid = ''Y'' ' );
       ADOQueryTmp.Open;
       if trim(ADOQueryTmp.fieldbyname('flag').asstring) = 'CUSTYPE' then  //主类产品
          begin
            fSqlStr := fSqlStr + ' from V_BC_customer A ,md_class_type B '+
                           'where ((b.typeno  = '''+TmpDept+''' ) or (b.class  = '''+TmpDept+''')) and  '+
                           '      a.customno = b.datano ';
           InitGrid(fSqlStr);
          end
       else   //不是主类产品
          begin
            fSqlStr := fSqlStr + ' from V_BC_customer A ,md_class_type B '+
                         ' where b.typeno =  '''+TmpDept+''' and  '+
                         '      a.customno = b.datano ';
            InitGrid(fSqlStr);
          end;
     end
   else
     begin
       fSqlStr:='select a.CustomNo,a.ShortName,a.CustomName,a.CustomNameEn,a.Contactor,ywyCode from V_BC_customer A where 1=1 ';
       InitGrid(fSqlStr);  //全部产品
     end;
end;

procedure TfrmCustHelp.Edit1Change(Sender: TObject);
var
  TmpSql:string;
begin
   TmpSql := 'SELECT CustomNo,ShortName,CustomName,a.Contactor,ywyCode  FROM V_BC_Customer 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 TfrmCustHelp.tv1DblClick(Sender: TObject);
begin
  BtnOk.Click ;
end;

procedure TfrmCustHelp.frameLeftHelpTree1tt1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if not LoadFlag then exit;
  frameLeftHelpTree1.tt1FocusedRecordChanged(Sender,
  APrevFocusedRecord, AFocusedRecord, ANewItemRecordFocusingChanged);
  fSqlStr:='select a.CustomNo,a.ShortName,a.CustomName,a.CustomNameEn,a.Contactor,ywyCode from V_BC_customer A where 1=1 ';
  InitGrid(fSqlStr);
end;

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

end.
