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.

83 lines
2.6KB

  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {UploadAnalysisModel} from '../../models/upload.analysis.model';
  3. import {PayInModel} from '../../models/pay-in.model';
  4. import {CellCloseEvent} from '@progress/kendo-angular-grid';
  5. import {PayInConnectiveModel} from '../../models/pay-in-connective.model';
  6. import {DataResult, GroupDescriptor, process} from '@progress/kendo-data-query';
  7. @Component({
  8. selector: 'app-lender-connective-income',
  9. templateUrl: './lender-connective-income.component.html',
  10. styleUrls: ['./lender-connective-income.component.scss']
  11. })
  12. export class LenderConnectiveIncomeComponent implements OnInit {
  13. private Lender = 'Connective';
  14. @Input() public analysis = new UploadAnalysisModel({});
  15. @Input() newPayInUpdate: PayInModel;
  16. @Output() Selected: EventEmitter<PayInModel> = new EventEmitter<PayInModel>();
  17. public data: PayInConnectiveModel[] = [];
  18. private payInCandidates: PayInModel[] = [];
  19. public groups: GroupDescriptor[] = [{ field: 'Lender' }];
  20. public gridView: DataResult;
  21. constructor() { }
  22. ngOnInit(): void {
  23. this.analysis.Connective.forEach(v => {
  24. v.matchedPayIn = this.matchPayIn(v);
  25. this.data.push(new PayInConnectiveModel(v));
  26. });
  27. this.gridView = process(this.data, { group: this.groups });
  28. }
  29. ngOnChanges(changes): void { // Todo: build super class for common features
  30. if (changes.newPayInUpdate){
  31. const piNew = changes.newPayInUpdate.currentValue as PayInModel;
  32. this.data.forEach(v => {
  33. // remove old matches
  34. if (v.matchedPayIn !== -1 && v.matchedPayIn.Id === piNew.Id){
  35. v.matchedPayIn = -1;
  36. }
  37. // add new matches
  38. if ( this.isMatch(v, changes.newPayInUpdate.currentValue) ){
  39. v.matchedPayIn = changes.newPayInUpdate.currentValue;
  40. }
  41. });
  42. }
  43. }
  44. public onCellClick(event: CellCloseEvent): void{
  45. this.Selected.emit(event.dataItem.asPayIn());
  46. }
  47. private matchPayIn(v: PayInConnectiveModel): PayInModel | -1 {
  48. const idx = this.payInCandidates.findIndex( pi => this.isMatch(v, pi) );
  49. if ( idx === -1 ) { return -1; }
  50. return this.payInCandidates[idx];
  51. }
  52. private isMatch( v: PayInConnectiveModel, pi: PayInModel): boolean {
  53. if ( v === null || pi === null ){
  54. return false;
  55. }
  56. return v.isMatch(pi);
  57. }
  58. @Input() set payIn(pis: PayInModel[]) {
  59. this.payInCandidates = [];
  60. pis.forEach( v => this.payInCandidates.push(new PayInModel(v)));
  61. this.data.forEach( v => {
  62. v.matchedPayIn = this.matchPayIn(v);
  63. });
  64. }
  65. get PayIn(): PayInModel[] {
  66. return this.payInCandidates;
  67. }
  68. }