diff --git a/htdocs/compta/deplacement/deplacement.class.php b/htdocs/compta/deplacement/deplacement.class.php index d5997cd91a3..c3a3b8fa07e 100644 --- a/htdocs/compta/deplacement/deplacement.class.php +++ b/htdocs/compta/deplacement/deplacement.class.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004 Laurent Destailleur +/* Copyright (C) 2003 Rodolphe Quiedeville + * Copyright (C) 2004-2006 Laurent Destailleur * * 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 @@ -20,114 +20,145 @@ * $Source$ */ +/** + \file htdocs/compta/deplacement/deplacement.class.php + \ingroup deplacement + \brief Fichier de la classe des deplacements + \version $Revision$ +*/ + class Deplacement { - var $db; - var $id; - var $user; - var $km; - var $note; + var $db; + var $id; + var $user; + var $km; + var $note; - /* - * Initialistation automatique de la classe - */ - function Deplacement($DB) - { - $this->db = $DB; - - return 1; - } - /* - * - */ - function create($user) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (datec, fk_user_author) VALUES (now(), $user->id)"; - - $result = $this->db->query($sql); - if ($result) + /* + * Initialistation automatique de la classe + */ + function Deplacement($DB) { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement"); - if ( $this->update($user) ) - { - return $this->id; - } + $this->db = $DB; + + return 1; } - } - /* - * - */ - function update($user) - { - if (strlen($this->km)==0) - $this->km = 0; - $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement "; - $sql .= " SET km = $this->km"; - $sql .= " , dated = '".$this->db->idate($this->date)."'"; - $sql .= " , fk_user = $this->userid"; - $sql .= " , fk_soc = $this->socid"; - $sql .= " WHERE rowid = ".$this->id; - - $result = $this->db->query($sql); - if ($result) + /* + * + */ + function create($user) { - return 1; + $this->db->begin(); + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (datec, fk_user_author) VALUES (now(), $user->id)"; + + $result = $this->db->query($sql); + if ($result) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement"); + $result=$this->update($user); + if ($result > 0) + { + $this->db->commit(); + return $this->id; + } + else + { + $this->db->rollback(); + return $result; + } + } + else + { + $this->error=$this->db->error()." sql=".$sql; + $this->db->rollback(); + return -1; + } + } - else + + /* + * + */ + function update($user) { - print $this->db->error(); - print "
".$sql; - return 0; + global $langs; + + if (! is_numeric($this->km)) $this->km = 0; + if (! $this->socid) + { + $this->error=$langs->trans("ErrorSocidNotDefined"); + return -1; + } + + $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement "; + $sql .= " SET km = ".$this->km; + $sql .= " , dated = '".$this->db->idate($this->date)."'"; + $sql .= " , fk_user = ".$this->userid; + $sql .= " , fk_soc = ".$this->socid; + $sql .= " WHERE rowid = ".$this->id; + + $result = $this->db->query($sql); + if ($result) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } } - } - /* - * - */ - function fetch ($id) - { - $sql = "SELECT fk_user, km, fk_soc,".$this->db->pdate("dated")." as dated"; - $sql .= " FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = $id"; - $result = $this->db->query($sql) ; - - if ( $result ) + /* + * + */ + function fetch ($id) { - $result = $this->db->fetch_array(); + $sql = "SELECT fk_user, km, fk_soc,".$this->db->pdate("dated")." as dated"; + $sql .= " FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = $id"; - $this->id = $id; + $result = $this->db->query($sql) ; - $this->date = $result["dated"]; - $this->userid = $result["fk_user"]; - $this->socid = $result["fk_soc"]; - $this->km = $result["km"]; - return 1; + if ( $result ) + { + $result = $this->db->fetch_array(); + + $this->id = $id; + + $this->date = $result["dated"]; + $this->userid = $result["fk_user"]; + $this->socid = $result["fk_soc"]; + $this->km = $result["km"]; + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } } - else { - print $this->db->error(); - print "
".$sql; - } - } - /* - * - */ - function delete($id) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = $id"; - $result = $this->db->query($sql); - if ($result) + /* + * + */ + function delete($id) { - return 1; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id; + + $result = $this->db->query($sql); + if ($result) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } } - else - { - return 0; - } - } - /* - * - */ } diff --git a/htdocs/compta/deplacement/fiche.php b/htdocs/compta/deplacement/fiche.php index 86a43b69b9a..2867e2c66d7 100644 --- a/htdocs/compta/deplacement/fiche.php +++ b/htdocs/compta/deplacement/fiche.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004-2005 Laurent Destailleur + * Copyright (C) 2004-2006 Laurent Destailleur * * 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 @@ -40,59 +40,60 @@ $mesg = ''; */ if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == "yes") { - $deplacement = new Deplacement($db); - $deplacement->delete($_GET["id"]); - Header("Location: index.php"); + $deplacement = new Deplacement($db); + $deplacement->delete($_GET["id"]); + Header("Location: index.php"); + exit; } if ($_POST["action"] == 'add' && $_POST["cancel"] <> $langs->trans("Cancel")) { - $deplacement = new Deplacement($db); + $deplacement = new Deplacement($db); - $deplacement->date = mktime(12, 1 , 1, - $_POST["remonth"], - $_POST["reday"], - $_POST["reyear"]); - - $deplacement->km = $_POST["km"]; - $deplacement->socid = $_POST["socid"]; - $deplacement->userid = $user->id; //$_POST["km"]; - $id = $deplacement->create($user); + $deplacement->date = mktime(12, 1 , 1, + $_POST["remonth"], + $_POST["reday"], + $_POST["reyear"]); - if ($id) - { - Header ( "Location: fiche.php?id=".$id); - exit; - } - else - { - dolibarr_print_error($db); - } + $deplacement->km = $_POST["km"]; + $deplacement->socid = $_POST["socid"]; + $deplacement->userid = $user->id; //$_POST["km"]; + $id = $deplacement->create($user); + + if ($id > 0) + { + Header ( "Location: fiche.php?id=".$id); + exit; + } + else + { + dolibarr_print_error($db,$deplacement->error); + } } if ($_POST["action"] == 'update' && $_POST["cancel"] <> $langs->trans("Cancel")) { - $deplacement = new Deplacement($db); - $result = $deplacement->fetch($_POST["id"]); - - $deplacement->date = mktime(12, 1 , 1, - $_POST["remonth"], - $_POST["reday"], - $_POST["reyear"]); - - $deplacement->km = $_POST["km"]; - - $result = $deplacement->update($user); - - if ($result > 0) - { - Header ( "Location: fiche.php?id=".$_POST["id"]); - exit; - } - else - { - print $mesg=$langs->trans("ErrorUnknown"); - } + $deplacement = new Deplacement($db); + $result = $deplacement->fetch($_POST["id"]); + + $deplacement->date = mktime(12, 1 , 1, + $_POST["remonth"], + $_POST["reday"], + $_POST["reyear"]); + + $deplacement->km = $_POST["km"]; + + $result = $deplacement->update($user); + + if ($result > 0) + { + Header ( "Location: fiche.php?id=".$_POST["id"]); + exit; + } + else + { + print $mesg=$langs->trans("ErrorUnknown"); + } } @@ -110,9 +111,9 @@ if ($_GET["action"] == 'create') print ''; print_fiche_titre($langs->trans("NewTrip")); - + print ''; - print ''; + print ''; print ""; print ''; print ''; - print ''; + print ''; print '
'.$langs->trans("Person").''.$user->fullname.'
'.$langs->trans("Person").''.$user->fullname.'
'.$langs->trans("CompanyVisited").''; @@ -125,10 +126,10 @@ if ($_GET["action"] == 'create') print '
'.$langs->trans("Kilometers").'
  '; - print '
 '; + print '
'; - print ''; + print ''; } else { @@ -137,35 +138,35 @@ else $deplacement = new Deplacement($db); $result = $deplacement->fetch($id); if ($result) - { - + { + if ($mesg) print "$mesg
"; if ($_GET["action"] == 'edit') { $h=0; - + $head[$h][0] = DOL_URL_ROOT."/compta/deplacement/fiche.php?id=$deplacement->id"; $head[$h][1] = $langs->trans("TripCard"); - + dolibarr_fiche_head($head, $hselected, $langs->trans("Ref").' '.$deplacement->id); print "
\n"; print ''; print ''; - + print ''; $soc = new Societe($db); $soc->fetch($deplacement->socid); - print ''; + print ''; print ""; print ''; - + print ''; @@ -175,27 +176,27 @@ else print ''; print '
'.$langs->trans("Personn").''.$user->fullname.'
'.$langs->trans("Personn").''.$user->fullname.'
'.$langs->trans("CompanyVisited").''; print $html->select_societes($soc->id); print '
'.$langs->trans("Date").''; print $html->select_date($deplacement->date,'','','','','update'); print '
'; print '
'; - + print ''; - } + } else { $h=0; - + $head[$h][0] = DOL_URL_ROOT."/compta/deplacement/fiche.php?id=$deplacement->id"; $head[$h][1] = $langs->trans("TripCard"); - + dolibarr_fiche_head($head, $hselected, $langs->trans("Ref").' '.$deplacement->id); - + /* * Confirmation de la suppression du déplacement */ if ($_GET["action"] == 'delete') { - + $html = new Form($db); $html->form_confirm("fiche.php?id=".$id,$langs->trans("DeleteTrip"),$langs->trans("ConfirmDeleteTrip"),"confirm_delete"); - + print '
'; } @@ -208,12 +209,12 @@ else print ''.$langs->trans("Date").''; print dolibarr_print_date($deplacement->date); print ''; - print ''.$langs->trans("Kilometers").''.$deplacement->km.''; + print ''.$langs->trans("Kilometers").''.$deplacement->km.''; print ""; - + print ''; } - + } else {