general-form.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. constructor(
  112. private catalogService: CatalogsService,
  113. private incomesService: IncomesService,
  114. private authService: AuthService,
  115. private formInvestmentProposal: FormInvestmentProposalService,
  116. private router: Router,
  117. private route: ActivatedRoute,
  118. private formBuilder: FormBuilder
  119. ) {
  120. const decodedToken = this.helper.decodeToken(
  121. this.authService.getJwtToken()
  122. );
  123. this.userRole = decodedToken.groups;
  124. Swal.fire({
  125. allowOutsideClick: false,
  126. icon: "info",
  127. text: "Espere por favor..."
  128. });
  129. Swal.showLoading();
  130. }
  131. nameBankAccounts(id: string) {
  132. let bank;
  133. bank = this.banks.find(e => e.id_banco == id);
  134. return bank.nombre;
  135. }
  136. ngOnInit() {
  137. this.partial = false;
  138. const formDataObj = {};
  139. this.route.queryParams.subscribe(params => {
  140. // ALT
  141. this.idInversion = params["id_inversion"];
  142. this.idInstrumentIncome = params["id_proyeccion_ingreso_instrumento"];
  143. this.projectionDate = params["fecha_proyeccion_pago"];
  144. this.idProjection = params["id_proyeccion_ingreso"];
  145. });
  146. this.incomesService
  147. .getProjection(
  148. this.idInversion,
  149. this.idInstrumentIncome,
  150. this.projectionDate,
  151. this.idProjection
  152. )
  153. .subscribe(
  154. res => {
  155. this.projection_exists = true;
  156. this.projectionRes = res["result"][0];
  157. if (this.projectionRes.estado == "CONCILIADO") {
  158. this.has_conciliate = true;
  159. }
  160. if (this.projectionRes.estado == "NO CONCILIADO") {
  161. this.cantConciliate = false;
  162. }
  163. if (this.projectionRes.estado == "EN ESPERA") {
  164. // Estado
  165. this.showIncomeForm = true;
  166. }
  167. this.investmentProposalForm.setValue({
  168. capital: this.projectionRes.capital,
  169. ingreso_bruto: this.projectionRes.ingreso_bruto,
  170. renta: this.projectionRes.renta,
  171. ingreso_neto: this.projectionRes.ingreso_neto,
  172. tipo_pago: "",
  173. cuenta_bancaria: "",
  174. comentario: "",
  175. fecha_inicio: "",
  176. fecha_vencimiento: ""
  177. });
  178. Swal.close();
  179. },
  180. err => {
  181. Swal.fire({
  182. icon: "error",
  183. title: "Error en el servidor",
  184. text: err.message
  185. });
  186. }
  187. );
  188. this.catalogService.getPaymentTypes().subscribe(res => {
  189. this.payment_types = res;
  190. });
  191. this.catalogService.getCountries().subscribe(res => {
  192. this.funds = res;
  193. });
  194. this.catalogService.getCatalogInfo("bancos").subscribe(res => {
  195. this.banks = res;
  196. this.catalogService.getCatalogInfo("cuentas-bancarias").subscribe(res => {
  197. this.accounts = res;
  198. });
  199. //this.payment_types = 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. console.log(form);
  258. console.log(form.value.ingreso_bruto)
  259. console.log(inversionBruto)
  260. if (!form.valid) {
  261. return false;
  262. }
  263. this.incomeObject = {
  264. id_inversion_instrumento: this.projectionRes.id_inversion_instrumento,
  265. id_proyeccion_ingreso_instrumento: this.idInstrumentIncome,
  266. fecha_proyeccion_pago: this.projectionDate,
  267. capital: inversionCapital,
  268. ingreso_bruto: inversionBruto,
  269. renta: form.value.renta,
  270. ingreso_neto: inversionNeto,
  271. id_cuenta_bancaria: form.value.cuenta_bancaria,
  272. id_tipo_pago: form.value.tipo_pago,
  273. comentario: form.value.comentario
  274. };
  275. if (this.idProjection != undefined) {
  276. this.incomeObject["id_proyeccion_ingreso"] = this.idProjection;
  277. }
  278. if (this.partial == true) {
  279. this.incomeObject["fecha_inicio"] =
  280. form.value.fecha_inicio.singleDate.formatted;
  281. this.incomeObject["fecha_vencimiento"] =
  282. form.value.fecha_vencimiento.singleDate.formatted;
  283. }
  284. Swal.fire({
  285. allowOutsideClick: false,
  286. icon: "info",
  287. text: "Espere por favor..."
  288. });
  289. Swal.showLoading();
  290. this.incomesService.createIncome(this.incomeObject).subscribe(
  291. success => {
  292. if (success) {
  293. Swal.fire({
  294. allowOutsideClick: false,
  295. icon: "success",
  296. showCancelButton: false,
  297. title: "Exito",
  298. confirmButtonText: "El ingreso ha sido guardado"
  299. }).then(result => {
  300. Swal.close();
  301. this.cantConciliate = false;
  302. });
  303. }
  304. },
  305. err => {
  306. Swal.fire({
  307. icon: "error",
  308. title: "Error al guardar",
  309. text: err.message
  310. });
  311. }
  312. );
  313. }
  314. clearValor(value,tipoDato){
  315. value = parseFloat(value.toString().replace(",",""))
  316. return value;
  317. }
  318. public inputValidator(event: any) {
  319. //console.log(event.target.value);
  320. const pattern = /^[0-9]*$/;
  321. //let inputChar = String.fromCharCode(event.charCode)
  322. if (!pattern.test(event.target.value)) {
  323. event.target.value = event.target.value.replace(/[^\d,.]+/g, '');
  324. // invalid character, prevent input
  325. }
  326. }
  327. conciliateIncome() {
  328. this.conciliateObject = {
  329. id_inversion_instrumento: this.projectionRes.id_inversion_instrumento,
  330. id_proyeccion_ingreso_instrumento: this.idInstrumentIncome,
  331. fecha_proyeccion_pago: this.projectionDate
  332. };
  333. if (this.idProjection != undefined) {
  334. this.conciliateObject["id_proyeccion_ingreso"] = this.idProjection;
  335. }
  336. this.incomesService.conciliateIncome(this.conciliateObject).subscribe(
  337. success => {
  338. if (success) {
  339. Swal.fire({
  340. allowOutsideClick: false,
  341. icon: "success",
  342. showCancelButton: false,
  343. title: "Exito",
  344. confirmButtonText: "El ingreso ha sido conciliado"
  345. }).then(result => {
  346. Swal.close();
  347. window.location.href = "#/investment-incomes";
  348. });
  349. }
  350. },
  351. err => {
  352. Swal.fire({
  353. icon: "error",
  354. title: "Error al guardar",
  355. text: err.message
  356. });
  357. }
  358. );
  359. }
  360. }