/[dolibarr]/dolibarr/htdocs/compta/facture.php
ViewVC logotype

Diff of /dolibarr/htdocs/compta/facture.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.196  
changed lines
  Added in v.1.197

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26