commandestats.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.com>
  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/commande/class/commandestats.class.php
  23. * \ingroup commandes
  24. * \brief File of class to manage order statistics
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  29. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  30. /**
  31. * Class to manage order statistics (customer and supplier)
  32. */
  33. class CommandeStats 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 $from_line;
  43. public $field;
  44. public $field_line;
  45. public $categ_link;
  46. public $where;
  47. public $join;
  48. /**
  49. * Constructor
  50. *
  51. * @param DoliDB $db Database handler
  52. * @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.
  53. * @param string $mode Option ('customer', 'supplier')
  54. * @param int $userid Id user for filter (creation user)
  55. * @param int $typentid Id typent of thirdpary for filter
  56. * @param int $categid Id category of thirdpary for filter
  57. */
  58. public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
  59. {
  60. global $user, $conf;
  61. $this->db = $db;
  62. $this->socid = ($socid > 0 ? $socid : 0);
  63. $this->userid = $userid;
  64. $this->cachefilesuffix = $mode;
  65. $this->join = '';
  66. if ($mode == 'customer') {
  67. $object = new Commande($this->db);
  68. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  69. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  70. $this->field = 'total_ht';
  71. $this->field_line = 'total_ht';
  72. //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
  73. $this->categ_link = MAIN_DB_PREFIX.'categorie_societe';
  74. } elseif ($mode == 'supplier') {
  75. $object = new CommandeFournisseur($this->db);
  76. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  77. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  78. $this->field = 'total_ht';
  79. $this->field_line = 'total_ht';
  80. //$this->where .= " c.fk_statut > 2"; // Only approved & ordered
  81. $this->categ_link = MAIN_DB_PREFIX.'categorie_fournisseur';
  82. }
  83. //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
  84. $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('commande').')';
  85. if ($this->socid) {
  86. $this->where .= " AND c.fk_soc = ".((int) $this->socid);
  87. }
  88. if ($this->userid > 0) {
  89. $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
  90. }
  91. if ($typentid) {
  92. $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc';
  93. $this->where .= ' AND s.fk_typent = '.((int) $typentid);
  94. }
  95. if ($categid) {
  96. $this->where .= ' AND EXISTS (SELECT rowid FROM '.$this->categ_link.' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
  97. }
  98. }
  99. /**
  100. * Return orders number by month for a year
  101. *
  102. * @param int $year Year to scan
  103. * @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
  104. * @return array Array with number by month
  105. */
  106. public function getNbByMonth($year, $format = 0)
  107. {
  108. global $user;
  109. $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
  110. $sql .= " FROM ".$this->from;
  111. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  112. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  113. }
  114. $sql .= $this->join;
  115. $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  116. $sql .= " AND ".$this->where;
  117. $sql .= " GROUP BY dm";
  118. $sql .= $this->db->order('dm', 'DESC');
  119. $res = $this->_getNbByMonth($year, $sql, $format);
  120. return $res;
  121. }
  122. /**
  123. * Return orders number per year
  124. *
  125. * @return array Array with number by year
  126. *
  127. */
  128. public function getNbByYear()
  129. {
  130. global $user;
  131. $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  132. $sql .= " FROM ".$this->from;
  133. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  134. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  135. }
  136. $sql .= $this->join;
  137. $sql .= " WHERE ".$this->where;
  138. $sql .= " GROUP BY dm";
  139. $sql .= $this->db->order('dm', 'DESC');
  140. return $this->_getNbByYear($sql);
  141. }
  142. /**
  143. * Return the orders amount by month for a year
  144. *
  145. * @param int $year Year to scan
  146. * @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
  147. * @return array Array with amount by month
  148. */
  149. public function getAmountByMonth($year, $format = 0)
  150. {
  151. global $user;
  152. $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(c.".$this->field.")";
  153. $sql .= " FROM ".$this->from;
  154. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  155. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  156. }
  157. $sql .= $this->join;
  158. $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  159. $sql .= " AND ".$this->where;
  160. $sql .= " GROUP BY dm";
  161. $sql .= $this->db->order('dm', 'DESC');
  162. $res = $this->_getAmountByMonth($year, $sql, $format);
  163. return $res;
  164. }
  165. /**
  166. * Return the orders amount average by month for a year
  167. *
  168. * @param int $year year for stats
  169. * @return array array with number by month
  170. */
  171. public function getAverageByMonth($year)
  172. {
  173. global $user;
  174. $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$this->field.")";
  175. $sql .= " FROM ".$this->from;
  176. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  177. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  178. }
  179. $sql .= $this->join;
  180. $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  181. $sql .= " AND ".$this->where;
  182. $sql .= " GROUP BY dm";
  183. $sql .= $this->db->order('dm', 'DESC');
  184. return $this->_getAverageByMonth($year, $sql);
  185. }
  186. /**
  187. * Return nb, total and average
  188. *
  189. * @return array Array of values
  190. */
  191. public function getAllByYear()
  192. {
  193. global $user;
  194. $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
  195. $sql .= " FROM ".$this->from;
  196. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  197. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  198. }
  199. $sql .= $this->join;
  200. $sql .= " WHERE ".$this->where;
  201. $sql .= " GROUP BY year";
  202. $sql .= $this->db->order('year', 'DESC');
  203. return $this->_getAllByYear($sql);
  204. }
  205. /**
  206. * Return nb, amount of predefined product for year
  207. *
  208. * @param int $year Year to scan
  209. * @param int $limit Limit
  210. * @return array Array of values
  211. */
  212. public function getAllByProduct($year, $limit = 10)
  213. {
  214. global $user;
  215. $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
  216. $sql .= " FROM ".$this->from;
  217. $sql .= " INNER JOIN ".$this->from_line." ON c.rowid = tl.fk_commande";
  218. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
  219. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  220. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  221. }
  222. $sql .= $this->join;
  223. $sql .= " WHERE ".$this->where;
  224. $sql .= " AND c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
  225. $sql .= " GROUP BY product.ref";
  226. $sql .= $this->db->order('nb', 'DESC');
  227. //$sql.= $this->db->plimit(20);
  228. return $this->_getAllByProduct($sql, $limit);
  229. }
  230. }