存储过程(工作随笔)门诊统筹

USE [420923_2013]
GO
/****** Object:  StoredProcedure [dbo].[Pro_MzTc_GetCanFee]    Script Date: 01/29/2013 11:00:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/*重新计算报销项目*/
ALTER       proc [dbo].[Pro_MzTc_GetCanFee]
  @OrgCode varchar(12),      --服务机构
  @CoopMedCode varchar(12),  --合作医疗证号
  @IDNo varchar(10),         --个人序号
  @DiagNo varchar(10),       --就医序号
  @AreaCode varchar(12),     --行政区划
  @FreeFeeOld numeric(8,2),  --修改前的报销金额
  @MzTcFlag char(1),         --门诊统筹方式 1.县;2.乡镇;3.村;4.组
  @Type varchar(1),          --可报销费用类型 1.登记 2.一审 3.二审
  @dFee numeric(8,2) output
as
declare @Bl numeric(6,2),@MaxFee numeric(10,4),@TotalFee numeric(10,4),@FreeFee numeric(10,4),@FreeFee_Ts numeric(10,4),@AfterFreeFee numeric(10,4),@ZlfAfterFreeFee numeric(10,4),@dSumFreeFee numeric(8,2),@dFreeFee numeric(8,2)
declare @Indate varchar(10),@SGrade varchar(1),@CanFreeFee_Ts numeric(8,2),@HospitalCode varchar(12)
select @Bl=0,@MaxFee=0,@TotalFee=0,@FreeFee=0,@FreeFee_Ts=0,@AfterFreeFee=0,@ZlfAfterFreeFee=0,@dSumFreeFee=0,@dFreeFee=0,@dFee=0,@SGrade='0',@CanFreeFee_Ts=0

declare @dYearMaxFee numeric(8,2),@YearFlag char(1),@Digital int,@BlXz numeric(6,2),@BlCun numeric(6,2)
select @dYearMaxFee = 0,@YearFlag='1',@Digital=2,@BlXz=0,@BlCun=0
declare @DayMaxFeeXz numeric(10,4),@DayMaxFeeCun numeric(10,4),@DayFlag char(1),@MzTcMxb char(1),@Count int
select @DayMaxFeeXz=0,@DayMaxFeeCun=0,@DayFlag='1',@MzTcMxb='2',@Count=0
declare @Zlf varchar(20),@ZlfFlag char(1),@ZlfCount int,@ZlfYear char(1),@ZlFreeFee numeric(10,4)
select @Zlf='',@ZlfFlag='',@ZlfCount=0,@ZlfYear='',@ZlFreeFee=0
declare @FeeNo int,@dPrice numeric(10,4),@dLimitPrice numeric(10,4),@dNum numeric(6,2),@FeeType varchar(6)
select @FeeNo=0,@dPrice=0,@dLimitPrice=0,@dNum=0,@FeeType=''

--取当次就诊时间,医疗机构级别
select @Indate=convert(varchar(10),a.Indate,120),@SGrade=b.Grade,@HospitalCode=a.HospitalCode   
from o_cbmzbxdj a,p_Ylfwdw b
where a.HospitalCode=b.OrgCode and a.OrgCode=@OrgCode and a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.DiagNO=@DiagNO

if (@SGrade <> '1') and (@SGrade <> '4')
begin
    set @dFee=0
    return
end

--发生慢性病的不能报销
select @MzTcMxb=ParamValue from p_ParamLists where ParamName='MzTcMxb'
if @MzTcMxb='1'
begin
  select @Count=isnull(count(coopmedcode),0) from c_MxbCardInfos where coopmedcode=@CoopMedCode  and idno=@IDNo
  if @Count > 0
  begin
    set @dFee=-997
    return
  end
end

--年封顶及类别,小数位数
select @dYearMaxFee=ParamValue from p_ParamLists where ParamName='MzTcYearMaxFee'
select @YearFlag=ParamValue from p_ParamLists where ParamName='MzTcYearFlag'
select @Digital=ParamValue from p_ParamLists where ParamName='MzTcDigital'
--报销比例
select @BlXz=ParamValue from p_ParamLists where ParamName='MzTcBlXz'
select @BlCun=ParamValue from p_ParamLists where ParamName='MzTcBlCun'
--日封顶及类别
select @DayMaxFeeXz=ParamValue from p_ParamLists where ParamName='MzTcDayMaxFeeXz'
select @DayMaxFeeCun=ParamValue from p_ParamLists where ParamName='MzTcDayMaxFeeCun'
select @DayFlag=ParamValue from p_ParamLists where ParamName='MzTcDayFlag'
--一般诊疗费,7元/天,是否单独封顶,是否纳入年封顶
select @Zlf=ltrim(rtrim(ParamValue)) from p_ParamLists where ParamName='MzTcZlf'
select @ZlfYear=ParamValue from p_ParamLists where ParamName='MzTcZlfYear'
select @ZlfFlag=ParamValue from p_ParamLists where ParamName='MzTcZlfFlag'

