日期:2014-05-17 浏览次数:20602 次
--***************************************************
--创建日期:2010-09-6
--功能说明:生成单据编号
--参数说明:
--修改记录:
--***************************************************
CREATE PROCEDURE aa_createbillid
(
@iBillid int,
@iPost int,
@strBillid nvarchar(50) output
)
as
set nocount on
declare @Date varchar(20),
@Year varchar(20),
@Year2 varchar(20),
@Month varchar(20),
@Day varchar(20),
@BillCode varchar(20),
@BillWay varchar(20),
@SplitSign varchar(20),
@StartBill varchar(20),
@Temp varchar(20),
@strTmp varchar(20),
@SNextID varchar(20)
declare @MaxID int,
@BillID int,
@iError int,
@iTmp int,
@flag int
set @flag=0
set @Date = Convert(varchar(10),getdate(),120)
set @Year = Year(@Date)
set @Year2 = Year(@Date)
set @Month = Month(@Date)
set @Day = Day(@Date)
set @Year=SUBSTRING(@Year,3,2)
if(len(@Month) = 1)
set @Month = '0'+ @Month
if(len(@Day) = 1)
set @Day = '0'+ @Day
if not exists(select * from NOPlan where [ID]=@iBillid)
begin
execute aa_addbillid @iBillid
end
select @BillCode=Code,@BillWay=Style,@SplitSign=Tally,@StartBill=BeginNO from NOPlan where [ID]=@iBillid
if(@BillWay = '年')
begin
set @Temp = @Year
if exists(select * from NOList where datepart(yy,_Date)= @Year2 and [PlanID]=@iBillid)
begin
select @MaxID = MaxValue ,@BillID=[ID] from NOList where datepart(yy,_Date)= @Year2 and [PlanID]=@iBillid
set @flag=1
end
end
else if(@BillWay = '年月' or @BillWay = '月年')
begin
if(@BillWay = '年月')
set @Temp=@Year+@Month
else if(@BillWay = '月年')
set @Temp = @Month+@Year
if exists(select * from NOList where datepart(yy,_Date)= @Year2 and datepart(mm,_Date)=@Month and [PlanID]=@iBillid)
begin
select @MaxID = MaxValue ,@BillID=[ID] from NOList where datepart(yy,_Date)= @Year2 and datepart(mm,_Date)=@Month and [PlanID]=@iBillid
set @flag=1
end
end
else if(@BillWay = '年月日' or @BillWay = '月日年')
begin
if(@BillWay = '年月日')
set @Temp = @Year+@Month+@Day
else if(@BillWay = '月日年')
set @Temp = @Month+@Day+@Year
if exists(select * from NOList where _Date=@Date and [PlanID]=@iBillid)
begin
select @MaxID=MaxValue,@BillID=[ID] from NOList where _Date=@Date and [PlanID]=@iBillid
set @flag=1
end
end
else
begin
set @Temp = ''
if exists(select * from NOList where [PlanID]=@iBillid)
begin
select @MaxID=MaxValue,@BillID=[ID] from NOList where [PlanID]=@iBillid
set @flag=1
end
end
if(@flag=1)
begin
set @MaxID = @MaxID + 1
set @SNextID = @MaxID
--得到起始编号的最大值
set @strTmp='9'
while(len(@strTmp)<len(@StartBill))
begin
set @strTmp='9' + @strTmp
end
set @iTmp=convert(int,@strTmp)+1
--判断越界
if @iTmp=@MaxID
begin
set @StartBill='0' + @StartBill
update NOPlan set BeginNO=@StartBill where [ID]=@iBillid
end
--得到当前流水号
while(len(@SNextID)<len(@StartBill))
begin
set @SNextID='0' + @SNextID
end
--去除自定义多余的分隔符
if @Temp=''
set @strBillid=@BillCode+@Temp+@SplitSign+@SNextID
else
set @strBillid=@BillCode+@SplitSign+@Temp+@SplitSign+@SN