public BillNumTO generateBillNum(CounterTO counterTO){
		PreparedStatement pstmt = null;
		Connection con = null;
		BillNumTO billNumTO = new BillNumTO();
		int docNumId = ConstantValue.INTEGER_ZERO;
		String billNum = ConstantValue.EMPTY_STRING;
		int maxNum = ConstantValue.INTEGER_ZERO;
		String docType = ConstantValue.EMPTY_STRING;
		
		billNum += counterTO.getBranchTO().getCode();
		
		if (counterTO.getTrxType() == ConstantValue.TRXN_NEW_REG || counterTO.getTrxType() == ConstantValue.TRXN_REENTRY ||
				counterTO.getTrxType() == ConstantValue.TRXN_UPGRADE) {
			docType = ConstantValue.DOC_CASHBILL;
		} else {
			docType = ConstantValue.DOC_INVOICE;
		}
		
		billNum += "-" + docType;
		
		try {
			con = DAOFactory.getInstance().getConnection(Connection.TRANSACTION_READ_UNCOMMITTED);
			
			pstmt = con.prepareStatement(SQLConstants.GET_DOCUMENT_NUM_SQL);
			pstmt.setInt(1, Integer.parseInt(counterTO.getBranchTO().getId()));
			pstmt.setString(2, Utility.trim(docType));
			 
			ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
            	docNumId = rs.getInt("id");
            	maxNum = rs.getInt("maxNum");
            } 
            
            if (docNumId == 0) {
            	Utility.closeAll(rs, pstmt, null);
            	con.setAutoCommit(false);
            	
            	pstmt = con.prepareStatement(SQLConstants.INSERT_DOCUMENT_NUM_SQL, Statement.RETURN_GENERATED_KEYS);
				pstmt.setString(1, counterTO.getBranchTO().getId());
				pstmt.setString(2, docType);
				pstmt.setString(3, ConstantValue.STATUS_ACTIVE);
				pstmt.setInt(4, counterTO.getCreateBy());
				pstmt.executeUpdate();
				ResultSet rsKey = pstmt.getGeneratedKeys();
				if (rsKey.next()) {
					docNumId = rsKey.getInt(1);
				}
				Utility.closeAll(rsKey, pstmt, null);
				
				pstmt = con.prepareStatement(SQLConstants.INSERT_DOCUMENT_NUM_SEQ_SQL);
				pstmt.setInt(1, docNumId);
				pstmt.setInt(2, ConstantValue.INTEGER_ZERO);
				pstmt.setInt(3, counterTO.getCreateBy());
				pstmt.executeUpdate();
				Utility.closeAll(null, pstmt, null);
				
				con.commit();
            }
            
            billNumTO.setId(docNumId);
            
            int newSeq = maxNum + 1;
            billNumTO.setNxSeqNum(newSeq);
            String numStr = Integer.toString(newSeq);
            
            int numLength = numStr.length();
            while (numLength<6) {
            	numStr = "0" + numStr;
            	numLength++;
            }
            
            billNum += numStr;
            billNumTO.setBillNum(billNum);
            
		} catch (SQLException e) {
			throw new DataException(e.getMessage());
		} catch (NamingException e) {
			throw new DataException(e.getMessage());
		} finally {
			Utility.closeAll(null, pstmt, con);
		}
		
		return billNumTO;
	}