fichinterstats.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.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/fichinter/class/fichinterstats.class.php
  22. * \ingroup fichinter
  23. * \brief File of class to manage intervention statistics
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. /**
  29. * Class to manage intervention statistics
  30. */
  31. class FichinterStats extends Stats
  32. {
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element;
  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 for filter. This value must be forced during the new to external user company if user is an external user.
  47. * @param string $mode Option ('customer', 'supplier')
  48. * @param int $userid Id user for filter (creation user)
  49. */
  50. public function __construct($db, $socid, $mode, $userid = 0)
  51. {
  52. global $user, $conf;
  53. $this->db = $db;
  54. $this->socid = ($socid > 0 ? $socid : 0);
  55. $this->userid = $userid;
  56. $this->cachefilesuffix = $mode;
  57. if ($mode == 'customer') {
  58. $object = new Fichinter($this->db);
  59. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  60. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  61. $this->field = '0';
  62. $this->field_line = '0';
  63. //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
  64. }
  65. if (!$user->rights->societe->client->voir && !$this->socid) {
  66. $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id;
  67. }
  68. $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('fichinter').')';
  69. if ($this->socid) {
  70. $this->where .= " AND c.fk_soc = ".$this->socid;
  71. }
  72. if ($this->userid > 0) {
  73. $this->where .= ' AND c.fk_user_author = '.$this->userid;
  74. }
  75. }
  76. /**
  77. * Return intervention number by month for a year
  78. *
  79. * @param int $year Year to scan
  80. * @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
  81. * @return array Array with number by month
  82. */
  83. public function getNbByMonth($year, $format = 0)
  84. {
  85. global $user;
  86. $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
  87. $sql .= " FROM ".$this->from;
  88. if (!$user->rights->societe->client->voir && !$this->socid) {
  89. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  90. }
  91. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  92. $sql .= " AND ".$this->where;
  93. $sql .= " GROUP BY dm";
  94. $sql .= $this->db->order('dm', 'DESC');
  95. $res = $this->_getNbByMonth($year, $sql, $format);
  96. return $res;
  97. }
  98. /**
  99. * Return interventions number per year
  100. *
  101. * @return array Array with number by year
  102. *
  103. */
  104. public function getNbByYear()
  105. {
  106. global $user;
  107. $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
  108. $sql .= " FROM ".$this->from;
  109. if (!$user->rights->societe->client->voir && !$this->socid) {
  110. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  111. }
  112. $sql .= " WHERE ".$this->where;
  113. $sql .= " GROUP BY dm";
  114. $sql .= $this->db->order('dm', 'DESC');
  115. return $this->_getNbByYear($sql);
  116. }
  117. /**
  118. * Return the intervention amount by month for a year
  119. *
  120. * @param int $year Year to scan
  121. * @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
  122. * @return array Array with amount by month
  123. */
  124. public function getAmountByMonth($year, $format = 0)
  125. {
  126. global $user;
  127. $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
  128. $sql .= " FROM ".$this->from;
  129. if (!$user->rights->societe->client->voir && !$this->socid) {
  130. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  131. }
  132. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  133. $sql .= " AND ".$this->where;
  134. $sql .= " GROUP BY dm";
  135. $sql .= $this->db->order('dm', 'DESC');
  136. $res = $this->_getAmountByMonth($year, $sql, $format);
  137. return $res;
  138. }
  139. /**
  140. * Return the intervention amount average by month for a year
  141. *
  142. * @param int $year year for stats
  143. * @return array array with number by month
  144. */
  145. public function getAverageByMonth($year)
  146. {
  147. global $user;
  148. $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
  149. $sql .= " FROM ".$this->from;
  150. if (!$user->rights->societe->client->voir && !$this->socid) {
  151. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  152. }
  153. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  154. $sql .= " AND ".$this->where;
  155. $sql .= " GROUP BY dm";
  156. $sql .= $this->db->order('dm', 'DESC');
  157. return $this->_getAverageByMonth($year, $sql);
  158. }
  159. /**
  160. * Return nb, total and average
  161. *
  162. * @return array Array of values
  163. */
  164. public function getAllByYear()
  165. {
  166. global $user;
  167. $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
  168. $sql .= " FROM ".$this->from;
  169. if (!$user->rights->societe->client->voir && !$this->socid) {
  170. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  171. }
  172. $sql .= " WHERE ".$this->where;
  173. $sql .= " GROUP BY year";
  174. $sql .= $this->db->order('year', 'DESC');
  175. return $this->_getAllByYear($sql);
  176. }
  177. /**
  178. * Return nb, amount of predefined product for year
  179. *
  180. * @param int $year Year to scan
  181. * @param int $limit Limit
  182. * @return array Array of values
  183. */
  184. public function getAllByProduct($year, $limit = 0)
  185. {
  186. global $user;
  187. $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
  188. $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
  189. //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  190. $sql .= " WHERE ".$this->where;
  191. $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
  192. $sql .= " AND c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
  193. $sql .= " GROUP BY product.ref";
  194. $sql .= $this->db->order('nb', 'DESC');
  195. //$sql.= $this->db->plimit(20);
  196. return $this->_getAllByProduct($sql, $limit);
  197. }
  198. }