Files
dolibarr/htdocs/contrat/contrat.class.php
2004-02-01 00:55:46 +00:00

196 lines
4.5 KiB
PHP

<?PHP
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
*
* 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$
*
*/
class Contrat
{
var $id;
var $db;
/*
* Initialisation
*
*/
Function Contrat($DB)
{
$this->db = $DB ;
$this->product = new Product($DB);
$this->societe = new Societe($DB);
$this->user_service = new User($DB);
$this->user_cloture = new User($DB);
}
/*
*
*
*
*/
Function mise_en_service($user, $date, $duree)
{
$duree_value = substr($duree,0,strlen($duree)-1);
$duree_unit = substr($duree,-1);
$month = date("m",$date);
$day = date("d",$date);
$year = date("Y",$date);
switch($duree_unit)
{
case "d":
$day = $day + $duree_value;
break;
case "w":
$day = $day + ($duree_value * 7);
break;
case "m":
$month = $month + $duree_value;
break;
case "y":
$year = $year + $duree_value;
break;
}
$fin = mktime(date("H",$date),
date("i",$date),
0,
$month,
$day,
$year);
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET enservice = 1";
$sql .= " , mise_en_service = ".$this->db->idate($date).", fk_user_mise_en_service = ".$user->id;
$sql .= " , fin_validite = ". $this->db->idate($fin);
$sql .= " WHERE rowid = ".$this->id . " AND enservice = 0";
$result = $this->db->query($sql) ;
if (!$result)
{
print $this->db->error() . "<br>" . $sql;
}
}
/*
*
*
*/
Function cloture($user)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET enservice = 2";
$sql .= " , date_cloture = now(), fk_user_cloture = ".$user->id;
$sql .= " WHERE rowid = ".$this->id . " AND enservice = 1";
$result = $this->db->query($sql) ;
}
/*
*
*
*/
Function fetch ($id)
{
$sql = "SELECT rowid, enservice, fk_soc, fk_product, ".$this->db->pdate("mise_en_service")." as datemise";
$sql .= ", fk_user_mise_en_service, ".$this->db->pdate("date_cloture")." as datecloture";
$sql .= ", ".$this->db->pdate("fin_validite")." as datefin";
$sql .= ", fk_user_cloture, fk_facture";
$sql .= " FROM ".MAIN_DB_PREFIX."contrat WHERE rowid = $id";
$result = $this->db->query($sql) ;
if ( $result )
{
$result = $this->db->fetch_array();
$this->id = $result["rowid"];
$this->enservice = $result["enservice"];
$this->factureid = $result["fk_facture"];
$this->mise_en_service = $result["datemise"];
$this->date_cloture = $result["datecloture"];
$this->date_fin_validite = $result["datefin"];
$this->user_service->id = $result["fk_user_mise_en_service"];
$this->user_cloture->id = $result["fk_user_cloture"];
$this->product->fetch($result["fk_product"]);
$this->societe->fetch($result["fk_soc"]);
$this->db->free();
}
else
{
print $this->db->error();
}
return $result;
}
/*
*
*
*/
Function create_from_facture($factureid, $user, $socid)
{
$sql = "SELECT p.rowid FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."facturedet as f";
$sql .= " WHERE p.rowid = f.fk_product AND p.fk_product_type = 1 AND f.fk_facture = ".$factureid;
if ($this->db->query($sql))
{
$num = $this->db->num_rows();
if ($num > 0)
{
$i = 0;
while ($i < $num)
{
$objp = $this->db->fetch_object($i);
$contrats[$i] = $objp->rowid;
$i++;
}
$this->db->free();
while (list($key, $value) = each ($contrats))
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (fk_product, fk_facture, fk_soc, fk_user_author)";
$sql .= " VALUES ($value, $factureid, $socid, $user->id)";
if (! $this->db->query($sql))
{
print $this->db->error();
}
}
}
else
{
$this->db->free();
}
}
else
{
print $this->db->error();
}
return $result;
}
/*
*
*
*/
}
?>