[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/commande/ -> commande.class.php (source)

   1  <?php
   2  /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   * Copyright (C) 2004-2005 Laurent Destailleur  <eldy@users.sourceforge.net>
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License
  16   * along with this program; if not, write to the Free Software
  17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18   *
  19   * $Id: commande.class.php,v 1.40.2.1 2006/01/14 13:42:21 eldy Exp $
  20   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/commande/commande.class.php,v $
  21   */
  22   
  23  /**
  24          \file       htdocs/commande/commande.class.php
  25          \ingroup    commande
  26          \brief      Fichier des classes de commandes
  27          \version    $Revision: 1.40.2.1 $
  28  */
  29  
  30  require_once(DOL_DOCUMENT_ROOT."/product.class.php");
  31  
  32  /**
  33          \class      Commande
  34          \brief      Classe de gestion de commande
  35  */
  36  class Commande
  37  {
  38      var $db ;
  39      var $id ;
  40      var $brouillon;
  41  
  42      // Pour board
  43      var $nbtodo;
  44      var $nbtodolate;
  45  
  46      /**
  47       *        \brief      Constructeur
  48       *        \param      DB      Handler d'accès base
  49       */
  50  	function Commande($DB)
  51      {
  52          global $langs;
  53          $langs->load('orders');
  54          $this->db = $DB;
  55          $this->statuts[-1] = $langs->trans('StatusOrderCanceled');
  56          $this->statuts[0]  = $langs->trans('StatusOrderDraft');
  57          $this->statuts[1]  = $langs->trans('StatusOrderValidated');
  58          $this->statuts[2]  = $langs->trans('StatusOrderOnProcess');
  59          $this->statuts[3]  = $langs->trans('StatusOrderProcessed');
  60  
  61          $this->status_label_short[-1] = $langs->trans('StatusOrderCanceled');
  62          $this->status_label_short[0]  = $langs->trans('StatusOrderDraft');
  63          $this->status_label_short[1]  = $langs->trans('StatusOrderValidated');
  64          $this->status_label_short[2]  = $langs->trans('StatusOrderOnProcessShort');
  65          $this->status_label_short[3]  = $langs->trans('StatusOrderProcessed');
  66  
  67          $this->sources[0] = $langs->trans('OrderSource0');
  68          $this->sources[1] = $langs->trans('OrderSource1');
  69          $this->sources[2] = $langs->trans('OrderSource2');
  70          $this->sources[3] = $langs->trans('OrderSource3');
  71          $this->sources[4] = $langs->trans('OrderSource4');
  72          $this->sources[5] = $langs->trans('OrderSource5');
  73          $this->products = array();
  74      }
  75  
  76      /**     \brief      Créé la commande depuis une propale existante
  77              \param      user            Utilisateur qui crée
  78              \param      propale_id      id de la propale qui sert de modèle
  79      */
  80  	function create_from_propale($user, $propale_id)
  81      {
  82          $propal = new Propal($this->db);
  83          $propal->fetch($propale_id);
  84          $this->lines = array();
  85          $this->date_commande = time();
  86          $this->source = 0;
  87          for ($i = 0 ; $i < sizeof($propal->lignes) ; $i++)
  88          {
  89              $CommLigne = new CommandeLigne();
  90              $CommLigne->libelle        = $propal->lignes[$i]->libelle;
  91              $CommLigne->price          = $propal->lignes[$i]->subprice;
  92              $CommLigne->subprice       = $propal->lignes[$i]->subprice;
  93              $CommLigne->tva_tx         = $propal->lignes[$i]->tva_tx;
  94              $CommLigne->qty            = $propal->lignes[$i]->qty;
  95              $CommLigne->remise_percent = $propal->lignes[$i]->remise_percent;
  96              $CommLigne->product_id     = $propal->lignes[$i]->product_id;
  97              $this->lines[$i] = $CommLigne;
  98          }
  99  
 100          $this->soc_id = $propal->soc_id;
 101  
 102          /* Définit la société comme un client */
 103          $soc = new Societe($this->db);
 104          $soc->id = $this->soc_id;
 105          $soc->set_as_client();
 106          $this->propale_id = $propal->id;
 107  
 108          return $this->create($user);
 109      }
 110  
 111    /**   \brief      Valide la commande
 112          \param      user            Utilisateur qui valide
 113     */
 114  	function valid($user)
 115      {
 116          $result = 0;
 117          if ($user->rights->commande->valider)
 118          {
 119              if (defined('COMMANDE_ADDON'))
 120              {
 121                  if (is_readable(DOL_DOCUMENT_ROOT .'/includes/modules/commande/'.COMMANDE_ADDON.'.php'))
 122                  {
 123                      require_once DOL_DOCUMENT_ROOT .'/includes/modules/commande/'.COMMANDE_ADDON.'.php';
 124  
 125                      // Definition du nom de module de numerotation de commande
 126  
 127                      // \todo  Normer le nom des classes des modules de numérotation de ref de commande avec un nom du type NumRefCommandesXxxx
 128                      //
 129                      //$list=split('_',COMMANDE_ADDON);
 130                      //$numrefname=$list[2];
 131                      //$modName = 'NumRefCommandes'.ucfirst($numrefname);
 132                      $modName=COMMANDE_ADDON;
 133  
 134                      // Recuperation de la nouvelle reference
 135                      $objMod = new $modName($this->db);
 136                      $soc = new Societe($this->db);
 137                      $soc->fetch($this->soc_id);
 138                      $num = $objMod->commande_get_num($soc);
 139  
 140                      $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='$num', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id";
 141                      $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
 142  
 143                      if ($this->db->query($sql) )
 144                      {
 145                          $result = 1;
 146                      }
 147                      else
 148                      {
 149                          $result = -1;
 150                          dolibarr_print_error($this->db);
 151                      }
 152  
 153                  }
 154                  else
 155                  {
 156                      print 'Impossible de lire le module de numérotation';
 157                  }
 158              }
 159              else
 160              {
 161                  print 'Le module de numérotation n\'est pas défini' ;
 162              }
 163          }
 164          return $result ;
 165      }
 166    /**
 167     *    \brief      Cloture la commande
 168     *    \param      user        Objet utilisateur qui cloture
 169     *    \return     int         <0 si ko, >0 si ok
 170     */
 171  	function cloture($user)
 172      {
 173          if ($user->rights->commande->valider)
 174          {
 175              $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
 176              $sql.= ' SET fk_statut = 3,';
 177              $sql.= ' fk_user_cloture = '.$user->id.',';
 178              $sql.= ' date_cloture = now()';
 179              $sql.= " WHERE rowid = $this->id AND fk_statut > 0 ;";
 180  
 181              if ($this->db->query($sql) )
 182              {
 183                  return 1;
 184              }
 185              else
 186              {
 187                  dolibarr_print_error($this->db);
 188                  return -1;
 189              }
 190          }
 191      }
 192    /**
 193     * Annule la commande
 194     *
 195     */
 196  	function cancel($user)
 197      {
 198          if ($user->rights->commande->valider)
 199          {
 200              $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET fk_statut = -1';
 201              $sql .= " WHERE rowid = $this->id AND fk_statut = 1 ;";
 202  
 203              if ($this->db->query($sql) )
 204              {
 205                  return 1;
 206              }
 207              else
 208              {
 209                  dolibarr_print_error($this->db);
 210              }
 211          }
 212      }
 213    /**
 214     * Créé la commande
 215     *
 216     */
 217  	function create($user)
 218      {
 219          /* On positionne en mode brouillon la commande */
 220          $this->brouillon = 1;
 221          if (! $remise)
 222          {
 223              $remise = 0 ;
 224          }
 225  
 226          if (! $this->projetid)
 227          {
 228              $this->projetid = 0;
 229          }
 230          $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande (fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note, ref_client) ';
 231          $sql .= ' VALUES ('.$this->soc_id.', now(), '.$user->id.', '.$this->projetid.', '.$this->db->idate($this->date_commande).', '.$this->source.', \''.$this->note.'\', \''.$this->ref_client.'\')';
 232  
 233          if ( $this->db->query($sql) )
 234          {
 235              $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande');
 236              /*
 237               *  Insertion des produits dans la base
 238               */
 239              for ($i = 0 ; $i < sizeof($this->products) ; $i++)
 240              {
 241                  $prod = new Product($this->db, $this->products[$i]);
 242                  if ($prod->fetch($this->products[$i]))
 243                  {
 244                      $this->insert_product_generic($prod->libelle,
 245                          $prod->price,
 246                          $this->products_qty[$i],
 247                          $prod->tva_tx,
 248                          $this->products[$i],
 249                          $this->products_remise_percent[$i]);
 250                  }
 251              }
 252              $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id;
 253              if ($this->db->query($sql))
 254              {
 255                  if ($this->id && $this->propale_id)
 256                  {
 257                      $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_pr (fk_commande, fk_propale) VALUES ('.$this->id.','.$this->propale_id.')';
 258                      $this->db->query($sql);
 259                  }
 260                  /*
 261                  * Produits
 262                  *
 263                  */
 264                  for ($i = 0 ; $i < sizeof($this->lines) ; $i++)
 265                  {
 266                      $result_insert = $this->insert_product_generic(
 267                           $this->lines[$i]->libelle,
 268                           $this->lines[$i]->price,
 269                           $this->lines[$i]->qty,
 270                           $this->lines[$i]->tva_tx,
 271                           $this->lines[$i]->product_id,
 272                           $this->lines[$i]->remise_percent);
 273                      if ( $result_insert < 0)
 274                      {
 275                          dolibarr_print_error($this->db);
 276                      }
 277                  }
 278                  return $this->id;
 279              }
 280              else
 281              {
 282                  return -1;
 283              }
 284          }
 285          else
 286          {
 287              dolibarr_print_error($this->db);
 288              return 0;
 289          }
 290      }
 291    /**
 292     * Ajoute un produit
 293     *
 294     */
 295  	function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0)
 296      {
 297          if ($this->statut == 0)
 298          {
 299              if (strlen(trim($p_qty)) == 0)
 300              {
 301                  $p_qty = 1;
 302              }
 303  
 304              $p_price = ereg_replace(',','.',$p_price);
 305  
 306              $price = $p_price;
 307              $subprice = $p_price;
 308              if ($remise_percent > 0)
 309              {
 310                  $remise = round(($p_price * $remise_percent / 100), 2);
 311                  $price = $p_price - $remise;
 312              }
 313  
 314              $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ';
 315              $sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','". $price."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '$subprice') ; ";
 316              if ($this->db->query($sql) )
 317              {
 318                  if ($this->update_price() > 0)
 319                  {
 320                      return 1;
 321                  }
 322                  else
 323                  {
 324                      return -1;
 325                  }
 326              }
 327              else
 328              {
 329                  dolibarr_print_error($this->db);
 330                  return -2;
 331              }
 332          }
 333      }
 334  
 335    /**
 336     * Ajoute une ligne de commande
 337     *
 338     */
 339  	function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0)
 340      {
 341          // Nettoyage parametres
 342          $qty = ereg_replace(',','.',$qty);
 343          $pu = ereg_replace(',','.',$pu);
 344          $desc=trim($desc);
 345          if (strlen(trim($qty))==0)
 346          {
 347              $qty=1;
 348          }
 349  
 350          // Verifs
 351          if (! $this->brouillon) return -1;
 352  
 353          $this->db->begin();
 354  
 355          if ($fk_product > 0)
 356          {
 357              $prod = new Product($this->db, $fk_product);
 358              if ($prod->fetch($fk_product) > 0)
 359              {
 360                  $desc  = $desc?$desc:$prod->libelle;
 361                  $pu    = $prod->price;
 362                  $txtva = $prod->tva_tx;
 363              }
 364          }
 365  
 366          $remise = 0;
 367          $price = round(ereg_replace(',','.',$pu), 2);
 368          $subprice = $price;
 369          if (trim(strlen($remise_percent)) > 0)
 370          {
 371              $remise = round(($pu * $remise_percent / 100), 2);
 372              $price = $pu - $remise;
 373          }
 374  
 375          $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise)';
 376          $sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."') ;";
 377  
 378          if ( $this->db->query( $sql) )
 379          {
 380              $this->update_price();
 381              $this->db->commit();
 382              return 1;
 383          }
 384          else
 385          {
 386              $this->error=$this->db->error();
 387              $this->db->rollback();
 388              return -1;
 389          }
 390      }
 391  
 392      /**
 393      * Ajoute un produit dans la commande
 394      *
 395      */
 396  	function add_product($idproduct, $qty, $remise_percent=0)
 397      {
 398          if ($idproduct > 0)
 399          {
 400              $i = sizeof($this->products);
 401              $this->products[$i] = $idproduct;
 402              if (!$qty)
 403              {
 404                  $qty = 1 ;
 405              }
 406              $this->products_qty[$i] = $qty;
 407              $this->products_remise_percent[$i] = $remise_percent;
 408          }
 409      }
 410  
 411      /**
 412      * Lit une commande
 413      *
 414      */
 415  	function fetch ($id)
 416      {
 417          $sql = 'SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva';
 418          $sql .= ', '.$this->db->pdate('c.date_commande').' as date_commande, c.fk_projet, c.remise_percent, c.source, c.facture, c.note, c.ref_client';
 419          $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
 420          $sql .= ' WHERE c.rowid = '.$id;
 421  
 422          $result = $this->db->query($sql) ;
 423          if ( $result )
 424          {
 425              $obj = $this->db->fetch_object();
 426              $this->id              = $obj->rowid;
 427              $this->ref             = $obj->ref;
 428              $this->ref_client      = $obj->ref_client;
 429              $this->soc_id          = $obj->fk_soc;
 430              $this->statut          = $obj->fk_statut;
 431              $this->user_author_id  = $obj->fk_user_author;
 432              $this->total_ht        = $obj->total_ht;
 433              $this->total_tva       = $obj->tva;
 434              $this->total_ttc       = $obj->total_ttc;
 435              $this->date            = $obj->date_commande;
 436              $this->remise_percent  = $obj->remise_percent;
 437              $this->source          = $obj->source;
 438              $this->facturee        = $obj->facture;
 439              $this->note            = $obj->note;
 440              $this->projet_id       = $obj->fk_projet;
 441              $this->db->free();
 442              if ($this->statut == 0)
 443                  $this->brouillon = 1;
 444              /*
 445              * Propale associée
 446              */
 447              $sql = 'SELECT fk_propale FROM '.MAIN_DB_PREFIX.'co_pr WHERE fk_commande = '.$this->id;
 448              if ($this->db->query($sql) )
 449              {
 450                  if ($this->db->num_rows())
 451                  {
 452                      $obj = $this->db->fetch_object();
 453                      $this->propale_id = $obj->fk_propale;
 454                  }
 455                  return 1;
 456              }
 457              else
 458              {
 459                  dolibarr_print_error($this->db);
 460                  return -1;
 461              }
 462          }
 463          else
 464          {
 465              dolibarr_print_error($this->db);
 466              return -1;
 467          }
 468      }
 469    /**
 470     *
 471     *
 472     */
 473  	function fetch_lignes($only_product=0)
 474      {
 475          $this->lignes = array();
 476          $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice';
 477          if ($only_product==1)
 478          {
 479              $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as l LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product) WHERE l.fk_commande = '.$this->id.' AND p.fk_product_type <> 1 ORDER BY l.rowid';
 480          }
 481          else
 482          {
 483              $sql .= ' FROM '.MAIN_DB_PREFIX."commandedet as l WHERE l.fk_commande = $this->id ORDER BY l.rowid";
 484          }
 485          $result = $this->db->query($sql);
 486          if ($result)
 487          {
 488              $num = $this->db->num_rows();
 489              $i = 0;
 490              while ($i < $num)
 491              {
 492                  $ligne = new CommandeLigne();
 493                  $objp = $this->db->fetch_object($result);
 494                  $ligne->id             = $objp->rowid;
 495                  $ligne->qty            = $objp->qty;
 496                  $ligne->price          = $objp->price;
 497                  $ligne->tva_tx         = $objp->tva_tx;
 498                  $ligne->subprice       = $objp->subprice;
 499                  $ligne->remise_percent = $objp->remise_percent;
 500                  $ligne->product_id     = $objp->fk_product;
 501                  $ligne->description    = stripslashes($objp->description);
 502                  $this->lignes[$i] = $ligne;
 503                  $i++;
 504              }
 505              $this->db->free();
 506          }
 507          return $this->lignes;
 508      }
 509  
 510      /**
 511       *      \brief      Renvoie un tableau avec les livraison par ligne
 512       *      \param      filtre_statut       Filtre sur statut
 513       *      \return     int                 0 si OK, <0 si KO
 514       */
 515  	function livraison_array($filtre_statut=-1)
 516      {
 517          $this->livraisons = array();
 518          $sql = 'SELECT fk_product, sum(ed.qty)';
 519          $sql.=' FROM '.MAIN_DB_PREFIX.'expeditiondet as ed, '.MAIN_DB_PREFIX.'expedition as e, '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'commandedet as cd';
 520          $sql.=' WHERE ed.fk_expedition = e.rowid AND ed.fk_commande_ligne = cd .rowid AND cd.fk_commande = c.rowid';
 521          $sql.=' AND cd.fk_commande =' .$this->id;
 522          if ($filtre_statut >= 0) $sql.=' AND e.fk_statut = '.$filtre_statut;
 523          $sql .= ' GROUP BY fk_product ';
 524          $result = $this->db->query($sql);
 525          if ($result)
 526          {
 527              $num = $this->db->num_rows();
 528              $i = 0;
 529              while ($i < $num)
 530              {
 531                  $row = $this->db->fetch_row( $i);
 532                  $this->livraisons[$row[0]] = $row[1];
 533                  $i++;
 534              }
 535              $this->db->free();
 536          }
 537      
 538          return 0;
 539      }
 540      
 541    /**
 542     * Renvoie un tableau avec les livraison par ligne
 543     *
 544     */
 545  	function nb_expedition()
 546      {
 547          $sql = 'SELECT count(*) FROM '.MAIN_DB_PREFIX.'expedition as e';
 548          $sql .=" WHERE e.fk_commande = $this->id";
 549  
 550          $result = $this->db->query($sql);
 551          if ($result)
 552          {
 553              $row = $this->db->fetch_row(0);
 554              return $row[0];
 555          }
 556      }
 557    /**
 558     *    \brief      Supprime une ligne de la commande
 559     *    \param      idligne     Id de la ligne à supprimer
 560     *    \return     int         >0 si ok, <0 si ko
 561     */
 562  	function delete_line($idligne)
 563      {
 564          if ($this->statut == 0)
 565          {
 566              $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE rowid = $idligne";
 567              if ($this->db->query($sql) )
 568              {
 569                  $this->update_price();
 570                  return 1;
 571              }
 572              else
 573              {
 574                  return -1;
 575              }
 576          }
 577      }
 578  
 579    /**
 580     *
 581     *
 582     */
 583  	function set_remise($user, $remise)
 584      {
 585          if ($user->rights->commande->creer)
 586          {
 587              $remise = ereg_replace(',','.',$remise);
 588              $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET remise_percent = '.$remise;
 589              $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
 590              if ($this->db->query($sql) )
 591              {
 592                  $this->remise_percent = $remise;
 593                  $this->update_price();
 594                  return 1;
 595              }
 596              else
 597              {
 598                  dolibarr_syslog('Commande::set_remise Erreur SQL');
 599              }
 600          }
 601      }
 602  
 603      /**
 604       *      \brief      Positionne numero reference commande client
 605       *      \param      user            Utilisateur qui modifie
 606       *      \param      ref_client      Reference commande client
 607       *      \return     int             <0 si ko, >0 si ok
 608       */
 609  	function set_ref_client($user, $ref_client)
 610      {
 611          if ($user->rights->commande->creer)
 612          {
 613              dolibarr_syslog('Commande::set_ref_client this->id='.$this->id.', ref_client='.$ref_client);
 614  
 615              $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET ref_client = '.(empty($ref_client) ? 'NULL' : '\''.addslashes($ref_client).'\'');
 616              $sql.= ' WHERE rowid = '.$this->id;
 617              if ($this->db->query($sql) )
 618              {
 619                  $this->ref_client = $ref_client;
 620                  return 1;
 621              }
 622              else
 623              {
 624                  $this->error=$this->db->error();
 625                  dolibarr_syslog('Commande::set_ref_client Erreur '.$this->error.' - '.$sql);
 626                  return -2;
 627              }
 628          }
 629          else
 630          {
 631              return -1;
 632          }
 633      }
 634  
 635      /**
 636       *
 637       *
 638       */
 639  	function set_note($user, $note)
 640      {
 641          if ($user->rights->commande->creer)
 642          {
 643              $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET note = '".addslashes($note)."'";
 644              $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
 645              if ($this->db->query($sql))
 646              {
 647                  $this->note = $note;
 648                  return 1;
 649              }
 650              else
 651              {
 652                  dolibarr_print_error($this->db);
 653                  return 0;
 654              }
 655          }
 656          else
 657          {
 658              return 0;
 659          }
 660      }
 661    /**
 662     *        \brief      Classe la facture comme facturée
 663     *        \return     int     <0 si ko, >0 si ok
 664     */
 665  	function classer_facturee()
 666      {
 667          $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET facture = 1';
 668          $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > 0 ;';
 669          if ($this->db->query($sql) )
 670          {
 671              return 1;
 672          }
 673          else
 674          {
 675              dolibarr_print_error($this->db);
 676          }
 677      }
 678  
 679    /**
 680     * Mettre à jour le prix
 681     *
 682     */
 683  	function update_price()
 684      {
 685          include_once  DOL_DOCUMENT_ROOT . '/lib/price.lib.php';
 686          /*
 687          *  Liste des produits a ajouter
 688          */
 689          $sql = 'SELECT price, qty, tva_tx FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id";
 690          if ( $this->db->query($sql) )
 691          {
 692              $num = $this->db->num_rows();
 693              $i = 0;
 694              while ($i < $num)
 695              {
 696                  $obj = $this->db->fetch_object();
 697                  $products[$i][0] = $obj->price;
 698                  $products[$i][1] = $obj->qty;
 699                  $products[$i][2] = $obj->tva_tx;
 700                  $i++;
 701              }
 702          }
 703          $calculs = calcul_price($products, $this->remise_percent);
 704  
 705          $totalht = $calculs[0];
 706          $totaltva = $calculs[1];
 707          $totalttc = $calculs[2];
 708          $total_remise = $calculs[3];
 709  
 710          $this->remise         = $total_remise;
 711          $this->total_ht       = $totalht;
 712          $this->total_tva      = $totaltva;
 713          $this->total_ttc      = $totalttc;
 714        /*
 715         *
 716         */
 717          $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande set';
 718          $sql .= "  amount_ht ='".ereg_replace(',','.',$totalht)."'";
 719          $sql .= ", total_ht  ='".ereg_replace(',','.',$totalht)."'";
 720          $sql .= ", tva       ='".ereg_replace(',','.',$totaltva)."'";
 721          $sql .= ", total_ttc ='".ereg_replace(',','.',$totalttc)."'";
 722          $sql .= ", remise    ='".ereg_replace(',','.',$total_remise)."'";
 723          $sql .= " WHERE rowid = $this->id";
 724          if ( $this->db->query($sql) )
 725          {
 726              return 1;
 727          }
 728          else
 729          {
 730              print 'Erreur mise à jour du prix<p>'.$sql;
 731              return -1;
 732          }
 733      }
 734      /**
 735       *      \brief     Mets à jour une ligne de commande
 736       *      \param     rowid            Id de la ligne de facture
 737       *      \param     desc             Description de la ligne
 738       *      \param     pu               Prix unitaire
 739       *      \param     qty              Quantité
 740       *      \param     remise_percent   Pourcentage de remise de la ligne
 741       *      \param     tva_tx           Taux TVA
 742       *      \return    int              < 0 si erreur, > 0 si ok
 743       */
 744  	function update_line($rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx)
 745      {
 746          dolibarr_syslog("Commande::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx");
 747          if ($this->brouillon)
 748          {
 749              $this->db->begin();
 750              
 751              // Nettoyage paramètres
 752              $pu=price2num($pu);
 753              if (strlen(trim($qty))==0) $qty=1;
 754              $remise = 0;
 755              $price = $pu;
 756              $subprice = $price;
 757              $remise_percent=trim($remise_percent);
 758              if ($remise_percent > 0)
 759              {
 760                  $remise = round(($pu * $remise_percent / 100), 2);
 761                  $price = $pu - $remise;
 762              }
 763              else
 764              {
 765                  $remise_percent=0;
 766              }
 767  
 768              $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet';
 769              $sql.= " SET description='".addslashes($desc)."',price='$price',subprice='$subprice',";
 770              $sql.= " remise=$remise,remise_percent=$remise_percent,qty=$qty,tva_tx='".$tva_tx."'";
 771              $sql.= ' WHERE rowid = '.$rowid;
 772  
 773              $result=$this->db->query( $sql);
 774              if ( $result )
 775              {
 776                  $this->update_price($this->id);
 777                  $this->db->commit();
 778                 return $result;
 779              }
 780              else
 781              {
 782                  $this->db->rollback();
 783                  dolibarr_print_error($this->db);
 784                  return -1;
 785              }
 786          }
 787          else
 788          {
 789              return -2;
 790          }
 791      }
 792  
 793      /**
 794      * Supprime la commande
 795      *
 796      */
 797  	function delete()
 798      {
 799          $err = 0;
 800          $this->db->begin();
 801          $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id ;";
 802          if (! $this->db->query($sql) )
 803          {
 804              $err++;
 805          }
 806  
 807          $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = $this->id;";
 808          if (! $this->db->query($sql) )
 809          {
 810              $err++;
 811          }
 812  
 813          $sql = 'DELETE FROM '.MAIN_DB_PREFIX."co_pr WHERE fk_commande = $this->id;";
 814          if (! $this->db->query($sql) )
 815          {
 816              $err++;
 817          }
 818  
 819          if ($err == 0)
 820          {
 821              $this->db->commit();
 822              return 1;
 823          }
 824          else
 825          {
 826              $this->db->rollback();
 827              return -1;
 828          }
 829      }
 830    /**
 831     *        \brief      Classer la commande dans un projet
 832     *        \param      cat_id      Id du projet
 833     */
 834  	function classin($cat_id)
 835      {
 836          $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET fk_projet = $cat_id";
 837          $sql .= " WHERE rowid = $this->id;";
 838  
 839          if ($this->db->query($sql) )
 840          {
 841              return 1;
 842          }
 843          else
 844          {
 845              $this->error=$this->db->error();
 846              return -1;
 847          }
 848      }
 849  
 850      /**
 851       *      \brief          Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord
 852       *      \param          user    Objet user
 853       *      \return         int     <0 si ko, >0 si ok
 854       */
 855  	function load_board($user)
 856      {
 857          global $conf;
 858  
 859          $this->nbtodo=$this->nbtodolate=0;
 860          $sql = 'SELECT c.rowid,'.$this->db->pdate('c.date_creation').' as datec';
 861          $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c';
 862          $sql.= ' WHERE c.fk_statut BETWEEN 1 AND 2';
 863          if ($user->societe_id) $sql.=' AND fk_soc = '.$user->societe_id;
 864          $resql=$this->db->query($sql);
 865          if ($resql)
 866          {
 867              while ($obj=$this->db->fetch_object($resql))
 868              {
 869                  $this->nbtodo++;
 870                  if ($obj->datec < (time() - $conf->commande->traitement->warning_delay)) $this->nbtodolate++;
 871              }
 872              return 1;
 873          }
 874          else
 875          {
 876               $this->error=$this->db->error();
 877              return -1;
 878          }
 879      }
 880  
 881  
 882    /**
 883     *    \brief      Retourne le libellé du statut de la commande
 884     *    \return     string      Libellé
 885     */
 886  	function getLibStatut()
 887      {
 888          return $this->LibStatut($this->statut);
 889      }
 890  
 891    /**
 892     *    \brief      Renvoi le libellé d'un statut donné
 893     *    \param      statut      id statut
 894     *    \return     string      Libellé
 895     */
 896  	function LibStatut($statut)
 897      {
 898          return $this->status_label_short[$statut];
 899      }
 900  
 901      /**
 902       *      \brief     Charge les informations d'ordre info dans l'objet commande
 903       *      \param     id       Id de la commande a charger
 904       */
 905  	function info($id)
 906      {
 907          $sql = 'SELECT c.rowid, '.$this->db->pdate('date_creation').' as datec,';
 908          $sql.= ' '.$this->db->pdate('date_valid').' as datev,';
 909          $sql.= ' '.$this->db->pdate('date_cloture').' as datecloture,';
 910          $sql.= ' fk_user_author, fk_user_valid, fk_user_cloture';
 911          $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c';
 912          $sql.= ' WHERE c.rowid = '.$id;
 913          $result=$this->db->query($sql);
 914          if ($result)
 915          {
 916              if ($this->db->num_rows($result))
 917              {
 918                  $obj = $this->db->fetch_object($result);
 919                  $this->id = $obj->rowid;
 920                  if ($obj->fk_user_author)
 921                  {
 922                      $cuser = new User($this->db, $obj->fk_user_author);
 923                      $cuser->fetch();
 924                      $this->user_creation   = $cuser;
 925                  }
 926  
 927                  if ($obj->fk_user_valid)
 928                  {
 929                      $vuser = new User($this->db, $obj->fk_user_valid);
 930                      $vuser->fetch();
 931                      $this->user_validation = $vuser;
 932                  }
 933  
 934                  if ($obj->fk_user_cloture)
 935                  {
 936                      $cluser = new User($this->db, $obj->fk_user_cloture);
 937                      $cluser->fetch();
 938                      $this->user_cloture   = $cluser;
 939                  }
 940  
 941                  $this->date_creation     = $obj->datec;
 942                  $this->date_validation   = $obj->datev;
 943                  $this->date_cloture      = $obj->datecloture;
 944              }
 945  
 946              $this->db->free($result);
 947  
 948          }
 949          else
 950          {
 951              dolibarr_print_error($this->db);
 952          }
 953      }
 954  
 955  }
 956  
 957  
 958  
 959  /**
 960          \class      CommandeLigne
 961          \brief      Classe de gestion des lignes de commande
 962  */
 963  
 964  class CommandeLigne
 965  {
 966  	function CommandeLigne()
 967      {
 968      }
 969  }
 970  
 971  ?>


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