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