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
2020-03-12 12:45:44 +01:00
$id = GETPOST ( " id " , 'int' );
$ref = GETPOST ( 'ref' , 'alpha' );
$action = GETPOST ( 'action' , 'aZ09' );
2015-06-16 05:45:47 +02:00
$amounts = array ();
2020-03-12 12:45:44 +01:00
$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
2020-03-12 12:45:44 +01:00
$socid = 0 ;
2021-02-25 22:45:02 +01:00
if ( $user -> socid > 0 ) {
2019-10-31 20:46:31 +01:00
$socid = $user -> socid ;
2015-06-16 05:45:47 +02:00
}
/*
* Actions
*/
2021-02-25 22:45:02 +01:00
if ( $action == 'add_payment' ) {
2020-03-12 12:45:44 +01:00
$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 ;
}
2016-07-09 14:31:41 +02:00
$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' );
}
2016-07-09 14:31:41 +02:00
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 )) {
2019-01-27 11:55:16 +01:00
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 == '' ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " Date " )), null , 'errors' );
2015-06-16 05:45:47 +02:00
$error ++ ;
}
2021-02-25 22:45:02 +01:00
if ( ! empty ( $conf -> banque -> enabled ) && ! ( $accountid > 0 )) {
2020-10-27 19:46:07 +01:00
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " AccountToDebit " )), null , 'errors' );
$error ++ ;
}
2017-07-08 21:53:28 +02:00
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_' ) {
2016-07-09 14:31:41 +02:00
$amounts [ $expensereport -> fk_user_author ] = price2num ( $_POST [ $key ]);
2015-08-28 20:11:16 +02:00
$total += price2num ( $_POST [ $key ]);
2015-06-16 05:45:47 +02:00
}
}
2021-02-25 22:45:02 +01:00
if ( count ( $amounts ) <= 0 ) {
2020-10-27 19:46:07 +01:00
$error ++ ;
$errmsg = 'ErrorNoPaymentDefined' ;
}
2021-02-25 22:45:02 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$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' );
2021-02-25 22:45:02 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$paymentid = $payment -> create ( $user );
2021-02-25 22:45:02 +01:00
if ( $paymentid < 0 ) {
2020-10-27 19:46:07 +01:00
setEventMessages ( $payment -> error , $payment -> errors , 'errors' );
$error ++ ;
}
}
2021-02-25 22:45:02 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$result = $payment -> addPaymentToBank ( $user , 'payment_expensereport' , '(ExpenseReportPayment)' , $accountid , '' , '' );
2021-02-25 22:45:02 +01:00
if ( ! $result > 0 ) {
2020-10-27 19:46:07 +01:00
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 );
2020-10-27 19:46:07 +01:00
if ( ! $result > 0 ) {
setEventMessages ( $payment -> error , $payment -> errors , 'errors' );
$error ++ ;
}
}
}
2021-02-25 22:45:02 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$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
}
2020-03-12 12:45:44 +01:00
$action = 'create' ;
2015-06-16 05:45:47 +02:00
}
/*
* View
*/
llxHeader ();
2020-03-12 12:45:44 +01:00
$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
2018-11-29 23:37:07 +01:00
// autofill remainder amount
2020-03-12 12:45:44 +01:00
if ( ! empty ( $conf -> use_javascript_ajax )) {
2019-02-03 14:29:45 +01:00
print " \n " . '<script type="text/javascript" language="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 ;
}); ' ;
2020-10-27 19:46:07 +01:00
print " \t }); \n " ;
print " </script> \n " ;
}
2018-11-29 23:37:07 +01:00
2015-09-24 18:33:48 +02:00
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">' ;
2019-12-18 23:12:31 +01:00
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">' ;
2017-07-08 21:53:28 +02:00
2020-10-27 19:46:07 +01:00
print dol_get_fiche_head ( null , '0' , '' , - 1 );
2015-06-16 05:45:47 +02:00
2020-10-27 19:46:07 +01:00
$linkback = '' ;
// $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
2015-06-16 05:45:47 +02:00
2020-10-27 19:46:07 +01:00
dol_banner_tab ( $expensereport , 'ref' , $linkback , 1 , 'ref' , 'ref' , '' );
2015-06-16 05:45:47 +02:00
2020-10-27 19:46:07 +01:00
print '<div class="fichecenter">' ;
print '<div class="underbanner clearboth"></div>' ;
2015-06-16 05:45:47 +02:00
2020-10-27 19:46:07 +01:00
print '<table class="border centpercent">' . " \n " ;
2017-07-08 21:54:37 +02:00
2019-01-27 11:55:16 +01:00
print '<tr><td class="titlefield">' . $langs -> trans ( " Period " ) . '</td><td>' . get_date_range ( $expensereport -> date_debut , $expensereport -> date_fin , " " , $langs , 0 ) . '</td></tr>' ;
print '<tr><td>' . $langs -> trans ( " Amount " ) . '</td><td>' . price ( $expensereport -> total_ttc , 0 , $outputlangs , 1 , - 1 , - 1 , $conf -> currency ) . '</td></tr>' ;
2015-06-16 05:45:47 +02:00
$sql = " SELECT sum(p.amount) as total " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " payment_expensereport as p, " . MAIN_DB_PREFIX . " expensereport as e " ;
2021-03-30 19:12:07 +02:00
$sql .= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = " . (( int ) $id );
2020-10-27 19:46:07 +01:00
$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 ) {
2020-03-12 12:45:44 +01:00
$obj = $db -> fetch_object ( $resql );
2015-06-16 05:45:47 +02:00
$sumpaid = $obj -> total ;
$db -> free ();
}
2019-01-27 11:55:16 +01:00
print '<tr><td>' . $langs -> trans ( " AlreadyPaid " ) . '</td><td>' . price ( $sumpaid , 0 , $outputlangs , 1 , - 1 , - 1 , $conf -> currency ) . '</td></tr>' ;
2020-03-12 12:45:44 +01:00
print '<tr><td class="tdtop">' . $langs -> trans ( " RemainderToPay " ) . '</td><td>' . price ( $total - $sumpaid , 0 , $outputlangs , 1 , - 1 , - 1 , $conf -> currency ) . '</td></tr>' ;
2015-06-16 05:45:47 +02:00
2020-10-31 14:32:18 +01:00
print '</table>' ;
2017-07-08 21:54:37 +02:00
2020-10-31 14:32:18 +01:00
print '</div>' ;
2015-06-16 05:45:47 +02:00
2020-10-31 14:32:18 +01:00
print dol_get_fiche_end ();
2019-11-02 11:12:57 +01:00
2020-10-31 14:32:18 +01:00
print dol_get_fiche_head ();
2017-07-08 21:54:37 +02:00
2020-10-31 14:32:18 +01:00
print '<table class="border centpercent">' . " \n " ;
2017-07-08 21:54:37 +02:00
2020-10-31 14:32:18 +01:00
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' ));
2020-10-22 16:38:59 +02:00
$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 )) {
2020-10-27 19:46:07 +01:00
print '<tr>' ;
print '<td class="fieldrequired">' . $langs -> trans ( 'AccountToDebit' ) . '</td>' ;
print '<td colspan="2">' ;
$form -> select_comptes ( GETPOSTISSET ( " accountid " ) ? GETPOST ( " accountid " , " int " ) : $expensereport -> accountid , " accountid " , 0 , '' , 2 ); // Show open bank account list
print '</td></tr>' ;
2017-02-15 23:22:47 +01:00
}
2017-07-08 21:53:28 +02:00
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>' ;
2019-11-02 11:12:57 +01:00
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 ();
2017-07-08 21:54:37 +02:00
2019-11-02 11:12:57 +01:00
print '<br>' ;
2015-06-16 05:45:47 +02:00
2017-02-15 23:22:47 +01: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">' ;
2019-11-02 11:12:57 +01:00
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 " ;
2020-03-12 12:45:44 +01:00
$total = 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 ;
2017-04-14 11:22:48 +02:00
print '<tr class="oddeven">' ;
2015-06-16 05:45:47 +02:00
2019-11-02 11:12:57 +01: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 ;
2018-11-29 23:37:07 +01:00
$nameRemain = " remain_ " . $objp -> id ; // autofill remainder amount
2021-02-25 22:45:02 +01:00
if ( ! empty ( $conf -> use_javascript_ajax )) { // autofill remainder amount
2019-01-27 11:55:16 +01:00
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
}
2020-03-12 12:45:44 +01:00
$remaintopay = $objp -> total_ttc - $sumpaid ; // autofill remainder amount
2018-11-29 23:37:07 +01:00
print '<input type=hidden class="sum_remain" name="' . $nameRemain . '" value="' . $remaintopay . '">' ; // autofill remainder amount
print '<input type="text" size="8" name="' . $namef . '" id="' . $namef . '">' ;
2020-05-21 15:05:19 +02:00
} else {
2015-06-16 05:45:47 +02:00
print '-' ;
}
print " </td> " ;
print " </tr> \n " ;
2017-07-08 21:54:37 +02:00
2020-03-12 12:45:44 +01:00
$total += $objp -> total ;
$total_ttc += $objp -> total_ttc ;
$totalrecu += $objp -> am ;
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
2017-05-29 13:19:45 +02:00
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"> </td>' ;
2015-06-16 05:45:47 +02:00
print " </tr> \n " ;
}
print " </table> " ;
print '<br><div class="center">' ;
2020-11-19 20:23:38 +01:00
print '<input type="submit" class="button button-save" name="save" value="' . $langs -> trans ( " Save " ) . '">' ;
2015-06-16 05:45:47 +02:00
print ' ' ;
2020-11-23 15:12:52 +01:00
print '<input type="submit" class="button button-cancel" name="cancel" value="' . $langs -> trans ( " Cancel " ) . '">' ;
2015-06-16 05:45:47 +02:00
print '</div>' ;
print " </form> \n " ;
}
2018-08-02 14:03:50 +02:00
// End of page
2015-06-16 05:45:47 +02:00
llxFooter ();
2015-12-11 19:37:12 +01:00
$db -> close ();