Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
 
 
 
 
 
 

225 lines
5.4 KiB

  1. <template>
  2. <div id="new" class="page">
  3. <h2>New Loan</h2>
  4. <section class="loans-list">
  5. <h3 v-for="(l, indx) in loans"
  6. :class="sel == indx ? 'sel' : ''"
  7. @click="() => sel = indx"
  8. >
  9. {{l.title}}
  10. </h3>
  11. <div class="add">
  12. <svg @click="create"
  13. xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
  14. class="bi bi-plus" viewBox="0 0 16 16"> <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0
  15. 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/> </svg>
  16. </div>
  17. </section>
  18. <loan-details v-if="hash == '#new'"
  19. :estimate="estimate"
  20. :loans="estimate.loans"
  21. :sel="sel"
  22. :loan="loan"
  23. @update:name="(name) => loans[sel].title = name"
  24. @del="del"
  25. @update:borrowers="(b) => estimate.borrowers = b"
  26. @update:creditScore="(c) => estimate.creditScore = c"
  27. @update:mIncome="(m) => estimate.mIncome = m"
  28. @update:transaction="(t) => estimate.transaction = t"
  29. @update:price="setPrice"
  30. @update:property="(p) => estimate.property = p"
  31. @update:loanType="(lt) => loans[sel].type = lt"
  32. @update:term="(lt) => loans[sel].term = lt"
  33. @update:program="(p) => loans[sel].program = p"
  34. @update:ltv="setLtv"
  35. @update:amount="setAmount"
  36. @update:housingDti="setHousingDti"
  37. @update:dti="setDti"
  38. @update:hoa="(hoa) => loans[sel].hoa = hoa"
  39. @update:interest="(i) => loans[sel].interest = i"
  40. @update:interestDays="(d) => loans[sel].interestDays = d"
  41. @update:hazardEscrow="(h) => loans[sel].hazardEscrow = h"
  42. @update:hazard="(h) => loans[sel].hazard = h"
  43. @update:taxEscrow="(t) => loans[sel].taxEscrow = t"
  44. @update:tax="(t) => loans[sel].tax = t"
  45. @update:manualMI="perc => loans[sel].mi.rate = perc"
  46. @toggle:manualMIMonthly=
  47. "() => loans[sel].mi.monthly = !loans[sel].mi.monthly"
  48. @continue="generate"
  49. />
  50. <loan-summary v-if="hash == '#new/summary'"
  51. :loan="loan" :downpayment="estimate.price - loan.amount"/>
  52. </div>
  53. </template>
  54. <script>
  55. import { stripLetters, strip, stripInt, stripPerc } from "../../helpers.js"
  56. import LoanDetails from "./details.vue"
  57. import LoanSummary from "./summary.vue"
  58. // The default values of a new estimate
  59. const example = {
  60. title: "Example",
  61. type: "",
  62. term: 0,
  63. ltv: 0, // Loan to home value ratio
  64. dti: 0,
  65. housingDti: 0,
  66. amount: 0,
  67. interest: 0,
  68. interestDays: 0,
  69. hazard: 0, // Hazard insurance monthly payment
  70. hazardEscrow: 0, // Hazard insurance escrow in months (0 is none)
  71. tax: 0, // Real Estate taxes monthly payment
  72. taxEscrow: 0, // Months to escrow (0 is none)
  73. hoa: 100, // Home owner's association monthly fee
  74. program: "",
  75. pud: true, // Property under development
  76. zip: '',
  77. fees: [],
  78. mi: {monthly: false, rate: 0}
  79. }
  80. // The default loans on a new estimate
  81. const loans = [
  82. Object.assign({}, example,),
  83. Object.assign(
  84. Object.assign({}, example),
  85. {title: "Another One", mi: {}}
  86. ),
  87. ]
  88. // Default estimate fields
  89. const estimate = {
  90. property: "",
  91. transaction: 0,
  92. price: 0,
  93. borrowers: 0,
  94. creditScore: 0,
  95. mIncome: 0,
  96. loans: loans,
  97. }
  98. function loan() {
  99. return this.estimate.loans[this.sel]
  100. }
  101. // Clone loan from initial example as a new loan
  102. function create() {
  103. this.estimate.loans.push(
  104. Object.assign({}, example, {fees: this.createFees()})
  105. )
  106. }
  107. function createFees() {
  108. return this.fees.map(f => Object.assign({}, f))
  109. }
  110. function del() {
  111. if (this.loans.length > 1) {
  112. let x = this.sel
  113. this.sel = 0
  114. this.loans.splice(x, 1)
  115. }
  116. }
  117. // Updates the property price for all loans and their fee amounts.
  118. function setPrice(value) {
  119. this.estimate.price = value
  120. this.estimate.loans[this.sel].fees.forEach(fee => {
  121. if (fee.perc) fee.amount = (fee.perc / 100 * value).toFixed(2)
  122. })
  123. this.estimate.loans.forEach(l => {l.ltv = 0; l.amount = 0})
  124. }
  125. // Changes loan.ltv's <input> and data() values, then syncs with data.amount
  126. function setLtv(e) {
  127. let ltv = strip(e)
  128. let loan = this.loans[this.sel]
  129. if (!this.estimate.price) return
  130. if (ltv > 100) ltv = 100
  131. if (ltv < 0) ltv = 0
  132. loan.ltv = ltv
  133. loan.amount = (ltv / 100 * this.estimate.price).toFixed(2)
  134. }
  135. // Changes loan.amount\'s <input> and data() values, then syncs with data.ltv
  136. function setAmount(e) {
  137. let amount = strip(e)
  138. let loan = this.loans[this.sel]
  139. if (!this.estimate.price) return
  140. if (amount > loan.price) amount = loan.price
  141. if (amount < 0) amount = 0
  142. loan.amount = amount
  143. loan.ltv = (amount / this.estimate.price * 100).toFixed(2)
  144. }
  145. function setDti(e) {
  146. let dti = strip(e)
  147. let loan = this.loans[this.sel]
  148. if (!loan.price) return
  149. if (dti > 100) dti = 100
  150. if (dti < 0) dti = 0
  151. e.target.value = dti
  152. loan.dti = dti
  153. }
  154. function setHousingDti(e) {
  155. let housingDti = strip(e)
  156. let loan = this.loans[this.sel]
  157. if (!loan.price) return
  158. if (housingDti > 100) housingDti = 100
  159. if (housingDti < 0) housingDti = 0
  160. e.target.value = housingDti
  161. loan.housingDti = housingDti
  162. }
  163. function generate() {
  164. window.location.hash = 'new/summary'
  165. }
  166. // Percentage values of fees always takek precedent over amounts. The conversion
  167. // happens in setPrice()
  168. export default {
  169. components: { LoanSummary, LoanDetails },
  170. methods: {
  171. generate, createFees, del, create, setPrice, setLtv, setAmount,
  172. setDti, setHousingDti
  173. },
  174. computed: { loan },
  175. props: ['user', 'fees'],
  176. data() {
  177. return {
  178. estimate: estimate,
  179. loans: estimate.loans,
  180. sel: 0,
  181. errors: [],
  182. hash: window.location.hash
  183. }
  184. },
  185. created() {
  186. this.estimate.loans.forEach(l => l.fees = this.createFees())
  187. window.addEventListener("hashchange", () => this.hash = window.location.hash)
  188. }
  189. }
  190. </script>