payment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.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/expensereport/payment/payment.php
  21. * \ingroup Expense Report
  22. * \brief Page to add payment of an expense report
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('bills', 'banks', 'trips'));
  30. $id = GETPOST("id", 'int');
  31. $ref = GETPOST('ref', 'alpha');
  32. $action = GETPOST('action', 'aZ09');
  33. $amounts = array();
  34. $accountid = GETPOST('accountid', 'int');
  35. $cancel = GETPOST('cancel');
  36. // Security check
  37. $socid = 0;
  38. if ($user->socid > 0) {
  39. $socid = $user->socid;
  40. }
  41. /*
  42. * Actions
  43. */
  44. if ($action == 'add_payment') {
  45. $error = 0;
  46. if ($cancel) {
  47. $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
  48. header("Location: ".$loc);
  49. exit;
  50. }
  51. $expensereport = new ExpenseReport($db);
  52. $result = $expensereport->fetch($id, $ref);
  53. if (!$result) {
  54. $error++;
  55. setEventMessages($expensereport->error, $expensereport->errors, 'errors');
  56. }
  57. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  58. if (!(GETPOST("fk_typepayment", 'int') > 0)) {
  59. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
  60. $error++;
  61. }
  62. if ($datepaid == '') {
  63. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
  64. $error++;
  65. }
  66. if (!empty($conf->banque->enabled) && !($accountid > 0)) {
  67. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
  68. $error++;
  69. }
  70. if (!$error) {
  71. $paymentid = 0;
  72. $total = 0;
  73. // Read possible payments
  74. foreach ($_POST as $key => $value) {
  75. if (substr($key, 0, 7) == 'amount_') {
  76. $amounts[$expensereport->fk_user_author] = price2num(GETPOST($key));
  77. $total += price2num(GETPOST($key));
  78. }
  79. }
  80. if (count($amounts) <= 0) {
  81. $error++;
  82. $errmsg = 'ErrorNoPaymentDefined';
  83. }
  84. if (!$error) {
  85. $db->begin();
  86. // Create a line of payments
  87. $payment = new PaymentExpenseReport($db);
  88. $payment->fk_expensereport = $expensereport->id;
  89. $payment->datepaid = $datepaid;
  90. $payment->amounts = $amounts; // Tableau de montant
  91. $payment->total = $total;
  92. $payment->fk_typepayment = GETPOST("fk_typepayment", 'int');
  93. $payment->num_payment = GETPOST("num_payment", 'alphanothtml');
  94. $payment->note_public = GETPOST("note_public", 'restricthtml');
  95. if (!$error) {
  96. $paymentid = $payment->create($user);
  97. if ($paymentid < 0) {
  98. setEventMessages($payment->error, $payment->errors, 'errors');
  99. $error++;
  100. }
  101. }
  102. if (!$error) {
  103. $result = $payment->addPaymentToBank($user, 'payment_expensereport', '(ExpenseReportPayment)', $accountid, '', '');
  104. if (!$result > 0) {
  105. setEventMessages($payment->error, $payment->errors, 'errors');
  106. $error++;
  107. }
  108. }
  109. if (!$error) {
  110. $payment->fetch($paymentid);
  111. if ($expensereport->total_ttc - $payment->amount == 0) {
  112. $result = $expensereport->setPaid($expensereport->id, $user);
  113. if (!$result > 0) {
  114. setEventMessages($payment->error, $payment->errors, 'errors');
  115. $error++;
  116. }
  117. }
  118. }
  119. if (!$error) {
  120. $db->commit();
  121. $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
  122. header('Location: '.$loc);
  123. exit;
  124. } else {
  125. $db->rollback();
  126. }
  127. }
  128. }
  129. $action = 'create';
  130. }
  131. /*
  132. * View
  133. */
  134. llxHeader();
  135. $form = new Form($db);
  136. // Form to create expense report payment
  137. if ($action == 'create' || empty($action)) {
  138. $expensereport = new ExpenseReport($db);
  139. $expensereport->fetch($id, $ref);
  140. $total = $expensereport->total_ttc;
  141. // autofill remainder amount
  142. if (!empty($conf->use_javascript_ajax)) {
  143. print "\n".'<script type="text/javascript">';
  144. //Add js for AutoFill
  145. print ' $(document).ready(function () {';
  146. print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
  147. var amount = $(this).data("value");
  148. document.getElementById($(this).data(\'rowid\')).value = amount ;
  149. });';
  150. print "\t});\n";
  151. print "</script>\n";
  152. }
  153. print load_fiche_titre($langs->trans("DoPayment"));
  154. print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
  155. print '<input type="hidden" name="token" value="'.newToken().'">';
  156. print '<input type="hidden" name="id" value="'.$expensereport->id.'">';
  157. print '<input type="hidden" name="chid" value="'.$expensereport->id.'">';
  158. print '<input type="hidden" name="action" value="add_payment">';
  159. print dol_get_fiche_head(null, '0', '', -1);
  160. $linkback = '';
  161. // $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
  162. dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', '');
  163. print '<div class="fichecenter">';
  164. print '<div class="underbanner clearboth"></div>';
  165. print '<table class="border centpercent">'."\n";
  166. print '<tr><td class="titlefield">'.$langs->trans("Period").'</td><td>'.get_date_range($expensereport->date_debut, $expensereport->date_fin, "", $langs, 0).'</td></tr>';
  167. print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($expensereport->total_ttc, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  168. $sql = "SELECT sum(p.amount) as total";
  169. $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e";
  170. $sql .= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".((int) $id);
  171. $sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
  172. $resql = $db->query($sql);
  173. if ($resql) {
  174. $obj = $db->fetch_object($resql);
  175. $sumpaid = $obj->total;
  176. $db->free();
  177. }
  178. print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  179. print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total - $sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
  180. print '</table>';
  181. print '</div>';
  182. print dol_get_fiche_end();
  183. print dol_get_fiche_head();
  184. print '<table class="border centpercent">'."\n";
  185. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
  186. $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
  187. $datepayment = ($datepaid == '' ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $datepaid);
  188. print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1);
  189. print "</td>";
  190. print '</tr>';
  191. print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
  192. $form->select_types_paiements(GETPOSTISSET("fk_typepayment") ? GETPOST("fk_typepayment", 'alpha') : $expensereport->fk_c_paiement, "fk_typepayment");
  193. print "</td>\n";
  194. print '</tr>';
  195. if (!empty($conf->banque->enabled)) {
  196. print '<tr>';
  197. print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
  198. print '<td colspan="2">';
  199. $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", "int") : $expensereport->accountid, "accountid", 0, '', 2); // Show open bank account list
  200. print '</td></tr>';
  201. }
  202. // Number
  203. print '<tr><td>'.$langs->trans('Numero');
  204. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  205. print '</td>';
  206. print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
  207. print '<tr>';
  208. print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
  209. print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
  210. print '</tr>';
  211. print '</table>';
  212. print dol_get_fiche_end();
  213. print '<br>';
  214. // List of expenses ereport not already paid completely
  215. $num = 1;
  216. $i = 0;
  217. print '<table class="noborder centpercent">';
  218. print '<tr class="liste_titre">';
  219. print '<td>'.$langs->trans("ExpenseReport").'</td>';
  220. print '<td class="right">'.$langs->trans("Amount").'</td>';
  221. print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
  222. print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
  223. print '<td class="center">'.$langs->trans("Amount").'</td>';
  224. print "</tr>\n";
  225. $total = 0;
  226. $totalrecu = 0;
  227. while ($i < $num) {
  228. $objp = $expensereport;
  229. print '<tr class="oddeven">';
  230. print '<td>'.$expensereport->getNomUrl(1)."</td>";
  231. print '<td class="right">'.price($objp->total_ttc)."</td>";
  232. print '<td class="right">'.price($sumpaid)."</td>";
  233. print '<td class="right">'.price($objp->total_ttc - $sumpaid)."</td>";
  234. print '<td class="center">';
  235. if ($sumpaid < $objp->total_ttc) {
  236. $namef = "amount_".$objp->id;
  237. $nameRemain = "remain_".$objp->id; // autofill remainder amount
  238. if (!empty($conf->use_javascript_ajax)) { // autofill remainder amount
  239. print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->total_ttc - $sumpaid)."'"); // autofill remainder amount
  240. }
  241. $remaintopay = $objp->total_ttc - $sumpaid; // autofill remainder amount
  242. print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">'; // autofill remainder amount
  243. print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'">';
  244. } else {
  245. print '-';
  246. }
  247. print "</td>";
  248. print "</tr>\n";
  249. $total += $objp->total;
  250. $total_ttc += $objp->total_ttc;
  251. $totalrecu += $objp->am;
  252. $i++;
  253. }
  254. if ($i > 1) {
  255. // Print total
  256. print '<tr class="oddeven">';
  257. print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
  258. print '<td class="right"><b>'.price($total_ttc).'</b></td>';
  259. print '<td class="right"><b>'.price($totalrecu).'</b></td>';
  260. print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  261. print '<td class="center">&nbsp;</td>';
  262. print "</tr>\n";
  263. }
  264. print "</table>";
  265. print $form->buttonsSaveCancel();
  266. print "</form>\n";
  267. }
  268. // End of page
  269. llxFooter();
  270. $db->close();