[ 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/ -> facture.php (source)

   1  <?php
   2  /* Copyright (C) 2002-2005 Rodolphe Quiedeville  <rodolphe@quiedeville.org>
   3   * Copyright (C) 2004      Éric Seigne           <eric.seigne@ryxeo.com>
   4   * Copyright (C) 2004-2005 Laurent Destailleur   <eldy@users.sourceforge.net>
   5   * Copyright (C) 2005      Marc Barilley / Ocebo <marc@ocebo.com>
   6   *
   7   * This program is free software; you can redistribute it and/or modify
   8   * it under the terms of the GNU General Public License as published by
   9   * the Free Software Foundation; either version 2 of the License, or
  10   * (at your option) any later version.
  11   *
  12   * This program is distributed in the hope that it will be useful,
  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15   * GNU General Public License for more details.
  16   *
  17   * You should have received a copy of the GNU General Public License
  18   * along with this program; if not, write to the Free Software
  19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20   *
  21   * $Id: facture.php,v 1.252.2.4 2006/01/07 17:13:52 hregis Exp $
  22   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/compta/facture.php,v $
  23   */
  24  
  25  /**
  26          \file       htdocs/compta/facture.php
  27          \ingroup    facture
  28          \brief      Page de création d'une facture
  29          \version    $Revision: 1.252.2.4 $
  30  */
  31  
  32  require ('./pre.inc.php');
  33  require_once (DOL_DOCUMENT_ROOT ."/includes/modules/facture/modules_facture.php");
  34  require_once(DOL_DOCUMENT_ROOT.'/facture.class.php');
  35  require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php');
  36  require_once(DOL_DOCUMENT_ROOT.'/lib/invoice.lib.php');
  37  require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php');
  38  if ($conf->projet->enabled)   require_once(DOL_DOCUMENT_ROOT.'/project.class.php');
  39  if ($conf->propal->enabled)   require_once(DOL_DOCUMENT_ROOT.'/propal.class.php');
  40  if ($conf->contrat->enabled)  require_once(DOL_DOCUMENT_ROOT.'/contrat/contrat.class.php');
  41  if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php');
  42  
  43  $user->getrights('facture');
  44  $user->getrights('banque');
  45  
  46  if (!$user->rights->facture->lire)
  47      accessforbidden();
  48  
  49  $langs->load('bills');
  50  $langs->load('products');
  51  $langs->load('main');
  52  
  53  $sall=isset($_GET['sall'])?$_GET['sall']:$_POST['sall'];
  54  if (isset($_GET['msg'])) { $msg=urldecode($_GET['msg']); }
  55  if ($_GET['socidp']) { $socidp=$_GET['socidp']; }
  56  
  57  // Sécurité accés client
  58  if ($user->societe_id > 0)
  59  {
  60      $action = '';
  61      $socidp = $user->societe_id;
  62  }
  63  
  64  // Nombre de ligne pour choix de produit/service prédéfinis
  65  $NBLINES=4;
  66  
  67  
  68  /*
  69   *  Actions
  70   */
  71  
  72  if ($_POST['action'] == 'classin')
  73  {
  74      $facture = new Facture($db);
  75      $facture->fetch($_GET['facid']);
  76      $facture->classin($_POST['projetid']);
  77  }
  78  
  79  if ($_POST['action'] == 'setmode')
  80  {
  81      $facture = new Facture($db);
  82      $facture->fetch($_GET['facid']);
  83      $result=$facture->mode_reglement($_POST['mode_reglement_id']);
  84      if ($result < 0) dolibarr_print_error($db,$facture->error);
  85  }
  86  
  87  if ($_POST['action'] == 'setconditions')
  88  {
  89      $facture = new Facture($db);
  90      $facture->fetch($_GET['facid']);
  91      $result=$facture->cond_reglement($_POST['cond_reglement_id']);
  92      if ($result < 0) dolibarr_print_error($db,$facture->error);
  93  }
  94  
  95  if ($_POST['action'] == 'classin')
  96  {
  97      $facture = new Facture($db);
  98      $facture->fetch($_GET['facid']);
  99      $facture->classin($_POST['projetid']);
 100  }
 101  
 102  if ($_POST['action'] == 'set_ref_client')
 103  {
 104      $facture = new Facture($db);
 105      $facture->fetch($_GET['facid']);
 106      $facture->set_ref_client($_POST['ref_client']);
 107  }
 108  
 109  /*
 110   * Insertion facture
 111   */
 112  if ($_POST['action'] == 'add')
 113  {
 114      $datefacture = mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
 115  
 116      $facture = new Facture($db, $_POST['socid']);
 117  
 118      $facture->number         = $_POST['facnumber'];
 119      $facture->date           = $datefacture;
 120      $facture->note           = $_POST['note'];
 121  
 122      if ($_POST['fac_rec'] > 0)
 123      {
 124          // Facture récurrente
 125          $facture->fac_rec = $_POST['fac_rec'];
 126          $facid = $facture->create($user);
 127      }
 128      else
 129      {
 130          $facture->projetid          = $_POST['projetid'];
 131          $facture->cond_reglement_id = $_POST['cond_reglement_id'];
 132          $facture->mode_reglement_id = $_POST['mode_reglement_id'];
 133          $facture->amount            = $_POST['amount'];
 134          $facture->remise            = $_POST['remise'];
 135          $facture->remise_percent    = $_POST['remise_percent'];
 136          $facture->ref_client        = $_POST['ref_client'];
 137  
 138          if (! $_POST['propalid'] && ! $_POST['commandeid'] && ! $_POST['contratid'])
 139          {
 140              for ($i = 1 ; $i <= $NBLINES ; $i++)
 141              {
 142                  if ($_POST['idprod'.$i])
 143                  {
 144                      $startday='';
 145                      $endday='';
 146                      if ($_POST['date_start'.$i.'year'] && $_POST['date_start'.$i.'month'] && $_POST['date_start'.$i.'day']) {
 147                          $startday=$_POST['date_start'.$i.'year'].'-'.$_POST['date_start'.$i.'month'].'-'.$_POST['date_start'.$i.'day'];
 148                      }
 149                      if ($_POST['date_end'.$i.'year'] && $_POST['date_end'.$i.'month'] && $_POST['date_end'.$i.'day']) {
 150                          $endday=$_POST['date_end'.$i.'year'].'-'.$_POST['date_end'.$i.'month'].'-'.$_POST['date_end'.$i.'day'];
 151                      }
 152                      $facture->add_product($_POST['idprod'.$i],$_POST['qty'.$i],$_POST['remise_percent'.$i],$startday,$endday);
 153                  }
 154              }
 155              $facid = $facture->create($user);
 156  
 157              if ($facid)
 158              {
 159                  Header('Location: facture.php?facid='.$facid);
 160                  exit;
 161              }
 162          }
 163          else
 164          {
 165              /*
 166               * Si creation depuis propale
 167               */
 168              if ($_POST['propalid'])
 169              {
 170                  $facture->propalid = $_POST['propalid'];
 171  
 172                  $facid = $facture->create($user);
 173  
 174                  if ($facid)
 175                  {
 176                      $prop = New Propal($db);
 177                      if ( $prop->fetch($_POST['propalid']) )
 178                      {
 179                          for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++)
 180                          {
 181                              $liblignefac=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle);
 182  
 183                              $result = $facture->addline($facid,
 184                              addslashes($liblignefac),
 185                              $prop->lignes[$i]->subprice,
 186                              $prop->lignes[$i]->qty,
 187                              $prop->lignes[$i]->tva_tx,
 188                              $prop->lignes[$i]->product_id,
 189                              $prop->lignes[$i]->remise_percent);
 190                          }
 191                      }
 192                      else
 193                      {
 194                          print $langs->trans('UnknownError');
 195                      }
 196                  }
 197              }
 198  
 199              /*
 200               * Si création depuis commande
 201               */
 202              if ($_POST['commandeid'])
 203              {
 204                  $facture->commandeid = $_POST['commandeid'];
 205                  $facid = $facture->create($user);
 206                  if ($facid)
 207                  {
 208                      $comm = New Commande($db);
 209                      if ( $comm->fetch($_POST['commandeid']) )
 210                      {
 211                          $lines = $comm->fetch_lignes();
 212                          for ($i = 0 ; $i < sizeof($lines) ; $i++)
 213                          {
 214                              $result = $facture->addline($facid,
 215                              addslashes($lines[$i]->description),
 216                              $lines[$i]->subprice,
 217                              $lines[$i]->qty,
 218                              $lines[$i]->tva_tx,
 219                              $lines[$i]->product_id,
 220                              $lines[$i]->remise_percent);
 221                          }
 222                      }
 223                      else
 224                      {
 225                          print $langs->trans('UnknownError');
 226                      }
 227                  }
 228                  else
 229                  {
 230                      dolibarr_print_error($db);
 231                  }
 232              }
 233  
 234              /*
 235               * Si création depuis contrat
 236               */
 237              if ($_POST['contratid'])
 238              {
 239                  $facture->contratid = $_POST['contratid'];
 240                  $facid = $facture->create($user);
 241  
 242                  if ($facid)
 243                  {
 244                      $contrat = New Contrat($db);
 245                      if ($contrat->fetch($_POST['contratid']) > 0)
 246                      {
 247                          $lines = $contrat->fetch_lignes();
 248  
 249                          for ($i = 0 ; $i < sizeof($lines) ; $i++)
 250                          {
 251                              $liblignefac=($contrat->lignes[$i]->desc?$contrat->lignes[$i]->desc:$contrat->lignes[$i]->libelle);
 252  
 253                              // Plage de dates
 254                              $date_start=$contrat->lignes[$i]->date_debut_prevue;
 255                              if ($contrat->lignes[$i]->date_debut_reel) $date_start=$contrat->lignes[$i]->date_debut_reel;
 256                              $date_end=$contrat->lignes[$i]->date_fin_prevue;
 257                              if ($contrat->lignes[$i]->date_fin_reel) $date_end=$contrat->lignes[$i]->date_fin_reel;
 258  
 259                              $result = $facture->addline($facid,
 260                              addslashes($liblignefac),
 261                              $lines[$i]->subprice,
 262                              $lines[$i]->qty,
 263                              $lines[$i]->tva_tx,
 264                              $lines[$i]->product_id,
 265                              $lines[$i]->remise_percent,
 266                              $date_start,
 267                              $date_end);
 268                          }
 269                      }
 270                      else
 271                      {
 272                          print $langs->trans('UnknownError');
 273                      }
 274                  }
 275                  else
 276                  {
 277                      dolibarr_print_error($db);
 278                  }
 279              }
 280  
 281              // Fin création facture, on l'affiche
 282              if ($facid)
 283              {
 284                  Header('Location: facture.php?facid='.$facid);
 285                  exit;
 286              }
 287          }
 288      }
 289  }
 290  
 291  // Classe à "validée"
 292  if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->rights->facture->valider)
 293  {
 294      $fac = new Facture($db);
 295      $fac->fetch($_GET['facid']);
 296      $soc = new Societe($db);
 297      $soc->fetch($fac->socidp);
 298      $result = $fac->set_valid($fac->id, $user, $soc);
 299      if ($result)
 300      {
 301          facture_pdf_create($db, $fac->id);
 302      }
 303  }
 304  
 305  // Classe à "payée"
 306  if ($_POST['action'] == 'confirm_payed' && $_POST['confirm'] == 'yes' && $user->rights->facture->paiement)
 307  {
 308      $fac = new Facture($db);
 309      $fac->fetch($_GET['facid']);
 310      $result = $fac->set_payed($user);
 311  }
 312  
 313  if ($_POST['action'] == 'setremise' && $user->rights->facture->creer)
 314  {
 315      $fac = new Facture($db);
 316      $fac->fetch($_GET['facid']);
 317      $result = $fac->set_remise($user, $_POST['remise']);
 318  }
 319  
 320  if ($_POST['action'] == 'addligne' && $user->rights->facture->creer)
 321  {
 322      if ($_POST['qty'] && (($_POST['pu']>=0 && $_POST['desc']) || $_POST['idprod']))
 323      {
 324          $fac = new Facture($db);
 325          $fac->fetch($_POST['facid']);
 326          $datestart='';
 327          $dateend='';
 328          if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday'])
 329          {
 330              $datestart=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday'];
 331          }
 332          if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday'])
 333          {
 334              $dateend=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday'];
 335          }
 336          $result = $fac->addline($_POST['facid'],
 337              $_POST['desc'],
 338              $_POST['pu'],
 339              $_POST['qty'],
 340              $_POST['tva_tx'],
 341              $_POST['idprod'],
 342              $_POST['remise_percent'],
 343              $datestart,
 344              $dateend
 345              );
 346      }
 347  
 348      $_GET['facid']=$_POST['facid'];   // Pour réaffichage de la fiche en cours d'édition
 349  }
 350  
 351  if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save'))
 352  {
 353      $fac = new Facture($db,'',$_POST['facid']);
 354      $fac->fetch($_POST['facid']);
 355  
 356      $datestart='';
 357      $dateend='';
 358      if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) {
 359          $datestart=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday'];
 360      }
 361      if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) {
 362          $dateend=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday'];
 363      }
 364  
 365      $result = $fac->updateline($_POST['rowid'],
 366          $_POST['desc'],
 367          $_POST['price'],
 368          $_POST['qty'],
 369          $_POST['remise_percent'],
 370          $datestart,
 371          $dateend,
 372          $_POST['tva_tx']
 373          );
 374  
 375      $_GET['facid']=$_POST['facid'];   // Pour réaffichage de la fiche en cours d'édition
 376  }
 377  
 378  if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['cancel'] == $langs->trans('Cancel'))
 379  {
 380      Header('Location: facture.php?facid='.$_POST['facid']);   // Pour réaffichage de la fiche en cours d'édition
 381      exit;
 382  }
 383  
 384  if ($_GET['action'] == 'deleteline' && $user->rights->facture->creer)
 385  {
 386      $fac = new Facture($db,'',$_GET['facid']);
 387      $fac->fetch($_GET['facid']);
 388      $result = $fac->deleteline($_GET['rowid']);
 389  }
 390  
 391  if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes')
 392  {
 393      if ($user->rights->facture->supprimer )
 394      {
 395          $fac = new Facture($db);
 396          $result = $fac->delete($_GET['facid']);
 397          $_GET['facid'] = 0 ;
 398          Header('Location: facture.php');
 399          exit;
 400      }
 401  }
 402  
 403  if ($_POST['action'] == 'confirm_canceled' && $_POST['confirm'] == 'yes')
 404  {
 405      if ($user->rights->facture->supprimer )
 406      {
 407          $fac = new Facture($db);
 408          $fac->fetch($_GET['facid']);
 409          $result = $fac->set_canceled($user);
 410      }
 411  }
 412  
 413  /*
 414   * Ordonnancement des lignes
 415   */
 416  
 417  if ($_GET['action'] == 'up' && $user->rights->facture->creer)
 418  {
 419      $fac = new Facture($db,'',$_GET['facid']);
 420      $fac->line_up($_GET['rowid']);
 421  }
 422  
 423  if ($_GET['action'] == 'down' && $user->rights->facture->creer)
 424  {
 425      $fac = new Facture($db,'',$_GET['facid']);
 426      $fac->line_down($_GET['rowid']);
 427  }
 428  
 429  /*
 430   * Action envoi de mail
 431   */
 432  if ($_POST['action'] == 'send' || $_POST['action'] == 'relance')
 433  {
 434      $langs->load('mails');
 435  
 436      $fac = new Facture($db,'',$_POST['facid']);
 437      if ( $fac->fetch($_POST['facid']) )
 438      {
 439          $facref = sanitize_string($fac->ref);
 440          $file = $conf->facture->dir_output . '/' . $facref . '/' . $facref . '.pdf';
 441  
 442          if (is_readable($file))
 443          {
 444              $soc = new Societe($db, $fac->socidp);
 445  
 446              if ($_POST['sendto']) {
 447                  // Le destinataire a été fourni via le champ libre
 448                  $sendto = $_POST['sendto'];
 449                  $sendtoid = 0;
 450              }
 451              elseif ($_POST['receiver']) {
 452                  // Le destinataire a été fourni via la liste déroulante
 453                  $sendto = $soc->contact_get_email($_POST['receiver']);
 454                  $sendtoid = $_POST['receiver'];
 455              }
 456  
 457              if (strlen($sendto))
 458              {
 459                  $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>';
 460                  $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>';
 461                  $message = $_POST['message'];
 462                  if ($_POST['action'] == 'send')
 463                  {
 464                      $subject = $langs->trans('Bill').' '.$fac->ref;
 465                      $actiontypeid=9;
 466                      $actionmsg ='Mail envoyé par '.$from.' à '.$sendto.'.<br>';
 467                      if ($message) {
 468                          $actionmsg.='Texte utilisé dans le corps du message:<br>';
 469                          $actionmsg.=$message;
 470                      }
 471                      $actionmsg2='Envoi facture par mail';
 472                  }
 473                  if ($_POST['action'] == 'relance')
 474                  {
 475                      $subject = 'Relance facture '.$fac->ref;
 476                      $actiontypeid=10;
 477                      $actionmsg='Mail envoyé par '.$from.' à '.$sendto.'.<br>';
 478                      if ($message) {
 479                          $actionmsg.='Texte utilisé dans le corps du message:<br>';
 480                          $actionmsg.=$message;
 481                      }
 482                      $actionmsg2='Relance facture par mail';
 483                  }
 484  
 485                  $filepath[0] = $file;
 486                  $filename[0] = $fac->ref.'.pdf';
 487                  $mimetype[0] = 'application/pdf';
 488                  $filepath[1] = $_FILES['addedfile']['tmp_name'];
 489                  $filename[1] = $_FILES['addedfile']['name'];
 490                  $mimetype[1] = $_FILES['addedfile']['type'];
 491  
 492                  // Envoi de la facture
 493                  $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc);
 494  
 495                  if ($mailfile->sendfile())
 496                  {
 497                      $msg='<div class="ok">'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.</div>';
 498  
 499                      // Insertion action
 500                      require_once(DOL_DOCUMENT_ROOT.'/contact.class.php');
 501                      require_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php');
 502                      $actioncomm = new ActionComm($db);
 503                      $actioncomm->type_id     = $actiontypeid;
 504                      $actioncomm->label       = $actionmsg2;
 505                      $actioncomm->note        = $actionmsg;
 506                      $actioncomm->date        = time();
 507                      $actioncomm->percent     = 100;
 508                      $actioncomm->contact     = new Contact($db,$sendtoid);
 509                      $actioncomm->societe     = new Societe($db,$fac->socidp);
 510                      $actioncomm->user        = $user;   // User qui a fait l'action
 511                      $actioncomm->facid       = $fac->id;
 512  
 513                      $ret=$actioncomm->add($user);       // User qui saisit l'action
 514  
 515                      if ($ret < 0)
 516                      {
 517                          dolibarr_print_error($db);
 518                      }
 519                      else
 520                      {
 521                          // Renvoie sur la fiche
 522                          Header('Location: facture.php?facid='.$fac->id.'&msg='.urlencode($msg));
 523                          exit;
 524                      }
 525                  }
 526                  else
 527                  {
 528                      $msg='<div class="error">'.$langs->trans('ErrorFailedToSendMail',$from,$sendto).' !</div>';
 529                  }
 530              }
 531              else
 532              {
 533                  $msg='<div class="error">'.$langs->trans('ErrorMailRecipientIsEmpty').'</div>';
 534                  dolibarr_syslog('Le mail du destinataire est vide');
 535              }
 536  
 537          }
 538          else
 539          {
 540              dolibarr_syslog('Impossible de lire :'.$file);
 541          }
 542      }
 543      else
 544      {
 545          dolibarr_syslog('Impossible de lire les données de la facture. Le fichier facture n\'a peut-être pas été généré.');
 546      }
 547  }
 548  
 549  /*
 550   * Générer ou regénérer le PDF
 551   */
 552  if ($_GET['action'] == 'pdf')
 553  {
 554      facture_pdf_create($db, $_GET['facid']);
 555  }
 556  
 557  /*********************************************************************
 558  *
 559  * Fonctions internes
 560  *
 561  **********************************************************************/
 562  function print_date_range($date_start,$date_end)
 563  {
 564      global $langs;
 565  
 566      if ($date_start && $date_end)
 567      {
 568          print ' ('.$langs->trans('DateFromTo',dolibarr_print_date($date_start),dolibarr_print_date($date_end)).')';
 569      }
 570      if ($date_start && ! $date_end)
 571      {
 572          print ' ('.$langs->trans('DateFrom',dolibarr_print_date($date_start)).')';
 573      }
 574      if (! $date_start && $date_end)
 575      {
 576          print ' ('.$langs->trans('DateUntil',dolibarr_print_date($date_end)).')';
 577      }
 578  }
 579  
 580  
 581  
 582  llxHeader('',$langs->trans('Bill'),'Facture');
 583  
 584  $html = new Form($db);
 585  
 586  
 587  /*********************************************************************
 588  *
 589  * Mode creation
 590  *
 591  **********************************************************************/
 592  if ($_GET['action'] == 'create')
 593  {
 594      print_titre($langs->trans('NewBill'));
 595  
 596      $soc = new Societe($db);
 597  
 598      if ($_GET['propalid'])
 599      {
 600          $propal = New Propal($db);
 601          $propal->fetch($_GET['propalid']);
 602          $societe_id = $propal->soc_id;
 603          $projet=$propal->projetidp;
 604          $soc->fetch($societe_id);
 605      }
 606      elseif ($_GET['commandeid'])
 607      {
 608          $commande = New Commande($db);
 609          $commande->fetch($_GET['commandeid']);
 610          $societe_id = $commande->soc_id;
 611          $projet=$commande-> projet_id;
 612          $ref_client=$commande->ref_client;
 613          $soc->fetch($societe_id);
 614      }
 615      elseif ($_GET['contratid'])
 616      {
 617          $contrat = New Contrat($db);
 618          $contrat->fetch($_GET['contratid']);
 619          $societe_id = $contrat->societe->id;
 620          $projet=$contrat->fk_projet;
 621          $soc=$contrat->societe;
 622      }
 623      else
 624      {
 625          $societe_id=$socidp;
 626          $soc->fetch($societe_id);
 627      }
 628  
 629  
 630      print '<form action="facture.php" method="post">';
 631      print '<input type="hidden" name="action" value="add">';
 632      print '<input type="hidden" name="socid" value="'.$soc->id.'">' ."\n";
 633  
 634      print '<table class="border" width="100%">';
 635  
 636      // Reference
 637      print '<tr><td>'.$langs->trans('Ref').'</td><td colspan="2">'.$langs->trans('Draft').'</td></tr>';
 638      print '<input name="facnumber" type="hidden" value="provisoire">';
 639  
 640      print '<tr><td>'.$langs->trans('Company').'</td><td colspan="2">'.$soc->nom.'</td>';
 641      print '</tr>';
 642  
 643      print '<tr><td>'.$langs->trans('Author').'</td><td>'.$user->fullname.'</td>';
 644      print '<td class="border">'.$langs->trans('Comments').'</td>';
 645      print '</tr>';
 646  
 647      print '<tr><td>'.$langs->trans('Date').'</td><td>';
 648      $html->select_date();
 649      print '</td>';
 650  
 651      // Notes
 652      $nbrows=4;
 653      if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) $nbrows++;
 654      print '<td rowspan="'.$nbrows.'" valign="top">';
 655      print '<textarea name="note" wrap="soft" cols="70" rows="'.ROWS_5.'">';
 656      if (is_object($propal))
 657      {
 658          print $propal->note;
 659      }
 660      if (is_object($commande))
 661      {
 662          print $commande->note;
 663      }
 664      if (is_object($contrat))
 665      {
 666          print $contrat->note;
 667      }
 668      print '</textarea></td></tr>';
 669  
 670      // Conditions de réglement
 671      $cond_reglement_id_defaut=1;
 672      print '<tr><td nowrap>'.$langs->trans('PaymentConditions').'</td><td>';
 673      $html->select_conditions_paiements($cond_reglement_id_defaut,'cond_reglement_id');
 674      print '</td></tr>';
 675  
 676      // Mode de réglement
 677      print '<tr><td>'.$langs->trans('PaymentMode').'</td><td>';
 678      $html->select_types_paiements('','mode_reglement_id');
 679      print '</td></tr>';
 680  
 681      // Projet
 682      if ($conf->projet->enabled)
 683      {
 684          $langs->load('projects');
 685          print '<tr><td>'.$langs->trans('Project').'</td><td>';
 686          $html->select_projects($societe_id, $projet, 'projetid');
 687          print '</td></tr>';
 688      }
 689      else
 690      {
 691          print '<tr><td colspan="2">&nbsp;</td></tr>';
 692      }
 693  
 694      /*
 695        \todo
 696        L'info "Reference commande client" est une carac de la commande et non de la facture.
 697        Elle devrait donc etre stockée sur l'objet commande lié à la facture et non sur la facture.
 698        Pour ceux qui utilisent ainsi, positionner la constante FAC_USE_CUSTOMER_ORDER_REF à 1.
 699      */
 700      if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF)
 701      {
 702          print '<tr><td>'.$langs->trans('RefCdeClient').'</td><td>';
 703          print '<input type="text" name="ref_client" value="'.$ref_client.'">';
 704          print '</td></tr>';
 705      }
 706      
 707      if ($_GET['propalid'] > 0)
 708      {
 709          print '<input type="hidden" name="amount"         value="'.$propal->price.'">'."\n";
 710          print '<input type="hidden" name="total"          value="'.$propal->total.'">'."\n";
 711          print '<input type="hidden" name="remise"         value="'.$propal->remise.'">'."\n";
 712          print '<input type="hidden" name="remise_percent" value="'.$propal->remise_percent.'">'."\n";
 713          print '<input type="hidden" name="tva"            value="'.$propal->tva.'">'."\n";
 714          print '<input type="hidden" name="propalid"       value="'.$_GET['propalid'].'">';
 715  
 716          print '<tr><td>'.$langs->trans('Proposal').'</td><td colspan="2">'.$propal->ref.'</td></tr>';
 717          print '<tr><td>'.$langs->trans('GlobalDiscount').'</td><td colspan="2">'.$propal->remise_percent.'%</td></tr>';
 718          print '<tr><td>'.$langs->trans('TotalHT').'</td><td colspan="2">'.price($propal->price).'</td></tr>';
 719          print '<tr><td>'.$langs->trans('TotalVAT').'</td><td colspan="2">'.price($propal->tva)."</td></tr>";
 720          print '<tr><td>'.$langs->trans('TotalTTC').'</td><td colspan="2">'.price($propal->total)."</td></tr>";
 721      }
 722      elseif ($_GET['commandeid'] > 0)
 723      {
 724          $commande->remise_percent=$soc->remise_client;
 725          print '<input type="hidden" name="commandeid" value="'.$commande->id.'">';
 726          print '<input type="hidden" name="remise_percent" value="'.$commande->remise_percent.'">'."\n";
 727  
 728          print '<tr><td>'.$langs->trans('Order').'</td><td colspan="2">'.$commande->ref.'</td></tr>';
 729          print '<tr><td>'.$langs->trans('TotalHT').'</td><td colspan="2">'.price($commande->total_ht).'</td></tr>';
 730          print '<tr><td>'.$langs->trans('TotalVAT').'</td><td colspan="2">'.price($commande->total_tva)."</td></tr>";
 731          print '<tr><td>'.$langs->trans('TotalTTC').'</td><td colspan="2">'.price($commande->total_ttc)."</td></tr>";
 732      }
 733      elseif ($_GET['contratid'] > 0)
 734      {
 735          // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva
 736          $contrat->remise=0;
 737          $contrat->remise_percent=$soc->remise_client;
 738          $contrat->update_price();
 739  
 740          print '<input type="hidden" name="amount"    value="'.$contrat->total_ht.'">'."\n";
 741          print '<input type="hidden" name="total"     value="'.$contrat->total_ttc.'">'."\n";
 742          print '<input type="hidden" name="remise"    value="'.$contrat->remise.'">'."\n";
 743          print '<input type="hidden" name="remise_percent"   value="'.$contrat->remise_percent.'">'."\n";
 744          print '<input type="hidden" name="tva"       value="'.$contrat->total_tva.'">'."\n";
 745          print '<input type="hidden" name="contratid" value="'.$_GET["contratid"].'">';
 746  
 747          print '<tr><td>'.$langs->trans('Contract').'</td><td colspan="2">'.$contrat->ref.'</td></tr>';
 748          print '<tr><td>'.$langs->trans('TotalHT').'</td><td colspan="2">'.price($contrat->total_ht).'</td></tr>';
 749          print '<tr><td>'.$langs->trans('TotalVAT').'</td><td colspan="2">'.price($contrat->total_tva)."</td></tr>";
 750          print '<tr><td>'.$langs->trans('TotalTTC').'</td><td colspan="2">'.price($contrat->total_ttc)."</td></tr>";
 751      }
 752      else
 753      {
 754          print '<tr><td colspan="3">&nbsp;</td></tr>';
 755          print '<tr><td colspan="3">';
 756  
 757          print '<table class="noborder">';
 758          print '<tr><td>'.$langs->trans('ProductsAndServices').'</td><td>'.$langs->trans('Qty').'</td><td>'.$langs->trans('Discount').'</td><td> &nbsp; &nbsp; </td>';
 759          if ($conf->service->enabled)
 760          {
 761              print '<td>'.$langs->trans('ServiceLimitedDuration').'</td></tr>';
 762          }
 763          for ($i = 1 ; $i <= $NBLINES ; $i++)
 764          {
 765              print '<tr><td>';
 766              $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size);
 767              print '</td>';
 768              print '<td><input type="text" size="3" name="qty'.$i.'" value="1"></td>';
 769              print '<td nowrap="nowrap"><input type="text" size="1" name="remise_percent'.$i.'" value="0">%</td>';
 770              print '<td>&nbsp;</td>';
 771              // Si le module service est actif, on propose des dates de début et fin à la ligne
 772              if ($conf->service->enabled)
 773              {
 774                  print '<td nowrap="nowrap">';
 775                  print $langs->trans('From').' ';
 776                  print $html->select_date('','date_start'.$i,0,0,1);
 777                  print '<br>'.$langs->trans('to').' ';
 778                  print $html->select_date('','date_end'.$i,0,0,1);
 779                  print '</td>';
 780              }
 781              print "</tr>\n";
 782          }
 783  
 784          print '</table>';
 785          print '</td></tr>';
 786      }
 787  
 788      /*
 789       * Factures récurrentes
 790       */
 791      if ($_GET['propalid'] == 0 && $_GET['commandeid'] == 0 && $_GET['contratid'] == 0)
 792      {
 793          $sql = 'SELECT r.rowid, r.titre, r.amount FROM '.MAIN_DB_PREFIX.'facture_rec as r';
 794          $sql .= ' WHERE r.fk_soc = '.$soc->id;
 795          if ( $db->query($sql) )
 796          {
 797              $num = $db->num_rows();
 798              $i = 0;
 799  
 800              if ($num > 0)
 801              {
 802                  print '<tr><td colspan="3">'.$langs->trans('RecurringBills').' : <select class="flat" name="fac_rec">';
 803                  print '<option value="0" selected="true"></option>';
 804                  while ($i < $num)
 805                  {
 806                      $objp = $db->fetch_object();
 807                      print '<option value="'.$objp->rowid.'">'.$objp->titre.' : '.$objp->amount.'</option>';
 808                      $i++;
 809                  }
 810                  print '</select></td></tr>';
 811              }
 812              $db->free();
 813          }
 814          else
 815          {
 816              dolibarr_print_error($db);
 817          }
 818      }
 819  
 820      // Bouton "Create Draft"
 821      print '<tr><td colspan="3" align="center"><input type="submit" class="button" name="bouton" value="'.$langs->trans('CreateDraft').'"></td></tr>';
 822      print "</form>\n";
 823      print "</table>\n";
 824  
 825      // Si creation depuis un propal
 826      if ($_GET['propalid'])
 827      {
 828          print '<br>';
 829          print_titre($langs->trans('ProductsAndServices'));
 830  
 831          print '<table class="noborder" width="100%">';
 832          print '<tr class="liste_titre">';
 833          print '<td>'.$langs->trans('Ref').'</td>';
 834          print '<td>'.$langs->trans('Description').'</td>';
 835          print '<td align="right">'.$langs->trans('VAT').'</td>';
 836          print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
 837          print '<td align="right">'.$langs->trans('Qty').'</td>';
 838          print '<td align="right">'.$langs->trans('Discount').'</td></tr>';
 839  
 840          // Lignes de propal produits prédéfinis
 841          $sql = 'SELECT pt.rowid, p.label as product, p.ref, pt.tva_tx, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent, pt.description';
 842          $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt, '.MAIN_DB_PREFIX.'product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = '.$_GET['propalid'];
 843          $sql .= ' ORDER BY pt.rowid ASC';
 844          $result = $db->query($sql);
 845          if ($result)
 846          {
 847              $num = $db->num_rows($result);
 848              $i = 0;
 849              $var=True;
 850              while ($i < $num)
 851              {
 852                  $objp = $db->fetch_object($result);
 853                  $var=!$var;
 854                  print '<tr '.$bc[$var].'><td><a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$objp->prodid.'">'.img_object($langs->trans(''),'product').' '.$objp->ref.'</a>';
 855                  print $objp->product?' - '.$objp->product:'';
 856                  print "</td>\n";
 857                  print '<td>';
 858                  print dolibarr_trunc($objp->description,60);
 859                  print '</td>';
 860                  print '<td align="right">'.$objp->tva_tx.'%</td>';
 861                  print '<td align="right">'.price($objp->price).'</td>';
 862                  print '<td align="right">'.$objp->qty.'</td>';
 863                  print '<td align="right">'.$objp->remise_percent.'%</td>';
 864                  print '</tr>';
 865                  $i++;
 866              }
 867          }
 868          else
 869          {
 870              dolibarr_print_error($db);
 871          }
 872          // Lignes de propal non produits prédéfinis
 873          $sql = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.price, pt.qty, pt.remise_percent';
 874          $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt ';
 875          $sql .= ' WHERE  pt.fk_propal = '.$_GET['propalid'];
 876          $sql .= ' AND (pt.fk_product = 0 or pt.fk_product is null)';
 877          $sql .= ' ORDER BY pt.rowid ASC';
 878          $result=$db->query($sql);
 879          if ($result)
 880          {
 881              $num = $db->num_rows($result);
 882              $i = 0;
 883              while ($i < $num)
 884              {
 885                  $objp = $db->fetch_object($result);
 886                  $var=!$var;
 887                  print '<tr '.$bc[$var].'><td>&nbsp;</td>';
 888                  print '<td>'.dolibarr_trunc($objp->product,60).'</td>';
 889                  print '<td align="right">'.$objp->tva_tx.'%</td>';
 890                  print '<td align="right">'.price($objp->price).'</td>';
 891                  print '<td align="right">'.$objp->qty.'</td>';
 892                  print '<td align="right">'.$objp->remise_percent.'%</td>';
 893                  print '</tr>';
 894                  $i++;
 895              }
 896          }
 897          else
 898          {
 899              dolibarr_print_error($db);
 900          }
 901  
 902          print '</table>';
 903      }
 904  
 905      // Si creation depuis une commande
 906      if ($_GET['commandeid'])
 907      {
 908          print '<br>';
 909          print_titre($langs->trans('Products'));
 910  
 911          print '<table class="noborder" width="100%">';
 912          print '<tr class="liste_titre">';
 913          print '<td>'.$langs->trans('Ref').'</td>';
 914          print '<td>'.$langs->trans('Description').'</td>';
 915          print '<td align="right">'.$langs->trans('VAT').'</td>';
 916          print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
 917          print '<td align="right">'.$langs->trans('Qty').'</td>';
 918          print '<td align="right">'.$langs->trans('Discount').'</td></tr>';
 919  
 920          $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,';
 921          $sql.= ' p.label as product, p.ref, p.rowid as prodid';
 922          $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt, '.MAIN_DB_PREFIX.'product as p';
 923          $sql.= ' WHERE pt.fk_product = p.rowid AND pt.fk_commande = '.$commande->id;
 924          $sql.= ' ORDER BY pt.rowid ASC';
 925  
 926          $result = $db->query($sql);
 927          if ($result)
 928          {
 929              $num = $db->num_rows($result);
 930              $i = 0;
 931              $var=True;
 932              while ($i < $num)
 933              {
 934                  $objp = $db->fetch_object($result);
 935                  $var=!$var;
 936                  print '<tr '.$bc[$var].'><td><a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$objp->prodid.'">'.img_object($langs->trans(''),'product').' '.$objp->ref.'</a>';
 937                  print $objp->product?' - '.$objp->product:'';
 938                  print "</td>\n";
 939                  print '<td>';
 940                  print dolibarr_trunc($objp->description,60);
 941                  print '</td>';
 942                  print '<td align="right">'.$objp->tva_tx.'%</td>';
 943                  print '<td align="right">'.price($objp->subprice).'</td>';
 944                  print '<td align="right">'.$objp->qty.'</td>';
 945                  print '<td align="right">'.$objp->remise_percent.'%</td>';
 946                  print '</tr>';
 947                  $i++;
 948              }
 949          }
 950          else
 951          {
 952              dolibarr_print_error($db);
 953          }
 954          // Lignes de commande non produits prédéfinis
 955          $sql  = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent';
 956          $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt';
 957          $sql .= ' WHERE  pt.fk_commande = '.$commande->id;
 958          $sql .= ' AND (pt.fk_product = 0 or pt.fk_product is null)';
 959          $sql .= ' ORDER BY pt.rowid ASC';
 960  
 961          $result=$db->query($sql);
 962          if ($result)
 963          {
 964              $num = $db->num_rows($result);
 965              $i = 0;
 966              while ($i < $num)
 967              {
 968                  $objp = $db->fetch_object($result);
 969                  $var=!$var;
 970                  print '<tr '.$bc[$var].'><td>&nbsp;</td>';
 971                  print '<td>'.dolibarr_trunc($objp->product,60).'</td>';
 972                  print '<td align="right">'.$objp->tva_tx.'%</td>';
 973                  print '<td align="right">'.price($objp->subprice).'</td>';
 974                  print '<td align="right">'.$objp->qty.'</td>';
 975                  print '<td align="right">'.$objp->remise_percent.'%</td>';
 976                  print '</tr>';
 977                  $i++;
 978              }
 979          }
 980          else
 981          {
 982              dolibarr_print_error($db);
 983          }
 984  
 985          print '</table>';
 986      }
 987  
 988      // Si creation depuis un contrat
 989      if ($_GET['contratid'])
 990      {
 991          print '<br>';
 992          print_titre($langs->trans('Services'));
 993  
 994          print '<table class="noborder" width="100%">';
 995          print '<tr class="liste_titre">';
 996          print '<td>'.$langs->trans('Ref').'</td>';
 997          print '<td>'.$langs->trans('Description').'</td>';
 998          print '<td align="right">'.$langs->trans('VAT').'</td>';
 999          print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
1000          print '<td align="right">'.$langs->trans('Qty').'</td>';
1001          print '<td align="right">'.$langs->trans('Discount').'</td></tr>';
1002  
1003          // Lignes de contrat produits prédéfinis
1004          $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,';
1005          $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,';
1006          $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel,';
1007          $sql.= ' p.label as product, p.ref, p.rowid as prodid';
1008          $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt, '.MAIN_DB_PREFIX.'product as p';
1009          $sql.= ' WHERE pt.fk_product = p.rowid AND pt.fk_contrat = '.$contrat->id;
1010          $sql.= ' ORDER BY pt.rowid ASC';
1011  
1012          $result = $db->query($sql);
1013          if ($result)
1014          {
1015              $num = $db->num_rows($result);
1016              $i = 0;
1017              $var=True;
1018              while ($i < $num)
1019              {
1020                  $objp = $db->fetch_object($result);
1021                  $var=!$var;
1022                  print '<tr '.$bc[$var].'><td><a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$objp->prodid.'">'.img_object($langs->trans(''),'service').' '.$objp->ref.'</a>';
1023                  print $objp->product?' - '.$objp->product:'';
1024                  // Plage de dates
1025                  $date_start=$objp->date_debut_prevue;
1026                  if ($objp->date_debut_reel) $date_start=$objp->date_debut_reel;
1027                  $date_end=$objp->date_fin_prevue;
1028                  if ($objp->date_fin_reel) $date_end=$objp->date_fin_reel;
1029                  print_date_range($date_start,$date_end);
1030  
1031                  print "</td>\n";
1032                  print '<td>';
1033                  print dolibarr_trunc($objp->description,60);
1034                  print '</td>';
1035                  print '<td align="right">'.$objp->tva_tx.'%</td>';
1036                  print '<td align="right">'.price($objp->subprice).'</td>';
1037                  print '<td align="right">'.$objp->qty.'</td>';
1038                  print '<td align="right">'.$objp->remise_percent.'%</td>';
1039                  print '</tr>';
1040                  $i++;
1041              }
1042          }
1043          else
1044          {
1045              dolibarr_print_error($db);
1046          }
1047          // Lignes de contrat non produits prédéfinis
1048          $sql  = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent,';
1049          $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,';
1050          $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel';
1051          $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt';
1052          $sql.= ' WHERE  pt.fk_contrat = '.$contrat->id;
1053          $sql.= ' AND (pt.fk_product = 0 or pt.fk_product is null)';
1054          $sql.= ' ORDER BY pt.rowid ASC';
1055  
1056          $result=$db->query($sql);
1057          if ($result)
1058          {
1059              $num = $db->num_rows($result);
1060              $i = 0;
1061              while ($i < $num)
1062              {
1063                  $objp = $db->fetch_object($result);
1064                  $var=!$var;
1065                  print '<tr '.$bc[$var].'><td>&nbsp;</td>';
1066                  print '<td>'.dolibarr_trunc($objp->product,60).'</td>';
1067                  print '<td align="right">'.$objp->tva_tx.'%</td>';
1068                  print '<td align="right">'.price($objp->subprice).'</td>';
1069                  print '<td align="right">'.$objp->qty.'</td>';
1070                  print '<td align="right">'.$objp->remise_percent.'%</td>';
1071                  print '</tr>';
1072                  $i++;
1073              }
1074          }
1075          else
1076          {
1077              dolibarr_print_error($db);
1078          }
1079  
1080          print '</table>';
1081      }
1082  
1083  }
1084  else
1085  {
1086      if ($_GET['facid'] > 0)
1087      {
1088          /* *************************************************************************** */
1089          /*                                                                             */
1090          /* Fiche en mode visu                                                          */
1091          /*                                                                             */
1092          /* *************************************************************************** */
1093  
1094          if ($msg) print $msg.'<br>';
1095  
1096          $fac = New Facture($db);
1097          if ( $fac->fetch($_GET['facid'], $user->societe_id) > 0)
1098          {
1099              $soc = new Societe($db, $fac->socidp);
1100              $soc->fetch($fac->socidp);
1101  
1102              $author = new User($db);
1103              if ($fac->user_author)
1104              {
1105                  $author->id = $fac->user_author;
1106                  $author->fetch();
1107              }
1108  
1109              $head = facture_prepare_head($fac);
1110   
1111              dolibarr_fiche_head($head, 0, $langs->trans('Bill').' : '.$fac->ref);
1112  
1113              /*
1114               * Confirmation de la suppression de la facture
1115                */
1116              if ($_GET['action'] == 'delete')
1117              {
1118                  $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('DeleteBill'),$langs->trans('ConfirmDeleteBill'),'confirm_delete');
1119                  print '<br />';
1120              }
1121  
1122              /*
1123                * Confirmation de la validation
1124                */
1125              if ($_GET['action'] == 'valid')
1126              {
1127                  $numfa = $fac->getNextNumRef($soc);
1128                  $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ValidateBill'),$langs->trans('ConfirmValidateBill',$numfa),'confirm_valid');
1129                  print '<br />';
1130              }
1131  
1132              /*
1133                * Confirmation du classement payé
1134                */
1135              if ($_GET['action'] == 'payed')
1136              {
1137                  $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ClassifyPayed'),$langs->trans('ConfirmClassifyPayedBill',$fac->ref),'confirm_payed');
1138                  print '<br />';
1139              }
1140  
1141              /*
1142               * Confirmation du classement abandonn
1143               */
1144              if ($_GET['action'] == 'canceled')
1145              {
1146                  $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('CancelBill'),$langs->trans('ConfirmCancelBill',$fac->ref),'confirm_canceled');
1147                  print '<br />';
1148              }
1149  
1150              /*
1151              *   Facture
1152              */
1153              print '<table class="border" width="100%">';
1154              
1155              // Reference
1156              print '<tr><td width="20%">'.$langs->trans('Ref').'</td><td colspan="5">'.$fac->ref.'</td></tr>';
1157  
1158              // Société
1159              print '<tr><td>'.$langs->trans('Company').'</td>';
1160              print '<td colspan="5">';
1161              print '<a href="fiche.php?socid='.$soc->id.'">'.$soc->nom.'</a></td>';
1162              print '</tr>';
1163  
1164              // Dates
1165              print '<tr><td>'.$langs->trans('Date').'</td>';
1166              print '<td colspan="3">'.dolibarr_print_date($fac->date,'%A %d %B %Y').'</td>';
1167              print '<td>'.$langs->trans('DateClosing').'</td><td>' . dolibarr_print_date($fac->date_lim_reglement,'%A %d %B %Y');
1168              if ($fac->date_lim_reglement < (time() - $conf->facture->client->warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans('Late'));
1169              print '</td></tr>';
1170  
1171              // Conditions et modes de réglement
1172              print '<tr><td>';
1173              print '<table class="nobordernopadding" width="100%"><tr><td>';
1174              print $langs->trans('PaymentConditions');
1175              print '</td>';
1176              if ($_GET['action'] != 'editconditions' && $fac->brouillon) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editconditions&amp;facid='.$fac->id.'">'.img_edit($langs->trans('SetConditions'),1).'</a></td>';
1177              print '</tr></table>';
1178              print '</td><td colspan="3">';
1179              if ($_GET['action'] == 'editconditions')
1180              {
1181                  $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'cond_reglement_id');
1182              }
1183              else
1184              {
1185                  $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'none');
1186              }
1187              print '</td>';
1188              print '<td width="25%">';
1189              print '<table class="nobordernopadding" width="100%"><tr><td>';
1190              print $langs->trans('PaymentMode');
1191              print '</td>';
1192              if ($_GET['action'] != 'editmode' && $fac->brouillon) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editmode&amp;facid='.$fac->id.'">'.img_edit($langs->trans('SetMode'),1).'</a></td>';
1193              print '</tr></table>';
1194              print '</td><td width="25%">';
1195              if ($_GET['action'] == 'editmode')
1196              {
1197                  $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'mode_reglement_id');
1198              }
1199              else
1200              {
1201                  $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'none');
1202              }
1203              print '</td></tr>';
1204  
1205              // Projet
1206              print '<tr>';
1207              if ($conf->projet->enabled)
1208              {
1209                  $langs->load('projects');
1210                  print '<td>';
1211                  
1212                  print '<table class="nobordernopadding" width="100%"><tr><td>';
1213                  print $langs->trans('Project');
1214                  print '</td>';
1215                  if ($_GET['action'] != 'classer')
1216                  {
1217                      print '<td align="right"><a href="facture.php?action=classer&amp;facid='.$fac->id.'">';
1218                      print img_edit($langs->trans('SetProject'),1);
1219                      print '</a></td>';
1220                  }
1221                  print '</tr></table>';
1222                  
1223                  print '</td><td colspan="3">';
1224                  if ($_GET['action'] == 'classer')
1225                  {
1226                      $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->fk_soc,$fac->projetid,'projetid');
1227                  }
1228                  else
1229                  {
1230                      $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->fk_soc,$fac->projetid,'none');
1231                  }
1232                  print '</td>';
1233              }
1234              else
1235              {
1236                  print '<td>&nbsp;</td><td colspan="3">&nbsp;</td>';
1237              }
1238  
1239              $nbrows=8;
1240              if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) $nbrows++;
1241              print '<td rowspan="'.$nbrows.'" colspan="2" valign="top">';
1242  
1243              /*
1244               * Liste des paiements
1245               */
1246              print $langs->trans('Payments').' :<br>';
1247              $sql = 'SELECT '.$db->pdate('datep').' as dp, pf.amount,';
1248              $sql.= ' c.libelle as paiement_type, p.num_paiement, p.rowid';
1249              $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p, '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement_facture as pf';
1250              $sql.= ' WHERE pf.fk_facture = '.$fac->id.' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid';
1251              $sql.= ' ORDER BY dp DESC';
1252  
1253              $result = $db->query($sql);
1254  
1255              if ($result)
1256              {
1257                  $num = $db->num_rows($result);
1258                  $i = 0; $totalpaye = 0;
1259                  print '<table class="noborder" width="100%">';
1260                  print '<tr class="liste_titre"><td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Type').'</td>';
1261                  print '<td align="right">'.$langs->trans('Amount').'</td><td>&nbsp;</td></tr>';
1262  
1263                  $var=True;
1264                  while ($i < $num)
1265                  {
1266                      $objp = $db->fetch_object($result);
1267                      $var=!$var;
1268                      print '<tr '.$bc[$var].'><td>';
1269                      print '<a href="'.DOL_URL_ROOT.'/compta/paiement/fiche.php?id='.$objp->rowid.'">'.img_object($langs->trans('ShowPayment'),'payment').' ';
1270                      print dolibarr_print_date($objp->dp).'</td>';
1271                      print '<td>'.$objp->paiement_type.' '.$objp->num_paiement.'</td>';
1272                      print '<td align="right">'.price($objp->amount).'</td><td>'.$langs->trans('Currency'.$conf->monnaie).'</td>';
1273                      print '</tr>';
1274                      $totalpaye += $objp->amount;
1275                      $i++;
1276                  }
1277  
1278                  if ($fac->paye == 0)
1279                  {
1280                      print '<tr><td colspan="2" align="right">'.$langs->trans('AlreadyPayed').' :</td><td align="right"><b>'.price($totalpaye).'</b></td><td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1281                      print '<tr><td colspan="2" align="right">'.$langs->trans("Billed").' :</td><td align="right" style="border: 1px solid;">'.price($fac->total_ttc).'</td><td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1282  
1283                      $resteapayer = $fac->total_ttc - $totalpaye;
1284  
1285                      print '<tr><td colspan="2" align="right">'.$langs->trans('RemainderToPay').' :</td>';
1286                      print '<td align="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($resteapayer).'</b></td><td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1287                  }
1288                  print '</table>';
1289                  $db->free($result);
1290              }
1291              else
1292              {
1293                  dolibarr_print_error($db);
1294              }
1295  
1296              print '</td></tr>';
1297  
1298              print '<tr><td>'.$langs->trans('Author').'</td><td colspan="3">'.$author->fullname.'</td></tr>';
1299  
1300              print '<tr><td>'.$langs->trans('GlobalDiscount').'</td>';
1301              if ($fac->brouillon == 1 && $user->rights->facture->creer)
1302              {
1303                  print '<form action="facture.php?facid='.$fac->id.'" method="post">';
1304                  print '<input type="hidden" name="action" value="setremise">';
1305                  print '<td colspan="3"><input type="text" name="remise" size="1" value="'.$fac->remise_percent.'">% ';
1306                  print '<input type="submit" class="button" value="'.$langs->trans('Modify').'"></td>';
1307                  print '</form>';
1308              }
1309              else
1310              {
1311                  print '<td colspan="3">'.$fac->remise_percent.'%</td>';
1312              }
1313              print '</tr>';
1314  
1315              /*
1316                \todo
1317                L'info "Reference commande client" est une carac de la commande et non de la facture.
1318                Elle devrait donc etre stockée sur l'objet commande lié à la facture et non sur la facture.
1319                Pour ceux qui utilisent ainsi, positionner la constante FAC_USE_CUSTOMER_ORDER_REF à 1.
1320              */
1321              if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF)
1322              {
1323                  print '<tr><td>'.$langs->trans('RefCdeClient').'</td>';
1324  
1325                  if ($fac->brouillon == 1 && $user->rights->facture->creer)
1326                  {
1327                          print '<form action="facture.php?facid='.$fac->id.'" method="post">';
1328                          print '<input type="hidden" name="action" value="set_ref_client">';
1329                          print '<td colspan="3"><input type="text" name="ref_client" size="20" value="'.$fac->ref_client.'">';
1330                          print '<input type="submit" class="button" value="'.$langs->trans('Modify').'"></td>';
1331                          print '</form>';
1332                  }
1333                  else
1334                  {
1335                      print '<td colspan="3">'.$fac->ref_client.'</td>';
1336                  }
1337              }
1338  
1339              print '<tr><td>'.$langs->trans('AmountHT').'</td>';
1340              print '<td align="right" colspan="2" nowrap><b>'.price($fac->total_ht).'</b></td>';
1341              print '<td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1342  
1343              print '<tr><td>'.$langs->trans('AmountVAT').'</td><td align="right" colspan="2" nowrap>'.price($fac->total_tva).'</td>';
1344              print '<td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1345              print '<tr><td>'.$langs->trans('AmountTTC').'</td><td align="right" colspan="2" nowrap>'.price($fac->total_ttc).'</td>';
1346              print '<td>'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';
1347  
1348              // Statut
1349              print '<tr><td>'.$langs->trans('Status').'</td><td align="left" colspan="3">'.($fac->getLibStatut()).'</td></tr>';
1350  
1351              if ($fac->note)
1352              {
1353                  print '<tr><td colspan="4" valign="top">'.$langs->trans('Note').' : '.nl2br($fac->note).'</td></tr>';
1354              }
1355              else
1356              {
1357                  print '<tr><td colspan="4">&nbsp;</td></tr>';
1358              }
1359  
1360              print '</table><br>';
1361  
1362              /*
1363               * Lignes de factures
1364               */
1365              $sql  = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux,';
1366              $sql .= ' l.remise_percent, l.subprice,';
1367              $sql .= ' '.$db->pdate('l.date_start').' as date_start,';
1368              $sql .= ' '.$db->pdate('l.date_end').' as date_end, ';
1369              $sql .= ' p.ref, p.fk_product_type, p.label as product';
1370              $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet as l ';
1371              $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid';
1372              $sql .= ' WHERE l.fk_facture = '.$fac->id;
1373              $sql .= ' ORDER BY l.rang ASC, l.rowid';
1374  
1375              $resql = $db->query($sql);
1376              if ($resql)
1377              {
1378                  $num_lignes = $db->num_rows($resql);
1379                  $i = 0; $total = 0;
1380  
1381                  print '<table class="noborder" width="100%">';
1382                  if ($num_lignes)
1383                  {
1384                      print '<tr class="liste_titre">';
1385                      print '<td>'.$langs->trans('Description').'</td>';
1386                      print '<td align="right" width="50">'.$langs->trans('VAT').'</td>';
1387                      print '<td align="right" width="80">'.$langs->trans('PriceUHT').'</td>';
1388                      print '<td align="right" width="50">'.$langs->trans('Qty').'</td>';
1389                      print '<td align="right" width="50">'.$langs->trans('Discount').'</td>';
1390                      print '<td align="right" width="50">'.$langs->trans('AmountHT').'</td>';
1391                      print '<td width="16">&nbsp;</td><td width="16">&nbsp;</td><td width="16">&nbsp;</td>';
1392                      print "</tr>\n";
1393                  }
1394                  $var=true;
1395                  while ($i < $num_lignes)
1396                  {
1397                      $objp = $db->fetch_object($resql);
1398                      $var=!$var;
1399  
1400                      // Ligne en mode visu
1401                      if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid)
1402                      {
1403                          print '<tr '.$bc[$var].'>';
1404                          if ($objp->fk_product > 0)
1405                          {
1406                              print '<td><a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$objp->fk_product.'">';
1407                              if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service');
1408                              else print img_object($langs->trans('ShowProduct'),'product');
1409                              print ' '.$objp->ref.'</a>';
1410                              print ' - '.nl2br(stripslashes($objp->product));
1411                              print_date_range($objp->date_start,$objp->date_end);
1412                              print ($objp->description && $objp->description!=$objp->product)?'<br>'.$objp->description:'';
1413                              print '</td>';
1414                          }
1415                          else
1416                          {
1417                              print '<td>'.stripslashes(nl2br($objp->description));
1418                              print_date_range($objp->date_start,$objp->date_end);
1419                              print "</td>\n";
1420                          }
1421                          print '<td align="right">'.$objp->tva_taux.'%</td>';
1422                          print '<td align="right">'.price($objp->subprice)."</td>\n";
1423                          print '<td align="right">'.$objp->qty.'</td>';
1424                          if ($objp->remise_percent > 0)
1425                          {
1426                              print '<td align="right">'.$objp->remise_percent."%</td>\n";
1427                          }
1428                          else
1429                          {
1430                              print '<td>&nbsp;</td>';
1431                          }
1432                          print '<td align="right">'.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."</td>\n";
1433  
1434                          // Icone d'edition et suppression
1435                          if ($fac->statut == 0  && $user->rights->facture->creer)
1436                          {
1437                              print '<td align="right"><a href="facture.php?facid='.$fac->id.'&amp;action=editline&amp;rowid='.$objp->rowid.'">';
1438                              print img_edit();
1439                              print '</a></td>';
1440                              print '<td align="right"><a href="facture.php?facid='.$fac->id.'&amp;action=deleteline&amp;rowid='.$objp->rowid.'">';
1441                              print img_delete();
1442                              print '</a></td>';
1443                              print '<td align="right">';
1444                              if ($i > 0)
1445                              {
1446                                  print '<a href="facture.php?facid='.$fac->id.'&amp;action=up&amp;rowid='.$objp->rowid.'">';
1447                                  print img_up();
1448                                  print '</a>';
1449                              }
1450                              if ($i < $num_lignes-1)
1451                              {
1452                                  print '<a href="facture.php?facid='.$fac->id.'&amp;action=down&amp;rowid='.$objp->rowid.'">';
1453                                  print img_down();
1454                                  print '</a>';
1455                              }
1456                              print '</td>';
1457                          }
1458                          else
1459                          {
1460                              print '<td colspan="3">&nbsp;</td>';
1461                          }
1462                          print '</tr>';
1463                      }
1464  
1465                      // Ligne en mode update
1466                      if ($_GET['action'] == 'editline' && $user->rights->facture->creer && $_GET['rowid'] == $objp->rowid)
1467                      {
1468                          print '<form action="facture.php" method="post">';
1469                          print '<input type="hidden" name="action" value="updateligne">';
1470                          print '<input type="hidden" name="facid" value="'.$fac->id.'">';
1471                          print '<input type="hidden" name="rowid" value="'.$_GET['rowid'].'">';
1472                          print '<tr '.$bc[$var].'>';
1473                          print '<td>';
1474                          if ($objp->fk_product > 0)
1475                          {
1476                              print '<a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$objp->fk_product.'">';
1477                              if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service');
1478                              else print img_object($langs->trans('ShowProduct'),'product');
1479                              print ' '.$objp->ref.'</a>';
1480                              print ' - '.stripslashes(nl2br($objp->product));
1481                              print '<br>';
1482                          }
1483                          print '<textarea name="desc" cols="70" rows="'.ROWS_2.'">'.stripslashes($objp->description).'</textarea></td>';
1484                          print '<td align="right">';
1485                          print $html->select_tva('tva_tx',$objp->tva_taux,$mysoc,$soc);
1486                          print '</td>';
1487                          print '<td align="right"><input size="6" type="text" name="price" value="'.price($objp->subprice).'"></td>';
1488                          print '<td align="right"><input size="2" type="text" name="qty" value="'.$objp->qty.'"></td>';
1489                          print '<td align="right" nowrap><input size="2" type="text" name="remise_percent" value="'.$objp->remise_percent.'">%</td>';
1490                          print '<td align="center" rowspan="1" colspan="5" valign="center"><input type="submit" class="button" name="save" value="'.$langs->trans('Save').'">';
1491                          print '<br /><input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'"></td>';
1492                          print '</tr>' . "\n";
1493                          if ($conf->service->enabled)
1494                          {
1495                              print '<tr '.$bc[$var].'>';
1496                              print '<td colspan="9">'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' ';
1497                              print $html->select_date($objp->date_start,'date_start',0,0,$objp->date_start?0:1);
1498                              print ' '.$langs->trans('to').' ';
1499                              print $html->select_date($objp->date_end,'date_end',0,0,$objp->date_end?0:1);
1500                              print '</td>';
1501                              print '</tr>';
1502                          }
1503                          print "</form>\n";
1504                      }
1505  
1506                      $total = $total + ($objp->qty * $objp->price);
1507                      $i++;
1508                  }
1509  
1510                  $db->free($resql);
1511              }
1512              else
1513              {
1514                  dolibarr_print_error($db);
1515              }
1516  
1517              /*
1518               * Ajouter une ligne
1519               */
1520              if ($fac->statut == 0 && $user->rights->facture->creer && $_GET['action'] <> 'valid')
1521              {
1522                  print '<tr class="liste_titre">';
1523                  print '<td>'.$langs->trans('Description').'</td>';
1524                  print '<td align="right">'.$langs->trans('VAT').'</td>';
1525                  print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
1526                  print '<td align="right">'.$langs->trans('Qty').'</td>';
1527                  print '<td align="right">'.$langs->trans('Discount').'</td>';
1528                  print '<td>&nbsp;</td>';
1529                  print '<td>&nbsp;</td>';
1530                  print '<td>&nbsp;</td>';
1531                  print '<td>&nbsp;</td>';
1532                  print "</tr>\n";
1533  
1534                  // Ajout produit produits/services personalisés
1535                  print '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
1536                  print '<input type="hidden" name="facid" value="'.$fac->id.'">';
1537                  print '<input type="hidden" name="action" value="addligne">';
1538  
1539                  $var=true;
1540                  print '<tr '.$bc[$var].'>';
1541                  print '<td colspan="1">';
1542                  print '<textarea name="desc" cols="70" rows="'.ROWS_2.'"></textarea></td>';
1543                  print '<td align="right">';
1544                  $html->select_tva('tva_tx',$conf->defaulttx,$mysoc,$soc);
1545                  print '</td>';
1546                  print '<td align="right"><input type="text" name="pu" size="6"></td>';
1547                  print '<td align="right"><input type="text" name="qty" value="1" size="2"></td>';
1548                  print '<td align="right" nowrap><input type="text" name="remise_percent" size="1" value="0">%</td>';
1549                  print '<td align="center" valign="middle" rowspan="1" colspan="4"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td>';
1550                  print '</tr>';
1551                  if ($conf->service->enabled)
1552                  {
1553                      print '<tr '.$bc[$var].'>';
1554                      print '<td colspan="9">'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' ';
1555                      print $html->select_date('','date_start',0,0,1);
1556                      print ' '.$langs->trans('to').' ';
1557                      print $html->select_date('','date_end',0,0,1);
1558                      print '</td>';
1559                      print '</tr>';
1560                  }
1561                  print '</form>';
1562  
1563                  // Ajout de produits/services prédéfinis
1564                  if ($conf->produit->enabled)
1565                  {
1566                      print '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
1567                      print '<input type="hidden" name="facid" value="'.$fac->id.'">';
1568                      print '<input type="hidden" name="action" value="addligne">';
1569      
1570                      $var=! $var;
1571                      print '<tr '.$bc[$var].'>';
1572                      print '<td colspan="2">';
1573                      $html->select_produits('','idprod','',$conf->produit->limit_size);
1574                      print '<br>';
1575                      print '<textarea name="desc" cols="70" rows="'.ROWS_2.'"></textarea></td>';
1576                      print '<td>&nbsp;</td>';
1577                      print '<td align="right"><input type="text" name="qty" value="1" size="2"></td>';
1578                      print '<td align="right" nowrap><input type="text" name="remise_percent" size="2" value="0">%</td>';
1579                      print '<td align="center" valign="middle" rowspan="2" colspan="5"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td>';
1580                      print '</tr>';
1581                      if ($conf->service->enabled)
1582                      {
1583                          print '<tr '.$bc[$var].'>';
1584                          print '<td colspan="5">'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' ';
1585                          print $html->select_date('','date_start',0,0,1);
1586                          print ' '.$langs->trans('to').' ';
1587                          print $html->select_date('','date_end',0,0,1);
1588                          print '</td>';
1589                          print '</tr>';
1590                      }
1591                      print '</form>';
1592                  }
1593              }
1594              print "</table>\n";
1595  
1596              print "</div>\n";
1597  
1598  
1599              /*
1600               * Boutons actions
1601               */
1602  
1603              if ($user->societe_id == 0 && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline')
1604              {
1605                  print '<div class="tabsAction">';
1606                  // Valider
1607                  if ($fac->statut == 0 && $num_lignes > 0)
1608                  {
1609                      if ($user->rights->facture->valider)
1610                      {
1611                          print '  <a class="butAction" href="facture.php?facid='.$fac->id.'&amp;action=valid">'.$langs->trans('Validate').'</a>';
1612                      }
1613                  }
1614                  else
1615                  {
1616                      // Générer
1617                      if ($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer)
1618                      {
1619                          if ($fac->paye == 0)
1620                          {
1621                              print '  <a class="butAction" href="facture.php?facid='.$fac->id.'&amp;action=pdf">'.$langs->trans('BuildPDF').'</a>';
1622                          }
1623                          else
1624                          {
1625                              print '  <a class="butAction" href="facture.php?facid='.$fac->id.'&amp;action=pdf">'.$langs->trans('RebuildPDF').'</a>';
1626                          }
1627                      }
1628                  }
1629  
1630                  // Supprimer
1631                  if ($fac->statut == 0 && $user->rights->facture->supprimer && $_GET['action'] != 'delete')
1632                  {
1633                      print '<a class="butActionDelete" href="facture.php?facid='.$fac->id.'&amp;action=delete">'.$langs->trans('Delete').'</a>';
1634                  }
1635  
1636                  // Envoyer
1637                  if ($fac->statut == 1 && $user->rights->facture->envoyer)
1638                  {
1639                      print '  <a class="butAction" href="'.$_SERVER['PHP_SELF'].'?facid='.$fac->id.'&amp;action=presend">'.$langs->trans('Send').'</a>';
1640                  }
1641  
1642                  // Envoyer une relance
1643                  if ($fac->statut == 1 && price($resteapayer) > 0 && $user->rights->facture->envoyer)
1644                  {
1645                      print '  <a class="butAction" href="'.$_SERVER['PHP_SELF'].'?facid='.$fac->id.'&amp;action=prerelance">'.$langs->trans('SendRemind').'</a>';
1646                  }
1647  
1648                  // Emettre paiement
1649                  if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement)
1650                  {
1651                      print '  <a class="butAction" href="paiement.php?facid='.$fac->id.'&amp;action=create">'.$langs->trans('DoPaiement').'</a>';
1652                  }
1653  
1654                  // Classer 'payé'
1655                  if ($fac->statut == 1 && price($resteapayer) <= 0
1656                  && $fac->paye == 0 && $user->rights->facture->paiement)
1657                  {
1658                      print '  <a class="butAction" href="'.$_SERVER['PHP_SELF'].'?facid='.$fac->id.'&amp;action=payed">'.$langs->trans('ClassifyPayed').'</a>';
1659                  }
1660  
1661                  // Classer 'abandonnée' (possible si validée et pas encore classer payée)
1662                  if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement)
1663                  {
1664                      print '  <a class="butAction" href="'.$_SERVER['PHP_SELF'].'?facid='.$fac->id.'&amp;action=canceled">'.$langs->trans('ClassifyCanceled').'</a>';
1665                  }
1666  
1667                  // Récurrente
1668                  if (! defined('FACTURE_DISABLE_RECUR') || FACTURE_DISABLE_RECUR == 0)     // Possibilité de désactiver les factures récurrentes
1669                  {
1670                      if ($fac->statut > 0)
1671                      {
1672                          print '  <a class="butAction" href="facture/fiche-rec.php?facid='.$fac->id.'&amp;action=create">Récurrente</a>';
1673                      }
1674                  }
1675  
1676                  print '</div>';
1677              }
1678  
1679              print '<table width="100%"><tr><td width="50%" valign="top">';
1680  
1681              /*
1682               * Documents générés
1683               */
1684              $filename=sanitize_string($fac->ref);
1685              $filedir=$conf->facture->dir_output . '/' . sanitize_string($fac->ref);
1686              $urlsource=$_SERVER['PHP_SELF'].'?facid='.$fac->id;
1687  //            $genallowed=($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer);
1688  //            $delallowed=$user->rights->facture->supprimer;
1689              $genallowed=0;
1690              $delallowed=0;
1691  
1692              $var=true;
1693  
1694              print '<br>';
1695              $html->show_documents('facture',$filename,$filedir,$urlsource,$genallowed,$delallowed);
1696  
1697              /*
1698               *   Propales rattachées
1699               */
1700              $sql = 'SELECT '.$db->pdate('p.datep').' as dp, p.price, p.ref, p.rowid as propalid';
1701              $sql .= ' FROM '.MAIN_DB_PREFIX.'propal as p, '.MAIN_DB_PREFIX.'fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = '.$fac->id;
1702  
1703              $resql = $db->query($sql);
1704              if ($resql)
1705              {
1706                  $num = $db->num_rows($resql);
1707                  if ($num)
1708                  {
1709                      $i = 0; $total = 0;
1710                      print '<br>';
1711                      print_titre($langs->trans('RelatedCommercialProposals'));
1712                      print '<table class="noborder" width="100%">';
1713                      print '<tr class="liste_titre">';
1714                      print '<td>'.$langs->trans('Ref').'</td>';
1715                      print '<td>'.$langs->trans('Date').'</td>';
1716                      print '<td align="right">'.$langs->trans('Price').'</td>';
1717                      print '</tr>';
1718  
1719                      $var=True;
1720                      while ($i < $num)
1721                      {
1722                          $objp = $db->fetch_object($resql);
1723                          $var=!$var;
1724                          print '<tr '.$bc[$var].'>';
1725                          print '<td><a href="propal.php?propalid='.$objp->propalid.'">'.img_object($langs->trans('ShowPropal'),'propal').' '.$objp->ref.'</a></td>';
1726                          print '<td>'.dolibarr_print_date($objp->dp).'</td>';
1727                          print '<td align="right">'.price($objp->price).'</td>';
1728                          print '</tr>';
1729                          $total = $total + $objp->price;
1730                          $i++;
1731                      }
1732                      print '<tr class="liste_total"><td>&nbsp;</td><td align="left">'.$langs->trans('TotalHT').'</td><td align="right">'.price($total).'</td></tr>';
1733                      print '</table>';
1734                  }
1735              }
1736              else
1737              {
1738                  dolibarr_print_error($db);
1739              }
1740  
1741              /*
1742               * Commandes rattachées
1743               */
1744              if($conf->commande->enabled)
1745              {
1746                  $sql = 'SELECT '.$db->pdate('c.date_commande').' as date_commande, c.total_ht, c.ref, c.ref_client, c.rowid as id';
1747                  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'co_fa as co_fa WHERE co_fa.fk_commande = c.rowid AND co_fa.fk_facture = '.$fac->id;
1748                  $resql = $db->query($sql);
1749                  if ($resql)
1750                  {
1751                      $num = $db->num_rows($resql);
1752                      if ($num)
1753                      {
1754                          $i = 0; $total = 0;
1755                          print '<br>';
1756                          print_titre($langs->trans('RelatedOrders'));
1757                          print '<table class="noborder" width="100%">';
1758                          print '<tr class="liste_titre">';
1759                          print '<td>'.$langs->trans('Ref').'</td>';
1760                             print '<td>'.$langs->trans('RefCdeClientShort').'</td>';
1761                          print '<td align="center">'.$langs->trans('Date').'</td>';
1762                          print '<td align="right">'.$langs->trans('AmountHT').'</td>';
1763                          print '</tr>';
1764                          $var=true;
1765                          while ($i < $num)
1766                          {
1767                              $objp = $db->fetch_object($resql);
1768                              $var=!$var;
1769                              print '<tr '.$bc[$var].'><td>';
1770                              print '<a href="'.DOL_URL_ROOT.'/commande/fiche.php?id='.$objp->id.'">'.img_object($langs->trans('ShowOrder'), 'order').' '.$objp->ref."</a></td>\n";
1771                                 print '<td>'.$objp->ref_client.'</td>';
1772                              print '<td align="center">'.dolibarr_print_date($objp->date_commande).'</td>';
1773                              print '<td align="right">'.price($objp->total_ht).'</td>';
1774                              print "</tr>\n";
1775                              $total = $total + $objp->total_ht;
1776                              $i++;
1777                          }
1778                          print '<tr class="liste_total">';
1779                          print '<td align="left">'.$langs->trans('TotalHT').'</td>';
1780                             print '<td>&nbsp;</td>';
1781                             print '<td>&nbsp;</td>';
1782                          print '<td align="right">'.price($total).'</td></tr>';
1783                          print '</table>';
1784                      }
1785                  }
1786                  else
1787                  {
1788                      dolibarr_print_error($db);
1789                  }
1790              }
1791  
1792              print '</td><td valign="top" width="50%">';
1793  
1794              /*
1795               * Liste des actions propres à la facture
1796               */
1797              $sql = 'SELECT id, '.$db->pdate('a.datea').' as da, a.label, a.note, code';
1798              $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a, '.MAIN_DB_PREFIX.'user as u ';
1799              $sql .= ' WHERE a.fk_user_author = u.rowid ';
1800              $sql .= ' AND a.fk_action in (9,10) ';
1801              $sql .= ' AND a.fk_soc = '.$fac->socidp ;
1802              $sql .= ' AND a.fk_facture = '.$fac->id;
1803  
1804              $resql = $db->query($sql);
1805              if ($resql)
1806              {
1807                  $num = $db->num_rows($resql);
1808                  if ($num)
1809                  {
1810                      print '<br>';
1811                      print_titre($langs->trans('ActionsOnBill'));
1812  
1813                      $i = 0; $total = 0;
1814                      print '<table class="border" width="100%">';
1815                      print '<tr '.$bc[$var].'><td>'.$langs->trans('Ref').'</td><td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Action').'</td><td>'.$langs->trans('By').'</td></tr>';
1816                      print "\n";
1817  
1818                      $var=True;
1819                      while ($i < $num)
1820                      {
1821                          $objp = $db->fetch_object($resql);
1822                          $var=!$var;
1823                          print '<tr '.$bc[$var].'>';
1824                          print '<td><a href="'.DOL_URL_ROOT.'/comm/action/fiche.php?id='.$objp->id.'">'.img_object($langs->trans('ShowTask'),'task').' '.$objp->id.'</a></td>';
1825                          print '<td>'.dolibarr_print_date($objp->da).'</td>';
1826                          print '<td>'.stripslashes($objp->label).'</td>';
1827                          print '<td>'.$objp->code.'</td>';
1828                          print '</tr>';
1829                          $i++;
1830                      }
1831                      print '</table>';
1832                  }
1833              }
1834              else
1835              {
1836                  dolibarr_print_error($db);
1837              }
1838  
1839              print '</td></tr></table>';
1840  
1841  
1842              /*
1843               * Affiche formulaire mail
1844               */
1845              if ($_GET['action'] == 'presend')
1846              {
1847                  print '<br>';
1848                  print_titre($langs->trans('SendBillByMail'));
1849  
1850                  $liste[0]='&nbsp;';
1851                  foreach ($soc->contact_email_array() as $key=>$value)
1852                  {
1853                      $liste[$key]=$value;
1854                  }
1855  
1856                  // Créé l'objet formulaire mail
1857                  include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php');
1858                  $formmail = new FormMail($db);
1859                  $formmail->fromname = $user->fullname;
1860                  $formmail->frommail = $user->email;
1861                  $formmail->withfrom=1;
1862                  $formmail->withto=$liste;
1863                  $formmail->withcc=1;
1864                  $formmail->withtopic=$langs->trans('SendBillRef','__FACREF__');
1865                  $formmail->withfile=1;
1866                  $formmail->withbody=1;
1867                  // Tableau des substitutions
1868                  $formmail->substit['__FACREF__']=$fac->ref;
1869                  // Tableau des paramètres complémentaires du post
1870                  $formmail->param['action']='send';
1871                  $formmail->param['models']='facture_send';
1872                  $formmail->param['facid']=$fac->id;
1873                  $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id;
1874  
1875                  $formmail->show_form();
1876  
1877                  print '<br>';
1878              }
1879  
1880              if ($_GET['action'] == 'prerelance')
1881              {
1882                  print '<br>';
1883                  print_titre($langs->trans('SendReminderBillByMail'));
1884  
1885                  $liste[0]='&nbsp;';
1886                  foreach ($soc->contact_email_array() as $key=>$value)
1887                  {
1888                      $liste[$key]=$value;
1889                  }
1890  
1891                  // Créé l'objet formulaire mail
1892                  include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php');
1893                  $formmail = new FormMail($db);
1894                  $formmail->fromname = $user->fullname;
1895                  $formmail->frommail = $user->email;
1896                  $formmail->withfrom=1;
1897                  $formmail->withto=$liste;
1898                  $formmail->withcc=1;
1899                  $formmail->withtopic=$langs->trans('SendReminderBillRef','__FACREF__');
1900                  $formmail->withfile=1;
1901                  $formmail->withbody=1;
1902                  // Tableau des substitutions
1903                  $formmail->substit['__FACREF__']=$fac->ref;
1904                  // Tableau des paramètres complémentaires
1905                  $formmail->param['action']='relance';
1906                  $formmail->param['models']='facture_relance';
1907                  $formmail->param['facid']=$fac->id;
1908                  $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id;
1909  
1910                  $formmail->show_form();
1911  
1912                  print '<br>';
1913              }
1914  
1915          }
1916          else
1917          {
1918              dolibarr_print_error($db,$fac->error);
1919          }
1920      }
1921      else
1922      {
1923          /***************************************************************************
1924           *                                                                         *
1925           *                      Mode Liste                                         *
1926           *                                                                         *
1927           ***************************************************************************/
1928          $page = $_GET['page'];
1929          $sortorder=$_GET['sortorder'];
1930          $sortfield=$_GET['sortfield'];
1931          $month=$_GET['month'];
1932          $year=$_GET['year'];
1933  
1934          $fac=new Facture($db);
1935  
1936          if ($page == -1) $page = 0 ;
1937  
1938          if ($user->rights->facture->lire)
1939          {
1940              $limit = $conf->liste_limit;
1941              $offset = $limit * $page ;
1942  
1943              if (! $sortorder) $sortorder='DESC';
1944              if (! $sortfield) $sortfield='f.datef';
1945  
1946              $sql = 'SELECT s.nom,s.idp,f.facnumber,f.increment,f.total,f.total_ttc,';
1947              $sql.= $db->pdate('f.datef').' as df, '.$db->pdate('f.date_lim_reglement').' as datelimite, ';
1948              $sql.= ' f.paye as paye, f.rowid as facid, f.fk_statut';
1949              if (! $sall) $sql.= ' ,sum(pf.amount) as am';
1950              $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s';
1951              $sql.= ','.MAIN_DB_PREFIX.'facture as f';
1952              if (! $sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON f.rowid=pf.fk_facture ';
1953              if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet as fd ON f.rowid=fd.fk_facture ';
1954              $sql.= ' WHERE f.fk_soc = s.idp';
1955              if ($socidp) $sql .= ' AND s.idp = '.$socidp;
1956              if ($month > 0) $sql .= ' AND date_format(f.datef, \'%m\') = '.$month;
1957              if ($_GET['filtre'])
1958              {
1959                  $filtrearr = split(',', $_GET['filtre']);
1960                  foreach ($filtrearr as $fil)
1961                  {
1962                      $filt = split(':', $fil);
1963                      $sql .= ' AND ' . $filt[0] . ' = ' . $filt[1];
1964                  }
1965              }
1966              if ($_GET['search_ref'])
1967              {
1968                  $sql .= ' AND f.facnumber like \'%'.$_GET['search_ref'].'%\'';
1969              }
1970              if ($_GET['search_societe'])
1971              {
1972                  $sql .= ' AND s.nom like \'%'.$_GET['search_societe'].'%\'';
1973              }
1974              if ($_GET['search_montant_ht'])
1975              {
1976                  $sql .= ' AND f.total = \''.$_GET['search_montant_ht'].'\'';
1977              }
1978              if ($_GET['search_montant_ttc'])
1979              {
1980                  $sql .= ' AND f.total_ttc = \''.$_GET['search_montant_ttc'].'\'';
1981              }
1982              if ($year > 0)
1983              {
1984                  $sql .= ' AND date_format(f.datef, \'%Y\') = '.$year;
1985              }
1986              if ($_POST['sf_ref'])
1987              {
1988                  $sql .= ' AND f.facnumber like \'%'.$_POST['sf_ref'] . '%\'';
1989              }
1990              if ($sall)
1991              {
1992                  $sql .= ' AND (s.nom like \'%'.$sall.'%\' OR f.facnumber like \'%'.$sall.'%\' OR f.note like \'%'.$sall.'%\' OR fd.description like \'%'.$sall.'%\')';
1993              }
1994  
1995              $sql .= ' GROUP BY f.facnumber';
1996  
1997              $sql .= ' ORDER BY ';
1998              $listfield=split(',',$sortfield);
1999              foreach ($listfield as $key => $value)
2000                  $sql.= $listfield[$key].' '.$sortorder.',';
2001              $sql .= ' f.rowid DESC ';
2002  
2003              $sql .= $db->plimit($limit+1,$offset);
2004  
2005              $resql = $db->query($sql);
2006              if ($resql)
2007              {
2008                  $num = $db->num_rows($resql);
2009  
2010                  if ($socidp)
2011                  {
2012                      $soc = new Societe($db);
2013                      $soc->fetch($socidp);
2014                  }
2015  
2016                  print_barre_liste($langs->trans('BillsCustomers').' '.($socidp?' '.$soc->nom:''),$page,'facture.php','&amp;socidp='.$socidp,$sortfield,$sortorder,'',$num);
2017  
2018                  $i = 0;
2019                  print '<table class="liste" width="100%">';
2020                  print '<tr class="liste_titre">';
2021                  print_liste_field_titre($langs->trans('Ref'),$_SERVER['PHP_SELF'],'f.facnumber','','&amp;socidp='.$socidp,'',$sortfield);
2022                  print_liste_field_titre($langs->trans('Date'),$_SERVER['PHP_SELF'],'f.datef','','&amp;socidp='.$socidp,'align="center"',$sortfield);
2023                  print_liste_field_titre($langs->trans('Company'),$_SERVER['PHP_SELF'],'s.nom','','&amp;socidp='.$socidp,'',$sortfield);
2024                  print_liste_field_titre($langs->trans('AmountHT'),$_SERVER['PHP_SELF'],'f.total','','&amp;socidp='.$socidp,'align="right"',$sortfield);
2025                  print_liste_field_titre($langs->trans('AmountTTC'),$_SERVER['PHP_SELF'],'f.total_ttc','','&amp;socidp='.$socidp,'align="right"',$sortfield);
2026                  print_liste_field_titre($langs->trans('Received'),$_SERVER['PHP_SELF'],'am','','&amp;socidp='.$socidp,'align="right"',$sortfield);
2027                  print_liste_field_titre($langs->trans('Status'),$_SERVER['PHP_SELF'],'fk_statut,paye','','&amp;socidp='.$socidp,'align="right"',$sortfield);
2028                  print '</tr>';
2029  
2030                  // Lignes des champs de filtre
2031                  print '<form method="get" action="facture.php">';
2032                  print '<tr class="liste_titre">';
2033                  print '<td class="liste_titre" valign="right">';
2034                  print '<input class="flat" size="10" type="text" name="search_ref" value="'.$_GET['search_ref'].'">';
2035                  print '</td><td class="liste_titre">&nbsp;</td>';
2036                  print '<td class="liste_titre" align="left">';
2037                  print '<input class="flat" type="text" name="search_societe" value="'.$_GET['search_societe'].'">';
2038                  print '</td><td class="liste_titre" align="right">';
2039                  print '<input class="flat" type="text" size="10" name="search_montant_ht" value="'.$_GET['search_montant_ht'].'">';
2040                  print '</td><td class="liste_titre" align="right">';
2041                  print '<input class="flat" type="text" size="10" name="search_montant_ttc" value="'.$_GET['search_montant_ttc'].'">';
2042                  print '</td><td class="liste_titre" colspan="2" align="right">';
2043                  print '<input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" alt="'.$langs->trans('Search').'">';
2044                  print '</td>';
2045                  print '</tr>';
2046                  print '</form>';
2047  
2048                  if ($num > 0)
2049                  {
2050                      $var=True;
2051                      $total=0;
2052                      $totalrecu=0;
2053  
2054                      while ($i < min($num,$limit))
2055                      {
2056                          $objp = $db->fetch_object($resql);
2057                          $var=!$var;
2058  
2059                          print '<tr '.$bc[$var].'>';
2060                          if ($objp->paye)
2061                          {
2062                              $class = 'normal';
2063                          }
2064                          else
2065                          {
2066                              if ($objp->fk_statut == 0)
2067                              {
2068                                  $class = 'normal';
2069                              }
2070                              else
2071                              {
2072                                  $class = 'impayee';
2073                              }
2074                          }
2075  
2076                          print '<td nowrap="nowrap"><a href="facture.php?facid='.$objp->facid.'">'.img_object($langs->trans('ShowBill'),'bill').'</a> ';
2077                          print '<a href="facture.php?facid='.$objp->facid.'">'.$objp->facnumber.'</a>'.$objp->increment;
2078                          if ($objp->datelimite < (time() - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $objp->am) print img_warning($langs->trans('Late'));
2079                          print '</td>';
2080  
2081                          if ($objp->df > 0 )
2082                          {
2083                              print '<td align="center" nowrap>';
2084                              $y = strftime('%Y',$objp->df);
2085                              $m = strftime('%m',$objp->df);
2086                              print strftime('%d',$objp->df);
2087                              print ' <a href="facture.php?year='.$y.'&amp;month='.$m.'">';
2088                              print substr(strftime('%B',$objp->df),0,3).'</a>';
2089                              print ' <a href="facture.php?year='.$y.'">';
2090                              print strftime('%Y',$objp->df).'</a></td>';
2091                          }
2092                          else
2093                          {
2094                              print '<td align="center"><b>!!!</b></td>';
2095                          }
2096                          print '<td><a href="fiche.php?socid='.$objp->idp.'">'.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.'</a></td>';
2097                          print '<td align="right">'.price($objp->total).'</td>';
2098                          print '<td align="right">'.price($objp->total_ttc).'</td>';
2099                          print '<td align="right">'.price($objp->am).'</td>';
2100  
2101                          // Affiche statut de la facture
2102                          if (! $objp->paye)
2103                          {
2104                              if ($objp->fk_statut == 0)
2105                              {
2106                                  print '<td align="center">'.$langs->trans('BillShortStatusDraft').'</td>';
2107                              }
2108                              elseif ($objp->fk_statut == 3)
2109                              {
2110                                  print '<td align="center">'.$langs->trans('BillShortStatusCanceled').'</td>';
2111                              }
2112                              else
2113                              {
2114                                  print '<td align="center"><a class="'.$class.'" href="facture.php?filtre=paye:0,fk_statut:1">'.($objp->am?$langs->trans('BillShortStatusStarted'):$langs->trans('BillShortStatusNotPayed')).'</a></td>';
2115                              }
2116                          }
2117                          else
2118                          {
2119                              print '<td align="center">'.$langs->trans('BillShortStatusPayed').'</td>';
2120                          }
2121  
2122                          print '</tr>';
2123                          $total+=$objp->total;
2124                          $total_ttc+=$objp->total_ttc;
2125                          $totalrecu+=$objp->am;
2126                          $i++;
2127                      }
2128  
2129                      if (($offset + $num) <= $limit)
2130                      {
2131                          // Print total
2132                          print '<tr class="liste_total">';
2133                          print '<td class="liste_total" colspan="3" align="left">'.$langs->trans('Total').'</td>';
2134                          print '<td class="liste_total" align="right">'.price($total).'</td>';
2135                          print '<td class="liste_total" align="right">'.price($total_ttc).'</td>';
2136                          print '<td class="liste_total" align="right">'.price($totalrecu).'</td>';
2137                          print '<td class="liste_total" align="center">&nbsp;</td>';
2138                          print '</tr>';
2139                      }
2140                  }
2141  
2142                  print '</table>';
2143                  $db->free($resql);
2144              }
2145              else
2146              {
2147                  dolibarr_print_error($db);
2148              }
2149          }
2150      }
2151  }
2152  
2153  $db->close();
2154  
2155  llxFooter('$Date: 2006/01/07 17:13:52 $ - $Revision: 1.252.2.4 $');
2156  ?>


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