IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_169_check]') AND type = 'P')
DROP PROCEDURE [dbo].[order_169_check]
GO

-- 生成时间:2018-06-27 12:20:30 by ebs-core  
CREATE  procedure order_169_check(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 set @operation = 'continue'
 set @errorinfo = '' 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#master' 
select * into #master from order_169 where ord_id=@ordid  

exec drop_temp_table '#detail4' 
select * into #detail4 from order_169_4 where ord2_ordid=@ordid  

 ---- 以下是开发人员写的 check order 事件 --------------
 /*验证抵扣金额是否大于借款金额*/
if exists (select 1 from #master where ord_jkje < ord_dkje)
begin
    set @operation='abort'
    set @errorinfo='抵扣金额不能大于借款金额'
end
/*验证报销金额是否大于预算余额
if exists (select 1 from #master where ord_bxje > ord_ysye)
begin
    set @operation = 'abort'
    set @errorinfo = '报销金额不能大于预算余额'
end*/


/*提交时验证责任部门是否是有预算。
若此部门有预算
则判断报销明细中的报销明细项是否每一项都存在于当前责任部门中
若存在任意一项不存在
则无法提交
若全都存在
则提交成功
若此部门无预算
则无法提交
*/
/*查询责任部门是否存在预算*/
declare @acdata bigint
select 
@acdata = count(account.acc_fiid) 
from 
mps_account account,#master m
where account.acc_type = 'expn' 
and account.acc_category = 'dpt' 
and account.acc_date = m.ord_bxrq 
and account.acc_objid = m.ord_dpid

/*存在预算*/
if(@acdata > 0)
begin
/*存在任意一项报销明细费用项不存在于报销部门的预算费用中*/
if exists
(
SELECT * FROM (select * from mps_account account,#master m
where account.acc_type = 'expn' 
and account.acc_category = 'dpt' 
and account.acc_date = m.ord_bxrq
and m.ord_dpid = account.acc_objid
) S right JOIN #detail4 xx
        ON  acc_fiid = xx.ord2_qtaiid
        where s.acc_type is null
)
begin
set @operation = 'abort'
set @errorinfo = '报销明细存在不包含于报销部门的预算费用项，无法提交'
end
end
/*不存在预算*/
else
begin
set @operation = 'abort'
set @errorinfo = '报销部门无预算，请重新选择报销部门'
end
 ---- 以上是开发人员写的 check order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @sys_recid=@sys_recid  -- here useless 
 
end 
--- 清除临时表 
exec drop_temp_table '#detail4'

exec drop_temp_table '#master' 

set nocount off 
end