In my project there are almost 150 transaction forms each and every time after final approval we need to provide a unique transaction No. for that financial year, each and every transaction will be identified with a doc_code(String)
Ex: Employee table will have doc_code as EMP Department table will have doc_code as DEPT
A table called doc series is used to get last recent generated document no for all transactions.
In doc series table for all transactions a field called Next No will be assigned to 0. Each and every time when a transaction form got an approval we will increase that Next No by 1. While approving the transaction form we will take the next No value for that particular doc series and we will assign that value as the transaction No.
Method to get next Transaction No:-
List<IptDocumentseries>idslist=
iidsd.findByDocSeriesCode(doccode,financial_year);
if(idslist!=null && !idslist.isEmpty())
{
docNo = idslist.get(0).getNextNo();
}
I will use this above method from Employee Action class by passing its doc code and financial year.
After completion of final approval I will increment that next No by 1 for that series
Method to update next Transaction No:-
public synchronized void updateDocSeries(String doccode,String docseries)
{
Long nextno= 0L;
List<IptDocumentseries>
idslist =iidsd.findByDocSeriesCode(doccode,docseries,portcode,companycode);
try{
if(idslist!=null && !idslist.isEmpty())
{
nextno = idslist.get(0).getNextNo();
IptDocumentseries ids = new IptDocumentseries();
ids = idslist.get(0);
ids.setNextNo(++nextno);
ids.setModifiedDate(getCurrentTimeStamp());
iidsd.update(ids);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Is this a correct approach because there are some scenarios where we are getting duplicates and missing sequence.
Is there any alternative approach for this generating document no like this, please let me know.
Sorry if I mislead u anyway.
My database table structure is:
Doc_series table
S.no --- Doc_code ---- Next_no ---- Doc_series_year ---- Active(T/F)
1 ---- EMP ---- 7 ---- EMP13-14 ---- F
2 ---- EMP ---- 5 ---- EMP14-15 --- T
3 ---- DEPT ---- 3 ---- DEPT13-14 ---- F
4 ---- DEPT ---- 6 ---- DEPT14-15 ---- T
Emp table
S.no ---- Document.No ---- Doc_series_year ---- Approved Status
1 ---- 1 ---- EMP13-14 ---- Approved
2 ---- ---- EMP13-14 ---- draft
3 ---- 2 ---- EMP13-14 ---- Approved
4 ---- 3 ---- EMP13-14 ---- Approved
5 ---- 4 ---- EMP13-14 ---- Approved
6 ---- 5 ---- EMP13-14 ---- Approved
7 ---- ---- EMP13-14 ---- draft
8 ---- 6 ---- EMP13-14 ---- Approved
9 ---- 1 ---- EMP14-15 ---- Approved
10 ---- 2 ---- EMP14-15 ---- Approved
11 ---- 3 ---- EMP14-15 ---- Approved
12 ---- 4 ---- EMP14-15 ---- Approved
Department table
S.no ----| Document.No ---- Doc_series_year ---- Approved Status
1 ---- 1 ---- DEPT13-14 ---- Approved
2 ---- ---- DEPT13-14 ---- draft
3 ---- 2 ---- DEPT13-14 ---- Approved
4 ---- 1 ---- DEPT14-15 ---- Approved
5 ---- 2 ---- DEPT14-15 ---- Approved
6 ---- 3 ---- DEPT14-15 ---- Approved
7 ---- ---- DEPT14-15 ---- draft
8 ---- 4 ---- DEPT14-15 ---- Approved
9 ---- 5 ---- DEPT14-15 ---- Approved
I may switch to one doc_series to other doc series by making them Active/Inactive but sequence no. should be continue on basis of doc_series_year.