From 5c78a2ca8a79356e9d41bbf1a4c7cb4b1f53064c Mon Sep 17 00:00:00 2001 From: Sunandar Date: Mon, 6 Feb 2017 18:32:22 +0630 Subject: [PATCH] add new file --- .../batch_line_items_controller.rb | 41 ++++++------- app/controllers/clients_controller.rb | 2 +- app/models/batch_line_item.rb | 19 +++--- app/models/client.rb | 5 +- app/views/batch_line_items/create.csv.erb | 0 app/views/batch_line_items/index.html.erb | 58 +------------------ app/views/clients/_form.html.erb | 15 ++--- app/views/clients/index.html.erb | 6 +- app/views/clients/show.html.erb | 6 +- app/views/layouts/_navigation.html.erb | 16 +++-- config/routes.rb | 2 +- db/migrate/20170203091136_create_clients.rb | 3 +- db/schema.rb | 1 - 13 files changed, 54 insertions(+), 120 deletions(-) delete mode 100644 app/views/batch_line_items/create.csv.erb diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index ca21559..2c017cb 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -2,9 +2,10 @@ class BatchLineItemsController < ApplicationController require "csv" skip_before_filter :verify_authenticity_token before_action :authenticate_member! - respond_to :json, :html + # respond_to :json, :html,:csv def index + user_id=current_member.user_id @clients=Client.all @@ -18,33 +19,33 @@ class BatchLineItemsController < ApplicationController client_id=params[:client_id] batches=Batch.where('client_id=?',client_id) if !batches.nil? - respond_with(data: batches,status: true) + render :json => { :data => batches,:status => true } + # respond_with(data: batches,status: true) else - respond_with(data: nil,status: false) + # respond_with(data: nil,status: false) end end - def export + def export batch_list="" + @batchLineItems="" + user_id=current_member.user_id + + @clients=Client.all + @batches=Batch.where('user_id=?',user_id) + if params[:batch].present? batch_list=params[:batch] end client_id=params[:client] - - user_id=current_member.user_id - if !batch_list.empty? - if batch_list=="ALL" - Batch.where("client_id=?",client_id).update_all("export_count = export_count + 1") - @batchLineItems = BatchLineItem.where('client_id=?',client_id).select('serial_no,wristband_code,batch_id,manufacture_uid,card_type') - else - Batch.where("client_id=? and batch_id in (?)",client_id,batch_list).update_all("export_count = export_count + 1") - @batchLineItems = BatchLineItem.where('client_id=? and batch_id in (?)',client_id,batch_list).select('*') - 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") + respond_to do |format| - format.html - format.csv { send_data @batchLineItems.to_csv(user_id), filename: "encoder-#{Date.today}.csv" } - end - else - redirect_to batch_line_items_path, notice: 'No batch to export for this client.' - end + format.html + format.csv { send_data @batchLineItems.to_csv(client_id), filename: "encoder-#{Date.today}.csv" } + end + end end end \ No newline at end of file diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index e6dc4cd..ca39af1 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, :nrc, :email, :phone, :address,:product_type) + params.require(:client).permit(:name,:email, :phone, :address,:product_type) end end diff --git a/app/models/batch_line_item.rb b/app/models/batch_line_item.rb index 199afe2..b2993b7 100644 --- a/app/models/batch_line_item.rb +++ b/app/models/batch_line_item.rb @@ -1,8 +1,8 @@ class BatchLineItem < ApplicationRecord belongs_to :batch - attr_accessor :wristband_token - - def self.generate_account_no + attr_accessor :secret_token + + def self.generate_account_no super_merchant = "101" account_type='1' location='101' @@ -41,21 +41,20 @@ class BatchLineItem < ApplicationRecord end end - def self.to_csv(user_id) + def self.to_csv(client_id) encrypt_key="" - find_user=User.find_by_id(user_id) - if !find_user.nil? - encrypt_key=find_user.secrect_key + find_client=Client.find_by_id(client_id) + if !find_client.nil? + encrypt_key=find_client.secrect_key end - attributes = %w{serial_no wristband_code batch_id manufacture_uid card_type wristband_token} + attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_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 + encoder.secret_token=encryptd_data csv << attributes.map{ |attr| encoder.send(attr)} end diff --git a/app/models/client.rb b/app/models/client.rb index 2525c51..5970fca 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -1,7 +1,4 @@ class Client < ApplicationRecord validates :name, presence: { message: "Please enter client name." } - validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create - validates :email, presence: { message: "Please enter client email." } - validates :phone, presence: { message: "Please enter client phone." } - validates :address, presence: { message: "Please enter client address." } + validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create end diff --git a/app/views/batch_line_items/create.csv.erb b/app/views/batch_line_items/create.csv.erb deleted file mode 100644 index e69de29..0000000 diff --git a/app/views/batch_line_items/index.html.erb b/app/views/batch_line_items/index.html.erb index 9f7fbe3..aa4ad3f 100644 --- a/app/views/batch_line_items/index.html.erb +++ b/app/views/batch_line_items/index.html.erb @@ -5,32 +5,7 @@ -
- <% flash.each do |name, msg| %> -
- × - <%= msg %> -
- <% end %> -

