dolibarr 24.0.0-beta
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
35'
36@phan-var-force string $projectsListId
37@phan-var-force int $socid
38@phan-var-force string[] $listofoppstatus
39@phan-var-force ?array<int,string> $listofopplabel
40@phan-var-force array<string,string> $colorseries
41';
42
43if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
44 $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";
45 $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
46 $sql .= " WHERE p.entity IN (".getEntity('project').")";
47 $sql .= " AND p.fk_statut = 1"; // Opened projects only
48 if ($mine || !$user->hasRight('projet', 'all', 'lire')) {
49 $sql .= " AND p.rowid IN (".$db->sanitize($projectsListId).")";
50 }
51 if ($socid > 0) {
52 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
53 }
54 $sql .= " GROUP BY p.fk_opp_status, cls.code";
55 $resql = $db->query($sql);
56
57 if ($resql) {
58 $num = $db->num_rows($resql);
59 $i = 0;
60
61 $total = 0;
62 $totalnb = 0;
63 $totaloppnb = 0;
64 $totalamount = 0;
65 $ponderated_opp_amount = 0;
66 $valsnb = array();
67 $valsamount = array();
68 $dataseries = array();
69 // -1=Canceled, 0=Draft, 1=Validated, (2=Accepted/On process not managed for sale orders), 3=Closed (Sent/Received, billed or not)
70 while ($i < $num) {
71 $obj = $db->fetch_object($resql);
72 if ($obj) {
73 $valsnb[$obj->opp_status] = $obj->nb;
74 $valsamount[$obj->opp_status] = $obj->opp_amount;
75 $totalnb += $obj->nb;
76 if ($obj->opp_status) {
77 $totaloppnb += $obj->nb;
78 }
79 if (!in_array($obj->code, array('WON', 'LOST'))) {
80 $totalamount += $obj->opp_amount;
81 $ponderated_opp_amount += $obj->ponderated_opp_amount;
82 }
83 $total += $obj->nb;
84 }
85 $i++;
86 }
87 $db->free($resql);
88
89 $ponderated_opp_amount /= 100;
90
91 print '<div class="div-table-responsive-no-min">';
92 print '<table class="noborder nohover centpercent">';
93 print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("OpportunitiesStatusForOpenedProjects").'</th></tr>'."\n";
94
95 $listofstatus = array_keys($listofoppstatus);
96 // Complete with values found into database and not into the dictionary
97 foreach ($valsamount as $key => $val) {
98 if (!in_array($key, $listofstatus) && $key) {
99 $listofstatus[] = $key;
100 }
101 }
102
103 foreach ($listofstatus as $status) {
104 $labelStatus = '';
105
106 $code = dol_getIdFromCode($db, $status, 'c_lead_status', 'rowid', 'code');
107 if ($code) {
108 $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code);
109 if ($code == 'WON' || $code == 'LOST') {
110 $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code).' ('.$langs->transnoentitiesnoconv("NotClosedYet").")";
111 }
112 }
113 if (empty($labelStatus) && isset($listofopplabel[$status])) {
114 $labelStatus = $listofopplabel[$status];
115 }
116 if (empty($labelStatus)) {
117 $labelStatus = $langs->transnoentitiesnoconv('OldValue', $status); // When id is id of an entry no more in dictionary for example.
118 }
119
120 //$labelStatus .= ' ('.$langs->trans("Coeff").': '.price2num($listofoppstatus[$status]).')';
121 //$labelStatus .= ' - '.price2num($listofoppstatus[$status]).'%';
122
123 $dataseries[] = array($labelStatus, (isset($valsamount[$status]) ? (float) $valsamount[$status] : 0));
124 if (!$conf->use_javascript_ajax) {
125 print '<tr class="oddeven">';
126 print '<td>'.$labelStatus.'</td>';
127 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>';
128 print "</tr>\n";
129 }
130 }
131 if ($conf->use_javascript_ajax) {
132 print '<tr><td class="center nopaddingleftimp nopaddingrightimp pair nohover" colspan="2">';
133
134 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
135 $dolgraph = new DolGraph();
136 $dolgraph->SetData($dataseries);
137 $dolgraph->SetDataColor(array_values($colorseries));
138 $dolgraph->setShowLegend(2);
139 $dolgraph->setShowPercent(1);
140 $dolgraph->SetType(array('pie'));
141 //$dolgraph->setWidth('100%');
142 $dolgraph->SetHeight('200');
143 $dolgraph->draw('idgraphstatus');
144 print $dolgraph->show($totaloppnb ? 0 : 1);
145
146 print '</td></tr>';
147 }
148 //if ($totalinprocess != $total)
149 //print '<tr class="liste_total"><td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td><td class="right">'.$totalinprocess.'</td></tr>';
150 print '<tr class="liste_total"><td class="maxwidth200 tdoverflow">';
151 print $form->textwithpicto($langs->trans("OpportunityTotalAmount"), $langs->trans("WonLostExcluded")).'</td><td class="right">'.price($totalamount, 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
152 print '<tr class="liste_total"><td class="minwidth200 tdoverflow">';
153 //print $langs->trans("OpportunityPonderatedAmount").' ('.$langs->trans("WonLostExcluded").')';
154 print $form->textwithpicto($langs->trans("OpportunityPonderatedAmount"), $langs->trans("OpportunityPonderatedAmountDesc").' ('.$langs->trans("WonLostExcluded").')', 1);
155 print '</td><td class="right">'.price(price2num($ponderated_opp_amount, 'MT'), 0, '', 1, -1, -1, $conf->currency).'</td></tr>';
156 print "</table>";
157 print "</div>";
158
159 print "<br>";
160 } else {
162 }
163}
Class to build graphs.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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)
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.