deplacementstats.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2005-2008 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/compta/deplacement/class/deplacementstats.class.php
  21. * \ingroup factures
  22. * \brief Fichier de la classe de gestion des stats des deplacement et notes de frais
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  25. include_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacement.class.php';
  26. /**
  27. * Classe permettant la gestion des stats des deplacements et notes de frais
  28. */
  29. class DeplacementStats extends Stats
  30. {
  31. /**
  32. * @var string Name of table without prefix where object is stored
  33. */
  34. public $table_element;
  35. public $socid;
  36. public $userid;
  37. public $from;
  38. public $field;
  39. public $where;
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDB $db Database handler
  44. * @param int $socid Id third party
  45. * @param mixed $userid Id user for filter or array of user ids
  46. * @return void
  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 Deplacement($this->db);
  55. $this->from = MAIN_DB_PREFIX.$object->table_element;
  56. $this->field = 'km';
  57. $this->where = " fk_statut > 0";
  58. $this->where .= " AND entity = ".$conf->entity;
  59. if ($this->socid > 0) {
  60. $this->where .= " AND fk_soc = ".((int) $this->socid);
  61. }
  62. if (is_array($this->userid) && count($this->userid) > 0) {
  63. $this->where .= ' AND fk_user IN ('.$this->db->sanitize(join(',', $this->userid)).')';
  64. } elseif ($this->userid > 0) {
  65. $this->where .= ' AND fk_user = '.((int) $this->userid);
  66. }
  67. }
  68. /**
  69. * Renvoie le nombre de facture par annee
  70. *
  71. * @return array Array of values
  72. */
  73. public function getNbByYear()
  74. {
  75. $sql = "SELECT YEAR(dated) as dm, count(*)";
  76. $sql .= " FROM ".$this->from;
  77. $sql .= " GROUP BY dm DESC";
  78. $sql .= " WHERE ".$this->where;
  79. return $this->_getNbByYear($sql);
  80. }
  81. /**
  82. * Renvoie le nombre de facture par mois pour une annee donnee
  83. *
  84. * @param string $year Year to scan
  85. * @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
  86. * @return array Array of values
  87. */
  88. public function getNbByMonth($year, $format = 0)
  89. {
  90. $sql = "SELECT MONTH(dated) as dm, count(*)";
  91. $sql .= " FROM ".$this->from;
  92. $sql .= " WHERE YEAR(dated) = ".((int) $year);
  93. $sql .= " AND ".$this->where;
  94. $sql .= " GROUP BY dm";
  95. $sql .= $this->db->order('dm', 'DESC');
  96. $res = $this->_getNbByMonth($year, $sql, $format);
  97. //var_dump($res);print '<br>';
  98. return $res;
  99. }
  100. /**
  101. * Renvoie le montant de facture par mois pour une annee donnee
  102. *
  103. * @param int $year Year to scan
  104. * @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
  105. * @return array Array of values
  106. */
  107. public function getAmountByMonth($year, $format = 0)
  108. {
  109. $sql = "SELECT date_format(dated,'%m') as dm, sum(".$this->field.")";
  110. $sql .= " FROM ".$this->from;
  111. $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
  112. $sql .= " AND ".$this->where;
  113. $sql .= " GROUP BY dm";
  114. $sql .= $this->db->order('dm', 'DESC');
  115. $res = $this->_getAmountByMonth($year, $sql, $format);
  116. //var_dump($res);print '<br>';
  117. return $res;
  118. }
  119. /**
  120. * Return average amount
  121. *
  122. * @param int $year Year to scan
  123. * @return array Array of values
  124. */
  125. public function getAverageByMonth($year)
  126. {
  127. $sql = "SELECT date_format(dated,'%m') as dm, avg(".$this->field.")";
  128. $sql .= " FROM ".$this->from;
  129. $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
  130. $sql .= " AND ".$this->where;
  131. $sql .= " GROUP BY dm";
  132. $sql .= $this->db->order('dm', 'DESC');
  133. return $this->_getAverageByMonth($year, $sql);
  134. }
  135. /**
  136. * Return nb, total and average
  137. *
  138. * @return array Array of values
  139. */
  140. public function getAllByYear()
  141. {
  142. $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  143. $sql .= " FROM ".$this->from;
  144. $sql .= " WHERE ".$this->where;
  145. $sql .= " GROUP BY year";
  146. $sql .= $this->db->order('year', 'DESC');
  147. return $this->_getAllByYear($sql);
  148. }
  149. }