diff --git a/htdocs/compta/resultat/clientfourn.php b/htdocs/compta/resultat/clientfourn.php index 6fe2755865d..72d87c0bf65 100644 --- a/htdocs/compta/resultat/clientfourn.php +++ b/htdocs/compta/resultat/clientfourn.php @@ -52,16 +52,16 @@ $html=new Form($db); if ($modecompta=="CREANCES-DETTES") { $nom="Bilan des recettes et dépenses, détail"; - $nom.=' (Voir le rapport en recettes-dépenses pour n\'inclure que les factures effectivement payées)'; - $period="".img_previous()." ".$langs->trans("Year")." $year ".img_next().""; + $nom.=' (Voir le rapport en recettes-dépenses pour n\'inclure que les factures effectivement payées)'; + $period=''.img_previous()." ".$langs->trans("Year")." ".$year.' '.img_next().''; $description=$langs->trans("RulesResultDue"); $builddate=time(); $exportlink=$langs->trans("NotYetAvailable"); } else { $nom="Bilan des recettes et dépenses, détail"; - $nom.=' (Voir le rapport en créances-dettes pour inclure les factures non encore payée)'; - $period="".img_previous()." ".$langs->trans("Year")." $year ".img_next().""; + $nom.=' (Voir le rapport en créances-dettes pour inclure les factures non encore payée)'; + $period=''.img_previous()." ".$langs->trans("Year")." ".$year.' '.img_next().''; $description=$langs->trans("RulesResultInOut"); $builddate=time(); $exportlink=$langs->trans("NotYetAvailable"); diff --git a/htdocs/compta/stats/casoc.php b/htdocs/compta/stats/casoc.php index 275f731dbbe..fc405a4e66a 100644 --- a/htdocs/compta/stats/casoc.php +++ b/htdocs/compta/stats/casoc.php @@ -29,6 +29,16 @@ require("./pre.inc.php"); +$year=$_GET["year"]; +if (! $year) { $year = strftime("%Y", time()); } +$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="desc"; +if (! $sortfield) $sortfield="amount_ttc"; + /* * Sécurité accés client */ @@ -36,94 +46,167 @@ if ($user->societe_id > 0) { $socidp = $user->societe_id; } -/* - * - */ + + + llxHeader(); -print_titre("Chiffre d'affaire par société (euros HT)"); +$html=new Form($db); -/* - * Ca total - * - */ - -$sql = "SELECT sum(f.total) as ca FROM ".MAIN_DB_PREFIX."facture as f"; -$sql .= " WHERE f.fk_statut = 1"; -if ($conf->compta->mode != 'CREANCES-DETTES') { - $sql .= " AND f.paye = 1"; -} -if ($socidp) +// Affiche en-tête de rapport +if ($modecompta=="CREANCES-DETTES") { - $sql .= " AND f.fk_soc = $socidp"; + $nom="Chiffre d'affaire par société"; + $nom.=' (Voir le rapport en recettes-dépenses pour n\'inclure que les factures effectivement payées)'; + $period=''.img_previous()." ".$langs->trans("Year")." ".$year.' '.img_next().''; + $description=$langs->trans("RulesCADue"); + $builddate=time(); + $exportlink=$langs->trans("NotYetAvailable"); } +else { + $nom="Chiffre d'affaire par société"; + $nom.=' (Voir le rapport en créances-dettes pour inclure les factures non encore payée)'; + $period=''.img_previous()." ".$langs->trans("Year")." ".$year.' '.img_next().''; + $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 s.idp as rowid, s.nom as name, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f"; + $sql .= " WHERE f.fk_statut = 1 AND f.fk_soc = s.idp"; + if ($year) $sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'"; +} +else +{ + /* + * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les + * vieilles versions, ils n'étaient pas liés via paiement_facture. On les ajoute plus loin) + */ + $sql = "SELECT s.idp as rowid, s.nom as name, sum(pf.amount) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".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_soc = s.idp"; + if ($year) $sql .= " AND p.datep between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'"; +} +if ($socidp) $sql .= " AND f.fk_soc = $socidp"; +$sql .= " GROUP BY rowid"; +$sql .= " ORDER BY rowid"; + $result = $db->query($sql); if ($result) { - if ($db->num_rows() > 0) + $num = $db->num_rows($result); + $i=0; + while ($i < $num) { - $objp = $db->fetch_object($result); - $catotal = $objp->ca; + $obj = $db->fetch_object($result); + $amount[$obj->rowid] = $obj->amount_ttc; + $name[$obj->rowid] = $obj->name; + $catotal+=$obj->amount_ttc; + $i++; } } else { - print $db->error() . "
" . $sql; + dolibarr_print_error($db); } -print "
Cumul : ".price($catotal).""; - -if ($catotal == 0) { $catotal = 1; }; - - -$sql = "SELECT s.nom, s.idp, sum(f.total) as ca"; -$sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f"; -$sql .= " WHERE f.fk_statut = 1 AND f.fk_soc = s.idp"; -if ($conf->compta->mode != 'CREANCES-DETTES') { - $sql .= " AND f.paye = 1"; -} -if ($socidp) +// On ajoute les paiements anciennes version, non liés par paiement_facture +if ($modecompta != 'CREANCES-DETTES') { - $sql .= " AND f.fk_soc = $socidp"; -} -$sql .= " GROUP BY s.nom, s.idp ORDER BY ca DESC"; - -$result = $db->query($sql); -if ($result) -{ - $num = $db->num_rows(); - if ($num > 0) + $sql = "SELECT 'Autres' as nom, '0' as idp, 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 nom"; + $sql .= " ORDER BY nom"; + + $result = $db->query($sql); + if ($result) { - $i = 0; - print "

"; - print ""; - print ""; - print ''; - print "\n"; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ""; - - print "\n"; - print ''; - print ''; - print "\n"; - - print "\n"; - $i++; - } - print "
SociétéMontantPourcentage 
idp\">$objp->nom'.price($objp->ca).''.price(100 / $catotal * $objp->ca).'%idp\">Voir les factures
"; + $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; + $catotal+=$obj->amount_ttc; + $i++; + } + } + else { + dolibarr_print_error($db); } - $db->free(); } -else + + +$i = 0; +print ""; +print ""; +print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"nom","",'&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); +print "\n"; +$var=true; + +if (sizeof($amount)) { - print $db->error(); + $arrayforsort=$name; + + // On définit tableau arrayforsort + if ($sortfield == 'nom' && $sortorder == 'asc') { + asort($name); + $arrayforsort=$name; + } + if ($sortfield == 'nom' && $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("ShowCompany"),'company').' '.$fullname.''; + } + else { + $linkname=$langs->trans("Autres paiements liés à aucune facture, donc aucune société"); + } + print "\n"; + print ''; + print ''; + print "\n"; + $i++; + } + + // Total + print ''; + + $db->free($result); } +print "
".$linkname."'.price($amount[$key]).''.($catotal > 0 ? price(100 / $catotal * $amount[$key]).'%' : ' ').'
'.$langs->trans("Total").''.$catotal.' 
"; + $db->close(); -llxFooter("Dernière modification $Date$ révision $Revision$"); + +llxFooter('$Date$ - $Revision$'); ?>