create.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2010-2020 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. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. * Copyright (C) 2019 Markus Welters <markus@welters.de>
  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. */
  23. /**
  24. * \file htdocs/compta/prelevement/create.php
  25. * \ingroup prelevement
  26. * \brief Page to create a direct debit order or a credit transfer order
  27. */
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/prelevement.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  36. // Load translation files required by the page
  37. $langs->loadLangs(array('banks', 'categories', 'withdrawals', 'companies', 'bills'));
  38. // Security check
  39. if ($user->socid) $socid = $user->socid;
  40. $result = restrictedArea($user, 'prelevement', '', '', 'bons');
  41. $type = GETPOST('type', 'aZ09');
  42. // Get supervariables
  43. $action = GETPOST('action', 'alpha');
  44. $mode = GETPOST('mode', 'alpha') ?GETPOST('mode', 'alpha') : 'real';
  45. $format = GETPOST('format', 'aZ09');
  46. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  47. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  48. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  49. $offset = $limit * $page;
  50. $hookmanager->initHooks(array('directdebitcreatecard', 'globalcard'));
  51. /*
  52. * Actions
  53. */
  54. $parameters = array('mode' => $mode, 'format' => $format, 'limit' => $limit, 'page' => $page, 'offset' => $offset);
  55. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  56. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  57. if (empty($reshook))
  58. {
  59. // Change customer bank information to withdraw
  60. if ($action == 'modify')
  61. {
  62. for ($i = 1; $i < 9; $i++)
  63. {
  64. dolibarr_set_const($db, GETPOST("nom$i"), GETPOST("value$i"), 'chaine', 0, '', $conf->entity);
  65. }
  66. }
  67. if ($action == 'create')
  68. {
  69. $delayindays = 0;
  70. if ($type != 'bank-transfer') {
  71. $conf->global->PRELEVEMENT_ADDDAYS;
  72. } else {
  73. $conf->global->PAYMENTBYBANKTRANSFER_ADDDAYS;
  74. }
  75. $bprev = new BonPrelevement($db);
  76. $executiondate = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), (GETPOST('reday', 'int') + $delayindays), GETPOST('reyear', 'int'));
  77. // $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty (we don't use them anymore)
  78. $result = $bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET, $mode, $format, $executiondate, 0, $type);
  79. if ($result < 0) {
  80. setEventMessages($bprev->error, $bprev->errors, 'errors');
  81. } elseif ($result == 0) {
  82. $mesg = $langs->trans("NoInvoiceCouldBeWithdrawed", $format);
  83. setEventMessages($mesg, null, 'errors');
  84. $mesg .= '<br>'."\n";
  85. foreach ($bprev->invoice_in_error as $key => $val)
  86. {
  87. $mesg .= '<span class="warning">'.$val."</span><br>\n";
  88. }
  89. } else {
  90. if ($type != 'bank-transfer') {
  91. setEventMessages($langs->trans("DirectDebitOrderCreated", $bprev->getNomUrl(1)), null);
  92. } else {
  93. setEventMessages($langs->trans("CreditTransferOrderCreated", $bprev->getNomUrl(1)), null);
  94. }
  95. header("Location: ".DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$bprev->id);
  96. exit;
  97. }
  98. }
  99. }
  100. /*
  101. * View
  102. */
  103. $form = new Form($db);
  104. $thirdpartystatic = new Societe($db);
  105. if ($type != 'bank-transfer') {
  106. $invoicestatic = new Facture($db);
  107. } else {
  108. $invoicestatic = new FactureFournisseur($db);
  109. }
  110. $bprev = new BonPrelevement($db);
  111. llxHeader('', $langs->trans("NewStandingOrder"));
  112. if (prelevement_check_config() < 0)
  113. {
  114. $langs->load("errors");
  115. setEventMessages($langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Withdraw")), null, 'errors');
  116. }
  117. /*$h=0;
  118. $head[$h][0] = DOL_URL_ROOT.'/compta/prelevement/create.php';
  119. $head[$h][1] = $langs->trans("NewStandingOrder");
  120. $head[$h][2] = 'payment';
  121. $hselected = 'payment';
  122. $h++;
  123. dol_fiche_head($head, $hselected, $langs->trans("StandingOrders"), 0, 'payment');
  124. */
  125. $title = $langs->trans("NewStandingOrder");
  126. if ($type == 'bank-transfer') {
  127. $title = $langs->trans("NewPaymentByBankTransfer");
  128. }
  129. print load_fiche_titre($title);
  130. dol_fiche_head();
  131. $nb = $bprev->nbOfInvoiceToPay($type);
  132. $pricetowithdraw = $bprev->SommeAPrelever($type);
  133. if ($nb < 0)
  134. {
  135. dol_print_error($bprev->error);
  136. }
  137. print '<table class="border centpercent tableforfield">';
  138. $title = $langs->trans("NbOfInvoiceToWithdraw");
  139. if ($type == 'bank-transfer') {
  140. $title = $langs->trans("NbOfInvoiceToPayByBankTransfer");
  141. }
  142. print '<tr><td class="titlefieldcreate">'.$title.'</td>';
  143. print '<td>';
  144. print $nb;
  145. print '</td></tr>';
  146. print '<tr><td>'.$langs->trans("AmountTotal").'</td>';
  147. print '<td>';
  148. print price($pricetowithdraw);
  149. print '</td>';
  150. print '</tr>';
  151. print '</table>';
  152. print '</div>';
  153. if ($mesg) print $mesg;
  154. print '<div class="tabsAction">'."\n";
  155. print '<form action="'.$_SERVER['PHP_SELF'].'?action=create" method="POST">';
  156. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  157. print '<input type="hidden" name="type" value="'.$type.'">';
  158. if ($nb) {
  159. if ($pricetowithdraw) {
  160. print $langs->trans('ExecutionDate').' ';
  161. $datere = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  162. print $form->selectDate($datere, 're');
  163. if ($mysoc->isInEEC()) {
  164. $title = $langs->trans("CreateForSepa");
  165. if ($type == 'bank-transfer') {
  166. $title = $langs->trans("CreateSepaFileForPaymentByBankTransfer");
  167. }
  168. if ($type != 'bank-transfer') {
  169. print '<select name="format">';
  170. print '<option value="FRST"'.(GETPOST('format', 'aZ09') == 'FRST' ? ' selected="selected"' : '').'>'.$langs->trans('SEPAFRST').'</option>';
  171. print '<option value="RCUR"'.(GETPOST('format', 'aZ09') == 'RCUR' ? ' selected="selected"' : '').'>'.$langs->trans('SEPARCUR').'</option>';
  172. print '</select>';
  173. }
  174. print '<input class="butAction" type="submit" value="'.$title.'"/>';
  175. } else {
  176. $title = $langs->trans("CreateAll");
  177. if ($type == 'bank-transfer') {
  178. $title = $langs->trans("CreateFileForPaymentByBankTransfer");
  179. }
  180. print '<a class="butAction" type="submit" href="create.php?action=create&format=ALL&type='.$type.'">'.$title."</a>\n";
  181. }
  182. } else {
  183. if ($mysoc->isInEEC())
  184. {
  185. $title = $langs->trans("CreateForSepaFRST");
  186. if ($type == 'bank-transfer') {
  187. $title = $langs->trans("CreateSepaFileForPaymentByBankTransfer");
  188. }
  189. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("AmountMustBePositive").'">'.$title."</a>\n";
  190. if ($type != 'bank-transfer') {
  191. $title = $langs->trans("CreateForSepaRCUR");
  192. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("AmountMustBePositive").'">'.$title."</a>\n";
  193. }
  194. } else {
  195. $title = $langs->trans("CreateAll");
  196. if ($type == 'bank-transfer') {
  197. $title = $langs->trans("CreateFileForPaymentByBankTransfer");
  198. }
  199. print '<a class="butActionRefused classfortooltip" href="#">'.$title."</a>\n";
  200. }
  201. }
  202. } else {
  203. $titlefortab = $langs->transnoentitiesnoconv("StandingOrders");
  204. $title = $langs->trans("CreateAll");
  205. if ($type == 'bank-transfer') {
  206. $titlefortab = $langs->transnoentitiesnoconv("PaymentByBankTransfers");
  207. $title = $langs->trans("CreateFileForPaymentByBankTransfer");
  208. }
  209. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoInvoiceToWithdraw", $titlefortab, $titlefortab)).'">'.$title."</a>\n";
  210. }
  211. print "</form>\n";
  212. print "</div>\n";
  213. print '</form>';
  214. print '<br>';
  215. /*
  216. * Invoices waiting for withdraw
  217. */
  218. $sql = "SELECT f.ref, f.rowid, f.total_ttc, s.nom as name, s.rowid as socid,";
  219. $sql .= " pfd.date_demande, pfd.amount";
  220. if ($type == 'bank-transfer') {
  221. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,";
  222. } else {
  223. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f,";
  224. }
  225. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  226. $sql .= " ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  227. $sql .= " WHERE s.rowid = f.fk_soc";
  228. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  229. if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS))
  230. {
  231. $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
  232. }
  233. //$sql .= " AND pfd.amount > 0";
  234. $sql .= " AND f.total_ttc > 0"; // Avoid credit notes
  235. $sql .= " AND pfd.traite = 0";
  236. $sql .= " AND pfd.ext_payment_id IS NULL";
  237. if ($type == 'bank-transfer') {
  238. $sql .= " AND pfd.fk_facture_fourn = f.rowid";
  239. } else {
  240. $sql .= " AND pfd.fk_facture = f.rowid";
  241. }
  242. if ($socid > 0) $sql .= " AND f.fk_soc = ".$socid;
  243. $nbtotalofrecords = '';
  244. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  245. {
  246. $result = $db->query($sql);
  247. $nbtotalofrecords = $db->num_rows($result);
  248. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  249. {
  250. $page = 0;
  251. $offset = 0;
  252. }
  253. }
  254. $sql .= $db->plimit($limit + 1, $offset);
  255. $resql = $db->query($sql);
  256. if ($resql)
  257. {
  258. $num = $db->num_rows($resql);
  259. $i = 0;
  260. $param = '';
  261. if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.urlencode($limit);
  262. if ($socid) $param .= '&socid='.urlencode($socid);
  263. if ($option) $param .= "&option=".urlencode($option);
  264. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  265. print '<input type="hidden" name="token" value="'.newToken().'">';
  266. print '<input type="hidden" name="page" value="'.$page.'">';
  267. if (!empty($limit)) {
  268. print '<input type="hidden" name="limit" value="'.$limit.'"/>';
  269. }
  270. $title = $langs->trans("InvoiceWaitingWithdraw");
  271. if ($type == 'bank-transfer') {
  272. $title = $langs->trans("InvoiceWaitingPaymentByBankTransfer");
  273. }
  274. print_barre_liste($title, $page, $_SERVER['PHP_SELF'], $param, '', '', '', $num, $nbtotalofrecords, 'bill', 0, '', '', $limit);
  275. $tradinvoice = "Invoice";
  276. if ($type == 'bank-transfer') {
  277. $tradinvoice = "SupplierInvoice";
  278. }
  279. print '<table class="noborder centpercent">';
  280. print '<tr class="liste_titre">';
  281. print '<td>'.$langs->trans($tradinvoice).'</td>';
  282. print '<td>'.$langs->trans("ThirdParty").'</td>';
  283. print '<td>'.$langs->trans("RIB").'</td>';
  284. print '<td>'.$langs->trans("RUM").'</td>';
  285. print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
  286. print '<td class="right">'.$langs->trans("DateRequest").'</td>';
  287. print '</tr>';
  288. if ($num)
  289. {
  290. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  291. $bac = new CompanyBankAccount($db);
  292. while ($i < $num && $i < $limit)
  293. {
  294. $obj = $db->fetch_object($resql);
  295. $bac->fetch(0, $obj->socid);
  296. print '<tr class="oddeven">';
  297. // Ref invoice
  298. print '<td>';
  299. $invoicestatic->id = $obj->rowid;
  300. $invoicestatic->ref = $obj->ref;
  301. print $invoicestatic->getNomUrl(1, 'withdraw');
  302. print '</td>';
  303. // Thirdparty
  304. print '<td>';
  305. $thirdpartystatic->fetch($obj->socid);
  306. print $thirdpartystatic->getNomUrl(1, 'ban');
  307. print '</td>';
  308. // RIB
  309. print '<td>';
  310. print $bac->iban.(($bac->iban && $bac->bic) ? ' / ' : '').$bac->bic;
  311. if ($bac->verif() <= 0) print img_warning('Error on default bank number for IBAN : '.$bac->error_message);
  312. print '</td>';
  313. // RUM
  314. print '<td>';
  315. $rumtoshow = $thirdpartystatic->display_rib('rum');
  316. if ($rumtoshow) {
  317. print $rumtoshow;
  318. $format = $thirdpartystatic->display_rib('format');
  319. if ($type != 'bank-transfer') {
  320. if ($format) print ' ('.$format.')';
  321. }
  322. } else {
  323. print img_warning($langs->trans("NoBankAccount"));
  324. }
  325. print '</td>';
  326. // Amount
  327. print '<td class="right">';
  328. print price($obj->amount, 0, $langs, 0, 0, -1, $conf->currency);
  329. print '</td>';
  330. // Date
  331. print '<td class="right">';
  332. print dol_print_date($db->jdate($obj->date_demande), 'day');
  333. print '</td>';
  334. print '</tr>';
  335. $i++;
  336. }
  337. } else {
  338. print '<tr class="oddeven"><td colspan="6"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  339. }
  340. print "</table>";
  341. print "</form>";
  342. print "<br>\n";
  343. } else {
  344. dol_print_error($db);
  345. }
  346. /*
  347. * List of latest withdraws
  348. */
  349. /*
  350. $limit=5;
  351. print load_fiche_titre($langs->trans("LastWithdrawalReceipts",$limit),'','');
  352. $sql = "SELECT p.rowid, p.ref, p.amount, p.statut";
  353. $sql.= ", p.datec";
  354. $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  355. $sql.= " WHERE p.entity IN (".getEntity('invoice').")";
  356. $sql.= " ORDER BY datec DESC";
  357. $sql.=$db->plimit($limit);
  358. $result = $db->query($sql);
  359. if ($result)
  360. {
  361. $num = $db->num_rows($result);
  362. $i = 0;
  363. print"\n<!-- debut table -->\n";
  364. print '<table class="noborder centpercent">';
  365. print '<tr class="liste_titre"><td>'.$langs->trans("Ref").'</td>';
  366. print '<td class="center">'.$langs->trans("Date").'</td><td class="right">'.$langs->trans("Amount").'</td>';
  367. print '</tr>';
  368. while ($i < min($num,$limit))
  369. {
  370. $obj = $db->fetch_object($result);
  371. print '<tr class="oddeven">';
  372. print "<td>";
  373. $bprev->id=$obj->rowid;
  374. $bprev->ref=$obj->ref;
  375. print $bprev->getNomUrl(1);
  376. print "</td>\n";
  377. print '<td class="center">'.dol_print_date($db->jdate($obj->datec),'day')."</td>\n";
  378. print '<td class="right">'.price($obj->amount,0,$langs,0,0,-1,$conf->currency)."</td>\n";
  379. print "</tr>\n";
  380. $i++;
  381. }
  382. print "</table><br>";
  383. $db->free($result);
  384. }
  385. else
  386. {
  387. dol_print_error($db);
  388. }
  389. */
  390. // End of page
  391. llxFooter();
  392. $db->close();