$PBExportHeader$w_report_inexport.srw
forward
global type w_report_inexport from w_publ_base
end type
type sle_filename from singlelineedit within w_report_inexport
end type
type cb_1 from uo_imflatbutton within w_report_inexport
end type
type cb_2 from uo_imflatbutton within w_report_inexport
end type
type gb_1 from groupbox within w_report_inexport
end type
end forward

global type w_report_inexport from w_publ_base
integer width = 1993
integer height = 520
string title = "导入导出报表格式"
boolean minbox = false
windowtype windowtype = response!
sle_filename sle_filename
cb_1 cb_1
cb_2 cb_2
gb_1 gb_1
end type
global w_report_inexport w_report_inexport

type variables
//u_report_imex uo_imex
//
  s_rpt_inexport s_imexport_reposts

 s_preview_billformatset_tran s_billformat
end variables

forward prototypes
public function integer f_expout (string arg_filename, blob arg_bdata, ref string arg_msg)
public function integer f_inexp (string arg_filename, ref blob arg_bdata, ref string arg_msg)
public function integer wf_save (ref string arg_msg, transaction pictrans)
public function integer wf_in_outport (ref string arg_msg)
end prototypes

public function integer f_expout (string arg_filename, blob arg_bdata, ref string arg_msg);int rslt=1
long fend,li_FileNum,fend_t,ls_i,ls_jh
blob b_dtdata_p
 
if FileExists(arg_filename) then
	if not FileDelete(arg_filename) then
		rslt=0
		arg_msg="删除临时文件失败("+arg_filename+"),请先手动清除!"
		goto ext
	end if
end if

fend=len(arg_bdata)
if fend<=32765 then
	fend_t=1
else
	fend_t=fend/32765
	if mod(fend,32765)<>0 then
		fend_t=fend_t+1
	end if
end if
li_FileNum = FileOpen( arg_filename,StreamMode!, Write!, LockWrite!)
if li_FileNum<=0 then
	arg_msg="打开文件"+arg_filename+"失败"
	rslt=0
	fileclose(li_FileNum)
	goto ext
end if
for ls_i = 1 to fend_t
	b_dtdata_p=blobmid(arg_bdata,1+(ls_i - 1 )*32765,32765)

	ls_jh=FileWrite(li_FileNum, b_dtdata_p)
	if ls_jh<=0 then
		arg_msg="写文件"+arg_filename+"失败"
		rslt=0
		fileclose(li_FileNum)
		goto ext
	end if
next

fileclose(li_FileNum)

ext:
return rslt
end function

public function integer f_inexp (string arg_filename, ref blob arg_bdata, ref string arg_msg);//f_inexp(arg_bdata,arg_filename,arg_msg)
long rslt=1
long flen,li_FileNum,loops,ls_i,bytes_read,new_pos


blob b_dtdata_p,tot_b

SetPointer(HourGlass!)

// Get the file length, and open the file

flen = FileLength(arg_filename)
li_FileNum = FileOpen(arg_filename, StreamMode!, Read!, LockRead!)
if li_FileNum=-1 then
		rslt=0
		arg_msg='打开文件'+arg_filename+'操作失败!'
		fileclose(li_FileNum)
		goto ext
	end if
// Determine how many times to call FileRead
IF flen > 32765 THEN
	IF Mod(flen, 32765) = 0 THEN
		loops = flen/32765
	ELSE
		loops = (flen/32765) + 1
	END IF
ELSE
	loops = 1
END IF

// Read the file

new_pos = 1
FOR ls_i = 1 to loops
	bytes_read = FileRead(li_FileNum, b_dtdata_p)
	if bytes_read=0 then
		rslt=0
		arg_msg='读取文件'+arg_filename+'操作失败!'
		fileclose(li_FileNum)
		goto ext
	end if
	tot_b = tot_b + b_dtdata_p
	 
NEXT

arg_bdata=tot_b
 
fileclose(li_FileNum)

ext:
return rslt
end function

public function integer wf_save (ref string arg_msg, transaction pictrans);//wf_save()
int rslt=1
 
updateblob  sys_dwnSyntax set dwsyntaxb=:s_imexport_reposts.dwsyntaxb where dwname = :s_imexport_reposts.dwname and  dname =:s_imexport_reposts.dname using pictrans;
 	if pictrans.sqlcode=-1 then
			rslt=0 
			arg_msg='更新格式失败!原因:'+pictrans.sqlerrtext
			goto ext
	end if
	
 	commit using pictrans;
 
 updateblob  sys_dwnSyntax set subdwsyntaxb =:s_imexport_reposts.subdwsyntaxb where dwname = :s_imexport_reposts.dwname and  dname =:s_imexport_reposts.dname using pictrans;
 	if pictrans.sqlcode=-1 then
			rslt=0 
			arg_msg='更新格式失败!原因:'+pictrans.sqlerrtext
			goto ext
	end if
	
 	commit using pictrans;

ext:
if rslt=0 then 
	rollback using pictrans;
end if
return (rslt)
end function

public function integer wf_in_outport (ref string arg_msg);//wf_in_outport(arg_msg)
string ls_1
blob arg_allblob
long cnt=0,rslt=1
blob ll_blob
Transaction sqlca_blob
sqlca_blob=create Transaction
ls_1=""
rslt=1
if s_billformat.ifimporflag=0 then
	
	if f_inexp(sle_filename.text,arg_allblob,arg_msg)=0 then
      arg_msg="系统提示:"+"错误!"+arg_msg
		rslt=0
	   goto ext
  end if
 
