venue_management/app/controllers/admin/venue_management_bills_cont...

245 lines
13 KiB
Ruby
Raw Normal View History

2020-09-08 06:12:14 +00:00
class Admin::VenueManagementBillsController < Admin::VenueAdminController
include Admin::VenueManagementsHelper
include ActionView::Helpers::FormOptionsHelper
#include ActionView::Helpers::FormTagHelper #don't include this helper,or you will got an error when using redirect_to and url_for
#include ActionView::Helpers::FormHelper #don't include this helper,or you will got an error when using redirect_to and url_for
include VenueAdminHelper
include ActionView::Context
2020-09-08 06:12:14 +00:00
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_venue_management_bill, only: [:edit, :update, :destroy]
def initialize
super
@app_title = 'venue_management'
end
def filter_fields(categories)
{
:category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}}
}
end
2020-09-08 06:12:14 +00:00
def index
@table_fields = [
'venue_management.title',
"contractor",
"accounting_month",
"bill_type",
"caculation_basis",
"reason",
2020-10-05 16:09:17 +00:00
"total_amount",
2020-09-08 06:12:14 +00:00
"pay_date",
"pay_method",
"note"
]
@bills = nil
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories)
@tags = @module_app.tags
@filter_fields['vm_inviting.type'] = @tags.map{|t| {:title => t.name,:id => t.id} }
bill_type_select_str = "<div class=\"controls\"><form>"
value_bill_typeA = params[:filters]["bill_typeA"] rescue "0"
value_bill_typeB = params[:filters]["bill_typeB"] rescue "0"
bill_type_select_str += render_html('<%= select_tag "filters[bill_typeA]", options_for_select(VenueManagementBill::BILLTYPE.keys.map { |k| [ I18n.t("vm_bill.#{k}"), k ] },value_bill_typeA) ,{:onchange=>"var $this=this;(function(){var obj=#{VenueManagementBill::BILLTYPE.map{|k,v| [k,v.map{|vv| [vv,I18n.t("vm_bill.#{vv}")]}.to_h]}.to_h.to_s.gsub(\'=>\',\':\')};$($this).siblings(\'select\').html($.map(obj[$this.value],function(v,k){return \'<option value=\"\'+k+\'\">\'+v+\'</option>\'}).join(\' \'))})()"} %>',{:value_bill_typeA=>value_bill_typeA})
bill_typeB = (params[:filters]["bill_typeA"].blank? rescue true) ? VenueManagementBill::BILLTYPE.values.first : VenueManagementBill::BILLTYPE[params[:filters]["bill_typeA"]]
bill_type_select_str += render_html('<%= select_tag "filters[bill_typeB]", options_for_select(bill_typeB.map { |v| [ I18n.t("vm_bill.#{v}"), v ] },value_bill_typeB) , {:onchange=> "var $this=this;(function(){if($this.value == \'other\') $(\'#bill_other_field\').removeClass(\'hidden\');else $(\'#bill_other_field\').addClass(\'hidden\');})()"} %>',{:bill_typeB=>bill_typeB,:value_bill_typeB=>value_bill_typeB})
bill_type_select_str += render_html('<%= submit_tag(I18n.t(\'venue_management.search\')) %>')
bill_type_select_str += "</form></div>"
@filter_fields['vm_bill.bill_type'] = bill_type_select_str
@filter_fields['vm_bill.accounting_month'] = ('<form><div class="controls"><div style="float: left;">'+
(render_html('<%= org_datetime_picker("filters[accounting_month_start]", {:no_label => true, :format=>"yyyy/MM", :new_record=>false, :value=>(DateTime.parse(params[:filters]["accounting_month_start"]) rescue nil)}) %>')) +
"</div><span style=\"float: left;\">~</span><div style=\"float: left;\">" +
(render_html('<%= org_datetime_picker("filters[accounting_month_end]", {:no_label => true, :format=>"yyyy/MM", :new_record=>false, :value=>(DateTime.parse(params[:filters]["accounting_month_end"]) rescue nil)}) %>' ) ) +
2020-10-15 06:56:21 +00:00
render_html("<%= submit_tag(I18n.t('venue_management.search')) %>") +
'</div></div></form>')
filter_tags = params[:filters]["type"].to_a rescue []
@main_ids = nil
if !filter_tags.blank?
@main_ids = VenueManagementInviting.with_tags(filter_tags).pluck(:venue_management_main_id)
if !filters("category").blank?
@main_ids = VenueManagementMain.where(:id.in=>@main_ids).with_categories(filters("category")).pluck(:id)
end
elsif !filters("category").blank?
@main_ids = VenueManagementMain.all.with_categories(filters("category")).pluck(:id)
end
if ( !params[:filters]["bill_typeA"].blank? rescue false)
@bills = VenueManagementBill.where(:bill_typeA=>params[:filters]["bill_typeA"],:bill_typeB=>params[:filters]["bill_typeB"]).page(params[:page]).per(10)
end
accounting_month_start = DateTime.parse(params[:filters]["accounting_month_start"]) rescue 0
accounting_month_end = DateTime.parse(params[:filters]["accounting_month_end"]) rescue 0
if(accounting_month_end != 0)
accounting_month_end = accounting_month_end + 1.month - 1.day
end
if @bills.nil?
if accounting_month_start != 0 && accounting_month_end != 0
@bills = VenueManagementBill.where(:accounting_month.gte=>accounting_month_start,:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
elsif accounting_month_start != 0
@bills = VenueManagementBill.where(:accounting_month.gte=>accounting_month_start).page(params[:page]).per(10)
elsif accounting_month_end != 0
@bills = VenueManagementBill.where(:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
end
else
if accounting_month_start != 0 && accounting_month_end != 0
@bills = @bills.where(:accounting_month.gte=>accounting_month_start,:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
elsif accounting_month_start != 0
@bills = @bills.where(:accounting_month.gte=>accounting_month_start).page(params[:page]).per(10)
elsif accounting_month_end != 0
@bills = @bills.where(:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
end
end
if !@main_ids.nil?
2020-10-16 04:03:26 +00:00
if @bills.nil?
@bills = VenueManagementBill.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10)
else
@bills = @bills.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10)
end
end
if @bills.nil?
@bills = VenueManagementBill.all.page(params[:page]).per(10)
end
2020-09-08 06:12:14 +00:00
@venue_management_page_url = Page.where(:module =>"venue_management").last.url rescue ""
if request.xhr?
render '_index'
end
2020-09-08 06:12:14 +00:00
end
def new
@venue_management = VenueManagementMain.find(params[:id])
@venue_management_bill = VenueManagementBill.new(venue_management_main_id: params[:id])
end
def create
@venue_management_bill = VenueManagementBill.new(venue_management_bill_params)
@venue_management_bill.save
redirect_to params['referer_url']
end
def edit
@venue_management = VenueManagementMain.find(@venue_management_bill.venue_management_main_id)
end
2020-10-05 16:09:17 +00:00
def copy
@venue_management_bill_to_copy = VenueManagementBill.find(params[:id])
attributes = @venue_management_bill_to_copy.attributes.except("_id","updated_at","created_at")
VenueManagementBill.fields.each do |k,v|
if v.options[:localize]
attributes.delete k
attributes[k+"_translations"] = @venue_management_bill_to_copy[k]
elsif attributes[k].class == Hash
attributes[k] = BSON::Document.new(attributes[k])
end
end
@venue_management_bill = VenueManagementBill.new(attributes)
#render :html =>attributes and return
@venue_management = VenueManagementMain.find(@venue_management_bill_to_copy.venue_management_main_id)
end
2020-09-08 06:12:14 +00:00
def update
@venue_management_bill.update_attributes(venue_management_bill_params)
redirect_to venue_management_bills_admin_venue_management_path(@venue_management_bill.venue_management_main_id)
end
def destroy
@venue_management_main_id = @venue_management_bill.venue_management_main_id
@venue_management_bill.destroy
redirect_to venue_management_bills_admin_venue_management_path(@venue_management_main_id)
end
def render_html(code,pass_variables={})
render_to_string(:inline=>code,:locals=>pass_variables)
end
2020-10-16 04:03:26 +00:00
def export
filename = ""
@bills = nil
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories)
filter_tags = params[:filters]["type"].to_a rescue []
@main_ids = nil
if !filter_tags.blank?
filename += (I18n.t("vm_inviting.type") + ": #{Tag.where(:id.in=>filter_tags).map{|t| t.name}.join(',')}")
2020-10-16 04:03:26 +00:00
@main_ids = VenueManagementInviting.with_tags(filter_tags).pluck(:venue_management_main_id)
if !filters("category").blank?
@main_ids = VenueManagementMain.where(:id.in=>@main_ids).with_categories(filters("category")).pluck(:id)
end
elsif !filters("category").blank?
filename += (I18n.t(:category) + ": #{Category.where(:id.in=>filters("category")).map{|t| t.title}.join(',')}") if filename.blank?
@main_ids = VenueManagementMain.all.with_categories(filters("category")).pluck(:id)
end
if ( !params[:filters]["bill_typeA"].blank? rescue false)
@bills = VenueManagementBill.where(:bill_typeA=>params[:filters]["bill_typeA"],:bill_typeB=>params[:filters]["bill_typeB"]).page(params[:page]).per(10)
end
accounting_month_start = DateTime.parse(params[:filters]["accounting_month_start"]) rescue 0
accounting_month_end = DateTime.parse(params[:filters]["accounting_month_end"]) rescue 0
if(accounting_month_end != 0)
accounting_month_end = accounting_month_end + 1.month - 1.day
end
2020-10-16 04:03:26 +00:00
if @bills.nil?
if accounting_month_start != 0 && accounting_month_end != 0
filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~#{accounting_month_end}") if filename.blank?
@bills = VenueManagementBill.where(:accounting_month.gte=>accounting_month_start,:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
2020-10-16 04:03:26 +00:00
elsif accounting_month_start != 0
filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~") if filename.blank?
@bills = VenueManagementBill.where(:accounting_month.gte=>accounting_month_start).page(params[:page]).per(10)
2020-10-16 04:03:26 +00:00
elsif accounting_month_end != 0
filename += (I18n.t("vm_bill.accounting_month") + ": ~#{accounting_month_end}") if filename.blank?
@bills = VenueManagementBill.where(:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
end
else
if accounting_month_start != 0 && accounting_month_end != 0
filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~#{accounting_month_end}") if filename.blank?
@bills = @bills.where(:accounting_month.gt=>accounting_month_start,:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
elsif accounting_month_start != 0
filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~") if filename.blank?
@bills = @bills.where(:accounting_month.gt=>accounting_month_start).page(params[:page]).per(10)
elsif accounting_month_end != 0
filename += (I18n.t("vm_bill.accounting_month") + ": ~#{accounting_month_end}") if filename.blank?
@bills = @bills.where(:accounting_month.lte=>accounting_month_end).page(params[:page]).per(10)
end
end
if !@main_ids.nil?
if @bills.nil?
@bills = VenueManagementBill.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10)
else
@bills = @bills.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10)
end
end
2020-10-16 08:12:28 +00:00
if @bills.nil? && params[:id].blank?
2020-10-16 04:03:26 +00:00
@bills = VenueManagementBill.all.page(params[:page]).per(10)
2020-10-16 08:12:28 +00:00
elsif @bills.nil?
@bills = VenueManagementBill.where(:venue_management_main_id=>params[:id])
else
@bills = @bills.where(:venue_management_main_id=>params[:id])
2020-10-16 04:03:26 +00:00
end
@bills = @bills.page(1).per(@bills.count)
@protocol = (request.referer.blank? ? "http" : URI(request.referer).scheme)
@host = "#{@protocol}://#{request.host_with_port}"
@site_in_use_locales = Site.first.in_use_locales
filename = "export_bills" if filename.blank?
filename = filename + "_export.xlsx"
@id = params[:id]
2020-10-16 04:03:26 +00:00
respond_to do |format|
format.xlsx {
response.headers['Content-Transfer-Encoding'] = 'binary'
response.headers['Content-Type'] = 'binary/octet-stream'
response.headers['Content-Disposition'] = 'attachment; filename="' + filename + '"'
}
end
render :layout => false
# file_path = "tmp/cache/#{filename}"
# File.open(file_path, 'w') do |f|
# f.write render_to_string( handlers: [:axlsx], formats: [:xlsx] ,locals: {:@bills=>@bills,:@protocol=>@protocol,:@host=>@host,:@site_in_use_locales=>@site_in_use_locales}, :layout=>false )
# end
# send_file file_path
end
2020-09-08 06:12:14 +00:00
private
def set_venue_management_bill
@venue_management_bill = VenueManagementBill.find(params[:id])
end
def venue_management_bill_params
params.require(:venue_management_bill).permit!
end
def venue_management_bill_params
params.require(:venue_management_bill).permit!
end
end