From 90eeb2ea1cd8b0c2dd33fd7c4a5df84f8e713987 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Mon, 6 Feb 2017 13:08:52 +0630 Subject: [PATCH] edit api --- .../batch_line_items_controller.rb | 63 +++++++++++-------- app/views/batch_line_items/index.html.erb | 57 +++++++++++++---- config/routes.rb | 5 +- 3 files changed, 85 insertions(+), 40 deletions(-) diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index 471084b..ca21559 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -2,40 +2,49 @@ class BatchLineItemsController < ApplicationController require "csv" skip_before_filter :verify_authenticity_token before_action :authenticate_member! + respond_to :json, :html + def index user_id=current_member.user_id + @clients=Client.all + @batches=Batch.where('user_id=?',user_id) @batchLineItems=BatchLineItem.joins('inner join batches on batches.id=batch_line_items.batch_id') .where('batches.user_id=?',user_id) .select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page]) end - def export - batch_list=params[:batch] - user_id=current_member.user_id - if !batch_list.nil? - batch_list.each do |batch| - find_batch=Batch.find_by_id(batch) - if !find_batch.nil? - export_count=find_batch.export_count - find_batch.export_count=export_count.to_i+1 - find_batch.save - end - end - @batchLineItems = BatchLineItem.where('batch_id in (?) ',batch_list).select('*') - else - batches=Batch.all - batches.each do |batch| - export_count=batch.export_count - batch.export_count=export_count.to_i+1 - batch.save - end - @batchLineItems = BatchLineItem.all.select('serial_no,wristband_code,batch_id,manufacture_uid,card_type') - end - - respond_to do |format| - format.html - format.csv { send_data @batchLineItems.to_csv(user_id), filename: "encoder-#{Date.today}.csv" } - end + def get_batch + client_id=params[:client_id] + batches=Batch.where('client_id=?',client_id) + if !batches.nil? + respond_with(data: batches,status: true) + else + respond_with(data: nil,status: false) + end end + def export + batch_list="" + 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 + 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 + end end \ No newline at end of file diff --git a/app/views/batch_line_items/index.html.erb b/app/views/batch_line_items/index.html.erb index 17aecac..9f7fbe3 100644 --- a/app/views/batch_line_items/index.html.erb +++ b/app/views/batch_line_items/index.html.erb @@ -4,21 +4,32 @@ Batch Line Item List +
+ <% 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 %> + <% end %>
@@ -58,9 +69,33 @@
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 48d1c8f..6537f2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,8 @@ Rails.application.routes.draw do get 'check_registered_email' => "users#check_registered_email" #Encoder match 'batch_line_items/export' => 'batch_line_items#export', :as => 'export', :via => :post - + + get 'get_batch' => 'batch_line_items#get_batch',:as => 'get_batch' namespace :api, defaults: {format: :json} do #login post "auth/login" =>"auth#login" @@ -31,6 +32,6 @@ Rails.application.routes.draw do get "batches/batch_progress_list" =>"batches#batch_progress_list" #client - get "clients/list" => 'clients#index' + get "clients" => 'clients#index' end end \ No newline at end of file