[ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * Copyright (C) 2004-2005 Laurent Destailleur <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: facture-rec.class.php,v 1.20 2005/11/11 19:25:20 eldy Exp $ 20 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/compta/facture/facture-rec.class.php,v $ 21 */ 22 23 /** 24 \file htdocs/compta/facture/facture-rec.class.php 25 \ingroup facture 26 \brief Fichier de la classe des factures recurentes 27 \version $Revision: 1.20 $ 28 */ 29 30 require_once(DOL_DOCUMENT_ROOT."/notify.class.php"); 31 require_once(DOL_DOCUMENT_ROOT."/product.class.php"); 32 33 34 /** 35 \class FactureRec 36 \brief Classe de gestion des factures recurrentes 37 */ 38 class FactureRec 39 { 40 var $id; 41 var $db; 42 var $socidp; 43 var $number; 44 var $author; 45 var $date; 46 var $ref; 47 var $amount; 48 var $remise; 49 var $tva; 50 var $total; 51 var $note; 52 var $db_table; 53 var $propalid; 54 var $projetid; 55 56 /** 57 * Initialisation de la class 58 * 59 */ 60 function FactureRec($DB, $facid=0) 61 { 62 $this->db = $DB ; 63 $this->facid = $facid; 64 } 65 /** 66 * Créé la facture 67 */ 68 function create($user) 69 { 70 $facsrc = new Facture($this->db); 71 72 if ($facsrc->fetch($this->facid) > 0) 73 { 74 /* 75 * On positionne en mode brouillon la facture 76 */ 77 $this->brouillon = 1; 78 if (! $facsrc->projetid) 79 { 80 $facsrc->projetid = "NULL"; 81 } 82 83 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (titre, fk_soc, datec, amount, remise, remise_percent, note, fk_user_author,fk_projet, fk_cond_reglement) "; 84 $sql .= " VALUES ('$this->titre', '$facsrc->socidp', now(), '$facsrc->amount', '$facsrc->remise', '$facsrc->remise_percent', '$this->note','$user->id', '$facsrc->projetid', '$facsrc->cond_reglement_id')"; 85 if ( $this->db->query($sql) ) 86 { 87 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec"); 88 89 /* 90 * Produits 91 */ 92 for ($i = 0 ; $i < sizeof($facsrc->lignes) ; $i++) 93 { 94 if ($facsrc->lignes[$i]->produit_id > 0) 95 { 96 $prod = new Product($this->db); 97 $prod->fetch($facsrc->lignes[$i]->produit_id); 98 } 99 100 101 $result_insert = $this->addline($this->id, 102 addslashes($facsrc->lignes[$i]->desc), 103 $facsrc->lignes[$i]->subprice, 104 $facsrc->lignes[$i]->qty, 105 $facsrc->lignes[$i]->tva_taux, 106 $facsrc->lignes[$i]->produit_id, 107 $facsrc->lignes[$i]->remise_percent); 108 109 110 if ( $result_insert < 0) 111 { 112 print '<br>' . $this->db->error() .'<br>'; 113 } 114 } 115 116 return $this->id; 117 } 118 else 119 { 120 print $this->db->error() . '<b><br>'.$sql; 121 return 0; 122 } 123 } 124 else 125 { 126 return -1; 127 } 128 } 129 130 /** 131 * Recupére l'objet facture 132 */ 133 function fetch($rowid, $societe_id=0) 134 { 135 136 $sql = "SELECT f.fk_soc,f.titre,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent,f.fk_projet, c.rowid as crid, c.libelle, c.libelle_facture, f.note, f.fk_user_author"; 137 $sql .= " FROM ".MAIN_DB_PREFIX."facture_rec as f, ".MAIN_DB_PREFIX."cond_reglement as c"; 138 $sql .= " WHERE f.rowid=$rowid AND c.rowid = f.fk_cond_reglement"; 139 140 if ($societe_id > 0) 141 { 142 $sql .= " AND f.fk_soc = ".$societe_id; 143 } 144 145 if ($this->db->query($sql) ) 146 { 147 if ($this->db->num_rows()) 148 { 149 $obj = $this->db->fetch_object(); 150 151 $this->id = $rowid; 152 $this->datep = $obj->dp; 153 $this->titre = $obj->titre; 154 $this->amount = $obj->amount; 155 $this->remise = $obj->remise; 156 $this->total_ht = $obj->total; 157 $this->total_tva = $obj->tva; 158 $this->total_ttc = $obj->total_ttc; 159 $this->paye = $obj->paye; 160 $this->remise_percent = $obj->remise_percent; 161 $this->socidp = $obj->fk_soc; 162 $this->statut = $obj->fk_statut; 163 $this->date_lim_reglement = $obj->dlr; 164 $this->cond_reglement_id = $obj->crid; 165 $this->cond_reglement = $obj->libelle; 166 $this->cond_reglement_facture = $obj->libelle_facture; 167 $this->projetid = $obj->fk_projet; 168 $this->note = stripslashes($obj->note); 169 $this->user_author = $obj->fk_user_author; 170 $this->lignes = array(); 171 172 if ($this->statut == 0) 173 { 174 $this->brouillon = 1; 175 } 176 177 $this->db->free(); 178 179 /* 180 * Lignes 181 */ 182 183 $sql = "SELECT l.fk_product,l.description, l.subprice, l.price, l.qty, l.rowid, l.tva_taux, l.remise_percent"; 184 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet_rec as l WHERE l.fk_facture = ".$this->id." ORDER BY l.rowid ASC"; 185 186 $result = $this->db->query($sql); 187 if ($result) 188 { 189 $num = $this->db->num_rows(); 190 $i = 0; $total = 0; 191 192 while ($i < $num) 193 { 194 $objp = $this->db->fetch_object($result); 195 $faclig = new FactureLigne($this->db); 196 $faclig->produit_id = $objp->fk_product; 197 $faclig->desc = stripslashes($objp->description); 198 $faclig->qty = $objp->qty; 199 $faclig->price = $objp->price; 200 $faclig->subprice = $objp->subprice; 201 $faclig->tva_taux = $objp->tva_taux; 202 $faclig->remise_percent = $objp->remise_percent; 203 $this->lignes[$i] = $faclig; 204 $i++; 205 } 206 207 $this->db->free(); 208 209 return 1; 210 } 211 else 212 { 213 print $this->db->error(); 214 return -1; 215 } 216 } 217 else 218 { 219 print "Error"; 220 return -2; 221 } 222 } 223 else 224 { 225 print $this->db->error(); 226 return -3; 227 } 228 } 229 230 /** 231 * Recupére l'objet client lié à la facture 232 */ 233 function fetch_client() 234 { 235 $client = new Societe($this->db); 236 $client->fetch($this->socidp); 237 $this->client = $client; 238 239 } 240 241 /** 242 * Valide la facture 243 */ 244 function valid($userid, $dir) 245 { 246 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET fk_statut = 1, date_valid=now(), fk_user_valid=$userid"; 247 $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; 248 249 if ($this->db->query($sql) ) 250 { 251 return 1; 252 } 253 else 254 { 255 print $this->db->error() . ' in ' . $sql; 256 } 257 } 258 259 /** 260 * Supprime la facture 261 */ 262 function delete($rowid) 263 { 264 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = $rowid;"; 265 266 if ($this->db->query( $sql) ) 267 { 268 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = $rowid"; 269 270 if ($this->db->query( $sql) ) 271 { 272 return 1; 273 } 274 else 275 { 276 print "Err : ".$this->db->error(); 277 return -1; 278 } 279 } 280 else 281 { 282 print "Err : ".$this->db->error(); 283 return -2; 284 } 285 } 286 287 /** 288 * Valide la facture 289 */ 290 function set_valid($rowid, $user, $soc) 291 { 292 if ($this->brouillon) 293 { 294 $action_notify = 2; // ne pas modifier cette valeur 295 296 $numfa = $this->getNextNumRef($soc); 297 298 $sql = "UPDATE ".MAIN_DB_PREFIX."facture set facnumber='$numfa', fk_statut = 1, fk_user_valid = $user->id WHERE rowid = $rowid ;"; 299 $result = $this->db->query( $sql); 300 301 /* 302 * Notify 303 */ 304 $facref = sanitize_string($this->ref); 305 $filepdf = $conf->facture->dir_output . "/" . $facref . "/" . $facref . ".pdf"; 306 307 308 $mesg = "La facture ".$this->ref." a été validée.\n"; 309 310 $notify = New Notify($this->db); 311 $notify->send($action_notify, $this->socidp, $mesg, "facture", $rowid, $filepdf); 312 /* 313 * Update Stats 314 * 315 */ 316 $sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."facturedet WHERE fk_facture = ".$this->id; 317 $sql .= " AND fk_product IS NOT NULL"; 318 319 $result = $this->db->query($sql); 320 321 if ($result) 322 { 323 $num = $this->db->num_rows(); 324 $i = 0; 325 while ($i < $num) 326 { 327 $obj = $this->db->fetch_object($result); 328 329 $sql = "UPDATE ".MAIN_DB_PREFIX."product SET nbvente=nbvente+1 WHERE rowid = ".$obj->fk_product; 330 $db2 = $this->db->dbclone(); 331 $result = $db2->query($sql); 332 $i++; 333 } 334 } 335 /* 336 * Contrats 337 */ 338 $contrat = new Contrat($this->db); 339 $contrat->create_from_facture($this->id, $user, $this->socidp); 340 341 return $result; 342 } 343 } 344 345 /** 346 * \brief Renvoie la référence de facture suivante non utilisée en fonction du module 347 * de numérotation actif défini dans FACTURE_ADDON 348 * \param soc objet societe 349 * \return string reference libre pour la facture 350 */ 351 function getNextNumRef($soc) 352 { 353 global $db, $langs; 354 $langs->load("bills"); 355 356 $dir = DOL_DOCUMENT_ROOT . "/includes/modules/facture/"; 357 358 if (defined("FACTURE_ADDON") && FACTURE_ADDON) 359 { 360 $file = FACTURE_ADDON."/".FACTURE_ADDON.".modules.php"; 361 362 // Chargement de la classe de numérotation 363 $classname = "mod_facture_".FACTURE_ADDON; 364 require_once($dir.$file); 365 366 $obj = new $classname(); 367 368 $numref = ""; 369 $numref = $obj->getNumRef($soc,$this); 370 371 if ( $numref != "") 372 { 373 return $numref; 374 } 375 else 376 { 377 dolibarr_print_error($db,"modules_facture::getNextNumRef ".$obj->error); 378 return ""; 379 } 380 } 381 else 382 { 383 print $langs->trans("Error")." ".$langs->trans("Error_FACTURE_ADDON_NotDefined"); 384 return ""; 385 } 386 } 387 388 /** 389 * Ajoute un produit dans la facture 390 */ 391 function add_product($idproduct, $qty, $remise_percent) 392 { 393 if ($idproduct > 0) 394 { 395 $i = sizeof($this->products); 396 $this->products[$i] = $idproduct; 397 if (!$qty) 398 { 399 $qty = 1 ; 400 } 401 $this->products_qty[$i] = $qty; 402 $this->products_remise_percent[$i] = $remise_percent; 403 } 404 } 405 406 /** 407 * Ajoute une ligne de facture 408 */ 409 function addline($facid, $desc, $pu, $qty, $txtva, $fk_product='NULL', $remise_percent=0) 410 { 411 if ($this->brouillon) 412 { 413 if (strlen(trim($qty))==0) 414 { 415 $qty=1; 416 } 417 $remise = 0; 418 $price = round(ereg_replace(",",".",$pu), 2); 419 $subprice = $price; 420 if (trim(strlen($remise_percent)) > 0) 421 { 422 $remise = round(($pu * $remise_percent / 100), 2); 423 $price = $pu - $remise; 424 } 425 426 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise)"; 427 $sql .= " VALUES ('$facid', '$desc'"; 428 $sql .= ",".ereg_replace(",",".",$price); 429 $sql .= ",".ereg_replace(",",".",$qty); 430 $sql .= ",".ereg_replace(",",".",$txtva); 431 $sql .= ",'$fk_product'"; 432 $sql .= ",'".ereg_replace(",",".",$remise_percent)."'"; 433 $sql .= ",'".ereg_replace(",",".",$subprice)."'"; 434 $sql .= ",'".ereg_replace(",",".",$remise)."') ;"; 435 436 if ( $this->db->query( $sql) ) 437 { 438 $this->updateprice($facid); 439 return 1; 440 } 441 else 442 { 443 print "$sql"; 444 return -1; 445 } 446 } 447 } 448 449 /** 450 * Mets à jour une ligne de facture 451 */ 452 function updateline($rowid, $desc, $pu, $qty, $remise_percent=0) 453 { 454 if ($this->brouillon) 455 { 456 if (strlen(trim($qty))==0) 457 { 458 $qty=1; 459 } 460 $remise = 0; 461 $price = round(ereg_replace(",",".",$pu), 2); 462 $subprice = $price; 463 if (trim(strlen($remise_percent)) > 0) 464 { 465 $remise = round(($pu * $remise_percent / 100), 2); 466 $price = $pu - $remise; 467 } 468 else 469 { 470 $remise_percent=0; 471 } 472 473 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='$desc'"; 474 $sql .= ",price=".ereg_replace(",",".",$price); 475 $sql .= ",subprice=".ereg_replace(",",".",$subprice); 476 $sql .= ",remise=".ereg_replace(",",".",$remise); 477 $sql .= ",remise_percent=".ereg_replace(",",".",$remise_percent); 478 $sql .= ",qty=".ereg_replace(",",".",$qty); 479 $sql .= " WHERE rowid = $rowid ;"; 480 481 $result = $this->db->query( $sql); 482 483 $this->updateprice($this->id); 484 } 485 } 486 487 /** 488 * Supprime une ligne 489 */ 490 function deleteline($rowid) 491 { 492 if ($this->brouillon) 493 { 494 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;"; 495 $result = $this->db->query( $sql); 496 497 $this->updateprice($this->id); 498 } 499 } 500 501 /** 502 * Mise à jour des sommes de la facture 503 */ 504 function updateprice($facid) 505 { 506 include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; 507 $err=0; 508 $sql = "SELECT price, qty, tva_taux FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = $facid;"; 509 510 $result = $this->db->query($sql); 511 512 if ($result) 513 { 514 $num = $this->db->num_rows(); 515 $i = 0; 516 while ($i < $num) 517 { 518 $obj = $this->db->fetch_object($result); 519 520 $products[$i][0] = $obj->price; 521 $products[$i][1] = $obj->qty; 522 $products[$i][2] = $obj->tva_taux; 523 524 $i++; 525 } 526 527 $this->db->free(); 528 529 $calculs = calcul_price($products, $this->remise_percent); 530 531 $this->total_remise = $calculs[3]; 532 $this->amount_ht = $calculs[4]; 533 $this->total_ht = $calculs[0]; 534 $this->total_tva = $calculs[1]; 535 $this->total_ttc = $calculs[2]; 536 $tvas = $calculs[5]; 537 538 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET "; 539 $sql .= " amount = ".ereg_replace(",",".",$this->amount_ht); 540 $sql .= ", remise=".ereg_replace(",",".",$this->total_remise); 541 $sql .= ", total=".ereg_replace(",",".",$this->total_ht); 542 $sql .= ", tva=".ereg_replace(",",".",$this->total_tva); 543 $sql .= ", total_ttc=".ereg_replace(",",".",$this->total_ttc); 544 545 $sql .= " WHERE rowid = $facid ;"; 546 547 if ( $this->db->query($sql) ) 548 { 549 if ($err == 0) 550 { 551 return 1; 552 } 553 else 554 { 555 return -3; 556 } 557 } 558 else 559 { 560 print "$sql<br>"; 561 return -2; 562 } 563 } 564 else 565 { 566 print "Error"; 567 return -1; 568 } 569 } 570 571 /** 572 * Applique une remise 573 */ 574 function set_remise($user, $remise) 575 { 576 if ($user->rights->facture->creer) 577 { 578 579 $this->remise_percent = $remise ; 580 581 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET remise_percent = ".ereg_replace(",",".",$remise); 582 $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; 583 584 if ($this->db->query($sql) ) 585 { 586 $this->updateprice($this->id); 587 return 1; 588 } 589 else 590 { 591 print $this->db->error() . ' in ' . $sql; 592 return -1; 593 } 594 } 595 } 596 /** 597 * Rend la facture automatique 598 * 599 */ 600 function set_auto($user, $freq, $courant) 601 { 602 if ($user->rights->facture->creer) 603 { 604 605 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec "; 606 $sql .= " SET frequency = '".$freq."', last_gen='".$courant."'"; 607 $sql .= " WHERE rowid = ".$this->facid.";"; 608 609 $resql = $this->db->query($sql); 610 611 if ($resql) 612 { 613 $this->frequency = $freq; 614 $this->last_gen = $courant; 615 return 0; 616 } 617 else 618 { 619 print $this->db->error() . ' in ' . $sql; 620 return -1; 621 } 622 } 623 else 624 { 625 return -2; 626 } 627 } 628 } 629 ?>
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 |
![]() |