2004-10-19 22:35:36 +02:00
< ? php
2003-09-23 19:22:11 +02:00
/* Copyright ( C ) 2001 - 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2005-05-06 02:21:33 +02:00
* Copyright ( C ) 2004 - 2005 Laurent Destailleur < eldy @ users . sourceforge . net >
2003-09-23 19:22:11 +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-05-06 02:21:33 +02:00
/**
\file htdocs / compta / stats / cabyuser . php
\brief Page reporting CA par utilisateur
\version $Revision $
*/
2003-09-23 19:22:11 +02:00
require ( " ./pre.inc.php " );
2005-05-06 02:21:33 +02:00
$year = $_GET [ " year " ];
if ( ! $year ) { $year = strftime ( " %Y " , time ()); }
$modecompta = $conf -> compta -> mode ;
if ( $_GET [ " modecompta " ]) $modecompta = $_GET [ " modecompta " ];
2005-05-07 04:12:34 +02:00
$sortorder = isset ( $_GET [ " sortorder " ]) ? $_GET [ " sortorder " ] : $_POST [ " sortorder " ];
$sortfield = isset ( $_GET [ " sortfield " ]) ? $_GET [ " sortfield " ] : $_POST [ " sortfield " ];
2005-06-11 13:58:06 +02:00
if ( ! $sortorder ) $sortorder = " asc " ;
if ( ! $sortfield ) $sortfield = " name " ;
2005-05-07 04:12:34 +02:00
// S<> curit<69> acc<63> s client
if ( $user -> societe_id > 0 ) $socidp = $user -> societe_id ;
2005-05-06 02:21:33 +02:00
2003-09-23 19:22:11 +02:00
llxHeader ();
2005-05-06 02:21:33 +02:00
$html = new Form ( $db );
2003-09-23 19:22:11 +02:00
2005-05-06 02:21:33 +02:00
// Affiche en-t<> te du rapport
if ( $modecompta == " CREANCES-DETTES " )
{
$nom = " Chiffre d'affaire par utilisateur, auteur de la facture " ;
$nom .= ' (Voir le rapport <a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year ) . '&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 = $langs -> trans ( " Year " ) . " $year " ;
$periodlink = " <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year= " . ( $year - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year= " . ( $year + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " ;
2005-05-06 02:21:33 +02:00
$description = $langs -> trans ( " RulesCADue " );
$builddate = time ();
$exportlink = $langs -> trans ( " NotYetAvailable " );
2004-02-14 03:04:29 +01:00
}
2005-05-06 02:21:33 +02:00
else {
$nom = " Chiffre d'affaire par utilisateur, auteur de la facture " ;
$nom .= ' (Voir le rapport en <a href="' . $_SERVER [ " PHP_SELF " ] . '?year=' . ( $year ) . '&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 = $langs -> trans ( " Year " ) . " $year " ;
$periodlink = " <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year= " . ( $year - 1 ) . " &modecompta= " . $modecompta . " '> " . img_previous () . " </a> <a href=' " . $_SERVER [ " PHP_SELF " ] . " ?year= " . ( $year + 1 ) . " &modecompta= " . $modecompta . " '> " . img_next () . " </a> " ;
2005-05-06 02:21:33 +02:00
$description = $langs -> trans ( " RulesCAIn " );
$builddate = time ();
$exportlink = $langs -> trans ( " NotYetAvailable " );
}
$html -> report_header ( $nom , $nomlink , $period , $periodlink , $description , $builddate , $exportlink );
// Charge tableau
$catotal = 0 ;
if ( $modecompta == 'CREANCES-DETTES' )
{
$sql = " SELECT u.rowid as rowid, u.name as name, u.firstname as firstname, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u, " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " WHERE f.fk_statut = 1 AND f.fk_user_author = u.rowid " ;
if ( $year ) $sql .= " AND f.datef between ' " . $year . " -01-01 00:00:00' and ' " . $year . " -12-31 23:59:59' " ;
}
else
2003-09-23 19:22:11 +02:00
{
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 )
*/
$sql = " SELECT u.rowid as rowid, u.name as name, u.firstname as firstname, sum(pf.amount) as amount_ttc " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u, " . 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 AND f.fk_user_author = u.rowid " ;
if ( $year ) $sql .= " AND p.datep between ' " . $year . " -01-01 00:00:00' and ' " . $year . " -12-31 23:59:59' " ;
2003-09-23 19:22:11 +02:00
}
2005-05-06 02:21:33 +02:00
if ( $socidp ) $sql .= " AND f.fk_soc = $socidp " ;
$sql .= " GROUP BY rowid " ;
$sql .= " ORDER BY rowid " ;
2003-09-23 19:22:11 +02:00
$result = $db -> query ( $sql );
if ( $result )
{
2005-05-06 02:21:33 +02:00
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
2003-09-23 19:22:11 +02:00
{
2005-05-06 02:21:33 +02:00
$obj = $db -> fetch_object ( $result );
$amount [ $obj -> rowid ] = $obj -> amount_ttc ;
$name [ $obj -> rowid ] = $obj -> name . ' ' . $obj -> firstname ;
$catotal += $obj -> amount_ttc ;
$i ++ ;
2003-09-23 19:22:11 +02:00
}
}
2005-05-06 02:21:33 +02:00
else {
dolibarr_print_error ( $db );
}
2003-09-23 19:22:11 +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' )
{
$sql = " SELECT -1 as rowid, '' as name, '' as firstname, sum(p.amount) as amount_ttc " ;
$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 " ;
if ( $year ) $sql .= " AND p.datep between ' " . $year . " -01-01 00:00:00' and ' " . $year . " -12-31 23:59:59' " ;
$sql .= " GROUP BY rowid " ;
$sql .= " ORDER BY rowid " ;
2003-09-23 19:22:11 +02:00
2005-05-06 02:21:33 +02:00
$result = $db -> query ( $sql );
if ( $result )
{
$num = $db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ( $result );
$amount [ $obj -> rowid ] = $obj -> amount_ttc ;
$name [ $obj -> rowid ] = $obj -> name . ' ' . $obj -> firstname ;
$catotal += $obj -> amount_ttc ;
$i ++ ;
}
}
else {
dolibarr_print_error ( $db );
}
}
2003-09-23 19:22:11 +02:00
2005-05-06 02:21:33 +02:00
$i = 0 ;
print " <table class= \" noborder \" width= \" 100% \" > " ;
print " <tr class= \" liste_titre \" > " ;
2005-05-07 04:12:34 +02:00
print_liste_field_titre ( $langs -> trans ( " User " ), $_SERVER [ " PHP_SELF " ], " name " , " " , '&year=' . ( $year ) . '&modecompta=' . $modecompta , " " , $sortfield );
print_liste_field_titre ( $langs -> trans ( " AmountTTC " ), $_SERVER [ " PHP_SELF " ], " amount_ttc " , " " , '&year=' . ( $year ) . '&modecompta=' . $modecompta , 'align="right"' , $sortfield );
print_liste_field_titre ( $langs -> trans ( " Percentage " ), $_SERVER [ " PHP_SELF " ], " amount_ttc " , " " , '&year=' . ( $year ) . '&modecompta=' . $modecompta , 'align="right"' , $sortfield );
2005-05-06 02:21:33 +02:00
print " </tr> \n " ;
$var = true ;
if ( sizeof ( $amount ))
2003-09-23 19:22:11 +02:00
{
2005-05-07 04:12:34 +02:00
$arrayforsort = $name ;
// On d<> finit tableau arrayforsort
2005-06-11 13:58:06 +02:00
if ( $sortfield == 'name' && $sortorder == 'asc' ) {
2005-05-07 04:12:34 +02:00
asort ( $name );
$arrayforsort = $name ;
}
2005-06-11 13:58:06 +02:00
if ( $sortfield == 'name' && $sortorder == 'desc' ) {
2005-05-07 04:12:34 +02:00
arsort ( $name );
$arrayforsort = $name ;
}
if ( $sortfield == 'amount_ttc' && $sortorder == 'asc' ) {
asort ( $amount );
$arrayforsort = $amount ;
}
if ( $sortfield == 'amount_ttc' && $sortorder == 'desc' ) {
arsort ( $amount );
$arrayforsort = $amount ;
}
2005-06-11 13:58:06 +02:00
foreach ( $arrayforsort as $key => $value )
2003-09-23 19:22:11 +02:00
{
2005-05-06 02:21:33 +02:00
$var =! $var ;
print " <tr $bc[$var] > " ;
$fullname = $name [ $key ];
if ( $key >= 0 ) {
$linkname = '<a href="' . DOL_URL_ROOT . '/user/fiche.php?id=' . $key . '">' . img_object ( $langs -> trans ( " ShowUser " ), 'user' ) . ' ' . $fullname . '</a>' ;
}
else {
$linkname = $langs -> trans ( " Paiements li<6C> s <20> aucune facture " );
}
print " <td> " . $linkname . " </td> \n " ;
2005-05-07 04:12:34 +02:00
print '<td align="right">' . price ( $amount [ $key ]) . '</td>' ;
print '<td align="right">' . ( $catotal > 0 ? price ( 100 / $catotal * $amount [ $key ]) . '%' : ' ' ) . '</td>' ;
2005-05-06 02:21:33 +02:00
print " </tr> \n " ;
$i ++ ;
2003-09-23 19:22:11 +02:00
}
2005-05-06 02:21:33 +02:00
// Total
2005-09-05 21:27:58 +02:00
print '<tr class="liste_total"><td>' . $langs -> trans ( " Total " ) . '</td><td align="right">' . price ( $catotal ) . '</td><td> </td></tr>' ;
2005-05-06 02:21:33 +02:00
$db -> free ( $result );
2003-09-23 19:22:11 +02:00
}
2005-05-06 02:21:33 +02:00
print " </table> " ;
2003-09-23 19:22:11 +02:00
$db -> close ();
2005-05-06 02:21:33 +02:00
llxFooter ( '$Date$ - $Revision$' );
2003-09-23 19:22:11 +02:00
?>