2016-10-24 07:16:26 +02:00
< ? php
2018-10-15 06:53:48 +02:00
/* Copyright ( C ) 2007 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2007 - 2010 Jean Heimburger < jean @ tiaris . info >
* Copyright ( C ) 2011 Juanjo Menent < jmenent @ 2 byte . es >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2012 Regis Houssin < regis . houssin @ inodbox . com >
2018-10-16 06:32:55 +02:00
* Copyright ( C ) 2013 - 2018 Alexandre Spangaro < aspangaro @ zendsi . com >
2018-10-15 06:53:48 +02:00
* Copyright ( C ) 2013 - 2016 Olivier Geffroy < jeff @ jeffinfo . com >
* Copyright ( C ) 2013 - 2016 Florian Henry < florian . henry @ open - concept . pro >
2018-09-09 11:25:59 +02:00
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2016-10-24 07:16:26 +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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
/**
2017-06-08 06:35:09 +02:00
* \file htdocs / accountancy / journal / expensereportsjournal . php
* \ingroup Advanced accountancy
* \brief Page with expense reports journal
2016-10-24 07:16:26 +02:00
*/
require '../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php' ;
2017-06-05 06:44:53 +02:00
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php' ;
2016-10-24 07:16:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php' ;
2018-05-26 23:52:52 +02:00
// Load translation files required by the page
2017-07-15 12:37:31 +02:00
$langs -> loadLangs ( array ( " commercial " , " compta " , " bills " , " other " , " accountancy " , " trips " , " errors " ));
2016-10-24 07:16:26 +02:00
2017-06-05 06:44:53 +02:00
$id_journal = GETPOST ( 'id_journal' , 'int' );
2017-06-08 06:35:09 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2017-06-05 06:44:53 +02:00
2016-10-24 07:16:26 +02:00
$date_startmonth = GETPOST ( 'date_startmonth' );
$date_startday = GETPOST ( 'date_startday' );
$date_startyear = GETPOST ( 'date_startyear' );
$date_endmonth = GETPOST ( 'date_endmonth' );
$date_endday = GETPOST ( 'date_endday' );
$date_endyear = GETPOST ( 'date_endyear' );
2017-07-01 02:22:08 +02:00
$in_bookkeeping = GETPOST ( 'in_bookkeeping' );
2017-07-15 03:41:07 +02:00
if ( $in_bookkeeping == '' ) $in_bookkeeping = 'notyet' ;
2016-10-24 07:16:26 +02:00
$now = dol_now ();
// Security check
if ( $user -> societe_id > 0 )
accessforbidden ();
/*
* Actions
*/
2018-10-16 06:32:55 +02:00
$accountingaccount = new AccountingAccount ( $db );
2017-06-15 19:29:00 +02:00
2017-06-08 06:35:09 +02:00
// Get informations of journal
2017-06-05 06:44:53 +02:00
$accountingjournalstatic = new AccountingJournal ( $db );
$accountingjournalstatic -> fetch ( $id_journal );
$journal = $accountingjournalstatic -> code ;
2017-06-08 06:35:09 +02:00
$journal_label = $accountingjournalstatic -> label ;
2016-10-24 07:16:26 +02:00
$year_current = strftime ( " %Y " , dol_now ());
$pastmonth = strftime ( " %m " , dol_now ()) - 1 ;
$pastmonthyear = $year_current ;
if ( $pastmonth == 0 ) {
$pastmonth = 12 ;
$pastmonthyear -- ;
}
$date_start = dol_mktime ( 0 , 0 , 0 , $date_startmonth , $date_startday , $date_startyear );
$date_end = dol_mktime ( 23 , 59 , 59 , $date_endmonth , $date_endday , $date_endyear );
2018-01-04 15:52:07 +01:00
if ( ! GETPOSTISSET ( 'date_startmonth' ) && ( empty ( $date_start ) || empty ( $date_end ))) // We define date_start and date_end, only if we did not submit the form
2016-10-24 07:16:26 +02:00
{
$date_start = dol_get_first_day ( $pastmonthyear , $pastmonth , false );
$date_end = dol_get_last_day ( $pastmonthyear , $pastmonth , false );
}
2017-06-15 23:58:56 +02:00
$idpays = $mysoc -> country_id ;
2016-10-24 07:16:26 +02:00
$sql = " SELECT er.rowid, er.ref, er.date_debut as de, " ;
2017-07-15 12:37:31 +02:00
$sql .= " erd.rowid as erdid, erd.comments, erd.total_ht, erd.total_tva, erd.total_localtax1, erd.total_localtax2, erd.tva_tx, erd.total_ttc, erd.fk_code_ventilation, erd.vat_src_code, " ;
2017-01-27 10:50:50 +01:00
$sql .= " u.rowid as uid, u.firstname, u.lastname, u.accountancy_code as user_accountancy_account, " ;
2017-06-19 21:39:43 +02:00
$sql .= " f.accountancy_code, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte " ;
2017-06-16 00:49:52 +02:00
//$sql .= " ct.accountancy_code_buy as account_tva";
2016-10-24 07:16:26 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " expensereport_det as erd " ;
2017-06-16 00:49:52 +02:00
//$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva as ct ON erd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'";
2016-10-24 07:16:26 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_type_fees as f ON f.id = erd.fk_c_type_fees " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa ON aa.rowid = erd.fk_code_ventilation " ;
$sql .= " JOIN " . MAIN_DB_PREFIX . " expensereport as er ON er.rowid = erd.fk_expensereport " ;
2017-01-27 10:50:50 +01:00
$sql .= " JOIN " . MAIN_DB_PREFIX . " user as u ON u.rowid = er.fk_user_author " ;
2017-06-16 00:49:52 +02:00
$sql .= " WHERE er.fk_statut > 0 " ;
$sql .= " AND erd.fk_code_ventilation > 0 " ;
2017-06-03 11:49:59 +02:00
$sql .= " AND er.entity IN ( " . getEntity ( 'expensereport' , 0 ) . " ) " ; // We don't share object for accountancy
2016-10-24 07:16:26 +02:00
if ( $date_start && $date_end )
$sql .= " AND er.date_debut >= ' " . $db -> idate ( $date_start ) . " ' AND er.date_debut <= ' " . $db -> idate ( $date_end ) . " ' " ;
2017-12-03 19:50:15 +01:00
// Already in bookkeeping or not
2017-07-15 03:41:07 +02:00
if ( $in_bookkeeping == 'already' )
2017-12-03 19:50:15 +01:00
{
2017-07-15 03:41:07 +02:00
$sql .= " AND er.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping as ab WHERE ab.doc_type='expense_report') " ;
2017-12-03 19:50:15 +01:00
}
2017-07-15 03:41:07 +02:00
if ( $in_bookkeeping == 'notyet' )
2017-12-03 19:50:15 +01:00
{
2017-07-08 20:52:53 +02:00
$sql .= " AND er.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping as ab WHERE ab.doc_type='expense_report') " ;
2017-12-03 19:50:15 +01:00
}
2016-10-24 07:16:26 +02:00
$sql .= " ORDER BY er.date_debut " ;
2017-07-15 12:37:31 +02:00
dol_syslog ( 'accountancy/journal/expensereportsjournal.php' , LOG_DEBUG );
2016-10-24 07:16:26 +02:00
$result = $db -> query ( $sql );
if ( $result ) {
$taber = array ();
$tabht = array ();
$tabtva = array ();
$def_tva = array ();
$tabttc = array ();
2017-07-15 12:37:31 +02:00
$tablocaltax1 = array ();
$tablocaltax2 = array ();
2016-10-24 07:16:26 +02:00
$tabuser = array ();
2017-07-15 12:37:31 +02:00
$num = $db -> num_rows ( $result );
2017-07-15 13:31:14 +02:00
2017-07-15 12:37:31 +02:00
// Variables
$account_salary = ( ! empty ( $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT )) ? $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : 'NotDefined' ;
$account_vat = ( ! empty ( $conf -> global -> ACCOUNTING_VAT_BUY_ACCOUNT )) ? $conf -> global -> ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined' ;
2016-10-24 07:16:26 +02:00
$i = 0 ;
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
// Controls
2016-10-25 07:41:01 +02:00
$compta_user = ( ! empty ( $obj -> user_accountancy_account )) ? $obj -> user_accountancy_account : $account_salary ;
$compta_fees = $obj -> compte ;
2017-06-16 00:49:52 +02:00
$vatdata = getTaxesFromId ( $obj -> tva_tx . ( $obj -> vat_src_code ? ' (' . $obj -> vat_src_code . ')' : '' ), $mysoc , $mysoc , 0 );
2018-03-21 10:59:13 +01:00
$compta_tva = ( ! empty ( $vatdata [ 'accountancy_code_buy' ]) ? $vatdata [ 'accountancy_code_buy' ] : $account_vat );
$compta_localtax1 = ( ! empty ( $vatdata [ 'accountancy_code_buy' ]) ? $vatdata [ 'accountancy_code_buy' ] : $cpttva );
$compta_localtax2 = ( ! empty ( $vatdata [ 'accountancy_code_buy' ]) ? $vatdata [ 'accountancy_code_buy' ] : $cpttva );
2017-07-15 13:31:14 +02:00
2017-06-15 23:58:56 +02:00
// Define array to display all VAT rates that use this accounting account $compta_tva
2017-06-20 02:05:42 +02:00
if ( price2num ( $obj -> tva_tx ) || ! empty ( $obj -> vat_src_code ))
2017-06-15 23:58:56 +02:00
{
2017-06-17 07:54:35 +02:00
$def_tva [ $obj -> rowid ][ $compta_tva ][ vatrate ( $obj -> tva_tx ) . ( $obj -> vat_src_code ? ' (' . $obj -> vat_src_code . ')' : '' )] = ( vatrate ( $obj -> tva_tx ) . ( $obj -> vat_src_code ? ' (' . $obj -> vat_src_code . ')' : '' ));
2017-06-15 23:58:56 +02:00
}
2016-10-24 07:16:26 +02:00
2016-10-25 07:41:01 +02:00
$taber [ $obj -> rowid ][ " date " ] = $db -> jdate ( $obj -> de );
$taber [ $obj -> rowid ][ " ref " ] = $obj -> ref ;
$taber [ $obj -> rowid ][ " comments " ] = $obj -> comments ;
$taber [ $obj -> rowid ][ " fk_expensereportdet " ] = $obj -> erdid ;
2017-07-15 13:31:14 +02:00
2017-07-15 12:37:31 +02:00
// Avoid warnings
if ( ! isset ( $tabttc [ $obj -> rowid ][ $compta_user ])) $tabttc [ $obj -> rowid ][ $compta_user ] = 0 ;
if ( ! isset ( $tabht [ $obj -> rowid ][ $compta_fees ])) $tabht [ $obj -> rowid ][ $compta_fees ] = 0 ;
if ( ! isset ( $tabtva [ $obj -> rowid ][ $compta_tva ])) $tabtva [ $obj -> rowid ][ $compta_tva ] = 0 ;
if ( ! isset ( $tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ])) $tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ] = 0 ;
if ( ! isset ( $tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ])) $tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ] = 0 ;
2017-07-15 13:31:14 +02:00
2016-10-25 07:41:01 +02:00
$tabttc [ $obj -> rowid ][ $compta_user ] += $obj -> total_ttc ;
$tabht [ $obj -> rowid ][ $compta_fees ] += $obj -> total_ht ;
2016-10-24 07:16:26 +02:00
$tabtva [ $obj -> rowid ][ $compta_tva ] += $obj -> total_tva ;
2017-07-15 12:37:31 +02:00
$tablocaltax1 [ $obj -> rowid ][ $compta_localtax1 ] += $obj -> total_localtax1 ;
$tablocaltax2 [ $obj -> rowid ][ $compta_localtax2 ] += $obj -> total_localtax2 ;
2016-10-25 07:41:01 +02:00
$tabuser [ $obj -> rowid ] = array (
'id' => $obj -> uid ,
2017-06-17 07:54:35 +02:00
'name' => dolGetFirstLastname ( $obj -> firstname , $obj -> lastname ),
2016-10-25 07:41:01 +02:00
'user_accountancy_code' => $obj -> user_accountancy_account
2016-10-24 07:16:26 +02:00
);
$i ++ ;
}
} else {
dol_print_error ( $db );
}
// Bookkeeping Write
if ( $action == 'writebookkeeping' ) {
$now = dol_now ();
$error = 0 ;
2017-07-15 12:37:31 +02:00
foreach ( $taber as $key => $val ) // Loop on each expense report
2016-10-24 07:16:26 +02:00
{
2016-10-25 07:41:01 +02:00
$errorforline = 0 ;
2016-10-24 07:16:26 +02:00
2017-11-27 17:12:48 +01:00
$totalcredit = 0 ;
$totaldebit = 0 ;
2017-06-05 06:44:53 +02:00
$db -> begin ();
2017-06-17 07:54:35 +02:00
// Thirdparty
2017-06-05 06:44:53 +02:00
if ( ! $errorforline )
{
foreach ( $tabttc [ $key ] as $k => $mt ) {
if ( $mt ) {
$bookkeeping = new BookKeeping ( $db );
$bookkeeping -> doc_date = $val [ " date " ];
$bookkeeping -> doc_ref = $val [ " ref " ];
$bookkeeping -> date_create = $now ;
$bookkeeping -> doc_type = 'expense_report' ;
$bookkeeping -> fk_doc = $key ;
$bookkeeping -> fk_docdet = $val [ " fk_expensereportdet " ];
2017-06-15 19:29:00 +02:00
$bookkeeping -> subledger_account = $tabuser [ $key ][ 'user_accountancy_code' ];
2018-10-15 06:53:48 +02:00
$bookkeeping -> subledger_label = $tabuser [ $key ][ 'name' ];
2017-06-05 06:44:53 +02:00
$bookkeeping -> numero_compte = $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT ;
2018-10-16 06:32:55 +02:00
2018-10-16 20:10:13 +02:00
$accountingaccount -> fetch ( null , $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT , true );
2018-10-16 06:32:55 +02:00
$bookkeeping -> label_compte = $accountingaccount -> label ;
2017-07-15 12:37:31 +02:00
$bookkeeping -> label_operation = $tabuser [ $key ][ 'name' ];
2017-06-05 06:44:53 +02:00
$bookkeeping -> montant = $mt ;
$bookkeeping -> sens = ( $mt >= 0 ) ? 'C' : 'D' ;
2017-07-25 21:03:30 +02:00
$bookkeeping -> debit = ( $mt <= 0 ) ? - $mt : 0 ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> credit = ( $mt > 0 ) ? $mt : 0 ;
$bookkeeping -> code_journal = $journal ;
2017-06-08 06:35:09 +02:00
$bookkeeping -> journal_label = $journal_label ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> fk_user_author = $user -> id ;
2018-03-06 19:56:08 +01:00
$bookkeeping -> entity = $conf -> entity ;
2017-06-05 06:44:53 +02:00
2017-11-27 17:12:48 +01:00
$totaldebit += $bookkeeping -> debit ;
$totalcredit += $bookkeeping -> credit ;
2017-06-05 06:44:53 +02:00
$result = $bookkeeping -> create ( $user );
if ( $result < 0 ) {
if ( $bookkeeping -> error == 'BookkeepingRecordAlreadyExists' ) // Already exists
{
$error ++ ;
$errorforline ++ ;
2017-07-15 17:32:22 +02:00
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
2017-06-05 06:44:53 +02:00
}
else
{
$error ++ ;
$errorforline ++ ;
setEventMessages ( $bookkeeping -> error , $bookkeeping -> errors , 'errors' );
}
}
}
}
}
2017-06-17 07:54:35 +02:00
// Fees
2017-06-05 06:44:53 +02:00
if ( ! $errorforline )
{
foreach ( $tabht [ $key ] as $k => $mt ) {
if ( $mt ) {
// get compte id and label
if ( $accountingaccount -> fetch ( null , $k , true )) {
$bookkeeping = new BookKeeping ( $db );
$bookkeeping -> doc_date = $val [ " date " ];
$bookkeeping -> doc_ref = $val [ " ref " ];
$bookkeeping -> date_create = $now ;
$bookkeeping -> doc_type = 'expense_report' ;
$bookkeeping -> fk_doc = $key ;
$bookkeeping -> fk_docdet = $val [ " fk_expensereportdet " ];
2017-06-15 19:29:00 +02:00
$bookkeeping -> subledger_account = '' ;
$bookkeeping -> subledger_label = '' ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> numero_compte = $k ;
2018-10-16 06:32:55 +02:00
$bookkeeping -> label_compte = $accountingaccount -> label ;
2017-07-15 12:37:31 +02:00
$bookkeeping -> label_operation = $accountingaccount -> label ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> montant = $mt ;
$bookkeeping -> sens = ( $mt < 0 ) ? 'C' : 'D' ;
$bookkeeping -> debit = ( $mt > 0 ) ? $mt : 0 ;
$bookkeeping -> credit = ( $mt <= 0 ) ? $mt : 0 ;
$bookkeeping -> code_journal = $journal ;
2017-06-08 06:35:09 +02:00
$bookkeeping -> journal_label = $journal_label ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> fk_user_author = $user -> id ;
2018-03-06 19:56:08 +01:00
$bookkeeping -> entity = $conf -> entity ;
2017-06-05 06:44:53 +02:00
2017-11-27 17:12:48 +01:00
$totaldebit += $bookkeeping -> debit ;
$totalcredit += $bookkeeping -> credit ;
2017-06-05 06:44:53 +02:00
$result = $bookkeeping -> create ( $user );
if ( $result < 0 ) {
if ( $bookkeeping -> error == 'BookkeepingRecordAlreadyExists' ) // Already exists
{
$error ++ ;
$errorforline ++ ;
2017-07-15 17:32:22 +02:00
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
2017-06-05 06:44:53 +02:00
}
else
{
$error ++ ;
$errorforline ++ ;
setEventMessages ( $bookkeeping -> error , $bookkeeping -> errors , 'errors' );
}
}
}
}
}
}
2017-06-17 07:54:35 +02:00
// VAT
2016-10-24 07:16:26 +02:00
if ( ! $errorforline )
{
2017-07-15 12:37:31 +02:00
$listoftax = array ( 0 , 1 , 2 );
foreach ( $listoftax as $numtax )
{
$arrayofvat = $tabtva ;
if ( $numtax == 1 ) $arrayofvat = $tablocaltax1 ;
if ( $numtax == 2 ) $arrayofvat = $tablocaltax2 ;
foreach ( $arrayofvat [ $key ] as $k => $mt ) {
if ( $mt ) {
2017-06-05 06:44:53 +02:00
// get compte id and label
$bookkeeping = new BookKeeping ( $db );
$bookkeeping -> doc_date = $val [ " date " ];
$bookkeeping -> doc_ref = $val [ " ref " ];
$bookkeeping -> date_create = $now ;
$bookkeeping -> doc_type = 'expense_report' ;
$bookkeeping -> fk_doc = $key ;
$bookkeeping -> fk_docdet = $val [ " fk_expensereportdet " ];
2017-06-15 19:29:00 +02:00
$bookkeeping -> subledger_account = '' ;
$bookkeeping -> subledger_label = '' ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> numero_compte = $k ;
2018-10-16 06:32:55 +02:00
$accountingaccount -> fetch ( $k , null , true );
$bookkeeping -> label_compte = $accountingaccount -> label ;
2017-10-05 06:56:13 +02:00
$bookkeeping -> label_operation = $langs -> trans ( " VAT " ) . ' ' . join ( ', ' , $def_tva [ $key ][ $k ]) . ' %' ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> montant = $mt ;
$bookkeeping -> sens = ( $mt < 0 ) ? 'C' : 'D' ;
$bookkeeping -> debit = ( $mt > 0 ) ? $mt : 0 ;
$bookkeeping -> credit = ( $mt <= 0 ) ? $mt : 0 ;
$bookkeeping -> code_journal = $journal ;
2017-06-08 06:35:09 +02:00
$bookkeeping -> journal_label = $journal_label ;
2017-06-05 06:44:53 +02:00
$bookkeeping -> fk_user_author = $user -> id ;
2018-03-06 19:56:08 +01:00
$bookkeeping -> entity = $conf -> entity ;
2017-06-05 06:44:53 +02:00
2017-11-27 17:12:48 +01:00
$totaldebit += $bookkeeping -> debit ;
$totalcredit += $bookkeeping -> credit ;
2017-06-05 06:44:53 +02:00
$result = $bookkeeping -> create ( $user );
if ( $result < 0 ) {
if ( $bookkeeping -> error == 'BookkeepingRecordAlreadyExists' ) // Already exists
{
$error ++ ;
$errorforline ++ ;
2017-07-15 17:32:22 +02:00
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
2017-06-05 06:44:53 +02:00
}
else
{
$error ++ ;
$errorforline ++ ;
setEventMessages ( $bookkeeping -> error , $bookkeeping -> errors , 'errors' );
}
}
2017-07-15 12:37:31 +02:00
}
2017-06-05 06:44:53 +02:00
}
}
}
2018-02-12 12:02:01 +01:00
// Protection against a bug on line before
if ( price2num ( $totaldebit ) != price2num ( $totalcredit ))
2017-11-27 17:12:48 +01:00
{
2017-11-27 21:53:22 +01:00
$error ++ ;
2017-11-27 17:12:48 +01:00
$errorforline ++ ;
2017-11-27 21:53:22 +01:00
setEventMessages ( 'Try to insert a non balanced transaction in book for ' . $val [ " ref " ] . '. Canceled. Surely a bug.' , null , 'errors' );
2017-11-27 17:12:48 +01:00
}
2017-06-05 06:44:53 +02:00
if ( ! $errorforline )
{
$db -> commit ();
2016-10-24 07:16:26 +02:00
}
else
{
2017-06-05 06:44:53 +02:00
$db -> rollback ();
2017-06-15 23:58:56 +02:00
if ( $error >= 10 )
{
setEventMessages ( $langs -> trans ( " ErrorTooManyErrorsProcessStopped " ), null , 'errors' );
break ; // Break in the foreach
}
2016-10-24 07:16:26 +02:00
}
}
2017-11-27 21:53:22 +01:00
$tabpay = $taber ;
2017-07-15 12:37:31 +02:00
if ( empty ( $error ) && count ( $tabpay ) > 0 ) {
2017-06-05 06:44:53 +02:00
setEventMessages ( $langs -> trans ( " GeneralLedgerIsWritten " ), null , 'mesgs' );
2016-10-24 07:16:26 +02:00
}
elseif ( count ( $tabpay ) == $error )
{
2017-06-05 06:44:53 +02:00
setEventMessages ( $langs -> trans ( " NoNewRecordSaved " ), null , 'warnings' );
2016-10-24 07:16:26 +02:00
}
else
{
2017-06-05 06:44:53 +02:00
setEventMessages ( $langs -> trans ( " GeneralLedgerSomeRecordWasNotRecorded " ), null , 'warnings' );
2016-10-24 07:16:26 +02:00
}
2016-10-25 07:41:01 +02:00
2016-10-24 07:16:26 +02:00
$action = '' ;
2017-07-15 13:31:14 +02:00
// Must reload data, so we make a redirect
if ( count ( $tabpay ) != $error )
{
$param = 'id_journal=' . $id_journal ;
$param .= '&date_startday=' . $date_startday ;
$param .= '&date_startmonth=' . $date_startmonth ;
$param .= '&date_startyear=' . $date_startyear ;
$param .= '&date_endday=' . $date_endday ;
$param .= '&date_endmonth=' . $date_endmonth ;
$param .= '&date_endyear=' . $date_endyear ;
2017-11-27 21:53:22 +01:00
$param .= '&in_bookkeeping=' . $in_bookkeeping ;
2018-10-10 20:01:01 +02:00
2017-07-15 13:31:14 +02:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . ( $param ? '?' . $param : '' ));
exit ;
}
2016-10-24 07:16:26 +02:00
}
2018-10-10 20:01:01 +02:00
2016-10-24 07:16:26 +02:00
/*
* View
*/
$form = new Form ( $db );
2016-10-25 07:41:01 +02:00
$userstatic = new User ( $db );
2016-10-24 07:16:26 +02:00
// Export
2018-04-07 06:21:49 +02:00
/* if ( $action == 'exportcsv' ) { // ISO and not UTF8 !
2016-10-24 07:16:26 +02:00
$sep = $conf -> global -> ACCOUNTING_EXPORT_SEPARATORCSV ;
2018-04-07 06:21:49 +02:00
$filename = 'journal' ;
2018-11-01 07:24:48 +01:00
$type_export = 'journal' ;
2016-10-24 07:16:26 +02:00
include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php' ;
// Model Cegid Expert Export
if ( $conf -> global -> ACCOUNTING_EXPORT_MODELCSV == 2 ) {
$sep = " ; " ;
foreach ( $taber as $key => $val ) {
$date = dol_print_date ( $val [ " date " ], '%d%m%Y' );
2016-10-25 07:41:01 +02:00
// Fees
2016-10-24 07:16:26 +02:00
foreach ( $tabht [ $key ] as $k => $mt ) {
$userstatic -> id = $tabuser [ $key ][ 'id' ];
$userstatic -> name = $tabuser [ $key ][ 'name' ];
$userstatic -> client = $tabuser [ $key ][ 'code_client' ];
if ( $mt ) {
print $date . $sep ;
print $journal . $sep ;
print length_accountg ( html_entity_decode ( $k )) . $sep ;
print $sep ;
print ( $mt < 0 ? 'C' : 'D' ) . $sep ;
print ( $mt <= 0 ? price ( - $mt ) : $mt ) . $sep ;
2016-10-25 07:41:01 +02:00
print dol_trunc ( $val [ " comments " ], 32 ) . $sep ;
2016-10-24 07:16:26 +02:00
print $val [ " ref " ];
print " \n " ;
}
}
// VAT
foreach ( $tabtva [ $key ] as $k => $mt ) {
if ( $mt ) {
print $date . $sep ;
print $journal . $sep ;
print length_accountg ( html_entity_decode ( $k )) . $sep ;
print $sep ;
print ( $mt < 0 ? 'C' : 'D' ) . $sep ;
print ( $mt <= 0 ? price ( - $mt ) : $mt ) . $sep ;
print $langs -> trans ( " VAT " ) . $sep ;
print $val [ " ref " ];
print " \n " ;
}
}
foreach ( $tabttc [ $key ] as $k => $mt ) {
print $date . $sep ;
print $journal . $sep ;
2016-10-25 07:41:01 +02:00
print length_accountg ( $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT ) . $sep ;
2016-10-24 07:16:26 +02:00
print length_accounta ( html_entity_decode ( $k )) . $sep ;
print ( $mt < 0 ? 'D' : 'C' ) . $sep ;
print ( $mt <= 0 ? price ( - $mt ) : $mt ) . $sep ;
2016-10-25 07:41:01 +02:00
print $userstatic -> name . $sep ;
2016-10-24 07:16:26 +02:00
print $val [ " ref " ];
print " \n " ;
}
}
} elseif ( $conf -> global -> ACCOUNTING_EXPORT_MODELCSV == 1 ) {
// Model Classic Export
foreach ( $taber as $key => $val ) {
$date = dol_print_date ( $val [ " date " ], 'day' );
2016-10-25 07:41:01 +02:00
$userstatic -> id = $tabuser [ $key ][ 'id' ];
$userstatic -> name = $tabuser [ $key ][ 'name' ];
2016-10-24 07:16:26 +02:00
2016-10-25 07:41:01 +02:00
// Fees
2016-10-24 07:16:26 +02:00
foreach ( $tabht [ $key ] as $k => $mt ) {
$accountingaccount = new AccountingAccount ( $db );
2016-12-10 12:59:10 +01:00
$accountingaccount -> fetch ( null , $k , true );
2016-10-24 07:16:26 +02:00
if ( $mt ) {
print '"' . $date . '"' . $sep ;
print '"' . $val [ " ref " ] . '"' . $sep ;
print '"' . length_accountg ( html_entity_decode ( $k )) . '"' . $sep ;
2016-10-25 07:41:01 +02:00
print '"' . dol_trunc ( $accountingaccount -> label , 32 ) . '"' . $sep ;
2016-10-24 07:16:26 +02:00
print '"' . ( $mt >= 0 ? price ( $mt ) : '' ) . '"' . $sep ;
print '"' . ( $mt < 0 ? price ( - $mt ) : '' ) . '"' ;
print " \n " ;
}
}
// VAT
foreach ( $tabtva [ $key ] as $k => $mt ) {
if ( $mt ) {
print '"' . $date . '"' . $sep ;
print '"' . $val [ " ref " ] . '"' . $sep ;
print '"' . length_accountg ( html_entity_decode ( $k )) . '"' . $sep ;
2016-10-25 07:41:01 +02:00
print '"' . dol_trunc ( $langs -> trans ( " VAT " )) . '"' . $sep ;
2016-10-24 07:16:26 +02:00
print '"' . ( $mt >= 0 ? price ( $mt ) : '' ) . '"' . $sep ;
print '"' . ( $mt < 0 ? price ( - $mt ) : '' ) . '"' ;
print " \n " ;
}
}
// Third party
foreach ( $tabttc [ $key ] as $k => $mt ) {
print '"' . $date . '"' . $sep ;
print '"' . $val [ " ref " ] . '"' . $sep ;
print '"' . length_accounta ( html_entity_decode ( $k )) . '"' . $sep ;
2016-10-25 07:41:01 +02:00
print '"' . dol_trunc ( $userstatic -> name ) . '"' . $sep ;
2016-10-24 07:16:26 +02:00
print '"' . ( $mt < 0 ? price ( - $mt ) : '' ) . '"' . $sep ;
print '"' . ( $mt >= 0 ? price ( $mt ) : '' ) . '"' ;
}
print " \n " ;
}
}
}
2017-04-29 14:53:29 +02:00
*/
2016-10-24 07:16:26 +02:00
if ( empty ( $action ) || $action == 'view' ) {
2016-10-25 07:41:01 +02:00
llxHeader ( '' , $langs -> trans ( " ExpenseReportsJournal " ));
2016-10-24 07:16:26 +02:00
2018-06-05 20:37:39 +02:00
$nom = $langs -> trans ( " ExpenseReportsJournal " ) . ' | ' . $accountingjournalstatic -> getNomUrl ( 0 , 1 , 1 , '' , 1 );
2016-10-24 07:16:26 +02:00
$nomlink = '' ;
$periodlink = '' ;
$exportlink = '' ;
2017-09-05 09:43:22 +02:00
$builddate = dol_now ();
2016-10-24 07:16:26 +02:00
$description .= $langs -> trans ( " DescJournalOnlyBindedVisible " ) . '<br>' ;
2018-03-26 14:13:18 +02:00
$listofchoices = array ( 'notyet' => $langs -> trans ( " NotYetInGeneralLedger " ), 'already' => $langs -> trans ( " AlreadyInGeneralLedger " ));
2018-09-09 11:25:59 +02:00
$period = $form -> selectDate ( $date_start ? $date_start :- 1 , 'date_start' , 0 , 0 , 0 , '' , 1 , 0 ) . ' - ' . $form -> selectDate ( $date_end ? $date_end :- 1 , 'date_end' , 0 , 0 , 0 , '' , 1 , 0 );
$period .= ' - ' . $langs -> trans ( " JournalizationInLedgerStatus " ) . ' ' . $form -> selectarray ( 'in_bookkeeping' , $listofchoices , $in_bookkeeping , 1 );
2016-10-24 07:16:26 +02:00
2017-06-05 06:44:53 +02:00
$varlink = 'id_journal=' . $id_journal ;
journalHead ( $nom , $nomlink , $period , $periodlink , $description , $builddate , $exportlink , array ( 'action' => '' ), '' , $varlink );
2017-04-29 14:53:29 +02:00
2017-07-15 12:37:31 +02:00
// Button to write into Ledger
if ( empty ( $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT ) || $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1' ) {
2017-12-09 06:00:13 +01:00
print '<br>' . img_warning () . ' ' . $langs -> trans ( " SomeMandatoryStepsOfSetupWereNotDone " );
print ' : ' . $langs -> trans ( " AccountancyAreaDescMisc " , 4 , '<strong>' . $langs -> transnoentitiesnoconv ( " MenuAccountancy " ) . '-' . $langs -> transnoentitiesnoconv ( " MenuAccountancy " ) . '-' . $langs -> transnoentitiesnoconv ( " Setup " ) . " - " . $langs -> transnoentitiesnoconv ( " MenuDefaultAccounts " ) . '</strong>' );
2017-07-15 12:37:31 +02:00
}
2017-06-15 19:29:00 +02:00
print '<div class="tabsAction tabsActionNoBottom">' ;
2017-07-15 12:37:31 +02:00
if ( empty ( $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT ) || $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1' ) {
2018-11-13 21:40:17 +01:00
print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag ( $langs -> trans ( " SomeMandatoryStepsOfSetupWereNotDone " )) . '" value="' . $langs -> trans ( " WriteBookKeeping " ) . '" />' ;
2017-07-15 12:37:31 +02:00
}
else {
2017-11-28 11:01:02 +01:00
if ( $in_bookkeeping == 'notyet' ) print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs -> trans ( " WriteBookKeeping " ) . '" onclick="writebookkeeping();" />' ;
2018-11-13 21:40:17 +01:00
else print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs -> trans ( " WriteBookKeeping " ) . '</a>' ;
2017-07-15 12:37:31 +02:00
}
2017-09-12 15:10:51 +02:00
//print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
2017-06-05 06:44:53 +02:00
print '</div>' ;
2017-09-12 15:10:51 +02:00
// TODO Avoid using js. We can use a direct link with $param
2016-10-24 07:16:26 +02:00
print '
< script type = " text/javascript " >
function launch_export () {
2018-01-22 03:07:11 +01:00
$ ( " div.fiche form input[name= \" action \" ] " ) . val ( " exportcsv " );
$ ( " div.fiche form input[type= \" submit \" ] " ) . click ();
$ ( " div.fiche form input[name= \" action \" ] " ) . val ( " " );
2016-10-24 07:16:26 +02:00
}
function writebookkeeping () {
2018-01-22 03:07:11 +01:00
console . log ( " click on writebookkeeping " );
$ ( " div.fiche form input[name= \" action \" ] " ) . val ( " writebookkeeping " );
$ ( " div.fiche form input[type= \" submit \" ] " ) . click ();
$ ( " div.fiche form input[name= \" action \" ] " ) . val ( " " );
2016-10-24 07:16:26 +02:00
}
</ script > ' ;
/*
* Show result array
*/
2017-04-29 15:12:45 +02:00
print '<br>' ;
2016-10-24 07:16:26 +02:00
$i = 0 ;
2017-12-09 06:00:13 +01:00
print '<div class="div-table-responsive">' ;
2016-10-24 07:16:26 +02:00
print " <table class= \" noborder \" width= \" 100% \" > " ;
print " <tr class= \" liste_titre \" > " ;
print " <td></td> " ;
print " <td> " . $langs -> trans ( " Date " ) . " </td> " ;
2016-10-25 07:41:01 +02:00
print " <td> " . $langs -> trans ( " Piece " ) . ' (' . $langs -> trans ( " ExpenseReportRef " ) . " )</td> " ;
2017-07-15 12:37:31 +02:00
print " <td> " . $langs -> trans ( " AccountAccounting " ) . " </td> " ;
print " <td> " . $langs -> trans ( " SubledgerAccount " ) . " </td> " ;
2017-10-05 06:56:13 +02:00
print " <td> " . $langs -> trans ( " LabelOperation " ) . " </td> " ;
2016-10-25 07:41:01 +02:00
print " <td align='right'> " . $langs -> trans ( " Debit " ) . " </td> " ;
print " <td align='right'> " . $langs -> trans ( " Credit " ) . " </td> " ;
2016-10-24 07:16:26 +02:00
print " </tr> \n " ;
$r = '' ;
2016-10-25 07:41:01 +02:00
$expensereportstatic = new ExpenseReport ( $db );
$expensereportlinestatic = new ExpenseReportLine ( $db );
2016-10-24 07:16:26 +02:00
foreach ( $taber as $key => $val ) {
2016-10-25 07:41:01 +02:00
$expensereportstatic -> id = $key ;
$expensereportstatic -> ref = $val [ " ref " ];
$expensereportlinestatic -> comments = html_entity_decode ( dol_trunc ( $val [ " comments " ], 32 ));
2016-10-24 07:16:26 +02:00
$date = dol_print_date ( $val [ " date " ], 'day' );
2016-10-25 07:41:01 +02:00
// Fees
2016-10-24 07:16:26 +02:00
foreach ( $tabht [ $key ] as $k => $mt ) {
$accountingaccount = new AccountingAccount ( $db );
2016-12-10 12:59:10 +01:00
$accountingaccount -> fetch ( null , $k , true );
2016-10-24 07:16:26 +02:00
if ( $mt ) {
2017-04-11 14:35:10 +02:00
print '<tr class="oddeven">' ;
2016-10-25 07:41:01 +02:00
print " <td><!-- Fees --></td> " ;
2016-10-24 07:16:26 +02:00
print " <td> " . $date . " </td> " ;
2016-10-25 07:41:01 +02:00
print " <td> " . $expensereportstatic -> getNomUrl ( 1 ) . " </td> " ;
$userstatic -> id = $tabuser [ $key ][ 'id' ];
$userstatic -> name = $tabuser [ $key ][ 'name' ];
2017-07-15 12:37:31 +02:00
// Account
2016-10-24 07:16:26 +02:00
print " <td> " ;
2016-10-27 06:04:23 +02:00
$accountoshow = length_accountg ( $k );
if ( empty ( $accountoshow ) || $accountoshow == 'NotDefined' )
{
print '<span class="error">' . $langs -> trans ( " FeeAccountNotDefined " ) . '</span>' ;
}
else print $accountoshow ;
2017-07-15 12:37:31 +02:00
print '</td>' ;
// Subledger account
print " <td> " ;
print '</td>' ;
2016-10-25 07:41:01 +02:00
$userstatic -> id = $tabuser [ $key ][ 'id' ];
$userstatic -> name = $tabuser [ $key ][ 'name' ];
2016-10-27 06:04:23 +02:00
print " <td> " . $userstatic -> getNomUrl ( 0 , 'user' , 16 ) . ' - ' . $accountingaccount -> label . " </td> " ;
2016-10-24 07:16:26 +02:00
print '<td align="right">' . ( $mt >= 0 ? price ( $mt ) : '' ) . " </td> " ;
print '<td align="right">' . ( $mt < 0 ? price ( - $mt ) : '' ) . " </td> " ;
print " </tr> " ;
}
}
2016-10-25 07:41:01 +02:00
2016-10-24 07:16:26 +02:00
// Third party
foreach ( $tabttc [ $key ] as $k => $mt ) {
2017-06-05 06:44:53 +02:00
print '<tr class="oddeven">' ;
print " <td><!-- Thirdparty --></td> " ;
print " <td> " . $date . " </td> " ;
2016-10-25 07:41:01 +02:00
print " <td> " . $expensereportstatic -> getNomUrl ( 1 ) . " </td> " ;
$userstatic -> id = $tabuser [ $key ][ 'id' ];
$userstatic -> name = $tabuser [ $key ][ 'name' ];
2017-07-15 12:37:31 +02:00
// Account
2016-10-24 07:16:26 +02:00
print " <td> " ;
2017-07-15 12:37:31 +02:00
$accountoshow = length_accounta ( $conf -> global -> SALARIES_ACCOUNTING_ACCOUNT_PAYMENT );
2016-10-24 07:16:26 +02:00
if ( empty ( $accountoshow ) || $accountoshow == 'NotDefined' )
{
2017-07-15 12:37:31 +02:00
print '<span class="error">' . $langs -> trans ( " MainAccountForUsersNotDefined " ) . '</span>' ;
2016-10-24 07:16:26 +02:00
}
else print $accountoshow ;
2016-10-27 06:04:23 +02:00
print " </td> " ;
2017-07-15 12:37:31 +02:00
// Subledger account
print " <td> " ;
$accountoshow = length_accounta ( $k );
if ( empty ( $accountoshow ) || $accountoshow == 'NotDefined' )
{
print '<span class="error">' . $langs -> trans ( " UserAccountNotDefined " ) . '</span>' ;
}
else print $accountoshow ;
print '</td>' ;
2017-07-08 20:52:53 +02:00
print " <td> " . $userstatic -> getNomUrl ( 0 , 'user' , 16 ) . ' - ' . $langs -> trans ( " SubledgerAccount " ) . " </td> " ;
2016-10-24 07:16:26 +02:00
print '<td align="right">' . ( $mt < 0 ? - price ( - $mt ) : '' ) . " </td> " ;
print '<td align="right">' . ( $mt >= 0 ? price ( $mt ) : '' ) . " </td> " ;
2017-06-05 06:44:53 +02:00
print " </tr> " ;
2016-10-24 07:16:26 +02:00
}
2017-07-15 12:37:31 +02:00
// VAT
$listoftax = array ( 0 , 1 , 2 );
foreach ( $listoftax as $numtax ) {
$arrayofvat = $tabtva ;
if ( $numtax == 1 ) $arrayofvat = $tablocaltax1 ;
if ( $numtax == 2 ) $arrayofvat = $tablocaltax2 ;
foreach ( $arrayofvat [ $key ] as $k => $mt ) {
if ( $mt ) {
print '<tr class="oddeven">' ;
print " <td><!-- VAT --></td> " ;
print " <td> " . $date . " </td> " ;
print " <td> " . $expensereportstatic -> getNomUrl ( 1 ) . " </td> " ;
// Account
print " <td> " ;
$accountoshow = length_accountg ( $k );
if ( empty ( $accountoshow ) || $accountoshow == 'NotDefined' )
{
print '<span class="error">' . $langs -> trans ( " VATAccountNotDefined " ) . '</span>' ;
}
else print $accountoshow ;
print " </td> " ;
// Subledger account
print " <td> " ;
print '</td>' ;
2017-10-05 06:56:13 +02:00
print " <td> " . $userstatic -> getNomUrl ( 0 , 'user' , 16 ) . ' - ' . $langs -> trans ( " VAT " ) . ' ' . join ( ', ' , $def_tva [ $key ][ $k ]) . ' %' . ( $numtax ? ' - Localtax ' . $numtax : '' );
2017-07-15 12:37:31 +02:00
print " </td> " ;
print '<td align="right">' . ( $mt >= 0 ? price ( $mt ) : '' ) . " </td> " ;
print '<td align="right">' . ( $mt < 0 ? price ( - $mt ) : '' ) . " </td> " ;
print " </tr> " ;
}
}
}
2016-10-24 07:16:26 +02:00
}
print " </table> " ;
2017-09-05 20:42:34 +02:00
print '</div>' ;
2016-10-24 07:16:26 +02:00
// End of page
llxFooter ();
}
$db -> close ();