index.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  32. $hookmanager = new HookManager($db);
  33. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  34. $hookmanager->initHooks(array('expensereportindex'));
  35. // Load translation files required by the page
  36. $langs->loadLangs(array('companies', 'users', 'trips'));
  37. // Security check
  38. $socid = GETPOST('socid', 'int');
  39. if ($user->socid) {
  40. $socid = $user->socid;
  41. }
  42. $result = restrictedArea($user, 'expensereport', '', '');
  43. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  44. $sortfield = GETPOST('sortfield', 'aZ09comma');
  45. $sortorder = GETPOST('sortorder', 'aZ09comma');
  46. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  47. if (empty($page) || $page == -1) {
  48. $page = 0;
  49. } // If $page is not defined, or '' or -1
  50. $offset = $limit * $page;
  51. $pageprev = $page - 1;
  52. $pagenext = $page + 1;
  53. if (!$sortorder) {
  54. $sortorder = "DESC";
  55. }
  56. if (!$sortfield) {
  57. $sortfield = "d.date_create";
  58. }
  59. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  60. /*
  61. * View
  62. */
  63. $tripandexpense_static = new ExpenseReport($db);
  64. $childids = $user->getAllChildIds();
  65. $childids[] = $user->id;
  66. $help_url = "EN:Module_Expense_Reports|FR:Module_Notes_de_frais";
  67. llxHeader('', $langs->trans("ListOfFees"), $help_url);
  68. $label = $somme = $nb = array();
  69. $totalnb = $totalsum = 0;
  70. $sql = "SELECT tf.code, tf.label, count(de.rowid) as nb, sum(de.total_ht) as km";
  71. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as d, ".MAIN_DB_PREFIX."expensereport_det as de, ".MAIN_DB_PREFIX."c_type_fees as tf";
  72. $sql .= " WHERE de.fk_expensereport = d.rowid AND d.entity IN (".getEntity('expensereport').") AND de.fk_c_type_fees = tf.id";
  73. // RESTRICT RIGHTS
  74. if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
  75. && (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || empty($user->rights->expensereport->writeall_advance))) {
  76. $childids = $user->getAllChildIds();
  77. $childids[] = $user->id;
  78. $sql .= " AND d.fk_user_author IN (".$db->sanitize(join(',', $childids)).")\n";
  79. }
  80. $sql .= " GROUP BY tf.code, tf.label";
  81. $result = $db->query($sql);
  82. if ($result) {
  83. $num = $db->num_rows($result);
  84. $i = 0;
  85. while ($i < $num) {
  86. $objp = $db->fetch_object($result);
  87. $somme[$objp->code] = $objp->km;
  88. $nb[$objp->code] = $objp->nb;
  89. $label[$objp->code] = $objp->label;
  90. $totalnb += $objp->nb;
  91. $totalsum += $objp->km;
  92. $i++;
  93. }
  94. $db->free($result);
  95. } else {
  96. dol_print_error($db);
  97. }
  98. print load_fiche_titre($langs->trans("ExpensesArea"), '', 'trip');
  99. print '<div class="fichecenter"><div class="fichethirdleft">';
  100. print '<div class="div-table-responsive-no-min">';
  101. print '<table class="noborder nohover centpercent">';
  102. print '<tr class="liste_titre">';
  103. print '<th colspan="4">'.$langs->trans("Statistics").'</th>';
  104. print "</tr>\n";
  105. $listoftype = $tripandexpense_static->listOfTypes();
  106. foreach ($listoftype as $code => $label) {
  107. $dataseries[] = array($label, (isset($somme[$code]) ? (int) $somme[$code] : 0));
  108. }
  109. // Sort array with most important first
  110. $dataseries = dol_sort_array($dataseries, 1, 'desc');
  111. // Merge all entrie after the $KEEPNFIRST one into one entry called "Other..." (to avoid to have too much entries in graphic).
  112. $KEEPNFIRST = 7; // Keep first $KEEPNFIRST one + 1 with the remain
  113. $i = 0;
  114. if (count($dataseries) > ($KEEPNFIRST + 1)) {
  115. foreach ($dataseries as $key => $val) {
  116. if ($i < $KEEPNFIRST) {
  117. $i++;
  118. continue;
  119. }
  120. // Here $key = $KEEPNFIRST
  121. $dataseries[$KEEPNFIRST][0] = $langs->trans("Others").'...';
  122. if ($key == $KEEPNFIRST) {
  123. $i++;
  124. continue;
  125. }
  126. $dataseries[$KEEPNFIRST][1] += $dataseries[$key][1];
  127. unset($dataseries[$key]);
  128. $i++;
  129. }
  130. }
  131. if ($conf->use_javascript_ajax) {
  132. print '<tr><td class="center" colspan="4">';
  133. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  134. $dolgraph = new DolGraph();
  135. $dolgraph->SetData($dataseries);
  136. $dolgraph->setHeight(350);
  137. $dolgraph->combine = empty($conf->global->MAIN_EXPENSEREPORT_COMBINE_GRAPH_STAT) ? 0.05 : $conf->global->MAIN_EXPENSEREPORT_COMBINE_GRAPH_STAT;
  138. $dolgraph->setShowLegend(2);
  139. $dolgraph->setShowPercent(1);
  140. $dolgraph->SetType(array('pie'));
  141. $dolgraph->setHeight('200');
  142. $dolgraph->draw('idgraphstatus');
  143. print $dolgraph->show($totalnb ? 0 : 1);
  144. print '</td></tr>';
  145. }
  146. print '<tr class="liste_total">';
  147. print '<td>'.$langs->trans("Total").'</td>';
  148. print '<td class="right" colspan="3">'.price($totalsum, 1, $langs, 0, 0, 0, $conf->currency).'</td>';
  149. print '</tr>';
  150. print '</table>';
  151. print '</div>';
  152. // Right area
  153. print '</div><div class="fichetwothirdright">';
  154. $max = 10;
  155. $langs->load("boxes");
  156. $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.login, u.statut as user_status, u.photo, u.email, u.admin,";
  157. $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";
  158. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as d, ".MAIN_DB_PREFIX."user as u";
  159. $sql .= " WHERE u.rowid = d.fk_user_author";
  160. // RESTRICT RIGHTS
  161. if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
  162. && (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || empty($user->rights->expensereport->writeall_advance))) {
  163. $childids = $user->getAllChildIds();
  164. $childids[] = $user->id;
  165. $sql .= " AND d.fk_user_author IN (".$db->sanitize(join(',', $childids)).")\n";
  166. }
  167. $sql .= ' AND d.entity IN ('.getEntity('expensereport').')';
  168. $sql .= $db->order($sortfield, $sortorder);
  169. $sql .= $db->plimit($max, 0);
  170. $result = $db->query($sql);
  171. if ($result) {
  172. $var = false;
  173. $num = $db->num_rows($result);
  174. $i = 0;
  175. print '<div class="div-table-responsive-no-min">';
  176. print '<table class="noborder centpercent">';
  177. print '<tr class="liste_titre">';
  178. print '<th colspan="2">'.$langs->trans("BoxTitleLastModifiedExpenses", min($max, $num)).'</th>';
  179. print '<th class="right">'.$langs->trans("AmountHT").'</th>';
  180. print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
  181. print '<th class="right">'.$langs->trans("DateModificationShort").'</th>';
  182. print '<th>&nbsp;</th>';
  183. print '</tr>';
  184. if ($num) {
  185. $total_ttc = $totalam = $total = 0;
  186. $expensereportstatic = new ExpenseReport($db);
  187. $userstatic = new User($db);
  188. while ($i < $num && $i < $max) {
  189. $obj = $db->fetch_object($result);
  190. $expensereportstatic->id = $obj->rowid;
  191. $expensereportstatic->ref = $obj->ref;
  192. $expensereportstatic->status = $obj->status;
  193. $userstatic->id = $obj->uid;
  194. $userstatic->admin = $obj->admin;
  195. $userstatic->email = $obj->email;
  196. $userstatic->lastname = $obj->lastname;
  197. $userstatic->firstname = $obj->firstname;
  198. $userstatic->login = $obj->login;
  199. $userstatic->statut = $obj->user_status;
  200. $userstatic->photo = $obj->photo;
  201. print '<tr class="oddeven">';
  202. print '<td class="tdoverflowmax200">'.$expensereportstatic->getNomUrl(1).'</td>';
  203. print '<td class="tdoverflowmax150">'.$userstatic->getNomUrl(-1).'</td>';
  204. print '<td class="right amount">'.price($obj->total_ht).'</td>';
  205. print '<td class="right amount">'.price($obj->total_ttc).'</td>';
  206. print '<td class="right">'.dol_print_date($db->jdate($obj->dm), 'day').'</td>';
  207. print '<td class="right">';
  208. print $expensereportstatic->getLibStatut(3);
  209. print '</td>';
  210. print '</tr>';
  211. $i++;
  212. }
  213. } else {
  214. print '<tr class="oddeven"><td colspan="6" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  215. }
  216. print '</table></div><br>';
  217. } else {
  218. dol_print_error($db);
  219. }
  220. print '</div></div>';
  221. $parameters = array('user' => $user);
  222. $reshook = $hookmanager->executeHooks('dashboardExpenseReport', $parameters, $object); // Note that $action and $object may have been modified by hook
  223. // End of page
  224. llxFooter();
  225. $db->close();