--乡镇卫生院
if (@SGrade = '1')
begin
  set @Bl=@BlXz/100
  set @MaxFee =@DayMaxFeeXz
end
--村卫生室
if (@SGrade = '4')
begin
  set @Bl=@BlCun/100
  set @MaxFee =@DayMaxFeeCun
end

--取当前患者当日已发生的费用
if @DayFlag='1'
begin
  select @AfterFreeFee=isnull(sum(c.FreeFee),0)
  from (
    select isnull(sum(FreeFee),0) as FreeFee  --已审核的
    from s_cbmzbxdj
    where CoopMedCode=@CoopMedCode and IDNo=@IDNo and convert(varchar(10),Indate,120)=@Indate and mzflag='2'
    union all
    select isnull(sum(FreeFee),0) as FreeFee
    from o_cbmzbxdj
    where CoopMedCode=@CoopMedCode and IDNo=@IDNo and convert(varchar(10),Indate,120)=@Indate and mzflag='2'
    union all                        
    select -isnull(sum(FreeFee),0) as FreeFee 
    from o_cbmzbxdj
    where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo
) c
end
else
begin
  select @AfterFreeFee=isnull(sum(c.FreeFee),0)
  from (
    select isnull(sum(FreeFee),0) as FreeFee  --已审核的
    from s_cbmzbxdj
    where CoopMedCode=@CoopMedCode and convert(varchar(10),Indate,120)=@Indate and mzflag='2'
    union all
    select isnull(sum(FreeFee),0) as FreeFee
    from o_cbmzbxdj
    where CoopMedCode=@CoopMedCode and convert(varchar(10),Indate,120)=@Indate and mzflag='2'
    union all                        
    select -isnull(sum(FreeFee),0) as FreeFee 
    from o_cbmzbxdj
    where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo) c
end

--取可报销费用
if @Type = '1'
  select @TotalFee=isnull(sum(Fee),0),@FreeFee=isnull(sum(CanFreeFee),0) from o_CbMzTcGrDjZb
  where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode
    and IDNo=@IDNo and DiagNo=@DiagNO
else if @Type = '2'
  select @TotalFee=isnull(sum(Fee),0),@FreeFee=isnull(sum(CanFreeFee-KnockOffFee1),0) from o_CbMzTcGrDjZb
  where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode
    and IDNo=@IDNo and DiagNo=@DiagNO
else if @Type = '3'
  select @TotalFee=isnull(sum(Fee),0),@FreeFee=isnull(sum(CanFreeFee-KnockOffFee),0) from o_CbMzTcGrDjZb
  where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode
    and IDNo=@IDNo and DiagNo=@DiagNO

