/[dolibarr]/dolibarr/htdocs/contrat/contrat.class.php
ViewVC logotype

Diff of /dolibarr/htdocs/contrat/contrat.class.php

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

revision 1.41 by eldy, Sat Sep 3 00:09:55 2005 UTC revision 1.42 by eldy, Sun Sep 4 14:52:59 2005 UTC
# Line 216  class Contrat Line 216  class Contrat
216      }      }
217            
218      /**      /**
219       *    \brief      Charge de la base les données du contrat       *    \brief      Chargement depuis la base des données du contrat
220       *    \param      id      id du contrat à charger       *    \param      id      Id du contrat à charger
221       *    \return     int     <0 si KO, >0 si OK       *    \return     int     <0 si KO, >0 si OK
222       */       */
223      function fetch($id)      function fetch($id)
# Line 257  class Contrat Line 257  class Contrat
257              $this->societe->fetch($result["fk_soc"]);              $this->societe->fetch($result["fk_soc"]);
258            
259              $this->db->free($resql);              $this->db->free($resql);
260        
261              return 1;              return 1;
262          }          }
263          else          else
264          {          {
265                dolibarr_syslog("Contrat::Fetch Erreur lecture contrat");
266              $this->error=$this->db->error();              $this->error=$this->db->error();
267              return -1;              return -1;
268          }          }
# Line 269  class Contrat Line 270  class Contrat
270      }      }
271            
272      /**      /**
273         *      \brief      Reinitialise le tableau lignes
274         */
275        function fetch_lignes()
276        {
277            $this->lignes = array();
278        
279            /*
280             * Lignes contrats liées à un produit
281             */
282            $sql = "SELECT d.description, p.rowid, p.label, p.description as product_desc, p.ref,";
283            $sql.= " d.price_ht, d.tva_tx, d.qty, d.remise_percent, d.subprice";
284            $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as d, ".MAIN_DB_PREFIX."product as p";
285            $sql.= " WHERE d.fk_contrat = ".$this->id ." AND d.fk_product = p.rowid";
286            $sql.= " ORDER by d.rowid ASC";
287            
288            $result = $this->db->query($sql);
289            if ($result)
290            {
291                $num = $this->db->num_rows($result);
292                $i = 0;
293            
294                while ($i < $num)
295                {
296                    $objp                  = $this->db->fetch_object($result);
297            
298                    $ligne                 = new ContratLigne();
299                    $ligne->desc           = stripslashes($objp->description);  // Description ligne
300                    $ligne->libelle        = stripslashes($objp->label);        // Label produit
301                    $ligne->product_desc   = stripslashes($objp->product_desc); // Description produit
302                    $ligne->qty            = $objp->qty;
303                    $ligne->ref            = $objp->ref;
304                    $ligne->tva_tx         = $objp->tva_tx;
305                    $ligne->subprice       = $objp->subprice;
306                    $ligne->remise_percent = $objp->remise_percent;
307                    $ligne->price          = $objp->price;
308                    $ligne->product_id     = $objp->rowid;
309            
310                    $this->lignes[$i]      = $ligne;
311                    //dolibarr_syslog("1 ".$ligne->desc);
312                    //dolibarr_syslog("2 ".$ligne->product_desc);
313                    $i++;
314                }
315                $this->db->free($result);
316            }
317            else
318            {
319                dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrats liées aux produits");
320                return -3;
321            }
322            
323            /*
324             * Lignes contrat liées à aucun produit
325             */
326            $sql = "SELECT d.qty, d.description, d.price_ht, d.subprice, d.tva_tx, d.rowid, d.remise_percent";
327            $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d";
328            $sql .= " WHERE d.fk_contrat = ".$this->id ." AND d.fk_product = 0";
329            
330            $result = $this->db->query($sql);
331            if ($result)
332            {
333                $num = $this->db->num_rows($result);
334                $j = 0;
335            
336                while ($j < $num)
337                {
338                    $objp                  = $this->db->fetch_object($result);
339                    $ligne                 = new PropaleLigne();
340                    $ligne->libelle        = stripslashes($objp->description);
341                    $ligne->desc           = stripslashes($objp->description);
342                    $ligne->qty            = $objp->qty;
343                    $ligne->ref            = $objp->ref;
344                    $ligne->tva_tx         = $objp->tva_tx;
345                    $ligne->subprice       = $objp->subprice;
346                    $ligne->remise_percent = $objp->remise_percent;
347                    $ligne->price          = $objp->price;
348                    $ligne->product_id     = 0;
349            
350                    $this->lignes[$i]      = $ligne;
351                    $i++;
352                    $j++;
353                }
354            
355                $this->db->free($result);
356            }
357            else
358            {
359                dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrat non liées aux produits");
360                $this->error=$this->db->error();
361                return -2;
362            }
363        
364            return $this->lignes;
365        }
366      
367        /**
368       *      \brief      Crée un contrat vierge en base       *      \brief      Crée un contrat vierge en base
369       *      \param      user        Utilisateur qui crée       *      \param      user        Utilisateur qui crée
370       *      \param      lang        Environnement langue de l'utilisateur       *      \param      lang        Environnement langue de l'utilisateur
# Line 372  class Contrat Line 468  class Contrat
468      {      {
469          global $langs;          global $langs;
470                    
471          $qty = ereg_replace(",",".",$qty);          dolibarr_syslog("contrat.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent, $datestart, $dateend");
         $pu = ereg_replace(",",".",$pu);  
           
         dolibarr_syslog("Contrat::AddLine $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart, $dateend");  
472    
473          if ($fk_product > 0)          if ($this->statut == 0)
474          {          {
475              $prod = new Product($this->db, $fk_product);              $qty = ereg_replace(",",".",$qty);
476              if ($prod->fetch($fk_product) > 0)              $pu = ereg_replace(",",".",$pu);
477                
478                if ($fk_product > 0)
479              {              {
480                  $label = $prod->libelle;                  $prod = new Product($this->db, $fk_product);
481                  $pu    = $prod->price;                  if ($prod->fetch($fk_product) > 0)
482                  $txtva = $prod->tva_tx;                  {
483                        $label = $prod->libelle;
484                        $pu    = $prod->price;
485                        $txtva = $prod->tva_tx;
486                    }
487                }
488                
489                $remise = 0;
490                $price = ereg_replace(",",".",round($pu, 2));
491                $subprice = $price;
492                if (strlen($remise_percent) > 0)
493                {
494                    $remise = round(($pu * $remise_percent / 100), 2);
495                    $price = $pu - $remise;
496                }
497                
498                // Insertion dans la base
499                $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
500                $sql.= " (fk_contrat, label, description, fk_product, price_ht, qty, tva_tx,";
501                $sql.= " remise_percent, subprice, remise";
502                if ($datestart > 0) { $sql.= ",date_ouverture_prevue"; }
503                if ($dateend > 0)  { $sql.= ",date_fin_validite"; }
504                $sql.= ") VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',";
505                $sql.= ($fk_product>0 ? $fk_product : "null");
506                $sql.= ",".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."'";
507                if ($datestart > 0) { $sql.= ",".$this->db->idate($datestart); }
508                if ($dateend > 0) { $sql.= ",".$this->db->idate($dateend); }
509                $sql.= ");";
510                
511                if ( $this->db->query($sql) )
512                {
513                    $this->update_price();
514                    return 1;
515                }
516                else
517                {
518                    $this->error=$this->db->error();
519                    return -1;
520              }              }
         }  
           
         $remise = 0;  
         $price = ereg_replace(",",".",round($pu, 2));  
         $subprice = $price;  
         if (strlen($remise_percent) > 0)  
         {  
             $remise = round(($pu * $remise_percent / 100), 2);  
             $price = $pu - $remise;  
         }  
           
         // Insertion dans la base  
         $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";  
         $sql.= " (fk_contrat, label, description, fk_product, price_ht, qty, tva_tx,";  
         $sql.= " remise_percent, subprice, remise";  
         if ($datestart > 0) { $sql.= ",date_ouverture_prevue"; }  
         if ($dateend > 0)  { $sql.= ",date_fin_validite"; }  
         $sql.= ") VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',";  
         $sql.= ($fk_product>0 ? $fk_product : "null");  
         $sql.= ",".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."'";  
         if ($datestart > 0) { $sql.= ",".$this->db->idate($datestart); }  
         if ($dateend > 0) { $sql.= ",".$this->db->idate($dateend); }  
         $sql.= ");";  
           
         // Retour  
         if ( $this->db->query($sql) )  
         {  
             //$this->update_price();  
             return 0;  
521          }          }
522          else          else
523          {          {
524              dolibarr_print_error($this->db);              return -2;
             return -1;  
525          }          }
526      }      }
527    
# Line 437  class Contrat Line 539  class Contrat
539       */       */
540      function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart='', $dateend='', $tvatx)      function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart='', $dateend='', $tvatx)
541      {      {
542          // Nettoyage parametres          if ($this->statut == 0)
         $qty=trim($qty);  
         $desc=trim($desc);  
         $desc=trim($desc);  
         $price = ereg_replace(",",".",$pu);  
         $tvatx = ereg_replace(",",".",$tvatx);  
         $subprice = $price;  
         $remise = 0;  
         if (strlen($remise_percent) > 0)  
543          {          {
544              $remise = round(($pu * $remise_percent / 100), 2);              // Nettoyage parametres
545              $price = $pu - $remise;              $qty=trim($qty);
546                $desc=trim($desc);
547                $desc=trim($desc);
548                $price = ereg_replace(",",".",$pu);
549                $tvatx = ereg_replace(",",".",$tvatx);
550                $subprice = $price;
551                $remise = 0;
552                if (strlen($remise_percent) > 0)
553                {
554                    $remise = round(($pu * $remise_percent / 100), 2);
555                    $price = $pu - $remise;
556                }
557                else
558                {
559                    $remise_percent=0;
560                }
561        
562                dolibarr_syslog("Contrat::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $datestart, $dateend, $tvatx");
563            
564                $this->db->begin();
565        
566                $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".addslashes($desc)."'";
567                $sql .= ",price_ht='" .     ereg_replace(",",".",$price)."'";
568                $sql .= ",subprice='" .     ereg_replace(",",".",$subprice)."'";
569                $sql .= ",remise='" .       ereg_replace(",",".",$remise)."'";
570                $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'";
571                $sql .= ",qty='$qty'";
572                $sql .= ",tva_tx='".        ereg_replace(",",".",$tvatx)."'";
573        
574                if ($datestart > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($datestart); }
575                else { $sql.=",date_ouverture_prevue=null"; }
576                if ($dateend > 0) { $sql.= ",date_fin_validite=".$this->db->idate($dateend); }
577                else { $sql.=",date_fin_validite=null"; }
578        
579                $sql .= " WHERE rowid = $rowid ;";
580        
581                $result = $this->db->query($sql);
582                if ($result)
583                {
584                    $this->update_price();
585        
586                    $this->db->commit();
587        
588                    return 1;
589                }
590                else
591                {
592                    $this->db->rollback();
593                    $this->error=$this->db->error();
594                    dolibarr_syslog("Contrat::UpdateLigne Erreur -1");
595        
596                    return -1;
597                }
598          }          }
599          else          else
600          {          {
601              $remise_percent=0;              dolibarr_syslog("Contrat::UpdateLigne Erreur -2 Contrat en mode incompatible pour cette action");
602                return -2;
603          }          }
604        }
         dolibarr_syslog("Contrat::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $datestart, $dateend, $tvatx");  
605            
606          $this->db->begin();      /**
607         *      \brief      Supprime une ligne de detail
608          $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".addslashes($desc)."'";       *      \param      idligne     Id de la ligne detail à supprimer
609          $sql .= ",price_ht='" .     ereg_replace(",",".",$price)."'";       *      \return     int         >0 si ok, <0 si ko
610          $sql .= ",subprice='" .     ereg_replace(",",".",$subprice)."'";       */
611          $sql .= ",remise='" .       ereg_replace(",",".",$remise)."'";      function delete_line($idligne)
612          $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'";      {
613          $sql .= ",qty='$qty'";          if ($this->statut == 0)
614          $sql .= ",tva_tx='".        ereg_replace(",",".",$tvatx)."'";          {
615                $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet WHERE rowid =".$idligne;
616          if ($datestart > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($datestart); }          
617          else { $sql.=",date_ouverture_prevue=null"; }              if ($this->db->query($sql) )
618          if ($dateend > 0) { $sql.= ",date_fin_validite=".$this->db->idate($dateend); }              {
619          else { $sql.=",date_fin_validite=null"; }                  $this->update_price();
620            
621                    return 1;
622                }
623                else
624                {
625                    return -1;
626                }
627            }
628            else
629            {
630                return -2;
631            }
632        }
633    
         $sql .= " WHERE rowid = $rowid ;";  
