expensereportstats.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/expensereport/class/expensereportstats.class.php
  21. * \ingroup expensereport
  22. * \brief Fichier de la classe de gestion des stats des expensereport et notes de frais
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  26. /**
  27. * Classe permettant la gestion des stats des expensereports et notes de frais
  28. */
  29. class ExpenseReportStats 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. private $datetouse = 'date_valid';
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. * @param int $socid Id third party
  46. * @param int $userid Id user for filter
  47. * @return void
  48. */
  49. public function __construct($db, $socid = 0, $userid = 0)
  50. {
  51. global $conf, $user;
  52. $this->db = $db;
  53. $this->socid = $socid;
  54. $this->userid = $userid;
  55. $object = new ExpenseReport($this->db);
  56. $this->from = MAIN_DB_PREFIX.$object->table_element." as e";
  57. $this->field = 'total_ht';
  58. //$this->where = " e.fk_statut > 0";
  59. //$this->where.= " AND e.date_valid > '2000-01-01'"; // To filter only correct "valid date". If date is invalid, the group by on it will fails. Launch a repair.php if you have.
  60. $this->where .= ' e.entity IN ('.getEntity('expensereport').')';
  61. //$this->where.= " AND entity = ".$conf->entity;
  62. if ($this->socid) {
  63. $this->where .= " AND e.fk_soc = ".((int) $this->socid);
  64. }
  65. // Only me and subordinates
  66. if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)) {
  67. $childids = $user->getAllChildIds();
  68. $childids[] = $user->id;
  69. $this->where .= " AND e.fk_user_author IN (".$this->db->sanitize(join(',', $childids)).")";
  70. }
  71. if ($this->userid > 0) {
  72. $this->where .= ' AND e.fk_user_author = '.((int) $this->userid);
  73. }
  74. }
  75. /**
  76. * Return nb of expense report per year
  77. *
  78. * @return array Array of values
  79. */
  80. public function getNbByYear()
  81. {
  82. $sql = "SELECT YEAR(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).") as dm, count(*)";
  83. $sql .= " FROM ".$this->from;
  84. $sql .= " GROUP BY dm DESC";
  85. $sql .= " WHERE ".$this->where;
  86. return $this->_getNbByYear($sql);
  87. }
  88. /**
  89. * Renvoie le nombre de facture par mois pour une annee donnee
  90. *
  91. * @param string $year Year to scan
  92. * @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
  93. * @return array Array of values
  94. */
  95. public function getNbByMonth($year, $format = 0)
  96. {
  97. $sql = "SELECT MONTH(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).") as dm, count(*)";
  98. $sql .= " FROM ".$this->from;
  99. $sql .= " WHERE YEAR(e.".$this->datetouse.") = ".((int) $year);
  100. $sql .= " AND ".$this->where;
  101. $sql .= " GROUP BY dm";
  102. $sql .= $this->db->order('dm', 'DESC');
  103. $res = $this->_getNbByMonth($year, $sql, $format);
  104. //var_dump($res);print '<br>';
  105. return $res;
  106. }
  107. /**
  108. * Renvoie le montant de facture par mois pour une annee donnee
  109. *
  110. * @param int $year Year to scan
  111. * @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
  112. * @return array Array of values
  113. */
  114. public function getAmountByMonth($year, $format = 0)
  115. {
  116. $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%m') as dm, sum(".$this->field.")";
  117. $sql .= " FROM ".$this->from;
  118. $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') = '".$this->db->escape($year)."'";
  119. $sql .= " AND ".$this->where;
  120. $sql .= " GROUP BY dm";
  121. $sql .= $this->db->order('dm', 'DESC');
  122. $res = $this->_getAmountByMonth($year, $sql, $format);
  123. //var_dump($res);print '<br>';
  124. return $res;
  125. }
  126. /**
  127. * Return average amount
  128. *
  129. * @param int $year Year to scan
  130. * @return array Array of values
  131. */
  132. public function getAverageByMonth($year)
  133. {
  134. $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%m') as dm, avg(".$this->field.")";
  135. $sql .= " FROM ".$this->from;
  136. $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') = '".$this->db->escape($year)."'";
  137. $sql .= " AND ".$this->where;
  138. $sql .= " GROUP BY dm";
  139. $sql .= $this->db->order('dm', 'DESC');
  140. return $this->_getAverageByMonth($year, $sql);
  141. }
  142. /**
  143. * Return nb, total and average
  144. *
  145. * @return array Array of values
  146. */
  147. public function getAllByYear()
  148. {
  149. $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  150. $sql .= " FROM ".$this->from;
  151. $sql .= " WHERE ".$this->where;
  152. $sql .= " GROUP BY year";
  153. $sql .= $this->db->order('year', 'DESC');
  154. return $this->_getAllByYear($sql);
  155. }
  156. }