facturestats.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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) 2020 Maxime DEMAREST <maxime@indelog.fr>
  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/compta/facture/class/facturestats.class.php
  22. * \ingroup factures
  23. * \brief Fichier de la classe de gestion des stats des factures
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. /**
  30. * Class to manage stats for invoices (customer and supplier)
  31. */
  32. class FactureStats extends Stats
  33. {
  34. public $socid;
  35. public $userid;
  36. /**
  37. * @var string Name of table without prefix where object is stored
  38. */
  39. public $table_element;
  40. public $from;
  41. public $field;
  42. public $where;
  43. public $join;
  44. /**
  45. * Constructor
  46. *
  47. * @param DoliDB $db Database handler
  48. * @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.
  49. * @param string $mode Option ('customer', 'supplier')
  50. * @param int $userid Id user for filter (creation user)
  51. * @param int $typentid Id typent of thirdpary for filter
  52. * @param int $categid Id category of thirdpary for filter
  53. */
  54. public function __construct(DoliDB $db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
  55. {
  56. global $user, $conf;
  57. $this->db = $db;
  58. $this->socid = ($socid > 0 ? $socid : 0);
  59. $this->userid = $userid;
  60. $this->cachefilesuffix = $mode;
  61. $this->join = '';
  62. if ($mode == 'customer') {
  63. $object = new Facture($this->db);
  64. $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
  65. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  66. $this->field = 'total_ht';
  67. $this->field_line = 'total_ht';
  68. }
  69. if ($mode == 'supplier') {
  70. $object = new FactureFournisseur($this->db);
  71. $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
  72. $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
  73. $this->field = 'total_ht';
  74. $this->field_line = 'total_ht';
  75. }
  76. $this->where = " f.fk_statut >= 0";
  77. $this->where .= " AND f.entity IN (".getEntity('invoice').")";
  78. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  79. $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  80. }
  81. if ($mode == 'customer') {
  82. $this->where .= " AND (f.fk_statut <> 3 OR f.close_code <> 'replaced')"; // Exclude replaced invoices as they are duplicated (we count closed invoices for other reasons)
  83. }
  84. if ($this->socid) {
  85. $this->where .= " AND f.fk_soc = ".((int) $this->socid);
  86. }
  87. if ($this->userid > 0) {
  88. $this->where .= ' AND f.fk_user_author = '.((int) $this->userid);
  89. }
  90. if ($mode == 'customer') {
  91. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  92. $this->where .= " AND f.type IN (0,1,2,5)";
  93. } else {
  94. $this->where .= " AND f.type IN (0,1,2,3,5)";
  95. }
  96. }
  97. if ($mode == 'supplier') {
  98. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  99. $this->where .= " AND f.type IN (0,1,2,5)";
  100. } else {
  101. $this->where .= " AND f.type IN (0,1,2,3,5)";
  102. }
  103. }
  104. if ($typentid) {
  105. $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
  106. $this->where .= ' AND s.fk_typent = '.((int) $typentid);
  107. }
  108. if ($categid) {
  109. $this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = f.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
  110. }
  111. }
  112. /**
  113. * Return orders number by month for a year
  114. *
  115. * @param int $year Year to scan
  116. * @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
  117. * @return array Array of values
  118. */
  119. public function getNbByMonth($year, $format = 0)
  120. {
  121. global $user;
  122. $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
  123. $sql .= " FROM ".$this->from;
  124. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  125. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  126. }
  127. $sql .= $this->join;
  128. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  129. $sql .= " AND ".$this->where;
  130. $sql .= " GROUP BY dm";
  131. $sql .= $this->db->order('dm', 'DESC');
  132. $res = $this->_getNbByMonth($year, $sql, $format);
  133. //var_dump($res);print '<br>';
  134. return $res;
  135. }
  136. /**
  137. * Return invoices number per year
  138. *
  139. * @return array Array with number by year
  140. */
  141. public function getNbByYear()
  142. {
  143. global $user;
  144. $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
  145. $sql .= " FROM ".$this->from;
  146. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  147. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  148. }
  149. $sql .= $this->join;
  150. $sql .= " WHERE ".$this->where;
  151. $sql .= " GROUP BY dm";
  152. $sql .= $this->db->order('dm', 'DESC');
  153. return $this->_getNbByYear($sql);
  154. }
  155. /**
  156. * Return the invoices amount by month for a year
  157. *
  158. * @param int $year Year to scan
  159. * @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
  160. * @return array Array with amount by month
  161. */
  162. public function getAmountByMonth($year, $format = 0)
  163. {
  164. global $user;
  165. $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
  166. $sql .= " FROM ".$this->from;
  167. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  168. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  169. }
  170. $sql .= $this->join;
  171. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  172. $sql .= " AND ".$this->where;
  173. $sql .= " GROUP BY dm";
  174. $sql .= $this->db->order('dm', 'DESC');
  175. $res = $this->_getAmountByMonth($year, $sql, $format);
  176. //var_dump($res);print '<br>';
  177. return $res;
  178. }
  179. /**
  180. * Return average amount
  181. *
  182. * @param int $year Year to scan
  183. * @return array Array of values
  184. */
  185. public function getAverageByMonth($year)
  186. {
  187. global $user;
  188. $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
  189. $sql .= " FROM ".$this->from;
  190. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  191. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  192. }
  193. $sql .= $this->join;
  194. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  195. $sql .= " AND ".$this->where;
  196. $sql .= " GROUP BY dm";
  197. $sql .= $this->db->order('dm', 'DESC');
  198. return $this->_getAverageByMonth($year, $sql);
  199. }
  200. /**
  201. * Return nb, total and average
  202. *
  203. * @return array Array of values
  204. */
  205. public function getAllByYear()
  206. {
  207. global $user;
  208. $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
  209. $sql .= " FROM ".$this->from;
  210. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  211. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  212. }
  213. $sql .= $this->join;
  214. $sql .= " WHERE ".$this->where;
  215. $sql .= " GROUP BY year";
  216. $sql .= $this->db->order('year', 'DESC');
  217. return $this->_getAllByYear($sql);
  218. }
  219. /**
  220. * Return nb, amount of predefined product for year
  221. *
  222. * @param int $year Year to scan
  223. * @param int $limit Limit
  224. * @return array Array of values
  225. */
  226. public function getAllByProduct($year, $limit = 10)
  227. {
  228. global $user;
  229. $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
  230. $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
  231. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  232. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  233. }
  234. $sql .= $this->join;
  235. $sql .= " WHERE ".$this->where;
  236. $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
  237. $sql .= " AND f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
  238. $sql .= " GROUP BY product.ref";
  239. $sql .= $this->db->order('nb', 'DESC');
  240. //$sql.= $this->db->plimit(20);
  241. return $this->_getAllByProduct($sql, $limit);
  242. }
  243. /**
  244. * Return the invoices amount by year for a number of past years
  245. *
  246. * @param int $numberYears Years to scan
  247. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is year, 2=Label of abscissa is last number of year
  248. * @return array Array with amount by year
  249. */
  250. public function getAmountByYear($numberYears, $format = 0)
  251. {
  252. global $user;
  253. $endYear = date('Y');
  254. $startYear = $endYear - $numberYears;
  255. $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
  256. $sql .= " FROM ".$this->from;
  257. if (empty($user->rights->societe->client->voir) && !$this->socid) {
  258. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  259. }
  260. $sql .= $this->join;
  261. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
  262. $sql .= " AND ".$this->where;
  263. $sql .= " GROUP BY dm";
  264. $sql .= $this->db->order('dm', 'ASC');
  265. $res = $this->_getAmountByYear($sql);
  266. return $res;
  267. }
  268. }