require 'orbit_form_helper' class VenueManagementBill include Mongoid::Document include Mongoid::Timestamps include Mongoid::Enum BILLTYPE = {'venue_fee'=>["usage_fee", "royalties", "admin_fee", "land_rent"],'utility_bill'=>['electricity','water'],'tax'=>['house_tax','land_tax'],'prejudgment_interest'=>["usage_fee", "royalties", "admin_fee", "land_rent",'electricity','water','house_tax','land_tax','other'],'other'=>['penalty','other']} CACULATIONBASISTYPE = {'revenue'=>{'revenue'=>{},'level_0'=>{'fields'=>['field_value','percent']},'level_1'=>{'fields'=>['field_value','percent']},'level_2'=>{'fields'=>['field_value','percent']},'level_3'=>{'fields'=>['field_value','percent']},'level_4'=>{'fields'=>['field_value','percent']},'level_5'=>{'fields'=>['field_value','percent']}}, 'degree'=>{'current_degree'=>{'float'=>'left'},'consumption'=>{'float'=>'left','clear'=>true,'no_margin'=>true},'level_0'=>{'fields'=>['field_value','dollar/degree']},'level_1'=>{'fields'=>['field_value','dollar/degree']},'level_2'=>{'fields'=>['field_value','dollar/degree']},'level_3'=>{'fields'=>['field_value','dollar/degree']},'add_and_subtract'=>{'unit'=>'add_and_subtract_hint'}}, 'actual_bill'=>{'bill_amount'=>{}}, 'fixed'=>{'amount'=>{}}, 'taxable_present_value'=>{'present_value'=>{},'taxable_area'=>{'unit'=>'㎡'},'taxable_tax'=>{}}, 'announced_land_values'=>{'land_values'=>{},'taxable_area'=>{'unit'=>'㎡'},'taxable_tax'=>{}}, 'other'=>{'amount'=>{}}} CACILATYIONFORMULA = {'revenue'=>" i = find_insert_index(field_values,revenue) field_values = field_values.slice(0,i) percents = percents.slice(0,i) field_values.push(revenue) percents.push(0) result = -inner_product(field_values,percents) f=field_values.shift(1) field_values.push(f[0]) result += inner_product(field_values,percents) result = result / 100 if(result < 0) result = 0", "degree"=>" i = find_insert_index(field_values,consumption) field_values = field_values.slice(0,i) dollardegrees = dollardegrees.slice(0,i) field_values.push(consumption) dollardegrees.push(0) result = -inner_product(field_values,dollardegrees) f=field_values.shift(1) field_values.push(f[0]) result += inner_product(field_values,dollardegrees) result += add_and_subtract if(result < 0) result = 0", "actual_bill"=>" result = bill_amount if(result < 0) result = 0", "fixed"=>" result = amount if(result < 0) result = 0", "taxable_present_value"=>" result = taxable_tax if(result < 0) result = 0", "announced_land_values"=>" result = taxable_tax if(result < 0) result = 0", "other"=>" result = amount if(result < 0) result = 0" } CACULATIONBASIS = {'revenue'=>"revenue", "degree"=>"consumption", "actual_bill" => "bill_amount", "fixed"=>"amount", "taxable_present_value"=>"present_value", "announced_land_values"=>"land_values", "other"=>"amount" } field :accounting_month, type: Date, default: Time.now field :bill_typeA, type: String, default: "" field :bill_typeB, type: String, default: "" field :bill_other, type: String, default: "", localize: true field :caculation_basis_type, type: String, default: "" field :caculation_basis, type: String, default: "" field :reason, type: String, default: "", localize: true field :amount, type: Integer, default: 0 field :deadline, type: Date, default: Time.now field :received_date, type: Date, default: Time.now field :pay_method, type: String, default: "", localize: true field :note, type: String, default: "", localize: true field :revenue field :bill_amount field :level_0 field :level_1 field :level_2 field :level_3 field :level_4 field :level_5 field :current_degree field :consumption field :tax_excluded, type: Boolean, default: false field :total_amount field :actual_amount field :tax field :present_value field :taxable_area field :land_values field :taxable_tax field :add_and_subtract belongs_to :venue_management_main include VenueLinkFile # after_initialize do |record| # if record.caculation_basis_type == "degree" # if record.caculation_basis != "" && record.consumption.blank? # record.update(:consumption => record.caculation_basis) # end # elsif record.caculation_basis_type == "revenue" # if record.caculation_basis != "" && record.revenue.blank? # record.update(:revenue => record.caculation_basis) # end # end # end # after_save do |record| # venue_management_main = record.venue_management_main # if !venue_management_main.venue_management_bill_ids.include?(record.id) # venue_management_main.update(venue_management_main.venue_management_bill_ids.push(record.id)) # end # end def display_caculation_basis caculation_basis_val = eval("#{self.class::CACULATIONBASIS[self.caculation_basis_type]}") return "#{I18n.t("vm_bill.#{self.class::CACULATIONBASIS[self.caculation_basis_type]}")}: #{caculation_basis_val}" end def display_accounting_month return (self.accounting_month.strftime("%Y-%m") rescue "") end def case_no return (self.contractor.case_no rescue "") end def contractor return self.venue_management_main.venue_management_contracts.where(:contract_start_date.lte=>self.accounting_month,:contract_end_date.gte=>self.accounting_month).first end def pay_date return "#{self.deadline}/#{self.received_date}" end def inner_product(a,b) result = 0 a.each.with_index{|v,i| result += v*b[i] if b[i].present?} return result end end