Class: BlendSpreadsheetLoanGenerator::Restructure

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
CsvConcern, SpreadsheetConcern
Defined in:
lib/blend_spreadsheet_loan_generator/restructure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loanObject

Returns the value of attribute loan.



6
7
8
# File 'lib/blend_spreadsheet_loan_generator/restructure.rb', line 6

def loan
  @loan
end

Instance Method Details

#call(last_paid_term:, from_path:, duration:, rate:, **options) ⇒ Object



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
57
58
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
# File 'lib/blend_spreadsheet_loan_generator/restructure.rb', line 26

def call(last_paid_term:, from_path:, duration:, rate:, **options)
  begin
    session = GoogleDrive::Session.from_config(
      File.join(ENV['SPREADSHEET_LOAN_GENERATOR_DIR'], 'config.json')
    )
  rescue StandardError => e
    if ENV['SPREADSHEET_LOAN_GENERATOR_DIR'].blank?
      puts 'please set SPREADSHEET_LOAN_GENERATOR_DIR'
    else
      puts 'Cannot connect to google drive. Did you run slg init CLIENT_ID CLIENT_SECRET ?'
    end
    return
  end

  f = CSV.open(from_path)
  values = f.to_a
  values.map! { |r| set_types([columns, r].transpose.to_h.with_indifferent_access) }

  last_paid_line = values.index { |term| term[:index] == last_paid_term.to_i }

  starting_capitalized_interests = (
    values[last_paid_line + 1][:capitalized_interests_start] +
    values[last_paid_line + 1][:period_calculated_interests]
  )
  starting_capitalized_fees = (
    values[last_paid_line + 1][:capitalized_fees_start] +
    values[last_paid_line + 1][:period_calculated_fees]
  )

  due_on = values[last_paid_line + 1][:due_on] + 1.month

  @loan = Loan.new(
    amount: values[last_paid_line][:remaining_capital_end],
    duration: duration,
    rate: rate,
    fees_rate: options.fetch(:fees_rate),
    period_duration: options.fetch(:period_duration),
    due_on: due_on,
    deferred_and_capitalized: options.fetch(:deferred_and_capitalized),
    deferred: options.fetch(:deferred),
    type: options.fetch(:type),
    interests_type: options.fetch(:interests_type),
    starting_capitalized_interests: starting_capitalized_interests,
    starting_capitalized_fees: starting_capitalized_fees
  )

  spreadsheet = session.create_spreadsheet(loan.name)
  worksheet = spreadsheet.add_worksheet(loan.type, loan.duration + 2, columns.count + 1, index: 0)

  @formula = Formula.new(loan: loan)

  apply_formulas(worksheet: worksheet)
  apply_formats(worksheet: worksheet)

  worksheet.save
  worksheet.reload

  generate_csv(worksheet: worksheet, target_path: options.fetch(:target_path))

  puts worksheet.human_url
end

#set_types(h) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/blend_spreadsheet_loan_generator/restructure.rb', line 88

def set_types(h)
  h[:index] = h[:index].to_i

  h[:due_on] = Date.strptime(h[:due_on], '%m/%d/%Y')
  h[:remaining_capital_start] = h[:remaining_capital_start].to_f
  h[:remaining_capital_end] = h[:remaining_capital_end].to_f
  h[:period_theoric_interests] = h[:period_theoric_interests].to_f
  h[:delta] = h[:delta].to_f
  h[:accrued_delta] = h[:accrued_delta].to_f
  h[:amount_to_add] = h[:amount_to_add].to_f
  h[:period_interests] = h[:period_interests].to_f
  h[:period_capital] = h[:period_capital].to_f
  h[:total_paid_capital_end_of_period] = h[:total_paid_capital_end_of_period].to_f
  h[:total_paid_interests_end_of_period] = h[:total_paid_interests_end_of_period].to_f
  h[:period_total] = h[:period_total].to_f
  h[:capitalized_interests_start] = h[:capitalized_interests_start].to_f
  h[:capitalized_interests_end] = h[:capitalized_interests_end].to_f
  h[:period_rate] = h[:period_rate].to_f
  h[:period_calculated_capital] = h[:period_calculated_capital].to_f
  h[:period_calculated_interests] = h[:period_calculated_interests].to_f
  h[:period_reimbursed_capitalized_interests] = h[:period_reimbursed_capitalized_interests].to_f
  h[:period_leap_days] = h[:period_leap_days].to_i
  h[:period_non_leap_days] = h[:period_non_leap_days].to_i
  h[:period_fees] = h[:period_fees].to_f
  h[:period_calculated_fees] = h[:period_calculated_fees].to_f
  h[:capitalized_fees_start] = h[:capitalized_fees_start].to_f
  h[:capitalized_fees_end] = h[:capitalized_fees_end].to_f
  h[:period_reimbursed_capitalized_fees] = h[:period_reimbursed_capitalized_fees].to_f
  h[:period_fees_rate] = h[:period_fees_rate].to_f

  h
end