$PBExportHeader$uo_tv_mtrltype.sru
forward
global type uo_tv_mtrltype from treeview
end type
end forward

global type uo_tv_mtrltype from treeview
integer width = 549
integer height = 452
integer textsize = -12
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Arial"
long textcolor = 33554432
borderstyle borderstyle = stylelowered!
string picturename[] = {"Application!","Continue!"}
long picturemaskcolor = 536870912
long statepicturemaskcolor = 536870912
end type
global uo_tv_mtrltype uo_tv_mtrltype

type variables
Long uo_deep = 0
s_mtrltype uo_cur_info
end variables

forward prototypes
public function integer f_getinfo (long arg_mtrltypeid)
public function integer f_maketree (integer arg_mtrlorigin)
public function integer pf_treegrowth (long arg_handl, long arg_parentid)
public function integer f_getinfo_typecode (long arg_mtrltypeid)
end prototypes

public function integer f_getinfo (long arg_mtrltypeid);Int rslt = 1
SELECT mtrltypeid,
	mtrltype,
	handtype,
	sonflag,
	parentid,
	mtrlprp,
	typecode,
	typecodestr
	INTO :uo_cur_info.mtrltypeid,
	:uo_cur_info.mtrltype,
	:uo_cur_info.handtype,
	:uo_cur_info.sonflag,
	:uo_cur_info.parentid,
	:uo_cur_info.mtrlprp,
	:uo_cur_info.typecode,
	:uo_cur_info.typecodestr
	FROM u_mtrltype
	Where mtrltypeid = :arg_mtrltypeid;
	
IF sqlca.SQLCode <> 0 THEN
	uo_cur_info.mtrltypeid = 0
	uo_cur_info.mtrltype = ''
	uo_cur_info.handtype = ''
	uo_cur_info.sonflag = 0
	uo_cur_info.parentid = 0
	uo_cur_info.parenthandtype = ''
	uo_cur_info.mtrlprp = 0
	uo_cur_info.typecode = ''
	uo_cur_info.typecodestr = ''
	rslt = 0
	GOTO ext
END IF

String ls_parenthandtype,ls_parenttypecodestr
Long ll_parentid

ll_parentid = uo_cur_info.parentid

IF uo_cur_info.parentid = 0 THEN
	uo_cur_info.parenthandtype = ''
	uo_cur_info.parenttypecodestr = ''
ELSE
	SELECT handtype,typecodestr
		INTO :ls_parenthandtype,:ls_parenttypecodestr
		FROM u_mtrltype
		Where mtrltypeid = :ll_parentid;
	IF sqlca.SQLCode <> 0 THEN
		uo_cur_info.parenthandtype = ''
		uo_cur_info.parenttypecodestr = ''
	ELSE
		uo_cur_info.parenthandtype = ls_parenthandtype
		uo_cur_info.parenttypecodestr = ls_parenttypecodestr
	END IF
	uo_cur_info.typecodeall = uo_cur_info.typecode
	f_getinfo_typecode(uo_cur_info.parentid)
	
END IF

ext:

RETURN rslt



end function

public function integer f_maketree (integer arg_mtrlorigin);//wf_maketree()
long ll_hand
uo_deep=1

long tvi_hdl = 0

DO UNTIL this.FindItem(RootTreeItem!, 0) = -1
	this.DeleteItem(tvi_hdl)
LOOP

ll_hand = this.insertitemlast(0,'全部类别',1)

pf_treegrowth(ll_hand,0)

this.expanditem(ll_hand)

return 0
end function

public function integer pf_treegrowth (long arg_handl, long arg_parentid);
long rslt=1

long  ll_mtrltypeid,ll_count=0,ll_i
string ls_mtrltype,ls_handtype
int li_sonflag

s_mtrltype l_s_pfmx[]

long li_handl
TreeViewItem l_tvi

uo_deep++

if uo_deep > 1000 then //防止死递归
	return 0
end if

DECLARE pf_cur CURSOR FOR 
  SELECT u_mtrltype.mtrltypeid,   
         u_mtrltype.mtrltype,   
         u_mtrltype.handtype,    
         u_mtrltype.typecode,   
         u_mtrltype.sonflag,    
         u_mtrltype.typecodestr
    FROM u_mtrltype
   WHERE ( u_mtrltype.parentid = :arg_parentid )
	  order by u_mtrltype.typecode;
open pf_cur;
ll_count = 1
fetch pf_cur into :l_s_pfmx[ll_count].mtrltypeid,:l_s_pfmx[ll_count].mtrltype,&
						:l_s_pfmx[ll_count].handtype,:l_s_pfmx[ll_count].typecode,&
						:l_s_pfmx[ll_count].sonflag,:l_s_pfmx[ll_count].typecodestr;

do while sqlca.sqlcode = 0
	ll_count++
	fetch pf_cur into :l_s_pfmx[ll_count].mtrltypeid,:l_s_pfmx[ll_count].mtrltype,&
							:l_s_pfmx[ll_count].handtype,:l_s_pfmx[ll_count].typecode,&
							:l_s_pfmx[ll_count].sonflag,:l_s_pfmx[ll_count].typecodestr;
loop
ll_count = ll_count - 1
close pf_cur;

for ll_i = 1 to ll_count
	li_handl = this.insertitemsort(arg_handl,l_s_pfmx[ll_i].typecode + ' ' + l_s_pfmx[ll_i].mtrltype,2) 
	
	if this.GetItem ( li_handl, l_tvi) = 1 then
		l_tvi.label = l_s_pfmx[ll_i].typecode + ' ' + l_s_pfmx[ll_i].mtrltype
		l_tvi.data  = l_s_pfmx[ll_i].mtrltypeid
		this.SetItem( li_handl, l_tvi )
	end if
	
	pf_treegrowth(li_handl,l_s_pfmx[ll_i].mtrltypeid) 
next

return 1
end function

public function integer f_getinfo_typecode (long arg_mtrltypeid);Int rslt = 1
String ls_typecode
Long ll_parentid
SELECT parentid,
	typecode
	INTO :ll_parentid,
	:ls_typecode
	FROM u_mtrltype
	Where mtrltypeid = :arg_mtrltypeid;
IF sqlca.SQLCode <> 0 THEN
	rslt = 0
	GOTO ext
END IF

uo_cur_info.typecodeall = ls_typecode + uo_cur_info.typecodeall
IF ll_parentid > 0 THEN
	f_getinfo_typecode(ll_parentid)
END IF
ext:
RETURN rslt



end function

on uo_tv_mtrltype.create
end on

on uo_tv_mtrltype.destroy
end on

event constructor;f_maketree(-1)
end event

event selectionchanged;long ll_hand
long ll_mtrltypeid
TreeViewItem l_tvi 
ll_hand = this.finditem(CurrentTreeItem!,0)

if this.getitem(ll_hand,l_tvi) = 1 then
	if ll_hand = 1 then
		ll_mtrltypeid = 0
	else
		ll_mtrltypeid = l_tvi.data	
	end if
	f_getinfo(ll_mtrltypeid)
	this.expanditem(ll_hand)	
end if


end event

