[ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net> 4 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr> 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: fournisseur.facture.class.php,v 1.18 2005/12/02 09:43:08 hregis Exp $ 22 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/fourn/fournisseur.facture.class.php,v $ 23 */ 24 25 /** 26 \file htdocs/fourn/fournisseur.facture.class.php 27 \ingroup fournisseur,facture 28 \brief Fichier de la classe des factures fournisseurs 29 \version $Revision: 1.18 $ 30 */ 31 32 33 /** 34 \class FactureFournisseur 35 \brief Classe permettant la gestion des factures fournisseurs 36 */ 37 38 class FactureFournisseur 39 { 40 var $id; 41 var $db; 42 var $socidp; 43 var $statut; 44 var $paye; 45 var $author; 46 var $libelle; 47 var $date; 48 var $date_echeance; 49 var $ref; 50 var $amount; 51 var $remise; 52 var $tva; 53 var $total_ht; 54 var $total_tva; 55 var $total_ttc; 56 var $note; 57 var $propalid; 58 var $lignes; 59 var $fournisseur; 60 61 /** 62 * \brief Constructeur de la classe 63 * \param DB handler accès base de données 64 * \param soc_idp id societe ('' par defaut) 65 * \param facid id facture ('' par defaut) 66 */ 67 function FactureFournisseur($DB, $soc_idp='', $facid='') 68 { 69 $this->db = $DB ; 70 $this->socidp = $soc_idp; 71 $this->products = array(); 72 $this->amount = 0; 73 $this->remise = 0; 74 $this->tva = 0; 75 $this->total_ht = 0; 76 $this->total_tva = 0; 77 $this->total_ttc = 0; 78 $this->propalid = 0; 79 $this->id = $facid; 80 81 $this->lignes = array(); 82 } 83 84 /** 85 * \brief Création de la facture en base 86 * \param user object utilisateur qui crée 87 * \return int id facture si ok, < 0 si erreur 88 */ 89 function create($user) 90 { 91 global $langs; 92 93 $socidp = $this->socidp; 94 $number = $this->ref; 95 $amount = $this->amount; 96 $remise = $this->remise; 97 98 $this->db->begin(); 99 100 if (! $remise) $remise = 0 ; 101 $totalht = ($amount - $remise); 102 // NE ME SEMBLE PLUS JUSTIFIE ICI 103 // $tva = tva($totalht); 104 // $total = $totalht + $tva; 105 106 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author, date_lim_reglement) '; 107 $sql .= " VALUES ('".addslashes($number)."','".addslashes($this->libelle)."',"; 108 $sql .= $this->socidp.", now(),'".$this->db->idate($this->date)."','".addslashes($this->note)."', ".$user->id.",'".$this->db->idate($this->date_echeance)."');"; 109 $resql=$this->db->query($sql); 110 if ($resql) 111 { 112 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn'); 113 for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) 114 { 115 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)'; 116 $sql .= ' VALUES ('.$this->id.');'; 117 $resql_insert=$this->db->query($sql); 118 if ($resql_insert) 119 { 120 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det'); 121 $this->updateline($idligne, 122 $this->lignes[$i][0], 123 $this->lignes[$i][1], 124 $this->lignes[$i][2], 125 $this->lignes[$i][3]); 126 } 127 } 128 // Mise à jour prix 129 if ($this->updateprice($this->id) > 0) 130 { 131 $this->db->commit(); 132 return $this->id; 133 } 134 else 135 { 136 $this->error=$langs->trans('FailedToUpdatePrice'); 137 $this->db->rollback(); 138 return -3; 139 } 140 } 141 else 142 { 143 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') 144 { 145 $this->error=$langs->trans('ErrorBillRefAlreadyExists'); 146 $this->db->rollback(); 147 return -1; 148 } 149 else 150 { 151 $this->error=$this->db->error(); 152 $this->db->rollback(); 153 return -2; 154 } 155 } 156 } 157 158 /** 159 * \brief Recupére l'objet facture et ses lignes de factures 160 * \param rowid id de la facture a récupérer 161 */ 162 function fetch($rowid) 163 { 164 $sql = 'SELECT libelle, facnumber, amount, remise, '.$this->db->pdate(datef).'as df'; 165 $sql .= ', total_ht, total_tva, total_ttc, fk_user_author'; 166 $sql .= ', fk_statut, paye, f.note,'.$this->db->pdate('date_lim_reglement').'as de'; 167 $sql .= ', s.nom as socnom, s.idp as socidp'; 168 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f,'.MAIN_DB_PREFIX.'societe as s'; 169 $sql .= ' WHERE f.rowid='.$rowid.' AND f.fk_soc = s.idp ;'; 170 $resql = $this->db->query($sql); 171 if ($resql) 172 { 173 $num=$this->db->num_rows($resql); 174 if ($num) 175 { 176 $obj = $this->db->fetch_object(); 177 178 $this->id = $rowid; 179 $this->datep = $obj->df; 180 $this->date_echeance = $obj->de; 181 $this->ref = $obj->facnumber; 182 $this->libelle = $obj->libelle; 183 184 $this->remise = $obj->remise; 185 $this->socidp = $obj->socidp; 186 187 $this->total_ht = $obj->total_ht; 188 $this->total_tva = $obj->total_tva; 189 $this->total_ttc = $obj->total_ttc; 190 191 $this->author = $obj->fk_user_author; 192 193 $this->statut = $obj->fk_statut; 194 $this->paye = $obj->paye; 195 196 $this->socnom = $obj->socnom; 197 $this->note = $obj->note; 198 $this->db->free($resql); 199 200 /* 201 * Lignes 202 */ 203 $sql = 'SELECT rowid,description, pu_ht, qty, tva_taux, tva, total_ht, total_ttc'; 204 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det'; 205 $sql .= ' WHERE fk_facture_fourn='.$this->id; 206 $resql_rows = $this->db->query($sql); 207 if ($resql_rows) 208 { 209 $num_rows = $this->db->num_rows($resql_rows); 210 $i = 0; 211 if ($num_rows) 212 { 213 while ($i < $num_rows) 214 { 215 $obj = $this->db->fetch_object(); 216 $this->lignes[$i][0] = stripslashes($obj->description); 217 $this->lignes[$i][1] = $obj->pu_ht; 218 $this->lignes[$i][2] = $obj->tva_taux; 219 $this->lignes[$i][3] = $obj->qty; 220 $this->lignes[$i][4] = $obj->total_ht; 221 $this->lignes[$i][5] = $obj->tva; 222 $this->lignes[$i][6] = $obj->total_ttc; 223 $this->lignes[$i][7] = $obj->rowid; 224 $i++; 225 } 226 } 227 $this->db->free($resql_rows); 228 } 229 else 230 { 231 dolibarr_print_error($this->db); 232 } 233 } 234 } 235 else 236 { 237 dolibarr_print_error($this->db); 238 } 239 } 240 241 /** 242 * \brief Recupére l'objet fournisseur lié à la facture 243 * 244 */ 245 function fetch_fournisseur() 246 { 247 $fournisseur = new Fournisseur($this->db); 248 $fournisseur->fetch($this->socidp); 249 $this->fournisseur = $fournisseur; 250 } 251 252 /** 253 * \brief Supprime la facture 254 * \param rowid id de la facture à supprimer 255 */ 256 function delete($rowid) 257 { 258 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE rowid = '.$rowid.' AND fk_statut = 0;'; 259 $resql = $this->db->query($sql); 260 if ($resql) 261 { 262 $num = $this->db->affected_rows($resql); 263 if ($num) 264 { 265 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det WHERE fk_facture_fourn = '.$rowid.';'; 266 $resql2 = $this->db->query($sql); 267 if ($resql2) 268 { 269 return 1; 270 } 271 else 272 { 273 dolibarr_print_error($this->db); 274 } 275 } 276 } 277 else 278 { 279 dolibarr_print_error($this->db); 280 } 281 } 282 283 284 /** 285 * \brief Tag la facture comme payée complètement 286 * \param user Objet utilisateur qui modifie l'état 287 * \return int <0 si ko, >0 si ok 288 */ 289 function set_payed($user) 290 { 291 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn'; 292 $sql.= ' SET paye = 1'; 293 $sql.= ' WHERE rowid = '.$this->id; 294 $resql = $this->db->query($sql); 295 if (! $resql) 296 { 297 $this->error=$this->db->error(); 298 dolibarr_print_error($this->db); 299 return -1; 300 } 301 return 1; 302 } 303 304 305 /** 306 * \brief Tag la facture comme validée 307 * \param user Objet utilisateur qui valide la facture 308 * \return int <0 si ko, >0 si ok 309 */ 310 function set_valid($user) 311 { 312 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn"; 313 $sql.= " SET fk_statut = 1, fk_user_valid = ".$user->id; 314 $sql.= " WHERE rowid = ".$this->id; 315 $resql = $this->db->query($sql); 316 if (! $resql) 317 { 318 $this->error=$this->db->error(); 319 dolibarr_print_error($this->db); 320 return -1; 321 } 322 return 1; 323 } 324 325 326 /** 327 * \brief Ajoute une ligne de facture (associé à aucun produit/service prédéfini) 328 * \param desc description de la ligne 329 * \param pu prix unitaire 330 * \param tauxtva taux de tva 331 * \param qty quantité 332 */ 333 function addline($desc, $pu, $tauxtva, $qty) 334 { 335 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)'; 336 $sql .= ' VALUES ('.$this->id.');'; 337 $resql = $this->db->query($sql); 338 if ($resql) 339 { 340 $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det'); 341 $this->updateline($idligne, $desc, $pu, $tauxtva, $qty); 342 } 343 else 344 { 345 dolibarr_print_error($this->db); 346 } 347 // Mise a jour prix facture 348 $this->updateprice($this->id); 349 } 350 351 /** 352 * \brief Mets à jour une ligne de facture 353 * \param id id de la ligne de facture 354 * \param label description de la ligne 355 * \param puht prix unitaire 356 * \param tauxtva taux tva 357 * \param qty quantité 358 * \return int <0 si ko, >0 si ok 359 */ 360 function updateline($id, $label, $puht, $tauxtva, $qty=1) 361 { 362 $puht = price2num($puht); 363 $qty = price2num($qty); 364 365 if (is_numeric($puht) && is_numeric($qty)) 366 { 367 $totalht = ($puht * $qty); 368 $tva = ($totalht * $tauxtva / 100); 369 $totalttc = $totalht + $tva; 370 371 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn_det '; 372 $sql .= 'SET '; 373 $sql .= 'description =\''.addslashes($label).'\''; 374 $sql .= ', pu_ht = ' .$puht; 375 $sql .= ', qty =' .$qty; 376 $sql .= ', total_ht=' .price2num($totalht); 377 $sql .= ', tva=' .price2num($tva); 378 $sql .= ', tva_taux=' .price2num($tauxtva); 379 $sql .= ', total_ttc='.price2num($totalttc); 380 $sql .= ' WHERE rowid = '.$id.';'; 381 382 $resql=$this->db->query($sql); 383 if ($resql) 384 { 385 // Mise a jour prix facture 386 return $this->updateprice($this->id); 387 } 388 else 389 { 390 $this->error=$this->db->error(); 391 return -1; 392 } 393 } 394 } 395 396 /** 397 * \brief Supprime une ligne facture de la base 398 * \param rowid id de la ligne de facture a supprimer 399 */ 400 function deleteline($rowid) 401 { 402 // Supprime ligne 403 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det '; 404 $sql .= ' WHERE rowid = '.$rowid.';'; 405 $resql = $this->db->query($sql); 406 if (! $resql) 407 { 408 dolibarr_print_error($this->db); 409 } 410 // Mise a jour prix facture 411 $this->updateprice($this->id); 412 return 1; 413 } 414 415 /** 416 * \brief Mise à jour des sommes de la facture 417 * \param facid id de la facture a modifier 418 * \return int <0 si ko, >0 si ok 419 */ 420 function updateprice($facid) 421 { 422 $total_ht = 0; 423 $total_tva = 0; 424 $total_ttc = 0; 425 426 $sql = 'SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM '.MAIN_DB_PREFIX.'facture_fourn_det'; 427 $sql .= ' WHERE fk_facture_fourn = '.$facid.';'; 428 $resql = $this->db->query($sql); 429 if ($resql) 430 { 431 $num = $this->db->num_rows($resql); 432 if ($num) 433 { 434 $row = $this->db->fetch_row(); 435 $total_ht = $row[0]; 436 $total_tva = $row[1]; 437 $total_ttc = $row[2]; 438 } 439 $this->db->free($resql); 440 441 $total_ht = $total_ht != '' ? $total_ht : 0; 442 $total_tva = $total_tva != '' ? $total_tva : 0; 443 $total_ttc = $total_ttc != '' ? $total_ttc : 0; 444 445 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET'; 446 $sql .= ' total_ht = '. price2num($total_ht); 447 $sql .= ',total_tva = '.price2num($total_tva); 448 $sql .= ',total_ttc = '.price2num($total_ttc); 449 $sql .= ' WHERE rowid = '.$facid.';'; 450 $resql2 = $this->db->query($sql); 451 if ($resql2) 452 { 453 return 1; 454 } 455 else 456 { 457 $this->error=$this->db->error(); 458 return -2; 459 } 460 } 461 else 462 { 463 dolibarr_print_error($this->db); 464 return -1; 465 } 466 } 467 468 469 /** 470 * \brief Retourne le libellé du statut d'une facture (brouillon, validée, abandonnée, payée) 471 * \return string Libellé 472 */ 473 function getLibStatut() 474 { 475 return $this->LibStatut($this->paye,$this->statut); 476 } 477 478 /** 479 * \brief Renvoi le libellé court d'un statut donné 480 * \param paye etat paye 481 * \param statut id statut 482 * \return string Libellé long du statut 483 */ 484 function LibStatutShort($paye,$statut) 485 { 486 global $langs; 487 $langs->load('bills'); 488 if (! $paye) 489 { 490 if ($statut == 0) return $langs->trans('BillShortStatusDraft'); 491 if ($statut == 3) return $langs->trans('BillShortStatusCanceled'); 492 return $langs->trans('BillShortStatusValidated'); 493 } 494 else 495 { 496 return $langs->trans('BillShortStatusPayed'); 497 } 498 } 499 500 /** 501 * \brief Renvoi le libellé long d'un statut donné 502 * \param paye etat paye 503 * \param statut id statut 504 * \return string Libellé long du statut 505 */ 506 function LibStatut($paye,$statut) 507 { 508 global $langs; 509 $langs->load('bills'); 510 if (! $paye) 511 { 512 if ($statut == 0) return $langs->trans('BillStatusDraft'); 513 if ($statut == 3) return $langs->trans('BillStatusCanceled'); 514 return $langs->trans('BillStatusValidated'); 515 } 516 else 517 { 518 return $langs->trans('BillStatusPayed'); 519 } 520 } 521 522 /** 523 * \brief Renvoi le libellé court d'un statut donné 524 * \param paye etat paye 525 * \param statut id statut 526 * \param amount amount already payed 527 * \return string Libellé court du statut 528 */ 529 function PayedLibStatut($paye,$statut,$amount=0) 530 { 531 global $langs; 532 $langs->load('bills'); 533 if (! $paye) 534 { 535 if ($statut == 0) return $langs->trans('BillShortStatusDraft'); 536 if ($statut == 3) return $langs->trans('BillStatusCanceled'); 537 if ($amount) return $langs->trans('BillStatusStarted'); 538 return $langs->trans('BillStatusNotPayed'); 539 } 540 else 541 { 542 return $langs->trans('BillStatusPayed'); 543 } 544 } 545 546 547 /** 548 * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord 549 * \param user Objet user 550 * \return int <0 si ko, >0 si ok 551 */ 552 function load_board($user) 553 { 554 global $conf; 555 556 $this->nbtodo=$this->nbtodolate=0; 557 $sql = 'SELECT ff.rowid,'.$this->db->pdate('ff.date_lim_reglement').' as datefin'; 558 $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as ff'; 559 $sql.= ' WHERE ff.paye=0'; 560 if ($user->societe_id) $sql.=' AND fk_soc = '.$user->societe_id; 561 $resql=$this->db->query($sql); 562 if ($resql) 563 { 564 while ($obj=$this->db->fetch_object($resql)) 565 { 566 $this->nbtodo++; 567 if ($obj->datefin < (time() - $conf->facture->fournisseur->warning_delay)) $this->nbtodolate++; 568 } 569 $this->db->free($resql); 570 return 1; 571 } 572 else 573 { 574 dolibarr_print_error($this->db); 575 $this->error=$this->db->error(); 576 return -1; 577 } 578 } 579 580 } 581 ?>
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 |
![]() |