dolibarr/htdocs/expensereport/payment/payment.php

341 lines
11 KiB
PHP
Raw Normal View History

2015-06-16 05:45:47 +02:00
<?php
2019-01-28 21:39:22 +01:00
/* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
2018-09-09 10:40:00 +02:00
* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
2015-06-16 05:45:47 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2015-06-16 05:45:47 +02:00
*/
/**
* \file htdocs/expensereport/payment/payment.php
* \ingroup Expense Report
* \brief Page to add payment of an expense report
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
2018-05-26 20:51:17 +02:00
// Load translation files required by the page
$langs->loadLangs(array('bills', 'banks', 'trips'));
2015-06-16 05:45:47 +02:00
$id = GETPOST("id", 'int');
$ref = GETPOST('ref', 'alpha');
$action = GETPOST('action', 'aZ09');
2015-06-16 05:45:47 +02:00
$amounts = array();
$accountid = GETPOST('accountid', 'int');
2021-03-24 19:53:31 +01:00
$cancel = GETPOST('cancel');
2015-06-16 05:45:47 +02:00
// Security check
$socid = 0;
2021-02-25 22:45:02 +01:00
if ($user->socid > 0) {
$socid = $user->socid;
2015-06-16 05:45:47 +02:00
}
/*
* Actions
*/
2021-02-25 22:45:02 +01:00
if ($action == 'add_payment') {
$error = 0;
2015-06-16 05:45:47 +02:00
2021-03-24 19:53:31 +01:00
if ($cancel) {
2018-03-27 12:09:34 +02:00
$loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
2015-06-16 05:45:47 +02:00
header("Location: ".$loc);
exit;
}
$expensereport = new ExpenseReport($db);
2018-03-27 12:09:34 +02:00
$result = $expensereport->fetch($id, $ref);
2021-02-25 22:45:02 +01:00
if (!$result) {
2018-03-27 12:09:34 +02:00
$error++;
setEventMessages($expensereport->error, $expensereport->errors, 'errors');
}
2021-03-24 19:53:31 +01:00
$datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
2015-06-16 05:45:47 +02:00
2021-03-24 19:53:31 +01:00
if (!(GETPOST("fk_typepayment", 'int') > 0)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
2015-06-16 05:45:47 +02:00
$error++;
}
2021-02-25 22:45:02 +01:00
if ($datepaid == '') {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
2015-06-16 05:45:47 +02:00
$error++;
}
2022-08-12 15:45:27 +02:00
2021-02-25 22:45:02 +01:00
if (!empty($conf->banque->enabled) && !($accountid > 0)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
$error++;
}
2021-02-25 22:45:02 +01:00
if (!$error) {
2015-06-16 05:45:47 +02:00
$paymentid = 0;
2015-08-28 20:11:16 +02:00
$total = 0;
2015-06-16 05:45:47 +02:00
// Read possible payments
2021-02-25 22:45:02 +01:00
foreach ($_POST as $key => $value) {
if (substr($key, 0, 7) == 'amount_') {
2022-08-12 15:45:27 +02:00
if (GETPOST($key)) {
$amounts[$expensereport->fk_user_author] = price2num(GETPOST($key));
$total += price2num(GETPOST($key));
}
2015-06-16 05:45:47 +02:00
}
}
2021-02-25 22:45:02 +01:00
if (count($amounts) <= 0) {
$error++;
2022-08-12 15:45:27 +02:00
setEventMessages('ErrorNoPaymentDefined', null, 'errors');
}
2021-02-25 22:45:02 +01:00
if (!$error) {
$db->begin();
// Create a line of payments
$payment = new PaymentExpenseReport($db);
$payment->fk_expensereport = $expensereport->id;
$payment->datepaid = $datepaid;
$payment->amounts = $amounts; // Tableau de montant
$payment->total = $total;
$payment->fk_typepayment = GETPOST("fk_typepayment", 'int');
$payment->num_payment = GETPOST("num_payment", 'alphanothtml');
$payment->note_public = GETPOST("note_public", 'restricthtml');
2022-08-12 15:45:27 +02:00
$payment->fk_bank = $accountid;
2021-02-25 22:45:02 +01:00
if (!$error) {
$paymentid = $payment->create($user);
2021-02-25 22:45:02 +01:00
if ($paymentid < 0) {
setEventMessages($payment->error, $payment->errors, 'errors');
$error++;
}
}
2021-02-25 22:45:02 +01:00
if (!$error) {
$result = $payment->addPaymentToBank($user, 'payment_expensereport', '(ExpenseReportPayment)', $accountid, '', '');
2022-08-12 15:45:27 +02:00
if ($result <= 0) {
setEventMessages($payment->error, $payment->errors, 'errors');
$error++;
}
}
if (!$error) {
$payment->fetch($paymentid);
if ($expensereport->total_ttc - $payment->amount == 0) {
2021-02-09 10:26:17 +01:00
$result = $expensereport->setPaid($expensereport->id, $user);
2022-08-12 15:54:50 +02:00
if (!($result > 0)) {
setEventMessages($payment->error, $payment->errors, 'errors');
$error++;
}
}
}
2021-02-25 22:45:02 +01:00
if (!$error) {
$db->commit();
$loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
header('Location: '.$loc);
exit;
} else {
$db->rollback();
}
}
2015-06-16 05:45:47 +02:00
}
$action = 'create';
2015-06-16 05:45:47 +02:00
}
/*
* View
*/
llxHeader();
$form = new Form($db);
2015-06-16 05:45:47 +02:00
// Form to create expense report payment
2021-02-25 22:45:02 +01:00
if ($action == 'create' || empty($action)) {
2015-06-16 05:45:47 +02:00
$expensereport = new ExpenseReport($db);
2018-03-27 12:09:34 +02:00
$expensereport->fetch($id, $ref);
2015-06-16 05:45:47 +02:00
2015-06-17 07:19:25 +02:00
$total = $expensereport->total_ttc;
2015-06-16 05:45:47 +02:00
// autofill remainder amount
if (!empty($conf->use_javascript_ajax)) {
print "\n".'<script type="text/javascript">';
//Add js for AutoFill
print ' $(document).ready(function () {';
print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
var amount = $(this).data("value");
document.getElementById($(this).data(\'rowid\')).value = amount ;
});';
print "\t});\n";
print "</script>\n";
}
print load_fiche_titre($langs->trans("DoPayment"));
2015-06-16 05:45:47 +02:00
print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
print '<input type="hidden" name="token" value="'.newToken().'">';
2018-03-27 12:09:34 +02:00
print '<input type="hidden" name="id" value="'.$expensereport->id.'">';
print '<input type="hidden" name="chid" value="'.$expensereport->id.'">';
2015-06-16 05:45:47 +02:00
print '<input type="hidden" name="action" value="add_payment">';
print dol_get_fiche_head(null, '0', '', -1);
2015-06-16 05:45:47 +02:00
$linkback = '';
// $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
2015-06-16 05:45:47 +02:00
dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', '');
2015-06-16 05:45:47 +02:00
print '<div class="fichecenter">';
print '<div class="underbanner clearboth"></div>';
2015-06-16 05:45:47 +02:00
print '<table class="border centpercent">'."\n";
print '<tr><td class="titlefield">'.$langs->trans("Period").'</td><td>'.get_date_range($expensereport->date_debut, $expensereport->date_fin, "", $langs, 0).'</td></tr>';
2022-08-12 15:45:27 +02:00
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>';
2015-06-16 05:45:47 +02:00
$sql = "SELECT sum(p.amount) as total";
$sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e";
$sql .= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".((int) $id);
$sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
2015-06-16 05:45:47 +02:00
$resql = $db->query($sql);
2021-02-25 22:45:02 +01:00
if ($resql) {
$obj = $db->fetch_object($resql);
2015-06-16 05:45:47 +02:00
$sumpaid = $obj->total;
2022-05-15 22:29:19 +02:00
$db->free($resql);
2015-06-16 05:45:47 +02:00
}
2022-08-12 15:45:27 +02:00
print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td><span class="amount">'.price($sumpaid, 0, $langs, 1, -1, -1, $conf->currency).'</span></td></tr>';
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>';
2015-06-16 05:45:47 +02:00
print '</table>';
print '</div>';
2015-06-16 05:45:47 +02:00
print dol_get_fiche_end();
2022-08-12 15:45:27 +02:00
print '<br>';
print dol_get_fiche_head();
print '<table class="border centpercent">'."\n";
print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
2020-03-06 15:05:18 +01:00
$datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
$datepayment = ($datepaid == '' ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $datepaid);
print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1);
2015-06-16 05:45:47 +02:00
print "</td>";
print '</tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
2020-03-06 15:05:18 +01:00
$form->select_types_paiements(GETPOSTISSET("fk_typepayment") ? GETPOST("fk_typepayment", 'alpha') : $expensereport->fk_c_paiement, "fk_typepayment");
2015-06-16 05:45:47 +02:00
print "</td>\n";
print '</tr>';
2021-02-25 22:45:02 +01:00
if (!empty($conf->banque->enabled)) {
print '<tr>';
print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
print '<td colspan="2">';
2022-08-12 15:45:27 +02:00
print img_picto('', 'bank_account', 'class="pictofixedwidth"');
$form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", "int") : 0, "accountid", 0, '', 2); // Show open bank account list
print '</td></tr>';
}
2015-06-16 05:45:47 +02:00
// Number
print '<tr><td>'.$langs->trans('Numero');
print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
print '</td>';
print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
print '<tr>';
2017-01-22 12:13:32 +01:00
print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
2015-06-16 05:45:47 +02:00
print '</tr>';
print '</table>';
2020-10-27 18:19:31 +01:00
print dol_get_fiche_end();
print '<br>';
2015-06-16 05:45:47 +02:00
// List of expenses ereport not already paid completely
2015-06-16 05:45:47 +02:00
$num = 1;
$i = 0;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">';
2015-06-16 05:45:47 +02:00
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("ExpenseReport").'</td>';
2019-03-14 19:06:30 +01:00
print '<td class="right">'.$langs->trans("Amount").'</td>';
print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
print '<td class="center">'.$langs->trans("Amount").'</td>';
2015-06-16 05:45:47 +02:00
print "</tr>\n";
2022-08-12 15:45:27 +02:00
$total_ttc = 0;
$totalrecu = 0;
2015-06-16 05:45:47 +02:00
2021-02-25 22:45:02 +01:00
while ($i < $num) {
2015-06-16 05:45:47 +02:00
$objp = $expensereport;
print '<tr class="oddeven">';
2015-06-16 05:45:47 +02:00
print '<td>'.$expensereport->getNomUrl(1)."</td>";
2019-03-14 19:06:30 +01:00
print '<td class="right">'.price($objp->total_ttc)."</td>";
print '<td class="right">'.price($sumpaid)."</td>";
print '<td class="right">'.price($objp->total_ttc - $sumpaid)."</td>";
print '<td class="center">';
2021-02-25 22:45:02 +01:00
if ($sumpaid < $objp->total_ttc) {
2015-06-16 05:45:47 +02:00
$namef = "amount_".$objp->id;
$nameRemain = "remain_".$objp->id; // autofill remainder amount
2021-02-25 22:45:02 +01:00
if (!empty($conf->use_javascript_ajax)) { // autofill remainder amount
print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->total_ttc - $sumpaid)."'"); // autofill remainder amount
2021-02-25 22:45:02 +01:00
}
$remaintopay = $objp->total_ttc - $sumpaid; // autofill remainder amount
print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">'; // autofill remainder amount
2022-08-12 15:45:27 +02:00
print '<input type="text" class="width75" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST($namef).'">';
2020-05-21 15:05:19 +02:00
} else {
2015-06-16 05:45:47 +02:00
print '-';
}
print "</td>";
print "</tr>\n";
$total_ttc += $objp->total_ttc;
2022-08-12 15:45:27 +02:00
$totalrecu += $sumpaid;
2015-06-16 05:45:47 +02:00
$i++;
}
2021-02-25 22:45:02 +01:00
if ($i > 1) {
2015-06-16 05:45:47 +02:00
// Print total
print '<tr class="oddeven">';
2019-03-14 19:06:30 +01:00
print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
print '<td class="right"><b>'.price($total_ttc).'</b></td>';
print '<td class="right"><b>'.price($totalrecu).'</b></td>';
print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
print '<td class="center">&nbsp;</td>';
2015-06-16 05:45:47 +02:00
print "</tr>\n";
}
print "</table>";
print $form->buttonsSaveCancel();
2015-06-16 05:45:47 +02:00
print "</form>\n";
}
2018-08-02 14:03:50 +02:00
// End of page
2015-06-16 05:45:47 +02:00
llxFooter();
$db->close();