index.php 9.9 KB

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