From 26d42631c73057d0220a62a197b9c4ad4bb28147 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Wed, 8 Feb 2017 17:35:37 +0630 Subject: [PATCH] edit api --- .../api/batch_line_items_controller.rb | 96 +++++++++---------- app/controllers/api/clients_controller.rb | 2 +- .../batch_line_items_controller.rb | 14 +-- app/controllers/clients_controller.rb | 2 +- app/models/batch_line_item.rb | 23 ++++- app/views/batch_line_items/_search.html.erb | 8 +- app/views/batch_line_items/index.html.erb | 16 ++-- app/views/batch_line_items/search.js.erb | 3 +- app/views/clients/_form.html.erb | 8 +- app/views/clients/index.html.erb | 8 +- config/routes.rb | 3 + db/migrate/20170203091136_create_clients.rb | 6 +- db/schema.rb | 19 +++- lib/tasks/setup.rake | 27 ++++++ 14 files changed, 140 insertions(+), 95 deletions(-) diff --git a/app/controllers/api/batch_line_items_controller.rb b/app/controllers/api/batch_line_items_controller.rb index 686b4e3..7eb5d9d 100644 --- a/app/controllers/api/batch_line_items_controller.rb +++ b/app/controllers/api/batch_line_items_controller.rb @@ -4,7 +4,8 @@ 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] + client_id="" check_member= Member.authenticate_session_token(session_token) @@ -14,69 +15,58 @@ 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_user=Client.find_by_id(find_batch.client_id) - if !find_user.nil? - encrypt_key=find_user.secrect_key - product_type=find_user.product_type - client_id=find_user.id + card_type=find_batch.adult_or_child.upcase + find_product_category=ProductCategory.find_by_id(product_category_id) + if !find_product_category.nil? + product_type=find_product_category.product_type_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.downcase=="ticket" - 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=create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) - else - @out=false,'Asset Identity is already registered!' + find_user=Client.find_by_id(find_batch.client_id) + if !find_user.nil? + encrypt_key=find_user.secrect_key + # 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 + @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 + + 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!' + end end - elsif product_type=="card" - @out=create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) - end + else + @out=false,'Manufacture UID is already registered!' + end else - @out=false,'Manufacture UID is already registered!' - end + @out=false,'Invalid User!' + end else - @out=false,'Invalid User!' + @out=false,"Client doesn't exist!" end else - @out=false,'Client does not exist!' + @out=false,"Product Category doesn't exist!" end else - @out=false,'Batch does not exist!' + @out=false,"Batch doesn't exist!" end else @out=false,'Sorry!Unauthorized user!' end end - def create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) - batchLineItem=BatchLineItem.new - batchLineItem.asset_identity=asset_identity - batchLineItem.serial_no=serial_no - batchLineItem.batch_id=batch_id - batchLineItem.manufacture_uid = manufacture_uid - batchLineItem.asset_type = card_type - batchLineItem.encoded_at = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") - batchLineItem.verified_at =DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") - - if batchLineItem.save - lookup=Lookup.find_by_name('generate_serial_no') - max_serail_no=lookup.max_value - lookup.max_value=max_serail_no.to_i+1 - lookup.save - - @result = true,batchLineItem.serial_no,batchLineItem.asset_identity - else - @result=false,'Error occurs in registration encoder!' - end - end + end \ No newline at end of file diff --git a/app/controllers/api/clients_controller.rb b/app/controllers/api/clients_controller.rb index 9006537..c1c9c46 100644 --- a/app/controllers/api/clients_controller.rb +++ b/app/controllers/api/clients_controller.rb @@ -15,5 +15,5 @@ class Api::ClientsController < ApplicationController else @out=false,"Sorry!Unauthorized user!" end - end + end end \ No newline at end of file diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index f0dba26..eee4f7b 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -6,8 +6,8 @@ class BatchLineItemsController < ApplicationController def index user_id=current_member.user_id - @clients=Client.all.order('id desc') - find_client=Client.select(:id).order('id desc').first + @clients=Client.all.order('id asc') + find_client=Client.select(:id).order('id asc').first if !find_client.nil? client_id=find_client.id @@ -20,7 +20,8 @@ class BatchLineItemsController < ApplicationController .select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page]).per(10) end end - def search + def search + user_id=current_member.user_id batch_list=params[:batch] client_id=params[:client] @@ -38,7 +39,8 @@ class BatchLineItemsController < ApplicationController @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]) - end + end + # if @batchLineItems.nil? # @message="No search found!" # end @@ -68,7 +70,7 @@ class BatchLineItemsController < ApplicationController 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") + @batchLineItems = BatchLineItem.where('batch_id in (?)',batch_list).select("*,'' as secret_token") 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+")" @@ -81,7 +83,7 @@ class BatchLineItemsController < ApplicationController format.csv { send_data @batchLineItems.to_csv(client_id), filename: "encoder-#{Date.today}.csv" } end else - flash[:notice] = "No data to export." + flash[:message] = "No data to export." redirect_to batch_line_items_path end end diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index ca39af1..fa809fb 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -90,6 +90,6 @@ class ClientsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def client_params - params.require(:client).permit(:name,:email, :phone, :address,:product_type) + params.require(:client).permit(:name,:email, :phone, :address) end end diff --git a/app/models/batch_line_item.rb b/app/models/batch_line_item.rb index 1c8758c..c4468f9 100644 --- a/app/models/batch_line_item.rb +++ b/app/models/batch_line_item.rb @@ -11,7 +11,7 @@ class BatchLineItem < ApplicationRecord 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) + 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 @@ -77,6 +77,27 @@ class BatchLineItem < ApplicationRecord return encrypted end + def self.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) + batchLineItem=BatchLineItem.new + batchLineItem.asset_identity=asset_identity + batchLineItem.serial_no=serial_no + batchLineItem.batch_id=batch_id + batchLineItem.manufacture_uid = manufacture_uid + batchLineItem.asset_type = card_type + batchLineItem.encoded_at = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") + batchLineItem.verified_at =DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") + + if batchLineItem.save + lookup=Lookup.find_by_name('generate_serial_no') + max_serail_no=lookup.max_value + lookup.max_value=max_serail_no.to_i+1 + lookup.save + + @result = true,batchLineItem.serial_no,batchLineItem.asset_identity + else + @result=false,'Error occurs in registration encoder!' + end + end # def self.encrypted(data,encrypt_key) # crypt = ActiveSupport::MessageEncryptor.new(encrypt_key) diff --git a/app/views/batch_line_items/_search.html.erb b/app/views/batch_line_items/_search.html.erb index a4ece21..296b471 100644 --- a/app/views/batch_line_items/_search.html.erb +++ b/app/views/batch_line_items/_search.html.erb @@ -2,10 +2,10 @@ Serail No - Wristband Code + Asset Identity Manufacture UID Batch No - Card Type + Asset Type Created At Update At @@ -15,10 +15,10 @@ <% @batchLineItems.each do |item| %> <%= item.serial_no rescue '' %> - <%= item.wristband_code rescue '' %> + <%= item.asset_identity rescue '' %> <%= item.manufacture_uid rescue '' %> <%= item.batch_name rescue '' %> - <%= item.card_type rescue '' %> + <%= item.asset_type rescue '' %> <%= item.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> <%= item.updated_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> diff --git a/app/views/batch_line_items/index.html.erb b/app/views/batch_line_items/index.html.erb index c462981..5581f33 100644 --- a/app/views/batch_line_items/index.html.erb +++ b/app/views/batch_line_items/index.html.erb @@ -29,10 +29,12 @@ Batch Line Item List
- <% flash.each do |key, value| %> + <% flash.each do |key, value| %> + <% if key =='message' %>
× - <%= value %> + <%= value %> + <% end %>
<% end %>
@@ -40,10 +42,10 @@ Serail No - Wristband Code + Asset Identity Manufacture UID Batch No - Card Type + Asset Type Created At Update At @@ -53,10 +55,10 @@ <% @batchLineItems.each do |item| %> <%= item.serial_no rescue '' %> - <%= item.wristband_code rescue '' %> + <%= item.asset_identity rescue '' %> <%= item.manufacture_uid rescue '' %> <%= item.batch_name rescue '' %> - <%= item.card_type rescue '' %> + <%= item.asset_type rescue '' %> <%= item.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> <%= item.updated_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> @@ -108,7 +110,7 @@ $(document).ready(function(){ batch=$(this).val(); filter(client,batch); - }); + }); function filter(client,batch){ if (batch==null){ batch="" diff --git a/app/views/batch_line_items/search.js.erb b/app/views/batch_line_items/search.js.erb index 2158b00..a4dc811 100644 --- a/app/views/batch_line_items/search.js.erb +++ b/app/views/batch_line_items/search.js.erb @@ -2,4 +2,5 @@ render(partial: 'batch_line_items/search', locals: { batchLineItems: @batchLineItems }) ) %> -$("#filterrific_results").html("<%= js %>"); \ No newline at end of file +$("#filterrific_results").html("<%= js %>"); +$('.pagination a').attr('data-remote', 'true'); \ No newline at end of file diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb index 469f93a..b37abe9 100644 --- a/app/views/clients/_form.html.erb +++ b/app/views/clients/_form.html.erb @@ -28,13 +28,7 @@
<%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %> -
-
- - <%= f.select :product_type,options_for_select([['CARD','card'],['TICKET','ticket']], params[:product_type]), - {}, { :class => 'form-control' } %> - <%= f.error :product_type ,style: 'color: red' %> -
+
diff --git a/app/views/clients/index.html.erb b/app/views/clients/index.html.erb index f6fb04d..c1787c3 100644 --- a/app/views/clients/index.html.erb +++ b/app/views/clients/index.html.erb @@ -21,8 +21,7 @@ Name Email Phone - Address - Product Type + Address Created At Action @@ -33,15 +32,14 @@ <%= client.name rescue '' %> <%= client.email rescue '' %> <%= client.phone rescue '' %> - <%= client.address rescue '' %> - <%= client.product_type rescue '' %> + <%= client.address rescue '' %> <%= client.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> <%= link_to 'Detail', client_path(client), :class => 'btn btn-primary btn-sm' %> <%= link_to 'Edit', edit_client_path(client), :class => 'btn btn-primary btn-sm' %> - <%= link_to 'Delete', clients_path(client), method: :delete, data: { confirm: 'Are you sure?' },:class => 'btn btn-primary btn-sm' %> + <%= link_to 'Delete', client_path(client), method: :delete, data: { confirm: 'Are you sure?' },:class => 'btn btn-primary btn-sm' %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 255cbf7..d632061 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,5 +35,8 @@ Rails.application.routes.draw do #client get "clients" => 'clients#index' + + #product + get "products" => "products#index" end end \ No newline at end of file diff --git a/db/migrate/20170203091136_create_clients.rb b/db/migrate/20170203091136_create_clients.rb index 04c4a31..949203d 100644 --- a/db/migrate/20170203091136_create_clients.rb +++ b/db/migrate/20170203091136_create_clients.rb @@ -4,11 +4,9 @@ class CreateClients < ActiveRecord::Migration[5.0] t.string :name,:null => false t.string :email t.string :phone - t.string :address - t.string :product_type + t.string :address t.string :secrect_key - t.timestamps null: false - t.timestamps + t.timestamps null: false end end end diff --git a/db/schema.rb b/db/schema.rb index d28650b..882a4d6 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: 20170203091136) do +ActiveRecord::Schema.define(version: 20170208065316) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -51,14 +51,13 @@ ActiveRecord::Schema.define(version: 20170203091136) do end create_table "clients", force: :cascade do |t| - t.string "name", null: false + t.string "name", null: false t.string "email" t.string "phone" t.string "address" - t.string "product_type" t.string "secrect_key" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "lookups", force: :cascade do |t| @@ -101,6 +100,16 @@ ActiveRecord::Schema.define(version: 20170203091136) do t.index ["user_id"], name: "index_members_on_user_id", using: :btree end + create_table "product_categories", force: :cascade do |t| + t.string "name" + t.integer "product_type_id" + t.index ["product_type_id"], name: "index_product_categories_on_product_type_id", using: :btree + end + + create_table "product_types", force: :cascade do |t| + t.string "name" + end + create_table "users", force: :cascade do |t| t.string "name", null: false t.string "nrc" diff --git a/lib/tasks/setup.rake b/lib/tasks/setup.rake index da8a7b8..3989daf 100644 --- a/lib/tasks/setup.rake +++ b/lib/tasks/setup.rake @@ -10,5 +10,32 @@ namespace :setup do lookup.max_length=16 lookup.save end + end + desc "Product Type" + task :set_product_type, [:name] => :environment do |t, args| + find_product_type=ProductType.find_by_name(args.name) + if find_product_type.nil? + product_type=ProductType.new + product_type.name=args.name + product_type.save + else + puts 'Product Type is already taken!' + end + end + desc "Product Categories" + task :set_product_category, [:name,:product_type] => :environment do |t, args| + find_product_type=ProductType.find_by_id(args.product_type) + if !find_product_type.nil? + find_product_category=ProductCategory.find_by_name(args.name) + + if find_product_category.nil? + product_category=ProductCategory.new + product_category.name=args.name + product_category.product_type_id=args.product_type + product_category.save + end + else + puts 'Produt Type does not exist!' + end end end \ No newline at end of file