[ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org> 4 * Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net> 5 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr> 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: account.class.php,v 1.52.2.3 2005/12/20 23:42:32 eldy Exp $ 22 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/compta/bank/account.class.php,v $ 23 */ 24 25 /** 26 \file htdocs/compta/bank/account.class.php 27 \ingroup banque 28 \brief Fichier de la classe des comptes bancaires 29 \version $Revision: 1.52.2.3 $ 30 */ 31 32 33 /** 34 \class Account 35 \brief Classe permettant la gestion des comptes bancaires 36 */ 37 38 class Account 39 { 40 var $rowid; 41 42 var $label; 43 var $type; 44 var $bank; 45 var $clos; 46 var $rappro; 47 48 var $code_banque; 49 var $code_guichet; 50 var $number; 51 var $cle_rib; 52 var $bic; 53 var $iban_prefix; 54 var $proprio; 55 var $adresse_proprio; 56 var $type_lib=array(); 57 58 /** 59 * Constructeur 60 */ 61 function Account($DB, $rowid=0) 62 { 63 global $langs; 64 65 $this->db = $DB; 66 $this->rowid = $rowid; 67 68 $this->clos = 0; 69 $this->solde = 0; 70 71 $this->type_lib[0]=$langs->trans("BankType0"); 72 $this->type_lib[1]=$langs->trans("BankType1"); 73 $this->type_lib[2]=$langs->trans("BankType2"); 74 75 $this->status[0]=$langs->trans("StatusAccountOpened"); 76 $this->status[1]=$langs->trans("StatusAccountClosed"); 77 78 return 1; 79 } 80 81 /** 82 * \brief Efface une entree dans la table ".MAIN_DB_PREFIX."bank 83 * \param rowid Id de l'ecriture a effacer 84 * \return int <0 si ko, >0 si ok 85 */ 86 function deleteline($rowid) 87 { 88 $nbko=0; 89 90 $this->db->begin(); 91 92 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".$rowid; 93 $result = $this->db->query($sql); 94 if (! $result) $nbko++; 95 96 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".$rowid; 97 $result = $this->db->query($sql); 98 if (! $result) $nbko++; 99 100 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".$rowid; 101 $result = $this->db->query($sql); 102 if (! $result) $nbko++; 103 104 if (! $nbko) 105 { 106 $this->db->commit(); 107 return 1; 108 } 109 else 110 { 111 $this->db->rollback(); 112 return -$nbko; 113 } 114 } 115 116 /** 117 * \brief Ajoute lien entre ecriture bancaire et sources 118 * \param line_id Id ecriture bancaire 119 * \param url_id Id parametre url 120 * \param url Url 121 * \param label Libellé du lien 122 * \param type Type de lien (payment, company, member, ...) 123 * \return int <0 si ko, id line si ok 124 */ 125 function add_url_line($line_id, $url_id, $url, $label, $type='') 126 { 127 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (fk_bank, url_id, url, label, type)"; 128 $sql .= " VALUES ('$line_id', '$url_id', '$url', '$label', '$type')"; 129 130 if ($this->db->query($sql)) 131 { 132 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url"); 133 return $rowid; 134 } 135 else 136 { 137 $this->error=$this->db->error(); 138 return -1; 139 } 140 } 141 142 /** 143 * \brief Renvoi tableau des liens 144 * \param line_id Id ligne écriture 145 * \retuen array Tableau des liens 146 */ 147 function get_url($line_id) 148 { 149 $lines = array(); 150 $sql = "SELECT url_id, url, label, type"; 151 $sql.= " FROM ".MAIN_DB_PREFIX."bank_url"; 152 $sql.= " WHERE fk_bank = ".$line_id; 153 $sql.= " ORDER BY type, label"; 154 155 $result = $this->db->query($sql); 156 if ($result) 157 { 158 $i = 0; 159 $num = $this->db->num_rows($result); 160 while ($i < $num) 161 { 162 $obj = $this->db->fetch_object($result); 163 // Anciens liens (pour compatibilité) 164 $lines[$i][0] = $obj->url; 165 $lines[$i][1] = $obj->url_id; 166 $lines[$i][2] = $obj->label; 167 $lines[$i][3] = $obj->type; 168 // Nouveaux liens 169 $lines[$i]['url'] = $obj->url; 170 $lines[$i]['url_id'] = $obj->url_id; 171 $lines[$i]['label'] = $obj->label; 172 $lines[$i]['type'] = $obj->type; 173 $i++; 174 } 175 return $lines; 176 } 177 } 178 179 /** 180 * \brief Ajoute une entree dans la table ".MAIN_DB_PREFIX."bank 181 * \return int rowid de l'entrée ajoutée, <0 si erreur 182 */ 183 function addline($date, $oper, $label, $amount, $num_chq='', $categorie='', $user='') 184 { 185 dolibarr_syslog("Account::addline: $date, $oper, $label, $amount, $num_chq, $categorie, $user"); 186 if ($this->rowid) 187 { 188 $this->db->begin(); 189 190 switch ($oper) 191 { 192 case 1: 193 $oper = 'TIP'; 194 break; 195 case 2: 196 $oper = 'VIR'; 197 break; 198 case 3: 199 $oper = 'PRE'; 200 break; 201 case 4: 202 $oper = 'LIQ'; 203 break; 204 case 5: 205 $oper = 'VAD'; 206 break; 207 case 6: 208 $oper = 'CB'; 209 break; 210 case 7: 211 $oper = 'CHQ'; 212 break; 213 } 214 215 $datev = $date; 216 217 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (datec, dateo, datev, label, amount, fk_user_author, num_chq,fk_account, fk_type)"; 218 $sql.= " VALUES (now(), '".$date."', '$datev', '$label', '" . ereg_replace(',','.',$amount) . "', '".$user->id."' ,'$num_chq', '".$this->rowid."', '$oper')"; 219 220 if ($this->db->query($sql)) 221 { 222 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank"); 223 if ($categorie) 224 { 225 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (lineid, fk_categ) VALUES ('$rowid', '$categorie')"; 226 $result = $this->db->query($sql); 227 if (! $result) 228 { 229 $this->db->rollback(); 230 $this->error=$this->db->error(); 231 return -3; 232 } 233 } 234 $this->db->commit(); 235 return $rowid; 236 } 237 else 238 { 239 $this->db->rollback(); 240 $this->error=$this->db->error(); 241 return -2; 242 } 243 } 244 else 245 { 246 $this->error="Account::addline rowid not defined"; 247 return -1; 248 } 249 } 250 251 /* 252 * \brief Creation du compte bancaire en base 253 * \return int < 0 si erreur, > 0 si ok 254 */ 255 function create() 256 { 257 global $langs; 258 259 // Chargement librairie pour acces fonction controle RIB 260 require_once DOL_DOCUMENT_ROOT . '/compta/bank/bank.lib.php'; 261 262 if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) { 263 $this->error="Le contrôle de la clé indique que les informations de votre compte bancaire sont incorrectes."; 264 return 0; 265 } 266 267 if (! $pcgnumber) $pcgnumber="51"; 268 269 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (datec, label, account_number) values (now(),'".addslashes($this->label)."','$pcgnumber');"; 270 $resql=$this->db->query($sql); 271 if ($resql) 272 { 273 if ($this->db->affected_rows($resql)) 274 { 275 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account"); 276 if ( $this->update() ) 277 { 278 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (datec, label, amount, fk_account, datev, dateo, fk_type, rappro) "; 279 $sql .= " VALUES (now(),'".$langs->trans("Balance")."','" . ereg_replace(',','.',$this->solde) . "','$this->id','".$this->db->idate($this->date_solde)."','".$this->db->idate($this->date_solde)."','SOLD',1);"; 280 $this->db->query($sql); 281 } 282 return $this->id; 283 } 284 } 285 else 286 { 287 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { 288 $this->error=$langs->trans("ErrorBankLabelAlreadyExists"); 289 return -1; 290 } 291 else { 292 $this->error=$this->db->error(); 293 return -2; 294 } 295 } 296 } 297 298 /* 299 * \brief Mise a jour compte 300 * \param user Object utilisateur qui modifie 301 */ 302 function update($user='') 303 { 304 // Chargement librairie pour acces fonction controle RIB 305 require_once DOL_DOCUMENT_ROOT . '/compta/bank/bank.lib.php'; 306 307 if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) { 308 $this->error="Le contrôle de la clé indique que les informations de votre compte bancaire sont incorrectes."; 309 return 0; 310 } 311 312 if (! $this->label) $this->label = "???"; 313 314 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET "; 315 316 $sql .= " bank = '" .addslashes($this->bank)."'"; 317 $sql .= ",label = '".addslashes($this->label)."'"; 318 319 $sql .= ",code_banque='".$this->code_banque."'"; 320 $sql .= ",code_guichet='".$this->code_guichet."'"; 321 $sql .= ",number='".$this->number."'"; 322 $sql .= ",cle_rib='".$this->cle_rib."'"; 323 $sql .= ",bic='".$this->bic."'"; 324 $sql .= ",iban_prefix = '".$this->iban_prefix."'"; 325 $sql .= ",domiciliation='".addslashes($this->domiciliation)."'"; 326 $sql .= ",proprio = '".addslashes($this->proprio)."'"; 327 $sql .= ",adresse_proprio = '".addslashes($this->adresse_proprio)."'"; 328 $sql .= ",courant = ".$this->courant; 329 $sql .= ",clos = ".$this->clos; 330 $sql .= ",rappro = ".$this->rappro; 331 332 $sql .= " WHERE rowid = ".$this->id; 333 334 $result = $this->db->query($sql); 335 336 if ($result) 337 { 338 return 1; 339 } 340 else 341 { 342 dolibarr_print_error($this->db); 343 return 0; 344 } 345 } 346 347 /* 348 * \brief Charge en memoire depuis la base le compte 349 * \param id Id du compte à récupérer 350 */ 351 function fetch($id) 352 { 353 $this->id = $id; 354 $sql = "SELECT rowid, label, bank, number, courant, clos, rappro,"; 355 $sql.= " code_banque, code_guichet, cle_rib, bic, iban_prefix, domiciliation, proprio, adresse_proprio FROM ".MAIN_DB_PREFIX."bank_account"; 356 $sql.= " WHERE rowid = ".$id; 357 358 $result = $this->db->query($sql); 359 360 if ($result) 361 { 362 if ($this->db->num_rows($result)) 363 { 364 $obj = $this->db->fetch_object($result); 365 366 $this->label = $obj->label; 367 $this->type = $obj->courant; 368 $this->courant = $obj->courant; 369 $this->bank = $obj->bank; 370 $this->clos = $obj->clos; 371 $this->rappro = $obj->rappro; 372 373 $this->code_banque = $obj->code_banque; 374 $this->code_guichet = $obj->code_guichet; 375 $this->number = $obj->number; 376 $this->cle_rib = $obj->cle_rib; 377 $this->bic = $obj->bic; 378 $this->iban_prefix = $obj->iban_prefix; 379 $this->domiciliation = $obj->domiciliation; 380 $this->proprio = $obj->proprio; 381 $this->adresse_proprio = $obj->adresse_proprio; 382 } 383 $this->db->free($result); 384 } 385 else 386 { 387 dolibarr_print_error($this->db); 388 } 389 } 390 391 392 /* 393 * \brief Efface le compte 394 */ 395 function delete() 396 { 397 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account"; 398 $sql .= " WHERE rowid = ".$this->rowid; 399 $result = $this->db->query($sql); 400 if ($result) { 401 return 1; 402 } 403 else { 404 dolibarr_print_error($this->db); 405 return -1; 406 } 407 } 408 409 410 /* 411 * \brief Renvoi si un compte peut etre supprimer ou non (sans mouvements) 412 * \return boolean vrai si peut etre supprimé, faux sinon 413 */ 414 function can_be_deleted() 415 { 416 $can_be_deleted=false; 417 418 $sql = "SELECT COUNT(rowid) as nb"; 419 $sql.= " FROM ".MAIN_DB_PREFIX."bank"; 420 $sql.= " WHERE fk_account=".$this->id; 421 $resql = $this->db->query($sql); 422 if ($resql) { 423 $obj=$this->db->fetch_object($resql); 424 if ($obj->nb <= 1) $can_be_deleted=true; // Juste le solde 425 } 426 else { 427 dolibarr_print_error($this->db); 428 } 429 return $can_be_deleted; 430 } 431 432 433 /* 434 * 435 */ 436 function error() 437 { 438 return $this->error; 439 } 440 441 /* 442 * 443 */ 444 function solde() 445 { 446 $sql = "SELECT sum(amount) FROM ".MAIN_DB_PREFIX."bank WHERE fk_account=$this->id AND dateo <=" . $this->db->idate(time() ); 447 448 $result = $this->db->query($sql); 449 450 if ($result) 451 { 452 if ($this->db->num_rows()) 453 { 454 $solde = $this->db->result(0,0); 455 456 return $solde; 457 } 458 $this->db->free(); 459 } 460 } 461 462 /* 463 * 464 */ 465 function datev_next($rowid) 466 { 467 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET "; 468 469 $sql .= " datev = adddate(datev, interval 1 day)"; 470 471 $sql .= " WHERE rowid = $rowid"; 472 473 $result = $this->db->query($sql); 474 475 if ($result) 476 { 477 if ($this->db->affected_rows()) 478 { 479 return 1; 480 } 481 } 482 else 483 { 484 dolibarr_print_error($this->db); 485 return 0; 486 } 487 } 488 489 /* 490 * 491 */ 492 function datev_previous($rowid) 493 { 494 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET "; 495 496 $sql .= " datev = adddate(datev, interval -1 day)"; 497 498 $sql .= " WHERE rowid = $rowid"; 499 500 $result = $this->db->query($sql); 501 502 if ($result) 503 { 504 if ($this->db->affected_rows()) 505 { 506 return 1; 507 } 508 } 509 else 510 { 511 dolibarr_print_error($this->db); 512 return 0; 513 } 514 } 515 516 517 /** 518 * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord 519 * \param user Objet user 520 * \return int <0 si ko, >0 si ok 521 */ 522 function load_board($user) 523 { 524 global $conf; 525 526 if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe 527 528 $this->nbtodo=$this->nbtodolate=0; 529 $sql = "SELECT b.rowid,".$this->db->pdate("b.datev")." as datefin"; 530 $sql.= " FROM ".MAIN_DB_PREFIX."bank as b, ".MAIN_DB_PREFIX."bank_account as ba"; 531 $sql.= " WHERE b.rappro=0 AND b.fk_account = ba.rowid"; 532 $sql.= " AND ba.rappro = 1"; 533 $resql=$this->db->query($sql); 534 if ($resql) 535 { 536 while ($obj=$this->db->fetch_object($resql)) 537 { 538 $this->nbtodo++; 539 if ($obj->datefin < (time() - $conf->bank->rappro->warning_delay)) $this->nbtodolate++; 540 } 541 return 1; 542 } 543 else 544 { 545 dolibarr_print_error($this->db); 546 $this->error=$this->db->error(); 547 return -1; 548 } 549 } 550 551 } 552 553 554 class AccountLine 555 { 556 var $db; 557 558 /** 559 * Constructeur 560 */ 561 function AccountLine($DB, $rowid=0) 562 { 563 global $langs; 564 565 $this->db = $DB; 566 $this->rowid = $rowid; 567 568 return 1; 569 } 570 571 /** 572 * \brief Charge en memoire depuis la base, une ecriture sur le compte 573 * \param id Id de la ligne écriture à récupérer 574 */ 575 function fetch($rowid) 576 { 577 $sql = "SELECT datec, datev, dateo, amount, label, fk_user_author, fk_user_rappro,"; 578 $sql.= " fk_type, num_releve, num_chq, rappro, note"; 579 $sql.= " FROM ".MAIN_DB_PREFIX."bank"; 580 $sql.= " WHERE rowid = ".$rowid; 581 582 $result = $this->db->query($sql); 583 if ($result) 584 { 585 if ($this->db->num_rows($result)) 586 { 587 $obj = $this->db->fetch_object($result); 588 589 $this->rowid = $rowid; 590 $this->ref = $rowid; 591 592 $this->datec = $obj->datec; 593 $this->datev = $obj->datev; 594 $this->dateo = $obj->dateo; 595 $this->amount = $obj->amount; 596 $this->label = $obj->label; 597 $this->note = $obj->note; 598 599 $this->fk_user_author = $obj->fk_user_author; 600 $this->fk_user_rappro = $obj->fk_user_rappro; 601 602 $this->fk_type = $obj->fk_type; 603 $this->num_releve = $obj->num_releve; 604 $this->num_chq = $obj->num_chq; 605 606 $this->rappro = $obj->rappro; 607 } 608 $this->db->free($result); 609 } 610 else 611 { 612 dolibarr_print_error($this->db); 613 } 614 } 615 616 617 /** 618 * \brief Charge les informations d'ordre info dans l'objet facture 619 * \param id Id de la facture a charger 620 */ 621 function info($rowid) 622 { 623 $sql = 'SELECT b.rowid, '.$this->db->pdate('datec').' as datec,'; 624 $sql.= ' fk_user_author, fk_user_rappro'; 625 $sql.= ' FROM '.MAIN_DB_PREFIX.'bank as b'; 626 $sql.= ' WHERE b.rowid = '.$rowid; 627 628 $result=$this->db->query($sql); 629 if ($result) 630 { 631 if ($this->db->num_rows($result)) 632 { 633 $obj = $this->db->fetch_object($result); 634 $this->id = $obj->rowid; 635 636 if ($obj->fk_user_author) 637 { 638 $cuser = new User($this->db, $obj->fk_user_author); 639 $cuser->fetch(); 640 $this->user_creation = $cuser; 641 } 642 if ($obj->fk_user_rappro) 643 { 644 $ruser = new User($this->db, $obj->fk_user_rappro); 645 $ruser->fetch(); 646 $this->user_rappro = $ruser; 647 } 648 649 $this->date_creation = $obj->datec; 650 //$this->date_rappro = $obj->daterappro; // \todo pas encore gérée 651 } 652 $this->db->free($result); 653 } 654 else 655 { 656 dolibarr_print_error($this->db); 657 } 658 } 659 660 } 661 662 ?>
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 |
![]() |