payment.php 10 KB

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