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/** 25 * \file htdocs/compta/prelevement/create.php 26 * \ingroup prelevement 27 * \brief Page to create a direct debit order or a credit transfer order 28 */ 29 30require '../../main.inc.php'; 31require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php'; 32require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; 33require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; 34require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; 35require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; 36require_once DOL_DOCUMENT_ROOT.'/core/lib/prelevement.lib.php'; 37require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; 38 39// Load translation files required by the page 40$langs->loadLangs(array('banks', 'categories', 'withdrawals', 'companies', 'bills')); 41 42// Security check 43if ($user->socid) { 44 $socid = $user->socid; 45} 46$result = restrictedArea($user, 'prelevement', '', '', 'bons'); 47 48$type = GETPOST('type', 'aZ09'); 49 50// Get supervariables 51$action = GETPOST('action', 'aZ09'); 52$massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists) 53$toselect = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list 54$mode = GETPOST('mode', 'alpha') ?GETPOST('mode', 'alpha') : 'real'; 55$format = GETPOST('format', 'aZ09'); 56$id_bankaccount = GETPOST('id_bankaccount', 'int'); 57$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit; 58$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); 59if (empty($page) || $page == -1) { 60 $page = 0; 61} // If $page is not defined, or '' or -1 62$offset = $limit * $page; 63 64$hookmanager->initHooks(array('directdebitcreatecard', 'globalcard')); 65 66 67/* 68 * Actions 69 */ 70 71if (GETPOST('cancel', 'alpha')) { 72 $massaction = ''; 73} 74 75$parameters = array('mode' => $mode, 'format' => $format, '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 77if ($reshook < 0) { 78 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); 79} 80 81if (empty($reshook)) { 82 // Change customer bank information to withdraw 83 if ($action == 'modify') { 84 for ($i = 1; $i < 9; $i++) { 85 dolibarr_set_const($db, GETPOST("nom$i"), GETPOST("value$i"), 'chaine', 0, '', $conf->entity); 86 } 87 } 88 if ($action == 'create') { 89 $default_account=($type == 'bank-transfer' ? 'PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT' : 'PRELEVEMENT_ID_BANKACCOUNT'); 90 91 if ($id_bankaccount != $conf->global->{$default_account}) { 92 $res = dolibarr_set_const($db, $default_account, $id_bankaccount, 'chaine', 0, '', $conf->entity); //Set as default 93 } 94 95 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; 96 $bank = new Account($db); 97 $bank->fetch($conf->global->{$default_account}); 98 if ((empty($bank->ics) && $type !== 'bank-transfer') 99 || (empty($bank->ics_transfer) && $type === 'bank-transfer') 100 ) { 101 $errormessage = str_replace('{url}', $bank->getNomUrl(1, '', '', -1, 1), $langs->trans("ErrorICSmissing", '{url}')); 102 setEventMessages($errormessage, null, 'errors'); 103 header("Location: ".DOL_URL_ROOT.'/compta/prelevement/create.php'); 104 exit; 105 } 106 107 108 $delayindays = 0; 109 if ($type != 'bank-transfer') { 110 $delayindays = $conf->global->PRELEVEMENT_ADDDAYS; 111 } else { 112 $delayindays = $conf->global->PAYMENTBYBANKTRANSFER_ADDDAYS; 113 } 114 $bprev = new BonPrelevement($db); 115 $executiondate = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), (GETPOST('reday', 'int') + $delayindays), GETPOST('reyear', 'int')); 116 117 // $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty (we don't use them anymore) 118 $result = $bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET, $mode, $format, $executiondate, 0, $type); 119 if ($result < 0) { 120 setEventMessages($bprev->error, $bprev->errors, 'errors'); 121 } elseif ($result == 0) { 122 $mesg = $langs->trans("NoInvoiceCouldBeWithdrawed", $format); 123 setEventMessages($mesg, null, 'errors'); 124 $mesg .= '<br>'."\n"; 125 foreach ($bprev->invoice_in_error as $key => $val) { 126 $mesg .= '<span class="warning">'.$val."</span><br>\n"; 127 } 128 } else { 129 if ($type != 'bank-transfer') { 130 $texttoshow = $langs->trans("DirectDebitOrderCreated", '{s}'); 131 $texttoshow = str_replace('{s}', $bprev->getNomUrl(1), $texttoshow); 132 setEventMessages($texttoshow, null); 133 } else { 134 $texttoshow = $langs->trans("CreditTransferOrderCreated", '{s}'); 135 $texttoshow = str_replace('{s}', $bprev->getNomUrl(1), $texttoshow); 136 setEventMessages($texttoshow, null); 137 } 138 139 header("Location: ".DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$bprev->id); 140 exit; 141 } 142 } 143 $objectclass = "BonPrelevement"; 144 $uploaddir = $conf->prelevement->dir_output; 145 include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php'; 146} 147 148 149/* 150 * View 151 */ 152 153$form = new Form($db); 154 155$thirdpartystatic = new Societe($db); 156if ($type != 'bank-transfer') { 157 $invoicestatic = new Facture($db); 158} else { 159 $invoicestatic = new FactureFournisseur($db); 160} 161$bprev = new BonPrelevement($db); 162$arrayofselected = is_array($toselect) ? $toselect : array(); 163// List of mass actions available 164$arrayofmassactions = array( 165); 166if (GETPOST('nomassaction', 'int') || in_array($massaction, array('presend', 'predelete'))) { 167 $arrayofmassactions = array(); 168} 169$massactionbutton = $form->selectMassAction('', $arrayofmassactions); 170 171llxHeader('', $langs->trans("NewStandingOrder")); 172 173if (prelevement_check_config($type) < 0) { 174 $langs->load("errors"); 175 $modulenametoshow = "Withdraw"; 176 if ($type == 'bank-transfer') { 177 $modulenametoshow = "PaymentByBankTransfer"; 178 } 179 setEventMessages($langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv($modulenametoshow)), null, 'errors'); 180} 181 182 183/*$h=0; 184$head[$h][0] = DOL_URL_ROOT.'/compta/prelevement/create.php'; 185$head[$h][1] = $langs->trans("NewStandingOrder"); 186$head[$h][2] = 'payment'; 187$hselected = 'payment'; 188$h++; 189 190print dol_get_fiche_head($head, $hselected, $langs->trans("StandingOrders"), 0, 'payment'); 191*/ 192 193$title = $langs->trans("NewStandingOrder"); 194if ($type == 'bank-transfer') { 195 $title = $langs->trans("NewPaymentByBankTransfer"); 196} 197 198print load_fiche_titre($title); 199 200print dol_get_fiche_head(); 201 202$nb = $bprev->nbOfInvoiceToPay($type); 203$pricetowithdraw = $bprev->SommeAPrelever($type); 204if ($nb < 0) { 205 dol_print_error($bprev->error); 206} 207print '<table class="border centpercent tableforfield">'; 208 209$title = $langs->trans("NbOfInvoiceToWithdraw"); 210if ($type == 'bank-transfer') { 211 $title = $langs->trans("NbOfInvoiceToPayByBankTransfer"); 212} 213 214print '<tr><td class="titlefieldcreate">'.$title.'</td>'; 215print '<td>'; 216print $nb; 217print '</td></tr>'; 218 219print '<tr><td>'.$langs->trans("AmountTotal").'</td>'; 220print '<td class="amount">'; 221print price($pricetowithdraw); 222print '</td>'; 223print '</tr>'; 224 225print '</table>'; 226print '</div>'; 227 228if ($mesg) { 229 print $mesg; 230} 231 232print '<div class="tabsAction">'."\n"; 233 234print '<form action="'.$_SERVER['PHP_SELF'].'?action=create" method="POST">'; 235print '<input type="hidden" name="token" value="'.newToken().'">'; 236print '<input type="hidden" name="type" value="'.$type.'">'; 237if ($nb) { 238 if ($pricetowithdraw) { 239 $title = $langs->trans('BankToReceiveWithdraw').': '; 240 if ($type == 'bank-transfer') { 241 $title = $langs->trans('BankToPayCreditTransfer').': '; 242 } 243 print $title; 244 print img_picto('', 'bank_account'); 245 print $form->select_comptes($conf->global->PRELEVEMENT_ID_BANKACCOUNT, 'id_bankaccount', 0, "courant=1", 0, '', 0, '', 1); 246 print ' - '; 247 248 print $langs->trans('ExecutionDate').' '; 249 $datere = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int')); 250 print $form->selectDate($datere, 're'); 251 252 253 if ($mysoc->isInEEC()) { 254 $title = $langs->trans("CreateForSepa"); 255 if ($type == 'bank-transfer') { 256 $title = $langs->trans("CreateSepaFileForPaymentByBankTransfer"); 257 } 258 259 if ($type != 'bank-transfer') { 260 print '<select name="format">'; 261 print '<option value="FRST"'.(GETPOST('format', 'aZ09') == 'FRST' ? ' selected="selected"' : '').'>'.$langs->trans('SEPAFRST').'</option>'; 262 print '<option value="RCUR"'.(GETPOST('format', 'aZ09') == 'RCUR' ? ' selected="selected"' : '').'>'.$langs->trans('SEPARCUR').'</option>'; 263 print '</select>'; 264 } 265 print '<input class="butAction" type="submit" value="'.$title.'"/>'; 266 } else { 267 $title = $langs->trans("CreateAll"); 268 if ($type == 'bank-transfer') { 269 $title = $langs->trans("CreateFileForPaymentByBankTransfer"); 270 } 271 print '<a class="butAction" type="submit" href="create.php?action=create&format=ALL&type='.$type.'">'.$title."</a>\n"; 272 } 273 } else { 274 if ($mysoc->isInEEC()) { 275 $title = $langs->trans("CreateForSepaFRST"); 276 if ($type == 'bank-transfer') { 277 $title = $langs->trans("CreateSepaFileForPaymentByBankTransfer"); 278 } 279 print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("AmountMustBePositive").'">'.$title."</a>\n"; 280 281 if ($type != 'bank-transfer') { 282 $title = $langs->trans("CreateForSepaRCUR"); 283 print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("AmountMustBePositive").'">'.$title."</a>\n"; 284 } 285 } else { 286 $title = $langs->trans("CreateAll"); 287 if ($type == 'bank-transfer') { 288 $title = $langs->trans("CreateFileForPaymentByBankTransfer"); 289 } 290 print '<a class="butActionRefused classfortooltip" href="#">'.$title."</a>\n"; 291 } 292 } 293} else { 294 $titlefortab = $langs->transnoentitiesnoconv("StandingOrders"); 295 $title = $langs->trans("CreateAll"); 296 if ($type == 'bank-transfer') { 297 $titlefortab = $langs->transnoentitiesnoconv("PaymentByBankTransfers"); 298 $title = $langs->trans("CreateFileForPaymentByBankTransfer"); 299 } 300 print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoInvoiceToWithdraw", $titlefortab, $titlefortab)).'">'.$title."</a>\n"; 301} 302 303print "</form>\n"; 304 305print "</div>\n"; 306print '</form>'; 307print '<br>'; 308 309 310/* 311 * Invoices waiting for withdraw 312 */ 313 314$sql = "SELECT f.ref, f.rowid, f.total_ttc, s.nom as name, s.rowid as socid,"; 315$sql .= " pfd.rowid as request_row_id, pfd.date_demande, pfd.amount"; 316if ($type == 'bank-transfer') { 317 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,"; 318} else { 319 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f,"; 320} 321$sql .= " ".MAIN_DB_PREFIX."societe as s,"; 322$sql .= " ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; 323$sql .= " WHERE s.rowid = f.fk_soc"; 324$sql .= " AND f.entity IN (".getEntity('invoice').")"; 325if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) { 326 $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED; 327} 328//$sql .= " AND pfd.amount > 0"; 329$sql .= " AND f.total_ttc > 0"; // Avoid credit notes 330$sql .= " AND pfd.traite = 0"; 331$sql .= " AND pfd.ext_payment_id IS NULL"; 332if ($type == 'bank-transfer') { 333 $sql .= " AND pfd.fk_facture_fourn = f.rowid"; 334} else { 335 $sql .= " AND pfd.fk_facture = f.rowid"; 336} 337if ($socid > 0) { 338 $sql .= " AND f.fk_soc = ".((int) $socid); 339} 340 341$nbtotalofrecords = ''; 342if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { 343 $result = $db->query($sql); 344 $nbtotalofrecords = $db->num_rows($result); 345 if (($page * $limit) > $nbtotalofrecords) { 346 // if total resultset is smaller then paging size (filtering), goto and load page 0 347 $page = 0; 348 $offset = 0; 349 } 350} 351 352$sql .= $db->plimit($limit + 1, $offset); 353 354$resql = $db->query($sql); 355if ($resql) { 356 $num = $db->num_rows($resql); 357 $i = 0; 358 359 $param = ''; 360 if ($limit > 0 && $limit != $conf->liste_limit) { 361 $param .= '&limit='.urlencode($limit); 362 } 363 if ($socid) { 364 $param .= '&socid='.urlencode($socid); 365 } 366 if ($option) { 367 $param .= "&option=".urlencode($option); 368 } 369 370 print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'; 371 print '<input type="hidden" name="token" value="'.newToken().'">'; 372 print '<input type="hidden" name="page" value="'.$page.'">'; 373 if (!empty($limit)) { 374 print '<input type="hidden" name="limit" value="'.$limit.'"/>'; 375 } 376 377 $title = $langs->trans("InvoiceWaitingWithdraw"); 378 if ($type == 'bank-transfer') { 379 $title = $langs->trans("InvoiceWaitingPaymentByBankTransfer"); 380 } 381 print_barre_liste($title, $page, $_SERVER['PHP_SELF'], $param, '', '', $massactionbutton, $num, $nbtotalofrecords, 'bill', 0, '', '', $limit); 382 383 $tradinvoice = "Invoice"; 384 if ($type == 'bank-transfer') { 385 $tradinvoice = "SupplierInvoice"; 386 } 387 388 print '<table class="noborder centpercent">'; 389 print '<tr class="liste_titre">'; 390 print '<td>'.$langs->trans($tradinvoice).'</td>'; 391 print '<td>'.$langs->trans("ThirdParty").'</td>'; 392 print '<td>'.$langs->trans("RIB").'</td>'; 393 print '<td>'.$langs->trans("RUM").'</td>'; 394 print '<td class="right">'.$langs->trans("AmountTTC").'</td>'; 395 print '<td class="right">'.$langs->trans("DateRequest").'</td>'; 396 if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined 397 print '<td align="center">'.$form->showCheckAddButtons('checkforselect', 1).'</td>'; 398 } 399 print '</tr>'; 400 401 if ($num) { 402 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php'; 403 $bac = new CompanyBankAccount($db); 404 405 while ($i < $num && $i < $limit) { 406 $obj = $db->fetch_object($resql); 407 408 $bac->fetch(0, $obj->socid); 409 410 print '<tr class="oddeven">'; 411 412 // Ref invoice 413 print '<td>'; 414 $invoicestatic->id = $obj->rowid; 415 $invoicestatic->ref = $obj->ref; 416 print $invoicestatic->getNomUrl(1, 'withdraw'); 417 print '</td>'; 418 419 // Thirdparty 420 print '<td>'; 421 $thirdpartystatic->fetch($obj->socid); 422 print $thirdpartystatic->getNomUrl(1, 'ban'); 423 print '</td>'; 424 425 // RIB 426 print '<td>'; 427 print $bac->iban.(($bac->iban && $bac->bic) ? ' / ' : '').$bac->bic; 428 if ($bac->verif() <= 0) { 429 print img_warning('Error on default bank number for IBAN : '.$bac->error_message); 430 } 431 print '</td>'; 432 433 // RUM 434 print '<td>'; 435 $rumtoshow = $thirdpartystatic->display_rib('rum'); 436 if ($rumtoshow) { 437 print $rumtoshow; 438 $format = $thirdpartystatic->display_rib('format'); 439 if ($type != 'bank-transfer') { 440 if ($format) { 441 print ' ('.$format.')'; 442 } 443 } 444 } else { 445 print img_warning($langs->trans("NoBankAccountDefined")); 446 } 447 print '</td>'; 448 // Amount 449 print '<td class="right amount">'; 450 print price($obj->amount, 0, $langs, 0, 0, -1, $conf->currency); 451 print '</td>'; 452 // Date 453 print '<td class="right">'; 454 print dol_print_date($db->jdate($obj->date_demande), 'day'); 455 print '</td>'; 456 // Action column 457 if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined 458 print '<td class="nowrap center">'; 459 $selected = 0; 460 if (in_array($obj->request_row_id, $arrayofselected)) { 461 $selected = 1; 462 } 463 print '<input id="cb'.$obj->request_row_id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->request_row_id.'"'.($selected ? ' checked="checked"' : '').'>'; 464 print '</td>'; 465 } 466 print '</tr>'; 467 $i++; 468 } 469 } else { 470 print '<tr class="oddeven"><td colspan="6"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>'; 471 } 472 print "</table>"; 473 print "</form>"; 474 print "<br>\n"; 475} else { 476 dol_print_error($db); 477} 478 479 480/* 481 * List of latest withdraws 482 */ 483/* 484$limit=5; 485 486print load_fiche_titre($langs->trans("LastWithdrawalReceipts",$limit),'',''); 487 488$sql = "SELECT p.rowid, p.ref, p.amount, p.statut"; 489$sql.= ", p.datec"; 490$sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p"; 491$sql.= " WHERE p.entity IN (".getEntity('invoice').")"; 492$sql.= " ORDER BY datec DESC"; 493$sql.=$db->plimit($limit); 494 495$result = $db->query($sql); 496if ($result) 497{ 498 $num = $db->num_rows($result); 499 $i = 0; 500 501 print"\n<!-- debut table -->\n"; 502 print '<table class="noborder centpercent">'; 503 print '<tr class="liste_titre"><td>'.$langs->trans("Ref").'</td>'; 504 print '<td class="center">'.$langs->trans("Date").'</td><td class="right">'.$langs->trans("Amount").'</td>'; 505 print '</tr>'; 506 507 while ($i < min($num,$limit)) 508 { 509 $obj = $db->fetch_object($result); 510 511 512 print '<tr class="oddeven">'; 513 514 print "<td>"; 515 $bprev->id=$obj->rowid; 516 $bprev->ref=$obj->ref; 517 print $bprev->getNomUrl(1); 518 print "</td>\n"; 519 520 print '<td class="center">'.dol_print_date($db->jdate($obj->datec),'day')."</td>\n"; 521 522 print '<td class="right"><span class="amount">'.price($obj->amount,0,$langs,0,0,-1,$conf->currency)."</span></td>\n"; 523 524 print "</tr>\n"; 525 $i++; 526 } 527 print "</table><br>"; 528 $db->free($result); 529} 530else 531{ 532 dol_print_error($db); 533} 534*/ 535 536// End of page 537llxFooter(); 538$db->close(); 539