dap.component.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { Component, OnInit, Input, ViewChild } from "@angular/core";
  2. import { InstrumentComponent } from "@app/components/investment-proposals/instrument/instrument.component";
  3. import { FormBuilder, FormGroup, Validators } from "@angular/forms";
  4. import { IAngularMyDpOptions, IMyDateModel } from "angular-mydatepicker";
  5. import { formatDate, DatePipe } from "@angular/common";
  6. import { Router } from "@angular/router";
  7. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  8. import { CatalogsService } from "@app/services/catalogs.service";
  9. import { InstrumentCalculations } from "@app/services/instrument-calculations.service";
  10. import Swal from "sweetalert2";
  11. import { GeneralInfo } from "@app/models/investment-proposal-form";
  12. import { parse } from "date-fns";
  13. import { MatPaginator } from "@angular/material/paginator";
  14. import { MatSort } from "@angular/material/sort";
  15. import { MatTableDataSource } from "@angular/material/table";
  16. @Component({
  17. selector: "app-dap",
  18. templateUrl: "./dap.component.html"
  19. })
  20. export class DAP implements InstrumentComponent {
  21. title: string = "Depósitos a plazo";
  22. @Input() data: any;
  23. @Input() summary: boolean;
  24. @Input() investmentID: string;
  25. form: any;
  26. general: GeneralInfo;
  27. displayedColumns: string[] = [
  28. "posicion",
  29. "plazo",
  30. "fecha_pago",
  31. "ingreso_bruto",
  32. "renta",
  33. "ingreso_neto"
  34. ];
  35. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  36. @ViewChild(MatSort, { static: true }) sort: MatSort;
  37. // For daterange
  38. daysLabels: any = {
  39. su: "Dom",
  40. mo: "Lun",
  41. tu: "Mar",
  42. we: "Mie",
  43. th: "Jue",
  44. fr: "Vie",
  45. sa: "Sab"
  46. };
  47. monthsLabels: any = {
  48. 1: "Ene",
  49. 2: "Feb",
  50. 3: "Mar",
  51. 4: "Abr",
  52. 5: "May",
  53. 6: "Jun",
  54. 7: "Jul",
  55. 8: "Ago",
  56. 9: "Sep",
  57. 10: "Oct",
  58. 11: "Nov",
  59. 12: "Dic"
  60. };
  61. instrument_exists: boolean;
  62. instrument_work: any = [];
  63. investmentProposalForm: FormGroup;
  64. myDpOptions: IAngularMyDpOptions = {
  65. dateRange: false,
  66. dateFormat: "dd/mm/yyyy",
  67. dayLabels: this.daysLabels,
  68. monthLabels: this.monthsLabels
  69. };
  70. submitted: boolean;
  71. myDateInit: boolean = true;
  72. model: IMyDateModel = null;
  73. ingreso_bruto: number = 0.0;
  74. ingreso_neto: number = 0.0;
  75. valor_transado: number = 0.0;
  76. precio_porcentaje: number = 0.0;
  77. rendimiento_neto: number = 0.0;
  78. plazo: number = 0;
  79. fecha_inicio_vigencia: string;
  80. rendimiento_bruto: number = 0;
  81. monto_inversion: number = 0;
  82. proyecciones: any;
  83. dataSource = new MatTableDataSource(this.proyecciones);
  84. dapObject: {};
  85. renta: any;
  86. constructor(
  87. private formBuilder: FormBuilder,
  88. private router: Router,
  89. private formDataService: FormInvestmentProposalService,
  90. private catalogService: CatalogsService,
  91. private instrumentCalcService: InstrumentCalculations,
  92. public datepipe: DatePipe
  93. ) {
  94. this.instrument_work = this.formDataService.getWork();
  95. this.instrument_exists = this.instrument_work == undefined;
  96. this.general = this.formDataService.getGeneralInfo();
  97. this.investmentProposalForm = this.formBuilder.group({
  98. monto_inversion: [
  99. this.instrument_exists ? "" : this.instrument_work.monto_inversion,
  100. [
  101. Validators.required,
  102. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  103. ]
  104. ],
  105. renta_porcentaje: [
  106. this.instrument_exists ? "" : this.instrument_work.renta_porcentaje,
  107. [Validators.required]
  108. ],
  109. tasa_porcentaje: [
  110. this.instrument_exists ? "" : this.instrument_work.tasa_porcentaje,
  111. [
  112. Validators.required,
  113. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  114. ]
  115. ],
  116. plazo: [
  117. this.instrument_exists ? "" : this.instrument_work.plazo,
  118. [Validators.required]
  119. ],
  120. /*formato_ingreso: [
  121. this.instrument_exists ? "" : this.instrument_work.formato_ingreso,
  122. [Validators.required]
  123. ],*/
  124. fecha_vencimiento: [
  125. this.instrument_exists
  126. ? ""
  127. : {
  128. isRange: false,
  129. singleDate: {
  130. jsDate: parse(
  131. this.instrument_work.fecha_vencimiento,
  132. "dd/MM/yyyy",
  133. new Date()
  134. ),
  135. formatted: this.instrument_work.fecha_vencimiento
  136. }
  137. },
  138. Validators.required
  139. ],
  140. fecha_operacion: [
  141. this.instrument_exists
  142. ? ""
  143. : {
  144. isRange: false,
  145. singleDate: {
  146. jsDate: parse(
  147. this.instrument_work.fecha_operacion,
  148. "dd/MM/yyyy",
  149. new Date()
  150. ),
  151. formatted: this.instrument_work.fecha_operacion
  152. }
  153. },
  154. Validators.required
  155. ]
  156. });
  157. }
  158. get f() {
  159. return this.investmentProposalForm.controls;
  160. }
  161. save(form: any): boolean {
  162. if (!form.valid) {
  163. return false;
  164. }
  165. return true;
  166. }
  167. getCalculations(form: any, saveForm: boolean) {
  168. this.submitted = true;
  169. if (!form.valid) {
  170. return false;
  171. }
  172. Swal.fire({
  173. allowOutsideClick: false,
  174. icon: "info",
  175. text: "Espere por favor..."
  176. });
  177. Swal.showLoading();
  178. this.instrumentCalcService
  179. .dapCalc(
  180. "DAP", // Codigo del instrumento
  181. {
  182. id_tipo_base: this.general.base_anual,
  183. id_periodicidad: this.general.periodicidad,
  184. id_formato_ingreso: this.general.formato_ingreso
  185. },
  186. {
  187. monto_inversion: +this.f.monto_inversion.value,
  188. tasa_porcentaje: +this.f.tasa_porcentaje.value,
  189. renta_porcentaje: +this.f.renta_porcentaje.value,
  190. plazo: +this.f.plazo.value,
  191. //id_formato_ingreso: this.f.formato_ingreso.value,
  192. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  193. fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate.formatted
  194. }
  195. )
  196. .subscribe(
  197. ans => {
  198. this.monto_inversion = ans["result"]["monto_inversion"];
  199. this.ingreso_bruto = ans["result"]["ingreso_bruto"];
  200. this.ingreso_neto = ans["result"]["ingreso_neto"];
  201. this.rendimiento_bruto = ans["result"]["rendimiento_bruto"];
  202. this.rendimiento_neto = ans["result"]["rendimiento_neto"];
  203. this.plazo = ans["result"]["plazo"];
  204. this.fecha_inicio_vigencia = ans["result"]["fecha_inicio_vigencia"];
  205. this.renta = ans["result"]["renta"];
  206. this.proyecciones = ans["result"]["proyecciones"];
  207. this.dataSource.data = this.proyecciones;
  208. this.dataSource.paginator = this.paginator;
  209. this.dataSource.sort = this.sort;
  210. this.dapObject = {
  211. monto_inversion: this.investmentProposalForm.value.monto_inversion,
  212. renta_porcentaje: this.investmentProposalForm.value
  213. .renta_porcentaje,
  214. tasa_porcentaje: this.investmentProposalForm.value.tasa_porcentaje,
  215. plazo: +this.investmentProposalForm.value.plazo,
  216. ingreso_bruto: this.ingreso_bruto,
  217. ingreso_neto: this.ingreso_neto,
  218. rendimiento_bruto: this.rendimiento_bruto,
  219. rendimiento_neto: this.rendimiento_neto,
  220. fecha_inicio_vigencia: this.fecha_inicio_vigencia,
  221. renta: this.renta,
  222. proyecciones: this.proyecciones,
  223. fecha_operacion: this.investmentProposalForm.value.fecha_operacion
  224. .singleDate.formatted,
  225. fecha_vencimiento:
  226. this.investmentProposalForm.value.fecha_vencimiento != undefined
  227. ? this.investmentProposalForm.value.fecha_vencimiento.singleDate
  228. .formatted
  229. : ""
  230. };
  231. this.formDataService.setWork(this.dapObject);
  232. Swal.close();
  233. if (saveForm == true) {
  234. if (this.investmentID != undefined) {
  235. this.router.navigate(["/investment-proposal/complement-info"], {
  236. queryParams: { id: this.investmentID }
  237. });
  238. } else {
  239. this.router.navigate(["/investment-proposal/complement-info"]);
  240. }
  241. }
  242. },
  243. err => {
  244. Swal.fire({
  245. icon: "error",
  246. title: "Error en el servidor",
  247. text: "No su pudo obtener la informacion"
  248. });
  249. return false;
  250. }
  251. );
  252. }
  253. goToPrevious() {
  254. this.submitted = true;
  255. if (this.investmentID != undefined) {
  256. this.router.navigate(["/investment-proposal/general-info"], {
  257. queryParams: { id: this.investmentID }
  258. });
  259. } else {
  260. this.router.navigate(["/investment-proposal/general-info"]);
  261. }
  262. }
  263. goToNext(form: any) {
  264. this.getCalculations(form, true);
  265. }
  266. }