Files
nemo_encoder/app/models/batch_line_item.rb
2017-01-25 17:35:30 +06:30

92 lines
2.7 KiB
Ruby

class BatchLineItem < ApplicationRecord
belongs_to :batch
attr_accessor :wristband_token
def self.generate_account_no
super_merchant = "101"
account_type='1'
location='101'
o = [('0'..'9')].map { |i| i.to_a }.flatten
random_account_no = (0...9).map { o[rand(o.length)] }.join
account_no = super_merchant+account_type+location+random_account_no
find_account_no = BatchLineItem.find_by_wristband_code(account_no)
if !find_account_no.nil?
o = [('0'..'9')].map { |i| i.to_a }.flatten
random_account_no = (0...9).map { o[rand(o.length)] }.join
account_no = super_merchant+account_type+location+random_account_no
end
return account_no
end
def self.generate_serial_no(user_id)
find_lookup=Lookup.find_by_name('generate_serial_no')
if !find_lookup.nil?
max_value=find_lookup.max_value
max_value=max_value +1
prefix=find_lookup.prefix
max_length=find_lookup.max_length
sufix_len=max_length-prefix.length
sufix_str="0" * sufix_len
value_len= max_value.to_s.length
start=0
ends=sufix_len-value_len-1
sufix_str= sufix_str[start..ends]
prefix=prefix[start..2]
prefix_str=prefix.to_s + user_id.to_s
serial_no=prefix_str.to_s+sufix_str+max_value.to_s
return serial_no
end
end
def self.to_csv(user_id)
encrypt_key=""
find_user=User.find_by_id(user_id)
if !find_user.nil?
encrypt_key=find_user.secrect_key
end
attributes = %w{serial_no wristband_code batch_id manufacture_uid card_type wristband_token}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |encoder|
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encoder.wristband_token=encryptd_data
csv << attributes.map{ |attr| encoder.send(attr)}
end
end
end
def self.encrypted(message,encrypt_key)
cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
cipher.encrypt
key = Digest::SHA1.hexdigest(encrypt_key)
iv =encrypt_key
cipher.key = encrypt_key
cipher.iv = encrypt_key
encrypted = cipher.update(message)
encrypted << cipher.final
encrypted=Base64.encode64(encrypted)
return encrypted
end
# def self.encrypted(data,encrypt_key)
# crypt = ActiveSupport::MessageEncryptor.new(encrypt_key)
# encrypted_data = crypt.encrypt_and_sign(data)
# encrypted_data=Digest::MD5.hexdigest(encrypted_data)
# data=encrypted_data.hex
# data=data.to_s
# return data[0..15]
# end
end