pbur.component.ts 12 KB

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