From 893826021248c05567d5326c65be354de4d15baf Mon Sep 17 00:00:00 2001 From: bohung Date: Fri, 16 Oct 2020 12:03:26 +0800 Subject: [PATCH] Add export to bill page. --- .../venue_management_bills_controller.rb | 81 ++++++++++++++++++- .../admin/venue_managements_controller.rb | 2 +- .../venue_management_bills/_form.html.erb | 1 + .../venue_management_bills/_index.html.erb | 28 ++++--- .../venue_management_bills/export.xlsx.axlsx | 62 ++++++++++++++ .../venue_management_invitings/_form.html.erb | 4 +- config/routes.rb | 1 + 7 files changed, 163 insertions(+), 16 deletions(-) create mode 100644 app/views/admin/venue_management_bills/export.xlsx.axlsx diff --git a/app/controllers/admin/venue_management_bills_controller.rb b/app/controllers/admin/venue_management_bills_controller.rb index 549d1fd..aaa1f5b 100644 --- a/app/controllers/admin/venue_management_bills_controller.rb +++ b/app/controllers/admin/venue_management_bills_controller.rb @@ -82,7 +82,11 @@ class Admin::VenueManagementBillsController < Admin::VenueAdminController end end if !@main_ids.nil? - @bills = @bills.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10) + 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) @@ -139,6 +143,81 @@ class Admin::VenueManagementBillsController < Admin::VenueAdminController def render_html(code,pass_variables={}) render_to_string(:inline=>code,:locals=>pass_variables) end + 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_bill.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? + 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 @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.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 = VenueManagementBill.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 = 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 + if @bills.nil? + @bills = VenueManagementBill.all.page(params[:page]).per(10) + 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" + 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 private def set_venue_management_bill diff --git a/app/controllers/admin/venue_managements_controller.rb b/app/controllers/admin/venue_managements_controller.rb index ea2cc9c..b93fa50 100644 --- a/app/controllers/admin/venue_managements_controller.rb +++ b/app/controllers/admin/venue_managements_controller.rb @@ -364,7 +364,7 @@ class Admin::VenueManagementsController < Admin::VenueAdminController (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'))") + + render_html("<%= submit_tag(I18n.t('venue_management.search')) %>") + '
') filter_tags = params[:filters]["type"].to_a rescue [] @main_ids = nil diff --git a/app/views/admin/venue_management_bills/_form.html.erb b/app/views/admin/venue_management_bills/_form.html.erb index 4c94094..443da6a 100644 --- a/app/views/admin/venue_management_bills/_form.html.erb +++ b/app/views/admin/venue_management_bills/_form.html.erb @@ -203,6 +203,7 @@ <% end %> <% end %> + <%= render partial: 'admin/venue_shared/venue_file_link',locals: {venue: @venue_management_bill,f: f} %> diff --git a/app/views/admin/venue_management_bills/_index.html.erb b/app/views/admin/venue_management_bills/_index.html.erb index 07e2d8b..2c82eca 100644 --- a/app/views/admin/venue_management_bills/_index.html.erb +++ b/app/views/admin/venue_management_bills/_index.html.erb @@ -1,9 +1,5 @@ <% content_for :page_specific_javascript do %> - <%= javascript_include_tag "lib/bootstrap-fileupload" %> - <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> - <%= javascript_include_tag "lib/file-type" %> - <%= javascript_include_tag "lib/module-area" %> <% end %>