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

325 lines
10KB

  1. import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
  2. import {PayInModel} from '../models/pay-in.model';
  3. import {FormControl, FormGroup, Validators} from '@angular/forms';
  4. import {PayInListFilterModel} from '../models/pay-in-list.filter.model';
  5. import {PayInService} from '../service/pay-in.service';
  6. import {PayInListResult} from '../models/pay-in-list-result.model';
  7. import {Router} from '@angular/router';
  8. import {PopupIncomeFilterComponent} from '../popup-income-filter/popup-income-filter.component';
  9. import {GridComponent, PageChangeEvent, SortSettings} from '@progress/kendo-angular-grid';
  10. import {SortDescriptor} from '@progress/kendo-data-query';
  11. import {UploadMetaModel} from '../models/uploadMetaModel';
  12. import {debounce} from 'ts-debounce';
  13. import {LoanModel} from '../models/loan.model';
  14. import {LoanSingleService} from '../service/loan.single.service';
  15. import {PayInModelEx} from '../models/pay-in-ex.model';
  16. import {Observable} from 'rxjs';
  17. import {FunderNameService} from '../service/funder.name.service';
  18. @Component({
  19. selector: 'app-pay-in',
  20. templateUrl: './pay-in.component.html',
  21. styleUrls: ['./pay-in.component.scss']
  22. })
  23. export class PayInComponent implements OnInit {
  24. @Input() allowAddNew = true;
  25. @Input() allowEdit = true;
  26. @Input() showLoanColumn = true;
  27. @Input() showUploadColumn = true;
  28. private filterUploadMeta: UploadMetaModel = new UploadMetaModel({});
  29. public filterLoan = new LoanModel({});
  30. @Input() filter: PayInListFilterModel = new PayInListFilterModel({});
  31. @Output() errorOccurred = new EventEmitter<string>();
  32. @ViewChild('filterDialog', {static: true}) filterDialog: PopupIncomeFilterComponent;
  33. @ViewChild('grid', {static: true}) grid: GridComponent;
  34. private privateLoadDataNow = false;
  35. public gridData: PayInListResult = { data: [], total: 0};
  36. public incomeFormGroup: FormGroup;
  37. public editedRowIndex: number;
  38. public showBalance = true;
  39. public showOffsetBalance = true;
  40. @Input() public pageable = true;
  41. public gridSelection: number[] = [];
  42. public sortable: SortSettings = {
  43. mode: 'single'
  44. };
  45. public loading = false;
  46. public funderListView: Observable<string[]>;
  47. private debouncedLoadFilteredData = () => {};
  48. constructor(private pis: PayInService,
  49. private lss: LoanSingleService,
  50. private router: Router,
  51. private funderListService: FunderNameService) {
  52. this.funderListView = this.funderListService;
  53. this.funderListService.query();
  54. }
  55. ngOnInit(): void {
  56. this.debouncedLoadFilteredData = debounce(this.loadFilteredData, 500);
  57. if (this.privateLoadDataNow) { // not in delayed loading mode
  58. this.debouncedLoadFilteredData();
  59. }
  60. }
  61. public loadFilteredData(): void{
  62. this.loading = true;
  63. this.pis.getPayInList(this.filter).subscribe(
  64. ( resp: PayInListResult) => {
  65. this.gridData.total = resp.total;
  66. this.gridData.data = [];
  67. resp.data.forEach(v => {
  68. this.gridData.data.push(new PayInModelEx(v));
  69. });
  70. this.loading = false;
  71. }, err => {
  72. this.loading = false;
  73. }, () => {
  74. this.loading = false;
  75. }
  76. );
  77. }
  78. // Upload Filter
  79. @Input() set uploadMeta(value: UploadMetaModel) {
  80. this.filterUploadMeta = value;
  81. if ( value.Id <= 0 ) {
  82. this.filter.UploadIds = [];
  83. } else{
  84. this.filter.UploadIds = [ value.Id.toString() ];
  85. }
  86. this.debouncedLoadFilteredData();
  87. //console.log('upload filter changed', value, this.filter);
  88. }
  89. get uploadMeta(): UploadMetaModel {
  90. return this.filterUploadMeta;
  91. }
  92. // LoanId filter
  93. @Input() set Loan(value: LoanModel){
  94. this.filterLoan = value;
  95. if ( value.Id === '' ) {
  96. this.filter.LoanIds = [];
  97. }else{
  98. this.filter.LoanIds = [ value.Id ];
  99. }
  100. this.debouncedLoadFilteredData();
  101. // console.log('filter loanId changed', value, this.filter);
  102. }
  103. get Loan(): LoanModel {
  104. return this.filterLoan;
  105. }
  106. @Input() set LoadDataNow( value: boolean) {
  107. if ( value === true && this.privateLoadDataNow === false) {
  108. this.debouncedLoadFilteredData();
  109. }
  110. this.privateLoadDataNow = value;
  111. }
  112. get LoadDataNow(): boolean {
  113. return this.privateLoadDataNow;
  114. }
  115. public addHandler({ sender }): void {
  116. this.closeEditor(sender);
  117. const balance = -1;
  118. const offsetBalance = -1;
  119. this.showBalance = balance >= 0 ;
  120. this.showOffsetBalance = offsetBalance >= 0 ;
  121. this.incomeFormGroup = this.createFormGroup(new PayInModelEx({}));
  122. // console.log(this.incomeFormGroup);
  123. sender.addRow(this.incomeFormGroup);
  124. }
  125. private createFormGroup(dataItem: PayInModelEx): FormGroup {
  126. const Loan = this.filterLoan.Id !== '' ? this.filterLoan : dataItem.Loan;
  127. const UploadMeta = this.filterUploadMeta.Id !== 0 ? this.filterUploadMeta : dataItem.UploadMeta ;
  128. return new FormGroup({
  129. Id: new FormControl({value: dataItem.Id, disabled: true}, Validators.required),
  130. Lender: new FormControl({value: dataItem.Lender, disabled: Loan.Id !== '' }, Validators.required),
  131. Amount: new FormControl(dataItem.Amount),
  132. LoanNumber: new FormControl({value: dataItem.LoanNumber, disabled: Loan.Id !== ''}, Validators.required),
  133. IncomeAmount: new FormControl(dataItem.IncomeAmount, Validators.required),
  134. IncomeType: new FormControl(dataItem.IncomeType, Validators.required),
  135. Ts: new FormControl(dataItem.Ts, Validators.required),
  136. Balance: new FormControl(dataItem.Balance, Validators.required),
  137. OffsetBalance: new FormControl(dataItem.OffsetBalance, Validators.required),
  138. Loan: new FormControl(Loan),
  139. UploadMeta: new FormControl(UploadMeta)
  140. });
  141. }
  142. public editHandler({ sender, rowIndex, dataItem }): void {
  143. this.closeEditor(sender);
  144. this.showBalance = dataItem.Balance >= 0 ;
  145. this.showOffsetBalance = dataItem.OffsetBalance >= 0 ;
  146. this.incomeFormGroup = this.createFormGroup(dataItem);
  147. this.editedRowIndex = rowIndex;
  148. sender.editRow(rowIndex, this.incomeFormGroup);
  149. }
  150. public cancelHandler({ sender, rowIndex }): void {
  151. // console.log(sender);
  152. this.closeEditor(sender, rowIndex);
  153. }
  154. public saveHandler({ sender, rowIndex, formGroup, isNew }): void {
  155. const v = formGroup.getRawValue();
  156. if ( !this.showBalance) { v.Balance = -1; }
  157. if ( !this.showOffsetBalance) { v.OffsetBalance = -1; }
  158. const pi = this.buildPayInForSave(v, isNew);
  159. this.lss.savePayIn(pi, isNew).subscribe(
  160. (resp: PayInModelEx) => {
  161. this.Loan.cuPayIn( new PayInModel(resp));
  162. this.updatePiInGrid(new PayInModelEx(resp));
  163. },
  164. err => {
  165. // TODO: this.errorOccurred.emit('Error saving Income');
  166. }
  167. );
  168. sender.closeRow(rowIndex);
  169. }
  170. private buildPayInForSave(v: any, isNew: boolean): PayInModel {
  171. const pi = new PayInModel({});
  172. if ( isNew ) { pi.Id = 0; }
  173. // are we using filtered loan?
  174. let loan = v.Loan;
  175. if (loan === null || this.filterLoan !== undefined && this.filterLoan.Id !== '') {
  176. loan = this.filterLoan;
  177. }
  178. pi.Id = v.Id;
  179. pi.Amount = loan.Amount;
  180. pi.Balance = v.Balance;
  181. pi.Lender = loan.Id === '' ? v.Lender : loan.Lender;
  182. pi.LoanId = loan.Id;
  183. pi.LoanNumber = loan.Id === '' ? v.LoanNumber : loan.LenderLoanNumber;
  184. pi.OffsetBalance = v.OffsetBalance;
  185. pi.Settlement = loan.Settlement;
  186. pi.IncomeAmount = v.IncomeAmount;
  187. pi.IncomeType = v.IncomeType;
  188. pi.UploadId = this.filterUploadMeta.Id;
  189. pi.Ts = v.Ts;
  190. return pi;
  191. }
  192. private updatePiInGrid(pi: PayInModel): void {
  193. const notFound = this.gridData.data.every( v => {
  194. if ( v.Id === pi.Id ){
  195. Object.assign(v, pi);
  196. return false;
  197. }
  198. return true;
  199. });
  200. if ( notFound ) {
  201. this.gridData.data.unshift(pi);
  202. }
  203. }
  204. public removeHandler({ dataItem }): void {
  205. // console.log('delete', dataItem);
  206. this.Loan.PayIn = this.Loan.PayIn.filter(v => v.Id !== dataItem.Id );
  207. this.gridData.data = this.gridData.data.filter(v => v.Id !== dataItem.Id) ;
  208. this.lss.removePayIn(dataItem.Id);
  209. }
  210. private closeEditor(grid, rowIndex = this.editedRowIndex): void{
  211. grid.closeRow(rowIndex);
  212. this.editedRowIndex = undefined;
  213. this.incomeFormGroup = undefined;
  214. }
  215. public showUpload(id: number): void {
  216. this.router.navigate(['/upload-details', id]);
  217. }
  218. public setBalance(): void{
  219. if ( this.showBalance ){
  220. this.incomeFormGroup.get('Balance').setValue(0);
  221. }else{
  222. this.incomeFormGroup.get('Balance').setValue(-1);
  223. }
  224. }
  225. public setOffsetBalance(): void{
  226. if ( this.showOffsetBalance ){
  227. this.incomeFormGroup.get('OffsetBalance').setValue(0);
  228. }else{
  229. this.incomeFormGroup.get('OffsetBalance').setValue(-1);
  230. }
  231. }
  232. public showFilter(): void{
  233. this.filterDialog.show();
  234. }
  235. public doFilter(filter: PayInListFilterModel): void {
  236. this.filter.BalanceTo = filter.BalanceTo;
  237. this.filter.BalanceFrom = filter.BalanceFrom;
  238. this.filter.AmountFrom = filter.AmountFrom;
  239. this.filter.AmountTo = filter.AmountTo;
  240. this.filter.Take = filter.Take;
  241. this.filter.TrailFrom = filter.TrailFrom;
  242. this.filter.TrailTo = filter.TrailTo;
  243. this.filter.IdFrom = filter.IdFrom;
  244. this.filter.IdTo = filter.IdTo;
  245. this.filter.TsFrom = filter.TsFrom;
  246. this.filter.TsTo = filter.TsTo;
  247. this.loadFilteredData();
  248. }
  249. public pageChange(event: PageChangeEvent): void {
  250. this.filter.Skip = event.skip;
  251. this.loadFilteredData();
  252. }
  253. public sortChange(sort: SortDescriptor[]): void {
  254. this.filter.Sort = sort;
  255. this.loadFilteredData();
  256. }
  257. public onLoanChange(loan: LoanModel): void {
  258. if ( loan.Id !== '' ) {
  259. this.incomeFormGroup.get('Lender').setValue(loan.Lender);
  260. this.incomeFormGroup.get('Lender').disable({onlySelf: true, emitEvent: false});
  261. this.incomeFormGroup.get('LoanNumber').setValue(loan.LenderLoanNumber);
  262. this.incomeFormGroup.get('LoanNumber').disable({onlySelf: true, emitEvent: false});
  263. }
  264. }
  265. public ScrollTo(row: number): void {
  266. this.grid.scrollTo({ row});
  267. this.grid.focusCell(row + 1, 1 );
  268. this.gridSelection = [row];
  269. }
  270. }