| [ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * Copyright (C) 2004-2005 Destailleur Laurent <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: contrat.class.php,v 1.62 2005/12/17 14:36:37 eldy Exp $ 20 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/contrat/contrat.class.php,v $ 21 */ 22 23 /** 24 \file htdocs/contrat/contrat.class.php 25 \ingroup contrat 26 \brief Fichier de la classe des contrats 27 \version $Revision: 1.62 $ 28 */ 29 30 require_once(DOL_DOCUMENT_ROOT."/product.class.php"); 31 32 33 /** 34 \class Contrat 35 \brief Classe permettant la gestion des contrats 36 */ 37 38 class Contrat 39 { 40 var $db; 41 42 var $id; 43 var $ref; 44 var $product; 45 var $societe; 46 47 var $user_author; 48 var $user_service; 49 var $user_cloture; 50 var $date_creation; 51 var $date_validation; 52 var $date_cloture; 53 var $commercial_signature_id; 54 var $commercial_suivi_id; 55 56 var $fk_projet; 57 58 var $statuts=array(); 59 60 61 /** 62 * \brief Constructeur de la classe 63 * \param DB handler accès base de données 64 */ 65 function Contrat($DB) 66 { 67 global $langs; 68 69 $this->db = $DB ; 70 $this->product = new Product($DB); 71 $this->societe = new Societe($DB); 72 $this->user_service = new User($DB); 73 $this->user_cloture = new User($DB); 74 75 // Statut 0=ouvert, 1=actif, 2=cloturé 76 $this->statuts[0]=$langs->trans("Draft"); 77 $this->statuts[1]=$langs->trans("Validated"); 78 $this->statuts[2]=$langs->trans("Closed"); 79 } 80 81 /** 82 * \brief Active une ligne detail d'un contrat 83 * \param user Objet User qui avtice le contrat 84 * \param line_id Id de la ligne de detail à activer 85 * \param date Date d'ouverture 86 * \param dateend Date fin prévue 87 * \return int < 0 si erreur, > 0 si ok 88 */ 89 function active_line($user, $line_id, $date, $dateend='') 90 { 91 global $langs,$conf; 92 93 // statut actif : 4 94 95 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = 4,"; 96 $sql.= " date_ouverture = '".$this->db->idate($date)."',"; 97 if ($dateend) $sql.= " date_fin_validite = '".$this->db->idate($dateend)."',"; 98 $sql.= " fk_user_ouverture = ".$user->id; 99 $sql.= " WHERE rowid = ".$line_id . " AND (statut = 0 OR statut = 3) "; 100 101 $resql = $this->db->query($sql); 102 if ($resql) 103 { 104 // Appel des triggers 105 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 106 $interface=new Interfaces($this->db); 107 $result=$interface->run_triggers('CONTRACT_SERVICE_ACTIVATE',$this,$user,$langs,$conf); 108 // Fin appel triggers 109 110 return 1; 111 } 112 else 113 { 114 $this->error=$this->db->error(); 115 return -1; 116 } 117 } 118 119 120 /** 121 * \brief Active une ligne detail d'un contrat 122 * \param user Objet User qui avtice le contrat 123 * \param line_id Id de la ligne de detail à activer 124 * \param dateend Date fin 125 * \return int <0 si erreur, >0 si ok 126 */ 127 function close_line($user, $line_id, $dateend) 128 { 129 global $langs,$conf; 130 131 // statut actif : 4 132 133 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = 5,"; 134 $sql.= " date_cloture = '".$this->db->idate($dateend)."',"; 135 $sql.= " fk_user_cloture = ".$user->id; 136 $sql.= " WHERE rowid = ".$line_id . " AND statut = 4"; 137 138 $resql = $this->db->query($sql) ; 139 if ($resql) 140 { 141 // Appel des triggers 142 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 143 $interface=new Interfaces($this->db); 144 $result=$interface->run_triggers('CONTRACT_SERVICE_CLOSE',$this,$user,$langs,$conf); 145 // Fin appel triggers 146 147 return 1; 148 } 149 else 150 { 151 $this->error=$this->db->error(); 152 return -1; 153 } 154 } 155 156 157 /** 158 * \brief Cloture un contrat 159 * \param user Objet User qui cloture 160 * \param langs Environnement langue de l'utilisateur 161 * \param conf Environnement de configuration lors de l'opération 162 * 163 */ 164 function cloture($user,$langs='',$conf='') 165 { 166 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 2"; 167 $sql .= " , date_cloture = now(), fk_user_cloture = ".$user->id; 168 $sql .= " WHERE rowid = ".$this->id . " AND statut = 1"; 169 170 $resql = $this->db->query($sql) ; 171 if ($resql) 172 { 173 $this->use_webcal=($conf->global->PHPWEBCALENDAR_CONTRACTSTATUS=='always'?1:0); 174 175 // Appel des triggers 176 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 177 $interface=new Interfaces($this->db); 178 $result=$interface->run_triggers('CONTRACT_CLOSE',$this,$user,$langs,$conf); 179 // Fin appel triggers 180 181 return 1; 182 } 183 else 184 { 185 $this->error=$this->db->error(); 186 return -1; 187 } 188 } 189 190 /** 191 * \brief Valide un contrat 192 * \param user Objet User qui valide 193 * \param langs Environnement langue de l'utilisateur 194 * \param conf Environnement de configuration lors de l'opération 195 */ 196 function validate($user,$langs,$conf) 197 { 198 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 1"; 199 $sql .= " WHERE rowid = ".$this->id . " AND statut = 0"; 200 201 $resql = $this->db->query($sql) ; 202 if ($resql) 203 { 204 $this->use_webcal=($conf->global->PHPWEBCALENDAR_CONTRACTSTATUS=='always'?1:0); 205 206 // Appel des triggers 207 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 208 $interface=new Interfaces($this->db); 209 $result=$interface->run_triggers('CONTRACT_VALIDATE',$this,$user,$langs,$conf); 210 // Fin appel triggers 211 212 return 1; 213 } 214 else 215 { 216 $this->error=$this->db->error(); 217 return -1; 218 } 219 } 220 221 /** 222 * \brief Annule un contrat 223 * \param user Objet User qui annule 224 * \param langs Environnement langue de l'utilisateur 225 * \param conf Environnement de configuration lors de l'opération 226 */ 227 function annule($user,$langs='',$conf='') 228 { 229 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0"; 230 $sql .= " , date_cloture = now(), fk_user_cloture = ".$user->id; 231 $sql .= " WHERE rowid = ".$this->id . " AND statut = 1"; 232 233 $resql = $this->db->query($sql) ; 234 if ($resql) 235 { 236 $this->use_webcal=($conf->global->PHPWEBCALENDAR_CONTRACTSTATUS=='always'?1:0); 237 238 // Appel des triggers 239 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 240 $interface=new Interfaces($this->db); 241 $result=$interface->run_triggers('CONTRACT_CANCEL',$this,$user,$langs,$conf); 242 // Fin appel triggers 243 244 return 1; 245 } 246 else 247 { 248 $this->error=$this->db->error(); 249 return -1; 250 } 251 } 252 253 /** 254 * \brief Chargement depuis la base des données du contrat 255 * \param id Id du contrat à charger 256 * \return int <0 si ko, id du contrat chargé si ok 257 */ 258 function fetch($id) 259 { 260 $sql = "SELECT rowid, statut, ref, fk_soc, ".$this->db->pdate("mise_en_service")." as datemise,"; 261 $sql.= " fk_user_mise_en_service, ".$this->db->pdate("date_contrat")." as datecontrat,"; 262 $sql.= " fk_user_author,"; 263 $sql.= " fk_projet,"; 264 $sql.= " fk_commercial_signature, fk_commercial_suivi "; 265 $sql.= " FROM ".MAIN_DB_PREFIX."contrat WHERE rowid = $id"; 266 267 $resql = $this->db->query($sql) ; 268 269 if ($resql) 270 { 271 $result = $this->db->fetch_array($resql); 272 273 if ($result) 274 { 275 $this->id = $result["rowid"]; 276 $this->ref = (!isset($result["ref"]) || !$result["ref"]) ? $result["rowid"] : $result["ref"]; 277 $this->statut = $result["statut"]; 278 $this->factureid = $result["fk_facture"]; 279 $this->facturedetid = $result["fk_facturedet"]; 280 $this->mise_en_service = $result["datemise"]; 281 $this->date_fin_validite = $result["datefin"]; 282 $this->date_contrat = $result["datecontrat"]; 283 284 $this->user_author_id = $result["fk_user_author"]; 285 286 $this->commercial_signature_id = $result["fk_commercial_signature"]; 287 $this->commercial_suivi_id = $result["fk_commercial_suivi"]; 288 289 $this->user_service->id = $result["fk_user_mise_en_service"]; 290 $this->user_cloture->id = $result["fk_user_cloture"]; 291 292 $this->fk_projet = $result["fk_projet"]; 293 294 $this->societe->fetch($result["fk_soc"]); 295 296 $this->db->free($resql); 297 298 return $this->id; 299 } 300 else 301 { 302 dolibarr_syslog("Contrat::Fetch Erreur contrat non trouve"); 303 $this->error="Contrat non trouve"; 304 return -2; 305 } 306 } 307 else 308 { 309 dolibarr_syslog("Contrat::Fetch Erreur lecture contrat"); 310 $this->error=$this->db->error(); 311 return -1; 312 } 313 314 } 315 316 /** 317 * \brief Reinitialise le tableau lignes 318 */ 319 function fetch_lignes() 320 { 321 dolibarr_syslog("Contrat.class.php::fetch_lignes"); 322 $this->lignes = array(); 323 324 // Selectionne les lignes contrats liées à un produit 325 $sql = "SELECT p.rowid, p.label, p.description as product_desc, p.ref,"; 326 $sql.= " d.description, d.statut, d.price_ht, d.tva_tx, d.qty, d.remise_percent, d.subprice,"; 327 $sql.= " d.date_ouverture_prevue, d.date_ouverture,"; 328 $sql.= " d.date_fin_validite, d.date_cloture"; 329 $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as d, ".MAIN_DB_PREFIX."product as p"; 330 $sql.= " WHERE d.fk_contrat = ".$this->id ." AND d.fk_product = p.rowid"; 331 $sql.= " ORDER by d.rowid ASC"; 332 333 $result = $this->db->query($sql); 334 if ($result) 335 { 336 $num = $this->db->num_rows($result); 337 $i = 0; 338 339 while ($i < $num) 340 { 341 $objp = $this->db->fetch_object($result); 342 343 $ligne = new ContratLigne(); 344 $ligne->id = $objp->rowid; 345 $ligne->desc = stripslashes($objp->description); // Description ligne 346 $ligne->libelle = stripslashes($objp->label); // Label produit 347 $ligne->product_desc = stripslashes($objp->product_desc); // Description produit 348 $ligne->qty = $objp->qty; 349 $ligne->ref = $objp->ref; 350 $ligne->tva_tx = $objp->tva_tx; 351 $ligne->subprice = $objp->subprice; 352 $ligne->statut = $objp->statut; 353 $ligne->remise_percent = $objp->remise_percent; 354 $ligne->price = $objp->price; 355 $ligne->product_id = $objp->rowid; 356 357 $ligne->date_debut_prevue = $objp->date_ouverture_prevue; 358 $ligne->date_debut_reel = $objp->date_ouverture; 359 $ligne->date_fin_prevue = $objp->date_fin_validite; 360 $ligne->date_fin_reel = $objp->date_cloture; 361 362 $this->lignes[$i] = $ligne; 363 //dolibarr_syslog("1 ".$ligne->desc); 364 //dolibarr_syslog("2 ".$ligne->product_desc); 365 $i++; 366 } 367 $this->db->free($result); 368 } 369 else 370 { 371 dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrats liées aux produits"); 372 return -3; 373 } 374 375 // Selectionne les lignes contrat liées à aucun produit 376 $sql = "SELECT d.rowid, d.statut, d.qty, d.description, d.price_ht, d.subprice, d.tva_tx, d.rowid, d.remise_percent,"; 377 $sql.= " d.date_ouverture_prevue, d.date_ouverture,"; 378 $sql.= " d.date_fin_validite, d.date_cloture"; 379 $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as d"; 380 $sql.= " WHERE d.fk_contrat = ".$this->id; 381 $sql.= " AND (d.fk_product IS NULL OR d.fk_product = 0)"; // fk_product = 0 gardé pour compatibilité 382 383 $result = $this->db->query($sql); 384 if ($result) 385 { 386 $num = $this->db->num_rows($result); 387 $j = 0; 388 389 while ($j < $num) 390 { 391 $objp = $this->db->fetch_object($result); 392 $ligne = new ContratLigne(); 393 $ligne->id = $objp->rowid; 394 $ligne->libelle = stripslashes($objp->description); 395 $ligne->desc = stripslashes($objp->description); 396 $ligne->qty = $objp->qty; 397 $ligne->statut = $objp->statut; 398 $ligne->ref = $objp->ref; 399 $ligne->tva_tx = $objp->tva_tx; 400 $ligne->subprice = $objp->subprice; 401 $ligne->remise_percent = $objp->remise_percent; 402 $ligne->price = $objp->price; 403 $ligne->product_id = 0; 404 405 $ligne->date_debut_prevue = $objp->date_ouverture_prevue; 406 $ligne->date_debut_reel = $objp->date_ouverture; 407 $ligne->date_fin_prevue = $objp->date_fin_validite; 408 $ligne->date_fin_reel = $objp->date_cloture; 409 410 $this->lignes[$i] = $ligne; 411 $i++; 412 $j++; 413 } 414 415 $this->db->free($result); 416 } 417 else 418 { 419 dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrat non liées aux produits"); 420 $this->error=$this->db->error(); 421 return -2; 422 } 423 424 return $this->lignes; 425 } 426 427 /** 428 * \brief Crée un contrat vierge en base 429 * \param user Utilisateur qui crée 430 * \param langs Environnement langue de l'utilisateur 431 * \param conf Environnement de configuration lors de l'opération 432 * \return int <0 si erreur, id contrat créé sinon 433 */ 434 function create($user,$langs='',$conf='') 435 { 436 // Controle validité des paramètres 437 $paramsok=1; 438 if ($this->commercial_signature_id <= 0) 439 { 440 $langs->load("commercial"); 441 $this->error.=$langs->trans("ErrorFieldRequired",$langs->trans("SalesRepresentativeSignature")); 442 $paramsok=0; 443 } 444 if ($this->commercial_suivi_id <= 0) 445 { 446 $langs->load("commercial"); 447 $this->error.=($this->error?"<br>":''); 448 $this->error.=$langs->trans("ErrorFieldRequired",$langs->trans("SalesRepresentativeFollowUp")); 449 $paramsok=0; 450 } 451 if (! $paramsok) return -1; 452 453 $this->db->begin(); 454 455 // Insère contrat 456 $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat"; 457 // $sql.= ", fk_commercial_signature, fk_commercial_suivi"; 458 $sql.= " , ref)"; 459 $sql.= " VALUES (now(),".$this->soc_id.",".$user->id; 460 $sql.= ",".$this->db->idate($this->date_contrat); 461 // $sql.= ",".($this->commercial_signature_id>=0?$this->commercial_signature_id:"null"); 462 // $sql.= ",".($this->commercial_suivi_id>=0?$this->commercial_suivi_id:"null"); 463 $sql .= ", " . (strlen($this->ref)<=0 ? "null" : "'".$this->ref."'"); 464 $sql.= ")"; 465 $resql=$this->db->query($sql); 466 if ($resql) 467 { 468 $error=0; 469 470 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat"); 471 472 // Insère contacts commerciaux ('SALESREPSIGN','contrat') 473 $result=$this->add_contact($this->commercial_signature_id,'SALESREPSIGN','internal'); 474 if ($result < 0) $error++; 475 476 // Insère contacts commerciaux ('SALESREPFOLL','contrat') 477 $result=$this->add_contact($this->commercial_suivi_id,'SALESREPFOLL','internal'); 478 if ($result < 0) $error++; 479 480 if (! $error) 481 { 482 // Appel des triggers 483 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 484 $interface=new Interfaces($this->db); 485 $result=$interface->run_triggers('CONTRACT_CREATE',$this,$user,$langs,$conf); 486 if ($result < 0) $error++; 487 // Fin appel triggers 488 489 if (! $error) 490 { 491 $this->db->commit(); 492 return $this->id; 493 } 494 else 495 { 496 $this->error=$interface->error; 497 dolibarr_syslog("Contrat::create - 30 - ".$this->error); 498 499 $this->db->rollback(); 500 return -3; 501 } 502 } 503 else 504 { 505 $this->error="Failed to add contact"; 506 dolibarr_syslog("Contrat::create - 20 - ".$this->error); 507 508 $this->db->rollback(); 509 return -2; 510 } 511 } 512 else 513 { 514 $this->error=$langs->trans("UnknownError: ".$this->db->error()." - sql=".$sql); 515 dolibarr_syslog("Contrat::create - 10 - ".$this->error); 516 517 $this->db->rollback(); 518 return -1; 519 } 520 } 521 522 523 /** 524 * \brief Supprime un contrat de la base 525 * \param user Utilisateur qui supprime 526 * \param langs Environnement langue de l'utilisateur 527 * \param conf Environnement de configuration lors de l'opération 528 * \return int < 0 si erreur, > 0 si ok 529 */ 530 function delete($user,$langs='',$conf='') 531 { 532 $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat"; 533 $sql.= " WHERE rowid=".$this->id; 534 535 $resql=$this->db->query($sql); 536 if ($resql) 537 { 538 // Appel des triggers 539 include_once (DOL_DOCUMENT_ROOT . "/interfaces.class.php"); 540 $interface=new Interfaces($this->db); 541 $result=$interface->run_triggers('CONTRACT_DELETE',$this,$user,$langs,$conf); 542 // Fin appel triggers 543 544 return 1; 545 } 546 else 547 { 548 $this->error=$this->db->error(); 549 return -1; 550 } 551 } 552 553 554 /** 555 * \brief Ajoute une ligne de contrat en base 556 * \param desc Description de la ligne 557 * \param pu Prix unitaire 558 * \param qty Quantité 559 * \param txtva Taux tva 560 * \param fk_product Id produit 561 * \param remise_percent Pourcentage de remise de la ligne 562 * \param datestart Date de debut prévue 563 * \param dateend Date de fin prévue 564 * \return int <0 si erreur, >0 si ok 565 */ 566 function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart, $dateend) 567 { 568 global $langs; 569 570 dolibarr_syslog("contrat.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent, $datestart, $dateend"); 571 572 if ($this->statut == 0) 573 { 574 $qty = ereg_replace(",",".",$qty); 575 $pu = ereg_replace(",",".",$pu); 576 577 if ($fk_product > 0) 578 { 579 $prod = new Product($this->db, $fk_product); 580 if ($prod->fetch($fk_product) > 0) 581 { 582 $label = $prod->libelle; 583 $pu = $prod->price; 584 $txtva = $prod->tva_tx; 585 } 586 } 587 588 $remise = 0; 589 $price = ereg_replace(",",".",round($pu, 2)); 590 $subprice = $price; 591 if (strlen($remise_percent) > 0) 592 { 593 $remise = round(($pu * $remise_percent / 100), 2); 594 $price = $pu - $remise; 595 } 596 597 // Insertion dans la base 598 $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet"; 599 $sql.= " (fk_contrat, label, description, fk_product, price_ht, qty, tva_tx,"; 600 $sql.= " remise_percent, subprice, remise"; 601 if ($datestart > 0) { $sql.= ",date_ouverture_prevue"; } 602 if ($dateend > 0) { $sql.= ",date_fin_validite"; } 603 $sql.= ") VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',"; 604 $sql.= ($fk_product>0 ? $fk_product : "null"); 605 $sql.= ",".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."'"; 606 if ($datestart > 0) { $sql.= ",".$this->db->idate($datestart); } 607 if ($dateend > 0) { $sql.= ",".$this->db->idate($dateend); } 608 $sql.= ");"; 609 610 if ( $this->db->query($sql) ) 611 { 612 $this->update_price(); 613 return 1; 614 } 615 else 616 { 617 $this->error=$this->db->error(); 618 return -1; 619 } 620 } 621 else 622 { 623 return -2; 624 } 625 } 626 627 /** 628 * \brief Mets à jour une ligne de contrat 629 * \param rowid Id de la ligne de facture 630 * \param desc Description de la ligne 631 * \param pu Prix unitaire 632 * \param qty Quantité 633 * \param remise_percent Pourcentage de remise de la ligne 634 * \param datestart Date de debut prévue 635 * \param dateend Date de fin prévue 636 * \param tvatx Taux TVA 637 * \param date_debut_reel Date de debut réelle 638 * \param date_fin_reel Date de fin réelle 639 * \return int < 0 si erreur, > 0 si ok 640 */ 641 function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, 642 $datestart='', $dateend='', $tvatx, 643 $date_debut_reel='', $date_fin_reel='') 644 { 645 // Nettoyage parametres 646 $qty=trim($qty); 647 $desc=trim($desc); 648 $desc=trim($desc); 649 $price = ereg_replace(",",".",$pu); 650 $tvatx = ereg_replace(",",".",$tvatx); 651 $subprice = $price; 652 $remise = 0; 653 if (strlen($remise_percent) > 0) 654 { 655 $remise = round(($pu * $remise_percent / 100), 2); 656 $price = $pu - $remise; 657 } 658 else 659 { 660 $remise_percent=0; 661 } 662 663 dolibarr_syslog("Contrat::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $datestart, $dateend, $tvatx"); 664 665 $this->db->begin(); 666 667 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".addslashes($desc)."'"; 668 $sql .= ",price_ht='" . ereg_replace(",",".",$price)."'"; 669 $sql .= ",subprice='" . ereg_replace(",",".",$subprice)."'"; 670 $sql .= ",remise='" . ereg_replace(",",".",$remise)."'"; 671 $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'"; 672 $sql .= ",qty='$qty'"; 673 $sql .= ",tva_tx='". ereg_replace(",",".",$tvatx)."'"; 674 675 if ($datestart > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($datestart); } 676 else { $sql.=",date_ouverture_prevue=null"; } 677 if ($dateend > 0) { $sql.= ",date_fin_validite=".$this->db->idate($dateend); } 678 else { $sql.=",date_fin_validite=null"; } 679 if ($date_debut_reel > 0) { $sql.= ",date_ouverture=".$this->db->idate($date_debut_reel); } 680 else { $sql.=",date_ouverture=null"; } 681 if ($date_fin_reel > 0) { $sql.= ",date_cloture=".$this->db->idate($date_fin_reel); } 682 else { $sql.=",date_cloture=null"; } 683 684 $sql .= " WHERE rowid = $rowid ;"; 685 686 $result = $this->db->query($sql); 687 if ($result) 688 { 689 $this->update_price(); 690 691 $this->db->commit(); 692 693 return 1; 694 } 695 else 696 { 697 $this->db->rollback(); 698 $this->error=$this->db->error(); 699 dolibarr_syslog("Contrat::UpdateLigne Erreur -1"); 700 701 return -1; 702 } 703 } 704 705 /** 706 * \brief Supprime une ligne de detail 707 * \param idligne Id de la ligne detail à supprimer 708 * \return int >0 si ok, <0 si ko 709 */ 710 function delete_line($idligne) 711 { 712 if ($this->statut == 0) 713 { 714 $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet WHERE rowid =".$idligne; 715 716 if ($this->db->query($sql) ) 717 { 718 $this->update_price(); 719 720 return 1; 721 } 722 else 723 { 724 return -1; 725 } 726 } 727 else 728 { 729 return -2; 730 } 731 } 732 733 734 /** 735 * \brief Mets à jour le prix total du contrat 736 */ 737 738 function update_price() 739 { 740 include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; 741 742 /* 743 * Liste des produits a ajouter 744 */ 745 $sql = "SELECT price_ht, qty, tva_tx"; 746 $sql.= " FROM ".MAIN_DB_PREFIX."contratdet"; 747 $sql.= " WHERE fk_contrat = ".$this->id; 748 $resql=$this->db->query($sql); 749 if ($resql) 750 { 751 $num = $this->db->num_rows($resql); 752 $i = 0; 753 754 while ($i < $num) 755 { 756 $obj = $this->db->fetch_object($resql); 757 $products[$i][0] = $obj->price_ht; 758 $products[$i][1] = $obj->qty; 759 $products[$i][2] = $obj->tva_tx; 760 $i++; 761 } 762 } 763 else 764 { 765 $this->error=$this->db->error(); 766 return -1; 767 } 768 $calculs = calcul_price($products, $this->remise_percent); 769 770 $this->remise = $calculs[3]; 771 $this->total_ht = $calculs[0]; 772 $this->total_tva = $calculs[1]; 773 $this->total_ttc = $calculs[2]; 774 } 775 776 777 /** 778 * \brief Classe le contrat dans un projet 779 * \param projid Id du projet dans lequel classer le contrat 780 */ 781 function classin($projid) 782 { 783 $sql = "UPDATE ".MAIN_DB_PREFIX."contrat"; 784 if ($projid) $sql.= " SET fk_projet = $projid"; 785 else $sql.= " SET fk_projet = NULL"; 786 $sql.= " WHERE rowid = ".$this->id; 787 788 if ($this->db->query($sql)) 789 { 790 return 1; 791 } 792 else 793 { 794 dolibarr_print_error($this->db); 795 return -1; 796 } 797 } 798 799 800 /** 801 * \brief Retourne le libellé du statut du contrat 802 * \return string Libellé 803 */ 804 function getLibStatut() 805 { 806 return $this->LibStatut($this->statut); 807 } 808 809 /** 810 * \brief Renvoi le libellé d'un statut donné 811 * \param statut id statut 812 * \return string Libellé 813 */ 814 function LibStatut($statut) 815 { 816 global $langs; 817 $langs->load("contracts"); 818 819 if ($statut == 0) { return $langs->trans("ContractStatusDraft"); } 820 if ($statut == 1) { return $langs->trans("ContractStatusValidated"); } 821 if ($statut == 2) { return $langs->trans("ContractStatusClosed"); } 822 } 823 824 825 /* 826 * \brief Charge les informations d'ordre info dans l'objet contrat 827 * \param id id du contrat a charger 828 */ 829 function info($id) 830 { 831 $sql = "SELECT c.rowid, c.ref, ".$this->db->pdate("datec")." as datec, ".$this->db->pdate("date_cloture")." as date_cloture,"; 832 $sql.= $this->db->pdate("c.tms")." as date_modification,"; 833 $sql.= " fk_user_author, fk_user_cloture"; 834 $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c"; 835 $sql.= " WHERE c.rowid = ".$id; 836 837 $result=$this->db->query($sql); 838 if ($result) 839 { 840 if ($this->db->num_rows($result)) 841 { 842 $obj = $this->db->fetch_object($result); 843 844 $this->id = $obj->rowid; 845 846 if ($obj->fk_user_author) { 847 $cuser = new User($this->db, $obj->fk_user_author); 848 $cuser->fetch(); 849 $this->user_creation = $cuser; 850 } 851 852 if ($obj->fk_user_cloture) { 853 $cuser = new User($this->db, $obj->fk_user_cloture); 854 $cuser->fetch(); 855 $this->user_cloture = $cuser; 856 } 857 $this->ref = (! $obj->ref) ? $obj->rowid : $obj->ref; 858 $this->date_creation = $obj->datec; 859 $this->date_modification = $obj->date_modification; 860 $this->date_cloture = $obj->date_cloture; 861 } 862 863 $this->db->free($result); 864 865 } 866 else 867 { 868 dolibarr_print_error($this->db); 869 } 870 } 871 872 873 /** 874 * \brief Récupère les lignes de detail du contrat 875 * \param statut Statut des lignes detail à récupérer 876 * \return array Tableau des lignes de details 877 */ 878 function array_detail($statut=-1) 879 { 880 $tab=array(); 881 882 $sql = "SELECT cd.rowid"; 883 $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd"; 884 $sql.= " WHERE fk_contrat =".$this->id; 885 if ($statut >= 0) $sql.= " AND statut = '$statut'"; 886 887 $resql=$this->db->query($sql); 888 if ($resql) 889 { 890 $num=$this->db->num_rows($resql); 891 $i=0; 892 while ($i < $num) 893 { 894 $obj = $this->db->fetch_object($resql); 895 $tab[$i]=$obj->rowid; 896 $i++; 897 } 898 return $tab; 899 } 900 else 901 { 902 $this->error=$this->db->error(); 903 return -1; 904 } 905 } 906 907 908 /** 909 * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord 910 * \param user Objet user 911 * \param mode "inactive" pour services à activer, "expired" pour services expirés 912 * \return int <0 si ko, >0 si ok 913 */ 914 function load_board($user,$mode) 915 { 916 global $conf; 917 918 $this->nbtodo=$this->nbtodolate=0; 919 if ($mode == 'inactives') 920 { 921 $sql = "SELECT cd.rowid,".$this->db->pdate("cd.date_ouverture_prevue")." as datefin"; 922 $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd"; 923 $sql.= " WHERE c.statut = 1 AND c.rowid = cd.fk_contrat"; 924 $sql.= " AND cd.statut = 0"; 925 } 926 if ($mode == 'expired') 927 { 928 $sql = "SELECT cd.rowid,".$this->db->pdate("cd.date_fin_validite")." as datefin"; 929 $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd"; 930 $sql.= " WHERE c.statut = 1 AND c.rowid = cd.fk_contrat"; 931 $sql.= " AND cd.statut = 4"; 932 $sql.= " AND cd.date_fin_validite < '".$this->db->idate(time())."'"; 933 } 934 if ($user->societe_id) $sql.=" AND fk_soc = ".$user->societe_id; 935 $resql=$this->db->query($sql); 936 if ($resql) 937 { 938 while ($obj=$this->db->fetch_object($resql)) 939 { 940 $this->nbtodo++; 941 if ($mode == 'inactives') 942 if ($obj->datefin && $obj->datefin < (time() - $conf->contrat->services->inactifs->warning_delay)) $this->nbtodolate++; 943 if ($mode == 'expired') 944 if ($obj->datefin && $obj->datefin < (time() - $conf->contrat->services->expires->warning_delay)) $this->nbtodolate++; 945 } 946 return 1; 947 } 948 else 949 { 950 dolibarr_print_error($this->db); 951 $this->error=$this->db->error(); 952 return -1; 953 } 954 } 955 956 957 /* gestion des contacts d'un contrat */ 958 959 /** 960 * \brief Ajoute un contact associé au contrat 961 * \param fk_socpeople Id du contact a ajouter. 962 * \param type_contact Type de contact 963 * \param source extern=Contact externe (llx_socpeople), intern=Contact interne (llx_user) 964 * \return int <0 si erreur, >0 si ok 965 */ 966 function add_contact($fk_socpeople, $type_contact, $source='extern') 967 { 968 dolibarr_syslog("Contrat::add_contact $fk_socpeople, $type_contact, $source"); 969 970 if ($fk_socpeople <= 0) return -1; 971 972 // Si type_contact = texte, aller chercher code dans table llx_c_type_contact 973 if ($type_contact == 'SALESREPSIGN' || $type_contact == 'SALESREPFOLL') 974 { 975 $sql.="SELECT rowid from ".MAIN_DB_PREFIX."c_type_contact"; 976 $sql.=" WHERE element='contrat' AND source='internal'"; 977 $sql.=" AND code='".$type_contact."'"; 978 $resql = $this->db->query($sql); 979 if ( $resql ) 980 { 981 $obj = $this->db->fetch_object($resql); 982 if ($obj) $type_contact=$obj->rowid; 983 } 984 else 985 { 986 dolibarr_print_error($this->db); 987 $this->error=$this->db->error()." - $sql"; 988 return -2; 989 } 990 } 991 992 // Verifie type_contact 993 if (! $type_contact) 994 { 995 $this->error="Valeur pour type_contact incorrect"; 996 return -3; 997 } 998 999 $datecreate = time(); 1000 1001 // Insertion dans la base 1002 $sql = "INSERT INTO ".MAIN_DB_PREFIX."element_contact"; 1003 $sql.= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) "; 1004 $sql.= " VALUES (".$this->id.", ".$fk_socpeople." , " ; 1005 $sql.= $this->db->idate($datecreate); 1006 $sql.= ", 4, '". $type_contact . "' "; 1007 $sql.= ")"; 1008 1009 // Retour 1010 if ( $this->db->query($sql) ) 1011 { 1012 return 1; 1013 } 1014 else 1015 { 1016 $this->error=$this->db->error()." - $sql"; 1017 return -1; 1018 } 1019 } 1020 1021 /** 1022 * \brief Misea jour du contact associé au contrat 1023 * \param rowid La reference du lien contrat-contact 1024 * \param statut Le nouveau statut 1025 * \param nature Description du contact 1026 * \return int <0 si erreur, =0 si ok 1027 */ 1028 function update_contact($rowid, $statut, $type_contact_id) 1029 { 1030 // Insertion dans la base 1031 $sql = "UPDATE ".MAIN_DB_PREFIX."element_contact set "; 1032 $sql.= " statut = $statut ,"; 1033 $sql.= " fk_c_type_contact = '".$type_contact_id ."'"; 1034 $sql.= " where rowid = ".$rowid; 1035 // Retour 1036 if ( $this->db->query($sql) ) 1037 { 1038 return 0; 1039 } 1040 else 1041 { 1042 dolibarr_print_error($this->db); 1043 return -1; 1044 } 1045 } 1046 1047 /** 1048 * \brief Supprime une ligne de contact 1049 * \param idligne La reference du contact 1050 * \return statur >0 si ok, <0 si ko 1051 */ 1052 function delete_contact($idligne) 1053 { 1054 1055 $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; 1056 $sql.= " WHERE rowid =".$idligne; 1057 if ($this->db->query($sql)) 1058 { 1059 return 1; 1060 } 1061 else 1062 { 1063 return -1; 1064 } 1065 } 1066 1067 /** 1068 * \brief Récupère les lignes de contact de l'objet 1069 * \param statut Statut des lignes detail à récupérer 1070 * \param source Source du contact external (llx_socpeople) ou internal (llx_user) 1071 * \return array Tableau des rowid des contacts 1072 */ 1073 function liste_contact($statut=-1,$source='external') 1074 { 1075 global $langs; 1076 1077 $element='contrat'; // Contact sur le contrat 1078 1079 $tab=array(); 1080 1081 $sql = "SELECT ec.rowid, ec.statut, ec.fk_socpeople as id,"; 1082 if ($source == 'internal') $sql.=" '-1' as socid,"; 1083 if ($source == 'external') $sql.=" t.fk_soc as socid,"; 1084 if ($source == 'internal') $sql.=" t.name as nom,"; 1085 if ($source == 'external') $sql.=" t.name as nom,"; 1086 $sql.= "tc.source, tc.element, tc.code, tc.libelle"; 1087 $sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,"; 1088 if ($source == 'internal') $sql.=" ".MAIN_DB_PREFIX."user t,"; 1089 if ($source == 'external') $sql.=" ".MAIN_DB_PREFIX."socpeople t,"; 1090 $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc"; 1091 $sql.= " WHERE element_id =".$this->id; 1092 $sql.= " AND ec.fk_c_type_contact=tc.rowid"; 1093 $sql.= " AND tc.element='".$element."'"; 1094 if ($source == 'internal') $sql.= " AND tc.source = 'internal'"; 1095 if ($source == 'external') $sql.= " AND tc.source = 'external'"; 1096 $sql.= " AND tc.active=1"; 1097 if ($source == 'internal') $sql.= " AND ec.fk_socpeople = t.rowid"; 1098 if ($source == 'external') $sql.= " AND ec.fk_socpeople = t.idp"; 1099 if ($statut >= 0) $sql.= " AND statut = '$statut'"; 1100 $sql.=" ORDER BY t.name ASC"; 1101 1102 $resql=$this->db->query($sql); 1103 if ($resql) 1104 { 1105 $num=$this->db->num_rows($resql); 1106 $i=0; 1107 while ($i < $num) 1108 { 1109 $obj = $this->db->fetch_object($resql); 1110 1111 $transkey="TypeContact_".$obj->element."_".$obj->source."_".$obj->code; 1112 $libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle); 1113 $tab[$i]=array('source'=>$obj->source,'socid'=>$obj->socid,'id'=>$obj->id,'nom'=>$obj->nom, 1114 'rowid'=>$obj->rowid,'code'=>$obj->code,'libelle'=>$libelle_type,'status'=>$obj->statut); 1115 $i++; 1116 } 1117 return $tab; 1118 } 1119 else 1120 { 1121 $this->error=$this->db->error(); 1122 dolibarr_print_error($this->db); 1123 return -1; 1124 } 1125 } 1126 1127 /** 1128 * \brief Le détail d'un contact 1129 * \param rowid L'identifiant du contact 1130 * \return object L'objet construit par DoliDb.fetch_object 1131 */ 1132 function detail_contact($rowid) 1133 { 1134 $element='contrat'; // Contact sur le contrat 1135 1136 $sql = "SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact,"; 1137 $sql.= " tc.code, tc.libelle, s.fk_soc"; 1138 $sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc, "; 1139 $sql.= " ".MAIN_DB_PREFIX."socpeople as s"; 1140 $sql.= " WHERE ec.rowid =".$rowid; 1141 $sql.= " AND ec.fk_socpeople=s.idp"; 1142 $sql.= " AND ec.fk_c_type_contact=tc.rowid"; 1143 $sql.= " AND tc.element = '".$element."'"; 1144 1145 $resql=$this->db->query($sql); 1146 if ($resql) 1147 { 1148 $obj = $this->db->fetch_object($resql); 1149 return $obj; 1150 } 1151 else 1152 { 1153 $this->error=$this->db->error(); 1154 dolibarr_print_error($this->db); 1155 return null; 1156 } 1157 } 1158 1159 /** 1160 * \brief La liste des valeurs possibles de type de contacts 1161 * \param source internal ou externam 1162 * \return array La liste des natures 1163 */ 1164 function liste_type_contact($source) 1165 { 1166 global $langs; 1167 1168 $element='contrat'; // Contact sur le contrat 1169 1170 $tab = array(); 1171 1172 $sql = "SELECT distinct tc.rowid, tc.code, tc.libelle"; 1173 $sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc"; 1174 $sql.= " WHERE element='".$element."'"; 1175 $sql.= " AND source='".$source."'"; 1176 $sql.= " ORDER by tc.code"; 1177 1178 $resql=$this->db->query($sql); 1179 if ($resql) 1180 { 1181 $num=$this->db->num_rows($resql); 1182 $i=0; 1183 while ($i < $num) 1184 { 1185 $obj = $this->db->fetch_object($resql); 1186 1187 $transkey="TypeContact_".$element."_".$source."_".$obj->code; 1188 $libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle); 1189 $tab[$obj->rowid]=$libelle_type; 1190 $i++; 1191 } 1192 return $tab; 1193 } 1194 else 1195 { 1196 $this->error=$this->db->error(); 1197 // dolibarr_print_error($this->db); 1198 return null; 1199 } 1200 } 1201 1202 /** 1203 * \brief Retourne id des contacts d'une source et d'un type donné 1204 * Exemple: contact client de facturation ('external', 'BILLING') 1205 * Exemple: contact client de livraison ('external', 'SHIPPING') 1206 * Exemple: contact interne suivi paiement ('internal', 'SALESREPFOLL') 1207 * \return array Liste des id contacts 1208 */ 1209 function getIdContact($source,$code) 1210 { 1211 $element='contrat'; // Contact sur le contrat 1212 1213 $result=array(); 1214 $i=0; 1215 1216 $sql = "SELECT ec.fk_socpeople"; 1217 $sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc"; 1218 $sql.= " WHERE ec.element_id = ".$this->id; 1219 $sql.= " AND ec.fk_c_type_contact=tc.rowid"; 1220 $sql.= " AND tc.element = '".$element."'"; 1221 $sql.= " AND tc.source = '".$source."'"; 1222 $sql.= " AND tc.code = '".$code."'"; 1223 1224 $resql=$this->db->query($sql); 1225 if ($resql) 1226 { 1227 while ($obj = $this->db->fetch_object($resql)) 1228 { 1229 $result[$i]=$obj->fk_socpeople; 1230 $i++; 1231 } 1232 } 1233 else 1234 { 1235 $this->error=$this->db->error(); 1236 return null; 1237 } 1238 1239 return $result; 1240 } 1241 1242 /** 1243 * \brief Retourne id des contacts clients de facturation 1244 * \return array Liste des id contacts facturation 1245 */ 1246 function getIdBillingContact() 1247 { 1248 return $this->getIdContact('external','BILLING'); 1249 } 1250 1251 /** 1252 * \brief Retourne id des contacts clients de prestation 1253 * \return array Liste des id contacts prestation 1254 */ 1255 function getIdServiceContact() 1256 { 1257 return $this->getIdContact('external','SERVICE'); 1258 } 1259 1260 } 1261 1262 1263 /** 1264 \class ContratLigne 1265 \brief Classe permettant la gestion des lignes de contrats 1266 */ 1267 1268 class ContratLigne 1269 { 1270 var $id; 1271 var $desc; 1272 var $libelle; 1273 var $product_desc; 1274 var $qty; 1275 var $ref; 1276 var $tva_tx; 1277 var $subprice; 1278 var $remise_percent; 1279 var $price; 1280 var $product_id; 1281 1282 var $statut; 1283 var $date_debut_prevue; 1284 var $date_debut_reel; 1285 var $date_fin_prevue; 1286 var $date_fin_reel; 1287 1288 function ContratLigne() 1289 { 1290 } 1291 1292 function is_activated() 1293 { 1294 return $this->statut == 4 ; 1295 } 1296 } 1297 1298 1299 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 12:29:37 2007 | par Balluche grâce à PHPXref 0.7 |
|