2004-10-19 22:43:23 +02:00
< ? php
2003-09-19 13:27:17 +02:00
/* Copyright ( C ) 2002 - 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2014-09-18 08:58:47 +02:00
* Copyright ( C ) 2004 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2013-08-22 16:49:23 +02:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
* Copyright ( C ) 2011 - 2013 Juanjo Menent < jmenent @ 2 byte . es >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
2015-12-04 11:28:28 +01:00
* Copyright ( C ) 2015 Charlie Benke < charlie @ patas - monkey . com >
2018-06-15 09:47:28 +02:00
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2013-08-22 16:49:23 +02: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 3 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 , see < http :// www . gnu . org / licenses />.
*/
2003-09-19 13:27:17 +02:00
2011-08-27 17:14:31 +02:00
/**
* \file htdocs / fichinter / class / fichinter . class . php
2013-08-22 16:49:23 +02:00
* \ingroup ficheinter
* \brief Fichier de la classe des gestion des fiches interventions
*/
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2014-08-01 08:59:08 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2005-06-26 00:54:02 +02:00
2005-01-09 19:30:34 +01:00
2008-06-21 01:01:02 +02:00
/**
2014-09-18 08:58:47 +02:00
* Class to manage interventions
2008-06-21 01:01:02 +02:00
*/
2006-06-18 16:35:35 +02:00
class Fichinter extends CommonObject
2003-09-19 13:27:17 +02:00
{
2011-09-20 19:19:46 +02:00
public $element = 'fichinter' ;
public $table_element = 'fichinter' ;
public $fk_element = 'fk_fichinter' ;
public $table_element_line = 'fichinterdet' ;
2017-10-07 13:09:31 +02:00
public $picto = 'intervention' ;
2017-07-25 21:22:22 +02:00
2014-12-25 21:47:39 +01:00
/**
* { @ inheritdoc }
*/
protected $table_ref_field = 'ref' ;
2008-02-24 17:46:42 +01:00
var $socid ; // Id client
2006-06-23 21:40:28 +02:00
2008-06-21 01:01:02 +02:00
var $author ;
2008-11-02 23:11:34 +01:00
var $datec ;
var $datev ;
2015-12-04 11:28:28 +01:00
var $dateo ;
var $datee ;
2015-12-15 11:52:11 +01:00
var $datet ;
2008-11-02 23:11:34 +01:00
var $datem ;
2015-06-11 23:18:29 +02:00
var $duration ;
2018-04-21 15:46:55 +02:00
var $statut = 0 ; // 0=draft, 1=validated, 2=invoiced, 3=Terminate
2008-06-21 01:01:02 +02:00
var $description ;
2018-04-21 15:46:55 +02:00
var $fk_contrat = 0 ;
var $fk_project = 0 ;
2012-03-16 16:01:45 +01:00
var $extraparams = array ();
2008-06-21 01:01:02 +02:00
2010-12-15 08:12:28 +01:00
var $lines = array ();
2008-06-21 01:01:02 +02:00
2017-07-25 21:22:22 +02:00
/**
* Draft status
*/
const STATUS_DRAFT = 0 ;
/**
* Validated status
*/
const STATUS_VALIDATED = 1 ;
/**
* Billed
*/
const STATUS_BILLED = 2 ;
/**
* Closed
*/
const STATUS_CLOSED = 3 ;
2008-06-21 01:01:02 +02:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
2011-09-10 19:52:21 +02:00
*
2012-02-24 17:25:53 +01:00
* @ param DoliDB $db Database handler
2017-10-07 13:09:31 +02:00
*/
2012-02-24 17:25:53 +01:00
function __construct ( $db )
2008-06-21 01:01:02 +02:00
{
2012-07-30 17:17:33 +02:00
$this -> db = $db ;
2018-04-21 15:46:55 +02:00
2008-06-21 01:01:02 +02:00
$this -> products = array ();
}
2016-04-23 13:16:19 +02:00
/**
* Load indicators into this -> nb for board
*
* @ return int < 0 if KO , > 0 if OK
*/
function load_state_board ()
{
global $user ;
$this -> nb = array ();
$clause = " WHERE " ;
$sql = " SELECT count(fi.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as fi " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON fi.fk_soc = s.rowid " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $user -> societe_id )
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON s.rowid = sc.fk_soc " ;
$sql .= " WHERE sc.fk_user = " . $user -> id ;
$clause = " AND " ;
}
2018-03-22 16:18:11 +01:00
$sql .= " " . $clause . " fi.entity IN ( " . getEntity ( 'intervention' ) . " ) " ;
2016-04-23 13:16:19 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nb [ " fichinters " ] = $obj -> nb ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2008-06-21 01:01:02 +02:00
2011-07-08 11:18:54 +02:00
/**
2011-10-27 16:38:49 +02:00
* Create an intervention into data base
*
2013-05-18 12:53:11 +02:00
* @ param User $user Objet user that make creation
2017-10-07 13:09:31 +02:00
* @ param int $notrigger Disable all triggers
2011-07-08 11:18:54 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2013-05-18 12:53:11 +02:00
function create ( $user , $notrigger = 0 )
2007-09-22 03:47:43 +02:00
{
2018-01-27 19:31:48 +01:00
global $conf , $langs ;
2009-07-07 18:19:37 +02:00
2011-10-27 16:04:50 +02:00
dol_syslog ( get_class ( $this ) . " ::create ref= " . $this -> ref );
2007-09-22 03:47:43 +02:00
2010-05-04 20:12:43 +02:00
// Check parameters
2014-05-13 10:12:45 +02:00
if ( ! empty ( $this -> ref )) // We check that ref is not already used
{
$result = self :: isExistingObject ( $this -> element , 0 , $this -> ref ); // Check ref is not yet used
if ( $result > 0 )
{
$this -> error = 'ErrorRefAlreadyExists' ;
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_WARNING );
$this -> db -> rollback ();
return - 1 ;
}
2013-05-19 18:28:27 +02:00
}
2015-06-11 23:18:29 +02:00
if ( ! is_numeric ( $this -> duration )) $this -> duration = 0 ;
2014-05-13 10:12:45 +02:00
2008-06-21 01:01:02 +02:00
if ( $this -> socid <= 0 )
2007-09-22 03:47:43 +02:00
{
$this -> error = 'ErrorBadParameterForFunc' ;
2011-10-27 16:04:50 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2007-09-22 03:47:43 +02:00
return - 1 ;
}
2014-05-13 10:12:45 +02:00
2007-09-22 03:47:43 +02:00
$soc = new Societe ( $this -> db );
$result = $soc -> fetch ( $this -> socid );
2008-06-21 01:01:02 +02:00
2010-05-04 20:12:43 +02:00
$now = dol_now ();
$this -> db -> begin ();
2009-05-02 16:26:54 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " fichinter ( " ;
$sql .= " fk_soc " ;
$sql .= " , datec " ;
$sql .= " , ref " ;
$sql .= " , entity " ;
$sql .= " , fk_user_author " ;
2016-01-28 11:59:18 +01:00
$sql .= " , fk_user_modif " ;
2009-05-02 16:26:54 +02:00
$sql .= " , description " ;
$sql .= " , model_pdf " ;
2010-05-04 20:03:45 +02:00
$sql .= " , fk_projet " ;
2013-06-03 01:43:12 +02:00
$sql .= " , fk_contrat " ;
2010-05-04 20:12:43 +02:00
$sql .= " , fk_statut " ;
2011-10-27 16:04:50 +02:00
$sql .= " , note_private " ;
$sql .= " , note_public " ;
2007-10-05 02:54:36 +02:00
$sql .= " ) " ;
2009-05-02 16:26:54 +02:00
$sql .= " VALUES ( " ;
$sql .= $this -> socid ;
2010-05-04 20:03:45 +02:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
2015-09-12 11:23:44 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2009-05-02 16:26:54 +02:00
$sql .= " , " . $conf -> entity ;
2013-05-18 12:53:11 +02:00
$sql .= " , " . $user -> id ;
2016-01-28 11:59:18 +01:00
$sql .= " , " . $user -> id ;
2011-02-24 19:11:12 +01:00
$sql .= " , " . ( $this -> description ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " );
2015-09-12 11:23:44 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> modelpdf ) . " ' " ;
2010-03-11 15:31:16 +01:00
$sql .= " , " . ( $this -> fk_project ? $this -> fk_project : 0 );
2013-06-03 01:43:12 +02:00
$sql .= " , " . ( $this -> fk_contrat ? $this -> fk_contrat : 0 );
2010-05-04 20:12:43 +02:00
$sql .= " , " . $this -> statut ;
2011-10-27 16:04:50 +02:00
$sql .= " , " . ( $this -> note_private ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " );
$sql .= " , " . ( $this -> note_public ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " );
2007-10-05 02:54:36 +02:00
$sql .= " ) " ;
2007-09-22 03:47:43 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2007-09-22 03:47:43 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2012-08-19 09:05:50 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " fichinter " );
2013-05-19 11:18:45 +02:00
2013-03-10 13:58:32 +01:00
if ( $this -> id )
{
$this -> ref = '(PROV' . $this -> id . ')' ;
2017-05-12 16:55:11 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " fichinter SET ref=' " . $this -> db -> escape ( $this -> ref ) . " ' WHERE rowid= " . $this -> id ;
2013-05-20 21:21:03 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2013-03-10 13:58:32 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $error ++ ;
}
2014-08-28 14:48:16 +02:00
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))
2017-10-07 13:09:31 +02:00
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2013-05-18 12:17:43 +02:00
// Add linked object
if ( ! $error && $this -> origin && $this -> origin_id )
{
$ret = $this -> add_object_linked ();
if ( ! $ret ) dol_print_error ( $this -> db );
}
2012-12-05 18:48:02 +01:00
2018-04-10 12:03:01 +02:00
if ( ! $error && ! $notrigger )
2017-10-07 13:09:31 +02:00
{
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_CREATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
2012-07-29 12:54:19 +02:00
2012-12-30 17:13:43 +01:00
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
$this -> error = join ( ',' , $this -> errors );
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
return - 1 ;
}
2007-09-22 03:47:43 +02:00
}
else
{
$this -> error = $this -> db -> error ();
$this -> db -> rollback ();
return - 1 ;
}
2003-09-19 13:27:17 +02:00
2007-09-22 03:47:43 +02:00
}
2005-02-10 23:55:42 +01:00
2011-07-08 11:18:54 +02:00
/**
2011-10-27 16:38:49 +02:00
* Update an intervention
*
2013-05-18 12:53:11 +02:00
* @ param User $user Objet user that make creation
2017-10-07 13:09:31 +02:00
* @ param int $notrigger Disable all triggers
2011-07-02 18:48:31 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2013-05-18 12:53:11 +02:00
function update ( $user , $notrigger = 0 )
2008-06-21 01:01:02 +02:00
{
2018-08-17 15:56:23 +02:00
global $conf ;
2015-06-11 23:18:29 +02:00
if ( ! is_numeric ( $this -> duration )) {
$this -> duration = 0 ;
2013-05-19 18:28:27 +02:00
}
if ( ! dol_strlen ( $this -> fk_project )) {
$this -> fk_project = 0 ;
}
2008-06-21 01:01:02 +02:00
2018-08-12 16:29:26 +02:00
$error = 0 ;
2011-10-27 16:04:50 +02:00
$this -> db -> begin ();
2011-11-04 00:27:30 +01:00
2008-06-21 01:01:02 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter SET " ;
2014-08-22 16:17:45 +02:00
$sql .= " description = ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
2015-06-11 23:18:29 +02:00
$sql .= " , duree = " . $this -> duration ;
2010-03-11 15:31:16 +01:00
$sql .= " , fk_projet = " . $this -> fk_project ;
2011-10-27 16:04:50 +02:00
$sql .= " , note_private = " . ( $this -> note_private ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " );
$sql .= " , note_public = " . ( $this -> note_public ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " );
2014-10-20 11:16:51 +02:00
$sql .= " , fk_user_modif = " . $user -> id ;
2011-07-02 18:48:31 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2005-09-07 23:47:16 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2011-10-27 16:04:50 +02:00
if ( $this -> db -> query ( $sql ))
{
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2013-05-20 21:21:03 +02:00
2018-08-12 16:29:26 +02:00
if ( ! $error && ! $notrigger )
2013-05-18 12:53:11 +02:00
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_MODIFY' , $user );
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
// End call triggers
2013-05-18 12:53:11 +02:00
}
2013-06-16 21:31:21 +02:00
2011-10-27 16:04:50 +02:00
$this -> db -> commit ();
return 1 ;
}
else
2008-06-21 01:01:02 +02:00
{
2007-09-22 03:47:43 +02:00
$this -> error = $this -> db -> error ();
2011-10-27 16:04:50 +02:00
$this -> db -> rollback ();
2006-04-29 03:28:16 +02:00
return - 1 ;
2008-06-21 01:01:02 +02:00
}
}
/**
2011-07-08 11:18:54 +02:00
* Fetch a intervention
2011-10-27 16:38:49 +02:00
*
2012-01-08 00:52:07 +01:00
* @ param int $rowid Id of intervention
* @ param string $ref Ref of intervention
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2009-07-07 18:19:37 +02:00
function fetch ( $rowid , $ref = '' )
2008-06-21 01:01:02 +02:00
{
2012-03-16 16:01:45 +01:00
$sql = " SELECT f.rowid, f.ref, f.description, f.fk_soc, f.fk_statut, " ;
2017-05-09 22:54:33 +02:00
$sql .= " f.datec, f.dateo, f.datee, f.datet, f.fk_user_author, " ;
2012-03-16 16:01:45 +01:00
$sql .= " f.date_valid as datev, " ;
$sql .= " f.tms as datem, " ;
2014-08-29 11:34:07 +02:00
$sql .= " f.duree, f.fk_projet, f.note_public, f.note_private, f.model_pdf, f.extraparams, fk_contrat " ;
2009-07-07 18:19:37 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as f " ;
2018-03-22 16:18:11 +01:00
if ( $ref ) {
$sql .= " WHERE f.entity IN ( " . getEntity ( 'intervention' ) . " ) " ;
$sql .= " AND f.ref=' " . $this -> db -> escape ( $ref ) . " ' " ;
}
2009-07-07 18:19:37 +02:00
else $sql .= " WHERE f.rowid= " . $rowid ;
2005-09-07 23:47:16 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2008-06-21 01:01:02 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2009-07-07 18:19:37 +02:00
$this -> id = $obj -> rowid ;
2008-06-21 01:01:02 +02:00
$this -> ref = $obj -> ref ;
$this -> description = $obj -> description ;
$this -> socid = $obj -> fk_soc ;
$this -> statut = $obj -> fk_statut ;
2015-06-11 23:18:29 +02:00
$this -> duration = $obj -> duree ;
2010-01-13 19:51:19 +01:00
$this -> datec = $this -> db -> jdate ( $obj -> datec );
2018-05-10 16:08:45 +02:00
$this -> dateo = $this -> db -> jdate ( $obj -> dateo );
$this -> datee = $this -> db -> jdate ( $obj -> datee );
2015-12-15 11:52:11 +01:00
$this -> datet = $this -> db -> jdate ( $obj -> datet );
2010-01-13 19:51:19 +01:00
$this -> datev = $this -> db -> jdate ( $obj -> datev );
$this -> datem = $this -> db -> jdate ( $obj -> datem );
2010-03-11 15:31:16 +01:00
$this -> fk_project = $obj -> fk_projet ;
2008-06-21 01:01:02 +02:00
$this -> note_public = $obj -> note_public ;
$this -> note_private = $obj -> note_private ;
$this -> modelpdf = $obj -> model_pdf ;
2014-08-29 11:34:07 +02:00
$this -> fk_contrat = $obj -> fk_contrat ;
2012-03-18 01:25:08 +01:00
2017-05-09 22:24:24 +02:00
$this -> user_creation = $obj -> fk_user_author ;
2017-07-25 21:22:22 +02:00
2012-03-18 19:59:38 +01:00
$this -> extraparams = ( array ) json_decode ( $obj -> extraparams , true );
2008-06-21 01:01:02 +02:00
if ( $this -> statut == 0 ) $this -> brouillon = 1 ;
2011-07-02 18:48:31 +02:00
2018-08-12 16:29:26 +02:00
// Retreive extrafields
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2013-05-18 12:53:11 +02:00
2011-07-01 18:24:44 +02:00
/*
* Lines
2018-02-20 19:38:18 +01:00
*/
2011-07-01 18:24:44 +02:00
$result = $this -> fetch_lines ();
if ( $result < 0 )
{
return - 3 ;
}
2008-06-21 01:01:02 +02:00
$this -> db -> free ( $resql );
return 1 ;
}
}
else
{
2016-12-23 13:04:46 +01:00
$this -> error = $this -> db -> lasterror ();
2008-06-21 01:01:02 +02:00
return - 1 ;
}
}
/**
2011-07-08 11:18:54 +02:00
* Set status to draft
2011-12-19 17:29:09 +01:00
*
* @ param User $user User that set draft
2011-07-08 11:18:54 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2008-11-02 23:11:34 +01:00
function setDraft ( $user )
2008-06-21 01:01:02 +02:00
{
2008-04-10 01:07:13 +02:00
global $langs , $conf ;
2008-06-21 01:01:02 +02:00
2008-11-02 23:11:34 +01:00
if ( $this -> statut != 0 )
2008-06-21 01:01:02 +02:00
{
2008-11-02 23:11:34 +01:00
$this -> db -> begin ();
2009-03-12 23:49:05 +01:00
2008-11-02 23:11:34 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
$sql .= " SET fk_statut = 0 " ;
$sql .= " WHERE rowid = " . $this -> id ;
2009-05-02 16:26:54 +02:00
$sql .= " AND entity = " . $conf -> entity ;
2009-03-12 23:49:05 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " Fichinter::setDraft " , LOG_DEBUG );
2008-11-02 23:11:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-07-12 14:45:29 +02:00
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
2008-11-02 23:11:34 +01:00
$this -> error = $this -> db -> lasterror ();
2008-07-12 14:45:29 +02:00
return - 1 ;
}
2009-03-12 23:49:05 +01:00
}
2008-11-02 23:11:34 +01:00
}
2009-03-12 23:49:05 +01:00
2008-11-02 23:11:34 +01:00
/**
2011-07-08 11:18:54 +02:00
* Validate a intervention
2011-12-19 17:29:09 +01:00
*
* @ param User $user User that validate
2017-10-07 13:09:31 +02:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2014-10-27 03:24:45 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-11-02 23:11:34 +01:00
*/
2014-10-27 03:24:45 +01:00
function setValid ( $user , $notrigger = 0 )
2008-11-02 23:11:34 +01:00
{
2014-08-20 19:03:58 +02:00
global $conf ;
2013-12-01 21:03:12 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-01-04 23:56:10 +01:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-01-04 23:56:10 +01:00
2008-11-02 23:11:34 +01:00
if ( $this -> statut != 1 )
2008-06-21 01:01:02 +02:00
{
2008-11-02 23:11:34 +01:00
$this -> db -> begin ();
2012-07-29 12:54:19 +02:00
2012-04-30 15:01:25 +02:00
$now = dol_now ();
2013-05-20 21:21:03 +02:00
2013-03-10 13:58:32 +01:00
// Define new ref
2015-10-06 16:38:27 +02:00
if ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ))) // empty should not happened, but when it occurs, the test save life
2013-03-10 13:58:32 +01:00
{
2013-05-12 17:54:30 +02:00
$num = $this -> getNextNumRef ( $this -> thirdparty );
2013-03-10 13:58:32 +01:00
}
else
{
$num = $this -> ref ;
}
2017-10-07 13:09:31 +02:00
$this -> newref = $num ;
2009-03-12 23:49:05 +01:00
2008-11-02 23:11:34 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2009-05-02 16:26:54 +02:00
$sql .= " SET fk_statut = 1 " ;
2013-03-10 13:58:32 +01:00
$sql .= " , ref = ' " . $num . " ' " ;
2013-12-01 21:03:12 +01:00
$sql .= " , date_valid = ' " . $this -> db -> idate ( $now ) . " ' " ;
2009-05-02 16:26:54 +02:00
$sql .= " , fk_user_valid = " . $user -> id ;
$sql .= " WHERE rowid = " . $this -> id ;
$sql .= " AND entity = " . $conf -> entity ;
$sql .= " AND fk_statut = 0 " ;
2009-03-12 23:49:05 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setValid " , LOG_DEBUG );
2008-11-02 23:11:34 +01:00
$resql = $this -> db -> query ( $sql );
2013-12-01 21:03:12 +01:00
if ( ! $resql )
{
dol_print_error ( $this -> db );
$error ++ ;
}
2014-10-27 03:24:45 +01:00
if ( ! $error && ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_VALIDATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2014-10-27 03:24:45 +01:00
}
2013-12-01 21:03:12 +01:00
if ( ! $error )
{
2014-10-27 03:24:45 +01:00
$this -> oldref = $this -> ref ;
2013-12-01 21:03:12 +01:00
// Rename directory if dir was a temporary ref
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ))
{
// Rename of object directory ($this->ref = old ref, $num = new ref)
// to not lose the linked files
$oldref = dol_sanitizeFileName ( $this -> ref );
2014-10-27 03:24:45 +01:00
$newref = dol_sanitizeFileName ( $num );
2013-12-01 21:03:12 +01:00
$dirsource = $conf -> ficheinter -> dir_output . '/' . $oldref ;
2014-10-27 03:24:45 +01:00
$dirdest = $conf -> ficheinter -> dir_output . '/' . $newref ;
2013-12-01 21:03:12 +01:00
if ( file_exists ( $dirsource ))
{
2014-10-27 03:24:45 +01:00
dol_syslog ( get_class ( $this ) . " ::setValid rename dir " . $dirsource . " into " . $dirdest );
2013-12-01 21:03:12 +01:00
if ( @ rename ( $dirsource , $dirdest ))
{
2017-10-07 13:09:31 +02:00
dol_syslog ( " Rename ok " );
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list ( $conf -> ficheinter -> dir_output . '/' . $newref , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
foreach ( $listoffiles as $fileentry )
{
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
@ rename ( $dirsource , $dirdest );
}
2013-12-01 21:03:12 +01:00
}
}
}
}
// Set new ref and define current statut
if ( ! $error )
{
$this -> ref = $num ;
$this -> statut = 1 ;
$this -> brouillon = 0 ;
$this -> date_validation = $now ;
}
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
2008-11-02 23:11:34 +01:00
}
else
{
$this -> db -> rollback ();
2012-12-30 17:13:43 +01:00
dol_syslog ( get_class ( $this ) . " ::setValid " . $this -> error , LOG_ERR );
2008-11-02 23:11:34 +01:00
return - 1 ;
}
2008-06-21 01:01:02 +02:00
}
}
2011-07-02 18:48:31 +02:00
2017-05-09 22:24:24 +02:00
/**
* Returns amount based on user thm
*
2017-09-11 14:41:46 +02:00
* @ return float Amount
2017-05-09 22:24:24 +02:00
*/
2017-09-11 14:41:46 +02:00
function getAmount ()
{
2017-05-09 22:24:24 +02:00
global $db ;
2017-07-25 21:22:22 +02:00
2017-05-09 22:24:24 +02:00
$amount = 0 ;
2017-07-25 21:22:22 +02:00
2017-05-09 22:24:24 +02:00
$this -> author = new User ( $db );
$this -> author -> fetch ( $this -> user_creation );
2017-07-25 21:22:22 +02:00
2017-05-09 22:24:24 +02:00
$thm = $this -> author -> thm ;
2017-07-25 21:22:22 +02:00
2017-09-11 14:41:46 +02:00
foreach ( $this -> lines as $line ) {
$amount += ( $line -> duration / 60 / 60 * $thm );
2017-05-09 22:24:24 +02:00
}
2017-07-25 21:22:22 +02:00
2017-09-11 14:41:46 +02:00
return price2num ( $amount , 'MT' );
2017-05-09 22:24:24 +02:00
}
2017-09-11 14:50:00 +02:00
2017-09-12 13:26:25 +02:00
2017-09-09 20:00:09 +02:00
/**
* Create a document onto disk according to template module .
*
* @ param string $modele Force model to use ( '' to not force )
* @ param Translate $outputlangs Object langs to use for output
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
2018-06-15 09:47:28 +02:00
* @ param null | array $moreparams Array to provide more information
2017-09-09 20:00:09 +02:00
* @ return int 0 if KO , 1 if OK
*/
2018-06-15 09:47:28 +02:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
2017-09-09 20:00:09 +02:00
{
global $conf , $langs ;
$langs -> load ( " interventions " );
if ( ! dol_strlen ( $modele )) {
2017-09-11 14:50:00 +02:00
$modele = 'soleil' ;
2017-09-09 20:00:09 +02:00
if ( $this -> modelpdf ) {
$modele = $this -> modelpdf ;
2017-09-11 14:50:00 +02:00
} elseif ( ! empty ( $conf -> global -> FICHEINTER_ADDON_PDF )) {
$modele = $conf -> global -> FICHEINTER_ADDON_PDF ;
2017-09-09 20:00:09 +02:00
}
}
$modelpath = " core/modules/fichinter/doc/ " ;
2018-06-15 09:47:28 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
2017-09-09 20:00:09 +02:00
}
2017-07-25 21:22:22 +02:00
2008-06-21 01:01:02 +02:00
/**
2011-07-08 11:18:54 +02:00
* Returns the label status
2012-01-08 00:52:07 +01:00
*
2011-12-19 17:29:09 +01:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Label
2008-06-21 01:01:02 +02:00
*/
function getLibStatut ( $mode = 0 )
{
2006-05-10 22:34:25 +02:00
return $this -> LibStatut ( $this -> statut , $mode );
2008-06-21 01:01:02 +02:00
}
/**
2011-07-08 11:18:54 +02:00
* Returns the label of a statut
2011-12-19 17:29:09 +01:00
*
* @ param int $statut id statut
2016-09-19 13:09:50 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
2011-12-19 17:29:09 +01:00
* @ return string Label
2008-06-21 01:01:02 +02:00
*/
function LibStatut ( $statut , $mode = 0 )
{
2018-04-21 15:46:55 +02:00
// Init/load array of translation of status
if ( empty ( $this -> statuts ) || empty ( $this -> statuts_short ))
{
global $langs ;
$langs -> load ( " fichinter " );
$this -> statuts [ 0 ] = $langs -> trans ( 'Draft' );
$this -> statuts [ 1 ] = $langs -> trans ( 'Validated' );
$this -> statuts [ 2 ] = $langs -> trans ( 'StatusInterInvoiced' );
$this -> statuts [ 3 ] = $langs -> trans ( 'Done' );
$this -> statuts_short [ 0 ] = $langs -> trans ( 'Draft' );
$this -> statuts_short [ 1 ] = $langs -> trans ( 'Validated' );
$this -> statuts_short [ 2 ] = $langs -> trans ( 'StatusInterInvoiced' );
$this -> statuts_short [ 3 ] = $langs -> trans ( 'Done' );
$this -> statuts_logo [ 0 ] = 'statut0' ;
$this -> statuts_logo [ 1 ] = 'statut1' ;
$this -> statuts_logo [ 2 ] = 'statut6' ;
$this -> statuts_logo [ 3 ] = 'statut6' ;
}
2010-01-04 22:45:59 +01:00
2008-06-21 01:01:02 +02:00
if ( $mode == 0 )
2018-04-21 15:46:55 +02:00
return $this -> statuts [ $statut ];
2008-06-21 01:01:02 +02:00
if ( $mode == 1 )
2018-04-21 15:46:55 +02:00
return $this -> statuts_short [ $statut ];
2008-06-21 01:01:02 +02:00
if ( $mode == 2 )
2018-04-21 15:46:55 +02:00
return img_picto ( $this -> statuts_short [ $statut ], $this -> statuts_logo [ $statut ]) . ' ' . $this -> statuts_short [ $statut ];
2008-06-21 01:01:02 +02:00
if ( $mode == 3 )
2018-04-21 15:46:55 +02:00
return img_picto ( $this -> statuts_short [ $statut ], $this -> statuts_logo [ $statut ]);
2008-06-21 01:01:02 +02:00
if ( $mode == 4 )
2018-04-21 15:46:55 +02:00
return img_picto ( $this -> statuts_short [ $statut ], $this -> statuts_logo [ $statut ]) . ' ' . $this -> statuts [ $statut ];
2008-06-21 01:01:02 +02:00
if ( $mode == 5 )
2018-04-21 15:46:55 +02:00
return '<span class="hideonsmartphone">' . $this -> statuts_short [ $statut ] . ' </span>' . img_picto ( $this -> statuts [ $statut ], $this -> statuts_logo [ $statut ]);
2016-09-19 13:09:50 +02:00
if ( $mode == 6 )
2018-04-21 15:46:55 +02:00
return '<span class="hideonsmartphone">' . $this -> statuts [ $statut ] . ' </span>' . img_picto ( $this -> statuts [ $statut ], $this -> statuts_logo [ $statut ]);
2017-07-25 21:22:22 +02:00
2016-09-19 13:09:50 +02:00
return '' ;
2008-06-21 01:01:02 +02:00
}
2010-02-27 14:37:13 +01:00
/**
2011-07-08 11:18:54 +02:00
* Return clicable name ( with picto eventually )
2011-12-19 17:29:09 +01:00
*
2017-10-03 14:50:52 +02:00
* @ param int $withpicto 0 = _No picto , 1 = Includes the picto in the linkn , 2 = Picto only
* @ param string $option Options
2017-10-07 13:09:31 +02:00
* @ param int $notooltip 1 = Disable tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2017-10-03 14:50:52 +02:00
* @ return string String with URL
2010-02-27 14:37:13 +01:00
*/
2017-10-03 14:50:52 +02:00
function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $save_lastsearch_value =- 1 )
2010-02-27 14:37:13 +01:00
{
2018-06-04 13:36:38 +02:00
global $conf , $langs , $hookmanager ;
2010-02-27 14:37:13 +01:00
$result = '' ;
2017-10-03 14:50:52 +02:00
2017-10-07 13:09:31 +02:00
$label = '<u>' . $langs -> trans ( " ShowIntervention " ) . '</u>' ;
if ( ! empty ( $this -> ref ))
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
2010-02-27 14:37:13 +01:00
2017-10-07 13:09:31 +02:00
$url = DOL_URL_ROOT . '/fichinter/card.php?id=' . $this -> id ;
2010-02-27 14:37:13 +01:00
2017-10-07 13:09:31 +02:00
if ( $option !== 'nolink' )
{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) $add_save_lastsearch_values = 1 ;
if ( $add_save_lastsearch_values ) $url .= '&save_lastsearch_values=1' ;
}
2017-10-03 14:50:52 +02:00
$linkclose = '' ;
if ( empty ( $notooltip ))
{
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowIntervention " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
2018-05-30 14:19:30 +02:00
2018-06-18 13:31:29 +02:00
/*
2018-05-30 14:19:30 +02:00
$hookmanager -> initHooks ( array ( 'fichinterdao' ));
$parameters = array ( 'id' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'getnomurltooltip' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $linkclose = $hookmanager -> resPrint ;
2018-06-18 13:31:29 +02:00
*/
2017-10-03 14:50:52 +02:00
}
2010-02-27 14:37:13 +01:00
2017-10-03 14:50:52 +02:00
$linkstart = '<a href="' . $url . '"' ;
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2010-02-27 14:37:13 +01:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
if ( $withpicto ) $result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
if ( $withpicto != 2 ) $result .= $this -> ref ;
$result .= $linkend ;
2018-06-18 13:31:29 +02:00
global $action ;
$hookmanager -> initHooks ( array ( 'intervnetiondao' ));
$parameters = array ( 'id' => $this -> id , 'getnomurl' => $result );
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $result = $hookmanager -> resPrint ;
else $result .= $hookmanager -> resPrint ;
2010-02-27 14:37:13 +01:00
return $result ;
}
2008-06-21 01:01:02 +02:00
/**
2011-07-08 11:18:54 +02:00
* Returns the next non used reference of intervention
* depending on the module numbering assets within FICHEINTER_ADDON
2011-12-19 17:29:09 +01:00
*
2014-12-20 15:42:03 +01:00
* @ param Societe $soc Thirdparty object
2011-12-19 17:29:09 +01:00
* @ return string Free reference for intervention
2008-06-21 01:01:02 +02:00
*/
function getNextNumRef ( $soc )
{
2009-03-23 20:13:51 +01:00
global $conf , $db , $langs ;
2008-06-21 01:01:02 +02:00
$langs -> load ( " interventions " );
2009-03-23 20:13:51 +01:00
if ( ! empty ( $conf -> global -> FICHEINTER_ADDON ))
2008-06-21 01:01:02 +02:00
{
2015-01-17 18:58:05 +01:00
$mybool = false ;
2015-01-17 19:13:38 +01:00
$file = " mod_ " . $conf -> global -> FICHEINTER_ADDON . " .php " ;
$classname = " mod_ " . $conf -> global -> FICHEINTER_ADDON ;
2015-01-17 18:58:05 +01:00
// Include file with class
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
foreach ( $dirmodels as $reldir ) {
$dir = dol_buildpath ( $reldir . " core/modules/fichinter/ " );
// Load file with numbering class (if found)
$mybool |=@ include_once $dir . $file ;
2009-03-23 20:13:51 +01:00
}
2009-04-29 20:02:50 +02:00
2015-01-17 18:58:05 +01:00
if ( ! $mybool )
{
dol_print_error ( '' , " Failed to include file " . $file );
return '' ;
}
2008-06-21 01:01:02 +02:00
$obj = new $classname ();
$numref = " " ;
2015-01-17 18:58:05 +01:00
$numref = $obj -> getNextValue ( $soc , $this );
2008-06-21 01:01:02 +02:00
if ( $numref != " " )
{
return $numref ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $db , " Fichinter::getNextNumRef " . $obj -> error );
2008-06-21 01:01:02 +02:00
return " " ;
}
}
else
{
2015-01-17 18:58:05 +01:00
$langs -> load ( " errors " );
2008-06-21 01:01:02 +02:00
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " Error_FICHEINTER_ADDON_NotDefined " );
return " " ;
}
}
2007-06-22 14:36:50 +02:00
/**
2013-11-05 14:24:37 +01:00
* Load information on object
2012-01-04 23:56:10 +01:00
*
2013-11-05 14:24:37 +01:00
* @ param int $id Id of object
2012-01-08 00:52:07 +01:00
* @ return void
2008-06-21 01:01:02 +02:00
*/
2008-02-24 18:01:48 +01:00
function info ( $id )
{
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2010-01-13 19:51:19 +01:00
$sql = " SELECT f.rowid, " ;
2014-10-20 11:16:51 +02:00
$sql .= " f.datec, " ;
$sql .= " f.tms as date_modification, " ;
2010-01-13 19:51:19 +01:00
$sql .= " f.date_valid as datev, " ;
$sql .= " f.fk_user_author, " ;
2014-10-20 11:16:51 +02:00
$sql .= " f.fk_user_modif as fk_user_modification, " ;
2010-01-13 19:51:19 +01:00
$sql .= " f.fk_user_valid " ;
2008-06-21 01:01:02 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as f " ;
$sql .= " WHERE f.rowid = " . $id ;
2013-11-05 14:24:37 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-06-21 01:01:02 +02:00
{
2013-11-05 14:24:37 +01:00
if ( $this -> db -> num_rows ( $resql ))
2008-06-21 01:01:02 +02:00
{
2013-11-05 14:24:37 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2008-06-21 01:01:02 +02:00
$this -> id = $obj -> rowid ;
2010-01-13 19:51:19 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
2014-10-20 11:16:51 +02:00
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2010-01-13 19:51:19 +01:00
$this -> date_validation = $this -> db -> jdate ( $obj -> datev );
2008-06-21 01:01:02 +02:00
2010-04-28 09:31:34 +02:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
2008-06-21 01:01:02 +02:00
$this -> user_creation = $cuser ;
if ( $obj -> fk_user_valid )
{
2010-04-28 09:31:34 +02:00
$vuser = new User ( $this -> db );
$vuser -> fetch ( $obj -> fk_user_valid );
2008-06-21 01:01:02 +02:00
$this -> user_validation = $vuser ;
}
2014-10-20 11:16:51 +02:00
if ( $obj -> fk_user_modification )
{
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modification );
$this -> user_modification = $muser ;
}
2008-06-21 01:01:02 +02:00
}
2013-11-05 14:24:37 +01:00
$this -> db -> free ( $resql );
2008-06-21 01:01:02 +02:00
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-06-21 01:01:02 +02:00
}
}
2008-04-22 01:02:50 +02:00
/**
2011-07-08 11:18:54 +02:00
* Delete intervetnion
2011-12-19 17:29:09 +01:00
*
2013-07-07 13:14:32 +02:00
* @ param User $user Object user who delete
* @ param int $notrigger Disable trigger
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2013-05-19 18:28:27 +02:00
function delete ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2013-05-19 18:28:27 +02:00
global $conf , $langs ;
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2010-05-04 20:03:45 +02:00
2010-04-30 07:44:19 +02:00
$error = 0 ;
2008-06-21 01:01:02 +02:00
2008-04-22 01:02:50 +02:00
$this -> db -> begin ();
2011-07-02 18:48:31 +02:00
2017-11-06 19:27:10 +01:00
if ( ! $error && ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_DELETE' , $user );
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
// End call triggers
}
2011-07-01 18:24:44 +02:00
// Delete linked object
2017-11-06 19:27:10 +01:00
if ( ! $error )
{
$res = $this -> deleteObjectLinked ();
if ( $res < 0 ) $error ++ ;
}
2011-07-02 18:48:31 +02:00
2011-07-01 18:24:44 +02:00
// Delete linked contacts
2017-11-06 19:27:10 +01:00
if ( ! $error )
2011-07-01 18:24:44 +02:00
{
2017-11-06 19:27:10 +01:00
$res = $this -> delete_linked_contact ();
if ( $res < 0 )
{
$this -> error = 'ErrorFailToDeleteLinkedContact' ;
$error ++ ;
}
2011-07-01 18:24:44 +02:00
}
2011-07-02 18:48:31 +02:00
2017-11-06 19:27:10 +01:00
if ( ! $error )
2011-07-01 18:24:44 +02:00
{
2017-11-06 19:27:10 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fichinterdet " ;
$sql .= " WHERE fk_fichinter = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $error ++ ;
2010-04-30 07:44:19 +02:00
}
2008-06-21 01:01:02 +02:00
2017-11-06 19:27:10 +01:00
if (( ! $error ) && ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))) // For avoid conflicts if trigger used
{
// Remove extrafields
$res = $this -> deleteExtraFields ();
if ( $res < 0 ) $error ++ ;
}
2009-07-07 18:19:37 +02:00
2017-11-06 19:27:10 +01:00
if ( ! $error )
2007-08-28 09:44:17 +02:00
{
2017-11-06 19:27:10 +01:00
// Delete object
2009-05-02 16:26:54 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fichinter " ;
$sql .= " WHERE rowid = " . $this -> id ;
2009-07-07 18:19:37 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " Fichinter::delete " , LOG_DEBUG );
2017-11-06 19:27:10 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $error ++ ;
}
2008-06-21 01:01:02 +02:00
2017-11-06 19:27:10 +01:00
if ( ! $error )
{
// Remove directory with files
$fichinterref = dol_sanitizeFileName ( $this -> ref );
if ( $conf -> ficheinter -> dir_output )
{
$dir = $conf -> ficheinter -> dir_output . " / " . $fichinterref ;
$file = $conf -> ficheinter -> dir_output . " / " . $fichinterref . " / " . $fichinterref . " .pdf " ;
if ( file_exists ( $file ))
2007-08-28 09:44:17 +02:00
{
2017-11-06 19:27:10 +01:00
dol_delete_preview ( $this );
2008-06-21 01:01:02 +02:00
2017-11-06 19:27:10 +01:00
if ( ! dol_delete_file ( $file , 0 , 0 , 0 , $this )) // For triggers
2007-08-28 09:44:17 +02:00
{
2017-11-06 19:27:10 +01:00
$this -> error = $langs -> trans ( " ErrorCanNotDeleteFile " , $file );
return 0 ;
2007-08-28 09:44:17 +02:00
}
}
2017-11-06 19:27:10 +01:00
if ( file_exists ( $dir ))
2013-05-19 18:28:27 +02:00
{
2017-11-06 19:27:10 +01:00
if ( ! dol_delete_dir_recursive ( $dir ))
{
$this -> error = $langs -> trans ( " ErrorCanNotDeleteDir " , $dir );
return 0 ;
}
2013-05-19 18:28:27 +02:00
}
2007-08-28 09:44:17 +02:00
}
}
2017-11-06 19:27:10 +01:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
2007-08-28 09:44:17 +02:00
else
{
2008-04-22 01:02:50 +02:00
$this -> db -> rollback ();
2007-08-28 09:44:17 +02:00
return - 1 ;
}
}
2008-06-21 01:01:02 +02:00
/**
2011-07-08 11:18:54 +02:00
* Defines a delivery date of intervention
2011-12-19 17:29:09 +01:00
*
* @ param User $user Object user who define
* @ param date $date_delivery date of delivery
* @ return int < 0 if ko , > 0 if ok
2008-06-21 01:01:02 +02:00
*/
function set_date_delivery ( $user , $date_delivery )
{
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2008-06-21 01:01:02 +02:00
if ( $user -> rights -> ficheinter -> creer )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2014-03-15 23:12:00 +01:00
$sql .= " SET datei = ' " . $this -> db -> idate ( $date_delivery ) . " ' " ;
2009-05-02 16:26:54 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
$sql .= " AND fk_statut = 0 " ;
2008-06-21 01:01:02 +02:00
if ( $this -> db -> query ( $sql ))
{
$this -> date_delivery = $date_delivery ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " Fichinter::set_date_delivery Erreur SQL " );
2008-06-21 01:01:02 +02:00
return - 1 ;
}
}
}
/**
2011-07-08 11:18:54 +02:00
* Define the label of the intervention
2011-12-19 17:29:09 +01:00
*
* @ param User $user Object user who modify
* @ param string $description description
2014-09-15 12:48:24 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
function set_description ( $user , $description )
{
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2008-06-21 01:01:02 +02:00
if ( $user -> rights -> ficheinter -> creer )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2014-10-20 11:16:51 +02:00
$sql .= " SET description = ' " . $this -> db -> escape ( $description ) . " ', " ;
$sql .= " fk_user_modif = " . $user -> id ;
2009-05-02 16:26:54 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2008-06-21 01:01:02 +02:00
if ( $this -> db -> query ( $sql ))
{
$this -> description = $description ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " Fichinter::set_description Erreur SQL " );
2008-06-21 01:01:02 +02:00
return - 1 ;
}
}
}
2013-06-03 01:43:12 +02:00
/**
2013-08-22 16:49:23 +02:00
* Link intervention to a contract
2013-06-03 01:43:12 +02:00
*
* @ param User $user Object user who modify
2013-07-07 13:14:32 +02:00
* @ param int $contractid Description
* @ return int < 0 if ko , > 0 if ok
2013-06-03 01:43:12 +02:00
*/
2013-08-22 16:49:23 +02:00
function set_contrat ( $user , $contractid )
2013-06-03 01:43:12 +02:00
{
global $conf ;
if ( $user -> rights -> ficheinter -> creer )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2013-08-22 16:49:23 +02:00
$sql .= " SET fk_contrat = ' " . $contractid . " ' " ;
2013-06-03 01:43:12 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
2013-08-22 16:49:23 +02:00
$this -> fk_contrat = $contractid ;
2013-06-03 01:43:12 +02:00
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2013-08-22 16:49:23 +02:00
return - 2 ;
2013-06-03 01:43:12 +02:00
}
2017-07-25 21:22:22 +02:00
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
/**
* Load an object from its id and create a new one in database
*
* @ param int $socid Id of thirdparty
* @ return int New id of clone
*/
function createFromClone ( $socid = 0 )
{
global $user , $hookmanager ;
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
$error = 0 ;
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
$this -> context [ 'createfromclone' ] = 'createfromclone' ;
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2016-07-01 18:12:37 +02:00
// get extrafields so they will be clone
foreach ( $this -> lines as $line )
$line -> fetch_optionals ( $line -> rowid );
2017-10-07 13:09:31 +02:00
// Load source object
$objFrom = clone $this ;
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
// Change socid if needed
if ( ! empty ( $socid ) && $socid != $this -> socid )
{
$objsoc = new Societe ( $this -> db );
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
if ( $objsoc -> fetch ( $socid ) > 0 )
{
$this -> socid = $objsoc -> id ;
//$this->cond_reglement_id = (! empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
//$this->mode_reglement_id = (! empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
$this -> fk_project = '' ;
$this -> fk_delivery_address = '' ;
}
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
// TODO Change product price if multi-prices
}
2016-07-01 18:12:37 +02:00
2017-10-07 13:09:31 +02:00
$this -> id = 0 ;
2016-07-01 18:12:37 +02:00
$this -> ref = '' ;
2017-10-07 13:09:31 +02:00
$this -> statut = 0 ;
// Clear fields
$this -> user_author_id = $user -> id ;
$this -> user_valid = '' ;
$this -> date_creation = '' ;
$this -> date_validation = '' ;
$this -> ref_client = '' ;
// Create clone
$result = $this -> create ( $user );
if ( $result < 0 ) $error ++ ;
if ( ! $error )
{
// Add lines because it is not included into create function
foreach ( $this -> lines as $line )
{
$this -> addline ( $user , $this -> id , $line -> desc , $line -> datei , $line -> duration );
}
// Hook of thirdparty module
if ( is_object ( $hookmanager ))
{
$parameters = array ( 'objFrom' => $objFrom );
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'createFrom' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook < 0 ) $error ++ ;
}
}
unset ( $this -> context [ 'createfromclone' ]);
// End
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
2017-07-25 21:22:22 +02:00
2008-06-21 01:01:02 +02:00
/**
2011-07-08 11:18:54 +02:00
* Adding a line of intervention into data base
2011-12-19 17:29:09 +01:00
*
2013-05-19 18:28:27 +02:00
* @ param user $user User that do the action
2011-12-19 17:29:09 +01:00
* @ param int $fichinterid Id of intervention
* @ param string $desc Line description
* @ param date $date_intervention Intervention date
* @ param int $duration Intervention duration
2015-02-28 04:59:27 +01:00
* @ param array $array_options Array option
2011-12-19 17:29:09 +01:00
* @ return int > 0 if ok , < 0 if ko
2008-06-21 01:01:02 +02:00
*/
2015-02-28 04:59:27 +01:00
function addline ( $user , $fichinterid , $desc , $date_intervention , $duration , $array_options = '' )
2008-06-21 01:01:02 +02:00
{
2014-08-28 14:48:16 +02:00
dol_syslog ( get_class ( $this ) . " ::addline $fichinterid , $desc , $date_intervention , $duration " );
2009-03-12 23:49:05 +01:00
2008-06-21 01:01:02 +02:00
if ( $this -> statut == 0 )
{
$this -> db -> begin ();
2009-03-12 23:49:05 +01:00
2008-06-21 01:01:02 +02:00
// Insertion ligne
2010-12-15 08:12:28 +01:00
$line = new FichinterLigne ( $this -> db );
2009-03-12 23:49:05 +01:00
2010-12-15 08:12:28 +01:00
$line -> fk_fichinter = $fichinterid ;
$line -> desc = $desc ;
$line -> datei = $date_intervention ;
$line -> duration = $duration ;
2009-03-12 23:49:05 +01:00
2015-02-28 04:59:27 +01:00
if ( is_array ( $array_options ) && count ( $array_options ) > 0 ) {
$line -> array_options = $array_options ;
2014-08-26 17:08:03 +02:00
}
2013-05-19 18:28:27 +02:00
$result = $line -> insert ( $user );
2014-08-28 14:48:16 +02:00
2014-08-26 17:08:03 +02:00
if ( $result >= 0 )
2008-06-21 01:01:02 +02:00
{
$this -> db -> commit ();
return 1 ;
}
else
2014-08-28 14:48:16 +02:00
{
2008-06-21 01:01:02 +02:00
$this -> error = $this -> db -> error ();
$this -> db -> rollback ();
return - 1 ;
}
}
}
2007-08-28 09:44:17 +02:00
2007-08-29 09:55:34 +02:00
/**
2013-05-19 11:18:45 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
2008-06-21 01:01:02 +02:00
*/
2007-08-29 09:55:34 +02:00
function initAsSpecimen ()
{
2009-05-02 16:26:54 +02:00
global $user , $langs , $conf ;
2007-08-29 09:55:34 +02:00
2011-09-12 21:34:59 +02:00
$now = dol_now ();
2007-08-29 09:55:34 +02:00
2007-12-08 15:06:01 +01:00
// Initialise parametres
2007-08-29 09:55:34 +02:00
$this -> id = 0 ;
$this -> ref = 'SPECIMEN' ;
$this -> specimen = 1 ;
2010-09-01 16:13:51 +02:00
$this -> socid = 1 ;
2012-09-17 00:43:23 +02:00
$this -> datec = $now ;
$this -> note_private = 'Private note' ;
2007-08-29 09:55:34 +02:00
$this -> note_public = 'SPECIMEN' ;
2015-06-11 23:18:29 +02:00
$this -> duration = 0 ;
2015-02-03 13:43:37 +01:00
$nbp = 25 ;
2007-08-29 09:55:34 +02:00
$xnbp = 0 ;
while ( $xnbp < $nbp )
{
2010-10-27 16:47:32 +02:00
$line = new FichinterLigne ( $this -> db );
$line -> desc = $langs -> trans ( " Description " ) . " " . $xnbp ;
2011-09-12 21:34:59 +02:00
$line -> datei = ( $now - 3600 * ( 1 + $xnbp ));
$line -> duration = 600 ;
2013-05-19 11:18:45 +02:00
$line -> fk_fichinter = 0 ;
2010-10-27 16:47:32 +02:00
$this -> lines [ $xnbp ] = $line ;
2007-08-29 09:55:34 +02:00
$xnbp ++ ;
2011-09-12 20:43:01 +02:00
2015-06-11 23:18:29 +02:00
$this -> duration += $line -> duration ;
2011-09-12 21:34:59 +02:00
}
2007-08-29 09:55:34 +02:00
}
2007-08-28 09:44:17 +02:00
2008-06-21 01:01:02 +02:00
/**
2014-09-04 18:58:33 +02:00
* Load array lines -> lines
2011-12-19 17:29:09 +01:00
*
2014-09-04 18:58:33 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
function fetch_lines ()
{
2018-02-08 12:41:31 +01:00
$this -> lines = array ();
2011-07-01 18:24:44 +02:00
$sql = 'SELECT rowid, description, duree, date, rang' ;
2008-06-21 01:01:02 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'fichinterdet' ;
2016-04-14 10:50:32 +02:00
$sql .= ' WHERE fk_fichinter = ' . $this -> id . ' ORDER BY rang ASC, date ASC' ;
2007-08-28 09:44:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_lines " , LOG_DEBUG );
2008-06-21 01:01:02 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2011-07-01 18:24:44 +02:00
$num = $this -> db -> num_rows ( $resql );
2008-06-21 01:01:02 +02:00
$i = 0 ;
while ( $i < $num )
{
2011-07-01 18:24:44 +02:00
$objp = $this -> db -> fetch_object ( $resql );
2008-06-21 01:01:02 +02:00
2010-12-15 08:12:28 +01:00
$line = new FichinterLigne ( $this -> db );
$line -> id = $objp -> rowid ;
2011-07-01 18:24:44 +02:00
$line -> desc = $objp -> description ;
2016-07-01 19:15:30 +02:00
$line -> duration = $objp -> duree ;
2011-07-01 18:24:44 +02:00
//For invoicing we calculing hours
$line -> qty = round ( $objp -> duree / 3600 , 2 );
$line -> date = $this -> db -> jdate ( $objp -> date );
2016-07-01 19:15:30 +02:00
$line -> datei = $this -> db -> jdate ( $objp -> date );
2011-07-01 18:24:44 +02:00
$line -> rang = $objp -> rang ;
2011-07-02 19:02:00 +02:00
$line -> product_type = 1 ;
2007-08-28 09:44:17 +02:00
2010-12-15 08:12:28 +01:00
$this -> lines [ $i ] = $line ;
2008-06-21 01:01:02 +02:00
$i ++ ;
}
2011-07-01 18:24:44 +02:00
$this -> db -> free ( $resql );
2010-10-27 16:47:32 +02:00
2008-06-21 01:01:02 +02:00
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
* @ return bool
*/
public static function replaceThirdparty ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'fichinter'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2008-06-21 01:01:02 +02:00
}
/**
2012-09-17 00:43:23 +02:00
* Classe permettant la gestion des lignes d ' intervention
2008-06-21 01:01:02 +02:00
*/
2014-08-01 08:59:08 +02:00
class FichinterLigne extends CommonObjectLine
2007-08-28 09:44:17 +02:00
{
var $db ;
var $error ;
2008-06-21 01:01:02 +02:00
// From llx_fichinterdet
2007-08-28 09:44:17 +02:00
var $fk_fichinter ;
2008-06-21 01:01:02 +02:00
var $desc ; // Description ligne
var $datei ; // Date intervention
var $duration ; // Duree de l'intervention
var $rang = 0 ;
2007-08-28 09:44:17 +02:00
2014-08-26 11:50:02 +02:00
public $element = 'fichinterdet' ;
public $table_element = 'fichinterdet' ;
public $fk_element = 'fk_fichinter' ;
2014-08-28 14:48:16 +02:00
2007-08-28 09:44:17 +02:00
/**
2012-01-08 00:52:07 +01:00
* Constructor
*
2012-02-24 17:25:53 +01:00
* @ param DoliDB $db Database handler
2007-08-28 09:44:17 +02:00
*/
2012-02-24 17:25:53 +01:00
function __construct ( $db )
2007-08-28 09:44:17 +02:00
{
2012-02-24 17:25:53 +01:00
$this -> db = $db ;
2007-08-28 09:44:17 +02:00
}
/**
2011-07-08 11:18:54 +02:00
* Retrieve the line of intervention
2011-12-19 17:29:09 +01:00
*
2012-01-08 00:52:07 +01:00
* @ param int $rowid Line id
* @ return int < 0 if KO , > 0 if OK
2007-08-28 09:44:17 +02:00
*/
function fetch ( $rowid )
{
2007-11-04 22:21:33 +01:00
$sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,' ;
2010-01-13 19:51:19 +01:00
$sql .= ' ft.date as datei' ;
2007-11-04 22:21:33 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'fichinterdet as ft' ;
2007-08-28 09:44:17 +02:00
$sql .= ' WHERE ft.rowid = ' . $rowid ;
2007-11-04 22:21:33 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::fetch " , LOG_DEBUG );
2007-08-28 09:44:17 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$objp = $this -> db -> fetch_object ( $result );
2007-11-04 22:21:33 +01:00
$this -> rowid = $objp -> rowid ;
2014-08-26 17:25:13 +02:00
$this -> id = $objp -> rowid ;
2007-11-04 22:21:33 +01:00
$this -> fk_fichinter = $objp -> fk_fichinter ;
2010-01-13 19:51:19 +01:00
$this -> datei = $this -> db -> jdate ( $objp -> datei );
2007-11-04 22:21:33 +01:00
$this -> desc = $objp -> description ;
$this -> duration = $objp -> duree ;
$this -> rang = $objp -> rang ;
2007-08-28 09:44:17 +02:00
$this -> db -> free ( $result );
2007-11-04 22:21:33 +01:00
return 1 ;
2007-08-28 09:44:17 +02:00
}
else
{
2007-11-04 22:21:33 +01:00
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
return - 1 ;
2007-08-28 09:44:17 +02:00
}
}
2008-06-21 01:01:02 +02:00
2007-08-28 09:44:17 +02:00
/**
2011-07-08 11:18:54 +02:00
* Insert the line into database
2011-12-19 17:29:09 +01:00
*
2013-05-19 18:28:27 +02:00
* @ param User $user Objet user that make creation
2017-10-07 13:09:31 +02:00
* @ param int $notrigger Disable all triggers
2011-07-08 11:18:54 +02:00
* @ return int < 0 if ko , > 0 if ok
2007-08-28 09:44:17 +02:00
*/
2013-05-19 18:28:27 +02:00
function insert ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2013-05-19 18:28:27 +02:00
global $langs , $conf ;
2013-05-19 11:18:45 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " FichinterLigne::insert rang= " . $this -> rang );
2010-01-19 11:47:26 +01:00
2007-08-28 09:44:17 +02:00
$this -> db -> begin ();
2008-06-21 01:01:02 +02:00
2007-08-28 09:44:17 +02:00
$rangToUse = $this -> rang ;
if ( $rangToUse == - 1 )
{
2007-12-08 15:06:01 +01:00
// Recupere rang max de la ligne d'intervention dans $rangmax
2007-08-28 09:44:17 +02:00
$sql = 'SELECT max(rang) as max FROM ' . MAIN_DB_PREFIX . 'fichinterdet' ;
$sql .= ' WHERE fk_fichinter =' . $this -> fk_fichinter ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$rangToUse = $obj -> max + 1 ;
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2007-08-28 09:44:17 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2008-06-21 01:01:02 +02:00
}
2007-08-28 09:44:17 +02:00
// Insertion dans base de la ligne
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'fichinterdet' ;
$sql .= ' (fk_fichinter, description, date, duree, rang)' ;
$sql .= " VALUES ( " . $this -> fk_fichinter . " , " ;
2011-02-24 19:11:12 +01:00
$sql .= " ' " . $this -> db -> escape ( $this -> desc ) . " ', " ;
2013-05-18 12:53:11 +02:00
$sql .= " ' " . $this -> db -> idate ( $this -> datei ) . " ', " ;
2007-08-28 09:44:17 +02:00
$sql .= " " . $this -> duration . " , " ;
$sql .= ' ' . $rangToUse ;
$sql .= ')' ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::insert " , LOG_DEBUG );
2007-08-28 09:44:17 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2018-06-07 15:17:22 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'fichinterdet' );
$this -> rowid = $this -> id ;
2014-08-28 14:48:16 +02:00
2014-08-26 17:08:03 +02:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2017-10-07 13:09:31 +02:00
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2007-11-04 22:21:33 +01:00
$result = $this -> update_total ();
2014-08-28 14:48:16 +02:00
2007-11-04 22:21:33 +01:00
if ( $result > 0 )
{
$this -> rang = $rangToUse ;
2013-05-20 21:21:03 +02:00
2013-05-19 18:28:27 +02:00
if ( ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'LINEFICHINTER_CREATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2013-05-19 18:28:27 +02:00
}
}
2013-05-20 21:21:03 +02:00
2013-05-19 18:28:27 +02:00
if ( ! $error ) {
2007-11-04 22:21:33 +01:00
$this -> db -> commit ();
return $result ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
2007-08-28 09:44:17 +02:00
}
else
{
$this -> error = $this -> db -> error () . " sql= " . $sql ;
$this -> db -> rollback ();
2007-11-04 22:21:33 +01:00
return - 1 ;
2007-08-28 09:44:17 +02:00
}
}
2008-06-21 01:01:02 +02:00
2007-08-28 09:44:17 +02:00
/**
2011-07-08 11:18:54 +02:00
* Update intervention into database
2011-12-19 17:29:09 +01:00
*
2013-05-19 18:28:27 +02:00
* @ param User $user Objet user that make creation
2017-10-07 13:09:31 +02:00
* @ param int $notrigger Disable all triggers
2011-07-08 11:18:54 +02:00
* @ return int < 0 if ko , > 0 if ok
2007-08-28 09:44:17 +02:00
*/
2013-05-19 18:28:27 +02:00
function update ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2013-05-19 18:28:27 +02:00
global $langs , $conf ;
2013-05-20 21:21:03 +02:00
2007-08-28 09:44:17 +02:00
$this -> db -> begin ();
// Mise a jour ligne en base
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinterdet SET " ;
2011-02-24 19:11:12 +01:00
$sql .= " description=' " . $this -> db -> escape ( $this -> desc ) . " ' " ;
2016-12-02 11:35:12 +01:00
$sql .= " ,date=' " . $this -> db -> idate ( $this -> datei ) . " ' " ;
2007-08-28 09:44:17 +02:00
$sql .= " ,duree= " . $this -> duration ;
2017-05-12 16:55:11 +02:00
$sql .= " ,rang=' " . $this -> db -> escape ( $this -> rang ) . " ' " ;
2018-03-22 16:18:11 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2007-08-28 09:44:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::update " , LOG_DEBUG );
2007-11-04 22:21:33 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2014-08-28 14:48:16 +02:00
2014-08-26 17:08:03 +02:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2017-10-07 13:09:31 +02:00
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2007-11-04 22:21:33 +01:00
$result = $this -> update_total ();
if ( $result > 0 )
{
2013-05-20 21:21:03 +02:00
2013-05-19 18:28:27 +02:00
if ( ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'LINEFICHINTER_UPDATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2013-05-19 18:28:27 +02:00
}
}
2013-05-20 21:21:03 +02:00
2013-05-19 18:28:27 +02:00
if ( ! $error )
2013-05-19 11:18:45 +02:00
{
2007-11-04 22:21:33 +01:00
$this -> db -> commit ();
return $result ;
}
else
{
2008-11-02 23:11:34 +01:00
$this -> error = $this -> db -> lasterror ();
2007-11-04 22:21:33 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
2008-11-02 23:11:34 +01:00
$this -> error = $this -> db -> lasterror ();
2007-11-04 22:21:33 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2008-06-21 01:01:02 +02:00
2007-11-28 22:15:25 +01:00
/**
2011-07-08 11:18:54 +02:00
* Update total duration into llx_fichinter
2011-12-19 17:29:09 +01:00
*
2011-07-08 11:18:54 +02:00
* @ return int < 0 si ko , > 0 si ok
2007-11-28 22:15:25 +01:00
*/
2007-11-04 22:21:33 +01:00
function update_total ()
{
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2010-01-19 11:47:26 +01:00
$this -> db -> begin ();
2015-12-04 11:16:49 +01:00
$sql = " SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee " ;
2007-11-04 22:21:33 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinterdet " ;
$sql .= " WHERE fk_fichinter= " . $this -> fk_fichinter ;
2007-08-28 09:44:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::update_total " , LOG_DEBUG );
2007-08-28 09:44:17 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2007-11-04 22:21:33 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$total_duration = 0 ;
2009-10-29 08:07:48 +01:00
if ( ! empty ( $obj -> total_duration )) $total_duration = $obj -> total_duration ;
2009-03-12 23:49:05 +01:00
2007-11-04 22:21:33 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
$sql .= " SET duree = " . $total_duration ;
2015-12-04 11:16:49 +01:00
$sql .= " , dateo = " . ( ! empty ( $obj -> dateo ) ? " ' " . $this -> db -> idate ( $obj -> dateo ) . " ' " : " null " );
$sql .= " , datee = " . ( ! empty ( $obj -> datee ) ? " ' " . $this -> db -> idate ( $obj -> datee ) . " ' " : " null " );
2007-11-04 22:21:33 +01:00
$sql .= " WHERE rowid = " . $this -> fk_fichinter ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::update_total " , LOG_DEBUG );
2007-11-04 22:21:33 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
$this -> db -> rollback ();
return - 2 ;
}
2007-08-28 09:44:17 +02:00
}
else
{
$this -> error = $this -> db -> error ();
$this -> db -> rollback ();
2007-11-04 22:21:33 +01:00
return - 1 ;
2007-08-28 09:44:17 +02:00
}
}
2008-06-21 01:01:02 +02:00
/**
2011-07-08 11:18:54 +02:00
* Delete a intervention line
2011-12-19 17:29:09 +01:00
*
2013-05-19 18:28:27 +02:00
* @ param User $user Objet user that make creation
2017-10-07 13:09:31 +02:00
* @ param int $notrigger Disable all triggers
2011-07-08 11:18:54 +02:00
* @ return int > 0 if ok , < 0 if ko
2008-06-21 01:01:02 +02:00
*/
2013-05-19 18:28:27 +02:00
function deleteline ( $user , $notrigger = 0 )
2008-06-21 01:01:02 +02:00
{
2013-05-19 18:28:27 +02:00
global $langs , $conf ;
2013-05-20 21:21:03 +02:00
2017-10-07 20:21:47 +02:00
$error = 0 ;
2008-06-21 01:01:02 +02:00
if ( $this -> statut == 0 )
{
2018-03-22 16:18:11 +01:00
dol_syslog ( get_class ( $this ) . " ::deleteline lineid= " . $this -> id );
2008-06-21 01:01:02 +02:00
$this -> db -> begin ();
2009-03-12 23:49:05 +01:00
2018-03-22 16:18:11 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fichinterdet WHERE rowid = " . $this -> id ;
2008-06-21 01:01:02 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$result = $this -> update_total ();
if ( $result > 0 )
{
2013-05-19 18:28:27 +02:00
if ( ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'LINEFICHINTER_DELETE' , $user );
if ( $result < 0 ) { $error ++ ; $this -> db -> rollback (); return - 1 ; }
// End call triggers
2013-05-19 18:28:27 +02:00
}
2013-05-20 21:21:03 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return $result ;
2008-06-21 01:01:02 +02:00
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
$this -> error = $this -> db -> error () . " sql= " . $sql ;
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
return - 2 ;
}
}
2007-08-28 09:44:17 +02:00
}