From 07477f1a06de8545159a8157dee07302fded9fd3 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Thu, 9 Feb 2017 18:01:50 +0630 Subject: [PATCH] add new field --- .../api/batch_line_items_controller.rb | 80 +++++++++++++------ app/controllers/api/batches_controller.rb | 22 +++-- app/controllers/api/clients_controller.rb | 2 +- .../batch_line_items_controller.rb | 24 ++++-- app/models/batch.rb | 3 +- app/models/batch_line_item.rb | 70 ++++++++-------- db/schema.rb | 22 ++--- 7 files changed, 132 insertions(+), 91 deletions(-) diff --git a/app/controllers/api/batch_line_items_controller.rb b/app/controllers/api/batch_line_items_controller.rb index 631917f..ddf241a 100644 --- a/app/controllers/api/batch_line_items_controller.rb +++ b/app/controllers/api/batch_line_items_controller.rb @@ -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 diff --git a/app/controllers/api/batches_controller.rb b/app/controllers/api/batches_controller.rb index 24a52b1..0a73ce5 100644 --- a/app/controllers/api/batches_controller.rb +++ b/app/controllers/api/batches_controller.rb @@ -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 diff --git a/app/controllers/api/clients_controller.rb b/app/controllers/api/clients_controller.rb index c1c9c46..c53a45e 100644 --- a/app/controllers/api/clients_controller.rb +++ b/app/controllers/api/clients_controller.rb @@ -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 diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index f3b134a..a8937fb 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -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? diff --git a/app/models/batch.rb b/app/models/batch.rb index a4fed70..eb85b04 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -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 diff --git a/app/models/batch_line_item.rb b/app/models/batch_line_item.rb index 6b5a0bd..2a354d5 100644 --- a/app/models/batch_line_item.rb +++ b/app/models/batch_line_item.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index acfef53..d30c891 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170208111712) do +ActiveRecord::Schema.define(version: 20170209102414) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,24 +29,26 @@ ActiveRecord::Schema.define(version: 20170208111712) do end create_table "batches", force: :cascade do |t| - t.datetime "date", null: false + t.datetime "date", null: false t.integer "user_id" t.integer "client_id" - t.string "order_ref", null: false + t.string "order_ref", null: false t.boolean "batch_start" t.boolean "batch_end" - t.string "created_by", null: false - t.integer "qty_processing", null: false - t.integer "qty_success", null: false - t.integer "qty_fail", null: false + t.string "created_by", null: false + t.integer "qty_processing", null: false + t.integer "qty_success", null: false + t.integer "qty_fail", null: false t.datetime "batch_start_time" t.datetime "batch_end_time" t.integer "export_count" t.string "remark" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "adult_or_child", default: "CHILD" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "adult_or_child", default: "CHILD" + t.integer "product_category_id" t.index ["client_id"], name: "index_batches_on_client_id", using: :btree + t.index ["product_category_id"], name: "index_batches_on_product_category_id", using: :btree t.index ["user_id"], name: "index_batches_on_user_id", using: :btree end