salariesstats.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (c) 2018 Fidesio <contact@fidesio.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/salaries/class/salariesstats.class.php
  20. * \ingroup salaries
  21. * \brief Fichier de la classe de gestion des stats des salaires
  22. */
  23. include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
  24. include_once DOL_DOCUMENT_ROOT . '/compta/salaries/class/paymentsalary.class.php';
  25. /**
  26. * Classe permettant la gestion des stats des salaires
  27. */
  28. class SalariesStats extends Stats
  29. {
  30. /**
  31. * @var string Name of table without prefix where object is stored
  32. */
  33. public $table_element;
  34. var $socid;
  35. var $userid;
  36. var $from;
  37. var $field;
  38. var $where;
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. * @param int $socid Id third party
  44. * @param mixed $userid Id user for filter or array of user ids
  45. * @return void
  46. */
  47. function __construct($db, $socid = 0, $userid = 0)
  48. {
  49. global $conf;
  50. $this->db = $db;
  51. $this->socid = $socid;
  52. $this->userid = $userid;
  53. $object=new PaymentSalary($this->db);
  54. $this->from = MAIN_DB_PREFIX.$object->table_element;
  55. $this->field='amount';
  56. $this->where.= " entity = ".$conf->entity;
  57. if ($this->socid)
  58. {
  59. $this->where.=" AND fk_soc = ".$this->socid;
  60. }
  61. if (is_array($this->userid) && count($this->userid) > 0) $this->where.=' AND fk_user IN ('.join(',',$this->userid).')';
  62. elseif ($this->userid > 0) $this->where.=' AND fk_user = '.$this->userid;
  63. }
  64. /**
  65. * Return the number of salary by year
  66. *
  67. * @return array Array of values
  68. */
  69. function getNbByYear()
  70. {
  71. $sql = "SELECT YEAR(datep) as dm, count(*)";
  72. $sql.= " FROM ".$this->from;
  73. $sql.= " GROUP BY dm DESC";
  74. $sql.= " WHERE ".$this->where;
  75. return $this->_getNbByYear($sql);
  76. }
  77. /**
  78. * Return the number of salary by month, for a given year
  79. *
  80. * @param string $year Year to scan
  81. * @param int $format 0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
  82. * @return array Array of values
  83. */
  84. function getNbByMonth($year, $format = 0)
  85. {
  86. $sql = "SELECT MONTH(datep) as dm, count(*)";
  87. $sql.= " FROM ".$this->from;
  88. $sql.= " WHERE YEAR(datep) = ".$year;
  89. $sql.= " AND ".$this->where;
  90. $sql.= " GROUP BY dm";
  91. $sql.= $this->db->order('dm','DESC');
  92. $res=$this->_getNbByMonth($year, $sql, $format);
  93. //var_dump($res);print '<br>';
  94. return $res;
  95. }
  96. /**
  97. * Return amount of salaries by month for a given year
  98. *
  99. * @param int $year Year to scan
  100. * @param int $format 0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
  101. * @return array Array of values
  102. */
  103. function getAmountByMonth($year, $format = 0)
  104. {
  105. $sql = "SELECT date_format(datep,'%m') as dm, sum(".$this->field.")";
  106. $sql.= " FROM ".$this->from;
  107. $sql.= " WHERE date_format(datep,'%Y') = '".$year."'";
  108. $sql.= " AND ".$this->where;
  109. $sql.= " GROUP BY dm";
  110. $sql.= $this->db->order('dm','DESC');
  111. $res=$this->_getAmountByMonth($year, $sql, $format);
  112. //var_dump($res);print '<br>';
  113. return $res;
  114. }
  115. /**
  116. * Return average amount
  117. *
  118. * @param int $year Year to scan
  119. * @return array Array of values
  120. */
  121. function getAverageByMonth($year)
  122. {
  123. $sql = "SELECT date_format(datep,'%m') as dm, avg(".$this->field.")";
  124. $sql.= " FROM ".$this->from;
  125. $sql.= " WHERE date_format(datep,'%Y') = '".$year."'";
  126. $sql.= " AND ".$this->where;
  127. $sql.= " GROUP BY dm";
  128. $sql.= $this->db->order('dm','DESC');
  129. return $this->_getAverageByMonth($year, $sql);
  130. }
  131. /**
  132. * Return nb, total and average
  133. *
  134. * @return array Array of values
  135. */
  136. function getAllByYear()
  137. {
  138. $sql = "SELECT date_format(datep,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
  139. $sql.= " FROM ".$this->from;
  140. $sql.= " WHERE ".$this->where;
  141. $sql.= " GROUP BY year";
  142. $sql.= $this->db->order('year','DESC');
  143. return $this->_getAllByYear($sql);
  144. }
  145. }