Anu
Joined: 01 Jan 2010 Posts: 82
|
Posted: Tue Jul 13, 2010 11:06 am Post subject: Generation of Account Code (ex: TEST0001) |
|
|
Hi All,
In my project i met a requirment to generate the some code.its in a size of 8 digits.
For Example:
If your filling the company name of your organisation,then it have to generate the code automatically like the below,
Comapny name : NYROS Techonologies
(it has to generate the account code like (NYRO0001))
* If the company name again repated or anyother user enters the same company name then the code for that corresponding company should be NYRO0002.
* It means that the company code is diffrent from one and each other.
~~~~~~~~~~~
To Implement this written a method in the controller:
def generate_account_code(var)
if var
company_data = Company.find(:all, :conditions => ["code like ?, var])
if !company_data.blank?
var_size = company_data.size
var_size_final = var_size+1
if var_size >= 1 && var_size <= 9
return new_code = var+"000"+var_size_final.to_s
elsif var_size >= 10 && var_size <= 99
return new_code = var+"00"+var_size_final.to_s
elsif var_size >= 100 && var_size <= 999
return new_code = var+"0"+var_size_final.to_s
else
return new_code = var+var_size.to_s
end
else
return new_code = var+"0001"
end
end
end
In the above code iam taking the first 4 characters of the company name by using the strip method,and saved that data in the "code",variable.which is used as var above.
To extract the first 4 characters of the string,we need to use the strip method:
code = @company_name.strip[0,4]
and after that we are calculating the size of the same code strings(incase of same company names),if the company is name new ,then it will generate the code like the first 4 characters of the company name,and 0001.
If the company name is already existed,and founded the size of the code like 2,then the code added like "NYR00002"
Thank you,
Anu  |
|