--特殊项目:一般诊疗费
if ((@SGrade = '1') or (@SGrade = '4')) and (@Zlf<>'')
begin
  --判断本次是否有一般诊疗费及金额
  select @FeeNo=isnull(FeeNo,0),@FeeType=FeeTypeCode,@dPrice=Price,@dLimitPrice=LimitPrice,@dNum=Num from o_CbMzTcGrDjZb where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and ItemCode=@Zlf
  if @FeeNo > 0
  begin
    exec Pro_MzTc_GetItem @OrgCode,@CoopMedCode,@IDNo,@DiagNo,@FeeNo,'1',@FeeType,@Zlf,@dPrice,@dLimitPrice,@dNum,@CanFreeFee_Ts output
    update o_CbMzTcGrDjZb set CanFreeFee=@CanFreeFee_Ts
     where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and FeeNo=@FeeNo
    --取可报销费用   
    if @Type = '1'
 select @FreeFee=isnull(sum(CanFreeFee),0) from o_CbMzTcGrDjZb where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNO
    else if @Type = '2'
 select @FreeFee=isnull(sum(CanFreeFee-KnockOffFee1),0) from o_CbMzTcGrDjZb where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNO
    else if @Type = '3'
 select @FreeFee=isnull(sum(CanFreeFee-KnockOffFee),0) from o_CbMzTcGrDjZb  where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNO
  end
 --判断是否超过每日次封顶线,,本次录入一般诊疗且报销(当天未发生过一般诊疗费,@ZlfAfterFreeFee=0)
  if (@FeeNo>0) and (@CanFreeFee_Ts<>0)
  begin
    --单独日封顶
    if (@ZlfFlag ='1')
    begin
      if @MaxFee > 0
      begin
        if @AfterFreeFee >= @MaxFee
          set @dFee=0
        else
        begin
          if (@FreeFee-@CanFreeFee_Ts)*@Bl + @AfterFreeFee > @MaxFee
            set @dFee = @MaxFee - @AfterFreeFee
          else
            set @dFee = (@FreeFee-@CanFreeFee_Ts)*@Bl
        end
      end
      else
        set @dFee = (@FreeFee-@CanFreeFee_Ts)*@Bl

      set @dFee=@dFee + @CanFreeFee_Ts
    end
    else
    begin
      --纳入日封顶
      if @MaxFee > 0
      begin
        if @AfterFreeFee >= @MaxFee
        begin
          set @dFee=-998
          if @CanFreeFee_Ts > 0
          begin
       set @CanFreeFee_Ts = 0
            update o_cbmztcgrdjzb set CanFreeFee=@CanFreeFee_Ts where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and ItemCode=@Zlf and CanFreeFee<>0
          end
        end
        else
        begin
          if (@FreeFee-@CanFreeFee_Ts)*@Bl + @CanFreeFee_Ts + @AfterFreeFee > @MaxFee
   begin
            set @dFee = @MaxFee - @AfterFreeFee
        if @dFee < @CanFreeFee_Ts
     begin
       set @CanFreeFee_Ts = @dFee
       update o_cbmztcgrdjzb set CanFreeFee=@CanFreeFee_Ts where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and ItemCode=@Zlf and CanFreeFee<>0
     end
   end
          else
            set @dFee = (@FreeFee-@CanFreeFee_Ts)*@Bl + @CanFreeFee_Ts
        end
      end
      else
        set @dFee = (@FreeFee-@CanFreeFee_Ts)*@Bl + @CanFreeFee_Ts

    end
  end
  else
  begin
    --单独日封顶
    if (@ZlfFlag ='1')
    begin
      --当天已发生的一般诊疗费
      select @ZlfAfterFreeFee=isnull(sum(aa.ZlfFee),0) from
      (select isnull(a.ZlfFee,0) as ZlfFee 
      from s_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and convert(varchar(10),a.Indate,120)=@Indate and a.mzflag='2'
      union all                        
      select isnull(a.ZlfFee,0) as ZlfFee 
      from o_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and convert(varchar(10),a.Indate,120)=@Indate and a.mzflag='2'
      union all                        
      select -isnull(a.ZlfFee,0) as ZlfFee 
      from o_cbmzbxdj a
      where a.OrgCode=@OrgCode and a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.DiagNo=@DiagNo) aa

      if @MaxFee > 0
      begin
        if (@AfterFreeFee-@ZlfAfterFreeFee)>= @MaxFee
          set @dFee=-998
        else
        begin
          if @FreeFee*@Bl + (@AfterFreeFee-@ZlfAfterFreeFee) > @MaxFee
            set @dFee = @MaxFee - (@AfterFreeFee-@ZlfAfterFreeFee)
          else
            set @dFee = @FreeFee*@Bl
        end
      end
      else
        set @dFee = @FreeFee*@Bl
    end
    else
    begin
      --纳入日封顶
      if @MaxFee > 0
      begin
        if @AfterFreeFee >= @MaxFee
          set @dFee=-998
        else
        begin
          if @FreeFee*@Bl + @AfterFreeFee > @MaxFee
            set @dFee = @MaxFee - @AfterFreeFee
          else
            set @dFee = @FreeFee*@Bl
        end
      end
      else
        set @dFee = @FreeFee*@Bl
    end
  end
end
else
begin
  --判断是否超过每日次封顶线
  if @MaxFee > 0
  begin
    if @AfterFreeFee >= @MaxFee
      set @dFee=-998
    else
    begin
      if @FreeFee*@Bl + @AfterFreeFee > @MaxFee
        set @dFee = @MaxFee - @AfterFreeFee
      else
        set @dFee = @FreeFee*@Bl
    end
  end
  else
    set @dFee = @FreeFee*@Bl
end
set @dFee=round(@dFee,@Digital)

