graph_opportunities.inc.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* Copyright (C) 2013-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. // variable $listofopplabel and $listofoppstatus should be defined
  18. if (!empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
  19. $sql = "SELECT p.fk_opp_status as opp_status, cls.code, COUNT(p.rowid) as nb, SUM(p.opp_amount) as opp_amount, SUM(p.opp_amount * p.opp_percent) as ponderated_opp_amount";
  20. $sql .= " FROM ".MAIN_DB_PREFIX."projet as p LEFT JOIN ".MAIN_DB_PREFIX."c_lead_status as cls ON p.fk_opp_status = cls.rowid"; // If lead status has been removed, we must show it in stats as unknown
  21. $sql .= " WHERE p.entity IN (".getEntity('project').")";
  22. $sql .= " AND p.fk_statut = 1"; // Opend projects only
  23. if ($mine || empty($user->rights->projet->all->lire)) {
  24. $sql .= " AND p.rowid IN (".$db->sanitize($projectsListId).")";
  25. }
  26. if ($socid > 0) {
  27. $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
  28. }
  29. $sql .= " GROUP BY p.fk_opp_status, cls.code";
  30. $resql = $db->query($sql);
  31. if ($resql) {
  32. $num = $db->num_rows($resql);
  33. $i = 0;
  34. $total = 0;
  35. $totalnb = 0;
  36. $totaloppnb = 0;
  37. $totalamount = 0;
  38. $ponderated_opp_amount = 0;
  39. $valsnb = array();
  40. $valsamount = array();
  41. $dataseries = array();
  42. // -1=Canceled, 0=Draft, 1=Validated, (2=Accepted/On process not managed for customer orders), 3=Closed (Sent/Received, billed or not)
  43. while ($i < $num) {
  44. $obj = $db->fetch_object($resql);
  45. if ($obj) {
  46. $valsnb[$obj->opp_status] = $obj->nb;
  47. $valsamount[$obj->opp_status] = $obj->opp_amount;
  48. $totalnb += $obj->nb;
  49. if ($obj->opp_status) {
  50. $totaloppnb += $obj->nb;
  51. }
  52. if (!in_array($obj->code, array('WON', 'LOST'))) {
  53. $totalamount += $obj->opp_amount;
  54. $ponderated_opp_amount += $obj->ponderated_opp_amount;
  55. }
  56. $total += $obj->nb;
  57. }
  58. $i++;
  59. }
  60. $db->free($resql);
  61. $ponderated_opp_amount = $ponderated_opp_amount / 100;
  62. print '<div class="div-table-responsive-no-min">';
  63. print '<table class="noborder nohover centpercent">';
  64. print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("OpportunitiesStatusForOpenedProjects").'</th></tr>'."\n";
  65. $listofstatus = array_keys($listofoppstatus);
  66. // Complete with values found into database and not into the dictionary
  67. foreach ($valsamount as $key => $val) {
  68. if (!in_array($key, $listofstatus) && $key) {
  69. $listofstatus[] = $key;
  70. }
  71. }
  72. foreach ($listofstatus as $status) {
  73. $labelStatus = '';
  74. $code = dol_getIdFromCode($db, $status, 'c_lead_status', 'rowid', 'code');
  75. if ($code) {
  76. $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code);
  77. if ($code == 'WON' || $code == 'LOST') {
  78. $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code).' ('.$langs->transnoentitiesnoconv("NotClosedYet").")";
  79. }
  80. }
  81. if (empty($labelStatus)) {
  82. $labelStatus = $listofopplabel[$status];
  83. }
  84. if (empty($labelStatus)) {
  85. $labelStatus = $langs->transnoentitiesnoconv('OldValue', $status); // When id is id of an entry no more in dictionary for example.
  86. }
  87. //$labelStatus .= ' ('.$langs->trans("Coeff").': '.price2num($listofoppstatus[$status]).')';
  88. //$labelStatus .= ' - '.price2num($listofoppstatus[$status]).'%';
  89. $dataseries[] = array($labelStatus, (isset($valsamount[$status]) ? (float) $valsamount[$status] : 0));
  90. if (!$conf->use_javascript_ajax) {
  91. print '<tr class="oddeven">';
  92. print '<td>'.$labelStatus.'</td>';
  93. print '<td class="right"><a href="list.php?statut='.$status.'">'.price((isset($valsamount[$status]) ? (float) $valsamount[$status] : 0), 0, '', 1, -1, -1, $conf->currency).'</a></td>';
  94. print "</tr>\n";
  95. }
  96. }
  97. if ($conf->use_javascript_ajax) {
  98. print '<tr><td class="center nopaddingleftimp nopaddingrightimp" colspan="2">';
  99. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  100. $dolgraph = new DolGraph();
  101. $dolgraph->SetData($dataseries);
  102. $dolgraph->SetDataColor(array_values($colorseries));
  103. $dolgraph->setShowLegend(2);
  104. $dolgraph->setShowPercent(1);
  105. $dolgraph->SetType(array('pie'));
  106. //$dolgraph->setWidth('100%');
  107. $dolgraph->SetHeight('200');
  108. $dolgraph->draw('idgraphstatus');
  109. print $dolgraph->show($totaloppnb ? 0 : 1);
  110. print '</td></tr>';
  111. }
  112. //if ($totalinprocess != $total)
  113. //print '<tr class="liste_total"><td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td><td class="right">'.$totalinprocess.'</td></tr>';
  114. print '<tr class="liste_total"><td class="maxwidth200 tdoverflow">'.$langs->trans("OpportunityTotalAmount").' ('.$langs->trans("WonLostExcluded").')</td><td class="right">'.price($totalamount, 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
  115. print '<tr class="liste_total"><td class="minwidth200 tdoverflow">';
  116. //print $langs->trans("OpportunityPonderatedAmount").' ('.$langs->trans("WonLostExcluded").')';
  117. print $form->textwithpicto($langs->trans("OpportunityPonderatedAmount").' ('.$langs->trans("WonLostExcluded").')', $langs->trans("OpportunityPonderatedAmountDesc"), 1);
  118. print '</td><td class="right">'.price(price2num($ponderated_opp_amount, 'MT'), 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
  119. print "</table>";
  120. print "</div>";
  121. print "<br>";
  122. } else {
  123. dol_print_error($db);
  124. }
  125. }