if exists(select * from sysobjects where name='p_rz_prepare_cla' and xtype='p') drop procedure p_rz_prepare_cla go create procedure p_rz_prepare_cla ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @contrno varchar(40) declare @contrstatus varchar(1) select @contrno=contrno from t_adjclause_head where billno=@psbillno if not exists(select contrstatus from t_contract where contrno=@contrno) begin set @psmsg='扣款变更单对应的合同不存在,不能记账' return(-1) end select @contrstatus=contrstatus from t_contract where contrno=@contrno; if @contrstatus='9' begin set @psmsg='扣款变更单对应的合同尚未执行,不必通过合同扣款变更单进行扣款变更' return(-1) end if not exists(select billno from t_adjclause_body where billno=@psbillno and optype <> '3') begin set @psmsg='扣款变更单中没有需要变更的扣项,不能记账' return(-1) end set @psmsg='' return(1) end go if exists(select * from sysobjects where name='p_rz_cla' and xtype='p') drop procedure p_rz_cla go create procedure p_rz_cla ( @psorgcode varchar(10), @psbillno varchar(20), @psparams varchar(200), @psmsg varchar(2000) out ) as begin declare @contrno varchar(40) declare @supcode varchar(15) declare @lrdate varchar(10) declare @usercode varchar(6) declare @username varchar(10) declare @orgcode varchar(10) declare @itemcode varchar(4) declare @itemno varchar(10) declare @endjsdate varchar(10) declare @optype varchar(1) declare @calmode varchar(1) declare @enddate varchar(10) declare @calenddate varchar(10) declare @sup_payable numeric(19,4) declare @ssql varchar(2000) select @contrno=contrno,@supcode=supcode,@lrdate=lrdate,@usercode=usercode,@username=username from t_adjclause_head where billno=@psbillno --取出当前合同供应商的最后一次结算日期(未记账的对账单也算在其中),目前只支持经销对账 select @endjsdate=isnull(enddate,'') from t_paysoa_head where contrno=@contrno declare cur cursor local for select orgcode,itemcode,itemno,optype,calmode,enddate,calenddate from t_adjclause_body where billno=@psbillno and optype<>'3' open cur fetch next from cur into @orgcode,@itemcode,@itemno,@optype,@calmode,@enddate,@calenddate while @@fetch_status=0 begin --如果是增加或修改合同扣项,则如果合同的最后一次结算日期不为空,则新扣项的结束日期必须大于等于最后一次结算日期的第二天 if (@optype in ('0','1') and @endjsdate is not null) begin if (@calmode='0' and @enddate<=@endjsdate) begin set @psmsg='合同的最后一次对账日期为:'+@endjsdate+',要求扣项的结束日期必须大于这个日期,否则下次对账计算不出来' return(-1) end if (@calmode = '1' and @calenddate<=@endjsdate) begin set @psmsg='合同的最后一次对账日期为:'+@endjsdate+',要求扣项的计算区间结束日期必须大于这个日期,否则下次对账计算不出来' return(-1) end end if exists(select contrno from t_contr_clause where contrno=@contrno and supcode=@supcode and orgcode=@orgcode and itemcode=@itemcode and itemno=@itemno) begin if (@optype='0') begin set @psmsg='合同中已经存在扣项(条款项目:'+@itemcode+',组织'+@orgcode+',条款号:'+@itemno+'),不能再增加本扣项' return(-1) end end fetch next from cur into @orgcode,@itemcode,@itemno,@optype,@calmode,@enddate,@calenddate end close cur deallocate cur delete a from t_contr_clause a, t_adjclause_body b where a.contrno=b.contrno and a.supcode=b.supcode and a.orgcode=b.orgcode and a.itemcode=b.itemcode and a.itemno=b.itemno and b.billno=@psbillno and b.optype<> '3' insert into t_contr_clause(contrno,supcode,orgcode,itemcode,itemno,bgndate,enddate,basetype,feetotal,feerate, cltype,zftype,iscalprofit,calmode,calbgndate,calenddate,isinvoice,midclscode,clausetype,tendcode,plucode,cldate, clscode,brandcode,isladderrate,laddercaltype,lrdate,usercode,username,ext1,ext2,ext3,ext4,ext5,remark) select contrno,supcode,orgcode,itemcode,itemno,bgndate,enddate,basetype,feetotal,feerate, cltype,zftype,iscalprofit,calmode,calbgndate,calenddate,isinvoice,midclscode,clausetype,tendcode,plucode,cldate, clscode,brandcode,isladderrate,laddercaltype,@lrdate as lrdate,@usercode as usercode,@username as username,ext1,ext2,ext3,ext4,ext5,remark from t_adjclause_body where billno=@psbillno and optype not in ('2','3') set @psmsg='' return(1) end go