$PBExportHeader$f_check_inoutdate.srf
global type f_check_inoutdate from function_object
end type

forward prototypes
global function integer f_check_inoutdate (long arg_storageid, ref datetime arg_date, boolean arg_ifcheck_balc, ref string arg_msg)
end prototypes

global function integer f_check_inoutdate (long arg_storageid, ref datetime arg_date, boolean arg_ifcheck_balc, ref string arg_msg);Int rslt = 1
DateTime server_dt
DateTime ld_ctdate,ld_balcdate

//IF sys_power_issuper THEN
//	rslt = 1
//	GOTO ext
//END IF
//
SELECT Top 1 getdate() INTO :server_dt FROM u_user;
IF sqlca.SQLCode <> 0 THEN
	rslt = 0
	arg_msg = "查询操作失败,日期"
	GOTO ext
END IF

IF DaysAfter(Date(String(server_dt,'yyyy-mm-dd')),Date(String(arg_date,'yyyy-mm-dd'))) > sys_option_input_days THEN
	rslt = 0
	arg_msg = '发生日期错误,日期不能超前系统日期'
	GOTO ext
END IF

IF DaysAfter(Date(String(arg_date,'yyyy-mm-dd')),Date(String(server_dt,'yyyy-mm-dd'))) > sys_option_input_days THEN
	rslt = 0
	arg_msg = '发生日期错误,日期不能落后系统日期'
	GOTO ext
END IF

SELECT max(ct_datetime)
	INTO :ld_ctdate
	FROM sys_ct_data;
IF sqlca.SQLCode = -1 THEN
	rslt = 0
	arg_msg = '查询最后截数日期出错'
	GOTO ext
ELSEIF sqlca.SQLCode = 100 THEN
	GOTO balc
END IF

IF arg_storageid = -100 THEN
	IF arg_date <= ld_ctdate THEN
		arg_msg = '非法发生日期,日期:'+String(arg_date,'yyyy-mm-dd')+'小于最后截数日期:'+String(ld_ctdate,'yyyy-mm-dd')
		rslt = 0
		GOTO ext
	END IF
END IF

balc:

IF arg_ifcheck_balc THEN
	SELECT max(balcdate)
		INTO :ld_balcdate
		FROM u_warebalc
		WHERE balcdateint > 0
		AND storageid = :arg_storageid;
	IF sqlca.SQLCode = -1 THEN
		rslt = 0
		arg_msg = '查询最后结存日期出错'
		GOTO ext
	ELSEIF sqlca.SQLCode = 100 THEN
		rslt = 1
		GOTO ext
	END IF
	
	IF arg_date <= ld_balcdate THEN
		arg_msg = '非法发生日期,日期:'+String(arg_date,'yyyy-mm-dd')+'小于最后结存日期:'+String(ld_balcdate,'yyyy-mm-dd')
		rslt = 0
		GOTO ext
	END IF
END IF


ext:
RETURN rslt



end function