- <%= form_tag export_path(:format => :csv),:method => :post do%> -
-
-
- <%= select_tag "client", options_from_collection_for_select(@clients, "id", "name"),:class => 'form-control'%> -
-
- - -     - <%= button_tag "Export CSV",:class =>'btn btn-primary btn-sm',:id =>"btnexport" %> -
- <% end %> -
+
@@ -68,34 +43,3 @@
- \ No newline at end of file diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb index 849cca9..469f93a 100644 --- a/app/views/clients/_form.html.erb +++ b/app/views/clients/_form.html.erb @@ -16,25 +16,18 @@ <%= f.input :name ,:label =>false,:error => false,:placeholder =>'Please enter client name',input_html: { class: "form-control" } %> <%= f.error :name ,style: 'color: red' %> - -
- - <%= f.input :nrc ,:error=>false,:label =>false,:placeholder =>'Please enter client NRC',input_html: { class: "form-control" } %> -
+
- <%= f.input :email ,:error=>false,:label =>false ,:placeholder =>'Please enter client email',input_html: { class: "form-control" } %> - <%= f.error :email ,style: 'color: red' %> + <%= f.input :email ,:error=>false,:label =>false ,:placeholder =>'Please enter client email',input_html: { class: "form-control" } %>
- <%= f.input :phone,:error=>false,:label =>false,:placeholder =>'Please enter client phone',input_html: { class: "form-control" } %> - <%= f.error :phone ,style: 'color: red' %> + <%= f.input :phone,:error=>false,:label =>false,:placeholder =>'Please enter client phone',input_html: { class: "form-control" } %>
- <%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %> - <%= f.error :address ,style: 'color: red' %> + <%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %>
diff --git a/app/views/clients/index.html.erb b/app/views/clients/index.html.erb index 73341e4..f6fb04d 100644 --- a/app/views/clients/index.html.erb +++ b/app/views/clients/index.html.erb @@ -18,8 +18,7 @@ - - + @@ -31,8 +30,7 @@ <% @clients.each do |client| %> - - + diff --git a/app/views/clients/show.html.erb b/app/views/clients/show.html.erb index 1939df1..4163fe0 100644 --- a/app/views/clients/show.html.erb +++ b/app/views/clients/show.html.erb @@ -10,11 +10,7 @@
Name:
<%= @client.name %>
-
-
-
NRC:
-
<%= @client.nrc %>
-
+
Email:
<%= @client.email %>
diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index 0a3dada..95ccc0a 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -12,14 +12,22 @@ <%= link_to "Batches",batches_path, :class => "nav-link" %> + + + + +
NameNrcName Email Phone Address
<%= client.name rescue '' %><%= client.nrc rescue '' %><%= client.name rescue '' %> <%= client.email rescue '' %> <%= client.phone rescue '' %> <%= client.address rescue '' %>