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

-- 生成时间:2018-06-13 09:55:54 by ebs-core  
CREATE  procedure order_140_print(
@cmpid int,  
    @ordid bigint, 
    @useruid int, 
    @jbid bigint,
    @mpid int ) 
as  
begin  
set nocount on 
-- 首先取模板  t1 t2 
declare @tmpid int 
set @tmpid = @mpid 
declare @ordfinished int 
declare @ordbizid int 
declare @ord_ftype varchar(20),@ord_fbizid int,@ord_ford2tag int, @ord_ford2id bigint,@ord_fordid bigint 
declare @ord_rtype varchar(20),@ord_rbizid int,@ord_rord2tag int, @ord_rord2id bigint,@ord_rordid bigint 
declare @fordid bigint  
  
declare @ord_sxcuid bigint 

select  @fordid = ord_fordid, @ordbizid = ord_bizid, @tmpid = ord_templateid, @ordfinished = ord_finished  ,
  @ord_ftype=ord_ftype,@ord_fbizid=ord_fbizid,@ord_ford2tag=ord_ford2tag, @ord_ford2id=ord_ford2id,@ord_fordid =ord_fordid ,
  @ord_rtype=ord_rtype,@ord_rbizid=ord_rbizid,@ord_rord2tag=ord_rord2tag, @ord_rord2id=ord_rord2id,@ord_rordid =ord_rordid , @ord_sxcuid=ord_sxcuid
from order_140 where ord_id = @ordid and ord_cmpid = @cmpid 
declare @jbtmpid int   --保存工作步骤的模板 
declare @jbwsid int 
declare @jbwrkid int 

set @jbtmpid = isnull(@jbtmpid, 0) 
set @jbwsid = isnull(@jbwsid, -1) 
set @jbwrkid = isnull(@jbwrkid, 0) 

select * from mps_template where tmp_id = @mpid  
select 0 none where 0 = 1  --没有子模板 


    -- 返回工作记录模板 t3
    if (@jbwrkid>0) 
    begin
if (exists (select * from mps_template where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid)) 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid 
end else 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
end
    end else
    begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
    end
    -- 返回当前业务定义t4 
    select * from mps_biztype where biz_id = @ordbizid 
    -- 返回业务单中的特殊权限  t5 
    select * from order_140_users where(ordp_type = 'bizaudit' or ordp_type = 'bizdelete' or ordp_type = 'bizabolish') and ordp_ordid = @ordid 
    -- 返回工作中的特殊权限  t6 
    if (@jbid>0 and @ordfinished <= 1) 
    select * from mps_job_users where jp_jbid = @jbid and jp_cuid=@useruid 
    else select 0 none where 0 = 1
    --返回业务单数据 t7 
    select * from order_140 where ord_id = @ordid 
    -- 获取业务表主根据扩展数据  t8 
    select  * from order_140_0 where ord3_ordid = @ordid 
    -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t9  
    if (@jbwsid = 0 and @ordfinished <= 1) 
    select a.*, b.* from mps_job a, mps_flowwork b where jb_id = @jbid and wrk_id = jb_wrkid 
    else select 0 none where 0 = 1 
    --返回所有附件  t10 
    select *, da_categoryid as datagroup from mps_attach where da_cmpid = @cmpid and da_type = 'biz140'   and da_objid = @ordid and da_objid2 = 0 order by da_categoryid 
    -- 返回需要签字的工作记录。  t11 
    if (@ordfinished <= 1) 
    select a.* from mps_job a  where jb_crmid = @ordbizid and jb_ordid = @ordid and  jb_wsid>0 and jb_sign = 0  and    jb_back = 1 
    else select a.* from mps_job a   where jb_crmid = @ordbizid and jb_ordid = @ordid and jb_wsid>0 and jb_sign = 0  and jb_back = 1  

--明细表数据
 
----以下是数据源 
select '手续办理人' datasource, * from mps_employee
where cu_id=@ord_sxcuid

--- 以下获取用户自定义SQL afterloaded 

select '负责人' datasource , (ord2_fzcuid-@useruid) editable ,* 
from order_140_102,order_140
where ord2_ordid=@ordid
and ord_id=@ordid 
and (ord2_fzcuid = @useruid or @useruid = ord_createcuid or @useruid=115 or   @useruid=185)


--- 以上获取用户自定义SQL afterloaded 
set nocount off
end