unit U_Xc_CodeZDYTree;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxGraphics, cxCustomData, cxStyles, cxTL, DB,
  ADODB, cxControls, cxInplaceContainer, cxTLData, cxDBTL, cxMaskEdit,
  ImgList;

type
  TfrmXc_CodeZDYTree = class(TForm)
    ToolBar2: TToolBar;
    TOk: TToolButton;
    ToolButton7: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    ADOQueryHelp: TADOQuery;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ImageList24: TImageList;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyleTree: TcxStyle;
    cxStyle3: TcxStyle;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQueryCmd: TADOQuery;
    procedure ToolButton7Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure DoQuery();
  public
    fnote:string;
    fflag:string;
    fflagname:string;
  end;

var
  frmXc_CodeZDYTree: TfrmXc_CodeZDYTree;

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

procedure TfrmXc_CodeZDYTree.ToolButton7Click(Sender: TObject);
begin
  close;
end;

procedure TfrmXc_CodeZDYTree.FormCreate(Sender: TObject);
begin
  cxDBTreeList1.Align :=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
end;
///////////////////////////////////////////////////////////
  //
///////////////////////////////////////////////////////////
procedure TfrmXc_CodeZDYTree.DoQuery();
var
  fsj:string;
begin
  with ADOQueryHelp do
  begin
    close;
    sql.Clear ;
    sql.Add('select * from XC_Code');
    sql.Add('where valid=''Y''');
    sql.Add('and note='''+Trim(fnote)+'''');
    sql.Add('order by orderno ');
    Open;
  end;
  if not ADOQueryHelp.IsEmpty then 
    cxDBTreeList1.Nodes[0].Expand(True)
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into XC_Code(code,name,flag,note)');
      sql.Add('select '''+Trim(fflag)+'''');
      sql.Add(','''+Trim(fflagname)+'''');
      SQL.Add(',''BASECODE'' ');
      sql.Add(','''+Trim(fnote)+'''');
      sql.Add('insert into XC_Code(code,name,flag,note)');
      fsj:=Trim(fflag)+'01';
      sql.Add('select '''+Trim(fsj)+'''');
      sql.Add(','''+Trim(fflagname)+'''');
      SQL.Add(','''+Trim(fflag)+'''');
      sql.Add(','''+Trim(fnote)+'''');
      ExecSQL;
    end;
    with ADOQueryHelp do
    begin
      close;
      sql.Clear ;
      sql.Add('select * from XC_Code');
      sql.Add('where valid=''Y''');
      sql.Add('and note='''+Trim(fnote)+'''');
      sql.Add('order by orderno ');
      Open;
    end;
  end;
end;

procedure TfrmXc_CodeZDYTree.FormShow(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmXc_CodeZDYTree.TOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then
     ModalResult:=-1
  else
    ModalResult:=1;
end;

procedure TfrmXc_CodeZDYTree.cxDBTreeList1DblClick(Sender: TObject);
begin
  tok.Click  ;
end;

procedure TfrmXc_CodeZDYTree.ToolButton1Click(Sender: TObject);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      if Trim(Self.ADOQueryHelp.fieldbyname('code').AsString)<>'' then
        flag:=Trim(Self.ADOQueryHelp.fieldbyname('code').AsString)
      else
        flag:=Trim(Self.fflag);
      if Trim(Self.ADOQueryHelp.fieldbyname('name').AsString)<>'' then
        flagname:=Trim(Self.ADOQueryHelp.fieldbyname('name').AsString)
      else
        flagname:=Trim(Self.fflagname);
      //fnote:=True;
      snote:=Trim(Self.fnote);
      //forderno:=True;
      if ShowModal=1 then
      begin
        Self.DoQuery;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmXc_CodeZDYTree.ToolButton2Click(Sender: TObject);
begin
  DoQuery();
end;

end.
