Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
- import { ActivatedRoute } from '@angular/router';
- import { TranslateService } from '@ngx-translate/core';
- import { CollaboratorAutoCompleteService, PerimeterAutoCompleteService } from 'app/pages/services/auto-complete.service';
- import { TrackingStatusService } from 'app/pages/services/tracking-status.service';
- import { ICollaboratorEventTracking } from 'app/shared/model/collaborator-event-tracking.model';
- import { ICollaborator } from 'app/shared/model/collaborator.model';
- import { TypeRelation } from 'app/shared/model/enumerations/type-relation.model';
- import { IPerimeter } from 'app/shared/model/perimeter.model';
- import { ITrackingDefinition } from 'app/shared/model/tracking-definition.model';
- import { Pagination } from 'app/shared/util/pagination-util';
- import { ChartData } from 'chart.js';
- import * as moment from 'moment';
- import { SelectItem } from 'primeng/api';
- import { UIChart } from 'primeng/chart';
- import { Subscription } from 'rxjs';
- interface ISearchCriteria {
- trackingDefinition: ITrackingDefinition;
- collaborator: ICollaborator;
- perimeter: IPerimeter;
- withSubPerimeters: boolean;
- responsible: ICollaborator;
- relationType: TypeRelation;
- startContractDateAfter: Date;
- startContractDateBefore: Date;
- endContractDateAfter: Date;
- endContractDateBefore: Date;
- valid: boolean[];
- }
- @Component({
- selector: 'jhi-trackings',
- templateUrl: './trackings.component.html',
- styleUrls: ['./trackings.scss']
- })
- export class TrackingsComponent implements OnInit, AfterViewInit, OnDestroy {
- searchCriteria: ISearchCriteria;
- currentSearchCriteria: ISearchCriteria;
- trackingPagination = new Pagination<ICollaboratorEventTracking>();
- enumTypeRelation: SelectItem[] = [
- { value: TypeRelation.HIERARCHICAL, label: TypeRelation.HIERARCHICAL },
- { value: TypeRelation.OPERATIONAL, label: TypeRelation.OPERATIONAL }
- ];
- chartData: ChartData = {
- labels: ['Valides', 'Invalides', 'Alertes']
- };
- @ViewChild('chart', { static: false })
- chart: UIChart;
- exporting = false;
- translationSubscription: Subscription;
- constructor(
- private activatedRoute: ActivatedRoute,
- private trackingStatusService: TrackingStatusService,
- public collaboratorAutoComplete: CollaboratorAutoCompleteService,
- public perimeterAutoComplete: PerimeterAutoCompleteService,
- public translateService: TranslateService
- ) {}
- ngOnInit() {
- this.trackingPagination.itemsPerPage = 10;
- this.trackingPagination.sort = 'limitDate';
- this.trackingPagination.order = 1;
- this.translationSubscription = this.translateService
- .stream('wfmanagementApp.TypeRelation')
- .subscribe(val => this.enumTypeRelation.forEach(item => (item.label = val[item.value])));
- this.reset();
- this.activatedRoute.data.subscribe(({ trackingDefinition }) => {
- this.searchCriteria.trackingDefinition = trackingDefinition;
- this.reset();
- this.search();
- });
- }
- ngAfterViewInit() {
- this.translateService.stream('wfmanagementApp.tracking.trackings').subscribe(map => {
- this.chartData.labels[0] = map['valids'];
- this.chartData.labels[1] = map['invalids'];
- this.chartData.labels[2] = map['alerts'];
- this.chart.refresh();
- });
- }
- search() {
- this.currentSearchCriteria = Object.assign({}, this.searchCriteria);
- this.trackingPagination.first = 0;
- this.lazyLoadStatus();
- }
- reset() {
- this.searchCriteria = {
- trackingDefinition: this.searchCriteria ? this.searchCriteria.trackingDefinition : null,
- collaborator: null,
- perimeter: null,
- withSubPerimeters: true,
- responsible: null,
- relationType: null,
- startContractDateAfter: null,
- startContractDateBefore: new Date(),
- endContractDateAfter: new Date(),
- endContractDateBefore: null,
- valid: [true, false]
- };
- }
- clearCollaboratorCriteria() {
- setTimeout(() => (this.searchCriteria.collaborator = null));
- }
- clearResponsibleCriteria() {
- setTimeout(() => (this.searchCriteria.responsible = null));
- }
- clearPerimeterCriteria() {
- setTimeout(() => (this.searchCriteria.perimeter = null));
- }
- lazyLoadStatus(event?: any) {
- if (this.currentSearchCriteria.trackingDefinition) {
- this.trackingPagination.load(event, options => {
- return this.trackingStatusService.search(
- this.currentSearchCriteria.trackingDefinition.id,
- this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
- this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
- this.currentSearchCriteria.withSubPerimeters,
- undefined,
- this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
- this.currentSearchCriteria.relationType,
- this.currentSearchCriteria.startContractDateAfter,
- this.currentSearchCriteria.startContractDateBefore,
- this.currentSearchCriteria.endContractDateAfter,
- this.currentSearchCriteria.endContractDateBefore,
- this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1
- ? this.currentSearchCriteria.valid[0]
- : undefined,
- options
- );
- });
- // If it is not a table event (sort or page)
- if (!event) {
- this.trackingStatusService
- .summary(
- this.currentSearchCriteria.trackingDefinition.id,
- this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
- this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
- this.currentSearchCriteria.withSubPerimeters,
- undefined,
- this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
- this.currentSearchCriteria.relationType,
- this.currentSearchCriteria.startContractDateAfter,
- this.currentSearchCriteria.startContractDateBefore,
- this.currentSearchCriteria.endContractDateAfter,
- this.currentSearchCriteria.endContractDateBefore,
- this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1
- ? this.currentSearchCriteria.valid[0]
- : undefined
- )
- .subscribe(result => {
- const summary = result.body;
- this.chartData.datasets = [
- {
- data: [summary.total - summary.invalidNoAlert - summary.alert, summary.invalidNoAlert, summary.alert],
- backgroundColor: ['#b2be3a', '#ffca28', '#d64b13'],
- hoverBackgroundColor: ['#b2be3a', '#ffca28', '#d64b13']
- }
- ];
- this.chart.refresh();
- });
- }
- }
- }
- public dateIsNullOrAfterNow(dateStr) {
- if (!dateStr) {
- return true;
- }
- const now = moment();
- const date = moment(dateStr);
- return date.isSameOrAfter(now, 'day');
- }
- public exportCSV() {
- this.exporting = true;
- const options = {
- page: 0,
- size: this.trackingPagination.totalItems,
- sort: this.trackingPagination.sortRequest()
- };
- this.trackingStatusService
- .search(
- this.currentSearchCriteria.trackingDefinition.id,
- this.currentSearchCriteria.collaborator ? this.currentSearchCriteria.collaborator.id : undefined,
- this.currentSearchCriteria.perimeter ? this.currentSearchCriteria.perimeter.id : undefined,
- this.currentSearchCriteria.withSubPerimeters,
- undefined,
- this.currentSearchCriteria.responsible ? this.currentSearchCriteria.responsible.id : undefined,
- this.currentSearchCriteria.relationType,
- this.currentSearchCriteria.startContractDateAfter,
- this.currentSearchCriteria.startContractDateBefore,
- this.currentSearchCriteria.endContractDateAfter,
- this.currentSearchCriteria.endContractDateBefore,
- this.currentSearchCriteria.valid && this.currentSearchCriteria.valid.length === 1 ? this.currentSearchCriteria.valid[0] : undefined,
- options
- )
- .subscribe(result => this.exportDataCSV(result.body), () => (this.exporting = false));
- }
- private exportDataCSV(trackings: ICollaboratorEventTracking[]) {
- try {
- const separator = ';';
- let csv = '\ufeff';
- csv += this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.serialNumber'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.firstName'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.lastName'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.email'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.lastDate'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.limitDate'));
- csv += separator + this.csvData(this.translateService.instant('wfmanagementApp.tracking.trackings.plannedDate'));
- csv += '\r\n';
- trackings.forEach(tracking => {
- csv += this.csvData(tracking.collaborator.serialNumber || '');
- csv += separator + this.csvData(tracking.collaborator.firstName || '');
- csv += separator + this.csvData(tracking.collaborator.lastName || '');
- csv += separator + this.csvData(tracking.collaborator.email || '');
- csv +=
- separator +
- this.csvData(
- tracking.lastRealizedEvent ? tracking.lastRealizedEvent.date.locale(this.translateService.currentLang).format('YYYY-MM-DD') : ''
- );
- csv +=
- separator +
- this.csvData(tracking.limitDate ? tracking.limitDate.locale(this.translateService.currentLang).format('YYYY-MM-DD') : '');
- csv +=
- separator +
- this.csvData(
- tracking.plannedEvent ? tracking.plannedEvent.date.locale(this.translateService.currentLang).format('YYYY-MM-DD') : ''
- );
- csv += '\r\n';
- });
- const blob = new Blob([csv], {
- type: 'text/csv;charset=utf-8;'
- });
- if (window.navigator.msSaveOrOpenBlob) {
- navigator.msSaveOrOpenBlob(blob, 'trackings.csv');
- } else {
- const link = document.createElement('a');
- link.style.display = 'none';
- document.body.appendChild(link);
- if (link.download !== undefined) {
- link.setAttribute('href', URL.createObjectURL(blob));
- link.setAttribute('download', 'trackings.csv');
- link.click();
- } else {
- csv = 'data:text/csv;charset=utf-8,' + csv;
- window.open(encodeURI(csv));
- }
- document.body.removeChild(link);
- }
- } finally {
- this.exporting = false;
- }
- }
- private csvData(value): string {
- return '"' + String(value).replace(/"/g, '""') + '"';
- }
- ngOnDestroy() {
- if (this.translationSubscription) {
- this.translationSubscription.unsubscribe();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement