| [ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?PHP 2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * $Id: telephonie.contrat.class.php,v 1.22 2005/12/15 10:06:40 rodolphe Exp $ 19 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/telephonie/telephonie.contrat.class.php,v $ 20 * 21 */ 22 23 class TelephonieContrat { 24 var $db; 25 var $id; 26 var $ligne; 27 28 function TelephonieContrat($DB, $id=0) 29 { 30 global $config; 31 32 $this->db = $DB; 33 $this->error_message = ''; 34 $this->statuts[-1] = "En attente"; 35 $this->statuts[0] = "En traitement"; 36 $this->statuts[1] = "A commander"; 37 $this->statuts[2] = "En commande"; 38 $this->statuts[3] = "Actif"; 39 $this->statuts[4] = "A résilier"; 40 $this->statuts[5] = "Résiliation demandée"; 41 $this->statuts[6] = "Résilié"; 42 $this->statuts[7] = "Rejeté"; 43 44 return 1; 45 } 46 /* 47 * Creation du contrat 48 * Le commercial qui fait le suivi est par defaut le commercial qui a signe 49 */ 50 function create($user, $isfacturable='oui', $mode_paiement='pre') 51 { 52 $this->mode_paiement = $mode_paiement; 53 54 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat"; 55 $sql .= " (ref, fk_soc, fk_client_comm, fk_soc_facture, note"; 56 $sql .= " , fk_commercial_sign, fk_commercial_suiv, fk_user_creat, date_creat)"; 57 58 $sql .= " VALUES ('PROV".time()."'"; 59 60 $sql .= ", $this->client,$this->client_comm,$this->client_facture,'$this->note'"; 61 $sql .= ",$this->commercial_sign, $this->commercial_sign, $user->id, now())"; 62 63 if ( $this->db->query($sql) ) 64 { 65 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."telephonie_contrat"); 66 67 $sql = "UPDATE ".MAIN_DB_PREFIX."telephonie_contrat"; 68 $sql .= " SET ref='".substr("00000000".$this->id,-8)."'"; 69 $sql .= " , isfacturable = '".$isfacturable."'"; 70 $sql .= " , mode_paiement = '".$mode_paiement."'"; 71 $sql .= " WHERE rowid=".$this->id; 72 $this->db->query($sql); 73 74 /* 75 * On applique la grille de tarif du distributeur 76 * 77 */ 78 $grille_tarif = 0; 79 80 $sql = "SELECT d.grille_tarif "; 81 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_distributeur as d"; 82 $sql .= " , ".MAIN_DB_PREFIX."telephonie_distributeur_commerciaux as dc"; 83 $sql .= " WHERE dc.fk_distributeur = d.rowid"; 84 $sql .= " AND dc.fk_user = ".$this->commercial_sign; 85 86 $resql = $this->db->query($sql); 87 88 if ($resql) 89 { 90 if ($this->db->num_rows($resql)) 91 { 92 $row = $this->db->fetch_row($resql); 93 94 $grille_tarif = $row[0]; 95 } 96 $this->db->free($resql); 97 } 98 99 100 if ($grille_tarif > 0) 101 { 102 $sql = "UPDATE ".MAIN_DB_PREFIX."telephonie_contrat"; 103 $sql .= " SET grille_tarif =".$grille_tarif; 104 $sql .= " WHERE rowid=".$this->id; 105 $this->db->query($sql); 106 } 107 108 return 0; 109 } 110 else 111 { 112 $this->error_message = "Echec de la création du contrat"; 113 dolibarr_syslog("TelephonieContrat::Create Error -1"); 114 dolibarr_syslog($this->db->error()); 115 return -1; 116 } 117 } 118 /* 119 * 120 * 121 */ 122 function update($user) 123 { 124 $error = 0 ; 125 126 if (!$this->db->begin()) 127 { 128 $error++; 129 dolibarr_syslog("TelephonieContrat::Update Error -1"); 130 } 131 132 if (!$error) 133 { 134 135 $sql = "UPDATE ".MAIN_DB_PREFIX."telephonie_contrat"; 136 $sql .= " SET "; 137 $sql .= " fk_soc = ".$this->client ; 138 $sql .= ", fk_soc_facture = ".$this->client_facture; 139 $sql .= ", fk_commercial_suiv = ".$this->commercial_suiv_id; 140 $sql .= ", mode_paiement = '".$this->mode_paiement."'"; 141 $sql .= ", note = '$this->note'"; 142 143 $sql .= " WHERE rowid = ".$this->id; 144 145 if (! $this->db->query($sql) ) 146 { 147 $error++; 148 dolibarr_syslog("TelephonieContrat::Update Error -2"); 149 } 150 } 151 152 if (!$error) 153 { 154 $sql = "UPDATE ".MAIN_DB_PREFIX."telephonie_societe_ligne"; 155 $sql .= " SET "; 156 $sql .= " fk_soc = ".$this->client ; 157 $sql .= ", fk_soc_facture = ".$this->client_facture; 158 $sql .= ", fk_commercial_suiv = ".$this->commercial_suiv_id; 159 $sql .= ", mode_paiement = '".$this->mode_paiement."'"; 160 $sql .= " WHERE fk_contrat = ".$this->id; 161 162 163 if (! $this->db->query($sql) ) 164 { 165 $error++; 166 dolibarr_syslog("TelephonieContrat::Update Error -3"); 167 } 168 } 169 170 if (!$error) 171 { 172 $this->db->commit(); 173 return 0; 174 } 175 else 176 { 177 $this->db->rollback(); 178 return -1; 179 } 180 } 181 /* 182 * 183 * 184 * 185 */ 186 function fetch($id) 187 { 188 $sql = "SELECT c.rowid, c.ref, c.fk_client_comm, c.fk_soc, c.fk_soc_facture, c.note"; 189 $sql .= ", c.fk_commercial_sign, c.fk_commercial_suiv"; 190 $sql .= ", c.isfacturable, c.mode_paiement, c.statut"; 191 $sql .= ", c.fk_user_creat, ".$this->db->pdate("c.date_creat") ." as dc"; 192 $sql .= ", c.grille_tarif"; 193 194 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_contrat as c"; 195 $sql .= " WHERE c.rowid = ".$id; 196 197 $resql = $this->db->query($sql); 198 199 if ($resql) 200 { 201 if ($this->db->num_rows($resql)) 202 { 203 $obj = $this->db->fetch_object($resql); 204 205 $this->id = $obj->rowid; 206 $this->socid = $obj->fk_soc; 207 $this->ref = $obj->ref; 208 $this->remise = $obj->remise; 209 $this->client_comm_id = $obj->fk_client_comm; 210 $this->client_id = $obj->fk_soc; 211 $this->client_facture_id = $obj->fk_soc_facture; 212 213 $this->commercial_sign_id = $obj->fk_commercial_sign; 214 $this->commercial_suiv_id = $obj->fk_commercial_suiv; 215 216 $this->statut = $obj->statut; 217 $this->mode_paiement = $obj->mode_paiement; 218 $this->code_analytique = $obj->code_analytique; 219 220 $this->user_creat = $obj->fk_user_creat; 221 $this->date_creat = $obj->dc; 222 223 $this->grille_tarif_id = $obj->grille_tarif; 224 225 if ($obj->isfacturable == 'oui') 226 { 227 $this->facturable = 1; 228 } 229 else 230 { 231 $this->facturable = 0; 232 } 233 234 $this->ref_url = '<a href="'.DOL_URL_ROOT.'/telephonie/contrat/fiche.php?id='.$this->id.'">'.$this->ref.'</a>'; 235 236 237 $result = 1; 238 } 239 else 240 { 241 dolibarr_syslog("TelephonieContrat::Fecth Erreur -2"); 242 $result = -2; 243 } 244 245 $this->db->free($resql); 246 } 247 else 248 { 249 /* Erreur select SQL */ 250 print $this->db->error(); 251 $result = -1; 252 dolibarr_syslog("TelephonieContrat::Fecth Erreur -1"); 253 } 254 255 $sql = "SELECT libelle"; 256 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_tarif_grille"; 257 $sql .= " WHERE rowid = ".$this->grille_tarif_id; 258 259 $resql = $this->db->query($sql); 260 261 if ($resql) 262 { 263 if ($this->db->num_rows($resql)) 264 { 265 $obj = $this->db->fetch_object($resql); 266 267 $this->grille_tarif_nom = $obj->libelle; 268 } 269 $this->db->free($resql); 270 } 271 272 273 return $result; 274 } 275 /* 276 * 277 * 278 */ 279 function delete() 280 { 281 $sql = "DELETE FROM ".MAIN_DB_PREFIX."telephonie_contrat"; 282 $sql .= " WHERE rowid = ".$this->id; 283 284 $this->db->query($sql); 285 } 286 /* 287 * 288 * 289 * 290 */ 291 function load_previous_next_id($filtre='') 292 { 293 $sql = "SELECT rowid"; 294 $sql.= " FROM ".MAIN_DB_PREFIX."telephonie_contrat"; 295 $sql.= " WHERE rowid > ".$this->id.""; 296 $sql .= " ORDER BY rowid ASC LIMIT 1"; 297 298 $resql = $this->db->query($sql) ; 299 if ($resql) 300 { 301 while ($row = $this->db->fetch_row($resql)) 302 { 303 $this->ref_next = $row[0]; 304 } 305 } 306 307 $sql = "SELECT rowid"; 308 $sql.= " FROM ".MAIN_DB_PREFIX."telephonie_contrat"; 309 $sql.= " WHERE rowid < ".$this->id.""; 310 $sql .= " ORDER BY rowid DESC LIMIT 1"; 311 312 $resql = $this->db->query($sql) ; 313 if ($resql) 314 { 315 while ($row = $this->db->fetch_row($resql)) 316 { 317 $this->ref_previous = $row[0]; 318 } 319 } 320 321 322 323 324 return 1; 325 } 326 /* 327 * 328 * 329 * 330 */ 331 function add_contact_facture($cid) 332 { 333 334 $this->del_contact_facture($cid); 335 336 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat_contact_facture"; 337 $sql .= " (fk_contrat, fk_contact) "; 338 $sql .= " VALUES ($this->id, $cid )"; 339 340 $this->db->query($sql); 341 } 342 /* 343 * 344 * 345 */ 346 function del_contact_facture($cid) 347 { 348 349 $sql = "DELETE FROM ".MAIN_DB_PREFIX."telephonie_contrat_contact_facture"; 350 $sql .= " WHERE fk_contrat=".$this->id." AND fk_contact=".$cid; 351 352 return $this->db->query($sql); 353 } 354 /* 355 * 356 * 357 */ 358 function count_associated_services() 359 { 360 $num = 0; 361 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."telephonie_contrat_service"; 362 $sql .= " WHERE fk_contrat=".$this->id; 363 364 if ( $this->db->query( $sql) ) 365 { 366 $num = $this->db->num_rows(); 367 } 368 369 return $num; 370 } 371 /* 372 * 373 * 374 */ 375 function add_service($user, $sid) 376 { 377 $result = 0; 378 379 $sql = "SELECT montant FROM ".MAIN_DB_PREFIX."telephonie_service"; 380 $sql .= " WHERE rowid=".$sid; 381 382 $resql = $this->db->query( $sql); 383 384 if ($resql) 385 { 386 $row = $this->db->fetch_row($resql); 387 $montant = $row[0]; 388 } 389 else 390 { 391 $result = -1; 392 } 393 394 395 if ($result == 0) 396 { 397 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat_service"; 398 $sql .= " (fk_contrat, fk_service, fk_user_creat, date_creat, montant) "; 399 $sql .= " VALUES ($this->id, $sid, $user->id, now(),".$montant.")"; 400 401 $resql = $this->db->query( $sql); 402 403 if ($resql) 404 { 405 return 0 ; 406 } 407 } 408 } 409 /* 410 * 411 * 412 */ 413 function remove_service($user, $sid) 414 { 415 $sql = "DELETE FROM ".MAIN_DB_PREFIX."telephonie_contrat_service"; 416 $sql .= " WHERE fk_contrat = ".$this->id; 417 $sql .= " AND rowid = ".$sid; 418 419 if ($this->db->query($sql) ) 420 { 421 return 0 ; 422 } 423 } 424 /* 425 * 426 * 427 */ 428 function get_contact_facture() 429 { 430 $this->contact_facture_id = array(); 431 $res = array(); 432 $resid = array(); 433 434 $sql = "SELECT c.idp, c.name, c.firstname, c.email "; 435 $sql .= "FROM ".MAIN_DB_PREFIX."socpeople as c"; 436 $sql .= ",".MAIN_DB_PREFIX."telephonie_contrat_contact_facture as cf"; 437 $sql .= " WHERE c.idp = cf.fk_contact "; 438 $sql .= " AND cf.fk_contrat = ".$this->id." ORDER BY name "; 439 440 $resql = $this->db->query($sql); 441 442 if ( $resql ) 443 { 444 $num = $this->db->num_rows($resql); 445 if ( $num > 0 ) 446 { 447 $i = 0; 448 while ($i < $num) 449 { 450 $row = $this->db->fetch_row($resql); 451 452 array_push($res, $row[1] . " " . $row[2] . " <".$row[3].">"); 453 array_push($resid, $row[0]); 454 $i++; 455 } 456 457 $this->db->free($resql); 458 } 459 } 460 $this->contact_facture_id = $resid; 461 return $res; 462 } 463 464 /* 465 * 466 * 467 */ 468 function priseordre_totale() 469 { 470 $po = 0; 471 $sql = "SELECT sum(montant) "; 472 $sql .= "FROM ".MAIN_DB_PREFIX."telephonie_contrat_priseordre"; 473 $sql .= " WHERE fk_contrat = ".$this->id; 474 $resql = $this->db->query($sql); 475 if ($resql) 476 { 477 $num = $this->db->num_rows($resql); 478 if ( $num > 0 ) 479 { 480 $row = $this->db->fetch_row($resql); 481 $po = $row[0]; 482 } 483 $this->db->free(); 484 485 } 486 else 487 { 488 dolibarr_syslog($sql); 489 } 490 return $po; 491 } 492 493 /* 494 * 495 * 496 */ 497 function addpo($montant, $user) 498 { 499 $po = 0; 500 501 $montant = ereg_replace(",",".",$montant); 502 503 if ($montant > 0) 504 { 505 $did = 0; 506 507 $sql = "SELECT dc.fk_distributeur "; 508 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_distributeur_commerciaux as dc"; 509 $sql .= " WHERE dc.fk_user = ".$this->commercial_sign_id; 510 511 $resql = $this->db->query($sql); 512 513 if ($resql) 514 { 515 if ($this->db->num_rows($resql)) 516 { 517 $row = $this->db->fetch_row($resql); 518 $did = $row[0]; 519 } 520 $this->db->free($resql); 521 } 522 523 524 if ($did > 0) 525 { 526 527 $sql = "SELECT avance_pourcent, avance_duree, rem_pour_prev, rem_pour_autr"; 528 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_distributeur "; 529 $sql .= " WHERE rowid = ".$did; 530 531 $resql = $this->db->query($sql); 532 533 if ($resql) 534 { 535 if ($this->db->num_rows($resql)) 536 { 537 $pour = $this->db->fetch_row($resql); 538 } 539 $this->db->free($resql); 540 } 541 542 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat_priseordre"; 543 $sql .= " (fk_contrat, datepo, montant, fk_commercial, fk_distributeur, fk_user"; 544 $sql .= " , avance_pourcent, avance_duree, rem_pour_prev, rem_pour_autr"; 545 $sql .= ", mode_paiement)"; 546 $sql .= " VALUES (".$this->id.",now(), ".$montant; 547 $sql .= ",".$this->commercial_sign_id.",".$did.",".$user->id; 548 $sql .= ",".$pour[0].",".$pour[1].",".$pour[2].",".$pour[3]; 549 $sql .= ",'".$this->mode_paiement."');"; 550 551 $resql = $this->db->query($sql); 552 if ($resql) 553 { 554 555 } 556 else 557 { 558 dolibarr_syslog($sql); 559 } 560 } 561 else 562 { 563 564 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat_priseordre"; 565 $sql .= " (fk_contrat, datepo, montant, fk_commercial, fk_distributeur, fk_user"; 566 $sql .= ", mode_paiement)"; 567 $sql .= " VALUES (".$this->id.",now(), ".$montant; 568 $sql .= ",".$this->commercial_sign_id.",".$did.",".$user->id; 569 $sql .= ",'".$this->mode_paiement."');"; 570 $resql = $this->db->query($sql); 571 if ($resql) 572 { 573 574 } 575 else 576 { 577 dolibarr_syslog($sql); 578 } 579 } 580 } 581 } 582 /* 583 * 584 * 585 */ 586 function update_statut() 587 { 588 $sql = "SELECT statut, count(*) "; 589 $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_societe_ligne"; 590 $sql .= " WHERE fk_contrat = ".$this->id; 591 $sql .= " GROUP BY statut"; 592 593 $resql = $this->db->query($sql); 594 595 if ($resql) 596 { 597 $num = $this->db->num_rows($resql); 598 if ($num) 599 { 600 $i = 0; 601 while ($i < $num) 602 { 603 $row = $this->db->fetch_row($resql); 604 $lignes[$row[0]] = $row[1]; 605 $i++; 606 } 607 } 608 $this->db->free($resql); 609 } 610 611 $statut = 0; 612 613 if ($num > 0) $statut = 1; 614 615 if ($lignes[2] > 0) $statut = 2; 616 617 if ($lignes[7] == $num) $statut = 7; 618 619 if ($lignes[6] > 0) $statut = 6; 620 621 if ($lignes[6] > 0 && $lignes[2] > 0) $statut = 2; 622 623 if ($lignes[3] > 0) $statut = 3; 624 625 $sql = "UPDATE ".MAIN_DB_PREFIX."telephonie_contrat"; 626 $sql .= " SET statut = ".$statut; 627 $sql .= " WHERE rowid = ".$this->id; 628 629 $resql = $this->db->query($sql); 630 if ($resql) 631 { 632 633 } 634 else 635 { 636 dolibarr_syslog("Telephonie::Contrat Error". $sql); 637 } 638 } 639 /* 640 * Log des consultations 641 * 642 */ 643 function log_consult($user,$mode) 644 { 645 $sql = "INSERT INTO ".MAIN_DB_PREFIX."telephonie_contrat_consult (fk_contrat,fk_user,action)"; 646 $sql .= " VALUES (".$this->id.",".$user->id.",'".$mode."');"; 647 $this->db->query($sql); 648 } 649 650 } 651 ?>
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 |
|