payment.php 10 KB

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