pbur.component.ts 13 KB

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