index.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <http://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. require '../../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacementstats.class.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array('trips', 'companies'));
  29. $WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
  30. $HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
  31. $userid=GETPOST('userid','int'); if ($userid < 0) $userid=0;
  32. $socid=GETPOST('socid','int'); if ($socid < 0) $socid=0;
  33. $id = GETPOST('id','int');
  34. // Security check
  35. if ($user->societe_id > 0)
  36. {
  37. $action = '';
  38. $socid = $user->societe_id;
  39. }
  40. if ($user->societe_id) $socid=$user->societe_id;
  41. $result = restrictedArea($user, 'deplacement', $id,'');
  42. // Other security check
  43. $childids = $user->getAllChildIds();
  44. $childids[]=$user->id;
  45. if ($userid > 0)
  46. {
  47. if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous) && ! in_array($userid, $childids))
  48. {
  49. accessforbidden();
  50. exit;
  51. }
  52. }
  53. $nowyear=strftime("%Y", dol_now());
  54. $year = GETPOST('year')>0?GETPOST('year'):$nowyear;
  55. //$startyear=$year-2;
  56. $startyear=$year-1;
  57. $endyear=$year;
  58. $mode=GETPOST("mode")?GETPOST("mode"):'customer';
  59. /*
  60. * View
  61. */
  62. $form=new Form($db);
  63. llxHeader();
  64. $title=$langs->trans("TripsAndExpensesStatistics");
  65. $dir=$conf->deplacement->dir_temp;
  66. print load_fiche_titre($title, $mesg);
  67. dol_mkdir($dir);
  68. $useridtofilter=$userid; // Filter from parameters
  69. if (empty($useridtofilter))
  70. {
  71. $useridtofilter=$childids;
  72. if (! empty($user->rights->deplacement->readall) || ! empty($user->rights->deplacement->lire_tous)) $useridtofilter=0;
  73. }
  74. $stats = new DeplacementStats($db, $socid, $useridtofilter);
  75. // Build graphic number of object
  76. // $data = array(array('Lib',val1,val2,val3),...)
  77. //print "$endyear, $startyear";
  78. $data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
  79. //var_dump($data);
  80. $filenamenb = $dir."/tripsexpensesnbinyear-".$year.".png";
  81. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesnbinyear-'.$year.'.png';
  82. $px1 = new DolGraph();
  83. $mesg = $px1->isGraphKo();
  84. if (! $mesg)
  85. {
  86. $px1->SetData($data);
  87. $px1->SetPrecisionY(0);
  88. $i=$startyear;$legend=array();
  89. while ($i <= $endyear)
  90. {
  91. $legend[]=$i;
  92. $i++;
  93. }
  94. $px1->SetLegend($legend);
  95. $px1->SetMaxValue($px1->GetCeilMaxValue());
  96. $px1->SetWidth($WIDTH);
  97. $px1->SetHeight($HEIGHT);
  98. $px1->SetYLabel($langs->trans("Number"));
  99. $px1->SetShading(3);
  100. $px1->SetHorizTickIncrement(1);
  101. $px1->SetPrecisionY(0);
  102. $px1->mode='depth';
  103. $px1->SetTitle($langs->trans("NumberByMonth"));
  104. $px1->draw($filenamenb,$fileurlnb);
  105. }
  106. // Build graphic amount of object
  107. $data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
  108. //var_dump($data);
  109. // $data = array(array('Lib',val1,val2,val3),...)
  110. $filenameamount = $dir."/tripsexpensesamountinyear-".$year.".png";
  111. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=tripsexpensesstats&amp;file=tripsexpensesamountinyear-'.$year.'.png';
  112. $px2 = new DolGraph();
  113. $mesg = $px2->isGraphKo();
  114. if (! $mesg)
  115. {
  116. $px2->SetData($data);
  117. $i=$startyear;$legend=array();
  118. while ($i <= $endyear)
  119. {
  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->SetPrecisionY(0);
  132. $px2->mode='depth';
  133. $px2->SetTitle($langs->trans("AmountTotal"));
  134. $px2->draw($filenameamount,$fileurlamount);
  135. }
  136. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  137. if (!$user->rights->societe->client->voir || $user->societe_id)
  138. {
  139. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  140. if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  141. if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  142. }
  143. else
  144. {
  145. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  146. if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  147. if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  148. }
  149. $px3 = new DolGraph();
  150. $mesg = $px3->isGraphKo();
  151. if (! $mesg)
  152. {
  153. $px3->SetData($data);
  154. $i = $startyear;$legend=array();
  155. while ($i <= $endyear)
  156. {
  157. $legend[]=$i;
  158. $i++;
  159. }
  160. $px3->SetLegend($legend);
  161. $px3->SetYLabel($langs->trans("AmountAverage"));
  162. $px3->SetMaxValue($px3->GetCeilMaxValue());
  163. $px3->SetMinValue($px3->GetFloorMinValue());
  164. $px3->SetWidth($WIDTH);
  165. $px3->SetHeight($HEIGHT);
  166. $px3->SetShading(3);
  167. $px3->SetHorizTickIncrement(1);
  168. $px3->SetPrecisionY(0);
  169. $px3->mode='depth';
  170. $px3->SetTitle($langs->trans("AmountAverage"));
  171. $px3->draw($filename_avg,$fileurl_avg);
  172. }
  173. // Show array
  174. $data = $stats->getAllByYear();
  175. $arrayyears=array();
  176. foreach($data as $val) {
  177. $arrayyears[$val['year']]=$val['year'];
  178. }
  179. if (! count($arrayyears)) $arrayyears[$nowyear]=$nowyear;
  180. $h=0;
  181. $head = array();
  182. $head[$h][0] = DOL_URL_ROOT . '/compta/deplacement/stats/index.php';
  183. $head[$h][1] = $langs->trans("ByMonthYear");
  184. $head[$h][2] = 'byyear';
  185. $h++;
  186. complete_head_from_modules($conf,$langs,null,$head,$h,'trip_stats');
  187. dol_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  188. print '<div class="fichecenter"><div class="fichethirdleft">';
  189. // Show filter box
  190. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  191. print '<input type="hidden" name="mode" value="'.$mode.'">';
  192. print '<table class="border" width="100%">';
  193. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  194. // Company
  195. print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
  196. $filter='';
  197. print $form->select_company($socid,'socid',$filter,1,1,0,array(),0,'','style="width: 95%"');
  198. print '</td></tr>';
  199. // User
  200. print '<tr><td>'.$langs->trans("User").'</td><td>';
  201. $include='';
  202. if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous)) $include='hierarchy';
  203. print $form->select_dolusers($userid, 'userid', 1, '', 0, $include, '', 0, 0, 0, '', 0, '', 'maxwidth300');
  204. print '</td></tr>';
  205. // Year
  206. print '<tr><td>'.$langs->trans("Year").'</td><td>';
  207. if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
  208. arsort($arrayyears);
  209. print $form->selectarray('year',$arrayyears,$year,0);
  210. print '</td></tr>';
  211. print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
  212. print '</table>';
  213. print '</form>';
  214. print '<br><br>';
  215. print '<table class="border" width="100%">';
  216. print '<tr height="24">';
  217. print '<td align="center">'.$langs->trans("Year").'</td>';
  218. print '<td align="center">'.$langs->trans("Number").'</td>';
  219. print '<td align="center">'.$langs->trans("AmountTotal").'</td>';
  220. print '<td align="center">'.$langs->trans("AmountAverage").'</td>';
  221. print '</tr>';
  222. $oldyear=0;
  223. foreach ($data as $val)
  224. {
  225. $year = $val['year'];
  226. while ($year && $oldyear > $year+1)
  227. { // If we have empty year
  228. $oldyear--;
  229. print '<tr height="24">';
  230. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.'">'.$oldyear.'</a></td>';
  231. print '<td align="right">0</td>';
  232. print '<td align="right">0</td>';
  233. print '<td align="right">0</td>';
  234. print '</tr>';
  235. }
  236. print '<tr height="24">';
  237. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
  238. print '<td align="right">'.$val['nb'].'</td>';
  239. print '<td align="right">'.price(price2num($val['total'],'MT'),1).'</td>';
  240. print '<td align="right">'.price(price2num($val['avg'],'MT'),1).'</td>';
  241. print '</tr>';
  242. $oldyear=$year;
  243. }
  244. print '</table>';
  245. print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
  246. // Show graphs
  247. print '<table class="border" width="100%"><tr class="pair nohover"><td align="center">';
  248. if ($mesg) { print $mesg; }
  249. else {
  250. print $px1->show();
  251. print "<br>\n";
  252. print $px2->show();
  253. print "<br>\n";
  254. print $px3->show();
  255. }
  256. print '</td></tr></table>';
  257. print '</div></div></div>';
  258. print '<div style="clear:both"></div>';
  259. dol_fiche_end();
  260. // End of page
  261. llxFooter();
  262. $db->close();