create or replace procedure SP_YJ_YJSQWH
(
  Popflag  in     number                       ,  --操作内容 1修改 2增加 3删除
  Pyjdjh0  in out number                       ,  --操作的医技单据号
  Pzyghid  in     number                       ,  --住院挂号ID
  Pmzzybz  in     varchar2                     ,  --门诊住院标志
  Pzlxmid  in     number                       ,  --诊疗项目id
  Pzxmid0  in     varchar2                     ,  --子项目id串,如果没有传入参数，表示使用所有明细项
  pzdid00  in     varchar2                     ,  --诊断id
  pzdmc00  in     varchar2                     ,  --诊断名称
  Pbbid00  in     varchar2                     ,  --标本ID
  Pbh0000  in     varchar2                     ,  --特殊病种编号
  Pyjlrbz  in     varchar2                     ,  --医技录入标志
  Pjzbz00  in     varchar2                     ,  --急诊标志
  Pkdys00  in     number                       ,  --开单医生
  Pkdksbh  in     number                       ,  --开单科室编号
  Pkdbq00  in     number                       ,  --开单病区
  Pyjksbh  in     number                       ,  --医技科室编号
  Psl0000  in     number                       ,  --数量
  Pbz0000  in     varchar2                     ,  --备注
  Pfjdjbz  in     char                         ,  --附加单据 0主单1采血费2试管费3挂号费4诊查费5病历本6预约挂号7代煎9其他子单
  Pczy000  in     number                       ,  --操作员
  Pczyks0  in     number                       ,  --操作员科室
  Pcommit  in     varchar2   default 'N'       ,  --是否提交
  Pzxcgbz  out    number                       ,  --执行成功标志 1成功 0失败
  Ptsxx00  out    varchar2                     ,  --执行结果提示信息，成功的情况下也可能有警告信息
  Pxxxx00  out    varchar2                     ,  --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
  Pywmxid  in     number     default null      ,  --业务明细ID  Popflag=3时 可能用到
  Pbwmc00  in     varchar2   default null      ,  --部位名称
  Pfyjdjh  in     number     default null      ,  --父医技单据号
  Pcxjj00  in     number     default 1         ,  --重新计价
  Pyzcfid  in     varchar2   default null      ,  --医嘱处方ID
  Pssdh00  in     varchar2   default null      ,  --手术单号
  Ptxm000  in     varchar2   default null      ,  --条形码
  Pwzbm00  in     varchar2   default null      ,  --物资编码
  Pssbm00  in     varchar2   default null      ,  --手术编码
  Pssmc00  in     varchar2   default null      ,  --手术名称
  Pssyjdh  in     varchar2   default null      ,  --手术医技单据号
  Pwzbh00  in     varchar2   default null      ,  --物资备货
  Ptqpc00  in     number     default 0         ,  --提取批次
  Pxmzk00  in     number     default 1         ,  --项目折扣（基数)
  Pyjzhje  in     number     default 0         ,  --医技折后金额
  Ptcid00  in     number     default null      ,  --套餐ID
  Pmbmxid  in     number     default null      ,  --模板明细ID
  PSFTJ00  in     varchar2   default 'N'       ,  --是否体检 N:否 Y:是
  PSFZF00  in     varchar2   default '0'       ,  --是否自费 1:是;
  PYYGXJG  in     varchar2   default '0'       ,  --是否引用共享结果 1:是 其他否
  Pbwid00  in     number     default 0         ,  --部位ID, BM_ZDBW00.BWID00
  Pzlxmjc  in     varchar2   default null      ,  --诊疗项目简称
  Pyexm00  in     varchar2   default null         --婴儿姓名
)
-- MODifICATION HisTORY
-- Person      Date        Comments
-- qks         2015.07.17  手术室录入的医技单对应的开单科室编号，应该为手术医生对应医生组所在科室；麻醉科录入的申请科室还是属于麻醉科
-- qks         2015.03.10  新增入参
-- qks         2014.03.06  三级库存耗材管理改造:生成住院试管费附加费,YJ_YW0000.YZCFID值没写入，也要写入；
-- qks         2014.12.26  XK_SXSQ00.BQMC00值写入
-- qks         2014.08.18  借床病人 执行收入归被借床所属住院科室;出院登记状态不允许开单;
-- qks         2014.06.10  如果手术单已登记,那么手术对应医技单最少要保留一个,不允许删除,只允许手术撤消操作;
-- qks         2014.02.27  住院检验项目收“一次性采血管”费用:条码已生成情况下,不允许并管;
-- qks         2014.01.24  根据BM_ZLZD00.JYTZBH自动合并收取“一次性采血管”费用:门诊按未收费,住院按未执行合并；门诊和住院急诊单据不合并;
-- qks         2014.01.22  修改:取未执行的附加费数量,同时判断表HIS_REQUISITION状态
-- qks         2014.01.15  修改:取未执行的附加费数量
-- lintj       2014.01.09  附加费产生按照JZBZ00区分
-- lintj       2013.12.18  修改附加费的生成方法 SFQR00
-- lintj       2013.09.25  修改试管费只计一个的BUG
-- qks         2013.09.07  科室耗材退预扣处理SP_KS_HCJJCX
-- qks         2013.08.19  考虑转区未接收情况:取转区前科室
-- qks         2013.08.08  增加入参Pssdh00:手术室开单,住院Vdqks00取手术单对应的申请科室;
-- lintj       2013.07.17  加 Vyszid0
-- lintj       2013.07.16  加 Vkdksbh
-- lintj       2013.07.12  写 DQKS00
-- lintj       2013.06.24  加 SP_YJ_YjComverToYz
-- lintj       2013.05.28  住院非本人或本科室开单的允许删除
-- lintj       2013.05.24  病区的抽血费执行科室改到病区
-- lintj       2013.03.25  加 Pyzcfid
-- lintj       2012.11.05  modify: XT_BBDCZL改成XT_ZLDCZL
-- lintj       2012.09.01  modify: 血库子单不能产生新的输血申请单
-- JXQ         2012.09.19  Add: 不能删除非本人新增的记录
-- lintj       2012.09.01  modify: 增加附加单据的情况
-- lintj       2012.08.08  create
-- yangy       2012.09.27  传入诊断ID没有效果，如果没有诊断不需要自动补诊断
-- jlg         2012.10.10  SS_YW0000.SSZT00由原来的'6'改为'0'
-- zhangwz     2012.10.23  因为程序里面的 YJ_YW0000.LRY000 数据基本为null,导致删除的限制没有起作用，增加判断 YJ_YW0000.KDKSBH<>Pczyks0 时不让删除
-- zhangwz     2012.10.31  更改对于输血类单据进行 update YJ_YW0000.XMZT00='0' 而导致执行时报项目没有定价的错误,更改为 update YJ_YW0000.XMZT00='1'.
-- xuzw        2016.01.19  区分带出的诊疗项目是门诊还是住院 YJ9-20160119-001
-- zhaoxz      2016.01.29  新增Pwzbm00，Pssbm00，Pssmc00，Pssyjdh，Pwzbh00，Ptqpc00 入参，脚本交叉整理 MZYS9-20160129-001
-- qks         2016.08.12  省人民医院：麻醉科要求门诊病人增加麻醉医技费用,要求所选择的挂号记录是有效挂号才允许增加。by SSMZ9-20160812-001
-- qks         2016.09.19  北京优翔生命院:套餐改造需求,新增入参Pyjzhje。for YJ9-20160921-001
-- zhaoxz      2016.09.23  北京优翔生命院，整理冲突脚本Pxmzk00,增加入参Ptcid00，Pmbmxid for MZYS9-20160824-002
-- zhengzk     2016.12.26  增加参数YJ_KDKSSFSQKS控制由医技科室直接开单时门诊病人保存的开单科室 for YJ9-20161213-001
-- qks         2017.01.01  增加参数控制检验项目数量是否允许大于1; for YJ9-20170101-001
-- zhangyc     2017.01.14  增加入参PSFTJ00是否体检 N:否 Y:是 MZSF9-20170106-003
-- qks         2017.03.04  可以根据年龄和性别进行开单限制; for XMGL9-20170217-001
-- qks         2017.06.15  要求出院登记病人代煎费也能开单; for YF9-20170615-002
-- qks         2017.07.04  省人民：出现个别病人抽血费漏收问题; for YJ9-20170705-001
-- qks         2017.08.24  YJ_YW0000.YEXM00字段值写入婴儿姓名; for YJ9-20170824-001
-- ruanbh      2017.09.12  福州福清市医院新院：项目开单科室限制 for MZYS9-20170908-002
-- huanglw     2018.05.11  省人民医院：新增诊疗项目时不自动引用原有申请单 for MZDZBLS-20180507-001
-- linyx       2018.05.30  婴儿药品的用药附加费，要写入婴儿姓名 for BQHS9.0-20180528-001
-- liwm        2018.06.05  统一添加调用日志 for YJ9-20180605-001
-- qks         2018.06.28  取最大ZY_BRXXB0.ZYID00值,即MAX(ZYID00),取消入院病人不参与判断; for YJ9-20180628-001
-- qks         2018.11.16  诊疗项目附带出诊疗项目维护:现要求检查项目也可以带出维护项目; for YJ9-20181115-002
-- gzj         2018.11.28  删除冲销单据的同时进行单据的退费撤销处理 for YJ9-20181122-001
-- qks         2019.03.01  新增检验抽血同组编号字段BM_ZLZD00.CXTZBH：空值合并收1次（保留原来）,非空相同值合并收1次，不同值情况下都另行再收1次; for YJ9-20190327-001
-- jlg         2019.04.12  Vzxmid0长度由255加长为2000 YJ9-20190412-002
-- qks         2019.04.25  住院检验中间表从原HIS_REQUISITION改用YJ_BBZJB0; for BQHS9.0-20190424-002
-- gzj         2019.05.18  增加住院病人项目申请单写入医技业务表处理 YJ9-20190518-001
-- gzj         2019.06.25  多个项目使用同一个申请单时不能写入医技业务表问题处理 YJ9-20190625-001
-- qks         2019.07.11  处理门诊医生开出手术后撤销，手麻系统记录没有同步撤销问题; for SSMZ9-20190711-002
-- gzj         2019.07.12  修改操作时只更新修改的信息 YJ9-20190712-001
-- liwm        2019.10.21  省人民输血系统上线不写XK_SXSQ00表 YJ9-20190824-002
-- dongxb      2019.10.22  修改当医嘱为婴儿医嘱时，诊疗项目性别限制应判断婴儿性别，不能直接判断患者性别 for BQHS9.0-20190916-002
-- qks         2019.10.24  修正SSMZ9-20190711-002修改记录; for SSMZ9-20191030-001
-- linshu      2019.12.10  增加入参是否自费:PSFZF00 by YJ9-20191210-001
-- ruanbh      2019.12.31  增加入参是否引用共享结果:PYYGXJG MZYS9-20191120-001
-- qks         2020.01.20  同组不同管(耗材诊疗项目ID不同)并管，删除1个0元管; for YJ9-20200203-001
-- linzetao    2020.07.20  处理启用部位收费时按部位对应的收费项目进行收费       YJ9-20200716-001
-- linzetao    2020.09.03  修正修改医技单时YJ_YW0000.TQPC00被重写成0问题      YJ9-20200826-001
-- luowf       2020.10.15  处理手术医嘱kdksbh=0                 SSMZ9-20201015-001
-- linshu      2020.11.02  对参数SS_MZKDKSPDYXGH扩展进行了处理  SSMZ9-20201030-001
-- linyx       2020.12.09  医嘱生成单据的时候，有录入临床情况或目的 要写入到yj_yw0000 for BQHS9.0-20201123-003 (YJ9-20201127-001转提交)
-- huangzw     2020.12.28  诊疗页签可以手动输入描述性医嘱  for  MZYSSJ5-20201126-002  (YJ9-20201228-002转提交)
-- jlg         2021.01.15  项目执行年龄限制的(BM_ZLZD00.NLXZ00)，对应婴儿医嘱不进行限制（当前直接不限制，若有需要，可再加参数处理） YJ9-20210113-001
-- linzetao    2021.01.18  增加入参Pyexm00(婴儿姓名)    YJ9-20210114-001
-- linzetao    2021.02.23  判断Pcxjj00为null时赋默认值1      YJ9-20210223-001
-- jlg         2021.02.24  针对Pzlxmjc传入为空会导致报错的处理 SSMZ9-20210224-001
-- linshu      2021.03.30  修正转归档错误将Pzlxmjc入参位置有原来放在Pmbmxid后面修正为放到Pbwid0后面 YJ9-20210330-001 
-- qiulf       2021.04.14  BM_XMBZ00指定特殊病种不允许开某些诊疗字典的规则库进行判断 ，有在规则库中的不允许保存并提示 by   YJ9-20210413-002 
-- qks         2021.04.14  新增：通用字典表(ZDMC00='采血费诊疗项目')维护自动带出检验项目采血费;04.17增加标本名称含'血'以及不等'末梢血'判断; for YJ9-20210409-001
-- qks         2021.05.14  修正YJ9-20190327-001：当存在多个项目可抽血同组且各项目的抽血费或试管费 数量不同时,要求取当前可抽血同组项目中的数量最大值; for YJ9-20210328-001
-- qiulf       2021.06.16  增加参数YJ_TSBZXMXZ控制医技项目增加修改时特殊病种限制方式(关联参数YS_TSBZXZCFXM),0:BM_XMBZ00有维护的诊疗项目无法保存 1：BM_XMBZ00没有维护的诊疗项目无法保存 for   YJ9-20210615-002 
-- chenHeyi    2021.07.08  参数YJ_JYXMSL扩展为两段，第1段为限制检验项目数量最大值，与第2段之间用分号分隔；第2段为诊疗项目ID，诊疗项目ID之间逗号分隔  for YJ9-20210706-001
-- jlg         2021.07.26  重新梳理参数YJ_SFBSCSXXT的处理，当参数值为M时，原来有维护的科室写XK_SXSQ00，现在改为有维护的科室不写XK_SXSQ00表   YJ9-20210720-001
-- chenHeyi    2021.08.11  参数YJ_CKBRKYBLDYJXM,出科病人可以补入的医技项目,输入诊疗项目ID,多个用逗号分隔,默认为-1     YJ9-20210721-001
-- qks         2021.08.25  属于输血诊疗项目（通过系统参数XK_SXZLXMID维护）开单保存后，并且参数YJ_SFBSCSXXT不启用情况下，项目状态要求从申请改为计价; for YJ9-20210825-002
-- jlg         2021.11.02  如果是手术费用划价录入的，YJ_YW0000.KDBQ00优先取SS_YW0000.SQBQ00的值（即按照手术申请单收费时，开单病区取手术申请单时的病区，不取转区后的病区）   SSMZ9-20211102-002
-- qks         2022.03.20  标本存在带出项目ID 可带出对应诊疗项目 for XMGL9EX-20220320-001 
-- qks         2022.03.29  调用过程SP_ZY_DKFMX0,计算患者账户的待扣费医技单和处方单自付金额; for ZYSF9EX-20220329-002
-- qks         2022.04.20  优化存储过程调用参数日志:改为只保存部分关键参数; for MZSF9EX-20220420-002
-- jlg         2022.04.24  删除操作限制判断增加nvl(Vyw0000.JZDH00,0)>=0条件，以便取消结算删除之前未扣费医技对应的冲销单时能通过 MZSF9EX-20220411-004
-- chenHeyi    2022.05.20  增加参数YJ_YSKDXMSFKSC：医生开单的项目是否可删除（门诊与住院），0均不可删除，1均可删除，2门诊不可删除，住院可以，3住院不可删除,门诊可以；默认为1  YJ9EX-20220513-002
-- chenHeyi    2022.05.27  删除操作，在判断医生开单的医技项目，要排除冲销记录     MZSF9EX-20220526-002
-- jiangjh     2022.06.06  住院出院结算-补费-允许录入医技单; for ZYSF13-20220606-001
-- liwm        2022.07.16  lry000
as
   Vczrq00             char(8);
   Vczsj00             char(8);
   Vdelall             boolean;
   Vyjdsx0             number(1);
   Vzlxmjc             BM_ZLZD00.ZLXMJC%type;      --诊疗项目简称
   Vlbbh00             BM_ZLZD00.LBBH00%type;
   Vdw0000             BM_ZLZD00.DW0000%type;
   Vfbzxbz             BM_ZLZD00.FBZXBZ%type;
   Vzyhghh             ZY_BRXXB0.ZYH000%type;
   Vbrxm00             ZY_BRXXB0.XM0000%type;
   Vxb0000             ZY_BRXXB0.XB0000%type;
   Vcsrq00             ZY_BRXXB0.CSRQ00%type;
   Vch0000             ZY_BRXXB0.RYCWH0%type;
   Vkdbq00             ZY_BRXXB0.DQBQ00%type;
   Vbrzt00             ZY_BRXXB0.BRZT00%type;
   Vzyid00             ZY_BRXXB0.ZYID00%type;
   Vzldccf             YJ_YW0000.ZLDCCF%type;
   Vzdmc00             YJ_YW0000.ZDMC00%type;
   Vsqid00             YJ_YW0000.SQID00%type;
   Vmc0000             BM_TSBZB0.MC0000%type;      --名称变量
   Vczyxm0             BM_YGBM00.ZWXM00%type;      --操作员姓名
   Vkdysxm             BM_YGBM00.ZWXM00%type;
   Vysszks             BM_YGBM00.BMBH00%type;
   Vybzxlb             BM_TSBZB0.YBZXLB%type;      --医保执行类别
   Vbrid00             BM_BRXXB0.BRID00%type;
   Vyblb00             BM_BRXXB0.YBLB00%type;      --医保类别
   Vfbbh00             BM_BRXXB0.FBBH00%type;      --费别编号
   Vxmsqd0             XT_XTCS00.VALUE0%type;      --是否启用医生站的项目申请单 Y:是 N否  默认为N
   Vcfsfmr             XT_XTCS00.VALUE0%type;
   Vzxmid0             varchar2(2000);             --子项目id  255-->2000
   Vyw0000             YJ_YW0000%rowtype;          --医技单
   Vyjdjh0             YJ_YW0000.YJDJH0%type;      --安排日期,检验系统一次开多天的检验单，预计的检验日期
   Vzdid00             YJ_YW0000.ZDID00%type;
   Vssdh00             SS_YW0000.SSDH00%type;
   Vssdh01             SS_YW0000.SSDH00%type;
   Vssxh00             SS_YW0000.SSXH00%type;
   Vsqdh00             XK_SXSQ00.SQDH00%type;
   Vxkbmbh             BM_BMBM00.BMBH00%type;
   Vsxcf00             XK_SXSQ00.SXCF00%type;
   Vsxsz00             XK_SXSQ00.SXSZ00%type;
   Vyjksbh             YJ_YW0000.YJKSBH%type;
   Vdqks00             YJ_YW0000.DQKS00%type;
   Vkdksbh             YJ_YW0000.DQKS00%type;
   Vyszid0             YJ_YW0000.YSZID0%type;
   Vsqks00             SS_YW0000.SQKS00%type;
   VLDLSH0             BQ_BRLDXX.LDLSH0%type;
   VJYTZBH             BM_ZLZD00.JYTZBH%type;
   VZYJDJH             YJ_YJDJGL.ZYJDJH%type;
   VJCKSBH             ZY_BRXXB0.DQKS00%type;      --借床 所属科室编号
   VBQMC00             BM_BMBM00.BMMC00%type;
   VXBBZ00             BM_ZLZD00.XBBZ00%type;
   VNLXZ00             BM_ZLZD00.NLXZ00%type;
   VYEXM00             BQ_YJYZ00.YEXM00%type;
   VCXTZBH             BM_ZLZD00.CXTZBH%type;
   Vsl0000             number;
   Vcs0000             number;
   Vsysdate            date;                       --日期变量
   Vcounter            number;                     --计数器变量
   Vjyxmsl             number(5);                  --检验项目数量允许最大值
   VBRNL00             number(5);                  --患者年龄
   Ecustom             exception;                  --错误变量
   Vyj_scfxmsfzdsczxm  char(1);
   V_MZBLXZSFYS        varchar2(250);              --门诊病历限制收费医生
   Vxxxlog             varchar2(2000);             --参数信息目前用2000
   Vid0000             number;
   VYJ_SFBSCSXXT       XT_XTCS00.VALUE0%type;      --是否开通联轩输血系统的参数
   VYEXB00             BQ_JSXX00.YEXB00%type;      --婴儿性别
   Vtzbtg0             number;                     --同组不同管并管
   VZYJDJH1            YJ_YJDJGL.ZYJDJH%type;      --0元子医技单据号
   Vbwid00             BM_ZDBW00.BWID00%type;      --部位ID
   VBQZY00             YJ_YW0000.BQZY00%type;      --病情摘要
   Vcxjj00             number;                     --重新计价
   VTYZDMC             BM_TYZD00.MC0000%type;
   VZLXMID             BM_ZLZD00.ZLXMID%type;   
   vTSBZXZ             XT_XTCS00.VALUE0%type;
   VYBBRLB             BM_BRXXB0.YBBRLB%type;      --医保病人类别
   YJ_TSBZXMXZ         XT_XTCS00.VALUE0%type;
   VMLXZBZ             BM_TSBZB0.MLXZBZ%type;
   VYJ_JYXMSL          XT_XTCS00.VALUE0%type;      --限制检验项目数量最大值
   VYJ_JYXMSL_1        XT_XTCS00.VALUE0%type;      --YJ_JYXMSL第1段检验项目数量最大值
   VYJ_JYXMSL_2        XT_XTCS00.VALUE0%type;      --YJ_JYXMSL第2段诊疗项目ID串
   VYJ_CKBRKYBLDYJXM   XT_XTCS00.VALUE0%type;      --参数YJ_CKBRKYBLDYJXM,出科病人可以补入的医技项目,输入诊疗项目ID,多个用逗号分隔,默认为-1     YJ9-20210721-001
   VYJ_YSKDXMSFKSC     XT_XTCS00.VALUE0%type;      --参数YJ_YSKDXMSFKSC，门诊医生开单项目或住院护士站提取的项目是否可删除（其它不限制），0均不可删除，1均可删除，2门诊不可删除，住院可以，3住院不可删除,门诊可以；默认为1 
   VZYSF_BFSFYXYJBL    XT_XTCS00.VALUE0%type;      --住院病人出院结算补费是否允许录入医技单
   cursor Cjydcxm is select DCXMID ZLXMID,FJDJBZ,SL0000,CJFS00,DCLX00,0 FLAG00,NVL(MZZYBZ,'2') MZZYBZ from XT_ZLDCZL where ZLXMID=Pzlxmid  --XT_BBDCZL
                     union
                     select b.ZLXMID,'2' FJDJBZ,1 SL0000,'0' CJFS00,'1' DCLX00,1 FLAG00,'4' MZZYBZ from JY_TXMDY0 a,JY_TXMLB0 b
                      where a.TXMQZ0=b.TXMQZ0 and a.ZLXMID=Pzlxmid and b.zlxmid is not null
                        and not exists(
                            select 1 from YJ_YJDJGL t1, YJ_YW0000 t2, YJ_YW0000 t3, JY_TXMDY0 t4
                             where t2.ZYGHID=Pzyghid and t2.MZZYBZ=Pmzzybz and t2.FJDJBZ='2'
                               and t1.ZYJDJH=t2.YJDJH0 and t1.SCX000='0'
                               and t1.FYJDJH=t3.YJDJH0 and t3.XMZT00 not in ('2','3')
                               and t3.ZLXMID=t4.ZLXMID and t4.TXMQZ0=a.TXMQZ0
                             );

   cursor Cjyzxm0 is select ZXMID0 from VW_YS_YWMXXM where SJXMID=Pzlxmid and GDXMBZ='Y' ;
   cursor CUR_ZYJDJH is select b.YJDJH0 from YJ_YJDJGL a,YJ_YW0000 b
                         where FYJDJH = Pyjdjh0 and a.ZYJDJH=b.YJDJH0 and a.SCX000='0' and b.XMZT00 not in ('2','3','4');
                         --2017.07.04
                         --and not exists (select 1 from YJ_YJDJGL where FYJDJH<>Pyjdjh0 and ZYJDJH=a.ZYJDJH and SCX000='0');
