general-info.component.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import {
  2. Component,
  3. OnInit,
  4. OnChanges,
  5. ViewChild,
  6. ComponentRef,
  7. Type,
  8. ElementRef
  9. } from "@angular/core";
  10. import { FormBuilder, FormGroup, Validators } from "@angular/forms";
  11. import Swal from "sweetalert2";
  12. import {
  13. GeneralInfo,
  14. ComplementInfo
  15. } from "@app/models/investment-proposal-form";
  16. import { Router, ActivatedRoute } from "@angular/router";
  17. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  18. import { CatalogsService } from "@app/services/catalogs.service";
  19. import { IAngularMyDpOptions, IMyDateModel } from "angular-mydatepicker";
  20. import { InvestmentsService } from "@app/services/investments.service";
  21. import { DatePipe } from "@angular/common";
  22. import { parse } from "date-fns";
  23. import { IOption } from "ng-select";
  24. @Component({
  25. selector: "app-new-investment-proposal",
  26. templateUrl: "./general-info.component.html",
  27. styleUrls: ["./general-info.component.scss"]
  28. })
  29. export class InvestmentProposalGeneralInfoComponent
  30. implements OnInit, OnChanges {
  31. general: GeneralInfo;
  32. complement: ComplementInfo;
  33. instrument: {};
  34. form: any;
  35. investmentProposalID: any = undefined;
  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. model: IMyDateModel = null;
  69. title: string = "Formulario propuesta de inversión";
  70. submitted: boolean = false;
  71. role_number: any;
  72. periodicities: any;
  73. rates: any;
  74. revenues: any;
  75. funds: any;
  76. instrumentTypes: Array<IOption>;
  77. generalInfoDontExists: boolean;
  78. plazos: any;
  79. base_types: any;
  80. financials: Array<IOption>;
  81. format_incomes: any;
  82. gInfo: any;
  83. gInstrument: any;
  84. gComplement: any;
  85. datez: any;
  86. instrumentName: any;
  87. instrumentValue: any;
  88. myOptions: Array<IOption> = [
  89. { label: "Belgium", value: "BE" },
  90. { label: "Luxembourg", value: "LU" },
  91. { label: "Netherlands", value: "NL" }
  92. ];
  93. @ViewChild("instrumentType", { static: false })
  94. public instrumentType: any;
  95. @ViewChild("casaSelect", { static: false })
  96. public casaSelect: any;
  97. constructor(
  98. private formBuilder: FormBuilder,
  99. private router: Router,
  100. private route: ActivatedRoute,
  101. private formDataService: FormInvestmentProposalService,
  102. private catalogService: CatalogsService,
  103. private investmentsService: InvestmentsService,
  104. public datepipe: DatePipe
  105. ) {}
  106. ngOnInit() {
  107. Swal.close();
  108. this.route.params.subscribe(params => {
  109. this.investmentProposalID = params["id"];
  110. });
  111. if (this.investmentProposalID == undefined)
  112. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  113. if (this.investmentProposalID != undefined) {
  114. this.investmentsService
  115. .getProposalInvestment(this.investmentProposalID)
  116. .subscribe(
  117. res => {
  118. this.gInfo = {
  119. asunto: res["result"]["asunto"],
  120. origenes_fondo:
  121. res["result"]["id_origen_fondo"] == null
  122. ? ""
  123. : res["result"]["id_origen_fondo"]["id_origen_fondo"],
  124. name: res["result"]["nombre_inversion"],
  125. //date: "",
  126. tipo_tasa: res["result"]["id_tipo_tasa"] || "",
  127. tipo_renta:
  128. res["result"]["id_tipo_renta"] == null
  129. ? ""
  130. : res["result"]["id_tipo_renta"]["id_tipo_renta"],
  131. periodicidad:
  132. res["result"]["id_periodicidad"] == null
  133. ? ""
  134. : res["result"]["id_periodicidad"]["id_periodicidad"],
  135. instrumentos:
  136. res["result"]["id_inversion_instrumento"][
  137. "id_tipo_instrumento"
  138. ]["codigo"],
  139. base_anual:
  140. res["result"]["id_tipo_base"] == null
  141. ? ""
  142. : res["result"]["id_tipo_base"]["id_tipo_base"],
  143. casa:
  144. res["result"]["id_entidad"] == null
  145. ? ""
  146. : res["result"]["id_entidad"]["id_entidad_financiera"],
  147. formato_ingreso:
  148. res["result"]["id_formato_ingreso"] == null
  149. ? ""
  150. : res["result"]["id_formato_ingreso"]["id_formato_ingreso"]
  151. };
  152. this.gInstrument =
  153. res["result"]["id_inversion_instrumento"]["instrumento"];
  154. this.gInstrument["id_inversion_instrumento"] =
  155. res["result"]["id_inversion_instrumento"][
  156. "id_inversion_instrumento"
  157. ];
  158. this.gComplement = {
  159. tipo_mercado:
  160. res["result"]["id_tipo_mercado"] == null
  161. ? ""
  162. : res["result"]["id_tipo_mercado"]["id_tipo_mercado"],
  163. emisores:
  164. res["result"]["id_tipo_emisor"] == null
  165. ? ""
  166. : res["result"]["id_tipo_emisor"]["id_tipo_emisor"],
  167. empresa:
  168. res["result"]["id_empresa"] == null
  169. ? ""
  170. : res["result"]["id_empresa"]["id_empresa"],
  171. pais:
  172. res["result"]["id_pais"] == null
  173. ? ""
  174. : res["result"]["id_pais"]["id_pais"],
  175. plazo:
  176. res["result"]["id_plazo"] == null
  177. ? ""
  178. : res["result"]["id_plazo"]["id_plazo"],
  179. operaciones:
  180. res["result"]["id_tipo_operacion"] == null
  181. ? ""
  182. : res["result"]["id_tipo_operacion"]["id_tipo_operacion"],
  183. comentarios: res["result"]["comentario"],
  184. justificacion: res["result"]["justificacion"]
  185. };
  186. this.instrumentValue =
  187. res["result"]["id_inversion_instrumento"]["id_tipo_instrumento"][
  188. "nombre"
  189. ];
  190. // Setear objeto dinamico con valores obtenidos del endpoint
  191. this.general = this.formDataService.getGeneralInfo();
  192. // Validar que el objeto dinamico contenga valores, si no, setear con valores obtenidos del endpoint
  193. if (
  194. Object.values(this.general).every(x => x === null || x === "")
  195. ) {
  196. this.formDataService.setGeneralInfo(this.gInfo);
  197. this.general = this.formDataService.getGeneralInfo();
  198. this.generalInfoDontExists = this.general == undefined;
  199. this.investmentProposalForm.patchValue({
  200. asunto: this.gInfo.asunto,
  201. origenes_fondo: this.gInfo.origenes_fondo,
  202. name: this.gInfo.name,
  203. tipo_tasa: this.gInfo.tipo_tasa,
  204. tipo_renta: this.gInfo.tipo_renta,
  205. periodicidad: this.gInfo.periodicidad,
  206. instrumentos: this.gInfo.instrumentos,
  207. base_anual: this.gInfo.base_anual,
  208. casa: this.gInfo.casa.toString(),
  209. formato_ingreso: this.gInfo.formato_ingreso
  210. });
  211. if (this.gInfo.casa != "") {
  212. this.casaSelect.select(this.gInfo.casa.toString());
  213. }
  214. }
  215. this.instrument = this.formDataService.getWork();
  216. if (
  217. this.instrument == undefined ||
  218. Object.values(this.instrument).every(x => x === null || x === "")
  219. ) {
  220. this.formDataService.setWork(this.gInstrument);
  221. this.instrument = this.formDataService.getWork();
  222. }
  223. this.complement = this.formDataService.getComplementInfo();
  224. if (
  225. Object.values(this.complement).every(x => x === null || x === "")
  226. ) {
  227. this.formDataService.setComplementInfo(this.gComplement);
  228. }
  229. },
  230. err => {
  231. Swal.fire({
  232. icon: "error",
  233. title: "Error en el servidor",
  234. text: err.message
  235. });
  236. }
  237. );
  238. } else {
  239. }
  240. //this.datepipe.transform(new Date(), "yyyy-MM-dd");
  241. this.general = this.formDataService.getGeneralInfo();
  242. this.generalInfoDontExists = false;
  243. // Obtener catalogos
  244. this.catalogService.getPaymentTerms().subscribe(res => {
  245. this.plazos = res;
  246. });
  247. this.catalogService.getFinancialEntities().subscribe(res => {
  248. this.financials = [];
  249. for (let property in res) {
  250. this.financials.push({
  251. label: res[property]["nombre"],
  252. value: res[property]["id_entidad_financiera"].toString()
  253. });
  254. }
  255. });
  256. this.catalogService.getBaseTypes().subscribe(res => {
  257. this.base_types = res;
  258. });
  259. this.catalogService.getPeriodicities().subscribe(res => {
  260. this.periodicities = res;
  261. });
  262. this.catalogService.getRateTypes().subscribe(res => {
  263. this.rates = res;
  264. });
  265. this.catalogService.getRevenueTypes().subscribe(res => {
  266. this.revenues = res;
  267. });
  268. this.catalogService.getFundsOrigins().subscribe(res => {
  269. this.funds = res;
  270. });
  271. this.catalogService.getInstrumentTypes().subscribe(res => {
  272. res;
  273. this.instrumentTypes = [];
  274. for (let property in res) {
  275. this.instrumentTypes.push({
  276. label: res[property]["nombre"],
  277. value: res[property]["codigo"]
  278. });
  279. }
  280. if (this.generalInfoDontExists == false) {
  281. if (
  282. this.instrumentType != undefined &&
  283. (this.general.instrumentos != "" ||
  284. this.general.instrumentos != undefined)
  285. ) {
  286. this.instrumentType.select(this.general.instrumentos);
  287. }
  288. this.instrumentName = res.find(
  289. e => e.codigo == this.general.instrumentos
  290. );
  291. this.instrumentName =
  292. this.instrumentName != undefined ? this.instrumentName.nombre : "-";
  293. }
  294. });
  295. //getIncomeFormat
  296. this.catalogService.getIncomeFormat().subscribe(res => {
  297. this.format_incomes = res;
  298. });
  299. // Si el arreglo principal de objetos viene con todos sus elementos vacios, inicializo el formulario.
  300. //if (Object.values(this.general).every(x => x === null || x === "")) {
  301. this.callForm(this.generalInfoDontExists, this.general);
  302. }
  303. ngOnChanges() {}
  304. save(form: any): boolean {
  305. if (!form.valid) {
  306. return false;
  307. }
  308. this.formDataService.setGeneralInfo(this.investmentProposalForm.value);
  309. return true;
  310. }
  311. callForm(formExists: boolean, form: any) {
  312. this.investmentProposalForm = this.formBuilder.group({
  313. asunto: ["", Validators.required],
  314. origenes_fondo: ["", Validators.required],
  315. name: ["", Validators.required],
  316. //date: ["", Validators.required],
  317. tipo_tasa: ["", Validators.required],
  318. tipo_renta: ["", Validators.required],
  319. periodicidad: ["", Validators.required],
  320. base_anual: ["", Validators.required],
  321. casa: ["", Validators.required],
  322. instrumentos: ["", Validators.required],
  323. formato_ingreso: ["", Validators.required]
  324. });
  325. this.investmentProposalForm.setValue({
  326. asunto: form.asunto,
  327. origenes_fondo: form.origenes_fondo || "",
  328. name: form.name || "",
  329. //date: form.date.singleDate == undefined ? "" : form.date,
  330. tipo_tasa: form.tipo_tasa || "",
  331. tipo_renta: form.tipo_renta || "",
  332. periodicidad: form.periodicidad || "",
  333. instrumentos: form.instrumentos || "",
  334. base_anual: form.base_anual || "",
  335. casa: form.casa || "",
  336. formato_ingreso: form.formato_ingreso || ""
  337. });
  338. }
  339. goToNext(form: any) {
  340. this.submitted = true;
  341. if (this.save(form)) {
  342. // Navigate to the work page
  343. if (this.investmentProposalID != undefined) {
  344. this.router.navigate(["/investment-proposal/instrument-work"], {
  345. queryParams: { id: this.investmentProposalID },
  346. relativeTo: this.route
  347. });
  348. //this.router.navigateByUrl( `/investment-proposal/${this.investmentProposalID}/instrument-work/`);
  349. } else {
  350. this.router.navigate(["/investment-proposal/instrument-work"]);
  351. }
  352. }
  353. }
  354. get f() {
  355. return this.investmentProposalForm.controls;
  356. }
  357. pre_filled_fields(event: IOption) {
  358. const selectedInstrument = event.value;
  359. switch (selectedInstrument) {
  360. case "LETE":
  361. this.investmentProposalForm.patchValue({
  362. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  363. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  364. periodicidad: this.investmentProposalForm.value.periodicidad = 7,
  365. base_anual: 6
  366. });
  367. case "CETE":
  368. this.investmentProposalForm.patchValue({
  369. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  370. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  371. periodicidad: this.investmentProposalForm.value.periodicidad = 7,
  372. base_anual: 6
  373. });
  374. break;
  375. case "VCN":
  376. this.investmentProposalForm.patchValue({
  377. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  378. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2
  379. });
  380. break;
  381. case "PBUR":
  382. this.investmentProposalForm.patchValue({
  383. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  384. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2
  385. });
  386. break;
  387. case "DAP":
  388. this.investmentProposalForm.patchValue({
  389. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  390. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  391. base_anual: 8
  392. });
  393. break;
  394. case "BONO":
  395. this.investmentProposalForm.patchValue({
  396. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  397. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  398. base_anual: 7
  399. });
  400. break;
  401. case "EURB":
  402. this.investmentProposalForm.patchValue({
  403. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  404. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  405. base_anual: 6
  406. });
  407. break;
  408. case "CINV":
  409. this.investmentProposalForm.patchValue({
  410. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  411. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  412. base_anual: 7
  413. });
  414. break;
  415. case "TIT":
  416. this.investmentProposalForm.patchValue({
  417. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  418. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  419. base_anual: 7
  420. });
  421. break;
  422. case "FINV":
  423. this.investmentProposalForm.patchValue({
  424. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  425. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  426. base_anual: 7
  427. });
  428. break;
  429. case "OPC":
  430. this.investmentProposalForm.patchValue({
  431. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 3,
  432. tipo_renta: this.investmentProposalForm.value.tipo_renta = 3,
  433. base_anual: 7
  434. });
  435. break;
  436. case "FUTU":
  437. this.investmentProposalForm.patchValue({
  438. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 3,
  439. tipo_renta: this.investmentProposalForm.value.tipo_renta = 3,
  440. base_anual: 7
  441. });
  442. break;
  443. case "PEMP":
  444. this.investmentProposalForm.patchValue({
  445. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  446. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  447. base_anual: 7
  448. });
  449. break;
  450. case "PPER":
  451. this.investmentProposalForm.patchValue({
  452. tipo_tasa: this.investmentProposalForm.value.tipo_tasa = 2,
  453. tipo_renta: this.investmentProposalForm.value.tipo_renta = 2,
  454. base_anual: 7
  455. });
  456. break;
  457. }
  458. }
  459. }