donstats.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/don/class/donstats.class.php
  22. * \ingroup donations
  23. * \brief File of class to manage donations statistics
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. /**
  29. * Class to manage donations statistics
  30. */
  31. class DonationStats extends Stats
  32. {
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element;
  37. public $socid;
  38. public $userid;
  39. /**
  40. * @var string FROM
  41. */
  42. public $from;
  43. /**
  44. * @var string field
  45. */
  46. public $field;
  47. /**
  48. * @var string WHERE
  49. */
  50. public $where;
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDB $db Database handler
  55. * @param int $socid Id third party for filter
  56. * @param string $mode Option (not used)
  57. * @param int $userid Id user for filter (creation user)
  58. */
  59. public function __construct($db, $socid, $mode, $userid = 0)
  60. {
  61. global $conf;
  62. $this->db = $db;
  63. $this->field = 'amount';
  64. $this->socid = ($socid > 0 ? $socid : 0);
  65. $this->userid = $userid;
  66. $this->cachefilesuffix = $mode;
  67. $object = new Don($this->db);
  68. $this->from = MAIN_DB_PREFIX.$object->table_element." as d";
  69. //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
  70. //$this->field='weight'; // Warning, unit of weight is NOT USED AND MUST BE
  71. $this->where .= " d.fk_statut > 0"; // Not draft and not cancelled
  72. //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
  73. $this->where .= " AND d.entity = ".$conf->entity;
  74. if ($this->userid > 0) {
  75. $this->where .= ' WHERE c.fk_user_author = '.((int) $this->userid);
  76. }
  77. }
  78. /**
  79. * Return shipment number by month for a year
  80. *
  81. * @param int $year Year to scan
  82. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  83. * @return array Array with number by month
  84. */
  85. public function getNbByMonth($year, $format = 0)
  86. {
  87. $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
  88. $sql .= " FROM ".$this->from;
  89. $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  90. $sql .= " AND ".$this->where;
  91. $sql .= " GROUP BY dm";
  92. $sql .= $this->db->order('dm', 'DESC');
  93. $res = $this->_getNbByMonth($year, $sql, $format);
  94. return $res;
  95. }
  96. /**
  97. * Return shipments number per year
  98. *
  99. * @return array Array with number by year
  100. *
  101. */
  102. public function getNbByYear()
  103. {
  104. $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
  105. $sql .= " FROM ".$this->from;
  106. $sql .= " WHERE ".$this->where;
  107. $sql .= " GROUP BY dm";
  108. $sql .= $this->db->order('dm', 'DESC');
  109. return $this->_getNbByYear($sql);
  110. }
  111. /**
  112. * Return the number of subscriptions by month for a given year
  113. *
  114. * @param int $year Year
  115. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  116. * @return array Array of amount each month
  117. */
  118. public function getAmountByMonth($year, $format = 0)
  119. {
  120. $sql = "SELECT date_format(d.datedon,'%m') as dm, sum(d.".$this->field.")";
  121. $sql .= " FROM ".$this->from;
  122. //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  123. $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
  124. $sql .= " AND ".$this->where;
  125. $sql .= " GROUP BY dm";
  126. $sql .= $this->db->order('dm', 'DESC');
  127. return $this->_getAmountByMonth($year, $sql, $format);
  128. }
  129. /**
  130. * Return average amount each month
  131. *
  132. * @param int $year Year
  133. * @return array Array of average each month
  134. */
  135. public function getAverageByMonth($year)
  136. {
  137. $sql = "SELECT date_format(d.datedon,'%m') as dm, avg(d.".$this->field.")";
  138. $sql .= " FROM ".$this->from;
  139. //if (empty($user->rights->societe->client->voir) && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  140. $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
  141. $sql .= " AND ".$this->where;
  142. $sql .= " GROUP BY dm";
  143. $sql .= $this->db->order('dm', 'DESC');
  144. return $this->_getAverageByMonth($year, $sql);
  145. }
  146. /**
  147. * Return nb, total and average
  148. *
  149. * @return array Array of values
  150. */
  151. public function getAllByYear()
  152. {
  153. $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
  154. $sql .= " FROM ".$this->from;
  155. $sql .= " WHERE ".$this->where;
  156. $sql .= " GROUP BY year";
  157. $sql .= $this->db->order('year', 'DESC');
  158. return $this->_getAllByYear($sql);
  159. }
  160. }