vcn.component.ts 13 KB

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