dolibarr/htdocs/don.class.php

500 lines
11 KiB
PHP
Raw Normal View History

2004-10-20 23:06:45 +02:00
<?php
2002-12-17 20:40:02 +01:00
/* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 Laurent Destailleur <eldy@users.sourceforge.net>
2002-12-17 20:40:02 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*
*/
2004-10-20 23:06:45 +02:00
/*! \file htdocs/don.class.php
\ingroup don
\brief Fichier de la classe des dons
\version $Revision$
*/
/*! \class Don
\brief Classe permettant la gestion des dons
*/
2002-12-17 20:40:02 +01:00
class Don
{
var $id;
var $db;
2002-12-22 16:11:07 +01:00
var $date;
2002-12-17 20:40:02 +01:00
var $amount;
2002-12-22 16:11:07 +01:00
var $prenom;
2002-12-19 19:55:38 +01:00
var $nom;
2002-12-22 16:11:07 +01:00
var $societe;
2002-12-19 19:55:38 +01:00
var $adresse;
var $cp;
var $ville;
var $pays;
2002-12-22 16:11:07 +01:00
var $email;
2002-12-19 19:55:38 +01:00
var $public;
var $projetid;
var $modepaiement;
2002-12-21 18:35:17 +01:00
var $modepaiementid;
var $commentaire;
2002-12-19 19:55:38 +01:00
var $statut;
var $projet;
2002-12-20 19:07:49 +01:00
var $errorstr;
2002-12-17 20:40:02 +01:00
/*
2002-12-19 19:55:38 +01:00
* Statut du don
* 0 : promesse non valid<EFBFBD>e
* 1 : promesse valid<EFBFBD>e
* 2 : don valid<EFBFBD>
* 3 : don pay<EFBFBD>
2002-12-17 20:40:02 +01:00
*
*/
/*
* \brief Constructeur
* \param DB handler d'acc<EFBFBD>s base
* \param soc_idp id soci<EFBFBD>t<EFBFBD>
2002-12-17 20:40:02 +01:00
*/
function Don($DB, $soc_idp="")
2002-12-17 20:40:02 +01:00
{
$this->db = $DB ;
2002-12-21 18:35:17 +01:00
$this->modepaiementid = 0;
2002-12-20 19:07:49 +01:00
}
2002-12-20 19:07:49 +01:00
/*
*
*/
function print_error_list()
2002-12-20 19:07:49 +01:00
{
$num = sizeof($this->errorstr);
for ($i = 0 ; $i < $num ; $i++)
{
print "<li>" . $this->errorstr[$i];
}
}
2002-12-22 21:04:24 +01:00
/*
*
*
*/
function check($minimum=0)
2002-12-20 19:07:49 +01:00
{
$err = 0;
2002-12-22 16:11:07 +01:00
if (strlen(trim($this->societe)) == 0)
2002-12-20 19:07:49 +01:00
{
2002-12-22 16:11:07 +01:00
if ((strlen(trim($this->nom)) + strlen(trim($this->prenom))) == 0)
{
$error_string[$err] = "Vous devez saisir vos nom et pr<70>nom ou le nom de votre soci<63>t<EFBFBD>.";
$err++;
}
2002-12-20 19:07:49 +01:00
}
if (strlen(trim($this->adresse)) == 0)
{
$error_string[$err] = "L'adresse saisie est invalide";
$err++;
}
if (strlen(trim($this->cp)) == 0)
{
$error_string[$err] = "Le code postal saisi est invalide";
$err++;
}
if (strlen(trim($this->ville)) == 0)
{
$error_string[$err] = "La ville saisie est invalide";
$err++;
}
2002-12-22 16:11:07 +01:00
if (strlen(trim($this->email)) == 0)
2002-12-20 19:07:49 +01:00
{
2002-12-22 16:11:07 +01:00
$error_string[$err] = "L'email saisi est invalide";
2002-12-20 19:07:49 +01:00
$err++;
}
2002-12-22 16:11:07 +01:00
$this->amount = trim($this->amount);
$map = range(0,9);
for ($i = 0; $i < strlen($this->amount) ; $i++)
{
if (!isset($map[substr($this->amount, $i, 1)] ))
{
$error_string[$err] = "Le montant du don contient un/des caract<63>re(s) invalide(s)";
$err++;
2002-12-22 19:46:40 +01:00
$amount_invalid = 1;
2002-12-22 16:11:07 +01:00
break;
}
}
2002-12-22 19:46:40 +01:00
if (! $amount_invalid)
2002-12-22 16:11:07 +01:00
{
2002-12-22 19:46:40 +01:00
if ($this->amount == 0)
2002-12-22 19:37:53 +01:00
{
2002-12-22 19:46:40 +01:00
$error_string[$err] = "Le montant du don est null";
2002-12-22 19:37:53 +01:00
$err++;
}
2002-12-22 19:46:40 +01:00
else
{
if ($this->amount < $minimum && $minimum > 0)
{
$error_string[$err] = "Le montant minimum du don est de $minimum";
$err++;
}
}
2002-12-22 19:37:53 +01:00
}
2002-12-22 19:46:40 +01:00
2002-12-20 19:07:49 +01:00
if ($err)
{
$this->errorstr = $error_string;
return 0;
}
else
{
return 1;
}
2002-12-17 20:40:02 +01:00
}
2002-12-17 20:40:02 +01:00
/*
* \brief Cr<EFBFBD>ation du don en base
* \param userid utilisateur qui cr<EFBFBD>e le don
2002-12-17 20:40:02 +01:00
*/
function create($userid)
2002-12-17 20:40:02 +01:00
{
2002-12-20 19:07:49 +01:00
$this->date = $this->db->idate($this->date);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."don (datec, amount, fk_paiement,prenom, nom, societe,adresse, cp, ville, pays, public,";
if ($this->projetid) {
$sql .= " fk_don_projet,";
}
$sql .= " note, fk_user_author, datedon, email)";
$sql .= " VALUES (now(), $this->amount, $this->modepaiementid,'$this->prenom','$this->nom','$this->societe','$this->adresse', '$this->cp','$this->ville','$this->pays',$this->public, ";
if ($this->projetid) {
$sql .= " $this->projetid,";
}
$sql .= " '$this->commentaire', $userid, '$this->date','$this->email')";
2002-12-17 20:40:02 +01:00
$result = $this->db->query($sql);
if ($result)
{
2002-12-30 16:13:28 +01:00
return $this->db->last_insert_id();
2002-12-17 20:40:02 +01:00
}
else
{
dolibarr_print_error($this->db);
2002-12-19 19:55:38 +01:00
return 0;
2002-12-17 20:40:02 +01:00
}
}
2002-12-27 22:54:03 +01:00
/*
* \brief Mise <EFBFBD> jour du don
* \param userid utilisateur qui cr<EFBFBD>e le don
2002-12-27 22:54:03 +01:00
*
*/
function update($userid)
2002-12-27 22:54:03 +01:00
{
$this->date = $this->db->idate($this->date);
$sql = "UPDATE ".MAIN_DB_PREFIX."don SET ";
2002-12-27 22:54:03 +01:00
$sql .= "amount = " . $this->amount;
$sql .= ",fk_paiement = ".$this->modepaiementid;
$sql .= ",prenom = '".$this->prenom ."'";
$sql .= ",nom='".$this->nom."'";
$sql .= ",societe='".$this->societe."'";
$sql .= ",adresse='".$this->adresse."'";
$sql .= ",cp='".$this->cp."'";
$sql .= ",ville='".$this->ville."'";
$sql .= ",pays='".$this->pays."'";
$sql .= ",public=".$this->public;
if ($this->projetid) { $sql .= ",fk_don_projet=".$this->projetid; }
2002-12-27 22:54:03 +01:00
$sql .= ",note='".$this->commentaire."'";
$sql .= ",datedon='".$this->date."'";
$sql .= ",email='".$this->email."'";
2002-12-28 01:13:44 +01:00
$sql .= ",fk_statut=".$this->statut;
2002-12-27 22:54:03 +01:00
$sql .= " WHERE rowid = $this->id";
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
dolibarr_print_error($this->db);
2002-12-27 22:54:03 +01:00
return 0;
}
}
2002-12-19 19:55:38 +01:00
/*
* \brief Suppression du don de la base
* \param rowid id du don <EFBFBD> supprimer
2002-12-19 19:55:38 +01:00
*/
function delete($rowid)
2002-12-20 19:07:49 +01:00
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."don WHERE rowid = $rowid AND fk_statut = 0;";
2002-12-19 19:55:38 +01:00
if ( $this->db->query( $sql) )
{
if ( $this->db->affected_rows() )
{
return 1;
}
else
{
return 0;
}
}
else
{
dolibarr_print_error($this->db);
return 0;
2002-12-19 19:55:38 +01:00
}
}
2002-12-19 19:55:38 +01:00
/*
* \brief Charge l'objet don en m<EFBFBD>moire depuis la base de donn<EFBFBD>e
* \param rowid id du don <EFBFBD> charger
2002-12-19 19:55:38 +01:00
*
*/
function fetch($rowid)
2002-12-19 19:55:38 +01:00
{
2002-12-27 22:54:03 +01:00
$sql = "SELECT d.rowid, ".$this->db->pdate("d.datedon")." as datedon, d.prenom, d.nom, d.societe, d.amount, p.libelle as projet, d.fk_statut, d.adresse, d.cp, d.ville, d.pays, d.public, d.amount, d.fk_paiement, d.note, cp.libelle, d.email, d.fk_don_projet";
$sql .= " FROM ".MAIN_DB_PREFIX."don as d, ".MAIN_DB_PREFIX."c_paiement as cp LEFT JOIN ".MAIN_DB_PREFIX."don_projet as p";
$sql .= " ON p.rowid = d.fk_don_projet WHERE cp.id = d.fk_paiement AND d.rowid = $rowid";
2002-12-19 19:55:38 +01:00
if ( $this->db->query( $sql) )
{
if ($this->db->num_rows())
{
$obj = $this->db->fetch_object();
2002-12-19 19:55:38 +01:00
2002-12-27 21:21:44 +01:00
$this->id = $obj->rowid;
2002-12-21 18:35:17 +01:00
$this->date = $obj->datedon;
2002-12-22 16:11:07 +01:00
$this->prenom = stripslashes($obj->prenom);
2002-12-21 18:35:17 +01:00
$this->nom = stripslashes($obj->nom);
2002-12-22 16:11:07 +01:00
$this->societe = stripslashes($obj->societe);
2002-12-21 18:35:17 +01:00
$this->statut = $obj->fk_statut;
$this->adresse = stripslashes($obj->adresse);
$this->cp = stripslashes($obj->cp);
$this->ville = stripslashes($obj->ville);
2002-12-22 16:11:07 +01:00
$this->email = stripslashes($obj->email);
$this->pays = stripslashes($obj->pays);
2002-12-21 18:35:17 +01:00
$this->projet = $obj->projet;
2002-12-27 22:54:03 +01:00
$this->projetid = $obj->fk_don_projet;
2002-12-21 18:35:17 +01:00
$this->public = $obj->public;
$this->modepaiementid = $obj->fk_paiement;
$this->modepaiement = $obj->libelle;
$this->amount = $obj->amount;
$this->commentaire = stripslashes($obj->note);
2002-12-19 19:55:38 +01:00
}
}
else
{
dolibarr_print_error($this->db);
2002-12-19 19:55:38 +01:00
}
}
2002-12-19 19:55:38 +01:00
/*
* \brief Valide une promesse de don
* \param rowid id du don <EFBFBD> modifier
* \param userid utilisateur qui valide la promesse
2002-12-19 19:55:38 +01:00
*
*/
function valid_promesse($rowid, $userid)
2002-12-19 19:55:38 +01:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = $userid WHERE rowid = $rowid AND fk_statut = 0;";
2002-12-19 19:55:38 +01:00
if ( $this->db->query( $sql) )
{
if ( $this->db->affected_rows() )
{
return 1;
}
else
{
return 0;
}
}
else
{
dolibarr_print_error($this->db);
return 0;
2002-12-19 19:55:38 +01:00
}
}
2002-12-19 19:55:38 +01:00
/*
* \brief Classe le don comme pay<EFBFBD>, le don a <EFBFBD>t<EFBFBD> recu
* \param rowid id du don <EFBFBD> modifier
* \param modepaiementd mode de paiement
2002-12-19 19:55:38 +01:00
*
*/
function set_paye($rowid, $modepaiement='')
2002-12-19 19:55:38 +01:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2";
2002-12-19 19:55:38 +01:00
2002-12-21 18:35:17 +01:00
if ($modepaiement)
{
$sql .= ", fk_paiement=$modepaiement";
}
$sql .= " WHERE rowid = $rowid AND fk_statut = 1;";
2002-12-19 19:55:38 +01:00
if ( $this->db->query( $sql) )
{
if ( $this->db->affected_rows() )
{
return 1;
}
else
{
return 0;
}
}
else
{
dolibarr_print_error($this->db);
return 0;
2002-12-19 19:55:38 +01:00
}
}
2002-12-26 11:21:15 +01:00
/*
* \brief D<EFBFBD>fini le commentaire
* \param rowid id du don <EFBFBD> modifier
* \param commentaire commentaire <EFBFBD> d<EFBFBD>finir ('' par d<EFBFBD>faut)
2002-12-26 11:21:15 +01:00
*
*/
function set_commentaire($rowid, $commentaire='')
2002-12-26 11:21:15 +01:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."don SET note = '$commentaire'";
2002-12-26 11:21:15 +01:00
$sql .= " WHERE rowid = $rowid ;";
if ( $this->db->query( $sql) )
{
if ( $this->db->affected_rows() )
{
return 1;
}
else
{
return 0;
}
}
else
{
dolibarr_print_error($this->db);
return 0;
2002-12-26 11:21:15 +01:00
}
}
2002-12-19 19:55:38 +01:00
/*
* \brief Classe le don comme encaiss<EFBFBD>
* \param rowid id du don <EFBFBD> modifier
2002-12-19 19:55:38 +01:00
*
*/
function set_encaisse($rowid)
2002-12-19 19:55:38 +01:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 3 WHERE rowid = $rowid AND fk_statut = 2;";
2002-12-19 19:55:38 +01:00
if ( $this->db->query( $sql) )
{
if ( $this->db->affected_rows() )
{
return 1;
}
else
{
return 0;
}
}
else
{
dolibarr_print_error($this->db);
return 0;
2002-12-19 19:55:38 +01:00
}
}
2002-12-21 19:39:38 +01:00
/*
* \brief Somme des dons encaiss<EFBFBD>s
2002-12-21 19:39:38 +01:00
*/
function sum_actual()
2002-12-21 19:39:38 +01:00
{
$sql = "SELECT sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."don";
2002-12-21 19:39:38 +01:00
$sql .= " WHERE fk_statut = 3";
if ( $this->db->query( $sql) )
{
$row = $this->db->fetch_row(0);
return $row[0];
}
}
/*
* \brief Paiement recu en attente d'encaissement
2002-12-21 19:39:38 +01:00
*
*/
function sum_pending()
2002-12-21 19:39:38 +01:00
{
$sql = "SELECT sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."don";
2002-12-21 19:39:38 +01:00
$sql .= " WHERE fk_statut = 2";
if ( $this->db->query( $sql) )
{
$row = $this->db->fetch_row(0);
return $row[0];
}
}
2002-12-21 19:39:38 +01:00
/*
* \brief Somme des promesses de dons valid<EFBFBD>es
2002-12-21 19:39:38 +01:00
*
*/
function sum_intent()
2002-12-21 19:39:38 +01:00
{
$sql = "SELECT sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."don";
2002-12-21 19:39:38 +01:00
$sql .= " WHERE fk_statut = 1";
if ( $this->db->query( $sql) )
{
$row = $this->db->fetch_row(0);
return $row[0];
}
}
2002-12-17 20:40:02 +01:00
}
?>