Class: BookingTemplate

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/models/booking_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_booking(code, params = {}) ⇒ Booking

Build booking for template

Raises an exception if template for given [code] cannot be found.

Parameters:

  • code (String)

    to lookup template

  • params (Hash) (defaults to: {})

    parameters to set on the Booking

Returns:



118
119
120
121
122
123
# File 'app/models/booking_template.rb', line 118

def self.build_booking(code, params = {})
  template = find_by_code(code)
  fail "BookingTemplate not found for '#{code}'" unless template

  template.build_booking params
end

.create_booking(code, params = {}) ⇒ Object



125
126
127
# File 'app/models/booking_template.rb', line 125

def self.create_booking(code, params = {})
  find_by_code(code).try(:create_booking, params)
end

.import(struct) ⇒ Object



172
173
174
175
176
177
178
179
# File 'app/models/booking_template.rb', line 172

def self.import(struct)
  templates = all.inject([]) do |found, template|
    puts 'matcher: ' + template.matcher
    puts 'text: ' + struct.text
    found << template unless Regexp.new(template.matcher).match(struct.text).eql? nil
  end
  puts templates.inspect
end

Instance Method Details

#amount_to_sObject



42
43
44
45
46
47
48
# File 'app/models/booking_template.rb', line 42

def amount_to_s
  if amount_relates_to.present?
    return '%.2f%%' % (amount.to_f * 100)
  else
    return currency_fmt(amount)
  end
end

#booking_parameters(params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/booking_template.rb', line 59

def booking_parameters(params = {})
  params = HashWithIndifferentAccess.new(params)

  # Prepare parameters set by template
  booking_params = attributes.reject { |key, _value| !%w(title comments credit_account_id debit_account_id).include?(key) }

  # Calculate amount
  booking_amount = BigDecimal.new(amount.to_s || '0')

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

  person_id = params.delete(:person_id)

  if reference
    # Calculate amount
    booking_amount = amount(reference.value_date, person_id: person_id) if person_id

    case amount_relates_to
      when 'reference_amount'
        booking_amount *= reference.amount unless reference.amount.nil?
      when 'reference_balance'
        booking_amount *= reference.balance unless reference.balance.nil?
      when 'reference_amount_minus_balance'
        booking_amount *= reference.amount - reference.balance unless reference.amount.nil? || reference.balance.nil?
    end
  end

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

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

#build_booking(params = {}) ⇒ Object

Factory methods



103
104
105
# File 'app/models/booking_template.rb', line 103

def build_booking(params = {})
  Booking.new(booking_parameters(params))
end

#build_line_itemObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/booking_template.rb', line 132

def build_line_item
  if amount.match(/%/) || amount_relates_to.blank?
    line_item_class = LineItem
  else
    line_item_class = SaldoLineItem
  end

  line_item = line_item_class.new(
    booking_template: self,
    title: title,
    code: code,
    credit_account: ,
    debit_account: ,
    position: position,
    include_in_saldo_list: include_in_saldo_list,
    reference_code: amount_relates_to
  )

  if amount.match(/%/)
    line_item.quantity = '%'
    line_item.times    = amount.delete('%')
    # TODO: hack
    line_item.price    = line_item.price
  elsif amount_relates_to.present?
    line_item.quantity = 'saldo_of'
    # TODO: hack
    line_item.price    = line_item.price
  else
    line_item.quantity = 'x'
    line_item.times    = 1
    line_item.price    = amount
  end

  line_item
end

#create_booking(params = {}) ⇒ Object



107
108
109
# File 'app/models/booking_template.rb', line 107

def create_booking(params = {})
  Booking.create(booking_parameters(params))
end

#to_s(format = :default) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/booking_template.rb', line 21

def to_s(format = :default)
  case format
  when :short
    '%s / %s %s' % [
       ? .to_s(:short) : '?',
       ? .to_s(:short) : '?',
      amount ? '%0.2f' % amount.to_f : '?'
    ]
  when :long
    '%s an %s %s, %s (%s)' % [
       ? .to_s : '?',
       ? .to_s : '?',
      amount ? '%0.2f' % amount.to_f : '?',
      title.present? ? title : '?',
      comments.present? ? comments : '?'
    ]
  else
    title
  end
end