1.先删除临时发票表
delete Out_TempPrintInvoice where ShouKuanYBM=@ShouKuanBM
2. 更新发票主表
update Out_invoice set TuiFeiSJ=@TuiFeiSJ where DanJuID=@DanJuID
3.获取就诊ID
select @OldJiuZhenID=JiuZhenID from Out_Invoice where DanJuID=@DanJuID
4.插入就诊主表//取消
insert into tblmzgh select @SerialNo,mzh,ylkh,name,0,0,@ShouKuanBM,@JiuZhenID,0,null,null from tblmzgh where ID=@OldJiuZhenID
{insert into Out_ClinicInfo select @SerialNo,BingLiHao,@JiuZhenID,BingRenLB,LeiBieBM,XingMing,KeShiBM,JiuZhenRQ,@ShouKuanBM from Out_clinicInfo where JiuZhenID=@OldJiuZhenID}
5.更新退费记录表
update Out_RetCharge set TuiFeiBZ=1 where DanJuID=@DanJuID
6.更新药库数量
update DR_Store
set ZhangMianShu=ZhangMianShu+a.ShuLiang
from (select YaoPinID,LingYaoKSBM,
Sum(convert(Numeric(10,2),ShuLiang*TieShu*TianShu)) as ShuLiang
from Out_RecipeDetail where
DanJuID=@DanJuID Group by YaoPinID,LingYaoKSBM) a,DR_Store b
where
a.YaoPinID=b.YaoPinID and
b.KeShiBM=a.LingYaoKSBM
insert into Out_Recipe select @SerialNo,@BingLiHao,@JiuZhenID,2,@RiQi,@YiShengBM,@KeShiBM,@ShouKuanBM,-YingShouJE,-YingShouJE,1,0,0 from Out_Invoice where DanJuID=@DanJuID
8.获取新发票号并判断 发票是否用完。
exec @NewDanJuID=pSys_GetBill @ShouKuanBM,1
9.插入单据进入发票临时表
insert into Out_TempPrintInvoice (ShouKuanYBM,DanJuID) values (@ShouKuanBM,@NewDanJuID)
10.生成负处方明细表 (danjuid 用的是新的发票号)
11.生成负的发药状态表
insert into Out_DistriMedicStatus select @SerialNo,@NewDanJuID,KeShiBM,FaYaoCZ,1,FaYaoRQ,FaYaoZT from Out_DistriMedicStatus where DanJuID=@DanJuID
12.生成负的发票主表
insert into Out_Invoice select SerialNo,@JiuZhenID,@NewDanJuID,@RiQi,XingMing,-YingShouJE,-ShiShouJE,-YingShouZLJE,-ShiShouZLJE,@ShouKuanBM,1,@SerialNo,Null
from Out_Invoice where DanJuID=@DanJuID
insert into Out_InvoiceDetail select @SerialNo,@JiuZhenID,@NewDanJuID,HeSuanBM,-YingShouJE,-ShiShouJE,-YingShouZLJE,-ShiShouZLJE from Out_InvoiceDetail where DanJuID=@DanJuID
14.更新处方主表的结算标志为1
update Out_Recipe Set JieSuanBZ=1 where JiuZhenID=@JiuZhenID and ChuFangLH=1