Add export to bill page.

This commit is contained in:
BoHung Chiu 2020-10-16 12:03:26 +08:00
parent b717ce7a14
commit 8938260212
7 changed files with 163 additions and 16 deletions

View File

@ -82,7 +82,11 @@ class Admin::VenueManagementBillsController < Admin::VenueAdminController
end end
end end
if !@main_ids.nil? 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 end
if @bills.nil? if @bills.nil?
@bills = VenueManagementBill.all.page(params[:page]).per(10) @bills = VenueManagementBill.all.page(params[:page]).per(10)
@ -139,6 +143,81 @@ class Admin::VenueManagementBillsController < Admin::VenueAdminController
def render_html(code,pass_variables={}) def render_html(code,pass_variables={})
render_to_string(:inline=>code,:locals=>pass_variables) render_to_string(:inline=>code,:locals=>pass_variables)
end 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 private
def set_venue_management_bill def set_venue_management_bill

View File

@ -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_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;\">" + "</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)}) %>' ) ) + (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')) %>") +
'</div></div></form>') '</div></div></form>')
filter_tags = params[:filters]["type"].to_a rescue [] filter_tags = params[:filters]["type"].to_a rescue []
@main_ids = nil @main_ids = nil

View File

@ -203,6 +203,7 @@
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
<%= render partial: 'admin/venue_shared/venue_file_link',locals: {venue: @venue_management_bill,f: f} %>
</div> </div>
</div> </div>

View File

