Project

General

Profile

1
import { DocumentClassificationService } from './../../../../shared/services/document-classification.service';
2
import { Component, OnInit, Input } from '@angular/core';
3
import { FormGroup } from '@angular/forms';
4
import { InvoiceProcessesService } from '../../../../shared/services/invoice-processes.service';
5
import { FILE_TYPE } from '../../../../shared/enums/FILE_TYPE.enum';
6
import { IPowerClient } from '../../../../shared/models/ipower-client.interface';
7
import { IpowerClientsService } from '../../../../shared/services/administration/ipower-clients.service';
8
import { DocumentClassification } from '../../../../shared/models/document-classification.interface';
9
import { Observable } from 'rxjs';
10
import { ErrorHandlingService } from 'src/app/shared/services/error-handling/error-handling.service';
11
import { AuthService } from 'src/app/shared/services/auth.service';
12
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
13
import { environment } from 'src/environments/environment';
14

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

    
22
  @Input() searchCriteriaForm: FormGroup;
23

    
24
  DOCS_CLASSIFICATIONS: DocumentClassification[];
25
  suggestionsForIpowerClientCode: any;
26
  FILE_TYPE_ARRAY: { label: any; value: number }[] = Object.keys(FILE_TYPE).map((key, index) => ({ label: FILE_TYPE[key], value: index }));
27
  PROCESS_STATUS: any;
28
  iPowerClientNameSuggestions: any;
29
  iPowerClientCodeSuggestions: any;
30
  assignToSuggestions: any;
31
  private iPowerClientSuggestions: IPowerClient[];
32
  private selectedIPowerClient: IPowerClient;
33
  private selectedAssignToUsername: string;
34

    
35

    
36
  constructor(private iPowerClientsService: IpowerClientsService,
37
    private processesService: InvoiceProcessesService,
38
    private documentClassificationService: DocumentClassificationService,
39
    private errorHandlingService: ErrorHandlingService,
40
    private authService: AuthService) {
41
  }
42

    
43
  ngOnInit(): void {
44
    this.processesService.getAllStatuses().subscribe(
45
      x => {
46
        this.PROCESS_STATUS = x;
47
      }
48
    );
49

    
50
    this.documentClassificationService.getAll().subscribe(x => {
51
      this.DOCS_CLASSIFICATIONS = x;
52
    });
53

    
54
    const ipowerClientCode = this.searchCriteriaForm.get('ipowerClientCode').value;
55
    if (ipowerClientCode) {
56
      this.iPowerClientsService.getClientsByCodeDistinct(ipowerClientCode).subscribe(
57
        (values: IPowerClient[]) => {
58
          this.iPowerClientSuggestions = values;
59

    
60
          const temp: string[] = [];
61
          values.map(val => temp.push(val.clientCode));
62
          this.iPowerClientCodeSuggestions = temp;
63
          this.iPowerClientCodeSelected(ipowerClientCode);
64
        },
65
        err => console.error(err)
66
      );
67
    }
68
  }
69

    
70
  autosuggestIPowerClientCode(event) {
71

    
72
    if (event.query.length < 3) {
73
      this.iPowerClientCodeSuggestions = [];
74
      return;
75
    }
76

    
77
    // If the user has the right to Preview of Scheduling Procedure (D01), we use the endpoint that returns all iPowerClients,
78
    // otherwise, the one that checks for the user's User_Access too. Whether the user can see ANY rule has already been handled by the 'search' button.
79
    let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.D01, environment.globalRightsClientID)
80
      ? this.iPowerClientsService.getClientsByCodeOnly(event.query)
81
      : this.authService.userRights.find(rdc => USER_RIGHTS.D02.isGrantedToUser(rdc.rights)) != null ? this.iPowerClientsService.getClientsByCodeDistinct(event.query) : null;
82

    
83
    endpointToSubscribeTo.subscribe(
84
      (values: IPowerClient[]) => {
85
        let temp: string[] = [];
86
        this.iPowerClientSuggestions = values;
87
        values.map(val => temp.push(val.clientCode));
88
        this.iPowerClientCodeSuggestions = temp
89
      },
90
      err => this.errorHandlingService.showHttpResponseError(err)
91
    );
92
  }
93

    
94
  /*
95
  * Auto-suggest & Auto-complete IPower Client
96
  */
97
  autosuggestIPowerClientName(event): void {
98

    
99
    if (!event.query || event.query.length < 3) {
100
      this.iPowerClientNameSuggestions = [];
101
      return;
102
    }
103

    
104
    // If the user has the right to Preview of Scheduling Procedure (D01), we use the endpoint that returns all iPowerClients,
105
    // otherwise, the one that checks for the user's User_Access too. Whether the user can see ANY rule has already been handled by the 'search' button.
106
    let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.D01, environment.globalRightsClientID)
107
      ? this.iPowerClientsService.getClientsByNameOnly(event.query)
108
      : this.authService.userRights.find(rdc => USER_RIGHTS.D02.isGrantedToUser(rdc.rights)) != null ? this.iPowerClientsService.getClientsByNameDistinct(event.query) : null;
109

    
110
    endpointToSubscribeTo.subscribe(
111
      (values: IPowerClient[]) => {
112
        this.iPowerClientSuggestions = values;
113

    
114
        const temp: string[] = [];
115
        values.map(val => temp.push(val.name));
116
        this.iPowerClientNameSuggestions = temp;
117
      },
118
      err => this.errorHandlingService.showHttpResponseError(err)
119
    );
120
  }
121

    
122
  autosuggestAssignTo(event): void {
123

    
124
    if (!event.query || event.query.length < 3) {
125
      this.assignToSuggestions = [];
126
      return;
127
    }
128

    
129
    this.processesService.getAssignToUsersDistinct(event.query).subscribe(
130
      (values: string[]) => {
131
        this.assignToSuggestions = values;
132
      },
133
      err => console.error(err)   // TODO: Handle this via a Messaging Service
134
    );
135
  }
136

    
137
  iPowerClientNameSelected(name: string): void {
138
    this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.name === name);
139
    this.searchCriteriaForm.get('ipowerClientCode').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.clientCode : '');
140
    this.searchCriteriaForm.updateValueAndValidity();
141
  }
142

    
143
  iPowerClientCodeSelected(code: string): void {
144
    this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.clientCode === code);
145
    this.searchCriteriaForm.get('ipowerClientName').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.name : '');
146
    this.searchCriteriaForm.updateValueAndValidity();
147
  }
148

    
149
  assignToSelected(username: string): void {
150
    this.selectedAssignToUsername = this.assignToSuggestions.find(suggestion => suggestion === username);
151
    this.searchCriteriaForm.get('username').patchValue(this.selectedAssignToUsername ? this.selectedAssignToUsername : '');
152
    this.searchCriteriaForm.updateValueAndValidity();
153
  }
154

    
155
}
(4-4/4)