pbur.component.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import { Component, OnInit, Input, ViewChild, OnChanges } 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, MatSort, MatTableDataSource } from "@angular/material";
  14. @Component({
  15. selector: "app-pbur",
  16. templateUrl: "./pbur.component.html"
  17. })
  18. export class PBUR implements InstrumentComponent {
  19. title: string = "Papel bursátil";
  20. @Input() data: any;
  21. @Input() summary: boolean;
  22. @Input() investmentID: string;
  23. form: any;
  24. general: GeneralInfo;
  25. displayedColumns: string[] = [
  26. "posicion",
  27. "plazo",
  28. "fecha_pago",
  29. "ingreso_bruto",
  30. "ingreso_neto",
  31. "costo_cedeval",
  32. "renta",
  33. "costo_transferencia",
  34. "costo_banco",
  35. "otros_costos"
  36. ];
  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_null: 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. pburObject: {};
  74. format_incomes: any;
  75. ingreso_bruto: number = 0.0;
  76. ingreso_neto: number = 0.0;
  77. valor_transado: number = 0.0;
  78. precio_porcentaje: number = 0.0;
  79. rendimiento_neto: number = 0.0;
  80. total_pagar: number = 0.0;
  81. comision_bolsa: number = 0.0;
  82. comision_casa: number = 0.0;
  83. plazo: number = 0;
  84. interes_acumulado: number = 0;
  85. fecha_inicio_vigencia: string;
  86. proyecciones: any;
  87. hasProjections: boolean;
  88. dataSource = new MatTableDataSource(this.proyecciones);
  89. dataSource2 = new MatTableDataSource(this.proyecciones);
  90. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  91. @ViewChild(MatSort, { static: true }) sort: MatSort;
  92. fecha_vencimiento: any;
  93. constructor(
  94. private formBuilder: FormBuilder,
  95. private router: Router,
  96. private formDataService: FormInvestmentProposalService,
  97. private catalogService: CatalogsService,
  98. private instrumentCalcService: InstrumentCalculations,
  99. public datepipe: DatePipe
  100. ) {
  101. this.instrument_work = this.formDataService.getWork();
  102. this.instrument_null = this.instrument_work == undefined;
  103. this.general = this.formDataService.getGeneralInfo();
  104. if (
  105. this.instrument_work != undefined &&
  106. this.instrument_work.valor_par == true &&
  107. this.instrument_work.proyecciones != undefined
  108. ) {
  109. this.hasProjections = true;
  110. this.dataSource2.data = this.instrument_work.proyecciones;
  111. this.dataSource2.paginator = this.paginator;
  112. this.dataSource2.sort = this.sort;
  113. } else {
  114. this.hasProjections = false;
  115. }
  116. //getIncomeFormat
  117. this.catalogService.getIncomeFormat().subscribe(res => {
  118. this.format_incomes = res;
  119. });
  120. this.investmentProposalForm = this.formBuilder.group({
  121. valor_par: [
  122. this.instrument_null || this.instrument_work.valor_par == null
  123. ? false
  124. : this.instrument_work.valor_par
  125. ],
  126. valor_nominal: [
  127. this.instrument_null ? "" : this.instrument_work.valor_nominal,
  128. [
  129. Validators.required,
  130. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  131. ]
  132. ],
  133. otros_costos: [
  134. this.instrument_null || this.instrument_work.otros_costos == null
  135. ? 0
  136. : this.instrument_work.otros_costos,
  137. [Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)]
  138. ],
  139. renta_porcentaje: [
  140. this.instrument_null || this.instrument_work.renta_porcentaje == null
  141. ? 10
  142. : this.instrument_work.renta_porcentaje,
  143. [Validators.required]
  144. ],
  145. comision_casa_porcentaje: [
  146. this.instrument_null
  147. ? ""
  148. : this.instrument_work.comision_casa_porcentaje,
  149. [
  150. Validators.required,
  151. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  152. ]
  153. ],
  154. comision_bolsa_porcentaje: [
  155. this.instrument_null
  156. ? ""
  157. : this.instrument_work.comision_bolsa_porcentaje,
  158. [
  159. Validators.required,
  160. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  161. ]
  162. ],
  163. plazo: [
  164. this.instrument_null || this.instrument_work.plazo == null
  165. ? 0
  166. : this.instrument_work.plazo,
  167. [Validators.required]
  168. ],
  169. rendimiento_bruto: [
  170. this.instrument_null ? "" : this.instrument_work.rendimiento_bruto,
  171. [
  172. Validators.required,
  173. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  174. ]
  175. ],
  176. /*formato_ingreso: [
  177. this.instrument_null ? "" : this.instrument_work.formato_ingreso,
  178. [Validators.required]
  179. ],*/
  180. fecha_liquidacion: [
  181. this.instrument_null
  182. ? ""
  183. : {
  184. isRange: false,
  185. singleDate: {
  186. jsDate: parse(
  187. this.instrument_work.fecha_liquidacion,
  188. "dd/MM/yyyy",
  189. new Date()
  190. ),
  191. formatted: this.instrument_work.fecha_liquidacion
  192. }
  193. },
  194. Validators.required
  195. ],
  196. fecha_operacion: [
  197. this.instrument_null
  198. ? ""
  199. : {
  200. isRange: false,
  201. singleDate: {
  202. jsDate: parse(
  203. this.instrument_work.fecha_operacion,
  204. "dd/MM/yyyy",
  205. new Date()
  206. ),
  207. formatted: this.instrument_work.fecha_operacion
  208. }
  209. },
  210. Validators.required
  211. ],
  212. fecha_ultima_cupon: [
  213. this.instrument_null
  214. ? ""
  215. : {
  216. isRange: false,
  217. singleDate: {
  218. jsDate: parse(
  219. this.instrument_work.fecha_ultima_cupon,
  220. "dd/MM/yyyy",
  221. new Date()
  222. ),
  223. formatted: this.instrument_work.fecha_ultima_cupon
  224. }
  225. },
  226. Validators.required
  227. ]
  228. });
  229. }
  230. get f() {
  231. return this.investmentProposalForm.controls;
  232. }
  233. save(form: any): boolean {
  234. if (!form.valid) {
  235. return false;
  236. }
  237. return true;
  238. }
  239. getCalculations(form: any, saveForm: boolean) {
  240. this.submitted = true;
  241. if (!form.valid) {
  242. return false;
  243. }
  244. Swal.fire({
  245. allowOutsideClick: false,
  246. icon: "info",
  247. text: "Espere por favor..."
  248. });
  249. Swal.showLoading();
  250. this.instrumentCalcService
  251. .pburCalc(
  252. "PBUR", // Codigo del instrumento
  253. {
  254. id_tipo_base: +this.general.base_anual,
  255. id_periodicidad: +this.general.periodicidad,
  256. id_formato_ingreso: +this.general.formato_ingreso
  257. },
  258. {
  259. valor_par: this.f.valor_par.value,
  260. valor_nominal: +this.f.valor_nominal.value,
  261. plazo: this.f.plazo.value,
  262. comision_casa_porcentaje: this.f.comision_casa_porcentaje.value,
  263. comision_bolsa_porcentaje: this.f.comision_bolsa_porcentaje.value,
  264. rendimiento_bruto: this.f.rendimiento_bruto.value,
  265. otros_costos: this.f.otros_costos.value,
  266. renta_porcentaje: this.f.renta_porcentaje.value,
  267. //id_formato_ingreso: this.f.formato_ingreso.value,
  268. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  269. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  270. .formatted,
  271. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  272. .formatted
  273. }
  274. )
  275. .subscribe(
  276. ans => {
  277. this.ingreso_bruto = ans["result"]["ingreso_bruto"];
  278. this.ingreso_neto = ans["result"]["ingreso_neto"];
  279. this.valor_transado = ans["result"]["valor_transado"];
  280. this.precio_porcentaje = ans["result"]["precio_porcentaje"];
  281. this.rendimiento_neto = ans["result"]["rendimiento_neto"];
  282. this.total_pagar = ans["result"]["total_pagar"];
  283. this.comision_bolsa = ans["result"]["comision_bolsa"];
  284. this.comision_casa = ans["result"]["comision_casa"];
  285. this.interes_acumulado = ans["result"]["interes_acumulado"];
  286. this.fecha_inicio_vigencia = ans["result"]["fecha_inicio_vigencia"];
  287. this.proyecciones = ans["result"]["proyecciones"];
  288. this.fecha_vencimiento = ans["result"]["fecha_vencimiento"];
  289. if (this.f.valor_par.value == true) {
  290. this.hasProjections = true;
  291. } else {
  292. this.hasProjections = false;
  293. }
  294. this.dataSource.data = this.proyecciones;
  295. this.dataSource.paginator = this.paginator;
  296. this.dataSource.sort = this.sort;
  297. this.pburObject = {
  298. valor_par: this.investmentProposalForm.value.valor_par,
  299. valor_nominal: this.investmentProposalForm.value.valor_nominal,
  300. renta_porcentaje: this.investmentProposalForm.value
  301. .renta_porcentaje,
  302. comision_casa_porcentaje: this.investmentProposalForm.value
  303. .comision_casa_porcentaje,
  304. comision_bolsa_porcentaje: this.investmentProposalForm.value
  305. .comision_bolsa_porcentaje,
  306. comision_casa: this.comision_casa,
  307. comision_bolsa: this.comision_bolsa,
  308. rendimiento_bruto: this.investmentProposalForm.value
  309. .rendimiento_bruto,
  310. otros_costos: this.investmentProposalForm.value.otros_costos,
  311. ingreso_bruto: this.ingreso_bruto,
  312. ingreso_neto: this.ingreso_neto,
  313. valor_transado: this.valor_transado,
  314. precio_porcentaje: this.precio_porcentaje,
  315. rendimiento_neto: this.rendimiento_neto,
  316. total_pagar: this.total_pagar,
  317. interes_acumulado: this.interes_acumulado,
  318. fecha_inicio_vigencia: this.fecha_inicio_vigencia,
  319. proyecciones: this.proyecciones,
  320. plazo: this.investmentProposalForm.value.plazo,
  321. //id_formato_ingreso: this.investmentProposalForm.value.id_formato_ingreso,
  322. fecha_operacion: this.investmentProposalForm.value.fecha_operacion
  323. .singleDate.formatted,
  324. fecha_liquidacion:
  325. this.investmentProposalForm.value.fecha_liquidacion.singleDate
  326. .formatted || "",
  327. fecha_ultima_cupon:
  328. this.investmentProposalForm.value.fecha_ultima_cupon != undefined
  329. ? this.investmentProposalForm.value.fecha_ultima_cupon
  330. .singleDate.formatted
  331. : "",
  332. fecha_vencimiento: this.fecha_vencimiento
  333. };
  334. this.formDataService.setWork(this.pburObject);
  335. Swal.close();
  336. if (saveForm == true) {
  337. if (this.investmentID != undefined) {
  338. this.router.navigate(["/investment-proposal/complement-info"], {
  339. queryParams: { id: this.investmentID }
  340. });
  341. } else {
  342. this.router.navigate(["/investment-proposal/complement-info"]);
  343. }
  344. }
  345. },
  346. err => {
  347. Swal.fire({
  348. icon: "error",
  349. title: "Error en el servidor",
  350. text: "No su pudo obtener la informacion"
  351. });
  352. return false;
  353. }
  354. );
  355. }
  356. goToPrevious() {
  357. this.submitted = true;
  358. if (this.investmentID != undefined) {
  359. this.router.navigate(["/investment-proposal/general-info"], {
  360. queryParams: { id: this.investmentID }
  361. });
  362. } else {
  363. this.router.navigate(["/investment-proposal/general-info"]);
  364. }
  365. }
  366. goToNext(form: any) {
  367. this.getCalculations(form, true);
  368. }
  369. }