|
| 1 | +<?php |
| 2 | +/* Copyright (C) 2024 EVARISK <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * \file class/digiboarddashboard.class.php |
| 20 | + * \ingroup digiboard |
| 21 | + * \brief Class file for manage DigiBoardDashboard |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Class for DigiBoardDashboard |
| 26 | + */ |
| 27 | +class DigiboardDashboard |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var DoliDB Database handler |
| 31 | + */ |
| 32 | + public DoliDB $db; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor |
| 36 | + * |
| 37 | + * @param DoliDB $db Database handler |
| 38 | + */ |
| 39 | + public function __construct(DoliDB $db) |
| 40 | + { |
| 41 | + $this->db = $db; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Load dashboard info |
| 46 | + * |
| 47 | + * @return array |
| 48 | + * @throws Exception |
| 49 | + */ |
| 50 | + public function load_dashboard(): array |
| 51 | + { |
| 52 | + $array['lists'] = []; |
| 53 | + |
| 54 | + if (isModEnabled('digiriskdolibarr')) { |
| 55 | + $getDigiRiskStatsList = $this->getDigiRiskStatsList(); |
| 56 | + $array['digiriskdolibarr']['lists'] = [$getDigiRiskStatsList]; |
| 57 | + } |
| 58 | + |
| 59 | + return $array; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get digirisk stats list with API |
| 64 | + * |
| 65 | + * @return array Graph datas (label/color/type/title/data etc..) |
| 66 | + * @throws Exception |
| 67 | + */ |
| 68 | + public function getDigiRiskStatsList(): array |
| 69 | + { |
| 70 | + global $conf, $db, $mc, $langs; |
| 71 | + |
| 72 | + // Graph Title parameters |
| 73 | + $array['title'] = $langs->transnoentities('DigiRiskStatsList'); |
| 74 | + $array['picto'] = $this->picto; |
| 75 | + |
| 76 | + // Graph parameters |
| 77 | + $array['type'] = 'list'; |
| 78 | + $array['labels'] = ['Site', 'Siret', 'RiskAssessmentDocument', 'DelayGenerateDate', 'NbEmployees', 'NbEmployeesInvolved', 'GreyRisk', 'OrangeRisk', 'RedRisk', 'BlackRisk', 'NbPresquAccidents', 'NbAccidents', 'NbAccidentsByEmployees', 'NbAccidentInvestigations', 'WorkStopDays', 'FrequencyIndex', 'FrequencyRate', 'GravityRate']; |
| 79 | + |
| 80 | + require_once __DIR__ . '/../../digiriskdolibarr/class/digiriskdolibarrdocuments/riskassessmentdocument.class.php'; |
| 81 | + require_once __DIR__ . '/../../digiriskdolibarr/class/evaluator.class.php'; |
| 82 | + require_once __DIR__ . '/../../digiriskdolibarr/class/digiriskelement.class.php'; |
| 83 | + require_once __DIR__ . '/../../digiriskdolibarr/class/riskanalysis/risk.class.php'; |
| 84 | + require_once __DIR__ . '/../../digiriskdolibarr/class/accident.class.php'; |
| 85 | + require_once __DIR__ . '/../../digiriskdolibarr/class/accidentinvestigation.class.php'; |
| 86 | + |
| 87 | + $riskAssessmentDocument = new RiskAssessmentDocument($this->db); |
| 88 | + $evaluator = new Evaluator($this->db); |
| 89 | + $digiriskElement = new DigiriskElement($this->db); |
| 90 | + $risk = new Risk($this->db); |
| 91 | + $accident = new Accident($this->db); |
| 92 | + $accidentInvestigation = new AccidentInvestigation($this->db); |
| 93 | + |
| 94 | + $riskAssessmentDocument->ismultientitymanaged = 0; |
| 95 | + $accident->ismultientitymanaged = 0; |
| 96 | + $entities = $mc->getEntitiesList(false, false, true); |
| 97 | + |
| 98 | + $arrayDigiRiskStatsList = []; |
| 99 | + $currentEntity[] = 1; |
| 100 | + $riskAssessmentCotation = [1 => 'GreyRisk', 2 => 'OrangeRisk', 3 => 'RedRisk', 4 => 'BlackRisk']; |
| 101 | + $sharingEntities = $mc->sharings['digiriskstats']; |
| 102 | + $sharingEntities = array_unique(array_merge($currentEntity, $sharingEntities)); |
| 103 | + if (!empty($sharingEntities)) { |
| 104 | + foreach ($sharingEntities as $key => $sharingEntity) { |
| 105 | + $arrayDigiRiskStatsList[$key]['Site']['value'] = $entities[$sharingEntity]; |
| 106 | + $arrayDigiRiskStatsList[$key]['Site']['morecss'] = 'left bold'; |
| 107 | + $arrayDigiRiskStatsList[$key]['Siret']['value'] = dolibarr_get_const($db, 'MAIN_INFO_SIRET', $sharingEntity); |
| 108 | + |
| 109 | + $moreParam['entity'] = $sharingEntity; |
| 110 | + $moreParam['filter'] = ' AND t.entity = ' . $sharingEntity; |
| 111 | + $arrayGetGenerationDateInfos = $riskAssessmentDocument->getGenerationDateInfos($moreParam); |
| 112 | + $arrayDigiRiskStatsList[$key]['RiskAssessmentDocument']['value'] = $arrayGetGenerationDateInfos['lastgeneratedate'] . $arrayGetGenerationDateInfos['moreContent']; |
| 113 | + $arrayDigiRiskStatsList[$key]['DelayGenerateDate']['value'] = $arrayGetGenerationDateInfos['delaygeneratedate']; |
| 114 | + |
| 115 | + $employees = $evaluator->getNbEmployees(); |
| 116 | + $arrayDigiRiskStatsList[$key]['NbEmployees']['value'] = $employees['nbemployees']; |
| 117 | + $arrayDigiRiskStatsList[$key]['NbEmployeesInvolved']['value'] = $evaluator->getNbEmployeesInvolved()['nbemployeesinvolved']; |
| 118 | + |
| 119 | + $filter = $digiriskElement->getTrashExclusionSqlFilter(); |
| 120 | + $filter .= $moreParam['filter']; |
| 121 | + $getRisksByCotation = $risk->getRisksByCotation($filter)['data']; |
| 122 | + for ($i = 1; $i <= 4; $i++) { |
| 123 | + $arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['value'] = $getRisksByCotation[$i]; |
| 124 | + $arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['morecss'] = 'risk-evaluation-cotation'; |
| 125 | + $arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['moreAttr'] = 'data-scale=' . $i . ' style="line-height: 0; border-radius: 0;"'; |
| 126 | + } |
| 127 | + |
| 128 | + $join = ' LEFT JOIN ' . MAIN_DB_PREFIX . $accident->table_element . ' as a ON a.rowid = t.fk_accident'; |
| 129 | + $accidentsWithWorkStops = saturne_fetch_all_object_type('AccidentWorkStop', 'DESC', 't.rowid', 0, 0, ['customsql' => 't.entity = ' . $sharingEntity], 'AND', false, false, false, $join); |
| 130 | + $accidents = $accident->fetchAll('', '', 0, 0, ['customsql' => ' t.status > ' . Accident::STATUS_DRAFT . ' AND t.entity = ' . $sharingEntity]); |
| 131 | + if (empty($accidents) && !is_array($accidents)) { |
| 132 | + $accidents = []; |
| 133 | + } |
| 134 | + if (empty($accidentsWithWorkStops) && !is_array($accidentsWithWorkStops)) { |
| 135 | + $accidentsWithWorkStops = []; |
| 136 | + } |
| 137 | + |
| 138 | + $arrayDigiRiskStatsList[$key]['NbPresquAccidents']['value'] = $accident->getNbPresquAccidents()['nbpresquaccidents']; |
| 139 | + $arrayDigiRiskStatsList[$key]['NbAccidents']['value'] = $accident->getNbAccidents($accidents, $accidentsWithWorkStops)['data']['accidents']; |
| 140 | + $arrayDigiRiskStatsList[$key]['NbAccidentsByEmployees']['value'] = $accident->getNbAccidentsByEmployees($accidents, $accidentsWithWorkStops, $employees)['nbaccidentsbyemployees']; |
| 141 | + $arrayDigiRiskStatsList[$key]['NbAccidentInvestigations']['value'] = $accident->getNbAccidentInvestigations()['nbaccidentinvestigations']; |
| 142 | + $arrayDigiRiskStatsList[$key]['WorkStopDays']['value'] = $accident->getNbWorkstopDays($accidentsWithWorkStops)['nbworkstopdays']; |
| 143 | + $arrayDigiRiskStatsList[$key]['FrequencyIndex']['value'] = $accident->getFrequencyIndex($accidentsWithWorkStops, $employees)['frequencyindex']; |
| 144 | + $arrayDigiRiskStatsList[$key]['FrequencyRate']['value'] = $accident->getFrequencyRate($employees)['frequencyrate']; |
| 145 | + $arrayDigiRiskStatsList[$key]['GravityRate']['value'] = $accident->getGravityRate($employees)['gravityrate']; |
| 146 | + } |
| 147 | + } |
| 148 | + $array['data'] = $arrayDigiRiskStatsList; |
| 149 | + |
| 150 | + return $array; |
| 151 | + } |
| 152 | +} |
0 commit comments