2004-10-19 22:35:36 +02:00
< ? php
2005-01-31 19:47:14 +01:00
/* Copyright ( C ) 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2012-01-14 12:38:03 +01:00
* Copyright ( C ) 2004 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2014-03-28 18:31:43 +01:00
* Copyright ( C ) 2014 Ferran Marcet < fmarcet @ 2 byte . es >
2014-09-16 21:05:57 +02:00
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2014 Florian Henry < florian . henry @ open - concept . pro >
2002-06-19 00:23:12 +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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2002-06-19 00:23:12 +02:00
* ( 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
2011-08-03 02:45:22 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2002-06-19 00:23:12 +02:00
*/
2005-01-31 19:47:14 +01:00
/**
2010-01-13 19:51:19 +01:00
* \file htdocs / compta / resultat / index . php
2015-02-07 14:55:56 +01:00
* \brief Page reporting result
2010-01-13 19:51:19 +01:00
*/
2005-01-31 19:47:14 +01:00
2012-08-22 23:24:21 +02:00
require '../../main.inc.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php' ;
2008-01-10 18:12:07 +01:00
2003-11-08 20:05:47 +01:00
2012-02-18 18:14:45 +01:00
$year_start = GETPOST ( 'year_start' );
2005-01-31 19:47:14 +01:00
$year_current = strftime ( " %Y " , time ());
2005-05-07 03:27:54 +02:00
$nbofyear = 4 ;
2005-01-31 19:47:14 +01:00
if ( ! $year_start ) {
2010-01-13 19:51:19 +01:00
$year_start = $year_current - ( $nbofyear - 1 );
$year_end = $year_current ;
2005-01-31 19:47:14 +01:00
}
else {
2010-01-13 19:51:19 +01:00
$year_end = $year_start + ( $nbofyear - 1 );
2005-01-31 19:47:14 +01:00
}
2010-02-03 00:23:20 +01:00
// Security check
2012-02-27 22:26:22 +01:00
$socid = GETPOST ( 'socid' , 'int' );
2010-02-03 00:23:20 +01:00
if ( $user -> societe_id > 0 ) $socid = $user -> societe_id ;
2013-01-10 08:27:12 +01:00
if ( ! empty ( $conf -> comptabilite -> enabled )) $result = restrictedArea ( $user , 'compta' , '' , '' , 'resultat' );
if ( ! empty ( $conf -> accounting -> enabled )) $result = restrictedArea ( $user , 'accounting' , '' , '' , 'comptarapport' );
2013-01-02 18:43:59 +01:00
2004-03-04 22:26:03 +01:00
2008-02-16 18:50:25 +01:00
// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
2014-09-21 20:30:55 +02:00
$modecompta = ( GETPOST ( " modecompta " ) ? GETPOST ( " modecompta " ) : $conf -> global -> ACCOUNTING_MODE );
2005-01-31 19:47:14 +01:00
2010-02-03 00:23:20 +01:00
/*
* View
*/
2005-05-03 02:41:51 +02:00
llxHeader ();
2011-11-08 10:18:45 +01:00
$form = new Form ( $db );
2003-10-28 17:49:34 +01:00
2012-08-03 20:09:37 +02:00
$nomlink = '' ;
$exportlink = '' ;
2009-05-08 17:58:01 +02:00
// Affiche en-tete du rapport
2012-01-08 23:43:32 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
2005-01-17 20:50:34 +01:00
{
2010-01-13 19:51:19 +01:00
$nom = $langs -> trans ( " AnnualSummaryDueDebtMode " );
2013-10-10 19:26:07 +02:00
$calcmode = $langs -> trans ( " CalcModeDebt " );
$calcmode .= '<br>(' . $langs -> trans ( " SeeReportInInputOutputMode " , '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year_start=' . $year_start . '&modecompta=RECETTES-DEPENSES">' , '</a>' ) . ')' ;
2010-01-13 19:51:19 +01:00
$period = " $year_start - $year_end " ;
$periodlink = ( $year_start ? " <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year_start= " . ( $year_start - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year_start= " . ( $year_start + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " : " " );
2012-12-09 18:40:01 +01:00
$description = $langs -> trans ( " RulesAmountWithTaxIncluded " );
$description .= '<br>' . $langs -> trans ( " RulesResultDue " );
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) $description .= " <br> " . $langs -> trans ( " DepositsAreNotIncluded " );
else $description .= " <br> " . $langs -> trans ( " DepositsAreIncluded " );
2010-01-13 19:51:19 +01:00
$builddate = time ();
2011-01-10 23:36:34 +01:00
//$exportlink=$langs->trans("NotYetAvailable");
2005-01-17 20:50:34 +01:00
}
else {
2010-01-13 19:51:19 +01:00
$nom = $langs -> trans ( " AnnualSummaryInputOutputMode " );
2013-10-10 19:26:07 +02:00
$calcmode = $langs -> trans ( " CalcModeEngagement " );
$calcmode .= '<br>(' . $langs -> trans ( " SeeReportInDueDebtMode " , '<a href="' . $_SERVER [ " PHP_SELF " ] . '?year_start=' . $year_start . '&modecompta=CREANCES-DETTES">' , '</a>' ) . ')' ;
2010-01-13 19:51:19 +01:00
$period = " $year_start - $year_end " ;
$periodlink = ( $year_start ? " <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year_start= " . ( $year_start - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year_start= " . ( $year_start + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " : " " );
2012-12-09 18:40:01 +01:00
$description = $langs -> trans ( " RulesAmountWithTaxIncluded " );
$description .= '<br>' . $langs -> trans ( " RulesResultInOut " );
2010-01-13 19:51:19 +01:00
$builddate = time ();
2011-01-10 23:36:34 +01:00
//$exportlink=$langs->trans("NotYetAvailable");
2005-01-17 20:50:34 +01:00
}
2013-10-10 19:26:07 +02:00
report_header ( $nom , $nomlink , $period , $periodlink , $description , $builddate , $exportlink , array ( 'modecompta' => $modecompta ), $calcmode );
2005-05-03 02:41:51 +02:00
2002-06-20 15:23:17 +02:00
2005-01-17 20:50:34 +01:00
/*
* Factures clients
*/
2007-11-11 19:05:08 +01:00
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
2012-01-08 23:43:32 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
{
2010-01-13 19:51:19 +01:00
$sql = " SELECT sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " , " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " WHERE f.fk_soc = s.rowid " ;
2012-02-18 18:14:45 +01:00
$sql .= " AND f.fk_statut IN (1,2) " ;
2014-11-14 16:43:49 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) $sql .= " AND f.type IN (0,1,2,5) " ;
else $sql .= " AND f.type IN (0,1,2,3,5) " ;
2012-01-08 23:43:32 +01:00
}
else
{
2010-01-13 19:51:19 +01:00
/*
* Liste des paiements ( les anciens paiements ne sont pas vus par cette requete car , sur les
* vieilles versions , ils n ' etaient pas lies via paiement_facture . On les ajoute plus loin )
*/
2005-05-06 02:21:33 +02:00
$sql = " SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm " ;
2009-05-08 17:58:01 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " , " . MAIN_DB_PREFIX . " paiement_facture as pf " ;
$sql .= " , " . MAIN_DB_PREFIX . " paiement as p " ;
2010-01-13 19:51:19 +01:00
$sql .= " WHERE p.rowid = pf.fk_paiement " ;
$sql .= " AND pf.fk_facture = f.rowid " ;
2005-01-17 20:50:34 +01:00
}
2009-05-08 17:58:01 +02:00
$sql .= " AND f.entity = " . $conf -> entity ;
if ( $socid ) $sql .= " AND f.fk_soc = $socid " ;
$sql .= " GROUP BY dm " ;
$sql .= " ORDER BY dm " ;
2010-01-14 21:03:35 +01:00
//print $sql;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get customers invoices " , LOG_DEBUG );
2005-03-18 23:50:53 +01:00
$result = $db -> query ( $sql );
if ( $result )
2004-03-04 22:26:03 +01:00
{
2010-01-13 19:51:19 +01:00
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
$encaiss [ $row -> dm ] = ( isset ( $row -> amount_ht ) ? $row -> amount_ht : 0 );
2010-01-13 19:51:19 +01:00
$encaiss_ttc [ $row -> dm ] = $row -> amount_ttc ;
$i ++ ;
}
$db -> free ( $result );
2002-06-19 00:23:12 +02:00
}
2004-03-04 22:26:03 +01:00
else {
2010-01-13 19:51:19 +01:00
dol_print_error ( $db );
2004-03-04 22:26:03 +01:00
}
2005-04-07 23:21:44 +02:00
2009-05-08 17:58:01 +02:00
// On ajoute les paiements clients anciennes version, non lies par paiement_facture
2012-01-08 23:43:32 +01:00
if ( $modecompta != 'CREANCES-DETTES' )
{
2010-01-13 19:51:19 +01:00
$sql = " SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " bank as b " ;
$sql .= " , " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= " , " . MAIN_DB_PREFIX . " paiement as p " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " paiement_facture as pf ON p.rowid = pf.fk_paiement " ;
$sql .= " WHERE pf.rowid IS NULL " ;
$sql .= " AND p.fk_bank = b.rowid " ;
$sql .= " AND b.fk_account = ba.rowid " ;
$sql .= " AND ba.entity = " . $conf -> entity ;
$sql .= " GROUP BY dm " ;
$sql .= " ORDER BY dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get old customers payments not linked to invoices " , LOG_DEBUG );
2010-01-13 19:51:19 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
if ( ! isset ( $encaiss [ $row -> dm ])) $encaiss [ $row -> dm ] = 0 ;
$encaiss [ $row -> dm ] += ( isset ( $row -> amount_ht ) ? $row -> amount_ht : 0 );
if ( ! isset ( $encaiss_ttc [ $row -> dm ])) $encaiss_ttc [ $row -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$encaiss_ttc [ $row -> dm ] += $row -> amount_ttc ;
2005-07-02 19:16:15 +02:00
2010-01-13 19:51:19 +01:00
$i ++ ;
}
}
else {
dol_print_error ( $db );
}
2005-05-06 02:21:33 +02:00
}
2010-02-03 00:23:20 +01:00
2004-03-04 22:26:03 +01:00
2005-01-17 20:50:34 +01:00
/*
* Frais , factures fournisseurs .
*/
2007-11-11 19:05:08 +01:00
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
2009-12-09 10:26:30 +01:00
2012-01-08 23:43:32 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
{
2010-01-13 19:51:19 +01:00
$sql = " SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture_fourn as f " ;
$sql .= " WHERE f.fk_statut IN (1,2) " ;
2012-01-08 23:43:32 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) $sql .= " AND f.type IN (0,1,2) " ;
else $sql .= " AND f.type IN (0,1,2,3) " ;
}
else
{
2010-02-07 00:10:54 +01:00
$sql = " SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm " ;
2009-05-08 17:58:01 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " paiementfourn as p " ;
2009-12-09 10:26:30 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " facture_fourn as f " ;
$sql .= " , " . MAIN_DB_PREFIX . " paiementfourn_facturefourn as pf " ;
$sql .= " WHERE f.rowid = pf.fk_facturefourn " ;
$sql .= " AND p.rowid = pf.fk_paiementfourn " ;
2004-03-04 22:26:03 +01:00
}
2009-05-08 17:58:01 +02:00
$sql .= " AND f.entity = " . $conf -> entity ;
if ( $socid ) $sql .= " AND f.fk_soc = " . $socid ;
$sql .= " GROUP BY dm " ;
2002-06-20 15:23:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " get suppliers invoices " , LOG_DEBUG );
2005-03-18 23:50:53 +01:00
$result = $db -> query ( $sql );
if ( $result )
2004-03-04 22:26:03 +01:00
{
2010-01-13 19:51:19 +01:00
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_object ( $result );
2005-07-02 19:16:15 +02:00
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss [ $row -> dm ])) $decaiss [ $row -> dm ] = 0 ;
$decaiss [ $row -> dm ] = ( isset ( $row -> amount_ht ) ? $row -> amount_ht : 0 );
if ( ! isset ( $decaiss_ttc [ $row -> dm ])) $decaiss_ttc [ $row -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$decaiss_ttc [ $row -> dm ] = $row -> amount_ttc ;
2007-11-11 19:05:08 +01:00
2010-01-13 19:51:19 +01:00
$i ++ ;
}
$db -> free ( $result );
2002-06-20 15:23:17 +02:00
}
2004-03-04 22:26:03 +01:00
else {
2010-01-13 19:51:19 +01:00
dol_print_error ( $db );
2002-06-19 11:20:41 +02:00
}
2010-02-03 00:23:20 +01:00
2010-01-13 19:51:19 +01:00
2005-05-11 03:23:52 +02:00
/*
* TVA
*/
2007-11-11 19:05:08 +01:00
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
2012-01-08 23:43:32 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
{
2010-01-13 19:51:19 +01:00
// TVA a payer
$sql = " SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " WHERE f.fk_statut IN (1,2) " ;
2014-11-14 16:43:49 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) $sql .= " AND f.type IN (0,1,2,5) " ;
else $sql .= " AND f.type IN (0,1,2,3,5) " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
2012-01-08 23:43:32 +01:00
$sql .= " GROUP BY dm " ;
2010-01-13 19:51:19 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " get vat to pay " , LOG_DEBUG );
2010-01-13 19:51:19 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss [ $obj -> dm ])) $decaiss [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$decaiss [ $obj -> dm ] += $obj -> amount ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss_ttc [ $obj -> dm ])) $decaiss_ttc [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$decaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
}
}
} else {
dol_print_error ( $db );
}
// TVA a recuperer
$sql = " SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture_fourn as f " ;
$sql .= " WHERE f.fk_statut IN (1,2) " ;
2012-01-08 23:43:32 +01:00
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) $sql .= " AND f.type IN (0,1,2) " ;
else $sql .= " AND f.type IN (0,1,2,3) " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
$sql .= " GROUP BY dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get vat to receive back " , LOG_DEBUG );
2010-01-13 19:51:19 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
if ( ! isset ( $encaiss [ $obj -> dm ])) $encaiss [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$encaiss [ $obj -> dm ] += $obj -> amount ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $encaiss_ttc [ $obj -> dm ])) $encaiss_ttc [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$encaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
}
}
} else {
dol_print_error ( $db );
}
2005-05-11 03:23:52 +02:00
}
else {
2010-01-13 19:51:19 +01:00
// TVA reellement deja payee
$sql = " SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " tva as t " ;
$sql .= " WHERE amount > 0 " ;
$sql .= " AND t.entity = " . $conf -> entity ;
$sql .= " GROUP BY dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get vat really paid " , LOG_DEBUG );
2010-01-13 19:51:19 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss [ $obj -> dm ])) $decaiss [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$decaiss [ $obj -> dm ] += $obj -> amount ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss_ttc [ $obj -> dm ])) $decaiss_ttc [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$decaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
2007-11-11 19:05:08 +01:00
}
2010-01-13 19:51:19 +01:00
}
} else {
dol_print_error ( $db );
}
// TVA recuperee
$sql = " SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " tva as t " ;
$sql .= " WHERE amount < 0 " ;
$sql .= " AND t.entity = " . $conf -> entity ;
$sql .= " GROUP BY dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get vat really received back " , LOG_DEBUG );
2010-01-13 19:51:19 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2012-08-03 20:09:37 +02:00
if ( ! isset ( $encaiss [ $obj -> dm ])) $encaiss [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$encaiss [ $obj -> dm ] += $obj -> amount ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $encaiss_ttc [ $obj -> dm ])) $encaiss_ttc [ $obj -> dm ] = 0 ;
2010-01-13 19:51:19 +01:00
$encaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
2007-11-11 19:05:08 +01:00
}
2010-01-13 19:51:19 +01:00
}
} else {
dol_print_error ( $db );
}
2005-05-11 03:23:52 +02:00
}
2005-04-16 01:50:38 +02:00
/*
2008-02-20 01:39:33 +01:00
* Charges sociales non deductibles
2005-04-16 01:50:38 +02:00
*/
2007-11-11 19:05:08 +01:00
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
2012-01-08 23:43:32 +01:00
if ( $modecompta == 'CREANCES-DETTES' )
{
2012-08-03 20:09:37 +02:00
$sql = " SELECT c.libelle as nom, date_format(cs.date_ech,'%Y-%m') as dm, sum(cs.amount) as amount " ;
2010-01-13 19:51:19 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_chargesociales as c " ;
2012-02-01 11:32:55 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " chargesociales as cs " ;
$sql .= " WHERE cs.fk_type = c.id " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND c.deductible = 0 " ;
2005-04-16 01:50:38 +02:00
}
2012-01-08 23:43:32 +01:00
else
{
2012-08-03 20:09:37 +02:00
$sql = " SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount " ;
2010-01-13 19:51:19 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_chargesociales as c " ;
2012-02-01 11:32:55 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " chargesociales as cs " ;
2010-01-13 19:51:19 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " paiementcharge as p " ;
2012-02-01 11:32:55 +01:00
$sql .= " WHERE p.fk_charge = cs.rowid " ;
$sql .= " AND cs.fk_type = c.id " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND c.deductible = 0 " ;
2005-04-16 01:50:38 +02:00
}
2012-02-01 11:32:55 +01:00
$sql .= " AND cs.entity = " . $conf -> entity ;
2009-05-08 17:58:01 +02:00
$sql .= " GROUP BY c.libelle, dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get social contributions deductible=0 " , LOG_DEBUG );
2005-04-16 01:50:38 +02:00
$result = $db -> query ( $sql );
if ( $result ) {
2010-01-13 19:51:19 +01:00
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2005-07-02 19:16:15 +02:00
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss [ $obj -> dm ])) $decaiss [ $obj -> dm ] = 0 ;
$decaiss [ $obj -> dm ] += $obj -> amount ;
if ( ! isset ( $decaiss_ttc [ $obj -> dm ])) $decaiss_ttc [ $obj -> dm ] = 0 ;
$decaiss_ttc [ $obj -> dm ] += $obj -> amount ;
2010-01-13 19:51:19 +01:00
$i ++ ;
2007-11-11 19:05:08 +01:00
}
2010-01-13 19:51:19 +01:00
}
2005-04-16 01:50:38 +02:00
} else {
2010-01-13 19:51:19 +01:00
dol_print_error ( $db );
2005-04-16 01:50:38 +02:00
}
/*
2008-02-20 01:39:33 +01:00
* Charges sociales deductibles
2005-04-16 01:50:38 +02:00
*/
2010-02-03 00:23:20 +01:00
2007-11-11 19:05:08 +01:00
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
if ( $modecompta == 'CREANCES-DETTES' )
{
2012-08-03 20:09:37 +02:00
$sql = " SELECT c.libelle as nom, date_format(cs.date_ech,'%Y-%m') as dm, sum(cs.amount) as amount " ;
2010-01-13 19:51:19 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_chargesociales as c " ;
2012-02-18 18:14:45 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " chargesociales as cs " ;
$sql .= " WHERE cs.fk_type = c.id " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND c.deductible = 1 " ;
2005-04-16 01:50:38 +02:00
}
2007-11-11 19:05:08 +01:00
else
{
2012-08-03 20:09:37 +02:00
$sql = " SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount " ;
2010-01-13 19:51:19 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_chargesociales as c " ;
2012-02-01 11:32:55 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " chargesociales as cs " ;
2010-01-13 19:51:19 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " paiementcharge as p " ;
2012-02-01 11:32:55 +01:00
$sql .= " WHERE p.fk_charge = cs.rowid " ;
$sql .= " AND cs.fk_type = c.id " ;
2010-01-13 19:51:19 +01:00
$sql .= " AND c.deductible = 1 " ;
2005-04-16 01:50:38 +02:00
}
2012-02-01 11:32:55 +01:00
$sql .= " AND cs.entity = " . $conf -> entity ;
2009-05-08 17:58:01 +02:00
$sql .= " GROUP BY c.libelle, dm " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " get social contributions paid deductible=1 " , LOG_DEBUG );
2005-04-16 01:50:38 +02:00
$result = $db -> query ( $sql );
if ( $result ) {
2010-01-13 19:51:19 +01:00
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num ) {
while ( $i < $num ) {
$obj = $db -> fetch_object ( $result );
2007-11-11 19:05:08 +01:00
2012-08-03 20:09:37 +02:00
if ( ! isset ( $decaiss [ $obj -> dm ])) $decaiss [ $obj -> dm ] = 0 ;
$decaiss [ $obj -> dm ] += $obj -> amount ;
if ( ! isset ( $decaiss_ttc [ $obj -> dm ])) $decaiss_ttc [ $obj -> dm ] = 0 ;
$decaiss_ttc [ $obj -> dm ] += $obj -> amount ;
2010-01-13 19:51:19 +01:00
$i ++ ;
2007-11-11 19:05:08 +01:00
}
2010-01-13 19:51:19 +01:00
}
2005-04-16 01:50:38 +02:00
} else {
2010-01-13 19:51:19 +01:00
dol_print_error ( $db );
2005-04-16 01:50:38 +02:00
}
2014-03-28 18:31:43 +01:00
$action = " balance " ;
$object = array ( & $encaiss , & $encaiss_ttc , & $decaiss , & $decaiss_ttc );
$parameters [ " mode " ] = $modecompta ;
// Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
$hookmanager -> initHooks ( array ( 'externalbalance' ));
$reshook = $hookmanager -> executeHooks ( 'addStatisticLine' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2005-01-31 19:47:14 +01:00
2014-09-16 21:05:57 +02:00
/*
* Salaries
*/
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
$sql = " SELECT p.label as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " payment_salary as p " ;
$sql .= " WHERE p.entity = " . $conf -> entity ;
$sql .= " GROUP BY p.label, dm " ;
dol_syslog ( " get social salaries payments " );
$result = $db -> query ( $sql );
if ( $result )
{
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num )
{
while ( $i < $num )
{
$obj = $db -> fetch_object ( $result );
2014-09-16 21:17:22 +02:00
if ( ! isset ( $decaiss [ $obj -> dm ])) $decaiss [ $obj -> dm ] = 0 ;
$decaiss [ $obj -> dm ] += $obj -> amount ;
2014-09-16 21:05:57 +02:00
if ( ! isset ( $decaiss_ttc [ $obj -> dm ])) $decaiss_ttc [ $obj -> dm ] = 0 ;
$decaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
}
}
}
else
{
dol_print_error ( $db );
}
/*
* get dunning paiement
*/
$subtotal_ht = 0 ;
$subtotal_ttc = 0 ;
$sql = " SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " don as p " ;
$sql .= " WHERE p.entity = " . $conf -> entity ;
$sql .= " AND fk_statut=2 " ;
$sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm " ;
dol_syslog ( " get social salaries payments " );
$result = $db -> query ( $sql );
if ( $result )
{
$num = $db -> num_rows ( $result );
$var = false ;
$i = 0 ;
if ( $num )
{
while ( $i < $num )
{
$obj = $db -> fetch_object ( $result );
2014-09-16 21:17:22 +02:00
if ( ! isset ( $encaiss [ $obj -> dm ])) $encaiss [ $obj -> dm ] = 0 ;
$encaiss [ $obj -> dm ] += $obj -> amount ;
2014-09-16 21:05:57 +02:00
if ( ! isset ( $encaiss_ttc [ $obj -> dm ])) $encaiss_ttc [ $obj -> dm ] = 0 ;
$encaiss_ttc [ $obj -> dm ] += $obj -> amount ;
$i ++ ;
}
}
}
else
{
dol_print_error ( $db );
}
2005-01-17 20:50:34 +01:00
/*
2010-02-03 00:23:20 +01:00
* Show result array
2005-01-17 20:50:34 +01:00
*/
2010-02-03 00:23:20 +01:00
2007-11-11 19:05:08 +01:00
$totentrees = array ();
$totsorties = array ();
2005-01-17 20:50:34 +01:00
print '<table class="noborder" width="100%">' ;
2012-01-14 12:38:03 +01:00
print '<tr class="liste_titre"><td class="liste_titre"> </td>' ;
2002-06-19 11:20:41 +02:00
2004-03-04 22:26:03 +01:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2014-11-07 23:25:39 +01:00
print '<td align="center" colspan="2" class="borderrightlight">' ;
2011-05-27 19:30:23 +02:00
print '<a href="clientfourn.php?year=' . $annee . '">' ;
print $annee ;
if ( $conf -> global -> SOCIETE_FISCAL_MONTH_START > 1 ) print '-' . ( $annee + 1 );
print '</a></td>' ;
2004-03-04 22:26:03 +01:00
}
print '</tr>' ;
2012-01-14 12:38:03 +01:00
print '<tr class="liste_titre"><td class="liste_titre">' . $langs -> trans ( " Month " ) . '</td>' ;
2004-03-04 22:26:03 +01:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2015-02-07 14:55:56 +01:00
print '<td align="center">' . $langs -> trans ( " Outcome " ) . '</td>' ;
print '<td align="center" class="borderrightlight">' . $langs -> trans ( " Income " ) . '</td>' ;
2004-03-04 22:26:03 +01:00
}
print '</tr>' ;
$var = True ;
2011-01-05 16:15:19 +01:00
2010-02-03 00:23:20 +01:00
// Loop on each month
2011-01-05 16:15:19 +01:00
$nb_mois_decalage = $conf -> global -> SOCIETE_FISCAL_MONTH_START ? ( $conf -> global -> SOCIETE_FISCAL_MONTH_START - 1 ) : 0 ;
for ( $mois = 1 + $nb_mois_decalage ; $mois <= 12 + $nb_mois_decalage ; $mois ++ )
2004-03-04 22:26:03 +01:00
{
2011-01-05 16:15:19 +01:00
$mois_modulo = $mois ;
if ( $mois > 12 ) { $mois_modulo = $mois - 12 ;}
2007-11-11 19:05:08 +01:00
$var =! $var ;
print '<tr ' . $bc [ $var ] . '>' ;
2011-01-05 16:15:19 +01:00
print " <td> " . dol_print_date ( dol_mktime ( 12 , 0 , 0 , $mois_modulo , 1 , $annee ), " %B " ) . " </td> " ;
2007-11-11 19:05:08 +01:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
2004-03-04 22:26:03 +01:00
{
2011-01-05 16:15:19 +01:00
$annee_decalage = $annee ;
if ( $mois > 12 ) { $annee_decalage = $annee + 1 ;}
$case = strftime ( " %Y-%m " , dol_mktime ( 12 , 0 , 0 , $mois_modulo , 1 , $annee_decalage ));
2007-11-11 19:05:08 +01:00
print '<td align="right"> ' ;
2012-08-03 20:09:37 +02:00
if ( isset ( $decaiss_ttc [ $case ]) && $decaiss_ttc [ $case ] != 0 )
2007-11-11 19:05:08 +01:00
{
2012-10-13 17:00:38 +02:00
print '<a href="clientfourn.php?year=' . $annee_decalage . '&month=' . $mois_modulo . ( $modecompta ? '&modecompta=' . $modecompta : '' ) . '">' . price ( price2num ( $decaiss_ttc [ $case ], 'MT' )) . '</a>' ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $totsorties [ $annee ])) $totsorties [ $annee ] = 0 ;
2007-11-11 19:05:08 +01:00
$totsorties [ $annee ] += $decaiss_ttc [ $case ];
}
print " </td> " ;
2014-11-07 23:25:39 +01:00
print '<td align="right" class="borderrightlight"> ' ;
2012-12-09 18:40:01 +01:00
//if (isset($encaiss_ttc[$case]) && $encaiss_ttc[$case] != 0)
if ( isset ( $encaiss_ttc [ $case ]))
2007-11-11 19:05:08 +01:00
{
2012-10-13 17:00:38 +02:00
print '<a href="clientfourn.php?year=' . $annee_decalage . '&month=' . $mois_modulo . ( $modecompta ? '&modecompta=' . $modecompta : '' ) . '">' . price ( price2num ( $encaiss_ttc [ $case ], 'MT' )) . '</a>' ;
2012-08-03 20:09:37 +02:00
if ( ! isset ( $totentrees [ $annee ])) $totentrees [ $annee ] = 0 ;
2007-11-11 19:05:08 +01:00
$totentrees [ $annee ] += $encaiss_ttc [ $case ];
}
print " </td> " ;
2004-03-04 22:26:03 +01:00
}
2002-06-19 00:23:12 +02:00
2007-11-11 19:05:08 +01:00
print '</tr>' ;
2004-03-04 22:26:03 +01:00
}
2002-06-19 00:23:12 +02:00
2005-05-07 03:27:54 +02:00
// Total
2004-03-04 22:26:03 +01:00
$var =! $var ;
2005-06-11 13:58:06 +02:00
$nbcols = 0 ;
2005-05-03 02:41:51 +02:00
print '<tr class="liste_total"><td>' . $langs -> trans ( " TotalTTC " ) . '</td>' ;
2004-03-04 22:26:03 +01:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2010-01-13 19:51:19 +01:00
$nbcols += 2 ;
2012-04-09 01:20:33 +02:00
print '<td align="right">' . ( isset ( $totsorties [ $annee ]) ? price ( price2num ( $totsorties [ $annee ], 'MT' )) : ' ' ) . '</td>' ;
2014-11-07 23:25:39 +01:00
print '<td align="right" style="border-right: 1px solid #DDD">' . ( isset ( $totentrees [ $annee ]) ? price ( price2num ( $totentrees [ $annee ], 'MT' )) : ' ' ) . '</td>' ;
2005-05-07 03:27:54 +02:00
}
print " </tr> \n " ;
2011-01-05 16:15:19 +01:00
// Empty line
2005-06-11 13:58:06 +02:00
print '<tr><td> </td>' ;
print '<td colspan="' . $nbcols . '"> </td>' ;
print " </tr> \n " ;
2005-05-07 03:27:54 +02:00
// Balance
$var =! $var ;
2015-02-07 14:55:56 +01:00
print '<tr class="liste_total"><td>' . $langs -> trans ( " AccountingResult " ) . '</td>' ;
2005-05-07 03:27:54 +02:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2014-11-07 23:25:39 +01:00
print '<td align="right" colspan="2" class="borderrightlight"> ' ;
2012-01-08 23:43:32 +01:00
if ( isset ( $totentrees [ $annee ]) || isset ( $totsorties [ $annee ]))
{
2012-08-03 20:09:37 +02:00
$in = ( isset ( $totentrees [ $annee ]) ? price2num ( $totentrees [ $annee ], 'MT' ) : 0 );
2013-03-12 11:49:15 +01:00
$out = ( isset ( $totsorties [ $annee ]) ? price2num ( $totsorties [ $annee ], 'MT' ) : 0 );
2012-08-03 20:09:37 +02:00
print price ( $in - $out ) . '</td>' ;
2010-01-13 19:51:19 +01:00
// print '<td> </td>';
}
2004-03-04 22:26:03 +01:00
}
print " </tr> \n " ;
2002-06-19 00:23:12 +02:00
2004-03-04 22:26:03 +01:00
print " </table> " ;
2002-06-19 00:23:12 +02:00
2011-08-27 16:24:16 +02:00
llxFooter ();
2012-01-08 23:43:32 +01:00
$db -> close ();