# frozen_string_literal: true class Admin::VenueManagementContractsController < Admin::VenueAdminController include Admin::VenueManagementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_venue_management_contract, only: [:edit, :update, :destroy] def filter_fields(categories) { :category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}} } end def initialize super @app_title = 'venue_management' end def fetch_inviting invitings = VenueManagementInviting.where(id: params[:id]) data = {} if !invitings.first.nil? key = [:early_rent,:operation_rent,:royalty,:house_tax_payer,:land_tax_payer] invitings.pluck(*key)[0].each_with_index do |v,i| data[key[i]] = v end end render :json => data end def index @table_fields = [ 'venue_management.title', 'vm_contract.case_no', 'vm_inviting.type', 'vm_contract.vendor', 'vm_inviting.rents', 'vm_inviting.royalty', 'vm_contract.start_end', 'vm_contract.deposit_amount_type_exp_date', 'vm_contract.renewal_permission', 'vm_contract.other_commitment', 'vm_contract.note' ] @filter_fields = filter_fields(@categories) @tags = @module_app.tags @filter_fields['vm_inviting.type'] = @tags.map{|t| {:title => t.name,:id => t.id} } @filter_fields['vm_contract.start_end'] = ('
') filter_tags = params[:filters]["type"].to_a rescue [] @main_ids = nil @inviting_ids = nil if !filter_tags.blank? @inviting_ids = VenueManagementInviting.with_tags(filter_tags).pluck(:id) end if !filters("category").blank? @main_ids = VenueManagementMain.all.with_categories(filters("category")).pluck(:id) end contract_start_date = DateTime.parse(params[:filters]["contract_start_date"]) rescue 0 contract_end_date = DateTime.parse(params[:filters]["contract_end_date"]) rescue 0 if @contracts.nil? if contract_start_date != 0 && contract_end_date != 0 @contracts = VenueManagementContract.where(:contract_start_date.gte=>contract_start_date,:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) elsif contract_start_date != 0 @contracts = VenueManagementContract.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10) elsif contract_end_date != 0 @contracts = VenueManagementContract.where(:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) end else if contract_start_date != 0 && contract_end_date != 0 @contracts = @contracts.where(:contract_start_date.gte=>contract_start_date,:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) elsif contract_start_date != 0 @contracts = @contracts.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10) elsif contract_end_date != 0 @contracts = @contracts.where(:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) end end if !@main_ids.nil? if @contracts.nil? @contracts = VenueManagementContract.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10) else @contracts = @contracts.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10) end end if @contracts.nil? @contracts = VenueManagementContract.all.page(params[:page]).per(10) end if !@inviting_ids.nil? @contracts = @contracts.where(:venue_management_inviting_id.in=>@inviting_ids) end @contracts = @contracts.order(updated_at: :desc).page(params[:page]).per(10) @venue_management_page_url = Page.where(:module =>"venue_management").last.url rescue "" end def render_html(code,pass_variables={}) render_to_string(:inline=>code,:locals=>pass_variables) end def new @venue_management = VenueManagementMain.find(params[:id]) @venue_management_contract = @venue_management.venue_management_contracts.build end def create OrbitHelper.set_params(params,current_user) @venue_management_contract = VenueManagementContract.new(venue_management_contract_params) @venue_management_contract.save redirect_to params['referer_url'] end def edit @venue_management = VenueManagementMain.find(@venue_management_contract.venue_management_main_id) end def update OrbitHelper.set_params(params,current_user) @venue_management_contract.update_attributes!(venue_management_contract_params) redirect_to venue_management_contracts_admin_venue_management_path(@venue_management_contract.venue_management_main_id) end def destroy @venue_management_main_id = @venue_management_contract.venue_management_main_id @venue_management_contract.destroy redirect_to venue_management_contracts_admin_venue_management_path(@venue_management_main_id) end def export filename = "" filter_tags = params[:filters]["type"].to_a rescue [] @main_ids = nil @inviting_ids = nil if !filter_tags.blank? filename += (I18n.t("vm_bill.type") + ": #{Tag.where(:id.in=>filter_tags).map{|t| t.name}.join(',')}") @inviting_ids = VenueManagementInviting.with_tags(filter_tags).pluck(:id) end if !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 contract_start_date = DateTime.parse(params[:filters]["contract_start_date"]) rescue 0 contract_end_date = DateTime.parse(params[:filters]["contract_end_date"]) rescue 0 if @contracts.nil? if contract_start_date != 0 && contract_end_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~#{contract_end_date}") if filename.blank? @contracts = VenueManagementContract.where(:contract_start_date.gte=>contract_start_date,:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) elsif contract_start_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~") if filename.blank? @contracts = VenueManagementContract.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10) elsif contract_end_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": ~#{contract_end_date}") if filename.blank? @contracts = VenueManagementContract.where(:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) end else if contract_start_date != 0 && contract_end_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~#{contract_end_date}") if filename.blank? @contracts = @contracts.where(:contract_start_date.gte=>contract_start_date,:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) elsif contract_start_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~") if filename.blank? @contracts = @contracts.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10) elsif contract_end_date != 0 filename += (I18n.t("vm_bill.accounting_month") + ": ~#{contract_end_date}") if filename.blank? @contracts = @contracts.where(:contract_end_date.lte=>contract_end_date).page(params[:page]).per(10) end end if !@main_ids.nil? if @contracts.nil? @contracts = VenueManagementContract.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10) else @contracts = @contracts.where(:venue_management_main_id.in=>@main_ids).page(params[:page]).per(10) end end if @contracts.nil? @contracts = VenueManagementContract.all.page(params[:page]).per(10) end if !@inviting_ids.nil? @contracts = @contracts.where(:venue_management_inviting_id.in=>@inviting_ids) end if params[:id].present? filename = "#{VenueManagementMain.find(params[:id]).title rescue ''}_export_invitings" if filename.blank? @contracts = @contracts.and(:venue_management_main_id=>params[:id]) end @contracts = @contracts.order(updated_at: :desc).page(1).per(@contracts.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_contracts" if filename.blank? filename = 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="' + filename + '"' } end render :layout => false end private def set_venue_management_contract @venue_management_contract = VenueManagementContract.find(params[:id]) end def venue_management_contract_params contract_params = params.require(:venue_management_contract).permit! contract_params["contractor_ids"] = [] if contract_params["contractor_ids"].nil? return contract_params end end