adherentstats.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  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. * \file htdocs/adherents/class/adherentstats.class.php
  21. * \ingroup member
  22. * \brief Fichier de la classe de gestion des stats des adhérents
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  25. include_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  26. /**
  27. * Class to manage statistics of members
  28. */
  29. class AdherentStats extends Stats
  30. {
  31. /**
  32. * @var string Name of table without prefix where object is stored
  33. */
  34. public $table_element;
  35. public $memberid;
  36. public $socid;
  37. public $userid;
  38. public $from;
  39. public $field;
  40. public $where;
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. * @param int $socid Id third party
  46. * @param int $userid Id user for filter
  47. */
  48. public function __construct($db, $socid = 0, $userid = 0)
  49. {
  50. global $user, $conf;
  51. $this->db = $db;
  52. $this->socid = $socid;
  53. $this->userid = $userid;
  54. $object = new Subscription($this->db);
  55. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  56. $this->from .= ", ".MAIN_DB_PREFIX."adherent as m";
  57. $this->field = 'subscription';
  58. $this->where .= " m.statut != 0";
  59. $this->where .= " AND p.fk_adherent = m.rowid AND m.entity IN (".getEntity('adherent').")";
  60. //if (!$user->rights->societe->client->voir && !$user->socid) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
  61. if ($this->memberid)
  62. {
  63. $this->where .= " AND m.rowid = ".$this->memberid;
  64. }
  65. //if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
  66. }
  67. /**
  68. * Return the number of proposition by month for a given year
  69. *
  70. * @param int $year Year
  71. * @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
  72. * @return array Array of nb each month
  73. */
  74. public function getNbByMonth($year, $format = 0)
  75. {
  76. global $user;
  77. $sql = "SELECT date_format(p.dateadh,'%m') as dm, count(*)";
  78. $sql .= " FROM ".$this->from;
  79. //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  80. $sql .= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  81. $sql .= " AND ".$this->where;
  82. $sql .= " GROUP BY dm";
  83. $sql .= $this->db->order('dm', 'DESC');
  84. return $this->_getNbByMonth($year, $sql, $format);
  85. }
  86. /**
  87. * Return the number of subscriptions by year
  88. *
  89. * @return array Array of nb each year
  90. */
  91. public function getNbByYear()
  92. {
  93. global $user;
  94. $sql = "SELECT date_format(p.dateadh,'%Y') as dm, count(*)";
  95. $sql .= " FROM ".$this->from;
  96. //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  97. $sql .= " WHERE ".$this->where;
  98. $sql .= " GROUP BY dm";
  99. $sql .= $this->db->order('dm', 'DESC');
  100. return $this->_getNbByYear($sql);
  101. }
  102. /**
  103. * Return the number of subscriptions by month for a given year
  104. *
  105. * @param int $year Year
  106. * @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
  107. * @return array Array of amount each month
  108. */
  109. public function getAmountByMonth($year, $format = 0)
  110. {
  111. global $user;
  112. $sql = "SELECT date_format(p.dateadh,'%m') as dm, sum(p.".$this->field.")";
  113. $sql .= " FROM ".$this->from;
  114. //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  115. $sql .= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  116. $sql .= " AND ".$this->where;
  117. $sql .= " GROUP BY dm";
  118. $sql .= $this->db->order('dm', 'DESC');
  119. return $this->_getAmountByMonth($year, $sql, $format);
  120. }
  121. /**
  122. * Return average amount each month
  123. *
  124. * @param int $year Year
  125. * @return array Array of average each month
  126. */
  127. public function getAverageByMonth($year)
  128. {
  129. global $user;
  130. $sql = "SELECT date_format(p.dateadh,'%m') as dm, avg(p.".$this->field.")";
  131. $sql .= " FROM ".$this->from;
  132. //if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  133. $sql .= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  134. $sql .= " AND ".$this->where;
  135. $sql .= " GROUP BY dm";
  136. $sql .= $this->db->order('dm', 'DESC');
  137. return $this->_getAverageByMonth($year, $sql);
  138. }
  139. /**
  140. * Return nb, total and average
  141. *
  142. * @return array Array with nb, total amount, average for each year
  143. */
  144. public function getAllByYear()
  145. {
  146. global $user;
  147. $sql = "SELECT date_format(p.dateadh,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  148. $sql .= " FROM ".$this->from;
  149. //if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  150. $sql .= " WHERE ".$this->where;
  151. $sql .= " GROUP BY year";
  152. $sql .= $this->db->order('year', 'DESC');
  153. return $this->_getAllByYear($sql);
  154. }
  155. }