index.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/deplacement/stats/index.php
  21. * \ingroup deplacement
  22. * \brief Page for statistics of module trips and expenses
  23. */
  24. // Load Dolibarr environment
  25. require '../../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacementstats.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('trips', 'companies'));
  30. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  31. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  32. $userid = GETPOST('userid', 'int'); if ($userid < 0) {
  33. $userid = 0;
  34. }
  35. $socid = GETPOST('socid', 'int'); if ($socid < 0) {
  36. $socid = 0;
  37. }
  38. $id = GETPOST('id', 'int');
  39. // Security check
  40. if ($user->socid > 0) {
  41. $action = '';
  42. $socid = $user->socid;
  43. }
  44. if ($user->socid) {
  45. $socid = $user->socid;
  46. }
  47. $result = restrictedArea($user, 'deplacement', $id, '');
  48. // Other security check
  49. $childids = $user->getAllChildIds();
  50. $childids[] = $user->id;
  51. if ($userid > 0) {
  52. if (!$user->hasRight('deplacement', 'readall') && !$user->hasRight('deplacement', 'lire_tous') && !in_array($userid, $childids)) {
  53. accessforbidden();
  54. exit;
  55. }
  56. }
  57. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  58. $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
  59. $startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  60. $endyear = $year;
  61. $mode = GETPOST("mode") ?GETPOST("mode") : 'customer';
  62. /*
  63. * View
  64. */
  65. $form = new Form($db);
  66. llxHeader();
  67. $title = $langs->trans("TripsAndExpensesStatistics");
  68. $dir = $conf->deplacement->dir_temp;
  69. print load_fiche_titre($title, $mesg);
  70. dol_mkdir($dir);
  71. $useridtofilter = $userid; // Filter from parameters
  72. if (empty($useridtofilter)) {
  73. $useridtofilter = $childids;
  74. if ($user->hasRight('deplacement', 'readall') || $user->hasRight('deplacement', 'lire_tous')) {
  75. $useridtofilter = 0;
  76. }
  77. }
  78. $stats = new DeplacementStats($db, $socid, $useridtofilter);
  79. // Build graphic number of object
  80. // $data = array(array('Lib',val1,val2,val3),...)
  81. //print "$endyear, $startyear";
  82. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  83. //var_dump($data);
  84. $filenamenb = $dir."/tripsexpensesnbinyear-".$year.".png";
  85. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesnbinyear-'.$year.'.png';
  86. $px1 = new DolGraph();
  87. $mesg = $px1->isGraphKo();
  88. if (!$mesg) {
  89. $px1->SetData($data);
  90. $i = $startyear;
  91. $legend = array();
  92. while ($i <= $endyear) {
  93. $legend[] = $i;
  94. $i++;
  95. }
  96. $px1->SetLegend($legend);
  97. $px1->SetMaxValue($px1->GetCeilMaxValue());
  98. $px1->SetWidth($WIDTH);
  99. $px1->SetHeight($HEIGHT);
  100. $px1->SetYLabel($langs->trans("Number"));
  101. $px1->SetShading(3);
  102. $px1->SetHorizTickIncrement(1);
  103. $px1->mode = 'depth';
  104. $px1->SetTitle($langs->trans("NumberByMonth"));
  105. $px1->draw($filenamenb, $fileurlnb);
  106. }
  107. // Build graphic amount of object
  108. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
  109. //var_dump($data);
  110. // $data = array(array('Lib',val1,val2,val3),...)
  111. $filenameamount = $dir."/tripsexpensesamountinyear-".$year.".png";
  112. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesamountinyear-'.$year.'.png';
  113. $px2 = new DolGraph();
  114. $mesg = $px2->isGraphKo();
  115. if (!$mesg) {
  116. $px2->SetData($data);
  117. $i = $startyear;
  118. $legend = array();
  119. while ($i <= $endyear) {
  120. $legend[] = $i;
  121. $i++;
  122. }
  123. $px2->SetLegend($legend);
  124. $px2->SetMaxValue($px2->GetCeilMaxValue());
  125. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  126. $px2->SetWidth($WIDTH);
  127. $px2->SetHeight($HEIGHT);
  128. $px2->SetYLabel($langs->trans("Amount"));
  129. $px2->SetShading(3);
  130. $px2->SetHorizTickIncrement(1);
  131. $px2->mode = 'depth';
  132. $px2->SetTitle($langs->trans("AmountTotal"));
  133. $px2->draw($filenameamount, $fileurlamount);
  134. }
  135. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  136. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  137. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  138. if ($mode == 'customer') {
  139. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  140. }
  141. if ($mode == 'supplier') {
  142. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  143. }
  144. } else {
  145. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  146. if ($mode == 'customer') {
  147. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  148. }
  149. if ($mode == 'supplier') {
  150. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  151. }
  152. }
  153. $px3 = new DolGraph();
  154. $mesg = $px3->isGraphKo();
  155. if (!$mesg) {
  156. $px3->SetData($data);
  157. $i = $startyear;
  158. $legend = array();
  159. while ($i <= $endyear) {
  160. $legend[] = $i;
  161. $i++;
  162. }
  163. $px3->SetLegend($legend);
  164. $px3->SetYLabel($langs->trans("AmountAverage"));
  165. $px3->SetMaxValue($px3->GetCeilMaxValue());
  166. $px3->SetMinValue($px3->GetFloorMinValue());
  167. $px3->SetWidth($WIDTH);
  168. $px3->SetHeight($HEIGHT);
  169. $px3->SetShading(3);
  170. $px3->SetHorizTickIncrement(1);
  171. $px3->mode = 'depth';
  172. $px3->SetTitle($langs->trans("AmountAverage"));
  173. $px3->draw($filename_avg, $fileurl_avg);
  174. }
  175. // Show array
  176. $data = $stats->getAllByYear();
  177. $arrayyears = array();
  178. foreach ($data as $val) {
  179. $arrayyears[$val['year']] = $val['year'];
  180. }
  181. if (!count($arrayyears)) {
  182. $arrayyears[$nowyear] = $nowyear;
  183. }
  184. $h = 0;
  185. $head = array();
  186. $head[$h][0] = DOL_URL_ROOT.'/compta/deplacement/stats/index.php';
  187. $head[$h][1] = $langs->trans("ByMonthYear");
  188. $head[$h][2] = 'byyear';
  189. $h++;
  190. complete_head_from_modules($conf, $langs, null, $head, $h, 'trip_stats');
  191. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  192. print '<div class="fichecenter"><div class="fichethirdleft">';
  193. // Show filter box
  194. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  195. print '<input type="hidden" name="token" value="'.newToken().'">';
  196. print '<input type="hidden" name="mode" value="'.$mode.'">';
  197. print '<table class="border centpercent">';
  198. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  199. // Company
  200. print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
  201. print img_picto('', 'company', 'class="pictofixedwidth"');
  202. print $form->select_company($socid, 'socid', '', 1, 1, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  203. print '</td></tr>';
  204. // User
  205. print '<tr><td>'.$langs->trans("User").'</td><td>';
  206. $include = '';
  207. if (!$user->hasRight('deplacement', 'readall') && !$user->hasRight('deplacement', 'lire_tous')) {
  208. $include = 'hierarchy';
  209. }
  210. print img_picto('', 'user', 'class="pictofixedwidth"');
  211. print $form->select_dolusers($userid, 'userid', 1, '', 0, $include, '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  212. print '</td></tr>';
  213. // Year
  214. print '<tr><td>'.$langs->trans("Year").'</td><td>';
  215. if (!in_array($year, $arrayyears)) {
  216. $arrayyears[$year] = $year;
  217. }
  218. arsort($arrayyears);
  219. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  220. print '</td></tr>';
  221. print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  222. print '</table>';
  223. print '</form>';
  224. print '<br><br>';
  225. print '<div class="div-table-responsive-no-min">';
  226. print '<table class="border centpercent">';
  227. print '<tr>';
  228. print '<td class="center">'.$langs->trans("Year").'</td>';
  229. print '<td class="center">'.$langs->trans("Number").'</td>';
  230. print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
  231. print '<td class="center">'.$langs->trans("AmountAverage").'</td>';
  232. print '</tr>';
  233. $oldyear = 0;
  234. foreach ($data as $val) {
  235. $year = $val['year'];
  236. while ($year && $oldyear > $year + 1) { // If we have empty year
  237. $oldyear--;
  238. print '<tr>';
  239. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.'">'.$oldyear.'</a></td>';
  240. print '<td class="right">0</td>';
  241. print '<td class="right">0</td>';
  242. print '<td class="right">0</td>';
  243. print '</tr>';
  244. }
  245. // Total
  246. print '<tr>';
  247. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
  248. print '<td class="right">'.$val['nb'].'</td>';
  249. print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
  250. print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
  251. print '</tr>';
  252. $oldyear = $year;
  253. }
  254. print '</table>';
  255. print '</div>';
  256. print '</div><div class="fichetwothirdright">';
  257. // Show graphs
  258. print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
  259. if ($mesg) {
  260. print $mesg;
  261. } else {
  262. print $px1->show();
  263. print "<br>\n";
  264. print $px2->show();
  265. print "<br>\n";
  266. print $px3->show();
  267. }
  268. print '</td></tr></table>';
  269. print '</div></div>';
  270. print '<div class="clearboth"></div>';
  271. print dol_get_fiche_end();
  272. // End of page
  273. llxFooter();
  274. $db->close();