dolibarr  13.0.2
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 Frederic 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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
27 include_once DOL_DOCUMENT_ROOT . "/core/boxes/modules_boxes.php";
28 
33 {
34  public $boxcode = "FunnelOfProspection";
35  public $boximg = "object_projectpub";
36  public $boxlabel = "BoxTitleFunnelOfProspection";
37  public $depends = array("projet");
38 
42  public $db;
43 
44  public $param;
45 
46  public $info_box_head = array();
47  public $info_box_contents = array();
48 
55  public function __construct($db, $param = '')
56  {
57  global $user, $langs, $conf;
58 
59  // Load translation files required by the page
60  $langs->loadLangs(array('boxes', 'projects'));
61 
62  $this->db = $db;
63 
64  $this->enabled = ($conf->global->MAIN_FEATURES_LEVEL >= 1 ? 1 : 0); // Not enabled by default, still need some work
65 
66  $this->hidden = empty($user->rights->projet->lire);
67  }
68 
75  public function loadBox($max = 5)
76  {
77  global $conf;
78 
79  // default values
80  $badgeStatus0 = '#cbd3d3'; // draft
81  $badgeStatus1 = '#bc9526'; // validated
82  $badgeStatus1b = '#bc9526'; // validated
83  $badgeStatus2 = '#9c9c26'; // approved
84  $badgeStatus3 = '#bca52b';
85  $badgeStatus4 = '#25a580'; // Color ok
86  $badgeStatus4b = '#25a580'; // Color ok
87  $badgeStatus5 = '#cad2d2';
88  $badgeStatus6 = '#cad2d2';
89  $badgeStatus7 = '#baa32b';
90  $badgeStatus8 = '#993013';
91  $badgeStatus9 = '#e7f0f0';
92  if (file_exists(DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php')) {
93  include DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
94  }
95  $listofoppstatus = array();
96  $listofopplabel = array();
97  $listofoppcode = array();
98  $colorseriesstat = array();
99  $sql = "SELECT cls.rowid, cls.code, cls.percent, cls.label";
100  $sql .= " FROM " . MAIN_DB_PREFIX . "c_lead_status as cls";
101  $sql .= " WHERE active=1";
102  $sql .= " AND cls.code <> 'LOST'";
103  $sql .= $this->db->order('cls.rowid', 'ASC');
104  $resql = $this->db->query($sql);
105  if ($resql) {
106  $num = $this->db->num_rows($resql);
107  $i = 0;
108 
109  while ($i < $num) {
110  $objp = $this->db->fetch_object($resql);
111  $listofoppstatus[$objp->rowid] = $objp->percent;
112  $listofopplabel[$objp->rowid] = $objp->label;
113  $listofoppcode[$objp->rowid] = $objp->code;
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  case 'WON':
128  $colorseriesstat[$objp->rowid] = $badgeStatus6;
129  break;
130  default:
131  break;
132  }
133  $i++;
134  }
135  } else {
136  dol_print_error($this->db);
137  }
138 
139  global $conf, $user, $langs;
140  $this->max = $max;
141 
142  $this->info_box_head = array(
143  'text' => $langs->trans("Statistics") . ' - ' . $langs->trans("BoxTitleFunnelOfProspection"),
144  'graph' => '1'
145  );
146 
147  if ($user->rights->projet->lire || !empty($conf->global->PROJECT_USE_OPPORTUNITIES)) {
148  $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";
149  $sql .= " FROM " . MAIN_DB_PREFIX . "projet as p, " . MAIN_DB_PREFIX . "c_lead_status as cls";
150  $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
151  $sql .= " AND p.fk_opp_status = cls.rowid";
152  $sql .= " AND p.fk_statut = 1"; // Opend projects only
153  $sql .= " AND cls.code NOT IN ('LOST')";
154  $sql .= " GROUP BY p.fk_opp_status, cls.code";
155  $resql = $this->db->query($sql);
156 
157  $form = new Form($this->db);
158  if ($resql) {
159  $num = $this->db->num_rows($resql);
160  $i = 0;
161 
162  $totalnb = 0;
163  $totaloppnb = 0;
164  $totalamount = 0;
165  $ponderated_opp_amount = 0;
166  $valsnb = array();
167  $valsamount = array();
168  $dataseries = array();
169 
170  while ($i < $num) {
171  $obj = $this->db->fetch_object($resql);
172  if ($obj) {
173  $valsnb[$obj->opp_status] = $obj->nb;
174  $valsamount[$obj->opp_status] = $obj->opp_amount;
175  $totalnb += $obj->nb;
176  if ($obj->opp_status) {
177  $totaloppnb += $obj->nb;
178  }
179  if (!in_array($obj->code, array('WON', 'LOST'))) {
180  $totalamount += $obj->opp_amount;
181  $ponderated_opp_amount += $obj->ponderated_opp_amount;
182  }
183  }
184  $i++;
185  }
186  $this->db->free($resql);
187  $ponderated_opp_amount = $ponderated_opp_amount / 100;
188 
189  $stringtoprint = '';
190  $stringtoprint .= '<div class="div-table-responsive-no-min ">';
191  $listofstatus = array_keys($listofoppstatus);
192  $liststatus = array();
193  $data = array('');
194  foreach ($listofstatus as $status) {
195  $labelStatus = '';
196  if ($status != 7) {
197  $code = dol_getIdFromCode($this->db, $status, 'c_lead_status', 'rowid', 'code');
198  if ($code) {
199  $labelStatus = $langs->transnoentitiesnoconv("OppStatus" . $code);
200  }
201  if (empty($labelStatus)) {
202  $labelStatus = $listofopplabel[$status];
203  }
204 
205  $data[] = (isset($valsamount[$status]) ? (float) $valsamount[$status] : 0);
206  $liststatus[] = $labelStatus;
207  if (!$conf->use_javascript_ajax) {
208  $stringtoprint .= '<tr class="oddeven">';
209  $stringtoprint .= '<td>' . $labelStatus . '</td>';
210  $stringtoprint .= '<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>';
211  $stringtoprint .= "</tr>\n";
212  }
213  }
214  }
215  $dataseries[] = $data;
216  if ($conf->use_javascript_ajax) {
217  include_once DOL_DOCUMENT_ROOT . '/core/class/dolgraph.class.php';
218  $dolgraph = new DolGraph();
219  $dolgraph->SetMinValue(0);
220  $dolgraph->SetData($dataseries);
221  $dolgraph->SetLegend($liststatus);
222  $dolgraph->SetDataColor(array_values($colorseriesstat));
223  $dolgraph->setShowLegend(2);
224  $dolgraph->setShowPercent(1);
225  $dolgraph->setTitle('');
226  $dolgraph->SetType(array('horizontalbars'));
227  $dolgraph->SetHeight('200');
228  $dolgraph->SetWidth('600');
229  $dolgraph->mode = 'depth';
230  $dolgraph->draw('idgraphleadfunnel');
231  $stringtoprint .= $dolgraph->show($totaloppnb ? 0 : 1);
232  }
233  $stringtoprint .= '</div>';
234 
235  $line = 0;
236  $this->info_box_contents[$line][] = array(
237  'tr' => 'class="nohover left "',
238  'text' => ''
239  );
240  $this->info_box_contents[$line][] = array(
241  'tr' => 'class="nohover left "',
242  'text' => ''
243  );
244  $line++;
245  $this->info_box_contents[$line][] = array(
246  'tr' => '',
247  'td' => 'class="center nopaddingleftimp nopaddingrightimp" colspan="2"',
248  'text' => $stringtoprint
249  );
250  $line++;
251  $this->info_box_contents[$line][] = array(
252  'tr' => 'class="oddeven"',
253  'td' => 'class="left "',
254  'maxlength' => 500,
255  'text' => $langs->trans("OpportunityTotalAmount") . ' (' . $langs->trans("WonLostExcluded") . ')'
256  );
257  $this->info_box_contents[$line][] = array(
258  'tr' => 'class="oddeven"',
259  'td' => 'class="right "',
260  'maxlength' => 500,
261  'text' => price($totalamount, 0, '', 1, -1, -1, $conf->currency)
262  );
263  $line++;
264  $this->info_box_contents[$line][] = array(
265  'tr' => 'class="oddeven"',
266  'td' => 'class="left "',
267  'maxlength' => 500,
268  'text' => $form->textwithpicto($langs->trans("OpportunityPonderatedAmount") . ' (' . $langs->trans("WonLostExcluded") . ')', $langs->trans("OpportunityPonderatedAmountDesc"), 1)
269 
270  );
271  $this->info_box_contents[$line][] = array(
272  'td' => 'class="right "',
273  'maxlength' => 500,
274  'text' => price(price2num($ponderated_opp_amount, 'MT'), 0, '', 1, -1, -1, $conf->currency)
275  );
276  } else {
277  $this->info_box_contents[0][0] = array(
278  'td' => 'class="center opacitymedium"',
279  'text' => $langs->trans("NoRecordedCustomers")
280  );
281  }
282  } else {
283  $this->info_box_contents[0][0] = array(
284  'td' => '',
285  'text' => $langs->trans("ReadPermissionNotAllowed")
286  );
287  }
288  }
289 
298  public function showBox($head = null, $contents = null, $nooutput = 0)
299  {
300  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
301  }
302 }
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
loadBox($max=5)
Load data for box to show them later.
Class ModeleBoxes.
$conf db
API class for accounts.
Definition: inc.php:54
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...
Class to manage generation of HTML components Only common components must be here.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
__construct($db, $param= '')
Constructor.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
div float
Buy price without taxes.
Definition: style.css.php:650
Class to build graphs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Class to manage the box to show last projet.