factures.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2010-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/factures.php
  22. * \ingroup prelevement
  23. * \brief Page list of invoice paied by direct debit or credit transfer
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/prelevement.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/rejetprelevement.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('banks', 'categories', 'bills', 'companies', 'withdrawals', 'salaries', 'suppliers'));
  36. // Get supervariables
  37. $id = GETPOST('id', 'int');
  38. $ref = GETPOST('ref', 'alpha');
  39. $socid = GETPOST('socid', 'int');
  40. $userid = GETPOST('userid', 'int');
  41. $type = GETPOST('type', 'aZ09');
  42. // Load variable for pagination
  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 (!$sortfield) {
  54. $sortfield = 'p.ref';
  55. }
  56. if (!$sortorder) {
  57. $sortorder = 'DESC';
  58. }
  59. $object = new BonPrelevement($db);
  60. // Load object
  61. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals
  62. $hookmanager->initHooks(array('directdebitprevcard', 'globalcard', 'directdebitprevlist'));
  63. // Security check
  64. if ($user->socid > 0) {
  65. accessforbidden();
  66. }
  67. $type = $object->type;
  68. $salaryBonPl = $object->checkIfSalaryBonPrelevement();
  69. if ($type == 'bank-transfer') {
  70. $result = restrictedArea($user, 'paymentbybanktransfer', '', '', '');
  71. } else {
  72. $result = restrictedArea($user, 'prelevement', '', '', 'bons');
  73. }
  74. /*
  75. * View
  76. */
  77. $form = new Form($db);
  78. $invoicetmp = new Facture($db);
  79. $thirdpartytmp = new Societe($db);
  80. llxHeader('', $langs->trans("WithdrawalsReceipts"));
  81. if ($id > 0 || $ref) {
  82. if ($object->fetch($id, $ref) >= 0) {
  83. $head = prelevement_prepare_head($object);
  84. print dol_get_fiche_head($head, 'invoices', $langs->trans("WithdrawalsReceipts"), -1, 'payment');
  85. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/prelevement/orders_list.php?restore_lastsearch_values=1'.($object->type != 'bank-transfer' ? '' : '&type=bank-transfer').'">'.$langs->trans("BackToList").'</a>';
  86. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref');
  87. print '<div class="fichecenter">';
  88. print '<div class="underbanner clearboth"></div>';
  89. print '<table class="border centpercent tableforfield">'."\n";
  90. //print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td>'.$object->getNomUrl(1).'</td></tr>';
  91. print '<tr><td class="titlefieldcreate">'.$langs->trans("Date").'</td><td>'.dol_print_date($object->datec, 'day').'</td></tr>';
  92. print '<tr><td>'.$langs->trans("Amount").'</td><td><span class="amount">'.price($object->amount).'</span></td></tr>';
  93. if (!empty($object->date_trans)) {
  94. $muser = new User($db);
  95. $muser->fetch($object->user_trans);
  96. print '<tr><td>'.$langs->trans("TransData").'</td><td>';
  97. print dol_print_date($object->date_trans, 'day');
  98. print ' &nbsp; <span class="opacitymedium">'.$langs->trans("By").'</span> '.$muser->getNomUrl(-1).'</td></tr>';
  99. print '<tr><td>'.$langs->trans("TransMetod").'</td><td>';
  100. print $object->methodes_trans[$object->method_trans];
  101. print '</td></tr>';
  102. }
  103. if (!empty($object->date_credit)) {
  104. print '<tr><td>'.$langs->trans('CreditDate').'</td><td>';
  105. print dol_print_date($object->date_credit, 'day');
  106. print '</td></tr>';
  107. }
  108. print '</table>';
  109. print '<br>';
  110. print '<div class="underbanner clearboth"></div>';
  111. print '<table class="border centpercent tableforfield">';
  112. // Get bank account for the payment
  113. $acc = new Account($db);
  114. $fk_bank_account = $object->fk_bank_account;
  115. if (empty($fk_bank_account)) {
  116. $fk_bank_account = ($object->type == 'bank-transfer' ? getDolGlobalInt('PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT') : getDolGlobalInt('PRELEVEMENT_ID_BANKACCOUNT'));
  117. }
  118. if ($fk_bank_account > 0) {
  119. $result = $acc->fetch($fk_bank_account);
  120. }
  121. $labelofbankfield = "BankToReceiveWithdraw";
  122. if ($object->type == 'bank-transfer') {
  123. $labelofbankfield = 'BankToPayCreditTransfer';
  124. }
  125. print '<tr><td class="titlefieldcreate">';
  126. print $form->textwithpicto($langs->trans("BankAccount"), $langs->trans($labelofbankfield));
  127. print '</td>';
  128. print '<td>';
  129. if ($acc->id > 0) {
  130. print $acc->getNomUrl(1);
  131. }
  132. print '</td>';
  133. print '</tr>';
  134. $modulepart = 'prelevement';
  135. if ($object->type == 'bank-transfer') {
  136. $modulepart = 'paymentbybanktransfer';
  137. }
  138. print '<tr><td class="titlefieldcreate">';
  139. $labelfororderfield = 'WithdrawalFile';
  140. if ($object->type == 'bank-transfer') {
  141. $labelfororderfield = 'CreditTransferFile';
  142. }
  143. print $langs->trans($labelfororderfield).'</td><td>';
  144. if (isModEnabled('multicompany')) {
  145. $labelentity = $conf->entity;
  146. $relativepath = 'receipts/'.$object->ref.'-'.$labelentity.'.xml';
  147. if ($type != 'bank-transfer') {
  148. $dir = $conf->prelevement->dir_output;
  149. } else {
  150. $dir = $conf->paymentbybanktransfer->dir_output;
  151. }
  152. if (!dol_is_file($dir.'/'.$relativepath)) { // For backward compatibility
  153. $relativepath = 'receipts/'.$object->ref.'.xml';
  154. }
  155. } else {
  156. $relativepath = 'receipts/'.$object->ref.'.xml';
  157. }
  158. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?type=text/plain&amp;modulepart='.$modulepart.'&amp;file='.urlencode($relativepath).'">'.$relativepath;
  159. print img_picto('', 'download', 'class="paddingleft"');
  160. print '</a>';
  161. print '</td></tr></table>';
  162. print '</div>';
  163. print dol_get_fiche_end();
  164. } else {
  165. dol_print_error($db);
  166. }
  167. }
  168. // List of invoices or salaries
  169. if ($salaryBonPl) {
  170. $sql = "SELECT pf.rowid, p.type,";
  171. $sql .= " f.rowid as salaryid, f.ref as ref, f.amount,";
  172. $sql .= " u.rowid as userid, u.firstname, u.lastname, pl.statut as status, pl.amount as amount_requested";
  173. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  174. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON pl.fk_prelevement_bons = p.rowid";
  175. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."prelevement as pf ON pf.fk_prelevement_lignes = pl.rowid";
  176. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."salary as f ON f.rowid = pf.fk_salary AND f.entity IN (".getEntity('salary').")";
  177. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON f.fk_user = u.rowid";
  178. $sql .= " WHERE 1 = 1";
  179. if ($object->id > 0) {
  180. $sql .= " AND p.rowid = ".((int) $object->id);
  181. }
  182. if ($userid > 0) {
  183. $sql .= " AND u.rowid = ".((int) $userid);
  184. }
  185. } else {
  186. $sql = "SELECT pf.rowid, p.type,";
  187. $sql .= " f.rowid as facid, f.ref as ref, f.total_ttc,";
  188. if ($object->type == 'bank-transfer') {
  189. $sql .= " f.ref_supplier,";
  190. }
  191. $sql .= " s.rowid as socid, s.nom as name, pl.statut as status, pl.amount as amount_requested";
  192. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p,";
  193. $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
  194. $sql .= " ".MAIN_DB_PREFIX."prelevement as pf";
  195. if ($object->type != 'bank-transfer') {
  196. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  197. } else {
  198. $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  199. }
  200. $sql .= ", ".MAIN_DB_PREFIX."societe as s";
  201. $sql .= " WHERE pf.fk_prelevement_lignes = pl.rowid";
  202. $sql .= " AND pl.fk_prelevement_bons = p.rowid";
  203. $sql .= " AND f.fk_soc = s.rowid";
  204. if ($object->type != 'bank-transfer') {
  205. $sql .= " AND pf.fk_facture = f.rowid";
  206. } else {
  207. $sql .= " AND pf.fk_facture_fourn = f.rowid";
  208. }
  209. if ($object->type != 'bank-transfer') {
  210. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  211. } else {
  212. $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")";
  213. }
  214. if ($object->id > 0) {
  215. $sql .= " AND p.rowid = ".((int) $object->id);
  216. }
  217. if ($socid > 0) {
  218. $sql .= " AND s.rowid = ".((int) $socid);
  219. }
  220. $sql .= $db->order($sortfield, $sortorder);
  221. }
  222. // Count total nb of records
  223. $nbtotalofrecords = '';
  224. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  225. $resql = $db->query($sql);
  226. $nbtotalofrecords = $db->num_rows($resql);
  227. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  228. $page = 0;
  229. $offset = 0;
  230. }
  231. }
  232. $sql .= $db->plimit($limit + 1, $offset);
  233. $resql = $db->query($sql);
  234. if ($resql) {
  235. $num = $db->num_rows($resql);
  236. $i = 0;
  237. if ($limit > 0 && $limit != $conf->liste_limit) {
  238. $param.='&limit='.((int) $limit);
  239. }
  240. $param = "&id=".urlencode($id);
  241. // Lines of title fields
  242. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  243. if ($optioncss != '') {
  244. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  245. }
  246. print '<input type="hidden" name="token" value="'.newToken().'">';
  247. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  248. print '<input type="hidden" name="action" value="list">';
  249. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  250. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  251. print '<input type="hidden" name="page" value="'.$page.'">';
  252. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  253. print '<input type="hidden" name="id" value="'.$id.'">';
  254. $massactionbutton = '';
  255. $title = ($salaryBonPl ? $langs->trans("Salaries") : ($object->type == 'bank-transfer' ? $langs->trans("SupplierInvoices") : $langs->trans("Invoices")));
  256. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, '', '', $limit);
  257. print"\n<!-- debut table -->\n";
  258. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  259. print '<table class="liste centpercent">';
  260. print '<tr class="liste_titre">';
  261. print_liste_field_titre(($salaryBonPl ? "Salary" : "Bill"), $_SERVER["PHP_SELF"], "p.ref", '', $param, '', $sortfield, $sortorder);
  262. if ($object->type == 'bank-transfer' && !$salaryBonPl) {
  263. print_liste_field_titre("RefSupplierShort", $_SERVER["PHP_SELF"], "f.ref_supplier", '', $param, '', $sortfield, $sortorder);
  264. }
  265. print_liste_field_titre(($salaryBonPl ? "Employee" : "ThirdParty"), $_SERVER["PHP_SELF"], "s.nom", '', $param, '', $sortfield, $sortorder);
  266. print_liste_field_titre(($salaryBonPl ? "AmountSalary" : "AmountInvoice"), $_SERVER["PHP_SELF"], "f.total_ttc", "", $param, 'class="right"', $sortfield, $sortorder);
  267. print_liste_field_titre("AmountRequested", $_SERVER["PHP_SELF"], "pl.amount", "", $param, 'class="right"', $sortfield, $sortorder);
  268. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "", "", $param, 'align="center"', $sortfield, $sortorder);
  269. print "</tr>\n";
  270. $totalinvoices = 0;
  271. $totalamount_requested = 0;
  272. if ($salaryBonPl) {
  273. $salarytmp = new Salary($db);
  274. $usertmp = new User($db);
  275. } else {
  276. $invoicetmpcustomer = new Facture($db);
  277. $invoicetmpsupplier = new FactureFournisseur($db);
  278. }
  279. while ($i < min($num, $limit)) {
  280. $obj = $db->fetch_object($resql);
  281. if ($salaryBonPl) {
  282. $salarytmp->fetch($obj->salaryid);
  283. $usertmp->fetch($obj->userid);
  284. } else {
  285. if ($obj->type == 'bank-transfer') {
  286. $invoicetmp = $invoicetmpsupplier;
  287. } else {
  288. $invoicetmp = $invoicetmpcustomer;
  289. }
  290. $invoicetmp->fetch($obj->facid);
  291. $thirdpartytmp->fetch($obj->socid);
  292. }
  293. print '<tr class="oddeven">';
  294. print '<td class="nowraponall">';
  295. print ($salaryBonPl ? $salarytmp->getNomUrl(1) : $invoicetmp->getNomUrl(1));
  296. print "</td>\n";
  297. if ($object->type == 'bank-transfer' && !$salaryBonPl) {
  298. print '<td class="tdoverflowmax150" title="'.dol_escape_htmltag($invoicetmp->ref_supplier).'">';
  299. print dol_escape_htmltag($invoicetmp->ref_supplier);
  300. print "</td>\n";
  301. }
  302. print '<td class="tdoverflowmax125">';
  303. print($salaryBonPl ? $usertmp->getNomUrl(-1) : $thirdpartytmp->getNomUrl(1));
  304. print "</td>\n";
  305. // Amount of invoice
  306. print '<td class="right"><span class="amount">'.price(($salaryBonPl ? $obj->amount : $obj->total_ttc))."</span></td>\n";
  307. // Amount requested
  308. print '<td class="right"><span class="amount">'.price($obj->amount_requested)."</span></td>\n";
  309. // Status of requests
  310. print '<td class="center">';
  311. if ($obj->status == 0) {
  312. print $langs->trans("StatusWaiting");
  313. } elseif ($obj->status == 2) {
  314. if ($obj->type == 'bank-transfer') {
  315. print $langs->trans("StatusDebited");
  316. } else {
  317. print $langs->trans("StatusCredited");
  318. }
  319. } elseif ($obj->status == 3) {
  320. print '<span class="error">'.$langs->trans("StatusRefused").'</span>';
  321. }
  322. print "</td>";
  323. print "</tr>\n";
  324. $totalinvoices += $obj->total_ttc;
  325. $totalamount_requested += $obj->amount_requested;
  326. $i++;
  327. }
  328. if ($num > 0) {
  329. print '<tr class="liste_total">';
  330. print '<td>'.$langs->trans("Total").'</td>';
  331. if ($object->type == 'bank-transfer' && !$salaryBonPl) {
  332. print '<td>&nbsp;</td>';
  333. }
  334. print '<td>&nbsp;</td>';
  335. print '<td class="right">';
  336. print "</td>\n";
  337. print '<td class="right">';
  338. // If the page show all record (no pagination) and total does not match total of file, we show a warning. Should not happen.
  339. if (($nbtotalofrecords <= $num) && $totalamount_requested != $object->amount) {
  340. print img_warning("AmountOfFileDiffersFromSumOfInvoices");
  341. }
  342. print price($totalamount_requested);
  343. print "</td>\n";
  344. print '<td>&nbsp;</td>';
  345. print "</tr>\n";
  346. }
  347. print "</table>";
  348. print '</div>';
  349. $db->free($resql);
  350. } else {
  351. dol_print_error($db);
  352. }
  353. // End of page
  354. llxFooter();
  355. $db->close();