fix seed
This commit is contained in:
@@ -1,42 +1,15 @@
|
|||||||
class SeedGenerator < ApplicationRecord
|
class SeedGenerator < ApplicationRecord
|
||||||
# Generate ID for Tables
|
# Generate ID for Tables
|
||||||
def self.generate_id(model, prefix)
|
def self.generate_id(model, prefix)
|
||||||
seed = SeedGenerator.find_by_model(model)
|
cur_val, next_val = self.update_seed(model)
|
||||||
new_receipt_no = 0
|
|
||||||
|
|
||||||
if (seed.nil?)
|
if (cur_val == 0)
|
||||||
seed = SeedGenerator.new()
|
cur_val, next_val = self.execute_query(model)
|
||||||
seed.model = model
|
end
|
||||||
new_receipt_no = seed.next
|
|
||||||
seed.save
|
|
||||||
else
|
|
||||||
next_no = seed.next
|
|
||||||
current_no = seed.next
|
|
||||||
seed.next = seed.next + seed.increase_by
|
|
||||||
seed.current = current_no
|
|
||||||
seed.save
|
|
||||||
# cur_val, next_val = self.update_seed(model, seed.next, seed.increase_by)
|
|
||||||
|
|
||||||
# if next_no == cur_val
|
|
||||||
# puts "SSS"
|
|
||||||
# puts next_val
|
|
||||||
# cur_val2, next_val2 = self.update_seed(model, next_val, seed.increase_by)
|
|
||||||
# puts next_val2
|
|
||||||
# padding_len = 15 - prefix.length
|
|
||||||
# saleOrderId = prefix +"-"+ cur_val2.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
|
||||||
# puts saleOrderId
|
|
||||||
# return saleOrderId
|
|
||||||
# end
|
|
||||||
|
|
||||||
# padding_len = 15 - prefix.length
|
|
||||||
# saleOrderId = prefix +"-"+ cur_val.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
|
||||||
# return saleOrderId
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
padding_len = 15 - prefix.length
|
padding_len = 15 - prefix.length
|
||||||
saleOrderId = prefix +"-"+ seed.current.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
saleOrderId = prefix +"-"+ cur_val.to_s.to_s.rjust((14-prefix.length)+1,'0')
|
||||||
return saleOrderId
|
return saleOrderId
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate Receipt No
|
# Generate Receipt No
|
||||||
@@ -88,22 +61,41 @@ class SeedGenerator < ApplicationRecord
|
|||||||
return next_code
|
return next_code
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.update_seed(model, current, inc)
|
def self.execute_query(model)
|
||||||
cur_val = 0
|
current = 0
|
||||||
next_val = 0
|
nex = 0
|
||||||
nex = current + inc
|
|
||||||
|
sql = "INSERT INTO seed_generators (model, created_at, updated_at)
|
||||||
|
VALUES('#{ model }', NOW(), NOW())
|
||||||
|
ON DUPLICATE KEY UPDATE current = current + 1, next = next + 1;"
|
||||||
|
|
||||||
update_sql = "update seed_generators set current= #{current}, next= #{nex} where model='#{model}';";
|
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
|
||||||
|
|
||||||
|
return current, nex
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.update_seed(model)
|
||||||
|
current = 0
|
||||||
|
nex = 0
|
||||||
|
|
||||||
|
update_sql = "UPDATE seed_generators set current = current + 1, next = next + 1 WHERE model='#{model}';"
|
||||||
select_sql = "select * from seed_generators where model='#{model}';"
|
select_sql = "select * from seed_generators where model='#{model}';"
|
||||||
update_result = ActiveRecord::Base.connection.execute(update_sql);
|
update_result = ActiveRecord::Base.connection.execute(update_sql);
|
||||||
|
|
||||||
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
select_result = ActiveRecord::Base.connection.execute(select_sql);
|
||||||
|
|
||||||
select_result.each do |row|
|
select_result.each do |row|
|
||||||
cur_val = row [3]
|
current = row [3]
|
||||||
next_val = row[4]
|
nex = row[4]
|
||||||
end
|
end
|
||||||
|
|
||||||
return cur_val, next_val
|
return current, nex
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user