Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.4KB

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