Broker System for Supercredit
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

160 Zeilen
4.6KB

  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {FormControl, FormGroup, NgForm, Validators} from '@angular/forms';
  3. import {LoanModel} from '../../models/loan.model';
  4. import {LoanSingleService} from '../../service/loan.single.service';
  5. import {AuthService} from '../../service/auth.service';
  6. import {PayInModel} from '../../models/pay-in.model';
  7. import {Router} from '@angular/router';
  8. const createFormGroup = dataItem => new FormGroup({
  9. Id: new FormControl({value: dataItem.Id, disabled: true}, Validators.required),
  10. IncomeAmount : new FormControl(dataItem.IncomeAmount, Validators.required),
  11. IncomeType : new FormControl(dataItem.IncomeType, Validators.required),
  12. Ts: new FormControl(dataItem.Ts, Validators.required),
  13. Balance: new FormControl(dataItem.Balance, Validators.required),
  14. OffsetBalance: new FormControl(dataItem.OffsetBalance, Validators.required),
  15. });
  16. @Component({
  17. selector: 'app-loan-trail-income',
  18. templateUrl: './trail-income.component.html',
  19. styleUrls: ['./trail-income.component.scss']
  20. })
  21. export class TrailIncomeComponent implements OnInit {
  22. @Input() public Loan: LoanModel;
  23. @Output() public NotifyNext = new EventEmitter<boolean>();
  24. @Output() public NotifyPrev = new EventEmitter<boolean>();
  25. @Output() public errorOccurred = new EventEmitter<string>();
  26. public formGroup: FormGroup;
  27. private editedRowIndex: number;
  28. public trailIncome: FormGroup;
  29. public showBalance = true;
  30. public showOffsetBalance = true;
  31. constructor(private ls: LoanSingleService,
  32. private auth: AuthService,
  33. private router: Router) { }
  34. ngOnInit(): void {
  35. // this.Loan.PayIn
  36. }
  37. public addHandler({ sender }): void {
  38. this.closeEditor(sender);
  39. let balance = -1;
  40. let offsetBalance = -1;
  41. if ( this.Loan.PayIn.length > 0) {
  42. const idx = this.Loan.PayIn.length - 1;
  43. balance = this.Loan.PayIn[idx].Balance;
  44. offsetBalance = this.Loan.PayIn[idx].OffsetBalance;
  45. }
  46. this.showBalance = balance >= 0 ;
  47. this.showOffsetBalance = offsetBalance >= 0 ;
  48. this.formGroup = createFormGroup({
  49. Id: 0,
  50. IncomeAmount: 168,
  51. IncomeType: 'Trail',
  52. Ts: new Date(),
  53. Balance: balance,
  54. OffsetBalance: offsetBalance,
  55. });
  56. sender.addRow(this.formGroup);
  57. }
  58. public editHandler({ sender, rowIndex, dataItem }): void {
  59. this.closeEditor(sender);
  60. this.showBalance = dataItem.Balance >= 0 ;
  61. this.showOffsetBalance = dataItem.OffsetBalance >= 0 ;
  62. this.formGroup = createFormGroup(dataItem);
  63. this.editedRowIndex = rowIndex;
  64. sender.editRow(rowIndex, this.formGroup);
  65. }
  66. public cancelHandler({ sender, rowIndex }): void {
  67. console.log(sender);
  68. this.closeEditor(sender, rowIndex);
  69. }
  70. public saveHandler({ sender, rowIndex, formGroup, isNew }): void {
  71. const v = formGroup.getRawValue();
  72. if ( this.Loan.Lender === '') {
  73. this.errorOccurred.emit('Lender should not be empty');
  74. return;
  75. }
  76. if ( this.Loan.LenderLoanNumber === '') {
  77. this.errorOccurred.emit('Lender Identification should not be empty');
  78. return;
  79. }
  80. if ( !this.showBalance) { v.Balance = -1; }
  81. if ( !this.showOffsetBalance) { v.OffsetBalance = -1; }
  82. const pi = new PayInModel({});
  83. pi.Id = v.Id;
  84. pi.Amount = this.Loan.Amount;
  85. pi.Balance = v.Balance;
  86. pi.Lender = this.Loan.Lender;
  87. pi.LoanId = this.Loan.Id;
  88. pi.LoanNumber = this.Loan.LenderLoanNumber;
  89. pi.OffsetBalance = v.OffsetBalance;
  90. pi.Settlement = this.Loan.Settlement;
  91. pi.IncomeAmount = v.IncomeAmount;
  92. pi.IncomeType = v.IncomeType;
  93. pi.Ts = new Date(v.Ts);
  94. pi.UploadId = v.UploadId;
  95. console.log('saving PayIn', pi);
  96. this.ls.savePayIn(pi, isNew).subscribe(
  97. (resp: PayInModel) => {
  98. this.Loan.cuPayIn(resp);
  99. },
  100. err => {
  101. this.errorOccurred.emit('Error saving Income');
  102. }
  103. );
  104. sender.closeRow(rowIndex);
  105. }
  106. public removeHandler({ dataItem }): void {
  107. console.log(dataItem);
  108. this.Loan.PayIn = this.Loan.PayIn.filter(v => {
  109. return v.Id !== dataItem.Id;
  110. });
  111. this.ls.removePayIn(dataItem.Id);
  112. }
  113. private closeEditor(grid, rowIndex = this.editedRowIndex): void{
  114. grid.closeRow(rowIndex);
  115. this.editedRowIndex = undefined;
  116. this.formGroup = undefined;
  117. }
  118. public next(trailIncome: NgForm): void{
  119. this.NotifyNext.emit(true);
  120. this.router.navigate(['./list-all-loans']);
  121. }
  122. public prev(): void {
  123. this.NotifyPrev.emit(true);
  124. }
  125. public visitUploads(pi: PayInModel): void {
  126. this.router.navigate(['./uploads/' + pi.UploadId]);
  127. }
  128. }