数据结构
create table settlement_m ( settlement_bi char (12) not null , folio_ref char (10) null , supplier_id char (6) null , str_dt varchar(8), end_dt varchar(8), input_dt datetime null , inputter char (10) null , audite_dt datetime null , auditer char (10) null , status char (1) default ('0'), memo varchar (50) null , primary key clustered ( settlement_bi ) ) go create table settlement_d ( settlement_bi char (12) not null , seq int not null, sale_a decimal(13,2), kd decimal(9,3), kd_a decimal(13,2), memo varchar(50) primary key (settlement_bi,seq) ) go id(自增列) 商户编号 开始日期 结束日期 销售金额 扣点 备注 create table settlement_list ( id int identity(1,1) not null, supplier_id varchar(6), str_dt varchar(8), end_dt varchar(8), sale_a decimal(13,2), kd decimal(9,3), kd_a decimal(13,2), memo varchar(50) primary key(id) )
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
ALTER procedure usp_calc_settlement_list(@supplier_id varchar(6),@str_dt varchar(8),@end_dt varchar(8))
as
/*
--测试代码
begin tran
insert into supplier_sale_da(dt,supplier_id,tax_sale_a)values('20110412','010101',100000)
execute dbo.usp_calc_settlement_list '010101','20110412','20110412'
select * from settlement_list
rollback
*/
begin
declare @bd_a decimal(13,2),--保底金额
@bd_kd decimal(9,3),--保底扣点
@bd_flag char(1),--保底是否单独计算,0:单独计算,1:参与分额度
@fg_flag char(1),--分额度是否覆盖,0:不覆盖,1:覆盖
@str_a decimal(19,2),--起始金额
@end_a decimal(19,2),--结束金额
@kd decimal(9,3),
--分段扣点
@sale_a decimal(19,2)--销售金额
declare @msg varchar(50)
select @bd_a=isnull(bd_a,0),
@bd_kd=isnull(bd_kd,0),
@bd_flag=isnull(bd_flag,'0'),
@fg_flag=isnull(fg_flag,'0')
from supplier
where supplier_id=@supplier_id
select @sale_a=sum(tax_sale_a) from supplier_sale_da where dt>=@str_dt and dt<=@end_dt and supplier_id=@supplier_id
delete settlement_list where supplier_id=@supplier_id and str_dt=@str_dt and end_dt=@end_dt
--是否保底
if isnull(@bd_a,0)>0
begin
--有保底
if isnull(@bd_flag,'0')='0'
begin--单独计算
--计算保底部分--保底额*保底扣点
select @msg='保底单独计算,保底额:'+convert(varchar(13),@bd_a)
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @bd_a ,@bd_kd,@bd_a*@bd_kd*0.01,@msg )
--是否超出保底
if @sale_a>@bd_a
begin--超出保底
if isnull(@fg_flag,'0')='0'
begin--不覆盖
--计算超出保底部分--超出保底部分在哪个额度分段,按照分段取出扣点,计算出扣点金额
execute usp_calc_settlement_list_2 @supplier_id,@sale_a ,@str_dt,@end_dt,'0',@bd_a
end--不覆盖
else
begin--覆盖
--计算超出保底部分--超出保底部分在哪个额度分段,取出分段最高的扣点,计算出扣点金额
execute usp_calc_settlement_list_2 @supplier_id,@sale_a ,@str_dt,@end_dt,'1',@bd_a
end--覆盖
end----超出保底
end--单独计算
else
begin--不单独计算
if @sale_a<=@bd_a
begin--未超出保底,按照保底算
select @msg='未超出保底额('+convert(varchar(13),@bd_a)+'),按照保底算'
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @bd_a ,@bd_kd,@bd_a*@bd_kd*0.01,@msg )
end--未超出保底,按照保底算
else
begin--超出保底
--按照无保底算
--是否覆盖
if isnull(@fg_flag,'0')='0'
begin--不覆盖
execute usp_calc_settlement_list_1 @supplier_id,@sale_a ,@str_dt ,@end_dt,'0'
end--不覆盖
else
begin
--覆盖
execute usp_calc_settlement_list_1 @supplier_id,@sale_a ,@str_dt ,@end_dt,'1'
end--覆盖
end--超出保底
end--不单独计算
end--有保底
else
begin--无保底
--是否覆盖
if isnull(@fg_flag,'0')='0'
begin--不覆盖
execute usp_calc_settlement_list_1 @supplier_id,@sale_a ,@str_dt ,@end_dt,'0'
end--不覆盖
else
begin
--覆盖
execute usp_calc_settlement_list_1 @supplier_id,@sale_a ,@str_dt ,@end_dt,'1'
end--覆盖
end--无保底
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure usp_calc_settlement_list_1(@supplier_id varchar(6),
@sale_a decimal(19,2),
@str_dt varchar(8),
@end_dt varchar(8),
@fg_flag char(1))
/*
无保底、覆盖和无保底、不覆盖
*/
as
begin
declare
@seq int,
@str_a decimal(19,2),
@end_a decimal(19,2),
@kd decimal(9,3),
@m_seq int,
@max_seq int,
@msg varchar(200)
select @max_seq=max(seq)
from supplier_kd
where supplier_id=@supplier_id
select @seq=min(seq)
from supplier_kd
where supplier_id=@supplier_id and str_a<=@sale_a and end_a>=@sale_a
--找不到则取最大
if @sale_a>0
begin
select @seq=isnull(@seq,@max_seq)
end
else
begin
select @msg='销售额为负值'
raiserror(@msg,16,1)
return
end
--delete settlement_list where supplier_id=@supplier_id and str_dt=@str_dt and end_dt=@end_dt
if @fg_flag='1'
begin--无保底、覆盖
select @kd=kd
from supplier_kd
where supplier_id=@supplier_id and seq=@seq
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @sale_a ,@kd,@sale_a*@kd*0.01,'' )
end--无保底、覆盖
else
begin--无保底、不覆盖
declare cur_fd cursor for
select seq,str_a,end_a,kd
from supplier_kd
where supplier_id=@supplier_id and seq<=@seq order by seq
open cur_fd
fetch cur_fd into @m_seq,@str_a,@end_a,@kd
while @@fetch_status=0
begin
if @m_seq<@seq
begin
--print @end_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt,@end_a - @str_a ,@kd,(@end_a - @str_a)*@kd*0.01,'' )
end
else
begin
--print @sale_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @sale_a - @str_a ,@kd,( @sale_a - @str_a)*@kd*0.01,'' )
end
fetch cur_fd into @m_seq,@str_a,@end_a,@kd
end
close cur_fd
deallocate cur_fd
end--无保底、不覆盖
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure usp_calc_settlement_list_2(@supplier_id varchar(6),
@sale_a decimal(19,2),
@str_dt varchar(8),
@end_dt varchar(8),
@fg_flag char(1),
@bd_a decimal(19,2))
/*
单独计算--有保底、覆盖和有保底、不覆盖
*/
as
begin
declare
@seq int,
@str_a decimal(19,2),
@end_a decimal(19,2),
@kd decimal(9,3),
@m_seq int,
@max_seq int,
@msg varchar(200)
select @max_seq=max(seq)
from supplier_kd
where supplier_id=@supplier_id
select @seq=min(seq)
from supplier_kd
where supplier_id=@supplier_id and str_a<=@sale_a and end_a>=@sale_a
--找不到则取最大
if @sale_a>0
begin
select @seq=isnull(@seq,@max_seq)
end
else
begin
select @msg='销售额为负值'
raiserror(@msg,16,1)
return
end
--delete settlement_list where supplier_id=@supplier_id and str_dt=@str_dt and end_dt=@end_dt
if @fg_flag='1'
begin--保底、覆盖--计算超出保底部分的扣点
select @kd=kd
from supplier_kd
where supplier_id=@supplier_id and seq=@seq
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @sale_a - @bd_a ,@kd,(@sale_a - @bd_a)*@kd*0.01,'' )
end--保底、覆盖--计算超出保底部分的扣点
else
begin--保底、不覆盖--计算超出保底部分的扣点
declare cur_fd cursor for
select seq,str_a,end_a,kd
from supplier_kd
where supplier_id=@supplier_id and seq<=@seq order by seq
open cur_fd
fetch cur_fd into @m_seq,@str_a,@end_a,@kd
while @@fetch_status=0
begin
if @bd_a>=@end_a
begin--在保底内--什么也不做
fetch cur_fd into @m_seq,@str_a,@end_a,@kd
end
else
begin
if @bd_a>@str_a
begin--保底>开始
if @m_seq<@seq
begin
--print @end_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt,@end_a - @bd_a ,@kd,(@end_a - @bd_a)*@kd*0.01,'' )
end
else
begin
--print @sale_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @sale_a - @bd_a ,@kd,( @sale_a - @bd_a)*@kd*0.01,'' )
end
end--保底>开始
else
begin--保底<=开始
if @m_seq<@seq
begin
--print @end_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt,@end_a - @str_a ,@kd,(@end_a - @str_a)*@kd*0.01,'' )
end
else
begin
--print @sale_a - @str_a
insert into settlement_list(supplier_id,str_dt,end_dt,sale_a,kd,kd_a,memo)
values(@supplier_id,@str_dt,@end_dt, @sale_a - @str_a ,@kd,( @sale_a - @str_a)*@kd*0.01,'' )
end
end--保底<=开始
end
fetch cur_fd into @m_seq,@str_a,@end_a,@kd
end
close cur_fd
deallocate cur_fd
end--保底、不覆盖--计算超出保底部分的扣点
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO