add new files

This commit is contained in:
Sunandar
2017-01-24 17:45:40 +06:30
parent 2f427ae5b9
commit 2893f33a23
138 changed files with 36367 additions and 0 deletions

22
app/models/batch.rb Normal file
View File

@@ -0,0 +1,22 @@
class Batch < ApplicationRecord
has_many :batch_line_item
def self.create_batch(created_by,date,order_ref, card_qty,user_id)
batch = Batch.new
batch.created_by = created_by
batch.order_ref = order_ref
batch.date = date
batch.batch_start = true
batch.batch_start_time=DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d")
batch.qty_processing = card_qty.to_i
batch.qty_success = 0
batch.qty_fail = 0
batch.user_id=user_id
batch.export_count=0
if batch.save
return batch
else
return nil
end
end
end

View File

@@ -0,0 +1,89 @@
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-256-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

2
app/models/lookup.rb Normal file
View File

@@ -0,0 +1,2 @@
class Lookup < ApplicationRecord
end

21
app/models/member.rb Normal file
View File

@@ -0,0 +1,21 @@
class Member < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
belongs_to :user,optional: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:uid, :confirmable, :lockable
def self.authenticate_session_token(token)
if token.nil? || token.blank? || token.to_s == ' '
return false
else
mem = Member.find_by_session_token(token)
if mem.nil?
return false
else
return mem
end
end
end
end

4
app/models/user.rb Normal file
View File

@@ -0,0 +1,4 @@
class User < ApplicationRecord
has_many :members
validates :name, presence: { message: "Please enter name." }
end