vcn.component.ts 12 KB

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