[ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2004-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: bon-prelevement.class.php,v 1.22 2005/12/08 23:47:42 eldy Exp $ 19 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/bon-prelevement.class.php,v $ 20 */ 21 22 /** 23 \file htdocs/bon-prelevement.class.php 24 \ingroup prelevement 25 \brief Fichier de la classe des bons de prélévements 26 \version $Revision: 1.22 $ 27 */ 28 29 30 require_once (DOL_DOCUMENT_ROOT."/facture.class.php"); 31 require_once (DOL_DOCUMENT_ROOT."/societe.class.php"); 32 33 class BonPrelevement 34 { 35 var $db; 36 37 var $date_echeance; 38 var $raison_sociale; 39 var $reference_remise; 40 var $emetteur_code_guichet; 41 var $emetteur_numero_compte; 42 var $emetteur_code_etablissement; 43 var $total; 44 var $_fetched; 45 46 function BonPrelevement($DB, $filename='') 47 { 48 $error = 0; 49 $this->db = $DB; 50 51 $this->filename=$filename; 52 53 $this->date_echeance = time(); 54 $this->raison_sociale = ""; 55 $this->reference_remise = ""; 56 57 $this->emetteur_code_guichet = ""; 58 $this->emetteur_numero_compte = ""; 59 $this->emetteur_code_etablissement = ""; 60 61 $this->factures = array(); 62 63 $this->numero_national_emetteur = ""; 64 65 $this->methodes_trans = array(); 66 67 $this->methodes_trans[0] = "Internet"; 68 69 $this->_fetched = 0; 70 71 return 1; 72 } 73 74 /** 75 * 76 * 77 */ 78 function AddFacture($facture_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number) 79 { 80 $result = 0; 81 $ligne_id = 0; 82 83 $result = $this->AddLigne($ligne_id, $client_id, $client_nom, 84 $amount, $code_banque, $code_guichet, $number); 85 86 if ($result == 0) 87 { 88 if ($ligne_id > 0) 89 { 90 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_facture "; 91 $sql .= " (fk_facture,fk_prelevement_lignes)"; 92 $sql .= " VALUES (".$facture_id.",".$ligne_id.")"; 93 94 if ($this->db->query($sql)) 95 { 96 $result = 0; 97 } 98 else 99 { 100 $result = -1; 101 dolibarr_syslog("BonPrelevement::AddFacture Erreur $result"); 102 } 103 } 104 else 105 { 106 $result = -2; 107 dolibarr_syslog("BonPrelevement::AddFacture Erreur $result"); 108 } 109 } 110 else 111 { 112 $result = -3; 113 dolibarr_syslog("BonPrelevement::AddFacture Erreur $result"); 114 } 115 116 return $result; 117 118 } 119 120 /** 121 * 122 * 123 */ 124 function AddLigne(&$ligne_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number) 125 { 126 $result = -1; 127 $concat = 0; 128 129 if ($concat == 1) 130 { 131 /* 132 * On aggrège les lignes 133 */ 134 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."prelevement_lignes"; 135 $sql .= " WHERE fk_prelevement_bons".$this->id; 136 $sql .= " AND fk_soc =".$client_id; 137 $sql .= " AND code_banque ='".$code_banque."'"; 138 $sql .= " AND code_guichet ='".$code_guichet."'"; 139 $sql .= " AND number ='".$number."'"; 140 141 if ($this->db->query($sql)) 142 { 143 $num = $this->db->num_rows(); 144 } 145 else 146 { 147 $result = -1; 148 } 149 } 150 else 151 { 152 /* 153 * Pas de d'agrégation 154 */ 155 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_lignes (fk_prelevement_bons"; 156 $sql .= " , fk_soc , client_nom "; 157 $sql .= " , amount"; 158 $sql .= " , code_banque , code_guichet , number)"; 159 160 $sql .= " VALUES (".$this->id; 161 $sql .= ",".$client_id.",'".addslashes($client_nom)."'"; 162 $sql .= ",'".ereg_replace(",",".",$amount)."'"; 163 $sql .= ", '$code_banque', '$code_guichet', '$number')"; 164 165 if ($this->db->query($sql)) 166 { 167 $ligne_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_lignes"); 168 $result = 0; 169 } 170 else 171 { 172 dolibarr_syslog("BonPrelevement::AddLigne Erreur -2"); 173 $result = -2; 174 } 175 176 } 177 178 return $result; 179 } 180 181 /** 182 * 183 * 184 */ 185 function ReadError($error) 186 { 187 $errors = array(); 188 189 $errors[1027] = "Date invalide"; 190 191 return $errors[abs($error)]; 192 } 193 194 /** 195 * 196 * 197 */ 198 function Fetch($rowid) 199 { 200 $sql = "SELECT p.rowid, p.ref, p.amount, p.note, p.credite"; 201 $sql .= ",".$this->db->pdate("p.datec")." as dc"; 202 $sql .= ",".$this->db->pdate("p.date_trans")." as date_trans"; 203 $sql .= " , method_trans, fk_user_trans"; 204 $sql .= ",".$this->db->pdate("p.date_credit")." as date_credit"; 205 $sql .= " , fk_user_credit"; 206 $sql .= " , statut"; 207 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p"; 208 209 $sql .= " WHERE p.rowid=".$rowid; 210 211 $result=$this->db->query($sql); 212 if ($result) 213 { 214 if ($this->db->num_rows($result)) 215 { 216 $obj = $this->db->fetch_object(); 217 218 $this->id = $obj->rowid; 219 $this->ref = $obj->ref; 220 $this->amount = $obj->amount; 221 $this->note = stripslashes($obj->note); 222 $this->datec = $obj->dc; 223 $this->credite = $obj->credite; 224 225 $this->date_trans = $obj->date_trans; 226 $this->method_trans = $obj->method_trans; 227 $this->user_trans = $obj->fk_user_trans; 228 229 $this->date_credit = $obj->date_credit; 230 $this->user_credit = $obj->fk_user_credit; 231 232 $this->statut = $obj->statut; 233 234 $this->_fetched = 1; 235 236 return 0; 237 } 238 else 239 { 240 dolibarr_syslog("BonPrelevement::Fetch Erreur aucune ligne retournée"); 241 return -1; 242 } 243 } 244 else 245 { 246 dolibarr_syslog("BonPrelevement::Fetch Erreur "); 247 dolibarr_syslog($sql); 248 return -2; 249 } 250 } 251 252 /** 253 * 254 * 255 */ 256 function set_credite() 257 { 258 global $user; 259 260 $error == 0; 261 262 if ($this->db->begin()) 263 { 264 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons "; 265 $sql .= " SET credite = 1"; 266 $sql .= " WHERE rowid=".$this->id; 267 268 $result=$this->db->query($sql); 269 if (! $result) 270 { 271 dolibarr_syslog("bon-prelevement::set_credite Erreur 1"); 272 $error++; 273 } 274 275 if ($error == 0) 276 { 277 $facs = array(); 278 $facs = $this->_get_list_factures(); 279 280 for ($i = 0 ; $i < sizeof($facs) ; $i++) 281 { 282 /* Tag la facture comme impayée */ 283 dolibarr_syslog("BonPrelevement::set_credite set_payed fac ".$facs[$i]); 284 $fac = new Facture($this->db); 285 $fac->fetch($facs[$i]); 286 $result = $fac->set_payed($user); 287 } 288 } 289 290 if ($error == 0) 291 { 292 293 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes "; 294 $sql .= " SET statut = 2"; 295 $sql .= " WHERE fk_prelevement_bons=".$this->id; 296 297 if (! $this->db->query($sql)) 298 { 299 dolibarr_syslog("BonPrelevement::set_credite Erreur 1"); 300 $error++; 301 } 302 } 303 304 /* 305 * Fin de la procédure 306 * 307 */ 308 if ($error == 0) 309 { 310 $this->db->commit(); 311 return 0; 312 } 313 else 314 { 315 316 $this->db->rollback(); 317 dolibarr_syslog("BonPrelevement::set_credite ROLLBACK "); 318 319 return -1; 320 } 321 322 323 } 324 else 325 { 326 327 dolibarr_syslog("BonPrelevement::set_credite Ouverture transaction SQL impossible "); 328 return -2; 329 } 330 } 331 332 /** 333 * 334 * 335 */ 336 function set_infocredit($user, $date) 337 { 338 $error == 0; 339 340 if ($this->_fetched == 1) 341 { 342 if ($date >= $this->date_trans) 343 { 344 if ($this->db->begin()) 345 { 346 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons "; 347 $sql .= " SET fk_user_credit = ".$user->id; 348 $sql .= " , statut = 2"; 349 $sql .= " , date_credit='".$this->db->idate($date)."'"; 350 $sql .= " WHERE rowid=".$this->id; 351 $sql .= " AND statut = 1"; 352 353 if ($this->db->query($sql)) 354 { 355 $subject = "Crédit prélèvement ".$this->ref." à la banque"; 356 $message = "Le bon de prélèvement ".$this->ref; 357 $message .= " a été crédité par la banque.\n"; 358 $message .= "Date crédit : ".strftime("%A %e %B %Y", $date); 359 360 $this->Notify($user, "cr", $subject, $message); 361 } 362 else 363 { 364 dolibarr_syslog("BonPrelevement::set_infocredit Erreur 1"); 365 $error++; 366 } 367 368 /* 369 * Fin de la procédure 370 * 371 */ 372 if ($error == 0) 373 { 374 $this->db->commit(); 375 return 0; 376 } 377 else 378 { 379 $this->db->rollback(); 380 dolibarr_syslog("bon-prelevment::set_infocredit ROLLBACK "); 381 return -1; 382 } 383 } 384 else 385 { 386 dolibarr_syslog("bon-prelevement::set_infocredit Ouverture transaction SQL impossible "); 387 return -1025; 388 } 389 } 390 else 391 { 392 dolibarr_syslog("bon-prelevment::set_infocredit 1027 Date de credit < Date de trans "); 393 return -1027; 394 } 395 } 396 else 397 { 398 return -1026; 399 } 400 } 401 402 /** 403 * 404 * 405 */ 406 function set_infotrans($user, $date, $method) 407 { 408 $error == 0; 409 dolibarr_syslog("bon-prelevement::set_infotrans Start",LOG_INFO); 410 if ($this->db->begin()) 411 { 412 $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons "; 413 $sql .= " SET fk_user_trans = ".$user->id; 414 $sql .= " , date_trans='".$this->db->idate($date)."'"; 415 $sql .= " , method_trans=".$method; 416 $sql .= " , statut = 1"; 417 $sql .= " WHERE rowid=".$this->id; 418 $sql .= " AND statut = 0"; 419 420 if ($this->db->query($sql)) 421 { 422 $this->method_trans = $method; 423 424 $subject = "Transmission du prélèvement ".$this->ref." à la banque"; 425 $message = "Le bon de prélèvement ".$this->ref; 426 $message .= " a été transmis à la banque par ".$user->prenom. " ".$user->nom; 427 $message .= "\n\n"; 428 $message .= "\nMontant : ".price($this->amount); 429 $message .= "\nMéthode : ".$this->methodes_trans[$this->method_trans]; 430 $message .= "\nDate : ".strftime("%A %e %B %Y", $date); 431 432 $this->Notify($user,"tr", $subject, $message, 1); 433 } 434 else 435 { 436 dolibarr_syslog("bon-prelevement::set_infotrans Erreur 1", LOG_ERR); 437 dolibarr_syslog($this->db->error()); 438 $error++; 439 } 440 441 /* 442 * Fin de la procédure 443 * 444 */ 445 if ($error == 0) 446 { 447 $this->db->commit(); 448 return 0; 449 } 450 else 451 { 452 $this->db->rollback(); 453 dolibarr_syslog("BonPrelevement::set_infotrans ROLLBACK", LOG_ERR); 454 455 return -1; 456 } 457 } 458 else 459 { 460 461 dolibarr_syslog("BonPrelevement::set_infotrans Ouverture transaction SQL impossible", LOG_CRIT); 462 return -2; 463 } 464 } 465 466 /** 467 * 468 * 469 */ 470 function Notify($user, $action, $subject, $message, $joinfile=0) 471 { 472 $message .= "\n\n--\n"; 473 $message .= "Ceci est un message automatique envoyé par Dolibarr"; 474 475 $sql = "SELECT u.name, u.firstname, u.email"; 476 $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; 477 $sql .= " , ".MAIN_DB_PREFIX."prelevement_notifications as pn"; 478 $sql .= " WHERE pn.action ='".$action."'"; 479 $sql .= " AND u.rowid = pn.fk_user;"; 480 481 $resql = $this->db->query($sql); 482 if ($resql) 483 { 484 $num = $this->db->num_rows($resql); 485 $i = 0; 486 while ($i < $num) 487 { 488 $obj = $this->db->fetch_object($resql); 489 490 require_once (DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php"); 491 492 $sendto = $obj->firstname . " " .$obj->name . "<".$obj->email.">"; 493 $from = $user->prenom . " " .$user->nom . "<".$user->email.">"; 494 $arr_file = array(); 495 $arr_mime = array(); 496 $arr_name = array(); 497 if ($joinfile == 1) 498 { 499 $arr_file = array(DOL_DATA_ROOT.'/prelevement/bon/'.$this->ref.'.ps'); 500 $arr_mime = array("application/ps"); 501 $arr_name = array($this->ref.".ps"); 502 } 503 504 $mailfile = new CMailFile($subject,$sendto,$from,$message,$arr_file,$arr_mime,$arr_name); 505 506 $result=$mailfile->sendfile(); 507 508 $i++; 509 } 510 $this->db->free($resql); 511 } 512 } 513 514 /** 515 * \brief Recupére la liste des factures concernées 516 */ 517 function _get_list_factures() 518 { 519 $arr = array(); 520 521 /* 522 * Renvoie toutes les factures présente 523 * dans un bon de prélèvement 524 */ 525 $sql = "SELECT fk_facture"; 526 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p"; 527 $sql .= " , ".MAIN_DB_PREFIX."prelevement_lignes as pl"; 528 $sql .= " , ".MAIN_DB_PREFIX."prelevement_facture as pf"; 529 $sql .= " WHERE pf.fk_prelevement_lignes = pl.rowid"; 530 $sql .= " AND pl.fk_prelevement_bons = p.rowid"; 531 $sql .= " AND p.rowid=".$this->id; 532 533 $resql=$this->db->query($sql); 534 if ($resql) 535 { 536 $num = $this->db->num_rows($resql); 537 538 if ($num) 539 { 540 $i = 0; 541 while ($i < $num) 542 { 543 $row = $this->db->fetch_row($resql); 544 $arr[$i] = $row[0]; 545 $i++; 546 } 547 } 548 $this->db->free($resql); 549 } 550 else 551 { 552 dolibarr_syslog("Bon-Prelevement::_get_list_factures Erreur"); 553 } 554 555 return $arr; 556 } 557 558 /** 559 * 560 * 561 */ 562 function SommeAPrelever() 563 { 564 $sql = "SELECT sum(f.total_ttc)"; 565 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; 566 $sql .= " , ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; 567 568 $sql .= " WHERE f.fk_statut = 1"; 569 $sql .= " AND f.rowid = pfd.fk_facture"; 570 $sql .= " AND f.paye = 0"; 571 $sql .= " AND pfd.traite = 0"; 572 $sql .= " AND f.total_ttc > 0"; 573 $sql .= " AND f.fk_mode_reglement = 3"; 574 575 $resql = $this->db->query($sql); 576 577 if ( $resql ) 578 { 579 $row = $this->db->fetch_row($resql); 580 581 return $row[0]; 582 583 $this->db->free($resql); 584 } 585 else 586 { 587 $error = 1; 588 dolibarr_syslog("BonPrelevement::SommeAPrelever Erreur -1"); 589 dolibarr_syslog($this->db->error()); 590 } 591 } 592 593 /** 594 * \brief Renvoi nombre de factures a prélever 595 * \param banque bank 596 * \param agence agence 597 * \return int <O si erreur, sinon nbre de factures 598 */ 599 function NbFactureAPrelever($banque=0,$agence=0) 600 { 601 $sql = "SELECT count(f.total_ttc)"; 602 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; 603 $sql .= " , ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; 604 $sql .= " , ".MAIN_DB_PREFIX."societe_rib as sr"; 605 606 $sql .= " WHERE f.fk_statut = 1"; 607 $sql .= " AND f.rowid = pfd.fk_facture"; 608 $sql .= " AND f.fk_soc = sr.fk_soc"; 609 $sql .= " AND f.paye = 0"; 610 $sql .= " AND pfd.traite = 0"; 611 $sql .= " AND f.total_ttc > 0"; 612 $sql .= " AND f.fk_mode_reglement = 3"; 613 614 if ($banque == 1) 615 { 616 $sql .= " AND sr.code_banque = '".PRELEVEMENT_CODE_BANQUE."'"; 617 } 618 619 if ($agence == 1) 620 { 621 $sql .= " AND sr.code_guichet = '".PRELEVEMENT_CODE_GUICHET."'"; 622 } 623 624 $resql = $this->db->query($sql); 625 626 if ( $resql ) 627 { 628 $row = $this->db->fetch_row($resql); 629 630 $this->db->free($resql); 631 632 return $row[0]; 633 } 634 else 635 { 636 $this->error="BonPrelevement::SommeAPrelever Erreur -1 sql=".$this->db->error(); 637 dolibarr_syslog($this->error); 638 return -1; 639 } 640 } 641 642 /** 643 * \brief Cree prelevement 644 * \return int <0 si ko, nbre de facture prélevé sinon 645 */ 646 function Create($banque=0, $guichet=0) 647 { 648 global $conf; 649 650 dolibarr_syslog("BonPrelevement::Create"); 651 652 require_once (DOL_DOCUMENT_ROOT."/bon-prelevement.class.php"); 653 require_once (DOL_DOCUMENT_ROOT."/facture.class.php"); 654 require_once (DOL_DOCUMENT_ROOT."/societe.class.php"); 655 require_once (DOL_DOCUMENT_ROOT."/paiement.class.php"); 656 657 $error = 0; 658 659 $datetimeprev = time(); 660 661 $month = strftime("%m", $datetimeprev); 662 $year = strftime("%Y", $datetimeprev); 663 664 $user = new user($this->db, PRELEVEMENT_USER); 665 666 /** 667 * Lectures des factures 668 */ 669 $factures = array(); 670 $factures_prev = array(); 671 $factures_result = array(); 672 673 if (! $error) 674 { 675 676 $sql = "SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc"; 677 $sql .= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib"; 678 $sql .= ", pfd.amount"; 679 $sql .= ", s.nom"; 680 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; 681 $sql .= " , ".MAIN_DB_PREFIX."societe as s"; 682 $sql .= " , ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; 683 $sql .= " , ".MAIN_DB_PREFIX."societe_rib as sr"; 684 685 $sql .= " WHERE f.rowid = pfd.fk_facture"; 686 $sql .= " AND s.idp = f.fk_soc"; 687 $sql .= " AND s.idp = sr.fk_soc"; 688 $sql .= " AND f.fk_statut = 1"; 689 $sql .= " AND f.paye = 0"; 690 $sql .= " AND pfd.traite = 0"; 691 $sql .= " AND f.total_ttc > 0"; 692 $sql .= " AND f.fk_mode_reglement = 3"; // Mode prélèvement 693 694 if ($banque == 1) 695 { 696 $sql .= " AND sr.code_banque = '".PRELEVEMENT_CODE_BANQUE."'"; 697 } 698 if ($agence == 1) 699 { 700 $sql .= " AND sr.code_guichet = '".PRELEVEMENT_CODE_GUICHET."'"; 701 } 702 703 $resql = $this->db->query($sql); 704 705 if ($resql) 706 { 707 $num = $this->db->num_rows($resql); 708 $i = 0; 709 710 while ($i < $num) 711 { 712 $row = $this->db->fetch_row($resql); 713 $factures[$i] = $row; 714 $i++; 715 } 716 $this->db->free($resql); 717 dolibarr_syslog("$i factures à prélever"); 718 } 719 else 720 { 721 $error = 1; 722 dolibarr_syslog("Erreur -1"); 723 dolibarr_syslog($this->db->error()); 724 } 725 } 726 727 /* 728 * 729 * Verif des clients 730 * 731 */ 732 733 if (! $error) 734 { 735 /* 736 * Vérification des RIB 737 * 738 */ 739 $i = 0; 740 dolibarr_syslog("Début vérification des RIB"); 741 742 if (sizeof($factures) > 0) 743 { 744 foreach ($factures as $fac) 745 { 746 $fact = new Facture($this->db); 747 748 if ($fact->fetch($fac[0]) >= 0) 749 { 750 $soc = new Societe($this->db); 751 if ($soc->fetch($fact->socidp) >= 0) 752 { 753 if ($soc->verif_rib() == 1) 754 { 755 $factures_prev[$i] = $fac; 756 /* second tableau necessaire pour bon-prelevement */ 757 $factures_prev_id[$i] = $fac[0]; 758 $i++; 759 } 760 else 761 { 762 dolibarr_syslog("Erreur de RIB societe $fact->socidp $soc->nom"); 763 $facture_errors[$fac[0]]="Erreur de RIB societe $fact->socidp $soc->nom"; 764 } 765 } 766 else 767 { 768 dolibarr_syslog("Impossible de lire la société"); 769 } 770 } 771 else 772 { 773 dolibarr_syslog("Impossible de lire la facture"); 774 } 775 } 776 } 777 else 778 { 779 dolibarr_syslog("Aucune factures a traiter"); 780 } 781 } 782 783 784 /* 785 * 786 * 787 */ 788 789 dolibarr_syslog(sizeof($factures_prev)." factures seront prélevées"); 790 791 if (sizeof($factures_prev) > 0) 792 { 793 /* 794 * Ouverture de la transaction 795 * 796 */ 797 $result=$this->db->begin(); 798 if ($result <= 0) 799 { 800 $error++; 801 } 802 803 /* 804 * Traitements 805 * 806 */ 807 if (!$error) 808 { 809 $ref = "T".substr($year,-2).$month; 810 811 $sql = "SELECT count(*) FROM ".MAIN_DB_PREFIX."prelevement_bons"; 812 $sql .= " WHERE ref LIKE '$ref%'"; 813 814 $resql = $this->db->query($sql); 815 816 if ($resql) 817 { 818 $row = $this->db->fetch_row($resql); 819 } 820 else 821 { 822 $error++; 823 dolibarr_syslog("Erreur recherche reference"); 824 } 825 826 $ref = $ref . substr("00".($row[0]+1), -2); 827 828 $filebonprev = $ref; 829 830 /* 831 * Creation du bon de prelevement 832 * 833 */ 834 835 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_bons (ref,datec)"; 836 $sql .= " VALUES ('".$ref."',now())"; 837 838 $resql = $this->db->query($sql); 839 840 if ($resql) 841 { 842 $prev_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_bons"); 843 844 $dir=$conf->prelevement->dir_output.'/bon'; 845 $file=$filebonprev; 846 if (! is_dir($dir)) create_exdir($dir); 847 848 $bonprev = new BonPrelevement($this->db, $dir."/".$file); 849 $bonprev->id = $prev_id; 850 } 851 else 852 { 853 $error++; 854 dolibarr_syslog("Erreur création du bon de prelevement"); 855 } 856 } 857 858 /* 859 * 860 * 861 */ 862 if (!$error) 863 { 864 dolibarr_syslog("Début génération des paiements"); 865 dolibarr_syslog("Nombre de factures ".sizeof($factures_prev)); 866 867 if (sizeof($factures_prev) > 0) 868 { 869 foreach ($factures_prev as $fac) 870 { 871 $fact = new Facture($this->db); 872 $fact->fetch($fac[0]); 873 874 $pai = new Paiement($this->db); 875 876 $pai->amounts = array(); 877 $pai->amounts[$fac[0]] = $fact->total_ttc; 878 $pai->datepaye = $this->db->idate($datetimeprev); 879 $pai->paiementid = 3; // prélèvement 880 $pai->num_paiement = $ref; 881 882 if ($pai->create($user, 1) == -1) // on appelle en no_commit 883 { 884 $error++; 885 dolibarr_syslog("Erreur creation paiement facture ".$fac[0]); 886 } 887 else 888 { 889 /* 890 * Validation du paiement 891 */ 892 $pai->valide(); 893 894 /* 895 * Ajout d'une ligne de prélèvement 896 * 897 * 898 * $fac[3] : banque 899 * $fac[4] : guichet 900 * $fac[5] : number 901 * $fac[6] : cle rib 902 * $fac[7] : amount 903 * $fac[8] : client nom 904 * $fac[2] : client id 905 */ 906 907 $ri = $bonprev->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], 908 $fac[3], $fac[4], $fac[5], $fac[6]); 909 if ($ri <> 0) 910 { 911 $error++; 912 } 913 914 /* 915 * Mise à jour des demandes 916 * 917 */ 918 $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_facture_demande"; 919 $sql .= " SET traite = 1"; 920 $sql .= ", date_traite=now()"; 921 $sql .= ", fk_prelevement_bons = ".$prev_id; 922 $sql .= " WHERE rowid=".$fac[1]; 923 924 if ($this->db->query($sql)) 925 { 926 927 } 928 else 929 { 930 $error++; 931 dolibarr_syslog("Erreur mise a jour des demandes"); 932 dolibarr_syslog($this->db->error()); 933 } 934 935 } 936 } 937 } 938 939 dolibarr_syslog("Fin des paiements"); 940 } 941 942 if (!$error) 943 { 944 /* 945 * Bon de Prelevement 946 * 947 * 948 */ 949 950 dolibarr_syslog("Debut prelevement"); 951 dolibarr_syslog("Nombre de factures ".sizeof($factures_prev)); 952 953 if (sizeof($factures_prev) > 0) 954 { 955 $bonprev->date_echeance = $datetimeprev; 956 $bonprev->reference_remise = $ref; 957 958 $bonprev->numero_national_emetteur = PRELEVEMENT_NUMERO_NATIONAL_EMETTEUR; 959 $bonprev->raison_sociale = PRELEVEMENT_RAISON_SOCIALE; 960 961 $bonprev->emetteur_code_etablissement = PRELEVEMENT_CODE_BANQUE; 962 $bonprev->emetteur_code_guichet = PRELEVEMENT_CODE_GUICHET; 963 $bonprev->emetteur_numero_compte = PRELEVEMENT_NUMERO_COMPTE; 964 965 966 $bonprev->factures = $factures_prev_id; 967 968 $bonprev->generate(); 969 } 970 dolibarr_syslog( $filebonprev ) ; 971 dolibarr_syslog("Fin prelevement"); 972 } 973 974 /* 975 * Mise à jour du total 976 * 977 */ 978 979 $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons"; 980 $sql .= " SET amount = ".ereg_replace(",",".",$bonprev->total); 981 $sql .= " WHERE rowid = ".$prev_id; 982 983 $resql=$this->db->query($sql); 984 if (! $resql) 985 { 986 $error++; 987 dolibarr_syslog("Erreur mise à jour du total - $sql"); 988 } 989 990 /* 991 * Rollback ou Commit 992 * 993 */ 994 if (!$error) 995 { 996 $this->db->commit(); 997 dolibarr_syslog("COMMIT"); 998 } 999 else 1000 { 1001 $this->db->rollback(); 1002 dolibarr_syslog("ROLLBACK"); 1003 } 1004 1005 return sizeof($factures_prev); 1006 } 1007 else 1008 { 1009 return 0; 1010 } 1011 } 1012 1013 /** 1014 * 1015 * 1016 */ 1017 function DeleteNotificationById($rowid) 1018 { 1019 $result = 0; 1020 1021 $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_notifications "; 1022 $sql .= " WHERE rowid = '".$rowid."';"; 1023 1024 if ($this->db->query($sql)) 1025 { 1026 return 0; 1027 } 1028 else 1029 { 1030 return -1; 1031 } 1032 } 1033 1034 /** 1035 * 1036 * 1037 */ 1038 function DeleteNotification($user, $action) 1039 { 1040 $result = 0; 1041 1042 $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_notifications "; 1043 $sql .= " WHERE fk_user = '".$user."' AND action = '".$action."';"; 1044 1045 if ($this->db->query($sql)) 1046 { 1047 return 0; 1048 } 1049 else 1050 { 1051 return -1; 1052 } 1053 } 1054 1055 /* 1056 * 1057 * 1058 */ 1059 function AddNotification($user, $action) 1060 { 1061 $result = 0; 1062 1063 if ($this->DeleteNotification($user, $action) == 0) 1064 { 1065 1066 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_notifications "; 1067 $sql .= " (fk_user, action )"; 1068 $sql .= " VALUES ('".$user."','".$action."');"; 1069 1070 if ($this->db->query($sql)) 1071 { 1072 $result = 0; 1073 } 1074 else 1075 { 1076 $result = -1; 1077 dolibarr_syslog("BonPrelevement::AddNotification Erreur $result"); 1078 } 1079 } 1080 1081 return $result; 1082 } 1083 1084 /** 1085 * Génération d'un bon de prélèvement 1086 * 1087 */ 1088 function Generate() 1089 { 1090 $result = -1; 1091 1092 $this->file = fopen ($this->filename,"w"); 1093 1094 1095 /* 1096 * En-tete Emetteur 1097 */ 1098 $this->EnregEmetteur(); 1099 1100 /* 1101 * Lignes 1102 */ 1103 $this->total = 0; 1104 1105 $sql = "SELECT rowid, client_nom, code_banque, code_guichet, number, amount"; 1106 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_lignes"; 1107 $sql .= " WHERE fk_prelevement_bons = ".$this->id; 1108 1109 $i = 0; 1110 1111 if ($this->db->query($sql)) 1112 { 1113 $num = $this->db->num_rows(); 1114 1115 while ($i < $num) 1116 { 1117 $row = $this->db->fetch_row(); 1118 1119 $this->EnregDestinataire($row[0], 1120 $row[1], 1121 $row[2], 1122 $row[3], 1123 $row[4], 1124 $row[5]); 1125 1126 $this->total = $this->total + $row[5]; 1127 1128 $i++; 1129 } 1130 } 1131 else 1132 { 1133 $result = -2; 1134 } 1135 /* 1136 $nbfactures = sizeof($this->factures); 1137 for ($i = 0 ; $i < $nbfactures ; $i++) 1138 { 1139 $fac = new Facture($this->db); 1140 $fac->fetch($this->factures[$i]); 1141 $fac->fetch_client(); 1142 $fac->client->rib(); 1143 if ($fac->client->bank_account->verif()) { 1144 $this->total = $this->total + $fac->total_ttc; 1145 $this->EnregDestinataire($fac); 1146 }else{ 1147 print $fac->client->bank_account->error_message; 1148 print $fac->client->nom; } 1149 } 1150 */ 1151 1152 /* 1153 * Pied de page total 1154 */ 1155 1156 $this->EnregTotal($this->total); 1157 1158 fclose($this->file); 1159 1160 return $result; 1161 } 1162 1163 1164 /** 1165 * Enregistrements destinataires 1166 * 1167 * 1168 */ 1169 function EnregDestinataire($rowid, $client_nom, $rib_banque, $rib_guichet, $rib_number, $amount) 1170 { 1171 fputs ($this->file, "06"); 1172 fputs ($this->file, "08"); // Prélèvement ordinaire 1173 1174 fputs ($this->file, " "); // Zone Réservée B2 1175 1176 fputs ($this->file, $this->numero_national_emetteur); // Numéro National d'emmetteur B3 1177 1178 // Date d'échéance C1 1179 1180 fputs ($this->file, " "); 1181 fputs ($this->file, strftime("%d%m", $this->date_echeance)); 1182 fputs ($this->file, substr(strftime("%y", $this->date_echeance),1)); 1183 1184 // Raison Sociale Destinataire C2 1185 1186 fputs ($this->file, substr($client->nom. " ",0,24)); 1187 1188 // Domiciliation facultative D1 1189 1190 fputs ($this->file, substr(" ",0,24)); 1191 1192 // Zone Réservée D2 1193 1194 fputs ($this->file, substr(" ",0,8)); 1195 1196 // Code Guichet D3 1197 1198 fputs ($this->file, $rib_guichet); 1199 1200 // Numero de compte D4 1201 1202 fputs ($this->file, substr("000000000000000".$rib_number, -11)); 1203 1204 // Zone E Montant 1205 1206 $montant = (round($amount,2) * 100); 1207 1208 fputs ($this->file, substr("000000000000000".$montant, -16)); 1209 1210 // Libellé F 1211 1212 fputs ($this->file, substr("*".$this->ref.$rowid." ",0,13)); 1213 fputs ($this->file, substr(" ",0,18)); 1214 1215 // Code établissement G1 1216 1217 fputs ($this->file, $rib_banque); 1218 1219 // Zone Réservée G2 1220 1221 fputs ($this->file, substr(" ",0,5)); 1222 1223 fputs ($this->file, "\n"); 1224 } 1225 1226 1227 /** 1228 * Enregistrements destinataires 1229 * 1230 * 1231 */ 1232 function EnregDestinataireVersion1($fac) 1233 { 1234 fputs ($this->file, "06"); 1235 fputs ($this->file, "08"); // Prélèvement ordinaire 1236 1237 fputs ($this->file, " "); // Zone Réservée B2 1238 1239 fputs ($this->file, $this->numero_national_emetteur); // Numéro National d'emmetteur B3 1240 1241 // Date d'échéance C1 1242 1243 fputs ($this->file, " "); 1244 fputs ($this->file, strftime("%d%m", $this->date_echeance)); 1245 fputs ($this->file, substr(strftime("%y", $this->date_echeance),1)); 1246 1247 // Raison Sociale Destinataire C2 1248 1249 fputs ($this->file, substr($fac->client->nom. " ",0,24)); 1250 1251 // Reference de la remise créancier D1 1252 1253 fputs ($this->file, substr(" ",0,24)); 1254 1255 // Zone Réservée D2 1256 1257 fputs ($this->file, substr(" ",0,8)); 1258 1259 // Code Guichet D3 1260 1261 fputs ($this->file, $fac->client->bank_account->code_guichet); 1262 1263 // Numero de compte D4 1264 1265 fputs ($this->file, substr("000000000000000".$fac->client->bank_account->number, -11)); 1266 1267 // Zone E Montant 1268 1269 $montant = (round($fac->total_ttc,2) * 100); 1270 1271 fputs ($this->file, substr("000000000000000".$montant, -16)); 1272 1273 // Libellé F 1274 1275 fputs ($this->file, substr("*".$fac->ref." ",0,13)); 1276 fputs ($this->file, substr(" ",0,18)); 1277 1278 // Code établissement G1 1279 1280 fputs ($this->file, $fac->client->bank_account->code_banque); 1281 1282 // Zone Réservée G2 1283 1284 fputs ($this->file, substr(" ",0,5)); 1285 1286 fputs ($this->file, "\n"); 1287 } 1288 1289 /** 1290 * 1291 * 1292 */ 1293 function EnregEmetteur() 1294 { 1295 fputs ($this->file, "03"); 1296 fputs ($this->file, "08"); // Prélèvement ordinaire 1297 1298 fputs ($this->file, " "); // Zone Réservée B2 1299 1300 fputs ($this->file, $this->numero_national_emetteur); // Numéro National d'emmetteur B3 1301 1302 // Date d'échéance C1 1303 1304 fputs ($this->file, " "); 1305 fputs ($this->file, strftime("%d%m", $this->date_echeance)); 1306 fputs ($this->file, substr(strftime("%y", $this->date_echeance),1)); 1307 1308 // Raison Sociale C2 1309 1310 fputs ($this->file, substr($this->raison_sociale. " ",0,24)); 1311 1312 // Reference de la remise créancier D1 sur 7 caractéres 1313 1314 fputs ($this->file, substr($this->reference_remise. " ",0,7)); 1315 1316 // Zone Réservée D1-2 1317 1318 fputs ($this->file, substr(" ",0,17)); 1319 1320 // Zone Réservée D2 1321 1322 fputs ($this->file, substr(" ",0,2)); 1323 fputs ($this->file, "E"); 1324 fputs ($this->file, substr(" ",0,5)); 1325 1326 // Code Guichet D3 1327 1328 fputs ($this->file, $this->emetteur_code_guichet); 1329 1330 // Numero de compte D4 1331 1332 fputs ($this->file, substr("000000000000000".$this->emetteur_numero_compte, -11)); 1333 1334 // Zone Réservée E 1335 1336 fputs ($this->file, substr(" ",0,16)); 1337 1338 // Zone Réservée F 1339 1340 fputs ($this->file, substr(" ",0,31)); 1341 1342 // Code établissement 1343 1344 fputs ($this->file, $this->emetteur_code_etablissement); 1345 1346 // Zone Réservée G 1347 1348 fputs ($this->file, substr(" ",0,5)); 1349 1350 fputs ($this->file, "\n"); 1351 1352 } 1353 1354 /** 1355 * Pied de page 1356 * 1357 */ 1358 function EnregTotal($total) 1359 { 1360 fputs ($this->file, "08"); 1361 fputs ($this->file, "08"); // Prélèvement ordinaire 1362 1363 fputs ($this->file, " "); // Zone Réservée B2 1364 1365 fputs ($this->file, $this->numero_national_emetteur); // Numéro National d'emmetteur B3 1366 1367 // Réservé C1 1368 1369 fputs ($this->file, substr(" ",0,12)); 1370 1371 1372 // Raison Sociale C2 1373 1374 fputs ($this->file, substr(" ",0,24)); 1375 1376 // D1 1377 1378 fputs ($this->file, substr(" ",0,24)); 1379 1380 // Zone Réservée D2 1381 1382 fputs ($this->file, substr(" ",0,8)); 1383 1384 // Code Guichet D3 1385 1386 fputs ($this->file, substr(" ",0,5)); 1387 1388 // Numero de compte D4 1389 1390 fputs ($this->file, substr(" ",0,11)); 1391 1392 // Zone E Montant 1393 1394 $montant = ($total * 100); 1395 1396 fputs ($this->file, substr("000000000000000".$montant, -16)); 1397 1398 // Zone Réservée F 1399 1400 fputs ($this->file, substr(" ",0,31)); 1401 1402 // Code établissement 1403 1404 fputs ($this->file, substr(" ",0,5)); 1405 1406 // Zone Réservée F 1407 1408 fputs ($this->file, substr(" ",0,5)); 1409 1410 fputs ($this->file, "\n"); 1411 } 1412 } 1413 1414 ?>
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 |
![]() |