634    
635          $result = $this->db->query($sql);      /**
636          if ($result)       *      \brief      Mets à jour le prix total du contrat
637         */
638        
639        function update_price()
640        {
641            include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php";
642        
643            /*
644             *  Liste des produits a ajouter
645             */
646            $sql = "SELECT price_ht, qty, tva_tx";
647            $sql.= " FROM ".MAIN_DB_PREFIX."contratdet";
648            $sql.= " WHERE fk_contrat = ".$this->id;
649            $resql=$this->db->query($sql);
650            if ($resql)
651          {          {
652              $this->db->commit();              $num = $this->db->num_rows($resql);
653                $i = 0;
654              return $result;      
655                while ($i < $num)
656                {
657                    $obj = $this->db->fetch_object($resql);
658                    $products[$i][0] = $obj->price_ht;
659                    $products[$i][1] = $obj->qty;
660                    $products[$i][2] = $obj->tva_tx;
661                    $i++;
662                }
663          }          }
664          else          else
665          {          {
666              $this->db->rollback();              $this->error=$this->db->error();
   
             dolibarr_print_error($this->db);  
667              return -1;              return -1;
668          }          }
669      }          $calculs = calcul_price($products, $this->remise_percent);
670            
671    /**          $this->remise         = $calculs[3];
672     *    \brief      Supprime une ligne de detail du contrat          $this->total_ht       = $calculs[0];
673     *    \param      idligne     id de la ligne detail de contrat à supprimer          $this->total_tva      = $calculs[1];
674     */          $this->total_ttc      = $calculs[2];
675    function delete_line($idligne)  
676      {          /*
677            // Met a jour en base
678        $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet WHERE rowid =".$idligne;          $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET";
679                  $sql .= " price='".  ereg_replace(",",".",$this->total_ht)."'";
680        if ($this->db->query($sql) )          $sql .= ", tva='".   ereg_replace(",",".",$this->total_tva)."'";
681          {          $sql .= ", total='". ereg_replace(",",".",$this->total_ttc)."'";
682            //$this->update_price();          $sql .= ", remise='".ereg_replace(",",".",$this->remise)."'";
683                      $sql .=" WHERE rowid = $this->id";
684            return 0;      
685          }          if ( $this->db->query($sql) )
686        else          {
687          {              return 1;
688            return 1;          }
689          }          else
690            {
691                $this->error=$this->db->error();
692                return -1;
693            }
694            */
695      }      }
696        
697    
698      /**      /**
699       *      \brief     Classe le contrat dans un projet       *      \brief     Classe le contrat dans un projet
# Line 879  class Contrat Line 1062  class Contrat
1062      }                                        }                                  
1063            
1064  }  }
1065    
1066    
1067    /**
1068            \class      ContratLigne
1069                    \brief      Classe permettant la gestion des lignes de contrats
1070    */
1071    
1072    class ContratLigne  
1073    {
1074        function ContratLigne()
1075        {
1076        }
1077    }
1078    
1079    
1080  ?>  ?>

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.42

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