index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  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. * \file htdocs/fichinter/stats/index.php
  19. * \ingroup fichinter
  20. * \brief Page with interventions statistics
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinterstats.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  27. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  28. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  29. $mode = 'customer';
  30. if (!$user->rights->ficheinter->lire) {
  31. accessforbidden();
  32. }
  33. $userid = GETPOST('userid', 'int');
  34. $socid = GETPOST('socid', 'int');
  35. // Security check
  36. if ($user->socid > 0) {
  37. $action = '';
  38. $socid = $user->socid;
  39. }
  40. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  41. $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
  42. $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  43. $endyear = $year;
  44. $object_status = GETPOST('object_status', 'intcomma');
  45. // Load translation files required by the page
  46. $langs->loadLangs(array('interventions', 'companies', 'other', 'suppliers'));
  47. /*
  48. * View
  49. */
  50. $form = new Form($db);
  51. $objectstatic = new FichInter($db);
  52. $title = $langs->trans("InterventionStatistics");
  53. $dir = $conf->ficheinter->dir_temp;
  54. llxHeader('', $title);
  55. print load_fiche_titre($title, '', 'intervention');
  56. dol_mkdir($dir);
  57. $stats = new FichinterStats($db, $socid, $mode, ($userid > 0 ? $userid : 0));
  58. if ($object_status != '' && $object_status > -1) {
  59. $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($db->escape($object_status)).')';
  60. }
  61. // Build graphic number of object
  62. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  63. // $data = array(array('Lib',val1,val2,val3),...)
  64. if (empty($user->rights->societe->client->voir) || $user->socid) {
  65. $filenamenb = $dir.'/interventionsnbinyear-'.$user->id.'-'.$year.'.png';
  66. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsnbinyear-'.$user->id.'-'.$year.'.png';
  67. } else {
  68. $filenamenb = $dir.'/interventionsnbinyear-'.$year.'.png';
  69. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsnbinyear-'.$year.'.png';
  70. }
  71. $px1 = new DolGraph();
  72. $mesg = $px1->isGraphKo();
  73. if (!$mesg) {
  74. $px1->SetData($data);
  75. $i = $startyear; $legend = array();
  76. while ($i <= $endyear) {
  77. $legend[] = $i;
  78. $i++;
  79. }
  80. $px1->SetLegend($legend);
  81. $px1->SetMaxValue($px1->GetCeilMaxValue());
  82. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  83. $px1->SetWidth($WIDTH);
  84. $px1->SetHeight($HEIGHT);
  85. $px1->SetYLabel($langs->trans("NbOfIntervention"));
  86. $px1->SetShading(3);
  87. $px1->SetHorizTickIncrement(1);
  88. $px1->mode = 'depth';
  89. $px1->SetTitle($langs->trans("NumberOfInterventionsByMonth"));
  90. $px1->draw($filenamenb, $fileurlnb);
  91. }
  92. // Build graphic amount of object
  93. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
  94. // $data = array(array('Lib',val1,val2,val3),...)
  95. if (empty($user->rights->societe->client->voir) || $user->socid) {
  96. $filenameamount = $dir.'/interventionsamountinyear-'.$user->id.'-'.$year.'.png';
  97. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsamountinyear-'.$user->id.'-'.$year.'.png';
  98. } else {
  99. $filenameamount = $dir.'/interventionsamountinyear-'.$year.'.png';
  100. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsamountinyear-'.$year.'.png';
  101. }
  102. $px2 = new DolGraph();
  103. $mesg = $px2->isGraphKo();
  104. if (!$mesg) {
  105. $px2->SetData($data);
  106. $i = $startyear; $legend = array();
  107. while ($i <= $endyear) {
  108. $legend[] = $i;
  109. $i++;
  110. }
  111. $px2->SetLegend($legend);
  112. $px2->SetMaxValue($px2->GetCeilMaxValue());
  113. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  114. $px2->SetWidth($WIDTH);
  115. $px2->SetHeight($HEIGHT);
  116. $px2->SetYLabel($langs->trans("AmountOfinterventions"));
  117. $px2->SetShading(3);
  118. $px2->SetHorizTickIncrement(1);
  119. $px2->mode = 'depth';
  120. $px2->SetTitle($langs->trans("AmountOfinterventionsByMonthHT"));
  121. $px2->draw($filenameamount, $fileurlamount);
  122. }
  123. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  124. if (empty($user->rights->societe->client->voir) || $user->socid) {
  125. $filename_avg = $dir.'/interventionsaverage-'.$user->id.'-'.$year.'.png';
  126. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsaverage-'.$user->id.'-'.$year.'.png';
  127. } else {
  128. $filename_avg = $dir.'/interventionsaverage-'.$year.'.png';
  129. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=interventionstats&file=interventionsaverage-'.$year.'.png';
  130. }
  131. $px3 = new DolGraph();
  132. $mesg = $px3->isGraphKo();
  133. if (!$mesg) {
  134. $px3->SetData($data);
  135. $i = $startyear; $legend = array();
  136. while ($i <= $endyear) {
  137. $legend[] = $i;
  138. $i++;
  139. }
  140. $px3->SetLegend($legend);
  141. $px3->SetYLabel($langs->trans("AmountAverage"));
  142. $px3->SetMaxValue($px3->GetCeilMaxValue());
  143. $px3->SetMinValue($px3->GetFloorMinValue());
  144. $px3->SetWidth($WIDTH);
  145. $px3->SetHeight($HEIGHT);
  146. $px3->SetShading(3);
  147. $px3->SetHorizTickIncrement(1);
  148. $px3->mode = 'depth';
  149. $px3->SetTitle($langs->trans("AmountAverage"));
  150. $px3->draw($filename_avg, $fileurl_avg);
  151. }
  152. // Show array
  153. $data = $stats->getAllByYear();
  154. $arrayyears = array();
  155. foreach ($data as $val) {
  156. if (!empty($val['year'])) {
  157. $arrayyears[$val['year']] = $val['year'];
  158. }
  159. }
  160. if (!count($arrayyears)) {
  161. $arrayyears[$nowyear] = $nowyear;
  162. }
  163. $h = 0;
  164. $head = array();
  165. $head[$h][0] = DOL_URL_ROOT.'/fichinter/stats/index.php';
  166. $head[$h][1] = $langs->trans("ByMonthYear");
  167. $head[$h][2] = 'byyear';
  168. $h++;
  169. $type = 'fichinter_stats';
  170. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  171. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  172. print '<div class="fichecenter"><div class="fichethirdleft">';
  173. // Show filter box
  174. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  175. print '<input type="hidden" name="token" value="'.newToken().'">';
  176. print '<input type="hidden" name="mode" value="'.$mode.'">';
  177. print '<table class="noborder centpercent">';
  178. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  179. // Company
  180. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  181. $filter = 's.client IN (1,2,3)';
  182. print img_picto('', 'company', 'class="pictofixedwidth"');
  183. print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  184. print '</td></tr>';
  185. // User
  186. print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
  187. print img_picto('', 'user', 'class="pictofixedwidth"');
  188. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  189. // Status
  190. print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
  191. $tmp = $objectstatic->LibStatut(0); // To force load of $this->statuts_short
  192. $liststatus = $objectstatic->statuts_short;
  193. if (empty($conf->global->FICHINTER_CLASSIFY_BILLED)) {
  194. unset($liststatus[2]); // Option deprecated. In a future, billed must be managed with a dedicated field to 0 or 1
  195. }
  196. print $form->selectarray('object_status', $liststatus, $object_status, 1, 0, 0, '', 1);
  197. print '</td></tr>';
  198. // Year
  199. print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
  200. if (!in_array($year, $arrayyears)) {
  201. $arrayyears[$year] = $year;
  202. }
  203. if (!in_array($nowyear, $arrayyears)) {
  204. $arrayyears[$nowyear] = $nowyear;
  205. }
  206. arsort($arrayyears);
  207. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  208. print '</td></tr>';
  209. print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  210. print '</table>';
  211. print '</form>';
  212. print '<br><br>';
  213. print '<div class="div-table-responsive-no-min">';
  214. print '<table class="noborder centpercent">';
  215. print '<tr class="liste_titre" height="24">';
  216. print '<td class="center">'.$langs->trans("Year").'</td>';
  217. print '<td class="right">'.$langs->trans("NbOfinterventions").'</td>';
  218. print '<td class="right">%</td>';
  219. print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
  220. print '<td class="right">%</td>';
  221. print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
  222. print '<td class="right">%</td>';
  223. print '</tr>';
  224. $oldyear = 0;
  225. foreach ($data as $val) {
  226. $year = $val['year'];
  227. while (!empty($year) && $oldyear > $year + 1) {
  228. // If we have empty year
  229. $oldyear--;
  230. print '<tr class="oddeven" height="24">';
  231. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
  232. print '<td class="right">0</td>';
  233. print '<td class="right"></td>';
  234. print '<td class="right">0</td>';
  235. print '<td class="right"></td>';
  236. print '<td class="right">0</td>';
  237. print '<td class="right"></td>';
  238. print '</tr>';
  239. }
  240. print '<tr class="oddeven" height="24">';
  241. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  242. print '<td class="right">'.$val['nb'].'</td>';
  243. print '<td class="right opacitylow" style="'.((!isset($val['nb_diff']) || $val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['nb_diff']) ? round($val['nb_diff']): "0").'%</td>';
  244. print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
  245. print '<td class="right opacitylow" style="'.((!isset($val['total_diff']) || $val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['total_diff']) ? round($val['total_diff']) : "0").'%</td>';
  246. print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
  247. print '<td class="right opacitylow" style="'.((!isset($val['avg_diff']) || $val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['avg_diff']) ? round($val['avg_diff']) : "0").'%</td>';
  248. print '</tr>';
  249. $oldyear = $year;
  250. }
  251. print '</table>';
  252. print '</div>';
  253. print '</div><div class="fichetwothirdright">';
  254. // Show graphs
  255. print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
  256. if ($mesg) {
  257. print $mesg;
  258. } else {
  259. print $px1->show();
  260. /*print "<br>\n";
  261. print $px2->show();
  262. print "<br>\n";
  263. print $px3->show();*/
  264. }
  265. print '</td></tr></table>';
  266. print '</div></div>';
  267. print '<div style="clear:both"></div>';
  268. print dol_get_fiche_end();
  269. llxFooter();
  270. $db->close();