Corrections mauvais rsultat du ca par utilisateur et support des 2 modes comptables.

This commit is contained in:
Laurent Destailleur 2005-05-06 00:21:33 +00:00
parent be04b07eb5
commit 0ace4b44f2
7 changed files with 234 additions and 109 deletions

View File

@ -80,30 +80,26 @@ print '<tr><td colspan="4">&nbsp;</td></tr>';
/*
* Factures clients
*/
print '<tr><td colspan="4">Facturation clients</td></tr>';
if ($modecompta == 'CREANCES-DETTES') {
$sql = "SELECT s.nom, s.idp, sum(f.total) as amount_ht, 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_soc = s.idp AND f.fk_statut = 1";
if ($year) {
$sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
if ($year) $sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
} else {
/*
* Liste des paiements par société (les anciens paiements ne sont pas inclus
* car n'était pas liés sur les vieilles versions)
* 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.nom as nom, s.idp as idp, sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
$sql = "SELECT s.nom as nom, s.idp as idp, 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 f.fk_soc = s.idp AND f.rowid = pf.fk_facture AND pf.fk_paiement = p.rowid";
if ($year) {
$sql .= " AND p.datep between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$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'";
}
$sql .= " GROUP BY nom";
$sql .= " ORDER BY nom";
print '<tr><td colspan="4">Facturation clients</td></tr>';
$result = $db->query($sql);
if ($result) {
$num = $db->num_rows($result);
@ -130,15 +126,13 @@ if ($result) {
dolibarr_print_error($db);
}
// Ajoute paiements anciennes version non liés par paiement_facture
// On ajoute les paiements anciennes version, non liés par paiement_facture
if ($modecompta != 'CREANCES-DETTES') {
$sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
$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'";
}
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";

View File

@ -84,18 +84,17 @@ if ($modecompta == 'CREANCES-DETTES') {
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
$sql .= " WHERE f.fk_soc = s.idp AND f.fk_statut = 1";
} else {
$sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f";
$sql .= " ON f.rowid = p.fk_facture";
$sql .= " WHERE 1=1";
}
if ($socidp)
{
$sql .= " AND f.fk_soc = $socidp";
/*
* 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 sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
$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";
}
if ($socidp) $sql .= " AND f.fk_soc = $socidp";
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm";
$result=$db->query($sql);
if ($result)
@ -115,6 +114,32 @@ else {
dolibarr_print_error($db);
}
// On ajoute les paiements anciennes version, non liés par paiement_facture
if ($modecompta != 'CREANCES-DETTES') {
$sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
$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)
{
$row = $db->fetch_object($result);
$encaiss[$row->dm] += $row->amount_ht;
$encaiss_ttc[$row->dm] += $row->amount_ttc;
$i++;
}
}
else {
dolibarr_print_error($db);
}
}
/*
* Frais, factures fournisseurs.

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -20,98 +20,167 @@
* $Source$
*
*/
/**
\file htdocs/compta/stats/cabyuser.php
\brief Page reporting CA par utilisateur
\version $Revision$
*/
require("./pre.inc.php");
/*
* Sécurité accés client
*/
if ($user->societe_id > 0)
if ($user->societe_id > 0)
{
$socidp = $user->societe_id;
$socidp = $user->societe_id;
}
/*
*
*/
$year=$_GET["year"];
if (! $year) { $year = strftime("%Y", time()); }
$modecompta = $conf->compta->mode;
if ($_GET["modecompta"]) $modecompta=$_GET["modecompta"];
llxHeader();
print_titre("Chiffre d'affaire par utilisateur (euros HT)");
/*
* Ca total
*
*/
$html=new Form($db);
$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 du rapport
if ($modecompta=="CREANCES-DETTES")
{
$sql .= " AND f.fk_soc = $socidp";
$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ées)';
$period="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> ".$langs->trans("Year")." $year <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
$description=$langs->trans("RulesCADue");
$builddate=time();
$exportlink=$langs->trans("NotYetAvailable");
}
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éances-dettes</a> pour inclure les factures non encore payée)';
$period="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> ".$langs->trans("Year")." $year <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
$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
{
/*
* 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 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'";
}
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.' '.$obj->firstname;
$catotal+=$obj->amount_ttc;
$i++;
}
}
else {
dolibarr_print_error($db);
}
// On ajoute les paiements anciennes version, non lié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";
$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);
}
}
print "<br><b>Cumul : ".price($catotal)."</b>";
if ($catotal == 0) { $catotal = 1; };
$i = 0;
print "<table class=\"noborder\" width=\"100%\">";
print "<tr class=\"liste_titre\">";
print "<td>".$langs->trans("User")."</td>";
print '<td align="right">'.$langs->trans("AmountTTC").'</td><td align="right">'.$langs->trans("Percentage").'</td>';
print "</tr>\n";
$var=true;
$sql = "SELECT u.name, u.firstname, sum(f.total) as ca";
$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 ($conf->compta->mode != 'CREANCES-DETTES') {
$sql .= " AND f.paye = 1";
}
if ($socidp)
if (sizeof($amount))
{
$sql .= " AND f.fk_soc = $socidp";
}
$sql .= " GROUP BY u.name, u.firstname ORDER BY ca DESC";
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows();
if ($num > 0)
foreach($amount as $key=>$value)
{
$i = 0;
print "<p><table class=\"noborder\" width=\"100%\">";
print "<tr class=\"liste_titre\">";
print "<td>Utilisateur</td>";
print '<td align="right">Montant</TD><td align="right">Pourcentage</td>';
print "</tr>\n";
$var=True;
while ($i < $num)
{
$objp = $db->fetch_object($result);
$var=!$var;
print "<tr $bc[$var]>";
print "<td>$objp->firstname $objp->name</td>\n";
print '<td align="right">'.price($objp->ca).'</td>';
print '<td align="right">'.price(100 / $catotal * $objp->ca).'%</td>';
print "</tr>\n";
$i++;
}
print "</table>";
$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és à aucune facture");
}
print "<td>".$linkname."</td>\n";
print '<td align="right">'.price($value).'</td>';
print '<td align="right">'.($catotal > 0 ? price(100 / $catotal * $value).'%' : '&nbsp;').'</td>';
print "</tr>\n";
$i++;
}
$db->free();
}
else
{
print $db->error();
// Total
print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">'.$catotal.'</td><td>&nbsp;</td></tr>';
$db->free($result);
}
print "</table>";
$db->close();
llxFooter("<em>Derni&egrave;re modification $Date$ r&eacute;vision $Revision$</em>");
llxFooter('$Date$ - $Revision$');
?>

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -20,6 +20,13 @@
* $Source$
*
*/
/**
\file htdocs/compta/stats/cassoc.php
\brief Page reporting CA par société
\version $Revision$
*/
require("./pre.inc.php");
/*

View File

@ -79,27 +79,21 @@ $html->report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$
if ($modecompta == 'CREANCES-DETTES') {
$sql = "SELECT sum(f.total) as amount, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
$sql = "SELECT sum(f.total) as amount, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
$sql .= " WHERE f.fk_statut = 1";
$sql .= " AND f.paye = 1";
} else {
/* $sql = "SELECT sum(f.total) as amount, date_format(p.datep,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f ";
$sql .= "left join ".MAIN_DB_PREFIX."paiement as p ";
$sql .= "on f.rowid = p.fk_facture";*/
$sql = "SELECT sum(p.amount) as amount, date_format(p.datep,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."paiement as p ";
$sql .= "left join ".MAIN_DB_PREFIX."facture as f ";
$sql .= "on f.rowid = p.fk_facture";
}
if ($socidp)
{
$sql .= " AND f.fk_soc = $socidp";
/*
* 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 sum(pf.amount) as amount, date_format(p.datep,'%Y-%m') as dm";
$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";
}
if ($socidp) $sql .= " AND f.fk_soc = $socidp";
$sql .= " GROUP BY dm DESC";
$result = $db->query($sql);
if ($result)
{
@ -122,6 +116,40 @@ else {
dolibarr_print_error($db);
}
// On ajoute les paiements anciennes version, non liés par paiement_facture
if ($modecompta != 'CREANCES-DETTES') {
$sql = "SELECT sum(p.amount) as amount, date_format(p.datep,'%Y-%m') as dm";
$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);
$cum[$obj->dm] += $obj->amount;
if ($obj->amount)
{
$minyearmonth=($minyearmonth?min($minyearmonth,$obj->dm):$obj->dm);
$maxyearmonth=max($maxyearmonth,$obj->dm);
}
$i++;
}
}
else {
dolibarr_print_error($db);
}
}
print '<table width="100%" class="noborder">';
print '<tr class="liste_titre"><td rowspan="2">'.$langs->trans("Month").'</td>';

View File

@ -136,6 +136,7 @@ AmountTTC=Amount&nbsp;TTC
AmountVAT=Amount&nbsp;VAT
AmountTotal=Total amount
AmountAverage=Average amount
Percentage=Pourcentage
Total=Total
SubTotal=Subtotal
TotalHT=Total HT

View File

@ -136,6 +136,7 @@ AmountTTC=Montant&nbsp;TTC
AmountVAT=Montant&nbsp;TVA
AmountTotal=Montant total
AmountAverage=Montant moyen
Percentage=Pourcentage
Total=Total
SubTotal=Sous total
TotalHT=Total HT