payment.php 11 KB

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