29 require
'../../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/class/dolgraph.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formcompany.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facturestats.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
43 $langs->loadLangs(array(
'bills',
'companies',
'other'));
46 if ($mode ==
'customer' && !$user->rights->facture->lire) {
49 if ($mode ==
'supplier' && empty($user->rights->fournisseur->facture->lire)) {
53 $object_status =
GETPOST(
'object_status',
'intcomma');
54 $typent_id =
GETPOST(
'typent_id',
'int');
55 $categ_id =
GETPOST(
'categ_id',
'categ_id');
57 $userid =
GETPOST(
'userid',
'int');
58 $socid =
GETPOST(
'socid',
'int');
59 $custcats =
GETPOST(
'custcats',
'array');
61 if ($user->socid > 0) {
63 $socid = $user->socid;
66 $nowyear = strftime(
"%Y",
dol_now());
68 $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
76 $langs->load(
'categories');
85 $title = $langs->trans(
"BillsStatistics");
86 $dir = $conf->facture->dir_temp;
88 if ($mode ==
'supplier') {
89 $picto =
'supplier_invoice';
90 $title = $langs->trans(
"BillsStatisticsSuppliers");
91 $dir = $conf->fournisseur->facture->dir_temp;
99 $stats =
new FactureStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
100 if ($mode ==
'customer') {
101 if ($object_status !=
'' && $object_status >= 0) {
102 $stats->where .=
' AND f.fk_statut IN ('.$db->sanitize($object_status).
')';
104 if (is_array($custcats) && !empty($custcats)) {
105 $stats->from .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_societe as cat ON (f.fk_soc = cat.fk_soc)';
106 $stats->where .=
' AND cat.fk_categorie IN ('.$db->sanitize(implode(
',', $custcats)).
')';
109 if ($mode ==
'supplier') {
110 if ($object_status !=
'' && $object_status >= 0) {
111 $stats->where .=
' AND f.fk_statut IN ('.$db->sanitize($object_status).
')';
113 if (is_array($custcats) && !empty($custcats)) {
114 $stats->from .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_fournisseur as cat ON (f.fk_soc = cat.fk_soc)';
115 $stats->where .=
' AND cat.fk_categorie IN ('.$db->sanitize(implode(
',', $custcats)).
')';
121 $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
124 $filenamenb = $dir.
"/invoicesnbinyear-".$year.
".png";
125 if ($mode ==
'customer') {
126 $fileurlnb = DOL_URL_ROOT.
'/viewimage.php?modulepart=billstats&file=invoicesnbinyear-'.$year.
'.png';
128 if ($mode ==
'supplier') {
129 $fileurlnb = DOL_URL_ROOT.
'/viewimage.php?modulepart=billstatssupplier&file=invoicesnbinyear-'.$year.
'.png';
133 $mesg = $px1->isGraphKo();
135 $px1->SetData($data);
138 while ($i <= $endyear) {
142 $px1->SetLegend($legend);
143 $px1->SetMaxValue($px1->GetCeilMaxValue());
144 $px1->SetWidth($WIDTH);
145 $px1->SetHeight($HEIGHT);
146 $px1->SetYLabel($langs->trans(
"NumberOfBills"));
148 $px1->SetHorizTickIncrement(1);
149 $px1->mode =
'depth';
150 $px1->SetTitle($langs->trans(
"NumberOfBillsByMonth"));
152 $px1->draw($filenamenb, $fileurlnb);
156 $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
160 $filenameamount = $dir.
"/invoicesamountinyear-".$year.
".png";
161 if ($mode ==
'customer') {
162 $fileurlamount = DOL_URL_ROOT.
'/viewimage.php?modulepart=billstats&file=invoicesamountinyear-'.$year.
'.png';
164 if ($mode ==
'supplier') {
165 $fileurlamount = DOL_URL_ROOT.
'/viewimage.php?modulepart=billstatssupplier&file=invoicesamountinyear-'.$year.
'.png';
169 $mesg = $px2->isGraphKo();
171 $px2->SetData($data);
174 while ($i <= $endyear) {
178 $px2->SetLegend($legend);
179 $px2->SetMaxValue($px2->GetCeilMaxValue());
180 $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
181 $px2->SetWidth($WIDTH);
182 $px2->SetHeight($HEIGHT);
183 $px2->SetYLabel($langs->trans(
"AmountOfBills"));
185 $px2->SetHorizTickIncrement(1);
186 $px2->mode =
'depth';
187 $px2->SetTitle($langs->trans(
"AmountOfBillsByMonthHT"));
189 $px2->draw($filenameamount, $fileurlamount);
193 $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
195 if (empty($user->rights->societe->client->voir) || $user->socid) {
196 $filename_avg = $dir.
'/ordersaverage-'.$user->id.
'-'.$year.
'.png';
197 if ($mode ==
'customer') {
198 $fileurl_avg = DOL_URL_ROOT.
'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.
'-'.$year.
'.png';
200 if ($mode ==
'supplier') {
201 $fileurl_avg = DOL_URL_ROOT.
'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.
'-'.$year.
'.png';
204 $filename_avg = $dir.
'/ordersaverage-'.$year.
'.png';
205 if ($mode ==
'customer') {
206 $fileurl_avg = DOL_URL_ROOT.
'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.
'.png';
208 if ($mode ==
'supplier') {
209 $fileurl_avg = DOL_URL_ROOT.
'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.
'.png';
214 $mesg = $px3->isGraphKo();
216 $px3->SetData($data);
219 while ($i <= $endyear) {
223 $px3->SetLegend($legend);
224 $px3->SetYLabel($langs->trans(
"AmountAverage"));
225 $px3->SetMaxValue($px3->GetCeilMaxValue());
226 $px3->SetMinValue($px3->GetFloorMinValue());
227 $px3->SetWidth($WIDTH);
228 $px3->SetHeight($HEIGHT);
230 $px3->SetHorizTickIncrement(1);
231 $px3->mode =
'depth';
232 $px3->SetTitle($langs->trans(
"AmountAverage"));
234 $px3->draw($filename_avg, $fileurl_avg);
239 $data = $stats->getAllByYear();
240 $arrayyears = array();
241 foreach ($data as $val) {
242 $arrayyears[$val[
'year']] = $val[
'year'];
244 if (!count($arrayyears)) {
245 $arrayyears[$nowyear] = $nowyear;
251 $head[$h][0] = DOL_URL_ROOT.
'/compta/facture/stats/index.php?mode='.urlencode($mode);
252 $head[$h][1] = $langs->trans(
"ByMonthYear");
253 $head[$h][2] =
'byyear';
256 if ($mode ==
'customer') {
257 $type =
'invoice_stats';
259 if ($mode ==
'supplier') {
260 $type =
'supplier_invoice_stats';
269 if ($mode ==
'customer') {
270 $filter =
's.client in (1,2,3)';
272 if ($mode ==
'supplier') {
273 $filter =
's.fournisseur = 1';
276 print
'<div class="fichecenter"><div class="fichethirdleft">';
280 print
'<form name="stats" method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
281 print
'<input type="hidden" name="token" value="'.newToken().
'">';
282 print
'<input type="hidden" name="mode" value="'.$mode.
'">';
284 print
'<table class="noborder centpercent">';
285 print
'<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans(
"Filter").
'</td></tr>';
287 print
'<tr><td>'.$langs->trans(
"ThirdParty").
'</td><td>';
288 print
img_picto(
'',
'company',
'class="pictofixedwidth"');
289 print
$form->select_company($socid,
'socid', $filter, 1, 0, 0, array(), 0,
'widthcentpercentminusx maxwidth300');
293 print
'<tr><td>'.$langs->trans(
"ThirdPartyType").
'</td><td>';
294 $sortparam_typent = (empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ?
'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT);
295 print
$form->selectarray(
"typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0,
'', 0, 0, 0, $sortparam_typent,
'', 1);
297 print
' '.info_admin($langs->trans(
"YouCanChangeValuesForThisListFromDictionarySetup"), 1);
303 if ($mode ==
'customer') {
304 $cat_type = Categorie::TYPE_CUSTOMER;
305 $cat_label = $langs->trans(
"Category").
' '.lcfirst($langs->trans(
"Customer"));
307 if ($mode ==
'supplier') {
308 $cat_type = Categorie::TYPE_SUPPLIER;
309 $cat_label = $langs->trans(
"Category").
' '.lcfirst($langs->trans(
"Supplier"));
311 print
'<tr><td>'.$cat_label.
'</td><td>';
312 $cate_arbo =
$form->select_all_categories($cat_type,
null,
'parent',
null,
null, 1);
313 print
img_picto(
'',
'category',
'class="pictofixedwidth"');
314 print
$form->multiselectarray(
'custcats', $cate_arbo,
GETPOST(
'custcats',
'array'), 0, 0,
'widthcentpercentminusx maxwidth300');
320 print
'<tr><td>'.$langs->trans(
"CreatedBy").
'</td><td>';
321 print
img_picto(
'',
'user',
'class="pictofixedwidth"');
322 print
$form->select_dolusers($userid,
'userid', 1,
'', 0,
'',
'', 0, 0, 0,
'', 0,
'',
'widthcentpercentminusx maxwidth300');
325 print
'<tr><td>'.$langs->trans(
"Status").
'</td><td>';
326 if ($mode ==
'customer') {
327 $liststatus = array(
'0'=>$langs->trans(
"BillStatusDraft"),
'1'=>$langs->trans(
"BillStatusNotPaid"),
'2'=>$langs->trans(
"BillStatusPaid"),
'1,2'=>$langs->trans(
"BillStatusNotPaid").
' / '.$langs->trans(
"BillStatusPaid"),
'3'=>$langs->trans(
"BillStatusCanceled"));
328 print
$form->selectarray(
'object_status', $liststatus, $object_status, 1);
330 if ($mode ==
'supplier') {
331 $liststatus = array(
'0'=>$langs->trans(
"BillStatusDraft"),
'1'=>$langs->trans(
"BillStatusNotPaid"),
'2'=>$langs->trans(
"BillStatusPaid"));
332 print
$form->selectarray(
'object_status', $liststatus, $object_status, 1);
336 print
'<tr><td>'.$langs->trans(
"Year").
'</td><td>';
337 if (!in_array($year, $arrayyears)) {
338 $arrayyears[$year] = $year;
340 if (!in_array($nowyear, $arrayyears)) {
341 $arrayyears[$nowyear] = $nowyear;
344 print
$form->selectarray(
'year', $arrayyears, $year, 0, 0, 0,
'', 0, 0, 0,
'',
'width75');
346 print
'<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans(
"Refresh").
'"></td></tr>';
351 print
'<div class="div-table-responsive-no-min">';
352 print
'<table class="noborder centpercent">';
353 print
'<tr class="liste_titre" height="24">';
354 print
'<td class="center">'.$langs->trans(
"Year").
'</td>';
355 print
'<td class="right">'.$langs->trans(
"NumberOfBills").
'</td>';
356 print
'<td class="right">%</td>';
357 print
'<td class="right">'.$langs->trans(
"AmountTotal").
'</td>';
358 print
'<td class="right">%</td>';
359 print
'<td class="right">'.$langs->trans(
"AmountAverage").
'</td>';
360 print
'<td class="right">%</td>';
364 foreach ($data as $val) {
365 $year = $val[
'year'];
366 while ($year && $oldyear > $year + 1) {
369 print
'<tr class="oddeven" height="24">';
370 print
'<td align="center"><a href="'.$_SERVER[
"PHP_SELF"].
'?year='.$oldyear.
'&mode='.$mode.($socid > 0 ?
'&socid='.$socid :
'').($userid > 0 ?
'&userid='.$userid :
'').
'">'.$oldyear.
'</a></td>';
371 print
'<td class="right">0</td>';
372 print
'<td class="right"></td>';
373 print
'<td class="right amount">0</td>';
374 print
'<td class="right"></td>';
375 print
'<td class="right amount">0</td>';
376 print
'<td class="right"></td>';
380 print
'<tr class="oddeven" height="24">';
381 print
'<td align="center"><a href="'.$_SERVER[
"PHP_SELF"].
'?year='.$year.
'&mode='.$mode.($socid > 0 ?
'&socid='.$socid :
'').($userid > 0 ?
'&userid='.$userid :
'').
'">'.$year.
'</a></td>';
382 print
'<td class="right">'.$val[
'nb'].
'</td>';
383 print
'<td class="right opacitylow" style="'.((empty($val[
'nb_diff']) || $val[
'nb_diff'] >= 0) ?
'color: green;' :
'color: red;').
'">'.(!empty($val[
'nb_diff']) && $val[
'nb_diff'] < 0 ?
'' :
'+').round(!empty($val[
'nb_diff']) ? $val[
'nb_diff'] : 0).
'%</td>';
384 print
'<td class="right"><span class="amount">'.price(
price2num($val[
'total'],
'MT'), 1).
'</span></td>';
385 print
'<td class="right opacitylow" style="'.((empty($val[
'total_diff']) || $val[
'total_diff'] >= 0) ?
'color: green;' :
'color: red;').
'">'.( !empty($val[
'total_diff']) && $val[
'total_diff'] < 0 ?
'' :
'+').round(!empty($val[
'total_diff']) ? $val[
'total_diff'] : 0).
'%</td>';
386 print
'<td class="right"><span class="amount">'.price(
price2num($val[
'avg'],
'MT'), 1).
'</span></td>';
387 print
'<td class="right opacitylow" style="'.((empty($val[
'avg_diff']) || $val[
'avg_diff'] >= 0) ?
'color: green;' :
'color: red;').
'">'.(!empty($val[
'avg_diff']) && $val[
'avg_diff'] < 0 ?
'' :
'+').round(!empty($val[
'avg_diff']) ? $val[
'avg_diff'] : 0).
'%</td>';
395 print
'</div><div class="fichetwothirdright">';
399 print
'<table class="border centpercent"><tr class="pair nohover"><td align="center">';
409 print
'</td></tr></table>';
412 print
'</div></div>';
413 print
'<div style="clear:both"></div>';