Class: BlendSpreadsheetLoanGenerator::Formula

Inherits:
Object
  • Object
show all
Includes:
SpreadsheetConcern
Defined in:
lib/blend_spreadsheet_loan_generator/formula.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loan:) ⇒ Formula

Returns a new instance of Formula.



7
8
9
10
11
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 7

def initialize(loan:)
  @loan = loan
  @interests_formula = loan.interests_formula
  @loan_type_formula = loan.loan_type_formula
end

Instance Attribute Details

#loanObject

Returns the value of attribute loan.



5
6
7
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 5

def loan
  @loan
end

Instance Method Details

#accrued_delta_formula(line:) ⇒ Object



136
137
138
139
140
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 136

def accrued_delta_formula(line:)
  return excel_float(0.0) if line == 2

  "=#{accrued_delta(line - 1)} + #{delta(line)} - #{amount_to_add(line - 1)}"
end

#amount_to_add_formula(line:) ⇒ Object



142
143
144
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 142

def amount_to_add_formula(line:)
  "=TRONQUE(#{accrued_delta(line)}; 2)"
end

#capitalized_fees_end_formula(line:, first_term_skip: false) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 208

def capitalized_fees_end_formula(line:, first_term_skip: false)
  term = line - 1
  return excel_float(0.0) if first_term_skip && term == 1
  if term <= loan.deferred_and_capitalized
    index = first_term_skip ? index(line - 1) : index(line)
    with_fees = "#{remaining_capital_start(line)} * (1 + #{period_fees_rate(line)} + #{period_rate(line)})^#{index}"
    without_fees = "#{remaining_capital_start(line)} * (1 + #{period_rate(line)})^#{index}"
    "=ARRONDI(#{with_fees} - #{without_fees}; 2)"
  else
    "=ARRONDI(#{capitalized_fees_start(line)} - #{period_reimbursed_capitalized_fees(line)}; 2)"
  end
end

#capitalized_fees_start_formula(line:) ⇒ Object



202
203
204
205
206
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 202

def capitalized_fees_start_formula(line:)
  return excel_float(loan.starting_capitalized_fees) if line == 2

  "=ARRONDI(#{capitalized_fees_end(line - 1)}; 2)"
end

#capitalized_interests_end_formula(line:) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 110

def capitalized_interests_end_formula(line:)
  term = line - 1
  if term <= loan.deferred_and_capitalized
    "=ARRONDI(#{capitalized_interests_start(line)} + #{period_calculated_interests(line)}; 2)"
  else
    "=ARRONDI(#{capitalized_interests_start(line)} - #{period_reimbursed_capitalized_interests(line)}; 2)"
  end
end

#capitalized_interests_start_formula(line:) ⇒ Object



104
105
106
107
108
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 104

def capitalized_interests_start_formula(line:)
  return excel_float(loan.starting_capitalized_interests) if line == 2

  "=ARRONDI(#{capitalized_interests_end(line - 1)}; 2)"
end

#delta_formula(line:) ⇒ Object



132
133
134
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 132

def delta_formula(line:)
  "=#{period_theoric_interests(line)} - #{period_interests(line)}"
end

#due_on_formula(line:) ⇒ Object



17
18
19
20
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 17

def due_on_formula(line:)
  term = line - 1
  loan.due_on + ((term - 1) * loan.period_duration).months
end

#index_formula(line:) ⇒ Object



13
14
15
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 13

def index_formula(line:)
  line - 1
end

#period_calculated_capital_formula(line:) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 52

def period_calculated_capital_formula(line:)
  term = line - 1
  if term <= loan.total_deferred_duration
    excel_float(0.0)
  else
    @loan_type_formula.period_calculated_capital_formula(line: line)
  end
end

#period_calculated_fees_formula(line:, first_term_skip: false) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 171

def period_calculated_fees_formula(line:, first_term_skip: false)
  term = line - 1
  return excel_float(0.0) if first_term_skip && term == 1
  if loan.bullet_term?(term)
    index = first_term_skip ? index(line - 1) : index(line)

    with_fees = "#{remaining_capital_start(line)} * (1 + #{period_fees_rate(line)} + #{period_rate(line)})^#{index}"
    without_fees = "#{remaining_capital_start(line)} * (1 + #{period_rate(line)})^#{index}"
    current = "#{with_fees} - #{without_fees}"

    "=#{current} - #{capitalized_fees_start(line)}"
  elsif loan.deferred_and_capitalized > 0 && term > loan.deferred_and_capitalized
    index = first_term_skip ? loan.deferred_and_capitalized - 1 : loan.deferred_and_capitalized

    with_fees = "#{remaining_capital_start(line)} * (1 + #{period_rate(line)} + #{period_fees_rate(line)})^#{index} * (#{period_rate(line)} + #{period_fees_rate(line)})"
    without_fees = "#{remaining_capital_start(line)} * (1 + #{period_rate(line)})^#{index} * #{period_rate(line)}"

    "=#{with_fees} - #{without_fees}"
  else
    amount_to_capitalize = [
      '(',
      remaining_capital_start(line),
      '+',
      capitalized_interests_start(line),
      ')'
    ].join(' ')

    "=#{amount_to_capitalize} * #{period_fees_rate(line)}"
  end
end

#period_calculated_interests_formula(line:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 70

