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

   1  <?php
   2  /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   *
   4   * This program is free software; you can redistribute it and/or modify
   5   * it under the terms of the GNU General Public License as published by
   6   * the Free Software Foundation; either version 2 of the License, or
   7   * (at your option) any later version.
   8   *
   9   * This program is distributed in the hope that it will be useful,
  10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12   * GNU General Public License for more details.
  13   *
  14   * You should have received a copy of the GNU General Public License
  15   * along with this program; if not, write to the Free Software
  16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17   *
  18   * $Id: expedition.class.php,v 1.20 2005/09/21 23:43:08 eldy Exp $
  19   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/expedition/expedition.class.php,v $
  20   *
  21   */
  22  
  23  /**
  24          \file       htdocs/expedition/expedition.class.php
  25          \ingroup    expedition
  26          \brief      Fichier de la classe de gestion des expeditions
  27          \version    $Revision: 1.20 $
  28  */
  29  
  30  
  31  /** 
  32          \class      Expedition
  33          \brief      Classe de gestion des expeditions
  34  */
  35  class Expedition 
  36  {
  37    var $db ;
  38    var $id ;
  39    var $brouillon;
  40    var $entrepot_id;
  41  
  42    /**
  43     * Initialisation
  44     *
  45     */
  46    function Expedition($DB)
  47      {
  48        $this->db = $DB;
  49        $this->lignes = array();
  50  
  51        $this->sources[0] = "Proposition commerciale";
  52        $this->sources[1] = "Internet";
  53        $this->sources[2] = "Courrier";
  54        $this->sources[3] = "Téléphone";
  55        $this->sources[4] = "Fax";
  56  
  57        $this->statuts[-1] = "Annulée";
  58        $this->statuts[0] = "Brouillon";
  59        $this->statuts[1] = "Validée";
  60  
  61        $this->products = array();
  62      }
  63  
  64    /**
  65     *    \brief      Créé expédition en base
  66     *    \param      user        Objet du user qui cré
  67     *    \return     int         <0 si erreur, id expédition créée si ok
  68     */
  69    function create($user)
  70      {
  71          require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php";
  72          $error = 0;
  73          /* On positionne en mode brouillon la commande */
  74          $this->brouillon = 1;
  75      
  76          $this->user = $user;
  77      
  78          $this->db->begin();
  79      
  80          $sql = "INSERT INTO ".MAIN_DB_PREFIX."expedition (date_creation, fk_user_author, date_expedition, fk_commande";
  81          if ($this->entrepot_id) $sql.= ", fk_entrepot";
  82          $sql.= ")";
  83          $sql.= " VALUES (now(), $user->id, ".$this->db->idate($this->date_expedition).",$this->commande_id";
  84          if ($this->entrepot_id) $sql.= ", $this->entrepot_id";
  85          $sql.= ")";
  86      
  87          $resql=$this->db->query($sql);
  88          if ($resql)
  89          {
  90              $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expedition");
  91      
  92              $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id;
  93              if ($this->db->query($sql))
  94              {
  95      
  96                  $this->commande = new Commande($this->db);
  97                  $this->commande->id = $this->commande_id;
  98                  $this->commande->fetch_lignes();
  99      
 100                  /*
 101                  *  Insertion des produits dans la base
 102                  */
 103                  for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
 104                  {
 105                      //TODO
 106                      if (! $this->create_line(0, $this->lignes[$i]->commande_ligne_id, $this->lignes[$i]->qty))
 107                      {
 108                          $error++;
 109                      }
 110                  }
 111  
 112                  /*
 113                   *
 114                   */
 115                  $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET fk_statut = 2 WHERE rowid=".$this->commande_id;
 116                  if (! $this->db->query($sql))
 117                  {
 118                      $error++;
 119                  }
 120          
 121                  if ($error==0)
 122                  {
 123                      $this->db->commit();
 124                      return $this->id;
 125                  }
 126                  else
 127                  {
 128                      $error++;
 129                      $this->error=$this->db->error()." - sql=$sql";
 130                      $this->db->rollback();
 131                      return -3;
 132                  }
 133              }
 134              else
 135              {
 136                  $error++;
 137                  $this->error=$this->db->error()." - sql=$sql";
 138                  $this->db->rollback();
 139                  return -2;
 140              }
 141          }
 142          else
 143          {
 144              $error++;
 145              $this->error=$this->db->error()." - sql=$sql";
 146              $this->db->rollback();
 147              return -1;
 148          }
 149      }
 150  
 151    /**
 152     *
 153     *
 154     */
 155    function create_line($transaction, $commande_ligne_id, $qty)
 156    {
 157      $error = 0;
 158  
 159      $idprod = 0;
 160      $j = 0;
 161      while (($j < sizeof($this->commande->lignes)) && idprod == 0)
 162        {
 163      if ($this->commande->lignes[$j]->id == $commande_ligne_id)
 164        {
 165          $idprod = $this->commande->lignes[$j]->product_id;
 166        }
 167      $j++;
 168        }
 169  
 170      $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (fk_expedition, fk_commande_ligne, qty)";
 171      $sql .= " VALUES ($this->id,".$commande_ligne_id.",".$qty.")";
 172      
 173      if (! $this->db->query($sql) )
 174        {
 175      $error++;
 176        }
 177  
 178      if ($error == 0 )
 179        {
 180      return 1;
 181        }
 182    }
 183    /** 
 184     *
 185     * Lit une commande
 186     *
 187     */
 188      function fetch ($id)
 189      {
 190          global $conf;
 191      
 192          $sql = "SELECT e.rowid, e.date_creation, e.ref, e.fk_user_author, e.fk_statut, e.fk_commande, e.fk_entrepot";
 193          $sql .= ", ".$this->db->pdate("e.date_expedition")." as date_expedition ";
 194          $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
 195          $sql .= " WHERE e.rowid = $id";
 196      
 197          $result = $this->db->query($sql) ;
 198      
 199          if ( $result )
 200          {
 201              $obj = $this->db->fetch_object($result);
 202      
 203              $this->id              = $obj->rowid;
 204              $this->ref             = $obj->ref;
 205              $this->statut          = $obj->fk_statut;
 206              $this->commande_id     = $obj->fk_commande;
 207              $this->user_author_id  = $obj->fk_user_author;
 208              $this->date            = $obj->date_expedition;
 209              $this->entrepot_id     = $obj->fk_entrepot;
 210              $this->db->free();
 211      
 212              if ($this->statut == 0) $this->brouillon = 1;
 213      
 214              $file = $conf->expedition->dir_output . "/" .get_exdir($expedition->id) . "/" . $this->id.".pdf";
 215              $this->pdf_filename = $file;
 216      
 217              return 1;
 218          }
 219          else
 220          {
 221              $this->error=$this->db->error();
 222              return -1;
 223          }
 224      }
 225  
 226    /**
 227     *        \brief      Valide l'expedition, et met a jour le stock si stock géré
 228     *        \param      user        Objet de l'utilisateur qui valide
 229     *        \return     int
 230     */
 231      function valid($user)
 232      {
 233          global $conf;
 234          
 235          require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php";
 236      
 237          dolibarr_syslog("expedition.class.php::valid");
 238  
 239          $this->db->begin();
 240          
 241          $error = 0;
 242          
 243          if ($user->rights->expedition->valider)
 244          {
 245              $this->ref = "EXP".$this->id;
 246      
 247              // \todo Tester si non dejà au statut validé. Si oui, on arrete afin d'éviter
 248              //       de décrémenter 2 fois le stock.
 249  
 250              $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='".$this->ref."', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id";
 251              $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
 252      
 253              if ($this->db->query($sql) )
 254              {
 255                  // Si module stock géré et que expedition faite depuis un entrepot
 256                  if ($conf->stock->enabled && $this->entrepot_id)
 257                  {
 258                      /*
 259                       * Enregistrement d'un mouvement de stock pour chaque produit de l'expedition
 260                       */
 261  
 262                      dolibarr_syslog("expedition.class.php::valid enregistrement des mouvements");
 263  
 264                      $sql = "SELECT cd.fk_product, ed.qty ";
 265                      $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."expeditiondet as ed";
 266                      $sql.= " WHERE ed.fk_expedition = $this->id AND cd.rowid = ed.fk_commande_ligne";
 267          
 268                      $resql=$this->db->query($sql);
 269                      if ($resql)
 270                      {
 271                          $num = $this->db->num_rows($resql);
 272                          $i=0;
 273                          while($i < $num)
 274                          {
 275                              dolibarr_syslog("expedition.class.php::valid movment $i");
 276  
 277                              $obj = $this->db->fetch_object($resql);
 278  
 279                              $mouvS = new MouvementStock($this->db);
 280                              $result=$mouvS->livraison($user, $obj->fk_product, $this->entrepot_id, $obj->qty);
 281                              if ($result < 0)
 282                              {
 283                                  $this->db->rollback();
 284                                  $this->error=$this->db->error()." - sql=$sql";
 285                                  dolibarr_syslog("expedition.class.php::valid ".$this->error);
 286                                  return -3;
 287                              }
 288                              $i++;
 289                          }
 290                          
 291                      }
 292                      else
 293                      {
 294                          $this->db->rollback();
 295                          $this->error=$this->db->error()." - sql=$sql";
 296                          dolibarr_syslog("expedition.class.php::valid ".$this->error);
 297                          return -2;
 298                      }
 299                  }
 300              }
 301              else
 302              {
 303                  $this->db->rollback();
 304                  $this->error=$this->db->error()." - sql=$sql";
 305                  dolibarr_syslog("expedition.class.php::valid ".$this->error);
 306                  return -1;
 307              }
 308          }
 309          else
 310          {
 311              $this->error="Non autorise";
 312              dolibarr_syslog("expedition.class.php::valid ".$this->error);
 313              return -1;
 314          }
 315  
 316          $this->db->commit();
 317          //dolibarr_syslog("expedition.class.php::valid commit");
 318          return 1;
 319      }
 320  
 321  
 322    /**
 323     * Ajoute un produit
 324     *
 325     */
 326    function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0)
 327      {
 328        if ($this->statut == 0)
 329      {
 330        if (strlen(trim($p_qty)) == 0)
 331          {
 332            $p_qty = 1;
 333          }
 334  
 335        $p_price = ereg_replace(",",".",$p_price);
 336  
 337        $price = $p_price;
 338        $subprice = $p_price;
 339        if ($remise_percent > 0)
 340          {
 341            $remise = round(($p_price * $remise_percent / 100), 2);
 342            $price = $p_price - $remise;
 343          }
 344  
 345        $sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
 346        $sql .= " (".$this->id.", $p_product_id,". $p_qty.",". $price.",".$p_tva_tx.",'". addslashes($p_desc) ."',$remise_percent, $subprice) ; ";
 347        
 348        if ($this->db->query($sql) )
 349          {
 350            
 351            if ($this->update_price() > 0)
 352          {          
 353            return 1;
 354          }
 355            else
 356          {
 357            return -1;
 358          }
 359          }
 360        else
 361          {
 362            print $this->db->error();
 363            print "<br>".$sql;
 364            return -2;
 365          }
 366      }
 367      }
 368    /**
 369     * Ajoute une ligne
 370     *
 371     */
 372    function addline( $id, $qty )
 373      {
 374        $num = sizeof($this->lignes);
 375        $ligne = new ExpeditionLigne();
 376  
 377        $ligne->commande_ligne_id = $id;
 378        $ligne->qty = $qty;
 379  
 380        $this->lignes[$num] = $ligne;
 381      }
 382  
 383    /** 
 384     *
 385     *
 386     */
 387    function delete_line($idligne)
 388      {
 389        if ($this->statut == 0)
 390      {
 391        $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet WHERE rowid = $idligne";
 392        
 393        if ($this->db->query($sql) )
 394          {
 395            $this->update_price();
 396            
 397            return 1;
 398          }
 399        else
 400          {
 401            return 0;
 402          }
 403      }
 404      }
 405    /**
 406     * Supprime la fiche
 407     *
 408     */
 409    function delete()
 410    {
 411      $this->db->begin();
 412  
 413      $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition = $this->id ;";
 414      if ( $this->db->query($sql) ) 
 415        {
 416      $sql = "DELETE FROM ".MAIN_DB_PREFIX."expedition WHERE rowid = $this->id;";
 417      if ( $this->db->query($sql) ) 
 418        {
 419          $this->db->commit();
 420          return 1;
 421        }
 422      else
 423        {
 424          $this->db->rollback();
 425          return -2;
 426        }
 427        }
 428      else
 429        {
 430      $this->db->rollback();
 431      return -1;
 432        }
 433    }
 434    /**
 435     * Classe la commande
 436     *
 437     *
 438     */
 439    function classin($cat_id)
 440      {
 441        $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET fk_projet = $cat_id";
 442        $sql .= " WHERE rowid = $this->id;";
 443        
 444        if ($this->db->query($sql) )
 445      {
 446        return 1;
 447      }
 448        else
 449      {
 450        print $this->db->error() . ' in ' . $sql;
 451      }
 452      }
 453  
 454      /**
 455       * Genere le pdf
 456       */
 457      function PdfWrite()
 458      {
 459          global $conf;
 460      
 461          //EXPEDITION_ADDON_PDF
 462          if (defined("EXPEDITION_ADDON_PDF") && strlen(EXPEDITION_ADDON_PDF) > 0)
 463          {
 464              $module_file_name = DOL_DOCUMENT_ROOT."/expedition/mods/pdf/pdf_expedition_".EXPEDITION_ADDON_PDF.".modules.php";
 465      
 466              $mod = "pdf_expedition_".EXPEDITION_ADDON_PDF;
 467              $this->fetch_commande();
 468      
 469              require_once($module_file_name);
 470      
 471              $pdf = new $mod($this->db);
 472      
 473              $dir = $conf->expedition->dir_output . "/" .get_exdir($this->id);
 474      
 475              if (! file_exists($dir))
 476              {
 477                  create_exdir($dir);
 478              }
 479      
 480              $file = $dir . $this->id . ".pdf";
 481      
 482              if (file_exists($dir))
 483              {
 484                  $pdf->generate($this, $file);
 485              }
 486          }
 487      }
 488  
 489    /*
 490     * Lit la commande associée
 491     *
 492     */
 493    function fetch_commande()
 494    {
 495      $this->commande =& new Commande($this->db);
 496      $this->commande->fetch($this->commande_id);
 497    }
 498  
 499  
 500    function fetch_lignes()
 501    {
 502      $this->lignes = array();
 503  
 504      $sql = "SELECT c.description, c.qty as qtycom, e.qty as qtyexp";    
 505      $sql .= ", c.fk_product";
 506      $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as e";
 507      $sql .= " , ".MAIN_DB_PREFIX."commandedet as c";
 508  
 509      $sql .= " WHERE e.fk_expedition = ".$this->id;
 510      $sql .= " AND e.fk_commande_ligne = c.rowid";
 511  
 512  
 513      $resql = $this->db->query($sql);
 514      if ($resql)
 515        {
 516      $num = $this->db->num_rows($resql);
 517      $i = 0;
 518      while ($i < $num)
 519        {
 520          $ligne = new ExpeditionLigne();
 521  
 522          $obj = $this->db->fetch_object($resql);
 523  
 524          $ligne->product_id     = $obj->fk_product;
 525          $ligne->qty_commande   = $obj->qtycom;
 526          $ligne->qty_expedition = $obj->qtyexp;
 527          $ligne->description    = stripslashes($obj->description);        
 528  
 529          $this->lignes[$i] = $ligne;        
 530          $i++;
 531        }          
 532      $this->db->free($resql);
 533        }
 534  
 535      return $this->lignes;
 536    }
 537  
 538  }
 539  
 540  
 541  class ExpeditionLigne
 542  {
 543  
 544  }
 545  
 546  ?>


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