index.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  7. * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
  8. * Copyright (C) 2019 Frédéric FRANCE <frederic.france@netlogic.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. * or see https://www.gnu.org/
  23. */
  24. /**
  25. * \file htdocs/expensereport/index.php
  26. * \ingroup expensereport
  27. * \brief Page list of expenses
  28. */
  29. // Load Dolibarr environment
  30. require '../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  33. $hookmanager = new HookManager($db);
  34. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  35. $hookmanager->initHooks(array('expensereportindex'));
  36. // Load translation files required by the page
  37. $langs->loadLangs(array('companies', 'users', 'trips'));
  38. // Security check
  39. $socid = GETPOST('socid', 'int');
  40. if ($user->socid) {
  41. $socid = $user->socid;
  42. }
  43. $result = restrictedArea($user, 'expensereport', '', '');
  44. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  45. $sortfield = GETPOST('sortfield', 'aZ09comma');
  46. $sortorder = GETPOST('sortorder', 'aZ09comma');
  47. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  48. if (empty($page) || $page == -1) {
  49. $page = 0;
  50. } // If $page is not defined, or '' or -1
  51. $offset = $limit * $page;
  52. $pageprev = $page - 1;
  53. $pagenext = $page + 1;
  54. if (!$sortorder) {
  55. $sortorder = "DESC";
  56. }
  57. if (!$sortfield) {
  58. $sortfield = "d.date_create";
  59. }
  60. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  61. /*
  62. * View
  63. */
  64. $tripandexpense_static = new ExpenseReport($db);
  65. $childids = $user->getAllChildIds();
  66. $childids[] = $user->id;
  67. $help_url = "EN:Module_Expense_Reports|FR:Module_Notes_de_frais";
  68. llxHeader('', $langs->trans("TripsAndExpenses"), $help_url);
  69. $label = $somme = $nb = array();
  70. $totalnb = $totalsum = 0;
  71. $sql = "SELECT tf.code, tf.label, count(de.rowid) as nb, sum(de.total_ht) as km";
  72. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as d, ".MAIN_DB_PREFIX."expensereport_det as de, ".MAIN_DB_PREFIX."c_type_fees as tf";
  73. $sql .= " WHERE de.fk_expensereport = d.rowid AND d.entity IN (".getEntity('expensereport').") AND de.fk_c_type_fees = tf.id";
  74. // RESTRICT RIGHTS
  75. if (!$user->hasRight('expensereport', 'readall') && !$user->hasRight('expensereport', 'lire_tous')
  76. && (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') || !$user->hasRight('expensereport', 'writeall_advance'))) {
  77. $childids = $user->getAllChildIds();
  78. $childids[] = $user->id;
  79. $sql .= " AND d.fk_user_author IN (".$db->sanitize(join(',', $childids)).")\n";
  80. }
  81. $sql .= " GROUP BY tf.code, tf.label";
  82. $result = $db->query($sql);
  83. if ($result) {
  84. $num = $db->num_rows($result);
  85. $i = 0;
  86. while ($i < $num) {
  87. $objp = $db->fetch_object($result);
  88. $somme[$objp->code] = $objp->km;
  89. $nb[$objp->code] = $objp->nb;
  90. $label[$objp->code] = $objp->label;
  91. $totalnb += $objp->nb;
  92. $totalsum += $objp->km;
  93. $i++;
  94. }
  95. $db->free($result);
  96. } else {
  97. dol_print_error($db);
  98. }
  99. print load_fiche_titre($langs->trans("ExpensesArea"), '', 'trip');
  100. print '<div class="fichecenter"><div class="fichethirdleft">';
  101. print '<div class="div-table-responsive-no-min">';
  102. print '<table class="noborder nohover centpercent">';
  103. print '<tr class="liste_titre">';
  104. print '<th colspan="4">'.$langs->trans("Statistics").'</th>';
  105. print "</tr>\n";
  106. $listoftype = $tripandexpense_static->listOfTypes();
  107. foreach ($listoftype as $code => $label) {
  108. $dataseries[] = array($label, (isset($somme[$code]) ? (int) $somme[$code] : 0));
  109. }
  110. // Sort array with most important first
  111. $dataseries = dol_sort_array($dataseries, 1, 'desc');
  112. // Merge all entrie after the $KEEPNFIRST one into one entry called "Other..." (to avoid to have too much entries in graphic).
  113. $KEEPNFIRST = 7; // Keep first $KEEPNFIRST one + 1 with the remain
  114. $i = 0;
  115. if (count($dataseries) > ($KEEPNFIRST + 1)) {
  116. foreach ($dataseries as $key => $val) {
  117. if ($i < $KEEPNFIRST) {
  118. $i++;
  119. continue;
  120. }
  121. // Here $key = $KEEPNFIRST
  122. $dataseries[$KEEPNFIRST][0] = $langs->trans("Others").'...';
  123. if ($key == $KEEPNFIRST) {
  124. $i++;
  125. continue;
  126. }
  127. $dataseries[$KEEPNFIRST][1] += $dataseries[$key][1];
  128. unset($dataseries[$key]);
  129. $i++;
  130. }
  131. }
  132. if ($conf->use_javascript_ajax) {
  133. print '<tr><td class="center" colspan="4">';
  134. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  135. $dolgraph = new DolGraph();
  136. $dolgraph->SetData($dataseries);
  137. $dolgraph->setHeight(350);
  138. $dolgraph->combine = !getDolGlobalString('MAIN_EXPENSEREPORT_COMBINE_GRAPH_STAT') ? 0.05 : $conf->global->MAIN_EXPENSEREPORT_COMBINE_GRAPH_STAT;
  139. $dolgraph->setShowLegend(2);
  140. $dolgraph->setShowPercent(1);
  141. $dolgraph->SetType(array('pie'));
  142. $dolgraph->setHeight('200');
  143. $dolgraph->draw('idgraphstatus');
  144. print $dolgraph->show($totalnb ? 0 : 1);
  145. print '</td></tr>';
  146. }
  147. print '<tr class="liste_total">';
  148. print '<td>'.$langs->trans("Total").'</td>';
  149. print '<td class="right" colspan="3">'.price($totalsum, 1, $langs, 0, 0, 0, $conf->currency).'</td>';
  150. print '</tr>';
  151. print '</table>';
  152. print '</div>';
  153. // Right area
  154. print '</div><div class="fichetwothirdright">';
  155. $max = 10;
  156. $langs->load("boxes");
  157. $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.login, u.statut as user_status, u.photo, u.email, u.admin,";
  158. $sql .= " d.rowid, d.ref, d.date_debut as dated, d.date_fin as datef, d.date_create as dm, d.total_ht, d.total_ttc, d.fk_statut as status";
  159. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as d, ".MAIN_DB_PREFIX."user as u";
  160. $sql .= " WHERE u.rowid = d.fk_user_author";
  161. // RESTRICT RIGHTS
  162. if (!$user->hasRight('expensereport', 'readall') && !$user->hasRight('expensereport', 'lire_tous')
  163. && (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') || !$user->hasRight('expensereport', 'writeall_advance'))) {
  164. $childids = $user->getAllChildIds();
  165. $childids[] = $user->id;
  166. $sql .= " AND d.fk_user_author IN (".$db->sanitize(join(',', $childids)).")\n";
  167. }
  168. $sql .= ' AND d.entity IN ('.getEntity('expensereport').')';
  169. $sql .= $db->order($sortfield, $sortorder);
  170. $sql .= $db->plimit($max, 0);
  171. $result = $db->query($sql);
  172. if ($result) {
  173. $var = false;
  174. $num = $db->num_rows($result);
  175. $i = 0;
  176. print '<div class="div-table-responsive-no-min">';
  177. print '<table class="noborder centpercent">';
  178. print '<tr class="liste_titre">';
  179. print '<th colspan="2">'.$langs->trans("BoxTitleLastModifiedExpenses", min($max, $num)).'</th>';
  180. print '<th class="right">'.$langs->trans("AmountHT").'</th>';
  181. print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  182. print '<th class="right">'.$langs->trans("DateModificationShort").'</th>';
  183. print '<th>&nbsp;</th>';
  184. print '</tr>';
  185. if ($num) {
  186. $total_ttc = $totalam = $total = 0;
  187. $expensereportstatic = new ExpenseReport($db);
  188. $userstatic = new User($db);
  189. while ($i < $num && $i < $max) {
  190. $obj = $db->fetch_object($result);
  191. $expensereportstatic->id = $obj->rowid;
  192. $expensereportstatic->ref = $obj->ref;
  193. $expensereportstatic->status = $obj->status;
  194. $userstatic->id = $obj->uid;
  195. $userstatic->admin = $obj->admin;
  196. $userstatic->email = $obj->email;
  197. $userstatic->lastname = $obj->lastname;
  198. $userstatic->firstname = $obj->firstname;
  199. $userstatic->login = $obj->login;
  200. $userstatic->statut = $obj->user_status;
  201. $userstatic->photo = $obj->photo;
  202. print '<tr class="oddeven">';
  203. print '<td class="tdoverflowmax200">'.$expensereportstatic->getNomUrl(1).'</td>';
  204. print '<td class="tdoverflowmax150">'.$userstatic->getNomUrl(-1).'</td>';
  205. print '<td class="right amount">'.price($obj->total_ht).'</td>';
  206. print '<td class="right amount">'.price($obj->total_ttc).'</td>';
  207. print '<td class="right">'.dol_print_date($db->jdate($obj->dm), 'day').'</td>';
  208. print '<td class="right">';
  209. print $expensereportstatic->getLibStatut(3);
  210. print '</td>';
  211. print '</tr>';
  212. $i++;
  213. }
  214. } else {
  215. print '<tr class="oddeven"><td colspan="6" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  216. }
  217. print '</table></div><br>';
  218. } else {
  219. dol_print_error($db);
  220. }
  221. print '</div></div>';
  222. $parameters = array('user' => $user);
  223. $reshook = $hookmanager->executeHooks('dashboardExpenseReport', $parameters, $object); // Note that $action and $object may have been modified by hook
  224. // End of page
  225. llxFooter();
  226. $db->close();