def period_calculated_interests_formula(line:)
  amount_to_capitalize = [
    '(',
    remaining_capital_start(line),
    '+',
    capitalized_interests_start(line),
    ')'
  ].join(' ')

  "=#{amount_to_capitalize} * #{period_rate(line)}"
end

#period_capital_formula(line:) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 22

def period_capital_formula(line:)
  if line == loan.duration + 1
    "=ARRONDI(#{excel_float(loan.amount)} - #{total_paid_capital_end_of_period(line - 1)}; 2)"
  else
    "=ARRONDI(#{period_calculated_capital(line)} - #{period_reimbursed_capitalized_interests(line)} - #{period_reimbursed_capitalized_fees(line)}; 2)"
  end
end

#period_fees_formula(line:) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 162

def period_fees_formula(line:)
  term = line - 1
  if term <= loan.deferred_and_capitalized
    excel_float(0.0)
  else
    "=ARRONDI(#{period_calculated_fees(line)}; 2)"
  end
end

#period_fees_rate_formula(line:) ⇒ Object



230
231
232
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 230

def period_fees_rate_formula(line:)
  @interests_formula.period_fees_rate_formula(line: line)
end

#period_interests_formula(line:) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 30

def period_interests_formula(line:)
  term = line - 1
  if term <= loan.deferred_and_capitalized
    excel_float(0.0)
  else
    "=ARRONDI(#{period_calculated_interests(line)}; 2)"
  end
end

#period_leap_days_formula(line:) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 146

def period_leap_days_formula(line:)
  term = line - 1
  from = loan.due_on + ((term - 2) * loan.period_duration).months
  to = loan.due_on + ((term - 1) * loan.period_duration).months

  (from...to).sum { |d| d.leap? ? 1 : 0 }
end

#period_non_leap_days_formula(line:) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 154

def period_non_leap_days_formula(line:)
  term = line - 1
  from = loan.due_on + ((term - 2) * loan.period_duration).months
  to = loan.due_on + ((term - 1) * loan.period_duration).months

  (from...to).sum { |d| d.leap? ? 0 : 1 }
end

#period_rate_formula(line:) ⇒ Object



128
129
130
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 128

def period_rate_formula(line:)
  @interests_formula.period_rate_formula(line: line)
end

#period_reimbursed_capitalized_fees_formula(line:) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 221

def period_reimbursed_capitalized_fees_formula(line:)
  term = line - 1
  if term <= loan.total_deferred_duration
    excel_float(0.0)
  else
    "=ARRONDI(MIN(#{period_calculated_capital(line)}; #{capitalized_fees_start(line)}); 2)"
  end
end

#period_reimbursed_capitalized_interests_formula(line:) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 119

def period_reimbursed_capitalized_interests_formula(line:)
  term = line - 1
  if term <= loan.total_deferred_duration
    excel_float(0.0)
  else
    "=ARRONDI(MIN(#{period_calculated_capital(line)} - #{period_reimbursed_capitalized_fees(line)}; #{capitalized_interests_start(line)}); 2)"
  end
end

#period_reimbursed_guaranteed_fees_formula(line:) ⇒ Object



238
239
240
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 238

def period_reimbursed_guaranteed_fees_formula(line:)
  excel_float(0.0)
end

#period_reimbursed_guaranteed_interests_formula(line:) ⇒ Object



234
235
236
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 234

def period_reimbursed_guaranteed_interests_formula(line:)
  excel_float(0.0)
end

#period_theoric_interests_formula(line:) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 61

def period_theoric_interests_formula(line:)
  term = line - 1
  if term <= loan.deferred_and_capitalized
    excel_float(0.0)
  else
    "=#{period_calculated_interests(line)}"
  end
end

#period_total_formula(line:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 39

def period_total_formula(line:)
  total = [
    period_capital(line),
    '+',
    period_interests(line),
    '+',
    period_reimbursed_capitalized_interests(line),
    '+',
    period_reimbursed_guaranteed_interests(line)
  ].join(' ')
  "=ARRONDI(#{total}; 2)"
end

#remaining_capital_end_formula(line:) ⇒ Object



88
89
90
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 88

def remaining_capital_end_formula(line:)
  "=#{excel_float(loan.amount)} - #{total_paid_capital_end_of_period(line)}"
end

#remaining_capital_start_formula(line:) ⇒ Object



82
83
84
85
86
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 82

def remaining_capital_start_formula(line:)
  return excel_float(loan.amount) if line == 2

  "=#{excel_float(loan.amount)} - #{total_paid_capital_end_of_period(line - 1)}"
end

#total_paid_capital_end_of_period_formula(line:) ⇒ Object



92
93
94
95
96
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 92

def total_paid_capital_end_of_period_formula(line:)
  return "=#{period_capital(line)}" if line == 2

  "=ARRONDI(SOMME(#{column_range(column: period_capital, upto: line)}); 2)"
end

#total_paid_interests_end_of_period_formula(line:) ⇒ Object



98
99
100
101
102
# File 'lib/blend_spreadsheet_loan_generator/formula.rb', line 98

def total_paid_interests_end_of_period_formula(line:)
  return "=#{period_interests(line)}" if line == 2

  "=ARRONDI(SOMME(#{column_range(column: period_interests, upto: line)}); 2)"
end