@@ -438,15 +438,30 @@ public function LibStatut(int $status, int $mode = 0): string
438438 */
439439 public function load_dashboard (): array
440440 {
441- global $ langs , $ conf ;
441+ global $ langs , $ conf , $ db ;
442+
443+ // echo '<pre>'; print_r(getDolGlobalInt('SOCIETE_FISCAL_MONTH_START')); echo '</pre>'; exit;
442444
443445 $ confName = dol_strtoupper ($ this ->module ) . '_DASHBOARD_CONFIG ' ;
444446 $ dashboardConfig = json_decode (getDolUserString ($ confName ));
445- $ array = ['graphs ' => [], 'disabledGraphs ' => []];
447+ $ array = ['graphs ' => [], 'lists ' => [], 'disabledGraphs ' => []];
448+
449+ $ yearType = !empty ($ dashboardConfig ->filters ->yearType ) ? $ dashboardConfig ->filters ->yearType : 'calendar ' ;
450+ $ year = !empty ($ dashboardConfig ->filters ->year ) ? $ dashboardConfig ->filters ->year : -1 ;
451+
452+ $ now = dol_now ('tzuserrel ' );
453+ $ date = dol_getdate ($ now );
454+ $ startDate = dol_mktime (-1 , -1 , -1 , $ yearType == 'calendar ' ? 1 : getDolGlobalInt ('SOCIETE_FISCAL_MONTH_START ' ), 1 , $ year == -1 ? $ date ['year ' ] : $ year );
455+ if ($ startDate > $ now ) {
456+ $ startDate = dol_time_plus_duree ($ startDate , -1 , 'y ' );
457+ }
458+ $ endDate = dol_time_plus_duree ($ startDate , 1 , 'y ' );
446459
447460 $ join = ' LEFT JOIN ' . MAIN_DB_PREFIX . $ this ->table_element . ' as a ON a.rowid = t.fk_accident ' ;
448- $ accidentsWithWorkStops = saturne_fetch_all_object_type ('AccidentWorkStop ' , 'DESC ' , 't.rowid ' , 0 , 0 , [], 'AND ' , false , true , false , $ join );
449- $ accidents = $ this ->fetchAll ('' , '' , 0 , 0 , ['customsql ' => ' t.status > ' . self ::STATUS_DRAFT ]);
461+ $ accidentsWithWorkStops = saturne_fetch_all_object_type ('AccidentWorkStop ' , 'DESC ' , 't.rowid ' , 0 , 0 , $ year == -1 ? [] : ['customsql ' => ' t.date_start_workstop >= \'' . dol_print_date ($ startDate , '%Y/%m/%d ' ) . '\' AND t.date_start_workstop < \'' . dol_print_date ($ endDate , '%Y/%m/%d ' ) . '\'' ], 'AND ' , false , true , false , $ join );
462+ $ accidents = $ this ->fetchAll ('' , '' , 0 , 0 , ['customsql ' => ' t.status > ' . self ::STATUS_DRAFT . ($ year == -1 ? '' : ' AND t.accident_date >= \'' . dol_print_date ($ startDate , '%Y/%m/%d ' ) . '\' AND t.accident_date < \'' . dol_print_date ($ endDate , '%Y/%m/%d ' ) . '\'' )]);
463+ $ digiriskElement = new DigiriskElement ($ db );
464+ $ digiriskElements = $ digiriskElement ->fetchDigiriskElementFlat (0 );
450465
451466 if (empty ($ accidents ) && !is_array ($ accidents )) {
452467 $ accidents = [];
@@ -468,6 +483,29 @@ public function load_dashboard(): array
468483 $ arrayFrequencyRate = $ this ->getFrequencyRate ($ accidentsWithWorkStops );
469484 $ arrayGravityRate = $ this ->getGravityRate ($ accidentsWithWorkStops );
470485
486+ // dol_time_plus_duree
487+
488+ $ currentYear = (int ) dol_print_date (dol_now ('tzuserrel ' ), '%Y ' );
489+ $ years = range ($ currentYear , $ currentYear - 10 );
490+
491+
492+ $ array ['graphsFilters ' ] = [
493+ 'yearType ' => [
494+ 'title ' => $ langs ->transnoentities ('YearType ' ),
495+ 'type ' => 'selectarray ' ,
496+ 'filter ' => 'yearType ' ,
497+ 'values ' => ['calendar ' => $ langs ->transnoentities ('CalendarYear ' ), 'fiscal ' => $ langs ->transnoentities ('FiscalYear ' )],
498+ 'currentValue ' => $ yearType
499+ ],
500+ 'year ' => [
501+ 'title ' => $ langs ->transnoentities ('Year ' ),
502+ 'type ' => 'selectarray ' ,
503+ 'filter ' => 'year ' ,
504+ 'values ' => array_combine (array_merge ([-1 ], $ years ), array_merge ([$ langs ->transnoentities ('AllYears ' )], array_map ('strval ' , $ years ))),
505+ 'currentValue ' => $ year
506+ ]
507+ ];
508+
471509 $ array ['widgets ' ] = [
472510 'accident ' => [
473511 'title ' => $ langs ->transnoentities ('Accidents ' ),
@@ -489,18 +527,35 @@ public function load_dashboard(): array
489527 (($ conf ->global ->DIGIRISKDOLIBARR_NB_WORKED_HOURS > 0 && $ conf ->global ->DIGIRISKDOLIBARR_MANUAL_INPUT_NB_WORKED_HOURS ) ? $ langs ->transnoentities ('GravityRateTooltip ' ) . '<br> ' . $ langs ->transnoentities ('NbWorkedHoursTooltip ' ) : $ langs ->transnoentities ('GravityRateTooltip ' ))
490528 ],
491529 'widgetName ' => $ langs ->transnoentities ('AccidentRateIndicator ' )
492- ]
530+ ],
493531 ];
494532
495533 if (empty ($ dashboardConfig ->graphs ->AccidentRepartition ->hide )) {
496534 $ array ['graphs ' ][] = $ this ->getNbAccidents ($ accidents , $ accidentsWithWorkStops );
497535 } else {
498536 $ array ['disabledGraphs ' ]['AccidentRepartition ' ] = $ langs ->transnoentities ('AccidentRepartition ' );
499537 }
500- if (empty ($ dashboardConfig ->graphs ->AccidentByYear ->hide )) {
501- $ array ['graphs ' ][] = $ this ->getNbAccidentsLast3years ($ accidents );
538+ if ($ year == -1 ) {
539+ if (empty ($ dashboardConfig ->graphs ->AccidentByYear ->hide )) {
540+ $ array ['graphs ' ][] = $ this ->getNbAccidentsLast3years ($ accidents );
541+ } else {
542+ $ array ['disabledGraphs ' ]['AccidentByYear ' ] = $ langs ->transnoentities ('AccidentByYear ' );
543+ }
544+ }
545+ if (empty ($ dashboardConfig ->graphs ->AccidentRegister ->hide )) {
546+ $ array ['graphs ' ][] = $ this ->getAccidentRegister ($ accidents );
547+ } else {
548+ $ array ['disabledGraphs ' ]['AccidentRegister ' ] = $ langs ->transnoentities ('AccidentRegister ' );
549+ }
550+ if (empty ($ dashboardConfig ->graphs ->AccidentsByWorkStops ->hide )) {
551+ $ array ['graphs ' ][] = $ this ->getNbAccidentsByWorkStops ($ accidents , $ accidentsWithWorkStops );
502552 } else {
503- $ array ['disabledGraphs ' ]['AccidentByYear ' ] = $ langs ->transnoentities ('AccidentByYear ' );
553+ $ array ['disabledGraphs ' ]['AccidentsByWorkStops ' ] = $ langs ->transnoentities ('AccidentsByWorkStops ' );
554+ }
555+ if (empty ($ dashboardConfig ->graphs ->AccidentsWorkStopByDigiriskElem ->hide )) {
556+ $ array ['graphs ' ][] = $ this ->getAccidentsWorkStopByDigiriskElem ($ accidents , $ accidentsWithWorkStops , $ digiriskElements );
557+ } else {
558+ $ array ['disabledGraphs ' ]['AccidentsWorkStopByDigiriskElem ' ] = $ langs ->transnoentities ('AccidentsWorkStopByDigiriskElem ' );
504559 }
505560
506561 return $ array ;
@@ -798,6 +853,175 @@ public function getGravityRate(array $accidentsWithWorkStops = []): array
798853 return $ array ;
799854 }
800855
856+ /**
857+ * Get graph of AccidentRegister
858+ *
859+ * @param array $accident Array of accidents
860+ * @return array
861+ */
862+ public function getAccidentRegister (array $ accident = []): array
863+ {
864+ global $ langs ;
865+
866+ $ accidentWithoutRegister = count (array_filter ($ accident , function ($ elem ) {
867+ return $ elem ->fk_ticket === null ;
868+ }));
869+ $ array = [];
870+
871+ $ array ['title ' ] = $ langs ->transnoentities ('AccidentRegister ' );
872+ $ array ['name ' ] = 'AccidentRegister ' ;
873+ $ array ['picto ' ] = $ this ->picto ;
874+
875+ // Graph parameters
876+ $ array ['width ' ] = '100% ' ;
877+ $ array ['height ' ] = 400 ;
878+ $ array ['type ' ] = 'pie ' ;
879+ $ array ['showlegend ' ] = 2 ;
880+ $ array ['dataset ' ] = 1 ;
881+
882+ $ array ['labels ' ] = [
883+ [
884+ 'label ' => $ langs ->transnoentities ('AccidentWithoutRegister ' ),
885+ 'color ' => '#9567aa '
886+ ],
887+ [
888+ 'label ' => $ langs ->transnoentities ('AccidentWithRegister ' ),
889+ 'color ' => '#4f9ebe '
890+ ],
891+ ];
892+
893+ $ array ['data ' ] = [$ accidentWithoutRegister , count ($ accident ) - $ accidentWithoutRegister ];
894+ return $ array ;
895+ }
896+
897+ /**
898+ * Get graph of NbAccidentsByWorkStops
899+ *
900+ * @param array $accidents Array of accidents
901+ * @param array $accidentsWithWorkStop Array of work stops
902+ * @return array
903+ */
904+ public function getNbAccidentsByWorkStops (array $ accidents = [], array $ accidentsWithWorkStop = []): array
905+ {
906+ global $ langs ;
907+
908+ $ array ['title ' ] = $ langs ->transnoentities ('WorkStopDurationDistribution ' );
909+ $ array ['name ' ] = 'WorkStopDurationDistribution ' ;
910+ $ array ['picto ' ] = $ this ->picto ;
911+
912+ // Graph parameters
913+ $ array ['width ' ] = '100% ' ;
914+ $ array ['height ' ] = 400 ;
915+ $ array ['type ' ] = 'pie ' ;
916+ $ array ['showlegend ' ] = 2 ;
917+ $ array ['dataset ' ] = 1 ;
918+
919+ $ array ['labels ' ] = [
920+ 'noAbsence ' => [
921+ 'label ' => $ langs ->transnoentities ('NoAbsence ' ),
922+ 'color ' => '#4caf50 '
923+ ],
924+ 'upTo4Days ' => [
925+ 'label ' => $ langs ->transnoentities ('UpTo4Days ' ),
926+ 'color ' => '#8bc34a '
927+ ],
928+ 'upTo21Days ' => [
929+ 'label ' => $ langs ->transnoentities ('UpTo21Days ' ),
930+ 'color ' => '#ffc107 '
931+ ],
932+ 'upTo3Months ' => [
933+ 'label ' => $ langs ->transnoentities ('UpTo3Months ' ),
934+ 'color ' => '#ff9800 '
935+ ],
936+ 'upTo6Months ' => [
937+ 'label ' => $ langs ->transnoentities ('UpTo6Months ' ),
938+ 'color ' => '#ff5722 '
939+ ],
940+ 'moreThan6Months ' => [
941+ 'label ' => $ langs ->transnoentities ('MoreThan6Months ' ),
942+ 'color ' => '#f44336 '
943+ ],
944+ ];
945+ $ array ['data ' ] = [
946+ 'noAbsence ' => 0 ,
947+ 'upTo4Days ' => 0 ,
948+ 'upTo21Days ' => 0 ,
949+ 'upTo3Months ' => 0 ,
950+ 'upTo6Months ' => 0 ,
951+ 'moreThan6Months ' => 0 ,
952+ ];
953+
954+ $ nbAccidensWithWorkStop = [];
955+ foreach ($ accidentsWithWorkStop as $ workstop ) {
956+ $ days = abs ($ workstop ->date_end_workstop - $ workstop ->date_start_workstop ) / 86400 ;
957+
958+ if ($ days < 4 ) {
959+ $ array ['data ' ]['upTo4Days ' ]++;
960+ } elseif ($ days < 21 ) {
961+ $ array ['data ' ]['upTo21Days ' ]++;
962+ } elseif ($ days < 90 ) {
963+ $ array ['data ' ]['upTo3Months ' ]++;
964+ } elseif ($ days < 180 ) {
965+ $ array ['data ' ]['upTo6Months ' ]++;
966+ } else {
967+ $ array ['data ' ]['moreThan6Months ' ]++;
968+ }
969+ $ nbAccidensWithWorkStop [$ workstop ->fk_accident ] = $ workstop ->fk_accident ;
970+ }
971+ foreach ($ accidents as $ accident ) {
972+ if (!in_array ($ accident ->id , $ nbAccidensWithWorkStop )) {
973+ $ array ['data ' ]['noAbsence ' ]++;
974+ }
975+ }
976+ return $ array ;
977+ }
978+
979+ /**
980+ * Get graph of AccidentsWorkStopByDigiriskElem
981+ *
982+ * @param array $accidents Array of accidents
983+ * @param array $accidentsWithWorkStop Array of work stops
984+ * @param array $digiriskElements Array of digirisk elements
985+ * @return array
986+ */
987+ public function getAccidentsWorkStopByDigiriskElem (array $ accidents = [], array $ accidentsWithWorkStop = [], array $ digiriskElements = []): array
988+ {
989+ global $ langs ;
990+
991+ $ array ['title ' ] = $ langs ->transnoentities ('WorkStopDurationDistributionDigiriskElem ' );
992+ $ array ['name ' ] = 'WorkStopDurationDistributionDigiriskElem ' ;
993+ $ array ['picto ' ] = $ this ->picto ;
994+
995+ // Graph parameters
996+ $ array ['width ' ] = '100% ' ;
997+ $ array ['height ' ] = 400 ;
998+ $ array ['type ' ] = 'pie ' ;
999+ $ array ['showlegend ' ] = 2 ;
1000+ $ array ['dataset ' ] = 1 ;
1001+
1002+ $ array ['data ' ] = [];
1003+ foreach ($ accidentsWithWorkStop as $ workstop ) {
1004+ if (!empty ($ accidents [$ workstop ->fk_accident ])) {
1005+ $ accident = $ accidents [$ workstop ->fk_accident ];
1006+ if (empty ($ array ['data ' ][$ accident ->fk_element ])) {
1007+ $ array ['data ' ][$ accident ->fk_element ] = 0 ;
1008+ }
1009+ $ array ['data ' ][$ accident ->fk_element ] += abs ($ workstop ->date_end_workstop - $ workstop ->date_start_workstop ) / 86400 ;
1010+ }
1011+ }
1012+ unset($ array ['data ' ][null ]);
1013+
1014+ $ array ['labels ' ] = [];
1015+ foreach (array_keys ($ array ['data ' ]) as $ digiriskElemId ) {
1016+ $ digiriskElem = $ digiriskElements [$ digiriskElemId ]['object ' ];
1017+ $ array ['labels ' ][$ digiriskElemId ] = [
1018+ 'label ' => $ digiriskElem ->ref . ' - ' . $ digiriskElem ->label ,
1019+ ];
1020+ }
1021+
1022+ return $ array ;
1023+ }
1024+
8011025 /**
8021026 * Get user victim object.
8031027 *
0 commit comments