dolibarr 25.0.0-alpha
graph_opportunities.inc.php
1<?php
2/* Copyright (C) 2013-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20// variable $listofopplabel and $listofoppstatus should be defined
36'
37@phan-var-force string $projectsListId
38@phan-var-force int $socid
39@phan-var-force string[] $listofoppstatus
40@phan-var-force ?array<int,string> $listofopplabel
41@phan-var-force array<string,string> $colorseries
42@phan-var-force ?string $badgeStatus2
43';
44
45if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
46 $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";
47 $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
48 $sql .= " WHERE p.entity IN (".getEntity('project').")";
49 $sql .= " AND p.fk_statut = 1"; // Opened projects only
50 if ($mine || !$user->hasRight('projet', 'all', 'lire')) {
51 $sql .= " AND p.rowid IN (".$db->sanitize($projectsListId).")";
52 }
53 if ($socid > 0) {
54 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
55 }
56 $sql .= " GROUP BY p.fk_opp_status, cls.code";
57 $resql = $db->query($sql);
58
59 if ($resql) {
60 $num = $db->num_rows($resql);
61 $i = 0;
62
63 $total = 0;
64 $totalnb = 0;
65 $totaloppnb = 0;
66 $totalamount = 0;
67 $ponderated_opp_amount = 0;
68 $valsnb = array();
69 $valsamount = array();
70 $dataseries = array();
71 // -1=Canceled, 0=Draft, 1=Validated, (2=Accepted/On process not managed for sale orders), 3=Closed (Sent/Received, billed or not)
72 while ($i < $num) {
73 $obj = $db->fetch_object($resql);
74 if ($obj) {
75 $valsnb[$obj->opp_status] = $obj->nb;
76 $valsamount[$obj->opp_status] = $obj->opp_amount;
77 $totalnb += $obj->nb;
78 if ($obj->opp_status) {
79 $totaloppnb += $obj->nb;
80 }
81 if (!in_array($obj->code, array('WON', 'LOST'))) {
82 $totalamount += $obj->opp_amount;
83 $ponderated_opp_amount += $obj->ponderated_opp_amount;
84 }
85 $total += $obj->nb;
86 }
87 $i++;
88 }
89 $db->free($resql);
90
91 $ponderated_opp_amount /= 100;
92
93 print '<div class="div-table-responsive-no-min">';
94 print '<table class="noborder nohover centpercent">';
95 print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("OpportunitiesStatusForOpenedProjects").'</th></tr>'."\n";
96
97 $listofstatus = array_keys($listofoppstatus);
98 // Complete with values found into database and not into the dictionary
99 foreach ($valsamount as $key => $val) {
100 if (!in_array($key, $listofstatus) && $key) {
101 $listofstatus[] = $key;
102 if (!isset($colorseries[$key])) {
103 $colorseries[$key] = !empty($badgeStatus2) ? $badgeStatus2 : '#888888';
104 }
105 }
106 }
107
108 foreach ($listofstatus as $status) {
109 $labelStatus = '';
110
111 $code = dol_getIdFromCode($db, $status, 'c_lead_status', 'rowid', 'code');
112 if ($code) {
113 $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code);
114 if ($code == 'WON' || $code == 'LOST') {
115 $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code).' ('.$langs->transnoentitiesnoconv("NotClosedYet").")";
116 }
117 }
118 if (empty($labelStatus) && isset($listofopplabel[$status])) {
119 $labelStatus = $listofopplabel[$status];
120 }
121 if (empty($labelStatus)) {
122 $labelStatus = $langs->transnoentitiesnoconv('OldValue', $status); // When id is id of an entry no more in dictionary for example.
123 }
124
125 //$labelStatus .= ' ('.$langs->trans("Coeff").': '.price2num($listofoppstatus[$status]).')';
126 //$labelStatus .= ' - '.price2num($listofoppstatus[$status]).'%';
127
128 $dataseries[] = array($labelStatus, (isset($valsamount[$status]) ? (float) $valsamount[$status] : 0));
129 if (!$conf->use_javascript_ajax) {
130 print '<tr class="oddeven">';
131 print '<td>'.$labelStatus.'</td>';
132 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>';
133 print "</tr>\n";
134 }
135 }
136 if ($conf->use_javascript_ajax) {
137 print '<tr><td class="center nopaddingleftimp nopaddingrightimp pair nohover" colspan="2">';
138
139 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
140 $dolgraph = new DolGraph();
141 $dolgraph->SetData($dataseries);
142 $dolgraph->SetDataColor(array_values($colorseries));
143 $dolgraph->setShowLegend(2);
144 $dolgraph->setShowPercent(1);
145 $dolgraph->SetType(array('pie'));
146 //$dolgraph->setWidth('100%');
147 $dolgraph->SetHeight('200');
148 $dolgraph->draw('idgraphstatus');
149 print $dolgraph->show($totaloppnb ? 0 : 1);
150
151 print '</td></tr>';
152 }
153 //if ($totalinprocess != $total)
154 //print '<tr class="liste_total"><td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td><td class="right">'.$totalinprocess.'</td></tr>';
155 print '<tr class="liste_total"><td class="maxwidth200 tdoverflow">';
156 print $form->textwithpicto($langs->trans("OpportunityTotalAmount"), $langs->trans("WonLostExcluded")).'</td><td class="right">'.price($totalamount, 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
157 print '<tr class="liste_total"><td class="minwidth200 tdoverflow">';
158 //print $langs->trans("OpportunityPonderatedAmount").' ('.$langs->trans("WonLostExcluded").')';
159 print $form->textwithpicto($langs->trans("OpportunityPonderatedAmount"), $langs->trans("OpportunityPonderatedAmountDesc").' ('.$langs->trans("WonLostExcluded").')', 1);
160 print '</td><td class="right">'.price(price2num($ponderated_opp_amount, 'MT'), 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
161 print "</table>";
162 print "</div>";
163
164 print "<br>";
165 } else {
166 dol_print_error($db);
167 }
168}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to build graphs.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.