unit U_PbProductHelp10;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, Grids, DBGrids, ExtCtrls, ComCtrls, ImgList, ToolWin,
  StdCtrls, cxControls, cxPC,StrUtils, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxSplitter;
  
  type
   PMyRec = ^TMyRec;
   TMyRec = record
   wbcode: string;
   Tvtem_str: string;
  end;
type
  TfrmPbProductHelp10 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    ImageList24: TImageList;
    DataSource1: TDataSource;
    ADOQueryTmp: TADOQuery;
    ADOQueryHelp: TADOQuery;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxTabControl1: TcxTabControl;
    Edit2: TEdit;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1p_Code: TcxGridDBColumn;
    tv1P_chnName: TcxGridDBColumn;
    tv1P_engName: TcxGridDBColumn;
    tv1P_spec: TcxGridDBColumn;
    tv1P_gram: TcxGridDBColumn;
    tv1P_Breadth: TcxGridDBColumn;
    tv1P_upBreadth: TcxGridDBColumn;
    tv1P_typeName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    tv1Quantity: TcxGridDBColumn;
    tv1P_Breadthp: TcxGridDBColumn;
    tv1P_upBreadthp: TcxGridDBColumn;
    tv1rollUnit: TcxGridDBColumn;
    tv1RollNum: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TreeView1: TTreeView;
    cxSplitter1: TcxSplitter;
    ADOQueryChild: TADOQuery;
    AD_Rhl_Fllb: TADODataSet;
    ADOConnection1: TADOConnection;
    Label2: TLabel;
    edt_spec: TEdit;
    tv1store: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_clFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    tv1Packs: TcxGridDBColumn;
    tv1packUnit: TcxGridDBColumn;
    tv1ShortName: TcxGridDBColumn;
    tv1UnitName: TcxGridDBColumn;
    tv1P_Color: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    procedure BtnOkClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private

    fClassType:string;
    LoadFlag:boolean;
    SortID: Integer;
    SortCaption, SortField, SortOrder: String;
    procedure DoQuery();
    procedure Dofilter();
    procedure InitTree();
    procedure AddChildNode(parentNode:TtreeNode;mNo:string);
  public
    fCustomNo:string;
    fSysCode:string;
    fPbCtrl:integer;
    fBaseNode:integer;
  end;

var
  frmPbProductHelp10: TfrmPbProductHelp10;

implementation
uses
  U_global,U_cxGridSet;
{$R *.dfm}

procedure TfrmPbProductHelp10.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;

procedure TfrmPbProductHelp10.BtnCancelClick(Sender: TObject);
begin
  {if trim(self.Caption)='' then
      WriteCxGrid(trim(self.Name),tv1,'轧染订单管理')
  else
      WriteCxGrid(trim(self.Caption),tv1,'轧染订单管理'); }
  WriteCxGrid('基布选择',tv1,'轧染订单管理');
  ModalResult := -1;
end;

///////////////////////////////////////////
  //坯布控制
///////////////////////////////////////////
procedure TfrmPbProductHelp10.DoQuery();
var
  mstore:string;
begin
  //类型
  try
    LoadFlag:=false;
    if trim(fSysCode)='RZ' then
      mstore:= 'JRPC'
    else if trim(fSysCode)='ZR' then
      mstore:= 'GRPC'
    else if (trim(fSysCode)='YH') or (trim(fSysCode)='YR') then
      mstore:= 'YHPC';

    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp Do
    Begin
      close;
      filtered:=false;
      SQL.Clear;
      sql.Add('exec P_Calc_PBKC_CHANGE10');
      sql.Add(quotedStr(fSysCode));
      sql.Add(','+QuotedStr(mstore));  //仓库
      sql.Add(','+QuotedStr(fCustomNo));
      sql.Add(','+intTostr(fPbCtrl));
      sql.Add(','+intTostr(fBaseNode));
      sql.Add(','+quotedStr(fClassType));
      Open;
    end;
  finally
    LoadFlag:=true;
    ADOQueryHelp.EnableControls ;
  end;
end;

procedure TfrmPbProductHelp10.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;  
  InitTree();
end;

