card.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/paiement/card.php
  24. * \ingroup facture
  25. * \brief Page of a customer payment
  26. * \remarks Nearly same file than fournisseur/paiement/card.php
  27. */
  28. // Load Dolibarr environment
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  34. if (isModEnabled("banque")) {
  35. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  36. }
  37. // Load translation files required by the page
  38. $langs->loadLangs(array('bills', 'banks', 'companies'));
  39. $id = GETPOST('id', 'int');
  40. $ref = GETPOST('ref', 'alpha');
  41. $action = GETPOST('action', 'aZ09');
  42. $confirm = GETPOST('confirm', 'alpha');
  43. $backtopage = GETPOST('backtopage', 'alpha');
  44. $socid = GETPOST('socid', 'int'); if ($socid < 0) {
  45. $socid = 0;
  46. }
  47. $object = new Paiement($db);
  48. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  49. $hookmanager->initHooks(array('paymentcard', 'globalcard'));
  50. // Load object
  51. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  52. $result = restrictedArea($user, $object->element, $object->id, 'paiement');
  53. // Security check
  54. if ($user->socid) {
  55. $socid = $user->socid;
  56. }
  57. // Now check also permission on thirdparty of invoices of payments. Thirdparty were loaded by the fetch_object before based on first invoice.
  58. // It should be enough because all payments are done on invoices of the same thirdparty.
  59. if ($socid && $socid != $object->thirdparty->id) {
  60. accessforbidden();
  61. }
  62. $error = 0;
  63. /*
  64. * Actions
  65. */
  66. if ($action == 'setnote' && $user->hasRight('facture', 'paiement')) {
  67. $db->begin();
  68. $result = $object->update_note(GETPOST('note', 'restricthtml'));
  69. if ($result > 0) {
  70. $db->commit();
  71. $action = '';
  72. } else {
  73. setEventMessages($object->error, $object->errors, 'errors');
  74. $db->rollback();
  75. }
  76. }
  77. if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->facture->paiement) {
  78. $db->begin();
  79. $result = $object->delete();
  80. if ($result > 0) {
  81. $db->commit();
  82. if ($backtopage) {
  83. header("Location: ".$backtopage);
  84. exit;
  85. } else {
  86. header("Location: list.php");
  87. exit;
  88. }
  89. } else {
  90. $langs->load("errors");
  91. setEventMessages($object->error, $object->errors, 'errors');
  92. $db->rollback();
  93. }
  94. }
  95. if ($action == 'confirm_validate' && $confirm == 'yes' && $user->rights->facture->paiement) {
  96. $db->begin();
  97. if ($object->validate($user) > 0) {
  98. $db->commit();
  99. // Loop on each invoice linked to this payment to rebuild PDF
  100. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  101. $outputlangs = $langs;
  102. if (GETPOST('lang_id', 'aZ09')) {
  103. $outputlangs = new Translate("", $conf);
  104. $outputlangs->setDefaultLang(GETPOST('lang_id', 'aZ09'));
  105. }
  106. $hidedetails = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0;
  107. $hidedesc = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0;
  108. $hideref = !empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0;
  109. $sql = 'SELECT f.rowid as facid';
  110. $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf,'.MAIN_DB_PREFIX.'facture as f,'.MAIN_DB_PREFIX.'societe as s';
  111. $sql .= ' WHERE pf.fk_facture = f.rowid';
  112. $sql .= ' AND f.fk_soc = s.rowid';
  113. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  114. $sql .= ' AND pf.fk_paiement = '.((int) $object->id);
  115. $resql = $db->query($sql);
  116. if ($resql) {
  117. $i = 0;
  118. $num = $db->num_rows($resql);
  119. if ($num > 0) {
  120. while ($i < $num) {
  121. $objp = $db->fetch_object($resql);
  122. $invoice = new Facture($db);
  123. if ($invoice->fetch($objp->facid) <= 0) {
  124. $error++;
  125. setEventMessages($invoice->error, $invoice->errors, 'errors');
  126. break;
  127. }
  128. if ($invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref) < 0) {
  129. $error++;
  130. setEventMessages($invoice->error, $invoice->errors, 'errors');
  131. break;
  132. }
  133. $i++;
  134. }
  135. }
  136. $db->free($resql);
  137. } else {
  138. $error++;
  139. setEventMessages($db->error, $db->errors, 'errors');
  140. }
  141. }
  142. if (! $error) {
  143. header('Location: '.$_SERVER['PHP_SELF'].'?id='.$object->id);
  144. exit;
  145. }
  146. } else {
  147. $db->rollback();
  148. $langs->load("errors");
  149. setEventMessages($object->error, $object->errors, 'errors');
  150. }
  151. }
  152. if ($action == 'setnum_paiement' && GETPOST('num_paiement')) {
  153. $res = $object->update_num(GETPOST('num_paiement'));
  154. if ($res === 0) {
  155. setEventMessages($langs->trans('PaymentNumberUpdateSucceeded'), null, 'mesgs');
  156. } else {
  157. setEventMessages($langs->trans('PaymentNumberUpdateFailed'), null, 'errors');
  158. }
  159. }
  160. if ($action == 'setdatep' && GETPOST('datepday')) {
  161. $datepaye = dol_mktime(GETPOST('datephour', 'int'), GETPOST('datepmin', 'int'), GETPOST('datepsec', 'int'), GETPOST('datepmonth', 'int'), GETPOST('datepday', 'int'), GETPOST('datepyear', 'int'));
  162. $res = $object->update_date($datepaye);
  163. if ($res === 0) {
  164. setEventMessages($langs->trans('PaymentDateUpdateSucceeded'), null, 'mesgs');
  165. } else {
  166. setEventMessages($langs->trans('PaymentDateUpdateFailed'), null, 'errors');
  167. }
  168. }
  169. if ($action == 'createbankpayment' && !empty($user->rights->facture->paiement)) {
  170. $db->begin();
  171. // Create the record into bank for the amount of payment $object
  172. if (!$error) {
  173. $label = '(CustomerInvoicePayment)';
  174. if (GETPOST('type') == Facture::TYPE_CREDIT_NOTE) {
  175. $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
  176. }
  177. $bankaccountid = GETPOST('accountid', 'int');
  178. if ($bankaccountid > 0) {
  179. $object->paiementcode = $object->type_code;
  180. $object->amounts = $object->getAmountsArray();
  181. $result = $object->addPaymentToBank($user, 'payment', $label, $bankaccountid, '', '');
  182. if ($result < 0) {
  183. setEventMessages($object->error, $object->errors, 'errors');
  184. $error++;
  185. }
  186. } else {
  187. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
  188. $error++;
  189. }
  190. }
  191. if (!$error) {
  192. $db->commit();
  193. } else {
  194. $db->rollback();
  195. }
  196. }
  197. /*
  198. * View
  199. */
  200. llxHeader('', $langs->trans("Payment"));
  201. $thirdpartystatic = new Societe($db);
  202. $result = $object->fetch($id, $ref);
  203. if ($result <= 0) {
  204. dol_print_error($db, 'Payment '.$id.' not found in database');
  205. exit;
  206. }
  207. $form = new Form($db);
  208. $head = payment_prepare_head($object);
  209. print dol_get_fiche_head($head, 'payment', $langs->trans("PaymentCustomerInvoice"), -1, 'payment');
  210. // Confirmation of payment delete
  211. if ($action == 'delete') {
  212. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans("DeletePayment"), $langs->trans("ConfirmDeletePayment"), 'confirm_delete', '', 0, 2);
  213. }
  214. // Confirmation of payment validation
  215. if ($action == 'valide') {
  216. $facid = $_GET['facid'];
  217. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;facid='.$facid, $langs->trans("ValidatePayment"), $langs->trans("ConfirmValidatePayment"), 'confirm_validate', '', 0, 2);
  218. }
  219. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  220. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '');
  221. print '<div class="fichecenter">';
  222. print '<div class="underbanner clearboth"></div>';
  223. print '<table class="border centpercent">'."\n";
  224. // Date payment
  225. print '<tr><td class="titlefield">'.$form->editfieldkey("Date", 'datep', $object->date, $object, $user->rights->facture->paiement).'</td><td>';
  226. print $form->editfieldval("Date", 'datep', $object->date, $object, $user->rights->facture->paiement, 'datehourpicker', '', null, $langs->trans('PaymentDateUpdateSucceeded'), '', 0, '', 'id', 'tzuser');
  227. print '</td></tr>';
  228. // Payment type (VIR, LIQ, ...)
  229. $labeltype = $langs->trans("PaymentType".$object->type_code) != ("PaymentType".$object->type_code) ? $langs->trans("PaymentType".$object->type_code) : $object->type_label;
  230. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td>'.$labeltype;
  231. print $object->num_payment ? ' - '.$object->num_payment : '';
  232. print '</td></tr>';
  233. // Amount
  234. print '<tr><td>'.$langs->trans('Amount').'</td><td>'.price($object->amount, '', $langs, 0, -1, -1, $conf->currency).'</td></tr>';
  235. $disable_delete = 0;
  236. // Bank account
  237. if (isModEnabled("banque")) {
  238. $bankline = new AccountLine($db);
  239. if ($object->fk_account > 0) {
  240. $bankline->fetch($object->bank_line);
  241. if ($bankline->rappro) {
  242. $disable_delete = 1;
  243. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemoveConciliatedPayment"));
  244. }
  245. print '<tr>';
  246. print '<td>'.$langs->trans('BankAccount').'</td>';
  247. print '<td>';
  248. $accountstatic = new Account($db);
  249. $accountstatic->fetch($bankline->fk_account);
  250. print $accountstatic->getNomUrl(1);
  251. print '</td>';
  252. print '</tr>';
  253. }
  254. }
  255. // Payment numero
  256. /*
  257. $titlefield=$langs->trans('Numero').' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  258. print '<tr><td>'.$form->editfieldkey($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  259. print $form->editfieldval($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('PaymentNumberUpdateSucceeded'));
  260. print '</td></tr>';
  261. // Check transmitter
  262. $titlefield=$langs->trans('CheckTransmitter').' <em>('.$langs->trans("ChequeMaker").')</em>';
  263. print '<tr><td>'.$form->editfieldkey($titlefield,'chqemetteur',$object->,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  264. print $form->editfieldval($titlefield,'chqemetteur',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeMakeUpdateSucceeded'));
  265. print '</td></tr>';
  266. // Bank name
  267. $titlefield=$langs->trans('Bank').' <em>('.$langs->trans("ChequeBank").')</em>';
  268. print '<tr><td>'.$form->editfieldkey($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  269. print $form->editfieldval($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeBankUpdateSucceeded'));
  270. print '</td></tr>';
  271. */
  272. // Bank account
  273. if (isModEnabled("banque")) {
  274. if ($object->fk_account > 0) {
  275. if ($object->type_code == 'CHQ' && $bankline->fk_bordereau > 0) {
  276. include_once DOL_DOCUMENT_ROOT.'/compta/paiement/cheque/class/remisecheque.class.php';
  277. $bordereau = new RemiseCheque($db);
  278. $bordereau->fetch($bankline->fk_bordereau);
  279. print '<tr>';
  280. print '<td>'.$langs->trans('CheckReceipt').'</td>';
  281. print '<td>';
  282. print $bordereau->getNomUrl(1);
  283. print '</td>';
  284. print '</tr>';
  285. }
  286. }
  287. print '<tr>';
  288. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  289. print '<td>';
  290. if ($object->fk_account > 0) {
  291. print $bankline->getNomUrl(1, 0, 'showconciliatedandaccounted');
  292. } else {
  293. $langs->load("admin");
  294. print '<span class="opacitymedium">';
  295. print $langs->trans("NoRecordFoundIBankcAccount", $langs->transnoentitiesnoconv("Module85Name"));
  296. print '</span>';
  297. if (!empty($user->rights->facture->paiement)) {
  298. // Try to guess $bankaccountidofinvoices that is ID of bank account defined on invoice.
  299. // Return null if not found, return 0 if it has different value for at least 2 invoices, return the value if same on all invoices where a bank is defined.
  300. $amountofpayments = $object->getAmountsArray();
  301. $bankaccountidofinvoices = null;
  302. foreach ($amountofpayments as $idinvoice => $amountofpayment) {
  303. $tmpinvoice = new Facture($db);
  304. $tmpinvoice->fetch($idinvoice);
  305. if ($tmpinvoice->fk_account > 0 && $bankaccountidofinvoices !== 0) {
  306. if (is_null($bankaccountidofinvoices)) {
  307. $bankaccountidofinvoices = $tmpinvoice->fk_account;
  308. } elseif ($bankaccountidofinvoices != $tmpinvoice->fk_account) {
  309. $bankaccountidofinvoices = 0;
  310. }
  311. }
  312. }
  313. print '<form method="POST" name="createbankpayment">';
  314. print '<input type="hidden" name="token" value="'.newToken().'">';
  315. print '<input type="hidden" name="action" value="createbankpayment">';
  316. print '<input type="hidden" name="id" value="'.$object->id.'">';
  317. print ' '.$langs->trans("ToCreateRelatedRecordIntoBank").': ';
  318. print $form->select_comptes($bankaccountidofinvoices, 'accountid', 0, '', 2, '', 0, '', 1);
  319. //print '<span class="opacitymedium">';
  320. print '<input type="submit" class="button small smallpaddingimp" name="createbankpayment" value="'.$langs->trans("ClickHere").'">';
  321. //print '</span>';
  322. print '</form>';
  323. }
  324. }
  325. print '</td>';
  326. print '</tr>';
  327. }
  328. // Comments
  329. print '<tr><td class="tdtop">'.$form->editfieldkey("Comments", 'note', $object->note, $object, $user->rights->facture->paiement).'</td><td>';
  330. print $form->editfieldval("Note", 'note', $object->note, $object, $user->rights->facture->paiement, 'textarea:'.ROWS_3.':90%');
  331. print '</td></tr>';
  332. print '</table>';
  333. print '</div>';
  334. print dol_get_fiche_end();
  335. /*
  336. * List of invoices
  337. */
  338. $sql = 'SELECT f.rowid as facid, f.ref, f.type, f.total_ttc, f.paye, f.entity, f.fk_statut, pf.amount, s.nom as name, s.rowid as socid';
  339. $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf,'.MAIN_DB_PREFIX.'facture as f,'.MAIN_DB_PREFIX.'societe as s';
  340. $sql .= ' WHERE pf.fk_facture = f.rowid';
  341. $sql .= ' AND f.fk_soc = s.rowid';
  342. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  343. $sql .= ' AND pf.fk_paiement = '.((int) $object->id);
  344. $resql = $db->query($sql);
  345. if ($resql) {
  346. $num = $db->num_rows($resql);
  347. $i = 0;
  348. $total = 0;
  349. $moreforfilter = '';
  350. print '<br>';
  351. print '<div class="div-table-responsive">';
  352. print '<table class="noborder centpercent">';
  353. print '<tr class="liste_titre">';
  354. print '<td>'.$langs->trans('Bill').'</td>';
  355. print '<td>'.$langs->trans('Company').'</td>';
  356. if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED)) {
  357. print '<td>'.$langs->trans('Entity').'</td>';
  358. }
  359. print '<td class="right">'.$langs->trans('ExpectedToPay').'</td>';
  360. print '<td class="right">'.$langs->trans('PayedByThisPayment').'</td>';
  361. print '<td class="right">'.$langs->trans('RemainderToPay').'</td>';
  362. print '<td class="right">'.$langs->trans('Status').'</td>';
  363. print "</tr>\n";
  364. if ($num > 0) {
  365. while ($i < $num) {
  366. $objp = $db->fetch_object($resql);
  367. $thirdpartystatic->fetch($objp->socid);
  368. $invoice = new Facture($db);
  369. $invoice->fetch($objp->facid);
  370. $paiement = $invoice->getSommePaiement();
  371. $creditnotes = $invoice->getSumCreditNotesUsed();
  372. $deposits = $invoice->getSumDepositsUsed();
  373. $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
  374. $remaintopay = price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits, 'MT');
  375. print '<tr class="oddeven">';
  376. // Invoice
  377. print '<td>';
  378. print $invoice->getNomUrl(1);
  379. print "</td>\n";
  380. // Third party
  381. print '<td class="tdoverflowmax150">';
  382. print $thirdpartystatic->getNomUrl(1);
  383. print '</td>';
  384. // Expected to pay
  385. if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED)) {
  386. print '<td>';
  387. $mc->getInfo($objp->entity);
  388. print $mc->label;
  389. print '</td>';
  390. }
  391. // Expected to pay
  392. print '<td class="right"><span class="amount">'.price($objp->total_ttc).'</span></td>';
  393. // Amount payed
  394. print '<td class="right"><span class="amount">'.price($objp->amount).'</span></td>';
  395. // Remain to pay
  396. print '<td class="right"><span class="amount">'.price($remaintopay).'</span></td>';
  397. // Status
  398. print '<td class="right">'.$invoice->getLibStatut(5, $alreadypayed).'</td>';
  399. print "</tr>\n";
  400. // If at least one invoice is paid, disable delete. INVOICE_CAN_DELETE_PAYMENT_EVEN_IF_INVOICE_CLOSED Can be use for maintenance purpose. Never use this in production
  401. if ($objp->paye == 1 && empty($conf->global->INVOICE_CAN_DELETE_PAYMENT_EVEN_IF_INVOICE_CLOSED)) {
  402. $disable_delete = 1;
  403. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemovePaymentWithOneInvoicePaid"));
  404. }
  405. $total = $total + $objp->amount;
  406. $i++;
  407. }
  408. }
  409. print "</table>\n";
  410. print '</div>';
  411. $db->free($resql);
  412. } else {
  413. dol_print_error($db);
  414. }
  415. /*
  416. * Actions Buttons
  417. */
  418. print '<div class="tabsAction">';
  419. if (!empty($conf->global->BILL_ADD_PAYMENT_VALIDATION)) {
  420. if ($user->socid == 0 && $object->statut == 0 && $_GET['action'] == '') {
  421. if ($user->rights->facture->paiement) {
  422. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&facid='.$objp->facid.'&action=valide&token='.newToken().'">'.$langs->trans('Valid').'</a>';
  423. }
  424. }
  425. }
  426. if ($user->socid == 0 && $action == '') {
  427. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $user->rights->facture->paiement && !$disable_delete);
  428. }
  429. print '</div>';
  430. // End of page
  431. llxFooter();
  432. $db->close();