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