cete.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. constructor(
  87. private formBuilder: FormBuilder,
  88. private router: Router,
  89. private formDataService: FormInvestmentProposalService,
  90. private catalogService: CatalogsService,
  91. private instrumentCalcService: InstrumentCalculations,
  92. public datepipe: DatePipe
  93. ) {
  94. this.instrument_work = this.formDataService.getWork();
  95. this.instrument_exists = this.instrument_work == undefined;
  96. this.general = this.formDataService.getGeneralInfo();
  97. this.investmentProposalForm = this.formBuilder.group({
  98. valor_nominal: [
  99. this.instrument_exists ? "" : this.instrument_work.valor_nominal,
  100. [
  101. Validators.required,
  102. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  103. ]
  104. ],
  105. plazo: [
  106. this.instrument_exists ? "" : this.instrument_work.plazo,
  107. [Validators.required, Validators.pattern(/^[+]?([0-9]+)$/)]
  108. ],
  109. comision_casa_porcentaje: [
  110. this.instrument_exists
  111. ? ""
  112. : this.instrument_work.comision_casa_porcentaje,
  113. [
  114. Validators.required,
  115. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  116. ]
  117. ],
  118. comision_bolsa_porcentaje: [
  119. this.instrument_exists
  120. ? ""
  121. : this.instrument_work.comision_bolsa_porcentaje,
  122. [
  123. Validators.required,
  124. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  125. ]
  126. ],
  127. rendimiento_bruto: [
  128. this.instrument_exists ? "" : this.instrument_work.rendimiento_bruto,
  129. [
  130. Validators.required,
  131. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  132. ]
  133. ],
  134. otros_costos: [
  135. this.instrument_exists ? "" : this.instrument_work.rendimiento_bruto,
  136. [Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)]
  137. ],
  138. fecha_liquidacion: [
  139. this.instrument_exists
  140. ? ""
  141. : {
  142. isRange: false,
  143. singleDate: {
  144. jsDate: parse(
  145. this.instrument_work.fecha_liquidacion,
  146. "dd/MM/yyyy HH:mm:ss",
  147. new Date()
  148. ),
  149. formatted: this.instrument_work.fecha_liquidacion
  150. }
  151. },
  152. Validators.required
  153. ],
  154. fecha_vencimiento: [
  155. this.instrument_exists
  156. ? ""
  157. : {
  158. isRange: false,
  159. singleDate: {
  160. jsDate: parse(
  161. this.instrument_work.fecha_vencimiento,
  162. "dd/MM/yyyy HH:mm:ss",
  163. new Date()
  164. ),
  165. formatted: this.instrument_work.fecha_vencimiento
  166. }
  167. },
  168. Validators.required
  169. ],
  170. fecha_operacion: [
  171. this.instrument_exists
  172. ? ""
  173. : {
  174. isRange: false,
  175. singleDate: {
  176. jsDate: parse(
  177. this.instrument_work.fecha_operacion,
  178. "dd/MM/yyyy HH:mm:ss",
  179. new Date()
  180. ),
  181. formatted: this.instrument_work.fecha_operacion
  182. }
  183. },
  184. Validators.required
  185. ],
  186. fecha_ultima_cupon: [
  187. this.instrument_exists
  188. ? ""
  189. : {
  190. isRange: false,
  191. singleDate: {
  192. jsDate: parse(
  193. this.instrument_work.fecha_ultima_cupon,
  194. "dd/MM/yyyy HH:mm:ss",
  195. new Date()
  196. ),
  197. formatted: this.instrument_work.fecha_ultima_cupon
  198. }
  199. }
  200. ]
  201. });
  202. }
  203. get f() {
  204. return this.investmentProposalForm.controls;
  205. }
  206. save(form: any): boolean {
  207. if (!form.valid) {
  208. return false;
  209. }
  210. this.ceteObject = {
  211. valor_nominal: this.investmentProposalForm.value.valor_nominal,
  212. plazo: this.investmentProposalForm.value.plazo,
  213. comision_casa_porcentaje: this.investmentProposalForm.value
  214. .comision_casa_porcentaje,
  215. comision_bolsa_porcentaje: this.investmentProposalForm.value
  216. .comision_bolsa_porcentaje,
  217. rendimiento_bruto: this.investmentProposalForm.value.rendimiento_bruto,
  218. otros_costos: this.f.otros_costos.value,
  219. //renta_porcentaje: this.f.renta_porcentaje.value,
  220. //id_formato_ingreso: this.f.formato_ingreso.value,
  221. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  222. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate.formatted,
  223. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate.formatted,
  224. fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate.formatted
  225. };
  226. this.formDataService.setWork(this.ceteObject);
  227. return true;
  228. }
  229. getCalculations(form: any) {
  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 / 100,
  251. comision_bolsa_porcentaje:
  252. this.f.comision_bolsa_porcentaje.value / 100,
  253. plazo: +this.f.plazo.value,
  254. rendimiento_bruto: this.f.rendimiento_bruto.value / 100,
  255. otros_costos: this.f.otros_costos.value,
  256. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  257. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  258. .formatted,
  259. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  260. .formatted,
  261. fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate.formatted
  262. }
  263. )
  264. .subscribe(
  265. ans => {
  266. console.log(ans);
  267. this.ingreso_bruto = ans["result"]["ingreso_bruto"];
  268. this.ingreso_neto = ans["result"]["ingreso_neto"];
  269. this.valor_transado = ans["result"]["valor_transado"];
  270. this.precio_porcentaje = ans["result"]["precio_porcentaje"];
  271. this.rendimiento_neto = ans["result"]["rendimiento_neto"];
  272. this.total_pagar = ans["result"]["total_pagar"];
  273. this.comision_bolsa = ans["result"]["comision_bolsa"];
  274. this.comision_casa = ans["result"]["comision_casa"];
  275. this.proyecciones = ans["result"]["proyecciones"];
  276. this.dataSource.data = this.proyecciones;
  277. this.dataSource.paginator = this.paginator;
  278. this.dataSource.sort = this.sort;
  279. Swal.close();
  280. },
  281. err => {
  282. Swal.fire({
  283. icon: "error",
  284. title: "Error en el servidor",
  285. text: "No su pudo obtener la informacion"
  286. });
  287. }
  288. );
  289. }
  290. goToPrevious(form: any) {
  291. this.submitted = true;
  292. if (this.investmentID != undefined) {
  293. this.router.navigate(["/investment-proposal/general-info"], {
  294. queryParams: { id: this.investmentID }
  295. });
  296. } else {
  297. this.router.navigate(["/investment-proposal/general-info"]);
  298. }
  299. }
  300. goToNext(form: any) {
  301. this.submitted = true;
  302. if (this.save(form)) {
  303. if (this.investmentID != undefined) {
  304. console.log("paramsID");
  305. this.router.navigate(["/investment-proposal/complement-info"], {
  306. queryParams: { id: this.investmentID }
  307. });
  308. } else {
  309. this.router.navigate(["/investment-proposal/complement-info"]);
  310. }
  311. }
  312. }
  313. }