2004-10-19 22:35:36 +02:00
< ? php
2004-04-07 14:56:05 +02:00
/* Copyright ( C ) 2001 - 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2005-04-16 01:47:41 +02:00
* Copyright ( C ) 2004 - 2005 Laurent Destailleur < eldy @ users . sourceforge . net >
2003-06-29 11:01:29 +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 2 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 , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* $Id $
* $Source $
*
*/
2005-01-31 21:03:10 +01:00
/**
\file htdocs / compta / stats / index . php
\brief Page reporting CA
\version $Revision $
*/
2003-06-29 11:01:29 +02:00
require ( " ./pre.inc.php " );
2005-01-31 21:03:10 +01:00
$year_start = isset ( $_GET [ " year_start " ]) ? $_GET [ " year_start " ] : $_POST [ " year_start " ];
$year_current = strftime ( " %Y " , time ());
if ( ! $year_start ) {
2005-04-16 01:47:41 +02:00
$year_start = $year_current - 4 ;
2005-01-31 21:03:10 +01:00
$year_end = $year_current ;
}
else {
2005-04-16 01:47:41 +02:00
$year_end = $year_start + 4 ;
2005-01-31 21:03:10 +01:00
}
2003-06-29 11:01:29 +02:00
/*
* S<EFBFBD> curit<EFBFBD> acc<EFBFBD> s client
*/
if ( $user -> societe_id > 0 )
{
$socidp = $user -> societe_id ;
}
2005-04-07 23:21:44 +02:00
$modecompta = $conf -> compta -> mode ;
if ( $_GET [ " modecompta " ]) $modecompta = $_GET [ " modecompta " ];
2004-02-16 22:20:42 +01:00
2005-04-16 01:47:41 +02:00
2005-05-03 02:41:51 +02:00
llxHeader ();
2005-01-31 21:03:10 +01:00
2005-04-07 23:21:44 +02:00
2005-05-03 02:41:51 +02:00
$html = new Form ( $db );
// Affiche en-t<> te du rapport
2005-04-07 23:21:44 +02:00
if ( $modecompta == " CREANCES-DETTES " )
{
2005-05-06 02:59:14 +02:00
$nom = " Chiffre d'affaire " ;
2005-05-03 02:41:51 +02:00
$nom .= ' (Voir le rapport <a href="index.php?year_start=' . ( $year_start ) . '&modecompta=RECETTES-DEPENSES">recettes-d<> penses</a> pour n\'inclure que les factures effectivement pay<61> es)' ;
2005-06-11 13:58:06 +02:00
$period = " $year_start - $year_end " ;
$periodlink = ( $year_start ? " <a href='index.php?year_start= " . ( $year_start - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href='index.php?year_start= " . ( $year_start + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " : " " );
2005-05-03 02:41:51 +02:00
$description = $langs -> trans ( " RulesCADue " );
$builddate = time ();
$exportlink = $langs -> trans ( " NotYetAvailable " );
2005-04-07 23:21:44 +02:00
}
else {
2005-05-06 02:59:14 +02:00
$nom = " Chiffre d'affaire " ;
2005-05-03 02:41:51 +02:00
$nom .= ' (Voir le rapport en <a href="index.php?year_start=' . ( $year_start ) . '&modecompta=CREANCES-DETTES">cr<63> ances-dettes</a> pour inclure les factures non encore pay<61> e)' ;
2005-06-11 13:58:06 +02:00
$period = " $year_start - $year_end " ;
$periodlink = ( $year_start ? " <a href='index.php?year_start= " . ( $year_start - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href='index.php?year_start= " . ( $year_start + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " : " " );
2005-05-03 02:41:51 +02:00
$description = $langs -> trans ( " RulesCAIn " );
$builddate = time ();
$exportlink = $langs -> trans ( " NotYetAvailable " );
2005-04-07 23:21:44 +02:00
}
2005-05-03 02:41:51 +02:00
$html -> report_header ( $nom , $nomlink , $period , $periodlink , $description , $builddate , $exportlink );
2003-06-29 11:01:29 +02:00
2005-04-07 23:21:44 +02:00
2005-04-08 01:39:47 +02:00
if ( $modecompta == 'CREANCES-DETTES' ) {
2005-05-06 02:21:33 +02:00
$sql = " SELECT sum(f.total) as amount, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm " ;
2005-03-15 10:24:03 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " WHERE f.fk_statut = 1 " ;
} else {
2005-05-06 02:21:33 +02:00
/*
* Liste des paiements ( les anciens paiements ne sont pas vus par cette requete car , sur les
* vieilles versions , ils n ' <EFBFBD> taient pas li<EFBFBD> s via paiement_facture . On les ajoute plus loin )
*/
2005-05-06 02:59:14 +02:00
$sql = " SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm " ;
2005-05-06 02:21:33 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f, " . MAIN_DB_PREFIX . " paiement_facture as pf, " . MAIN_DB_PREFIX . " paiement as p " ;
$sql .= " WHERE p.rowid = pf.fk_paiement AND pf.fk_facture = f.rowid " ;
2003-06-29 11:01:29 +02:00
}
2005-05-06 02:21:33 +02:00
if ( $socidp ) $sql .= " AND f.fk_soc = $socidp " ;
2003-06-29 11:01:29 +02:00
$sql .= " GROUP BY dm DESC " ;
$result = $db -> query ( $sql );
if ( $result )
{
2005-04-07 23:21:44 +02:00
$num = $db -> num_rows ( $result );
2005-04-16 01:47:41 +02:00
$i = 0 ;
2005-04-07 23:21:44 +02:00
while ( $i < $num )
2003-06-29 11:01:29 +02:00
{
2005-04-07 23:21:44 +02:00
$obj = $db -> fetch_object ( $result );
2005-05-06 02:59:14 +02:00
$cum [ $obj -> dm ] = $obj -> amount_ttc ;
if ( $obj -> amount_ttc )
2005-04-16 01:47:41 +02:00
{
$minyearmonth = ( $minyearmonth ? min ( $minyearmonth , $obj -> dm ) : $obj -> dm );
$maxyearmonth = max ( $maxyearmonth , $obj -> dm );
}
2005-04-07 23:21:44 +02:00
$i ++ ;
2003-06-29 11:01:29 +02:00
}
2005-04-07 23:21:44 +02:00
$db -> free ( $result );
}
else {
dolibarr_print_error ( $db );
2003-06-29 11:01:29 +02:00
}
2005-05-06 02:21:33 +02:00
// On ajoute les paiements anciennes version, non li<6C> s par paiement_facture
if ( $modecompta != 'CREANCES-DETTES' ) {
2005-05-06 02:59:14 +02:00
$sql = " SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm " ;
2005-05-06 02:21:33 +02:00
$sql .= " FROM " . 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 .= " GROUP BY dm " ;
$sql .= " ORDER BY dm " ;
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ( $result );
2005-05-06 02:59:14 +02:00
$cum [ $obj -> dm ] += $obj -> amount_ttc ;
if ( $obj -> amount_ttc )
2005-05-06 02:21:33 +02:00
{
$minyearmonth = ( $minyearmonth ? min ( $minyearmonth , $obj -> dm ) : $obj -> dm );
$maxyearmonth = max ( $maxyearmonth , $obj -> dm );
}
$i ++ ;
}
}
else {
dolibarr_print_error ( $db );
}
}
2005-01-19 23:06:15 +01:00
print '<table width="100%" class="noborder">' ;
2004-07-31 15:46:12 +02:00
print '<tr class="liste_titre"><td rowspan="2">' . $langs -> trans ( " Month " ) . '</td>' ;
2003-06-29 11:01:29 +02:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2005-05-07 04:48:17 +02:00
print '<td align="center" width="10%" colspan="2"><a href="casoc.php?year=' . $annee . '">' . $annee . '</a></td>' ;
2005-06-11 13:58:06 +02:00
if ( $annee != $year_end ) print '<td width="15"> </td>' ;
2003-06-29 11:01:29 +02:00
}
2004-02-08 18:38:37 +01:00
print '</tr>' ;
2004-02-16 22:20:42 +01:00
print '<tr class="liste_titre">' ;
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2005-05-06 02:59:14 +02:00
print '<td align="right">' . $langs -> trans ( " AmountTTC " ) . '</td>' ;
2005-06-11 13:58:06 +02:00
print '<td align="right">' . $langs -> trans ( " Delta " ) . '</td>' ;
if ( $annee != $year_end ) print '<td width="15"> </td>' ;
2004-02-16 22:20:42 +01:00
}
print '</tr>' ;
2005-04-16 01:47:41 +02:00
2005-03-15 10:24:03 +01:00
$total_CA = 0 ;
2005-04-16 01:47:41 +02:00
$now_show_delta = 0 ;
$minyear = substr ( $minyearmonth , 0 , 4 );
$maxyear = substr ( $maxyearmonth , 0 , 4 );
$nowyear = strftime ( " %Y " , mktime ());
$nowyearmonth = strftime ( " %Y-%m " , mktime ());
2003-06-29 11:01:29 +02:00
for ( $mois = 1 ; $mois < 13 ; $mois ++ )
{
2005-04-16 01:47:41 +02:00
$var =! $var ;
print " <tr $bc[$var] > " ;
2003-06-29 11:01:29 +02:00
2005-04-16 01:47:41 +02:00
print " <td> " . strftime ( " %B " , mktime ( 1 , 1 , 1 , $mois , 1 , 2000 )) . " </td> " ;
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
2003-06-29 11:01:29 +02:00
{
2005-04-16 01:47:41 +02:00
$casenow = strftime ( " %Y-%m " , mktime ());
$case = strftime ( " %Y-%m " , mktime ( 1 , 1 , 1 , $mois , 1 , $annee ));
$caseprev = strftime ( " %Y-%m " , mktime ( 1 , 1 , 1 , $mois , 1 , $annee - 1 ));
2004-02-16 22:20:42 +01:00
2005-04-16 01:47:41 +02:00
if ( $annee == $year_current ) {
$total_CA += $cum [ $case ];
}
2004-04-07 14:56:05 +02:00
2005-04-16 01:47:41 +02:00
// Valeur CA du mois
print '<td align="right">' ;
if ( $cum [ $case ])
{
$now_show_delta = 1 ; // On a trouv<75> le premier mois de la premi<6D> re ann<6E> e g<> n<EFBFBD> rant du chiffre.
print price ( $cum [ $case ], 1 );
}
else
{
if ( $minyearmonth < $case && $case <= max ( $maxyearmonth , $nowyearmonth )) { print '0' ; }
else { print ' ' ; }
}
print " </td> " ;
// Pourcentage du mois
if ( $annee > $minyear && $case <= $casenow ) {
if ( $cum [ $caseprev ] && $cum [ $case ])
{
$percent = ( round (( $cum [ $case ] - $cum [ $caseprev ]) / $cum [ $caseprev ], 4 ) * 100 );
//print "X $cum[$case] - $cum[$caseprev] - $cum[$caseprev] - $percent X";
print '<td align="right">' . ( $percent >= 0 ? " + $percent " : " $percent " ) . '%</td>' ;
}
if ( $cum [ $caseprev ] && ! $cum [ $case ])
{
print '<td align="right">-100%</td>' ;
}
if ( ! $cum [ $caseprev ] && $cum [ $case ])
{
print '<td align="right">+Inf%</td>' ;
}
if ( ! $cum [ $caseprev ] && ! $cum [ $case ])
{
print '<td align="right">+0%</td>' ;
}
}
else
{
2005-06-11 13:58:06 +02:00
print '<td align="right">' ;
2005-04-16 01:47:41 +02:00
if ( $minyearmonth <= $case && $case <= $maxyearmonth ) { print '-' ; }
else { print ' ' ; }
print '</td>' ;
}
$total [ $annee ] += $cum [ $case ];
2005-06-11 13:58:06 +02:00
if ( $annee != $year_end ) print '<td width="15"> </td>' ;
2003-06-29 11:01:29 +02:00
}
2005-04-16 01:47:41 +02:00
print '</tr>' ;
2003-06-29 11:01:29 +02:00
}
2004-02-08 18:38:37 +01:00
// Affiche total
2005-06-11 13:58:06 +02:00
print '<tr class="liste_total"><td>' . $langs -> trans ( " Total " ) . '</td>' ;
2004-02-08 18:38:37 +01:00
for ( $annee = $year_start ; $annee <= $year_end ; $annee ++ )
{
2005-04-16 01:47:41 +02:00
// Montant total
if ( $annee >= $minyear && $annee <= max ( $nowyear , $maxyear ))
{
2005-05-03 02:41:51 +02:00
print " <td align= \" right \" nowrap> " . ( $total [ $annee ] ? price ( $total [ $annee ]) : " 0 " ) . " </td> " ;
2005-04-16 01:47:41 +02:00
}
else
{
print '<td> </td>' ;
}
// Pourcentage total
if ( $annee > $minyear && $annee <= max ( $nowyear , $maxyear )) {
if ( $total [ $annee - 1 ] && $total [ $annee ]) {
$percent = ( round (( $total [ $annee ] - $total [ $annee - 1 ]) / $total [ $annee - 1 ], 4 ) * 100 );
print '<td align="right" nowrap>' . ( $percent >= 0 ? " + $percent " : " $percent " ) . '%</td>' ;
}
if ( $total [ $annee - 1 ] && ! $total [ $annee ])
{
print '<td align="right">-100%</td>' ;
}
if ( ! $total [ $annee - 1 ] && $total [ $annee ])
{
print '<td align="right">+Inf%</td>' ;
}
if ( ! $total [ $annee - 1 ] && ! $total [ $annee ])
{
print '<td align="right">+0%</td>' ;
2005-01-19 23:06:15 +01:00
}
2004-04-07 14:56:05 +02:00
}
else
{
2005-06-11 13:58:06 +02:00
print '<td align="right">' ;
2005-04-16 01:47:41 +02:00
if ( $minyear <= $annee && $annee <= max ( $nowyear , $maxyear )) { print '-' ; }
else { print ' ' ; }
print '</td>' ;
2004-04-07 14:56:05 +02:00
}
2005-04-16 01:47:41 +02:00
2005-06-11 13:58:06 +02:00
if ( $annee != $year_end ) print '<td width="15"> </td>' ;
2004-02-08 18:38:37 +01:00
}
print " </tr> \n " ;
2005-04-16 01:47:41 +02:00
print " </table> " ;
2005-04-08 01:39:47 +02:00
2005-03-15 10:24:03 +01:00
/*
2005-04-08 01:39:47 +02:00
* En mode recettes / d<EFBFBD> penses , on compl<EFBFBD> te avec les montants factur<EFBFBD> s non r<EFBFBD> gl<EFBFBD> s
* et les propales sign<EFBFBD> es mais pas factur<EFBFBD> es . En effet , en recettes - d<EFBFBD> penses ,
* on comptabilise lorsque le montant est sur le compte donc il est int<EFBFBD> ressant
* d ' avoir une vision de ce qui va arriver .
*/
2005-04-16 01:47:41 +02:00
/*
Je commente toute cette partie car les chiffres affich<EFBFBD> es sont faux - Eldy .
En attendant correction .
if ( $modecompta != 'CREANCES-DETTES' )
{
2005-04-08 01:39:47 +02:00
2005-04-16 01:47:41 +02:00
print '<br><table width="100%" class="noborder">' ;
2005-04-08 01:39:47 +02:00
// Factures non r<> gl<67> es
2005-04-16 01:47:41 +02:00
// \todo Y a bug ici. Il faut prendre le reste <20> payer et non le total des factures non r<> gl<67> es !
2003-06-29 11:01:29 +02:00
2005-03-15 10:24:03 +01:00
$sql = " SELECT f.facnumber, f.rowid, s.nom, s.idp, f.total_ttc, sum(pf.amount) as am " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s, " . MAIN_DB_PREFIX . " facture as f left join " . MAIN_DB_PREFIX . " paiement_facture as pf on f.rowid=pf.fk_facture " ;
$sql .= " WHERE s.idp = f.fk_soc AND f.paye = 0 AND f.fk_statut = 1 " ;
if ( $socidp )
{
$sql .= " AND f.fk_soc = $socidp " ;
}
$sql .= " GROUP BY f.facnumber,f.rowid,s.nom, s.idp, f.total_ttc " ;
if ( $db -> query ( $sql ) )
{
$num = $db -> num_rows ();
$i = 0 ;
if ( $num )
{
$var = True ;
$total_ttc_Rac = $totalam_Rac = $total_Rac = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ();
$total_ttc_Rac += $obj -> total_ttc ;
$totalam_Rac += $obj -> am ;
$i ++ ;
}
$var =! $var ;
2005-04-16 01:47:41 +02:00
print " <tr $bc[$var] ><td align= \" right \" colspan= \" 5 \" ><i>Factur<75> <20> encaisser : </i></td><td align= \" right \" ><i> " . price ( $total_ttc_Rac ) . " </i></td><td colspan= \" 5 \" ><-- bug ici car n'exclut pas le deja r<> gl<67> des factures partiellement r<> gl<67> es</td></tr> " ;
2005-03-15 10:24:03 +01:00
$total_CA += $total_ttc_Rac ;
}
$db -> free ();
}
else
{
dolibarr_print_error ( $db );
}
2005-04-16 01:47:41 +02:00
*/
2005-03-15 10:24:03 +01:00
/*
*
* Propales sign<EFBFBD> es , et non factur<EFBFBD> es
*
*/
2005-04-16 01:47:41 +02:00
/*
Je commente toute cette partie car les chiffres affich<EFBFBD> es sont faux - Eldy .
En attendant correction .
2005-03-15 10:24:03 +01:00
$sql = " SELECT sum(f.total) as tot_fht,sum(f.total_ttc) as tot_fttc, p.rowid, p.ref, s.nom, s.idp, p.total_ht, p.total_ttc
FROM " .MAIN_DB_PREFIX. " commande AS p , llx_societe AS s
LEFT JOIN " .MAIN_DB_PREFIX. " co_fa AS co_fa ON co_fa . fk_commande = p . rowid
LEFT JOIN " .MAIN_DB_PREFIX. " facture AS f ON co_fa . fk_facture = f . rowid
WHERE p . fk_soc = s . idp
AND p . fk_statut >= 1
AND p . facture = 0 " ;
if ( $socidp )
{
$sql .= " AND f.fk_soc = $socidp " ;
}
$sql .= " GROUP BY p.rowid " ;
if ( $db -> query ( $sql ) )
{
$num = $db -> num_rows ();
$i = 0 ;
if ( $num )
{
$var = True ;
$total_pr = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ();
$total_pr += $obj -> total_ttc - $obj -> tot_fttc ;
$i ++ ;
}
$var =! $var ;
2005-04-16 01:47:41 +02:00
print " <tr $bc[$var] ><td align= \" right \" colspan= \" 5 \" ><i>Sign<67> et non factur<75> :</i></td><td align= \" right \" ><i> " . price ( $total_pr ) . " </i></td><td colspan= \" 5 \" ><-- bug ici, ca devrait exclure le d<> j<EFBFBD> factur<75> </td></tr> " ;
2005-03-15 10:24:03 +01:00
$total_CA += $total_pr ;
}
$db -> free ();
}
else
{
dolibarr_print_error ( $db );
}
2005-04-16 01:47:41 +02:00
print " <tr $bc[$var] ><td align= \" right \" colspan= \" 5 \" ><i>Total CA pr<70> visionnel : </i></td><td align= \" right \" ><i> " . price ( $total_CA ) . " </i></td><td colspan= \" 3 \" ><-- bug ici car bug sur les 2 pr<70> c<EFBFBD> dents</td></tr> " ;
2005-03-15 10:24:03 +01:00
}
print " </table> " ;
2005-04-16 01:47:41 +02:00
*/
2003-06-29 11:01:29 +02:00
$db -> close ();
2005-05-06 02:59:14 +02:00
llxFooter ( '$Date$ - $Revision$' );
2003-06-29 11:01:29 +02:00
?>