cete.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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-cete",
  18. templateUrl: "./cete.component.html"
  19. })
  20. export class CETE implements InstrumentComponent {
  21. title: string = "Cetes";
  22. @Input() data: any;
  23. @Input() summary: boolean;
  24. @Input() investmentID: string;
  25. displayedColumns: string[] = [
  26. "posicion",
  27. "plazo",
  28. "fecha_pago",
  29. "ingreso_bruto",
  30. "ingreso_neto"
  31. ];
  32. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  33. @ViewChild(MatSort, { static: true }) sort: MatSort;
  34. form: any;
  35. general: GeneralInfo;
  36. // For daterange
  37. daysLabels: any = {
  38. su: "Dom",
  39. mo: "Lun",
  40. tu: "Mar",
  41. we: "Mie",
  42. th: "Jue",
  43. fr: "Vie",
  44. sa: "Sab"
  45. };
  46. monthsLabels: any = {
  47. 1: "Ene",
  48. 2: "Feb",
  49. 3: "Mar",
  50. 4: "Abr",
  51. 5: "May",
  52. 6: "Jun",
  53. 7: "Jul",
  54. 8: "Ago",
  55. 9: "Sep",
  56. 10: "Oct",
  57. 11: "Nov",
  58. 12: "Dic"
  59. };
  60. investmentProposalForm: FormGroup;
  61. myDpOptions: IAngularMyDpOptions = {
  62. dateRange: false,
  63. dateFormat: "dd/mm/yyyy",
  64. dayLabels: this.daysLabels,
  65. monthLabels: this.monthsLabels
  66. };
  67. myDateInit: boolean = true;
  68. m_fecha_operacion: IMyDateModel;
  69. m_fecha_liquidacion: IMyDateModel;
  70. m_fecha_rendencion: IMyDateModel;
  71. submitted: boolean = false;
  72. instrument_exists: boolean;
  73. instrument_work: any = [];
  74. financials: any;
  75. base_types: any;
  76. ingreso_bruto: number = 0.0;
  77. ingreso_neto: number = 0.0;
  78. valor_transado: number = 0.0;
  79. precio_porcentaje: number = 0.0;
  80. rendimiento_neto: number = 0.0;
  81. total_pagar: number = 0.0;
  82. comision_bolsa: number = 0.0;
  83. comision_casa: number = 0.0;
  84. proyecciones: any;
  85. ceteObject: {};
  86. dataSource = new MatTableDataSource(this.proyecciones);
  87. dataSource2 = new MatTableDataSource(this.proyecciones);
  88. hasProjections: boolean;
  89. fecha_vencimiento: any;
  90. constructor(
  91. private formBuilder: FormBuilder,
  92. private router: Router,
  93. private formDataService: FormInvestmentProposalService,
  94. private catalogService: CatalogsService,
  95. private instrumentCalcService: InstrumentCalculations,
  96. public datepipe: DatePipe
  97. ) {
  98. this.instrument_work = this.formDataService.getWork();
  99. this.instrument_exists = this.instrument_work == undefined;
  100. this.general = this.formDataService.getGeneralInfo();
  101. if (
  102. this.instrument_work != undefined &&
  103. (this.instrument_work.proyecciones != "" ||
  104. this.instrument_work != undefined)
  105. ) {
  106. this.hasProjections = true;
  107. this.dataSource2.data = this.instrument_work.proyecciones;
  108. this.dataSource2.paginator = this.paginator;
  109. this.dataSource2.sort = this.sort;
  110. } else {
  111. this.hasProjections = false;
  112. }
  113. this.investmentProposalForm = this.formBuilder.group({
  114. valor_nominal: [
  115. this.instrument_exists ? "" : this.instrument_work.valor_nominal,
  116. [
  117. Validators.required,
  118. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  119. ]
  120. ],
  121. plazo: [
  122. this.instrument_exists ? "" : this.instrument_work.plazo,
  123. [Validators.required, Validators.pattern(/^[+]?([0-9]+)$/)]
  124. ],
  125. comision_casa_porcentaje: [
  126. this.instrument_exists
  127. ? ""
  128. : this.instrument_work.comision_casa_porcentaje,
  129. [
  130. Validators.required,
  131. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  132. ]
  133. ],
  134. comision_bolsa_porcentaje: [
  135. this.instrument_exists
  136. ? ""
  137. : this.instrument_work.comision_bolsa_porcentaje,
  138. [
  139. Validators.required,
  140. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  141. ]
  142. ],
  143. rendimiento_bruto: [
  144. this.instrument_exists ? "" : this.instrument_work.rendimiento_bruto,
  145. [
  146. Validators.required,
  147. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  148. ]
  149. ],
  150. otros_costos: [
  151. this.instrument_exists ? 0 : this.instrument_work.rendimiento_bruto,
  152. [Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)]
  153. ],
  154. fecha_liquidacion: [
  155. this.instrument_exists
  156. ? ""
  157. : {
  158. isRange: false,
  159. singleDate: {
  160. jsDate: parse(
  161. this.instrument_work.fecha_liquidacion,
  162. "dd/MM/yyyy",
  163. new Date()
  164. ),
  165. formatted: this.instrument_work.fecha_liquidacion
  166. }
  167. },
  168. Validators.required
  169. ] /*
  170. fecha_vencimiento: [
  171. this.instrument_exists
  172. ? ""
  173. : {
  174. isRange: false,
  175. singleDate: {
  176. jsDate: parse(
  177. this.instrument_work.fecha_vencimiento,
  178. "dd/MM/yyyy",
  179. new Date()
  180. ),
  181. formatted: this.instrument_work.fecha_vencimiento
  182. }
  183. },
  184. Validators.required
  185. ],*/,
  186. fecha_operacion: [
  187. this.instrument_exists
  188. ? ""
  189. : {
  190. isRange: false,
  191. singleDate: {
  192. jsDate: parse(
  193. this.instrument_work.fecha_operacion,
  194. "dd/MM/yyyy",
  195. new Date()
  196. ),
  197. formatted: this.instrument_work.fecha_operacion
  198. }
  199. },
  200. Validators.required
  201. ],
  202. fecha_ultima_cupon: [
  203. this.instrument_exists
  204. ? ""
  205. : {
  206. isRange: false,
  207. singleDate: {
  208. jsDate: parse(
  209. this.instrument_work.fecha_ultima_cupon,
  210. "dd/MM/yyyy",
  211. new Date()
  212. ),
  213. formatted: this.instrument_work.fecha_ultima_cupon
  214. }
  215. }
  216. ]
  217. });
  218. }
  219. get f() {
  220. return this.investmentProposalForm.controls;
  221. }
  222. save(form: any): boolean {
  223. if (!form.valid) {
  224. return false;
  225. }
  226. this.formDataService.setWork(this.ceteObject);
  227. return true;
  228. }
  229. getCalculations(form: any, saveForm: boolean) {
  230. this.submitted = true;
  231. if (!form.valid) {
  232. return false;
  233. }
  234. Swal.fire({
  235. allowOutsideClick: false,
  236. icon: "info",
  237. text: "Espere por favor..."
  238. });
  239. Swal.showLoading();
  240. this.instrumentCalcService
  241. .ceteCalc(
  242. "CETE", // Codigo del instrumento
  243. {
  244. id_tipo_base: +this.general.base_anual,
  245. id_formato_ingreso: +this.general.formato_ingreso,
  246. id_periodicidad: +this.general.periodicidad
  247. },
  248. {
  249. valor_nominal: +this.f.valor_nominal.value,
  250. comision_casa_porcentaje: this.f.comision_casa_porcentaje.value,
  251. comision_bolsa_porcentaje: this.f.comision_bolsa_porcentaje.value,
  252. plazo: +this.f.plazo.value,
  253. rendimiento_bruto: this.f.rendimiento_bruto.value,
  254. otros_costos: this.f.otros_costos.value,
  255. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  256. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  257. .formatted,
  258. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  259. .formatted
  260. //fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate.formatted
  261. }
  262. )
  263. .subscribe(
  264. ans => {
  265. this.ingreso_bruto = ans["result"]["ingreso_bruto"];
  266. this.ingreso_neto = ans["result"]["ingreso_neto"];
  267. this.valor_transado = ans["result"]["valor_transado"];
  268. this.precio_porcentaje = ans["result"]["precio_porcentaje"];
  269. this.rendimiento_neto = ans["result"]["rendimiento_neto"];
  270. this.total_pagar = ans["result"]["total_pagar"];
  271. this.comision_bolsa = ans["result"]["comision_bolsa"];
  272. this.comision_casa = ans["result"]["comision_casa"];
  273. this.proyecciones = ans["result"]["proyecciones"];
  274. this.fecha_vencimiento = ans["result"]["fecha_vencimiento"];
  275. this.dataSource.data = this.proyecciones;
  276. this.dataSource.paginator = this.paginator;
  277. this.dataSource.sort = this.sort;
  278. this.ceteObject = {
  279. valor_nominal: this.investmentProposalForm.value.valor_nominal,
  280. plazo: this.investmentProposalForm.value.plazo,
  281. comision_casa_porcentaje: this.investmentProposalForm.value
  282. .comision_casa_porcentaje,
  283. comision_bolsa_porcentaje: this.investmentProposalForm.value
  284. .comision_bolsa_porcentaje,
  285. rendimiento_bruto: this.investmentProposalForm.value
  286. .rendimiento_bruto,
  287. otros_costos: this.f.otros_costos.value,
  288. //renta_porcentaje: this.f.renta_porcentaje.value,
  289. //id_formato_ingreso: this.f.formato_ingreso.value,
  290. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  291. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  292. .formatted,
  293. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  294. .formatted,
  295. fecha_vencimiento: this.fecha_vencimiento,
  296. ingreso_bruto: this.ingreso_bruto,
  297. ingreso_neto: this.ingreso_neto,
  298. valor_transado: this.valor_transado,
  299. precio_porcentaje: this.precio_porcentaje,
  300. rendimiento_neto: this.rendimiento_neto,
  301. total_pagar: this.total_pagar,
  302. comision_bolsa: this.comision_bolsa,
  303. comision_casa: this.comision_casa,
  304. proyecciones: this.proyecciones
  305. };
  306. this.formDataService.setWork(this.ceteObject);
  307. Swal.close();
  308. if (saveForm == true) {
  309. if (this.investmentID != undefined) {
  310. this.router.navigate(["/investment-proposal/complement-info"], {
  311. queryParams: { id: this.investmentID }
  312. });
  313. } else {
  314. this.router.navigate(["/investment-proposal/complement-info"]);
  315. }
  316. }
  317. },
  318. err => {
  319. Swal.fire({
  320. icon: "error",
  321. title: "Error en el servidor",
  322. text: "No su pudo obtener la informacion"
  323. });
  324. return false;
  325. }
  326. );
  327. }
  328. goToPrevious() {
  329. this.submitted = true;
  330. if (this.investmentID != undefined) {
  331. this.router.navigate(["/investment-proposal/general-info"], {
  332. queryParams: { id: this.investmentID }
  333. });
  334. } else {
  335. this.router.navigate(["/investment-proposal/general-info"]);
  336. }
  337. }
  338. goToNext(form: any) {
  339. this.getCalculations(form, true);
  340. }
  341. }