adherentstats.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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@dolibarr.fr>
  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 2 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 <http://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/cotisation.class.php";
  26. /**
  27. * \class AdherentStats
  28. * \brief Classe permettant la gestion des stats des adherents
  29. */
  30. class AdherentStats extends Stats
  31. {
  32. var $db;
  33. var $socid;
  34. var $userid;
  35. var $table_element;
  36. var $from;
  37. var $field;
  38. var $where;
  39. /**
  40. * Constructor
  41. *
  42. * @param $DB Database handler
  43. * @param $socid Id third party
  44. * @param $userid Id user for filter
  45. * @return AdherentStats
  46. */
  47. function AdherentStats($DB, $socid=0, $userid=0)
  48. {
  49. global $user, $conf;
  50. $this->db = $DB;
  51. $this->socid = $socid;
  52. $this->userid = $userid;
  53. $object=new Cotisation($this->db);
  54. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  55. $this->from.= ", ".MAIN_DB_PREFIX."adherent as m";
  56. $this->field='cotisation';
  57. $this->where.= " m.statut != 0";
  58. $this->where.= " AND p.fk_adherent = m.rowid AND m.entity = ".$conf->entity;
  59. //if (!$user->rights->societe->client->voir && !$user->societe_id) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
  60. if($this->memberid)
  61. {
  62. $this->where .= " AND m.rowid = ".$this->memberid;
  63. }
  64. //if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
  65. }
  66. /**
  67. * Renvoie le nombre de proposition par mois pour une annee donnee
  68. */
  69. function getNbByMonth($year)
  70. {
  71. global $user;
  72. $sql = "SELECT date_format(p.dateadh,'%m') as dm, count(*)";
  73. $sql.= " FROM ".$this->from;
  74. //if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  75. $sql.= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  76. $sql.= " AND ".$this->where;
  77. $sql.= " GROUP BY dm";
  78. $sql.= $this->db->order('dm','DESC');
  79. return $this->_getNbByMonth($year, $sql);
  80. }
  81. /**
  82. * Renvoie le nombre de cotisation par annee
  83. *
  84. */
  85. function getNbByYear()
  86. {
  87. global $user;
  88. $sql = "SELECT date_format(p.dateadh,'%Y') as dm, count(*)";
  89. $sql.= " FROM ".$this->from;
  90. //if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  91. $sql.= " WHERE ".$this->where;
  92. $sql.= " GROUP BY dm";
  93. $sql.= $this->db->order('dm','DESC');
  94. return $this->_getNbByYear($sql);
  95. }
  96. /**
  97. * Renvoie le nombre de cotisation par mois pour une annee donnee
  98. *
  99. */
  100. function getAmountByMonth($year)
  101. {
  102. global $user;
  103. $sql = "SELECT date_format(p.dateadh,'%m') as dm, sum(p.".$this->field.")";
  104. $sql.= " FROM ".$this->from;
  105. //if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  106. $sql.= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  107. $sql.= " AND ".$this->where;
  108. $sql.= " GROUP BY dm";
  109. $sql.= $this->db->order('dm','DESC');
  110. return $this->_getAmountByMonth($year, $sql);
  111. }
  112. /**
  113. *
  114. *
  115. */
  116. function getAverageByMonth($year)
  117. {
  118. global $user;
  119. $sql = "SELECT date_format(p.dateadh,'%m') as dm, avg(p.".$this->field.")";
  120. $sql.= " FROM ".$this->from;
  121. //if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  122. $sql.= " WHERE date_format(p.dateadh,'%Y') = '".$year."'";
  123. $sql.= " AND ".$this->where;
  124. $sql.= " GROUP BY dm";
  125. $sql.= $this->db->order('dm','DESC');
  126. return $this->_getAverageByMonth($year, $sql);
  127. }
  128. /**
  129. * \brief Return nb, total and average
  130. * \return array Array of values
  131. */
  132. function getAllByYear()
  133. {
  134. global $user;
  135. $sql = "SELECT date_format(p.dateadh,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  136. $sql.= " FROM ".$this->from;
  137. //if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  138. $sql.= " WHERE ".$this->where;
  139. $sql.= " GROUP BY year";
  140. $sql.= $this->db->order('year','DESC');
  141. return $this->_getAllByYear($sql);
  142. }
  143. }
  144. ?>