index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. if ($mode == 'customer' && !$user->rights->commande->lire) {
  40. accessforbidden();
  41. }
  42. if ($mode == 'supplier' && empty($user->rights->fournisseur->commande->lire)) {
  43. accessforbidden();
  44. }
  45. if ($mode == 'supplier') {
  46. $object_status = GETPOST('object_status', 'array:int');
  47. $object_status = implode(',', $object_status);
  48. } else {
  49. $object_status = GETPOST('object_status', 'intcomma');
  50. }
  51. $typent_id = GETPOST('typent_id', 'int');
  52. $categ_id = GETPOST('categ_id', 'categ_id');
  53. $userid = GETPOST('userid', 'int');
  54. $socid = GETPOST('socid', 'int');
  55. // Security check
  56. if ($user->socid > 0) {
  57. $action = '';
  58. $socid = $user->socid;
  59. }
  60. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  61. $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
  62. $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  63. $endyear = $year;
  64. // Load translation files required by the page
  65. $langs->loadLangs(array('orders', 'companies', 'other', 'suppliers'));
  66. /*
  67. * View
  68. */
  69. $form = new Form($db);
  70. $formorder = new FormOrder($db);
  71. $formcompany = new FormCompany($db);
  72. $formother = new FormOther($db);
  73. $picto = 'order';
  74. $title = $langs->trans("OrdersStatistics");
  75. $dir = $conf->commande->dir_temp;
  76. if ($mode == 'supplier') {
  77. $picto = 'supplier_order';
  78. $title = $langs->trans("OrdersStatisticsSuppliers");
  79. $dir = $conf->fournisseur->commande->dir_temp;
  80. }
  81. llxHeader('', $title);
  82. print load_fiche_titre($title, '', $picto);
  83. dol_mkdir($dir);
  84. $stats = new CommandeStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
  85. if ($mode == 'customer') {
  86. if ($object_status != '' && $object_status >= -1) {
  87. $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($object_status).')';
  88. }
  89. }
  90. if ($mode == 'supplier') {
  91. if ($object_status != '' && $object_status >= 0) {
  92. $stats->where .= ' AND c.fk_statut IN ('.$db->sanitize($object_status).')';
  93. }
  94. }
  95. // Build graphic number of object
  96. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  97. //var_dump($data);
  98. // $data = array(array('Lib',val1,val2,val3),...)
  99. if (empty($user->rights->societe->client->voir) || $user->socid) {
  100. $filenamenb = $dir.'/ordersnbinyear-'.$user->id.'-'.$year.'.png';
  101. if ($mode == 'customer') {
  102. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  103. }
  104. if ($mode == 'supplier') {
  105. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  106. }
  107. } else {
  108. $filenamenb = $dir.'/ordersnbinyear-'.$year.'.png';
  109. if ($mode == 'customer') {
  110. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$year.'.png';
  111. }
  112. if ($mode == 'supplier') {
  113. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$year.'.png';
  114. }
  115. }
  116. $px1 = new DolGraph();
  117. $mesg = $px1->isGraphKo();
  118. if (!$mesg) {
  119. $px1->SetData($data);
  120. $i = $startyear; $legend = array();
  121. while ($i <= $endyear) {
  122. $legend[] = $i;
  123. $i++;
  124. }
  125. $px1->SetLegend($legend);
  126. $px1->SetMaxValue($px1->GetCeilMaxValue());
  127. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  128. $px1->SetWidth($WIDTH);
  129. $px1->SetHeight($HEIGHT);
  130. $px1->SetYLabel($langs->trans("NbOfOrder"));
  131. $px1->SetShading(3);
  132. $px1->SetHorizTickIncrement(1);
  133. $px1->mode = 'depth';
  134. $px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
  135. $px1->draw($filenamenb, $fileurlnb);
  136. }
  137. // Build graphic amount of object
  138. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
  139. //var_dump($data);
  140. // $data = array(array('Lib',val1,val2,val3),...)
  141. if (empty($user->rights->societe->client->voir) || $user->socid) {
  142. $filenameamount = $dir.'/ordersamountinyear-'.$user->id.'-'.$year.'.png';
  143. if ($mode == 'customer') {
  144. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  145. }
  146. if ($mode == 'supplier') {
  147. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  148. }
  149. } else {
  150. $filenameamount = $dir.'/ordersamountinyear-'.$year.'.png';
  151. if ($mode == 'customer') {
  152. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$year.'.png';
  153. }
  154. if ($mode == 'supplier') {
  155. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$year.'.png';
  156. }
  157. }
  158. $px2 = new DolGraph();
  159. $mesg = $px2->isGraphKo();
  160. if (!$mesg) {
  161. $px2->SetData($data);
  162. $i = $startyear; $legend = array();
  163. while ($i <= $endyear) {
  164. $legend[] = $i;
  165. $i++;
  166. }
  167. $px2->SetLegend($legend);
  168. $px2->SetMaxValue($px2->GetCeilMaxValue());
  169. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  170. $px2->SetWidth($WIDTH);
  171. $px2->SetHeight($HEIGHT);
  172. $px2->SetYLabel($langs->trans("AmountOfOrders"));
  173. $px2->SetShading(3);
  174. $px2->SetHorizTickIncrement(1);
  175. $px2->mode = 'depth';
  176. $px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
  177. $px2->draw($filenameamount, $fileurlamount);
  178. }
  179. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  180. if (empty($user->rights->societe->client->voir) || $user->socid) {
  181. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  182. if ($mode == 'customer') {
  183. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  184. }
  185. if ($mode == 'supplier') {
  186. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  187. }
  188. } else {
  189. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  190. if ($mode == 'customer') {
  191. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  192. }
  193. if ($mode == 'supplier') {
  194. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  195. }
  196. }
  197. $px3 = new DolGraph();
  198. $mesg = $px3->isGraphKo();
  199. if (!$mesg) {
  200. $px3->SetData($data);
  201. $i = $startyear; $legend = array();
  202. while ($i <= $endyear) {
  203. $legend[] = $i;
  204. $i++;
  205. }
  206. $px3->SetLegend($legend);
  207. $px3->SetYLabel($langs->trans("AmountAverage"));
  208. $px3->SetMaxValue($px3->GetCeilMaxValue());
  209. $px3->SetMinValue($px3->GetFloorMinValue());
  210. $px3->SetWidth($WIDTH);
  211. $px3->SetHeight($HEIGHT);
  212. $px3->SetShading(3);
  213. $px3->SetHorizTickIncrement(1);
  214. $px3->mode = 'depth';
  215. $px3->SetTitle($langs->trans("AmountAverage"));
  216. $px3->draw($filename_avg, $fileurl_avg);
  217. }
  218. // Show array
  219. $data = $stats->getAllByYear();
  220. $arrayyears = array();
  221. foreach ($data as $val) {
  222. if (!empty($val['year'])) {
  223. $arrayyears[$val['year']] = $val['year'];
  224. }
  225. }
  226. if (!count($arrayyears)) {
  227. $arrayyears[$nowyear] = $nowyear;
  228. }
  229. $h = 0;
  230. $head = array();
  231. $head[$h][0] = DOL_URL_ROOT.'/commande/stats/index.php?mode='.$mode;
  232. $head[$h][1] = $langs->trans("ByMonthYear");
  233. $head[$h][2] = 'byyear';
  234. $h++;
  235. if ($mode == 'customer') {
  236. $type = 'order_stats';
  237. }
  238. if ($mode == 'supplier') {
  239. $type = 'supplier_order_stats';
  240. }
  241. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  242. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  243. print '<div class="fichecenter"><div class="fichethirdleft">';
  244. // Show filter box
  245. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  246. print '<input type="hidden" name="token" value="'.newToken().'">';
  247. print '<input type="hidden" name="mode" value="'.$mode.'">';
  248. print '<table class="noborder centpercent">';
  249. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  250. // Company
  251. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  252. if ($mode == 'customer') {
  253. $filter = 's.client IN (1,2,3)';
  254. }
  255. if ($mode == 'supplier') {
  256. $filter = 's.fournisseur = 1';
  257. }
  258. print img_picto('', 'company', 'class="pictofixedwidth"');
  259. print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300');
  260. print '</td></tr>';
  261. // ThirdParty Type
  262. print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
  263. $sortparam_typent = (empty($conf->global->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.
  264. print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1);
  265. if ($user->admin) {
  266. print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  267. }
  268. print '</td></tr>';
  269. // Category
  270. if ($mode == 'customer') {
  271. $cat_type = Categorie::TYPE_CUSTOMER;
  272. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
  273. }
  274. if ($mode == 'supplier') {
  275. $cat_type = Categorie::TYPE_SUPPLIER;
  276. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
  277. }
  278. print '<tr><td>'.$cat_label.'</td><td>';
  279. print img_picto('', 'category', 'class="pictofixedwidth"');
  280. print $formother->select_categories($cat_type, $categ_id, 'categ_id', 0, 1, 'widthcentpercentminusx maxwidth300');
  281. print '</td></tr>';
  282. // User
  283. print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
  284. print img_picto('', 'user', 'class="pictofixedwidth"');
  285. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  286. // Status
  287. print '<tr><td>'.$langs->trans("Status").'</td><td>';
  288. if ($mode == 'customer') {
  289. $liststatus = array(
  290. Commande::STATUS_DRAFT=>$langs->trans("StatusOrderDraft"),
  291. Commande::STATUS_VALIDATED=>$langs->trans("StatusOrderValidated"),
  292. Commande::STATUS_SHIPMENTONPROCESS=>$langs->trans("StatusOrderSent"),
  293. Commande::STATUS_CLOSED=>$langs->trans("StatusOrderDelivered"),
  294. Commande::STATUS_CANCELED=>$langs->trans("StatusOrderCanceled")
  295. );
  296. print $form->selectarray('object_status', $liststatus, GETPOST('object_status', 'intcomma'), -4);
  297. }
  298. if ($mode == 'supplier') {
  299. $formorder->selectSupplierOrderStatus((strstr($object_status, ',') ? -1 : $object_status), 0, 'object_status');
  300. }
  301. print '</td></tr>';
  302. // Year
  303. print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
  304. if (!in_array($year, $arrayyears)) {
  305. $arrayyears[$year] = $year;
  306. }
  307. if (!in_array($nowyear, $arrayyears)) {
  308. $arrayyears[$nowyear] = $nowyear;
  309. }
  310. arsort($arrayyears);
  311. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  312. print '</td></tr>';
  313. print '<tr><td align="center" colspan="2"><input type="submit" class="button small" name="submit" value="'.$langs->trans("Refresh").'"></td></tr>';
  314. print '</table>';
  315. print '</form>';
  316. print '<br><br>';
  317. print '<div class="div-table-responsive-no-min">';
  318. print '<table class="noborder centpercent">';
  319. print '<tr class="liste_titre" height="24">';
  320. print '<td class="center">'.$langs->trans("Year").'</td>';
  321. print '<td class="right">'.$langs->trans("NbOfOrders").'</td>';
  322. print '<td class="right">%</td>';
  323. print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
  324. print '<td class="right">%</td>';
  325. print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
  326. print '<td class="right">%</td>';
  327. print '</tr>';
  328. $oldyear = 0;
  329. foreach ($data as $val) {
  330. $year = $val['year'];
  331. while (!empty($year) && $oldyear > $year + 1) { // If we have empty year
  332. $oldyear--;
  333. print '<tr class="oddeven" height="24">';
  334. 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>';
  335. print '<td class="right">0</td>';
  336. print '<td class="right"></td>';
  337. print '<td class="right">0</td>';
  338. print '<td class="right"></td>';
  339. print '<td class="right">0</td>';
  340. print '<td class="right"></td>';
  341. print '</tr>';
  342. }
  343. print '<tr class="oddeven" height="24">';
  344. 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>';
  345. print '<td class="right">'.$val['nb'].'</td>';
  346. 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>';
  347. print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
  348. 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>';
  349. print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
  350. 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>';
  351. print '</tr>';
  352. $oldyear = $year;
  353. }
  354. print '</table>';
  355. print '</div>';
  356. print '</div><div class="fichetwothirdright">';
  357. // Show graphs
  358. print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
  359. if ($mesg) {
  360. print $mesg;
  361. } else {
  362. print $px1->show();
  363. print "<br>\n";
  364. print $px2->show();
  365. print "<br>\n";
  366. print $px3->show();
  367. }
  368. print '</td></tr></table>';
  369. print '</div></div>';
  370. print '<div style="clear:both"></div>';
  371. print dol_get_fiche_end();
  372. // End of page
  373. llxFooter();
  374. $db->close();