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 '