add new field

This commit is contained in:
Sunandar
2017-02-09 18:01:50 +06:30
parent 9dc6fd3cbe
commit 07477f1a06
7 changed files with 132 additions and 91 deletions

View File

@@ -4,7 +4,7 @@ class Api::BatchLineItemsController < ApplicationController
batch_id=params[:batch_id]
session_token=params[:session_token]
manufacture_uid = params[:card_manufacture]
product_category_id=params[:product_category_id]
# product_category_id=params[:product_category_id]
client_id=""
@@ -16,7 +16,9 @@ class Api::BatchLineItemsController < ApplicationController
find_batch = Batch.find_by_id(batch_id)
if !find_batch.nil?
card_type=find_batch.adult_or_child.upcase
find_product_category=ProductCategory.find_by_id(product_category_id)
product_category_id=find_batch.product_category_id
find_product_category=ProductCategory.find_by_id(product_category_id)
if !find_product_category.nil?
product_type=find_product_category.product_type_id
@@ -27,32 +29,58 @@ class Api::BatchLineItemsController < ApplicationController
# product_type=find_user.product_type
client_id=find_user.id
if !encrypt_key.nil?
check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid)
if check_manufacture.nil?
serial_no=BatchLineItem.generate_serial_no(client_id.to_s)
if product_type ==1
asset_identity=BatchLineItem.generate_account_no(location_code)
@out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type)
elsif product_type==2
str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s
if !encrypt_key.nil?
arr=Array.new
arr=BatchLineItem.get_array
str_dup=""
str_wb=""
dup_count=0
wb_count=0
arr=arr.as_json
arr.each do |item|
manufacture_uid=item
check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid)
if check_manufacture.nil?
serial_no=BatchLineItem.generate_serial_no(client_id.to_s)
digest_data= Digest::MD5.hexdigest(str)
hex_data=digest_data.hex
hex_str=hex_data.to_s
asset_identity=hex_str[0..15]
check_asset=BatchLineItem.find_by_asset_identity(asset_identity)
if check_asset.nil?
@out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type)
else
@out=false,'Asset Identity is already registered!'
if product_type ==1
asset_identity=BatchLineItem.generate_account_no(location_code)
@out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,"")
elsif product_type==2
str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s
digest_data= Digest::MD5.hexdigest(str)
hex_data=digest_data.hex
hex_str=hex_data.to_s
asset_identity=hex_str[0..15]
check_asset=BatchLineItem.find_by_asset_identity(asset_identity)
if check_asset.nil?
@out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type)
else
@out=false,'Asset Identity is already registered!'
str_wb=str_wb+asset_identity.to_s+","
wb_count=wb_count+1
end
end
end
else
@out=false,'Manufacture UID is already registered!'
end
else
@out=false,'Manufacture UID is already registered!'
put @out
str_dup=str_dup+manufacture_uid.to_s+","
dup_count=dup_count+1
end
end
puts str_dup
puts dup_count
puts str_wb
puts wb_count
puts 'Asset Identity duplicat' + wb_count.to_s
puts 'Manufacture UID '+ dup_count.to_s
else
@out=false,'Invalid User!'
end

View File

@@ -10,16 +10,22 @@ class Api::BatchesController < ApplicationController
card_qty = params[:card_qty]
adult_or_child=params[:adult_or_child]
client_id=params[:client_id]
product_category_id=params[:product_category_id]
find_client=Client.find_by_id(client_id)
if !find_client.nil?
user_id=check_member.user_id
batch = Batch.create_batch(created_by,date,order_ref,card_qty,user_id,adult_or_child,client_id)
if !batch.nil?
@out=true,batch.id
else
@out=false,'Error occurs in creating batch!'
end
if !find_client.nil?
find_product_category=ProductCategory.find_by_id(product_category_id)
if !find_product_category.nil?
user_id=check_member.user_id
batch = Batch.create_batch(created_by,date,order_ref,card_qty,user_id,adult_or_child,client_id,product_category_id)
if !batch.nil?
@out=true,batch.id
else
@out=false,'Error occurs in creating batch!'
end
else
@out=false,'Product Category does not exists!'
end
else
@out = false, "Client does not exist!"
end

View File

@@ -8,7 +8,7 @@ class Api::ClientsController < ApplicationController
clients =Client.all
arr_client=Array.new
clients.each do |client|
str={:id => client.id,:name => client.name,:email => client.email,:phone => client.phone,:address => client.address,:product_type => client.product_type}
str={:id => client.id,:name => client.name,:email => client.email,:phone => client.phone,:address => client.address}
arr_client.push(str)
end
@out=true,arr_client

View File

