From 595d735feec89f8054be35c14db84f6fc01907da Mon Sep 17 00:00:00 2001 From: Thomas C <56068416+Thomas905@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:17:52 +0100 Subject: [PATCH] NEW : Statistic page donation (#28463) * v1 statistic page don * add link * fix phpstan * fix * fix * Update index.php --------- Co-authored-by: Laurent Destailleur --- htdocs/core/menus/standard/eldy.lib.php | 1 + htdocs/don/class/donstats.class.php | 41 +++- htdocs/don/stats/index.php | 239 +++++++++++++++--------- htdocs/langs/en_US/donations.lang | 1 + 4 files changed, 185 insertions(+), 97 deletions(-) diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 47cfd7c081d..c8561fd1f11 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -1547,6 +1547,7 @@ function get_left_menu_billing($mainmenu, &$newmenu, $usemenuhider = 1, $leftmen $newmenu->add("/don/card.php?leftmenu=donations&action=create", $langs->trans("NewDonation"), 1, $user->hasRight('don', 'creer')); $newmenu->add("/don/list.php?leftmenu=donations", $langs->trans("List"), 1, $user->hasRight('don', 'lire')); $newmenu->add("/don/paiement/list.php?leftmenu=donations", $langs->trans("Payments"), 1, $user->hasRight('don', 'lire')); + $newmenu->add("/don/stats/index.php", $langs->trans("Statistics"), 1, $user->hasRight('don', 'lire')); } // if ($leftmenu=="donations") $newmenu->add("/don/stats/index.php",$langs->trans("Statistics"), 1, $user->hasRight('don', 'lire')); } diff --git a/htdocs/don/class/donstats.class.php b/htdocs/don/class/donstats.class.php index a4b8043f5ae..15ab2e454e3 100644 --- a/htdocs/don/class/donstats.class.php +++ b/htdocs/don/class/donstats.class.php @@ -28,7 +28,6 @@ include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php'; include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; - /** * Class to manage donations statistics */ @@ -57,6 +56,10 @@ class DonationStats extends Stats */ public $where; + /** + * @var string JOIN + */ + public $join; /** * Constructor @@ -65,8 +68,10 @@ class DonationStats extends Stats * @param int $socid Id third party for filter * @param string $mode Option (not used) * @param int $userid Id user for filter (creation user) + * @param int $typentid Id of type of third party for filter + * @param int $status Status of donation for filter */ - public function __construct($db, $socid, $mode, $userid = 0) + public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $status = 4) { global $conf; @@ -76,17 +81,31 @@ class DonationStats extends Stats $this->socid = ($socid > 0 ? $socid : 0); $this->userid = $userid; $this->cachefilesuffix = $mode; + $this->join = ''; + + if ($status == 0 || $status == 1 || $status == 2) { + $this->where = ' d.fk_statut IN ('.$db->sanitize($status).')'; + } elseif ($status == 3) { + $this->where = ' d.fk_statut IN (-1)'; + } elseif ($status == 4) { + $this->where = ' d.fk_statut >= 0'; + } $object = new Don($this->db); $this->from = MAIN_DB_PREFIX.$object->table_element." as d"; - //$this->from.= ", ".MAIN_DB_PREFIX."societe as s"; - //$this->field='weight'; // Warning, unit of weight is NOT USED AND MUST BE - $this->where .= " d.fk_statut > 0"; // Not draft and not cancelled - //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity; + if ($socid !== "-1" && $socid > 0) { + $this->where .= " AND d.fk_soc = ".((int) $socid); + } + $this->where .= " AND d.entity = ".$conf->entity; if ($this->userid > 0) { - $this->where .= ' WHERE c.fk_user_author = '.((int) $this->userid); + $this->where .= ' AND d.fk_user_author = '.((int) $this->userid); + } + + if ($typentid) { + $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = d.fk_soc'; + $this->where .= ' AND s.fk_typent = '.((int) $typentid); } } @@ -101,13 +120,13 @@ class DonationStats extends Stats { $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb"; $sql .= " FROM ".$this->from; + $sql .= $this->join; $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; $sql .= " AND ".$this->where; $sql .= " GROUP BY dm"; $sql .= $this->db->order('dm', 'DESC'); - $res = $this->_getNbByMonth($year, $sql, $format); - return $res; + return $this->_getNbByMonth($year, $sql, $format); } /** @@ -120,6 +139,7 @@ class DonationStats extends Stats { $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")"; $sql .= " FROM ".$this->from; + $sql .= $this->join; $sql .= " WHERE ".$this->where; $sql .= " GROUP BY dm"; $sql .= $this->db->order('dm', 'DESC'); @@ -138,6 +158,7 @@ class DonationStats extends Stats { $sql = "SELECT date_format(d.datedon,'%m') as dm, sum(d.".$this->field.")"; $sql .= " FROM ".$this->from; + $sql .= $this->join; $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1); $sql .= " AND ".$this->where; $sql .= " GROUP BY dm"; @@ -156,6 +177,7 @@ class DonationStats extends Stats { $sql = "SELECT date_format(d.datedon,'%m') as dm, avg(d.".$this->field.")"; $sql .= " FROM ".$this->from; + $sql .= $this->join; $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1); $sql .= " AND ".$this->where; $sql .= " GROUP BY dm"; @@ -173,6 +195,7 @@ class DonationStats extends Stats { $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg"; $sql .= " FROM ".$this->from; + $sql .= $this->join; $sql .= " WHERE ".$this->where; $sql .= " GROUP BY year"; $sql .= $this->db->order('year', 'DESC'); diff --git a/htdocs/don/stats/index.php b/htdocs/don/stats/index.php index 1629ac2e984..1bb4458c37e 100644 --- a/htdocs/don/stats/index.php +++ b/htdocs/don/stats/index.php @@ -29,10 +29,16 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php'; require_once DOL_DOCUMENT_ROOT.'/don/class/donstats.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; - +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; +if (isModEnabled('category')) { + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; +} $WIDTH = DolGraph::getDefaultGraphSizeForStats('width'); $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height'); +// Load translation files required by the page +$langs->loadLangs("donations"); + $userid = GETPOSTINT('userid'); $socid = GETPOSTINT('socid'); // Security check @@ -41,44 +47,44 @@ if ($user->socid > 0) { $socid = $user->socid; } +$status = GETPOSTINT('status'); $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt'); +$typent_id = GETPOSTINT('typent_id'); $year = GETPOST('year') > 0 ? GETPOST('year') : $nowyear; -$startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS')))); +$startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, getDolGlobalInt('MAIN_STATS_GRAPHS_SHOW_N_YEARS')))); $endyear = $year; - -// Load translation files required by the page -$langs->loadLangs(array("companies", "other", "sendings")); +$mode = GETPOST("mode") ? GETPOST("mode") : 'customer'; +$custcats = GETPOST('custcats', 'array'); // Security check $result = restrictedArea($user, 'don'); - /* * View */ - $form = new Form($db); -$title = $langs->trans("DonationsStatistics"); -llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-donation page-stats'); +$formcompany = new FormCompany($db); -print load_fiche_titre($langs->trans("DonationsStatistics"), $mesg); +llxHeader(); +$dir = $conf->don->dir_temp; + +print load_fiche_titre($langs->trans("DonationsStatistics"), '', 'donation'); dol_mkdir($dir); -$stats = new DonationStats($db, $socid, '', ($userid > 0 ? $userid : 0)); +$stats = new DonationStats($db, $socid, '', ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($status > 0 ? $status : 4)); + +if (is_array($custcats) && !empty($custcats)) { + $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cat ON (d.fk_soc = cat.fk_soc)'; + $stats->where .= ' AND cat.fk_categorie IN ('.$db->sanitize(implode(',', $custcats)).')'; +} // Build graphic number of object $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear); -//var_dump($data);exit; -// $data = array(array('Lib',val1,val2,val3),...) - -if (!$user->hasRight('societe', 'client', 'voir')) { - $filenamenb = $dir.'/donationnbinyear-'.$user->id.'-'.$year.'.png'; -} else { - $filenamenb = $dir.'/donationnbinyear-'.$year.'.png'; -} +$filenamenb = $dir."/salariesnbinyear-".$year.".png"; +$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=donationStats&file=donationinyear-'.$year.'.png'; $px1 = new DolGraph(); $mesg = $px1->isGraphKo(); @@ -99,11 +105,67 @@ if (!$mesg) { $px1->SetShading(3); $px1->SetHorizTickIncrement(1); $px1->mode = 'depth'; - $px1->SetTitle($langs->trans("NumberOfDonationsByMonth")); + $px1->SetTitle($langs->trans("NumberByMonth")); $px1->draw($filenamenb, $fileurlnb); } +$data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear); + +$filenameamount = $dir."/donationamount-".$year.".png"; +$fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=donationStats&file=donationamoutinyear-'.$year.'.png'; + +$px2 = new DolGraph(); +$mesg = $px2->isGraphKo(); +if (!$mesg) { + $px2->SetData($data); + $i=$startyear;$legend=array(); + while ($i <= $endyear) { + $legend[]=$i; + $i++; + } + $px2->SetLegend($legend); + $px2->SetMaxValue($px2->GetCeilMaxValue()); + $px2->SetMinValue(min(0, $px2->GetFloorMinValue())); + $px2->SetWidth($WIDTH); + $px2->SetHeight($HEIGHT); + $px2->SetYLabel($langs->trans("Amount")); + $px2->SetShading(3); + $px2->SetHorizTickIncrement(1); + $px2->mode='depth'; + $px2->SetTitle($langs->trans("AmountTotal")); + + $px2->draw($filenameamount, $fileurlamount); +} + +$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear); + +$filename_avg = $dir."/donationaverage-".$year.".png"; +$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=donationStats&file=donationaverageinyear-'.$year.'.png'; + +$px3 = new DolGraph(); +$mesg = $px3->isGraphKo(); +if (!$mesg) { + $px3->SetData($data); + $i = $startyear; + $legend = array(); + while ($i <= $endyear) { + $legend[] = $i; + $i++; + } + $px3->SetLegend($legend); + $px3->SetYLabel($langs->trans("AmountAverage")); + $px3->SetMaxValue($px3->GetCeilMaxValue()); + $px3->SetMinValue($px3->GetFloorMinValue()); + $px3->SetWidth($WIDTH); + $px3->SetHeight($HEIGHT); + $px3->SetShading(3); + $px3->SetHorizTickIncrement(1); + $px3->mode = 'depth'; + $px3->SetTitle($langs->trans("AmountAverage")); + + $px3->draw($filename_avg, $fileurl_avg); +} // Show array $data = $stats->getAllByYear(); @@ -133,33 +195,62 @@ print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1); print '
'; - // Show filter box print '
'; print ''; -print ''; +print '
'; print ''; + // Company -print ''; +} + +// ThirdParty Type +print ''; + +// Category +if (isModEnabled('category')) { + $cat_type = Categorie::TYPE_CUSTOMER; + $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer")); + print ''; +} + // User -print ''; + +// Status +print ''; print ''; print '
'.$langs->trans("Filter").'
'.$langs->trans("ThirdParty").''; -print img_picto('', 'company', 'class="pictofixedwidth"'); -print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', ''); +if (empty(!$conf->global->DONATION_USE_THIRDPARTIES)) { + print '
'.$langs->trans("ThirdParty").''; + print img_picto('', 'company', 'class="pictofixedwidth"'); + print $form->select_company($socid, 'socid', '', 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300', ''); + print '
'.$langs->trans("ThirdPartyType").''; +$sortparam_typent = (empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ? 'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT); +print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1); +if ($user->admin) { + print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); +} print '
'.$cat_label.''; + $cate_arbo = $form->select_all_categories($cat_type, null, 'parent', null, null, 1); + print img_picto('', 'category', 'class="pictofixedwidth"'); + print $form->multiselectarray('custcats', $cate_arbo, GETPOST('custcats', 'array'), 0, 0, 'widthcentpercentminusx maxwidth300'); + print '
'.$langs->trans("CreatedBy").''; +print '
'.$langs->trans("CreatedBy").''; print img_picto('', 'user', 'class="pictofixedwidth"'); print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300'); print '
'.$langs->trans("Status").''; +$liststatus = array( + '2' => $langs->trans("DonationStatusPaid"), + '0' => $langs->trans("DonationStatusPromiseNotValidated"), + '1' => $langs->trans("DonationStatusPromiseValidated"), + '3' => $langs->trans("Canceled") +); +print $form->selectarray('status', $liststatus, 4, 1); + // Year -print '
'.$langs->trans("Year").''; -if (!in_array($year, $arrayyears)) { - $arrayyears[$year] = $year; -} -if (!in_array($nowyear, $arrayyears)) { - $arrayyears[$nowyear] = $nowyear; -} +print '
'.$langs->trans("Year").''; arsort($arrayyears); print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75'); + print '
'; @@ -167,33 +258,46 @@ print '
'; print '

