cete.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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",
  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",
  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",
  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",
  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.formDataService.setWork(this.ceteObject);
  211. return true;
  212. }
  213. getCalculations(form: any, saveForm: boolean) {
  214. this.submitted = true;
  215. if (!form.valid) {
  216. return false;
  217. }
  218. Swal.fire({
  219. allowOutsideClick: false,
  220. icon: "info",
  221. text: "Espere por favor..."
  222. });
  223. Swal.showLoading();
  224. this.instrumentCalcService
  225. .ceteCalc(
  226. "CETE", // Codigo del instrumento
  227. {
  228. id_tipo_base: this.general.base_anual,
  229. id_formato_ingreso: this.general.formato_ingreso,
  230. id_periodicidad: this.general.periodicidad
  231. },
  232. {
  233. valor_nominal: +this.f.valor_nominal.value,
  234. comision_casa_porcentaje: this.f.comision_casa_porcentaje.value,
  235. comision_bolsa_porcentaje: this.f.comision_bolsa_porcentaje.value,
  236. plazo: +this.f.plazo.value,
  237. rendimiento_bruto: this.f.rendimiento_bruto.value,
  238. otros_costos: this.f.otros_costos.value,
  239. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  240. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  241. .formatted,
  242. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  243. .formatted,
  244. fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate.formatted
  245. }
  246. )
  247. .subscribe(
  248. ans => {
  249. console.log(ans);
  250. this.ingreso_bruto = ans["result"]["ingreso_bruto"];
  251. this.ingreso_neto = ans["result"]["ingreso_neto"];
  252. this.valor_transado = ans["result"]["valor_transado"];
  253. this.precio_porcentaje = ans["result"]["precio_porcentaje"];
  254. this.rendimiento_neto = ans["result"]["rendimiento_neto"];
  255. this.total_pagar = ans["result"]["total_pagar"];
  256. this.comision_bolsa = ans["result"]["comision_bolsa"];
  257. this.comision_casa = ans["result"]["comision_casa"];
  258. this.proyecciones = ans["result"]["proyecciones"];
  259. this.dataSource.data = this.proyecciones;
  260. this.dataSource.paginator = this.paginator;
  261. this.dataSource.sort = this.sort;
  262. this.ceteObject = {
  263. valor_nominal: this.investmentProposalForm.value.valor_nominal,
  264. plazo: this.investmentProposalForm.value.plazo,
  265. comision_casa_porcentaje: this.investmentProposalForm.value
  266. .comision_casa_porcentaje,
  267. comision_bolsa_porcentaje: this.investmentProposalForm.value
  268. .comision_bolsa_porcentaje,
  269. rendimiento_bruto: this.investmentProposalForm.value
  270. .rendimiento_bruto,
  271. otros_costos: this.f.otros_costos.value,
  272. //renta_porcentaje: this.f.renta_porcentaje.value,
  273. //id_formato_ingreso: this.f.formato_ingreso.value,
  274. fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted,
  275. fecha_liquidacion: this.f.fecha_liquidacion.value.singleDate
  276. .formatted,
  277. fecha_ultima_cupon: this.f.fecha_ultima_cupon.value.singleDate
  278. .formatted,
  279. fecha_vencimiento: this.f.fecha_vencimiento.value.singleDate
  280. .formatted,
  281. ingreso_bruto: this.ingreso_bruto,
  282. ingreso_neto: this.ingreso_neto,
  283. valor_transado: this.valor_transado,
  284. precio_porcentaje: this.precio_porcentaje,
  285. rendimiento_neto: this.rendimiento_neto,
  286. total_pagar: this.total_pagar,
  287. comision_bolsa: this.comision_bolsa,
  288. comision_casa: this.comision_casa,
  289. proyecciones: this.proyecciones
  290. };
  291. this.formDataService.setWork(this.ceteObject);
  292. Swal.close();
  293. if (saveForm == true) {
  294. if (this.investmentID != undefined) {
  295. this.router.navigate(["/investment-proposal/complement-info"], {
  296. queryParams: { id: this.investmentID }
  297. });
  298. } else {
  299. this.router.navigate(["/investment-proposal/complement-info"]);
  300. }
  301. }
  302. },
  303. err => {
  304. Swal.fire({
  305. icon: "error",
  306. title: "Error en el servidor",
  307. text: "No su pudo obtener la informacion"
  308. });
  309. return false;
  310. }
  311. );
  312. }
  313. goToPrevious() {
  314. this.submitted = true;
  315. if (this.investmentID != undefined) {
  316. this.router.navigate(["/investment-proposal/general-info"], {
  317. queryParams: { id: this.investmentID }
  318. });
  319. } else {
  320. this.router.navigate(["/investment-proposal/general-info"]);
  321. }
  322. }
  323. goToNext(form: any) {
  324. this.getCalculations(form, true);
  325. }
  326. }