Hi May,
below is the link where I have replied similar kind of question with sample code. this example for the custom document you can also use same concept with Standard BO but for generating a number you have to create one custom BO.
http://scn.sap.com/thread/3462911
I am not very sure about how to add in fine tuning but you can try with using SAP or custom BCO
Please follow that link, and let me know if you need any further help.
thanks
sunil Maurya