if  pos(string(arg_allblob),"***")>0 then
	  s_imexport_reposts.dwname= left(string(arg_allblob),pos(string(arg_allblob),"***") -1)
     ls_1=mid(string(arg_allblob),pos(string(arg_allblob),"***")+3,len(string(arg_allblob)) - pos(string(arg_allblob),"***") - 2)
   if  pos(ls_1,"***")>0 then
	  s_imexport_reposts.dname=left(ls_1,pos(ls_1,"***") - 1)
	 ls_1=mid(ls_1,pos(ls_1,"***")+3,len(ls_1) - pos(ls_1,"***") - 2)
 
  if  pos(ls_1,"*sheng*")>0 then
 	 s_imexport_reposts.dwsyntaxb=blob(left(ls_1,pos(ls_1,"*sheng*") - 1))
	 s_imexport_reposts.subdwsyntaxb=blob(mid(ls_1,pos(ls_1,"*sheng*")+7,len(ls_1) - pos(ls_1,"*sheng*") - 6))
  end if
end if
else
	 arg_msg="系统提示:"+"查询单据格式失败!"
	 rslt=0
	 goto ext
end if

 cnt=0
 select count(*)
 into:cnt
 from sys_dwnSyntax
 where dwname=:s_imexport_reposts.dwname and 
       dname=:s_imexport_reposts.dname;
 
 if sqlca.sqlcode<>0 then
	 arg_msg="系统提示:"+"查询单据格式失败!" 
	  rslt=0
	 goto ext
 end if
 
 if cnt=0 then
	
	insert into sys_dwnSyntax (dwname,dname)
	values (:s_imexport_reposts.dwname,:s_imexport_reposts.dname);
	 if sqlca.sqlcode<>0 then
	 arg_msg="系统提示:"+"插入单据格式失败!" 
	 rollback;
	 rslt=0
	 goto ext
    end if
  		commit;
  
 end if
 
   if  wf_save(arg_msg,sqlca)=0 then
	   arg_msg='系统提示:'+arg_msg
	   rslt=0
	   goto ext 
   end if
  
 arg_msg="导入单据格式成功!"
 	
elseif s_billformat.ifimporflag=1 then
 
 if f_expout(sle_filename.text,blob(s_billformat.dwname+"***"+s_billformat.dname+"***"+string(s_billformat.dwsyntaxb)+"*sheng*"+string(s_billformat.subdwsyntaxb)),arg_msg)=0 then
    arg_msg="系统提示:"+"错误!"+arg_msg
	 rslt=0
	 goto ext 
 end if
     arg_msg="导出单据格式成功!"
end if


ext:
return(rslt)
end function

on w_report_inexport.create
int iCurrent
call super::create
this.sle_filename=create sle_filename
this.cb_1=create cb_1
this.cb_2=create cb_2
this.gb_1=create gb_1
iCurrent=UpperBound(this.Control)
this.Control[iCurrent+1]=this.sle_filename
this.Control[iCurrent+2]=this.cb_1
this.Control[iCurrent+3]=this.cb_2
this.Control[iCurrent+4]=this.gb_1
end on

on w_report_inexport.destroy
call super::destroy
destroy(this.sle_filename)
destroy(this.cb_1)
destroy(this.cb_2)
destroy(this.gb_1)
end on

event open;call super::open;s_billformat = message.powerobjectparm
 
end event

event close;call super::close;CloseWithReturn(this,s_imexport_reposts)
end event

type cb_func from w_publ_base`cb_func within w_report_inexport
boolean visible = false
end type

type cb_exit from w_publ_base`cb_exit within w_report_inexport
integer x = 1463
integer y = 296
integer width = 302
integer height = 100
end type

type sle_filename from singlelineedit within w_report_inexport
integer x = 110
integer y = 124
integer width = 1655
integer height = 92
integer taborder = 30
boolean bringtotop = true
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
string text = " "
borderstyle borderstyle = stylelowered!
end type

type cb_1 from uo_imflatbutton within w_report_inexport
integer x = 1774
integer y = 120
integer width = 128
integer taborder = 40
boolean bringtotop = true
string text = "..."
end type

event clicked;call super::clicked;integer i
string pathname,filename
//i=getfilesavename("单据表格式文件",pathname,filename,"ljdw","单据表格式文件ljdw,*.ljdw")
i=getfilesavename("单据表格式文件",pathname,filename,"srd","srd,*.srd")
IF i=1 AND TRIM(pathname)<>'' AND TRIM(FILENAME)<>"" THEN
	sle_filename.text=pathname
END IF
end event

type cb_2 from uo_imflatbutton within w_report_inexport
integer x = 1010
integer y = 296
integer taborder = 20
boolean bringtotop = true
end type

event clicked;call super::clicked;string arg_msg=""
if wf_in_outport(arg_msg)=0 then
	messagebox('系统提示:',arg_msg)
	return 
else
		messagebox('系统提示:',arg_msg)
end if

end event

type gb_1 from groupbox within w_report_inexport
integer x = 82
integer y = 64
integer width = 1861
integer height = 204
integer taborder = 40
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
long backcolor = 134217739
end type

