dolibarr 23.0.3
box_funnel_of_prospection.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012-2014 Charles-François BENKE <charles.fr@benke.fr>
3 * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
4 * Copyright (C) 2015-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2016 Juan José Menent <jmenent@2byte.es>
6 * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28include_once DOL_DOCUMENT_ROOT."/core/boxes/modules_boxes.php";
29
30
35{
36 public $boxcode = "FunnelOfProspection";
37 public $boximg = "object_projectpub";
38 public $boxlabel = "BoxTitleFunnelOfProspection";
39 public $depends = array("projet");
40
41 public $version = 'dolibarr';
42
49 public function __construct($db, $param = '')
50 {
51 global $user, $langs;
52
53 // Load translation files required by the page
54 $langs->loadLangs(array('boxes', 'projects'));
55
56 $this->db = $db;
57
58 $this->enabled = getDolGlobalInt('PROJECT_USE_OPPORTUNITIES');
59
60 $this->hidden = !$user->hasRight('projet', 'lire');
61 }
62
69 public function loadBox($max = 5)
70 {
71 global $conf;
72
73 // default values
74 $badgeStatus0 = '#cbd3d3'; // draft
75 $badgeStatus1 = '#bc9526'; // validated
76 $badgeStatus1b = '#bc9526'; // validated
77 $badgeStatus2 = '#9c9c26'; // approved
78 $badgeStatus3 = '#bca52b';
79 $badgeStatus4 = '#25a580'; // Color ok
80 $badgeStatus4b = '#25a580'; // Color ok
81 $badgeStatus5 = '#cad2d2';
82 $badgeStatus6 = '#cad2d2';
83 $badgeStatus7 = '#baa32b';
84 $badgeStatus8 = '#993013';
85 $badgeStatus9 = '#e7f0f0';
86 if (file_exists(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php')) {
87 include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
88 }
89
90 $listofoppstatus = array();
91 $listofopplabel = array();
92 $listofoppposition = array();
93
94 $colorseriesstat = array();
95
96 $sql = "SELECT cls.rowid, cls.code, cls.percent, cls.label, cls.position";
97 $sql .= " FROM ".MAIN_DB_PREFIX."c_lead_status as cls";
98 $sql .= " WHERE active = 1";
99 $sql .= " AND cls.code NOT IN ('LOST', 'WON')";
100 $sql .= $this->db->order('cls.position,cls.rowid', 'ASC,ASC');
101
102 $resql = $this->db->query($sql);
103 if ($resql) {
104 $num = $this->db->num_rows($resql);
105 $i = 0;
106
107 while ($i < $num) {
108 $objp = $this->db->fetch_object($resql);
109
110 $listofoppstatus[$objp->rowid] = $objp->percent;
111 $listofopplabel[$objp->rowid] = $objp->label;
112 $listofoppposition[$objp->rowid] = $objp->position;
113
114 switch ($objp->code) {
115 case 'PROSP':
116 $colorseriesstat[$objp->rowid] = '-'.$badgeStatus0;
117 break;
118 case 'QUAL':
119 $colorseriesstat[$objp->rowid] = '-'.$badgeStatus1;
120 break;
121 case 'PROPO':
122 $colorseriesstat[$objp->rowid] = $badgeStatus1;
123 break;
124 case 'NEGO':
125 $colorseriesstat[$objp->rowid] = $badgeStatus4;
126 break;
127 default:
128 break;
129 }
130 $i++;
131 }
132 } else {
133 dol_print_error($this->db);
134 }
135
136 global $conf, $user, $langs;
137 $this->max = $max;
138
139 $this->info_box_head = array(
140 'text' => $langs->trans("Opportunities").' - '.$langs->trans("BoxTitleFunnelOfProspection"),
141 'nbcol' => 2,
142 'graph' => 1
143 );
144
145 if ($user->hasRight('projet', 'lire') || getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
146 $sql = "SELECT p.fk_opp_status as opp_status, cls.code, cls.position, COUNT(p.rowid) as nb, SUM(p.opp_amount) as opp_amount, SUM(p.opp_amount * p.opp_percent) as ponderated_opp_amount";
147 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p, ".MAIN_DB_PREFIX."c_lead_status as cls";
148 $sql .= " WHERE p.entity IN (".getEntity('project').")";
149 $sql .= " AND p.fk_opp_status = cls.rowid";
150 $sql .= " AND p.fk_statut = 1"; // Opened projects only
151 $sql .= " AND cls.active = 1"; // Active opportunities status only
152 $sql .= " AND cls.code NOT IN ('LOST', 'WON')";
153 $sql .= " GROUP BY p.fk_opp_status, cls.code, cls.position";
154 $sql .= " ORDER BY cls.position, p.fk_opp_status, cls.code";
155
156 $resql = $this->db->query($sql);
157
158 $form = new Form($this->db);
159 if ($resql) {
160 $num = $this->db->num_rows($resql);
161 $i = 0;
162
163 //$totalnb = 0;
164 $totaloppnb = 0;
165 $totalamount = 0;
166 $ponderated_opp_amount = 0;
167 //$valsnb = array();
168 $valsamount = array();
169 $dataseries = array();
170
171 while ($i < $num) {
172 $obj = $this->db->fetch_object($resql);
173 if ($obj) {
174 //$valsnb[$obj->opp_status] = $obj->nb;
175 $valsamount[$obj->opp_status] = $obj->opp_amount;
176 //$totalnb += $obj->nb;
177 if ($obj->opp_status) {
178 $totaloppnb += $obj->nb;
179 }
180 if (!in_array($obj->code, array('WON', 'LOST'))) {
181 $totalamount += $obj->opp_amount;
182 $ponderated_opp_amount += $obj->ponderated_opp_amount;
183 }
184 }
185 $i++;
186 }
187 $this->db->free($resql);
188
189 $ponderated_opp_amount /= 100;
190
191 $stringtoprint = '';
192 $stringtoprint .= '<div class="div-table-responsive-no-min ">';
193 $listofstatus = array_keys($listofoppstatus);
194 $liststatus = array();
195 $data = array('');
196 $customlabels = array();
197 // Set array $data that contains the length of the bar (not the real value)
198 // and the array $customlabels that contains the real value to show
199 $maxamount = 0;
200
201 foreach ($listofstatus as $status) {
202 $customlabel = '';
203 $labelStatus = '';
204 $customlabelmore = '';
205
206 $code = dol_getIdFromCode($this->db, $status, 'c_lead_status', 'rowid', 'code');
207 if ($code) {
208 $labelStatus = $langs->transnoentitiesnoconv("OppStatus".$code);
209 }
210 if (empty($labelStatus)) {
211 $labelStatus = $listofopplabel[$status];
212 }
213 $amount = (isset($valsamount[$status]) ? (float) $valsamount[$status] : 0);
214 $customlabel = price($amount, 0, $langs, 1, -1, 'MT', $conf->currency);
215
216 $amountforgraph = $amount;
217 if (getDolGlobalString('PROJECT_OPPORTUNITIES_CUMULATIVE_MODE')) {
218 // Add all other amount for next levels
219 $amountafter = 0;
220 foreach ($listofstatus as $status2) {
221 if ($listofoppposition[$status2] > $listofoppposition[$status]) {
222 $amountafter += $valsamount[$status2];
223 }
224 }
225 $amountforgraph += $amountafter;
226 //var_dump("For current status ".$status." the amountforgraph=".$amountforgraph);
227 $customlabelmore = price($amountafter, 0, $langs, 1, -1, 'MT', $conf->currency);
228 }
229
230 $data[] = $amountforgraph;
231 $liststatus[] = $labelStatus;
232 if (!$conf->use_javascript_ajax) {
233 $stringtoprint .= '<tr class="oddeven">';
234 $stringtoprint .= '<td>'.$labelStatus.'</td>';
235 $stringtoprint .= '<td class="nowraponall right amount"><a href="list.php?statut='.$status.'">'.price((isset($valsamount[$status]) ? (float) $valsamount[$status] : 0), 0, '', 1, -1, -1, $conf->currency).'</a></td>';
236 $stringtoprint .= "</tr>\n";
237 }
238
239 if (getDolGlobalString('PROJECT_OPPORTUNITIES_CUMULATIVE_MODE')) {
240 $customlabels[] = $customlabel.' + '.$customlabelmore;
241 } else {
242 $customlabels[] = $customlabel;
243 }
244
245 if ($maxamount < $amountforgraph) {
246 $maxamount = $amountforgraph;
247 }
248 }
249
250 // Permit to have a bar if value inferior to a certain value
251 $valuetoaddtomindata = $maxamount / 100;
252 foreach ($data as $key => $value) {
253 if ($value != "") {
254 $data[$key] = $valuetoaddtomindata + $value;
255 }
256 }
257 foreach ($data as $key => $value) {
258 if ($data[$key] == 0) {
259 $data[$key] = $valuetoaddtomindata;
260 }
261 }
262
263
264 $dataseries[] = $data;
265 if ($conf->use_javascript_ajax) {
266 include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
267 $dolgraph = new DolGraph();
268 $dolgraph->SetMinValue(0);
269 $dolgraph->SetData($dataseries);
270 $dolgraph->SetLegend($liststatus);
271 $dolgraph->setHideXValues(false);
272 $dolgraph->setHideYValues(true);
273 $dolgraph->SetDataColor(array_values($colorseriesstat));
274 $dolgraph->setShowLegend(2);
275 $dolgraph->setShowPercent(1);
276 $dolgraph->setMirrorGraphValues(true);
277 $dolgraph->setBorderWidth(2);
278 $dolgraph->setBorderSkip('false');
279 $dolgraph->SetType(array('horizontalbars'));
280 $dolgraph->SetHeight('150');
281 $dolgraph->SetWidth($conf->dol_optimize_smallscreen ? '300' : ((empty($_SESSION["dol_screenwidth"]) || $_SESSION["dol_screenwidth"] > 1600) ? '600' : '500'));
282 $dolgraph->setTooltipsTitles($liststatus);
283 $dolgraph->setTooltipsLabels($customlabels);
284 $dolgraph->mode = 'depth';
285 $dolgraph->draw('idgraphleadfunnel');
286
287 $stringtoprint .= $dolgraph->show($totaloppnb ? 0 : 1);
288 }
289 $stringtoprint .= '</div>';
290
291 $line = 0;
292
293 $this->info_box_contents[$line][] = array(
294 'tr' => '',
295 'td' => 'class="center nopaddingleftimp nopaddingrightimp" colspan="2"',
296 'text' => $stringtoprint
297 );
298 $line++;
299 $this->info_box_contents[$line][] = array(
300 'tr' => 'class="oddeven"',
301 'td' => 'class="left "',
302 'maxlength' => 500,
303 'asis' => 1,
304 'text' => $langs->trans("OpportunityTotalAmount").' <span class="opacitymedium hideonsmartphone">('.$langs->trans("WonLostExcluded").')</span>'
305 );
306 $this->info_box_contents[$line][] = array(
307 'tr' => 'class="oddeven"',
308 'td' => 'class="nowraponall right amount"',
309 'maxlength' => 500,
310 'text' => price($totalamount, 0, '', 1, -1, -1, $conf->currency)
311 );
312 $line++;
313 $this->info_box_contents[$line][] = array(
314 'tr' => 'class="oddeven"',
315 'td' => 'class="left "',
316 'maxlength' => 500,
317 'asis' => 1,
318 'text' => $form->textwithpicto($langs->trans("OpportunityPonderatedAmount").' <span class="opacitymedium hideonsmartphone">('.$langs->trans("WonLostExcluded").')</span>', $langs->trans("OpportunityPonderatedAmountDesc"), 1)
319
320 );
321 $this->info_box_contents[$line][] = array(
322 'td' => 'class="nowraponall right amount"',
323 'maxlength' => 500,
324 'text' => price(price2num($ponderated_opp_amount, 'MT'), 0, '', 1, -1, -1, $conf->currency)
325 );
326 } else {
327 $this->info_box_contents[0][0] = array(
328 'td' => 'class="center"',
329 'text' => '<span class="opacitymedium">'.$langs->trans("NoRecordedCustomers").'</span>'
330 );
331 }
332 } else {
333 $this->info_box_contents[0][0] = array(
334 'td' => '',
335 'text' => $langs->trans("ReadPermissionNotAllowed")
336 );
337 }
338 }
339
340
341
350 public function showBox($head = null, $contents = null, $nooutput = 0)
351 {
352 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
353 }
354}
Class to build graphs.
Class to manage generation of HTML components Only common components must be here.
Class ModeleBoxes.
Class to manage the box to show funnel of prospections.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param='')
Constructor.
loadBox($max=5)
Load data for box to show them later.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.