This commit is contained in:
Sunandar
2017-02-08 18:11:45 +06:30
parent 43ada02033
commit 9dc6fd3cbe
8 changed files with 46 additions and 23 deletions

View File

@@ -22,7 +22,8 @@ class Api::BatchLineItemsController < ApplicationController
find_user=Client.find_by_id(find_batch.client_id) find_user=Client.find_by_id(find_batch.client_id)
if !find_user.nil? if !find_user.nil?
encrypt_key=find_user.secrect_key encrypt_key=find_user.secrect_key
location_code=find_user.location_code
# product_type=find_user.product_type # product_type=find_user.product_type
client_id=find_user.id client_id=find_user.id
@@ -32,7 +33,7 @@ class Api::BatchLineItemsController < ApplicationController
serial_no=BatchLineItem.generate_serial_no(client_id.to_s) serial_no=BatchLineItem.generate_serial_no(client_id.to_s)
if product_type ==1 if product_type ==1
asset_identity=BatchLineItem.generate_account_no asset_identity=BatchLineItem.generate_account_no(location_code)
@out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) @out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type)
elsif product_type==2 elsif product_type==2
str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s

View File

@@ -62,7 +62,9 @@ class BatchLineItemsController < ApplicationController
@clients=Client.all @clients=Client.all
@batches=Batch.where('user_id=?',user_id) @batches=Batch.where('user_id=?',user_id)
client_id=params[:client] client_id=params[:client]
find_client=Client.find_by_id(client_id)
location_code=find_client.location_code
if params[:batch].present? if params[:batch].present?
batch_list=params[:batch] batch_list=params[:batch]
@@ -70,17 +72,17 @@ class BatchLineItemsController < ApplicationController
if !batch_list.empty? if !batch_list.empty?
Batch.where("client_id=? and id in (?)",client_id,batch_list).update_all("export_count = export_count + 1") 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") @batchLineItems = BatchLineItem.where('batch_id in (?)',batch_list).select("*,'' as secret_token,'' as location")
else else
Batch.where("client_id=? ",client_id).update_all("export_count = export_count + 1") 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+")" 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") @batchLineItems = BatchLineItem.joins('inner join '+sub_query+' as batches on batches.id=batch_line_items.batch_id').select("*,'' as secret_token,'' as location")
end end
if !@batchLineItems.empty? if !@batchLineItems.empty?
respond_to do |format| respond_to do |format|
format.html format.html
format.csv { send_data @batchLineItems.to_csv(client_id), filename: "encoder-#{Date.today}.csv" } format.csv { send_data @batchLineItems.to_csv(client_id,location_code), filename: "encoder-#{Date.today}.csv" }
end end
else else
flash[:message] = "No data to export." flash[:message] = "No data to export."

View File

@@ -90,6 +90,6 @@ class ClientsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def client_params def client_params
params.require(:client).permit(:name,:email, :phone, :address) params.require(:client).permit(:name,:email, :phone, :address,:location_code)
end end
end end

View File

@@ -1,11 +1,13 @@
class BatchLineItem < ApplicationRecord class BatchLineItem < ApplicationRecord
belongs_to :batch belongs_to :batch
attr_accessor :secret_token attr_accessor :secret_token
attr_accessor :location
def self.generate_account_no def self.generate_account_no(location_code)
super_merchant = "101" super_merchant = "101"
account_type='1' account_type='1'
location='101' location=location_code
o = [('0'..'9')].map { |i| i.to_a }.flatten o = [('0'..'9')].map { |i| i.to_a }.flatten
random_account_no = (0...9).map { o[rand(o.length)] }.join random_account_no = (0...9).map { o[rand(o.length)] }.join
@@ -41,20 +43,21 @@ class BatchLineItem < ApplicationRecord
end end
end end
def self.to_csv(client_id) def self.to_csv(client_id,location_code)
encrypt_key="" encrypt_key=""
find_client=Client.find_by_id(client_id) find_client=Client.find_by_id(client_id)
if !find_client.nil? if !find_client.nil?
encrypt_key=find_client.secrect_key encrypt_key=find_client.secrect_key
end end
attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token} attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location}
CSV.generate(headers: true) do |csv| CSV.generate(headers: true) do |csv|
csv << attributes csv << attributes
all.each do |encoder| all.each do |encoder|
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
encryptd_data=BatchLineItem.encrypted(str,encrypt_key) encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encoder.secret_token=encryptd_data encoder.secret_token=encryptd_data
encoder.location=location_code
csv << attributes.map{ |attr| encoder.send(attr)} csv << attributes.map{ |attr| encoder.send(attr)}
end end
@@ -98,7 +101,15 @@ class BatchLineItem < ApplicationRecord
@result=false,'Error occurs in registration encoder!' @result=false,'Error occurs in registration encoder!'
end end
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) # def self.encrypted(data,encrypt_key)
# crypt = ActiveSupport::MessageEncryptor.new(encrypt_key) # crypt = ActiveSupport::MessageEncryptor.new(encrypt_key)
# encrypted_data = crypt.encrypt_and_sign(data) # encrypted_data = crypt.encrypt_and_sign(data)

View File

@@ -1,4 +1,6 @@
class Client < ApplicationRecord class Client < ApplicationRecord
validates :name, presence: { message: "Please enter client name." } validates :name, presence: { message: "Please enter client name." }
validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create
validates :location_code ,presence: { message: "Please enter client location code." }
validates :location_code, :uniqueness => {:message =>"This location code is already taken." } ,on: :create
end end

View File

@@ -28,7 +28,13 @@
<div class ="form-group" > <div class ="form-group" >
<label for="address" class="string optional control-label">Address:</label> <label for="address" class="string optional control-label">Address:</label>
<%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %> <%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %>
</div> </div>
<% if @client.id.nil? %>
<div class ="form-group" >
<label for="address" class="string optional control-label">Location Code:</label>
<%= f.input :location_code,:error=>false,:maxlength=>3,:label =>false,:placeholder =>'Please enter client location code',input_html: { class: "form-control" } %>
</div>
<% end %>
<div class ="form-group" > <div class ="form-group" >
<label></label> <label></label>
<div class="actions"> <div class="actions">

View File

@@ -2,12 +2,12 @@ class CreateBatchLineItems < ActiveRecord::Migration[5.0]
def change def change
create_table :batch_line_items do |t| create_table :batch_line_items do |t|
t.references :batch t.references :batch
t.string :serial_no , null: false t.string :serial_no , null: false
t.string :asset_identity , null: false t.string :asset_identity , null: false
t.string :manufacture_uid ,null:false t.string :manufacture_uid ,null:false
t.string :asset_type,:default => "child" t.string :asset_type,:default => "child"
t.datetime :encoded_at t.datetime :encoded_at
t.datetime :verified_at t.datetime :verified_at
t.timestamps null: false t.timestamps null: false
end end
end end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170208065316) do ActiveRecord::Schema.define(version: 20170208111712) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -51,13 +51,14 @@ ActiveRecord::Schema.define(version: 20170208065316) do
end end
create_table "clients", force: :cascade do |t| create_table "clients", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "email" t.string "email"
t.string "phone" t.string "phone"
t.string "address" t.string "address"
t.string "secrect_key" t.string "secrect_key"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "location_code"
end end
create_table "lookups", force: :cascade do |t| create_table "lookups", force: :cascade do |t|