From 9db23774d85d95899263dc1b365f06ec1df726af Mon Sep 17 00:00:00 2001
From: Nestor <npegane@hotmail.com>
Date: Fri, 8 Jun 2018 17:07:36 +0200
Subject: [PATCH] FEAT #7739 Merged batch history with normal history

---
 .../history-administration.component.html     | 177 ++++++++++++------
 .../history-administration.component.ts       | 118 +++++++++++-
 src/app/group/models/ServiceModelAbstract.php |  10 +-
 3 files changed, 236 insertions(+), 69 deletions(-)
 mode change 100644 => 100755 apps/maarch_entreprise/Views/history-administration.component.html
 mode change 100644 => 100755 apps/maarch_entreprise/js/angular/app/administration/history-administration.component.ts
 mode change 100644 => 100755 src/app/group/models/ServiceModelAbstract.php

diff --git a/apps/maarch_entreprise/Views/history-administration.component.html b/apps/maarch_entreprise/Views/history-administration.component.html
old mode 100644
new mode 100755
index 1d95d10beac..8b40c2e57e4
--- a/apps/maarch_entreprise/Views/history-administration.component.html
+++ b/apps/maarch_entreprise/Views/history-administration.component.html
@@ -14,68 +14,129 @@
             [opened]="mobileQuery.matches ? false : true">
             <menu-nav></menu-nav>
             <mat-divider></mat-divider>
-        </mat-sidenav>
+        </mat-sidenav>        
         <mat-sidenav-content>
             <div *ngIf="loading" style="display:flex;height:100%;">
                 <mat-spinner style="margin:auto;"></mat-spinner>
             </div>
