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

287 line
7.3 KiB

  1. <template>
  2. <div class="page">
  3. <h2>Estimates</h2>
  4. <section class="form inputs">
  5. <h3>Default Fees</h3>
  6. <div
  7. v-for="(fee, indx) in fees"
  8. :key="fee.name + indx" class="fee"
  9. >
  10. <label @click="() => edit = fee">
  11. ${{fee.amount/100}}{{ fee.perc ? ` ${fee.perc}%` : ''}} - {{fee.name}}<br>
  12. {{fee.type}}
  13. </label>
  14. <img width="21" height="21" src="/assets/image/icon/x-red.svg"
  15. @click="() => remove(fee)">
  16. </div>
  17. <button @click="() => edit = {}">New Fee</button>
  18. </section>
  19. <fee-dialog v-if="edit"
  20. :heading="'Fee'"
  21. :initial="edit"
  22. :price="0"
  23. @close="() => edit = null"
  24. @save="newFee"
  25. />
  26. <section class="inputs estimates">
  27. <h3>Templates</h3>
  28. <div class="entry" v-for="t in templates" v-if="!template"
  29. @click="() => template = t" key="t.id">
  30. <span>
  31. {{t.estimate.id}} - {{t.estimate.property}} - ${{(t.estimate.price/100).toLocaleString()}}
  32. </span>
  33. </div>
  34. <div class="details" v-if="template">
  35. <label>
  36. #{{template.estimate.id}} -
  37. {{template.estimate.transaction}} -
  38. {{template.estimate.property}} -
  39. ${{(template.estimate.price / 100).toLocaleString()}}
  40. </label>
  41. <label>Borrowers: {{template.estimate.borrower.num}}</label>
  42. <label>Credit: {{template.estimate.borrower.credit}}</label>
  43. <label>Income: {{(template.estimate.borrower.income/100).toLocaleString()}}</label>
  44. <div v-for="l in template.estimate.loans" class="details">
  45. <h4>{{l.title}}</h4>
  46. <label>{{l.type.name}}</label>
  47. <label>Total monthly: ${{format(l.result.totalMonthly)}}</label>
  48. <label>Cash to close: ${{format(l.result.cashToClose)}}</label>
  49. </div>
  50. <button @click="() => download(template.estimate)">Generate PDF</button>
  51. <button @click="() => deleting = true">Remove</button>
  52. <button @click="() => template = null">Cancel</button>
  53. </div>
  54. </section>
  55. <section class="inputs estimates">
  56. <h3>Saved Estimates</h3>
  57. <div class="entry" v-for="e in estimates" v-if="!estimate"
  58. @click="() => estimate = e" key="e.id">
  59. <span>
  60. {{e.id}} - {{e.property}} - ${{(e.price/100).toLocaleString()}}
  61. </span>
  62. </div>
  63. <div class="details" v-if="estimate">
  64. <label>
  65. #{{estimate.id}} -
  66. {{estimate.transaction}} -
  67. {{estimate.property}} -
  68. ${{(estimate.price / 100).toLocaleString()}}
  69. </label>
  70. <label>Borrowers: {{estimate.borrower.num}}</label>
  71. <label>Credit: {{estimate.borrower.credit}}</label>
  72. <label>Income: {{(estimate.borrower.income/100).toLocaleString()}}</label>
  73. <div v-for="l in estimate.loans" class="details">
  74. <h4>{{l.title}}</h4>
  75. <label>{{l.type.name}}</label>
  76. <label>Total monthly: ${{format(l.result.totalMonthly)}}</label>
  77. <label>Cash to close: ${{format(l.result.cashToClose)}}</label>
  78. </div>
  79. <button @click="() => download(estimate)">Generate PDF</button>
  80. <button @click="() => saveTemplate(estimate)">Save As Template</button>
  81. <button @click="() => deleting = true" :disabled="isTemplate()">Delete</button>
  82. <button @click="() => estimate = null">Cancel</button>
  83. </div>
  84. </section>
  85. <DDialog v-if="dlink" @close="() => dlink = ''"
  86. :fileName="`estimate-${estimate.id}.pdf`" :url="dlink">
  87. </DDialog>
  88. <Dialog v-if="deleting" @close="() => deleting = false">
  89. <h3>Are you sure you want to delete this estimate?</h3>
  90. <button @click="() => del(estimate ?? template.estimate)">Confirm</button>
  91. </Dialog>
  92. </div>
  93. </template>
  94. <script setup>
  95. import { ref, computed, onMounted } from 'vue'
  96. import FeeDialog from "./fee-dialog.vue"
  97. import DDialog from "./download-dialog.vue"
  98. import Dialog from "./dialog.vue"
  99. import { format } from "../helpers.js"
  100. const props = defineProps(['user', 'fees', 'token'])
  101. const emit = defineEmits(['addFeeTemp', 'removeFeeTemp', 'preview'])
  102. let edit = ref(null)
  103. let estimates = ref([])
  104. let templates = ref([])
  105. let estimate = ref()
  106. let template = ref()
  107. let dlink = ref("")
  108. let deleting = ref()
  109. function newFee(fee, isDebit) {
  110. if (!isDebit) {
  111. fee.amount = -1 * fee.amount || 0
  112. fee.perc = -1 * fee.perc || 0
  113. }
  114. fetch(`/api/fee`,
  115. {method: 'POST',
  116. body: JSON.stringify(fee),
  117. headers: {
  118. "Accept": "application/json",
  119. "Authorization": `Bearer ${props.token}`,
  120. },
  121. }).then(resp => {
  122. if (resp.ok && resp.status == 200) {
  123. resp.json().then(r => emit('addFeeTemp', r))
  124. } else {
  125. // resp.text().then(t => this.errors = [t])
  126. // window.location.hash = 'new'
  127. resp.text().then(t => console.log(t))
  128. }
  129. })
  130. edit.value = null
  131. }
  132. function newType() {
  133. }
  134. function remove(fee) {
  135. fetch(`/api/fee`,
  136. {method: 'DELETE',
  137. headers: {
  138. "Accept": "application/json",
  139. "Authorization": `Bearer ${props.token}`,
  140. },
  141. body: JSON.stringify(fee)
  142. }).then(response => {
  143. if (response.ok) { emit('removeFeeTemp', fee) } else {
  144. response.text().then(t => console.log(t))
  145. }
  146. })
  147. }
  148. function getEstimates() {
  149. fetch(`/api/estimates`,
  150. {method: 'GET',
  151. headers: {
  152. "Accept": "application/json",
  153. "Authorization": `Bearer ${props.token}`,
  154. },
  155. }).then(response => {
  156. if (response.ok) { return response.json() } else {
  157. response.text().then(t => console.log(t))
  158. }
  159. }).then (result => {
  160. if (!result || !result.length) return // Exit if token is invalid or no fees are saved
  161. estimates.value = result
  162. })
  163. }
  164. function getTemplates() {
  165. fetch(`/api/estimate/templates`,
  166. {method: 'GET',
  167. headers: {
  168. "Accept": "application/json",
  169. "Authorization": `Bearer ${props.token}`,
  170. },
  171. }).then(response => {
  172. if (response.ok) { return response.json() } else {
  173. response.text().then(t => console.log(t))
  174. }
  175. }).then (result => {
  176. if (!result || !result.length) return // Exit if token is invalid or no fees are saved
  177. templates.value = result
  178. console.log(result)
  179. })
  180. }
  181. function saveTemplate(e) {
  182. fetch(`/api/estimate/templates`,
  183. {method: 'POST',
  184. body: JSON.stringify(e),
  185. headers: {
  186. "Accept": "application/json",
  187. "Authorization": `Bearer ${props.token}`,
  188. },
  189. }).then(response => {
  190. if (response.ok) { getTemplates() }
  191. })
  192. }
  193. function del(e) {
  194. fetch(`/api/estimate`,
  195. {method: 'DELETE',
  196. body: JSON.stringify(e),
  197. headers: {
  198. "Accept": "application/json",
  199. "Authorization": `Bearer ${props.token}`,
  200. },
  201. }).then(response => {
  202. if (response.ok) {
  203. estimates.value = estimates.value.filter(e => e.id != estimate.value.id)
  204. estimate.value = null
  205. deleting.value = false
  206. }
  207. })
  208. }
  209. function remove(t) {
  210. fetch(`/api/estimate/template/remove`,
  211. {method: 'DELETE',
  212. body: JSON.stringify(t),
  213. headers: {
  214. "Accept": "application/json",
  215. "Authorization": `Bearer ${props.token}`,
  216. },
  217. }).then(response => {
  218. if (response.ok) {
  219. estimates.value = estimates.value.filter(e => e.id != estimate.value.id)
  220. estimate.value = null
  221. deleting.value = false
  222. }
  223. })
  224. }
  225. function download(estimate) {
  226. fetch(`/api/pdf`,
  227. {method: 'POST',
  228. body: JSON.stringify(estimate),
  229. headers: {
  230. "Accept": "application/json",
  231. "Authorization": `Bearer ${props.token}`,
  232. },
  233. }).then(response => {
  234. if (response.ok) { return response.blob() }
  235. }).then (result => {
  236. if (!result) return // Exit if token is invalid or blank file returned
  237. dlink.value = URL.createObjectURL(result)
  238. })
  239. }
  240. function isTemplate() {
  241. return templates.value.some( t => t.estimate.id == estimate.value.id )
  242. }
  243. onMounted(() => {
  244. getEstimates()
  245. getTemplates()
  246. })
  247. </script>
  248. <style scoped>
  249. .modal a.button {
  250. margin: auto;
  251. margin-top: 30px;
  252. }
  253. </style>