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

   1  <?php
   2  /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   * Copyright (C) 2004      Éric Seigne          <eric.seigne@ryxeo.com>
   4   * Copyright (C) 2004-2005 Laurent Destailleur  <eldy@users.sourceforge.net>
   5   * Copyright (C) 2005      Marc Barilley / Ocebo <marc@ocebo.com>
   6   *
   7   * This program is free software; you can redistribute it and/or modify
   8   * it under the terms of the GNU General Public License as published by
   9   * the Free Software Foundation; either version 2 of the License, or
  10   * (at your option) any later version.
  11   *
  12   * This program is distributed in the hope that it will be useful,
  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15   * GNU General Public License for more details.
  16   *
  17   * You should have received a copy of the GNU General Public License
  18   * along with this program; if not, write to the Free Software
  19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20   *
  21   * $Id: propal.class.php,v 1.67 2005/12/16 23:47:07 eldy Exp $
  22   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/propal.class.php,v $
  23   */
  24  
  25  /**
  26          \file       htdocs/propal.class.php
  27          \brief      Fichier de la classe des propales
  28          \author     Rodolphe Qiedeville
  29          \author        Eric Seigne
  30          \author        Laurent Destailleur
  31          \version    $Revision: 1.67 $
  32  */
  33  
  34  require_once (DOL_DOCUMENT_ROOT ."/product.class.php");
  35  
  36  
  37  /**
  38          \class      Propal
  39          \brief      Classe permettant la gestion des propales
  40  */
  41  
  42  class Propal
  43  {
  44      var $id;
  45      var $db;
  46      var $socidp;
  47      var $contactid;
  48      var $projetidp;
  49      var $author;
  50      var $ref;
  51      var $datep;
  52      var $remise;
  53      var $products;
  54      var $products_qty;
  55      var $note;
  56      var $price;
  57      var $status;
  58      var $fin_validite;
  59      
  60      var $labelstatut=array();
  61      var $labelstatut_short=array();
  62      
  63      var $product=array();
  64  
  65      // Pour board
  66      var $nbtodo;
  67      var $nbtodolate;
  68  
  69  
  70      /** 
  71       *        \brief      Constructeur
  72       *      \param      DB          Handler d'accès base
  73       *      \param      soc_idp     Id de la société
  74       *      \param      propalid    Id de la propal
  75       */
  76      function Propal($DB, $soc_idp="", $propalid=0)
  77      {
  78        global $langs;
  79        
  80        $this->db = $DB ;
  81        $this->socidp = $soc_idp;
  82        $this->id = $propalid;
  83        $this->products = array();
  84        $this->remise = 0;
  85        
  86        $langs->load("propals");
  87        $this->labelstatut[0]=$langs->trans("PropalStatusDraft");
  88        $this->labelstatut[1]=$langs->trans("PropalStatusValidated");
  89        $this->labelstatut[2]=$langs->trans("PropalStatusSigned");
  90        $this->labelstatut[3]=$langs->trans("PropalStatusNotSigned");
  91        $this->labelstatut[4]=$langs->trans("PropalStatusBilled");
  92        $this->labelstatut_short[0]=$langs->trans("PropalStatusDraftShort");
  93        $this->labelstatut_short[1]=$langs->trans("Opened");
  94        $this->labelstatut_short[2]=$langs->trans("PropalStatusSignedShort");
  95        $this->labelstatut_short[3]=$langs->trans("PropalStatusNotSignedShort");
  96        $this->labelstatut_short[4]=$langs->trans("PropalStatusBilledShort");
  97      }
  98  
  99  
 100    /**
 101     * \brief     Ajout d'un produit dans la proposition, en memoire dans l'objet
 102     * \param     idproduct       id du produit à ajouter
 103     * \param     qty             quantité
 104     * \param     remise_percent  remise effectuée sur le produit
 105     * \return    void
 106     * \see       insert_product
 107     */
 108       
 109      function add_product($idproduct, $qty, $remise_percent=0)
 110      {
 111          if ($idproduct > 0)
 112          {
 113              $i = sizeof($this->products);
 114              $this->products[$i] = $idproduct;
 115              if (!$qty)
 116              {
 117                  $qty = 1 ;
 118              }
 119              $this->products_qty[$i] = $qty;
 120              $this->products_remise_percent[$i] = $remise_percent;
 121          }
 122      }
 123  
 124      /**
 125       *    \brief     Ajout d'un produit dans la proposition, en base
 126       *    \param     idproduct           Id du produit à ajouter
 127       *    \param     qty                 Quantité
 128       *    \param     remise_percent      Remise effectuée sur le produit
 129       *    \param     p_desc              Descriptif optionnel
 130       *    \return    int                 >0 si ok, <0 si ko
 131       *    \see       add_product
 132       */
 133      function insert_product($idproduct, $qty, $remise_percent=0, $p_desc='')
 134      {
 135          dolibarr_syslog("propal.class.php::insert_product $idproduct, $qty, $remise_percent, $p_desc");
 136          if ($this->statut == 0)
 137          {
 138              // Nettoyage parametres
 139              $remise_percent=price2num($remise_percent);
 140              $qty=price2num($qty);
 141              
 142              if ($idproduct)
 143              {
 144                  $prod = new Product($this->db, $idproduct);
 145                  if ($prod->fetch($idproduct) > 0)
 146                  {
 147                      $txtva = $prod->tva_tx;
 148                      $price = price2num($prod->price);
 149                      $subprice = price2num($prod->price);
 150                      
 151                      // Calcul remise et nouveau prix
 152                      $remise = 0;
 153                      if ($remise_percent > 0)
 154                      {
 155                          $remise = round(($prod->price * $remise_percent / 100), 2);
 156                          $price = $prod->price - $remise;
 157                      }
 158          
 159                      $sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
 160                      $sql .= " (".$this->id.",". $idproduct.",'". $qty."','". $price."','".$txtva."','".addslashes($p_desc?$p_desc:$prod->label)."','".ereg_replace(",",".",$remise_percent)."','".ereg_replace(",",".",$subprice)."')";
 161          
 162                      if ($this->db->query($sql) )
 163                      {
 164                          $this->update_price();
 165                          return 1;
 166                      }
 167                      else
 168                      {
 169                          $this->error=$this->db->error();
 170                          return -1;
 171                      }
 172                  }
 173                  else
 174                  {
 175                      $this->error=$this->db->error();
 176                      return -2;
 177                  }
 178              }
 179          }
 180          else
 181          {
 182              return -3;
 183          }
 184      }
 185  
 186  
 187      /**
 188       *    \brief     Ajout d'un produit dans la proposition, en base
 189       *    \param     p_desc             Descriptif optionnel
 190       *    \param     p_price            Prix
 191       *    \param     p_qty              Quantité
 192       *    \param     p_tva_tx           Taux tva
 193       *    \param     remise_percent     Remise effectuée sur le produit
 194       *    \return    int                >0 si ok, <0 si ko
 195       *    \see       add_product
 196       */
 197      function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx, $remise_percent=0)
 198      {
 199          dolibarr_syslog("propal.class.php::insert_product_generic $p_desc, $p_price, $p_qty, $p_tva_tx, $remise_percent");
 200          if ($this->statut == 0)
 201          {
 202              // Nettoyage paramètres
 203              $remise_percent=price2num($remise_percent);
 204              $p_qty=price2num($p_qty);
 205              if (strlen(trim($p_qty))==0) $p_qty=1;
 206              $p_price = price2num($p_price);
 207  
 208              $price = $p_price;
 209              $subprice = $p_price;
 210      
 211              if ($remise_percent > 0)
 212              {
 213                  $remise = round(($p_price * $remise_percent / 100), 2);
 214                  $price = $p_price - $remise;
 215              }
 216      
 217              $sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
 218              $sql .= " (".$this->id.", 0,'". $p_qty."','". ereg_replace(",",".",$price)."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '".ereg_replace(",",".",$subprice)."') ; ";
 219      
 220              if ($this->db->query($sql) )
 221              {
 222      
 223                  if ($this->update_price() > 0)
 224                  {
 225                      return 1;
 226                  }
 227                  else
 228                  {
 229                      return -1;
 230                  }
 231              }
 232              else
 233              {
 234                  return -2;
 235              }
 236          }
 237      }
 238      
 239      
 240    /**
 241     *    \brief      Mise à jour d'une ligne de produit
 242     *    \param      id              Id de la ligne
 243     *    \param      subprice        Prix unitaire
 244     *    \param      qty             Quantité
 245     *    \param      remise_percent  Remise effectuée sur le produit
 246     *    \param      tva_tx          Taux de TVA
 247     *    \param      desc            Description
 248     *    \return     int             0 en cas de succès
 249     */
 250          
 251      function UpdateLigne($id, $subprice, $qty, $remise_percent=0, $tva_tx, $desc='')
 252      {
 253          if ($this->statut == 0)
 254          {
 255              // Nettoyage paramètres
 256              $subprice=price2num($subprice);
 257              $price = $subprice;
 258              $remise_percent=price2num($remise_percent);
 259              $tva_tx=price2num($tva_tx);
 260              
 261              if ($remise_percent > 0)
 262              {
 263                  $remise = round(($subprice * $remise_percent / 100), 2);
 264                  $price = $subprice - $remise;
 265              }
 266      
 267              $sql = "UPDATE ".MAIN_DB_PREFIX."propaldet ";
 268              $sql.= " SET qty='".$qty."'";
 269              $sql.= " , price='". $price."'";
 270              $sql.= " , remise_percent='".$remise_percent."'";
 271              $sql.= " , subprice='".$subprice."'";
 272              $sql.= " , tva_tx='".$tva_tx."'";
 273              $sql.= " , description='".addslashes($desc)."'";
 274              $sql.= " WHERE rowid = '".$id."';";
 275      
 276              if ($this->db->query($sql))
 277              {
 278                  $this->update_price();
 279                  return 0;
 280              }
 281              else
 282              {
 283                  $this->error=$this->db->error();
 284                  dolibarr_syslog("Propal::UpdateLigne Erreur -1");
 285                  return -1;
 286              }
 287          }
 288          else
 289          {
 290              dolibarr_syslog("Propal::UpdateLigne Erreur -2 Propal en mode incompatible pour cette action");
 291              return -2;
 292          }
 293      }
 294      
 295          
 296    /**
 297     *
 298     *
 299     */
 300       
 301    function fetch_client()
 302      {
 303        $client = new Societe($this->db);
 304        $client->fetch($this->socidp);
 305        $this->client = $client;
 306      }
 307          
 308      /**
 309       *      \brief      Supprime une ligne de detail
 310       *      \param      idligne     Id de la ligne detail à supprimer
 311       *      \return     int         >0 si ok, <0 si ko
 312       */
 313      function delete_product($idligne)
 314      {
 315          if ($this->statut == 0)
 316          {
 317              $sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".$idligne;
 318      
 319              if ($this->db->query($sql) )
 320              {
 321                  $this->update_price();
 322      
 323                  return 1;
 324              }
 325              else
 326              {
 327                  return -1;
 328              }
 329          }
 330          else
 331          {
 332              return -2;
 333          }
 334      }
 335  
 336          
 337      /**
 338       *      \brief      Crée une propal
 339       *      \return     int     <0 si ko, >=0 si ok
 340       */
 341      function create()
 342      {
 343          global $langs,$conf;
 344  
 345          // Définition paramètres
 346          $this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
 347      
 348          $this->db->begin();
 349  
 350          // Insertion dans la base
 351          $sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (fk_soc, fk_soc_contact, price, remise, tva, total, datep, datec, ref, fk_user_author, note, model_pdf, fin_validite) ";
 352          $sql.= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, 0,0,".$this->db->idate($this->datep).", now(), '$this->ref', $this->author, '".addslashes($this->note)."','$this->modelpdf',".$this->db->idate($this->fin_validite).")";
 353  
 354          $resql=$this->db->query($sql);
 355          if ($resql)
 356          {
 357              $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
 358      
 359              if ($this->id)
 360              {
 361                  /*
 362                   *  Insertion du detail des produits dans la base
 363                   */
 364                  for ($i = 0 ; $i < sizeof($this->products) ; $i++)
 365                  {
 366                      $prod = new Product($this->db, $this->products[$i]);
 367                      $result=$prod->fetch($this->products[$i]);
 368  
 369                      $result=$this->insert_product($this->products[$i],
 370                                          $this->products_qty[$i],
 371                                          $this->products_remise_percent[$i]);
 372                  }
 373  
 374                  // Affectation au projet
 375                  if ($this->projetidp)
 376                  {
 377                      $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
 378                      $result=$this->db->query($sql);
 379                  }
 380  
 381                  $resql=$this->update_price();
 382                  if ($resql)
 383                  {
 384                      // Appel des triggers
 385                      include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 386                      $interface=new Interfaces($this->db);
 387                      $result=$interface->run_triggers('PROPAL_CREATE',$this,$user,$langs,$conf);
 388                      // Fin appel triggers
 389          
 390                      $this->db->commit();
 391                      return $this->id;
 392                  }
 393                  else
 394                  {
 395                      $this->error=$this->db->error();
 396                      dolibarr_syslog("Propal::Create -2 ".$this->error);
 397                      $this->db->rollback();
 398                      return -2;
 399                  }
 400              }
 401          }
 402          else
 403          {
 404              $this->error=$this->db->error();
 405              dolibarr_syslog("Propal::Create -1 ".$this->error);
 406              $this->db->rollback();
 407              return -1;
 408          }
 409          
 410          return $this->id;
 411      }
 412          
 413      /**
 414       *    \brief      Mets à jour le prix total de la proposition
 415       *    \return     int     <0 si ko, >0 si ok
 416       */
 417      function update_price()
 418      {
 419          include_once  DOL_DOCUMENT_ROOT . "/lib/price.lib.php";
 420      
 421          /*
 422           *  Liste des produits a ajouter
 423           */
 424          $sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = $this->id";
 425          if ( $this->db->query($sql) )
 426          {
 427              $num = $this->db->num_rows();
 428              $i = 0;
 429      
 430              while ($i < $num)
 431              {
 432                  $obj = $this->db->fetch_object();
 433                  $products[$i][0] = $obj->price;
 434                  $products[$i][1] = $obj->qty;
 435                  $products[$i][2] = $obj->tva_tx;
 436                  $i++;
 437              }
 438          }
 439          $calculs = calcul_price($products, $this->remise_percent);
 440      
 441          $this->remise         = $calculs[3];
 442          $this->total_ht       = $calculs[0];
 443          $this->total_tva      = $calculs[1];
 444          $this->total_ttc      = $calculs[2];
 445  
 446          // Met a jour en base
 447          $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET";
 448          $sql .= " price='".  ereg_replace(",",".",$this->total_ht)."'";
 449          $sql .= ", tva='".   ereg_replace(",",".",$this->total_tva)."'";
 450          $sql .= ", total='". ereg_replace(",",".",$this->total_ttc)."'";
 451          $sql .= ", remise='".ereg_replace(",",".",$this->remise)."'";
 452          $sql .=" WHERE rowid = $this->id";
 453      
 454          if ( $this->db->query($sql) )
 455          {
 456              return 1;
 457          }
 458          else
 459          {
 460              $this->error=$this->db->error();
 461              return -1;
 462          }
 463      }
 464  
 465          
 466      /**
 467       *    \brief      Recupère de la base les caractéristiques d'une propale
 468       *    \param      rowid       id de la propal à récupérer
 469       */
 470      function fetch($rowid)
 471      {
 472          $sql = "SELECT ref,total,price,remise,tva,fk_soc,fk_soc_contact";
 473          $sql .= " ,".$this->db->pdate("datep")."as dp";
 474          $sql .= " ,".$this->db->pdate("fin_validite")."as dfv, model_pdf, note";
 475          $sql .= " , fk_projet, fk_statut, remise_percent, fk_user_author";
 476          $sql .= ", c.label as statut_label";
 477          $sql .= " FROM ".MAIN_DB_PREFIX."propal";
 478          $sql .= "," . MAIN_DB_PREFIX."c_propalst as c";
 479          $sql .= " WHERE fk_statut = c.id";
 480          $sql .= " AND rowid='".$rowid."';";
 481      
 482          $resql=$this->db->query($sql);
 483      
 484          if ($resql)
 485          {
 486              if ($this->db->num_rows($resql))
 487              {
 488                  $obj = $this->db->fetch_object($resql);
 489      
 490                  $this->id             = $rowid;
 491      
 492                  $this->datep          = $obj->dp;
 493                  $this->fin_validite   = $obj->dfv;
 494                  $this->date           = $obj->dp;
 495                  $this->ref            = $obj->ref;
 496                  $this->price          = $obj->price;
 497                  $this->remise         = $obj->remise;
 498                  $this->remise_percent = $obj->remise_percent;
 499                  $this->total          = $obj->total;
 500                  $this->total_ht       = $obj->price;
 501                  $this->total_tva      = $obj->tva;
 502                  $this->total_ttc      = $obj->total;
 503                  $this->socidp         = $obj->fk_soc;
 504                  $this->soc_id         = $obj->fk_soc;
 505                  $this->projetidp      = $obj->fk_projet;
 506                  $this->contactid      = $obj->fk_soc_contact;
 507                  $this->modelpdf       = $obj->model_pdf;
 508                  $this->note           = $obj->note;
 509                  $this->statut         = $obj->fk_statut;
 510                  $this->statut_libelle = $obj->statut_label;
 511      
 512                  $this->user_author_id = $obj->fk_user_author;
 513      
 514                  if ($obj->fk_statut == 0)
 515                  {
 516                      $this->brouillon = 1;
 517                  }
 518      
 519                  $this->lignes = array();
 520                  $this->db->free($resql);
 521      
 522                  $this->ref_url = '<a href="'.DOL_URL_ROOT.'/comm/propal.php?propalid='.$this->id.'">'.$this->ref.'</a>';
 523      
 524                  /*
 525                  * Lignes propales liées à un produit
 526                  */
 527                  $sql = "SELECT d.description, p.rowid, p.label, p.description as product_desc, p.ref, d.price, d.tva_tx, d.qty, d.remise_percent, d.subprice";
 528                  $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."product as p";
 529                  $sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = p.rowid";
 530                  $sql .= " ORDER by d.rowid ASC";
 531      
 532                  $result = $this->db->query($sql);
 533                  if ($result)
 534                  {
 535                      $num = $this->db->num_rows($result);
 536                      $i = 0;
 537      
 538                      while ($i < $num)
 539                      {
 540                          $objp                  = $this->db->fetch_object($result);
 541      
 542                          $ligne                 = new PropaleLigne();
 543                          $ligne->desc           = $objp->description;  // Description ligne
 544                          $ligne->libelle        = $objp->label;        // Label produit
 545                          $ligne->product_desc   = $objp->product_desc; // Description produit
 546                          $ligne->qty            = $objp->qty;
 547                          $ligne->ref            = $objp->ref;
 548                          $ligne->tva_tx         = $objp->tva_tx;
 549                          $ligne->subprice       = $objp->subprice;
 550                          $ligne->remise_percent = $objp->remise_percent;
 551                          $ligne->price          = $objp->price;
 552                          $ligne->product_id     = $objp->rowid;
 553      
 554                          $this->lignes[$i]      = $ligne;
 555                          //dolibarr_syslog("1 ".$ligne->desc);
 556                          //dolibarr_syslog("2 ".$ligne->product_desc);
 557                          $i++;
 558                      }
 559                      $this->db->free($result);
 560                  }
 561                  else
 562                  {
 563                      dolibarr_syslog("Propal::Fetch Erreur lecture des produits");
 564                      return -1;
 565                  }
 566      
 567                  /*
 568                   * Lignes propales liées à aucun produit
 569                   */
 570                  $sql = "SELECT d.qty, d.description, d.price, d.subprice, d.tva_tx, d.rowid, d.remise_percent";
 571                  $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d";
 572                  $sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = 0";
 573      
 574                  $result = $this->db->query($sql);
 575                  if ($result)
 576                  {
 577                      $num = $this->db->num_rows($result);
 578                      $j = 0;
 579      
 580                      while ($j < $num)
 581                      {
 582                          $objp                  = $this->db->fetch_object($result);
 583                          $ligne                 = new PropaleLigne();
 584                          $ligne->libelle        = $objp->description;
 585                          $ligne->desc           = $objp->description;
 586                          $ligne->qty            = $objp->qty;
 587                          $ligne->ref            = $objp->ref;
 588                          $ligne->tva_tx         = $objp->tva_tx;
 589                          $ligne->subprice       = $objp->subprice;
 590                          $ligne->remise_percent = $objp->remise_percent;
 591                          $ligne->price          = $objp->price;
 592                          $ligne->product_id     = 0;
 593      
 594                          $this->lignes[$i]      = $ligne;
 595                          $i++;
 596                          $j++;
 597                      }
 598      
 599                      $this->db->free($result);
 600                  }
 601                  else
 602                  {
 603                      dolibarr_syslog("Propal::Fetch Erreur lecture des lignes de propale");
 604      
 605                      return -1;
 606                  }
 607              }
 608              return 1;
 609          }
 610          else
 611          {
 612              dolibarr_syslog("Propal::Fetch Erreur lecture de la propale $rowid");
 613              return -1;
 614          }
 615      }
 616    
 617      /**
 618       *      \brief      Passe au statut valider une propale
 619       *      \param      user        Objet utilisateur qui valide
 620       *      \return     int         <0 si ko, >=0 si ok
 621       */
 622      function valid($user)
 623      {
 624          global $conf,$langs;
 625          
 626          if ($user->rights->propale->valider)
 627          {
 628              $this->db->begin();
 629  
 630              $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
 631              $sql.= " SET fk_statut = 1, date_valid=now(), fk_user_valid=".$user->id;
 632              $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
 633      
 634              if ($this->db->query($sql))
 635              {
 636  
 637                  // Appel des triggers
 638                  include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 639                  $interface=new Interfaces($this->db);
 640                  $result=$interface->run_triggers('PROPAL_VALIDATE',$this,$user,$langs,$conf);
 641                  // Fin appel triggers
 642  
 643                  $this->db->commit();
 644                  return 1;
 645              }
 646              else
 647              {
 648                  $this->db->rollback();
 649                  return -1;
 650              }
 651          }
 652      }
 653      
 654  
 655      /**
 656       *      \brief      Définit une remise globale sur la proposition
 657       *      \param      user        Objet utilisateur qui modifie
 658       *      \param      remise      Montant remise    
 659       *      \return     int         <0 si ko, >0 si ok
 660       */
 661      function set_echeance($user, $date_fin_validite)
 662      {
 663          if ($user->rights->propale->creer)
 664          {
 665              $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".$this->db->idate($date_fin_validite);
 666              $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
 667      
 668              if ($this->db->query($sql) )
 669              {
 670                  $this->fin_validite = $date_fin_validite;
 671                  return 1;
 672              }
 673              else
 674              {
 675                  $this->error=$this->db->error();
 676                  dolibarr_syslog("Propal::set_echeance Erreur SQL");
 677                  return -1;
 678              }
 679          }
 680      }
 681  
 682  
 683      /**
 684       *      \brief      Définit une remise globale sur la proposition
 685       *      \param      user        Objet utilisateur qui modifie
 686       *      \param      remise      Montant remise    
 687       *      \return     int         <0 si ko, >0 si ok
 688       */
 689      function set_remise($user, $remise)
 690      {
 691          if ($user->rights->propale->creer)
 692          {
 693              $remise = price2num($remise);
 694      
 695              $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".$remise;
 696              $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
 697      
 698              if ($this->db->query($sql) )
 699              {
 700                  $this->remise_percent = $remise;
 701                  $this->update_price();
 702                  return 1;
 703              }
 704              else
 705              {
 706                  $this->error=$this->db->error();
 707                  dolibarr_syslog("Propal::set_remise Erreur SQL");
 708                  return -1;
 709              }
 710          }
 711      }
 712  
 713    
 714    /*
 715     *
 716     *
 717     *
 718     */
 719    function set_project($user, $project_id)
 720    {
 721      if ($user->rights->propale->creer)
 722        {
 723      //verif que le projet et la société concordent
 724      $sql = 'SELECT p.rowid, p.title FROM '.MAIN_DB_PREFIX.'projet as p WHERE p.fk_soc ='.$this->socidp.' AND p.rowid='.$project_id;
 725      $sqlres = $this->db->query($sql);
 726      if ($sqlres)
 727        {
 728          $numprojet = $this->db->num_rows($sqlres);
 729          if ($numprojet > 0)
 730            {
 731          $this->projetidp=$project_id;
 732          $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_projet = '.$project_id;
 733          $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;';
 734          $this->db->query($sql);
 735            }
 736        }
 737      else
 738        {
 739          
 740          dolibarr_syslog("Propal::set_project Erreur SQL");
 741        }
 742        }
 743    }
 744  
 745    /*
 746     *
 747     *
 748     *
 749     */
 750    
 751    function set_contact($user, $contact_id)
 752    {
 753      if ($user->rights->propale->creer)
 754        {
 755      //verif que le contact et la société concordent
 756      $sql = 'SELECT p.idp FROM '.MAIN_DB_PREFIX.'socpeople as p WHERE p.fk_soc = '.$this->socidp.' AND p.idp='.$contact_id;
 757      $sqlres = $this->db->query($sql);
 758      if ($sqlres)
 759        {
 760          $numprojet = $this->db->num_rows($sqlres);
 761          if ($numprojet > 0)
 762            {
 763          $this->projetidp=$project_id;
 764          $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_soc_contact = '.$contact_id;
 765          $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;';
 766          $this->db->query($sql);
 767            }
 768        }
 769      else
 770        {
 771          dolibarr_syslog("Propal::set_contact Erreur SQL");
 772        }
 773        }
 774    }
 775    
 776    /*
 777     *
 778     *
 779     *
 780     */
 781       
 782    function set_pdf_model($user, $modelpdf)
 783      {
 784        if ($user->rights->propale->creer)
 785      {
 786  
 787        $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET model_pdf = '$modelpdf'";
 788        $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
 789        
 790        if ($this->db->query($sql) )
 791          {
 792            return 1;
 793          }
 794        else
 795          {
 796            dolibarr_print_error($this->db);
 797            return 0;
 798          }
 799      }
 800    }
 801      
 802      /**
 803       *      \brief      Cloture de la proposition commerciale
 804       *      \param      user        Utilisateur qui cloture
 805       *      \param      statut      Statut
 806       *      \param      note        Commentaire
 807       *      \return     int         <0 si ko, >0 si ok
 808       */
 809      function cloture($user, $statut, $note)
 810      {
 811          $this->statut = $statut;
 812      
 813          $this->db->begin();
 814          
 815          $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
 816          $sql.= " SET fk_statut = $statut, note = '".addslashes($note)."', date_cloture=now(), fk_user_cloture=".$user->id;
 817          $sql.= " WHERE rowid = ".$this->id;
 818      
 819          $resql=$this->db->query($sql);
 820          if ($resql)
 821          {
 822              if ($statut == 2)
 823              {
 824                  // Propale signée
 825                  include_once (DOL_DOCUMENT_ROOT."/commande/commande.class.php");
 826  
 827                  $result=$this->create_commande($user);
 828  
 829                  if ($result >= 0)
 830                  {
 831                      // Classe la société rattachée comme client
 832                      $soc=new Societe($this->db);
 833                      $soc->id = $this->socidp;
 834                      $result=$soc->set_as_client();
 835                  }
 836                  
 837                  if ($result < 0)
 838                  {
 839                      $this->error=$this->db->error();
 840                      $this->db->rollback();
 841                      return -2;
 842                  }
 843                  
 844                  // Appel des triggers
 845                  include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 846                  $interface=new Interfaces($this->db);
 847                  $result=$interface->run_triggers('PROPAL_CLOSE_SIGNED',$this,$user,$langs,$conf);
 848                  // Fin appel triggers
 849              }
 850              else
 851              {
 852                  // Appel des triggers
 853                  include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php");
 854                  $interface=new Interfaces($this->db);
 855                  $result=$interface->run_triggers('PROPAL_CLOSE_REFUSED',$this,$user,$langs,$conf);
 856                  // Fin appel triggers
 857              }
 858              
 859              $this->db->commit();
 860              return 1;
 861          }
 862          else
 863          {
 864              $this->error=$this->db->error();
 865              $this->db->rollback();
 866              return -1;
 867          }
 868      }
 869  
 870  
 871      /**
 872       *      \brief      Crée une commande à partir de la proposition commerciale
 873       *      \param      user        Utilisateur
 874       *      \return     int         <0 si ko, >=0 si ok
 875       */
 876      function create_commande($user)
 877      {
 878          global $conf;
 879          
 880          if ($conf->commande->enabled)
 881          {
 882              if ($this->statut == 2)
 883              {
 884                  // Propale signée
 885                  include_once (DOL_DOCUMENT_ROOT."/commande/commande.class.php");
 886                  $commande = new Commande($this->db);
 887                  $result=$commande->create_from_propale($user, $this->id);
 888      
 889                  return $result;
 890              }
 891              else return 0;
 892          }
 893          else return 0;
 894      }
 895  
 896  
 897      /**
 898      *
 899      *
 900      */
 901      function reopen($userid)
 902      {
 903          $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = 0";
 904      
 905          $sql .= " WHERE rowid = $this->id;";
 906      
 907          if ($this->db->query($sql) )
 908          {
 909              return 1;
 910          }
 911          else
 912          {
 913              dolibarr_print_error($this->db);
 914          }
 915      }
 916      
 917          
 918    /**
 919     *    \brief      Renvoi la liste des propal (éventuellement filtrée sur un user) dans un tableau
 920     *    \param      brouillon       0=non brouillon, 1=brouillon
 921     *    \param      user            Objet user de filtre
 922     *    \return     int             -1 si erreur, tableau résultat si ok
 923     */
 924       
 925      function liste_array ($brouillon=0, $user='')
 926      {
 927          $ga = array();
 928      
 929          $sql = "SELECT rowid, ref FROM ".MAIN_DB_PREFIX."propal";
 930      
 931          if ($brouillon)
 932          {
 933              $sql .= " WHERE fk_statut = 0";
 934              if ($user)
 935              {
 936                  $sql .= " AND fk_user_author".$user;
 937              }
 938          }
 939          else
 940          {
 941              if ($user)
 942              {
 943                  $sql .= " WHERE fk_user_author".$user;
 944              }
 945          }
 946      
 947          $sql .= " ORDER BY datep DESC";
 948  
 949          $result=$this->db->query($sql);
 950          if ($result)
 951          {
 952              $nump = $this->db->num_rows($result);
 953      
 954              if ($nump)
 955              {
 956                  $i = 0;
 957                  while ($i < $nump)
 958                  {
 959                      $obj = $this->db->fetch_object($result);
 960      
 961                      $ga[$obj->rowid] = $obj->ref;
 962                      $i++;
 963                  }
 964              }
 965              return $ga;
 966          }
 967          else
 968          {
 969              return -1;
 970          }
 971      }
 972          
 973      /**
 974       *    \brief        Renvoie un tableau contenant les numéros de commandes associées
 975       *    \remarks      Fonction plus light que associated_orders
 976       *    \sa           associated_orders
 977       */
 978      function commande_liste_array ()
 979      {
 980          $ga = array();
 981          
 982          $sql = "SELECT fk_commande FROM ".MAIN_DB_PREFIX."co_pr";
 983          $sql .= " WHERE fk_propale = " . $this->id;
 984          if ($this->db->query($sql) )
 985          {
 986              $nump = $this->db->num_rows();
 987              
 988              if ($nump)
 989              {
 990                  $i = 0;
 991                  while ($i < $nump)
 992                  {
 993                      $obj = $this->db->fetch_object();
 994                      
 995                      $ga[$i] = $obj->fk_commande;
 996                      $i++;
 997                  }
 998              }
 999              return $ga;
1000          }
1001          else
1002          {
1003              dolibarr_print_error($this->db);
1004          }
1005      }
1006          
1007      /**
1008       *    \brief      Renvoie un tableau contenant les commandes associées
1009       *    \remarks    Fonction plus lourde que commande_liste_array
1010       *    \sa         commande_liste_array
1011       */
1012      function associated_orders()
1013      {
1014          $ga = array();
1015      
1016          $sql = "SELECT fk_commande FROM ".MAIN_DB_PREFIX."co_pr";
1017          $sql.= " WHERE fk_propale = " . $this->id;
1018          if ($this->db->query($sql) )
1019          {
1020              $nump = $this->db->num_rows();
1021      
1022              if ($nump)
1023              {
1024                  $i = 0;
1025                  while ($i < $nump)
1026                  {
1027                      $obj = $this->db->fetch_object();
1028                      $order=new Commande($this->db);
1029  
1030                      if ($obj->fk_commande)
1031                      {
1032                          $order->fetch($obj->fk_commande);
1033                          $ga[$i] = $order;
1034                      }
1035                      $i++;
1036                  }
1037              }
1038              return $ga;
1039          }
1040          else
1041          {
1042              print $this->db->error();
1043          }
1044      }
1045          
1046      /**
1047       *    \brief      Renvoie un tableau contenant les numéros de factures associées
1048       */
1049      function facture_liste_array ()
1050      {
1051          $ga = array();
1052          
1053          $sql = "SELECT fk_facture FROM ".MAIN_DB_PREFIX."fa_pr as fp";
1054          $sql .= " WHERE fk_propal = " . $this->id;
1055          if ($this->db->query($sql) )
1056          {
1057              $nump = $this->db->num_rows();
1058              
1059              if ($nump)
1060              {
1061                  $i = 0;
1062                  while ($i < $nump)
1063                  {
1064                      $obj = $this->db->fetch_object();
1065                      
1066                      $ga[$i] = $obj->fk_facture;
1067                      $i++;
1068                  }
1069              }
1070              return $ga;
1071          }
1072          else
1073          {
1074              dolibarr_print_error($this->db);
1075          }
1076      }
1077  
1078    /**
1079     *    \brief      Efface propal
1080     *    \param      user        Objet du user qui efface
1081     */
1082    function delete($user)
1083    {
1084      $sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = $this->id ;";
1085      if ( $this->db->query($sql) ) 
1086        {
1087      $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = $this->id;";
1088      if ( $this->db->query($sql) ) 
1089        {
1090          dolibarr_syslog("Suppression de la proposition $this->id par $user->fullname ($user->id)");
1091          return 1;
1092        }
1093      else
1094        {
1095          return -2;
1096        }
1097        }
1098      else
1099        {
1100      return -1;
1101        }
1102    }
1103      
1104      /**
1105       *      \brief      Mets à jour la note
1106       *      \param      note        Note à mettre à jour
1107       *      \return     int         <0 si ko, >0 si ok
1108       */
1109      function update_note($note)
1110      {
1111          $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
1112          $sql.= " SET note = '".addslashes($note)."'";
1113          $sql.= " WHERE rowid = ".$this->id;
1114      
1115          if ($this->db->query($sql))
1116          {
1117              return 1;
1118          }
1119          else
1120          {
1121              $this->error=$this->db->error();
1122              return -1;
1123          }
1124      }
1125    
1126    
1127    /**
1128     *      \brief      Information sur l'objet propal
1129     *      \param      id      id de la propale
1130     */
1131    function info($id)
1132    {
1133      $sql = "SELECT c.rowid, ";
1134      $sql.= $this->db->pdate("datec")." as datec, ".$this->db->pdate("date_valid")." as datev, ".$this->db->pdate("date_cloture")." as dateo";
1135      $sql.= ", fk_user_author, fk_user_valid, fk_user_cloture";
1136      $sql.= " FROM ".MAIN_DB_PREFIX."propal as c";
1137      $sql.= " WHERE c.rowid = $id";
1138      
1139      if ($this->db->query($sql))
1140        {
1141      if ($this->db->num_rows())
1142        {
1143          $obj = $this->db->fetch_object();
1144          
1145          $this->id                = $obj->rowid;
1146          
1147          $this->date_creation     = $obj->datec;
1148          $this->date_validation   = $obj->datev;
1149          $this->date_cloture      = $obj->dateo;
1150          
1151          $cuser = new User($this->db, $obj->fk_user_author);
1152          $cuser->fetch();
1153          $this->user_creation     = $cuser;
1154          
1155          if ($obj->fk_user_valid)
1156            {
1157          $vuser = new User($this->db, $obj->fk_user_valid);
1158          $vuser->fetch();
1159          $this->user_validation     = $vuser;
1160            }
1161          
1162          if ($obj->fk_user_cloture)
1163            {
1164          $cluser = new User($this->db, $obj->fk_user_cloture);
1165          $cluser->fetch();
1166          $this->user_cloture     = $cluser;
1167            }
1168          
1169          
1170        }
1171      $this->db->free();
1172      
1173        }
1174      else
1175        {
1176      dolibarr_print_error($this->db);
1177        }
1178    }
1179    
1180    
1181    /**
1182     *    \brief      Retourne le libellé du statut d'une propale (brouillon, validée, ...)
1183     *    \return     string      Libellé
1184     */
1185    function getLibStatut()
1186    {
1187      return $this->LibStatut($this->statut);
1188    }
1189    
1190    /**
1191     *    \brief      Renvoi le libellé d'un statut donné
1192     *    \param      statut      id statut
1193     *    \param      size        Libellé court si 0, long si non défini
1194     *    \return     string      Libellé
1195     */
1196      function LibStatut($statut,$size=1)
1197      {
1198          if ($size) return $this->labelstatut[$statut];
1199          else return $this->labelstatut_short[$statut];
1200      }
1201  
1202  
1203      /**
1204       *      \brief      Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord
1205       *      \param      user        Objet user
1206       *      \param      mode        "opened" pour propal à fermer, "signed" pour propale à facturer
1207       *      \return     int         <0 si ko, >0 si ok
1208       */
1209      function load_board($user,$mode)
1210      {
1211          global $conf;
1212          
1213          $this->nbtodo=$this->nbtodolate=0;
1214          $sql ="SELECT p.rowid,".$this->db->pdate("p.datec")." as datec,".$this->db->pdate("p.fin_validite")." as datefin";
1215          $sql.=" FROM ".MAIN_DB_PREFIX."propal as p";
1216          if ($mode == 'opened') $sql.=" WHERE p.fk_statut = 1";
1217          if ($mode == 'signed') $sql.=" WHERE p.fk_statut = 2";
1218          if ($user->societe_id) $sql.=" AND fk_soc = ".$user->societe_id;
1219          $resql=$this->db->query($sql);
1220          if ($resql)
1221          {
1222              while ($obj=$this->db->fetch_object($resql))
1223              {
1224                  $this->nbtodo++;
1225                  if ($obj->datefin < (time() - $conf->propal->cloture->warning_delay)) $this->nbtodolate++;
1226              }
1227              return 1;
1228          }
1229          else 
1230          {
1231              $this->error=$this->db->error();
1232              return -1;
1233          }
1234      }
1235  
1236  }
1237  
1238  
1239  /**
1240          \class      PropalLigne
1241          \brief      Classe permettant la gestion des lignes de propales
1242  */
1243  
1244  class PropaleLigne  
1245  {
1246      function PropaleLigne()
1247      {
1248      }
1249  }
1250  
1251  ?>


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