procedure TfrmPbProductHelp10.FormShow(Sender: TObject);
begin
  if fpbCtrl>0 then tv1store.Visible :=true
  else  tv1store.Visible :=false;
  {if trim(self.Caption)='' then
    ReadCxGrid(trim(self.Name),tv1,'轧染订单管理')
  else
    ReadCxGrid(trim(self.Caption),tv1,'轧染订单管理'); }
  readCxGrid('基布选择',tv1,'轧染订单管理');
  DoQuery();
  Edit2.SetFocus;
end;

procedure TfrmPbProductHelp10.cxTabControl1Change(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmPbProductHelp10.tv1DblClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;
/////////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////////
procedure TfrmPbProductHelp10.Dofilter();
var
  filterStr:string;
begin
  filterStr:='';
  if trim(edit2.Text)<>'' then
  begin
    filterStr:=' and (P_code like '+quotedStr('%'+trim(edit2.text)+'%')
               +' or P_chnName like '+quotedStr('%'+trim(edit2.text)+'%')
               +' or P_spec like '+quotedStr('%'+trim(edit2.text)+'%')
               +')';
  end;
  ////////////////////////
  if trim(edt_spec.Text)<>'' then
  begin
    filterStr:=' or P_spec like '+quotedStr('%'+trim(edt_spec.text)+'%');
  end;
  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 TfrmPbProductHelp10.InitTree();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
   i:integer;
   mNode,RootNode :TTreeNode;
   array_Node:array of TTreeNOde;
begin
   try
  	   New(MyRecPtr);
  	   MyRecPtr^.wbcode := 'ALL';
  	   MyRecPtr^.Tvtem_str := '全部产品';
       //treetop := 'ALL';
       with TreeView1 do
  	   begin
         Items.Clear;
         RootNode := Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
         RootNode.ImageIndex := 1;
         RootNode.SelectedIndex := 2;
  	   end;

  	   with TreeView1,AD_Rhl_Fllb do
  	   begin
         tmpstr := 'select code,name from xc_code where flag = ''CATLOGTYPE'' and valid = ''Y''';
         tmpstr := tmpstr +' order by code';
         active := false;
         CommandText := tmpstr;
         active := true;
         if recordcount = 0 then
         begin
           application.MessageBox('对不起,基础表里未定义产品基础分类:CATLOGTYPE!','提示信息',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)
                                   +'('+trim(fieldbyname('code').Asstring)+')';
           mNode := Items.AddChildObject(RootNode, MyRecPtr^.Tvtem_str, MyRecPtr);
           mNode.ImageIndex := 1;
           mNode.SelectedIndex := 2;
           array_Node[i]:=mNode;
           INC(i);
           Next;
         end;
         Close;
      end;
     ///////////////////////////////////////
     with ADOQueryChild do
     begin
       close;
       filtered:=false;
       sql.Clear ;
       sql.Add('select * from xc_code');
       sql.Add('where 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].Expanded:=true;
   except
   end;
end;
//////////////////////////////////////////////////////////////////
  //函数功能：增加子节点
//////////////////////////////////////////////////////////////////
procedure TfrmPbProductHelp10.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 TfrmPbProductHelp10.TreeView1Click(Sender: TObject);
var
   NodeXX:TTreeNode;
begin
   if not LoadFlag then exit;
   NodeXX := TreeView1.Selected;
   fClassType := trim(PMyRec(NodeXX.Data).wbcode);
   fBaseNode:=0;
   if (fClassType <> 'ALL') then  //不是全部产品
   begin
     with ADOQueryTmp do
     begin
       Close;
       SQL.Clear;
       SQL.Add('select flag from xc_code where code = '''+fClassType+''' and valid = ''Y'' ' );
       Open;
       if trim(fieldbyname('flag').asstring) = 'CATLOGTYPE' then  //主类产品
       begin
         fBaseNode:=1;
       end
       else //不是主类产品
       begin
         fBaseNode:=2;
       end;
     end;
   end
   else
     fClassType:='';
   DoQuery();
end;

procedure TfrmPbProductHelp10.Edit2Change(Sender: TObject);
begin
  Dofilter();
end;

procedure TfrmPbProductHelp10.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked=True then
  begin
    fPbCtrl:=1;
    fBaseNode:=0;
    fSysCode:='';
    DoQuery();
  end else
  begin
    fPbCtrl:=0;
    fBaseNode:=0;
    fSysCode:='';
    DoQuery();
  end;
end;

end.
