[ 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/fourn/facture/ -> paiementfourn.class.php (source)

   1  <?php
   2  /* Copyright (C) 2002-2004 Rodolphe Quiedeville  <rodolphe@quiedeville.org>
   3   * Copyright (C) 2004      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: paiementfourn.class.php,v 1.25 2005/11/04 01:34:36 eldy Exp $
  21   * $Source: /cvsroot/dolibarr/dolibarr/htdocs/fourn/facture/paiementfourn.class.php,v $
  22   */
  23  
  24  /*!
  25          \file       htdocs/fourn/facture/paiementfourn.class.php
  26          \ingroup    fournisseur, facture
  27          \brief      Page de création de paiement factures fournisseurs
  28          \version    $Revision: 1.25 $
  29  */
  30  
  31  require_once (DOL_DOCUMENT_ROOT.'/compta/bank/account.class.php');
  32  
  33  /**
  34      \class      PaiementFourn
  35      \brief      Classe permettant la gestion des paiements des factures fournisseurs
  36  */
  37  
  38  class PaiementFourn
  39  {
  40      var $id;
  41      var $ref;
  42      var $facid;
  43      var $datepaye;
  44      var $amount;
  45      var $total;
  46      var $author;
  47      var $paiementid;    // Type de paiement. Stocké dans fk_paiement
  48                          // de llx_paiement qui est lié aux types de
  49                          //paiement de llx_c_paiement
  50      var $num_paiement;    // Numéro du CHQ, VIR, etc...
  51      var $bank_account;    // Id compte bancaire du paiement
  52      var $bank_line;        // Id de la ligne d'écriture bancaire
  53      var $note;
  54      // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
  55      // fk_paiement dans llx_paiement_facture est le rowid du paiement
  56  
  57      var $db;
  58  
  59      /**
  60       *    \brief  Constructeur de la classe
  61       *    \param  DB          handler accès base de données
  62       */
  63  
  64  	function PaiementFourn($DB)
  65      {
  66          $this->db = $DB ;
  67      }
  68  
  69      /**
  70       *    \brief      Récupère l'objet paiement
  71       *    \param      id      id du paiement a récupérer
  72       *    \return     int     <0 si ko, >0 si ok
  73       */
  74  	function fetch($id)
  75      {
  76          $sql = 'SELECT p.rowid,'.$this->db->pdate('p.datep').' as dp, p.amount, p.statut, p.fk_bank,';
  77          $sql.= ' c.libelle as paiement_type,';
  78          $sql.= ' p.num_paiement, p.note, b.fk_account';
  79          $sql.= ' FROM '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiementfourn as p';
  80          $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid ';
  81          $sql.= ' WHERE p.fk_paiement = c.id';
  82          $sql.= ' AND p.rowid = '.$id;
  83          $resql = $this->db->query($sql);
  84          if ($resql)
  85          {
  86              $num = $this->db->num_rows($resql);
  87              if ($num > 0)
  88              {
  89                  $obj = $this->db->fetch_object($resql);
  90                  $this->id             = $obj->rowid;
  91                  $this->ref            = $obj->rowid;
  92                  $this->date           = $obj->dp;
  93                  $this->numero         = $obj->num_paiement;
  94                  $this->bank_account   = $obj->fk_account;
  95                  $this->bank_line      = $obj->fk_bank;
  96                  $this->montant        = $obj->amount;
  97                  $this->note           = $obj->note;
  98                  $this->type_libelle   = $obj->paiement_type;
  99                  $this->statut         = $obj->statut;
 100                  $error = 1;
 101              }
 102              else
 103              {
 104                  $error = -2;
 105              }
 106              $this->db->free($resql);
 107          }
 108          else
 109          {
 110              dolibarr_print_error($this->db);
 111              $error = -1;
 112          }
 113          return $error;
 114      }
 115  
 116      /**
 117       *    \brief      Création du paiement en base
 118       *    \param      user        object utilisateur qui crée
 119       *    \return     int         id du paiement crée, < 0 si erreur
 120       */
 121  	function create($user)
 122      {
 123          $sql_err = 0;
 124  
 125          $this->db->begin();
 126  
 127          $this->total = 0.0;
 128          foreach ($this->amounts as $key => $value)
 129          {
 130              $val = price2num($value);
 131              if (is_numeric($val))
 132              {
 133                  $val = price2num(round($val, 2));
 134                  $this->total += $val;
 135              }
 136              $this->amounts[$key] = $val;
 137          }
 138          $this->total = price2num($this->total);
 139          if ($this->total <> 0) /* On accepte les montants négatifs pour les avoirs ??? */
 140          {
 141              $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_author)';
 142              $sql .= ' VALUES (now(), '.$this->datepaye.', \''.$this->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.'paiementfourn');
 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.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, 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('PaiementFourn::Create Montant non numérique');
 163                      }
 164                  }
 165              }
 166              else
 167              {
 168                  dolibarr_syslog('PaiementFourn::Create Erreur INSERT dans paiementfourn');
 169                  $sql_err++;
 170              }
 171          }
 172  
 173          if ( $this->total <> 0 && $sql_err == 0 ) // On accepte les montants négatifs
 174          {
 175              $this->db->commit();
 176              dolibarr_syslog('PaiementFourn::Create Ok Total = '.$this->total);
 177              return $this->id;
 178          }
 179          else
 180          {
 181              $this->db->rollback();
 182              dolibarr_syslog('PaiementFourn::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' || $filtre == 'crédit')
 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' || $filtre == 'débit')
 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.'paiementfourn_facturefourn';
 258          $sql.= ' WHERE fk_paiementfourn = '.$this->id;
 259          $resql = $this->db->query($sql);
 260          if ($resql)
 261          {
 262              $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiementfourn';
 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              $this->db->commit();
 285              return 1;
 286          }
 287          else
 288          {
 289              $this->error=$this->db->error;
 290              $this->db->rollback();
 291              return -5;
 292          }
 293      }
 294  
 295      /**
 296       *      \brief      Mise a jour du lien entre le paiement et la ligne générée dans llx_bank
 297       *      \param      id_bank     Id compte bancaire
 298       */
 299  	function update_fk_bank($id_bank)
 300      {
 301          $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiementfourn set fk_bank = '.$id_bank;
 302          $sql.= ' WHERE rowid = '.$this->id;
 303          $result = $this->db->query($sql);
 304          if ($result)
 305          {
 306              return 1;
 307          }
 308          else
 309          {
 310              dolibarr_print_error($this->db);
 311              return 0;
 312          }
 313      }
 314  
 315      /**
 316       *    \brief      Valide le paiement
 317       *    \return     int     <0 si ko, >0 si ok
 318       */
 319  	function valide()
 320      {
 321          $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiementfourn SET statut = 1 WHERE rowid = '.$this->id;
 322          $result = $this->db->query($sql);
 323          if ($result)
 324          {
 325              return 0;
 326          }
 327          else
 328          {
 329              dolibarr_syslog('Paiement::Valide Error -1');
 330              return -1;
 331          }
 332      }
 333  
 334      /*
 335       *    \brief      Information sur l'objet
 336       *    \param      id      id du paiement dont il faut afficher les infos
 337       */
 338  	function info($id)
 339      {
 340          $sql = 'SELECT c.rowid, '.$this->db->pdate('datec').' as datec, fk_user_author';
 341          $sql .= ', '.$this->db->pdate('tms').' as tms';
 342          $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as c';
 343          $sql .= ' WHERE c.rowid = '.$id;
 344          $resql = $this->db->query($sql);
 345          if ($resql)
 346          {
 347              $num = $this->db->num_rows($resql);
 348              if ($num)
 349              {
 350                  $obj = $this->db->fetch_object($resql);
 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($resql);
 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_facturefourn';
 383          $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.'facture_fourn as f';
 384          $sql.= ' WHERE pf.fk_facturefourn = f.rowid AND fk_paiementfourn = '.$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('PaiementFourn::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