expeditionstats.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. *
  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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/expedition/class/expeditionstats.class.php
  22. * \ingroup expedition
  23. * \brief File of class fo tmanage shipment statistics
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. /**
  29. * Class to manage shipment statistics
  30. */
  31. class ExpeditionStats extends Stats
  32. {
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element;
  37. public $socid;
  38. public $userid;
  39. public $from;
  40. public $field;
  41. public $where;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $db Database handler
  46. * @param int $socid Id third party for filter
  47. * @param string $mode Option (not used)
  48. * @param int $userid Id user for filter (creation user)
  49. */
  50. public function __construct($db, $socid, $mode, $userid = 0)
  51. {
  52. global $user, $conf;
  53. $this->db = $db;
  54. $this->socid = ($socid > 0 ? $socid : 0);
  55. $this->userid = $userid;
  56. $this->cachefilesuffix = $mode;
  57. $object = new Expedition($this->db);
  58. $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
  59. //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
  60. $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
  61. $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
  62. //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
  63. $this->where .= " AND c.entity = ".$conf->entity;
  64. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  65. $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  66. }
  67. if ($this->socid) {
  68. $this->where .= " AND c.fk_soc = ".((int) $this->socid);
  69. }
  70. if ($this->userid > 0) {
  71. $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
  72. }
  73. }
  74. /**
  75. * Return shipment number by month for a year
  76. *
  77. * @param int $year Year to scan
  78. * @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
  79. * @return array Array with number by month
  80. */
  81. public function getNbByMonth($year, $format = 0)
  82. {
  83. global $user;
  84. $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
  85. $sql .= " FROM ".$this->from;
  86. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  87. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  88. }
  89. $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  90. $sql .= " AND ".$this->where;
  91. $sql .= " GROUP BY dm";
  92. $sql .= $this->db->order('dm', 'DESC');
  93. $res = $this->_getNbByMonth($year, $sql, $format);
  94. return $res;
  95. }
  96. /**
  97. * Return shipments number per year
  98. *
  99. * @return array Array with number by year
  100. *
  101. */
  102. public function getNbByYear()
  103. {
  104. global $user;
  105. $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
  106. $sql .= " FROM ".$this->from;
  107. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  108. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  109. }
  110. $sql .= " WHERE ".$this->where;
  111. $sql .= " GROUP BY dm";
  112. $sql .= $this->db->order('dm', 'DESC');
  113. return $this->_getNbByYear($sql);
  114. }
  115. /**
  116. * Return the orders amount by month for a year
  117. *
  118. * @param int $year Year to scan
  119. * @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
  120. * @return array Array with amount by month
  121. */
  122. public function getAmountByMonth($year, $format = 0)
  123. {
  124. global $user;
  125. $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
  126. $sql .= " FROM ".$this->from;
  127. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  128. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  129. }
  130. $sql .= $this->join;
  131. $sql .= " WHERE ".$this->where;
  132. $sql .= " GROUP BY dm";
  133. $sql .= $this->db->order('dm', 'DESC');
  134. $res = $this->_getAmountByMonth($year, $sql, $format);
  135. return $res;
  136. }
  137. /**
  138. * Return the orders amount average by month for a year
  139. *
  140. * @param int $year year for stats
  141. * @return array array with number by month
  142. */
  143. public function getAverageByMonth($year)
  144. {
  145. global $user;
  146. $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
  147. $sql .= " FROM ".$this->from;
  148. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  149. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  150. }
  151. $sql .= $this->join;
  152. $sql .= " WHERE ".$this->where;
  153. $sql .= " GROUP BY dm";
  154. $sql .= $this->db->order('dm', 'DESC');
  155. return $this->_getAverageByMonth($year, $sql);
  156. }
  157. /**
  158. * Return nb, total and average
  159. *
  160. * @return array Array of values
  161. */
  162. public function getAllByYear()
  163. {
  164. global $user;
  165. $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
  166. $sql .= " FROM ".$this->from;
  167. if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) {
  168. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  169. }
  170. $sql .= " WHERE ".$this->where;
  171. $sql .= " GROUP BY year";
  172. $sql .= $this->db->order('year', 'DESC');
  173. return $this->_getAllByYear($sql);
  174. }
  175. }