propalestats.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. global $user, $conf;
  58. $this->db = $db;
  59. $this->socid = ($socid > 0 ? $socid : 0);
  60. $this->userid = $userid;
  61. $this->join = '';
  62. if ($mode == 'customer') {
  63. $object = new Propal($this->db);
  64. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  65. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  66. $this->field_date = 'p.datep';
  67. $this->field = 'total_ht';
  68. $this->field_line = 'total_ht';
  69. //$this->where .= " p.fk_statut > 0";
  70. }
  71. if ($mode == 'supplier') {
  72. $object = new SupplierProposal($this->db);
  73. $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
  74. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  75. $this->field_date = 'p.date_valid';
  76. $this->field = 'total_ht';
  77. $this->field_line = 'total_ht';
  78. //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
  79. }
  80. //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
  81. $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
  82. if ($this->socid) {
  83. $this->where .= " AND p.fk_soc = ".((int) $this->socid);
  84. }
  85. if ($this->userid > 0) {
  86. $this->where .= ' AND fk_user_author = '.((int) $this->userid);
  87. }
  88. if ($typentid) {
  89. $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
  90. $this->where .= ' AND s.fk_typent = '.((int) $typentid);
  91. }
  92. if ($categid) {
  93. $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).')';
  94. }
  95. }
  96. /**
  97. * Return propals number by month for a year
  98. *
  99. * @param int $year Year to scan
  100. * @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
  101. * @return array Array with number by month
  102. */
  103. public function getNbByMonth($year, $format = 0)
  104. {
  105. global $user;
  106. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
  107. $sql .= " FROM ".$this->from;
  108. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  109. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  110. }
  111. $sql .= $this->join;
  112. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  113. $sql .= " AND ".$this->where;
  114. $sql .= " GROUP BY dm";
  115. $sql .= $this->db->order('dm', 'DESC');
  116. $res = $this->_getNbByMonth($year, $sql, $format);
  117. return $res;
  118. }
  119. /**
  120. * Return propals number per year
  121. *
  122. * @return array Array with number by year
  123. *
  124. */
  125. public function getNbByYear()
  126. {
  127. global $user;
  128. $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  129. $sql .= " FROM ".$this->from;
  130. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  131. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  132. }
  133. $sql .= $this->join;
  134. $sql .= " WHERE ".$this->where;
  135. $sql .= " GROUP BY dm";
  136. $sql .= $this->db->order('dm', 'DESC');
  137. return $this->_getNbByYear($sql);
  138. }
  139. /**
  140. * Return the propals amount by month for a year
  141. *
  142. * @param int $year Year to scan
  143. * @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
  144. * @return array Array with amount by month
  145. */
  146. public function getAmountByMonth($year, $format = 0)
  147. {
  148. global $user;
  149. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
  150. $sql .= " FROM ".$this->from;
  151. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  152. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  153. }
  154. $sql .= $this->join;
  155. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  156. $sql .= " AND ".$this->where;
  157. $sql .= " GROUP BY dm";
  158. $sql .= $this->db->order('dm', 'DESC');
  159. $res = $this->_getAmountByMonth($year, $sql, $format);
  160. return $res;
  161. }
  162. /**
  163. * Return the propals amount average by month for a year
  164. *
  165. * @param int $year year for stats
  166. * @return array array with number by month
  167. */
  168. public function getAverageByMonth($year)
  169. {
  170. global $user;
  171. $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
  172. $sql .= " FROM ".$this->from;
  173. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  174. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  175. }
  176. $sql .= $this->join;
  177. $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  178. $sql .= " AND ".$this->where;
  179. $sql .= " GROUP BY dm";
  180. $sql .= $this->db->order('dm', 'DESC');
  181. return $this->_getAverageByMonth($year, $sql);
  182. }
  183. /**
  184. * Return nb, total and average
  185. *
  186. * @return array Array of values
  187. */
  188. public function getAllByYear()
  189. {
  190. global $user;
  191. $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
  192. $sql .= " FROM ".$this->from;
  193. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  194. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  195. }
  196. $sql .= $this->join;
  197. $sql .= " WHERE ".$this->where;
  198. $sql .= " GROUP BY year";
  199. $sql .= $this->db->order('year', 'DESC');
  200. return $this->_getAllByYear($sql);
  201. }
  202. /**
  203. * Return nb, amount of predefined product for year
  204. *
  205. * @param int $year Year to scan
  206. * @param int $limit Limit
  207. * @return array Array of values
  208. */
  209. public function getAllByProduct($year, $limit = 10)
  210. {
  211. global $user;
  212. $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
  213. $sql .= " FROM ".$this->from;
  214. $sql .= " INNER JOIN ".$this->from_line." ON p.rowid = tl.fk_propal";
  215. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
  216. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  217. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  218. }
  219. $sql .= $this->join;
  220. $sql .= " WHERE ".$this->where;
  221. $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))."'";
  222. $sql .= " GROUP BY product.ref";
  223. $sql .= $this->db->order('nb', 'DESC');
  224. //$sql.= $this->db->plimit(20);
  225. return $this->_getAllByProduct($sql, $limit);
  226. }
  227. }