payment.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@zendsi.com>
  3. * Copyright (C) 2015-2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/loan/payment/payment.php
  20. * \ingroup Loan
  21. * \brief Page to add payment of a loan
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. $langs->loadLangs(array("bills","loan"));
  29. $chid=GETPOST('id','int');
  30. $action=GETPOST('action','aZ09');
  31. $cancel=GETPOST('cancel','alpha');
  32. $line_id = GETPOST('line_id', 'int');
  33. $last=GETPOST('last');
  34. // Security check
  35. $socid=0;
  36. if ($user->societe_id > 0)
  37. {
  38. $socid = $user->societe_id;
  39. }
  40. $loan = new Loan($db);
  41. $loan->fetch($chid);
  42. if($last)
  43. {
  44. $ls = new LoanSchedule($db);
  45. // grab all loanschedule
  46. $res = $ls->fetchAll($chid);
  47. if ($res > 0)
  48. {
  49. foreach ($ls->lines as $l)
  50. {
  51. // get the last unpaid loanschedule
  52. if (empty($l->fk_bank))
  53. {
  54. $line_id = $l->id;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. if (!empty($line_id))
  61. {
  62. $line = new LoanSchedule($db);
  63. $res = $line->fetch($line_id);
  64. if ($res > 0){
  65. $amount_capital = price($line->amount_capital);
  66. $amount_insurance = price($line->amount_insurance);
  67. $amount_interest = price($line->amount_interest);
  68. }
  69. }
  70. /*
  71. * Actions
  72. */
  73. if ($action == 'add_payment')
  74. {
  75. $error=0;
  76. if ($cancel)
  77. {
  78. $loc = DOL_URL_ROOT.'/loan/card.php?id='.$chid;
  79. header("Location: ".$loc);
  80. exit;
  81. }
  82. $datepaid = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  83. if (! GETPOST('paymenttype', 'int') > 0)
  84. {
  85. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  86. $error++;
  87. }
  88. if ($datepaid == '')
  89. {
  90. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  91. $error++;
  92. }
  93. if (! empty($conf->banque->enabled) && ! GETPOST('accountid', 'int') > 0)
  94. {
  95. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
  96. $error++;
  97. }
  98. if (! $error)
  99. {
  100. $paymentid = 0;
  101. $amount = GETPOST('amount_capital') + GETPOST('amount_insurance') + GETPOST('amount_interest');
  102. if ($amount == 0)
  103. {
  104. setEventMessages($langs->trans('ErrorNoPaymentDefined'), null, 'errors');
  105. $error++;
  106. }
  107. if (! $error)
  108. {
  109. $db->begin();
  110. // Create a line of payments
  111. $payment = new PaymentLoan($db);
  112. $payment->chid = $chid;
  113. $payment->datep = $datepaid;
  114. $payment->label = $loan->label;
  115. $payment->amount_capital = GETPOST('amount_capital');
  116. $payment->amount_insurance = GETPOST('amount_insurance');
  117. $payment->amount_interest = GETPOST('amount_interest');
  118. $payment->paymenttype = GETPOST('paymenttype', 'int');
  119. $payment->num_payment = GETPOST('num_payment');
  120. $payment->note_private = GETPOST('note_private','none');
  121. $payment->note_public = GETPOST('note_public','none');
  122. if (! $error)
  123. {
  124. $paymentid = $payment->create($user);
  125. if ($paymentid < 0)
  126. {
  127. setEventMessages($payment->error, $payment->errors, 'errors');
  128. $error++;
  129. }
  130. }
  131. if (! $error)
  132. {
  133. $result = $payment->addPaymentToBank($user, $chid, 'payment_loan', '(LoanPayment)', GETPOST('accountid', 'int'), '', '');
  134. if (! $result > 0)
  135. {
  136. setEventMessages($payment->error, $payment->errors, 'errors');
  137. $error++;
  138. }
  139. elseif(isset($line))
  140. {
  141. $line->fk_bank = $payment->fk_bank;
  142. $line->update($user);
  143. }
  144. }
  145. if (! $error)
  146. {
  147. $db->commit();
  148. $loc = DOL_URL_ROOT.'/loan/card.php?id='.$chid;
  149. header('Location: '.$loc);
  150. exit;
  151. }
  152. else
  153. {
  154. $db->rollback();
  155. }
  156. }
  157. }
  158. $action = 'create';
  159. }
  160. /*
  161. * View
  162. */
  163. llxHeader();
  164. $form=new Form($db);
  165. // Form to create loan's payment
  166. if ($action == 'create')
  167. {
  168. $total = $loan->capital;
  169. print load_fiche_titre($langs->trans("DoPayment"));
  170. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  171. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  172. print '<input type="hidden" name="id" value="'.$chid.'">';
  173. print '<input type="hidden" name="chid" value="'.$chid.'">';
  174. print '<input type="hidden" name="line_id" value="'.$line_id.'">';
  175. print '<input type="hidden" name="action" value="add_payment">';
  176. dol_fiche_head();
  177. print '<table cellspacing="0" class="border" width="100%" cellpadding="2">';
  178. print '<tr class="liste_titre"><td colspan="3">'.$langs->trans("Loan").'</td>';
  179. 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>';
  180. print '<tr><td>'.$langs->trans("DateStart").'</td><td colspan="2">'.dol_print_date($loan->datestart,'day')."</td></tr>\n";
  181. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="2">'.$loan->label."</td></tr>\n";
  182. print '<tr><td>'.$langs->trans("Amount").'</td><td colspan="2">'.price($loan->capital,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  183. $sql = "SELECT SUM(amount_capital) as total";
  184. $sql.= " FROM ".MAIN_DB_PREFIX."payment_loan";
  185. $sql.= " WHERE fk_loan = ".$chid;
  186. $resql = $db->query($sql);
  187. if ($resql)
  188. {
  189. $obj=$db->fetch_object($resql);
  190. $sumpaid = $obj->total;
  191. $db->free();
  192. }
  193. print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td colspan="2">'.price($sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  194. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td colspan="2">'.price($total-$sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  195. print '</tr>';
  196. print '</table>';
  197. print '<br>';
  198. print '<table cellspacing="0" class="border" width="100%" cellpadding="2">';
  199. print '<tr class="liste_titre">';
  200. print '<td colspan="3">'.$langs->trans("Payment").'</td>';
  201. print '</tr>';
  202. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
  203. $datepaid = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  204. $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaye):0;
  205. print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
  206. print "</td>";
  207. print '</tr>';
  208. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
  209. $form->select_types_paiements(isset($_POST["paymenttype"])?$_POST["paymenttype"]:$loan->paymenttype, "paymenttype");
  210. print "</td>\n";
  211. print '</tr>';
  212. print '<tr>';
  213. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  214. print '<td colspan="2">';
  215. $form->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$loan->accountid, "accountid", 0, '',1); // Show opend bank account list
  216. print '</td></tr>';
  217. // Number
  218. print '<tr><td>'.$langs->trans('Numero');
  219. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  220. print '</td>';
  221. print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
  222. print '<tr>';
  223. print '<td class="tdtop">'.$langs->trans("NotePrivate").'</td>';
  224. print '<td valign="top" colspan="2"><textarea name="note_private" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  225. print '</tr>';
  226. print '<tr>';
  227. print '<td class="tdtop">'.$langs->trans("NotePublic").'</td>';
  228. print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  229. print '</tr>';
  230. print '</table>';
  231. dol_fiche_end();
  232. print '<table class="noborder" width="100%">';
  233. print '<tr class="liste_titre">';
  234. print '<td class="left">'.$langs->trans("DateDue").'</td>';
  235. print '<td align="right">'.$langs->trans("LoanCapital").'</td>';
  236. print '<td align="right">'.$langs->trans("AlreadyPaid").'</td>';
  237. print '<td align="right">'.$langs->trans("RemainderToPay").'</td>';
  238. print '<td align="right">'.$langs->trans("Amount").'</td>';
  239. print "</tr>\n";
  240. print '<tr class="oddeven">';
  241. if ($loan->datestart > 0)
  242. {
  243. print '<td align="left" valign="center">'.dol_print_date($loan->datestart,'day').'</td>';
  244. }
  245. else
  246. {
  247. print '<td align="center" valign="center"><b>!!!</b></td>';
  248. }
  249. print '<td align="right" valign="center">'.price($loan->capital)."</td>";
  250. print '<td align="right" valign="center">'.price($sumpaid)."</td>";
  251. print '<td align="right" valign="center">'.price($loan->capital - $sumpaid)."</td>";
  252. print '<td align="right">';
  253. if ($sumpaid < $loan->capital)
  254. {
  255. print $langs->trans("LoanCapital") .': <input type="text" size="8" name="amount_capital" value="'.$amount_capital.'">';
  256. }
  257. else
  258. {
  259. print '-';
  260. }
  261. print '<br>';
  262. if ($sumpaid < $loan->capital)
  263. {
  264. print $langs->trans("Insurance") .': <input type="text" size="8" name="amount_insurance" value="'.$amount_insurance.'">';
  265. }
  266. else
  267. {
  268. print '-';
  269. }
  270. print '<br>';
  271. if ($sumpaid < $loan->capital)
  272. {
  273. print $langs->trans("Interest") .': <input type="text" size="8" name="amount_interest" value="'.$amount_interest.'">';
  274. }
  275. else
  276. {
  277. print '-';
  278. }
  279. print "</td>";
  280. print "</tr>\n";
  281. print '</table>';
  282. print '<br><div class="center">';
  283. print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
  284. print '&nbsp; &nbsp;';
  285. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  286. print '</div>';
  287. print "</form>\n";
  288. }
  289. llxFooter();
  290. $db->close();