add new file

This commit is contained in:
Sunandar
2017-02-06 18:32:22 +06:30
parent 90eeb2ea1c
commit 5c78a2ca8a
13 changed files with 54 additions and 120 deletions

View File

@@ -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
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
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.'
format.csv { send_data @batchLineItems.to_csv(client_id), filename: "encoder-#{Date.today}.csv" }
end
end
end
end

View File

@@ -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

View File

@@ -1,6 +1,6 @@
class BatchLineItem < ApplicationRecord
belongs_to :batch
attr_accessor :wristband_token
attr_accessor :secret_token
def self.generate_account_no
super_merchant = "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

View File

@@ -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." }
end

View File

@@ -5,32 +5,7 @@
</nav>
</div>
<div class="row top-content">
<% flash.each do |name, msg| %>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert">×</a>
<%= msg %>
</div>
<% end %>
<br><br>
<%= form_tag export_path(:format => :csv),:method => :post do%>
<div class="col-lg-4">
</div>
<div class="col-lg-3">
<%= select_tag "client", options_from_collection_for_select(@clients, "id", "name"),:class => 'form-control'%>
</div>
<div class="col-lg-5">
<label><strong>Choose Batch:</strong></label>
<select class='selectpicker' id='batch' name='batch[]' multiple= "multiple" >
<!--<% @batches.each do |batch| %>
<option value="<%= batch.id %>"><%= batch.order_ref %></option>
<% end %> -->
</select>
&nbsp; &nbsp;
<%= button_tag "Export CSV",:class =>'btn btn-primary btn-sm',:id =>"btnexport" %>
</div>
<% end %>
</div>
<div class="row content">
<div class="card">
<div class="card-header">
@@ -68,34 +43,3 @@
</div>
</div>
</div>
<script>
$(document).ready(function(){
client_id=$("#client").val();
get_batch(client_id)
$('#client').on('change', function() {
client_id=$(this).val()
get_batch(client_id)
});
function get_batch(client_id){
$('#batch').multiselect('disable');
$.ajax({
type: "GET",
url: '<%= get_batch_path %>',
dataType: "json",
data : {'client_id':client_id },
success: function(result){
if (result.status){
$('#batch').multiselect('enable');
$("#batch").empty();
$("#batch").append("<option value='all'>ALL</option>")
$.each(result.data, function (i, item){
$("#batch").append("<option value="+item.id+">"+ item.order_ref +"</option>")
});
$('#batch').multiselect("refresh");
$("#batch").multiselect('rebuild');
}
}
});
}
})
</script>

View File

@@ -17,24 +17,17 @@
<%= f.input :name ,:label =>false,:error => false,:placeholder =>'Please enter client name',input_html: { class: "form-control" } %>
<%= f.error :name ,style: 'color: red' %>
</div>
<div class ="form-group" >
<label for="nrc" class="string optional control-label">NRC:</label>
<%= f.input :nrc ,:error=>false,:label =>false,:placeholder =>'Please enter client NRC',input_html: { class: "form-control" } %>
</div>
<div class ="form-group" >
<label for="email" class="string optional control-label">Email:</label>
<%= f.input :email ,:error=>false,:label =>false ,:placeholder =>'Please enter client email',input_html: { class: "form-control" } %>
<%= f.error :email ,style: 'color: red' %>
</div>
<div class ="form-group" >
<label for="phone" class="string optional control-label">Phone:</label>
<%= f.input :phone,:error=>false,:label =>false,:placeholder =>'Please enter client phone',input_html: { class: "form-control" } %>
<%= f.error :phone ,style: 'color: red' %>
</div>
<div class ="form-group" >
<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.error :address ,style: 'color: red' %>
</div>
<div class ="form-group" >
<label for="product_type" class="string optional control-label">Product Type:</label>

View File

@@ -19,7 +19,6 @@
<thead>
<tr>
<th>Name</th>
<th>Nrc</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
@@ -32,7 +31,6 @@
<% @clients.each do |client| %>
<tr>
<td><%= client.name rescue '' %></td>
<td><%= client.nrc rescue '' %></td>
<td><%= client.email rescue '' %></td>
<td><%= client.phone rescue '' %></td>
<td><%= client.address rescue '' %></td>

View File

@@ -11,10 +11,6 @@
<div class="col-lg-4"><strong>Name:</strong></div>
<div class="col-lg-8 uppercase"><%= @client.name %></div>
</div>
<div class="row">
<div class="col-lg-4"><strong>NRC:</strong></div>
<div class="col-lg-8"><%= @client.nrc %></div>
</div>
<div class="row">
<div class="col-lg-4"><strong>Email:</strong></div>
<div class="col-lg-8"><%= @client.email %></div>

View File

@@ -12,7 +12,14 @@
<%= link_to "Batches",batches_path, :class => "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Issued Batch Item",batch_line_items_path, :class => "nav-link" %>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Batch Line Item</a>
<div class="dropdown-menu">
<%= link_to "List", batch_line_items_path, :class => "dropdown-item" %>
<%= link_to "Export CSV", export_path, :class => "dropdown-item" %>
</div>
</li>
</li>
<li class="nav-item">
<%= link_to "User",users_path, :class => "nav-link" %>
@@ -20,6 +27,7 @@
<li class="nav-item">
<%= link_to "Client",clients_path, :class => "nav-link" %>
</li>
</ul>
<div class="float-xs-right">
<ul class="nav navbar-nav right">

View File

@@ -14,7 +14,7 @@ Rails.application.routes.draw do
#users
get 'check_registered_email' => "users#check_registered_email"
#Encoder
match 'batch_line_items/export' => 'batch_line_items#export', :as => 'export', :via => :post
match 'batch_line_items/export' => 'batch_line_items#export', :as => 'export', :via => [:post ,:get]
get 'get_batch' => 'batch_line_items#get_batch',:as => 'get_batch'
namespace :api, defaults: {format: :json} do

View File

@@ -2,7 +2,6 @@ class CreateClients < ActiveRecord::Migration[5.0]
def change
create_table :clients do |t|
t.string :name,:null => false
t.string :nrc
t.string :email
t.string :phone
t.string :address

View File

@@ -52,7 +52,6 @@ ActiveRecord::Schema.define(version: 20170203091136) do
create_table "clients", force: :cascade do |t|
t.string "name", null: false
t.string "nrc"
t.string "email"
t.string "phone"
t.string "address"