1.--删除发票临时打印表
delete Out_TempPrintInvoice where ShouKuanYBM=@ShouKuanBM
2.更新处方主表
update Out_Recipeupdate Out_Recipe set JieSuanBZ=1 from Out_Recipe a,Out_TempRecipeNo b where a.JiuZhenID=b.JiuZhenID and a.ChuFangLH=b.ChuFangBM and b.RenYuanBM=@ShouKuanBM
3.生成单据号 并检查发票是否用完
exec @DanJuID=pSys_GetBill @ShouKuanBM,1
4.插入单据进入发票临时表
insert into Out_TempPrintInvoice (ShouKuanYBM,DanJuID) values (@ShouKuanBM,@DanJuID)
5.更新处方明细表Out_RecipeDetail
update Out_RecipeDetail update Out_RecipeDetail Set DanJuID=@DanJuID where JiuZhenID=@JiuZhenID and ChuFangLH in (select ChuFangBM from Out_TempRecipeNo where JiuZhenID=@JiuZhenID and RenYuanBM=@ShouKuanBM) and HeSuanBM=@HeSuanBM
6.生成发票明细表insert into Out_InvoiceDetail
insert into Out_InvoiceDetailinsert into Out_InvoiceDetail (SerialNo,JiuZhenID,DanJuID,HeSuanBM,YingShouJE,ShiShouJE,YingShouZLJE,ShiShouZLJE) values (@SerialNo,@JiuZhenID,@DanJuID,@HeSuanBM,@JinE,@RetRound,@ZiLiJE,@RetRound2)
insert into Out_Invoice insert into Out_Invoice (SerialNo,JiuZhenID,DanJuID,DanJuRQ,XingMing,YingShouJE,ShiShouJE,YingShouZLJE,ShiShouZLJE,ShouKuanYBM,JieSuanZT,TuiFeiSJ,JieZhangDH) values (@SerialNo,@JiuZhenID,@DanJuID,@RiQi,@XingMing,@YingShouJE,@ShiShouJE,@YingShouZLJE,@ShiShouZLJE, @ShouKuanBM,1,Null,Null)
8.生成发药状态表insert into Out_DistriMedicStatus
insert into Out_DistriMedicStatus insert into Out_DistriMedicStatus select distinct @SerialNo,@DanJuID,LingYaoKSBM,Null,1,Null,0 from Out_RecipeDetail where DanJuID=@DanJuID and LingYaoKSBM is not null
9.更新药库表update DR_Store
update DR_Storeupdate DR_Store set ZhangMianShu=ZhangMianShu-a.ShuLiang from (select a.YaoPinID,a.LingYaoKSBM,ShuLiang=Sum(convert(numeric(10,2),a.ShuLiang*a.TieShu*a.TianShu)) from Out_RecipeDetail a,Out_TempRecipeNo b where a.JiuZhenID=b.JiuZhenID and a.ChuFangLH=b.ChuFangBM and b.RenYuanBM=@ShouKuanBM Group by a.YaoPinID,a.LingYaoKSBM) a,DR_Store b where a.YaoPinID=b.YaoPinID and a.LingYaoKSBM=b.KeShiBM
完整代码:
pOut_Chg_RecipeSettl3 /***************************************/ /* 多张处方合并成一张发票门诊结帐 */ /***************************************/ CREATE PROCEDURE pOut_Chg_RecipeSettl3(@XingMing varchar(8),@ShouKuanBM varchar(10)) AS --Assay: Set Nocount on declare @SerialNo varchar(23),@RiQi varchar(10),@DanJuID varchar(10) declare @YingShouJE Numeric(10,2),@ShiShouJE Numeric(10,2),@RetRound Numeric(10,2),@RetRound2 Numeric(10,2),@YingShouZLJE Numeric(10,2),@ShiShouZLJE Numeric(10,2) set @SerialNo=convert(varchar(23),getdate(),121) set @RiQi=convert(varchar(10),getdate(),120) Begin Tran --删除发票临时打印表 delete Out_TempPrintInvoice where ShouKuanYBM=@ShouKuanBM if @@Error<>0 begin RollBack Tran Return 0 end --Assay: -- Update Out_Recipe Set JieSuanBZ=1 where (convert(varchar(10),JiuZhenID)+convert(varchar(10),ChuFangLH)) in -- (select convert(varchar(10),JiuZhenID)+convert(varchar(10),ChuFangBM) from Out_TempRecipeNo where RenYuanBM=@ShouKuanBM) update Out_Recipe set JieSuanBZ=1 from Out_Recipe a,Out_TempRecipeNo b where a.JiuZhenID=b.JiuZhenID and a.ChuFangLH=b.ChuFangBM and b.RenYuanBM=@ShouKuanBM if @@Error<>0 begin RollBack Tran Return 0--更新处方主表 end declare Out_Assay Cursor for select JiuZhenID,ChuFangBM from Out_TempRecipeNo where RenYuanBM=@ShouKuanBM Open Out_Assay Declare @JiuZhenID2 int,@ChuFangHao2 int Fetch Next from Out_Assay into @JiuZhenID2,@ChuFangHao2 while (@@Fetch_Status=0) begin exec pDoc_UpDateAssayOrExamineZT @JiuZhenID2,@ChuFangHao2,2 if @@Error<>0 begin Close Out_Assay Deallocate Out_Assay RollBack Tran Return 0 --更新检验表错误 end Fetch Next from Out_Assay into @JiuZhenID2,@ChuFangHao2 end Close Out_Assay Deallocate Out_Assay declare Out_Inv Cursor for select distinct JiuZhenID from Out_TempRecipeNo where RenYuanBM=@ShouKuanBM open Out_Inv declare @JiuZhenID int Fetch Next from Out_Inv into @JiuZhenID while (@@Fetch_Status=0) begin exec @DanJuID=pSys_GetBill @ShouKuanBM,1 if @@Error<>0 begin RollBack Tran Return 0--生成单据号错误 end if @DanJuID=-1 begin RollBack Tran Return 0--单据已用完 end --插入单据进入发票临时表 insert into Out_TempPrintInvoice (ShouKuanYBM,DanJuID) values (@ShouKuanBM,@DanJuID) if @@Error<>0 begin RollBack Tran Return 0 end set @YingShouJE=0.00 set @ShiShouJE=0.00 set @YingShouZLJE=0.00 set @ShiShouZLJE=0.00 declare Out_Invo Cursor for select HeSuanBM,Sum(JinE),Sum(ZiLiJE) from Out_RecipeDetail where JiuZhenID=@JiuZhenID and ChuFangLH in (select ChuFangBM from Out_TempRecipeNo where JiuZhenID=@JiuZhenID and RenYuanBM=@ShouKuanBM) Group By HeSuanBM order by convert(int,HeSuanBM) open Out_Invo declare @HeSuanBM varchar(5),@JinE Numeric(10,2),@ZiLiJE Numeric(10,2) Fetch Next from Out_Invo into @HeSuanBM,@JinE,@ZiLiJE while (@@Fetch_Status=0) begin --Assay: update Out_RecipeDetail Set DanJuID=@DanJuID where JiuZhenID=@JiuZhenID and ChuFangLH in (select ChuFangBM from Out_TempRecipeNo where JiuZhenID=@JiuZhenID and RenYuanBM=@ShouKuanBM) and HeSuanBM=@HeSuanBM if @@Error<>0 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0--更新处方明细表 end --精度控制 exec pSys_GetRound @Jine,@RetRound out if @RetRound=-1 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0 end exec pSys_GetRound @ZiLiJE,@RetRound2 out if @RetRound=-1 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0 end insert into Out_InvoiceDetail (SerialNo,JiuZhenID,DanJuID,HeSuanBM,YingShouJE,ShiShouJE,YingShouZLJE,ShiShouZLJE) values (@SerialNo,@JiuZhenID,@DanJuID,@HeSuanBM,@JinE,@RetRound,@ZiLiJE,@RetRound2) if @@Error<>0 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0--生成发票明细表 end set @YingShouJE=@YingShouJE+@JinE set @ShiShouJE=@ShiShouJE+@RetRound set @YingShouZLJE=@YingShouZLJE+@ZiLiJE set @ShiShouZLJE=@ShiShouZLJe+@RetRound2 Fetch Next from Out_Invo into @HeSuanBM,@JinE,@ZiLiJE end insert into Out_Invoice (SerialNo,JiuZhenID,DanJuID,DanJuRQ,XingMing,YingShouJE,ShiShouJE,YingShouZLJE,ShiShouZLJE,ShouKuanYBM, JieSuanZT,TuiFeiSJ,JieZhangDH) values (@SerialNo,@JiuZhenID,@DanJuID,@RiQi,@XingMing,@YingShouJE,@ShiShouJE,@YingShouZLJE,@ShiShouZLJE, @ShouKuanBM,1,Null,Null) if @@Error<>0 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0--生成发票主表 end insert into Out_DistriMedicStatus select distinct @SerialNo,@DanJuID,LingYaoKSBM,Null,1,Null,0 from Out_RecipeDetail where DanJuID=@DanJuID and LingYaoKSBM is not null if @@Error<>0 begin Close Out_Invo Deallocate Out_Invo Close Out_Inv Deallocate Out_Inv RollBack Tran Return 0--生成发药状态表 end Close Out_Invo Deallocate Out_Invo Fetch Next from Out_Inv into @JiuZhenID end Close Out_Inv Deallocate Out_Inv --Assay:以下游标可以使用一个语句替换 update DR_Store set ZhangMianShu=ZhangMianShu-a.ShuLiang from (select a.YaoPinID,a.LingYaoKSBM,ShuLiang=Sum(convert(numeric(10,2),a.ShuLiang*a.TieShu*a.TianShu)) from Out_RecipeDetail a,Out_TempRecipeNo b where a.JiuZhenID=b.JiuZhenID and a.ChuFangLH=b.ChuFangBM and b.RenYuanBM=@ShouKuanBM Group by a.YaoPinID,a.LingYaoKSBM) a,DR_Store b where a.YaoPinID=b.YaoPinID and a.LingYaoKSBM=b.KeShiBM /* declare Out_Rep Cursor for --Assay: -- select YaoPinID,ShuLiang,TieShu,TianShu,LingYaoKSBM from Out_RecipeDetail where (convert(varchar(10),JiuZhenID)+convert(varchar(10),ChuFangLH)) in -- (select convert(varchar(10),JiuZhenID)+convert(varchar(10),ChuFangBM) from Out_TempRecipeNo where RenYuanBM=@ShouKuanBM) select YaoPinID,ShuLiang,TieShu,TianShu,LingYaoKSBM from Out_RecipeDetail a,Out_TempRecipeNo b where a.JiuZhenID=b.JiuZhenID and a.ChuFangLH=b.ChuFangBM and b.RenYuanBM=@ShouKuanBM open Out_Rep declare @YaoPinID varchar(15),@ShuLiang Amount,@TieShu int,@TianShu int,@LingYaoKSBM varchar(10) Fetch Next from Out_Rep into @YaoPinID,@ShuLiang,@TieShu,@TianShu,@LingYaoKSBM while (@@Fetch_Status=0) begin declare @ZhangMianShu Amount set @ZhangMianShu=convert(numeric(10,2),@ShuLiang*@TieShu*@TianShu) update DR_Store set ZhangMianShu=ZhangMianShu-@ZhangMianShu where YaoPinID=@YaoPinID and KeShiBM=@LingYaoKSBM if @@Error<>0 begin Close Out_Rep Deallocate Out_Rep RollBack Tran Return 0--更新药房库存 end Fetch Next from Out_Rep into @YaoPinID,@ShuLiang,@TieShu,@TianShu,@LingYaoKSBM end Close Out_Rep Deallocate Out_Rep */ Commit Tran Return 1 --Assay: Set Nocount off GO