-            <mat-card *ngIf="!loading" class="card-app-content">
-                <div class="alert alert-warning" role="alert" [innerHTML]="lang.limitDataReached" *ngIf="limitExceeded">
-                </div>
-                <div class="row">
-                    <div class="col-md-6 col-xs-6">
-                        <mat-form-field>
-                            <input matInput (keyup)="applyFilter($event.target.value)" placeholder="{{lang.filterBy}}">
-                        </mat-form-field>
-                    </div>
-                    <div class="col-md-6 col-xs-6">
-                        <mat-paginator #paginator [length]="100" [pageSize]="10">
-                        </mat-paginator>
-                    </div>
-                </div>
-
-                <div class="row">
-                    <div class="col-sm-6" style="padding-top:10px;">
-                        <mat-form-field>
-                            <input matInput [(ngModel)]="startDate" [max]="endDate" [matDatepicker]="pickerStart" placeholder="{{lang.since}}" (dateChange)="refreshHistory()" (focus)="pickerStart.open()">
-                            <mat-datepicker-toggle matSuffix [for]="pickerStart"></mat-datepicker-toggle>
-                            <mat-datepicker #pickerStart touchUi="true" [startAt]="startDate"></mat-datepicker>
-                        </mat-form-field>
-                        
-                    </div>
-                    <div class="col-sm-6" style="padding-top:10px;">
-                        <mat-form-field>
-                            <input matInput [(ngModel)]="endDate" [min]="startDate" [matDatepicker]="pickerEnd" placeholder="{{lang.until}}" (dateChange)="refreshHistory()" (focus)="pickerEnd.open()">
-                            <mat-datepicker-toggle matSuffix [for]="pickerEnd"></mat-datepicker-toggle>
-                            <mat-datepicker #pickerEnd touchUi="true" [startAt]="endDate"></mat-datepicker>
-                        </mat-form-field>
-                    </div>
-                </div>
-                <mat-table #table [dataSource]="dataSource" matSort matSortActive="event_date" matSortDirection="desc">
-                    <ng-container matColumnDef="event_date">
-                        <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;">{{lang.date}}</mat-header-cell>
-                        <mat-cell *matCellDef="let element" style="flex:1;">{{element.event_date | date : "dd/MM/y HH:mm"}}</mat-cell>
-                    </ng-container>
-                    <ng-container matColumnDef="event_type">
-                        <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.event}}</mat-header-cell>
-                        <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.event_type}} </mat-cell>
-                    </ng-container>
-                    <ng-container matColumnDef="user_id">
-                        <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.user}}</mat-header-cell>
-                        <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.user_id}} </mat-cell>
-                    </ng-container>
-                    <ng-container matColumnDef="info">
-                        <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:3;">{{lang.description}}</mat-header-cell>
-                        <mat-cell *matCellDef="let element" style="flex:3;"> {{element.info}} </mat-cell>
-                    </ng-container>
-                    <ng-container matColumnDef="remote_ip">
-                        <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.ip}}</mat-header-cell>
-                        <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.remote_ip}} </mat-cell>
-                    </ng-container>
-                    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
-                    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
-                </mat-table>                
-            </mat-card>
+            <mat-tab-group (selectedIndexChange)="_setDataSource($event)">
+                    <mat-tab label="{{lang.history}}" *ngIf="accessHistory">
+                            <mat-card *ngIf="!loading" class="card-app-content">
+                                    <div class="alert alert-warning" role="alert" [innerHTML]="lang.limitDataReached" *ngIf="limitExceeded">
+                                    </div>
+                                    <div class="row">
+                                        <div class="col-md-6 col-xs-6">
+                                            <mat-form-field>
+                                                <input matInput (keyup)="applyFilter($event.target.value, 'normal')" placeholder="{{lang.filterBy}}">
+                                            </mat-form-field>
+                                        </div>
+                                        <div class="col-md-6 col-xs-6">
+                                            <mat-paginator #paginator [length]="100" [pageSize]="10">
+                                            </mat-paginator>
+                                        </div>
+                                    </div>
+                    
+                                    <div class="row">
+                                        <div class="col-sm-6" style="padding-top:10px;">
+                                            <mat-form-field>
+                                                <input matInput [(ngModel)]="startDate" [max]="endDate" [matDatepicker]="pickerStart" placeholder="{{lang.since}}" (dateChange)="refreshHistory('normal')" (focus)="pickerStart.open()">
+                                                <mat-datepicker-toggle matSuffix [for]="pickerStart"></mat-datepicker-toggle>
+                                                <mat-datepicker #pickerStart touchUi="true" [startAt]="startDate"></mat-datepicker>
+                                            </mat-form-field>
+                                            
+                                        </div>
+                                        <div class="col-sm-6" style="padding-top:10px;">
+                                            <mat-form-field>
+                                                <input matInput [(ngModel)]="endDate" [min]="startDate" [matDatepicker]="pickerEnd" placeholder="{{lang.until}}" (dateChange)="refreshHistory('normal')" (focus)="pickerEnd.open()">
+                                                <mat-datepicker-toggle matSuffix [for]="pickerEnd"></mat-datepicker-toggle>
+                                                <mat-datepicker #pickerEnd touchUi="true" [startAt]="endDate"></mat-datepicker>
+                                            </mat-form-field>
+                                        </div>
+                                    </div>
+                                    <mat-table #table [dataSource]="dataSource" matSort matSortActive="event_date" #sort="matSort" matSortDirection="desc">
+                                        <ng-container matColumnDef="event_date">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;">{{lang.date}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" style="flex:1;">{{element.event_date | date : "dd/MM/y HH:mm"}}</mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="event_type">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.event}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.event_type}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="user_id">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.user}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.user_id}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="info">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:3;">{{lang.description}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" style="flex:3;"> {{element.info}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="remote_ip">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches">{{lang.ip}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" style="flex:1;" [class.hide-for-mobile]="mobileQuery.matches"> {{element.remote_ip}} </mat-cell>
+                                        </ng-container>
+                                        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
+                                        <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
+                                    </mat-table>                
+                                </mat-card>
+                    </mat-tab>
+                    <mat-tab label="{{lang.historyBatch}}" *ngIf="accessBatchHistory">
+                            <mat-card *ngIf="!loading" class="card-app-content">
+                                    <div class="alert alert-warning" role="alert" [innerHTML]="lang.limitDataReached" *ngIf="limitExceeded"></div>
+                                    <div class="row">
+                                        <div class="col-md-6 col-xs-6">
+                                            <mat-form-field>
+                                                <input matInput (keyup)="applyFilter($event.target.value, 'batch')" placeholder="{{lang.filterBy}}">
+                                            </mat-form-field>
+                                        </div>
+                                        <div class="col-md-6 col-xs-6">
+                                            <mat-paginator #batchPaginator [length]="100" [pageSize]="10">
+                                            </mat-paginator>
+                                        </div>
+                                    </div>
+                                    <div class="row">
+                                        <div class="col-sm-6" style="padding-top:10px;">
+                                            <mat-form-field>
+                                                <input matInput [(ngModel)]="batchStartDate" [max]="batchEndDate" [matDatepicker]="pickerStart" placeholder="{{lang.since}}" (dateChange)="refreshHistory('batch')" (focus)="pickerStart.open()">
+                                                <mat-datepicker-toggle matSuffix [for]="pickerStart"></mat-datepicker-toggle>
+                                                <mat-datepicker #pickerStart touchUi="true" [startAt]="batchStartDate"></mat-datepicker>
+                                            </mat-form-field>       
+                                        </div>
+                                        <div class="col-sm-6" style="padding-top:10px;">
+                                            <mat-form-field>
+                                                <input matInput [(ngModel)]="batchEndDate" [min]="batchStartDate" [matDatepicker]="pickerEnd" placeholder="{{lang.until}}" (dateChange)="refreshHistory('batch')" (focus)="pickerEnd.open()">
+                                                <mat-datepicker-toggle matSuffix [for]="pickerEnd"></mat-datepicker-toggle>
+                                                <mat-datepicker #pickerEnd touchUi="true" [startAt]="batchEndDate"></mat-datepicker>
+                                            </mat-form-field>
+                                        </div>
+                                    </div>
+                                    <mat-table #table [dataSource]="batchDataSource" #batchSort="matSort" matSort matSortActive="event_date" matSortDirection="desc">
+                                        <ng-container matColumnDef="event_date">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header>{{lang.date}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element">{{element.event_date | date : "dd/MM/y HH:mm"}}</mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="total_processed">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header [class.hide-for-mobile]="mobileQuery.matches">{{lang.totalProcessed}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" [class.hide-for-mobile]="mobileQuery.matches"> {{element.total_processed}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="total_errors">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header [class.hide-for-mobile]="mobileQuery.matches">{{lang.totalErrors}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" [class.hide-for-mobile]="mobileQuery.matches"> {{element.total_errors}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="info">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header>{{lang.description}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element"> {{element.info}} </mat-cell>
+                                        </ng-container>
+                                        <ng-container matColumnDef="module_name">
+                                            <mat-header-cell *matHeaderCellDef mat-sort-header [class.hide-for-mobile]="mobileQuery.matches">{{lang.module}}</mat-header-cell>
+                                            <mat-cell *matCellDef="let element" [class.hide-for-mobile]="mobileQuery.matches"> {{element.module_name}} </mat-cell>
+                                        </ng-container>
+                                        <mat-header-row *matHeaderRowDef="batchDisplayedColumns"></mat-header-row>
+                                        <mat-row *matRowDef="let row; columns: batchDisplayedColumns;"></mat-row>
+                                    </mat-table>                
+                                </mat-card>                        
+                    </mat-tab>
+                </mat-tab-group>
+            
         </mat-sidenav-content>
         <mat-sidenav #snav2 [mode]="mobileQuery.matches ? 'over' : 'side'" [fixedInViewport]="mobileQuery.matches" fixedTopGap="56"
             position='end' [opened]="mobileQuery.matches ? false : false">
diff --git a/apps/maarch_entreprise/js/angular/app/administration/history-administration.component.ts b/apps/maarch_entreprise/js/angular/app/administration/history-administration.component.ts
old mode 100644
new mode 100755
index 53b9c4fc752..4e6ecb77c4b
--- a/apps/maarch_entreprise/js/angular/app/administration/history-administration.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/administration/history-administration.component.ts
@@ -21,22 +21,41 @@ export class HistoryAdministrationComponent implements OnInit {
     coreUrl                         : string;
     lang                            : any       = LANG;
     loading                         : boolean   = false;
+    loading1                        : boolean   = false;
+    loading2                        : boolean   = false;
     limitExceeded                   : boolean   = false;
+    batchLimitExceeded              : boolean   = false;
 
     data                            : any[]     = [];
+    batchData                       : any[]     = [];
     startDate                       : Date      = new Date();
     endDate                         : Date      = new Date();
+    batchStartDate                  : Date      = new Date();
+    batchEndDate                    : Date      = new Date();
+
 
     dataSource          = new MatTableDataSource(this.data);
+    batchDataSource     = new MatTableDataSource(this.batchData);
     displayedColumns    = ['event_date', 'event_type', 'user_id', 'info', 'remote_ip'];
+    batchDisplayedColumns = ['event_date', 'total_processed', 'total_errors', 'info', 'module_name'];
+
+    accessBatchHistory              : boolean   = true;
+    accessHistory                   : boolean   = true;
 
 
-    @ViewChild(MatPaginator) paginator: MatPaginator;
-    @ViewChild(MatSort) sort: MatSort;
-    applyFilter(filterValue: string) {
+    @ViewChild('paginator') paginator: MatPaginator;
+    @ViewChild('batchPaginator') batchPaginator: MatPaginator;
+    @ViewChild('sort') sort: MatSort;
+    @ViewChild('batchSort') batchSort: MatSort;
+    applyFilter(filterValue: string, historyType : string) {
+        console.log(historyType);
         filterValue = filterValue.trim();
         filterValue = filterValue.toLowerCase();
-        this.dataSource.filter = filterValue;
+        if(historyType == 'normal'){
+            this.dataSource.filter = filterValue;
+        } else {
+            this.batchDataSource.filter = filterValue
+        }        
     }
 
     constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher, public http: HttpClient) {
@@ -46,6 +65,10 @@ export class HistoryAdministrationComponent implements OnInit {
         this.startDate.setMonth(this.endDate.getMonth()-1);
         this.endDate.setHours(23,59,59,59);
 
+        this.batchStartDate.setHours(0,0,0,0);
+        this.batchStartDate.setMonth(this.endDate.getMonth()-1);
+        this.batchEndDate.setHours(23,59,59,59);
+
         this.mobileQuery = media.matchMedia('(max-width: 768px)');
         this._mobileQueryListener = () => changeDetectorRef.detectChanges();
         this.mobileQuery.addListener(this._mobileQueryListener);
@@ -65,20 +88,70 @@ export class HistoryAdministrationComponent implements OnInit {
                 this.limitExceeded = data['limitExceeded'];
                 this.loading = false;
                 setTimeout(() => {
+                    this.accessHistory = true;
                     this.dataSource = new MatTableDataSource(this.data);
                     this.dataSource.paginator = this.paginator;
                     this.dataSource.sort = this.sort;
                 }, 0);
-            }, () => {
-                location.href = "index.php";
+            }, (data: any) => {
+                if(data['error'].errors == 'Service forbidden'){
+                        this.loading = false;
+                        this.accessHistory = false;;
+                } else {
+                    location.href = "index.php";
+                }                
+            });
+
+        this.http.get(this.coreUrl + 'rest/batchHistories', {params: {"startDate" : (this.batchStartDate.getTime() / 1000).toString(), "endDate" : (this.batchEndDate.getTime() / 1000).toString()}})
+            .subscribe((data: any) => {
+                this.batchData = data['batchHistories'];
+                this.batchLimitExceeded = data['limitExceeded'];
+                this.loading = false;
+                setTimeout(() => {
+                    this.accessBatchHistory = true;
+                    this.batchDataSource = new MatTableDataSource(this.batchData);
+                    this.batchDataSource.paginator = this.batchPaginator;
+                    this.batchDataSource.sort = this.batchSort;
+                }, 0);
+            }, (data: any) => {
+                if(data['error'].errors == 'Service forbidden'){
+                        this.loading = false
+                        this.accessBatchHistory = false;
+                } else {
+                    location.href = "index.php";
+                }
+                
             });
+            
     }
 
-    refreshHistory() {
-        this.startDate.setHours(0,0,0,0);
-        this.endDate.setHours(23,59,59,59);
+    ngAfterViewInit() {
+        this.batchDataSource.paginator = this.batchPaginator;
+        this.batchDataSource.sort = this.batchSort;
+    }
+    
+    _setDataSource(indexNumber : any) {
+        console.log("datasource");
+        setTimeout(() => {
+          switch (indexNumber) {
+            case 0:
+              !this.dataSource.paginator ? this.dataSource.paginator = this.paginator : null;
+              !this.dataSource.sort ? this.dataSource.sort = this.sort : null;
+              break;
+            case 1:
+              !this.batchDataSource.paginator ? this.batchDataSource.paginator = this.batchPaginator : null;
+              !this.batchDataSource.sort ? this.batchDataSource.sort = this.batchSort : null;
+          }
+        });
+      }
+
+    refreshHistory(historyType : string) {
         
-        this.http.get(this.coreUrl + 'rest/histories', {params: {"startDate" : (this.startDate.getTime() / 1000).toString(), "endDate" : (this.endDate.getTime() / 1000).toString()}})
+        //console.log(historyType)
+        if(historyType == 'normal'){
+            this.startDate.setHours(0,0,0,0);
+            this.endDate.setHours(23,59,59,59);
+            this.http.get(this.coreUrl + 'rest/histories', {params: {"startDate" : (this.startDate.getTime() / 1000).toString(), "endDate" : (this.endDate.getTime() / 1000).toString()}})
             .subscribe((data: any) => {
                 this.data = data['histories'];
                 this.limitExceeded = data['limitExceeded'];
@@ -91,5 +164,30 @@ export class HistoryAdministrationComponent implements OnInit {
             }, () => {
                 location.href = "index.php";
             });
+        } else {
+            console.log(historyType)
+            this.batchStartDate.setHours(0,0,0,0);
+            this.batchEndDate.setHours(23,59,59,59);
+            this.http.get(this.coreUrl + 'rest/batchHistories', {params: {"startDate" : (this.batchStartDate.getTime() / 1000).toString(), "endDate" : (this.batchEndDate.getTime() / 1000).toString()}})
+            .subscribe((data: any) => {
+                this.batchData = data['batchHistories'];
+                this.batchLimitExceeded = data['limitExceeded'];
+                this.loading = false;
+                setTimeout(() => {
+                    this.accessBatchHistory = true;
+                    this.batchDataSource = new MatTableDataSource(this.batchData);
+                    this.batchDataSource.paginator = this.batchPaginator;
+                    this.batchDataSource.sort = this.batchSort;
+                }, 0);
+            }, (data: any) => {
+                if(data['error'].errors == 'Service forbidden'){
+                        this.loading = false
+                        this.accessBatchHistory = false;
+                } else {
+                    location.href = "index.php";
+                }                
+            });
+        }
+        
     }
 }
diff --git a/src/app/group/models/ServiceModelAbstract.php b/src/app/group/models/ServiceModelAbstract.php
old mode 100644
new mode 100755
index e08b1703af0..e75a55108d3
--- a/src/app/group/models/ServiceModelAbstract.php
+++ b/src/app/group/models/ServiceModelAbstract.php
@@ -321,8 +321,16 @@ abstract class ServiceModelAbstract
         $administrationApplication = ServiceModel::getApplicationAdministrationServicesByUserServices(['userServices' => $servicesStoredInDB]);
         $administrationModule = ServiceModel::getModulesAdministrationServicesByUserServices(['userServices' => $servicesStoredInDB]);
 
+        foreach($administrationApplication['supervision'] as $key => $value){
+            if($value['name'] == "Historique des batchs"){
+                array_splice($administrationApplication['supervision'], $key, 1);
+            }            
+        }
+        unset($key);
+
         $administration['administrations'] = array_merge_recursive($administrationApplication, $administrationModule);
-//        $administration = array_merge_recursive($administration, $administrationMenu);
+        
+        
 
         return $administration;
     }
-- 
GitLab