@ -1,9 +1,5 @@
<% content_for :page_specific_javascript do %> <% 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/datetimepicker/datetimepicker.js" %>
<%= javascript_include_tag "lib/file-type" %>
<%= javascript_include_tag "lib/module-area" %>
<% end %> <% end %>
<script> <script>
if(document.querySelectorAll("#orbit-bar").length==0) location.reload(); if(document.querySelectorAll("#orbit-bar").length==0) location.reload();
@ -55,27 +51,35 @@
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<% filter_url = request.fullpath.split("?")[1..-1].join("?") %>
<%=
content_tag :div, class: "bottomnav clearfix" do
content_tag :div, paginate(@bills), class: "pagination pagination-centered"
end
%>
<% if @venue_management.present? %> <% if @venue_management.present? %>
<!-- footer --> <!-- footer -->
<div class="bottomnav clearfix"> <div class="bottomnav clearfix">
<%= content_tag :div, class: "pagination pagination-centered" do %> <%= content_tag :div, class: "pagination-centered" do %>
<%= paginate(@bills) %> <%= paginate(@bills) %>
<div class="action pull-right"> <div class="action pull-right">
<%= link_to content_tag(:i, nil, class: "fa fa-download") + " " + t('venue_management.export'), export_admin_venue_management_bills_path(:format=>'xlsx')+"#{(filter_url.blank? ? '' : ('?'+filter_url))}", class: "btn btn-primary", target: '_blank' %>
<%= link_to content_tag(:i, nil, class: "icons-plus") + " " + t(:add), new_admin_venue_management_bill_path(id: @venue_management.id), class: "btn btn-primary" %> <%= link_to content_tag(:i, nil, class: "icons-plus") + " " + t(:add), new_admin_venue_management_bill_path(id: @venue_management.id), class: "btn btn-primary" %>
</div> </div>
<% end %> <% end %>
</div> </div>
<!-- footer:end --> <!-- footer:end -->
<% else %>
<%=
content_tag :div, class: "bottomnav clearfix" do
(content_tag(:div, class: "pagination-centered") do
paginate(@bills)
end) +
('<div class="action pull-right">'.html_safe +
link_to(content_tag(:i, nil, class: "fa fa-download") + " " + t('venue_management.export'), export_admin_venue_management_bills_path(:format=>'xlsx')+"#{(filter_url.blank? ? '' : ('?'+filter_url))}", class: "btn btn-primary", target: '_blank') +
'</div>'.html_safe)
end
%>
<% end %> <% end %>
<style type="text/css"> <style type="text/css">
.pagination-centered .pagination{ .pagination-centered .pagination{
width: calc(100% - 6em); width: calc(100% - 18em);
padding-left: 6em;
float: left; float: left;
} }
.main-list { .main-list {

View File

@ -0,0 +1,62 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: t('restful_actions.venue_management_bills')) do |sheet|
row = []
row << t('venue_management.contractor')
row << t('vm_bill.accounting_month')
row << t('vm_bill.bill_type')
row << t('vm_bill.caculation_basis')
@site_in_use_locales.each do |locale|
row << t('vm_bill.reason') + " - " + t(locale)
end
@site_in_use_locales.each do |locale|
row << t('vm_bill.pay_method') + " - " + t(locale)
end
@site_in_use_locales.each do |locale|
row << t('vm_bill.note') + " - " + t(locale)
end
row << t("link")
@site_in_use_locales.each do |locale|
row << t("link") + " " + t("url_alt") + " - " + t(locale)
end
row << t("file_")
@site_in_use_locales.each do |locale|
row << t("file_") + " " + t("description") + " - " + t(locale)
end
@site_in_use_locales.each do |locale|
row << t("file_") + " " + t("alternative") + " - " + t(locale)
end
sheet.add_row row
@bills.each do |bill|
row2 = []
row2 << (bill.contractor.display_contractors rescue "")
row2 << bill.display_accounting_month
row2 << "#{t("vm_bill.#{bill.bill_typeA}")}/#{t("vm_bill.#{bill.bill_typeB}")}"
row2 << bill.display_caculation_basis
@site_in_use_locales.each do |locale|
row2 << bill.reason_translations[locale]
end
@site_in_use_locales.each do |locale|
row2 << bill.pay_method_translations[locale]
end
@site_in_use_locales.each do |locale|
row2 << bill.note_translations[locale]
end
row2 << bill.venue_management_links.map{|l| l.url.to_s.strip}.join(";")
@site_in_use_locales.each do |locale|
row2 << bill.venue_management_links.map{|l| l.title_translations[locale].to_s.strip}.join(";")
end
row2 << bill.venue_management_files.map{|f| @host + f.file.url rescue nil}.select{|s| !s.nil?}.join(";")
@site_in_use_locales.each do |locale|
row2 << bill.venue_management_files.map{|f| f.description_translations[locale].to_s.strip}.join(";")
end
@site_in_use_locales.each do |locale|
row2 << bill.venue_management_files.map{|f| f.title_translations[locale].to_s.strip}.join(";")
end
sheet.add_row row2
end
end

View File

@ -86,13 +86,13 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t('vm_inviting.house_tax_payer') %></label> <label class="control-label muted"><%= t('vm_inviting.house_tax_payer') %></label>
<div class="controls"> <div class="controls">
<%= f.select :house_tax_payer, VenueManagementInviting::PAYERS.map { |p| [t("vm_inviting.enums.#{p}"), p] } %> <%= f.select :house_tax_payer, options_for_select(VenueManagementInviting::PAYERS.map { |p| [t("vm_inviting.enums.#{p}"), p] },(@venue_management_inviting.new_record? ? VenueManagementInviting::PAYERS.last : @venue_management_inviting.house_tax_payer)) %>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t('vm_inviting.land_tax_payer') %></label> <label class="control-label muted"><%= t('vm_inviting.land_tax_payer') %></label>
<div class="controls"> <div class="controls">
<%= f.select :land_tax_payer, VenueManagementInviting::PAYERS.map { |p| [t("vm_inviting.enums.#{p}"), p] } %> <%= f.select :land_tax_payer, options_for_select(VenueManagementInviting::PAYERS.map { |p| [t("vm_inviting.enums.#{p}"), p] },(@venue_management_inviting.new_record? ? VenueManagementInviting::PAYERS.last : @venue_management_inviting.land_tax_payer)) %>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">

View File

@ -34,6 +34,7 @@ Rails.application.routes.draw do
scope "(:id)" do scope "(:id)" do
get 'copy', to: 'venue_management_bills#copy' get 'copy', to: 'venue_management_bills#copy'
end end
get 'export'
end end
end end
resources :venue_management_contracts do resources :venue_management_contracts do