if (@dFee>0) and (@dYearMaxFee>0)
begin
  --取当前患者全年已发生的费用
  if @YearFlag='1'
    select @dSumFreeFee=isnull(sum(c.FreeFee),0)
    from (
      select isnull(sum(FreeFee),0) as FreeFee  --已审核的
      from s_cbmzbxdj
      where CoopMedCode=@CoopMedCode and IDNo=@IDNo and mzflag='2'
      union all
      select isnull(sum(FreeFee),0) as FreeFee
      from o_cbmzbxdj
      where CoopMedCode=@CoopMedCode and IDNo=@IDNo and mzflag='2'
      union all                        
      select -isnull(sum(FreeFee),0) as FreeFee 
      from o_cbmzbxdj
      where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo) c
  else
    select @dSumFreeFee=isnull(sum(c.FreeFee),0)
    from (
      select isnull(sum(FreeFee),0) as FreeFee  --已审核的
      from s_cbmzbxdj
      where CoopMedCode=@CoopMedCode and mzflag='2'
      union all
      select isnull(sum(FreeFee),0) as FreeFee
      from o_cbmzbxdj
      where CoopMedCode=@CoopMedCode and mzflag='2'
      union all                        
      select -isnull(sum(FreeFee),0) as FreeFee 
      from o_cbmzbxdj
      where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo) c

  --一般诊疗不纳入年封顶
  if (@Zlf<>'') and (@ZlfYear ='2')
  begin
    if @YearFlag='1'
      select @ZlFreeFee=isnull(sum(aa.ZlfFee),0) from
      (select isnull(a.ZlfFee,0) as ZlfFee
      from s_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.mzflag='2'
      union all                        
      select isnull(a.ZlfFee,0) as ZlfFee
      from o_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.mzflag='2'
      union all                        
      select -isnull(a.ZlfFee,0) as ZlfFee 
      from o_cbmzbxdj a
      where a.OrgCode=@OrgCode and a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.DiagNo=@DiagNo) aa
    else
      select @ZlFreeFee=isnull(sum(aa.ZlfFee),0) from
      (select isnull(a.ZlfFee,0) as ZlfFee
      from s_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.mzflag='2'
      union all                        
      select isnull(a.ZlfFee,0) as ZlfFee
      from o_cbmzbxdj a
      where a.CoopMedCode=@CoopMedCode and a.mzflag='2'
      union all                        
      select -isnull(a.ZlfFee,0) as ZlfFee 
      from o_cbmzbxdj a
      where a.OrgCode=@OrgCode and a.CoopMedCode=@CoopMedCode and a.IDNo=@IDNo and a.DiagNo=@DiagNo) aa


    set @dSumFreeFee=@dSumFreeFee-@ZlFreeFee
    --判断普通金额是否超过年封顶
    if (@dSumFreeFee>=@dYearMaxFee)
      set @dFee=@CanFreeFee_Ts
    else
    begin
      if (@dFee-@CanFreeFee_Ts)+@dSumFreeFee>@dYearMaxFee set @dFee=@dYearMaxFee-@dSumFreeFee+@CanFreeFee_Ts
    end
  end
  else
  begin
    --一般诊疗纳入年封顶
    if (@dSumFreeFee>=@dYearMaxFee)
    begin
      set @dFee=-999
      if @CanFreeFee_Ts > 0
      begin
        set @CanFreeFee_Ts=0
        update o_cbmztcgrdjzb set CanFreeFee=@CanFreeFee_Ts where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and ItemCode=@Zlf and CanFreeFee<>0
      end
    end
    else
    begin
      if @dFee+@dSumFreeFee>@dYearMaxFee
      begin
        set @dFee=@dYearMaxFee-@dSumFreeFee
 if @dFee < @CanFreeFee_Ts
 begin
          set @CanFreeFee_Ts = @dFee
       update o_cbmztcgrdjzb set CanFreeFee=@CanFreeFee_Ts where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode and IDNo=@IDNo and DiagNo=@DiagNo and ItemCode=@Zlf and CanFreeFee<>0
 end
      end
    end
  end
end

set @dFreeFee=@dFee
if @dFreeFee<0 set  @dFreeFee=0

--更新o_CbMzBxDj
if @Type = '1'
  update o_CbMzBxDj set TotalFee=@TotalFee,CanFreeFee=@FreeFee,FreeFee=@dFreeFee,ZlfFee=@CanFreeFee_Ts 
    where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode  and IDNo=@IDNo and DiagNo=@DiagNo
else if @Type = '2'
  update o_CbMzBxDj set CheckOneFee=@dFreeFee 
    where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode  and IDNo=@IDNo and DiagNo=@DiagNo
else if @Type = '3'
  update o_CbMzBxDj set CheckFee=@dFreeFee 
    where OrgCode=@OrgCode and CoopMedCode=@CoopMedCode  and IDNo=@IDNo and DiagNo=@DiagNo


 

原文地址:https://www.cnblogs.com/520cc/p/2881061.html