card.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2023 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. if (isModEnabled('margin')) {
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmargin.class.php';
  39. }
  40. // Load translation files required by the page
  41. $langs->loadLangs(array('bills', 'banks', 'companies'));
  42. $id = GETPOST('id', 'int');
  43. $ref = GETPOST('ref', 'alpha');
  44. $action = GETPOST('action', 'aZ09');
  45. $confirm = GETPOST('confirm', 'alpha');
  46. $backtopage = GETPOST('backtopage', 'alpha');
  47. $socid = GETPOST('socid', 'int');
  48. if ($socid < 0) {
  49. $socid = 0;
  50. }
  51. $object = new Paiement($db);
  52. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  53. $hookmanager->initHooks(array('paymentcard', 'globalcard'));
  54. // Load object
  55. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  56. $result = restrictedArea($user, $object->element, $object->id, 'paiement'); // This also test permission on read invoice
  57. // Security check
  58. if ($user->socid) {
  59. $socid = $user->socid;
  60. }
  61. // Now check also permission on thirdparty of invoices of payments. Thirdparty were loaded by the fetch_object before based on first invoice.
  62. // It should be enough because all payments are done on invoices of the same thirdparty.
  63. if ($socid && $socid != $object->thirdparty->id) {
  64. accessforbidden();
  65. }
  66. // Init Stripe objects
  67. if (isModEnabled('stripe')) {
  68. require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
  69. $service = 'StripeTest';
  70. $servicestatus = 0;
  71. if (getDolGlobalString('STRIPE_LIVE') && !GETPOST('forcesandbox', 'alpha')) {
  72. $service = 'StripeLive';
  73. $servicestatus = 1;
  74. }
  75. // Force to use the correct API key
  76. global $stripearrayofkeysbyenv;
  77. $site_account = $stripearrayofkeysbyenv[$servicestatus]['publishable_key'];
  78. $stripe = new Stripe($db);
  79. $stripeacc = $stripe->getStripeAccount($service); // Get Stripe OAuth connect account (no remote access to Stripe here)
  80. }
  81. $error = 0;
  82. /*
  83. * Actions
  84. */
  85. $parameters = array('socid'=>$socid);
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  87. if ($reshook < 0) {
  88. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  89. }
  90. if (empty($reshook)) {
  91. if ($action == 'setnote' && $user->hasRight('facture', 'paiement')) {
  92. $db->begin();
  93. $result = $object->update_note(GETPOST('note', 'restricthtml'));
  94. if ($result > 0) {
  95. $db->commit();
  96. $action = '';
  97. } else {
  98. setEventMessages($object->error, $object->errors, 'errors');
  99. $db->rollback();
  100. }
  101. }
  102. if ($action == 'confirm_delete' && $confirm == 'yes' && $user->hasRight('facture', 'paiement')) {
  103. $db->begin();
  104. $result = $object->delete();
  105. if ($result > 0) {
  106. $db->commit();
  107. if ($backtopage) {
  108. header("Location: ".$backtopage);
  109. exit;
  110. } else {
  111. header("Location: list.php");
  112. exit;
  113. }
  114. } else {
  115. $langs->load("errors");
  116. setEventMessages($object->error, $object->errors, 'errors');
  117. $db->rollback();
  118. }
  119. }
  120. if ($action == 'confirm_validate' && $confirm == 'yes' && $user->hasRight('facture', 'paiement')) {
  121. $db->begin();
  122. if ($object->validate($user) > 0) {
  123. $db->commit();
  124. // Loop on each invoice linked to this payment to rebuild PDF
  125. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  126. $outputlangs = $langs;
  127. if (GETPOST('lang_id', 'aZ09')) {
  128. $outputlangs = new Translate("", $conf);
  129. $outputlangs->setDefaultLang(GETPOST('lang_id', 'aZ09'));
  130. }
  131. $hidedetails = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0;
  132. $hidedesc = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0;
  133. $hideref = getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0;
  134. $sql = 'SELECT f.rowid as facid';
  135. $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf,'.MAIN_DB_PREFIX.'facture as f,'.MAIN_DB_PREFIX.'societe as s';
  136. $sql .= ' WHERE pf.fk_facture = f.rowid';
  137. $sql .= ' AND f.fk_soc = s.rowid';
  138. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  139. $sql .= ' AND pf.fk_paiement = '.((int) $object->id);
  140. $resql = $db->query($sql);
  141. if ($resql) {
  142. $i = 0;
  143. $num = $db->num_rows($resql);
  144. if ($num > 0) {
  145. while ($i < $num) {
  146. $objp = $db->fetch_object($resql);
  147. $invoice = new Facture($db);
  148. if ($invoice->fetch($objp->facid) <= 0) {
  149. $error++;
  150. setEventMessages($invoice->error, $invoice->errors, 'errors');
  151. break;
  152. }
  153. if ($invoice->generateDocument($invoice->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref) < 0) {
  154. $error++;
  155. setEventMessages($invoice->error, $invoice->errors, 'errors');
  156. break;
  157. }
  158. $i++;
  159. }
  160. }
  161. $db->free($resql);
  162. } else {
  163. $error++;
  164. setEventMessages($db->error, $db->errors, 'errors');
  165. }
  166. }
  167. if (! $error) {
  168. header('Location: '.$_SERVER['PHP_SELF'].'?id='.$object->id);
  169. exit;
  170. }
  171. } else {
  172. $db->rollback();
  173. $langs->load("errors");
  174. setEventMessages($object->error, $object->errors, 'errors');
  175. }
  176. }
  177. if ($action == 'setnum_paiement' && GETPOST('num_paiement') && $user->hasRight('facture', 'paiement')) {
  178. $res = $object->update_num(GETPOST('num_paiement'));
  179. if ($res === 0) {
  180. setEventMessages($langs->trans('PaymentNumberUpdateSucceeded'), null, 'mesgs');
  181. } else {
  182. setEventMessages($langs->trans('PaymentNumberUpdateFailed'), null, 'errors');
  183. }
  184. }
  185. if ($action == 'setdatep' && GETPOST('datepday') && $user->hasRight('facture', 'paiement')) {
  186. $datepaye = dol_mktime(GETPOST('datephour', 'int'), GETPOST('datepmin', 'int'), GETPOST('datepsec', 'int'), GETPOST('datepmonth', 'int'), GETPOST('datepday', 'int'), GETPOST('datepyear', 'int'));
  187. $res = $object->update_date($datepaye);
  188. if ($res === 0) {
  189. setEventMessages($langs->trans('PaymentDateUpdateSucceeded'), null, 'mesgs');
  190. } else {
  191. setEventMessages($langs->trans('PaymentDateUpdateFailed'), null, 'errors');
  192. }
  193. }
  194. if ($action == 'createbankpayment' && $user->hasRight('facture', 'paiement')) {
  195. $db->begin();
  196. // Create the record into bank for the amount of payment $object
  197. if (!$error) {
  198. $label = '(CustomerInvoicePayment)';
  199. if (GETPOST('type') == Facture::TYPE_CREDIT_NOTE) {
  200. $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
  201. }
  202. $bankaccountid = GETPOST('accountid', 'int');
  203. if ($bankaccountid > 0) {
  204. $object->paiementcode = $object->type_code;
  205. $object->amounts = $object->getAmountsArray();
  206. $result = $object->addPaymentToBank($user, 'payment', $label, $bankaccountid, '', '');
  207. if ($result < 0) {
  208. setEventMessages($object->error, $object->errors, 'errors');
  209. $error++;
  210. }
  211. } else {
  212. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
  213. $error++;
  214. }
  215. }
  216. if (!$error) {
  217. $db->commit();
  218. } else {
  219. $db->rollback();
  220. }
  221. }
  222. }
  223. /*
  224. * View
  225. */
  226. llxHeader('', $langs->trans("Payment"));
  227. $thirdpartystatic = new Societe($db);
  228. $result = $object->fetch($id, $ref);
  229. if ($result <= 0) {
  230. dol_print_error($db, 'Payment '.$id.' not found in database');
  231. exit;
  232. }
  233. $form = new Form($db);
  234. $head = payment_prepare_head($object);
  235. print dol_get_fiche_head($head, 'payment', $langs->trans("PaymentCustomerInvoice"), -1, 'payment');
  236. // Confirmation of payment delete
  237. if ($action == 'delete') {
  238. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans("DeletePayment"), $langs->trans("ConfirmDeletePayment"), 'confirm_delete', '', 0, 2);
  239. }
  240. // Confirmation of payment validation
  241. if ($action == 'valide') {
  242. $facid = $_GET['facid'];
  243. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;facid='.$facid, $langs->trans("ValidatePayment"), $langs->trans("ConfirmValidatePayment"), 'confirm_validate', '', 0, 2);
  244. }
  245. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  246. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '');
  247. print '<div class="fichecenter">';
  248. print '<div class="underbanner clearboth"></div>';
  249. print '<table class="border centpercent">'."\n";
  250. // Date payment
  251. print '<tr><td class="titlefield">'.$form->editfieldkey("Date", 'datep', $object->date, $object, $user->hasRight('facture', 'paiement')).'</td><td>';
  252. print $form->editfieldval("Date", 'datep', $object->date, $object, $user->hasRight('facture', 'paiement'), 'datehourpicker', '', null, $langs->trans('PaymentDateUpdateSucceeded'), '', 0, '', 'id', 'tzuser');
  253. print '</td></tr>';
  254. // Payment type (VIR, LIQ, ...)
  255. $labeltype = $langs->trans("PaymentType".$object->type_code) != "PaymentType".$object->type_code ? $langs->trans("PaymentType".$object->type_code) : $object->type_label;
  256. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td>'.$labeltype;
  257. print $object->num_payment ? ' - '.$object->num_payment : '';
  258. print '</td></tr>';
  259. // Amount
  260. print '<tr><td>'.$langs->trans('Amount').'</td><td>'.price($object->amount, '', $langs, 0, -1, -1, $conf->currency).'</td></tr>';
  261. $disable_delete = 0;
  262. // Bank account
  263. if (isModEnabled("banque")) {
  264. $bankline = new AccountLine($db);
  265. if ($object->fk_account > 0) {
  266. $bankline->fetch($object->bank_line);
  267. if ($bankline->rappro) {
  268. $disable_delete = 1;
  269. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemoveConciliatedPayment"));
  270. }
  271. print '<tr>';
  272. print '<td>'.$langs->trans('BankAccount').'</td>';
  273. print '<td>';
  274. $accountstatic = new Account($db);
  275. $accountstatic->fetch($bankline->fk_account);
  276. print $accountstatic->getNomUrl(1);
  277. print '</td>';
  278. print '</tr>';
  279. }
  280. }
  281. // Payment numero
  282. /*
  283. $titlefield=$langs->trans('Numero').' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  284. print '<tr><td>'.$form->editfieldkey($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  285. print $form->editfieldval($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('PaymentNumberUpdateSucceeded'));
  286. print '</td></tr>';
  287. // Check transmitter
  288. $titlefield=$langs->trans('CheckTransmitter').' <em>('.$langs->trans("ChequeMaker").')</em>';
  289. print '<tr><td>'.$form->editfieldkey($titlefield,'chqemetteur',$object->,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  290. print $form->editfieldval($titlefield,'chqemetteur',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeMakeUpdateSucceeded'));
  291. print '</td></tr>';
  292. // Bank name
  293. $titlefield=$langs->trans('Bank').' <em>('.$langs->trans("ChequeBank").')</em>';
  294. print '<tr><td>'.$form->editfieldkey($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  295. print $form->editfieldval($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeBankUpdateSucceeded'));
  296. print '</td></tr>';
  297. */
  298. // Bank account
  299. if (isModEnabled("banque")) {
  300. if ($object->fk_account > 0) {
  301. if ($object->type_code == 'CHQ' && $bankline->fk_bordereau > 0) {
  302. include_once DOL_DOCUMENT_ROOT.'/compta/paiement/cheque/class/remisecheque.class.php';
  303. $bordereau = new RemiseCheque($db);
  304. $bordereau->fetch($bankline->fk_bordereau);
  305. print '<tr>';
  306. print '<td>'.$langs->trans('CheckReceipt').'</td>';
  307. print '<td>';
  308. print $bordereau->getNomUrl(1);
  309. print '</td>';
  310. print '</tr>';
  311. }
  312. }
  313. print '<tr>';
  314. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  315. print '<td>';
  316. if ($object->fk_account > 0) {
  317. print $bankline->getNomUrl(1, 0, 'showconciliatedandaccounted');
  318. } else {
  319. $langs->load("admin");
  320. print '<span class="opacitymedium">';
  321. print $langs->trans("NoRecordFoundIBankcAccount", $langs->transnoentitiesnoconv("Module85Name"));
  322. print '</span>';
  323. if ($user->hasRight('facture', 'paiement')) {
  324. // Try to guess $bankaccountidofinvoices that is ID of bank account defined on invoice.
  325. // 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.
  326. $amountofpayments = $object->getAmountsArray();
  327. $bankaccountidofinvoices = null;
  328. foreach ($amountofpayments as $idinvoice => $amountofpayment) {
  329. $tmpinvoice = new Facture($db);
  330. $tmpinvoice->fetch($idinvoice);
  331. if ($tmpinvoice->fk_account > 0 && $bankaccountidofinvoices !== 0) {
  332. if (is_null($bankaccountidofinvoices)) {
  333. $bankaccountidofinvoices = $tmpinvoice->fk_account;
  334. } elseif ($bankaccountidofinvoices != $tmpinvoice->fk_account) {
  335. $bankaccountidofinvoices = 0;
  336. }
  337. }
  338. }
  339. print '<form method="POST" name="createbankpayment">';
  340. print '<input type="hidden" name="token" value="'.newToken().'">';
  341. print '<input type="hidden" name="action" value="createbankpayment">';
  342. print '<input type="hidden" name="id" value="'.$object->id.'">';
  343. print ' '.$langs->trans("ToCreateRelatedRecordIntoBank").': ';
  344. print $form->select_comptes($bankaccountidofinvoices, 'accountid', 0, '', 2, '', 0, '', 1);
  345. //print '<span class="opacitymedium">';
  346. print '<input type="submit" class="button small smallpaddingimp" name="createbankpayment" value="'.$langs->trans("ClickHere").'">';
  347. //print '</span>';
  348. print '</form>';
  349. }
  350. }
  351. print '</td>';
  352. print '</tr>';
  353. }
  354. // Comments
  355. print '<tr><td class="tdtop">'.$form->editfieldkey("Comments", 'note', $object->note_private, $object, $user->hasRight('facture', 'paiement')).'</td><td>';
  356. print $form->editfieldval("Note", 'note', $object->note_private, $object, $user->hasRight('facture', 'paiement'), 'textarea:'.ROWS_3.':90%');
  357. print '</td></tr>';
  358. if (!empty($object->ext_payment_id)) {
  359. // External payment ID
  360. print '<tr><td class="tdtop">'.$langs->trans("StripePaymentId").'</td><td>';
  361. if (isModEnabled('stripe') && in_array($object->ext_payment_site, array('Stripe', 'StripeLive'))) {
  362. $tmp1 = explode('@', $object->ext_payment_id);
  363. if (!empty($tmp1[1])) {
  364. $site_account_payment = $tmp1[1]; // pk_live_...
  365. }
  366. $tmp2 = explode(':', $tmp1[0]);
  367. if (!empty($tmp2[1])) {
  368. $stripecu = $tmp2[1];
  369. }
  370. print dol_escape_htmltag($tmp1[0]);
  371. $connect = '';
  372. if (!empty($stripeacc)) {
  373. $connect = $stripeacc.'/';
  374. }
  375. $url = 'https://dashboard.stripe.com/'.$connect.'test/customers/'.$stripecu;
  376. if (!empty($stripearrayofkeysbyenv[1]['publishable_key']) && $stripearrayofkeysbyenv[1]['publishable_key'] == $site_account_payment) {
  377. $url = 'https://dashboard.stripe.com/'.$connect.'customers/'.$stripecu;
  378. }
  379. print ' <a href="'.$url.'" target="_stripe">'.img_picto($langs->trans('ShowInStripe').' - Publishable key = '.$site_account_payment, 'globe').'</a>';
  380. } else {
  381. print dol_escape_htmltag($object->ext_payment_id);
  382. }
  383. print '</td></tr>';
  384. }
  385. print '</table>';
  386. print '</div>';
  387. print dol_get_fiche_end();
  388. /*
  389. * List of invoices
  390. */
  391. $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';
  392. $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf,'.MAIN_DB_PREFIX.'facture as f,'.MAIN_DB_PREFIX.'societe as s';
  393. $sql .= ' WHERE pf.fk_facture = f.rowid';
  394. $sql .= ' AND f.fk_soc = s.rowid';
  395. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  396. $sql .= ' AND pf.fk_paiement = '.((int) $object->id);
  397. $resql = $db->query($sql);
  398. if ($resql) {
  399. $num = $db->num_rows($resql);
  400. $i = 0;
  401. $total = 0;
  402. print '<br>';
  403. print '<div class="div-table-responsive">';
  404. print '<table class="noborder centpercent">';
  405. print '<tr class="liste_titre">';
  406. print '<td>'.$langs->trans('Bill').'</td>';
  407. print '<td>'.$langs->trans('Company').'</td>';
  408. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  409. print '<td>'.$langs->trans('Entity').'</td>';
  410. }
  411. //Add Margin
  412. if (isModEnabled('margin') && getDolGlobalInt('MARGIN_SHOW_MARGIN_ON_PAYMENT')) {
  413. print '<td class="right">'.$langs->trans('Margin').'</td>';
  414. }
  415. print '<td class="right">'.$langs->trans('ExpectedToPay').'</td>';
  416. print '<td class="right">'.$langs->trans('PayedByThisPayment').'</td>';
  417. print '<td class="right">'.$langs->trans('RemainderToPay').'</td>';
  418. print '<td class="right">'.$langs->trans('Status').'</td>';
  419. $parameters = array();
  420. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  421. print "</tr>\n";
  422. if ($num > 0) {
  423. while ($i < $num) {
  424. $objp = $db->fetch_object($resql);
  425. $thirdpartystatic->fetch($objp->socid);
  426. $invoice = new Facture($db);
  427. $invoice->fetch($objp->facid);
  428. // Add Margin
  429. if (isModEnabled('margin') && getDolGlobalInt('MARGIN_SHOW_MARGIN_ON_PAYMENT')) {
  430. $formmargin = new FormMargin($db);
  431. $marginInfo = array();
  432. $invoice->fetch_lines();
  433. $marginInfo = $formmargin->getMarginInfosArray($invoice);
  434. }
  435. $paiement = $invoice->getSommePaiement();
  436. $creditnotes = $invoice->getSumCreditNotesUsed();
  437. $deposits = $invoice->getSumDepositsUsed();
  438. $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
  439. $remaintopay = price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits, 'MT');
  440. print '<tr class="oddeven">';
  441. // Invoice
  442. print '<td>';
  443. print $invoice->getNomUrl(1);
  444. print "</td>\n";
  445. // Third party
  446. print '<td class="tdoverflowmax150">';
  447. print $thirdpartystatic->getNomUrl(1);
  448. print '</td>';
  449. // Expected to pay
  450. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_INVOICE_SHARING_ENABLED')) {
  451. print '<td>';
  452. $mc->getInfo($objp->entity);
  453. print $mc->label;
  454. print '</td>';
  455. }
  456. // Add margin
  457. if (isModEnabled('margin') && getDolGlobalInt('MARGIN_SHOW_MARGIN_ON_PAYMENT')) {
  458. print '<td class="right">'.price($marginInfo['total_margin']).'</td>';
  459. }
  460. // Expected to pay
  461. print '<td class="right"><span class="amount">'.price($objp->total_ttc).'</span></td>';
  462. // Amount payed
  463. print '<td class="right"><span class="amount">'.price($objp->amount).'</span></td>';
  464. // Remain to pay
  465. print '<td class="right"><span class="amount">'.price($remaintopay).'</span></td>';
  466. // Status
  467. print '<td class="right">'.$invoice->getLibStatut(5, $alreadypayed).'</td>';
  468. $parameters = array('fk_paiement'=> (int) $object->id);
  469. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $objp, $action); // Note that $action and $object may have been modified by hook
  470. print "</tr>\n";
  471. // 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
  472. if ($objp->paye == 1 && !getDolGlobalString('INVOICE_CAN_DELETE_PAYMENT_EVEN_IF_INVOICE_CLOSED')) {
  473. $disable_delete = 1;
  474. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemovePaymentWithOneInvoicePaid"));
  475. }
  476. $total = $total + $objp->amount;
  477. $i++;
  478. }
  479. }
  480. print "</table>\n";
  481. print '</div>';
  482. $db->free($resql);
  483. } else {
  484. dol_print_error($db);
  485. }
  486. /*
  487. * Actions Buttons
  488. */
  489. print '<div class="tabsAction">';
  490. if (getDolGlobalString('BILL_ADD_PAYMENT_VALIDATION')) {
  491. if ($user->socid == 0 && $object->statut == 0 && $action == '') {
  492. if ($user->hasRight('facture', 'paiement')) {
  493. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=valide&token='.newToken().'">'.$langs->trans('Valid').'</a>';
  494. }
  495. }
  496. }
  497. $params = array();
  498. if (! empty($title_button)) {
  499. $params['attr'] = array('title' => $title_button);
  500. }
  501. if ($user->socid == 0 && $action == '') {
  502. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $user->hasRight('facture', 'paiement') && !$disable_delete, $params);
  503. }
  504. print '</div>';
  505. // End of page
  506. llxFooter();
  507. $db->close();