payment.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2015-2018 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/loan/payment/payment.php
  21. * \ingroup Loan
  22. * \brief Page to add payment of a loan
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
  31. $langs->loadLangs(array("bills", "loan"));
  32. $chid = GETPOST('id', 'int');
  33. $action = GETPOST('action', 'aZ09');
  34. $cancel = GETPOST('cancel', 'alpha');
  35. $datepaid = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  36. // Security check
  37. $socid = 0;
  38. if ($user->socid > 0) {
  39. $socid = $user->socid;
  40. } elseif (GETPOSTISSET('socid')) {
  41. $socid = GETPOST('socid', 'int');
  42. }
  43. if (empty($user->rights->loan->write)) {
  44. accessforbidden();
  45. }
  46. $loan = new Loan($db);
  47. $loan->fetch($chid);
  48. $echance = 0;
  49. $ls = new LoanSchedule($db);
  50. // grab all loanschedule
  51. $res = $ls->fetchAll($chid);
  52. if ($res > 0) {
  53. foreach ($ls->lines as $l) {
  54. $echance++; // Count term pos
  55. // last unpaid term
  56. if (empty($l->fk_bank)) {
  57. $line_id = $l->id;
  58. break;
  59. } elseif ($line_id == $l->id) {
  60. // If line_id provided, only count temp pos
  61. break;
  62. }
  63. }
  64. }
  65. // Set current line with last unpaid line (only if shedule is used)
  66. if (!empty($line_id)) {
  67. $line = new LoanSchedule($db);
  68. $res = $line->fetch($line_id);
  69. if ($res > 0) {
  70. $amount_capital = price($line->amount_capital);
  71. $amount_insurance = price($line->amount_insurance);
  72. $amount_interest = price($line->amount_interest);
  73. if (empty($datepaid)) {
  74. $ts_temppaid = $line->datep;
  75. }
  76. }
  77. }
  78. /*
  79. * Actions
  80. */
  81. if ($action == 'add_payment') {
  82. $error = 0;
  83. if ($cancel) {
  84. $loc = DOL_URL_ROOT.'/loan/card.php?id='.$chid;
  85. header("Location: ".$loc);
  86. exit;
  87. }
  88. if (!GETPOST('paymenttype', 'int') > 0) {
  89. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  90. $error++;
  91. }
  92. if ($datepaid == '') {
  93. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  94. $error++;
  95. }
  96. if (isModEnabled("banque") && !GETPOST('accountid', 'int') > 0) {
  97. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  98. $error++;
  99. }
  100. if (!$error) {
  101. $paymentid = 0;
  102. $pay_amount_capital = price2num(GETPOST('amount_capital'));
  103. $pay_amount_insurance = price2num(GETPOST('amount_insurance'));
  104. // User can't set interest him self if schedule is set (else value in schedule can be incoherent)
  105. if (!empty($line)) {
  106. $pay_amount_interest = $line->amount_interest;
  107. } else {
  108. $pay_amount_interest = price2num(GETPOST('amount_interest'));
  109. }
  110. $remaindertopay = price2num(GETPOST('remaindertopay'));
  111. $amount = $pay_amount_capital + $pay_amount_insurance + $pay_amount_interest;
  112. // This term is allready paid
  113. if (!empty($line) && !empty($line->fk_bank)) {
  114. setEventMessages($langs->trans('TermPaidAllreadyPaid'), null, 'errors');
  115. $error++;
  116. }
  117. if (empty($remaindertopay)) {
  118. setEventMessages('Empty sumpaid', null, 'errors');
  119. $error++;
  120. }
  121. if ($amount == 0) {
  122. setEventMessages($langs->trans('ErrorNoPaymentDefined'), null, 'errors');
  123. $error++;
  124. }
  125. if (!$error) {
  126. $db->begin();
  127. // Create a line of payments
  128. $payment = new PaymentLoan($db);
  129. $payment->chid = $chid;
  130. $payment->datep = $datepaid;
  131. $payment->label = $loan->label;
  132. $payment->amount_capital = $pay_amount_capital;
  133. $payment->amount_insurance = $pay_amount_insurance;
  134. $payment->amount_interest = $pay_amount_interest;
  135. $payment->fk_bank = GETPOST('accountid', 'int');
  136. $payment->paymenttype = GETPOST('paymenttype', 'int');
  137. $payment->num_payment = GETPOST('num_payment');
  138. $payment->note_private = GETPOST('note_private', 'restricthtml');
  139. $payment->note_public = GETPOST('note_public', 'restricthtml');
  140. if (!$error) {
  141. $paymentid = $payment->create($user);
  142. if ($paymentid < 0) {
  143. setEventMessages($payment->error, $payment->errors, 'errors');
  144. $error++;
  145. }
  146. }
  147. if (!$error) {
  148. $result = $payment->addPaymentToBank($user, $chid, 'payment_loan', '(LoanPayment)', $payment->fk_bank, '', '');
  149. if (!($result > 0)) {
  150. setEventMessages($payment->error, $payment->errors, 'errors');
  151. $error++;
  152. }
  153. }
  154. // Update loan schedule with payment value
  155. if (!$error && !empty($line)) {
  156. // If payment values are modified, recalculate schedule
  157. if (($line->amount_capital <> $pay_amount_capital) || ($line->amount_insurance <> $pay_amount_insurance) || ($line->amount_interest <> $pay_amount_interest)) {
  158. $arr_term = loanCalcMonthlyPayment(($pay_amount_capital + $pay_amount_interest), $remaindertopay, ($loan->rate / 100), $echance, $loan->nbterm);
  159. foreach ($arr_term as $k => $v) {
  160. // Update fk_bank for current line
  161. if ($k == $echance) {
  162. $ls->lines[$k - 1]->fk_bank = $payment->fk_bank;
  163. $ls->lines[$k - 1]->fk_payment_loan = $payment->id;
  164. }
  165. $ls->lines[$k - 1]->amount_capital = $v['mens'] - $v['interet'];
  166. $ls->lines[$k - 1]->amount_interest = $v['interet'];
  167. $ls->lines[$k - 1]->tms = dol_now();
  168. $ls->lines[$k - 1]->fk_user_modif = $user->id;
  169. $result = $ls->lines[$k - 1]->update($user, 0);
  170. if ($result < 1) {
  171. setEventMessages(null, $ls->errors, 'errors');
  172. $error++;
  173. break;
  174. }
  175. }
  176. } else // Only add fk_bank bank to schedule line (mark as paid)
  177. {
  178. $line->fk_bank = $payment->fk_bank;
  179. $line->fk_payment_loan = $payment->id;
  180. $result = $line->update($user, 0);
  181. if ($result < 1) {
  182. setEventMessages(null, $line->errors, 'errors');
  183. $error++;
  184. }
  185. }
  186. }
  187. if (!$error) {
  188. $db->commit();
  189. $loc = DOL_URL_ROOT.'/loan/card.php?id='.$chid;
  190. header('Location: '.$loc);
  191. exit;
  192. } else {
  193. $db->rollback();
  194. }
  195. }
  196. }
  197. $action = 'create';
  198. }
  199. /*
  200. * View
  201. */
  202. llxHeader();
  203. $form = new Form($db);
  204. // Form to create loan's payment
  205. if ($action == 'create') {
  206. $total = $loan->capital;
  207. print load_fiche_titre($langs->trans("DoPayment"));
  208. $sql = "SELECT SUM(amount_capital) as total";
  209. $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan";
  210. $sql .= " WHERE fk_loan = ".((int) $chid);
  211. $resql = $db->query($sql);
  212. if ($resql) {
  213. $obj = $db->fetch_object($resql);
  214. $sumpaid = $obj->total;
  215. $db->free($resql);
  216. }
  217. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  218. print '<input type="hidden" name="token" value="'.newToken().'">';
  219. print '<input type="hidden" name="id" value="'.$chid.'">';
  220. print '<input type="hidden" name="chid" value="'.$chid.'">';
  221. print '<input type="hidden" name="line_id" value="'.$line_id.'">';
  222. print '<input type="hidden" name="remaindertopay" value="'.($total - $sumpaid).'">';
  223. print '<input type="hidden" name="action" value="add_payment">';
  224. print dol_get_fiche_head();
  225. /*
  226. print '<table class="border centpercent">';
  227. print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td><td colspan="2"><a href="'.DOL_URL_ROOT.'/loan/card.php?id='.$chid.'">'.$chid.'</a></td></tr>';
  228. if ($echance > 0)
  229. {
  230. print '<tr><td>'.$langs->trans("Term").'</td><td colspan="2"><a href="'.DOL_URL_ROOT.'/loan/schedule.php?loanid='.$chid.'#n'.$echance.'">'.$echance.'</a></td></tr>'."\n";
  231. }
  232. print '<tr><td>'.$langs->trans("DateStart").'</td><td colspan="2">'.dol_print_date($loan->datestart, 'day')."</td></tr>\n";
  233. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="2">'.$loan->label."</td></tr>\n";
  234. print '<tr><td>'.$langs->trans("Amount").'</td><td colspan="2">'.price($loan->capital, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  235. print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td colspan="2">'.price($sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  236. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td colspan="2">'.price($total - $sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  237. print '</tr>';
  238. print '</table>';
  239. */
  240. print '<table class="border centpercent">';
  241. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
  242. if (empty($datepaid)) {
  243. if (empty($ts_temppaid)) {
  244. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : dol_now();
  245. } else {
  246. $datepayment = $ts_temppaid;
  247. }
  248. } else {
  249. $datepayment = $datepaid;
  250. }
  251. print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
  252. print "</td>";
  253. print '</tr>';
  254. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
  255. print img_picto('', 'money-bill-alt', 'class="pictofixedwidth"');
  256. $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype", 'alphanohtml') : $loan->fk_typepayment, "paymenttype");
  257. print "</td>\n";
  258. print '</tr>';
  259. print '<tr>';
  260. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  261. print '<td colspan="2">';
  262. print img_picto('', 'bank_account', 'class="pictofixedwidth"');
  263. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $loan->accountid, "accountid", 0, 'courant = '.Account::TYPE_CURRENT, 1); // Show opend bank account list
  264. print '</td></tr>';
  265. // Number
  266. print '<tr><td>'.$langs->trans('Numero');
  267. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  268. print '</td>';
  269. print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td>'."\n";
  270. print "</tr>";
  271. print '<tr>';
  272. print '<td class="tdtop">'.$langs->trans("NotePrivate").'</td>';
  273. print '<td valign="top" colspan="2"><textarea name="note_private" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  274. print '</tr>';
  275. print '<tr>';
  276. print '<td class="tdtop">'.$langs->trans("NotePublic").'</td>';
  277. print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  278. print '</tr>';
  279. print '</table>';
  280. print dol_get_fiche_end();
  281. print '<table class="noborder centpercent">';
  282. print '<tr class="liste_titre">';
  283. print '<td class="left">'.$langs->trans("DateDue").'</td>';
  284. print '<td class="right">'.$langs->trans("LoanCapital").'</td>';
  285. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  286. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  287. print '<td class="right">'.$langs->trans("Amount").'</td>';
  288. print "</tr>\n";
  289. print '<tr class="oddeven">';
  290. if ($loan->datestart > 0) {
  291. print '<td class="left" valign="center">'.dol_print_date($loan->datestart, 'day').'</td>';
  292. } else {
  293. print '<td class="center" valign="center"><b>!!!</b></td>';
  294. }
  295. print '<td class="right" valign="center">'.price($loan->capital)."</td>";
  296. print '<td class="right" valign="center">'.price($sumpaid)."</td>";
  297. print '<td class="right" valign="center">'.price($loan->capital - $sumpaid)."</td>";
  298. print '<td class="right">';
  299. if ($sumpaid < $loan->capital) {
  300. print $langs->trans("LoanCapital").': <input type="text" size="8" name="amount_capital" value="'.(GETPOSTISSET('amount_capital') ?GETPOST('amount_capital') : $amount_capital).'">';
  301. } else {
  302. print '-';
  303. }
  304. print '<br>';
  305. if ($sumpaid < $loan->capital) {
  306. print $langs->trans("Insurance").': <input type="text" size="8" name="amount_insurance" value="'.(GETPOSTISSET('amount_insurance') ?GETPOST('amount_insurance') : $amount_insurance).'">';
  307. } else {
  308. print '-';
  309. }
  310. print '<br>';
  311. if ($sumpaid < $loan->capital) {
  312. print $langs->trans("Interest").': <input type="text" size="8" name="amount_interest" value="'.(GETPOSTISSET('amount_interest') ?GETPOST('amount_interest') : $amount_interest).'" '.(!empty($line) ? 'disabled title="'.$langs->trans('CantModifyInterestIfScheduleIsUsed').'"' : '').'>';
  313. } else {
  314. print '-';
  315. }
  316. print "</td>";
  317. print "</tr>\n";
  318. print '</table>';
  319. print $form->buttonsSaveCancel();
  320. print "</form>\n";
  321. }
  322. llxFooter();
  323. $db->close();