2017-10-19 19:53:29 +02:00
< ? php
2025-01-22 09:14:43 +01:00
/* Copyright ( C ) 2013 - 2020 Laurent Destailleur < eldy @ users . sourceforge . net >
2025-02-10 21:20:09 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2025-01-22 09:14:43 +01:00
* Copyright ( C ) 2024 - 2025 Frédéric France < frederic . france @ free . fr >
2020-08-04 14:44:52 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
// variable $listofopplabel and $listofoppstatus should be defined
2024-11-08 14:41:01 +01:00
/**
* @ var Conf $conf
* @ var Form $form
* @ var DoliDB $db
* @ var Translate $langs
* @ var User $user
*
* @ var string $projectsListId
* @ var int $socid
* @ var string [] $listofoppstatus
*/
2025-02-10 21:20:09 +01:00
'
@ phan - var - force string $projectsListId
@ phan - var - force int $socid
@ phan - var - force string [] $listofoppstatus
@ phan - var - force ? array < int , string > $listofopplabel
@ phan - var - force array < string , string > $colorseries
' ;
2020-08-04 14:44:52 +02:00
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PROJECT_USE_OPPORTUNITIES' )) {
2016-07-08 10:59:13 +02:00
$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 " ;
2021-10-25 22:07:31 +02:00
$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
2019-11-13 19:35:02 +01:00
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2025-01-22 09:14:43 +01:00
$sql .= " AND p.fk_statut = 1 " ; // Opened projects only
2023-10-15 18:39:13 +02:00
if ( $mine || ! $user -> hasRight ( 'projet' , 'all' , 'lire' )) {
2021-03-22 11:30:18 +01:00
$sql .= " AND p.rowid IN ( " . $db -> sanitize ( $projectsListId ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2021-03-22 11:30:18 +01:00
if ( $socid > 0 ) {
$sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = " . (( int ) $socid ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2019-11-13 19:35:02 +01:00
$sql .= " GROUP BY p.fk_opp_status, cls.code " ;
2015-11-28 17:35:52 +01:00
$resql = $db -> query ( $sql );
2016-01-28 20:04:02 +01:00
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2020-09-07 10:18:17 +02:00
$num = $db -> num_rows ( $resql );
$i = 0 ;
2015-11-28 17:35:52 +01:00
2021-03-15 21:13:15 +01:00
$total = 0 ;
2020-09-07 10:18:17 +02:00
$totalnb = 0 ;
$totaloppnb = 0 ;
$totalamount = 0 ;
$ponderated_opp_amount = 0 ;
$valsnb = array ();
$valsamount = array ();
$dataseries = array ();
2022-10-12 15:29:30 +02:00
// -1=Canceled, 0=Draft, 1=Validated, (2=Accepted/On process not managed for sale orders), 3=Closed (Sent/Received, billed or not)
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
2020-09-07 10:18:17 +02:00
$obj = $db -> fetch_object ( $resql );
2021-02-26 18:49:22 +01:00
if ( $obj ) {
2020-09-07 10:18:17 +02:00
$valsnb [ $obj -> opp_status ] = $obj -> nb ;
$valsamount [ $obj -> opp_status ] = $obj -> opp_amount ;
$totalnb += $obj -> nb ;
2021-02-26 18:49:22 +01:00
if ( $obj -> opp_status ) {
$totaloppnb += $obj -> nb ;
}
2020-03-28 15:20:26 +01:00
if ( ! in_array ( $obj -> code , array ( 'WON' , 'LOST' ))) {
2019-11-13 19:35:02 +01:00
$totalamount += $obj -> opp_amount ;
$ponderated_opp_amount += $obj -> ponderated_opp_amount ;
2019-10-20 11:59:21 +02:00
}
2020-09-07 10:18:17 +02:00
$total += $obj -> nb ;
}
$i ++ ;
}
$db -> free ( $resql );
2015-11-28 17:35:52 +01:00
2024-08-07 02:53:45 +02:00
$ponderated_opp_amount /= 100 ;
2017-10-19 19:53:29 +02:00
print '<div class="div-table-responsive-no-min">' ;
2021-02-26 18:49:22 +01:00
print '<table class="noborder nohover centpercent">' ;
print '<tr class="liste_titre"><th colspan="2">' . $langs -> trans ( " Statistics " ) . ' - ' . $langs -> trans ( " OpportunitiesStatusForOpenedProjects " ) . '</th></tr>' . " \n " ;
2021-01-14 12:58:50 +01:00
2021-02-26 18:49:22 +01:00
$listofstatus = array_keys ( $listofoppstatus );
2021-01-14 12:58:50 +01:00
// Complete with values found into database and not into the dictionary
2021-02-26 18:49:22 +01:00
foreach ( $valsamount as $key => $val ) {
if ( ! in_array ( $key , $listofstatus ) && $key ) {
$listofstatus [] = $key ;
}
}
2021-01-14 12:58:50 +01:00
2021-02-26 18:49:22 +01:00
foreach ( $listofstatus as $status ) {
$labelStatus = '' ;
2015-11-28 17:35:52 +01:00
$code = dol_getIdFromCode ( $db , $status , 'c_lead_status' , 'rowid' , 'code' );
2021-02-26 18:49:22 +01:00
if ( $code ) {
$labelStatus = $langs -> transnoentitiesnoconv ( " OppStatus " . $code );
2021-10-06 14:26:48 +02:00
if ( $code == 'WON' || $code == 'LOST' ) {
$labelStatus = $langs -> transnoentitiesnoconv ( " OppStatus " . $code ) . ' (' . $langs -> transnoentitiesnoconv ( " NotClosedYet " ) . " ) " ;
}
2021-02-26 18:49:22 +01:00
}
2023-06-15 16:59:45 +02:00
if ( empty ( $labelStatus ) && isset ( $listofopplabel [ $status ])) {
2021-02-26 18:49:22 +01:00
$labelStatus = $listofopplabel [ $status ];
}
if ( empty ( $labelStatus )) {
2021-10-25 22:07:31 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'OldValue' , $status ); // When id is id of an entry no more in dictionary for example.
2021-02-26 18:49:22 +01:00
}
2015-11-28 17:35:52 +01:00
2020-09-07 10:18:17 +02:00
//$labelStatus .= ' ('.$langs->trans("Coeff").': '.price2num($listofoppstatus[$status]).')';
//$labelStatus .= ' - '.price2num($listofoppstatus[$status]).'%';
2015-11-28 17:35:52 +01:00
2020-09-07 10:18:17 +02:00
$dataseries [] = array ( $labelStatus , ( isset ( $valsamount [ $status ]) ? ( float ) $valsamount [ $status ] : 0 ));
2021-02-26 18:49:22 +01:00
if ( ! $conf -> use_javascript_ajax ) {
2020-09-07 10:18:17 +02:00
print '<tr class="oddeven">' ;
print '<td>' . $labelStatus . '</td>' ;
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>' ;
print " </tr> \n " ;
}
}
2021-02-26 18:49:22 +01:00
if ( $conf -> use_javascript_ajax ) {
2020-09-07 10:18:17 +02:00
print '<tr><td class="center nopaddingleftimp nopaddingrightimp" colspan="2">' ;
2017-11-23 20:00:54 +01:00
2020-09-07 10:18:17 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/dolgraph.class.php' ;
$dolgraph = new DolGraph ();
$dolgraph -> SetData ( $dataseries );
$dolgraph -> SetDataColor ( array_values ( $colorseries ));
$dolgraph -> setShowLegend ( 2 );
$dolgraph -> setShowPercent ( 1 );
$dolgraph -> SetType ( array ( 'pie' ));
//$dolgraph->setWidth('100%');
$dolgraph -> SetHeight ( '200' );
$dolgraph -> draw ( 'idgraphstatus' );
print $dolgraph -> show ( $totaloppnb ? 0 : 1 );
2017-11-23 20:00:54 +01:00
2020-09-07 10:18:17 +02:00
print '</td></tr>' ;
}
//if ($totalinprocess != $total)
//print '<tr class="liste_total"><td>'.$langs->trans("Total").' ('.$langs->trans("CustomersOrdersRunning").')</td><td class="right">'.$totalinprocess.'</td></tr>';
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>' ;
print '<tr class="liste_total"><td class="minwidth200 tdoverflow">' ;
//print $langs->trans("OpportunityPonderatedAmount").' ('.$langs->trans("WonLostExcluded").')';
print $form -> textwithpicto ( $langs -> trans ( " OpportunityPonderatedAmount " ) . ' (' . $langs -> trans ( " WonLostExcluded " ) . ')' , $langs -> trans ( " OpportunityPonderatedAmountDesc " ), 1 );
print '</td><td class="right">' . price ( price2num ( $ponderated_opp_amount , 'MT' ), 0 , '' , 1 , - 1 , - 1 , $conf -> currency ) . '</td></tr>' ;
print " </table> " ;
print " </div> " ;
2017-10-19 19:53:29 +02:00
2020-09-07 10:18:17 +02:00
print " <br> " ;
2020-05-21 15:05:19 +02:00
} else {
2020-09-07 10:18:17 +02:00
dol_print_error ( $db );
2015-11-28 17:35:52 +01:00
}
}