salariesstats.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (c) 2018 Fidesio <contact@fidesio.com>
  4. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  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/salaries/class/salariesstats.class.php
  21. * \ingroup salaries
  22. * \brief File of class for statistics on salaries
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  25. include_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  26. /**
  27. * Classe permettant la gestion des stats des salaires
  28. */
  29. class SalariesStats 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 Salary($this->db);
  55. $this->from = MAIN_DB_PREFIX.$object->table_element;
  56. $this->field = 'amount';
  57. $this->where = " entity = ".$conf->entity;
  58. if ($this->socid > 0) {
  59. $this->where .= " AND fk_soc = ".((int) $this->socid);
  60. }
  61. if (is_array($this->userid) && count($this->userid) > 0) {
  62. $this->where .= ' AND fk_user IN ('.$this->db->sanitize(join(',', $this->userid)).')';
  63. } elseif ($this->userid > 0) {
  64. $this->where .= " AND fk_user = ".((int) $this->userid);
  65. }
  66. }
  67. /**
  68. * Return the number of salary by year
  69. *
  70. * @return array Array of values
  71. */
  72. public function getNbByYear()
  73. {
  74. $sql = "SELECT YEAR(dateep) as dm, count(*)";
  75. $sql .= " FROM ".$this->from;
  76. $sql .= " WHERE ".$this->where;
  77. $sql .= " GROUP BY dm DESC";
  78. return $this->_getNbByYear($sql);
  79. }
  80. /**
  81. * Return the number of salary by month, for a given year
  82. *
  83. * @param string $year Year to scan
  84. * @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
  85. * @return array Array of values
  86. */
  87. public function getNbByMonth($year, $format = 0)
  88. {
  89. $sql = "SELECT MONTH(dateep) as dm, count(*)";
  90. $sql .= " FROM ".$this->from;
  91. $sql .= " WHERE YEAR(dateep) = ".((int) $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 amount of salaries by month for a given year
  100. *
  101. * @param int $year Year to scan
  102. * @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
  103. * @return array Array of values
  104. */
  105. public function getAmountByMonth($year, $format = 0)
  106. {
  107. $sql = "SELECT date_format(dateep,'%m') as dm, sum(".$this->field.")";
  108. $sql .= " FROM ".$this->from;
  109. $sql .= " WHERE date_format(dateep,'%Y') = '".$this->db->escape($year)."'";
  110. $sql .= " AND ".$this->where;
  111. $sql .= " GROUP BY dm";
  112. $sql .= $this->db->order('dm', 'DESC');
  113. $res = $this->_getAmountByMonth($year, $sql, $format);
  114. return $res;
  115. }
  116. /**
  117. * Return average amount
  118. *
  119. * @param int $year Year to scan
  120. * @return array Array of values
  121. */
  122. public function getAverageByMonth($year)
  123. {
  124. $sql = "SELECT date_format(dateep,'%m') as dm, avg(".$this->field.")";
  125. $sql .= " FROM ".$this->from;
  126. $sql .= " WHERE date_format(dateep,'%Y') = '".$this->db->escape($year)."'";
  127. $sql .= " AND ".$this->where;
  128. $sql .= " GROUP BY dm";
  129. $sql .= $this->db->order('dm', 'DESC');
  130. return $this->_getAverageByMonth($year, $sql);
  131. }
  132. /**
  133. * Return nb, total and average
  134. *
  135. * @return array Array of values
  136. */
  137. public function getAllByYear()
  138. {
  139. $sql = "SELECT date_format(dateep,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  140. $sql .= " FROM ".$this->from;
  141. $sql .= " WHERE ".$this->where;
  142. $sql .= " GROUP BY year";
  143. $sql .= $this->db->order('year', 'DESC');
  144. return $this->_getAllByYear($sql);
  145. }
  146. }