* Copyright (C) 2004-2010 Laurent Destailleur * Copyright (C) 2005-2009 Regis Houssin * * 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. */ /** * \file htdocs/compta/stats/cabyuser.php * \brief Page reporting Salesover by user * \version $Id$ */ require('../../main.inc.php'); require_once(DOL_DOCUMENT_ROOT."/lib/report.lib.php"); require_once(DOL_DOCUMENT_ROOT."/lib/tax.lib.php"); require_once(DOL_DOCUMENT_ROOT."/lib/date.lib.php"); // Security check $socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:''; if ($user->societe_id > 0) $socid = $user->societe_id; if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire) accessforbidden(); // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES') $modecompta = $conf->compta->mode; if ($_GET["modecompta"]) $modecompta=$_GET["modecompta"]; $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"]; $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"]; if (! $sortorder) $sortorder="asc"; if (! $sortfield) $sortfield="name"; // Date range $year=$_REQUEST["year"]; if (empty($year)) { $year_current = strftime("%Y",dol_now()); $year_start = $year_current; } else { $year_current = $year; $year_start = $year; } $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]); $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]); // Quarter if (empty($date_start) || empty($date_end)) // We define date_start and date_end { $q=(! empty($_REQUEST["q"]))?$_REQUEST["q"]:0; if ($q==0) { if (isset($_REQUEST["month"])) { $date_start=dol_get_first_day($year_start,$_REQUEST["month"],false); $date_end=dol_get_last_day($year_start,$_REQUEST["month"],false); } else { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,12,false); } } if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); } if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); } if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); } if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); } } else { // TODO We define q } /* * View */ llxHeader(); $html=new Form($db); // Affiche en-tete du rapport if ($modecompta=="CREANCES-DETTES") { $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice"); $nom.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; $period=$html->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$html->select_date($date_end,'date_end',0,0,0,'',1,0,1); //$periodlink="".img_previous()." ".img_next().""; $description=$langs->trans("RulesCADue"); $builddate=time(); $exportlink=$langs->trans("NotYetAvailable"); } else { $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice"); $nom.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; $period=$html->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$html->select_date($date_end,'date_end',0,0,0,'',1,0,1); //$periodlink="".img_previous()." ".img_next().""; $description=$langs->trans("RulesCAIn"); $builddate=time(); $exportlink=$langs->trans("NotYetAvailable"); } 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"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid"; $sql.= " WHERE f.fk_statut in (1,2) "; if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; } else { /* * 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) */ $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" ; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid "; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement"; $sql.= " WHERE 1=1"; if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'"; } $sql.= " AND f.entity = ".$conf->entity; if ($socid) $sql.= " AND f.fk_soc = ".$socid; $sql .= " GROUP BY rowid, name, firstname"; $sql .= " ORDER BY rowid"; $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 { dol_print_error($db); } // On ajoute les paiements anciennes version, non lies 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."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; if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'"; $sql.= " GROUP BY rowid"; $sql.= " ORDER BY rowid"; $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 { dol_print_error($db); } } $i = 0; print ""; print ""; print_liste_field_titre($langs->trans("User"),$_SERVER["PHP_SELF"],"name","",'&year='.($year).'&modecompta='.$modecompta,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"amount_ttc","",'&year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Percentage"),$_SERVER["PHP_SELF"],"amount_ttc","",'&year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder); if ($conf->commande->enabled && $conf->global->MAIN_FEATURES_LEVEL == 2) print_liste_field_titre($langs->trans("OrderStats"),$_SERVER["PHP_SELF"],"","","",'align="center" width="20%"'); print "\n"; $var=true; if (sizeof($amount)) { $arrayforsort=$name; // We define arrayforsort if ($sortfield == 'name' && $sortorder == 'asc') { asort($name); $arrayforsort=$name; } if ($sortfield == 'name' && $sortorder == 'desc') { arsort($name); $arrayforsort=$name; } if ($sortfield == 'amount_ttc' && $sortorder == 'asc') { asort($amount); $arrayforsort=$amount; } if ($sortfield == 'amount_ttc' && $sortorder == 'desc') { arsort($amount); $arrayforsort=$amount; } foreach($arrayforsort as $key => $value) { $var=!$var; print ""; $fullname=$name[$key]; if ($key >= 0) { $linkname=''.img_object($langs->trans("ShowUser"),'user').' '.$fullname.''; } else { $linkname=$langs->trans("PaymentsNotLinkedToUser"); } print "\n"; print ''; print ''; if ($conf->commande->enabled && $conf->global->MAIN_FEATURES_LEVEL == 2) { if($key>0){ print ''; } else { print '' ; } } print "\n"; $i++; } // Total print ''; if ($conf->commande->enabled && $conf->global->MAIN_FEATURES_LEVEL == 2) print ''; print ''; $db->free($result); } print "
".$linkname."'.price($amount[$key]).''.($catotal > 0 ? round(100 * $amount[$key] / $catotal,2).'%' : ' ').''.img_picto($langs->trans("Show"),"vcard").'  
'.$langs->trans("Total").''.price($catotal).'  
"; $db->close(); llxFooter('$Date$ - $Revision$'); ?>