Broker System for Supercredit
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

241 line
7.6KB

  1. import {Component, EventEmitter, Input, OnInit, Output, ViewChild, ViewContainerRef, ViewEncapsulation} from '@angular/core';
  2. import {
  3. CellCloseEvent,
  4. DataStateChangeEvent,
  5. GridComponent,
  6. GridDataResult,
  7. SelectableSettings,
  8. SelectionEvent
  9. } from '@progress/kendo-angular-grid';
  10. import {CompositeFilterDescriptor, SortDescriptor, toODataString} from '@progress/kendo-data-query';
  11. import {LoanSummaryService} from '../service/loan_summary.service';
  12. import {AuthService} from '../service/auth.service';
  13. import {Observable} from 'rxjs';
  14. import {Router} from '@angular/router';
  15. import {LoanModel} from '../models/loan.model';
  16. import {LoanSelectComponent} from '../loan-select/loan-select.component';
  17. import {NotificationService, Type} from '@progress/kendo-angular-notification';
  18. import {animate, style, transition, trigger} from '@angular/animations';
  19. @Component({
  20. selector: 'app-list-all-loans',
  21. templateUrl: './list-all-loans.component.html',
  22. styleUrls: ['./list-all-loans.component.scss'],
  23. encapsulation: ViewEncapsulation.Emulated,
  24. animations: [
  25. trigger('fadeIn', [
  26. transition(':enter', [
  27. style({ opacity: '0' }),
  28. animate('.5s ease-out', style({ opacity: '1'})),
  29. ]),
  30. ]),
  31. trigger('slideOutUp', [
  32. transition(':leave', [
  33. style({ opacity: '1' }),
  34. animate('.5s ease-out', style({ opacity: '0' })),
  35. ]),
  36. ]),
  37. ],
  38. })
  39. export class ListAllLoansComponent implements OnInit {
  40. public view: LoanSummaryService;
  41. public sort: Array<SortDescriptor> = [{dir: 'desc', field: 'Settlement'}];
  42. public filter: CompositeFilterDescriptor;
  43. public pageSize = 20;
  44. public skip = 0;
  45. @Output() LoanSelected: EventEmitter<LoanModel[]|LoanModel> = new EventEmitter<LoanModel[]|LoanModel>();
  46. @Output() Cancelled: EventEmitter<LoanModel[]|LoanModel> = new EventEmitter<LoanModel[]|LoanModel>();
  47. @Output() LoanClicked: EventEmitter<LoanModel> = new EventEmitter<LoanModel>();
  48. @Input() EnableExportExcel = false;
  49. @Input() EnableExportPdf = false;
  50. @Input() EnableSelectButton = false;
  51. @Input() FinishButtonText = 'Finish Select';
  52. @Input() MaxSelect = 1;
  53. @Input() Preselect: LoanModel[] = [];
  54. public SelectedLoans: LoanModel[] = [];
  55. public selectableSettings: SelectableSettings|boolean;
  56. private privateSingleSelection = true;
  57. public gridSelection: string[] = [];
  58. @ViewChild(GridComponent, { static: true }) public grid: GridComponent;
  59. @ViewChild(GridComponent, { read: ViewContainerRef }) public gridVR: ViewContainerRef;
  60. constructor(private service: LoanSummaryService,
  61. private auth: AuthService,
  62. private router: Router,
  63. private notificationService: NotificationService) { }
  64. public ngOnInit(): void {
  65. // Bind directly to the service as it is a Subject
  66. this.view = this.service;
  67. // Fetch the data with the initial state
  68. this.loadData();
  69. // selectable
  70. this.setSelectableSettings();
  71. // Preselect on Init only
  72. this.Preselect.forEach( v => {
  73. if ( v !== undefined && v !== null && v.Id !== '' ){
  74. this.AddLoanToSelection(v);
  75. this.gridSelection.unshift(v.Id);
  76. }
  77. });
  78. }
  79. @Input() set SingleSelection(single: boolean) {
  80. this.privateSingleSelection = single;
  81. this.setSelectableSettings();
  82. }
  83. get SingleSelection(): boolean{
  84. return this.privateSingleSelection;
  85. }
  86. public setSelectableSettings(): void {
  87. this.privateSingleSelection = this.MaxSelect === 1;
  88. if ( this.EnableSelectButton ){
  89. this.selectableSettings = {
  90. checkboxOnly: true,
  91. mode: this.privateSingleSelection ? 'single' : 'multiple',
  92. drag: false,
  93. };
  94. }else{
  95. this.selectableSettings = false;
  96. }
  97. }
  98. public dataStateChange({ skip, take, sort, filter }: DataStateChangeEvent): void {
  99. // Save the current state of the Grid component
  100. this.skip = skip;
  101. this.pageSize = take;
  102. this.sort = sort;
  103. this.filter = filter;
  104. // Reload the data with the new state
  105. this.loadData();
  106. // Expand the first row initially
  107. // this.grid.expandRow(0);
  108. }
  109. public filterChange(filter: CompositeFilterDescriptor): void {
  110. this.filter = filter;
  111. console.log(filter);
  112. }
  113. private loadData(): void {
  114. this.service.queryAsLoanModel({ skip: this.skip, take: this.pageSize, sort: this.sort, filter: this.filter});
  115. }
  116. private photoURL(peopleId: any): string {
  117. const url = this.auth.getUrl('avatar/') + peopleId;
  118. return 'url("' + url + '")';
  119. }
  120. public allData = (): Observable<GridDataResult> => {
  121. return this.service.queryAll({skip: 0, take: 999999, sort: this.sort, filter: this.filter});
  122. }
  123. public onCellClick(event: CellCloseEvent): void {
  124. this.LoanClicked.emit(event.dataItem);
  125. }
  126. public AddLoanToSelection(loan: LoanModel): void{
  127. const idx = this.SelectedLoans.findIndex( v => {
  128. return v.Id === loan.Id;
  129. });
  130. if ( idx === -1 ) { // not found
  131. this.SelectedLoans.unshift(loan);
  132. }
  133. }
  134. public onFinishSelection(): void{
  135. if ( this.MaxSelect === 1 ){
  136. this.LoanSelected.emit(this.SelectedLoans[0]);
  137. }else{
  138. this.LoanSelected.emit(this.SelectedLoans);
  139. }
  140. this.notifyUser('try to save');
  141. }
  142. public onCancelSelection(): void{
  143. if ( this.MaxSelect === 1 ){
  144. this.Cancelled.emit(this.SelectedLoans[0]);
  145. }else{
  146. this.Cancelled.emit(this.SelectedLoans);
  147. }
  148. this.notifyUser('cancel saving');
  149. }
  150. public RemoveFromSelection(l: LoanModel): void{
  151. this.SelectedLoans = this.SelectedLoans.filter( v => l.Id !== v.Id );
  152. this.gridSelection = this.gridSelection.filter( v => v !== l.Id );
  153. }
  154. public onSelectionChange(sel: SelectionEvent): void {
  155. // to avoid race condition, we do action after 200ms
  156. setTimeout( () => {
  157. sel.deselectedRows.forEach(v => {
  158. this.RemoveFromSelection(v.dataItem);
  159. });
  160. sel.selectedRows.forEach(v => {
  161. if ( this.SelectedLoans.length < this.MaxSelect) {
  162. this.AddLoanToSelection(v.dataItem);
  163. }else{
  164. // remove those that was added by grid selection
  165. this.gridSelection = this.gridSelection.filter( existing => existing !== v.dataItem.Id );
  166. this.notifyUser('Maximum selection reached', { style: 'warning', icon: true });
  167. }
  168. });
  169. }, 200);
  170. }
  171. public notifyUser( msg: string, lookAndFeel?: Type): void {
  172. if (lookAndFeel === undefined ){
  173. lookAndFeel = { style: 'success', icon: true };
  174. }
  175. this.notificationService.show({
  176. appendTo: this.gridVR,
  177. content: msg,
  178. cssClass: 'button-notification',
  179. animation: { type: 'slide', duration: 400 },
  180. position: { horizontal: 'right', vertical: 'top' },
  181. type: lookAndFeel,
  182. closable: false,
  183. hideAfter: 5000,
  184. });
  185. }
  186. public getMileStone( di: LoanModel) : any {
  187. let ret = [
  188. { text: 'ApplicationReceived :' + this.dateToText(di.ApplicationReceived) },
  189. { text: 'Lodgement : ' + this.dateToText(di.Lodgement)},
  190. { text: 'ValuationOrdered : ' + this.dateToText(di.ValuationOrdered)},
  191. { text: 'ValuationCompleted: ' + this.dateToText(di.ValuationCompleted)},
  192. { text: 'ConditionalApproved: ' + this.dateToText(di.ConditionalApproved)},
  193. { text: 'OutstandingReturned: ' + this.dateToText(di.OutstandingReturned)},
  194. { text: 'FormalApproved: ' + this.dateToText(di.FormalApproved)},
  195. { text: 'SettlementBooked: ' + this.dateToText(di.SettlementBooked)},
  196. { text: 'SettlementCompleted: ' + this.dateToText(di.SettlementCompleted)},
  197. ];
  198. return ret ;
  199. }
  200. private dateToText(d: Date): string {
  201. if ( !d ) {
  202. return " ----- ";
  203. }
  204. if (d.getFullYear() < 1900 ) {
  205. return "----"
  206. }else{
  207. return d.toLocaleDateString("en-US")
  208. }
  209. }
  210. }