commandestats.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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@capnetworks.com>
  5. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/commande/class/commandestats.class.php
  22. * \ingroup commandes
  23. * \brief File of class to manage order statistics
  24. */
  25. include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
  27. include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.commande.class.php';
  28. include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  29. /**
  30. * Class to manage order statistics (customer and supplier)
  31. */
  32. class CommandeStats extends Stats
  33. {
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element;
  38. var $socid;
  39. var $userid;
  40. var $from;
  41. var $field;
  42. var $where;
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. * @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.
  48. * @param string $mode Option ('customer', 'supplier')
  49. * @param int $userid Id user for filter (creation user)
  50. */
  51. function __construct($db, $socid, $mode, $userid=0)
  52. {
  53. global $user, $conf;
  54. $this->db = $db;
  55. $this->socid = ($socid > 0 ? $socid : 0);
  56. $this->userid = $userid;
  57. $this->cachefilesuffix = $mode;
  58. if ($mode == 'customer')
  59. {
  60. $object=new Commande($this->db);
  61. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  62. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  63. $this->field='total_ht';
  64. $this->field_line='total_ht';
  65. $this->where.= " c.fk_statut > 0"; // Not draft and not cancelled
  66. }
  67. if ($mode == 'supplier')
  68. {
  69. $object=new CommandeFournisseur($this->db);
  70. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  71. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  72. $this->field='total_ht';
  73. $this->field_line='total_ht';
  74. $this->where.= " c.fk_statut > 2"; // Only approved & ordered
  75. }
  76. //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
  77. $this->where.= ' AND c.entity IN ('.getEntity('commande').')';
  78. if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
  79. if ($this->socid)
  80. {
  81. $this->where.=" AND c.fk_soc = ".$this->socid;
  82. }
  83. if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
  84. }
  85. /**
  86. * Return orders number by month for a year
  87. *
  88. * @param int $year Year to scan
  89. * @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
  90. * @return array Array with number by month
  91. */
  92. function getNbByMonth($year, $format=0)
  93. {
  94. global $user;
  95. $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
  96. $sql.= " FROM ".$this->from;
  97. if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  98. $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  99. $sql.= " AND ".$this->where;
  100. $sql.= " GROUP BY dm";
  101. $sql.= $this->db->order('dm','DESC');
  102. $res=$this->_getNbByMonth($year, $sql, $format);
  103. return $res;
  104. }
  105. /**
  106. * Return orders number per year
  107. *
  108. * @return array Array with number by year
  109. *
  110. */
  111. function getNbByYear()
  112. {
  113. global $user;
  114. $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  115. $sql.= " FROM ".$this->from;
  116. if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  117. $sql.= " WHERE ".$this->where;
  118. $sql.= " GROUP BY dm";
  119. $sql.= $this->db->order('dm','DESC');
  120. return $this->_getNbByYear($sql);
  121. }
  122. /**
  123. * Return the orders amount by month for a year
  124. *
  125. * @param int $year Year to scan
  126. * @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
  127. * @return array Array with amount by month
  128. */
  129. function getAmountByMonth($year, $format=0)
  130. {
  131. global $user;
  132. $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(c.".$this->field.")";
  133. $sql.= " FROM ".$this->from;
  134. if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  135. $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  136. $sql.= " AND ".$this->where;
  137. $sql.= " GROUP BY dm";
  138. $sql.= $this->db->order('dm','DESC');
  139. $res=$this->_getAmountByMonth($year, $sql, $format);
  140. return $res;
  141. }
  142. /**
  143. * Return the orders amount average by month for a year
  144. *
  145. * @param int $year year for stats
  146. * @return array array with number by month
  147. */
  148. function getAverageByMonth($year)
  149. {
  150. global $user;
  151. $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$this->field.")";
  152. $sql.= " FROM ".$this->from;
  153. if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  154. $sql.= " WHERE c.date_commande 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. return $this->_getAverageByMonth($year, $sql);
  159. }
  160. /**
  161. * Return nb, total and average
  162. *
  163. * @return array Array of values
  164. */
  165. function getAllByYear()
  166. {
  167. global $user;
  168. $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
  169. $sql.= " FROM ".$this->from;
  170. if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  171. $sql.= " WHERE ".$this->where;
  172. $sql.= " GROUP BY year";
  173. $sql.= $this->db->order('year','DESC');
  174. return $this->_getAllByYear($sql);
  175. }
  176. /**
  177. * Return nb, amount of predefined product for year
  178. *
  179. * @param int $year Year to scan
  180. * @return array Array of values
  181. */
  182. function getAllByProduct($year)
  183. {
  184. global $user;
  185. $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
  186. $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
  187. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  188. $sql.= " WHERE ".$this->where;
  189. $sql.= " AND c.rowid = tl.fk_commande AND tl.fk_product = product.rowid";
  190. $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))."'";
  191. $sql.= " GROUP BY product.ref";
  192. $sql.= $this->db->order('nb','DESC');
  193. //$sql.= $this->db->plimit(20);
  194. return $this->_getAllByProduct($sql);
  195. }
  196. }