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

-- 生成时间:2018-06-27 14:47:57 by ebs-core  
CREATE  procedure order_243_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_243 where ord_id=@ordid  

 ---- 以下是开发人员写的 check order 事件 --------------
 /*判断如果离职方式为【正常离职】或【考核未通过】，则【货品暂扣款】和【关联离职申请】不能为空*/
if exists (select 1 from #master where ord_lzfs in ('正常离职','考核未通过') and (ord_hpzkk < 0 or  isnull(ord_lzsqsh,'') = ''))
begin
    set @operation ='abort'
    set @errorinfo ='该离职人员存在未审批完成的【离职申请单】或【货品暂扣款】为空，请先处理离职申请单再提交离职确认单！'
end

/*判断离职方式是【自动离职】时不能【关联离职申请】*/
if exists (select 1 from #master where ord_lzfs = '自动离职' and isnull(ord_lzsqsh,'') <> '' )
begin
    set @operation ='abort'
    set @errorinfo ='该员工已完成离职申请审批，离职方式请勿选择【自动离职】'
end

/*验证实际离职时间是否小于等于当前时间(今天)*/
if exists(select 1 from #master where datediff(day,ord_sjlzrq,getdate()) < 0)
begin
    set @operation ='abort'
    set @errorinfo ='【实际离职日期】不能超过今天！'
end

/*判断离职方式是【自动离职】时，查询【离职申请单】是否存在此人，且单据为【已完成】状态*/
declare 
@gh nvarchar(50)='',
@lzfs nvarchar(50)=''
select @gh = isnull(ord_gh,''),@lzfs = isnull(ord_lzfs,'') from #master
if(@lzfs ='自动离职')
begin
    if exists (select 1 from order_45 where ord_gh = @gh and ord_status = '已完成')
    begin
        set @operation ='abort'
        set @errorinfo ='该离职人员已完成离职申请审批，离职方式请勿选择【自动离职】!'
    end
end
 ---- 以上是开发人员写的 check order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @sys_recid=@sys_recid  -- here useless 
 
end 
--- 清除临时表 

exec drop_temp_table '#master' 

set nocount off 
end