if exists(select * from sysobjects where name='p_rz_prepare_fk' and xtype='p') drop procedure p_rz_prepare_fk go create procedure p_rz_prepare_fk ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @optoverstatus varchar(10) declare @ssupcode varchar(15) declare @paytype varchar(1) declare @pretotal numeric(19,4) declare @prepay numeric(19,4) declare @ssupstatus varchar(1) declare @ssupvalidstatus varchar(1) select @ssupcode=supcode,@paytype=paytype,@pretotal=pretotal from t_pay_head where billno=@psbillno select @optoverstatus=itemvalue from t_syscfg where section='SYSTEM' and itemname='OVER' if @optoverstatus<>'STOP' begin set @psmsg='正在进行营业日结,请稍后记账' return(-1) end 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, @prepay=prepay 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 @pretotal<>0 begin if @paytype='0' --应付 ,不允许出现负数 begin if @pretotal<0 begin set @psmsg='使用的预付款不能为负数' return(-1) end if @prepay<@pretotal begin set @psmsg='没有足够的预付款可使用' return(-1) end end else --预付 可以出现负数 begin if @prepay+@pretotal<0 begin set @psmsg='没有足够的预付款' return(-1) end end end set @psmsg='' return(1) end go if exists(select * from sysobjects where name='p_rz_fk' and xtype='p') drop procedure p_rz_fk go create procedure p_rz_fk ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @orgcode varchar(10) declare @scurdate varchar(10) declare @scurtime varchar(8) declare @ssupcode varchar(15) declare @jymode varchar(1) declare @dztable varchar(20) declare @paysoano varchar(20) declare @dztotal numeric(19,4) declare @jstotal numeric(19,4) declare @bcjstotal numeric(19,4) declare @sup_payable numeric(19,4) declare @sup_prepaytotal numeric(19,4) declare @fktotal numeric(19,4) declare @bczrtotal numeric(19,4) declare @pretotal numeric(19,4) declare @ssql varchar(2000) declare @svbktablename varchar(20) select @orgcode=itemvalue from t_syscfg where section='SYSTEM' and itemname='BranchNo' select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108) select @ssupcode=a.supcode,@jymode=b.jymode,@fktotal=a.bcjstotal,@bczrtotal=bczrtotal,@pretotal=pretotal from t_pay_head a left join t_supplier b on a.supcode=b.supcode where billno=@psbillno if (@jymode='0' or @jymode='1') begin set @dztable='t_paysoa_head' end else begin set @dztable='t_jopaysoa_head' end --CircleProcedure --修改供应商应付款信息 declare cur cursor for select paysoano,dztotal,jstotal,bcjstotal from t_pay_body where billno=@psbillno open cur fetch next from cur into @paysoano,@dztotal,@jstotal,@bcjstotal while @@fetch_status=0 begin if (abs(@dztotal-@jstotal-@bcjstotal)<0.01) begin update a set isfinished='1' from t_pay_bill a,t_paysoa_body b where a.billtype=b.billtype and a.billno=b.ywbillno and a.orgcode=b.orgcode and b.billno=@paysoano and a.isaccounted='1' end --回写对账单累计付款金额 set @ssql='update '+@dztable+' set fktotal=fktotal+'+convert(varchar(20),@bcjstotal)+' where billno='''+@paysoano+'''' exec(@ssql) fetch next from cur into @paysoano,@dztotal,@jstotal,@bcjstotal end close cur deallocate cur --AlterVendor update t_supplier set fktotal=fktotal+@fktotal,zrtotal=zrtotal+@bczrtotal,prepay=prepay-@pretotal where supcode=@ssupcode --WriteVendorBook 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 select @sup_payable=case when (@jymode='0' or @jymode='1') then jhtotal-thtotal+tjtotal else amount-tctotal end,@sup_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('''+@orgcode+''','''+@scurdate+''','''+@scurtime+''','''+@ssupcode+''','''+@psbillno+''',''2'','+convert(varchar(20),@fktotal)+','+convert(varchar(20),@sup_payable-@fktotal)+','+convert(varchar(20),@sup_prepaytotal+@pretotal)+')' exec(@ssql) if (@bczrtotal<>0) begin set @ssql='insert into t_vbk_'+ substring(convert(varchar(100),getdate(),112),1,6)+'(orgcode,bookdate,booktime,supcode ,billno,billtype,fsamount,yfjytotal,prejytotal)' +' values('''+@orgcode+''','''+@scurdate+''','''+@scurtime+''','''+@ssupcode+''','''+@psbillno+''',''A'','+convert(varchar(20),-@bczrtotal)+','+convert(varchar(20),@sup_payable-@fktotal-@bczrtotal)+','+convert(varchar(20),@sup_prepaytotal)+')' exec(@ssql) end if (@pretotal<>0) begin set @ssql='insert into t_vbk_'+ substring(convert(varchar(100),getdate(),112),1,6)+'(orgcode,bookdate,booktime,supcode ,billno,billtype,fsamount,yfjytotal,prejytotal)' +' values('''+@orgcode+''','''+@scurdate+''','''+@scurtime+''','''+@ssupcode+''','''+@psbillno+''',''3'','+convert(varchar(20),-@pretotal)+','+convert(varchar(20),@sup_prepaytotal)+','+convert(varchar(20),@sup_payable)+')' exec(@ssql) end set @psmsg='' return(1) end go