index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/commande/stats/index.php
  23. * \ingroup commande
  24. * \brief Page with customers or suppliers orders statistics
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  31. $WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
  32. $HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
  33. $mode=GETPOST("mode")?GETPOST("mode"):'customer';
  34. if ($mode == 'customer' && ! $user->rights->commande->lire) accessforbidden();
  35. if ($mode == 'supplier' && ! $user->rights->fournisseur->commande->lire) accessforbidden();
  36. $object_status=GETPOST('object_status');
  37. $userid=GETPOST('userid','int');
  38. $socid=GETPOST('socid','int');
  39. // Security check
  40. if ($user->societe_id > 0)
  41. {
  42. $action = '';
  43. $socid = $user->societe_id;
  44. }
  45. $nowyear=strftime("%Y", dol_now());
  46. $year = GETPOST('year')>0?GETPOST('year'):$nowyear;
  47. //$startyear=$year-2;
  48. $startyear=$year-1;
  49. $endyear=$year;
  50. // Load translation files required by the page
  51. $langs->loadLangs(array('orders', 'companies', 'other', 'suppliers'));
  52. /*
  53. * View
  54. */
  55. $form=new Form($db);
  56. $formorder=new FormOrder($db);
  57. if ($mode == 'customer')
  58. {
  59. $title=$langs->trans("OrdersStatistics");
  60. $dir=$conf->commande->dir_temp;
  61. }
  62. if ($mode == 'supplier')
  63. {
  64. $title=$langs->trans("OrdersStatisticsSuppliers").' ('.$langs->trans("SentToSuppliers").")";
  65. $dir=$conf->fournisseur->commande->dir_temp;
  66. }
  67. llxHeader('', $title);
  68. print load_fiche_titre($title,'','title_commercial.png');
  69. dol_mkdir($dir);
  70. $stats = new CommandeStats($db, $socid, $mode, ($userid>0?$userid:0));
  71. if ($mode == 'customer')
  72. {
  73. if ($object_status != '' && $object_status >= -1) $stats->where .= ' AND c.fk_statut IN ('.$db->escape($object_status).')';
  74. }
  75. if ($mode == 'supplier')
  76. {
  77. if ($object_status != '' && $object_status >= 0) $stats->where .= ' AND c.fk_statut IN ('.$db->escape($object_status).')';
  78. }
  79. // Build graphic number of object
  80. $data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
  81. //var_dump($data);
  82. // $data = array(array('Lib',val1,val2,val3),...)
  83. if (!$user->rights->societe->client->voir || $user->societe_id)
  84. {
  85. $filenamenb = $dir.'/ordersnbinyear-'.$user->id.'-'.$year.'.png';
  86. if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  87. if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
  88. }
  89. else
  90. {
  91. $filenamenb = $dir.'/ordersnbinyear-'.$year.'.png';
  92. if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$year.'.png';
  93. if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$year.'.png';
  94. }
  95. $px1 = new DolGraph();
  96. $mesg = $px1->isGraphKo();
  97. if (! $mesg)
  98. {
  99. $px1->SetData($data);
  100. $px1->SetPrecisionY(0);
  101. $i=$startyear;$legend=array();
  102. while ($i <= $endyear)
  103. {
  104. $legend[]=$i;
  105. $i++;
  106. }
  107. $px1->SetLegend($legend);
  108. $px1->SetMaxValue($px1->GetCeilMaxValue());
  109. $px1->SetMinValue(min(0,$px1->GetFloorMinValue()));
  110. $px1->SetWidth($WIDTH);
  111. $px1->SetHeight($HEIGHT);
  112. $px1->SetYLabel($langs->trans("NbOfOrder"));
  113. $px1->SetShading(3);
  114. $px1->SetHorizTickIncrement(1);
  115. $px1->SetPrecisionY(0);
  116. $px1->mode='depth';
  117. $px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
  118. $px1->draw($filenamenb,$fileurlnb);
  119. }
  120. // Build graphic amount of object
  121. $data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
  122. //var_dump($data);
  123. // $data = array(array('Lib',val1,val2,val3),...)
  124. if (!$user->rights->societe->client->voir || $user->societe_id)
  125. {
  126. $filenameamount = $dir.'/ordersamountinyear-'.$user->id.'-'.$year.'.png';
  127. if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  128. if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
  129. }
  130. else
  131. {
  132. $filenameamount = $dir.'/ordersamountinyear-'.$year.'.png';
  133. if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$year.'.png';
  134. if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$year.'.png';
  135. }
  136. $px2 = new DolGraph();
  137. $mesg = $px2->isGraphKo();
  138. if (! $mesg)
  139. {
  140. $px2->SetData($data);
  141. $i=$startyear;$legend=array();
  142. while ($i <= $endyear)
  143. {
  144. $legend[]=$i;
  145. $i++;
  146. }
  147. $px2->SetLegend($legend);
  148. $px2->SetMaxValue($px2->GetCeilMaxValue());
  149. $px2->SetMinValue(min(0,$px2->GetFloorMinValue()));
  150. $px2->SetWidth($WIDTH);
  151. $px2->SetHeight($HEIGHT);
  152. $px2->SetYLabel($langs->trans("AmountOfOrders"));
  153. $px2->SetShading(3);
  154. $px2->SetHorizTickIncrement(1);
  155. $px2->SetPrecisionY(0);
  156. $px2->mode='depth';
  157. $px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
  158. $px2->draw($filenameamount,$fileurlamount);
  159. }
  160. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  161. if (!$user->rights->societe->client->voir || $user->societe_id)
  162. {
  163. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  164. if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  165. if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  166. }
  167. else
  168. {
  169. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  170. if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  171. if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  172. }
  173. $px3 = new DolGraph();
  174. $mesg = $px3->isGraphKo();
  175. if (! $mesg)
  176. {
  177. $px3->SetData($data);
  178. $i=$startyear;$legend=array();
  179. while ($i <= $endyear)
  180. {
  181. $legend[]=$i;
  182. $i++;
  183. }
  184. $px3->SetLegend($legend);
  185. $px3->SetYLabel($langs->trans("AmountAverage"));
  186. $px3->SetMaxValue($px3->GetCeilMaxValue());
  187. $px3->SetMinValue($px3->GetFloorMinValue());
  188. $px3->SetWidth($WIDTH);
  189. $px3->SetHeight($HEIGHT);
  190. $px3->SetShading(3);
  191. $px3->SetHorizTickIncrement(1);
  192. $px3->SetPrecisionY(0);
  193. $px3->mode='depth';
  194. $px3->SetTitle($langs->trans("AmountAverage"));
  195. $px3->draw($filename_avg,$fileurl_avg);
  196. }
  197. // Show array
  198. $data = $stats->getAllByYear();
  199. $arrayyears=array();
  200. foreach($data as $val) {
  201. if (! empty($val['year'])) {
  202. $arrayyears[$val['year']]=$val['year'];
  203. }
  204. }
  205. if (! count($arrayyears)) $arrayyears[$nowyear]=$nowyear;
  206. $h=0;
  207. $head = array();
  208. $head[$h][0] = DOL_URL_ROOT . '/commande/stats/index.php?mode='.$mode;
  209. $head[$h][1] = $langs->trans("ByMonthYear");
  210. $head[$h][2] = 'byyear';
  211. $h++;
  212. if ($mode == 'customer') $type='order_stats';
  213. if ($mode == 'supplier') $type='supplier_order_stats';
  214. complete_head_from_modules($conf,$langs,null,$head,$h,$type);
  215. dol_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  216. print '<div class="fichecenter"><div class="fichethirdleft">';
  217. // Show filter box
  218. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  219. print '<input type="hidden" name="mode" value="'.$mode.'">';
  220. print '<table class="noborder" width="100%">';
  221. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  222. // Company
  223. print '<tr><td align="left">'.$langs->trans("ThirdParty").'</td><td align="left">';
  224. if ($mode == 'customer') $filter='s.client in (1,2,3)';
  225. if ($mode == 'supplier') $filter='s.fournisseur = 1';
  226. print $form->select_company($socid,'socid',$filter,1,0,0,array(),0,'','style="width: 95%"');
  227. print '</td></tr>';
  228. // User
  229. print '<tr><td align="left">'.$langs->trans("CreatedBy").'</td><td align="left">';
  230. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
  231. // Status
  232. print '<tr><td align="left">'.$langs->trans("Status").'</td><td align="left">';
  233. if ($mode == 'customer')
  234. {
  235. $liststatus=array(
  236. Commande::STATUS_DRAFT=>$langs->trans("StatusOrderDraft"),
  237. Commande::STATUS_VALIDATED=>$langs->trans("StatusOrderValidated"),
  238. Commande::STATUS_SHIPMENTONPROCESS=>$langs->trans("StatusOrderSent"),
  239. Commande::STATUS_CLOSED=>$langs->trans("StatusOrderDelivered"),
  240. Commande::STATUS_CANCELED=>$langs->trans("StatusOrderCanceled")
  241. );
  242. print $form->selectarray('object_status', $liststatus, GETPOST('object_status'), -4);
  243. }
  244. if ($mode == 'supplier')
  245. {
  246. $formorder->selectSupplierOrderStatus((strstr($object_status, ',')?-1:$object_status), 0, 'object_status');
  247. }
  248. print '</td></tr>';
  249. // Year
  250. print '<tr><td align="left">'.$langs->trans("Year").'</td><td align="left">';
  251. if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
  252. if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
  253. arsort($arrayyears);
  254. print $form->selectarray('year',$arrayyears,$year,0);
  255. print '</td></tr>';
  256. print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
  257. print '</table>';
  258. print '</form>';
  259. print '<br><br>';
  260. print '<div class="div-table-responsive-no-min">';
  261. print '<table class="noborder" width="100%">';
  262. print '<tr class="liste_titre" height="24">';
  263. print '<td align="center">'.$langs->trans("Year").'</td>';
  264. print '<td align="right">'.$langs->trans("NbOfOrders").'</td>';
  265. print '<td align="right">%</td>';
  266. print '<td align="right">'.$langs->trans("AmountTotal").'</td>';
  267. print '<td align="right">%</td>';
  268. print '<td align="right">'.$langs->trans("AmountAverage").'</td>';
  269. print '<td align="right">%</td>';
  270. print '</tr>';
  271. $oldyear=0;
  272. foreach ($data as $val)
  273. {
  274. $year = $val['year'];
  275. while (! empty($year) && $oldyear > $year+1)
  276. { // If we have empty year
  277. $oldyear--;
  278. print '<tr class="oddeven" height="24">';
  279. 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>';
  280. print '<td align="right">0</td>';
  281. print '<td align="right"></td>';
  282. print '<td align="right">0</td>';
  283. print '<td align="right"></td>';
  284. print '<td align="right">0</td>';
  285. print '<td align="right"></td>';
  286. print '</tr>';
  287. }
  288. print '<tr class="oddeven" height="24">';
  289. 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>';
  290. print '<td align="right">'.$val['nb'].'</td>';
  291. print '<td align="right" style="'.(($val['nb_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['nb_diff']).'</td>';
  292. print '<td align="right">'.price(price2num($val['total'],'MT'),1).'</td>';
  293. print '<td align="right" style="'.(($val['total_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['total_diff']).'</td>';
  294. print '<td align="right">'.price(price2num($val['avg'],'MT'),1).'</td>';
  295. print '<td align="right" style="'.(($val['avg_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['avg_diff']).'</td>';
  296. print '</tr>';
  297. $oldyear=$year;
  298. }
  299. print '</table>';
  300. print '</div>';
  301. print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
  302. // Show graphs
  303. print '<table class="border" width="100%"><tr class="pair nohover"><td align="center">';
  304. if ($mesg) { print $mesg; }
  305. else {
  306. print $px1->show();
  307. print "<br>\n";
  308. print $px2->show();
  309. print "<br>\n";
  310. print $px3->show();
  311. }
  312. print '</td></tr></table>';
  313. print '</div></div></div>';
  314. print '<div style="clear:both"></div>';
  315. dol_fiche_end();
  316. // End of page
  317. llxFooter();
  318. $db->close();