card.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  33. if (!empty($conf->banque->enabled)) require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('bills', 'banks', 'companies'));
  36. $id = GETPOST('id', 'int');
  37. $ref = GETPOST('ref', 'alpha');
  38. $action = GETPOST('action', 'alpha');
  39. $confirm = GETPOST('confirm', 'alpha');
  40. $backtopage = GETPOST('backtopage', 'alpha');
  41. // Security check
  42. if ($user->socid) $socid = $user->socid;
  43. // TODO ajouter regle pour restreindre acces paiement
  44. //$result = restrictedArea($user, 'facture', $id,'');
  45. $object = new Paiement($db);
  46. /*
  47. * Actions
  48. */
  49. if ($action == 'setnote' && $user->rights->facture->paiement)
  50. {
  51. $db->begin();
  52. $object->fetch($id);
  53. $result = $object->update_note(GETPOST('note', 'none'));
  54. if ($result > 0)
  55. {
  56. $db->commit();
  57. $action = '';
  58. }
  59. else
  60. {
  61. setEventMessages($object->error, $object->errors, 'errors');
  62. $db->rollback();
  63. }
  64. }
  65. if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->facture->paiement)
  66. {
  67. $db->begin();
  68. $object->fetch($id);
  69. $result = $object->delete();
  70. if ($result > 0)
  71. {
  72. $db->commit();
  73. if ($backtopage)
  74. {
  75. header("Location: ".$backtopage);
  76. exit;
  77. }
  78. else
  79. {
  80. header("Location: list.php");
  81. exit;
  82. }
  83. }
  84. else
  85. {
  86. $langs->load("errors");
  87. setEventMessages($object->error, $object->errors, 'errors');
  88. $db->rollback();
  89. }
  90. }
  91. if ($action == 'confirm_valide' && $confirm == 'yes' && $user->rights->facture->paiement)
  92. {
  93. $db->begin();
  94. $object->fetch($id);
  95. if ($object->valide($user) > 0)
  96. {
  97. $db->commit();
  98. // Loop on each invoice linked to this payment to rebuild PDF
  99. $factures = array();
  100. foreach ($factures as $id)
  101. {
  102. $fac = new Facture($db);
  103. $fac->fetch($id);
  104. $outputlangs = $langs;
  105. if (!empty($_REQUEST['lang_id']))
  106. {
  107. $outputlangs = new Translate("", $conf);
  108. $outputlangs->setDefaultLang($_REQUEST['lang_id']);
  109. }
  110. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  111. $fac->generateDocument($fac->modelpdf, $outputlangs);
  112. }
  113. }
  114. header('Location: '.$_SERVER['PHP_SELF'].'?id='.$object->id);
  115. exit;
  116. }
  117. else
  118. {
  119. $langs->load("errors");
  120. setEventMessages($object->error, $object->errors, 'errors');
  121. $db->rollback();
  122. }
  123. }
  124. if ($action == 'setnum_paiement' && !empty($_POST['num_paiement']))
  125. {
  126. $object->fetch($id);
  127. $res = $object->update_num($_POST['num_paiement']);
  128. if ($res === 0)
  129. {
  130. setEventMessages($langs->trans('PaymentNumberUpdateSucceeded'), null, 'mesgs');
  131. }
  132. else
  133. {
  134. setEventMessages($langs->trans('PaymentNumberUpdateFailed'), null, 'errors');
  135. }
  136. }
  137. if ($action == 'setdatep' && !empty($_POST['datepday']))
  138. {
  139. $object->fetch($id);
  140. $datepaye = dol_mktime(GETPOST('datephour', 'int'), GETPOST('datepmin', 'int'), GETPOST('datepsec', 'int'), GETPOST('datepmonth', 'int'), GETPOST('datepday', 'int'), GETPOST('datepyear', 'int'));
  141. $res = $object->update_date($datepaye);
  142. if ($res === 0)
  143. {
  144. setEventMessages($langs->trans('PaymentDateUpdateSucceeded'), null, 'mesgs');
  145. }
  146. else
  147. {
  148. setEventMessages($langs->trans('PaymentDateUpdateFailed'), null, 'errors');
  149. }
  150. }
  151. /*
  152. * View
  153. */
  154. llxHeader('', $langs->trans("Payment"));
  155. $thirdpartystatic = new Societe($db);
  156. $result = $object->fetch($id, $ref);
  157. if ($result <= 0)
  158. {
  159. dol_print_error($db, 'Payement '.$id.' not found in database');
  160. exit;
  161. }
  162. $form = new Form($db);
  163. $head = payment_prepare_head($object);
  164. dol_fiche_head($head, 'payment', $langs->trans("PaymentCustomerInvoice"), -1, 'payment');
  165. // Confirmation de la suppression du paiement
  166. if ($action == 'delete')
  167. {
  168. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans("DeletePayment"), $langs->trans("ConfirmDeletePayment"), 'confirm_delete', '', 0, 2);
  169. }
  170. // Confirmation de la validation du paiement
  171. if ($action == 'valide')
  172. {
  173. $facid = $_GET['facid'];
  174. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;facid='.$facid, $langs->trans("ValidatePayment"), $langs->trans("ConfirmValidatePayment"), 'confirm_valide', '', 0, 2);
  175. }
  176. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  177. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '');
  178. print '<div class="fichecenter">';
  179. print '<div class="underbanner clearboth"></div>';
  180. print '<table class="border centpercent">'."\n";
  181. // Date payment
  182. print '<tr><td class="titlefield">'.$form->editfieldkey("Date", 'datep', $object->date, $object, $user->rights->facture->paiement).'</td><td>';
  183. print $form->editfieldval("Date", 'datep', $object->date, $object, $user->rights->facture->paiement, 'datehourpicker', '', null, $langs->trans('PaymentDateUpdateSucceeded'));
  184. print '</td></tr>';
  185. // Payment type (VIR, LIQ, ...)
  186. $labeltype = $langs->trans("PaymentType".$object->type_code) != ("PaymentType".$object->type_code) ? $langs->trans("PaymentType".$object->type_code) : $object->type_label;
  187. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td>'.$labeltype;
  188. print $object->num_payment? ' - '.$object->num_payment : '';
  189. print '</td></tr>';
  190. // Amount
  191. print '<tr><td>'.$langs->trans('Amount').'</td><td>'.price($object->amount, '', $langs, 0, -1, -1, $conf->currency).'</td></tr>';
  192. $disable_delete = 0;
  193. // Bank account
  194. if (!empty($conf->banque->enabled))
  195. {
  196. $bankline = new AccountLine($db);
  197. if ($object->fk_account > 0)
  198. {
  199. $bankline->fetch($object->bank_line);
  200. if ($bankline->rappro)
  201. {
  202. $disable_delete = 1;
  203. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemoveConciliatedPayment"));
  204. }
  205. print '<tr>';
  206. print '<td>'.$langs->trans('BankAccount').'</td>';
  207. print '<td>';
  208. $accountstatic = new Account($db);
  209. $accountstatic->fetch($bankline->fk_account);
  210. print $accountstatic->getNomUrl(1);
  211. print '</td>';
  212. print '</tr>';
  213. }
  214. }
  215. // Payment numero
  216. /*
  217. $titlefield=$langs->trans('Numero').' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  218. print '<tr><td>'.$form->editfieldkey($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  219. print $form->editfieldval($titlefield,'num_paiement',$object->num_paiement,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('PaymentNumberUpdateSucceeded'));
  220. print '</td></tr>';
  221. // Check transmitter
  222. $titlefield=$langs->trans('CheckTransmitter').' <em>('.$langs->trans("ChequeMaker").')</em>';
  223. print '<tr><td>'.$form->editfieldkey($titlefield,'chqemetteur',$object->,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  224. print $form->editfieldval($titlefield,'chqemetteur',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeMakeUpdateSucceeded'));
  225. print '</td></tr>';
  226. // Bank name
  227. $titlefield=$langs->trans('Bank').' <em>('.$langs->trans("ChequeBank").')</em>';
  228. print '<tr><td>'.$form->editfieldkey($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td>';
  229. print $form->editfieldval($titlefield,'chqbank',$object->aaa,$object,$object->statut == 0 && $user->rights->fournisseur->facture->creer,'string','',null,$langs->trans('ChequeBankUpdateSucceeded'));
  230. print '</td></tr>';
  231. */
  232. // Bank account
  233. if (!empty($conf->banque->enabled))
  234. {
  235. if ($object->fk_account > 0)
  236. {
  237. if ($object->type_code == 'CHQ' && $bankline->fk_bordereau > 0)
  238. {
  239. dol_include_once('/compta/paiement/cheque/class/remisecheque.class.php');
  240. $bordereau = new RemiseCheque($db);
  241. $bordereau->fetch($bankline->fk_bordereau);
  242. print '<tr>';
  243. print '<td>'.$langs->trans('CheckReceipt').'</td>';
  244. print '<td>';
  245. print $bordereau->getNomUrl(1);
  246. print '</td>';
  247. print '</tr>';
  248. }
  249. }
  250. print '<tr>';
  251. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  252. print '<td>';
  253. print $bankline->getNomUrl(1, 0, 'showconciliatedandaccounted');
  254. print '</td>';
  255. print '</tr>';
  256. }
  257. // Comments
  258. print '<tr><td class="tdtop">'.$form->editfieldkey("Comments", 'note', $object->note, $object, $user->rights->facture->paiement).'</td><td>';
  259. print $form->editfieldval("Note", 'note', $object->note, $object, $user->rights->facture->paiement, 'textarea:'.ROWS_3.':90%');
  260. print '</td></tr>';
  261. print '</table>';
  262. print '</div>';
  263. dol_fiche_end();
  264. /*
  265. * List of invoices
  266. */
  267. $sql = 'SELECT f.rowid as facid, f.ref, f.type, f.total_ttc, f.paye, f.fk_statut, pf.amount, s.nom as name, s.rowid as socid';
  268. $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf,'.MAIN_DB_PREFIX.'facture as f,'.MAIN_DB_PREFIX.'societe as s';
  269. $sql .= ' WHERE pf.fk_facture = f.rowid';
  270. $sql .= ' AND f.fk_soc = s.rowid';
  271. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  272. $sql .= ' AND pf.fk_paiement = '.$object->id;
  273. $resql = $db->query($sql);
  274. if ($resql)
  275. {
  276. $num = $db->num_rows($resql);
  277. $i = 0;
  278. $total = 0;
  279. $moreforfilter = '';
  280. print '<br>';
  281. print '<div class="div-table-responsive">';
  282. print '<table class="noborder centpercent">';
  283. print '<tr class="liste_titre">';
  284. print '<td>'.$langs->trans('Bill').'</td>';
  285. print '<td>'.$langs->trans('Company').'</td>';
  286. if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED)) print '<td>'.$langs->trans('Entity').'</td>';
  287. print '<td class="right">'.$langs->trans('ExpectedToPay').'</td>';
  288. print '<td class="right">'.$langs->trans('PayedByThisPayment').'</td>';
  289. print '<td class="right">'.$langs->trans('RemainderToPay').'</td>';
  290. print '<td class="right">'.$langs->trans('Status').'</td>';
  291. print "</tr>\n";
  292. if ($num > 0)
  293. {
  294. while ($i < $num)
  295. {
  296. $objp = $db->fetch_object($resql);
  297. $thirdpartystatic->fetch($objp->socid);
  298. $invoice = new Facture($db);
  299. $invoice->fetch($objp->facid);
  300. $paiement = $invoice->getSommePaiement();
  301. $creditnotes = $invoice->getSumCreditNotesUsed();
  302. $deposits = $invoice->getSumDepositsUsed();
  303. $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
  304. $remaintopay = price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits, 'MT');
  305. print '<tr class="oddeven">';
  306. // Invoice
  307. print '<td>';
  308. print $invoice->getNomUrl(1);
  309. print "</td>\n";
  310. // Third party
  311. print '<td>';
  312. print $thirdpartystatic->getNomUrl(1);
  313. print '</td>';
  314. // Expected to pay
  315. if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED)) {
  316. print '<td>';
  317. $mc->getInfo($objp->entity);
  318. print $mc->label;
  319. print '</td>';
  320. }
  321. // Expected to pay
  322. print '<td class="right">'.price($objp->total_ttc).'</td>';
  323. // Amount payed
  324. print '<td class="right">'.price($objp->amount).'</td>';
  325. // Remain to pay
  326. print '<td class="right">'.price($remaintopay).'</td>';
  327. // Status
  328. print '<td class="right">'.$invoice->getLibStatut(5, $alreadypayed).'</td>';
  329. print "</tr>\n";
  330. if ($objp->paye == 1) // If at least one invoice is paid, disable delete
  331. {
  332. $disable_delete = 1;
  333. $title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemovePaymentWithOneInvoicePaid"));
  334. }
  335. $total = $total + $objp->amount;
  336. $i++;
  337. }
  338. }
  339. print "</table>\n";
  340. print '</div>';
  341. $db->free($resql);
  342. }
  343. else
  344. {
  345. dol_print_error($db);
  346. }
  347. /*
  348. * Boutons Actions
  349. */
  350. print '<div class="tabsAction">';
  351. if (!empty($conf->global->BILL_ADD_PAYMENT_VALIDATION))
  352. {
  353. if ($user->socid == 0 && $object->statut == 0 && $_GET['action'] == '')
  354. {
  355. if ($user->rights->facture->paiement)
  356. {
  357. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;facid='.$objp->facid.'&amp;action=valide">'.$langs->trans('Valid').'</a>';
  358. }
  359. }
  360. }
  361. if ($user->socid == 0 && $action == '')
  362. {
  363. if ($user->rights->facture->paiement)
  364. {
  365. if (!$disable_delete)
  366. {
  367. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=delete">'.$langs->trans('Delete').'</a>';
  368. }
  369. else
  370. {
  371. print '<a class="butActionRefused classfortooltip" href="#" title="'.$title_button.'">'.$langs->trans('Delete').'</a>';
  372. }
  373. }
  374. }
  375. print '</div>';
  376. // End of page
  377. llxFooter();
  378. $db->close();