@@ -13,15 +13,14 @@ class BatchLineItemsController < ApplicationController
sub_query="(select * from batches where user_id="+user_id.to_s+" and client_id="+client_id.to_s+")"
@batchLineItems=BatchLineItem.joins('inner join '+sub_query+' as batches on batches.id=batch_line_items.batch_id')
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page]).per(10)
.select('batch_line_items.*,batches.order_ref as batch_name').order('batch_line_items.id desc').page(params[:page]).per(10)
else
sub_query="(select * from batches where user_id="+user_id.to_s+")"
@batchLineItems=BatchLineItem.joins('inner join '+ sub_query + ' as batches on batches.id=batch_line_items.batch_id')
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page]).per(10)
.select('batch_line_items.*,batches.order_ref as batch_name').order('batch_line_items.id desc').page(params[:page]).per(10)
end
end
def search
user_id=current_member.user_id
batch_list=params[:batch]
client_id=params[:client]
@@ -32,12 +31,12 @@ class BatchLineItemsController < ApplicationController
sub_query="(select * from batches where id in ("+batch_list+") and user_id="+user_id.to_s+" and client_id="+client_id.to_s+")"
@batchLineItems=BatchLineItem.joins('inner join '+ sub_query +' as batches on batches.id=batch_line_items.batch_id')
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page])
.select('batch_line_items.*,batches.order_ref as batch_name').order('batch_line_items.id desc').page(params[:page]).per(10)
else
sub_query="(select * from batches where user_id="+user_id.to_s+" and client_id="+client_id.to_s+")"
@batchLineItems=BatchLineItem.joins('inner join '+ sub_query +'as batches on batches.id=batch_line_items.batch_id')
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page])
.select('batch_line_items.*,batches.order_ref as batch_name').order('batch_line_items.id desc').page(params[:page]).per(10)
end
@@ -71,12 +70,21 @@ class BatchLineItemsController < ApplicationController
end
if !batch_list.empty?
Batch.where("client_id=? and id in (?)",client_id,batch_list).update_all("export_count = export_count + 1")
@batchLineItems = BatchLineItem.where('batch_id in (?)',batch_list).select("*,'' as secret_token,'' as location")
res=Batch.where("client_id=? and id in (?)",client_id,batch_list).update_all("export_count = export_count + 1")
# sub_query=Batch.where('id in (?)',batch_list).select('*').to_sql
sub_query= "(select * from batches where user_id="+user_id.to_s+" and client_id="+client_id.to_s+")"
@batchLineItems = BatchLineItem.joins('inner join '+sub_query +" as batches on batches.id=batch_line_items.batch_id")
.joins('inner join product_categories product on product.id=batches.product_category_id')
.where('batch_id in (?)',batch_list)
.select("*,product.product_type_id ,product.name,'' as secret_token,'' as location")
else
Batch.where("client_id=? ",client_id).update_all("export_count = export_count + 1")
sub_query="(select * from batches where client_id ="+client_id+" and user_id="+user_id.to_s+")"
@batchLineItems = BatchLineItem.joins('inner join '+sub_query+' as batches on batches.id=batch_line_items.batch_id').select("*,'' as secret_token,'' as location")
@batchLineItems = BatchLineItem.joins('inner join '+sub_query+' as batches on batches.id=batch_line_items.batch_id')
.joins('inner join product_categories as product on product.id=batches.product_category_id')
.select("*,product.product_type_id ,product.name,'' as secret_token,'' as location")
end
if !@batchLineItems.empty?

View File

@@ -1,7 +1,7 @@
class Batch < ApplicationRecord
has_many :batch_line_item
def self.create_batch(created_by,date,order_ref, card_qty,user_id,adult_or_child,client_id)
def self.create_batch(created_by,date,order_ref, card_qty,user_id,adult_or_child,client_id,product_category_id)
batch = Batch.new
batch.created_by = created_by
batch.order_ref = order_ref
@@ -14,6 +14,7 @@ class Batch < ApplicationRecord
batch.user_id=user_id
batch.export_count=0
batch.client_id=client_id
batch.product_category_id=product_category_id
batch.adult_or_child=adult_or_child.upcase
if batch.save
return batch

View File

@@ -2,12 +2,12 @@ class BatchLineItem < ApplicationRecord
belongs_to :batch
attr_accessor :secret_token
attr_accessor :location
attr_accessor :product_type
def self.generate_account_no(location_code)
super_merchant = "101"
account_type='1'
location=location_code
super_merchant = "101"
account_type='1'
location=location_code
o = [('0'..'9')].map { |i| i.to_a }.flatten
random_account_no = (0...9).map { o[rand(o.length)] }.join
@@ -15,14 +15,13 @@ class BatchLineItem < ApplicationRecord
account_no = super_merchant+account_type+location+random_account_no
find_account_no = BatchLineItem.find_by_asset_identity(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
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(client_id)
end
def self.generate_serial_no(client_id)
find_lookup=Lookup.find_by_name('generate_serial_no')
if !find_lookup.nil?
max_value=find_lookup.max_value
@@ -47,17 +46,24 @@ class BatchLineItem < ApplicationRecord
encrypt_key=""
find_client=Client.find_by_id(client_id)
if !find_client.nil?
encrypt_key=find_client.secrect_key
end
attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location}
if !find_client.nil?
encrypt_key=find_client.secrect_key
end
attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location}
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
all.each do |encoder|
if encoder.product_type_id ==1
puts 'Card'
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
else
puts 'Tickets'
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
end
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encoder.secret_token=encryptd_data
encoder.location=location_code
encoder.secret_token=encryptd_data
encoder.location=location_code
csv << attributes.map{ |attr| encoder.send(attr)}
end
@@ -102,22 +108,12 @@ class BatchLineItem < ApplicationRecord
end
end
def self.get_array
arr=Array.new
str= SecureRandom.hex(13)
uid=str[0..14]
arr=[
]
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
arr=Array.new
(1..50).each do |i|
str= SecureRandom.hex
uid=str[0..13]
arr.push(uid)
end
@arr=arr
end
end