Merge branch 'master' of bitbucket.org:code2lab/sxrestaurant into adminbsb_material_ui

This commit is contained in:
Aung Myo
2017-11-15 13:40:35 +06:30
18 changed files with 320 additions and 138 deletions

View File

@@ -17,7 +17,7 @@ class DiningCharge < ApplicationRecord
if charge_type == 'hr'
block_count, price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'hr')
elsif charge_type == 'day'
block_count, price = charges(dining_charges_obj, dining_minutes, 'day')
block_count, price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'day')
end
end
return block_count, price
@@ -27,40 +27,44 @@ class DiningCharge < ApplicationRecord
end
# dining charges calculate
def self.charges(chargesObj, dining_minutes, type)
solid_price = 0
charge_block = DiningCharge.convert_to_minutes(chargesObj.charge_block.utc.localtime.strftime('%H:%M'))
result = dining_minutes / charge_block
if result.to_i < 1
return result.to_i,chargesObj.unit_price
# for dining minute is under charge_block
return 1, result.to_i,chargesObj.unit_price
elsif result.to_i >= 1
solid_price = result * chargesObj.unit_price
remain_value = dining_minutes % charge_block
rounding_block = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.localtime.strftime('%H:%M'))
roundingblock = remain_value / rounding_block
rounding_time = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.localtime.strftime('%H:%M'))
roundingblock = remain_value / rounding_time
if roundingblock.to_i < 1
return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price)
# no time rounding block
return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price, roundingblock)
else
solid_price += roundingblock * chargesObj.time_rounding_block_price
return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price)
# remain_rounding = dining_minutes % charge_block
# if remain_rounding.to_i < 1
# return DiningCharge.check_rounding(chargesObj, solid_price)
# else
# return solid_price
# end
solid_price += (roundingblock * chargesObj.time_rounding_block_price)
return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price, roundingblock)
end
end
end
def self.check_rounding(chargesObj,solid_price)
if chargesObj.time_rounding == "down"
return solid_price
else
return solid_price += chargesObj.time_rounding_block_price
end
# check for rounding and calculate with rounding price
def self.check_rounding(chargesObj,solid_price, roundingblock)
rounding_block_remain = roundingblock % 1
if chargesObj.time_rounding == "down"
return solid_price
else
# check and calc for time rounding block for up
if rounding_block_remain > 0
return solid_price += chargesObj.time_rounding_block_price
else
return solid_price
end
end
end
def self.time_diff(start_time, end_time)

View File

