general-form.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import { Component, ViewChild, OnInit } from "@angular/core";
  2. import { MatPaginator } from "@angular/material/paginator";
  3. import { MatSort } from "@angular/material/sort";
  4. import { MatTableDataSource } from "@angular/material/table";
  5. import Swal from "sweetalert2";
  6. import { CatalogsService } from "src/app/services/catalogs.service";
  7. import { IncomesService } from "@app/services/incomes.service";
  8. import { AuthService } from "@app/services/auth2.service";
  9. import { JwtHelperService } from "@auth0/angular-jwt";
  10. import { InvestmentProposal } from "@app/models/investment-proposal";
  11. import { from } from "rxjs";
  12. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  13. import { Router, ActivatedRoute } from "@angular/router";
  14. import {
  15. FormBuilder,
  16. FormGroup,
  17. FormControl,
  18. FormArray,
  19. Validators
  20. } from "@angular/forms";
  21. import { IAngularMyDpOptions } from "angular-mydatepicker";
  22. @Component({
  23. selector: "app-general-form-income",
  24. templateUrl: "./general-form.component.html",
  25. styleUrls: ["./general-form.component.scss"]
  26. })
  27. export class GeneralIncomeFormComponent implements OnInit {
  28. helper = new JwtHelperService();
  29. title: string = "Ingresos para depósito a plazo";
  30. displayedColumns: string[] = [
  31. "fecha_proyeccion_ingreso",
  32. "fecha_ingreso",
  33. "fecha_conciliacion",
  34. "capital",
  35. "ingreso_bruto",
  36. "renta"
  37. ];
  38. //displayedColumns: string[] = ['state'];
  39. // For daterange
  40. daysLabels: any = {
  41. su: "Dom",
  42. mo: "Lun",
  43. tu: "Mar",
  44. we: "Mie",
  45. th: "Jue",
  46. fr: "Vie",
  47. sa: "Sab"
  48. };
  49. monthsLabels: any = {
  50. 1: "Ene",
  51. 2: "Feb",
  52. 3: "Mar",
  53. 4: "Abr",
  54. 5: "May",
  55. 6: "Jun",
  56. 7: "Jul",
  57. 8: "Ago",
  58. 9: "Sep",
  59. 10: "Oct",
  60. 11: "Nov",
  61. 12: "Dic"
  62. };
  63. myDpOptions: IAngularMyDpOptions = {
  64. dateRange: false,
  65. dateFormat: "dd/mm/yyyy",
  66. dayLabels: this.daysLabels,
  67. monthLabels: this.monthsLabels
  68. };
  69. myDateInit: boolean = true;
  70. listProposals: InvestmentProposal[];
  71. dataSource = new MatTableDataSource(this.listProposals);
  72. resultsLength = 0;
  73. isLoadingResults = true;
  74. isRateLimitReached = false;
  75. userRole: any;
  76. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  77. @ViewChild(MatSort, { static: true }) sort: MatSort;
  78. role_number: any;
  79. projectionID: string;
  80. form: FormArray;
  81. investmentProposalForm: FormGroup;
  82. proyecciones: any;
  83. proyeccionesProps = [];
  84. dataRetrieved: boolean = false;
  85. array1;
  86. array2;
  87. array3;
  88. investment: any;
  89. projection: any;
  90. projection_exists: boolean;
  91. instrumentCode: any;
  92. payment_types: any;
  93. funds: any;
  94. banks: any;
  95. accounts: any;
  96. payment_date: string = "";
  97. submitted: boolean;
  98. incomeObject: any;
  99. incomes: any;
  100. incomes_exists: boolean;
  101. idInversion: any;
  102. idInstrumentIncome: any;
  103. projectionDate: any;
  104. projectionRes: any;
  105. partial: boolean;
  106. idProjection: any;
  107. cantConciliate: boolean = true;
  108. conciliateObject: any;
  109. has_conciliate: boolean;
  110. showIncomeForm: boolean = false;
  111. accounts_origin: any;
  112. accounts_destination: any;
  113. constructor(
  114. private catalogService: CatalogsService,
  115. private incomesService: IncomesService,
  116. private authService: AuthService,
  117. private formInvestmentProposal: FormInvestmentProposalService,
  118. private router: Router,
  119. private route: ActivatedRoute,
  120. private formBuilder: FormBuilder
  121. ) {
  122. const decodedToken = this.helper.decodeToken(
  123. this.authService.getJwtToken()
  124. );
  125. this.userRole = decodedToken.groups;
  126. Swal.fire({
  127. allowOutsideClick: false,
  128. icon: "info",
  129. text: "Espere por favor..."
  130. });
  131. Swal.showLoading();
  132. }
  133. ngOnInit() {
  134. this.partial = false;
  135. const formDataObj = {};
  136. this.route.queryParams.subscribe(params => {
  137. // ALT
  138. this.idInversion = params["id_inversion"];
  139. this.idInstrumentIncome = params["id_proyeccion_ingreso_instrumento"];
  140. this.projectionDate = params["fecha_proyeccion_pago"];
  141. this.idProjection = params["id_proyeccion_ingreso"];
  142. });
  143. this.catalogService
  144. .getBankAccounts("origen", this.idInversion)
  145. .subscribe(res => {
  146. this.accounts_origin = res["result"];
  147. });
  148. this.catalogService
  149. .getBankAccounts("destino", this.idInversion)
  150. .subscribe(res => {
  151. this.accounts_destination = res["result"];
  152. });
  153. this.incomesService
  154. .getProjection(
  155. this.idInversion,
  156. this.idInstrumentIncome,
  157. this.projectionDate,
  158. this.idProjection
  159. )
  160. .subscribe(
  161. res => {
  162. this.projection_exists = true;
  163. this.projectionRes = res["result"][0];
  164. if (this.projectionRes.estado == "CONCILIADO") {
  165. this.has_conciliate = true;
  166. }
  167. if (this.projectionRes.estado == "NO CONCILIADO") {
  168. this.cantConciliate = false;
  169. }
  170. if (this.projectionRes.estado == "EN ESPERA") {
  171. // Estado
  172. this.showIncomeForm = true;
  173. }
  174. this.investmentProposalForm.setValue({
  175. capital: this.projectionRes.capital,
  176. ingreso_bruto: this.projectionRes.ingreso_bruto,
  177. renta: this.projectionRes.renta,
  178. ingreso_neto: this.projectionRes.ingreso_neto,
  179. tipo_pago: "",
  180. cuenta_bancaria: "",
  181. comentario: "",
  182. fecha_inicio: "",
  183. fecha_vencimiento: ""
  184. });
  185. Swal.close();
  186. },
  187. err => {
  188. Swal.fire({
  189. icon: "error",
  190. title: "Error en el servidor",
  191. text: err.message
  192. });
  193. }
  194. );
  195. this.catalogService.getPaymentTypes().subscribe(res => {
  196. this.payment_types = res;
  197. });
  198. this.catalogService.getCountries().subscribe(res => {
  199. this.funds = res;
  200. });
  201. this.investmentProposalForm = this.formBuilder.group({
  202. capital: [
  203. "",
  204. [
  205. Validators.required,
  206. //Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  207. Validators.pattern("^[0-9,.]+$")
  208. ]
  209. ],
  210. ingreso_bruto: [
  211. "",
  212. [
  213. Validators.required,
  214. //Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  215. Validators.pattern("^[0-9,.]+$")
  216. ]
  217. ],
  218. renta: [
  219. "",
  220. [
  221. Validators.required,
  222. //Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  223. Validators.pattern("^[0-9,.]+$")
  224. ]
  225. ],
  226. ingreso_neto: [
  227. "",
  228. [
  229. Validators.required,
  230. //Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  231. Validators.pattern("^[0-9,.]+$")
  232. ]
  233. ],
  234. tipo_pago: ["", Validators.required],
  235. cuenta_bancaria: ["", Validators.required],
  236. //fecha_ingreso: ["", Validators.required],
  237. comentario: ["", Validators.required],
  238. fecha_inicio: [""],
  239. fecha_vencimiento: [""]
  240. });
  241. }
  242. get f() {
  243. return this.investmentProposalForm.controls;
  244. }
  245. toggle = () => {
  246. if (this.partial == false) {
  247. this.partial = true;
  248. } else {
  249. this.partial = false;
  250. }
  251. };
  252. submitIncome(form: any) {
  253. this.submitted = true;
  254. let inversionCapital = this.clearValor(form.value.capital, "decimal");
  255. let inversionBruto = this.clearValor(form.value.ingreso_bruto, "decimal");
  256. let inversionNeto = this.clearValor(form.value.ingreso_neto, "decimal");
  257. if (!form.valid) {
  258. return false;
  259. }
  260. this.incomeObject = {
  261. id_inversion_instrumento: this.projectionRes.id_inversion_instrumento,
  262. id_proyeccion_ingreso_instrumento: this.idInstrumentIncome,
  263. fecha_proyeccion_pago: this.projectionDate,
  264. capital: inversionCapital,
  265. ingreso_bruto: inversionBruto,
  266. renta: form.value.renta,
  267. ingreso_neto: inversionNeto,
  268. id_cuenta_bancaria: form.value.cuenta_bancaria,
  269. id_tipo_pago: form.value.tipo_pago,
  270. comentario: form.value.comentario
  271. };
  272. if (this.idProjection != undefined) {
  273. this.incomeObject["id_proyeccion_ingreso"] = this.idProjection;
  274. }
  275. if (this.partial == true) {
  276. this.incomeObject["fecha_inicio"] =
  277. form.value.fecha_inicio.singleDate.formatted;
  278. this.incomeObject["fecha_vencimiento"] =
  279. form.value.fecha_vencimiento.singleDate.formatted;
  280. }
  281. Swal.fire({
  282. allowOutsideClick: false,
  283. icon: "info",
  284. text: "Espere por favor..."
  285. });
  286. Swal.showLoading();
  287. this.incomesService.createIncome(this.incomeObject).subscribe(
  288. success => {
  289. if (success) {
  290. Swal.fire({
  291. allowOutsideClick: false,
  292. icon: "success",
  293. showCancelButton: false,
  294. title: "Exito",
  295. confirmButtonText: "El ingreso ha sido guardado"
  296. }).then(result => {
  297. Swal.close();
  298. this.cantConciliate = false;
  299. });
  300. }
  301. },
  302. err => {
  303. Swal.fire({
  304. icon: "error",
  305. title: "Error al guardar",
  306. text: err.message
  307. });
  308. }
  309. );
  310. }
  311. clearValor(value, tipoDato) {
  312. value = parseFloat(value.toString().replace(",", ""));
  313. return value;
  314. }
  315. public inputValidator(event: any) {
  316. //console.log(event.target.value);
  317. const pattern = /^[0-9]*$/;
  318. //let inputChar = String.fromCharCode(event.charCode)
  319. if (!pattern.test(event.target.value)) {
  320. event.target.value = event.target.value.replace(/[^\d,.]+/g, "");
  321. // invalid character, prevent input
  322. }
  323. }
  324. conciliateIncome() {
  325. this.conciliateObject = {
  326. id_inversion_instrumento: this.projectionRes.id_inversion_instrumento,
  327. id_proyeccion_ingreso_instrumento: this.idInstrumentIncome,
  328. fecha_proyeccion_pago: this.projectionDate
  329. };
  330. if (this.idProjection != undefined) {
  331. this.conciliateObject["id_proyeccion_ingreso"] = this.idProjection;
  332. }
  333. this.incomesService.conciliateIncome(this.conciliateObject).subscribe(
  334. success => {
  335. if (success) {
  336. Swal.fire({
  337. allowOutsideClick: false,
  338. icon: "success",
  339. showCancelButton: false,
  340. title: "Exito",
  341. confirmButtonText: "El ingreso ha sido conciliado"
  342. }).then(result => {
  343. Swal.close();
  344. window.location.href = "#/investment-incomes";
  345. });
  346. }
  347. },
  348. err => {
  349. Swal.fire({
  350. icon: "error",
  351. title: "Error al guardar",
  352. text: err.message
  353. });
  354. }
  355. );
  356. }
  357. }