Project

General

Profile

1
import { Router } from '@angular/router';
2
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core';
3
import { DialogService } from 'primeng/dynamicdialog';
4
import {ConfirmationService, LazyLoadEvent} from 'primeng/api';
5
import { Page } from '../../../../shared/models/paging/page.interface';
6
import { InvoiceProcess } from '../../../../shared/models/invoice-process.interface';
7
import { InvoiceProcessesService } from '../../../../shared/services/invoice-processes.service';
8
import { Table } from 'primeng/table';
9
import { InvoiceProcessCriteriaInterface } from 'src/app/shared/models/invoice-process-search-criteria.interface';
10
import {AuthService} from '../../../../shared/services/auth.service';
11
import {FileViewerPopupComponent} from '../../../inbox/inbox-management/inbox-dialogs/file-viewer-popup/file-viewer-popup.component';
12
import {TranslateService} from '@ngx-translate/core';
13
import { SearchListStateService } from 'src/app/shared/back-button/search-list-state.service';
14
import { InvoiceProcessManagementComponent } from '../invoice-process-management.component';
15

    
16
@Component({
17
  selector: 'app-invoice-process-table',
18
  templateUrl: './invoice-process-table.component.html',
19
  styleUrls: ['./invoice-process-table.component.scss']
20
})
21
export class InvoiceProcessTableComponent implements OnInit, OnChanges {
22

    
23
  constructor(public dialogService: DialogService, private invoiceProcessService: InvoiceProcessesService,
24
              private router: Router, public auth: AuthService,
25
              private confirmationService: ConfirmationService,
26
              private translate: TranslateService,
27
              private searchListState: SearchListStateService) {
28
  }
29

    
30

    
31
  @Output() documentRequestOutput = new EventEmitter<InvoiceProcessCriteriaInterface>();
32
  @Input() documentRequest: InvoiceProcessCriteriaInterface;
33
  @ViewChild('pTable') pTable: Table;
34

    
35
  invoices = [];
36
  totalRecords: any;
37
  rows = 10;
38
  loading: any = true;
39
  lastPageVisit : number = 0;
40

    
41
  ngOnInit(): void {
42
    if (this.searchListState?.getLastPageVisit() == this.searchListState?.getPage()) {
43
      if (Object.keys(this.searchListState.getState()).length > 0) {
44
        this.documentRequestOutput.emit(this.searchListState.getState());
45
        this.documentRequest = this.searchListState.getState();
46
    }
47
      this.loadProcesses(this.searchListState.getEvent());
48

    
49
    }
50
  }
51

    
52
  ngOnChanges(changes: SimpleChanges): void {
53
    if (this.documentRequest && this.searchListState?.getLastPageVisit() != this.searchListState?.getPage()) {
54
      this.loadProcesses(null);
55
    }
56
  }
57

    
58
  ngAfterContentChecked(): void {
59
    if(this.pTable && this.searchListState.getLastPageVisit() == this.searchListState.getPage()){
60
      this.pTable.first = this.searchListState.getLastPageVisit() * 10;
61
      this.pTable.firstChange.emit(this.pTable.first);
62
    }
63
  }
64

    
65
  processingRedirect(document: InvoiceProcess): void {
66
    this.router.navigate(['/pages/processes/' + document.id]);
67
    console.log(this.pTable.first);
68
    this.searchListState.setLastPageVisit(this.lastPageVisit);
69
    this.searchListState.setPage(this.lastPageVisit);
70
    this.searchListState.setState(this.documentRequest);
71
  }
72

    
73
  onAssignClick(process: InvoiceProcess): void {
74
    this.confirmationService.confirm({
75
      message: this.translate.instant('ASSIGN-PROCESS-MESSAGE'),
76
      header: this.translate.instant('ASSIGN-PROCESS-CONFIRMATION'),
77
      accept: () => {
78
        this.assignToMe(process);
79
      }, reject: () => {
80

    
81
      }
82
    });
83
  }
84

    
85
  onUnassignClick(process: InvoiceProcess): void {
86
    this.confirmationService.confirm({
87
      message: this.translate.instant('UNASSIGN-PROCESS-MESSAGE'),
88
      header: this.translate.instant('UNASSIGN-PROCESS-CONFIRMATION'),
89
      accept: () => {
90
        this.unassignFromMe(process);
91
      }, reject: () => {
92

    
93
      }
94
    });
95
  }
96

    
97
  assignToMe(process: InvoiceProcess): void {
98
    this.invoiceProcessService.assignProcess(process.id, this.auth.userDetails.name).subscribe(result => {
99
      this.invoices.forEach((invoice, index) => {
100
        if (invoice.id === process.id) {
101
          this.invoices[index].userId = this.auth.userDetails.name;
102
        }
103
      });
104
    });
105
  }
106

    
107
  unassignFromMe(process: InvoiceProcess): void {
108
    this.invoiceProcessService.unassignProcess(process.id).subscribe(result => {
109
      this.invoices.forEach((invoice, index) => {
110
        if (invoice.id === process.id) {
111
          this.invoices[index].userId = null;
112
        }
113
      });
114
    });
115
  }
116

    
117
  showOrDownloadFile(document: any) {
118
    this.dialogService.open(FileViewerPopupComponent, {data: document});
119
  }
120

    
121

    
122
  loadProcesses(event: LazyLoadEvent): void {
123
    if(event && this.documentRequest){
124
    this.loading = true;
125
    this.invoiceProcessService.searchByCriteriaPaged(
126
      event.first / event.rows,
127
      event.rows,
128
      this.documentRequest,
129
      'search')
130
      .subscribe(x => {
131
        console.log(x);
132
        this.lastPageVisit = event.first / event.rows;
133
        this.searchListState.setEvent(event);
134
        this.setTableDataFromPage(x);
135
      });
136
  }else if(event == null && this.documentRequest){
137
    this.loading = true;
138
    this.invoiceProcessService.searchByCriteriaPaged(
139
      0,
140
      10,
141
      this.documentRequest,
142
      'search')
143
      .subscribe(x => {
144
        console.log(x);
145
        this.lastPageVisit = 0;
146
        this.searchListState.setEvent(null);
147
        this.setTableDataFromPage(x);
148
      });
149

    
150
  }}
151

    
152
  setTableDataFromPage(pageInvoiceProcess: Page<InvoiceProcess>): void {
153
    this.invoices = [];
154
    pageInvoiceProcess?.data?.forEach(process => {
155
      process.filesPerProcess.forEach(file => {
156
        const invoice = {
157
          receivedFile: null,
158
          id: null,
159
          documentClassificationId: null,
160
          processCreationDatetime: null,
161
          processStatus: null,
162
          userId: null,
163
        };
164
        invoice.receivedFile = file.receivedFile;
165
        invoice.id = process.id;
166
        invoice.documentClassificationId = process.documentClassificationId;
167
        invoice.processCreationDatetime = process.processCreationDatetime;
168
        invoice.processStatus = process.processStatus;
169
        invoice.userId = process.userId;
170
        this.invoices.push(invoice);
171
      });
172
    });
173
    this.totalRecords = pageInvoiceProcess.totalElements;
174
    this.loading = false;
175
  }
176

    
177

    
178
}
(4-4/4)