propalestats.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (c) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/comm/propal/class/propalestats.class.php
  23. * \ingroup propales
  24. * \brief File of class to manage proposals statistics
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  29. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  30. /**
  31. * Class to manage proposals statistics
  32. */
  33. class PropaleStats extends Stats
  34. {
  35. /**
  36. * @var string Name of table without prefix where object is stored
  37. */
  38. public $table_element;
  39. public $socid;
  40. public $userid;
  41. public $from;
  42. public $field;
  43. public $where;
  44. public $join;
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $db Database handler
  49. * @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.
  50. * @param int $userid Id user for filter (creation user)
  51. * @param string $mode Option ('customer', 'supplier')
  52. * @param int $typentid Id typent of thirdpary for filter
  53. * @param int $categid Id category of thirdpary for filter
  54. */
  55. public function __construct($db, $socid = 0, $userid = 0, $mode = 'customer', $typentid = 0, $categid = 0)
  56. {
  57. $this->db = $db;
  58. $this->socid = ($socid > 0 ? $socid : 0);
  59. $this->userid = $userid;
  60. $this->join = '';
  61. if ($mode == 'customer') {
  62. $object = new Propal($this->db);
  63. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  64. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  65. $this->field_date = 'p.datep';
  66. $this->field = 'total_ht';
  67. $this->field_line = 'total_ht';
  68. //$this->where .= " p.fk_statut > 0";
  69. }
  70. if ($mode == 'supplier') {
  71. $object = new SupplierProposal($this->db);
  72. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  73. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  74. $this->field_date = 'p.date_valid';
  75. $this->field = 'total_ht';
  76. $this->field_line = 'total_ht';
  77. //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
  78. }
  79. //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
  80. $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
  81. if ($this->socid) {
  82. $this->where .= " AND p.fk_soc = ".((int) $this->socid);
  83. }
  84. if ($this->userid > 0) {
  85. $this->where .= ' AND fk_user_author = '.((int) $this->userid);
  86. }
  87. if ($typentid) {
  88. $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
  89. $this->where .= ' AND s.fk_typent = '.((int) $typentid);
  90. }
  91. if ($categid) {
  92. $this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = p.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
  93. }
  94. }
  95. /**
  96. * Return propals number by month for a year
  97. *
  98. * @param int $year Year to scan
  99. * @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
  100. * @return array Array with number by month
  101. */
  102. public function getNbByMonth($year, $format = 0)
  103. {
  104. global $user;
  105. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
  106. $sql .= " FROM ".$this->from;
  107. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  108. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  109. }
  110. $sql .= $this->join;
  111. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  112. $sql .= " AND ".$this->where;
  113. $sql .= " GROUP BY dm";
  114. $sql .= $this->db->order('dm', 'DESC');
  115. $res = $this->_getNbByMonth($year, $sql, $format);
  116. return $res;
  117. }
  118. /**
  119. * Return propals number per year
  120. *
  121. * @return array Array with number by year
  122. *
  123. */
  124. public function getNbByYear()
  125. {
  126. global $user;
  127. $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  128. $sql .= " FROM ".$this->from;
  129. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  130. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  131. }
  132. $sql .= $this->join;
  133. $sql .= " WHERE ".$this->where;
  134. $sql .= " GROUP BY dm";
  135. $sql .= $this->db->order('dm', 'DESC');
  136. return $this->_getNbByYear($sql);
  137. }
  138. /**
  139. * Return the propals amount by month for a year
  140. *
  141. * @param int $year Year to scan
  142. * @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
  143. * @return array Array with amount by month
  144. */
  145. public function getAmountByMonth($year, $format = 0)
  146. {
  147. global $user;
  148. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
  149. $sql .= " FROM ".$this->from;
  150. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  151. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  152. }
  153. $sql .= $this->join;
  154. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  155. $sql .= " AND ".$this->where;
  156. $sql .= " GROUP BY dm";
  157. $sql .= $this->db->order('dm', 'DESC');
  158. $res = $this->_getAmountByMonth($year, $sql, $format);
  159. return $res;
  160. }
  161. /**
  162. * Return the propals amount average by month for a year
  163. *
  164. * @param int $year year for stats
  165. * @return array array with number by month
  166. */
  167. public function getAverageByMonth($year)
  168. {
  169. global $user;
  170. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
  171. $sql .= " FROM ".$this->from;
  172. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  173. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  174. }
  175. $sql .= $this->join;
  176. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  177. $sql .= " AND ".$this->where;
  178. $sql .= " GROUP BY dm";
  179. $sql .= $this->db->order('dm', 'DESC');
  180. return $this->_getAverageByMonth($year, $sql);
  181. }
  182. /**
  183. * Return nb, total and average
  184. *
  185. * @return array Array of values
  186. */
  187. public function getAllByYear()
  188. {
  189. global $user;
  190. $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
  191. $sql .= " FROM ".$this->from;
  192. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  193. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  194. }
  195. $sql .= $this->join;
  196. $sql .= " WHERE ".$this->where;
  197. $sql .= " GROUP BY year";
  198. $sql .= $this->db->order('year', 'DESC');
  199. return $this->_getAllByYear($sql);
  200. }
  201. /**
  202. * Return nb, amount of predefined product for year
  203. *
  204. * @param int $year Year to scan
  205. * @param int $limit Limit
  206. * @return array Array of values
  207. */
  208. public function getAllByProduct($year, $limit = 10)
  209. {
  210. global $user;
  211. $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
  212. $sql .= " FROM ".$this->from;
  213. $sql .= " INNER JOIN ".$this->from_line." ON p.rowid = tl.fk_propal";
  214. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
  215. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  216. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  217. }
  218. $sql .= $this->join;
  219. $sql .= " WHERE ".$this->where;
  220. $sql .= " AND ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
  221. $sql .= " GROUP BY product.ref";
  222. $sql .= $this->db->order('nb', 'DESC');
  223. //$sql.= $this->db->plimit(20);
  224. return $this->_getAllByProduct($sql, $limit);
  225. }
  226. }