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 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 def index @table_fields = [ 'venue_management.title', "contractor", "accounting_month", "bill_type", "caculation_basis", "reason", "total_amount", "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 = "
" 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 \'\'}).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 += "
" @filter_fields['vm_bill.bill_type'] = bill_type_select_str @filter_fields['vm_bill.accounting_month'] = ('
'+ (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)}) %>')) + "
~
" + (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)}) %>' ) ) + render_html("<%= submit_tag(I18n.t('venue_management.search')) %>") + '
') filter_bill @venue_management_page_url = Page.where(:module =>"venue_management").last.url rescue "" if request.xhr? render :partial => 'index' end end def filter_bill @ext_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? @ext_filename += (I18n.t("vm_inviting.type") + ": #{Tag.where(:id.in=>filter_tags).map{|t| t.name}.join(',')}") @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? @ext_filename += (I18n.t(:category) + ": #{Category.where(:id.in=>filters("category")).map{|t| t.title}.join(',')}") if @ext_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 if @bills.nil? if accounting_month_start != 0 && accounting_month_end != 0 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~#{accounting_month_end}") if @ext_filename.blank? @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 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~") if @ext_filename.blank? @bills = VenueManagementBill.where(:accounting_month.gte=>accounting_month_start).page(params[:page]).per(10) elsif accounting_month_end != 0 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": ~#{accounting_month_end}") if @ext_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 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~#{accounting_month_end}") if @ext_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 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{accounting_month_start}~") if @ext_filename.blank? @bills = @bills.where(:accounting_month.gt=>accounting_month_start).page(params[:page]).per(10) elsif accounting_month_end != 0 @ext_filename += (I18n.t("vm_bill.accounting_month") + ": ~#{accounting_month_end}") if @ext_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 if @bills.nil? && params[:id].blank? @bills = VenueManagementBill.all.page(params[:page]).per(10) elsif @bills.nil? @bills = VenueManagementBill.where(:venue_management_main_id=>params[:id]) elsif !params[:id].blank? @bills = @bills.where(:venue_management_main_id=>params[:id]) end if params[:keywords].present? @main_ids = @bills.pluck(:venue_management_main_id) @venue_managements = VenueManagementMain.where(:id.in=>@main_ids) if params[:keywords] == I18n.t(:empty) ids = @bills.map{|bill| ((bill.contractor.display_contractors.blank?) rescue true) ? bill.id : nil } else ids = @bills.map{|bill| (bill.contractor.display_contractors.include?(params[:keywords]) rescue false) ? bill.id : nil } end @main_ids = search_data(@venue_managements,[:title]).pluck(:id) ids1 = VenueManagementBill.where(:venue_management_main_id.in=>@main_ids).pluck(:id) @bills = VenueManagementBill.where(:id.in=>(ids + ids1)).page(params[:page]).per(10) end 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 def copy @venue_management_bill_to_copy = VenueManagementBill.find(params[:bill_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 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 def export filter_bill @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 @ext_filename = "export_bills" if @ext_filename.blank? @ext_filename = @ext_filename + "_export.xlsx" @id = params[:id] 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="' + @ext_filename + '"' } end render :layout => false # file_path = "tmp/cache/#{@ext_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 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