seed generator

This commit is contained in:
phyusin
2018-11-01 16:49:49 +06:30
parent 65bc094a97
commit 84a5490394

View File

@@ -8,8 +8,8 @@ class SeedGenerator < ApplicationRecord
end end
padding_len = 15 - prefix.length padding_len = 15 - prefix.length
last_val = prefix +"-"+ cur_val.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 last_val return saleOrderId
end end
# Generate Receipt No for number order (1,2,3) Don't touch # Generate Receipt No for number order (1,2,3) Don't touch
@@ -33,33 +33,33 @@ class SeedGenerator < ApplicationRecord
end end
# Generate for 4 digit Code # Generate for 4 digit Code
# def self.generate_code(model, prefix) def self.generate_code(model, prefix)
# seed = SeedGenerator.find_by_model(model) seed = SeedGenerator.find_by_model(model)
# new_code = 0 new_code = 0
# if (seed.nil?) if (seed.nil?)
# seed = SeedGenerator.new() seed = SeedGenerator.new()
# seed.model = model seed.model = model
# new_code = seed.next new_code = seed.next
# seed.save seed.save
# else else
# current_no = seed.next current_no = seed.next
# seed.next = seed.next + seed.increase_by seed.next = seed.next + seed.increase_by
# seed.current = current_no seed.current = current_no
# seed.save seed.save
# end end
# if prefix.length == 1 if prefix.length == 1
# padding_len = 5 - prefix.length padding_len = 5 - prefix.length
# count = 4-prefix.length count = 4-prefix.length
# else prefix.length == 2 else prefix.length == 2
# padding_len = 6 - prefix.length padding_len = 6 - prefix.length
# count = 5-prefix.length count = 5-prefix.length
# end end
# next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0') next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
# return next_code return next_code
# end end
def self.execute_query(model) def self.execute_query(model)
current = 0 current = 0
@@ -85,7 +85,7 @@ class SeedGenerator < ApplicationRecord
current = 0 current = 0
nex = 0 nex = 0
update_sql = "UPDATE seed_generators set current = next, next = next + 1 WHERE model='#{model}';" 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);