88 lines
3.4 KiB
Ruby
88 lines
3.4 KiB
Ruby
class BatchLineItemsController < ApplicationController
|
|
require "csv"
|
|
skip_before_filter :verify_authenticity_token
|
|
before_action :authenticate_member!
|
|
|
|
|
|
def index
|
|
user_id=current_member.user_id
|
|
@clients=Client.all.order('id desc')
|
|
find_client=Client.select(:id).order('id desc').first
|
|
if !find_client.nil?
|
|
client_id=find_client.id
|
|
|
|
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)
|
|
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)
|
|
end
|
|
end
|
|
def search
|
|
user_id=current_member.user_id
|
|
batch_list=params[:batch]
|
|
client_id=params[:client]
|
|
|
|
@batchLineItems=nil
|
|
|
|
if !batch_list.empty?
|
|
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])
|
|
|
|
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])
|
|
|
|
end
|
|
# if @batchLineItems.nil?
|
|
# @message="No search found!"
|
|
# end
|
|
end
|
|
def get_batch
|
|
client_id=params[:client_id]
|
|
batches=Batch.where('client_id=?',client_id)
|
|
if !batches.nil?
|
|
render :json => { :data => batches,:status => true }
|
|
# respond_with(data: batches,status: true)
|
|
else
|
|
# respond_with(data: nil,status: false)
|
|
end
|
|
end
|
|
def export
|
|
batch_list=""
|
|
@batchLineItems=nil
|
|
user_id=current_member.user_id
|
|
|
|
@clients=Client.all
|
|
@batches=Batch.where('user_id=?',user_id)
|
|
client_id=params[:client]
|
|
|
|
if params[:batch].present?
|
|
batch_list=params[:batch]
|
|
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")
|
|
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")
|
|
end
|
|
|
|
if !@batchLineItems.empty?
|
|
respond_to do |format|
|
|
format.html
|
|
format.csv { send_data @batchLineItems.to_csv(client_id), filename: "encoder-#{Date.today}.csv" }
|
|
end
|
|
else
|
|
flash[:notice] = "No data to export."
|
|
redirect_to batch_line_items_path
|
|
end
|
|
end
|
|
end |