index.php 15 KB

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