index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /* Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
  3. * Copyright (C) 2015 Laurent Destailleur <ldestailleur@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/projet/stats/index.php
  20. * \ingroup project
  21. * \brief Page for project statistics
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/projet/class/projectstats.class.php';
  27. // Security check
  28. if (!$user->rights->projet->lire) {
  29. accessforbidden();
  30. }
  31. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  32. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  33. $userid = GETPOST('userid', 'int');
  34. $socid = GETPOST('socid', 'int');
  35. // Security check
  36. if ($user->socid > 0) {
  37. $action = '';
  38. $socid = $user->socid;
  39. }
  40. $nowyear = strftime("%Y", dol_now());
  41. $year = GETPOST('year') > 0 ?GETPOST('year') : $nowyear;
  42. //$startyear=$year-2;
  43. $startyear = $year - 1;
  44. $endyear = $year;
  45. // Load translation files required by the page
  46. $langs->loadLangs(array('companies', 'projects'));
  47. /*
  48. * View
  49. */
  50. $form = new Form($db);
  51. $includeuserlist = array();
  52. llxHeader('', $langs->trans('Projects'));
  53. $title = $langs->trans("ProjectsStatistics");
  54. $dir = $conf->projet->dir_output.'/temp';
  55. print load_fiche_titre($title, '', 'project');
  56. dol_mkdir($dir);
  57. $stats_project = new ProjectStats($db);
  58. if (!empty($userid) && $userid != -1) {
  59. $stats_project->userid = $userid;
  60. }
  61. if (!empty($socid) && $socid != -1) {
  62. $stats_project->socid = $socid;
  63. }
  64. if (!empty($year)) {
  65. $stats_project->year = $year;
  66. }
  67. /*
  68. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES))
  69. {
  70. // Current stats of project amount per status
  71. $data1 = $stats_project->getAllProjectByStatus();
  72. if (!is_array($data1) && $data1 < 0) {
  73. setEventMessages($stats_project->error, null, 'errors');
  74. }
  75. if (empty($data1))
  76. {
  77. $showpointvalue = 0;
  78. $nocolor = 1;
  79. $data1 = array(array(0=>$langs->trans("None"), 1=>1));
  80. }
  81. $filenamenb = $conf->project->dir_output."/stats/projectbystatus.png";
  82. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectbystatus.png';
  83. $px = new DolGraph();
  84. $mesg = $px->isGraphKo();
  85. if (empty($mesg)) {
  86. $i = 0; $tot = count($data1); $legend = array();
  87. while ($i <= $tot)
  88. {
  89. $legend[] = $data1[$i][0];
  90. $i++;
  91. }
  92. $px->SetData($data1);
  93. unset($data1);
  94. if ($nocolor)
  95. $px->SetDataColor(array(
  96. array(
  97. 220,
  98. 220,
  99. 220
  100. )
  101. ));
  102. $px->SetLegend($legend);
  103. $px->setShowLegend(0);
  104. $px->setShowPointValue($showpointvalue);
  105. $px->setShowPercent(1);
  106. $px->SetMaxValue($px->GetCeilMaxValue());
  107. $px->SetWidth($WIDTH);
  108. $px->SetHeight($HEIGHT);
  109. $px->SetShading(3);
  110. $px->SetHorizTickIncrement(1);
  111. $px->SetCssPrefix("cssboxes");
  112. $px->SetType(array('pie'));
  113. $px->SetTitle($langs->trans('OpportunitiesStatusForProjects'));
  114. $result = $px->draw($filenamenb, $fileurlnb);
  115. if ($result < 0) {
  116. setEventMessages($px->error, null, 'errors');
  117. }
  118. } else {
  119. setEventMessages(null, $mesg, 'errors');
  120. }
  121. }*/
  122. // Build graphic number of object
  123. // $data = array(array('Lib',val1,val2,val3),...)
  124. $data = $stats_project->getNbByMonthWithPrevYear($endyear, $startyear);
  125. //var_dump($data);
  126. $filenamenb = $conf->project->dir_output."/stats/projectnbprevyear-".$year.".png";
  127. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectnbprevyear-'.$year.'.png';
  128. $px1 = new DolGraph();
  129. $mesg = $px1->isGraphKo();
  130. if (!$mesg) {
  131. $px1->SetData($data);
  132. $i = $startyear; $legend = array();
  133. while ($i <= $endyear) {
  134. $legend[] = $i;
  135. $i++;
  136. }
  137. $px1->SetLegend($legend);
  138. $px1->SetMaxValue($px1->GetCeilMaxValue());
  139. $px1->SetWidth($WIDTH);
  140. $px1->SetHeight($HEIGHT);
  141. $px1->SetYLabel($langs->trans("ProjectNbProject"));
  142. $px1->SetShading(3);
  143. $px1->SetHorizTickIncrement(1);
  144. $px1->mode = 'depth';
  145. $px1->SetTitle($langs->trans("ProjectNbProjectByMonth"));
  146. $px1->draw($filenamenb, $fileurlnb);
  147. }
  148. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  149. // Build graphic amount of object
  150. $data = $stats_project->getAmountByMonthWithPrevYear($endyear, $startyear);
  151. //var_dump($data);
  152. // $data = array(array('Lib',val1,val2,val3),...)
  153. $filenamenb = $conf->project->dir_output."/stats/projectamountprevyear-".$year.".png";
  154. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectamountprevyear-'.$year.'.png';
  155. $px2 = new DolGraph();
  156. $mesg = $px2->isGraphKo();
  157. if (!$mesg) {
  158. $i = $startyear; $legend = array();
  159. while ($i <= $endyear) {
  160. $legend[] = $i;
  161. $i++;
  162. }
  163. $px2->SetData($data);
  164. $px2->SetLegend($legend);
  165. $px2->SetMaxValue($px2->GetCeilMaxValue());
  166. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  167. $px2->SetWidth($WIDTH);
  168. $px2->SetHeight($HEIGHT);
  169. $px2->SetYLabel($langs->trans("ProjectOppAmountOfProjectsByMonth"));
  170. $px2->SetShading(3);
  171. $px2->SetHorizTickIncrement(1);
  172. $px2->SetType(array('bars', 'bars'));
  173. $px2->mode = 'depth';
  174. $px2->SetTitle($langs->trans("ProjectOppAmountOfProjectsByMonth"));
  175. $px2->draw($filenamenb, $fileurlnb);
  176. }
  177. }
  178. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  179. // Build graphic with transformation rate
  180. $data = $stats_project->getWeightedAmountByMonthWithPrevYear($endyear, $startyear, 0, 0);
  181. //var_dump($data);
  182. // $data = array(array('Lib',val1,val2,val3),...)
  183. $filenamenb = $conf->project->dir_output."/stats/projecttransrateprevyear-".$year.".png";
  184. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projecttransrateprevyear-'.$year.'.png';
  185. $px3 = new DolGraph();
  186. $mesg = $px3->isGraphKo();
  187. if (!$mesg) {
  188. $px3->SetData($data);
  189. $i = $startyear; $legend = array();
  190. while ($i <= $endyear) {
  191. $legend[] = $i;
  192. $i++;
  193. }
  194. $px3->SetLegend($legend);
  195. $px3->SetMaxValue($px3->GetCeilMaxValue());
  196. $px3->SetMinValue(min(0, $px3->GetFloorMinValue()));
  197. $px3->SetWidth($WIDTH);
  198. $px3->SetHeight($HEIGHT);
  199. $px3->SetYLabel($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
  200. $px3->SetShading(3);
  201. $px3->SetHorizTickIncrement(1);
  202. $px3->mode = 'depth';
  203. $px3->SetTitle($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
  204. $px3->draw($filenamenb, $fileurlnb);
  205. }
  206. }
  207. // Show array
  208. $stats_project->year = 0;
  209. $data_all_year = $stats_project->getAllByYear();
  210. if (!empty($year)) {
  211. $stats_project->year = $year;
  212. }
  213. $arrayyears = array();
  214. foreach ($data_all_year as $val) {
  215. $arrayyears[$val['year']] = $val['year'];
  216. }
  217. if (!count($arrayyears)) {
  218. $arrayyears[$nowyear] = $nowyear;
  219. }
  220. $h = 0;
  221. $head = array();
  222. $head[$h][0] = DOL_URL_ROOT.'/projet/stats/index.php';
  223. $head[$h][1] = $langs->trans("ByMonthYear");
  224. $head[$h][2] = 'byyear';
  225. $h++;
  226. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  227. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1, '');
  228. print '<div class="fichecenter"><div class="fichethirdleft">';
  229. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  230. print '<input type="hidden" name="token" value="'.newToken().'">';
  231. print '<table class="noborder centpercent">';
  232. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  233. // Company
  234. print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
  235. print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, '', 'style="width: 95%"');
  236. print '</td></tr>';
  237. // User
  238. /*print '<tr><td>'.$langs->trans("ProjectCommercial").'</td><td>';
  239. print $form->select_dolusers($userid, 'userid', 1, array(),0,$includeuserlist);
  240. print '</td></tr>';*/
  241. // Year
  242. print '<tr><td>'.$langs->trans("Year").'</td><td>';
  243. if (!in_array($year, $arrayyears)) {
  244. $arrayyears[$year] = $year;
  245. }
  246. if (!in_array($nowyear, $arrayyears)) {
  247. $arrayyears[$nowyear] = $nowyear;
  248. }
  249. arsort($arrayyears);
  250. print $form->selectarray('year', $arrayyears, $year, 0);
  251. print '</td></tr>';
  252. print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
  253. print '</table>';
  254. print '</form>';
  255. print '<br><br>';
  256. print '<div class="div-table-responsive-no-min">';
  257. print '<table class="noborder centpercent">';
  258. print '<tr class="liste_titre" height="24">';
  259. print '<td class="center">'.$langs->trans("Year").'</td>';
  260. print '<td class="right">'.$langs->trans("NbOfProjects").'</td>';
  261. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  262. print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
  263. print '<td class="right">'.$langs->trans("OpportunityAmountAverageShort").'</td>';
  264. print '<td class="right">'.$langs->trans("OpportunityAmountWeigthedShort").'</td>';
  265. }
  266. print '</tr>';
  267. $oldyear = 0;
  268. foreach ($data_all_year as $val) {
  269. $year = $val['year'];
  270. while ($year && $oldyear > $year + 1) { // If we have empty year
  271. $oldyear--;
  272. print '<tr class="oddeven" height="24">';
  273. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
  274. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  275. print '<td class="right">0</td>';
  276. print '<td class="right">0</td>';
  277. print '<td class="right">0</td>';
  278. }
  279. print '<td class="right">0</td>';
  280. print '</tr>';
  281. }
  282. print '<tr class="oddeven" height="24">';
  283. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  284. print '<td class="right">'.$val['nb'].'</td>';
  285. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  286. print '<td class="right">'.($val['total'] ?price(price2num($val['total'], 'MT'), 1) : '0').'</td>';
  287. print '<td class="right">'.($val['avg'] ?price(price2num($val['avg'], 'MT'), 1) : '0').'</td>';
  288. print '<td class="right">'.($val['weighted'] ?price(price2num($val['weighted'], 'MT'), 1) : '0').'</td>';
  289. }
  290. print '</tr>';
  291. $oldyear = $year;
  292. }
  293. print '</table>';
  294. print '</div>';
  295. print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
  296. $stringtoshow .= '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
  297. if ($mesg) {
  298. print $mesg;
  299. } else {
  300. $stringtoshow .= $px1->show();
  301. $stringtoshow .= "<br>\n";
  302. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  303. //$stringtoshow .= $px->show();
  304. //$stringtoshow .= "<br>\n";
  305. $stringtoshow .= $px2->show();
  306. $stringtoshow .= "<br>\n";
  307. $stringtoshow .= $px3->show();
  308. }
  309. }
  310. $stringtoshow .= '</td></tr></table>';
  311. print $stringtoshow;
  312. print '</div></div></div>';
  313. print '<div style="clear:both"></div>';
  314. // End of page
  315. llxFooter();
  316. $db->close();