receptionstats.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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@capnetworks.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/reception/class/receptionstats.class.php
  22. * \ingroup reception
  23. * \brief File of class fo tmanage reception statistics
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. /**
  29. * Class to manage reception statistics
  30. */
  31. class ReceptionStats extends Stats
  32. {
  33. public $table_element;
  34. public $socid;
  35. public $userid;
  36. public $from;
  37. public $field;
  38. public $where;
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. * @param int $socid Id third party for filter
  44. * @param string $mode Option (not used)
  45. * @param int $userid Id user for filter (creation user)
  46. */
  47. public function __construct($db, $socid, $mode, $userid = 0)
  48. {
  49. global $user, $conf;
  50. $this->db = $db;
  51. $this->socid = ($socid > 0 ? $socid : 0);
  52. $this->userid = $userid;
  53. $this->cachefilesuffix = $mode;
  54. $object = new Reception($this->db);
  55. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  56. //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
  57. $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
  58. $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
  59. //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
  60. $this->where .= " AND c.entity IN (".getEntity('reception').")";
  61. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  62. $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  63. }
  64. if ($this->socid) {
  65. $this->where .= " AND c.fk_soc = ".((int) $this->socid);
  66. }
  67. if ($this->userid > 0) {
  68. $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
  69. }
  70. }
  71. /**
  72. * Return reception number by month for a year
  73. *
  74. * @param int $year Year to scan
  75. * @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
  76. * @return array Array with number by month
  77. */
  78. public function getNbByMonth($year, $format = 0)
  79. {
  80. global $user;
  81. $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
  82. $sql .= " FROM ".$this->from;
  83. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  84. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  85. }
  86. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  87. $sql .= " AND ".$this->where;
  88. $sql .= " GROUP BY dm";
  89. $sql .= $this->db->order('dm', 'DESC');
  90. $res = $this->_getNbByMonth($year, $sql, $format);
  91. return $res;
  92. }
  93. /**
  94. * Return receptions number per year
  95. *
  96. * @return array Array with number by year
  97. *
  98. */
  99. public function getNbByYear()
  100. {
  101. global $user;
  102. $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  103. $sql .= " FROM ".$this->from;
  104. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  105. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  106. }
  107. $sql .= " WHERE ".$this->where;
  108. $sql .= " GROUP BY dm";
  109. $sql .= $this->db->order('dm', 'DESC');
  110. return $this->_getNbByYear($sql);
  111. }
  112. /**
  113. * Return the orders amount by month for a year
  114. *
  115. * @param int $year Year to scan
  116. * @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
  117. * @return array Array with amount by month
  118. */
  119. public function getAmountByMonth($year, $format = 0)
  120. {
  121. global $user;
  122. $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
  123. $sql .= " FROM ".$this->from;
  124. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  125. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  126. }
  127. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  128. $sql .= " AND ".$this->where;
  129. $sql .= " GROUP BY dm";
  130. $sql .= $this->db->order('dm', 'DESC');
  131. $res = $this->_getAmountByMonth($year, $sql, $format);
  132. return $res;
  133. }
  134. /**
  135. * Return the orders amount average by month for a year
  136. *
  137. * @param int $year year for stats
  138. * @return array array with number by month
  139. */
  140. public function getAverageByMonth($year)
  141. {
  142. global $user;
  143. $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
  144. $sql .= " FROM ".$this->from;
  145. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  146. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  147. }
  148. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  149. $sql .= " AND ".$this->where;
  150. $sql .= " GROUP BY dm";
  151. $sql .= $this->db->order('dm', 'DESC');
  152. return $this->_getAverageByMonth($year, $sql);
  153. }
  154. /**
  155. * Return nb, total and average
  156. *
  157. * @return array Array of values
  158. */
  159. public function getAllByYear()
  160. {
  161. global $user;
  162. $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
  163. $sql .= " FROM ".$this->from;
  164. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  165. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  166. }
  167. $sql .= " WHERE ".$this->where;
  168. $sql .= " GROUP BY year";
  169. $sql .= $this->db->order('year', 'DESC');
  170. return $this->_getAllByYear($sql);
  171. }
  172. }