index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, getDolGlobalString('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. $dir = (!empty($conf->expedition->multidir_temp[$conf->entity]) ? $conf->expedition->multidir_temp[$conf->entity] : $conf->service->multidir_temp[$conf->entity]);
  56. dol_mkdir($dir);
  57. $stats = new ExpeditionStats($db, $socid, '', ($userid > 0 ? $userid : 0));
  58. // Build graphic number of object
  59. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  60. //var_dump($data);exit;
  61. // $data = array(array('Lib',val1,val2,val3),...)
  62. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  63. $filenamenb = $dir.'/shipmentsnbinyear-'.$user->id.'-'.$year.'.png';
  64. } else {
  65. $filenamenb = $dir.'/shipmentsnbinyear-'.$year.'.png';
  66. }
  67. $px1 = new DolGraph();
  68. $mesg = $px1->isGraphKo();
  69. $fileurlnb = '';
  70. if (!$mesg) {
  71. $px1->SetData($data);
  72. $i = $startyear;
  73. $legend = array();
  74. while ($i <= $endyear) {
  75. $legend[] = $i;
  76. $i++;
  77. }
  78. $px1->SetLegend($legend);
  79. $px1->SetMaxValue($px1->GetCeilMaxValue());
  80. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  81. $px1->SetWidth($WIDTH);
  82. $px1->SetHeight($HEIGHT);
  83. $px1->SetYLabel($langs->trans("NbOfSendings"));
  84. $px1->SetShading(3);
  85. $px1->SetHorizTickIncrement(1);
  86. $px1->mode = 'depth';
  87. $px1->SetTitle($langs->trans("NumberOfShipmentsByMonth"));
  88. $px1->draw($filenamenb, $fileurlnb);
  89. }
  90. // Build graphic amount of object
  91. /*
  92. $data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
  93. //var_dump($data);
  94. // $data = array(array('Lib',val1,val2,val3),...)
  95. if (empty($user->rights->societe->client->voir) || $user->socid)
  96. {
  97. $filenameamount = $dir.'/shipmentsamountinyear-'.$user->id.'-'.$year.'.png';
  98. }
  99. else
  100. {
  101. $filenameamount = $dir.'/shipmentsamountinyear-'.$year.'.png';
  102. }
  103. $px2 = new DolGraph();
  104. $mesg = $px2->isGraphKo();
  105. if (! $mesg)
  106. {
  107. $px2->SetData($data);
  108. $i=$startyear;$legend=array();
  109. while ($i <= $endyear)
  110. {
  111. $legend[]=$i;
  112. $i++;
  113. }
  114. $px2->SetLegend($legend);
  115. $px2->SetMaxValue($px2->GetCeilMaxValue());
  116. $px2->SetMinValue(min(0,$px2->GetFloorMinValue()));
  117. $px2->SetWidth($WIDTH);
  118. $px2->SetHeight($HEIGHT);
  119. $px2->SetYLabel($langs->trans("AmountOfShipments"));
  120. $px2->SetShading(3);
  121. $px2->SetHorizTickIncrement(1);
  122. $px2->mode='depth';
  123. $px2->SetTitle($langs->trans("AmountOfShipmentsByMonthHT"));
  124. $px2->draw($filenameamount,$fileurlamount);
  125. }
  126. */
  127. /*
  128. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  129. if (empty($user->rights->societe->client->voir) || $user->socid)
  130. {
  131. $filename_avg = $dir.'/shipmentsaverage-'.$user->id.'-'.$year.'.png';
  132. }
  133. else
  134. {
  135. $filename_avg = $dir.'/shipmentsaverage-'.$year.'.png';
  136. }
  137. $px3 = new DolGraph();
  138. $mesg = $px3->isGraphKo();
  139. if (! $mesg)
  140. {
  141. $px3->SetData($data);
  142. $i=$startyear;$legend=array();
  143. while ($i <= $endyear)
  144. {
  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. */
  161. // Show array
  162. $data = $stats->getAllByYear();
  163. $arrayyears = array();
  164. foreach ($data as $val) {
  165. if (!empty($val['year'])) {
  166. $arrayyears[$val['year']] = $val['year'];
  167. }
  168. }
  169. if (!count($arrayyears)) {
  170. $arrayyears[$nowyear] = $nowyear;
  171. }
  172. $h = 0;
  173. $head = array();
  174. $head[$h][0] = DOL_URL_ROOT.'/expedition/stats/index.php';
  175. $head[$h][1] = $langs->trans("ByMonthYear");
  176. $head[$h][2] = 'byyear';
  177. $h++;
  178. $type = 'shipment_stats';
  179. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  180. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  181. print '<div class="fichecenter"><div class="fichethirdleft">';
  182. // Show filter box
  183. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  184. print '<input type="hidden" name="token" value="'.newToken().'">';
  185. print '<table class="noborder centpercent">';
  186. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  187. // Company
  188. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  189. print img_picto('', 'company', 'class="pictofixedwidth"');
  190. print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  191. print '</td></tr>';
  192. // User
  193. print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
  194. print img_picto('', 'user', 'class="pictofixedwidth"');
  195. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  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("NbOfSendings").'</td>';
  217. /*print '<td class="center">'.$langs->trans("AmountTotal").'</td>';
  218. print '<td class="center">'.$langs->trans("AmountAverage").'</td>';*/
  219. print '</tr>';
  220. $oldyear = 0;
  221. foreach ($data as $val) {
  222. $year = $val['year'];
  223. while (!empty($year) && $oldyear > $year + 1) { // If we have empty year
  224. $oldyear--;
  225. print '<tr class="oddeven" height="24">';
  226. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'">'.$oldyear.'</a></td>';
  227. print '<td class="right">0</td>';
  228. /*print '<td class="right">0</td>';
  229. print '<td class="right">0</td>';*/
  230. print '</tr>';
  231. }
  232. print '<tr class="oddeven" height="24">';
  233. print '<td class="center">';
  234. if ($year) {
  235. print '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'">'.$year.'</a>';
  236. } else {
  237. // Technical error that should not happen
  238. print 'Error: validation date of shipment is not defined. This looks strange because shipment is validated. Try to run /install/repair.php?standard=confirmed';
  239. }
  240. print '</td>';
  241. print '<td class="right">'.$val['nb'].'</td>';
  242. /*print '<td class="right">'.price(price2num($val['total'],'MT'),1).'</td>';
  243. print '<td class="right">'.price(price2num($val['avg'],'MT'),1).'</td>';*/
  244. print '</tr>';
  245. $oldyear = $year;
  246. }
  247. print '</table>';
  248. print '</div>';
  249. print '</div><div class="fichetwothirdright">';
  250. // Show graphs
  251. print '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
  252. if ($mesg) {
  253. print $mesg;
  254. } else {
  255. print $px1->show();
  256. print "<br>\n";
  257. /*print $px2->show();
  258. print "<br>\n";
  259. print $px3->show();*/
  260. }
  261. print '</td></tr></table>';
  262. print '</div></div>';
  263. print '<div class="clearboth"></div>';
  264. print dol_get_fiche_end();
  265. // TODO USe code similar to commande/stats/index.php instead of this one.
  266. /*
  267. print '<table class="border centpercent">';
  268. print '<tr><td class="center">'.$langs->trans("Year").'</td>';
  269. print '<td width="40%" class="center">'.$langs->trans("NbOfSendings").'</td></tr>';
  270. $sql = "SELECT count(*) as nb, date_format(date_expedition,'%Y') as dm";
  271. $sql.= " FROM ".MAIN_DB_PREFIX."expedition";
  272. $sql.= " WHERE fk_statut > 0";
  273. $sql.= " AND entity = ".$conf->entity;
  274. $sql.= " GROUP BY dm DESC";
  275. $resql=$db->query($sql);
  276. if ($resql)
  277. {
  278. $num = $db->num_rows($resql);
  279. $i = 0;
  280. while ($i < $num)
  281. {
  282. $row = $db->fetch_row($resql);
  283. $nbproduct = $row[0];
  284. $year = $row[1];
  285. print "<tr>";
  286. print '<td class="center"><a href="month.php?year='.$year.'">'.$year.'</a></td><td class="center">'.$nbproduct.'</td></tr>';
  287. $i++;
  288. }
  289. }
  290. $db->free($resql);
  291. print '</table>';
  292. */
  293. print '<br>';
  294. print '<i class="opacitymedium">'.$langs->trans("StatsOnShipmentsOnlyValidated").'</i>';
  295. // End of page
  296. llxFooter();
  297. $db->close();