unit U_JwlKcWlHelp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxTextEdit, cxGridLevel,strUtils,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ToolWin,
  ImgList, DBClient, ADODB;
type
   PMyRec = ^TMyRec;
   TMyRec = record
   wbcode: string;
   Tvtem_str: string;
end;
type
  TfrmJwlKcWlHelp = class(TForm)
    ToolBar1: TToolBar;
    Tok: TToolButton;
    Tclose: TToolButton;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    edttj: TEdit;
    edtSpec: TEdit;
    CheckBox1: TCheckBox;
    dbGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1mnumber1: TcxGridDBColumn;
    tv1mname1: TcxGridDBColumn;
    tv1model1: TcxGridDBColumn;
    tv1mareaname1: TcxGridDBColumn;
    tv1batchnum1: TcxGridDBColumn;
    tv1caizhi1: TcxGridDBColumn;
    tv1factoryName: TcxGridDBColumn;
    tv1endquan1: TcxGridDBColumn;
    tv1primaryunitname1: TcxGridDBColumn;
    dbGrid1Level1: TcxGridLevel;
    GroupBox1: TGroupBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2mnumber1: TcxGridDBColumn;
    Tv2mname1: TcxGridDBColumn;
    Tv2model1: TcxGridDBColumn;
    Tv2mareaname1: TcxGridDBColumn;
    Tv2batchnum1: TcxGridDBColumn;
    Tv2caizhi1: TcxGridDBColumn;
    Tv2factoryName: TcxGridDBColumn;
    Tv2primaryunitname1: TcxGridDBColumn;
    Tv2endquan1: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ImageList24: TImageList;
    DataSource1: TDataSource;
    ADOQueryHelp: TADOQuery;
    CDS_sel: TClientDataSet;
    DataSourceSel: TDataSource;
    ADOConnection1: TADOConnection;
    Label3: TLabel;
    edt_zjm: TEdit;
    TreeView1: TTreeView;
    ADOQueryTmp: TADOQuery;
    ADOQueryChild: TADOQuery;
    procedure TokClick(Sender: TObject);
    procedure TcloseClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edttjChange(Sender: TObject);
  private
    procedure DoQuery();
    procedure DoFilter();
    procedure AddSelRow();
    procedure InitTree();
    procedure AddChildNode(parentNode:TtreeNode;mNo:string);
  public
    FStoreCode:string;
    fIsRhl:Boolean;
  end;

var
  frmJwlKcWlHelp: TfrmJwlKcWlHelp;

implementation
uses
  U_formPas,U_global;
{$R *.dfm}
//////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////
procedure TfrmJwlKcWlHelp.DoQuery();
begin
  try
    adoqueryHelp.DisableControls ;
    with adoqueryHelp do
    begin
      close;
      sql.Clear;
      sql.Add('select distinct A.factory,A.factoryName,B.mNumber,B.mName,');
      sql.Add(''''' as BatchNum,C.zjm,'); 
      sql.Add('B.Model,B.primaryUnit,B.primaryUnitName'); 
      sql.Add('from kf_nowQuan A');
      sql.Add('inner join gy_material B on A.mNumber=B.mNumber');
      if fIsRhl then
        sql.Add('inner join sc_dyeInfo C on A.mNumber=C.dyeCode');
      if trim(FStoreCode)<>'' then
        sql.Add('where A.whcode='+quotedStr(trim(FStoreCode)));
      if not CheckBox1.Checked then
        sql.Add('and A.endQuan>0');
      //sql.Add('order by A.Mnumber');
      open;
    end;
    CreateCDS(adoqueryHelp,cds_sel);
  finally
    adoqueryHelp.EnableControls ;
  end;
end;

////////////////////////////////////////////////////
  //函数：过滤数据
////////////////////////////////////////////////////
procedure TfrmJwlKcWlHelp.DoFilter();
var
  filterStr:string;
begin
  filterStr:='';
  //
  if trim(edttj.Text) <>'' then
   filterStr:=filterStr+' and mName like '+quotedStr('%'+trim(edttj.Text)+'%');
  //规格
  if trim(edtSpec.Text)<>'' then
    filterStr:=filterStr+' and model like '+quotedStr('%'+trim(edtSpec.Text)+'%');
  //xjm
  if trim(edt_zjm.Text)<>'' then
    filterStr:=filterStr+' and zjm like '+quotedStr('%'+trim(edt_zjm.Text)+'%');

  try
    ADOQueryHelp.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryHelp.Filtered:=false;
      ADOQueryHelp.EnableControls;
      exit;
    end;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with ADOQueryHelp do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
  finally
    ADOQueryHelp.EnableControls;
  end;
end;
/////////////////////////////////////////////////////
  //函数功能：增加选择的记录
////////////////////////////////////////////////////
procedure TfrmJwlKcWlHelp.AddSelRow();
var
  i:integer;
begin
  if CDS_Sel.Locate('mNumber;factory',VarArrayOf([trim(ADOQueryHelp.fieldbyname('mNumber').AsString),
                                                     // trim(ADOQueryHelp.fieldbyname('BatchNum').AsString),
                                                      trim(ADOQueryHelp.fieldbyname('factory').AsString)]),
                                                      []) then
  begin
    application.MessageBox(pchar('已存在同货区批次的【'+trim(ADOQueryHelp.fieldbyname('mname').AsString)+'】物料记录!'),'提示信息',0);
    exit;
  end;

  with CDS_Sel do
  begin
    CDS_Sel.append;
    for i:=0 to ADOQueryHelp.FieldCount-1 do
    begin
      fields[i].Value:=ADOQueryHelp.Fields[i].Value;
    end;
    CDS_Sel.Post;
  end;

end;
procedure TfrmJwlKcWlHelp.TokClick(Sender: TObject);
begin
  if adoqueryHelp.IsEmpty then
    ModalResult:=-1
  else
    modalResult:=1;
end;

procedure TfrmJwlKcWlHelp.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmJwlKcWlHelp.tv1DblClick(Sender: TObject);
begin
  AddSelRow();
end;

procedure TfrmJwlKcWlHelp.Tv2DblClick(Sender: TObject);
begin
  if CDS_Sel.IsEmpty then exit;
  CDS_sel.Delete;
end;

procedure TfrmJwlKcWlHelp.FormCreate(Sender: TObject);
begin
  groupBox1.Align :=alClient;
  CDS_Sel.Close;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
end;

procedure TfrmJwlKcWlHelp.FormShow(Sender: TObject);
begin
  DoQuery();
  edttj.SetFocus ;
end;

procedure TfrmJwlKcWlHelp.edttjChange(Sender: TObject);
begin
  Dofilter();
end;

procedure TfrmJwlKcWlHelp.InitTree();
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 := '全部';
       with TreeView1 do
  	   begin
         Items.Clear;
         CNode := Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
         CNode.ImageIndex := 1;
         CNode.SelectedIndex := 2;
  	   end;

       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.clear;
         sql.Add('select code,name from xc_code');
         sql.Add('where flag = ''DYETYPE'' AND valid = ''Y''');
         sql.Add('order by code');
         Open;
         if recordcount = 0 then
         begin
           application.MessageBox('对不起,基础表里未定义染化料基础分类:DYETYPE!','提示信息',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 CLASSFLAG=''DYECLASS'' and  valid=''Y''');
       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].Expand(true);
     //InitGrid();
   except
       ;
   end;

end;
//////////////////////////////////////////////////////////////////
  //函数功能：增加子节点
//////////////////////////////////////////////////////////////////
procedure TfrmJwlKcWlHelp.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;


end.
