-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoanCalc.ps1
288 lines (260 loc) · 10.3 KB
/
LoanCalc.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
87
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
120
121
122
123
124
125
126
127
128
129
130
131
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
167
168
169
170
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
function New-SWFRadialButton {
param (
$Text = "Insert-Name",
$LocationX = 0,
$LocationY = 0,
$Location = (New-Object System.Drawing.Point($LocationX, $LocationY)),
$AutoScale = $true
)
$RADIAL = New-Object System.Windows.Forms.RadioButton
$RADIAL.Text = $Text
$RADIAL.Location = $Location
$RADIAL.AutoSize = $AutoScale
return $RADIAL
}
function New-SWFButton {
param (
$Text = "Insert-Name",
$LocationX = 0,
$LocationY = 0,
$Location = (New-Object System.Drawing.Point($LocationX, $LocationY)),
$AutoScale = $true
)
$BUTTON = New-Object System.Windows.Forms.Button
$BUTTON.Text = $Text
$BUTTON.Location = $Location
$BUTTON.AutoSize = $AutoScale
return $BUTTON
}
function New-SWFTextbox {
param (
$Text = "default",
$LocationX = 0,
$LocationY = 0,
$Location = (New-Object System.Drawing.Point($LocationX, $LocationY)),
$SizeW = 100,
$SizeH = 20,
$Size = (New-Object System.Drawing.Size($SizeW, $SizeH)),
$AutoScale = $true
)
$BUTTON = New-Object System.Windows.Forms.TextBox
$BUTTON.Text = $Text
$BUTTON.Location = $Location
$BUTTON.Size = $Size
$BUTTON.AutoSize = $AutoScale
return $BUTTON
}
function New-SWFLabel {
param (
$Text = "default",
$LocationX = 0,
$LocationY = 0,
$Location = (New-Object System.Drawing.Point($LocationX, $LocationY)),
$AutoScale = $true
)
$LABEL = New-Object System.Windows.Forms.Label
$LABEL.Text = $Text
$LABEL.Location = $Location
$LABEL.AutoSize = $AutoScale
return $LABEL
}
function New-SWFGroupBox {
param (
$Text = "Insert-Name",
$LocationX = 0,
$LocationY = 0,
$Location = (New-Object System.Drawing.Point($LocationX, $LocationY)),
$SizeW = 0,
$SizeH = 0,
$Size = (New-Object System.Drawing.Size($SizeW, $SizeH)),
$AutoScale = $true
)
$BOX = New-Object System.Windows.Forms.GroupBox
$BOX.Text = $Text
$BOX.Location = $Location
$BOX.Size = $Size
$BOX.AutoSize = $AutoScale
return $BOX
}
function Get-SWFBufferLocation {
# Returns sided coords that account for a buffer
param (
$Target = $1,
$Side = "right",
$Buffer = 10
)
# get loc coords (Top-Left)
$ObjLocX = $Target.Location.x
$ObjLocY = $Target.Location.y
# Get size
$ObjSizeW = $Target.Size.Width
$ObjSizeH = $Target.Size.Height
# The side of the obj that we need the coords to.
switch ($Side.ToLower()) {
"right" {
return ($ObjLocX + $ObjSizeW + $Buffer)
}
"bottom" {
return ($ObjLocY + $ObjSizeH + $Buffer)
}
Default {}
}
}
#
# CONSTANTS
$WINDOWW = 280
$WINDOWH = 440
$OBJBUFFERX = 10
# Load necessary .NET assembly
Add-Type -AssemblyName System.Windows.Forms
# Enables modern design from .net
[System.Windows.Forms.Application]::EnableVisualStyles()
# Create a new form
$form = New-Object System.Windows.Forms.Form
$form.Text = "LoanCalc"
$form.Size = New-Object System.Drawing.Size($WINDOWW, $WINDOWH)
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.StartPosition = "CenterScreen"
$form.AutoSize = $true
$form.MaximizeBox = $false
#$form.TopMost = $true
$form.Icon = New-Object System.Drawing.Icon("$PSScriptRoot/.assets/icon/favicon.ico")
# (left, top, right, bottom)
$form.padding = New-Object System.Windows.Forms.Padding(0, 0, 0, 15)
# Enable high DPI
$form.AutoScaleDimensions = '96, 96'
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
#
# <Title>
$TitleLabel = New-SWFLabel -Text "Loan Amortization Calculator - by Jimurrito" -LocationX $OBJBUFFERX -LocationY 20; $form.Controls.Add($TitleLabel)
#
# <Input Area>
# Input for total loan amount
$TotalAmountGroupBox = New-SWFGroupBox -Text "Total ($):" -LocationX $OBJBUFFERX -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $TitleLabel)
$form.Controls.Add($TotalAmountGroupBox)
$TotalAmountInput = New-SWFTextbox -Text 1000 -LocationX ($OBJBUFFERX / 2) -LocationY 20; $TotalAmountGroupBox.Controls.Add($TotalAmountInput)
# Number of payments/months
$NumoPayGroupBox = New-SWFGroupBox -Text "# of payments:" -LocationX (Get-SWFBufferLocation -Target $TotalAmountGroupBox -Buffer 20) -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $TitleLabel)
$form.Controls.Add($NumoPayGroupBox)
$NumoPayInput = New-SWFTextbox -Text 72 -LocationX ($OBJBUFFERX / 2) -LocationY 20; $NumoPayGroupBox.Controls.Add($NumoPayInput)
# APR
$APRGroupBox = New-SWFGroupBox -Text "APR (%):" -LocationX 10 -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $TotalAmountGroupBox)
$form.Controls.Add($APRGroupBox)
$APRInput = New-SWFTextbox -Text 1.0 -LocationX ($OBJBUFFERX / 2) -LocationY 20; $APRGroupBox.Controls.Add($APRInput)
# Payment Amount
$PayAmountGroupBox = New-SWFGroupBox -Text "Payment ($):" -LocationX (Get-SWFBufferLocation -Target $APRGroupBox -Buffer 20) -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $NumoPayGroupBox)
$form.Controls.Add($PayAmountGroupBox)
$PayAmountInput = New-SWFTextbox -Text 100 -LocationX ($OBJBUFFERX / 2) -LocationY 20; $PayAmountGroupBox.Controls.Add($PayAmountInput)
#
# <Select Calc>
# Group Box
$SelectBox = New-SWFGroupBox -Text "Find:" -LocationX $OBJBUFFERX -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $APRGroupBox)
$form.Controls.Add($SelectBox)
$SelectBox.Size = New-Object System.Drawing.Size(($form.Size.Width - $OBJBUFFERX - 30), 0)
# radial buttons
$MonthlyPayRADIAL = New-SWFRadialButton -Text "Monthly Payment" -LocationX $OBJBUFFERX -LocationY 20
$SelectBox.Controls.Add($MonthlyPayRADIAL)
$MonthlyPayRADIAL.add_checkedchanged(
{
$TotalAmountInput.Enabled = $true
$NumoPayInput.Enabled = $true
$PayAmountInput.Enabled = $false
}
)
#
$TotalPayRADIAL = New-SWFRadialButton -Text "Total Payment" -LocationX (Get-SWFBufferLocation -Target $MonthlyPayRADIAL -Buffer 5) -LocationY 20
$SelectBox.Controls.Add($TotalPayRADIAL)
$TotalPayRADIAL.add_checkedchanged(
{
$TotalAmountInput.Enabled = $false
$NumoPayInput.Enabled = $true
$PayAmountInput.Enabled = $true
}
)
#
$NumoPayRADIAL = New-SWFRadialButton -Text "Number of Payments" -LocationX $OBJBUFFERX -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $MonthlyPayRADIAL -Buffer 5)
$SelectBox.Controls.Add($NumoPayRADIAL)
$NumoPayRADIAL.add_checkedchanged(
{
$TotalAmountInput.Enabled = $true
$NumoPayInput.Enabled = $false
$PayAmountInput.Enabled = $true
}
)
#
# <Toggle for schedule generator>
# Group Box
$SchedToggleBox = New-SWFGroupBox -Text "Generate Schedule:" -LocationX $OBJBUFFERX -LocationY (Get-SWFBufferLocation -Side "bottom" -Target $SelectBox)
$form.Controls.Add($SchedToggleBox)
$SchedToggleBox.Size = New-Object System.Drawing.Size(($form.Size.Width - $OBJBUFFERX - 30), 0)
# radial buttons
$ONRADIAL = New-SWFRadialButton -Text "On" -LocationX $OBJBUFFERX -LocationY 20
$SchedToggleBox.Controls.Add($ONRADIAL)
$OFFRADIAL = New-SWFRadialButton -Text "Off" -LocationX (Get-SWFBufferLocation -Target $ONRADIAL) -LocationY 20
$SchedToggleBox.Controls.Add($OFFRADIAL)
#
# <Calc button>
$CalcButton = New-SWFButton -Text "Calculate"
$CalcButton.Location = New-Object System.Drawing.Point((($form.Size.Width / 2) - ($CalcButton.Size.Width / 1.5)), (Get-SWFBufferLocation -Side "bottom" -Target $SchedToggleBox))
$form.Controls.Add($CalcButton)
#
# <Calc Function>
$CalcButton.add_click({
#
[float]$TotalAmount = $TotalAmountInput.Text
[int]$NumoPayments = $NumoPayInput.Text
$APR = ($APRInput.Text / 100)
$MonthlyInterest = $APR / 12
[float]$MonthlyPayment = $PayAmountInput.Text
# Clear result prior
if ($form.Controls.Count -gt 8) {
# Remove the last control
$form.Controls.RemoveAt(($form.Controls.Count - 1))
}
# If calc monthly payment
if ($MonthlyPayRADIAL.Checked) {
# get payment amoritzation
$MonthlyPayment = [Math]::Round(($MonthlyInterest * $TotalAmount) / (1 - ([Math]::Pow((1 + $MonthlyInterest), - $NumoPayments))), 2)
# Create Output box
$OutBox = New-SWFGroupBox -Text "Monthly Payment:"
$form.Controls.add($OutBox)
# Create Output label
$OutValue = New-SWFLabel -Text ('$' + $MonthlyPayment)
$OutBox.Controls.add($OutValue)
}
elseif ($TotalPayRADIAL.Checked) {
# get TotalAmpunt amoritzation
$TotalAmount = [Math]::Round((($MonthlyPayment * (1 - [Math]::Pow((1 + $MonthlyInterest), - $NumoPayments))) / $MonthlyInterest), 2)
# Create Output box
$OutBox = New-SWFGroupBox -Text "Total Amount:"
$form.Controls.add($OutBox)
# Create Output label
$OutValue = New-SWFLabel -Text ('$' + $TotalAmount)
$OutBox.Controls.add($OutValue)
}
elseif ($NumoPayRADIAL.Checked) {
# get number of payments amoritzation (Ceiling rounds up)
$NumoPayments = [Math]::Ceiling(( - ([Math]::Log((1 - (($MonthlyInterest * $TotalAmount) / $MonthlyPayment))) / [Math]::Log(1 + $MonthlyInterest))))
# Create Output box
$OutBox = New-SWFGroupBox -Text "# of payments:"
$form.Controls.add($OutBox)
# Create Output label
$OutValue = New-SWFLabel -Text $NumoPayments
$OutBox.Controls.add($OutValue)
}
# Update and project Output
$OutBox.size = New-Object System.Drawing.Size(120, ($OutValue.size.Height + ($OBJBUFFERX)))
$OutBox.Location = New-Object System.Drawing.Point((($form.Size.Width / 2) - ($OutBox.Size.Width / 1.7)), (Get-SWFBufferLocation -Side "bottom" -Target $CalcButton -Buffer 15))
$OutValue.Location = New-Object System.Drawing.Point((($Outbox.Size.Width / 2) - ($OutValue.Size.Width / 1.7)), ($OBJBUFFERX * 2.25))
#
# If Generate schedule is toggled, do the thing
if ($ONRADIAL.Checked) {
./LoanCalc_sched.ps1 -TotalAmount $TotalAmount -NumoPayments $NumoPayments -MonthlyInterest $MonthlyInterest -MonthlyPayment $MonthlyPayment
}
})
#
# <Pre-load>
$MonthlyPayRADIAL.Checked = $true
$OFFRADIAL.Checked = $true
# Show the form
$Null = $form.ShowDialog()