# 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'] = ('
'+
(render_html('<%= org_datetime_picker("filters[contract_start_date]", {:no_label => true, :format=>"yyyy/MM/dd", :new_record=>false, :value=>(DateTime.parse(params[:filters]["contract_start_date"]) rescue nil)}) %>')) +
"
~" +
(render_html('<%= org_datetime_picker("filters[contract_end_date]", {:no_label => true, :format=>"yyyy/MM/dd", :new_record=>false, :value=>(DateTime.parse(params[:filters]["contract_end_date"]) rescue nil)}) %>' ) ) +
render_html("<%= submit_tag(I18n.t('venue_management.search')) %>") +
'
')
filter_contract
@venue_management_page_url = Page.where(:module =>"venue_management").last.url rescue ""
if request.xhr?
render :partial => 'index'
end
end
def filter_contract
@ext_filename = ""
filter_tags = params[:filters]["type"].to_a rescue []
@main_ids = nil
@inviting_ids = nil
if !filter_tags.blank?
@ext_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?
@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
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
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~#{contract_end_date}") if @ext_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
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~") if @ext_filename.blank?
@contracts = VenueManagementContract.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10)
elsif contract_end_date != 0
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": ~#{contract_end_date}") if @ext_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
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~#{contract_end_date}") if @ext_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
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": #{contract_start_date}~") if @ext_filename.blank?
@contracts = @contracts.where(:contract_start_date.gte=>contract_start_date).page(params[:page]).per(10)
elsif contract_end_date != 0
@ext_filename += (I18n.t("vm_bill.accounting_month") + ": ~#{contract_end_date}") if @ext_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?
@ext_filename = "#{VenueManagementMain.find(params[:id]).title rescue ''}_export_invitings" if @ext_filename.blank?
@contracts = @contracts.and(:venue_management_main_id=>params[:id])
end
if params[:keywords].present?
@contracts = @contracts.page(nil).per(@contracts.count)
@main_ids = @contracts.pluck(:venue_management_main_id)
@venue_managements = VenueManagementMain.where(:id.in=>@main_ids)
@main_ids = search_data(@venue_managements,[:title]).pluck(:id)
ids = []
if params[:keywords] == I18n.t(:empty)
ids = @contracts.where(:case_no.in=>["",nil]).pluck(:id)
else
ids = search_data(@contracts,[:case_no]).pluck(:id)
end
ids2 = [] #ids for search contractors
if params[:keywords] == I18n.t(:empty)
ids2 = @contracts.map{|contract| ((contract.display_contractors.blank?) rescue true) ? contract.id : nil }
else
ids2 = @contracts.map{|contract| (contract.display_contractors.include?(params[:keywords]) rescue false) ? contract.id : nil }
end
ids1 = VenueManagementContract.where(:venue_management_main_id.in=>@main_ids).pluck(:id)
@contracts = VenueManagementContract.where(:id.in=>(ids + ids1 + ids2)).page(params[:page]).per(10)
#render :html => ids and return
end
@contracts = @contracts.order(updated_at: :desc).page(params[:page]).per(10)
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
filter_contract
@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
@ext_filename = "export_contracts" 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
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