index.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.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/expedition/stats/index.php
  21. * \ingroup expedition
  22. * \brief Page with shipment statistics
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionstats.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  29. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  30. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  31. $userid = GETPOST('userid', 'int');
  32. $socid = GETPOST('socid', 'int');
  33. // Security check
  34. if ($user->socid > 0) {
  35. $action = '';
  36. $socid = $user->socid;
  37. }
  38. $nowyear = dol_print_date(dol_now(), "%Y");
  39. $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
  40. $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  41. $endyear = $year;
  42. // Load translation files required by the page
  43. $langs->loadLangs(array('sendings', 'other', 'companies'));
  44. // Security check
  45. if ($user->socid) {
  46. $socid = $user->socid;
  47. }
  48. restrictedArea($user, 'expedition');
  49. /*
  50. * View
  51. */
  52. $form = new Form($db);
  53. llxHeader();
  54. print load_fiche_titre($langs->trans("StatisticsOfSendings"), '', 'dolly');
  55. dol_mkdir($dir);
  56. $stats = new ExpeditionStats($db, $socid, '', ($userid > 0 ? $userid : 0));
  57. // Build graphic number of object
  58. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  59. //var_dump($data);exit;
  60. // $data = array(array('Lib',val1,val2,val3),...)
  61. if (empty($user->rights->societe->client->voir) || $user->socid) {
  62. $filenamenb = $dir.'/shipmentsnbinyear-'.$user->id.'-'.$year.'.png';
  63. } else {
  64. $filenamenb = $dir.'/shipmentsnbinyear-'.$year.'.png';
  65. }
  66. $px1 = new DolGraph();
  67. $mesg = $px1->isGraphKo();
  68. if (!$mesg) {
  69. $px1->SetData($data);
  70. $i = $startyear; $legend = array();
  71. while ($i <= $endyear) {
  72. $legend[] = $i;
  73. $i++;
  74. }
  75. $px1->SetLegend($legend);
  76. $px1->SetMaxValue($px1->GetCeilMaxValue());
  77. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  78. $px1->SetWidth($WIDTH);
  79. $px1->SetHeight($HEIGHT);
  80. $px1->SetYLabel($langs->trans("NbOfSendings"));
  81. $px1->SetShading(3);
  82. $px1->SetHorizTickIncrement(1);
  83. $px1->mode = 'depth';
  84. $px1->SetTitle($langs->trans("NumberOfShipmentsByMonth"));
  85. $px1->draw($filenamenb, $fileurlnb);
  86. }
  87. // Build graphic amount of object
  88. /*
  89. $data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
  90. //var_dump($data);
  91. // $data = array(array('Lib',val1,val2,val3),...)
  92. if (empty($user->rights->societe->client->voir) || $user->socid)
  93. {
  94. $filenameamount = $dir.'/shipmentsamountinyear-'.$user->id.'-'.$year.'.png';
  95. }
  96. else
  97. {
  98. $filenameamount = $dir.'/shipmentsamountinyear-'.$year.'.png';
  99. }
  100. $px2 = new DolGraph();
  101. $mesg = $px2->isGraphKo();
  102. if (! $mesg)
  103. {
  104. $px2->SetData($data);
  105. $i=$startyear;$legend=array();
  106. while ($i <= $endyear)
  107. {
  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("AmountOfShipments"));
  117. $px2->SetShading(3);
  118. $px2->SetHorizTickIncrement(1);
  119. $px2->mode='depth';
  120. $px2->SetTitle($langs->trans("AmountOfShipmentsByMonthHT"));
  121. $px2->draw($filenameamount,$fileurlamount);
  122. }
  123. */
  124. /*
  125. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  126. if (empty($user->rights->societe->client->voir) || $user->socid)
  127. {
  128. $filename_avg = $dir.'/shipmentsaverage-'.$user->id.'-'.$year.'.png';
  129. }
  130. else
  131. {
  132. $filename_avg = $dir.'/shipmentsaverage-'.$year.'.png';
  133. }
  134. $px3 = new DolGraph();
  135. $mesg = $px3->isGraphKo();
  136. if (! $mesg)
  137. {
  138. $px3->SetData($data);
  139. $i=$startyear;$legend=array();
  140. while ($i <= $endyear)
  141. {
  142. $legend[]=$i;
  143. $i++;
  144. }
  145. $px3->SetLegend($legend);
  146. $px3->SetYLabel($langs->trans("AmountAverage"));
  147. $px3->SetMaxValue($px3->GetCeilMaxValue());
  148. $px3->SetMinValue($px3->GetFloorMinValue());
  149. $px3->SetWidth($WIDTH);
  150. $px3->SetHeight($HEIGHT);
  151. $px3->SetShading(3);
  152. $px3->SetHorizTickIncrement(1);
  153. $px3->mode='depth';
  154. $px3->SetTitle($langs->trans("AmountAverage"));
  155. $px3->draw($filename_avg,$fileurl_avg);
  156. }
  157. */
  158. // Show array
  159. $data = $stats->getAllByYear();
  160. $arrayyears = array();
  161. foreach ($data as $val) {
  162. if (!empty($val['year'])) {
  163. $arrayyears[$val['year']] = $val['year'];
  164. }
  165. }
  166. if (!count($arrayyears)) {
  167. $arrayyears[$nowyear] = $nowyear;
  168. }
  169. $h = 0;
  170. $head = array();
  171. $head[$h][0] = DOL_URL_ROOT.'/expedition/stats/index.php';
  172. $head[$h][1] = $langs->trans("ByMonthYear");
  173. $head[$h][2] = 'byyear';
  174. $h++;
  175. $type = 'shipment_stats';
  176. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  177. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  178. print '<div class="fichecenter"><div class="fichethirdleft">';
  179. // Show filter box
  180. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  181. print '<input type="hidden" name="token" value="'.newToken().'">';
  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. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  186. print img_picto('', 'company', 'class="pictofixedwidth"');
  187. print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  188. print '</td></tr>';
  189. // User
  190. print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
  191. print img_picto('', 'user', 'class="pictofixedwidth"');
  192. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  193. print '</td></tr>';
  194. // Year
  195. print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
  196. if (!in_array($year, $arrayyears)) {
  197. $arrayyears[$year] = $year;
  198. }
  199. if (!in_array($nowyear, $arrayyears)) {
  200. $arrayyears[$nowyear] = $nowyear;
  201. }
  202. arsort($arrayyears);
  203. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  204. print '</td></tr>';
  205. print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  206. print '</table>';
  207. print '</form>';
  208. print '<br><br>';
  209. print '<div class="div-table-responsive-no-min">';
  210. print '<table class="noborder centpercent">';
  211. print '<tr class="liste_titre" height="24">';
  212. print '<td class="center">'.$langs->trans("Year").'</td>';
  213. print '<td class="right">'.$langs->trans("NbOfSendings").'</td>';
  214. /*print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
  215. print '<td class="center">'.$langs->trans("AmountAverage").'</td>';*/
  216. print '</tr>';
  217. $oldyear = 0;
  218. foreach ($data as $val) {
  219. $year = $val['year'];
  220. while (!empty($year) && $oldyear > $year + 1) { // If we have empty year
  221. $oldyear--;
  222. print '<tr class="oddeven" height="24">';
  223. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'">'.$oldyear.'</a></td>';
  224. print '<td class="right">0</td>';
  225. /*print '<td class="right">0</td>';
  226. print '<td class="right">0</td>';*/
  227. print '</tr>';
  228. }
  229. print '<tr class="oddeven" height="24">';
  230. print '<td class="center">';
  231. if ($year) {
  232. print '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'">'.$year.'</a>';
  233. } else {
  234. // Technical error that should not happen
  235. print 'Error: validation date of shipment is not defined. This looks strange because shipment is validated. Try to run /install/repair.php?standard=confirmed';
  236. }
  237. print '</td>';
  238. print '<td class="right">'.$val['nb'].'</td>';
  239. /*print '<td class="right">'.price(price2num($val['total'],'MT'),1).'</td>';
  240. print '<td class="right">'.price(price2num($val['avg'],'MT'),1).'</td>';*/
  241. print '</tr>';
  242. $oldyear = $year;
  243. }
  244. print '</table>';
  245. print '</div>';
  246. print '</div><div class="fichetwothirdright">';
  247. // Show graphs
  248. print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
  249. if ($mesg) {
  250. print $mesg;
  251. } else {
  252. print $px1->show();
  253. print "<br>\n";
  254. /*print $px2->show();
  255. print "<br>\n";
  256. print $px3->show();*/
  257. }
  258. print '</td></tr></table>';
  259. print '</div></div>';
  260. print '<div style="clear:both"></div>';
  261. print dol_get_fiche_end();
  262. // TODO USe code similar to commande/stats/index.php instead of this one.
  263. /*
  264. print '<table class="border centpercent">';
  265. print '<tr><td class="center">'.$langs->trans("Year").'</td>';
  266. print '<td width="40%" class="center">'.$langs->trans("NbOfSendings").'</td></tr>';
  267. $sql = "SELECT count(*) as nb, date_format(date_expedition,'%Y') as dm";
  268. $sql.= " FROM ".MAIN_DB_PREFIX."expedition";
  269. $sql.= " WHERE fk_statut > 0";
  270. $sql.= " AND entity = ".$conf->entity;
  271. $sql.= " GROUP BY dm DESC";
  272. $resql=$db->query($sql);
  273. if ($resql)
  274. {
  275. $num = $db->num_rows($resql);
  276. $i = 0;
  277. while ($i < $num)
  278. {
  279. $row = $db->fetch_row($resql);
  280. $nbproduct = $row[0];
  281. $year = $row[1];
  282. print "<tr>";
  283. print '<td class="center"><a href="month.php?year='.$year.'">'.$year.'</a></td><td class="center">'.$nbproduct.'</td></tr>';
  284. $i++;
  285. }
  286. }
  287. $db->free($resql);
  288. print '</table>';
  289. */
  290. print '<br>';
  291. print '<i class="opacitymedium">'.$langs->trans("StatsOnShipmentsOnlyValidated").'</i>';
  292. // End of page
  293. llxFooter();
  294. $db->close();