Class: ChargeBookingTemplate

Inherits:
BookingTemplate show all
Defined in:
app/models/charge_booking_template.rb

Instance Method Summary collapse

Methods inherited from BookingTemplate

build_booking, #build_booking, #create_booking, create_booking, import, #to_s

Instance Method Details

#amount(date = nil, params = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'app/models/charge_booking_template.rb', line 7

def amount(date = nil, params = {})
  rate = charge_rate(date, params)
  return 0.0 unless rate

  if self.amount_relates_to.present?
    return rate.rate / 100
  else
    return rate.rate
  end
end

#booking_parameters(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/charge_booking_template.rb', line 18

def booking_parameters(params = {})
  person_id = params.delete(:person_id)

  # Prepare parameters set by template
  booking_params = attributes.reject!{|key, value| !["title", "comments", "credit_account_id", "debit_account_id"].include?(key)}

  params.stringify_keys!

  # Lookup reference
  reference = params['reference']
  unless reference
    ref_type = params['reference_type']
    ref_id = params['reference_id']
    if ref_type.present? and ref_id.present?
      reference = ref_type.constantize.find(ref_id)
    end
  end

  if reference
    # Calculate amount
    booking_params['value_date'] = reference.value_date
    booking_amount = self.amount(reference.value_date, :person_id => person_id)

    case self.amount_relates_to
      when 'reference_amount'
        booking_amount *= reference.amount
      when 'reference_balance'
        booking_amount *= reference.balance
      when 'reference_amount_minus_balance'
        booking_amount *= reference.amount - reference.balance
    end
  end

  booking_amount = booking_amount.try(:round, 2)
  booking_params['amount'] = booking_amount

  # Override by passed in parameters
  booking_params.merge!(params)
end

#charge_rate(date = nil, params = {}) ⇒ Object

Charge Rates



3
4
5
# File 'app/models/charge_booking_template.rb', line 3

def charge_rate(date = nil, params = {})
  ChargeRate.by_person(params[:person_id]).current(charge_rate_code, date)
end