index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  5. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  7. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/facture/stats/index.php
  24. * \ingroup facture
  25. * \brief Page des stats factures
  26. */
  27. // Load Dolibarr environment
  28. require '../../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
  34. if (isModEnabled('categorie')) {
  35. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  36. }
  37. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
  38. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');
  39. // Load translation files required by the page
  40. $langs->loadLangs(array('bills', 'companies', 'other'));
  41. $mode = GETPOST("mode") ? GETPOST("mode") : 'customer';
  42. if ($mode == 'customer' && !$user->rights->facture->lire) {
  43. accessforbidden();
  44. }
  45. if ($mode == 'supplier' && empty($user->rights->fournisseur->facture->lire)) {
  46. accessforbidden();
  47. }
  48. $object_status = GETPOST('object_status', 'intcomma');
  49. $typent_id = GETPOST('typent_id', 'int');
  50. $categ_id = GETPOST('categ_id', 'categ_id');
  51. $userid = GETPOST('userid', 'int');
  52. $socid = GETPOST('socid', 'int');
  53. $custcats = GETPOST('custcats', 'array');
  54. // Security check
  55. if ($user->socid > 0) {
  56. $action = '';
  57. $socid = $user->socid;
  58. }
  59. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  60. $year = GETPOST('year') > 0 ? GETPOST('year', 'int') : $nowyear;
  61. $startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
  62. $endyear = $year;
  63. /*
  64. * View
  65. */
  66. if (isModEnabled('categorie')) {
  67. $langs->load('categories');
  68. }
  69. $form = new Form($db);
  70. $formcompany = new FormCompany($db);
  71. $formother = new FormOther($db);
  72. llxHeader();
  73. $picto = 'bill';
  74. $title = $langs->trans("BillsStatistics");
  75. $dir = $conf->facture->dir_temp;
  76. if ($mode == 'supplier') {
  77. $picto = 'supplier_invoice';
  78. $title = $langs->trans("BillsStatisticsSuppliers");
  79. $dir = $conf->fournisseur->facture->dir_temp;
  80. }
  81. print load_fiche_titre($title, '', $picto);
  82. dol_mkdir($dir);
  83. $stats = new FactureStats($db, $socid, $mode, ($userid > 0 ? $userid : 0), ($typent_id > 0 ? $typent_id : 0), ($categ_id > 0 ? $categ_id : 0));
  84. if ($mode == 'customer') {
  85. if ($object_status != '' && $object_status >= 0) {
  86. $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($object_status).')';
  87. }
  88. if (is_array($custcats) && !empty($custcats)) {
  89. $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cat ON (f.fk_soc = cat.fk_soc)';
  90. $stats->where .= ' AND cat.fk_categorie IN ('.$db->sanitize(implode(',', $custcats)).')';
  91. }
  92. }
  93. if ($mode == 'supplier') {
  94. if ($object_status != '' && $object_status >= 0) {
  95. $stats->where .= ' AND f.fk_statut IN ('.$db->sanitize($object_status).')';
  96. }
  97. if (is_array($custcats) && !empty($custcats)) {
  98. $stats->from .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_fournisseur as cat ON (f.fk_soc = cat.fk_soc)';
  99. $stats->where .= ' AND cat.fk_categorie IN ('.$db->sanitize(implode(',', $custcats)).')';
  100. }
  101. }
  102. // Build graphic number of object
  103. // $data = array(array('Lib',val1,val2,val3),...)
  104. $data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
  105. //var_dump($data);
  106. $filenamenb = $dir."/invoicesnbinyear-".$year.".png";
  107. if ($mode == 'customer') {
  108. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesnbinyear-'.$year.'.png';
  109. }
  110. if ($mode == 'supplier') {
  111. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicesnbinyear-'.$year.'.png';
  112. }
  113. $px1 = new DolGraph();
  114. $mesg = $px1->isGraphKo();
  115. if (!$mesg) {
  116. $px1->SetData($data);
  117. $i = $startyear;
  118. $legend = array();
  119. while ($i <= $endyear) {
  120. $legend[] = $i;
  121. $i++;
  122. }
  123. $px1->SetLegend($legend);
  124. $px1->SetMaxValue($px1->GetCeilMaxValue());
  125. $px1->SetWidth($WIDTH);
  126. $px1->SetHeight($HEIGHT);
  127. $px1->SetYLabel($langs->trans("NumberOfBills"));
  128. $px1->SetShading(3);
  129. $px1->SetHorizTickIncrement(1);
  130. $px1->mode = 'depth';
  131. $px1->SetTitle($langs->trans("NumberOfBillsByMonth"));
  132. $px1->draw($filenamenb, $fileurlnb);
  133. }
  134. // Build graphic amount of object
  135. $data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
  136. //var_dump($data);
  137. // $data = array(array('Lib',val1,val2,val3),...)
  138. $filenameamount = $dir."/invoicesamountinyear-".$year.".png";
  139. if ($mode == 'customer') {
  140. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$year.'.png';
  141. }
  142. if ($mode == 'supplier') {
  143. $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesamountinyear-'.$year.'.png';
  144. }
  145. $px2 = new DolGraph();
  146. $mesg = $px2->isGraphKo();
  147. if (!$mesg) {
  148. $px2->SetData($data);
  149. $i = $startyear;
  150. $legend = array();
  151. while ($i <= $endyear) {
  152. $legend[] = $i;
  153. $i++;
  154. }
  155. $px2->SetLegend($legend);
  156. $px2->SetMaxValue($px2->GetCeilMaxValue());
  157. $px2->SetMinValue(min(0, $px2->GetFloorMinValue()));
  158. $px2->SetWidth($WIDTH);
  159. $px2->SetHeight($HEIGHT);
  160. $px2->SetYLabel($langs->trans("AmountOfBills"));
  161. $px2->SetShading(3);
  162. $px2->SetHorizTickIncrement(1);
  163. $px2->mode = 'depth';
  164. $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT"));
  165. $px2->draw($filenameamount, $fileurlamount);
  166. }
  167. $data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
  168. if (empty($user->rights->societe->client->voir) || $user->socid) {
  169. $filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
  170. if ($mode == 'customer') {
  171. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  172. }
  173. if ($mode == 'supplier') {
  174. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
  175. }
  176. } else {
  177. $filename_avg = $dir.'/ordersaverage-'.$year.'.png';
  178. if ($mode == 'customer') {
  179. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
  180. }
  181. if ($mode == 'supplier') {
  182. $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$year.'.png';
  183. }
  184. }
  185. $px3 = new DolGraph();
  186. $mesg = $px3->isGraphKo();
  187. if (!$mesg) {
  188. $px3->SetData($data);
  189. $i = $startyear;
  190. $legend = array();
  191. while ($i <= $endyear) {
  192. $legend[] = $i;
  193. $i++;
  194. }
  195. $px3->SetLegend($legend);
  196. $px3->SetYLabel($langs->trans("AmountAverage"));
  197. $px3->SetMaxValue($px3->GetCeilMaxValue());
  198. $px3->SetMinValue($px3->GetFloorMinValue());
  199. $px3->SetWidth($WIDTH);
  200. $px3->SetHeight($HEIGHT);
  201. $px3->SetShading(3);
  202. $px3->SetHorizTickIncrement(1);
  203. $px3->mode = 'depth';
  204. $px3->SetTitle($langs->trans("AmountAverage"));
  205. $px3->draw($filename_avg, $fileurl_avg);
  206. }
  207. // Show array
  208. $data = $stats->getAllByYear();
  209. $arrayyears = array();
  210. foreach ($data as $val) {
  211. $arrayyears[$val['year']] = $val['year'];
  212. }
  213. if (!count($arrayyears)) {
  214. $arrayyears[$nowyear] = $nowyear;
  215. }
  216. $h = 0;
  217. $head = array();
  218. $head[$h][0] = DOL_URL_ROOT.'/compta/facture/stats/index.php?mode='.urlencode($mode);
  219. $head[$h][1] = $langs->trans("ByMonthYear");
  220. $head[$h][2] = 'byyear';
  221. $h++;
  222. if ($mode == 'customer') {
  223. $type = 'invoice_stats';
  224. }
  225. if ($mode == 'supplier') {
  226. $type = 'supplier_invoice_stats';
  227. }
  228. complete_head_from_modules($conf, $langs, null, $head, $h, $type);
  229. print dol_get_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
  230. // We use select_thirdparty_list instead of select_company so we can use $filter and share same code for customer and supplier.
  231. $filter = '';
  232. if ($mode == 'customer') {
  233. $filter = 's.client in (1,2,3)';
  234. }
  235. if ($mode == 'supplier') {
  236. $filter = 's.fournisseur = 1';
  237. }
  238. print '<div class="fichecenter"><div class="fichethirdleft">';
  239. // Show filter box
  240. print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  241. print '<input type="hidden" name="token" value="'.newToken().'">';
  242. print '<input type="hidden" name="mode" value="'.$mode.'">';
  243. print '<table class="noborder centpercent">';
  244. print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
  245. // Company
  246. print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
  247. print img_picto('', 'company', 'class="pictofixedwidth"');
  248. print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, 'widthcentpercentminusx maxwidth300');
  249. print '</td></tr>';
  250. // ThirdParty Type
  251. print '<tr><td>'.$langs->trans("ThirdPartyType").'</td><td>';
  252. $sortparam_typent = (empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ? 'ASC' : $conf->global->SOCIETE_SORT_ON_TYPEENT); // NONE means we keep sort of original array, so we sort on position. ASC, means next function will sort on label.
  253. print $form->selectarray("typent_id", $formcompany->typent_array(0), $typent_id, 1, 0, 0, '', 0, 0, 0, $sortparam_typent, '', 1);
  254. if ($user->admin) {
  255. print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  256. }
  257. print '</td></tr>';
  258. // Category
  259. if (isModEnabled('categorie')) {
  260. if ($mode == 'customer') {
  261. $cat_type = Categorie::TYPE_CUSTOMER;
  262. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Customer"));
  263. }
  264. if ($mode == 'supplier') {
  265. $cat_type = Categorie::TYPE_SUPPLIER;
  266. $cat_label = $langs->trans("Category").' '.lcfirst($langs->trans("Supplier"));
  267. }
  268. print '<tr><td>'.$cat_label.'</td><td>';
  269. $cate_arbo = $form->select_all_categories($cat_type, null, 'parent', null, null, 1);
  270. print img_picto('', 'category', 'class="pictofixedwidth"');
  271. print $form->multiselectarray('custcats', $cate_arbo, GETPOST('custcats', 'array'), 0, 0, 'widthcentpercentminusx maxwidth300');
  272. //print $formother->select_categories($cat_type, $categ_id, 'categ_id', true);
  273. print '</td></tr>';
  274. }
  275. // User
  276. print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
  277. print img_picto('', 'user', 'class="pictofixedwidth"');
  278. print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'widthcentpercentminusx maxwidth300');
  279. print '</td></tr>';
  280. // Status
  281. print '<tr><td>'.$langs->trans("Status").'</td><td>';
  282. if ($mode == 'customer') {
  283. $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"));
  284. print $form->selectarray('object_status', $liststatus, $object_status, 1);
  285. }
  286. if ($mode == 'supplier') {
  287. $liststatus = array('0'=>$langs->trans("BillStatusDraft"), '1'=>$langs->trans("BillStatusNotPaid"), '2'=>$langs->trans("BillStatusPaid"));
  288. print $form->selectarray('object_status', $liststatus, $object_status, 1);
  289. }
  290. print '</td></tr>';
  291. // Year
  292. print '<tr><td>'.$langs->trans("Year").'</td><td>';
  293. if (!in_array($year, $arrayyears)) {
  294. $arrayyears[$year] = $year;
  295. }
  296. if (!in_array($nowyear, $arrayyears)) {
  297. $arrayyears[$nowyear] = $nowyear;
  298. }
  299. arsort($arrayyears);
  300. print $form->selectarray('year', $arrayyears, $year, 0, 0, 0, '', 0, 0, 0, '', 'width75');
  301. print '</td></tr>';
  302. print '<tr><td class="center" colspan="2"><input type="submit" name="submit" class="button small" value="'.$langs->trans("Refresh").'"></td></tr>';
  303. print '</table>';
  304. print '</form>';
  305. print '<br><br>';
  306. print '<div class="div-table-responsive-no-min">';
  307. print '<table class="noborder centpercent">';
  308. print '<tr class="liste_titre" height="24">';
  309. print '<td class="center">'.$langs->trans("Year").'</td>';
  310. print '<td class="right">'.$langs->trans("NumberOfBills").'</td>';
  311. print '<td class="right">%</td>';
  312. print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
  313. print '<td class="right">%</td>';
  314. print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
  315. print '<td class="right">%</td>';
  316. print '</tr>';
  317. $oldyear = 0;
  318. foreach ($data as $val) {
  319. $year = $val['year'];
  320. while ($year && $oldyear > $year + 1) { // If we have empty year
  321. $oldyear--;
  322. print '<tr class="oddeven" height="24">';
  323. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$oldyear.'</a></td>';
  324. print '<td class="right">0</td>';
  325. print '<td class="right"></td>';
  326. print '<td class="right amount">0</td>';
  327. print '<td class="right"></td>';
  328. print '<td class="right amount">0</td>';
  329. print '<td class="right"></td>';
  330. print '</tr>';
  331. }
  332. if ($mode == 'supplier') {
  333. $greennb = (empty($val['nb_diff']) || $val['nb_diff'] <= 0);
  334. $greentotal = (empty($val['total_diff']) || $val['total_diff'] <= 0);
  335. $greenavg = (empty($val['avg_diff']) || $val['avg_diff'] <= 0);
  336. } else {
  337. $greennb = (empty($val['nb_diff']) || $val['nb_diff'] >= 0);
  338. $greentotal = (empty($val['total_diff']) || $val['total_diff'] >= 0);
  339. $greenavg = (empty($val['avg_diff']) || $val['avg_diff'] >= 0);
  340. }
  341. print '<tr class="oddeven" height="24">';
  342. print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid > 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year.'</a></td>';
  343. print '<td class="right">'.$val['nb'].'</td>';
  344. print '<td class="right opacitylow" style="'.($greennb ? 'color: green;' : 'color: red;').'">'.(!empty($val['nb_diff']) && $val['nb_diff'] < 0 ? '' : '+').round(!empty($val['nb_diff']) ? $val['nb_diff'] : 0).'%</td>';
  345. print '<td class="right"><span class="amount">'.price(price2num($val['total'], 'MT'), 1).'</span></td>';
  346. print '<td class="right opacitylow" style="'.($greentotal ? 'color: green;' : 'color: red;').'">'.( !empty($val['total_diff']) && $val['total_diff'] < 0 ? '' : '+').round(!empty($val['total_diff']) ? $val['total_diff'] : 0).'%</td>';
  347. print '<td class="right"><span class="amount">'.price(price2num($val['avg'], 'MT'), 1).'</span></td>';
  348. print '<td class="right opacitylow" style="'.($greenavg ? 'color: green;' : 'color: red;').'">'.(!empty($val['avg_diff']) && $val['avg_diff'] < 0 ? '' : '+').round(!empty($val['avg_diff']) ? $val['avg_diff'] : 0).'%</td>';
  349. print '</tr>';
  350. $oldyear = $year;
  351. }
  352. print '</table>';
  353. print '</div>';
  354. print '</div><div class="fichetwothirdright">';
  355. // Show graphs
  356. print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
  357. if ($mesg) {
  358. print $mesg;
  359. } else {
  360. print $px1->show();
  361. print "<br>\n";
  362. print $px2->show();
  363. print "<br>\n";
  364. print $px3->show();
  365. }
  366. print '</td></tr></table>';
  367. print '</div></div>';
  368. print '<div style="clear:both"></div>';
  369. print dol_get_fiche_end();
  370. // End of page
  371. llxFooter();
  372. $db->close();