SP to generate InvoiceNo.
CREATE PROCEDURE [dbo].[SPGenerateInvoiceNo]
-- Add the parameters for the stored procedure here
@InvoiceDate datetime,
@InvoiceNo varchar(10)='' output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @IncrementNo varchar(6);
DECLARE @FinancialYear varchar(4);
DECLARE @LastInvoiceNo varchar(50);
DECLARE @NextInvoiceNo varchar(50);
DECLARE @Alphabate VARCHAR(1);
SET @FinancialYear = (SELECT
CASE WHEN MONTH(@InvoiceDate)>=4 THEN
SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)),3,2)+SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)+1),3,2)
ELSE
SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)-1),3,2)+SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)),3,2)
END);
IF NOT EXISTS(SELECT * FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate > @InvoiceDate)
BEGIN
IF EXISTS(select TOP(1)InvoiceNo from [InvoiceSystem].[dbo].[Tbl_Invoice] order BY InvoiceNo desc)
BEGIN
SET @LastInvoiceNo =(select TOP(1)InvoiceNo from [InvoiceSystem].[dbo].[Tbl_Invoice] order BY InvoiceNo desc)
END
ELSE
BEGIN
SET @LastInvoiceNo = '00000000'
END
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4));
SELECT @IncrementNo = RIGHT('0000'+ CONVERT(VARCHAR,convert(INT,@IncrementNo)+1), 4);
END
ELSE
BEGIN
SET @NextInvoiceNo = (SELECT TOP(1)InvoiceNo FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate> @InvoiceDate ORDER BY InvoiceNo);
print(@NextInvoiceNo)
Declare @len varchar(5);
SET @len = (select len(@NextInvoiceNo));
IF (@len <= 8)
BEGIN
SET @LastInvoiceNo = (SELECT TOP(1)InvoiceNo FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate<= @InvoiceDate ORDER BY InvoiceNo DESC);
SET @len = (select len(@LastInvoiceNo));
IF (@len > 8)
BEGIN
SET @Alphabate = (SELECT char(ascii(SUBSTRING(@LastInvoiceNo,10,1)) +1));
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4)+'.'+@Alphabate);
END
ELSE
BEGIN
IF (@len is null)
BEGIN
SET @IncrementNo = '0';
END
ELSE
BEGIN
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4)+'.A');
END
END
END
ELSE
BEGIN
SET @IncrementNo=0
END
END
IF (@IncrementNo <> '0')
BEGIN
SET @InvoiceNo = (SELECT @FinancialYear + @IncrementNo);
SELECT @InvoiceNo AS 'InvoiceNo';
END
ELSE
BEGIN
SELECT '-99' AS 'InvoiceNo';
END
END
-- Add the parameters for the stored procedure here
@InvoiceDate datetime,
@InvoiceNo varchar(10)='' output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @IncrementNo varchar(6);
DECLARE @FinancialYear varchar(4);
DECLARE @LastInvoiceNo varchar(50);
DECLARE @NextInvoiceNo varchar(50);
DECLARE @Alphabate VARCHAR(1);
SET @FinancialYear = (SELECT
CASE WHEN MONTH(@InvoiceDate)>=4 THEN
SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)),3,2)+SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)+1),3,2)
ELSE
SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)-1),3,2)+SUBSTRING(convert(VARCHAR,YEAR(@InvoiceDate)),3,2)
END);
IF NOT EXISTS(SELECT * FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate > @InvoiceDate)
BEGIN
IF EXISTS(select TOP(1)InvoiceNo from [InvoiceSystem].[dbo].[Tbl_Invoice] order BY InvoiceNo desc)
BEGIN
SET @LastInvoiceNo =(select TOP(1)InvoiceNo from [InvoiceSystem].[dbo].[Tbl_Invoice] order BY InvoiceNo desc)
END
ELSE
BEGIN
SET @LastInvoiceNo = '00000000'
END
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4));
SELECT @IncrementNo = RIGHT('0000'+ CONVERT(VARCHAR,convert(INT,@IncrementNo)+1), 4);
END
ELSE
BEGIN
SET @NextInvoiceNo = (SELECT TOP(1)InvoiceNo FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate> @InvoiceDate ORDER BY InvoiceNo);
print(@NextInvoiceNo)
Declare @len varchar(5);
SET @len = (select len(@NextInvoiceNo));
IF (@len <= 8)
BEGIN
SET @LastInvoiceNo = (SELECT TOP(1)InvoiceNo FROM [InvoiceSystem].[dbo].[Tbl_Invoice] WHERE InvoiceDate<= @InvoiceDate ORDER BY InvoiceNo DESC);
SET @len = (select len(@LastInvoiceNo));
IF (@len > 8)
BEGIN
SET @Alphabate = (SELECT char(ascii(SUBSTRING(@LastInvoiceNo,10,1)) +1));
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4)+'.'+@Alphabate);
END
ELSE
BEGIN
IF (@len is null)
BEGIN
SET @IncrementNo = '0';
END
ELSE
BEGIN
SET @IncrementNo = (SELECT SUBSTRING(@LastInvoiceNo,5,4)+'.A');
END
END
END
ELSE
BEGIN
SET @IncrementNo=0
END
END
IF (@IncrementNo <> '0')
BEGIN
SET @InvoiceNo = (SELECT @FinancialYear + @IncrementNo);
SELECT @InvoiceNo AS 'InvoiceNo';
END
ELSE
BEGIN
SELECT '-99' AS 'InvoiceNo';
END
END
No comments:
Post a Comment