@@ -1,7 +1,7 @@
class License
include HTTParty
base_uri "secure.smartsales.asia/api"
base_uri "provision.zsai.ws/api"
attr_accessor :name, :address_1, :address_2, :township, :city, :country, :email, :phone, :fax, :logo, :subdomain,
:plan_activation_date, :plan_next_renewal_date, :plan_max_products,:plan_max_customers, :plan_active_connections,
@@ -13,14 +13,14 @@ class License
def initialize(server = "", lookup = "")
#this code is hard-code to reflect server mode - Very important.
self.server_mode = "cloud"
self.server_mode = ENV["SERVER_MODE"]
if (server != "")
self.class.base_uri server
end
@secret = SecureRandom.hex(10)
@params = { query: { device: "SXlite", token: SECRETS_CONFIG['provision_key'] } }
# @secret = ENV["aes_key"]
# @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } }
end
def shop_code
@@ -29,11 +29,11 @@ class License
else
return self.subdomain.upcase
end
end
end
def detail_with_local_cache(lookup, key)
def detail_with_local_cache(lookup, key, iv)
##Check from local redis - if available load local otherwise get from remote
cache_key = "store:license:#{key}:hostname"
cache_key = "#{lookup}:license:#{key}:hostname"
# No Needs for current
# @secret = key
@@ -46,13 +46,11 @@ class License
end
Rails.logger.info "Cache key - " + cache_key.to_s
if cache_license.nil?
##change the d/e key
# @options = { query: {device: "SXlite", lookup: lookup, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
@params = { query: { device: "SXlite", token: SECRETS_CONFIG['provision_key']} }
response = self.class.get("/request_license", @params)
@params = { query: { lookup_type: self.server_mode, lookup: lookup, encrypted_key: key, iv_key: iv} }
response = self.class.get("/subdomain", @params)
@license = response.parsed_response
if (@license["status"] == true)
@@ -72,23 +70,56 @@ class License
end
Rails.logger.info 'API License'
else
@license = Marshal.load(cache_license) if cache_license
Rails.logger.info 'Cache License'
if (@license["status"] == true)
assign()
return true
end
end
return false
end
def detail_with_local_file()
has_license = true #verify_license()
if has_license
puts "VERIFIED"
end
# if cache_license.nil?
# ##change the d/e key
# @params = { query: { lookup_type: self.server_mode, lookup: lookup, encrypted_key: key, iv_key: iv} }
# response = self.class.get("/request_license", @params)
# @license = response.parsed_response
# if (@license["status"] == true)
# assign()
# Rails.logger.info "License - " + response.parsed_response.to_s
# Redis.current do |conn|
# ##Remote - store the remote response in local redis cache
# conn.set(cache_key, Marshal.dump(@license))
# ##ADD to List to remove later
# conn.sadd("License:cache:keys", cache_key)
# end
# return true
# end
# Rails.logger.info 'API License'
# else
# @license = Marshal.load(cache_license) if cache_license
# Rails.logger.info 'Cache License'
# if (@license["status"] == true)
# assign()
# return true
# end
# end
# return false
end
def detail
response = self.class.get("/subdomain", @options)
@license = response.parsed_response
@@ -104,9 +135,26 @@ class License
return false
end
def verify_license
api_token = read_license("api_token")
@options = { query: {lookup_type: "application", token: api_token} }
response = self.class.get("/verify", @options)
@varified = response.parsed_response
Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
if (@varified["status"])
if (!check_expired(@varified["plan_next_renewal_date"]))
return true
end
else
delete_license_file
end
return false
end
def check_remote_license(license_key)
# @options = { query: {device: "cloud", key: license_key, skey: @secret, token: Rails.application.secrets.provision_key} }
@options = { query: {device: "SXlite", key: license_key, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
@options = { query: {lookup_type: "application", encrypted_key: @secret, token: SECRETS_CONFIG['provision_key']} }
response = self.class.get("/license", @options)
@license = response.parsed_response
@@ -120,7 +168,7 @@ class License
end
def verify_by_api_token(api_token)
@options = { query: {device: "SXlite", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
@options = { query: {device: "SX", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
response = self.class.get("/verify", @options)
@license = response.parsed_response
@@ -135,15 +183,32 @@ class License
#Load License is remove from the cloud license because - this license is must be validated against subdmain instead of license.data from file.
def expired?
if (self.plan_next_renewal_date < Date.today)
def check_expired(renewal_date)
if (renewal_date < Date.today)
return true
else
return false
end
end
def self.check_license_file
return File.exist?("config/license.yml")
end
private
# read line by key for license file
def read_license(key)
decrypted_line = ""
if File.exist?("config/license.yml")
File.open("config/license.yml").each do |line|
if line.include? (key)
decrypted_line_array = line.split(":")
decrypted_line = AESCrypt.decrypt(decrypted_line_array[1])
end
end
end
end
private
def assign
# self.name = @license["name"]
# self.address_1 = @license["address_1"]

View File

@@ -0,0 +1,38 @@
class MyAesCrypt
@cipher = ""
def initialize
@cipher = OpenSSL::Cipher::Cipher.new(ENV["CIPHER_TYPE"])
end
def export_key(passphrase)
# We want a 256 bit key symetric key based on passphrase
digest = Digest::SHA256.new
key = digest.update(passphrase)
key = digest.digest
ENV['AES_KEY'] = cipher_key = key # stores the key in key, and also sets the generated key on the @cipher
ENV['AES_IV'] = cipher_iv = @cipher.random_iv # stores the iv in iv, and also sets the generated iv on the @cipher
return cipher_key, cipher_iv
end
private
def encrypt(data)
cipher.encrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
encrypted = cipher.update(data) + cipher.final
encrypted = Base64.encode64(encrypted)
return encrypted
end
def decrypt(data)
cipher.decrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
# Start the decryption
decoded = Base64.decode64(data)
decrypted = cipher.update(decoded) + cipher.final
return decrypted
end
end