change seed generator
This commit is contained in:
@@ -1,100 +1,105 @@
|
||||
class SeedGenerator < ApplicationRecord
|
||||
# Generate ID for Tables
|
||||
def self.generate_id(model, prefix)
|
||||
cur_val, next_val = self.update_seed(model)
|
||||
query = SeedGenerator.select("next").where("model=?",model).first()
|
||||
|
||||
if (cur_val == 0)
|
||||
cur_val, next_val = self.execute_query(model)
|
||||
end
|
||||
if query.nil?
|
||||
nLast_val = 1
|
||||
sql = "INSERT INTO seed_generators (model, created_at, updated_at)
|
||||
VALUES('#{ model }', NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE current = #{nLast_val}, next = #{nLast_val} + 1;"
|
||||
else
|
||||
nLast_val = query.next
|
||||
sql = "UPDATE seed_generators set current = #{nLast_val}, next = #{nLast_val} + 1 WHERE model='#{model}';"
|
||||
end
|
||||
ActiveRecord::Base.connection.execute(sql);
|
||||
|
||||
padding_len = 15 - prefix.length
|
||||
saleOrderId = prefix +"-"+ cur_val.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
||||
return saleOrderId
|
||||
generate_id = prefix +"-"+ nLast_val.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
||||
|
||||
return generate_id
|
||||
end
|
||||
|
||||
# Generate Receipt No for number order (1,2,3) Don't touch
|
||||
def self.new_receipt_no
|
||||
seed = SeedGenerator.find_by_model("sale")
|
||||
query = SeedGenerator.select("current").where("model='Sale'").first()
|
||||
new_receipt_no = 0
|
||||
if (seed.nil?)
|
||||
seed = SeedGenerator.new()
|
||||
seed.model = "sale"
|
||||
new_receipt_no = seed.next
|
||||
seed.save
|
||||
|
||||
if query.nil?
|
||||
new_receipt_no = 1
|
||||
else
|
||||
current_no = seed.next
|
||||
seed.next = seed.next
|
||||
seed.current = current_no
|
||||
seed.save
|
||||
new_receipt_no = query.current
|
||||
end
|
||||
|
||||
return seed.current
|
||||
return new_receipt_no
|
||||
end
|
||||
|
||||
# Generate for 4 digit Code
|
||||
def self.generate_code(model, prefix)
|
||||
seed = SeedGenerator.find_by_model(model)
|
||||
new_code = 0
|
||||
# def self.generate_code(model, prefix)
|
||||
# seed = SeedGenerator.find_by_model(model)
|
||||
# new_code = 0
|
||||
|
||||
if (seed.nil?)
|
||||
seed = SeedGenerator.new()
|
||||
seed.model = model
|
||||
new_code = seed.next
|
||||
seed.save
|
||||
else
|
||||
current_no = seed.next
|
||||
seed.next = seed.next + seed.increase_by
|
||||
seed.current = current_no
|
||||
seed.save
|
||||
end
|
||||
# if (seed.nil?)
|
||||
# seed = SeedGenerator.new()
|
||||
# seed.model = model
|
||||
# new_code = seed.next
|
||||
# seed.save
|
||||
# else
|
||||
# current_no = seed.next
|
||||
# seed.next = seed.next + seed.increase_by
|
||||
# seed.current = current_no
|
||||
# seed.save
|
||||
# end
|
||||
|
||||
|
||||
if prefix.length == 1
|
||||
padding_len = 5 - prefix.length
|
||||
count = 4-prefix.length
|
||||
else prefix.length == 2
|
||||
padding_len = 6 - prefix.length
|
||||
count = 5-prefix.length
|
||||
end
|
||||
next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
|
||||
return next_code
|
||||
end
|
||||
# if prefix.length == 1
|
||||
# padding_len = 5 - prefix.length
|
||||
# count = 4-prefix.length
|
||||
# else prefix.length == 2
|
||||
# padding_len = 6 - prefix.length
|
||||
# count = 5-prefix.length
|
||||
# end
|
||||
# next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
|
||||
# return next_code
|
||||
# end
|
||||
|
||||
def self.execute_query(model)
|
||||
current = 0
|
||||
nex = 0
|
||||
# def self.execute_query(model)
|
||||
# current = 0
|
||||
# nex = 0
|
||||
|
||||
# sql = "INSERT INTO seed_generators (model, created_at, updated_at)
|
||||
# VALUES('#{ model }', NOW(), NOW())
|
||||
# ON DUPLICATE KEY UPDATE current = next, next = next + 1;"
|
||||
|
||||
# select_sql = "select * from seed_generators where model='#{model}';"
|
||||
|
||||
# ActiveRecord::Base.connection.execute(sql);
|
||||
# select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
|
||||
sql = "INSERT INTO seed_generators (model, created_at, updated_at)
|
||||
VALUES('#{ model }', NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE current = current + 1, next = next + 1;"
|
||||
|
||||
select_sql = "select * from seed_generators where model='#{model}';"
|
||||
ActiveRecord::Base.connection.execute(sql);
|
||||
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
|
||||
select_result.each do |row|
|
||||
current = row [3]
|
||||
nex = row[4]
|
||||
end
|
||||
# select_result.each do |row|
|
||||
# current = row [3]
|
||||
# nex = row[4]
|
||||
# end
|
||||
|
||||
return current, nex
|
||||
end
|
||||
# return current, nex
|
||||
# end
|
||||
|
||||
def self.update_seed(model)
|
||||
current = 0
|
||||
nex = 0
|
||||
# def self.update_seed(model)
|
||||
# current = 0
|
||||
# nex = 0
|
||||
|
||||
update_sql = "UPDATE seed_generators set current = next, next = next + 1 WHERE model='#{model}';"
|
||||
select_sql = "select * from seed_generators where model='#{model}';"
|
||||
update_result = ActiveRecord::Base.connection.execute(update_sql);
|
||||
|
||||
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
# update_sql = "UPDATE seed_generators set current = next, next = next + 1 WHERE model='#{model}';"
|
||||
# select_sql = "select * from seed_generators where model='#{model}';"
|
||||
|
||||
select_result.each do |row|
|
||||
current = row [3]
|
||||
nex = row[4]
|
||||
end
|
||||
# update_result = ActiveRecord::Base.connection.execute(update_sql);
|
||||
# select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||
|
||||
# select_result.each do |row|
|
||||
# current = row [3]
|
||||
# nex = row[4]
|
||||
# end
|
||||
|
||||
return current, nex
|
||||
end
|
||||
# return current, nex
|
||||
# end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user