cete.component.ts 11 KB

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