$PBExportHeader$uo_cmpl_mtrlprice.sru
forward
global type uo_cmpl_mtrlprice from nonvisualobject
end type
end forward

global type uo_cmpl_mtrlprice from nonvisualobject
end type
global uo_cmpl_mtrlprice uo_cmpl_mtrlprice

type variables
long deep = 0
boolean ifcmpl_1 = false
end variables

forward prototypes
public function integer uof_get_cost (long arg_mtrlid, string arg_mtrlcode, string arg_pfcode, decimal arg_qty, long arg_lp, ref s_mtrlprice_sum arg_s_price, ref string arg_msg)
public function integer uof_get_wageprice (long arg_mtrlid, string arg_mtrlcode, decimal arg_qty, ref s_mtrlprice_sum arg_s_price, ref string arg_msg)
end prototypes

public function integer uof_get_cost (long arg_mtrlid, string arg_mtrlcode, string arg_pfcode, decimal arg_qty, long arg_lp, ref s_mtrlprice_sum arg_s_price, ref string arg_msg);Int rslt = 1

Decimal Sonscale,SonLoss,SonDECLosS
Long  SonMtrlid,count = 0,ls_i
String mtrlcode, mtrlname
s_pfmx ls_s_pfmx[]
Decimal ld_price,ld_qty,ld_outcost,ld_wageprice
Decimal ld_planprice
Int li_mtrlorigin
String ls_mtrlsectype

deep++

IF deep > 10000 THEN //防止死递归
	arg_msg = '物料:'+arg_mtrlcode+'死循环'
	rslt = 0
	GOTO ext
END IF

DECLARE pf_cur CURSOR FOR
	SELECT u_PrdPF.Sonscale,
	u_PrdPF.SonLoss,
	u_PrdPF.SonDECLosS,
	u_PrdPF.SonMtrlid,
	u_mtrldef.mtrlcode,
	u_PrdPF.sonpfcode
	FROM u_PrdPF,
	u_mtrldef
	WHERE ( u_PrdPF.SonMtrlid = u_mtrldef.mtrlid ) AND
	( u_PrdPF.mtrlid = :arg_mtrlid ) AND
	( u_PrdPF.pfcode = :arg_pfcode );
	
OPEN pf_cur;
count = 1
FETCH pf_cur INTO :ls_s_pfmx[count].Sonscale,:ls_s_pfmx[count].SonLoss,&
	:ls_s_pfmx[count].SonDECLosS,:ls_s_pfmx[count].SonMtrlid,&
	:ls_s_pfmx[count].mtrlcode,:ls_s_pfmx[count].sonpfcode;
	
DO WHILE sqlca.SQLCode = 0
	count++
	FETCH pf_cur INTO :ls_s_pfmx[count].Sonscale,:ls_s_pfmx[count].SonLoss,&
		:ls_s_pfmx[count].SonDECLosS,:ls_s_pfmx[count].SonMtrlid,&
		:ls_s_pfmx[count].mtrlcode,:ls_s_pfmx[count].sonpfcode;
LOOP
count = count - 1
CLOSE pf_cur;

IF arg_lp > 1 OR ifcmpl_1 THEN
	
	
	SELECT mtrlorigin,planprice,mtrlsectype
		INTO :li_mtrlorigin,:ld_planprice,:ls_mtrlsectype
		FROM u_mtrldef
		Where mtrlid = :arg_mtrlid;
	IF sqlca.SQLCode <> 0 THEN
		arg_msg = '查询:'+arg_mtrlcode+'来源失败'
		rslt = 0
		GOTO ext
	END IF
	
	CHOOSE CASE li_mtrlorigin
		CASE 2,3
			SELECT price INTO :ld_price
				FROM  v_maxprice_sptprice
				Where mtrlid = :arg_mtrlid;
			IF sqlca.SQLCode = -1  THEN
				arg_msg = '查询物料:'+arg_mtrlcode+'最新采购价或最新外加工单价失败'
				rslt = 0
				GOTO ext
			END IF
			
			IF IsNull(ld_price) THEN ld_price = 0
			
			IF li_mtrlorigin = 3 THEN
				arg_s_price.wfjgprice = arg_s_price.wfjgprice + ld_price * arg_qty
			END IF
			
			IF li_mtrlorigin = 2 AND count = 0 THEN
				CHOOSE CASE ls_mtrlsectype
					CASE '木材'
						arg_s_price.mucai = arg_s_price.mucai +  ld_price * arg_qty
					CASE '板材/面板'
						arg_s_price.bancai = arg_s_price.bancai +  ld_price * arg_qty
					CASE '油漆'
						arg_s_price.youqi = arg_s_price.youqi +  ld_price * arg_qty
						
					CASE '皮/布'
						arg_s_price.pibu = arg_s_price.pibu +  ld_price * arg_qty
						
					CASE '海绵'
						arg_s_price.haimian = arg_s_price.haimian +  ld_price * arg_qty
						
					CASE '纸箱'
						arg_s_price.zhixiang = arg_s_price.zhixiang +  ld_price * arg_qty
						
					CASE '包材'
						arg_s_price.baocai = arg_s_price.baocai +  ld_price * arg_qty
						
					CASE '配件/五金'
						arg_s_price.peijian = arg_s_price.peijian +  ld_price * arg_qty
						
				END CHOOSE
				rslt = 1
				GOTO ext
			END IF
			
		CASE 0
			//计算人工
			IF uof_get_wageprice(arg_mtrlid,arg_mtrlcode,arg_qty,arg_s_price,arg_msg) = 0 THEN
				rslt = 0
				GOTO ext
			END IF
	END CHOOSE
