demandes.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011-2012 Juanjo Menent <jmenent@2byte.es>
  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/compta/prelevement/demandes.php
  22. * \ingroup prelevement
  23. * \brief Page to list bank transfer requests (debit order or payments of vendors)
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/modPrelevement.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array('banks', 'categories', 'withdrawals', 'companies'));
  35. // Security check
  36. $socid = GETPOST('socid', 'int');
  37. $status = GETPOST('status', 'int');
  38. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'directdebitcredittransferlist'; // To manage different context of search
  39. $backtopage = GETPOST('backtopage', 'alpha'); // Go back to a dedicated page
  40. $optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print')
  41. $type = GETPOST('type', 'aZ09');
  42. $sourcetype = GETPOST('sourcetype', 'aZ');
  43. $search_facture = GETPOST('search_facture', 'alpha');
  44. $search_societe = GETPOST('search_societe', 'alpha');
  45. // Load variable for pagination
  46. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  47. $sortfield = GETPOST('sortfield', 'aZ09comma');
  48. $sortorder = GETPOST('sortorder', 'aZ09comma');
  49. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  50. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  51. $page = 0;
  52. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  53. $offset = $limit * $page;
  54. $pageprev = $page - 1;
  55. $pagenext = $page + 1;
  56. if (!$sortorder) {
  57. $sortorder = "DESC";
  58. }
  59. if (!$sortfield) {
  60. $sortfield = "f.ref";
  61. }
  62. $massactionbutton = '';
  63. $hookmanager->initHooks(array('withdrawalstodolist'));
  64. if ($user->socid) {
  65. $socid = $user->socid;
  66. }
  67. if ($type == 'bank-transfer') {
  68. $result = restrictedArea($user, 'paymentbybanktransfer', '', '', '');
  69. } else {
  70. $result = restrictedArea($user, 'prelevement', '', '', 'bons');
  71. }
  72. /*
  73. * Actions
  74. */
  75. $parameters = array('socid' => $socid, 'limit' => $limit, 'page' => $page, 'offset' => $offset);
  76. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  77. if ($reshook < 0) {
  78. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  79. }
  80. // Purge search criteria
  81. 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
  82. $search_facture = '';
  83. $search_societe = '';
  84. $search_array_options = array();
  85. }
  86. /*
  87. * View
  88. */
  89. if ($type != 'bank-transfer') {
  90. if (!$status) {
  91. $title = $langs->trans("RequestStandingOrderToTreat");
  92. } else {
  93. $title = $langs->trans("RequestStandingOrderTreated");
  94. }
  95. } else {
  96. if (!$status) {
  97. $title = $langs->trans("RequestPaymentsByBankTransferToTreat");
  98. } else {
  99. $title = $langs->trans("RequestPaymentsByBankTransferTreated");
  100. }
  101. }
  102. llxHeader('', $title);
  103. $thirdpartystatic = new Societe($db);
  104. if ($type == 'bank-transfer') {
  105. $invoicestatic = new FactureFournisseur($db);
  106. } else {
  107. $invoicestatic = new Facture($db);
  108. }
  109. // List of requests
  110. if ($sourcetype != 'salary') {
  111. $sql = "SELECT f.ref, f.rowid, f.total_ttc,";
  112. $sql .= " s.nom as name, s.rowid as socid,";
  113. $sql .= " pd.date_demande as date_demande, pd.amount, pd.fk_user_demande";
  114. if ($type != 'bank-transfer') {
  115. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f,";
  116. } else {
  117. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,";
  118. }
  119. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  120. $sql .= " ".MAIN_DB_PREFIX."prelevement_demande as pd";
  121. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  122. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  123. }
  124. $sql .= " WHERE s.rowid = f.fk_soc";
  125. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  126. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  127. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  128. }
  129. if ($socid) {
  130. $sql .= " AND f.fk_soc = ".((int) $socid);
  131. }
  132. if (!$status) {
  133. $sql .= " AND pd.traite = 0";
  134. }
  135. $sql .= " AND pd.ext_payment_id IS NULL";
  136. if ($status) {
  137. $sql .= " AND pd.traite = ".((int) $status);
  138. }
  139. $sql .= " AND f.total_ttc > 0";
  140. if (!getDolGlobalString('WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS')) {
  141. $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
  142. }
  143. if ($type != 'bank-transfer') {
  144. $sql .= " AND pd.fk_facture = f.rowid";
  145. } else {
  146. $sql .= " AND pd.fk_facture_fourn = f.rowid";
  147. }
  148. if ($search_facture) {
  149. $sql .= natural_search("f.ref", $search_facture);
  150. }
  151. if ($search_societe) {
  152. $sql .= natural_search("s.nom", $search_societe);
  153. }
  154. $sql .= $db->order($sortfield, $sortorder);
  155. } else {
  156. $sql = "SELECT s.rowid,s.amount as total_ttc, pd.amount,";
  157. $sql .= " s.fk_user, pd.date_demande, pd.fk_salary, CONCAT(u.firstname,' ',u.lastname) as nom";
  158. $sql .= " FROM ".MAIN_DB_PREFIX."salary as s, ".MAIN_DB_PREFIX."user as u,";
  159. $sql .= " ".MAIN_DB_PREFIX."prelevement_demande as pd";
  160. $sql .= " WHERE s.rowid = pd.fk_salary";
  161. $sql .= " AND u.rowid = s.fk_user";
  162. $sql .= " AND s.entity IN (".getEntity("salary").")";
  163. /*if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) {
  164. $sql .= " AND s.statut = ".Salary::STATUS_UNPAID;
  165. }*/
  166. if (!$status) {
  167. $sql .= " AND pd.traite = 0";
  168. }
  169. $sql .= " AND pd.ext_payment_id IS NULL";
  170. if ($status) {
  171. $sql .= " AND pd.traite = ".((int) $status);
  172. }
  173. $sql .= " AND s.amount > 0";
  174. $sql .= " AND s.paye = ".Salary::STATUS_UNPAID;
  175. if ($search_facture) {
  176. $sql .= natural_search("s.rowid", $search_facture);
  177. }
  178. if ($search_societe) {
  179. $sql .= natural_search("CONCAT(u.firstname,' ',u.lastname)", $search_societe);
  180. }
  181. //print $sql;
  182. }
  183. // Count total nb of records
  184. $nbtotalofrecords = '';
  185. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  186. $resql = $db->query($sql);
  187. $nbtotalofrecords = $db->num_rows($resql);
  188. if (($page * $limit) > $nbtotalofrecords) { // if total of record found is smaller than page * limit, goto and load page 0
  189. $page = 0;
  190. $offset = 0;
  191. }
  192. }
  193. // if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
  194. if (is_numeric($nbtotalofrecords) && $limit > $nbtotalofrecords) {
  195. $num = $nbtotalofrecords;
  196. } else {
  197. $sql .= $db->plimit($limit + 1, $offset);
  198. $resql = $db->query($sql);
  199. if (!$resql) {
  200. dol_print_error($db);
  201. exit;
  202. }
  203. $num = $db->num_rows($resql);
  204. }
  205. $newcardbutton = '<a class="marginrightonly" href="'.DOL_URL_ROOT.'/compta/prelevement/index.php">'.$langs->trans("Back").'</a>';
  206. if ($type == 'bank-transfer') {
  207. $newcardbutton = '<a class="marginrightonly" href="'.DOL_URL_ROOT.'/compta/paymentbybanktransfer/index.php">'.$langs->trans("Back").'</a>';
  208. }
  209. if ($sourcetype != 'salary') {
  210. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" id="searchFormList" name="searchFormList">';
  211. } else {
  212. print '<form action="'.$_SERVER["PHP_SELF"].'?status=0&type=bank-transfer&sourcetype='.$sourcetype.'" method="POST" id="searchFormList" name="searchFormList">';
  213. }
  214. if ($optioncss != '') {
  215. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  216. }
  217. print '<input type="hidden" name="token" value="'.newToken().'">';
  218. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  219. print '<input type="hidden" name="action" value="list">';
  220. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  221. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  222. print '<input type="hidden" name="page" value="'.$page.'">';
  223. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  224. $param = '';
  225. $label = 'NewStandingOrder';
  226. $typefilter = '';
  227. if ($type == 'bank-transfer') {
  228. $label = 'NewPaymentByBankTransfer';
  229. $typefilter = 'type='.$type;
  230. }
  231. $newcardbutton .= dolGetButtonTitle($langs->trans($label), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/compta/prelevement/create.php'.($typefilter ? '?'.$typefilter : '').($sourcetype ? '&sourcetype='.$sourcetype : ''));
  232. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'generic', 0, $newcardbutton, '', $limit);
  233. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  234. print '<table class="liste centpercent">';
  235. print '<tr class="liste_titre">';
  236. // Action column
  237. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  238. print '<td class="liste_titre maxwidthsearch">';
  239. $searchpicto = $form->showFilterButtons();
  240. print $searchpicto;
  241. print '</td>';
  242. }
  243. print '<td class="liste_titre"><input type="text" class="flat maxwidth150" name="search_facture" value="'.dol_escape_htmltag($search_facture).'"></td>';
  244. print '<td class="liste_titre"><input type="text" class="flat maxwidth150" name="search_societe" value="'.dol_escape_htmltag($search_societe).'"></td>';
  245. print '<td class="liste_titre"></td>';
  246. print '<td class="liste_titre"></td>';
  247. // Action column
  248. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  249. print '<td class="liste_titre maxwidthsearch">';
  250. $searchpicto = $form->showFilterButtons();
  251. print $searchpicto;
  252. print '</td>';
  253. }
  254. print '</tr>';
  255. print '<tr class="liste_titre">';
  256. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  257. print_liste_field_titre('');
  258. }
  259. print_liste_field_titre(($sourcetype ? "RefSalary" : "Bill"), $_SERVER["PHP_SELF"]);
  260. print_liste_field_titre(($sourcetype ? "Employee" : "Company"), $_SERVER["PHP_SELF"]);
  261. print_liste_field_titre("AmountRequested", $_SERVER["PHP_SELF"], "", "", $param, '', '', '', 'right ');
  262. print_liste_field_titre("DateRequest", $_SERVER["PHP_SELF"], "", "", $param, '', '', '', 'center ');
  263. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  264. print_liste_field_titre('');
  265. }
  266. print '</tr>';
  267. $userstatic = new User($db);
  268. $salarystatic = new Salary($db);
  269. $i = 0;
  270. while ($i < min($num, $limit)) {
  271. $obj = $db->fetch_object($resql);
  272. if (empty($obj)) {
  273. break; // Should not happen
  274. }
  275. if ($sourcetype != 'salary') {
  276. $invoicestatic->fetch($obj->rowid);
  277. } else {
  278. $salarystatic->fetch($obj->fk_salary);
  279. $userstatic->fetch($obj->fk_user);
  280. }
  281. print '<tr class="oddeven">';
  282. // Action column
  283. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  284. print '<td class="right"></td>';
  285. }
  286. // Ref facture
  287. print '<td>';
  288. if ($sourcetype != 'salary') {
  289. print $invoicestatic->getNomUrl(1, 'withdraw');
  290. } else {
  291. print $salarystatic->getNomUrl(1, 'withdraw');
  292. }
  293. print '</td>';
  294. print '<td>';
  295. if ($sourcetype != 'salary') {
  296. $thirdpartystatic->id = $obj->socid;
  297. $thirdpartystatic->name = $obj->name;
  298. print $thirdpartystatic->getNomUrl(1, 'customer');
  299. } else {
  300. print $userstatic->getNomUrl(-1, 'accountancy');
  301. }
  302. print '</td>';
  303. print '<td class="right">';
  304. print '<span class="amount">';
  305. print price($obj->amount, 1, $langs, 1, -1, -1, $conf->currency).' / '.price($obj->total_ttc, 1, $langs, 1, -1, -1, $conf->currency);
  306. print '</span>';
  307. print '</td>';
  308. print '<td class="center">'.dol_print_date($db->jdate($obj->date_demande), 'day').'</td>';
  309. // Action column
  310. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  311. print '<td class="right"></td>';
  312. }
  313. print '</tr>';
  314. $i++;
  315. }
  316. print "</table>";
  317. print "</div>";
  318. print "<br>";
  319. print '</form>';
  320. // End of page
  321. llxFooter();
  322. $db->close();