index.php 9.5 KB

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