create or replace function SF_YF_GETZSMSL ( P_YPNM00 in number , P_CFLSH0 in varchar2, P_YPZSL0 in number , --(X.YPZSL0-X.JYSL00)*X.ZHL000 P_ZSMZHL in number , --nvl(B.ZSMZHL,B.K2J000) P_ZSMSL0 in number , --ceil(X.YPZSL0*X.ZHL000/B.K2J000) P_TYPE00 in number default 0 , --0发药1退药 P_CXCFID in number default 0 --正单CFID00 ) return number as -- modification history -- Person Date Comments -- luowf 2025.04.07 create 门诊追溯码获取应扫数量 for YF13-20250401-002 -- luowf 2025.04.11 增加入参P_TYPE00,P_CXCFID 扩展退药 YF13-20250410-003 -- luowf 2025.04.30 用YZLB00+YPQLPC 索引来查询 for YF13-20250430-001 V_CLSL00 YF_CFZSMX.YPZSL0%type; v_count number(5); v_count1 number(5); V_YPZSL0 YF_CFZSMX.YPZSL0%type; begin if P_TYPE00=0 then --发药 if mod(P_YPZSL0,P_ZSMZHL)>0 then --有拆零 select nvl(sum(YPZSL0),0) into V_CLSL00 from YF_CFZSMX where YZLB00='0' and YPQLPC=P_CXCFID and MZZYBZ='0' and CFLSH0=P_CFLSH0 and YPNM00=P_YPNM00 and SFCLBZ='1'; select nvl(sum(YPZSL0),0) into V_YPZSL0 from YF_CFZSMX where YZLB00='0' and YPQLPC=P_CXCFID and CFLSH0=P_CFLSH0 and YPNM00=P_YPNM00; select count(1) into v_count from YF_CFZSMX k where YZLB00='0' and YPQLPC=P_CXCFID and k.CFLSH0 = P_CFLSH0 and k.YPNM00 = P_YPNM00; if floor(P_ZSMSL0)>0 then --有零有整 --select count(1) into v_count from YF_CFZSMX k where k.CFLSH0 = P_CFLSH0 and k.YPNM00 = P_YPNM00; if V_CLSL00>0 and mod(P_YPZSL0,P_ZSMZHL)-V_CLSL00>0 then --有扫过了且拆零还没全匹配完所以还要再+1 return ceil(P_ZSMSL0)+1; elsif V_CLSL00>0 and mod(P_YPZSL0,P_ZSMZHL)=V_CLSL00 and P_YPZSL0>V_YPZSL0 then if v_count=ceil(P_ZSMSL0) then return v_count+1; end if; if v_count>ceil(P_ZSMSL0) then return v_count+1; end if; return ceil(P_ZSMSL0); elsif P_YPZSL0=V_YPZSL0 then return v_count; else return ceil(P_ZSMSL0); end if; else --纯拆零 if V_CLSL00>0 and mod(P_YPZSL0,P_ZSMZHL)-V_CLSL00>0 then --有扫过了且拆零还没全匹配完所以还要再+1 if v_count>ceil(P_ZSMSL0)+1 then return v_count+1; end if; return ceil(P_ZSMSL0)+1; else if v_count>ceil(P_ZSMSL0) then return v_count; end if; return ceil(P_ZSMSL0); end if; end if; else --没有拆零的,直接返回了 return ceil(P_ZSMSL0); end if; else --退药 select sum(YPZSL0) into V_YPZSL0 from YF_CFZSMX where YZLB00='0' and YPQLPC=P_CXCFID and MZZYBZ='0'; select count(1) into v_count from YF_CFZSMX where YZLB00='0' and YPQLPC=P_CXCFID and MZZYBZ='0'; if P_YPZSL0=V_YPZSL0 then return v_count; end if; return ceil(P_ZSMSL0); end if; end;