list.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  4. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/loan/list.php
  22. * \ingroup loan
  23. * \brief Page to list all loans
  24. */
  25. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("loan", "compta", "banks", "bills"));
  30. // Security check
  31. $socid = GETPOST('socid', 'int');
  32. if ($user->socid) {
  33. $socid = $user->socid;
  34. }
  35. $result = restrictedArea($user, 'loan', '', '', '');
  36. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  37. $sortfield = GETPOST('sortfield', 'aZ09comma');
  38. $sortorder = GETPOST('sortorder', 'aZ09comma');
  39. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  40. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  41. $page = 0;
  42. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  43. $offset = $limit * $page;
  44. $pageprev = $page - 1;
  45. $pagenext = $page + 1;
  46. // Initialize technical objects
  47. $loan_static = new Loan($db);
  48. $extrafields = new ExtraFields($db);
  49. if (!$sortfield) {
  50. $sortfield = "l.rowid";
  51. }
  52. if (!$sortorder) {
  53. $sortorder = "DESC";
  54. }
  55. $search_ref = GETPOST('search_ref', 'int');
  56. $search_label = GETPOST('search_label', 'alpha');
  57. $search_amount = GETPOST('search_amount', 'alpha');
  58. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'loanlist'; // To manage different context of search
  59. $optioncss = GETPOST('optioncss', 'alpha');
  60. /*
  61. * Actions
  62. */
  63. if (GETPOST('cancel', 'alpha')) {
  64. $action = 'list'; $massaction = '';
  65. }
  66. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  67. $massaction = '';
  68. }
  69. $parameters = array();
  70. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  71. if ($reshook < 0) {
  72. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  73. }
  74. if (empty($reshook)) {
  75. // Purge search criteria
  76. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  77. $search_ref = "";
  78. $search_label = "";
  79. $search_amount = "";
  80. }
  81. }
  82. /*
  83. * View
  84. */
  85. $now = dol_now();
  86. //$help_url="EN:Module_MyObject|FR:Module_MyObject_FR|ES:Módulo_MyObject";
  87. $help_url = '';
  88. $title = $langs->trans('Loans');
  89. $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.paid,";
  90. $sql .= " SUM(pl.amount_capital) as alreadypaid";
  91. $sqlfields = $sql; // $sql fields to remove for count total
  92. $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
  93. $linktopl = " LEFT JOIN ".MAIN_DB_PREFIX."payment_loan AS pl ON l.rowid = pl.fk_loan";
  94. $sql .= $linktopl;
  95. $sql .= " WHERE l.entity = ".$conf->entity;
  96. if ($search_amount) {
  97. $sql .= natural_search("l.capital", $search_amount, 1);
  98. }
  99. if ($search_ref) {
  100. $sql .= " AND l.rowid = ".((int) $search_ref);
  101. }
  102. if ($search_label) {
  103. $sql .= natural_search("l.label", $search_label);
  104. }
  105. $sql .= " GROUP BY l.rowid, l.label, l.capital, l.paid, l.datestart, l.dateend";
  106. // Count total nb of records
  107. $nbtotalofrecords = '';
  108. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  109. /* The fast and low memory method to get and count full list converts the sql into a sql count */
  110. $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
  111. $sqlforcount = preg_replace('/'.preg_quote($linktopl, '/').'/', '', $sqlforcount);
  112. $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
  113. $resql = $db->query($sqlforcount);
  114. if ($resql) {
  115. $objforcount = $db->fetch_object($resql);
  116. $nbtotalofrecords = $objforcount->nbtotalofrecords;
  117. } else {
  118. dol_print_error($db);
  119. }
  120. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  121. $page = 0;
  122. $offset = 0;
  123. }
  124. $db->free($resql);
  125. }
  126. // Complete request and execute it with limit
  127. $sql .= $db->order($sortfield, $sortorder);
  128. if ($limit) {
  129. $sql .= $db->plimit($limit + 1, $offset);
  130. }
  131. $resql = $db->query($sql);
  132. if (!$resql) {
  133. dol_print_error($db);
  134. exit;
  135. }
  136. $num = $db->num_rows($resql);
  137. // Output page
  138. // --------------------------------------------------------------------
  139. llxHeader('', $title, $help_url);
  140. if ($resql) {
  141. $i = 0;
  142. $param = '';
  143. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  144. $param .= '&contextpage='.urlencode($contextpage);
  145. }
  146. if ($limit > 0 && $limit != $conf->liste_limit) {
  147. $param .= '&limit='.urlencode($limit);
  148. }
  149. if ($search_ref) {
  150. $param .= "&search_ref=".urlencode($search_ref);
  151. }
  152. if ($search_label) {
  153. $param .= "&search_label=".urlencode($search_label);
  154. }
  155. if ($search_amount) {
  156. $param .= "&search_amount=".urlencode($search_amount);
  157. }
  158. if ($optioncss != '') {
  159. $param .= '&optioncss='.urlencode($optioncss);
  160. }
  161. $url = DOL_URL_ROOT.'/loan/card.php?action=create';
  162. if (!empty($socid)) {
  163. $url .= '&socid='.$socid;
  164. }
  165. $newcardbutton = dolGetButtonTitle($langs->trans('NewLoan'), '', 'fa fa-plus-circle', $url, '', $user->rights->loan->write);
  166. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  167. if ($optioncss != '') {
  168. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  169. }
  170. print '<input type="hidden" name="token" value="'.newToken().'">';
  171. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  172. print '<input type="hidden" name="action" value="list">';
  173. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  174. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  175. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  176. print_barre_liste($langs->trans("Loans"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'money-bill-alt', 0, $newcardbutton, '', $limit, 0, 0, 1);
  177. $moreforfilter = '';
  178. print '<div class="div-table-responsive">';
  179. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  180. // Filters lines
  181. print '<tr class="liste_titre_filter">';
  182. print '<td class="liste_titre"><input class="flat" size="4" type="text" name="search_ref" value="'.$search_ref.'"></td>';
  183. print '<td class="liste_titre"><input class="flat" size="12" type="text" name="search_label" value="'.$search_label.'"></td>';
  184. print '<td class="liste_titre right" ><input class="flat" size="8" type="text" name="search_amount" value="'.$search_amount.'"></td>';
  185. print '<td class="liste_titre">&nbsp;</td>';
  186. print '<td class="liste_titre">&nbsp;</td>';
  187. print '<td class="liste_titre"></td>';
  188. print '<td class="liste_titre maxwidthsearch">';
  189. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  190. print $searchpicto;
  191. print '</td>';
  192. // Fields title label
  193. // --------------------------------------------------------------------
  194. print '<tr class="liste_titre">';
  195. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "l.rowid", "", $param, "", $sortfield, $sortorder);
  196. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "l.label", "", $param, '', $sortfield, $sortorder, 'left ');
  197. print_liste_field_titre("LoanCapital", $_SERVER["PHP_SELF"], "l.capital", "", $param, '', $sortfield, $sortorder, 'right ');
  198. print_liste_field_titre("DateStart", $_SERVER["PHP_SELF"], "l.datestart", "", $param, '', $sortfield, $sortorder, 'center ');
  199. print_liste_field_titre("DateEnd", $_SERVER["PHP_SELF"], "l.dateend", "", $param, '', $sortfield, $sortorder, 'center ');
  200. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "l.paid", "", $param, '', $sortfield, $sortorder, 'right ');
  201. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch ');
  202. print "</tr>\n";
  203. print "</tr>\n";
  204. // Loop on record
  205. // --------------------------------------------------------------------
  206. $i = 0;
  207. $totalarray = array();
  208. while ($i < ($limit ? min($num, $limit) : $num)) {
  209. $obj = $db->fetch_object($resql);
  210. if (empty($obj)) {
  211. break; // Should not happen
  212. }
  213. $loan_static->id = $obj->rowid;
  214. $loan_static->ref = $obj->rowid;
  215. $loan_static->label = $obj->label;
  216. $loan_static->paid = $obj->paid;
  217. print '<tr class="oddeven">';
  218. // Ref
  219. print '<td>'.$loan_static->getNomUrl(1).'</td>';
  220. // Label
  221. print '<td>'.dol_trunc($obj->label, 42).'</td>';
  222. // Capital
  223. print '<td class="right maxwidth100"><span class="amount">'.price($obj->capital).'</span></td>';
  224. // Date start
  225. print '<td class="center width100">'.dol_print_date($db->jdate($obj->datestart), 'day').'</td>';
  226. // Date end
  227. print '<td class="center width100">'.dol_print_date($db->jdate($obj->dateend), 'day').'</td>';
  228. print '<td class="right nowrap">';
  229. print $loan_static->LibStatut($obj->paid, 5, $obj->alreadypaid);
  230. print '</td>';
  231. print '<td></td>';
  232. print "</tr>\n";
  233. $i++;
  234. }
  235. // If no record found
  236. if ($num == 0) {
  237. $colspan = 7;
  238. //foreach ($arrayfields as $key => $val) { if (!empty($val['checked'])) $colspan++; }
  239. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
  240. }
  241. $parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
  242. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object); // Note that $action and $object may have been modified by hook
  243. print $hookmanager->resPrint;
  244. print '</table>'."\n";
  245. print '</div>'."\n";
  246. print '</form>'."\n";
  247. $db->free($resql);
  248. } else {
  249. dol_print_error($db);
  250. }
  251. // End of page
  252. llxFooter();
  253. $db->close();