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

461 line
11 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. <section class="form inputs">
  19. <h3>Loan</h3>
  20. <label>Name</label>
  21. <input :value="loans[sel].title" required
  22. @input="(e) => loans[sel].title = stripLetters(e)">
  23. <button @click="del">Delete</button>
  24. </section>
  25. <section class="form inputs">
  26. <div class="hint">
  27. <img class="icon" src="/assets/image/icon/question-circle.svg" alt="">
  28. <div class="tooltip">
  29. <p>Assumes borrower is not self employed, not bankrupt in the past 7
  30. years, a citizen, and intends to occupy the property.</p>
  31. </div>
  32. </div>
  33. <h3>Borrower</h3>
  34. <label>Number of Borrowers</label>
  35. <input :value="estimate.borrowers"
  36. @input="(e) => estimate.borrowers = stripInt(e)">
  37. <label>Credit Score</label>
  38. <input :value="estimate.creditScore"
  39. @input="(e) => estimate.creditScore = stripInt(e)">
  40. <label>Monthly Income ($)</label>
  41. <input :value="estimate.mIncome"
  42. @input="(e) => estimate.mIncome = strip(e)">
  43. </section>
  44. <section class="radios form">
  45. <h3>Transaction Type</h3>
  46. <input selected type="radio" name="transaction_type" value="0"
  47. v-model="estimate.transaction" >
  48. <label>Purchase</label>
  49. <input type="radio" name="transaction_type" value="1"
  50. v-model="estimate.transaction">
  51. <label>Refinance</label>
  52. </section>
  53. <section class="form inputs">
  54. <h3>Property Details</h3>
  55. <label>Price ($)</label>
  56. <input :value="estimate.price" @input="setPrice">
  57. <label>Type</label>
  58. <select id="" name="" v-model="estimate.property">
  59. <option value="attched">Single Family Attached</option>
  60. <option value="detached">Single Family Detached</option>
  61. <option value="lorise">Lo-rise (4 stories or less)</option>
  62. <option value="hirise">Hi-rise (over 4 stories)</option>
  63. </select>
  64. </section>
  65. <section class="radios form">
  66. <h3>Loan Type</h3>
  67. <input type="radio" name="loan_type" value="conv" v-model="loans[sel].type"
  68. >
  69. <label>Conventional</label>
  70. <input type="radio" name="loan_type" value="fha" v-model="loans[sel].type">
  71. <label>FHA</label>
  72. <input type="radio" name="loan_type" value="va" v-model="loans[sel].type">
  73. <label>VA</label>
  74. <input type="radio" name="loan_type" value="usda" v-model="loans[sel].type">
  75. <label>USDA</label>
  76. </section>
  77. <section class="form inputs">
  78. <h3>Loan Details</h3>
  79. <label>Loan Term (years)</label>
  80. <input :value="loans[sel].term"
  81. @input="(e) => loans[sel].term = strip(e)">
  82. <label>Loan Program</label>
  83. <select id="" name="" v-model="loans[sel].program">
  84. <option value="none">None</option>
  85. </select>
  86. <label>Loan to Value (%)</label>
  87. <input :value="loans[sel].ltv" @input="setLtv">
  88. <label>Loan Amount ($)</label>
  89. <input :value="loans[sel].amount"
  90. @input="setAmount">
  91. <label>Housing Expense DTI (%) - Optional</label>
  92. <input :value="loans[sel].housingDti" @input="setHousingDti">
  93. <label>Total DTI (%) - Optional</label>
  94. <input :value="loans[sel].dti" @input="setDti">
  95. <label>Home Owner's Association ($/month)</label>
  96. <input :value="loans[sel].hoa"
  97. @input="(e) => { loans[sel].hoa = strip(e) }">
  98. <label>Interest Rate (%)</label>
  99. <input :value="loans[sel].interest"
  100. @input="(e) => { loans[sel].interest = stripPerc(e) }">
  101. <label>Days of Interest</label>
  102. <input :value="loans[sel].interestDays"
  103. @input="(e) => {loans[sel].interestDays = stripInt(e)}">
  104. <label>Hazard Insurance Escrow (months)</label>
  105. <input :value="loans[sel].hazardEscrow"
  106. @input="(e) => {loans[sel].hazardEscrow = stripInt(e)}">
  107. <label>Hazard Insurance ($/month)</label>
  108. <input :value="loans[sel].hazard"
  109. @input="(e) => {loans[sel].hazard = strip(e)}">
  110. <label>Real Estate Tax Escrow (months)</label>
  111. <input :value="loans[sel].taxEscrow"
  112. @input="e => {loans[sel].taxEscrow = stripInt(e)}">
  113. <label>Real Estate Tax ($/month)</label>
  114. <input :value="loans[sel].tax"
  115. @input="(e) => {loans[sel].tax = strip(e)}">
  116. </section>
  117. <section class="form inputs">
  118. <h3>Fees</h3>
  119. <div v-for="(fee, indx) in estimate.loans[sel].fees"
  120. :key="fee.name + indx" class="fee"
  121. >
  122. <label>
  123. ${{fee.amount}} - {{fee.name}}<br>
  124. {{fee.type}}
  125. </label>
  126. <img width="21" height="21" src="/assets/image/icon/x-red.svg"
  127. @click="() => estimate.loans[sel].fees.splice(indx, 1)">
  128. </div>
  129. <button @click="resetFees">Reset</button>
  130. <button @click="createFee">New</button>
  131. </section>
  132. <Dialog v-if="newFee" @close="() => newFee = null">
  133. <h3>New Fee</h3>
  134. <label>Name</label>
  135. <input type=""
  136. :value="newFee.name"
  137. @input="(e) => newFee.name = stripLetters(e)">
  138. <label>Amount</label>
  139. <input
  140. type=""
  141. :value="newFee.amount"
  142. @input="(e) => newFee.amount = strip(e)">
  143. <select id="" name="" v-model="newFee.type">
  144. <option value="title">Title Company</option>
  145. <option value="gov">Government</option>
  146. <option value="lender">Lender</option>
  147. <option value="lender_required">Required by Lender</option>
  148. <option value="other">Other</option>
  149. </select>
  150. <button :disabled="!validFee" @click="addFee(true)">Debit</button>
  151. <button :disabled="!validFee" @click="addFee(false)">Credit</button>
  152. </Dialog>
  153. <section class="form radios">
  154. <h3>Mortgage Insurance</h3>
  155. <input type="radio" name="transaction_type" value="transaction"
  156. @input="e => estimate.transaction = 0"
  157. selected="estimate.transaction == 0">
  158. <label>1.43% - National MI</label>
  159. <input type="radio" name="transaction_type" value="refinance"
  160. @input="e => estimate.transaction = 1"
  161. selected="estimate.transaction == 1">
  162. <label>0.73% - MGIC</label>
  163. </section>
  164. <section class="form inputs">
  165. <button @click="generate">Generate</button>
  166. <div class="errors">
  167. <span v-for="e in errors">{{e}}</span>
  168. </div>
  169. </section>
  170. </div>
  171. </template>
  172. <script>
  173. import Dialog from "./dialog.vue"
  174. // The default fees of a new loan
  175. const fees = [
  176. { name: 'Processing fee', type: 'Lender Fees', amount: 500 },
  177. { name: 'Underwriting fee', type: 'Lender Fees', amount: 500 },
  178. { name: 'Credit Report', type: 'Services Required by Lender',
  179. amount: 52.50 },
  180. { name: 'Appraisal', type: 'Services Required by Lender', amount: 52.50 },
  181. { name: 'Title Services', type: 'Title Company', amount: 1000 },
  182. { name: 'Lender\'s Title Insurance', type: 'Title Company', amount: 1599 },
  183. { name: 'Owner\'s Title Insurance', type: 'Title Company', amount: 451.00 },
  184. { name: 'Recording Charges', type: 'Government', amount: 99.00 },
  185. { name: 'State Tax', type: 'Government', amount: 2411.00 },
  186. ]
  187. // The default values of a new estimate
  188. const example = {
  189. title: "Example",
  190. type: "",
  191. term: 0,
  192. ltv: 0, // Loan to home value ratio
  193. dti: 0,
  194. housingDti: 0,
  195. amount: 0,
  196. interest: 0,
  197. interestDays: 0,
  198. hazard: 0, // Hazard insurance monthly payment
  199. hazardEscrow: 0, // Hazard insurance escrow in months (0 is none)
  200. tax: 0, // Real Estate taxes monthly payment
  201. taxEscrow: 0, // Months to escrow (0 is none)
  202. hoa: 100, // Home owner's association monthly fee
  203. program: "",
  204. pud: true, // Property under development
  205. zip: '',
  206. fees: fees,
  207. }
  208. // The default loans on a new estimate
  209. const loans = [
  210. Object.assign({}, example, {fees: createFees()}),
  211. Object.assign(
  212. Object.assign({}, example),
  213. {title: "Another One", fees: createFees()}
  214. ),
  215. ]
  216. // Default estimate fields
  217. const estimate = {
  218. property: "",
  219. transaction: 0,
  220. price: 0,
  221. borrowers: 0,
  222. creditScore: 0,
  223. mIncome: 0,
  224. loans: loans,
  225. }
  226. const newFee = {
  227. name: '', type: '', amount: 0
  228. }
  229. // Clone loan from initial example as a new loan
  230. function create() {
  231. this.estimate.loans.push(
  232. Object.assign({}, example, {fees: createFees()})
  233. )
  234. }
  235. function createFees() {
  236. return fees.map(f => Object.assign({}, f))
  237. }
  238. // Setup this.newFee for the creation dialog
  239. function createFee() {
  240. this.newFee = Object.assign({}, newFee)
  241. }
  242. function resetFees() {
  243. this.estimate.loans[this.sel].fees = createFees()
  244. }
  245. // If valid, add the current this.newFee to the array of fees and reset
  246. // this.newFee to null
  247. function addFee(isDebit) {
  248. const fee = this.newFee
  249. if (!this.validFee) {
  250. return
  251. }
  252. if (!isDebit) fee.amount = fee.amount * -1
  253. this.estimate.loans[this.sel].fees.push(fee)
  254. this.newFee = null
  255. }
  256. function validFee() {
  257. const fee = this.newFee
  258. if (!fee.name || !fee.type || !fee.amount) {
  259. return false
  260. }
  261. return true
  262. }
  263. // Strips non-digits from an input box event and returns it's rounded integer.
  264. // It also preserves current valid entry (.)
  265. function strip(e) {
  266. let valid = e.target.value.match(/\d+\.?\d?\d?/)?.[0] ?? ""
  267. e.target.value = valid
  268. return Number(valid || 0)
  269. }
  270. function stripInt(e) {
  271. let value = parseInt(e.target.value.replace(/\D/g, '') || 0)
  272. e.target.value = value
  273. return value
  274. }
  275. function stripPerc(e) {
  276. let num = strip(e)
  277. if (num > 100) {
  278. num = 100
  279. e.target.value = num
  280. }
  281. if (num < 0) {
  282. num = 0
  283. e.target.value = num
  284. }
  285. return num
  286. }
  287. function stripLetters(e) {
  288. let value = (e.target.value.replace(/[^\w\s]/g, '').slice(0, 20) || '')
  289. e.target.value = value
  290. return value
  291. }
  292. function del() {
  293. if (this.loans.length > 1) {
  294. let x = this.sel
  295. this.sel = 0
  296. this.loans.splice(x, 1)
  297. }
  298. }
  299. // Changes loan.ltv's <input> and data() values, then syncs with data.amount
  300. function setLtv(e) {
  301. let ltv = strip(e)
  302. let loan = this.loans[this.sel]
  303. if (!this.estimate.price) return
  304. if (ltv > 100) ltv = 100
  305. if (ltv < 0) ltv = 0
  306. loan.ltv = ltv
  307. loan.amount = (ltv / 100 * this.estimate.price).toFixed(2)
  308. }
  309. // Changes loan.amount's <input> and data() values, then syncs with data.ltv
  310. function setAmount(e) {
  311. let amount = strip(e)
  312. let loan = this.loans[this.sel]
  313. if (!this.estimate.price) return
  314. if (amount > loan.price) amount = loan.price
  315. if (amount < 0) amount = 0
  316. loan.amount = amount
  317. loan.ltv = (amount / this.estimate.price * 100).toFixed(2)
  318. }
  319. // Updates the property price for all loans
  320. function setPrice(e) {
  321. let value = strip(e)
  322. this.estimate.price = value
  323. }
  324. function setDti(e) {
  325. let dti = strip(e)
  326. let loan = this.loans[this.sel]
  327. if (!loan.price) return
  328. if (dti > 100) dti = 100
  329. if (dti < 0) dti = 0
  330. e.target.value = dti
  331. loan.dti = dti
  332. }
  333. function setHousingDti(e) {
  334. let housingDti = strip(e)
  335. let loan = this.loans[this.sel]
  336. if (!loan.price) return
  337. if (housingDti > 100) housingDti = 100
  338. if (housingDti < 0) housingDti = 0
  339. e.target.value = housingDti
  340. loan.housingDti = housingDti
  341. }
  342. function generate() {
  343. this.errors = this.validate()
  344. }
  345. function validate() {
  346. let errors = []
  347. const estimate = this.estimate
  348. // Alternative attribute names for error messages
  349. const names = {
  350. term: "loan term",
  351. ltv: "loan to value",
  352. hazard: "hazard insurance",
  353. hazardEscrow: "hazard insurance escrow",
  354. }
  355. if (!estimate.property) {
  356. errors.push("Missing property type.")
  357. } else if (!estimate.price) {
  358. errors.push("Missing property price.")
  359. } else if (!estimate.borrowers) {
  360. errors.push("Missing number of borrowers.")
  361. } else if (!estimate.creditScore) {
  362. errors.push("Missing credit score.")
  363. } else if (!estimate.mIncome) {
  364. errors.push("Missing monthly income.")
  365. }
  366. return errors
  367. }
  368. export default {
  369. components: { Dialog },
  370. methods: {
  371. setPrice, setLtv, setAmount, setDti, setHousingDti, strip, stripInt,
  372. stripLetters, stripPerc, del, create, createFees, createFee, resetFees,
  373. addFee, generate, validate
  374. },
  375. computed: {
  376. validFee,
  377. },
  378. props: ['user'],
  379. data() {
  380. return {
  381. estimate: estimate,
  382. loans: estimate.loans,
  383. sel: 0,
  384. newFee: null,
  385. errors: [],
  386. }
  387. },
  388. }
  389. </script>