begin
  Pzxcgbz := 0;
  Vsysdate := sysdate;
  Vczrq00 := to_char(sysdate,'yyyymmdd');
  Vczsj00 := to_char(sysdate,'hh24:mi:ss');
  --Vxxxlog := substr('POPFLAG:'||nvl(to_char(POPFLAG), 'null')||','||'PYJDJH0:'||nvl(to_char(PYJDJH0), 'null')||','||'PZYGHID:'||nvl(to_char(PZYGHID), 'null')||','||'PMZZYBZ:'||nvl(to_char(PMZZYBZ), 'null')||','||'PZLXMID:'||nvl(to_char(PZLXMID), 'null')||','||'PZXMID0:'||nvl(to_char(PZXMID0), 'null')||','||'PZDID00:'||nvl(to_char(PZDID00), 'null')||','||'PZDMC00:'||nvl(to_char(PZDMC00), 'null')||','||'PBBID00:'||nvl(to_char(PBBID00), 'null')||','||'PBH0000:'||nvl(to_char(PBH0000), 'null')||','||'PYJLRBZ:'||nvl(to_char(PYJLRBZ), 'null')||','||'PJZBZ00:'||nvl(to_char(PJZBZ00), 'null')||','||'PKDYS00:'||nvl(to_char(PKDYS00), 'null')||','||'PKDKSBH:'||nvl(to_char(PKDKSBH), 'null')||','||'PKDBQ00:'||nvl(to_char(PKDBQ00), 'null')||','||'PYJKSBH:'||nvl(to_char(PYJKSBH), 'null')||','||'PSL0000:'||nvl(to_char(PSL0000), 'null')||','||'PBZ0000:'||nvl(to_char(PBZ0000), 'null')||','||'PFJDJBZ:'||nvl(to_char(PFJDJBZ), 'null')||','||'PCZY000:'||nvl(to_char(PCZY000), 'null')||','||'PCZYKS0:'||nvl(to_char(PCZYKS0), 'null')||','||'PCOMMIT:'||nvl(to_char(PCOMMIT), 'null')||','||'PYWMXID:'||nvl(to_char(PYWMXID), 'null')||','||'PBWMC00:'||nvl(to_char(PBWMC00), 'null')||','||'PFYJDJH:'||nvl(to_char(PFYJDJH), 'null')||','||'PCXJJ00:'||nvl(to_char(PCXJJ00), 'null')||','||'PYZCFID:'||nvl(to_char(PYZCFID), 'null')||','||'PSSDH00:'||nvl(to_char(PSSDH00), 'null')||','||'PTXM000:'||nvl(to_char(PTXM000), 'null')||','||'PWZBM00:'||nvl(to_char(PWZBM00), 'null')||','||'PSSBM00:'||nvl(to_char(PSSBM00), 'null')||','||'PSSMC00:'||nvl(to_char(PSSMC00), 'null')||','||'PSSYJDH:'||nvl(to_char(PSSYJDH), 'null')||','||'PWZBH00:'||nvl(to_char(PWZBH00), 'null')||','||'PTQPC00:'||nvl(to_char(PTQPC00), 'null')||','||'PXMZK00:'||nvl(to_char(PXMZK00), 'null')||','||'PYJZHJE:'||nvl(to_char(PYJZHJE), 'null')||','||'PTCID00:'||nvl(to_char(PTCID00), 'null')||','||'PMBMXID:'||nvl(to_char(PMBMXID), 'null')||','||'PSFTJ00:'||nvl(to_char(PSFTJ00), 'null'), 1, 2000);
  Vxxxlog := substrb('POPFLAG:'||nvl(to_char(POPFLAG), 'null')||','||'PYJDJH0:'||nvl(to_char(PYJDJH0), 'null')||','||'PZYGHID:'||nvl(to_char(PZYGHID), 'null')||','||'PMZZYBZ:'||nvl(to_char(PMZZYBZ), 'null')||','||'PZLXMID:'||nvl(to_char(PZLXMID), 'null')||','||'PZXMID0:'||nvl(to_char(PZXMID0), 'null')||','||'PZDID00:'||nvl(to_char(PZDID00), 'null')||','||'PBBID00:'||nvl(to_char(PBBID00), 'null')||','||'PBH0000:'||nvl(to_char(PBH0000), 'null')||','||'PYJLRBZ:'||nvl(to_char(PYJLRBZ), 'null')||','||'PJZBZ00:'||nvl(to_char(PJZBZ00), 'null')||','||'PKDYS00:'||nvl(to_char(PKDYS00), 'null')||','||'PKDKSBH:'||nvl(to_char(PKDKSBH), 'null')||','||'PKDBQ00:'||nvl(to_char(PKDBQ00), 'null')||','||'PYJKSBH:'||nvl(to_char(PYJKSBH), 'null')||','||'PSL0000:'||nvl(to_char(PSL0000), 'null')||','||'PFJDJBZ:'||nvl(to_char(PFJDJBZ), 'null')||','||'PCOMMIT:'||nvl(to_char(PCOMMIT), 'null')||','||'PYWMXID:'||nvl(to_char(PYWMXID), 'null')||','||'PFYJDJH:'||nvl(to_char(PFYJDJH), 'null')||','||'PCXJJ00:'||nvl(to_char(PCXJJ00), 'null')||','||'PYZCFID:'||nvl(to_char(PYZCFID), 'null')||','||'PSSDH00:'||nvl(to_char(PSSDH00), 'null')||','||'PTXM000:'||nvl(to_char(PTXM000), 'null')||','||'PYJZHJE:'||nvl(to_char(PYJZHJE), 'null'), 1, 2000);
  --Pxxxx00 := substr(Vxxxlog, 1, 100);
  SP_TransLog(Vsysdate,'SP_YJ_YJSQWH',Pczy000,Pczyks0,Vxxxlog);

  --门诊病历限制收费
  select nvl(max(trim(value0)),'0') into V_MZBLXZSFYS  from XT_XTCS00 where trim(NAME00)='YS_MZBLXZSFYS';
  if (Pmzzybz = '0') and (V_MZBLXZSFYS <> '0') then
      Vcounter := instr(','||V_MZBLXZSFYS||',',','||to_char( Pkdys00)||',');
      if Vcounter>0 then
          select count(zlxmid) into Vcounter from bm_zlzd00 where zlxmid = Pzlxmid and lbbh00 in (20,21);
          if Vcounter = 0 then
            select count(wsid00) into Vcounter from ys_mzblws where ghh000=rpad(to_char(Pzyghid),12) and wszt00<>'3';
            if Vcounter = 0 then
              Ptsxx00 := '请先书写该病人的病历!';
              Raise Ecustom;
            end if;
          end if;
      end if;
  end if;

  --入参部位ID(Pbwid00)为空或0, 但部位名称不为空, 则重新根据部位名称取部位ID
  Vbwid00 := Pbwid00;
  if (Vbwid00 is null or Vbwid00 = 0) and trim(Pbwmc00) is not null then
    begin
        select BWID00 into Vbwid00 from BM_ZDBW00 where BWMC00 = trim(Pbwmc00) and rownum=1;
    exception
      when others then
        Vbwid00 := Pbwid00;
    end;
  end if;
  
  --重新计价标志, Pcxjj00转成Vcxjj00
  Vcxjj00 := Pcxjj00;
  if Vcxjj00 is null then
    Vcxjj00 := 1;
  end if;

  --#1.1 begin
  if pOpflag='3' or pOpflag='1' then
     --合法性判定
     select * into Vyw0000 from YJ_YW0000 where YJDJH0=Pyjdjh0;
     if pOpflag='1' and Vyw0000.FJDJBZ in ('1','2') then
       Pzxcgbz := 1;
       return;
     end if;
     if Vyw0000.XMZT00='2' then
        Ptsxx00 :='该单据已经收费,不能操作';
        Raise Ecustom;
     elsif Vyw0000.XMZT00='3' or Vyw0000.XMZT00='4' then
        Ptsxx00:='该单据已经执行,不能操作';
        Raise Ecustom;
     end if;     
     if (pOpflag = 3) and (Vyw0000.LRY000 <> Pczy000) and (Vyw0000.MZZYBZ='0') and (nvl(Vyw0000.JZDH00,0)>=0) then
        Ptsxx00 := '非本人新增业务不能删除';
        Raise Ecustom;
     end if;
     if (pOpflag = 3) and (Vyw0000.FJDJBZ='0') and (Vyw0000.KDKSBH <> Pczyks0) and (Pczyks0 is not null) and (Vyw0000.YJLRBZ='N') and (Vyw0000.MZZYBZ='0') and (nvl(Vyw0000.JZDH00,0)>=0) then
        Ptsxx00 := '非本科室新增业务不能删除';
        Raise Ecustom;
     end if;
     if pOpflag = 3 then
       select nvl(max(trim(value0)),'1') into VYJ_YSKDXMSFKSC  from XT_XTCS00 where trim(NAME00)='YJ_YSKDXMSFKSC';
       if Vyw0000.MZZYBZ='0' then
         if (VYJ_YSKDXMSFKSC='0' or VYJ_YSKDXMSFKSC='2') and Vyw0000.YJLRBZ='N' and (nvl(Vyw0000.CXDJH0,0)=0) then
             Ptsxx00 := '【'||Vyw0000.ZLXMJC||'】是医生开单的项目，不能删除';
             Raise Ecustom;
         end if;
       else
         if (VYJ_YSKDXMSFKSC='0' or VYJ_YSKDXMSFKSC='3')  and Vyw0000.YJLRBZ='N' and (nvl(Vyw0000.CXDJH0,0)=0)  then
             Ptsxx00 := '【'||Vyw0000.ZLXMJC||'】是医生开单的项目，不能删除';
             Raise Ecustom;
         end if;
       end if;
     end if;
     Vdelall := true;
     if Pywmxid is not null then
        select count(*) into Vcounter from YJ_YWMX00 where YJDJH0=(select YJDJH0 from YJ_YWMX00 where ID0000=Pywmxid);
        if Vcounter > 1 then
           Vdelall := false;
        end if;
     end if;

     select nvl(max(trim(value0)),'Y') into Vyj_scfxmsfzdsczxm from XT_XTCS00 where NAME00='YJ_SCFXMSFZDSCZXM';

     --1.1.1 begin
     if Vdelall then
        if Vyw0000.FJDJBZ = '0' then  --主项
           if Vyj_scfxmsfzdsczxm <> 'N' then
             for row in CUR_ZYJDJH loop
                select count(*) into Vcounter from YJ_YJDJGL where ZYJDJH = row.YJDJH0 and SCX000='0'
                   and not exists (select 1 from YJ_YJDJGL where ZYJDJH = row.YJDJH0 and FYJDJH<>Pyjdjh0 and SCX000='0');
                if Vcounter > 0 then
                   SP_YJ_YJSQWH(
                      3,                   --操作内容 1修改 2增加 3删除
                      row.YJDJH0,             --操作的医技单据号
                      null,      --挂号id
                      null,      --门诊住院标志
                      null,      --诊疗项目id
                      null,      --子项目id串,如果没有传入参数，表示使用所有明细项
                      null,      --诊断id
                      null,      --诊断名称
                      null,      --标本ID
                      null,      --特殊病种编号
                      null,      --医技录入标志
                      null,      --急诊标志
                      null,      --开单医生
                      null,      --开单科室编号
                      null,      --开单病区
                      null,      --医技科室编号
                      null,      --数量
                      null,      --备注
                      null,
                      Pczy000,             --操作员
                      Pczyks0,             --操作员科室
                      'N',                 --是否提交
                      Pzxcgbz,             --执行成功标志 1成功 0失败
                      Ptsxx00,             --执行结果提示信息，成功的情况下也可能有警告信息
                      Pxxxx00              --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
                   );
                   --delete from YJ_YJDJGL where ZYJDJH = row.YJDJH0 and SCX000='0';  --2017.07.04
                end if;
                delete from YJ_YJDJGL where ZYJDJH = row.YJDJH0 and SCX000='0' and FYJDJH=Pyjdjh0;
             end loop;
           end if;
        else                             --附加项
           delete from YJ_YJDJGL where ZYJDJH = Pyjdjh0;
        end if;

        Vyjdsx0 := SF_YJ_JudgeYJDSX(Pyjdjh0,Pfyjdjh);

        if Vyjdsx0 = 0 then
           select nvl(max(SSDH00),0) into Vssdh00 from SS_SSYJD0 where YJDJH0=Pyjdjh0;
           if Vssdh00 <> 0 then
              --delete SS_SSYJD0 where SSDH00=Vssdh00 and YJDJH0=Pyjdjh0;
              --delete SS_YWMX00 where SSDH00=Vssdh00 and SSBH00=Pzlxmid;
              select count(1) into Vcounter from SS_YW0000 where SSDH00=Vssdh00 and SSZT00 in ('2','4');
              if Vcounter>0 then
                 select count(1) into Vcounter from SS_SSYJD0 where SSDH00=Vssdh00 and YJDJH0<>Pyjdjh0 and SSMZBZ='0';
                 if Vcounter>0 then
                   delete SS_SSYJD0 where SSDH00=Vssdh00 and YJDJH0=Pyjdjh0;
                   delete SS_YWMX00 where SSDH00=Vssdh00 and SSBH00=Pzlxmid;
                 end if;
              else
                delete SS_SSYJD0 where SSDH00=Vssdh00 and YJDJH0=Pyjdjh0;
                delete SS_YWMX00 where SSDH00=Vssdh00 and SSBH00=Pzlxmid;
                --2019.07.11 删除SS_YW0000表记录,2019.10.24增加条件pOpflag='3'
                if pOpflag='3' and nvl(Pyjlrbz,'N')<>'S' then
                  select count(1) into Vcounter from SS_SSYJD0 where SSDH00=Vssdh00;
                  if Vcounter=0 then
                    update SS_YW0000 set SSZT00='3' where SSDH00=Vssdh00;
                  end if;
                end if;
              end if;
           end if;
        end if;

        if Vyjdsx0 = 1 then
           delete from JY_YW0000 where YJDJH0=Pyjdjh0;
        end if;

        if Vyjdsx0 = 2 then
           select nvl(max(SQDH00),0) into Vsqdh00 from XK_SXSQ00 where BRID00=Vyw0000.BRID00 and YZMXID=Pyjdjh0;
           delete from XK_SXSQ00 where BRID00=Vyw0000.BRID00 and YZMXID=Pyjdjh0;
        end if;

        if Popflag = 3 then
          select count(*) into Vcounter from YJ_YW0000 where YJDJH0=Vyw0000.CXDJH0;
          if Vcounter > 0 then
            SP_YJ_CXCX00(Pyjdjh0,Pczy000,Pczyks0);
          end if;
        end if;

        if Popflag = 3 or (Popflag = 1 and Vcxjj00=1) then
          SP_KS_HCJJCX(Pyjdjh0,null,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00,'0');
          delete YJ_YWJJ00 where YJDJH0=Pyjdjh0;
          delete YJ_YWMX00 where YJDJH0=Pyjdjh0;
        end if;
        delete YJ_YW0000 where YJDJH0=Pyjdjh0;
        delete YJ_YYBRB0 where YYID00=Vyw0000.YYID00;

        if pOpflag='1' then--修改
           Vzldccf :=SF_YJ_ZLDCYP(Pzlxmid,Pzyghid,Pmzzybz,Pyjksbh,Pkdys00,Psl0000,Vyw0000.ZLDCCF,2,Psl0000);
        else
           Vzldccf :=SF_YJ_ZLDCYP(Vyw0000.ZLXMID,Vyw0000.ZYGHID,Vyw0000.MZZYBZ,Vyw0000.YJKSBH,Vyw0000.KDYS00,Vyw0000.SL0000,Vyw0000.ZLDCCF,3,Vyw0000.SL0000);
        end if;
        SP_ZY_DKFMX0('1',Vyw0000.ZYGHID,Vyw0000.brid00,'1','1',Pyjdjh0,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);
        
     else
        delete YJ_YWMX00 where ID0000=Pywmxid;
        SF_YJ_YWJJ_NOCOMMIT(Pyjdjh0,Vcxjj00,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00,0);
        SP_ZY_DKFMX0('3',Vyw0000.ZYGHID,Vyw0000.brid00,'1','1',Pyjdjh0,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);
     end if;
     --1.1.1 end

     if Vyw0000.YZCFID is not null then
        delete from BQ_YJYZ00 where YZMXID=Vyw0000.YZCFID and YZZT00='0';
     end if;

     if nvl(Ptcid00,0) <> 0 and pOpflag='3' then
        delete from SF_TCXFMX where DJH000 = Pyjdjh0;
     end if;

     if pOpflag='3' then
        Pzxcgbz := 1;
        if pcommit='Y' then
           commit;
        end if;
        return;
     end if;
  end if;
  --1.1 end

  select ZWXM00 into Vczyxm0 from BM_YGBM00 where YGBH00=Pczy000;

  Vzxmid0:=Pzxmid0;
  if pOpflag='1' or Vzxmid0 is null then  --前台参数没有传参数，重新取字典定义固定的子项目作为本检验单的子项目
     for ZXM in Cjyzxm0 Loop
       if Vzxmid0 is null then
          Vzxmid0:=to_char(ZXM.ZXMID0);
       else
          Vzxmid0:=Vzxmid0||','||to_char(ZXM.ZXMID0);
       end if;
     end loop;
     if Vzxmid0 is null and PZLXMID<>0 then
        Ptsxx00:='没有选中子项目，不能保存';
        Raise ECustom;
     end if;
  end if;

  if Pfjdjbz in ('1','2','9') and nvl(Pfyjdjh,0) = 0 then
     Ptsxx00 := '附加单据必需指定主单据！';
     raise Ecustom;
  end if;

  if Popflag=2 then--增加
     if nvl(Pyjdjh0,0)=0 then
       select SQ_YJ_YW0000_YJDJH0.nextval into Pyjdjh0 from DUAL;
     end if;
     Vzldccf :=SF_YJ_ZLDCYP(Pzlxmid,Pzyghid,Pmzzybz,Pyjksbh,Pkdys00,Psl0000,null,1,0,Pczy000,Pyexm00);
     -- ruanbh      2017.09.12  福州福清市医院新院：项目开单科室限制 for MZYS9-20170908-002
     select count(*) into Vcounter from YS_XMKDKS where ZLXMID=Pzlxmid;
     if Vcounter>0 then
       select count(*) into Vcounter from YS_XMKDKS where ZLXMID=Pzlxmid and KDKSBH=Pkdksbh;
       if Vcounter=0 then
          select ZLXMJC into Vzlxmjc from BM_ZLZD00 where ZLXMID=Pzlxmid;
         Ptsxx00 := '当前科室禁止开'||Vzlxmjc||'！';
         raise Ecustom;
       end if;
     end if;
  end if;

  --1.2 begin
  ----1.2.1 begin
  if Pmzzybz='1' then--住院病人
    select BRID00,ZYH000,XM0000,XB0000,CSRQ00,RYCWH0,nvl(Pkdbq00,DQBQ00),BRZT00,DQKS00
      into Vbrid00,Vzyhghh,Vbrxm00,Vxb0000,Vcsrq00,Vch0000,Vkdbq00,Vbrzt00,Vdqks00
    from ZY_BRXXB0 where ZYID00=Pzyghid;
    if substr(Vbrzt00,1,1) not in ('1','2','3') then
       Ptsxx00 :='病人处于非在院状态，不能开单';
       Raise Ecustom;
    end if;
    -- jiangjh 2022.06.06 住院病人结算 补费功能允许医技录入 芜湖第一人民医院 for ZYSF13-20220606-001
    select nvl(max(VALUE0),'N') into VZYSF_BFSFYXYJBL from XT_XTCS00 a where a.name00='ZYSF_BFSFYXYJBL';
    if VZYSF_BFSFYXYJBL='N' then
      --出院登记状态不允许开单
      --要求出院登记病人代煎费也能开单
      --参数YJ_CKBRKYBLDYJXM,出科病人可以补入的医技项目,输入诊疗项目ID,多个用逗号分隔,默认为-1     YJ9-20210721-001
      select nvl(max(trim(VALUE0)),'-1') into VYJ_CKBRKYBLDYJXM from XT_XTCS00 where NAME00='YJ_CKBRKYBLDYJXM';    
      if substr(Vbrzt00,1,1) in ('3') and nvl(Pfjdjbz,'0')<>'7' and instr(','||VYJ_CKBRKYBLDYJXM||',' , ','||to_char(Pzlxmid)||',')=0 then
         Ptsxx00 :='病人处于非在院状态，不能开单!';
         Raise Ecustom;
      end if;
    end if;
    select max(ZYID00) into Vzyid00 from ZY_BRXXB0 where BRID00=Vbrid00 and ZYH000=Vzyhghh and substrb(BRZT00,1,1)<>'9';
    if Vzyid00<>Pzyghid then
       Ptsxx00 :='病人已经再次入院，请刷新后重新开单';
       Raise Ecustom;
    end if;
    
    Vkdksbh := Pkdksbh;
    --取手术单对应的申请科室及手术单对应的开单病区
    if Pssdh00 is not null and Pyjlrbz = 'S' then
      --2015.07.17 手术室录入的医技单对应的开单科室编号
      select nvl(max(KDKSBH),Vkdksbh) into Vkdksbh from BQ_YJYZ00 a,SS_YW0000 b where a.yzmxid=b.yzmxid
         and b.ssdh00=Pssdh00 and Vkdksbh not in (select BMBH00 from vw_bm_mzbm00);
      select max(SQKS00) into Vsqks00 from SS_YW0000 a where ssdh00=Pssdh00
        and Exists (select 1 from BM_BQKS00 where KSH000=a.SQKS00);
      
      --暂这么处理 SSMZ9-20201015-001
      select count(1) into Vcounter from XT_XTCS00 where NAME00 = 'YJ_KDKSSFSQKS' and VALUE0 = 'Y';
      if Vcounter > 0 then
        Vkdksbh := Pkdksbh;
      else
        --手术医嘱kdksbh=0处理：暂这么处理 SSMZ9-20201015-001
        if Vkdksbh=0 then
          Vkdksbh:=Pkdksbh;
        end if;
      end if;
      if Vsqks00 is not null then
        Vdqks00 := Vsqks00;
      elsif Vbrzt00 = '21' then --取转区前科室
        select max(LDLSH0) into VLDLSH0 from BQ_BRLDXX where ZYID00=Pzyghid and JSRQ00 is null and YXBZ00='1';
        if VLDLSH0 is not null then
          select nvl(max(KSH000),Vdqks00) into Vdqks00 from BQ_BRLDXX where ZYID00=Pzyghid and LDLSH0=(select max(LDLSH0) from BQ_BRLDXX where ZYID00=Pzyghid and LDLSH0<VLDLSH0);
        end if;
      end if;
      
      --手术费用划价生成的住院医技单，其开单病区取值优先顺序，0从当前病区获取，1从手术单对应病区获取，默认0 
      select count(1) into Vcounter from XT_XTCS00 where NAME00 = 'SS_YJZYSQKDBQQZSX' and VALUE0 = '1';
      if Vcounter >0 then 
        select nvl(max(SQBQ00),Vkdbq00) into Vkdbq00 from SS_YW0000 where SSDH00 = Pssdh00;
      end if;      
    end if;
    select SF_ZY_GETYSZID(Pzyghid, Vdqks00) into Vyszid0 from dual;
    
    --2017.08.24 获取婴儿姓名
    if Pyzcfid is not null then
      select max(YEXM00),max(BQZY00) into VYEXM00,VBQZY00 from BQ_YJYZ00 where YZMXID=Pyzcfid;
      --2018.05.50 药品附加费也要或者婴儿姓名
      if nvl(VYEXM00,' ')=' ' then
        select max(YEXM00) into VYEXM00 from BQ_YPYZ00 where YZID00=Pyzcfid;
      end if;
    else
      VYEXM00 := '';
    end if;
    select nvl(max(trim(VALUE0)),'N') into Vxmsqd0 from XT_XTCS00 where NAME00='YS_YSXMSQD';
    if Vxmsqd0 = 'Y' then
       select max(SQID00) into Vsqid00 from YS_XMSQD0 where GHID00=Pzyghid and instr(yzmxid,Pyzcfid)>0 and rownum=1 ;
    end if;
  --1.2.1 end
  --1.2.2 begin
  else--门诊病人
    select BRID00,GHH000,XM0000,XB0000,CSRQ00,GHKS00 into Vbrid00,Vzyhghh,Vbrxm00,Vxb0000,Vcsrq00,Vdqks00 from SF_BRXXB0 where GHID00=Pzyghid;
    select nvl(max(trim(VALUE0)),'N') into Vxmsqd0 from XT_XTCS00 where NAME00='YS_YSXMSQD';
    if Pyjlrbz = 'S' then
      --SSMZ9-20160812-001
      --select count(1) into Vcounter from XT_XTCS00 where name00='SS_MZKDKSPDYXGH' and instr(','||trim(Value0)||',',','||to_char(Pkdksbh)||',')>0;
      select count(1) into Vcounter from XT_XTCS00 where name00='SS_MZKDKSPDYXGH' and instr(','||SF_XT_RETURNINDEXEH(trim(Value0),'|',1)||',',','||to_char(Pkdksbh)||',')>0;
      if Vcounter>0 then
        select count(1) into Vcounter from VW_YS_YXGH00 where GHH000=Vzyhghh and GHLB00>0;
        if Vcounter<=0 then
          Ptsxx00 := '挂号'||Vzyhghh||' 已失效！请重新挂号或选择有效挂号再开单';
          raise Ecustom;
        end if;
      end if;
    end if;
    if Vxmsqd0 = 'Y' then
       if Popflag=1 then --huanglw 如果是修改,则读取是否原来申请单,否则不读取
           select count(1) into Vcounter from YS_XMSQD0 where SQDID0=(select SQDID0 from BM_ZLZD00 where ZLXMID=Pzlxmid) and GHID00=Pzyghid;
           if Vcounter>0 then
              select SQID00 into Vsqid00 from YS_XMSQD0 where SQDID0=(select SQDID0 from BM_ZLZD00 where ZLXMID=Pzlxmid) and GHID00=Pzyghid and rownum=1;
           end if;
       end if;
    end if;
  select count(1) into Vcounter from XT_XTCS00 where NAME00 = 'YJ_KDKSSFSQKS' and VALUE0 = 'Y';
  if Vcounter > 0 then
    Vkdksbh := Pkdksbh;
  else
      Vkdksbh := Vdqks00;
  end if;
    select nvl(max(a.YSZID0),0) into Vyszid0 from ZS_YSZMX0 a,ZS_YSZ000 b,BM_BMBM00 c
     where a.YSZID0=b.YSZID0 and b.SSKSBH=c.BMBH00 and c.BMXZ00='0' and a.YSBH00=Pkdys00;
  end if;
  --1.2.2 end
  ----1.2 end
  --YJ9-20210615-002
  select nvl(MAX(TRIM(VALUE0)),'N') INTO vTSBZXZ from XT_XTCS00 where NAME00='YS_TSBZXZCFXM';
  select nvl(max(trim(VALUE0)),'0') into YJ_TSBZXMXZ from XT_XTCS00 where NAME00='YJ_TSBZXMXZ';

  select ZLXMJC,DW0000,FBZXBZ,LBBH00,JYTZBH,XBBZ00,NLXZ00,CXTZBH into Vzlxmjc,Vdw0000,Vfbzxbz,Vlbbh00,VJYTZBH,VXBBZ00,VNLXZ00,VCXTZBH
    from BM_ZLZD00 where ZLXMID=Pzlxmid;
  select ZWXM00,BMBH00 into Vkdysxm,Vysszks from BM_YGBM00 where YGBH00=Pkdys00;
  
  if vTSBZXZ='Y' then
    if (trim(Pbh0000) is not null) then
      select YBLB00,FBBH00,YBBRLB into Vyblb00, Vfbbh00,vYBBRLB from BM_BRXXB0 where BRID00=Vbrid00;
      select YBZXLB into Vybzxlb from IC_YBBRLB where YBLB00=Vyblb00 and FBBH00=Vfbbh00;
      select nvl(max(MLXZBZ),'0'),max(MC0000) into VMLXZBZ,Vmc0000 from BM_TSBZB0 where trim(BH0000)=trim(Pbh0000) and YBZXLB=Vybzxlb;
        
      if VMLXZBZ in ('2','3') then
        select count(1) into Vcounter from BM_XMBZ00 b where B.YBZXLB=Vybzxlb and B.BH0000=Pbh0000;
        if Vcounter >0 then   --限制诊疗项目
          select count(1) into Vcounter from BM_XMBZ00 b where B.YBZXLB=Vybzxlb 
                   and b.bh0000=Pbh0000 and TYPE00='1' and XMBH00= Pzlxmid;
          if (YJ_TSBZXMXZ='1') and (Vcounter=0) then             
             Ptsxx00:='项目【'||Vzlxmjc||'】不属于选择特殊病种【'||Vmc0000||'】目录项目，无法保存';
             raise Ecustom;  
          elsif (YJ_TSBZXMXZ<>'1') and (Vcounter>0) then
            Ptsxx00:='项目【'||Vzlxmjc||'】不属于选择特殊病种【'||Vmc0000||'】目录项目，无法保存';
             raise Ecustom;          
          end if;                 
        else
            --根据医保收费项目
          select count(1) into VCOUNTER from BM_YBSFDY a, BM_TSBZXM b, YJ_YWJJ00 c where A.YBZXLB=B.YBZXLB 
                   and A.YBXMBH=B.XMBH00 and A.SFXMID=C.SFXMID and A.FBBH00=vFBBH00 and A.YBZXLB=Vybzxlb 
                   and A.YBBRLB=vYBBRLB and C.YJDJH0=Pyjdjh0 and B.BH0000=Pbh0000 AND A.SFYP00='n';   
          if vcounter=0 then
            
             Ptsxx00:='项目【'||Vzlxmjc||'】不属于选择特殊病种【'||Vmc0000||'】目录项目，无法保存';
             raise Ecustom;            
          end if;                               
        end if;             

      end if;       
    end if;
  end if; 
  --YJ9-20210615-002
    
  if Pzlxmid=0 and Pzlxmjc is not null then
    Vzlxmjc:=Pzlxmjc;
  end if;
  if Pyexm00 is not null and VYEXM00 is null then
    VYEXM00 := Pyexm00;
  end if;
  --2019.10.22若该医嘱为婴儿医嘱应判断婴儿性别，不能直接判断患者性别
  if VYEXM00 is not null then
      select max(YEXB00) into VYEXB00 from BQ_JSXX00 where ZYID00=PZYGHID and YEXM00=VYEXM00;
      if VXBBZ00='0' and VYEXB00='女' then
        Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给男性患者开单';
        raise Ecustom;
      elsif VXBBZ00='1' and VYEXB00='男' then
        Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给女性患者开单';
        raise Ecustom;
      end if;
  elsif VXBBZ00='0' and Vxb0000='女' then
    Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给男性患者开单';
    raise Ecustom;
  elsif VXBBZ00='1' and Vxb0000='男' then
    Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给女性患者开单';
    raise Ecustom;
  end if; 
         
  select floor((months_between(sysdate,to_date(Vcsrq00,'YYYYMMDD')))/12) into VBRNL00 from dual;  
  if VNLXZ00 is not null and VYEXM00 is null then
    if substrb(VNLXZ00,1,1)='>' then
      if to_number(nvl(substrb(VNLXZ00,2,3),'0'))>=VBRNL00 then
        Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给年龄'||VNLXZ00||'岁患者开单';
        raise Ecustom;
      end if;
    elsif substrb(VNLXZ00,1,1)='<' then
      if to_number(nvl(substrb(VNLXZ00,2,3),'0'))<=VBRNL00 then
        Ptsxx00 := '项目“'||Vzlxmjc||'”只允许给年龄'||VNLXZ00||'岁患者开单';
        raise Ecustom;
      end if;
    end if;
  end if;

  if nvl(Pzyghid,0)=0 then
    Ptsxx00 := '未指定病人不能开单';
    raise Ecustom;
  end if;

  if Pzdid00 is null then
    begin
      --如果没有选择诊断，不要自动填写诊断  yangy
      --if Pmzzybz = '0' then
         -- select trim(ICD900) into Vzdid00 from YS_BRZDXX where GHID00 = Pzyghid and ZZDBZ0 = '3' and rownum=1;
      --else
      if Pmzzybz = '1' then
          select count(*) into Vcounter from BQ_BRZDXX where ZYID00 = Pzyghid and ZDLB00='2'  and rownum=1;
          if Vcounter<>0 then
              select trim(ZDM000) into Vzdid00 from BQ_BRZDXX where ZYID00 = Pzyghid and ZDLB00='2' and rownum=1;
          else
              select trim(ZDM000) into Vzdid00 from BQ_BRZDXX where ZYID00 = Pzyghid and ZDLB00='3' and rownum=1;
          end if;
      end if;
    exception
      when no_data_found then
        Vzdid00 := null;
    end;
  else
    Vzdid00 := Pzdid00;
  end if;

  ----插入诊断编号，自动填写诊断名称
  --if (Vzdid00 is not null) and (Pzdmc00 is null) then
  --  select JBMC00 into Vzdmc00 from XT_ICD900 where ICD900=Vzdid00;
  --end if;

  if pOpflag='1' then
    insert into YJ_YW0000(YJDJH0, YJKSBH, MZZYBZ, ZYGHID, ZYHGHH, BRID00, BRXM00, XB0000, CSRQ00, NL0000,
                          CH0000, ZLXMID, ZLXMJC, DW0000, FBZXBZ, ZLDCCF, KDYS00, DQKS00,
                          KDYSXM, YSSZKS, KDKSBH, KDBQ00, LRXM00, JZBZ00, ZDID00, ZDMC00, XMZT00,
                          BZ0000, SL0000, APRQ00, YJFZH0, BBID00, BH0000, YZCFID,
                          MC0000, YJLRBZ, KDRQ00, KDSJ00, SQID00, FJDJBZ, BWMC00, YSZID0,TQPC00,TXM000,WZBM00,SSBM00,SSMC00,SSYJDH,
                          WZBH00, XMZK00, TCID00, YEXM00, YJDJDY, SFZF00, YYGXJG, BWID00, LRY000)
         values(Pyjdjh0, Pyjksbh, Pmzzybz, Pzyghid, Vzyhghh, Vbrid00, Vbrxm00, Vxb0000, Vcsrq00, SF_XT_CSRQTONL(Vcsrq00, Vczrq00),
                Vch0000, Pzlxmid, Vzlxmjc, Vdw0000, Vfbzxbz, Vzldccf, Pkdys00, Vdqks00,
                Vkdysxm, Vysszks, Vkdksbh, Vkdbq00, Vczyxm0, nvl(Pjzbz00,Vyw0000.JZBZ00), Vzdid00, nvl(Pzdmc00,Vzdmc00), '0',
                Pbz0000, Psl0000, Vyw0000.APRQ00,Vyw0000.YJFZH0,nvl(Pbbid00,Vyw0000.BBID00),nvl(PBH0000,Vyw0000.BH0000),nvl(Pyzcfid,Vyw0000.YZCFID),
                Vmc0000, Vyw0000.YJLRBZ, Vyw0000.KDRQ00, Vyw0000.KDSJ00, Vsqid00, PFJDJBZ, Pbwmc00, Vyszid0,decode(nvl(Ptqpc00,0),0,Vyw0000.TQPC00,Ptqpc00),
                nvl(Ptxm000,Vyw0000.TXM000),nvl(Pwzbm00,Vyw0000.WZBM00),nvl(Pssbm00,Vyw0000.SSBM00),nvl(Pssmc00,Vyw0000.SSMC00),nvl(Pssyjdh,Vyw0000.SSYJDH),
                nvl(Pwzbh00,Vyw0000.WZBH00),nvl(Pxmzk00,Vyw0000.XMZK00),nvl(Ptcid00,Vyw0000.TCID00),nvl(VYEXM00,Vyw0000.YEXM00),Vyw0000.YJDJDY,PSFZF00,PYYGXJG,Vbwid00, Pczy000);
  else
    insert into YJ_YW0000(YJDJH0, YJKSBH, MZZYBZ, ZYGHID, ZYHGHH, BRID00, BRXM00, XB0000, CSRQ00, NL0000,
                          CH0000, ZLXMID, ZLXMJC, DW0000, FBZXBZ, ZLDCCF, KDYS00, DQKS00,
                          KDYSXM, YSSZKS, KDKSBH, KDBQ00, LRXM00, JZBZ00, ZDID00, ZDMC00, XMZT00,
                          BZ0000, SL0000, APRQ00, YJFZH0, BBID00, BH0000, YZCFID,
                          MC0000, YJLRBZ, KDRQ00, KDSJ00, SQID00, FJDJBZ, BWMC00, YSZID0,TQPC00,TXM000,
                          WZBM00,SSBM00,SSMC00,SSYJDH,WZBH00,XMZK00,TCID00,YEXM00,SFZF00,YYGXJG,BWID00,BQZY00, LRY000)
         values(Pyjdjh0, Pyjksbh, Pmzzybz, Pzyghid, Vzyhghh, Vbrid00, Vbrxm00, Vxb0000, Vcsrq00, SF_XT_CSRQTONL(Vcsrq00, Vczrq00),
                Vch0000, Pzlxmid, Vzlxmjc, Vdw0000, Vfbzxbz, Vzldccf, Pkdys00, Vdqks00,
                Vkdysxm, Vysszks, Vkdksbh, Vkdbq00, Vczyxm0, nvl(Pjzbz00,'N'), Vzdid00, nvl(Pzdmc00,Vzdmc00), '0',
                Pbz0000, Psl0000, null, null, Pbbid00, PBH0000, Pyzcfid,
                Vmc0000, Pyjlrbz, Vczrq00, Vczsj00, Vsqid00, Pfjdjbz, Pbwmc00, Vyszid0,nvl(Ptqpc00,0),Ptxm000,
                Pwzbm00,Pssbm00,Pssmc00,Pssyjdh,Pwzbh00,Pxmzk00,Ptcid00,VYEXM00,PSFZF00,PYYGXJG,Vbwid00,VBQZY00, Pczy000);
  end if;

  if nvl(PYYGXJG,'0')<>'1' then
    if Pzlxmid<>0 then
       SP_EXECUTE_SQL('insert into YJ_YWMX00 (ID0000,YJDJH0,ZLXMID,XMMC00,JGDW00,CKZ000,SL0000)'||
                   ' select SQ_YJ_YWMX00_ID0000.nextval,'||to_char(Pyjdjh0)||',ZLXMID,ZLXMJC,JGDW00,CKZ000,'||to_char(Psl0000)||
                   ' from  BM_ZLZD00 where ZLXMID in ('||Vzxmid0||')');
       SF_YJ_YWJJ_NOCOMMIT(Pyjdjh0,Vcxjj00,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00,Pyjzhje);
    else
       select SQ_YJ_YWMX00_ID0000.nextval into Vid0000 from dual;
       insert into YJ_YWMX00 (ID0000,YJDJH0,ZLXMID,XMMC00,JGDW00,CKZ000,SL0000)
       values(Vid0000,to_char(Pyjdjh0),to_char(Pzlxmid),Vzlxmjc,null,null,to_char(Psl0000));
   end if;
  end if;
  --如果是套餐项目，需要处理病人套餐消费表数据
  if nvl(Ptcid00,0) <> 0 then
    if Popflag = 2 then
       insert into SF_TCXFMX(MXID00,DJH000,XMBH00,XMMC00,XMGG00,XMDW00,XMDJ00,XMSL00,HJJE00,CZY000,KDKS00,KDYS00,ZXKS00,BRID00,XM0000,TCID00,MBMXID)
          values(SQ_SF_TCXFMX_MXID00.Nextval,Pyjdjh0,Pzlxmid,Vzlxmjc,null,Vdw0000,null,Psl0000,Pyjzhje,Pkdys00,Vkdksbh,Pkdys00,Pyjksbh,Vbrid00,Vbrxm00,Ptcid00,Pmbmxid);
    elsif Popflag = 1 then
       update SF_TCXFMX set XMBH00=Pzlxmid,XMMC00=Vzlxmjc,XMDW00=Vdw0000,XMSL00=Psl0000,HJJE00=Pyjzhje,CZY000=Pkdys00,
          KDKS00=Vkdksbh,KDYS00=Pkdys00,ZXKS00=Pyjksbh where DJH000=Pyjdjh0;
    end if;
  end if;
  if Vcxjj00 = 0 then
    update YJ_YW0000 a set XMZT00='1',ZJE000=(select nvl(sum(Round(NVL(JJJE00,0)*SL0000*SFCS00,2)),0) from YJ_YWJJ00 where YJDJH0=a.YJDJH0) where YJDJH0=Pyjdjh0;
  end if;

  if Pfjdjbz in ('1','2','9') then
    insert into YJ_YJDJGL(FYJDJH,ZYJDJH) select Pfyjdjh, Pyjdjh0 from dual
                where not exists(select 1 from YJ_YJDJGL where FYJDJH=Pfyjdjh and ZYJDJH=Pyjdjh0);
  end if;
  
  SP_ZY_DKFMX0('0',Pzyghid,Vbrid00,'1','1',Pyjdjh0,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);    

  Vyjdsx0 := SF_YJ_JudgeYJDSX(Pyjdjh0,Pfyjdjh);

  if Vyjdsx0 = 0 then
    if nvl(Vssdh00,0) = 0 then
      if Pyzcfid is not null then
        select nvl(max(SSDH00),0) into Vssdh00 from SS_YW0000 where ZYGHID=Pzyghid and YZMXID=Pyzcfid;
      end if;

      if nvl(Vssdh00,0) = 0 then
        select SQ_SS_YW0000_SSDH00.nextval into Vssdh00 from dual;
      end if;
    end if;

    update SS_YW0000 set YZMXID=Pyzcfid,YYSSRQ=nvl(YYSSRQ,Vczrq00),YYSSSJ=nvl(YYSSSJ,Vczsj00),YSZID0=Vyszid0 where SSDH00=Vssdh00;
    if sql%notfound then
      select count(*)+1 into Vssxh00 from SS_YW0000 where ZYHGHH=Vzyhghh and SSZT00<>'3';
      insert into SS_YW0000(SSDH00,SSXH00,MZZYBZ,ZYHGHH,ZYGHID,BRID00,XM0000,XB0000,CSRQ00, JZBZ00,
                            SSKS00,SQYS00,SQKS00,SQR000,SRKSLB,YYSSRQ,YYSSSJ,YYBZ00,SSYS00,SSYSXM,
                            SSQSRQ,SSQSSJ,BZ0000,SQZD00,SQZDMC,CZY000,SQRQ00,SQSJ00,CZRQ00,SSZT00,YZMXID,YSZID0)
      values(Vssdh00,Vssxh00,Pmzzybz,Vzyhghh,Pzyghid,Vbrid00,Vbrxm00,Vxb0000,Vcsrq00, nvl(Pjzbz00,'N'),
            Pyjksbh,Pkdys00,Pkdksbh,Pczy000,SF_SS_YW0000_SRKSLB('8'),Vczrq00,Vczsj00,'N',Pkdys00,Vkdysxm,
            null,null,Pbz0000,Pzdid00,nvl(Pzdmc00,Vzdmc00),Pczy000,Vczrq00,Vczsj00,Vczrq00||Vczsj00,'0',Pyzcfid,Vyszid0);
    end if;

    update SS_YW0000 set SSDJMC=(select SSDJMC from BM_ZLZD00 a,BM_SSDJ00 b where a.ZLXMID=Pzlxmid and a.SSDJBH=b.SSDJBH) where SSDH00=Vssdh00;

    insert into SS_YWMX00(ID0000,SSDH00,SFZSS0,SSBH00,SSDJMC,SSXMJC,YJDJH0)
           select SQ_SS_SSYWMX_ID0000.nextval,Vssdh00,'0',Pzlxmid,SSDJMC,a.ZLXMJC,Pyjdjh0
           from BM_ZLZD00 a,BM_SSDJ00 b where a.ZLXMID=Pzlxmid and a.SSDJBH=b.SSDJBH(+)
           and not exists(select 1 from SS_YWMX00 where SSDH00=Vssdh00 and SSBH00=Pzlxmid);

    select SF_SS_GETSSDH(Pmzzybz, Pzyghid) into Vssdh01 from dual;

    insert into SS_SSYJD0(SSDH00,YJDJH0,SSMZBZ) select Vssdh00,Pyjdjh0,'0' from dual
          where not exists(select 1 from SS_SSYJD0 where SSDH00=Vssdh00 and YJDJH0=Pyjdjh0);
  end if;

  if Vyjdsx0 = 2 then
    if Pfjdjbz <> '5' then
      select nvl(trim(max(VALUE0)),0)   into Vxkbmbh from XT_XTCS00 where NAME00='XK_XKBMBH' ; --判断血库内部编号
      select nvl(max(trim(value0)),'N') into Vcfsfmr from XT_XTCS00 where NAME00='XK_SXXZSFMR';  --成分是否默认
      if Vxkbmbh <> 0 then
        if Vcfsfmr = 'Y' then
            Vsxsz00 :='常规';
            select nvl(max(MC0000),'') into Vsxcf00 from BM_TYZD00 a,BM_SQSXGL b where a.NBBH00=b.SXCFID and a.YXBZ00='1' and b.ZLXMID=Pzlxmid;
        else
            Vsxsz00 :='';
            Vsxcf00 :='';
        end if;
        if nvl(Vsqdh00,0) = 0 then
          select Lpad(SQ_XK_SXSQ00_SQDH00.nextval,6,'0') into Vsqdh00 from dual;
        end if;
        if nvl(Vkdbq00,0)>0 then
          select nvl(max(BMMC00),'') into VBQMC00 from bm_bmbm00 where bmbh00=Vkdbq00;
        else
          VBQMC00 := '';
        end if;
         
        --update YJ_YW0000 set XMZT00='0' where YJDJH0=Pyjdjh0 and XMZT00 in ('0','1');

        select nvl(max(VALUE0),'N') into VYJ_SFBSCSXXT from XT_XTCS00 where NAME00='YJ_SFBSCSXXT';

        if  (VYJ_SFBSCSXXT = 'Y') 
         or (instr(','||VYJ_SFBSCSXXT||',',',M,')>0 and instr(','||VYJ_SFBSCSXXT||',',','||Pkdksbh||',')<=0 ) 
         or (instr(','||VYJ_SFBSCSXXT||',',',P,')>0 and instr(','||VYJ_SFBSCSXXT||',',','||Pkdksbh||',')>0 ) then
            insert into XK_SXSQ00(XKBMBH,SQDH00,ZYID00,ZYH000,BRID00,SQKS00,KSMC00,CH0000,LRRXM0,LRRQ00,LRSJ00,SQZT00,YZMXID,SXSL00,
                                  SXDW00,SQYSBH,SQRQ00,SQSJ00,CYRQ00,BQMC00,LCZDMC,SXSZ00,SXCF00,MZZYBZ)
                 select Vxkbmbh,Vsqdh00,Pzyghid,Vzyhghh,Vbrid00,Pkdksbh,BMMC00,nvl(Vch0000,'0'),Vczyxm0,Vczrq00,Vczsj00,'1',Pyjdjh0,Psl0000,
                        Vdw0000,Pkdys00,Vczrq00,Vczsj00,Vczrq00,VBQMC00,nvl(Pzdmc00,Vzdmc00),
                        Vsxsz00,Vsxcf00,decode(Pmzzybz,'0','门诊','住院')
                 from BM_BMBM00 where BMBH00=Pkdksbh;
            delete from YJ_YWJJ00 where YJDJH0=Pyjdjh0;
            --2021.08.25 从上面移到这里
            update YJ_YW0000 set XMZT00='0' where YJDJH0=Pyjdjh0 and XMZT00 in ('0','1');
        end if;

        --未插入过病人信息的，第一次要插入病人信息
        insert into XK_BRXX00 (BRID00,MRN000,ZYH000,XM0000,XB0000,CSRQ00,MZZYBZ)
             select Vbrid00,0,Vzyhghh,Vbrxm00,Vxb0000,Vcsrq00,decode(Pmzzybz,'0','门诊','住院')
             from dual where rownum=1 and not exists (select 1 from XK_BRXX00 where BRID00=Vbrid00);
      end if;
    end if;
  end if;

  if Vyjdsx0=1 then
    begin
      select nvl(max(trim(VALUE0)),'1') into VYJ_JYXMSL from XT_XTCS00 where NAME00='YJ_JYXMSL';
      if instr(VYJ_JYXMSL,';')=0 then
        VYJ_JYXMSL_1:=VYJ_JYXMSL;
        VYJ_JYXMSL_2:='';
      else 
        if instr(VYJ_JYXMSL,';')=1 then
          VYJ_JYXMSL_1:='1';
          VYJ_JYXMSL_2:=substr(VYJ_JYXMSL,2,length(VYJ_JYXMSL));
        else
          VYJ_JYXMSL_1:=substr(VYJ_JYXMSL,1,instr(VYJ_JYXMSL,';')-1);
          VYJ_JYXMSL_2:=substr(VYJ_JYXMSL,instr(VYJ_JYXMSL,';')+1,length(VYJ_JYXMSL));
        end if;
      end if;
      Vjyxmsl:=to_number(VYJ_JYXMSL_1);
    exception
      when others then
        Vjyxmsl := 1;
    end;
    if (Psl0000 > Vjyxmsl) and (nvl(PSFTJ00,'N')='N') then
      if instr(','||VYJ_JYXMSL_2||',' , ','||to_char(Pzlxmid)||',')=0 then 
        Ptsxx00 := '检验项目数量只能为'||to_char(Vjyxmsl);
        raise Ecustom;
      end if;
    end if;

    --2022.03.20 qks
    if Pmzzybz = '0' and Psl0000>0 and Pbbid00 is not null then 
       select nvl(max(a.DCXMID),0) into VZLXMID from BM_BBZD00 a where BBID00=Pbbid00 and nvl(a.DCXMID,0)<>0;
       if VZLXMID>0 then
              select nvl(sum(ZJE000),0) into Vcounter from YJ_YW0000 where ZYGHID=Pzyghid and ZLXMID=VZLXMID and BBID00=Pbbid00 and XMZT00 in ('0','1') and MZZYBZ='0';
              if Vcounter<=0 then
                SP_YJ_YJSQWH(
                   2,            --操作内容 1修改 2增加 3删除
                   Vyjdjh0,      --操作的医技单据号
                   Pzyghid,      --挂号id
                   Pmzzybz,      --门诊住院标志
                   VZLXMID,      --诊疗项目id
                   null,         --子项目id串,如果没有传入参数，表示使用所有明细项
                   Pzdid00,      --诊断id
                   Pzdmc00,      --诊断名称
                   Pbbid00,      --标本ID
                   Pbh0000,      --特殊病种编号
                   Pyjlrbz,      --医技录入标志
                   Pjzbz00,      --急诊标志
                   Pkdys00,      --开单医生
                   Pkdksbh,      --开单科室编号
                   Pkdbq00,      --开单病区
                   Pyjksbh,      --医技科室编号
                   1,            --数量
                   null,         --备注
                   '0',          --是否为附加单据 0主单1采血费2试管费3挂号费4诊查费5病历本6预约挂号7代煎9其他子单
                   Pczy000,      --操作员
                   Pczyks0,      --操作员科室
                   'N',          --是否提交
                   Pzxcgbz,      --执行成功标志 1成功 0失败
                   Ptsxx00,      --执行结果提示信息，成功的情况下也可能有警告信息
                   Pxxxx00,      --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
                   null,         --业务明细ID  Popflag=3时 可能用到
                   null,         --部位名称
                   null,         --父医技单据号
                   1,            --重新计价
                   Pyzcfid,
                   null,
                   Ptxm000,
                   Pwzbm00,
                   Pssbm00,
                   Pssmc00,
                   Pssyjdh,
                   Pwzbh00,
                   Ptqpc00,
                   Pxmzk00,
                   Pyjzhje,
                   Ptcid00,
                   Pmbmxid,                 
                   PSFTJ00,
                   PSFZF00,
                   PYYGXJG,
                   Pbwid00,
                   Pzlxmjc,
                   Pyexm00
                );  
                SP_ZY_DKFMX0('1',Pzyghid,0,'0','1',Vyjdjh0,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);                             
              end if;
            end if;  
       end if;
    
  end if;

  select count(1) into Vcounter from XT_ZLDCZL where ZLXMID=Pzlxmid and (nvl(MZZYBZ,'2')='2' or nvl(MZZYBZ,'2')=Pmzzybz) and rownum=1;
  if Vcounter>0 or Vyjdsx0 = 1 then
    Vtzbtg0 := 0;
    --借床病人 床位所属于科室
    if Pmzzybz='1' then
      select nvl(max(KSH000),0) into VJCKSBH from bm_cwk000 where zyid00=Pzyghid and CWZT00='1' and Vdqks00<>KSH000;
    end if;

    if Pfjdjbz = '0' then  --and Pbbid00 is not null
      for row in Cjydcxm loop
        if (Pmzzybz='0' and row.mzzybz in (0,2)) or (Pmzzybz ='1' and row.mzzybz in (1,2)) or (row.mzzybz = '4') then
          if row.FLAG00 = 0 then
            --1.4 begin
            if row.FJDJBZ='2'then
              --1.3 begin
              if Pmzzybz='0' then
                Vtzbtg0 := 1;
                --2021.05.14 修正数量不准确问题
                select nvl(sum(sl0000),0),max(YJDJH0) into Vcounter,VZYJDJH from YJ_YW0000 where YJDJH0 in 
                ( 
                select distinct b.YJDJH0                  
                --select count(*),max(a.ZYJDJH) into Vcounter,VZYJDJH               
                  from YJ_YJDJGL a, YJ_YW0000 b,YJ_YW0000 c,BM_ZLZD00 d
                 where b.ZYGHID=Pzyghid and b.MZZYBZ=Pmzzybz and b.FJDJBZ='2' and a.ZYJDJH=b.YJDJH0 and a.SCX000='0' and b.ZLXMID=row.ZLXMID
                   and b.XMZT00 in ('0','1') and nvl(a.SFQR00,'0') <> '1' and b.JZBZ00=nvl(Pjzbz00,'N')
                   and a.FYJDJH=c.YJDJH0 and c.XMZT00 in ('0','1')
                   and c.ZLXMID=D.ZLXMID
                   and nvl(D.JYTZBH,'-')=nvl(VJYTZBH,'0')
                   and not exists(select 1 from YJ_YW0000 where ZYGHID=Pzyghid and MZZYBZ=Pmzzybz and ZLXMID=Pzlxmid and XMZT00 in ('0','1') and YJDJH0<>Pyjdjh0 and SL0000>0)
                   );
              else 
                 --2021.05.14 修正数量不准确问题 
                select nvl(sum(sl0000),0),max(YJDJH0) into Vcounter,VZYJDJH from YJ_YW0000 where YJDJH0 in 
                ( 
                select distinct b.YJDJH0                 
                --select count(*),max(a.ZYJDJH) into Vcounter,VZYJDJH 
                  from YJ_YJDJGL a, YJ_YW0000 b,YJ_YW0000 c,BM_ZLZD00 d
                 where b.ZYGHID=Pzyghid and b.MZZYBZ=Pmzzybz and b.FJDJBZ='2' and a.ZYJDJH=b.YJDJH0 and a.SCX000='0' and b.ZLXMID=row.ZLXMID
                   and b.XMZT00 in ('0','1','2') and nvl(a.SFQR00,'0') <> '1' and b.JZBZ00=nvl(Pjzbz00,'N')
                   and a.FYJDJH=c.YJDJH0 and c.XMZT00 in ('0','1','2')
                   and c.ZLXMID=D.ZLXMID
                   and nvl(D.JYTZBH,'-')=nvl(VJYTZBH,'0')
                   --and not exists(select 1 from HIS_REQUISITION where HIS_ID=to_char(c.YJDJH0) and REQUISITION_STATE not in ('preinspect','printed'))
                   --2014.02.27
                   and not exists(select 1 from HIS_REQUISITION where HIS_ID=to_char(c.YJDJH0) and REQUISITION_STATE is not null)
                   --2019.04.25
                   and not exists(select 1 from YJ_BBZJB0 where YJDJH0=to_char(c.YJDJH0))
                   and not exists(select 1 from YJ_YW0000 where ZYGHID=Pzyghid and MZZYBZ=Pmzzybz and ZLXMID=Pzlxmid and XMZT00 in ('0','1') and YJDJH0<>Pyjdjh0 and SL0000>0)
                   );
              end if;
              --1.3end
            else
              --取未执行的附加费数量
              --2021.05.14 修正数量不准确问题
              select nvl(sum(sl0000),0),max(YJDJH0) into Vcounter,VZYJDJH from YJ_YW0000 where YJDJH0 in 
              ( 
              select distinct b.YJDJH0
              --select count(*),max(a.ZYJDJH) into Vcounter,VZYJDJH  
                from YJ_YJDJGL a, YJ_YW0000 b,YJ_YW0000 c,BM_ZLZD00 d
               where b.ZYGHID=Pzyghid and b.MZZYBZ=Pmzzybz  and b.FJDJBZ<>'0' and a.ZYJDJH=b.YJDJH0 and a.SCX000='0' and b.ZLXMID=row.ZLXMID
                 and b.XMZT00 in ('0','1','2') and nvl(a.SFQR00,'0') <> '1' and b.JZBZ00=nvl(Pjzbz00,'N')
                 and a.FYJDJH=c.YJDJH0 and c.XMZT00 in ('0','1','2')
                 --2014.01.24
                 and b.FJDJBZ=row.FJDJBZ
                 --2019.03.01 抽血费算法修改:
                 and c.ZLXMID=D.ZLXMID
                 and nvl(D.CXTZBH,'-')=nvl(VCXTZBH,'-')
                 --2014.01.22
                 and not exists(select 1 from HIS_REQUISITION where HIS_ID=to_char(c.YJDJH0) and REQUISITION_STATE not in ('preinspect','printed'))
                 --2019.04.25
                 and not exists(select 1 from YJ_BBZJB0 where YJDJH0=to_char(c.YJDJH0) and trim(STATE0) not in ('preinspect','printed'))
                 and not exists(select 1 from YJ_YW0000 where ZYGHID=Pzyghid and MZZYBZ=Pmzzybz and ZLXMID=Pzlxmid and XMZT00 in ('0','1') and YJDJH0<>Pyjdjh0 and SL0000>0)
                 );
            end if;
            --1.4 end
            if row.FJDJBZ in ('1','2') and Vcounter>0 and (row.SL0000 - Vcounter)<=0 and VZYJDJH is not null and row.DCLX00 = '1' then
              insert into YJ_YJDJGL(FYJDJH,ZYJDJH) select Pyjdjh0, VZYJDJH from dual
               where not exists(select 1 from YJ_YJDJGL where FYJDJH=Pyjdjh0 and ZYJDJH=VZYJDJH);
            end if;

            if row.CJFS00 = '1' then
              if row.DCLX00 = '1' then
                Vcs0000 := row.SL0000 - Vcounter;
              else
                Vcs0000 := row.SL0000;
              end if;
              Vsl0000 := 1;
            else
              Vcs0000 := 1;
              if row.DCLX00 = '1' then
                Vsl0000 := row.SL0000 - Vcounter;
              else
                Vsl0000 := row.SL0000;
              end if;
            end if;
          else
            Vsl0000 := row.SL0000;
            Vcs0000 := 1;
          end if;
          if Vsl0000 > 0 and Vcs0000 > 0 then
            loop
              Vyjdjh0 := null;
              if Pmzzybz = '1' and row.FJDJBZ in ('1','2') then
                Vyjksbh := Pkdksbh;
                --借床病人
                if Pkdksbh = Vdqks00 and VJCKSBH>0 then
                  Vyjksbh := VJCKSBH;
                end if;
              else
                Vyjksbh := Pyjksbh;
              end if;

              SP_YJ_YJSQWH(
                 2,                   --操作内容 1修改 2增加 3删除
                 Vyjdjh0,             --操作的医技单据号
                 Pzyghid,      --挂号id
                 Pmzzybz,      --门诊住院标志
                 row.ZLXMID,      --诊疗项目id
                 null,      --子项目id串,如果没有传入参数，表示使用所有明细项
                 Pzdid00,      --诊断id
                 Pzdmc00,      --诊断名称
                 null,      --标本ID
                 Pbh0000,      --特殊病种编号
                 Pyjlrbz,      --医技录入标志
                 Pjzbz00,      --急诊标志
                 Pkdys00,      --开单医生
                 Pkdksbh,      --开单科室编号
                 Pkdbq00,      --开单病区
                 Vyjksbh,      --医技科室编号
                 Vsl0000,      --数量
                 null,      --备注
                 row.FJDJBZ,
                 Pczy000,             --操作员
                 Pczyks0,             --操作员科室
                 'N',                 --是否提交
                 Pzxcgbz,             --执行成功标志 1成功 0失败
                 Ptsxx00,             --执行结果提示信息，成功的情况下也可能有警告信息
                 Pxxxx00,             --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
                 null,             --业务明细ID  Popflag=3时 可能用到
                 null,             --部位名称
                 Pyjdjh0,             --父医技单据号
                 1,              --重新计价
                 Pyzcfid,
                 null,
                 Ptxm000,
                 Pwzbm00,
                 Pssbm00,
                 Pssmc00,
                 Pssyjdh,
                 Pwzbh00,
                 Ptqpc00,
                 Pxmzk00,
                 Pyjzhje,
                 Ptcid00,
                 Pmbmxid,                 
                 PSFTJ00,
                 PSFZF00,
                 PYYGXJG,
                 Pbwid00,
                 Pzlxmjc,
                 Pyexm00
              );
              if Pzxcgbz = 1 then
                insert into YJ_YJDJGL(FYJDJH,ZYJDJH) select Pyjdjh0, Vyjdjh0 from dual
                  where not exists(select 1 from YJ_YJDJGL where FYJDJH=Pyjdjh0 and ZYJDJH=Vyjdjh0);
              end if;
              SP_ZY_DKFMX0('1',Pzyghid,0,'1','1',Vyjdjh0,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);
              Vcs0000 := Vcs0000 - 1;
              exit when Vcs0000 < 1;
            end loop;
          end if;
        end if;
      end loop;

      --2020.01.20 同组不同管(耗材诊疗项目ID不同)并管，删除1个0元管;
      if Vtzbtg0 = 1 then
        if Pmzzybz='0' then
          --0元子医技单据号VZYJDJH1,非0元子医技单据号VZYJDJH
          select count(*),nvl(max(decode(b.ZJE000,0,a.ZYJDJH,0)),0),nvl(max(decode(b.ZJE000,0,0,a.ZYJDJH)),0) into Vcounter,VZYJDJH1,VZYJDJH from YJ_YJDJGL a, YJ_YW0000 b,YJ_YW0000 c,BM_ZLZD00 d
           where b.ZYGHID=Pzyghid and b.MZZYBZ=Pmzzybz and b.FJDJBZ='2' and a.ZYJDJH=b.YJDJH0 and a.SCX000='0'
             and b.XMZT00 in ('0','1') and nvl(a.SFQR00,'0') <> '1' and b.JZBZ00=nvl(Pjzbz00,'N')
             and a.FYJDJH=c.YJDJH0 and c.XMZT00 in ('0','1')
             and c.ZLXMID=D.ZLXMID
             and nvl(D.JYTZBH,'-')=nvl(VJYTZBH,'0')
             and nvl(b.SFDJH0,0)=0
             and not exists(select 1 from YJ_YW0000 where ZYGHID=Pzyghid and MZZYBZ=Pmzzybz and ZLXMID=Pzlxmid and XMZT00 in ('0','1') and YJDJH0<>Pyjdjh0 and SL0000>0);
             if Vcounter>1 and VZYJDJH1>0 and VZYJDJH>0 then
               update YJ_YJDJGL set ZYJDJH=VZYJDJH where ZYJDJH=VZYJDJH1;
               delete YJ_YWJJ00 where YJDJH0=VZYJDJH1;
               delete YJ_YWMX00 where YJDJH0=VZYJDJH1;
               delete YJ_YW0000 where YJDJH0=VZYJDJH1;
               SP_ZY_DKFMX0('1',Pzyghid,0,'1','1',VZYJDJH1,0,0,0,1,1,Pczy000,Pczyks0,'N',Pzxcgbz,Ptsxx00,Pxxxx00);
             end if;
         end if;
      end if;

    end if;
  end if;

  SP_YJ_YjComverToYz(
   Pyjdjh0,             --操作的医技单据号
   Pczy000,             --操作员
   Pczyks0,             --操作员科室
   'N',                 --是否提交
   Pzxcgbz,             --执行成功标志 1成功 0失败
   Ptsxx00,             --执行结果提示信息，成功的情况下也可能有警告信息
   Pxxxx00              --执行结果详细信息，成功的情况下也可能有警告信息，一般用来显示参数等指标
  );

  Pzxcgbz:=1;
  if pcommit='Y' then
     commit;
  end if;
exception
  when no_data_found then
    raise_application_error(-20001,substrb('没有找到数据！*'||sqlerrm,1,240));
  when Ecustom       then
    raise_application_error(-20010,substrb(Ptsxx00||'！*',1,240));
  when others        then
    raise_application_error(-20002,substrb(nvl(sqlerrm, '原因不明出错')||'！*',1,240));
end;

/