--盈亏 if exists(select * from sysobjects where name='p_rz_prepare_yk' and xtype='p') drop procedure p_rz_prepare_yk go create procedure p_rz_prepare_yk ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @volidplu varchar(100) declare @volidpluname varchar(100) declare @bookcount numeric(19,4) declare @realcount numeric(19,4) declare @ykcount numeric(19,4) declare @errmsg varchar(2000) declare @sorgcode varchar(10) select @sorgcode=orgcode from t_differ_head where billno=@psbillno declare cur_differprepare cursor for select ltrim(plucode)+'-'+pluname as plucode from t_differ_body d where billno=@psbillno and (not exists(select plucode from t_plu where plucode=d.plucode) or not exists(select plucode from t_plu_org where plucode=d.plucode and orgcode=@sorgcode and isright='1')) open cur_differprepare fetch next from cur_differprepare into @volidplu while @@fetch_status=0 begin if isnull(@errmsg,'')='' set @errmsg=@volidplu else set @errmsg=@errmsg+char(13)+@volidplu fetch next from cur_differprepare into @volidplu end close cur_differprepare deallocate cur_differprepare if isnull(@errmsg,'')<>'' begin set @psmsg='以下商品已不存在或在机构['+@sorgcode+']中已无经营权限,无法记账'+char(13)+@errmsg return(-1) end set @errmsg='' declare cur_checkbilldata cursor for select plucode,pluname,realcount,bookcount,ykcount from t_differ_body where realcount-bookcount<>ykcount and billno=@psbillno open cur_checkbilldata fetch next from cur_checkbilldata into @volidplu,@volidpluname,@realcount,@bookcount,@ykcount while @@fetch_status=0 begin if @errmsg='' set @errmsg='编码:'+@volidplu+' 名称:'+@volidpluname+' 账面数量:'+convert(varchar(30),@bookcount)+' 实盘数量:'+convert(varchar(30),@realcount)+' 盈亏数量:'+convert(varchar(30),@ykcount) else set @errmsg=@errmsg+char(13)+'编码:'+@volidplu+' 名称:'+@volidpluname+' 账面数量:'+convert(varchar(30),@bookcount)+' 实盘数量:'+convert(varchar(30),@realcount)+' 盈亏数量:'+convert(varchar(30),@ykcount) fetch next from cur_checkbilldata into @volidplu,@volidpluname,@realcount,@bookcount,@ykcount end close cur_checkbilldata deallocate cur_checkbilldata if @errmsg<>'' begin set @psmsg='如下商品盘点数据存在异常:'+char(13)+@errmsg return(-1) end set @psmsg='' return(1) end go if exists(select * from sysobjects where name='p_rz_yk' and xtype='p') drop procedure p_rz_yk go create procedure p_rz_yk ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @sorgcode varchar(10) declare @scurdate varchar(10) declare @scurtime varchar(8) declare @splace varchar(1) declare @bill_cost numeric(19,4) declare @detail_cost numeric(19,4) declare @detail_netcost numeric(19,4) declare @detail_total numeric(19,4) declare @detail_counts numeric(19,4) declare @detail_serialno int declare @detail_plucode varchar(20) declare @detail_depcode varchar(10) declare @detail_jtaxrate numeric(19,4) declare @detail_pluname varchar(40) declare @detail_jprice numeric(19,4) declare @detail_netjprice numeric(19,4) declare @detail_price numeric(19,4) declare @goods_price numeric(19,4) declare @goods_plutype varchar(1) declare @stock_jprice numeric(19,4) declare @stock_netjprice numeric(19,4) declare @stock_gcount numeric(19,4) declare @stock_ccount numeric(19,4) declare @stock_count numeric(19,4) declare @stock_cost numeric(19,4) declare @stock_netcost numeric(19,4) declare @stock_total numeric(19,4) declare @stock_tzamount numeric(19,4) declare @stock_jycost numeric(19,4) declare @fscost numeric(19,4) declare @fsnetcost numeric(19,4) declare @fstotal numeric(19,4) declare @jccost numeric(19,4) declare @jcnetcost numeric(19,4) declare @jctotal numeric(19,4) declare @jccount numeric(19,4) declare @sbranchno varchar(10) declare @bpricechgflag varchar(1) set @bpricechgflag='0' select @sorgcode=orgcode,@splace=place from t_differ_head where billno=@psbillno select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108) select @sbranchno=itemvalue from t_syscfg where section='SYSTEM' and itemname='BranchNo' update t_differ_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno update d set d.jprice=case when g.plutype='2' then 0 else o.jprice end, d.netjprice=case when g.plutype='2' then 0 else o.netjprice end,d.price=o.price, d.bookcost=round((case when g.plutype='2' then 0 else o.jprice end)*d.bookcount,2),d.realcost=round((case when g.plutype='2' then 0 else o.jprice end)*d.realcount,2), d.ykcost=round((case when g.plutype='2' then 0 else o.jprice end)*d.realcount,2)-round((case when g.plutype='2' then 0 else o.jprice end)*d.bookcount,2), d.booknetcost=round((case when g.plutype='2' then 0 else o.netjprice end)*d.bookcount,2),d.realnetcost=round((case when g.plutype='2' then 0 else o.netjprice end)*d.realcount,2), d.yknetcost=round((case when g.plutype='2' then 0 else o.netjprice end)*d.realcount,2)-round((case when g.plutype='2' then 0 else o.netjprice end)*d.bookcount,2), d.booktotal=case when g.plutype='2' then 0 else round(o.price*d.bookcount,2) end, d.realtotal=case when g.plutype='2' then 0 else round(o.price*d.realcount,2) end, d.yktotal=case when g.plutype='2' then 0 else round(o.price*d.realcount,2)-round(o.price*d.bookcount,2) end from t_differ_body d,t_differ_head h,t_plu_org o,t_plu g where h.billno=d.billno and h.billno=@psbillno and d.plucode=g.plucode and d.plucode=o.plucode and o.orgcode=h.orgcode and o.isright='1' and (h.billtype='0' or (h.billtype='1' and d.ykcount<0)) if @@error>0 begin set @psmsg='根据当前库存调整明细数据失败' return(-1) end update h set h.ykcost=d.ykcost,h.yknetcost=d.yknetcost,h.yktotal=d.yktotal from t_differ_head h,(select billno,sum(ykcost) as ykcost,sum(yknetcost) as yknetcost,sum(yktotal) as yktotal from t_differ_body where billno=@psbillno group by billno) d where h.billno=d.billno and h.billno=@psbillno if @@error >0 begin set @psmsg='根据当前库存调整单据头数据失败' return(-1) end declare cur_ykaccount cursor for select serialno,plucode,pluname,depcode,jtaxrate,ykcount,ykcost,yknetcost,yktotal,jprice,netjprice,price from t_differ_body where billno=@psbillno open cur_ykaccount fetch next from cur_ykaccount into @detail_serialno,@detail_plucode,@detail_pluname,@detail_depcode,@detail_jtaxrate,@detail_counts,@detail_cost,@detail_netcost,@detail_total,@detail_jprice,@detail_netjprice,@detail_price while @@fetch_status=0 begin select @stock_jprice=case when g.plutype='2' then 0 else o.jprice end, @stock_netjprice=case when g.plutype='2' then 0 else o.netjprice end, @goods_price=o.price, @stock_gcount=o.gcount,@stock_ccount=o.ccount, @stock_cost=case when g.plutype='2' then 0 else o.cost end, @stock_netcost=case when g.plutype='2' then 0 else o.netcost end, @stock_total=case when g.plutype='2' then 0 else o.total end, @stock_tzamount=case when g.plutype='2' then 0 else o.tzamount end, @goods_plutype=g.plutype from t_plu_org o,t_plu g where o.plucode=@detail_plucode and orgcode=@sorgcode and o.plucode=g.plucode and o.isright='1' if @@error>0 begin set @psmsg='查询商品当前库存数据失败' close cur_ykaccount deallocate cur_ykaccount return(-1) end if @splace='0' set @stock_gcount=@stock_gcount+@detail_counts else set @stock_ccount=@stock_ccount+@detail_counts set @stock_count=@stock_gcount+@stock_ccount if ((@stock_count=0) and (@detail_counts<0)) begin set @detail_cost=-@stock_cost set @detail_netcost=-@stock_netcost set @detail_total=-@stock_total set @stock_cost=0.00 set @stock_netcost=0.00 set @stock_total=0.00 -- set @stock_tzamount=@stock_tzamount update t_differ_body set ykcost=@detail_cost,yknetcost=@detail_netcost,yktotal=@detail_total,remark='尾差调整' where billno=@psbillno and serialno=@detail_serialno and plucode=@detail_plucode if @@error>0 begin set @psmsg='修改单据体中商品['+@detail_plucode+']的金额信息失败' close cur_ykaccount deallocate cur_ykaccount return(-1) end set @bpricechgflag='1' end else begin set @stock_cost=@stock_cost+@detail_cost set @stock_netcost=@stock_netcost+@detail_netcost if @goods_plutype='2' set @stock_total=0.00 else set @stock_total=round(@goods_price*@stock_count,2) set @stock_jycost=@stock_cost set @stock_tzamount=@stock_tzamount +@stock_jycost-@stock_cost end update t_plu_org set gcount=@stock_gcount,ccount=@stock_ccount, cost=@stock_cost,netcost=@stock_netcost,total=@stock_total,tzamount=@stock_tzamount where plucode=@detail_plucode and orgcode=@sorgcode and isright='1' if @@error>0 begin set @psmsg='修改商品当前库存数据失败' close cur_ykaccount deallocate cur_ykaccount return(-1) end if @detail_counts>0 begin update t_plu_org set yjdate=@scurdate where plucode=@detail_plucode and (yjdate is null or yjdate='') and orgcode=@sorgcode and isright='1' if @@error >0 begin set @psmsg='查询商品引进日期数据失败' close cur_ykaccount deallocate cur_ykaccount return(-1) end end if @detail_counts<>0 begin set @fscost=@detail_cost set @fsnetcost=@detail_netcost set @fstotal=@detail_total set @jccount=@stock_count set @jccost=@stock_cost+@stock_tzamount set @jcnetcost=@stock_netcost+round(@stock_tzamount/(1.00+@detail_jtaxrate/100.00),2) set @jctotal=@stock_total exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'2',@detail_jprice,@detail_netjprice,@detail_price, @detail_counts,@fscost,@fsnetcost,@fstotal,@jccount,@jccost,@jcnetcost,@jctotal,@sorgcode,'','',@psmsg if @@error>0 begin set @psmsg='生成商品台账数据失败' close cur_ykaccount deallocate cur_ykaccount return(-1) end end fetch next from cur_ykaccount into @detail_serialno,@detail_plucode,@detail_pluname,@detail_depcode,@detail_jtaxrate,@detail_counts,@detail_cost,@detail_netcost,@detail_total,@detail_jprice,@detail_netjprice,@detail_price end close cur_ykaccount deallocate cur_ykaccount if @bpricechgflag='1' begin update h set h.ykcost=d.ykcost,h.yknetcost=d.yknetcost,h.yktotal=d.yktotal from t_differ_head h,(select billno,sum(ykcost) as ykcost,sum(yknetcost) as yknetcost,sum(yktotal) as yktotal from t_differ_body where billno=@psbillno group by billno) d where h.billno=d.billno and h.billno=@psbillno if @@error >0 begin set @psmsg='修改单据头中的金额汇总数据失败' return(-1) end end --exec p_sup_stock_in @psbillno,'YK',@psmsg --exec p_sup_stock_out @psbillno,'YK',@psmsg set @psmsg='' return(1) end go