if exists(select * from sysobjects where name='p_rz_prepare_th' and xtype='p') drop procedure p_rz_prepare_th go create procedure p_rz_prepare_th ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @optaccepttimes varchar(1) declare @sacceptno varchar(20) declare @sbilltype varchar(1) declare @optupacceptrate numeric(19,4) declare @billcost numeric(19,4) declare @sorgcode varchar(10) declare @ssupcode varchar(15) declare @ssupstatus varchar(1) declare @ssupvalidstatus varchar(1) declare @ssupcontractstatus varchar(1) select @sorgcode=orgcode,@ssupcode=supcode from t_return_head where billno=@psbillno if not exists(select * from t_supplier where supcode=@ssupcode) begin set @psmsg='供应商不存在,不能记账' return(-1) end select @ssupstatus=supstatus, @ssupvalidstatus=case when (isnull(validdate,'')='' or validdate>=convert(varchar(100),getdate(),23)) then '1' else '0' end, @ssupcontractstatus=case when (not exists(select * from t_contract where supcode=t_supplier.supcode and contrstatus='0')) or (exists(select * from t_contract where supcode=t_supplier.supcode and contrstatus='0' and convert(varchar(100),getdate(),23) between bgndate and enddate)) then '1' else '0' end from t_supplier where supcode=@ssupcode if @ssupstatus<>'0' begin set @psmsg='供应商状态非正常,不能记账' return(-1) end if @ssupvalidstatus='0' begin set @psmsg='供应商已过有效期,不能记账' return(-1) end if @ssupcontractstatus='0' begin set @psmsg='供应商合同已过期,不能记账' return(-1) end select @psmsg=coalesce(@psmsg+char(13),'')+d.plucode from t_return_body d,t_plu_org g where d.plucode=g.plucode and g.orgcode=@sorgcode and g.isright='1' and d.billno=@psbillno and d.price<>g.price if isnull(@psmsg,'')<>'' begin set @psmsg='以下商品的售价已经改变:'+char(13)+@psmsg return(-1) end select @psmsg=coalesce(@psmsg+char(13),'')+b.plucode+'-'+b.pluname from t_return_body b,t_plu where b.plucode=t_plu.plucode and b.billno=@psbillno and b.depcode<>t_plu.depcode if isnull(@psmsg,'')<>'' begin set @psmsg='以下商品的部门已经改变:'+char(13)+@psmsg return(-1) end select @psmsg=coalesce(@psmsg+char(13),'')+ltrim(plucode)+'-'+pluname from t_return_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')) if isnull(@psmsg,'')<>'' begin set @psmsg='以下商品已不存在或在机构['+@sorgcode+']中无经营权限,无法记账'+char(13)+@psmsg return(-1) end end go if exists(select * from sysobjects where name='p_rz_th' and xtype='p') drop procedure p_rz_th go create procedure p_rz_th ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @sorgcode varchar(10) declare @finacode varchar(10) declare @sameacc varchar(1) declare @sbilltype varchar(1) declare @org_orgtype varchar(1) declare @org_isdbdeploy varchar(1) declare @org_isconfirm varchar(1) declare @sedition varchar(20) declare @scurver varchar(1) declare @scurdate varchar(10) declare @scurtime varchar(8) declare @optplumultivendor varchar(10) declare @splace varchar(1) declare @bill_cost numeric(19,4) declare @ssupcode varchar(15) declare @svendorname varchar(100) declare @ssupstatus varchar(1) declare @svendorvaliddate varchar(20) declare @svendortype varchar(2) declare @svendorjymode varchar(1) declare @siszs varchar(1) declare @szsywjs varchar(10) declare @vendor_payable numeric(19,4) declare @vendor_prepaytotal numeric(19,4) declare @v_org_type varchar(1) declare @stock_plutype varchar(1) declare @detail_cost numeric(19,4) declare @detail_netcost numeric(19,4) declare @detail_total numeric(19,4) declare @detail_jprice numeric(19,4) declare @detail_netjprice numeric(19,4) declare @detail_count numeric(19,4) declare @detail_plucode varchar(20) declare @detail_depcode varchar(10) declare @detail_price numeric(19,4) declare @detail_jtaxrate numeric(19,4) declare @detail_cgcount numeric(19,4) declare @detail_counts numeric(19,4) declare @detail_pluname varchar(40) declare @goods_jprice numeric(19,4) declare @goods_netjprice numeric(19,4) declare @goods_cost numeric(19,4) declare @goods_netcost numeric(19,4) declare @goods_tzamount numeric(19,4) declare @goods_counts numeric(19,4) declare @goods_jtaxrate numeric(19,4) declare @sflg varchar(1) declare @new_jprice numeric(19,4) declare @new_netjprice numeric(19,4) declare @new_cost numeric(19,4) declare @new_netcost numeric(19,4) declare @new_tzamount 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 @jynetcost numeric(19,4) declare @ssql varchar(2000) declare @svbktablename varchar(20) declare @spsacceptuptjprice varchar(20) declare @sbranchno varchar(10) declare @detailcs_plucode varchar(20) declare @detailcs_colorcode varchar(10) declare @detailcs_sizecode varchar(10) declare @detailcs_cgcount numeric(19,4) declare @detailcs_ccounts numeric(19,4) declare @stateflg int declare @mngcsstock varchar(1) declare @susercode varchar(6) declare @susername varchar(10) declare @vd_procdate datetime declare @vi_result integer declare @vs_message varchar(max) set @jynetcost=0 set @spsacceptuptjprice=null set @sedition='1' select @optplumultivendor=itemvalue from t_syscfg where section='SYSTEM' and itemname='PluMultiVendor' set @spsacceptuptjprice=isnull(@spsacceptuptjprice,'FALSE') select @sedition=itemvalue from t_syscfg where section='SYSTEM' and itemname='Edition' select @sorgcode=orgcode,@ssupcode=supcode,@svendorname=supname,@splace=place,@susercode=usercode,@susername=username,@sbilltype = billtype from t_return_head where billno=@psbillno select @org_orgtype=orgtype,@org_isdbdeploy=isdbdeploy,@org_isconfirm=isconfirm,@finacode=isnull(finacode,'') from t_shop where orgcode=@sorgcode select @scurver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER' 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' set @szsywjs='0' select @szsywjs=case when isnull(itemvalue,'FALSE')='TRUE' then '1' else '0' end from t_syscfg where section='SYSTEM' and itemname='ZsYwJs' if @splace='' begin if @org_orgtype='2' set @splace='1' else set @splace='0' end if @sbilltype = '2' begin exec p_get_finacode 'C009',@sorgcode,@finacode out,@sameacc out ,@psmsg out if @psmsg <> '' begin return (-1) end end select @ssupstatus=supstatus,@svendorvaliddate=validdate,@svendorjymode=jymode,@svendortype=suptype from t_supplier where supcode=@ssupcode if @@rowcount=0 begin set @psmsg='供应商['+@ssupcode+']不存在' return(-1) end else if @ssupstatus<>'0' begin set @psmsg='供应商['+@ssupcode+']状态非正常' return(-1) end if (@scurver='1') begin if @svendortype='1' set @siszs='1' else set @siszs='0' end else begin if @svendortype='0' set @siszs='1' else set @siszs='0' end set @svbktablename='t_vbk_'+substring(convert(varchar(100),getdate(),112),1,6) exec p_create_vbk @svbktablename,@psmsg out if isnull(@psmsg,'')<>'' begin return(-1) end if @psparams = '9'--不处理库存,仅生成配送退货单 begin select @v_org_type=orgtype from t_shop where orgcode=@ssupcode if (@scurver<>'0') and (@org_orgtype<>'2') and (@org_isdbdeploy='1') and(@v_org_type='2') begin set @psmsg='' exec p_return_create_dist_form @psbillno,@psmsg out if @psmsg<>'' return(-1) update t_dist_head set chkstatus='4' where billno=@psbillno exec p_rz_ps @psorgcode,@psbillno,'',@psmsg output begin if (@@error<>0) or (@psmsg<>'') return(-1) end end update t_return_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno set @psmsg='' return(1) end declare cur cursor for select plucode,pluname,depcode,price,jtaxrate,counts as cgcount,counts,cost as fscost,netcost as fsnetcost, total as fstotal,counts,cost,netcost,jprice,netjprice from t_return_body where billno=@psbillno order by plucode,jprice open cur fetch next from cur into @detail_plucode,@detail_pluname,@detail_depcode,@detail_price,@detail_jtaxrate,@detail_cgcount,@detail_counts,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice while @@fetch_status=0 begin set @sflg='0' select @goods_jprice=o.jprice,@goods_netjprice=o.netjprice,@goods_cost=o.cost,@goods_netcost=o.netcost,@goods_tzamount=o.tzamount,@goods_counts=o.gcount+o.ccount,@goods_jtaxrate=g.jtaxrate from t_plu_org o join t_plu g on o.plucode=g.plucode where o.plucode=@detail_plucode and o.orgcode=@sorgcode if @@rowcount=0 begin set @psmsg='商品['+@detail_pluname+']不存在' return(-1) end set @jynetcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100.00),2) if ((@detail_count>=@goods_counts) and (@detail_cost<=@goods_cost)) or ((@detail_count<=@goods_counts) and (@detail_cost>=@goods_cost)) or ((@goods_counts>=0) and (@detail_count>=@goods_counts) and (@detail_cost>=@goods_cost)) begin set @sflg='1' select @new_jprice=@goods_jprice,@new_netjprice=@goods_netjprice set @new_cost=round(@new_jprice*(@goods_counts-@detail_count),2) set @new_netcost=round(@new_netjprice*(@goods_counts-@detail_count),2) set @new_tzamount=@goods_tzamount+@goods_cost-@detail_cost-@new_cost if @jynetcost<>@new_netcost+round(@new_tzamount/(1+@goods_jtaxrate/100.00),2) set @new_netcost=@jynetcost-round(@new_tzamount/(1+@goods_jtaxrate/100.00),2) end else begin if @goods_tzamount<>0 begin if (@goods_tzamount>0) and (@goods_tzamount>@detail_cost) begin set @new_tzamount=@goods_tzamount-@detail_cost set @new_cost=@goods_cost set @new_netcost=@goods_netcost end else begin set @new_tzamount=0 set @new_cost=@goods_cost-@detail_cost+@goods_tzamount set @new_netcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100.00),2) end end else begin set @new_cost=@goods_cost-@detail_cost set @new_netcost=@goods_netcost-@detail_netcost set @new_tzamount=@goods_tzamount end set @new_jprice=round(@new_cost/(@goods_counts-@detail_count),4) set @new_netjprice=round(@new_netcost/(@goods_counts-@detail_count),4) end update a set gcount=case when @splace='0' then gcount-@detail_count else gcount end, ccount=case when @splace='1' then ccount-@detail_count else ccount end, jprice=@new_jprice,netjprice=@new_netjprice, tzamount=@new_tzamount,cost=@new_cost,netcost=@new_netcost, total=case when b.plutype='2' then 0 else round(a.price*(@goods_counts-@detail_count),2) end, @stock_plutype=b.plutype, @fstotal=case when b.plutype='2' then 0 else @fstotal end, @jccount=gcount+ccount-@detail_count, @jccost=@new_cost+@new_tzamount, @jcnetcost=@new_netcost+round(@new_tzamount/(1+@goods_jtaxrate/100.00),2), @jctotal=case when b.plutype='2' then 0 else round(a.price*(@goods_counts-@detail_count),2) end from t_plu_org a,t_plu b where a.plucode=b.plucode and a.orgcode=@sorgcode and a.plucode=@detail_plucode and isright='1' if exists(select plucode from t_plu where plucode=@detail_plucode and iszfcode='1') begin update a set jprice=b.jprice,netjprice=b.netjprice from t_plu_org a,(select plucode,jprice,netjprice,orgcode from t_plu_org where plucode=@detail_plucode and orgcode=@sorgcode) b, (select plucode,innercode from t_plu where iszfcode='2') c where a.plucode=c.plucode and b.plucode=c.innercode and a.orgcode=b.orgcode end exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'5',@detail_jprice,@detail_netjprice,@detail_price,@detail_count,@fscost,@fsnetcost,@fstotal, @jccount,@jccost,@jcnetcost,@jctotal,@sorgcode,'','',@psmsg fetch next from cur into @detail_plucode,@detail_pluname,@detail_depcode,@detail_price,@detail_jtaxrate,@detail_cgcount,@detail_counts,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice end close cur deallocate cur select @mngcsstock=mngcsstock from t_shop where orgcode=@sorgcode if (@sedition='4') and (@mngcsstock='1') begin insert into t_plu_org_cs(plucode,orgcode,colorcode,sizecode,csbarcode,gcount,ccount,remark) select distinct a.plucode as plucode,@sorgcode as orgcode,a.colorcode as colorcode,a.sizecode as sizecode,g.csbarcode as csbarcode,0 as gcount,0 as ccount,'' as remark from t_return_cs_body a,t_plu_cs g where a.plucode=g.plucode and a.colorcode=g.colorcode and a.sizecode=g.sizecode and a.billno=@psbillno and not exists (select plucode from t_plu_org_cs o where o.plucode=a.plucode and o.colorcode=a.colorcode and o.sizecode=a.sizecode and o.orgcode=@sorgcode) update a set a.gcount=case when @splace='0' then a.gcount-b.ccounts else a.gcount end, a.ccount=case when @splace='1' then a.ccount-b.ccounts else a.ccount end from t_plu_org_cs a,(select plucode,colorcode,sizecode,sum(ccounts) as ccounts from t_return_cs_body where billno=@psbillno group by plucode,colorcode,sizecode) b where a.plucode=b.plucode and a.orgcode=@sorgcode and a.colorcode=b.colorcode and a.sizecode=b.sizecode end if (@svendorjymode<>'2' and @sedition<>'4') and (@optplumultivendor='TRUE') begin insert into t_supaccept_detail(billtype,billno,serialno,plucode,pluname,orgcode,orgname,supcode ,supname,rzdate,rztime,jprice,netjprice,price,counts,cost,netcost,total,remark) select '1',billno,serialno,plucode,pluname,@sorgcode,(select top 1 orgname from t_shop where orgcode=@sorgcode) as orgname, @ssupcode,@svendorname,@scurdate,@scurtime,jprice,netjprice,price,-counts,-cost,-netcost,-total,remark from t_return_body where billno=@psbillno end --set @vd_procdate=getdate() --exec p_decbatchstk @psbillno,'TH',@susercode,@susername,@vd_procdate,@vi_result output,@vs_message output --if @vi_result<>1 --begin -- select @psmsg=case when isnull(@vs_message,'')='' then '退货处理批次库存冲减失败' else @vs_message end -- return(-1) --end update a set thtotal=thtotal+b.cost,@bill_cost=b.cost from t_supplier a,t_return_head b where a.supcode=@ssupcode and a.supcode=b.supcode and b.billno=@psbillno select @vendor_payable=jhtotal-thtotal+tjtotal,@vendor_prepaytotal=prepay from t_supplier where supcode=@ssupcode set @ssql='insert into t_vbk_'+ substring(convert(varchar(100),getdate(),112),1,6)+'(orgcode,bookdate,booktime,supcode,billno,billtype,fsamount,yfjytotal,prejytotal)' +' values('''+@sorgcode+''','''+@scurdate+''','''+@scurtime+''','''+@ssupcode+''','''+@psbillno+''',''1'','+convert(varchar(20),-@bill_cost)+','+convert(varchar(20),@vendor_payable)+','+convert(varchar(20),@vendor_prepaytotal)+')' exec(@ssql) if @svendorjymode<>'2' and @sbilltype<>'3' insert into t_pay_bill(orgcode,billtype,billno,bizdate,supcode,sqauremode,cost,paydate,jscost,zrcost,isfinished,total,contrno,finacode) select orgcode,case when (@scurver='1') and (billtype='2') then '4' else '1' end as billtype, billno,bizdate,supcode,sqauremode,-cost,paydate,0,0,case when cost=0 then '1' else '0' end,-total,contrno,@finacode from t_return_head where billno=@psbillno if @scurver='1' and @siszs='1' and @org_orgtype='1' and @szsywjs='1' begin insert into t_distpay_bill(billno,billtype,shopcode,orgcode,rzdate,sqauremode,zpamount,jsamount,zramount,cost,netcost,total,isfinished,finacode) select billno,'3',orgcode,@sbranchno,rzdate,sqauremode,-cost,0,0,-cost,-netcost,-total,case when cost=0 then '1' else '0' end,@finacode from t_return_head where billno=@psbillno update a set zsthamount=zsthamount+b.cost from t_shop a,t_return_head b where a.orgcode=@sorgcode and a.orgcode=b.orgcode and b.billno=@psbillno end select @v_org_type=orgtype from t_shop where orgcode=@ssupcode if (@scurver<>'0') and (@org_orgtype<>'2') and (@org_isdbdeploy='1') and(@v_org_type='2') begin set @psmsg='' exec p_return_create_dist_form @psbillno,@psmsg out if @psmsg<>'' return(-1) update t_dist_head set chkstatus='4' where billno=@psbillno exec p_rz_ps @psorgcode,@psbillno,'',@psmsg output begin if (@@error<>0) or (@psmsg<>'') return(-1) end end update t_return_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno if (@sbilltype = '2') and (@sameacc <> '1') begin exec p_nosameacc_th @psbillno,@psmsg out end set @psmsg='' return(1) end go