Click here to Skip to main content
15,906,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...Friends,

In my web application i have need to generate invoice for clients, Invoice no should be look like this :

120214/001 eg. mmddyy/serial number starting with three digits

this invoice no. always increase by 1 in it's serial number and it remains always unique...

How can i generate this, or any other idea for generating invoice no., Please help me...

Thanks...

Sunil Sharma
Posted
Comments
ZurdoDev 31-Jan-14 12:33pm    
This is easy to do. Where are you stuck?

That's not difficult. The only caveats are:
  • You need to store your last assigned invoice number (you could use a database, if your application already have it, or serialization, or whatever you like to keep this value persistent).
  • You have to check if date changed with respect to last assigned invoice number. In such a case you have to reset the serial number to 1.


The other stuff is just a matter of formatting.
 
Share this answer
 
do it from sql like below



SQL
declare @PrevReferenceNo varchar(50)
 declare @PrevMonth int
 declare @PrevNo int
 declare @ReferenceNo varchar(10)

 set @PrevReferenceNo = (select ReferenceNo from tbl_FuelConsumption where FuelConsumptionID = (select MAX(FuelConsumptionID) from tbl_FuelConsumption))
 set @PrevMonth = SUBSTRING(@PrevReferenceNo,5,2)
 set @PrevNo = SUBSTRING(@PrevReferenceNo,7,4)

 if(@PrevMonth = MONTH(GETDATE()))
  begin
   set @ReferenceNo  =  (CAST(YEAR(GETDATE())as varchar(10))+ RIGHT('00' + CAST(MONTH(GETDATE()) as varchar(10)),2) + RIGHT('0000' + CAST((@PrevNo + 1)as varchar(4)) ,4))
  end
 else
  begin
   set @ReferenceNo  =  (CAST(YEAR(GETDATE())as varchar(10))+ RIGHT('00' + CAST(MONTH(GETDATE()) as varchar(10)),2) + RIGHT('0000' + CAST((1)as varchar(4)) ,4))
  end



we have syntex like year and month. you can change that to month and days.
 
Share this answer
 
In your invoice class:

1. create an Id property that is autogenerated. If you are using an ORM you can have this Id parameter as a database auto-generated property.
2. create a read only propery Invoice number(string) which returns the concatanation of date and Id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900