'; print '
'; -print ''; -print ''; +print '
'; +print ''; print ''; print ''; -/*print ''; -print '';*/ +print ''; +print ''; +print ''; +print ''; +print ''; print ''; $oldyear = 0; foreach ($data as $val) { $year = $val['year']; - while (!empty($year) && $oldyear > $year + 1) { // If we have empty year + while (!empty($year) && $oldyear > $year + 1) { $oldyear--; - print ''; + print ''; print ''; print ''; - /*print ''; - print '';*/ + print ''; + print ''; + print ''; + print ''; + print ''; print ''; } - print ''; - print ''; + $greennb = (empty($val['nb_diff']) || $val['nb_diff'] >= 0); + $greentotal = (empty($val['total_diff']) || $val['total_diff'] >= 0); + $greenavg = (empty($val['avg_diff']) || $val['avg_diff'] >= 0); + + print ''; + print ''; print ''; - /*print ''; - print '';*/ + print ''; + print ''; + print ''; + print ''; + print ''; print ''; $oldyear = $year; } @@ -201,10 +305,8 @@ foreach ($data as $val) { print '
'.$langs->trans("Year").''.$langs->trans("NbOfDonations").''.$langs->trans("AmountTotal").''.$langs->trans("AmountAverage").'%'.$langs->trans("AmountTotal").'%'.$langs->trans("AmountAverage").'%
'.$oldyear.'00000
'.$year.'
0 ? '&userid='.$userid : '').'">'.$year.''.$val['nb'].''.price(price2num($val['total'],'MT'),1).''.price(price2num($val['avg'],'MT'),1).''.(!empty($val['nb_diff']) && $val['nb_diff'] < 0 ? '' : '+').round(!empty($val['nb_diff']) ? $val['nb_diff'] : 0).'%'.price(price2num($val['total'], 'MT'), 1).''.( !empty($val['total_diff']) && $val['total_diff'] < 0 ? '' : '+').round(!empty($val['total_diff']) ? $val['total_diff'] : 0).'%'.price(price2num($val['avg'], 'MT'), 1).''.(!empty($val['avg_diff']) && $val['avg_diff'] < 0 ? '' : '+').round(!empty($val['avg_diff']) ? $val['avg_diff'] : 0).'%
'; print '
'; - print '
'; - // Show graphs print '
'; if ($mesg) { @@ -212,55 +314,16 @@ if ($mesg) { } else { print $px1->show(); print "
\n"; - /*print $px2->show(); + print $px2->show(); print "
\n"; - print $px3->show();*/ + print $px3->show(); } print '
'; - print '
'; print '
'; print dol_get_fiche_end(); - - -// TODO USe code similar to commande/stats/index.php instead of this one. -/* -print ''; -print ''; -print ''; - -$sql = "SELECT count(*) as nb, date_format(date_expedition,'%Y') as dm"; -$sql.= " FROM ".MAIN_DB_PREFIX."expedition"; -$sql.= " WHERE fk_statut > 0"; -$sql.= " AND entity = ".$conf->entity; -$sql.= " GROUP BY dm DESC"; - -$resql=$db->query($sql); -if ($resql) -{ - $num = $db->num_rows($resql); - $i = 0; - while ($i < $num) - { - $row = $db->fetch_row($resql); - $nbproduct = $row[0]; - $year = $row[1]; - print ""; - print ''; - $i++; - } -} -$db->free($resql); - -print '
'.$langs->trans("Year").''.$langs->trans("NbOfSendings").'
'.$year.''.$nbproduct.'
'; -*/ - -print '
'; -print ''.$langs->trans("StatsOnDonationsOnlyValidated").''; - llxFooter(); - $db->close(); diff --git a/htdocs/langs/en_US/donations.lang b/htdocs/langs/en_US/donations.lang index e60a65efb46..eaebe7c2b03 100644 --- a/htdocs/langs/en_US/donations.lang +++ b/htdocs/langs/en_US/donations.lang @@ -35,3 +35,4 @@ DonationPayments=Donation payments DonationValidated=Donation %s validated DonationUseThirdparties=Use the address of an existing thirdparty as the address of the donor DonationsStatistics=Donation's statistics +NbOfDonations=Number of donations