PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/fichinter/stats/index.php

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