index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  7. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/commande/stats/index.php
  24. * \ingroup commande
  25. * \brief Page with customers or suppliers orders statistics
  26. */
  27. // Load Dolibarr environment
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  36. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  37. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  38. $mode = GETPOSTISSET("mode") ? GETPOST("mode", 'aZ09') : 'customer';
  39. $usercanreadcustumerstatistic = $user->hasRight('commande', 'lire');
  40. $usercanreadsupplierstatistic = $user->hasRight('fournisseur', 'commande', 'lire');
  41. if (getDolGlobalInt('MAIN_NEED_EXPORT_PERMISSION_TO_READ_STATISTICS')) {
  42. $usercanreadcustumerstatistic = $user->hasRight('commande', 'commande', 'export');
  43. $usercanreadsupplierstatistic = $user->hasRight('fournisseur', 'commande', 'export');
  44. }
  45. if ($mode == 'customer' && !$usercanreadcustumerstatistic) {
  46. accessforbidden();
  47. }
  48. if ($mode == 'supplier' && !$usercanreadsupplierstatistic) {
  49. accessforbidden();
  50. }
  51. if ($mode == 'supplier') {
  52. $object_status = GETPOST('object_status', 'array:int');
  53. $object_status = implode(',', $object_status);
  54. } else {
  55. $object_status = GETPOST('object_status', 'intcomma');
  56. }
  57. $typent_id = GETPOST('typent_id', 'int');
  58. $categ_id = GETPOST('categ_id', 'categ_id');
  59. $userid = GETPOST('userid', 'int');
  60. $socid = GETPOST('socid', 'int');
  61. // Security check
  62. if ($user->socid > 0) {
  63. $action = '';
  64. $socid = $user->socid;
  65. }
  66. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  67. $year = GETPOST('year') > 0 ? GETPOST('year') : $nowyear;
  68. $startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  69. $endyear = $year;
  70. // Load translation files required by the page
  71. $langs->loadLangs(array('orders', 'companies', 'other', 'suppliers'));
  72. /*
  73. * View
  74. */
  75. $form = new Form($db);
  76. $formorder = new FormOrder($db);
  77. $formcompany = new FormCompany($db);
  78. $formother = new FormOther($db);
  79. $picto = 'order';
  80. $title = $langs->trans("OrdersStatistics");
  81. $dir = $conf->commande->dir_temp;
  82. if ($mode == 'supplier') {
  83. $picto = 'supplier_order';
  84. $title = $langs->trans("OrdersStatisticsSuppliers");
  85. $dir = $conf->fournisseur->commande->dir_temp;
  86. }
  87. llxHeader('', $title);
  88. print load_fiche_titre($title, '', $picto);
  89. dol_mkdir($dir);
  90. $stats = new CommandeStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
  91. if ($mode == 'customer') {
  92. if ($object_status != '' && $object_status >= -1) {
  93. $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($object_status).')';
  94. }
  95. }
  96. if ($mode == 'supplier') {
  97. if ($object_status != '' && $object_status >= 0) {
  98. $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($object_status).')';
  99. }
  100. }
  101. // Build graphic number of object
  102. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  103. //var_dump($data);
  104. // $data = array(array('Lib',val1,val2,val3),...)
  105. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  106. $filenamenb = $dir.'/ordersnbinyear-'.$user->id.'-'.$year.'.png';
  107. if ($mode == 'customer') {
  108. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  109. }
  110. if ($mode == 'supplier') {
  111. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  112. }
  113. } else {
  114. $filenamenb = $dir.'/ordersnbinyear-'.$year.'.png';
  115. if ($mode == 'customer') {
  116. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$year.'.png';
  117. }
  118. if ($mode == 'supplier') {
  119. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$year.'.png';
  120. }
  121. }
  122. $px1 = new DolGraph();
  123. $mesg = $px1->isGraphKo();
  124. if (!$mesg) {
  125. $px1->SetData($data);
  126. $i = $startyear;
  127. $legend = array();
  128. while ($i <= $endyear) {
  129. $legend[] = $i;
  130. $i++;
  131. }
  132. $px1->SetLegend($legend);
  133. $px1->SetMaxValue($px1->GetCeilMaxValue());
  134. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  135. $px1->SetWidth($WIDTH);
  136. $px1->SetHeight($HEIGHT);
  137. $px1->SetYLabel($langs->trans("NbOfOrder"));
  138. $px1->SetShading(3);
  139. $px1->SetHorizTickIncrement(1);
  140. $px1->mode = 'depth';
  141. $px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
  142. $px1->draw($filenamenb, $fileurlnb);
  143. }
  144. // Build graphic amount of object
  145. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
  146. //var_dump($data);
  147. // $data = array(array('Lib',val1,val2,val3),...)
  148. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  149. $filenameamount = $dir.'/ordersamountinyear-'.$user->id.'-'.$year.'.png';
  150. if ($mode == 'customer') {
  151. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  152. }
  153. if ($mode == 'supplier') {
  154. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  155. }
  156. } else {
  157. $filenameamount = $dir.'/ordersamountinyear-'.$year.'.png';
  158. if ($mode == 'customer') {
  159. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$year.'.png';
  160. }
  161. if ($mode == 'supplier') {
  162. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$year.'.png';
  163. }
  164. }
  165. $px2 = new DolGraph();
  166. $mesg = $px2->isGraphKo();
  167. if (!$mesg) {
  168. $px2->SetData($data);
  169. $i = $startyear;
  170. $legend = array();
  171. while ($i <= $endyear) {
  172. $legend[] = $i;
  173. $i++;
  174. }
  175. $px2->SetLegend($legend);
  176. $px2->SetMaxValue($px2->GetCeilMaxValue());
  177. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  178. $px2->SetWidth($WIDTH);
  179. $px2->SetHeight($HEIGHT);
  180. $px2->SetYLabel($langs->trans("AmountOfOrders"));
  181. $px2->SetShading(3);
  182. $px2->SetHorizTickIncrement(1);
  183. $px2->mode = 'depth';
  184. $px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
  185. $px2->draw($filenameamount, $fileurlamount);
  186. }
  187. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  188. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  189. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  190. if ($mode == 'customer') {
  191. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  192. }
  193. if ($mode == 'supplier') {
  194. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  195. }
  196. } else {
  197. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  198. if ($mode == 'customer') {
  199. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  200. }
  201. if ($mode == 'supplier') {
  202. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  203. }
  204. }
  205. $px3 = new DolGraph();
  206. $mesg = $px3->isGraphKo();
  207. if (!$mesg) {
  208. $px3->SetData($data);
  209. $i = $startyear;
  210. $legend = array();
  211. while ($i <= $endyear) {
  212. $legend[] = $i;
  213. $i++;
  214. }
  215. $px3->SetLegend($legend);
  216. $px3->SetYLabel($langs->trans("AmountAverage"));
  217. $px3->SetMaxValue($px3->GetCeilMaxValue());
  218. $px3->SetMinValue($px3->GetFloorMinValue());
  219. $px3->SetWidth($WIDTH);
  220. $px3->SetHeight($HEIGHT);
  221. $px3->SetShading(3);
  222. $px3->SetHorizTickIncrement(1);
  223. $px3->mode = 'depth';
  224. $px3->SetTitle($langs->trans("AmountAverage"));
  225. $px3->draw($filename_avg, $fileurl_avg);
  226. }
  227. // Show array
  228. $data = $stats->getAllByYear();
  229. $arrayyears = array();
  230. foreach ($data as $val) {
  231. if (!empty($val['year'])) {
  232. $arrayyears[$val['year']] = $val['year'];
  233. }
  234. }
  235. if (!count($arrayyears)) {
  236. $arrayyears[$nowyear] = $nowyear;
  237. }
  238. $h = 0;
  239. $head = array();
  240. $head[$h][0] = DOL_URL_ROOT.'/commande/stats/index.php?mode='.$mode;
  241. $head[$h][1] = $langs->trans("ByMonthYear");
  242. $head[$h][2] = 'byyear';
  243. $h++;
  244. if ($mode == 'customer') {
  245. $type = 'order_stats';
  246. }
  247. if ($mode == 'supplier') {
  248. $type = 'supplier_order_stats';
  249. }
  250. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  251. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  252. print '<div class="fichecenter"><div class="fichethirdleft">';
  253. // Show filter box
  254. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  255. print '<input type="hidden" name="token" value="'.newToken().'">';
  256. print '<input type="hidden" name="mode" value="'.$mode.'">';
  257. print '<table class="noborder centpercent">';
  258. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  259. // Company
  260. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  261. $filter = '';
  262. if ($mode == 'customer') {
  263. $filter = '(s.client:IN:1,2,3)';
  264. }
  265. if ($mode == 'supplier') {
  266. $filter = '(s.fournisseur:=:1)';
  267. }
  268. print img_picto('', 'company', 'class="pictofixedwidth"');
  269. print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300');
  270. print '</td></tr>';
  271. // ThirdParty Type
  272. print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
  273. $sortparam_typent = (!getDolGlobalString('SOCIETE_SORT_ON_TYPEENT') ? 'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT); // NONE means we keep sort of original array, so we sort on position. ASC, means next function will sort on label.
  274. print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1);
  275. if ($user->admin) {
  276. print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  277. }
  278. print '</td></tr>';
  279. // Category
  280. if ($mode == 'customer') {
  281. $cat_type = Categorie::TYPE_CUSTOMER;
  282. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
  283. }
  284. if ($mode == 'supplier') {
  285. $cat_type = Categorie::TYPE_SUPPLIER;
  286. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
  287. }
  288. print '<tr><td>'.$cat_label.'</td><td>';
  289. print img_picto('', 'category', 'class="pictofixedwidth"');
  290. print $formother->select_categories($cat_type, $categ_id, 'categ_id', 0, 1, 'widthcentpercentminusx maxwidth300');
  291. print '</td></tr>';
  292. // User
  293. print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
  294. print img_picto('', 'user', 'class="pictofixedwidth"');
  295. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  296. // Status
  297. print '<tr><td>'.$langs->trans("Status").'</td><td>';
  298. if ($mode == 'customer') {
  299. $liststatus = array(
  300. Commande::STATUS_DRAFT => $langs->trans("StatusOrderDraft"),
  301. Commande::STATUS_VALIDATED => $langs->trans("StatusOrderValidated"),
  302. Commande::STATUS_SHIPMENTONPROCESS => $langs->trans("StatusOrderSent"),
  303. Commande::STATUS_CLOSED => $langs->trans("StatusOrderDelivered"),
  304. Commande::STATUS_CANCELED => $langs->trans("StatusOrderCanceled")
  305. );
  306. print $form->selectarray('object_status', $liststatus, GETPOST('object_status', 'intcomma'), -4);
  307. }
  308. if ($mode == 'supplier') {
  309. $formorder->selectSupplierOrderStatus((strstr($object_status, ',') ? -1 : $object_status), 0, 'object_status');
  310. }
  311. print '</td></tr>';
  312. // Year
  313. print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
  314. if (!in_array($year, $arrayyears)) {
  315. $arrayyears[$year] = $year;
  316. }
  317. if (!in_array($nowyear, $arrayyears)) {
  318. $arrayyears[$nowyear] = $nowyear;
  319. }
  320. arsort($arrayyears);
  321. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  322. print '</td></tr>';
  323. print '<tr><td align="center" colspan="2"><input type="submit" class="button small" name="submit" value="'.$langs->trans("Refresh").'"></td></tr>';
  324. print '</table>';
  325. print '</form>';
  326. print '<br><br>';
  327. print '<div class="div-table-responsive-no-min">';
  328. print '<table class="noborder centpercent">';
  329. print '<tr class="liste_titre" height="24">';
  330. print '<td class="center">'.$langs->trans("Year").'</td>';
  331. print '<td class="right">'.$langs->trans("NbOfOrders").'</td>';
  332. print '<td class="right">%</td>';
  333. print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
  334. print '<td class="right">%</td>';
  335. print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
  336. print '<td class="right">%</td>';
  337. print '</tr>';
  338. $oldyear = 0;
  339. foreach ($data as $val) {
  340. $year = $val['year'];
  341. while (!empty($year) && $oldyear > $year + 1) { // If we have empty year
  342. $oldyear--;
  343. print '<tr class="oddeven" height="24">';
  344. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
  345. print '<td class="right">0</td>';
  346. print '<td class="right"></td>';
  347. print '<td class="right">0</td>';
  348. print '<td class="right"></td>';
  349. print '<td class="right">0</td>';
  350. print '<td class="right"></td>';
  351. print '</tr>';
  352. }
  353. print '<tr class="oddeven" height="24">';
  354. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  355. print '<td class="right">'.$val['nb'].'</td>';
  356. print '<td class="right opacitylow" style="'.((!isset($val['nb_diff']) || $val['nb_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['nb_diff']) ? round($val['nb_diff']) : "0").'%</td>';
  357. print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
  358. print '<td class="right opacitylow" style="'.((!isset($val['total_diff']) || $val['total_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['total_diff']) ? round($val['total_diff']) : "0").'%</td>';
  359. print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
  360. print '<td class="right opacitylow" style="'.((!isset($val['avg_diff']) || $val['avg_diff'] >= 0) ? 'color: green;' : 'color: red;').'">'.(isset($val['avg_diff']) ? round($val['avg_diff']) : "0").'%</td>';
  361. print '</tr>';
  362. $oldyear = $year;
  363. }
  364. print '</table>';
  365. print '</div>';
  366. print '</div><div class="fichetwothirdright">';
  367. // Show graphs
  368. print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
  369. if ($mesg) {
  370. print $mesg;
  371. } else {
  372. print $px1->show();
  373. print "<br>\n";
  374. print $px2->show();
  375. print "<br>\n";
  376. print $px3->show();
  377. }
  378. print '</td></tr></table>';
  379. print '</div></div>';
  380. print '<div class="clearboth"></div>';
  381. print dol_get_fiche_end();
  382. // End of page
  383. llxFooter();
  384. $db->close();