26include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
27include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
28include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
29include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
42 public $table_element;
60 public function __construct(
DoliDB $db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
65 $this->socid = ($socid > 0 ? $socid : 0);
66 $this->userid = $userid;
67 $this->cachefilesuffix = $mode;
70 if ($mode ==
'customer') {
71 $object =
new Facture($this->db);
72 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as f";
73 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as tl";
74 $this->field =
'total_ht';
75 $this->field_line =
'total_ht';
77 if ($mode ==
'supplier') {
79 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as f";
80 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as tl";
81 $this->field =
'total_ht';
82 $this->field_line =
'total_ht';
86 $this->where =
" f.fk_statut >= 0";
87 $this->where .=
" AND f.entity IN (".getEntity(
'invoice').
")";
88 if (empty($user->rights->societe->client->voir) && !$this->socid) {
89 $this->where .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
91 if ($mode ==
'customer') {
92 $this->where .=
" AND (f.fk_statut <> 3 OR f.close_code <> 'replaced')";
95 $this->where .=
" AND f.fk_soc = ".((int) $this->socid);
97 if ($this->userid > 0) {
98 $this->where .=
' AND f.fk_user_author = '.((int) $this->userid);
100 if ($mode ==
'customer') {
101 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
102 $this->where .=
" AND f.type IN (0,1,2,5)";
104 $this->where .=
" AND f.type IN (0,1,2,3,5)";
107 if ($mode ==
'supplier') {
108 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
109 $this->where .=
" AND f.type IN (0,1,2,5)";
111 $this->where .=
" AND f.type IN (0,1,2,3,5)";
116 $this->join .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'societe as s ON s.rowid = f.fk_soc';
117 $this->where .=
' AND s.fk_typent = '.((int) $typentid);
121 $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).
')';
137 $sql =
"SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
138 $sql .=
" FROM ".$this->from;
139 if (empty($user->rights->societe->client->voir) && !$this->socid) {
140 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
144 $sql .=
" AND ".$this->where;
145 $sql .=
" GROUP BY dm";
146 $sql .= $this->db->order(
'dm',
'DESC');
163 $sql =
"SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.
")";
164 $sql .=
" FROM ".$this->from;
165 if (empty($user->rights->societe->client->voir) && !$this->socid) {
166 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
169 $sql .=
" WHERE ".$this->where;
170 $sql .=
" GROUP BY dm";
171 $sql .= $this->db->order(
'dm',
'DESC');
188 $sql =
"SELECT date_format(datef,'%m') as dm, SUM(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";
195 $sql .=
" AND ".$this->where;
196 $sql .=
" GROUP BY dm";
197 $sql .= $this->db->order(
'dm',
'DESC');
214 $sql =
"SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.
")";
215 $sql .=
" FROM ".$this->from;
216 if (empty($user->rights->societe->client->voir) && !$this->socid) {
217 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
221 $sql .=
" AND ".$this->where;
222 $sql .=
" GROUP BY dm";
223 $sql .= $this->db->order(
'dm',
'DESC');
237 $sql =
"SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.
") as total, AVG(f.".$this->field.
") as avg";
238 $sql .=
" FROM ".$this->from;
239 if (empty($user->rights->societe->client->voir) && !$this->socid) {
240 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
243 $sql .=
" WHERE ".$this->where;
244 $sql .=
" GROUP BY year";
245 $sql .= $this->db->order(
'year',
'DESC');
261 $sql =
"SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.
") as total, AVG(tl.".$this->field_line.
") as avg";
262 $sql .=
" FROM ".$this->from.
", ".$this->from_line.
", ".MAIN_DB_PREFIX.
"product as product";
263 if (empty($user->rights->societe->client->voir) && !$this->socid) {
264 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
267 $sql .=
" WHERE ".$this->where;
268 $sql .=
" AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
270 $sql .=
" GROUP BY product.ref";
271 $sql .= $this->db->order(
'nb',
'DESC');
287 $endYear = date(
'Y');
288 $startYear = $endYear - $numberYears;
289 $sql =
"SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.
")";
290 $sql .=
" FROM ".$this->from;
291 if (empty($user->rights->societe->client->voir) && !$this->socid) {
292 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
296 $sql .=
" AND ".$this->where;
297 $sql .=
" GROUP BY dm";
298 $sql .= $this->db->order(
'dm',
'ASC');
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage stats for invoices (customer and supplier)
getAverageByMonth($year)
Return average amount.
getNbByMonth($year, $format=0)
Return orders number by month for a year.
getAmountByMonth($year, $format=0)
Return the invoices amount by month for a year.
__construct(DoliDB $db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByYear()
Return invoices number per year.
getAmountByYear($numberYears, $format=0)
Return the invoices amount by year for a number of past years.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.