adherentstats.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 $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 != -1";
  59. $this->where .= " AND p.fk_adherent = m.rowid AND m.entity IN (".getEntity('adherent').")";
  60. //if (empty($user->rights->societe->client->voir) && !$user->socid) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .((int) $user->id);
  61. if ($this->memberid) {
  62. $this->where .= " AND m.rowid = ".((int) $this->memberid);
  63. }
  64. //if ($this->userid > 0) $this->where .= " AND fk_user_author = ".((int) $this->userid);
  65. }
  66. /**
  67. * Return the number of proposition by month for a given year
  68. *
  69. * @param int $year Year
  70. * @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
  71. * @return array Array of nb each month
  72. */
  73. public function getNbByMonth($year, $format = 0)
  74. {
  75. $sql = "SELECT date_format(p.dateadh,'%m') as dm, count(*)";
  76. $sql .= " FROM ".$this->from;
  77. //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  78. $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
  79. $sql .= " AND ".$this->where;
  80. $sql .= " GROUP BY dm";
  81. $sql .= $this->db->order('dm', 'DESC');
  82. return $this->_getNbByMonth($year, $sql, $format);
  83. }
  84. /**
  85. * Return the number of subscriptions by year
  86. *
  87. * @return array Array of nb each year
  88. */
  89. public function getNbByYear()
  90. {
  91. $sql = "SELECT date_format(p.dateadh,'%Y') as dm, count(*)";
  92. $sql .= " FROM ".$this->from;
  93. //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  94. $sql .= " WHERE ".$this->where;
  95. $sql .= " GROUP BY dm";
  96. $sql .= $this->db->order('dm', 'DESC');
  97. return $this->_getNbByYear($sql);
  98. }
  99. /**
  100. * Return the number of subscriptions by month for a given year
  101. *
  102. * @param int $year Year
  103. * @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
  104. * @return array Array of amount each month
  105. */
  106. public function getAmountByMonth($year, $format = 0)
  107. {
  108. $sql = "SELECT date_format(p.dateadh,'%m') as dm, sum(p.".$this->field.")";
  109. $sql .= " FROM ".$this->from;
  110. //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  111. $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
  112. $sql .= " AND ".$this->where;
  113. $sql .= " GROUP BY dm";
  114. $sql .= $this->db->order('dm', 'DESC');
  115. return $this->_getAmountByMonth($year, $sql, $format);
  116. }
  117. /**
  118. * Return average amount each month
  119. *
  120. * @param int $year Year
  121. * @return array Array of average each month
  122. */
  123. public function getAverageByMonth($year)
  124. {
  125. $sql = "SELECT date_format(p.dateadh,'%m') as dm, avg(p.".$this->field.")";
  126. $sql .= " FROM ".$this->from;
  127. //if (empty($user->rights->societe->client->voir) && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  128. $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
  129. $sql .= " AND ".$this->where;
  130. $sql .= " GROUP BY dm";
  131. $sql .= $this->db->order('dm', 'DESC');
  132. return $this->_getAverageByMonth($year, $sql);
  133. }
  134. /**
  135. * Return nb, total and average
  136. *
  137. * @return array Array with nb, total amount, average for each year
  138. */
  139. public function getAllByYear()
  140. {
  141. $sql = "SELECT date_format(p.dateadh,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  142. $sql .= " FROM ".$this->from;
  143. //if (empty($user->rights->societe->client->voir) && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  144. $sql .= " WHERE ".$this->where;
  145. $sql .= " GROUP BY year";
  146. $sql .= $this->db->order('year', 'DESC');
  147. return $this->_getAllByYear($sql);
  148. }
  149. }