2014-08-27 07:03:42 +02:00
< ? php
2022-06-17 04:41:15 +02:00
/* Copyright ( C ) 2013 Olivier Geffroy < jeff @ jeffinfo . com >
* Copyright ( C ) 2013 - 2014 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2013 - 2022 Alexandre Spangaro < aspangaro @ open - dsi . fr >
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2014-08-27 07:03:42 +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 />.
2014-08-27 07:03:42 +02:00
*
*/
/**
2017-07-14 00:24:49 +02:00
* \file htdocs / accountancy / customer / index . php
2019-05-13 22:25:15 +02:00
* \ingroup Accountancy ( Double entries )
2017-07-14 00:24:49 +02:00
* \brief Home customer journalization page
2014-08-27 07:03:42 +02:00
*/
2014-12-04 01:29:01 +01:00
2016-10-06 17:43:10 +02:00
require '../../main.inc.php' ;
2019-11-13 19:35:02 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2021-06-04 12:20:52 +02:00
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php' ;
2014-08-27 07:03:42 +02:00
2018-05-26 23:52:52 +02:00
// Load translation files required by the page
2021-06-04 10:04:56 +02:00
$langs -> loadLangs ( array ( " compta " , " bills " , " other " , " accountancy " ));
2014-08-27 07:03:42 +02:00
2021-11-22 03:34:50 +01:00
$validatemonth = GETPOST ( 'validatemonth' , 'int' );
$validateyear = GETPOST ( 'validateyear' , 'int' );
2014-08-27 07:03:42 +02:00
// Security check
2022-08-24 10:34:25 +02:00
if ( ! isModEnabled ( 'accounting' )) {
2017-06-20 06:52:41 +02:00
accessforbidden ();
2016-10-06 17:43:10 +02:00
}
2021-02-22 21:36:42 +01:00
if ( $user -> socid > 0 ) {
2014-08-27 07:03:42 +02:00
accessforbidden ();
2021-02-22 21:36:42 +01:00
}
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> accounting -> bind -> write )) {
2014-08-27 07:03:42 +02:00
accessforbidden ();
2021-02-22 21:36:42 +01:00
}
2016-08-08 14:52:19 +02:00
2021-06-04 12:20:52 +02:00
$accountingAccount = new AccountingAccount ( $db );
2018-03-27 12:31:08 +02:00
2019-11-13 19:35:02 +01:00
$month_start = ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ) : 1 );
2021-02-22 21:36:42 +01:00
if ( GETPOST ( " year " , 'int' )) {
$year_start = GETPOST ( " year " , 'int' );
} else {
2018-03-27 12:31:08 +02:00
$year_start = dol_print_date ( dol_now (), '%Y' );
2021-02-22 21:36:42 +01:00
if ( dol_print_date ( dol_now (), '%m' ) < $month_start ) {
$year_start -- ; // If current month is lower that starting fiscal month, we start last year
}
2018-03-27 12:31:08 +02:00
}
$year_end = $year_start + 1 ;
$month_end = $month_start - 1 ;
2021-02-22 21:36:42 +01:00
if ( $month_end < 1 ) {
2018-03-27 12:31:08 +02:00
$month_end = 12 ;
$year_end -- ;
2014-08-27 07:03:42 +02:00
}
2018-03-27 12:31:08 +02:00
$search_date_start = dol_mktime ( 0 , 0 , 0 , $month_start , 1 , $year_start );
$search_date_end = dol_get_last_day ( $year_end , $month_end );
$year_current = $year_start ;
2014-08-27 07:03:42 +02:00
// Validate History
2019-01-27 11:55:16 +01:00
$action = GETPOST ( 'action' , 'aZ09' );
2016-10-04 13:45:22 +02:00
2019-10-21 03:50:57 +02:00
$chartaccountcode = dol_getIdFromCode ( $db , $conf -> global -> CHARTOFACCOUNTS , 'accounting_system' , 'rowid' , 'pcg_version' );
2021-10-11 12:50:16 +02:00
// Security check
2022-08-24 10:34:25 +02:00
if ( ! isModEnabled ( 'accounting' )) {
2021-10-11 12:50:16 +02:00
accessforbidden ();
}
if ( $user -> socid > 0 ) {
accessforbidden ();
}
if ( empty ( $user -> rights -> accounting -> mouvements -> lire )) {
accessforbidden ();
}
2016-10-04 13:45:22 +02:00
/*
* Actions
*/
2021-10-11 12:50:16 +02:00
if (( $action == 'clean' || $action == 'validatehistory' ) && $user -> rights -> accounting -> bind -> write ) {
2021-12-13 19:04:25 +01:00
// Clean database by removing binding done on non existing or no more existing accounts
2018-06-18 13:14:35 +02:00
$db -> begin ();
2019-11-13 19:35:02 +01:00
$sql1 = " UPDATE " . MAIN_DB_PREFIX . " facturedet as fd " ;
2018-06-18 13:14:35 +02:00
$sql1 .= " SET fk_code_ventilation = 0 " ;
$sql1 .= ' WHERE fd.fk_code_ventilation NOT IN' ;
$sql1 .= ' (SELECT accnt.rowid ' ;
2019-11-13 19:35:02 +01:00
$sql1 .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt' ;
$sql1 .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst' ;
2021-08-27 22:42:04 +02:00
$sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . (( int ) $conf -> global -> CHARTOFACCOUNTS ) . ' AND accnt.entity = ' . (( int ) $conf -> entity ) . ')' ;
$sql1 .= ' AND fd.fk_facture IN (SELECT rowid FROM ' . MAIN_DB_PREFIX . 'facture WHERE entity = ' . (( int ) $conf -> entity ) . ')' ;
2018-06-18 13:14:35 +02:00
$sql1 .= ' AND fk_code_ventilation <> 0' ;
dol_syslog ( " htdocs/accountancy/customer/index.php fixaccountancycode " , LOG_DEBUG );
$resql1 = $db -> query ( $sql1 );
2019-11-13 19:35:02 +01:00
if ( ! $resql1 ) {
$error ++ ;
2018-06-18 13:14:35 +02:00
$db -> rollback ();
2018-07-23 17:35:30 +02:00
setEventMessages ( $db -> lasterror (), null , 'errors' );
2018-06-18 13:14:35 +02:00
} else {
$db -> commit ();
}
// End clean database
}
2014-08-27 07:03:42 +02:00
if ( $action == 'validatehistory' ) {
$error = 0 ;
2021-12-13 19:04:25 +01:00
$nbbinddone = 0 ;
2021-12-16 15:41:23 +01:00
$notpossible = 0 ;
2021-12-13 19:04:25 +01:00
2014-08-27 07:03:42 +02:00
$db -> begin ();
2016-08-08 14:52:19 +02:00
2016-11-17 16:32:19 +01:00
// Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
2019-10-21 03:50:57 +02:00
/* if ( $db -> type == 'pgsql' ) {
2016-08-08 14:52:19 +02:00
$sql1 = " UPDATE " . MAIN_DB_PREFIX . " facturedet " ;
$sql1 .= " SET fk_code_ventilation = accnt.rowid " ;
2015-09-05 08:03:06 +02:00
$sql1 .= " FROM " . MAIN_DB_PREFIX . " product as p, " . MAIN_DB_PREFIX . " accounting_account as accnt , " . MAIN_DB_PREFIX . " accounting_system as syst " ;
2021-08-27 22:42:04 +02:00
$sql1 .= " WHERE " . MAIN_DB_PREFIX . " facturedet.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid= " . (( int ) $conf -> global -> CHARTOFACCOUNTS ) . ' AND accnt.entity = ' . (( int ) $conf -> entity );
2014-08-27 07:03:42 +02:00
$sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number " ;
2016-08-08 14:52:19 +02:00
$sql1 .= " AND " . MAIN_DB_PREFIX . " facturedet.fk_code_ventilation = 0 " ;
2014-08-27 07:03:42 +02:00
} else {
2015-09-05 08:03:06 +02:00
$sql1 = " UPDATE " . MAIN_DB_PREFIX . " facturedet as fd, " . MAIN_DB_PREFIX . " product as p, " . MAIN_DB_PREFIX . " accounting_account as accnt , " . MAIN_DB_PREFIX . " accounting_system as syst " ;
2017-06-20 01:52:47 +02:00
$sql1 .= " SET fk_code_ventilation = accnt.rowid " ;
2021-08-27 22:42:04 +02:00
$sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid= " . (( int ) $conf -> global -> CHARTOFACCOUNTS ) . ' AND accnt.entity = ' . (( int ) $conf -> entity );
2014-08-27 07:03:42 +02:00
$sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number " ;
$sql1 .= " AND fd.fk_code_ventilation = 0 " ;
2019-10-21 03:50:57 +02:00
} */
// Customer Invoice lines (must be same request than into page list.php for manual binding)
$sql = " SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype, " ;
2019-11-13 19:35:02 +01:00
$sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, l.vat_src_code, " ;
2020-03-13 18:10:00 +01:00
$sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod, " ;
2021-03-27 05:23:20 +01:00
if ( ! empty ( $conf -> global -> MAIN_PRODUCT_PERENTITY_SHARED )) {
2021-04-14 04:45:17 +02:00
$sql .= " ppe.accountancy_code_sell as code_sell, ppe.accountancy_code_sell_intra as code_sell_intra, ppe.accountancy_code_sell_export as code_sell_export, " ;
2021-03-18 17:08:55 +01:00
} else {
$sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export, " ;
}
2020-12-04 04:13:42 +01:00
$sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export, aa4.rowid as aarowid_thirdparty, " ;
2019-11-13 19:35:02 +01:00
$sql .= " co.code as country_code, co.label as country_label, " ;
2021-03-18 17:08:55 +01:00
$sql .= " s.tva_intra, " ;
2021-04-14 04:45:17 +02:00
if ( ! empty ( $conf -> global -> MAIN_COMPANY_PERENTITY_SHARED )) {
2022-01-13 19:41:52 +01:00
$sql .= " spe.accountancy_code_sell as company_code_sell " ; // accounting code for product but stored on thirdparty
2021-03-18 17:08:55 +01:00
} else {
2022-01-13 19:41:52 +01:00
$sql .= " s.accountancy_code_sell as company_code_sell " ; // accounting code for product but stored on thirdparty
2021-03-18 17:08:55 +01:00
}
2019-11-13 19:35:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " societe as s ON s.rowid = f.fk_soc " ;
2021-06-16 06:23:50 +02:00
if ( ! empty ( $conf -> global -> MAIN_COMPANY_PERENTITY_SHARED )) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . (( int ) $conf -> entity );
}
2019-11-13 19:35:02 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_country as co ON co.rowid = s.fk_pays " ;
2021-12-16 15:41:23 +01:00
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " facturedet as l ON f.rowid = l.fk_facture " ; // the main table
2019-11-13 19:35:02 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON p.rowid = l.fk_product " ;
2021-03-27 07:25:31 +01:00
if ( ! empty ( $conf -> global -> MAIN_PRODUCT_PERENTITY_SHARED )) {
2021-04-14 04:45:17 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . (( int ) $conf -> entity );
2021-03-18 17:08:55 +01:00
}
2021-04-14 04:45:17 +02:00
$alias_societe_perentity = empty ( $conf -> global -> MAIN_COMPANY_PERENTITY_SHARED ) ? " s " : " spe " ;
$alias_product_perentity = empty ( $conf -> global -> MAIN_PRODUCT_PERENTITY_SHARED ) ? " p " : " ppe " ;
2021-03-30 03:27:46 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa ON " . $alias_product_perentity . " .accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = ' " . $db -> escape ( $chartaccountcode ) . " ' AND aa.entity = " . $conf -> entity ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa2 ON " . $alias_product_perentity . " .accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = ' " . $db -> escape ( $chartaccountcode ) . " ' AND aa2.entity = " . $conf -> entity ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa3 ON " . $alias_product_perentity . " .accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = ' " . $db -> escape ( $chartaccountcode ) . " ' AND aa3.entity = " . $conf -> entity ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa4 ON " . $alias_societe_perentity . " .accountancy_code_sell = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = ' " . $db -> escape ( $chartaccountcode ) . " ' AND aa4.entity = " . $conf -> entity ;
2019-11-13 19:35:02 +01:00
$sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0 " ;
$sql .= " AND l.product_type <= 2 " ;
2022-06-17 04:37:56 +02:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ; // We don't share object for accountancy
2021-07-08 21:49:53 +02:00
if ( ! empty ( $conf -> global -> ACCOUNTING_DATE_START_BINDING )) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $conf -> global -> ACCOUNTING_DATE_START_BINDING ) . " ' " ;
}
2021-11-22 03:34:50 +01:00
if ( $validatemonth && $validateyear ) {
$sql .= dolSqlDateFilter ( 'f.datef' , 0 , $validatemonth , $validateyear );
}
2016-08-08 14:52:19 +02:00
2017-07-01 13:31:44 +02:00
dol_syslog ( 'htdocs/accountancy/customer/index.php' );
2021-10-11 12:50:16 +02:00
2019-10-21 03:50:57 +02:00
$result = $db -> query ( $sql );
2019-11-13 19:35:02 +01:00
if ( ! $result ) {
2019-10-21 03:50:57 +02:00
$error ++ ;
2015-10-23 12:58:30 +02:00
setEventMessages ( $db -> lasterror (), null , 'errors' );
2014-08-27 07:03:42 +02:00
} else {
2019-10-21 03:50:57 +02:00
$num_lines = $db -> num_rows ( $result );
2021-10-13 21:36:04 +02:00
$facture_static = new Facture ( $db );
2019-10-21 03:50:57 +02:00
$isSellerInEEC = isInEEC ( $mysoc );
2021-06-04 12:20:52 +02:00
$thirdpartystatic = new Societe ( $db );
$facture_static = new Facture ( $db );
$facture_static_det = new FactureLigne ( $db );
$product_static = new Product ( $db );
2019-10-21 03:50:57 +02:00
$i = 0 ;
while ( $i < min ( $num_lines , 10000 )) { // No more than 10000 at once
$objp = $db -> fetch_object ( $result );
2021-06-04 12:20:52 +02:00
$thirdpartystatic -> id = $objp -> socid ;
$thirdpartystatic -> name = $objp -> name ;
$thirdpartystatic -> client = $objp -> client ;
$thirdpartystatic -> fournisseur = $objp -> fournisseur ;
$thirdpartystatic -> code_client = $objp -> code_client ;
$thirdpartystatic -> code_compta_client = $objp -> code_compta_client ;
$thirdpartystatic -> code_fournisseur = $objp -> code_fournisseur ;
$thirdpartystatic -> code_compta_fournisseur = $objp -> code_compta_fournisseur ;
$thirdpartystatic -> email = $objp -> email ;
$thirdpartystatic -> country_code = $objp -> country_code ;
$thirdpartystatic -> tva_intra = $objp -> tva_intra ;
2022-01-13 19:41:52 +01:00
$thirdpartystatic -> code_compta_product = $objp -> company_code_sell ; // The accounting account for product stored on thirdparty object (for level3 suggestion)
2021-06-04 12:20:52 +02:00
$product_static -> ref = $objp -> product_ref ;
$product_static -> id = $objp -> product_id ;
$product_static -> type = $objp -> type ;
$product_static -> label = $objp -> product_label ;
$product_static -> status = $objp -> status ;
$product_static -> status_buy = $objp -> status_buy ;
$product_static -> accountancy_code_sell = $objp -> code_sell ;
$product_static -> accountancy_code_sell_intra = $objp -> code_sell_intra ;
$product_static -> accountancy_code_sell_export = $objp -> code_sell_export ;
$product_static -> accountancy_code_buy = $objp -> code_buy ;
$product_static -> accountancy_code_buy_intra = $objp -> code_buy_intra ;
$product_static -> accountancy_code_buy_export = $objp -> code_buy_export ;
$product_static -> tva_tx = $objp -> tva_tx_prod ;
$facture_static -> ref = $objp -> ref ;
$facture_static -> id = $objp -> facid ;
$facture_static -> type = $objp -> ftype ;
2021-10-21 15:54:23 +02:00
$facture_static -> date = $objp -> datef ;
2021-06-04 12:20:52 +02:00
$facture_static_det -> id = $objp -> rowid ;
$facture_static_det -> total_ht = $objp -> total_ht ;
$facture_static_det -> tva_tx = $objp -> tva_tx_line ;
$facture_static_det -> vat_src_code = $objp -> vat_src_code ;
$facture_static_det -> product_type = $objp -> type_l ;
$facture_static_det -> desc = $objp -> description ;
2021-10-22 17:12:44 +02:00
$accountingAccountArray = array (
2021-06-04 12:20:52 +02:00
'dom' => $objp -> aarowid ,
'intra' => $objp -> aarowid_intra ,
'export' => $objp -> aarowid_export ,
'thirdparty' => $objp -> aarowid_thirdparty );
$code_sell_p_notset = '' ;
$code_sell_t_notset = '' ;
2021-11-22 04:13:16 +01:00
$suggestedid = 0 ;
2021-10-22 17:12:44 +02:00
$return = $accountingAccount -> getAccountingCodeToBind ( $thirdpartystatic , $mysoc , $product_static , $facture_static , $facture_static_det , $accountingAccountArray , 'customer' );
2021-12-16 15:41:23 +01:00
if ( ! is_array ( $return ) && $return < 0 ) {
2021-06-04 12:20:52 +02:00
setEventMessage ( $accountingAccount -> error , 'errors' );
2019-10-21 03:50:57 +02:00
} else {
2021-11-15 09:08:58 +01:00
$suggestedid = $return [ 'suggestedid' ];
$suggestedaccountingaccountfor = $return [ 'suggestedaccountingaccountfor' ];
2019-10-21 03:50:57 +02:00
2021-11-15 09:08:58 +01:00
if ( ! empty ( $suggestedid ) && $suggestedaccountingaccountfor != '' && $suggestedaccountingaccountfor != 'eecwithoutvatnumber' ) {
$suggestedid = $return [ 'suggestedid' ];
2021-06-04 12:20:52 +02:00
} else {
2021-11-15 09:08:58 +01:00
$suggestedid = 0 ;
2021-06-04 12:20:52 +02:00
}
2020-12-04 04:13:42 +01:00
}
2021-11-22 04:13:16 +01:00
if ( $suggestedid > 0 ) {
2019-11-13 19:35:02 +01:00
$sqlupdate = " UPDATE " . MAIN_DB_PREFIX . " facturedet " ;
2021-06-04 12:20:52 +02:00
$sqlupdate .= " SET fk_code_ventilation = " . (( int ) $suggestedid );
$sqlupdate .= " WHERE fk_code_ventilation <= 0 AND product_type <= 2 AND rowid = " . (( int ) $facture_static_det -> id );
2019-10-21 03:50:57 +02:00
$resqlupdate = $db -> query ( $sqlupdate );
2021-02-22 21:36:42 +01:00
if ( ! $resqlupdate ) {
2019-10-21 03:50:57 +02:00
$error ++ ;
setEventMessages ( $db -> lasterror (), null , 'errors' );
break ;
2021-12-13 19:04:25 +01:00
} else {
$nbbinddone ++ ;
2019-10-21 03:50:57 +02:00
}
2021-12-16 15:41:23 +01:00
} else {
$notpossible ++ ;
2019-10-21 03:50:57 +02:00
}
$i ++ ;
}
2021-12-16 15:41:23 +01:00
if ( $num_lines > 10000 ) {
$notpossible += ( $num_lines - 10000 );
}
2019-10-21 03:50:57 +02:00
}
2021-02-22 21:36:42 +01:00
if ( $error ) {
2019-10-21 03:50:57 +02:00
$db -> rollback ();
2020-05-21 15:05:19 +02:00
} else {
2014-08-27 07:03:42 +02:00
$db -> commit ();
2021-12-16 15:41:23 +01:00
setEventMessages ( $langs -> trans ( 'AutomaticBindingDone' , $nbbinddone , $notpossible ), null , 'mesgs' );
2014-08-27 07:03:42 +02:00
}
}
2016-09-30 18:38:48 +02:00
2014-08-27 07:03:42 +02:00
/*
* View
*/
2016-09-30 18:38:48 +02:00
2014-08-27 07:03:42 +02:00
llxHeader ( '' , $langs -> trans ( " CustomersVentilation " ));
2019-11-13 19:35:02 +01:00
$textprevyear = '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year_current - 1 ) . '">' . img_previous () . '</a>' ;
$textnextyear = ' <a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year_current + 1 ) . '">' . img_next () . '</a>' ;
2014-08-27 07:03:42 +02:00
2018-04-14 22:22:29 +02:00
2019-11-13 19:35:02 +01:00
print load_fiche_titre ( $langs -> trans ( " CustomersVentilation " ) . " " . $textprevyear . " " . $langs -> trans ( " Year " ) . " " . $year_start . " " . $textnextyear , '' , 'title_accountancy' );
2014-08-27 07:03:42 +02:00
2019-12-16 12:38:51 +01:00
print '<span class="opacitymedium">' . $langs -> trans ( " DescVentilCustomer " ) . '</span><br>' ;
print '<span class="opacitymedium hideonsmartphone">' . $langs -> trans ( " DescVentilMore " , $langs -> transnoentitiesnoconv ( " ValidateHistory " ), $langs -> transnoentitiesnoconv ( " ToBind " )) . '<br>' ;
2019-06-04 02:11:03 +02:00
print '</span><br>' ;
2014-08-27 07:03:42 +02:00
$y = $year_current ;
2021-11-22 03:22:15 +01:00
$buttonbind = '<a class="butAction" href="' . $_SERVER [ 'PHP_SELF' ] . '?action=validatehistory&token=' . newToken () . '">' . $langs -> trans ( " ValidateHistory " ) . '</a>' ;
2016-11-17 17:51:10 +01:00
2021-09-10 17:58:41 +02:00
print_barre_liste ( img_picto ( '' , 'unlink' , 'class="paddingright fa-color-unset"' ) . $langs -> trans ( " OverviewOfAmountOfLinesNotBound " ), '' , '' , '' , '' , '' , '' , - 1 , '' , '' , 0 , $buttonbind , '' , 0 , 1 , 1 );
2018-09-09 18:38:48 +02:00
//print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
2016-11-17 17:51:10 +01:00
2017-09-05 20:42:34 +02:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2021-09-10 17:39:08 +02:00
print '<tr class="liste_titre"><td class="minwidth100">' . $langs -> trans ( " Account " ) . '</td>' ;
2021-09-10 18:54:42 +02:00
print '<td>' . $langs -> trans ( " Label " ) . '</td>' ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-11-22 05:21:24 +01:00
$cursormonth = $j ;
if ( $cursormonth > 12 ) {
$cursormonth -= 12 ;
}
$cursoryear = ( $cursormonth < ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 )) ? $y + 1 : $y ;
$tmp = dol_getdate ( dol_get_last_day ( $cursoryear , $cursormonth , 'gmt' ), false , 'gmt' );
print '<td width="60" class="right">' ;
if ( ! empty ( $tmp [ 'mday' ])) {
$param = 'search_date_startday=1&search_date_startmonth=' . $cursormonth . '&search_date_startyear=' . $cursoryear ;
$param .= '&search_date_endday=' . $tmp [ 'mday' ] . '&search_date_endmonth=' . $tmp [ 'mon' ] . '&search_date_endyear=' . $tmp [ 'year' ];
print '<a href="' . DOL_URL_ROOT . '/accountancy/customer/list.php?' . $param . '">' ;
}
print $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT ));
if ( ! empty ( $tmp [ 'mday' ])) {
print '</a>' ;
}
print '</td>' ;
2016-06-22 11:28:21 +02:00
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " Total " ) . '</b></td></tr>' ;
$sql = " SELECT " . $db -> ifsql ( 'aa.account_number IS NULL' , " 'tobind' " , 'aa.account_number' ) . " AS codecomptable, " ;
$sql .= " " . $db -> ifsql ( 'aa.label IS NULL' , " 'tobind' " , 'aa.label' ) . " AS intitule, " ;
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-08-27 22:42:04 +02:00
$sql .= " SUM( " . $db -> ifsql ( " MONTH(f.datef)= " . $j , " fd.total_ht " , " 0 " ) . " ) AS month " . str_pad ( $j , 2 , " 0 " , STR_PAD_LEFT ) . " , " ;
2016-06-22 11:28:21 +02:00
}
$sql .= " SUM(fd.total_ht) as total " ;
2019-11-13 19:35:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa ON aa.rowid = fd.fk_code_ventilation " ;
$sql .= " WHERE f.datef >= ' " . $db -> idate ( $search_date_start ) . " ' " ;
$sql .= " AND f.datef <= ' " . $db -> idate ( $search_date_end ) . " ' " ;
2020-08-17 15:27:05 +02:00
// Define begin binding date
if ( ! empty ( $conf -> global -> ACCOUNTING_DATE_START_BINDING )) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $conf -> global -> ACCOUNTING_DATE_START_BINDING ) . " ' " ;
}
2018-03-27 11:10:12 +02:00
$sql .= " AND f.fk_statut > 0 " ;
2020-06-05 17:24:59 +02:00
$sql .= " AND fd.product_type <= 2 " ;
2019-11-13 19:35:02 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ; // We don't share object for accountancy
2016-11-17 17:51:10 +01:00
$sql .= " AND aa.account_number IS NULL " ;
2019-11-13 19:35:02 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-01-07 16:47:11 +01:00
} else {
2019-11-13 19:35:02 +01:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_DEPOSIT . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-01-07 16:47:11 +01:00
}
2016-06-22 11:28:21 +02:00
$sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label " ;
2014-08-27 07:03:42 +02:00
2021-08-27 22:42:04 +02:00
dol_syslog ( 'htdocs/accountancy/customer/index.php' , LOG_DEBUG );
2014-08-27 07:03:42 +02:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
2016-08-08 14:52:19 +02:00
2019-11-13 19:35:02 +01:00
while ( $row = $db -> fetch_row ( $resql )) {
2021-11-22 05:21:24 +01:00
print '<tr class="oddeven">' ;
print '<td>' ;
2021-02-22 21:36:42 +01:00
if ( $row [ 0 ] == 'tobind' ) {
2021-09-10 17:39:08 +02:00
print '<span class="opacitymedium">' . $langs -> trans ( " Unknown " ) . '</span>' ;
2021-02-22 21:36:42 +01:00
} else {
print length_accountg ( $row [ 0 ]);
}
2017-09-08 15:24:27 +02:00
print '</td>' ;
2021-09-10 18:54:42 +02:00
print '<td>' ;
2021-02-22 21:36:42 +01:00
if ( $row [ 0 ] == 'tobind' ) {
2021-11-22 03:22:15 +01:00
print $langs -> trans ( " UseMenuToSetBindindManualy " , DOL_URL_ROOT . '/accountancy/customer/list.php?search_year=' . (( int ) $y ), $langs -> transnoentitiesnoconv ( " ToBind " ));
2021-02-22 21:36:42 +01:00
} else {
print $row [ 1 ];
}
2017-09-08 15:24:27 +02:00
print '</td>' ;
2021-11-22 05:21:24 +01:00
for ( $i = 2 ; $i <= 13 ; $i ++ ) {
$cursormonth = (( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) + $i - 2 );
if ( $cursormonth > 12 ) {
$cursormonth -= 12 ;
}
$cursoryear = ( $cursormonth < ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 )) ? $y + 1 : $y ;
$tmp = dol_getdate ( dol_get_last_day ( $cursoryear , $cursormonth , 'gmt' ), false , 'gmt' );
print '<td class="right nowraponall amount">' ;
print price ( $row [ $i ]);
print '</td>' ;
2016-06-22 11:28:21 +02:00
}
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount"><b>' . price ( $row [ 14 ]) . '</b></td>' ;
2014-08-27 07:03:42 +02:00
print '</tr>' ;
}
$db -> free ( $resql );
2021-11-22 05:21:24 +01:00
if ( $num == 0 ) {
print '<tr class="oddeven"><td colspan="16">' ;
print '<span class="opacitymedium">' . $langs -> trans ( " NoRecordFound " ) . '</span>' ;
print '</td></tr>' ;
}
2014-08-27 07:03:42 +02:00
} else {
2014-08-28 06:30:01 +02:00
print $db -> lasterror (); // Show last sql error
2014-08-27 07:03:42 +02:00
}
print " </table> \n " ;
2017-09-05 20:42:34 +02:00
print '</div>' ;
2016-11-17 17:51:10 +01:00
2018-03-27 12:31:08 +02:00
2016-11-17 17:51:10 +01:00
print '<br>' ;
2021-09-10 17:58:41 +02:00
print_barre_liste ( img_picto ( '' , 'link' , 'class="paddingright fa-color-unset"' ) . $langs -> trans ( " OverviewOfAmountOfLinesBound " ), '' , '' , '' , '' , '' , '' , - 1 , '' , '' , 0 , '' , '' , 0 , 1 , 1 );
2018-09-09 18:38:48 +02:00
//print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
2016-11-17 17:51:10 +01:00
2017-09-05 20:42:34 +02:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2021-09-10 17:39:08 +02:00
print '<tr class="liste_titre"><td class="minwidth100">' . $langs -> trans ( " Account " ) . '</td>' ;
2021-09-10 18:54:42 +02:00
print '<td>' . $langs -> trans ( " Label " ) . '</td>' ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-11-22 05:21:24 +01:00
$cursormonth = $j ;
if ( $cursormonth > 12 ) {
$cursormonth -= 12 ;
}
$cursoryear = ( $cursormonth < ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 )) ? $y + 1 : $y ;
$tmp = dol_getdate ( dol_get_last_day ( $cursoryear , $cursormonth , 'gmt' ), false , 'gmt' );
print '<td width="60" class="right">' ;
if ( ! empty ( $tmp [ 'mday' ])) {
$param = 'search_date_startday=1&search_date_startmonth=' . $cursormonth . '&search_date_startyear=' . $cursoryear ;
$param .= '&search_date_endday=' . $tmp [ 'mday' ] . '&search_date_endmonth=' . $tmp [ 'mon' ] . '&search_date_endyear=' . $tmp [ 'year' ];
print '<a href="' . DOL_URL_ROOT . '/accountancy/customer/lines.php?' . $param . '">' ;
}
print $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT ));
if ( ! empty ( $tmp [ 'mday' ])) {
print '</a>' ;
}
print '</td>' ;
2016-11-17 17:51:10 +01:00
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " Total " ) . '</b></td></tr>' ;
$sql = " SELECT " . $db -> ifsql ( 'aa.account_number IS NULL' , " 'tobind' " , 'aa.account_number' ) . " AS codecomptable, " ;
$sql .= " " . $db -> ifsql ( 'aa.label IS NULL' , " 'tobind' " , 'aa.label' ) . " AS intitule, " ;
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-08-27 22:42:04 +02:00
$sql .= " SUM( " . $db -> ifsql ( " MONTH(f.datef)= " . $j , " fd.total_ht " , " 0 " ) . " ) AS month " . str_pad ( $j , 2 , " 0 " , STR_PAD_LEFT ) . " , " ;
2016-11-17 17:51:10 +01:00
}
$sql .= " SUM(fd.total_ht) as total " ;
2019-11-13 19:35:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " accounting_account as aa ON aa.rowid = fd.fk_code_ventilation " ;
$sql .= " WHERE f.datef >= ' " . $db -> idate ( $search_date_start ) . " ' " ;
$sql .= " AND f.datef <= ' " . $db -> idate ( $search_date_end ) . " ' " ;
2020-08-17 15:27:05 +02:00
// Define begin binding date
if ( ! empty ( $conf -> global -> ACCOUNTING_DATE_START_BINDING )) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $conf -> global -> ACCOUNTING_DATE_START_BINDING ) . " ' " ;
}
2019-11-13 19:35:02 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ; // We don't share object for accountancy
2018-03-27 11:10:12 +02:00
$sql .= " AND f.fk_statut > 0 " ;
2020-06-05 17:24:59 +02:00
$sql .= " AND fd.product_type <= 2 " ;
2019-11-13 19:35:02 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-01-07 16:47:11 +01:00
} else {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_DEPOSIT . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-01-07 16:47:11 +01:00
}
2016-11-17 17:51:10 +01:00
$sql .= " AND aa.account_number IS NOT NULL " ;
$sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label " ;
2022-02-13 21:11:45 +01:00
$sql .= ' ORDER BY aa.account_number' ;
2016-11-17 17:51:10 +01:00
2017-07-01 13:31:44 +02:00
dol_syslog ( 'htdocs/accountancy/customer/index.php' );
2016-11-17 17:51:10 +01:00
$resql = $db -> query ( $sql );
if ( $resql ) {
2017-06-20 06:52:41 +02:00
$num = $db -> num_rows ( $resql );
2019-11-13 19:35:02 +01:00
while ( $row = $db -> fetch_row ( $resql )) {
2021-11-22 05:21:24 +01:00
print '<tr class="oddeven">' ;
print '<td>' ;
2021-02-22 21:36:42 +01:00
if ( $row [ 0 ] == 'tobind' ) {
2017-09-08 15:24:27 +02:00
print $langs -> trans ( " Unknown " );
2021-02-22 21:36:42 +01:00
} else {
print length_accountg ( $row [ 0 ]);
}
2017-09-08 15:24:27 +02:00
print '</td>' ;
2017-10-05 05:38:02 +02:00
2021-09-10 18:54:42 +02:00
print '<td>' ;
2021-02-22 21:36:42 +01:00
if ( $row [ 0 ] == 'tobind' ) {
2021-11-22 03:22:15 +01:00
print $langs -> trans ( " UseMenuToSetBindindManualy " , DOL_URL_ROOT . '/accountancy/customer/list.php?search_year=' . (( int ) $y ), $langs -> transnoentitiesnoconv ( " ToBind " ));
2021-02-22 21:36:42 +01:00
} else {
print $row [ 1 ];
}
2017-09-08 15:24:27 +02:00
print '</td>' ;
2017-10-05 05:38:02 +02:00
2021-11-22 05:21:24 +01:00
for ( $i = 2 ; $i <= 13 ; $i ++ ) {
$cursormonth = (( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) + $i - 2 );
if ( $cursormonth > 12 ) {
$cursormonth -= 12 ;
}
$cursoryear = ( $cursormonth < ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 )) ? $y + 1 : $y ;
$tmp = dol_getdate ( dol_get_last_day ( $cursoryear , $cursormonth , 'gmt' ), false , 'gmt' );
print '<td class="right nowraponall amount">' ;
print price ( $row [ $i ]);
print '</td>' ;
2017-06-20 06:52:41 +02:00
}
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount"><b>' . price ( $row [ 14 ]) . '</b></td>' ;
2017-06-20 06:52:41 +02:00
print '</tr>' ;
}
$db -> free ( $resql );
2021-11-22 05:21:24 +01:00
if ( $num == 0 ) {
print '<tr class="oddeven"><td colspan="16">' ;
print '<span class="opacitymedium">' . $langs -> trans ( " NoRecordFound " ) . '</span>' ;
print '</td></tr>' ;
}
2016-11-17 17:51:10 +01:00
} else {
2017-06-20 06:52:41 +02:00
print $db -> lasterror (); // Show last sql error
2016-11-17 17:51:10 +01:00
}
print " </table> \n " ;
2017-09-05 20:42:34 +02:00
print '</div>' ;
2016-11-17 17:51:10 +01:00
2021-02-22 21:36:42 +01:00
if ( $conf -> global -> MAIN_FEATURES_LEVEL > 0 ) { // This part of code looks strange. Why showing a report that should rely on result of this step ?
2017-06-20 06:52:41 +02:00
print '<br>' ;
print '<br>' ;
2018-04-14 22:22:29 +02:00
print_barre_liste ( $langs -> trans ( " OtherInfo " ), '' , '' , '' , '' , '' , '' , - 1 , '' , '' , 0 , '' , '' , 0 , 1 , 1 );
2018-09-09 18:38:48 +02:00
//print load_fiche_titre($langs->trans("OtherInfo"), '', '');
2017-06-20 06:52:41 +02:00
2017-09-05 20:42:34 +02:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2021-09-10 17:39:08 +02:00
print '<tr class="liste_titre"><td lass="left">' . $langs -> trans ( " TotalVente " ) . '</td>' ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right">' . $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT )) . '</td>' ;
2017-06-20 06:52:41 +02:00
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " Total " ) . '</b></td></tr>' ;
2017-06-22 05:30:21 +02:00
2020-09-19 20:11:04 +02:00
$sql = " SELECT ' " . $db -> escape ( $langs -> trans ( " TotalVente " )) . " ' AS total, " ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-08-27 22:42:04 +02:00
$sql .= " SUM( " . $db -> ifsql ( " MONTH(f.datef)= " . $j , " fd.total_ht " , " 0 " ) . " ) AS month " . str_pad ( $j , 2 , " 0 " , STR_PAD_LEFT ) . " , " ;
2017-06-20 06:52:41 +02:00
}
$sql .= " SUM(fd.total_ht) as total " ;
2019-11-13 19:35:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " WHERE f.datef >= ' " . $db -> idate ( $search_date_start ) . " ' " ;
$sql .= " AND f.datef <= ' " . $db -> idate ( $search_date_end ) . " ' " ;
2020-08-17 15:27:05 +02:00
// Define begin binding date
if ( ! empty ( $conf -> global -> ACCOUNTING_DATE_START_BINDING )) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $conf -> global -> ACCOUNTING_DATE_START_BINDING ) . " ' " ;
}
2019-11-13 19:35:02 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ; // We don't share object for accountancy
2018-03-27 11:10:12 +02:00
$sql .= " AND f.fk_statut > 0 " ;
2020-06-05 17:24:59 +02:00
$sql .= " AND fd.product_type <= 2 " ;
2019-11-13 19:35:02 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-06-20 06:52:41 +02:00
} else {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_DEPOSIT . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-06-20 06:52:41 +02:00
}
dol_syslog ( 'htdocs/accountancy/customer/index.php' );
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
while ( $row = $db -> fetch_row ( $resql )) {
2019-11-13 19:35:02 +01:00
print '<tr><td>' . $row [ 0 ] . '</td>' ;
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount">' . price ( $row [ $i ]) . '</td>' ;
2017-06-20 06:52:41 +02:00
}
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount"><b>' . price ( $row [ 13 ]) . '</b></td>' ;
2017-06-20 06:52:41 +02:00
print '</tr>' ;
}
$db -> free ( $resql );
} else {
print $db -> lasterror (); // Show last sql error
}
print " </table> \n " ;
2017-09-05 20:42:34 +02:00
print '</div>' ;
2017-07-14 00:24:49 +02:00
2017-09-08 15:24:27 +02:00
2022-08-24 10:34:25 +02:00
if ( isModEnabled ( 'margin' )) {
2017-06-20 06:52:41 +02:00
print " <br> \n " ;
2017-09-05 20:42:34 +02:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2021-09-10 17:39:08 +02:00
print '<tr class="liste_titre"><td>' . $langs -> trans ( " TotalMarge " ) . '</td>' ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right">' . $langs -> trans ( 'MonthShort' . str_pad ( $j , 2 , '0' , STR_PAD_LEFT )) . '</td>' ;
2017-06-20 06:52:41 +02:00
}
2019-11-13 19:35:02 +01:00
print '<td width="60" class="right"><b>' . $langs -> trans ( " Total " ) . '</b></td></tr>' ;
2020-09-19 20:11:04 +02:00
$sql = " SELECT ' " . $db -> escape ( $langs -> trans ( " Vide " )) . " ' AS marge, " ;
2019-11-13 19:35:02 +01:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
$j = $i + ( $conf -> global -> SOCIETE_FISCAL_MONTH_START ? $conf -> global -> SOCIETE_FISCAL_MONTH_START : 1 ) - 1 ;
2021-02-22 21:36:42 +01:00
if ( $j > 12 ) {
$j -= 12 ;
}
2021-12-19 01:04:24 +01:00
$sql .= " SUM( " . $db -> ifsql ( " MONTH(f.datef)= " . $j ,
" ( " . $db -> ifsql ( " fd.total_ht < 0 " ,
" (-1 * (abs(fd.total_ht) - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100)))) " ,
" (fd.total_ht - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100))) " ) . " ) " ,
0 ) . " ) AS month " . str_pad ( $j , 2 , '0' , STR_PAD_LEFT ) . " , " ;
2017-06-20 06:52:41 +02:00
}
2021-12-19 01:04:24 +01:00
$sql .= " SUM( " . $db -> ifsql ( " fd.total_ht < 0 " ,
" (-1 * (abs(fd.total_ht) - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100)))) " ,
" (fd.total_ht - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100))) " ) . " ) as total " ;
2021-11-16 17:44:18 +01:00
2019-11-13 19:35:02 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as fd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as f ON f.rowid = fd.fk_facture " ;
$sql .= " WHERE f.datef >= ' " . $db -> idate ( $search_date_start ) . " ' " ;
$sql .= " AND f.datef <= ' " . $db -> idate ( $search_date_end ) . " ' " ;
2020-08-17 15:27:05 +02:00
// Define begin binding date
if ( ! empty ( $conf -> global -> ACCOUNTING_DATE_START_BINDING )) {
$sql .= " AND f.datef >= ' " . $db -> idate ( $conf -> global -> ACCOUNTING_DATE_START_BINDING ) . " ' " ;
}
2019-11-13 19:35:02 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'invoice' , 0 ) . " ) " ; // We don't share object for accountancy
2018-03-27 11:10:12 +02:00
$sql .= " AND f.fk_statut > 0 " ;
2020-06-05 17:24:59 +02:00
$sql .= " AND fd.product_type <= 2 " ;
2019-11-13 19:35:02 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-06-20 06:52:41 +02:00
} else {
2020-06-06 23:00:24 +02:00
$sql .= " AND f.type IN ( " . Facture :: TYPE_STANDARD . " , " . Facture :: TYPE_REPLACEMENT . " , " . Facture :: TYPE_CREDIT_NOTE . " , " . Facture :: TYPE_DEPOSIT . " , " . Facture :: TYPE_SITUATION . " ) " ;
2017-06-20 06:52:41 +02:00
}
2017-07-01 13:31:44 +02:00
dol_syslog ( 'htdocs/accountancy/customer/index.php' );
2017-06-20 06:52:41 +02:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
while ( $row = $db -> fetch_row ( $resql )) {
2019-11-13 19:35:02 +01:00
print '<tr><td>' . $row [ 0 ] . '</td>' ;
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount">' . price ( price2num ( $row [ $i ])) . '</td>' ;
2017-06-20 06:52:41 +02:00
}
2021-06-13 06:37:44 +02:00
print '<td class="right nowraponall amount"><b>' . price ( price2num ( $row [ 13 ])) . '</b></td>' ;
2017-06-20 06:52:41 +02:00
print '</tr>' ;
}
$db -> free ( $resql );
} else {
print $db -> lasterror (); // Show last sql error
}
print " </table> \n " ;
2017-09-05 20:42:34 +02:00
print '</div>' ;
2017-06-20 06:52:41 +02:00
}
2014-08-27 07:03:42 +02:00
}
2018-07-28 14:29:28 +02:00
// End of page
2014-08-27 07:03:42 +02:00
llxFooter ();
2016-06-14 15:32:55 +02:00
$db -> close ();