END IF


FOR ls_i = 1 TO count
	ld_qty = arg_qty * (ls_s_pfmx[ls_i].Sonscale / (1 - ls_s_pfmx[ls_i].SonLoss) + ls_s_pfmx[ls_i].SonDECLosS)
	IF uof_get_cost(ls_s_pfmx[ls_i].SonMtrlid,ls_s_pfmx[ls_i].mtrlcode,ls_s_pfmx[ls_i].sonpfcode,ld_qty,arg_lp+1,arg_s_price,arg_msg) = 0 THEN
		rslt = 0
		GOTO ext
	END IF
NEXT

ext:

RETURN rslt





end function

public function integer uof_get_wageprice (long arg_mtrlid, string arg_mtrlcode, decimal arg_qty, ref s_mtrlprice_sum arg_s_price, ref string arg_msg);Int rslt = 1
Decimal ld_wageprice

SELECT sum(u_sc_workprice.workprice * u_sc_workprice.workqty) INTO :ld_wageprice
	FROM u_sc_workprice INNER JOIN
	u_sc_workgroup ON u_sc_workprice.wrkGrpid = u_sc_workgroup.wrkGrpid
	WHERE  u_sc_workprice.ifownpro = 0
	AND u_sc_workprice.mtrlid = :arg_mtrlid
	AND u_sc_workgroup.dscrp = '木工';
IF sqlca.SQLCode = -1  THEN
	arg_msg = '查询物料:'+arg_mtrlcode+'本厂工价失败'
	rslt = 0
	GOTO ext
END IF
IF IsNull(ld_wageprice) THEN ld_wageprice = 0

arg_s_price.mugong_rl = arg_s_price.mugong_rl + ld_wageprice * arg_qty



SELECT sum(u_sc_workprice.workprice * u_sc_workprice.workqty) INTO :ld_wageprice
	FROM u_sc_workprice INNER JOIN
	u_sc_workgroup ON u_sc_workprice.wrkGrpid = u_sc_workgroup.wrkGrpid
	WHERE  u_sc_workprice.ifownpro = 0
	AND u_sc_workprice.mtrlid = :arg_mtrlid
	AND u_sc_workgroup.dscrp = '油漆';
IF sqlca.SQLCode = -1  THEN
	arg_msg = '查询物料:'+arg_mtrlcode+'本厂工价失败'
	rslt = 0
	GOTO ext
END IF
IF IsNull(ld_wageprice) THEN ld_wageprice = 0

arg_s_price.youqi_rl = arg_s_price.youqi_rl + ld_wageprice * arg_qty

SELECT sum(u_sc_workprice.workprice * u_sc_workprice.workqty) INTO :ld_wageprice
	FROM u_sc_workprice INNER JOIN
	u_sc_workgroup ON u_sc_workprice.wrkGrpid = u_sc_workgroup.wrkGrpid
	WHERE  u_sc_workprice.ifownpro = 0
	AND u_sc_workprice.mtrlid = :arg_mtrlid
	AND u_sc_workgroup.dscrp = '软体前工序';
IF sqlca.SQLCode = -1  THEN
	arg_msg = '查询物料:'+arg_mtrlcode+'本厂工价失败'
	rslt = 0
	GOTO ext
END IF
IF IsNull(ld_wageprice) THEN ld_wageprice = 0

arg_s_price.ruanti_rl = arg_s_price.ruanti_rl + ld_wageprice * arg_qty


SELECT sum(u_sc_workprice.workprice * u_sc_workprice.workqty) INTO :ld_wageprice
	FROM u_sc_workprice INNER JOIN
	u_sc_workgroup ON u_sc_workprice.wrkGrpid = u_sc_workgroup.wrkGrpid
	WHERE  u_sc_workprice.ifownpro = 0
	AND u_sc_workprice.mtrlid = :arg_mtrlid
	AND u_sc_workgroup.dscrp = '扪皮及包装';
IF sqlca.SQLCode = -1  THEN
	arg_msg = '查询物料:'+arg_mtrlcode+'本厂工价失败'
	rslt = 0
	GOTO ext
END IF
IF IsNull(ld_wageprice) THEN ld_wageprice = 0

arg_s_price.mengpi_rl = arg_s_price.mengpi_rl + ld_wageprice * arg_qty

ext:
RETURN rslt

end function

on uo_cmpl_mtrlprice.create
call super::create
TriggerEvent( this, "constructor" )
end on

on uo_cmpl_mtrlprice.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

