adherentstats.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * Copyright (C) 2023 Waël Almoman <info@almoman.com>
  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/adherents/class/adherentstats.class.php
  22. * \ingroup member
  23. * \brief Fichier de la classe de gestion des stats des adhérents
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  27. /**
  28. * Class to manage statistics of members
  29. */
  30. class AdherentStats extends Stats
  31. {
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element;
  36. public $memberid;
  37. public $socid;
  38. public $userid;
  39. public $from;
  40. public $field;
  41. public $where;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $db Database handler
  46. * @param int $socid Id third party
  47. * @param int $userid Id user for filter
  48. */
  49. public function __construct($db, $socid = 0, $userid = 0)
  50. {
  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. /**
  150. * Return count of member by status group by adh type, total and average
  151. *
  152. * @param int $numberYears Number of years to scan (0 = all)
  153. * @return array Array with total of draft, pending, uptodate, expired, resiliated for each member type
  154. */
  155. public function countMembersByTypeAndStatus($numberYears = 0)
  156. {
  157. global $user;
  158. $now = dol_now();
  159. $endYear = date('Y');
  160. $startYear = $endYear - $numberYears;
  161. $sql = "SELECT t.rowid as fk_adherent_type, t.libelle as label";
  162. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_DRAFT, "'members_draft'", 'NULL').") as members_draft";
  163. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin IS NULL AND t.subscription = '1')", "'members_pending'", 'NULL').") as members_pending";
  164. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin >= '".$this->db->idate($now)."' OR t.subscription = 0)", "'members_uptodate'", 'NULL').") as members_uptodate";
  165. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin < '".$this->db->idate($now)."' AND t.subscription = 1)", "'members_expired'", 'NULL').") as members_expired";
  166. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_EXCLUDED, "'members_excluded'", 'NULL').") as members_excluded";
  167. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_RESILIATED, "'members_resiliated'", 'NULL').") as members_resiliated";
  168. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as t";
  169. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as d ON t.rowid = d.fk_adherent_type AND d.entity IN (" . getEntity('adherent') . ")";
  170. if ($numberYears) {
  171. $sql .= " AND d.datefin > '".$this->db->idate(dol_get_first_day($startYear))."'";
  172. }
  173. $sql .= " WHERE t.entity IN (".getEntity('member_type').")";
  174. $sql .= " AND t.statut = 1";
  175. $sql .= " GROUP BY t.rowid, t.libelle";
  176. dol_syslog("box_members_by_type::select nb of members per type", LOG_DEBUG);
  177. $result = $this->db->query($sql);
  178. $MembersCountArray = array();
  179. if ($result) {
  180. $num = $this->db->num_rows($result);
  181. $i = 0;
  182. $totalstatus = array(
  183. 'label' => 'Total',
  184. 'members_draft' => 0,
  185. 'members_pending' => 0,
  186. 'members_uptodate' => 0,
  187. 'members_expired' => 0,
  188. 'members_excluded' => 0,
  189. 'members_resiliated' => 0
  190. );
  191. while ($i < $num) {
  192. $objp = $this->db->fetch_object($result);
  193. $MembersCountArray[$objp->fk_adherent_type] = array(
  194. 'label' => $objp->label,
  195. 'members_draft' => (int) $objp->members_draft,
  196. 'members_pending' => (int) $objp->members_pending,
  197. 'members_uptodate' => (int) $objp->members_uptodate,
  198. 'members_expired' => (int) $objp->members_expired,
  199. 'members_excluded' => (int) $objp->members_excluded,
  200. 'members_resiliated' => (int) $objp->members_resiliated
  201. );
  202. $totalrow = 0;
  203. foreach ($MembersCountArray[$objp->fk_adherent_type] as $key=>$nb) {
  204. if ($key != 'label') {
  205. $totalrow += $nb;
  206. $totalstatus[$key] += $nb;
  207. }
  208. }
  209. $MembersCountArray[$objp->fk_adherent_type]['total_adhtype'] = $totalrow;
  210. $i++;
  211. }
  212. $this->db->free($result);
  213. $MembersCountArray['total'] = $totalstatus;
  214. $MembersCountArray['total']['all'] = array_sum($totalstatus);
  215. }
  216. return $MembersCountArray;
  217. }
  218. /**
  219. * Return count of member by status group by adh type, total and average
  220. *
  221. * @param int $numberYears Number of years to scan (0 = all)
  222. * @return array Array with total of draft, pending, uptodate, expired, resiliated for each member tag
  223. */
  224. public function countMembersByTagAndStatus($numberYears = 0)
  225. {
  226. global $user;
  227. $now = dol_now();
  228. $endYear = date('Y');
  229. $startYear = $endYear - $numberYears;
  230. $sql = "SELECT c.rowid as fk_categorie, c.label as label";
  231. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_DRAFT, "'members_draft'", 'NULL').") as members_draft";
  232. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin IS NULL AND t.subscription = '1')", "'members_pending'", 'NULL').") as members_pending";
  233. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin >= '".$this->db->idate($now)."' OR t.subscription = 0)", "'members_uptodate'", 'NULL').") as members_uptodate";
  234. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin < '".$this->db->idate($now)."' AND t.subscription = 1)", "'members_expired'", 'NULL').") as members_expired";
  235. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_EXCLUDED, "'members_excluded'", 'NULL').") as members_excluded";
  236. $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_RESILIATED, "'members_resiliated'", 'NULL').") as members_resiliated";
  237. $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c";
  238. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_member as ct ON c.rowid = ct.fk_categorie";
  239. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as d ON d.rowid = ct.fk_member";
  240. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_type as t ON t.rowid = d.fk_adherent_type";
  241. $sql .= " WHERE c.entity IN (".getEntity('member_type').")";
  242. $sql .= " AND d.entity IN (" . getEntity('adherent') . ")";
  243. $sql .= " AND t.entity IN (" . getEntity('adherent') . ")";
  244. if ($numberYears) {
  245. $sql .= " AND d.datefin > '".$this->db->idate(dol_get_first_day($startYear))."'";
  246. }
  247. $sql .= " AND c.fk_parent = 0";
  248. $sql .= " GROUP BY c.rowid, c.label";
  249. $sql .= " ORDER BY label ASC";
  250. dol_syslog("box_members_by_tag::select nb of members per tag", LOG_DEBUG);
  251. $result = $this->db->query($sql);
  252. if ($result) {
  253. $num = $this->db->num_rows($result);
  254. $i = 0;
  255. $MembersCountArray = [];
  256. $totalstatus = array(
  257. 'label' => 'Total',
  258. 'members_draft' => 0,
  259. 'members_pending' => 0,
  260. 'members_uptodate' => 0,
  261. 'members_expired' => 0,
  262. 'members_excluded' => 0,
  263. 'members_resiliated' => 0
  264. );
  265. while ($i < $num) {
  266. $objp = $this->db->fetch_object($result);
  267. $MembersCountArray[$objp->fk_categorie] = array(
  268. 'label' => $objp->label,
  269. 'members_draft' => (int) $objp->members_draft,
  270. 'members_pending' => (int) $objp->members_pending,
  271. 'members_uptodate' => (int) $objp->members_uptodate,
  272. 'members_expired' => (int) $objp->members_expired,
  273. 'members_excluded' => (int) $objp->members_excluded,
  274. 'members_resiliated' => (int) $objp->members_resiliated
  275. );
  276. $totalrow = 0;
  277. foreach ($MembersCountArray[$objp->fk_categorie] as $key=>$nb) {
  278. if ($key != 'label') {
  279. $totalrow += $nb;
  280. $totalstatus[$key] += $nb;
  281. }
  282. }
  283. $MembersCountArray[$objp->fk_categorie]['total_adhtag'] = $totalrow;
  284. $i++;
  285. }
  286. $this->db->free($result);
  287. $MembersCountArray['total'] = $totalstatus;
  288. $MembersCountArray['total']['all'] = array_sum($totalstatus);
  289. }
  290. return $MembersCountArray;
  291. }
  292. }