index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
  3. * Copyright (C) 2015-2021 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. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/projet/class/projectstats.class.php';
  29. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  30. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  31. $search_opp_status = GETPOST("search_opp_status", 'alpha');
  32. $userid = GETPOST('userid', 'int');
  33. $socid = GETPOST('socid', 'int');
  34. // Security check
  35. if ($user->socid > 0) {
  36. $action = '';
  37. $socid = $user->socid;
  38. }
  39. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  40. $year = GETPOST('year', 'int') > 0 ? GETPOST('year', 'int') : $nowyear;
  41. $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  42. $endyear = $year;
  43. // Load translation files required by the page
  44. $langs->loadLangs(array('companies', 'projects'));
  45. // Security check
  46. if (!$user->hasRight('projet', 'lire')) {
  47. accessforbidden();
  48. }
  49. /*
  50. * View
  51. */
  52. $form = new Form($db);
  53. $formproject = new FormProjets($db);
  54. $includeuserlist = array();
  55. llxHeader('', $langs->trans('Projects'));
  56. $title = $langs->trans("ProjectsStatistics");
  57. $dir = $conf->project->dir_output.'/temp';
  58. print load_fiche_titre($title, '', 'project');
  59. dol_mkdir($dir);
  60. $stats_project = new ProjectStats($db);
  61. if (!empty($userid) && $userid != -1) {
  62. $stats_project->userid = $userid;
  63. }
  64. if (!empty($socid) && $socid != -1) {
  65. $stats_project->socid = $socid;
  66. }
  67. if (!empty($year)) {
  68. $stats_project->year = $year;
  69. }
  70. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  71. if ($search_opp_status) {
  72. $stats_project->opp_status = $search_opp_status;
  73. }
  74. }
  75. // Build graphic number of object
  76. // $data = array(array('Lib',val1,val2,val3),...)
  77. $data = $stats_project->getNbByMonthWithPrevYear($endyear, $startyear);
  78. //var_dump($data);
  79. $filenamenb = $conf->project->dir_output."/stats/projectnbprevyear-".$year.".png";
  80. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectnbprevyear-'.$year.'.png';
  81. $px1 = new DolGraph();
  82. $mesg = $px1->isGraphKo();
  83. if (!$mesg) {
  84. $px1->SetData($data);
  85. $i = $startyear; $legend = array();
  86. while ($i <= $endyear) {
  87. $legend[] = $i;
  88. $i++;
  89. }
  90. $px1->SetLegend($legend);
  91. $px1->SetMaxValue($px1->GetCeilMaxValue());
  92. $px1->SetWidth($WIDTH);
  93. $px1->SetHeight($HEIGHT);
  94. $px1->SetYLabel($langs->trans("ProjectNbProject"));
  95. $px1->SetShading(3);
  96. $px1->SetHorizTickIncrement(1);
  97. $px1->mode = 'depth';
  98. $px1->SetTitle($langs->trans("ProjectNbProjectByMonth"));
  99. $px1->draw($filenamenb, $fileurlnb);
  100. }
  101. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  102. // Build graphic amount of object
  103. $data = $stats_project->getAmountByMonthWithPrevYear($endyear, $startyear);
  104. //var_dump($data);
  105. // $data = array(array('Lib',val1,val2,val3),...)
  106. $filenamenb = $conf->project->dir_output."/stats/projectamountprevyear-".$year.".png";
  107. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projectamountprevyear-'.$year.'.png';
  108. $px2 = new DolGraph();
  109. $mesg = $px2->isGraphKo();
  110. if (!$mesg) {
  111. $i = $startyear; $legend = array();
  112. while ($i <= $endyear) {
  113. $legend[] = $i;
  114. $i++;
  115. }
  116. $px2->SetData($data);
  117. $px2->SetLegend($legend);
  118. $px2->SetMaxValue($px2->GetCeilMaxValue());
  119. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  120. $px2->SetWidth($WIDTH);
  121. $px2->SetHeight($HEIGHT);
  122. $px2->SetYLabel($langs->trans("ProjectOppAmountOfProjectsByMonth"));
  123. $px2->SetShading(3);
  124. $px2->SetHorizTickIncrement(1);
  125. $px2->SetType(array('bars', 'bars'));
  126. $px2->mode = 'depth';
  127. $px2->SetTitle($langs->trans("ProjectOppAmountOfProjectsByMonth"));
  128. $px2->draw($filenamenb, $fileurlnb);
  129. }
  130. }
  131. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  132. // Build graphic with transformation rate
  133. $data = $stats_project->getWeightedAmountByMonthWithPrevYear($endyear, $startyear, 0, 0);
  134. //var_dump($data);
  135. // $data = array(array('Lib',val1,val2,val3),...)
  136. $filenamenb = $conf->project->dir_output."/stats/projecttransrateprevyear-".$year.".png";
  137. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=projectstats&amp;file=projecttransrateprevyear-'.$year.'.png';
  138. $px3 = new DolGraph();
  139. $mesg = $px3->isGraphKo();
  140. if (!$mesg) {
  141. $px3->SetData($data);
  142. $i = $startyear;
  143. $legend = array();
  144. while ($i <= $endyear) {
  145. $legend[] = $i;
  146. $i++;
  147. }
  148. $px3->SetLegend($legend);
  149. $px3->SetMaxValue($px3->GetCeilMaxValue());
  150. $px3->SetMinValue(min(0, $px3->GetFloorMinValue()));
  151. $px3->SetWidth($WIDTH);
  152. $px3->SetHeight($HEIGHT);
  153. $px3->SetYLabel($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
  154. $px3->SetShading(3);
  155. $px3->SetHorizTickIncrement(1);
  156. $px3->mode = 'depth';
  157. $px3->SetTitle($langs->trans("ProjectWeightedOppAmountOfProjectsByMonth"));
  158. $px3->draw($filenamenb, $fileurlnb);
  159. }
  160. }
  161. // Show array
  162. $stats_project->year = 0;
  163. $data_all_year = $stats_project->getAllByYear();
  164. if (!empty($year)) {
  165. $stats_project->year = $year;
  166. }
  167. $arrayyears = array();
  168. foreach ($data_all_year as $val) {
  169. $arrayyears[$val['year']] = $val['year'];
  170. }
  171. if (!count($arrayyears)) {
  172. $arrayyears[$nowyear] = $nowyear;
  173. }
  174. $h = 0;
  175. $head = array();
  176. $head[$h][0] = DOL_URL_ROOT.'/projet/stats/index.php';
  177. $head[$h][1] = $langs->trans("ByMonthYear");
  178. $head[$h][2] = 'byyear';
  179. $h++;
  180. complete_head_from_modules($conf, $langs, null, $head, $h, 'project_stats');
  181. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1, '');
  182. print '<div class="fichecenter"><div class="fichethirdleft">';
  183. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  184. print '<input type="hidden" name="token" value="'.newToken().'">';
  185. print '<table class="noborder centpercent">';
  186. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  187. // Company
  188. print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
  189. print img_picto('', 'company', 'class="pictofixedwidth"');
  190. print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', '');
  191. print '</td></tr>';
  192. // Opportunity status
  193. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  194. print '<tr><td>'.$langs->trans("OpportunityStatusShort").'</td><td>';
  195. print $formproject->selectOpportunityStatus('search_opp_status', $search_opp_status, 1, 0, 1, 0, 'maxwidth300', 1, 1);
  196. print '</td></tr>';
  197. }
  198. // User
  199. /*print '<tr><td>'.$langs->trans("ProjectCommercial").'</td><td>';
  200. print $form->select_dolusers($userid, 'userid', 1, array(),0,$includeuserlist);
  201. print '</td></tr>';*/
  202. // Year
  203. print '<tr><td>'.$langs->trans("Year").' <span class="opacitymedium">('.$langs->trans("DateCreation").')</span></td><td>';
  204. if (!in_array($year, $arrayyears)) {
  205. $arrayyears[$year] = $year;
  206. }
  207. if (!in_array($nowyear, $arrayyears)) {
  208. $arrayyears[$nowyear] = $nowyear;
  209. }
  210. arsort($arrayyears);
  211. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  212. print '</td></tr>';
  213. print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  214. print '</table>';
  215. print '</form>';
  216. print '<br><br>';
  217. print '<div class="div-table-responsive-no-min">';
  218. print '<table class="noborder centpercent">';
  219. print '<tr class="liste_titre" height="24">';
  220. print '<td class="center">'.$langs->trans("Year").'</td>';
  221. print '<td class="right">'.$langs->trans("NbOfProjects").'</td>';
  222. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  223. print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
  224. print '<td class="right">'.$langs->trans("OpportunityAmountAverageShort").'</td>';
  225. print '<td class="right">'.$langs->trans("OpportunityAmountWeigthedShort").'</td>';
  226. }
  227. print '</tr>';
  228. $oldyear = 0;
  229. foreach ($data_all_year as $val) {
  230. $year = $val['year'];
  231. while ($year && $oldyear > $year + 1) { // If we have empty year
  232. $oldyear--;
  233. print '<tr class="oddeven" height="24">';
  234. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
  235. print '<td class="right">0</td>';
  236. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  237. print '<td class="right amount nowraponall">0</td>';
  238. print '<td class="right amount nowraponall">0</td>';
  239. print '<td class="right amount nowraponall">0</td>';
  240. }
  241. print '</tr>';
  242. }
  243. print '<tr class="oddeven" height="24">';
  244. print '<td class="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  245. print '<td class="right">'.$val['nb'].'</td>';
  246. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  247. print '<td class="right amount nowraponall">'.($val['total'] ? price(price2num($val['total'], 'MT'), 1) : '0').'</td>';
  248. print '<td class="right amount nowraponall">'.($val['avg'] ? price(price2num($val['avg'], 'MT'), 1) : '0').'</td>';
  249. print '<td class="right amount nowraponall">'.(isset($val['weighted']) ? price(price2num($val['weighted'], 'MT'), 1) : '0').'</td>';
  250. }
  251. print '</tr>';
  252. $oldyear = $year;
  253. }
  254. print '</table>';
  255. print '</div>';
  256. print '</div><div class="fichetwothirdright">';
  257. $stringtoshow = '<table class="border centpercent"><tr class="pair nohover"><td class="center">';
  258. if ($mesg) {
  259. print $mesg;
  260. } else {
  261. $stringtoshow .= $px1->show();
  262. $stringtoshow .= "<br>\n";
  263. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  264. //$stringtoshow .= $px->show();
  265. //$stringtoshow .= "<br>\n";
  266. $stringtoshow .= $px2->show();
  267. $stringtoshow .= "<br>\n";
  268. $stringtoshow .= $px3->show();
  269. }
  270. }
  271. $stringtoshow .= '</td></tr></table>';
  272. print $stringtoshow;
  273. print '</div></div>';
  274. print '<div class="clearboth"></div>';
  275. print dol_get_fiche_end();
  276. // End of page
  277. llxFooter();
  278. $db->close();