[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/ -> paiement.class.php (source)

   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)      2005 Marc Barilley / Ocebo <marc@ocebo.com>
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License
  17   * along with this program; if not, write to the Free Software
  18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19   *
  20   * $Id: paiement.class.php,v 1.43 2005/10/30 16:52:52 eldy Exp $
  21   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/paiement.class.php,v $
  22   */
  23  
  24  /**
  25          \file       htdocs/paiement.class.php
  26          \ingroup    facture
  27          \brief      Fichier de la classe des paiement de factures clients
  28          \version    $Revision: 1.43 $
  29  */
  30  
  31  
  32  /**     \class      Paiement
  33            \brief      Classe permettant la gestion des paiements des factures clients
  34  */
  35  
  36  class Paiement
  37  {
  38      var $id;
  39      var $ref;
  40      var $facid;
  41      var $datepaye;
  42      var $amount;
  43      var $author;
  44      var $paiementid;    // Type de paiement. Stocké dans fk_paiement
  45                          // de llx_paiement qui est lié aux types de
  46                          //paiement de llx_c_paiement
  47      var $num_paiement;    // Numéro du CHQ, VIR, etc...
  48      var $bank_account;    // Id compte bancaire du paiement
  49      var $bank_line;     // Id de la ligne d'écriture bancaire
  50      var $note;
  51      // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
  52      // fk_paiement dans llx_paiement_facture est le rowid du paiement
  53  
  54      var $db;
  55  
  56    /**
  57     *    \brief  Constructeur de la classe
  58     *    \param  DB          handler accès base de données
  59     *    \param  soc_idp     id societe ('' par defaut)
  60     */
  61  
  62  	function Paiement($DB, $soc_idp='')
  63      {
  64          $this->db = $DB ;
  65      }
  66  
  67      /**
  68       *    \brief      Récupère l'objet paiement
  69       *    \param      id      id du paiement a récupérer
  70       *    \return     int     <0 si ko, >0 si ok
  71       */
  72      function fetch($id)
  73      {
  74          $sql = 'SELECT p.rowid,'.$this->db->pdate('p.datep').' as dp, p.amount, p.statut, p.fk_bank';
  75          $sql .=', c.libelle as paiement_type';
  76          $sql .= ', p.num_paiement, p.note, b.fk_account';
  77          $sql .= ' FROM '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement as p';
  78          $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid ';
  79          $sql .= ' WHERE p.fk_paiement = c.id';
  80          $sql .= ' AND p.rowid = '.$id;
  81  
  82          if ($this->db->query($sql))
  83          {
  84              if ($this->db->num_rows())
  85              {
  86                  $obj = $this->db->fetch_object();
  87                  $this->id             = $obj->rowid;
  88                  $this->ref            = $obj->rowid;
  89                  $this->date           = $obj->dp;
  90                  $this->numero         = $obj->num_paiement;
  91                  $this->bank_account   = $obj->fk_account;
  92                  $this->bank_line      = $obj->fk_bank;
  93                  $this->montant        = $obj->amount;
  94                  $this->note           = $obj->note;
  95                  $this->type_libelle   = $obj->paiement_type;
  96                  $this->statut         = $obj->statut;
  97                  return 1;
  98              }
  99              else
 100              {
 101                  return -2;
 102              }
 103              $this->db->free();
 104          }
 105          else
 106          {
 107              dolibarr_print_error($this->db);
 108              return -1;
 109          }
 110      }
 111  
 112    /**
 113     *    \brief      Création du paiement en base
 114     *    \param      user        object utilisateur qui crée
 115     *    \return     int         id du paiement crée, < 0 si erreur
 116     */
 117  
 118  	function create($user)
 119      {
 120          $sql_err = 0;
 121  
 122          $this->db->begin();
 123  
 124          $total = 0.0;
 125          foreach ($this->amounts as $key => $value)
 126          {
 127              $val = $value;
 128              $val = str_replace(' ','',$val);
 129              $val = str_replace(',','.',$val);
 130              $val = round($val, 2);
 131              $val = str_replace(',','.',$val);
 132              if (is_numeric($val))
 133              {
 134                  $total += $val;
 135              }
 136              $this->amounts[$key] = $val;
 137          }
 138          $total = str_replace(',','.',$total);
 139          if ($total <> 0) /* On accepte les montants négatifs pour les rejets de prélèvement */
 140          {
 141              $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_creat)';
 142              $sql .= ' VALUES (now(), '.$this->datepaye.', \''.$total.'\', '.$this->paiementid.', \''.$this->num_paiement.'\', \''.$this->note.'\', '.$user->id.')';
 143              $resql = $this->db->query($sql);
 144              if ($resql)
 145              {
 146                  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
 147                  foreach ($this->amounts as $key => $amount)
 148                  {
 149                      $facid = $key;
 150                      if (is_numeric($amount) && $amount <> 0)
 151                      {
 152                          $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount)';
 153                          $sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\')';
 154                          if (! $this->db->query($sql) )
 155                          {
 156                              dolibarr_syslog('Paiement::Create Erreur INSERT dans paiement_facture '.$facid);
 157                              $sql_err++;
 158                          }
 159                      }
 160                      else
 161                      {
 162                          dolibarr_syslog('Paiement::Create Montant non numérique');
 163                      }
 164                  }
 165              }
 166              else
 167              {
 168                  dolibarr_syslog('Paiement::Create Erreur INSERT dans paiement');
 169                  $sql_err++;
 170              }
 171          }
 172  
 173          if ( $total <> 0 && $sql_err == 0 ) // On accepte les montants négatifs
 174          {
 175              $this->db->commit();
 176              dolibarr_syslog('Paiement::Create Ok Total = '.$total);
 177              return $this->id;
 178          }
 179          else
 180          {
 181              $this->db->rollback();
 182              dolibarr_syslog('Paiement::Create Erreur');
 183              return -1;
 184          }
 185      }
 186  
 187    /**
 188     *    \brief      Affiche la liste des modes de paiement possible
 189     *    \param      name        nom du champ select
 190     *    \param      filtre      filtre sur un sens de paiement particulier, norme ISO (CRDT=Mode propre à un crédit, DBIT=mode propre à un débit)
 191     *    \param      id          ???
 192     */
 193  	function select($name, $filtre='', $id='')
 194      {
 195          $form = new Form($this->db);
 196  
 197          if ($filtre == 'CRDT')
 198          {
 199              $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 AND type IN (0,2) ORDER BY libelle';
 200          }
 201          elseif ($filtre == 'DBIT')
 202          {
 203              $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 AND type IN (1,2) ORDER BY libelle';
 204          }
 205          else
 206          {
 207              $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 ORDER BY libelle';
 208          }
 209          $form->select($name, $sql, $id);
 210      }
 211  
 212      /**
 213       *      \brief      Supprime un paiement ainsi que les lignes qu'il a généré dans comptes
 214       *                  Si le paiement porte sur un écriture compte qui est rapprochée, on refuse
 215       *                  Si le paiement porte sur au moins une facture à "payée", on refuse
 216       *      \return     int     <0 si ko, >0 si ok
 217       */
 218  	function delete()
 219      {
 220            $bank_line_id = $this->bank_line;
 221  
 222          $this->db->begin();
 223  
 224          // Vérifier si paiement porte pas sur une facture à l'état payée
 225          // Si c'est le cas, on refuse la suppression
 226          $billsarray=$this->getBillsArray('paye=1');
 227          if (is_array($billsarray))
 228          {
 229              if (sizeof($billsarray))
 230              {
 231                  $this->error="Impossible de supprimer un paiement portant sur au moins une facture à l'état payé";
 232                  $this->db->rollback();
 233                  return -1;
 234              }
 235          }
 236          else
 237          {
 238              $this->db->rollback();
 239              return -2;
 240          }       
 241  
 242            // Vérifier si paiement ne porte pas sur ecriture bancaire rapprochée
 243            // Si c'est le cas, on refuse le paiement
 244          if ($bank_line_id)
 245          {
 246              $accline = new AccountLine($this->db,$bank_line_id);
 247              $accline->fetch($bank_line_id);
 248              if ($accline->rappro)
 249              {
 250                  $this->error="Impossible de supprimer un paiement qui a généré une écriture qui a été rapprochée";
 251                  $this->db->rollback();
 252                  return -3;
 253              }            
 254          }
 255  
 256          // Efface la ligne de paiement (dans paiement_facture et paiement)
 257          $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
 258          $sql.= ' WHERE fk_paiement = '.$this->id;
 259          $result = $this->db->query($sql);
 260          if ($result)
 261          {
 262              $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
 263              $sql.= ' WHERE rowid = '.$this->id;
 264              $result = $this->db->query($sql);
 265              if (! $result)
 266              {
 267                  $this->error=$this->db->error();
 268                  $this->db->rollback();
 269                  return -3;
 270              }
 271  
 272              // Supprimer l'écriture bancaire si paiement lié à écriture
 273              if ($bank_line_id)
 274              {
 275                  $acc = new Account($this->db);
 276                  $result=$acc->deleteline($bank_line_id);
 277                  if ($result < 0)
 278                  {
 279                      $this->error=$acc->error;
 280                      $this->db->rollback();
 281                      return -4;
 282                  }
 283              }
 284  
 285              $this->db->commit();
 286              return 1;
 287          }
 288          else
 289          {
 290              $this->error=$this->db->error;
 291              $this->db->rollback();
 292              return -5;
 293          }
 294      }
 295  
 296      /**
 297       *      \brief      Mise a jour du lien entre le paiement et la ligne générée dans llx_bank
 298       *      \param      id_bank     Id compte bancaire
 299       */
 300  	function update_fk_bank($id_bank)
 301      {
 302          $sql = 'UPDATE llx_paiement set fk_bank = '.$id_bank;
 303          $sql.= ' WHERE rowid = '.$this->id;
 304          $result = $this->db->query($sql);
 305          if ($result)
 306          {
 307              return 1;
 308          }
 309          else
 310          {
 311              dolibarr_print_error($this->db);
 312              return 0;
 313          }
 314      }
 315  
 316      /**
 317       *    \brief      Valide le paiement
 318       *    \return     int     <0 si ko, >0 si ok
 319       */
 320  	function valide()
 321      {
 322          $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiement SET statut = 1 WHERE rowid = '.$this->id;
 323          $result = $this->db->query($sql);
 324          if ($result)
 325          {
 326              return 0;
 327          }
 328          else
 329          {
 330              dolibarr_syslog('Paiement::Valide Error -1');
 331              return -1;
 332          }
 333      }
 334  
 335      /*
 336       *    \brief      Information sur l'objet
 337       *    \param      id      id du paiement dont il faut afficher les infos
 338       */
 339      function info($id)
 340      {
 341          $sql = 'SELECT c.rowid, '.$this->db->pdate('datec').' as datec, fk_user_creat, fk_user_modif';
 342          $sql .= ', '.$this->db->pdate('tms').' as tms';
 343          $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement as c';
 344          $sql .= ' WHERE c.rowid = '.$id;
 345  
 346          if ($this->db->query($sql))
 347          {
 348              if ($this->db->num_rows())
 349              {
 350                  $obj = $this->db->fetch_object();
 351                  $this->id = $obj->idp;
 352                  if ($obj->fk_user_creat)
 353                  {
 354                      $cuser = new User($this->db, $obj->fk_user_creat);
 355                      $cuser->fetch();
 356                      $this->user_creation     = $cuser;
 357                  }
 358                  if ($obj->fk_user_modif)
 359                  {
 360                      $muser = new User($this->db, $obj->fk_user_modif);
 361                      $muser->fetch();
 362                      $this->user_modification = $muser;
 363                  }
 364                  $this->date_creation     = $obj->datec;
 365                  $this->date_modification = $obj->tms;
 366              }
 367              $this->db->free();
 368          }
 369          else
 370          {
 371              dolibarr_print_error($this->db);
 372          }
 373      }
 374      
 375      /**
 376       *      \brief      Retourne la liste des factures sur lesquels porte le paiement
 377       *      \param      filter          Critere de filtre
 378       *      \return     array           Tableau des id de factures
 379       */
 380  	function getBillsArray($filter='')
 381      {
 382          $sql = 'SELECT fk_facture';
 383          $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f';
 384          $sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id;
 385          if ($filter) $sql.= ' AND '.$filter;
 386          $resql = $this->db->query($sql);
 387          if ($resql)
 388          {
 389              $i=0;
 390              $num=$this->db->num_rows($resql);
 391              $billsarray=array();
 392  
 393              while ($i < $num)
 394              {
 395                  $obj = $this->db->fetch_object($resql);
 396                  $billsarray[$i]=$obj->fk_facture;
 397                  $i++;
 398              }
 399  
 400              return $billsarray;
 401          }
 402          else
 403          {
 404              $this->error=$this->db->error();
 405              dolibarr_syslog('Paiement::getBillsArray Error '.$this->error.' - sql='.$sql);
 406              return -1;
 407          }
 408      }
 409  
 410  }
 411  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics