PageRenderTime 146ms CodeModel.GetById 60ms app.highlight 40ms RepoModel.GetById 40ms app.codeStats 1ms

/htdocs/compta/deplacement/class/deplacement.class.php

https://github.com/asterix14/dolibarr
PHP | 432 lines | 289 code | 48 blank | 95 comment | 47 complexity | 8bcedb4e86af6661e8a64f2963d1eed0 MD5 | raw file
  1<?php
  2/* Copyright (C) 2003      Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3 * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
  4 * Copyright (C) 2009      Regis Houssin        <regis@dolibarr.fr>
  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, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20/**
 21 *      \file       htdocs/compta/deplacement/class/deplacement.class.php
 22 *      \ingroup    deplacement
 23 *      \brief      File of class to manage trips
 24 */
 25
 26require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
 27
 28/**
 29 *      \class      Deplacement
 30 *      \brief      Class to manage trips and working credit notes
 31 */
 32class Deplacement extends CommonObject
 33{
 34	public $element='deplacement';
 35	public $table_element='deplacement';
 36	public $table_element_line = '';
 37	public $fk_element = '';
 38	protected $ismultientitymanaged = 0;	// 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
 39
 40	var $id;
 41
 42	var $datec;         // Creation date
 43	var $dated;
 44	var $fk_user_author;
 45	var $fk_user;
 46	var $km;
 47	var $note;			// TODO obsolete
 48	var $note_private;
 49	var $note_public;
 50	var $socid;
 51	var $statut;		// 0=draft, 1=validated
 52	var $fk_project;
 53
 54   /**
 55	* Constructor
 56	*
 57	* @param	DoliDB		$db		Database handler
 58	*/
 59	function __construct($db)
 60	{
 61		$this->db = $db;
 62
 63		return 1;
 64	}
 65
 66	/**
 67	 * Create object in database
 68	 * TODO Add ref number
 69	 *
 70	 * @param	User	$user	User that creates
 71	 * @return 	int				<0 if KO, >0 if OK
 72	 */
 73	function create($user)
 74	{
 75		global $conf;
 76
 77		// Check parameters
 78		if (empty($this->type) || $this->type < 0)
 79		{
 80			$this->error='ErrorBadParameter';
 81			return -1;
 82		}
 83		if (empty($this->fk_user) || $this->fk_user < 0)
 84		{
 85			$this->error='ErrorBadParameter';
 86			return -1;
 87		}
 88
 89        $now=dol_now();
 90
 91        $this->db->begin();
 92
 93		$sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
 94		$sql.= "datec";
 95		//$sql.= ", dated";
 96		$sql.= ", entity";
 97		$sql.= ", fk_user_author";
 98		$sql.= ", fk_user";
 99		$sql.= ", type";
100		$sql.= ", note";
101		$sql.= ", note_public";
102		$sql.= ", fk_projet";
103		$sql.= ", fk_soc";
104		$sql.= ") VALUES (";
105		$sql.= " '".$this->db->idate($now)."'";
106		$sql.= ", ".$conf->entity;
107		$sql.= ", ".$user->id;
108		$sql.= ", ".$this->fk_user;
109		$sql.= ", '".$this->type."'";
110		$sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
111		$sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
112		$sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
113		$sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
114		$sql.= ")";
115
116		dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
117		$result = $this->db->query($sql);
118		if ($result)
119		{
120			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
121			$result=$this->update($user);
122			if ($result > 0)
123			{
124				$this->db->commit();
125				return $this->id;
126			}
127			else
128			{
129				$this->error=$this->db->error();
130				$this->db->rollback();
131				return $result;
132			}
133		}
134		else
135		{
136			$this->error=$this->db->error()." sql=".$sql;
137			$this->db->rollback();
138			return -1;
139		}
140
141	}
142
143	/**
144	 *	Update record
145	 *
146	 *	@param	User	$user		User making update
147	 *	@return	int					<0 if KO, >0 if OK
148	 */
149	function update($user)
150	{
151		global $langs;
152
153		// Clean parameters
154		$this->km=price2num($this->km);
155
156		// Check parameters
157		if (! is_numeric($this->km)) $this->km = 0;
158        if (empty($this->date))
159        {
160            $this->error='ErrorBadParameter';
161            return -1;
162        }
163        if (empty($this->type) || $this->type < 0)
164		{
165			$this->error='ErrorBadParameter';
166			return -1;
167		}
168		if (empty($this->fk_user) || $this->fk_user < 0)
169		{
170			$this->error='ErrorBadParameter';
171			return -1;
172		}
173
174		$this->db->begin();
175
176		$sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
177		$sql .= " SET km = ".$this->km;		// This is a distance or amount
178		$sql .= " , dated = '".$this->db->idate($this->date)."'";
179		$sql .= " , type = '".$this->type."'";
180		$sql .= " , fk_user = ".$this->fk_user;
181		$sql .= " , fk_user_modif = ".$user->id;
182		$sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
183		$sql .= " , note = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
184		$sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
185		$sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
186		$sql .= " WHERE rowid = ".$this->id;
187
188		dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
189		$result = $this->db->query($sql);
190		if ($result)
191		{
192			$this->db->commit();
193			return 1;
194		}
195		else
196		{
197			$this->error=$this->db->lasterror();
198			$this->db->rollback();
199			return -1;
200		}
201	}
202
203   /**
204	* Load an object from database
205	*
206	* @param	int		$id		Id of record to load
207	* @return	int				<0 if KO, >0 if OK
208	*/
209	function fetch($id)
210	{
211		$sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note, note_public, fk_projet";
212		$sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
213		$sql.= " WHERE rowid = ".$id;
214
215		dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
216		$result = $this->db->query($sql);
217		if ( $result )
218		{
219			$obj = $this->db->fetch_object($result);
220
221			$this->id			= $obj->rowid;
222			$this->ref			= $obj->rowid;
223			$this->date			= $this->db->jdate($obj->dated);
224			$this->fk_user		= $obj->fk_user;
225			$this->socid		= $obj->fk_soc;
226			$this->km			= $obj->km;
227			$this->type			= $obj->type;
228			$this->fk_statut	= $obj->fk_statut;
229			$this->note_private	= $obj->note;
230			$this->note_public	= $obj->note_public;
231			$this->fk_project	= $obj->fk_projet;
232
233			return 1;
234		}
235		else
236		{
237			$this->error=$this->db->error();
238			return -1;
239		}
240	}
241
242   /**
243	*	Delete record
244	*
245	*	@param	int		$id		Id of record to delete
246	*	@return	int				<0 if KO, >0 if OK
247	*/
248	function delete($id)
249	{
250		$this->db->begin();
251		
252		$sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
253
254		dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
255		$result = $this->db->query($sql);
256		if ($result)
257		{
258			$this->db->commit();
259			return 1;
260		}
261		else
262		{
263			$this->error=$this->db->error();
264			$this->db->rollback();
265			return -1;
266		}
267	}
268
269
270	/**
271	 * Retourne le libelle du statut
272	 *
273	 * @param	int		$mode   	0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
274	 * @return  string   		   	Libelle
275	 */
276	function getLibStatut($mode=0)
277	{
278		return $this->LibStatut($this->statut,$mode);
279	}
280
281	/**
282	 *  Renvoi le libelle d'un statut donne
283	 *
284	 *  @param	int		$statut     Id status
285	 *  @param  int		$mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
286	 *  @return string      		Libelle
287	 */
288	function LibStatut($statut,$mode=0)
289	{
290		global $langs;
291
292		if ($mode == 0)
293		{
294			return $langs->trans($this->statuts[$statut]);
295		}
296		if ($mode == 1)
297		{
298			return $langs->trans($this->statuts_short[$statut]);
299		}
300		if ($mode == 2)
301		{
302			//if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
303			if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
304		}
305		if ($mode == 3)
306		{
307			//if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
308			if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
309		}
310		if ($mode == 4)
311		{
312			//if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
313			if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
314		}
315		if ($mode == 5)
316		{
317			//if ($statut==0) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
318			if ($statut==0 || $statut==1) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
319		}
320	}
321
322	/**
323	 *	Return clicable name (with picto eventually)
324	 *
325	 *	@param		int		$withpicto		0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
326	 *	@return		string					Chaine avec URL
327	 */
328	function getNomUrl($withpicto=0)
329	{
330		global $langs;
331
332		$result='';
333
334		$lien = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/fiche.php?id='.$this->id.'">';
335		$lienfin='</a>';
336
337		$picto='trip';
338
339		$label=$langs->trans("Show").': '.$this->ref;
340
341		if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
342		if ($withpicto && $withpicto != 2) $result.=' ';
343		if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
344		return $result;
345	}
346
347
348	/**
349	 * List of types
350	 *
351	 * @param	int		$active		Active or not
352	 * @return	void
353	 */
354	function listOfTypes($active=1)
355	{
356	   global $conf,$langs;
357
358	   $ret=array();
359
360        $sql = "SELECT id, code, libelle as label";
361        $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
362        $sql.= " WHERE active = ".$active;
363
364        dol_syslog(get_class($this)."::listOfTypes sql=".$sql, LOG_DEBUG);
365        $result = $this->db->query($sql);
366        if ( $result )
367        {
368            $num = $this->db->num_rows($result);
369            $i=0;
370            while ($i < $num)
371            {
372                $obj = $this->db->fetch_object($result);
373                $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
374                $i++;
375            }
376        }
377        else
378        {
379        	dol_print_error($this->db);
380        }
381
382        return $ret;
383	}
384
385	/**
386	 * Information on record
387	 *
388	 * @param	int		$id      Id of record
389	 * @return	void
390	 */
391	function info($id)
392	{
393		$sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
394		$sql.= ' c.tms';
395		$sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
396		$sql.= ' WHERE c.rowid = '.$id;
397
398		dol_syslog(get_class($this).'::info sql='.$sql);
399		$result = $this->db->query($sql);
400
401		if ($result)
402		{
403			if ($this->db->num_rows($result))
404			{
405				$obj = $this->db->fetch_object($result);
406				$this->id = $obj->rowid;
407				if ($obj->fk_user_author)
408				{
409					$cuser = new User($this->db);
410					$cuser->fetch($obj->fk_user_author);
411					$this->user_creation = $cuser;
412				}
413				if ($obj->fk_user_modif)
414				{
415					$muser = new User($this->db);
416					$muser->fetch($obj->fk_user_modif);
417					$this->user_modification = $muser;
418				}
419				$this->date_creation     = $this->db->jdate($obj->datec);
420				$this->date_modification = $this->db->jdate($obj->tms);
421			}
422			$this->db->free($result);
423		}
424		else
425		{
426			dol_print_error($this->db);
427		}
428	}
429
430}
431
432?>