[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/compta/resultat/ -> index.php (source)

   1  <?php
   2  /* Copyright (C) 2003      Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   * Copyright (C) 2004-2005 Laurent Destailleur  <eldy@users.sourceforge.net>
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License
  16   * along with this program; if not, write to the Free Software
  17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18   *
  19   * $Id: index.php,v 1.27 2005/07/02 17:16:15 eldy Exp $
  20   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/compta/resultat/index.php,v $
  21   *
  22   */
  23  
  24  /**
  25          \file        htdocs/compta/resultat/index.php
  26          \brief       Page reporting resultat
  27          \version     $Revision: 1.27 $
  28  */
  29  
  30  require ("./pre.inc.php");
  31  
  32  $year_start=isset($_GET["year_start"])?$_GET["year_start"]:$_POST["year_start"];
  33  $year_current = strftime("%Y",time());
  34  $nbofyear=4;
  35  if (! $year_start) {
  36      $year_start = $year_current - ($nbofyear-1);
  37      $year_end = $year_current;
  38  }
  39  else {
  40      $year_end=$year_start + ($nbofyear-1);
  41  }
  42  
  43  /*
  44   * Sécurité accés client
  45   */
  46  if ($user->societe_id > 0) 
  47  {
  48    $socidp = $user->societe_id;
  49  }
  50  
  51  $modecompta = $conf->compta->mode;
  52  if ($_GET["modecompta"]) $modecompta=$_GET["modecompta"];
  53  
  54  
  55  llxHeader();
  56  
  57  $html=new Form($db);
  58  
  59  // Affiche en-tête du rapport
  60  if ($modecompta=="CREANCES-DETTES")
  61  {
  62      $nom="Bilan des recettes et dépenses, résumé annuel";
  63      $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ées)';
  64      $period="$year_start - $year_end";
  65      $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>":"");
  66      $description=$langs->trans("RulesResultDue");
  67      $builddate=time();
  68      $exportlink=$langs->trans("NotYetAvailable");
  69  }
  70  else {
  71      $nom="Bilan des recettes et dépenses, résumé annuel";
  72      $nom.=' (Voir le rapport en <a href="index.php?year_start='.$year_start.'&modecompta=CREANCES-DETTES">créances-dettes</a> pour inclure les factures non encore payée)';
  73      $period="$year_start - $year_end";
  74      $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>":"");
  75      $description=$langs->trans("RulesResultInOut");
  76      $builddate=time();
  77      $exportlink=$langs->trans("NotYetAvailable");
  78  }
  79  $html->report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink);
  80  
  81  
  82  /*
  83   * Factures clients
  84   */
  85  if ($modecompta == 'CREANCES-DETTES') { 
  86      $sql  = "SELECT sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
  87      $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
  88      $sql .= " WHERE f.fk_soc = s.idp AND f.fk_statut = 1";
  89  } else {
  90      /*
  91       * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  92       * vieilles versions, ils n'étaient pas liés via paiement_facture. On les ajoute plus loin)
  93       */
  94      $sql  = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  95      $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p";
  96      $sql .= " WHERE p.rowid = pf.fk_paiement AND pf.fk_facture = f.rowid";
  97  }
  98  if ($socidp) $sql .= " AND f.fk_soc = $socidp";
  99  $sql .= " GROUP BY dm";
 100  $sql .= " ORDER BY dm";
 101  
 102  $result=$db->query($sql);
 103  if ($result)
 104  {
 105      $num = $db->num_rows($result);
 106      $i = 0; 
 107      while ($i < $num)
 108      {
 109          $row = $db->fetch_object($result);
 110          $encaiss[$row->dm] = $row->amount_ht;
 111          $encaiss_ttc[$row->dm] = $row->amount_ttc;
 112          $i++;
 113      }
 114      $db->free($result);
 115  }
 116  else {
 117      dolibarr_print_error($db);    
 118  }
 119  
 120  // On ajoute les paiements clients anciennes version, non liés par paiement_facture
 121  if ($modecompta != 'CREANCES-DETTES') { 
 122      $sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
 123      $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
 124      $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
 125      $sql .= " WHERE pf.rowid IS NULL";
 126      $sql .= " GROUP BY dm";
 127      $sql .= " ORDER BY dm";
 128  
 129      $result = $db->query($sql);
 130      if ($result) {
 131          $num = $db->num_rows($result);
 132          $i = 0;
 133          while ($i < $num)
 134          {
 135              $row = $db->fetch_object($result);
 136  
 137              $encaiss[$row->dm] += $row->amount_ht;
 138              $encaiss_ttc[$row->dm] += $row->amount_ttc;
 139  
 140              $i++;
 141          }
 142      }
 143      else {
 144          dolibarr_print_error($db);
 145      }
 146  }
 147  
 148  
 149  /*
 150   * Frais, factures fournisseurs.
 151   */
 152  if ($modecompta == 'CREANCES-DETTES') { 
 153      $sql  = "SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
 154      $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_fourn as f";
 155      $sql .= " WHERE f.fk_soc = s.idp AND f.fk_statut = 1";
 156  } else {
 157      $sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
 158      $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
 159      $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
 160      $sql .= " ON f.rowid = p.fk_facture_fourn";
 161      $sql .= " WHERE 1=1";
 162  }
 163  if ($socidp)
 164  {
 165    $sql .= " AND f.fk_soc = $socidp";
 166  }
 167  $sql .= " GROUP BY dm";
 168  
 169  $result=$db->query($sql);
 170  if ($result)
 171  {
 172      $num = $db->num_rows($result);
 173      $i = 0; 
 174      while ($i < $num)
 175      {
 176          $row = $db->fetch_object($result);
 177  
 178          $decaiss[$row->dm] = $row->amount_ht;
 179          $decaiss_ttc[$row->dm] = $row->amount_ttc;
 180          $i++;
 181      }
 182      $db->free($result);
 183  }
 184  else {
 185      dolibarr_print_error($db);    
 186  }
 187  
 188  
 189  /*
 190   * TVA
 191   */
 192  
 193  if ($modecompta == 'CREANCES-DETTES') {
 194      // TVA à payer
 195      $sql = "SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm"; 
 196      $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
 197      $sql .= " WHERE f.fk_statut = 1";
 198      $sql .= " GROUP BY dm DESC";
 199      $result=$db->query($sql);
 200      if ($result) {
 201          $num = $db->num_rows($result);
 202          $var=false;
 203          $i = 0;
 204          if ($num) {    
 205            while ($i < $num) {
 206              $obj = $db->fetch_object($result);
 207          
 208              $decaiss[$obj->dm] += $obj->amount;
 209              $decaiss_ttc[$obj->dm] += $obj->amount;
 210          
 211              $i++;
 212            }
 213          }
 214      } else {
 215          dolibarr_print_error($db);
 216      }
 217      // TVA à récupérer
 218      $sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm"; 
 219      $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
 220      $sql .= " WHERE f.fk_statut = 1";
 221      $sql .= " GROUP BY dm";
 222      $result=$db->query($sql);
 223      if ($result) {
 224          $num = $db->num_rows($result);
 225          $var=false;
 226          $i = 0;
 227          if ($num) {    
 228            while ($i < $num) {
 229              $obj = $db->fetch_object($result);
 230          
 231              $encaiss[$obj->dm] += $obj->amount;
 232              $encaiss_ttc[$obj->dm] += $obj->amount;
 233          
 234              $i++;
 235            }
 236          }
 237      } else {
 238          dolibarr_print_error($db);
 239      }
 240  }
 241  else {
 242      // TVA payée
 243      $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm"; 
 244      $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
 245      $sql .= " WHERE amount > 0";
 246      $sql .= " GROUP BY dm";
 247      $result=$db->query($sql);
 248      if ($result) {
 249          $num = $db->num_rows($result);
 250          $var=false;
 251          $i = 0;
 252          if ($num) {    
 253            while ($i < $num) {
 254              $obj = $db->fetch_object($result);
 255          
 256              $decaiss[$obj->dm] += $obj->amount;
 257              $decaiss_ttc[$obj->dm] += $obj->amount;
 258          
 259              $i++;
 260            }
 261          }
 262      } else {
 263          dolibarr_print_error($db);
 264      }
 265      // TVA récupérée
 266      $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm"; 
 267      $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
 268      $sql .= " WHERE amount < 0";
 269      $sql .= " GROUP BY dm";
 270      $result=$db->query($sql);
 271      if ($result) {
 272          $num = $db->num_rows($result);
 273          $var=false;
 274          $i = 0;
 275          if ($num) {    
 276            while ($i < $num) {
 277              $obj = $db->fetch_object($result);
 278          
 279              $encaiss[$obj->dm] += $obj->amount;
 280              $encaiss_ttc[$obj->dm] += $obj->amount;
 281          
 282              $i++;
 283            }
 284          }
 285      } else {
 286          dolibarr_print_error($db);
 287      }
 288  }
 289  
 290  
 291  /*
 292   * Charges sociales non déductibles
 293   */
 294  
 295  if ($modecompta == 'CREANCES-DETTES') {
 296      $sql = "SELECT c.libelle as nom, date_format(s.date_ech,'%Y-%m') as dm, sum(s.amount) as amount_ht, sum(s.amount) as amount_ttc";
 297      $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c, ".MAIN_DB_PREFIX."chargesociales as s";
 298      $sql .= " WHERE s.fk_type = c.id AND c.deductible=0";
 299      if ($year) {
 300          $sql .= " AND s.date_ech between '$year-01-01 00:00:00' and '$year-12-31 23:59:59'";
 301      }
 302      $sql .= " GROUP BY c.libelle, dm";
 303  }
 304  else {
 305      $sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ht, sum(p.amount) as amount_ttc";
 306      $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c, ".MAIN_DB_PREFIX."chargesociales as s, ".MAIN_DB_PREFIX."paiementcharge as p";
 307      $sql .= " WHERE p.fk_charge = s.rowid AND s.fk_type = c.id AND c.deductible=0";
 308      if ($year) {
 309          $sql .= " AND p.datep between '$year-01-01 00:00:00' and '$year-12-31 23:59:59'";
 310      }
 311      $sql .= " GROUP BY c.libelle, dm";
 312  }
 313  $result=$db->query($sql);
 314  
 315  if ($result) {
 316      $num = $db->num_rows($result);
 317      $var=false;
 318      $i = 0;
 319      if ($num) {    
 320        while ($i < $num) {
 321          $obj = $db->fetch_object($result);
 322      
 323          $decaiss[$obj->dm] += $obj->amount_ht;
 324          $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
 325  
 326              if (ereg('^2004',$obj->dm)) {
 327                  $total_ht = $total_ht + $obj->amount_ht;
 328                  $total_ttc = $total_ttc + $obj->amount_ttc;
 329              }
 330  
 331     
 332          $i++;
 333        }
 334      }
 335  } else {
 336    dolibarr_print_error($db);
 337  }
 338  
 339  
 340  /*
 341   * Charges sociales déductibles
 342   */
 343  
 344  if ($modecompta == 'CREANCES-DETTES') {
 345      $sql = "SELECT c.libelle as nom, date_format(s.date_ech,'%Y-%m') as dm, sum(s.amount) as amount_ht, sum(s.amount) as amount_ttc";
 346      $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c, ".MAIN_DB_PREFIX."chargesociales as s";
 347      $sql .= " WHERE s.fk_type = c.id AND c.deductible=1";
 348      if ($year) {
 349          $sql .= " AND s.date_ech between '$year-01-01 00:00:00' and '$year-12-31 23:59:59'";
 350      }
 351      $sql .= " GROUP BY c.libelle, dm";
 352  }
 353  else {
 354      $sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ht, sum(p.amount) as amount_ttc";
 355      $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c, ".MAIN_DB_PREFIX."chargesociales as s, ".MAIN_DB_PREFIX."paiementcharge as p";
 356      $sql .= " WHERE p.fk_charge = s.rowid AND s.fk_type = c.id AND c.deductible=1";
 357      if ($year) {
 358          $sql .= " AND p.datep between '$year-01-01 00:00:00' and '$year-12-31 23:59:59'";
 359      }
 360      $sql .= " GROUP BY c.libelle, dm";
 361  }
 362  $result=$db->query($sql);
 363  if ($result) {
 364      $num = $db->num_rows($result);
 365      $var=false;
 366      $i = 0;
 367      if ($num) {
 368          while ($i < $num) {
 369          $obj = $db->fetch_object($result);
 370          
 371          $decaiss[$obj->dm] += $obj->amount_ht;
 372          $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
 373          
 374          $i++;
 375          }
 376      }
 377  } else {
 378    dolibarr_print_error($db);
 379  }
 380  
 381  
 382  
 383  /*
 384   * Affiche tableau
 385   */
 386  
 387  print '<table class="noborder" width="100%">';
 388  print '<tr class="liste_titre"><td rowspan=2>'.$langs->trans("Month").'</td>';
 389  
 390  for ($annee = $year_start ; $annee <= $year_end ; $annee++)
 391  {
 392    print '<td align="center" colspan="2"><a href="clientfourn.php?year='.$annee.'">'.$annee.'</a></td>';
 393  }
 394  print '</tr>';
 395  print '<tr class="liste_titre">';
 396  for ($annee = $year_start ; $annee <= $year_end ; $annee++)
 397  {
 398    print '<td align="right">'.$langs->trans("Outcome").'</td>';
 399    print '<td align="right">'.$langs->trans("Income").'</td>';
 400  }
 401  print '</tr>';
 402  
 403  $var=True;
 404  for ($mois = 1 ; $mois < 13 ; $mois++)
 405  {
 406    $var=!$var;
 407    print '<tr '.$bc[$var].'>';
 408    print "<td>".strftime("%B",mktime(1,1,1,$mois,1,$annee))."</td>";
 409    for ($annee = $year_start ; $annee <= $year_end ; $annee++)
 410      {
 411        print '<td align="right">&nbsp;';
 412        $case = strftime("%Y-%m",mktime(1,1,1,$mois,1,$annee));
 413        if ($decaiss_ttc[$case]>0)
 414      {
 415        print price($decaiss_ttc[$case]);
 416        $totsorties[$annee]+=$decaiss_ttc[$case];
 417      }
 418        print "</td>";
 419  
 420        print '<td align="right">&nbsp;';
 421        $case = strftime("%Y-%m",mktime(1,1,1,$mois,1,$annee));
 422        if ($encaiss_ttc[$case]>0)
 423      {
 424        print price($encaiss_ttc[$case]);
 425        $totentrees[$annee]+=$encaiss_ttc[$case];
 426      }
 427        print "</td>";
 428  
 429      }
 430  
 431    print '</tr>';
 432  }
 433  
 434  // Total
 435  $var=!$var;
 436  $nbcols=0;
 437  print '<tr class="liste_total"><td>'.$langs->trans("TotalTTC").'</td>';
 438  for ($annee = $year_start ; $annee <= $year_end ; $annee++)
 439  {
 440      $nbcols+=2;
 441      print '<td align="right">'.(isset($totsorties[$annee])?price($totsorties[$annee]):'&nbsp;').'</td>';
 442      print '<td align="right">'.(isset($totentrees[$annee])?price($totentrees[$annee]):'&nbsp;').'</td>';
 443  }
 444  print "</tr>\n";
 445  
 446  // Ligne vierge
 447  print '<tr><td>&nbsp;</td>';
 448  print '<td colspan="'.$nbcols.'">&nbsp;</td>';
 449  print "</tr>\n";
 450  
 451  // Balance
 452  $var=!$var;
 453  print '<tr class="liste_total"><td>'.$langs->trans("Profit").'</td>';
 454  for ($annee = $year_start ; $annee <= $year_end ; $annee++)
 455  {
 456      print '<td align="right" colspan="2"> ';
 457      if (isset($totentrees[$annee]) || isset($totsorties[$annee])) {
 458          print price($totentrees[$annee]-$totsorties[$annee]).'</td>';
 459  //  print '<td>&nbsp;</td>';
 460      }
 461  }
 462  print "</tr>\n";
 463  
 464  print "</table>";
 465  
 466  $db->close();
 467  
 468  llxFooter('$Date: 2005/07/02 17:16:15 $ - $Revision: 1.27 $');
 469  
 470  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics