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