list.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. $massaction = GETPOST('massaction', 'alpha');
  41. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  42. $page = 0;
  43. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  44. $offset = $limit * $page;
  45. $pageprev = $page - 1;
  46. $pagenext = $page + 1;
  47. // Initialize technical objects
  48. $loan_static = new Loan($db);
  49. $extrafields = new ExtraFields($db);
  50. if (!$sortfield) {
  51. $sortfield = "l.rowid";
  52. }
  53. if (!$sortorder) {
  54. $sortorder = "DESC";
  55. }
  56. $search_ref = GETPOST('search_ref', 'int');
  57. $search_label = GETPOST('search_label', 'alpha');
  58. $search_amount = GETPOST('search_amount', 'alpha');
  59. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'loanlist'; // To manage different context of search
  60. $optioncss = GETPOST('optioncss', 'alpha');
  61. $mode = GETPOST('mode', 'alpha'); // mode view result
  62. /*
  63. * Actions
  64. */
  65. if (GETPOST('cancel', 'alpha')) {
  66. $action = 'list'; $massaction = '';
  67. }
  68. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  69. $massaction = '';
  70. }
  71. $parameters = array();
  72. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  73. if ($reshook < 0) {
  74. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  75. }
  76. if (empty($reshook)) {
  77. // Purge search criteria
  78. 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
  79. $search_ref = "";
  80. $search_label = "";
  81. $search_amount = "";
  82. }
  83. }
  84. /*
  85. * View
  86. */
  87. $now = dol_now();
  88. //$help_url="EN:Module_MyObject|FR:Module_MyObject_FR|ES:Módulo_MyObject";
  89. $help_url = '';
  90. $title = $langs->trans('Loans');
  91. $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.paid,";
  92. $sql .= " SUM(pl.amount_capital) as alreadypaid";
  93. $sqlfields = $sql; // $sql fields to remove for count total
  94. $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
  95. $linktopl = " LEFT JOIN ".MAIN_DB_PREFIX."payment_loan AS pl ON l.rowid = pl.fk_loan";
  96. $sql .= $linktopl;
  97. $sql .= " WHERE l.entity = ".$conf->entity;
  98. if ($search_amount) {
  99. $sql .= natural_search("l.capital", $search_amount, 1);
  100. }
  101. if ($search_ref) {
  102. $sql .= " AND l.rowid = ".((int) $search_ref);
  103. }
  104. if ($search_label) {
  105. $sql .= natural_search("l.label", $search_label);
  106. }
  107. $sql .= " GROUP BY l.rowid, l.label, l.capital, l.paid, l.datestart, l.dateend";
  108. // Count total nb of records
  109. $nbtotalofrecords = '';
  110. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  111. /* The fast and low memory method to get and count full list converts the sql into a sql count */
  112. $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
  113. $sqlforcount = preg_replace('/'.preg_quote($linktopl, '/').'/', '', $sqlforcount);
  114. $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
  115. $resql = $db->query($sqlforcount);
  116. if ($resql) {
  117. $objforcount = $db->fetch_object($resql);
  118. $nbtotalofrecords = $objforcount->nbtotalofrecords;
  119. } else {
  120. dol_print_error($db);
  121. }
  122. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  123. $page = 0;
  124. $offset = 0;
  125. }
  126. $db->free($resql);
  127. }
  128. $arrayfields = array();
  129. // Complete request and execute it with limit
  130. $sql .= $db->order($sortfield, $sortorder);
  131. if ($limit) {
  132. $sql .= $db->plimit($limit + 1, $offset);
  133. }
  134. $resql = $db->query($sql);
  135. if (!$resql) {
  136. dol_print_error($db);
  137. exit;
  138. }
  139. $num = $db->num_rows($resql);
  140. // Output page
  141. // --------------------------------------------------------------------
  142. llxHeader('', $title, $help_url);
  143. if ($resql) {
  144. $i = 0;
  145. $param = '';
  146. if (!empty($mode)) {
  147. $param .= '&mode='.urlencode($mode);
  148. }
  149. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  150. $param .= '&contextpage='.urlencode($contextpage);
  151. }
  152. if ($limit > 0 && $limit != $conf->liste_limit) {
  153. $param .= '&limit='.((int) $limit);
  154. }
  155. if ($search_ref) {
  156. $param .= "&search_ref=".urlencode($search_ref);
  157. }
  158. if ($search_label) {
  159. $param .= "&search_label=".urlencode($search_label);
  160. }
  161. if ($search_amount) {
  162. $param .= "&search_amount=".urlencode($search_amount);
  163. }
  164. if ($optioncss != '') {
  165. $param .= '&optioncss='.urlencode($optioncss);
  166. }
  167. $url = DOL_URL_ROOT.'/loan/card.php?action=create';
  168. if (!empty($socid)) {
  169. $url .= '&socid='.$socid;
  170. }
  171. $newcardbutton = '';
  172. $newcardbutton .= dolGetButtonTitle($langs->trans('ViewList'), '', 'fa fa-bars imgforviewmode', $_SERVER["PHP_SELF"].'?mode=common'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ((empty($mode) || $mode == 'common') ? 2 : 1), array('morecss'=>'reposition'));
  173. $newcardbutton .= dolGetButtonTitle($langs->trans('ViewKanban'), '', 'fa fa-th-list imgforviewmode', $_SERVER["PHP_SELF"].'?mode=kanban'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ($mode == 'kanban' ? 2 : 1), array('morecss'=>'reposition'));
  174. $newcardbutton .= dolGetButtonTitle($langs->trans('NewLoan'), '', 'fa fa-plus-circle', $url, '', $user->rights->loan->write);
  175. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  176. if ($optioncss != '') {
  177. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  178. }
  179. print '<input type="hidden" name="token" value="'.newToken().'">';
  180. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  181. print '<input type="hidden" name="action" value="list">';
  182. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  183. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  184. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  185. print '<input type="hidden" name="mode" value="'.$mode.'">';
  186. print_barre_liste($langs->trans("Loans"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'money-bill-alt', 0, $newcardbutton, '', $limit, 0, 0, 1);
  187. $moreforfilter = '';
  188. print '<div class="div-table-responsive">';
  189. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  190. // Filters lines
  191. print '<tr class="liste_titre_filter">';
  192. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  193. print '<td class="liste_titre maxwidthsearch">';
  194. $searchpicto = $form->showFilterAndCheckAddButtons();
  195. print $searchpicto;
  196. print '</td>';
  197. }
  198. print '<td class="liste_titre"><input class="flat" size="4" type="text" name="search_ref" value="'.$search_ref.'"></td>';
  199. print '<td class="liste_titre"><input class="flat" size="12" type="text" name="search_label" value="'.$search_label.'"></td>';
  200. print '<td class="liste_titre right" ><input class="flat" size="8" type="text" name="search_amount" value="'.$search_amount.'"></td>';
  201. print '<td class="liste_titre">&nbsp;</td>';
  202. print '<td class="liste_titre">&nbsp;</td>';
  203. print '<td class="liste_titre"></td>';
  204. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  205. print '<td class="liste_titre maxwidthsearch">';
  206. $searchpicto = $form->showFilterAndCheckAddButtons();
  207. print $searchpicto;
  208. print '</td>';
  209. }
  210. print '</tr>';
  211. // Fields title label
  212. // --------------------------------------------------------------------
  213. print '<tr class="liste_titre">';
  214. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  215. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch ');
  216. }
  217. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "l.rowid", "", $param, "", $sortfield, $sortorder);
  218. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "l.label", "", $param, '', $sortfield, $sortorder, 'left ');
  219. print_liste_field_titre("LoanCapital", $_SERVER["PHP_SELF"], "l.capital", "", $param, '', $sortfield, $sortorder, 'right ');
  220. print_liste_field_titre("DateStart", $_SERVER["PHP_SELF"], "l.datestart", "", $param, '', $sortfield, $sortorder, 'center ');
  221. print_liste_field_titre("DateEnd", $_SERVER["PHP_SELF"], "l.dateend", "", $param, '', $sortfield, $sortorder, 'center ');
  222. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "l.paid", "", $param, '', $sortfield, $sortorder, 'right ');
  223. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  224. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch ');
  225. }
  226. print "</tr>\n";
  227. print "</tr>\n";
  228. // Loop on record
  229. // --------------------------------------------------------------------
  230. $i = 0;
  231. $totalarray = array();
  232. $imaxinloop = ($limit ? min($num, $limit) : $num);
  233. while ($i < $imaxinloop) {
  234. $obj = $db->fetch_object($resql);
  235. if (empty($obj)) {
  236. break; // Should not happen
  237. }
  238. $loan_static->id = $obj->rowid;
  239. $loan_static->ref = $obj->rowid;
  240. $loan_static->label = $obj->label;
  241. $loan_static->paid = $obj->paid;
  242. if ($mode == 'kanban') {
  243. if ($i == 0) {
  244. print '<tr><td colspan="12">';
  245. print '<div class="box-flex-container kanban">';
  246. }
  247. // Output Kanban
  248. $loan_static->datestart= $obj->datestart;
  249. $loan_static->dateend = $obj->dateend;
  250. $loan_static->capital = $obj->capital;
  251. $loan_static->totalpaid = $obj->paid;
  252. print $loan_static->getKanbanView('', array('selected' => in_array($loan_static->id, $arrayofselected)));
  253. if ($i == ($imaxinloop - 1)) {
  254. print '</div>';
  255. print '</td></tr>';
  256. }
  257. } else {
  258. print '<tr class="oddeven">';
  259. // Action column
  260. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  261. print '<td></td>';
  262. }
  263. // Ref
  264. print '<td>'.$loan_static->getNomUrl(1).'</td>';
  265. // Label
  266. print '<td>'.dol_trunc($obj->label, 42).'</td>';
  267. // Capital
  268. print '<td class="right maxwidth100"><span class="amount">'.price($obj->capital).'</span></td>';
  269. // Date start
  270. print '<td class="center width100">'.dol_print_date($db->jdate($obj->datestart), 'day').'</td>';
  271. // Date end
  272. print '<td class="center width100">'.dol_print_date($db->jdate($obj->dateend), 'day').'</td>';
  273. print '<td class="right nowrap">';
  274. print $loan_static->LibStatut($obj->paid, 5, $obj->alreadypaid);
  275. print '</td>';
  276. // Action column
  277. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  278. print '<td></td>';
  279. }
  280. print "</tr>\n";
  281. }
  282. $i++;
  283. }
  284. // If no record found
  285. if ($num == 0) {
  286. $colspan = 7;
  287. //foreach ($arrayfields as $key => $val) { if (!empty($val['checked'])) $colspan++; }
  288. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
  289. }
  290. $parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
  291. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object); // Note that $action and $object may have been modified by hook
  292. print $hookmanager->resPrint;
  293. print '</table>'."\n";
  294. print '</div>'."\n";
  295. print '</form>'."\n";
  296. $db->free($resql);
  297. } else {
  298. dol_print_error($db);
  299. }
  300. // End of page
  301. llxFooter();
  302. $db->close();