index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 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/comm/propal/stats/index.php
  24. * \ingroup propale
  25. * \brief Page des stats propositions commerciales
  26. */
  27. // Load Dolibarr environment
  28. require '../../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formpropal.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. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  36. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  37. $mode = GETPOSTISSET("mode") ? GETPOST("mode", 'aZ09') : 'customer';
  38. $object_status = GETPOST('object_status', 'intcomma');
  39. $typent_id = GETPOST('typent_id', 'int');
  40. $categ_id = GETPOST('categ_id', 'categ_id');
  41. $userid = GETPOST('userid', 'int');
  42. $socid = GETPOST('socid', 'int');
  43. // Security check
  44. if ($user->socid > 0) {
  45. $action = '';
  46. $socid = $user->socid;
  47. }
  48. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  49. $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
  50. $startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  51. $endyear = $year;
  52. // Load translation files required by the page
  53. $langs->loadLangs(array('orders', 'companies', 'other', 'suppliers', 'supplier_proposal'));
  54. if ($mode == 'customer' && !$user->hasRight('propal', 'lire')) {
  55. accessforbidden();
  56. }
  57. if ($mode == 'supplier' && !$user->hasRight('supplier_proposal', 'lire')) {
  58. accessforbidden();
  59. }
  60. /*
  61. * View
  62. */
  63. $form = new Form($db);
  64. $formpropal = new FormPropal($db);
  65. $formcompany = new FormCompany($db);
  66. $formother = new FormOther($db);
  67. $langs->loadLangs(array('propal', 'other', 'companies'));
  68. if ($mode == 'customer') {
  69. $picto = 'propal';
  70. $title = $langs->trans("ProposalsStatistics");
  71. $dir = $conf->propal->dir_temp;
  72. $cat_type = Categorie::TYPE_CUSTOMER;
  73. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
  74. }
  75. if ($mode == 'supplier') {
  76. $picto = 'supplier_proposal';
  77. $title = $langs->trans("ProposalsStatisticsSuppliers");
  78. $dir = $conf->supplier_proposal->dir_temp;
  79. $cat_type = Categorie::TYPE_SUPPLIER;
  80. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
  81. }
  82. llxHeader('', $title);
  83. print load_fiche_titre($title, '', $picto);
  84. dol_mkdir($dir);
  85. $stats = new PropaleStats($db, $socid, ($userid > 0 ? $userid : 0), $mode, ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
  86. if ($object_status != '' && $object_status >= 0) {
  87. $stats->where .= ' AND p.fk_statut IN ('.$db->sanitize($object_status).')';
  88. }
  89. // Build graphic number of object
  90. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  91. // $data = array(array('Lib',val1,val2,val3),...)
  92. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  93. $filenamenb = $dir.'/proposalsnbinyear-'.$user->id.'-'.$year.'.png';
  94. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=proposalsnbinyear-'.$user->id.'-'.$year.'.png';
  95. } else {
  96. $filenamenb = $dir.'/proposalsnbinyear-'.$year.'.png';
  97. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=proposalsnbinyear-'.$year.'.png';
  98. }
  99. $px1 = new DolGraph();
  100. $mesg = $px1->isGraphKo();
  101. if (!$mesg) {
  102. $px1->SetData($data);
  103. $i = $startyear;
  104. $legend = array();
  105. while ($i <= $endyear) {
  106. $legend[] = $i;
  107. $i++;
  108. }
  109. $px1->SetLegend($legend);
  110. $px1->SetMaxValue($px1->GetCeilMaxValue());
  111. $px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
  112. $px1->SetWidth($WIDTH);
  113. $px1->SetHeight($HEIGHT);
  114. $px1->SetYLabel($langs->trans("NbOfProposals"));
  115. $px1->SetShading(3);
  116. $px1->SetHorizTickIncrement(1);
  117. $px1->mode = 'depth';
  118. $px1->SetTitle($langs->trans("NumberOfProposalsByMonth"));
  119. $px1->draw($filenamenb, $fileurlnb);
  120. }
  121. // Build graphic amount of object
  122. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, 0);
  123. // $data = array(array('Lib',val1,val2,val3),...)
  124. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  125. $filenameamount = $dir.'/proposalsamountinyear-'.$user->id.'-'.$year.'.png';
  126. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=proposalsamountinyear-'.$user->id.'-'.$year.'.png';
  127. } else {
  128. $filenameamount = $dir.'/proposalsamountinyear-'.$year.'.png';
  129. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=proposalsamountinyear-'.$year.'.png';
  130. }
  131. $px2 = new DolGraph();
  132. $mesg = $px2->isGraphKo();
  133. if (!$mesg) {
  134. $px2->SetData($data);
  135. $i = $startyear;
  136. $legend = array();
  137. while ($i <= $endyear) {
  138. $legend[] = $i;
  139. $i++;
  140. }
  141. $px2->SetLegend($legend);
  142. $px2->SetMaxValue($px2->GetCeilMaxValue());
  143. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  144. $px2->SetWidth($WIDTH);
  145. $px2->SetHeight($HEIGHT);
  146. $px2->SetYLabel($langs->trans("AmountOfProposals"));
  147. $px2->SetShading(3);
  148. $px2->SetHorizTickIncrement(1);
  149. $px2->mode = 'depth';
  150. $px2->SetTitle($langs->trans("AmountOfProposalsByMonthHT"));
  151. $px2->draw($filenameamount, $fileurlamount);
  152. }
  153. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  154. $fileurl_avg = '';
  155. if (!$user->hasRight('societe', 'client', 'voir') || $user->socid) {
  156. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  157. if ($mode == 'customer') {
  158. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  159. }
  160. if ($mode == 'supplier') {
  161. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  162. }
  163. } else {
  164. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  165. if ($mode == 'customer') {
  166. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  167. }
  168. if ($mode == 'supplier') {
  169. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  170. }
  171. }
  172. $px3 = new DolGraph();
  173. $mesg = $px3->isGraphKo();
  174. if (!$mesg) {
  175. $px3->SetData($data);
  176. $i = $startyear;
  177. $legend = array();
  178. while ($i <= $endyear) {
  179. $legend[] = $i;
  180. $i++;
  181. }
  182. $px3->SetLegend($legend);
  183. $px3->SetYLabel($langs->trans("AmountAverage"));
  184. $px3->SetMaxValue($px3->GetCeilMaxValue());
  185. $px3->SetMinValue($px3->GetFloorMinValue());
  186. $px3->SetWidth($WIDTH);
  187. $px3->SetHeight($HEIGHT);
  188. $px3->SetShading(3);
  189. $px3->SetHorizTickIncrement(1);
  190. $px3->mode = 'depth';
  191. $px3->SetTitle($langs->trans("AmountAverage"));
  192. $px3->draw($filename_avg, $fileurl_avg);
  193. }
  194. // Show array
  195. $data = $stats->getAllByYear();
  196. $arrayyears = array();
  197. foreach ($data as $val) {
  198. if (!empty($val['year'])) {
  199. $arrayyears[$val['year']] = $val['year'];
  200. }
  201. }
  202. if (!count($arrayyears)) {
  203. $arrayyears[$nowyear] = $nowyear;
  204. }
  205. $h = 0;
  206. $head = array();
  207. $head[$h][0] = DOL_URL_ROOT.'/comm/propal/stats/index.php';
  208. $head[$h][1] = $langs->trans("ByMonthYear");
  209. $head[$h][2] = 'byyear';
  210. $h++;
  211. complete_head_from_modules($conf, $langs, null, $head, $h, 'propal_stats');
  212. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  213. print '<div class="fichecenter"><div class="fichethirdleft">';
  214. // Show filter box
  215. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  216. print '<input type="hidden" name="token" value="'.newToken().'">';
  217. print '<input type="hidden" name="mode" value="'.$mode.'">';
  218. print '<table class="noborder centpercent">';
  219. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  220. // Company
  221. print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
  222. print img_picto('', 'company', 'class="pictofixedwidth"');
  223. $filter = '(s.client:IN:1,2,3)';
  224. print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  225. print '</td></tr>';
  226. // ThirdParty Type
  227. print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
  228. $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.
  229. print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1);
  230. if ($user->admin) {
  231. print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  232. }
  233. print '</td></tr>';
  234. // Category
  235. print '<tr><td>'.$cat_label.'</td><td>';
  236. print img_picto('', 'category', 'class="pictofixedwidth"');
  237. print $formother->select_categories($cat_type, $categ_id, 'categ_id', 0, 1, 'widthcentpercentminusx maxwidth300');
  238. print '</td></tr>';
  239. // User
  240. print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
  241. print img_picto('', 'user', 'class="pictofixedwidth"');
  242. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  243. print '</td></tr>';
  244. // Status
  245. print '<tr><td>'.$langs->trans("Status").'</td><td>';
  246. $formpropal->selectProposalStatus(($object_status != '' ? $object_status : -1), 0, 0, 1, $mode, 'object_status');
  247. print '</td></tr>';
  248. // Year
  249. print '<tr><td>'.$langs->trans("Year").'</td><td>';
  250. if (!in_array($year, $arrayyears)) {
  251. $arrayyears[$year] = $year;
  252. }
  253. if (!in_array($nowyear, $arrayyears)) {
  254. $arrayyears[$nowyear] = $nowyear;
  255. }
  256. arsort($arrayyears);
  257. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  258. print '</td></tr>';
  259. print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  260. print '</table>';
  261. print '</form>';
  262. print '<br><br>';
  263. print '<div class="div-table-responsive-no-min">';
  264. print '<table class="noborder centpercent">';
  265. print '<tr class="liste_titre" height="24">';
  266. print '<td class="center">'.$langs->trans("Year").'</td>';
  267. print '<td class="right">'.$langs->trans("NbOfProposals").'</td>';
  268. print '<td class="right">%</td>';
  269. print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
  270. print '<td class="right">%</td>';
  271. print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
  272. print '<td class="right">%</td>';
  273. print '</tr>';
  274. $oldyear = 0;
  275. foreach ($data as $val) {
  276. $year = $val['year'];
  277. while (!empty($year) && $oldyear > $year + 1) { // If we have empty year
  278. $oldyear--;
  279. print '<tr class="oddeven" height="24">';
  280. 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>';
  281. print '<td class="right">0</td>';
  282. print '<td class="right"></td>';
  283. print '<td class="right">0</td>';
  284. print '<td class="right"></td>';
  285. print '<td class="right">0</td>';
  286. print '<td class="right"></td>';
  287. print '</tr>';
  288. }
  289. print '<tr class="oddeven" height="24">';
  290. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  291. print '<td class="right">'.$val['nb'].'</td>';
  292. 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>';
  293. print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
  294. 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>';
  295. print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
  296. 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>';
  297. print '</tr>';
  298. $oldyear = $year;
  299. }
  300. print '</table>';
  301. print '</div>';
  302. print '</div><div class="fichetwothirdright">';
  303. // Show graphs
  304. print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
  305. if ($mesg) {
  306. print $mesg;
  307. } else {
  308. print $px1->show();
  309. print "<br>\n";
  310. print $px2->show();
  311. print "<br>\n";
  312. print $px3->show();
  313. }
  314. print '</td></tr></table>';
  315. print '</div></div>';
  316. print '<div class="clearboth"></div>';
  317. print dol_get_fiche_end();
  318. // End of page
  319. llxFooter();
  320. $db->close();