From bf48376d856c1a1166dbeb55ba85f9f357dc4d3a Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 17 Oct 2018 14:25:47 +0630 Subject: [PATCH] license --- .../concerns/license_verification.rb | 1 - app/models/seed_generator.rb | 93 +++++++++---------- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/app/controllers/concerns/license_verification.rb b/app/controllers/concerns/license_verification.rb index 94e9ad91..e28cc28a 100644 --- a/app/controllers/concerns/license_verification.rb +++ b/app/controllers/concerns/license_verification.rb @@ -29,7 +29,6 @@ module LicenseVerification # redirect_to activate_path # end end - authenticate_session_token end def authenticate_session_token diff --git a/app/models/seed_generator.rb b/app/models/seed_generator.rb index d501f9db..709e5f45 100755 --- a/app/models/seed_generator.rb +++ b/app/models/seed_generator.rb @@ -1,37 +1,34 @@ class SeedGenerator < ApplicationRecord # Generate ID for Tables def self.generate_id(model, prefix) - query = SeedGenerator.select("next").where("model=?",model).first() + cur_val, next_val = self.update_seed(model) - 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); + if (cur_val == 0) + cur_val, next_val = self.execute_query(model) + end padding_len = 15 - prefix.length - generate_id = prefix +"-"+ nLast_val.to_s.to_s.rjust((14-prefix.length)+1,'0') - - return generate_id + generate_id = prefix +"-"+ cur_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 - query = SeedGenerator.select("current").where("model='Sale'").first() + seed = SeedGenerator.find_by_model("sale") new_receipt_no = 0 - - if query.nil? - new_receipt_no = 1 + if (seed.nil?) + seed = SeedGenerator.new() + seed.model = "sale" + new_receipt_no = seed.next + seed.save else - new_receipt_no = query.current + new_receipt_no = seed.next + seed.next = seed.next + seed.current = new_receipt_no + seed.save end - return new_receipt_no + return seed.current end # Generate for 4 digit Code @@ -63,43 +60,43 @@ class SeedGenerator < ApplicationRecord # 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;" + 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}';" + select_sql = "select * from seed_generators where model='#{model}';" - # ActiveRecord::Base.connection.execute(sql); - # select_result = ActiveRecord::Base.connection.execute(select_sql); + 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_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_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 + select_result.each do |row| + current = row [3] + nex = row[4] + end - # return current, nex - # end + return current, nex + end end