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 >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2020-09-03 12:43:38 +02:00
* Copyright ( C ) 2011 - 2020 Juanjo Menent < jmenent @ 2 byte . es >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
2020-09-30 15:17:44 +02:00
* Copyright ( C ) 2015 - 2020 Charlene Benke < charlie @ patas - monkey . com >
2019-03-21 15:08:09 +01:00
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2020-11-01 21:52:34 +01:00
* Copyright ( C ) 2018 - 2020 Frédéric France < frederic . france @ netlogic . fr >
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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2013-08-22 16:49:23 +02:00
*/
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
*/
2019-12-18 15:20:36 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
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
{
2020-03-04 10:10:29 +01:00
2020-03-12 12:45:44 +01:00
public $fields = array (
2020-03-04 10:05:20 +01:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 10 ),
2022-05-21 00:08:18 +02:00
'fk_soc' => array ( 'type' => 'integer:Societe:societe/class/societe.class.php' , 'label' => 'ThirdParty' , 'enabled' => '$conf->societe->enabled' , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 15 ),
2022-06-14 17:53:17 +02:00
'fk_projet' => array ( 'type' => 'integer:Project:projet/class/project.class.php:1:fk_statut=1' , 'label' => 'Fk projet' , 'enabled' => '$conf->project->enabled' , 'visible' =>- 1 , 'position' => 20 ),
2022-05-21 00:08:18 +02:00
'fk_contrat' => array ( 'type' => 'integer' , 'label' => 'Fk contrat' , 'enabled' => '$conf->contrat->enabled' , 'visible' =>- 1 , 'position' => 25 ),
2020-03-04 10:05:20 +01:00
'ref' => array ( 'type' => 'varchar(30)' , 'label' => 'Ref' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'showoncombobox' => 1 , 'position' => 30 ),
'ref_ext' => array ( 'type' => 'varchar(255)' , 'label' => 'Ref ext' , 'enabled' => 1 , 'visible' => 0 , 'position' => 35 ),
2022-05-17 12:53:50 +02:00
'ref_client' => array ( 'type' => 'varchar(255)' , 'label' => 'RefCustomer' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 36 ),
2020-03-04 10:05:20 +01:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => 1 , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 40 , 'index' => 1 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 45 ),
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 50 ),
'date_valid' => array ( 'type' => 'datetime' , 'label' => 'DateValidation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 55 ),
'datei' => array ( 'type' => 'date' , 'label' => 'Datei' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 60 ),
'fk_user_author' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'Fk user author' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 65 ),
'fk_user_modif' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 70 ),
'fk_user_valid' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserValidation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 75 ),
'fk_statut' => array ( 'type' => 'smallint(6)' , 'label' => 'Fk statut' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 500 ),
'dateo' => array ( 'type' => 'date' , 'label' => 'Dateo' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 85 ),
'datee' => array ( 'type' => 'date' , 'label' => 'Datee' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 90 ),
'datet' => array ( 'type' => 'date' , 'label' => 'Datet' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 95 ),
'duree' => array ( 'type' => 'double' , 'label' => 'Duree' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 100 ),
2021-06-05 15:14:10 +02:00
'description' => array ( 'type' => 'text' , 'label' => 'Description' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 105 , 'showoncombobox' => 2 ),
2020-09-22 12:40:26 +02:00
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 110 ),
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 115 ),
2020-03-04 10:05:20 +01:00
'model_pdf' => array ( 'type' => 'varchar(255)' , 'label' => 'Model pdf' , 'enabled' => 1 , 'visible' => 0 , 'position' => 120 ),
'last_main_doc' => array ( 'type' => 'varchar(255)' , 'label' => 'Last main doc' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 125 ),
'import_key' => array ( 'type' => 'varchar(14)' , 'label' => 'ImportId' , 'enabled' => 1 , 'visible' =>- 2 , 'position' => 130 ),
'extraparams' => array ( 'type' => 'varchar(255)' , 'label' => 'Extraparams' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 135 ),
);
2018-08-23 18:35:45 +02:00
/**
* @ var string ID to identify managed object
*/
2019-12-18 15:20:36 +01:00
public $element = 'fichinter' ;
2018-09-02 23:25:10 +02:00
2018-08-22 18:48:53 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-12-18 15:20:36 +01:00
public $table_element = 'fichinter' ;
2018-09-02 23:25:10 +02:00
2018-09-01 15:27:55 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Field with ID of parent key if this field has a parent
2018-09-01 15:27:55 +02:00
*/
2019-12-18 15:20:36 +01:00
public $fk_element = 'fk_fichinter' ;
2018-09-01 15:27:55 +02:00
2018-09-02 11:08:41 +02:00
/**
* @ var int Name of subtable line
*/
2019-12-18 15:20:36 +01:00
public $table_element_line = 'fichinterdet' ;
2018-09-02 11:08:41 +02:00
2018-09-05 12:21:13 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
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' ;
2020-11-19 20:50:58 +01:00
/**
* @ var int Thirdparty Id
*/
public $socid ;
2006-06-23 21:40:28 +02:00
2018-09-01 15:27:55 +02:00
public $author ;
2019-03-21 20:15:19 +01:00
2019-03-21 15:08:09 +01:00
/**
2020-10-31 14:32:18 +01:00
* Date creation record ( datec )
*
* @ var integer
*/
public $datec ;
2019-03-21 20:15:19 +01:00
2018-09-01 15:27:55 +02:00
public $datev ;
public $dateo ;
public $datee ;
public $datet ;
2019-03-21 20:15:19 +01:00
2019-03-21 15:08:09 +01:00
/**
2020-10-31 14:32:18 +01:00
* Date modification record ( tms )
*
* @ var integer
*/
public $datem ;
2019-03-21 20:15:19 +01:00
2020-11-19 20:50:58 +01:00
/**
* @ var int duration
*/
2018-09-01 15:27:55 +02:00
public $duration ;
2020-11-19 20:50:58 +01:00
/**
* @ var int status
*/
2019-12-18 15:20:36 +01:00
public $statut = 0 ; // 0=draft, 1=validated, 2=invoiced, 3=Terminate
2018-08-31 19:26:08 +02:00
/**
* @ var string description
*/
public $description ;
2006-06-23 21:40:28 +02:00
2018-10-10 12:16:20 +02:00
/**
2020-11-19 20:50:58 +01:00
* @ var int Contract ID
2020-10-31 14:32:18 +01:00
*/
2018-09-01 15:27:55 +02:00
public $fk_contrat = 0 ;
2018-10-10 12:16:20 +02:00
/**
2020-11-19 20:50:58 +01:00
* @ var int Project ID
2020-10-31 14:32:18 +01:00
*/
2018-09-01 15:27:55 +02:00
public $fk_project = 0 ;
2018-10-10 12:16:20 +02:00
2022-05-17 12:53:50 +02:00
/**
* Customer Ref
* @ var string
*/
public $ref_client ;
2020-11-19 20:50:58 +01:00
/**
* @ var array extraparams
*/
2019-12-18 15:20:36 +01:00
public $extraparams = array ();
2008-06-21 01:01:02 +02:00
2020-11-19 20:50:58 +01:00
/**
* @ var array lines
*/
2018-09-01 15:27:55 +02:00
public $lines = array ();
2008-06-21 01:01:02 +02:00
2017-07-25 21:22:22 +02:00
/**
* Draft status
*/
const STATUS_DRAFT = 0 ;
2018-09-02 11:08:41 +02:00
2017-07-25 21:22:22 +02:00
/**
* Validated status
*/
const STATUS_VALIDATED = 1 ;
2018-09-02 11:08:41 +02:00
2017-07-25 21:22:22 +02:00
/**
* Billed
*/
const STATUS_BILLED = 2 ;
2018-09-02 11:08:41 +02:00
2017-07-25 21:22:22 +02:00
/**
* Closed
*/
const STATUS_CLOSED = 3 ;
2020-11-28 14:47:39 +01:00
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
*/
2020-10-31 14:32:18 +01:00
public function __construct ( $db )
{
2012-07-30 17:17:33 +02:00
$this -> db = $db ;
2020-10-31 14:32:18 +01:00
}
2008-06-21 01:01:02 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-23 13:16:19 +02:00
/**
* Load indicators into this -> nb for board
*
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function load_state_board ()
2016-04-23 13:16:19 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-04-23 13:16:19 +02:00
global $user ;
2019-12-18 15:20:36 +01:00
$this -> nb = array ();
2016-04-23 13:16:19 +02:00
$clause = " WHERE " ;
$sql = " SELECT count(fi.rowid) as nb " ;
2019-12-18 15:20:36 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as fi " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON fi.fk_soc = s.rowid " ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $user -> socid ) {
2019-12-18 15:20:36 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON s.rowid = sc.fk_soc " ;
2021-08-23 17:41:11 +02:00
$sql .= " WHERE sc.fk_user = " . (( int ) $user -> id );
2016-04-23 13:16:19 +02:00
$clause = " AND " ;
}
2019-12-18 15:20:36 +01:00
$sql .= " " . $clause . " fi.entity IN ( " . getEntity ( 'intervention' ) . " ) " ;
2016-04-23 13:16:19 +02:00
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2020-04-12 16:36:25 +02:00
$this -> nb [ " interventions " ] = $obj -> nb ;
2016-04-23 13:16:19 +02:00
}
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2016-04-23 13:16:19 +02:00
dol_print_error ( $this -> db );
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2016-04-23 13:16:19 +02:00
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
*/
2020-10-31 14:32:18 +01:00
public 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
2020-10-31 14:32:18 +01:00
$error = 0 ;
2019-12-12 20:25:36 +01: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
2021-02-25 23:00:32 +01:00
if ( ! empty ( $this -> ref )) { // We check that ref is not already used
2019-12-18 15:20:36 +01:00
$result = self :: isExistingObject ( $this -> element , 0 , $this -> ref ); // Check ref is not yet used
2021-02-25 23:00:32 +01:00
if ( $result > 0 ) {
2019-12-18 15:20:36 +01:00
$this -> error = 'ErrorRefAlreadyExists' ;
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_WARNING );
2014-05-13 10:12:45 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2013-05-19 18:28:27 +02:00
}
2021-02-25 23:00:32 +01:00
if ( ! is_numeric ( $this -> duration )) {
$this -> duration = 0 ;
}
2022-05-17 12:53:50 +02:00
if ( isset ( $this -> ref_client )) {
$this -> ref_client = trim ( $this -> ref_client );
}
2014-05-13 10:12:45 +02:00
2021-02-25 23:00:32 +01:00
if ( $this -> socid <= 0 ) {
2022-03-18 10:13:19 +01:00
$this -> error = 'ErrorFicheinterCompanyDoesNotExist' ;
2019-01-27 11:55:16 +01: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 );
2019-12-18 15:20:36 +01:00
$result = $soc -> fetch ( $this -> socid );
2008-06-21 01:01:02 +02:00
2019-12-18 15:20:36 +01:00
$now = dol_now ();
2010-05-04 20:12:43 +02:00
$this -> db -> begin ();
2009-05-02 16:26:54 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " fichinter ( " ;
2019-12-18 15:20:36 +01:00
$sql .= " fk_soc " ;
$sql .= " , datec " ;
$sql .= " , ref " ;
2022-05-17 12:53:50 +02:00
$sql .= " , ref_client " ;
2019-12-18 15:20:36 +01:00
$sql .= " , entity " ;
$sql .= " , fk_user_author " ;
$sql .= " , fk_user_modif " ;
$sql .= " , description " ;
$sql .= " , model_pdf " ;
$sql .= " , fk_projet " ;
$sql .= " , fk_contrat " ;
$sql .= " , fk_statut " ;
$sql .= " , note_private " ;
$sql .= " , note_public " ;
$sql .= " ) " ;
$sql .= " VALUES ( " ;
$sql .= $this -> socid ;
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2022-05-18 22:24:30 +02:00
$sql .= " , " . ( $this -> ref_client ? " ' " . $this -> db -> escape ( $this -> ref_client ) . " ' " : " null " );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $conf -> entity );
$sql .= " , " . (( int ) $user -> id );
$sql .= " , " . (( int ) $user -> id );
2019-12-18 15:20:36 +01:00
$sql .= " , " . ( $this -> description ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " );
2020-09-10 01:49:09 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> model_pdf ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . ( $this -> fk_project ? (( int ) $this -> fk_project ) : 0 );
$sql .= " , " . ( $this -> fk_contrat ? (( int ) $this -> fk_contrat ) : 0 );
$sql .= " , " . (( int ) $this -> statut );
2019-12-18 15:20:36 +01:00
$sql .= " , " . ( $this -> note_private ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " );
$sql .= " , " . ( $this -> note_public ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " );
$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 );
2019-12-18 15:20:36 +01:00
$result = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $result ) {
2019-12-18 15:20:36 +01:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " fichinter " );
2013-05-19 11:18:45 +02:00
2021-02-25 23:00:32 +01:00
if ( $this -> id ) {
2019-12-18 15:20:36 +01:00
$this -> ref = '(PROV' . $this -> id . ')' ;
2021-03-14 12:20:23 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " fichinter SET ref=' " . $this -> db -> escape ( $this -> ref ) . " ' WHERE rowid= " . (( int ) $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 );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
$error ++ ;
}
2013-03-10 13:58:32 +01:00
}
2014-08-28 14:48:16 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2019-12-18 15:20:36 +01:00
$result = $this -> insertExtraFields ();
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2013-05-18 12:17:43 +02:00
// Add linked object
2021-02-25 23:00:32 +01:00
if ( ! $error && $this -> origin && $this -> origin_id ) {
2013-05-18 12:17:43 +02:00
$ret = $this -> add_object_linked ();
2021-02-25 23:00:32 +01:00
if ( ! $ret ) {
dol_print_error ( $this -> db );
}
2013-05-18 12:17:43 +02:00
}
2012-12-05 18:48:02 +01:00
2021-02-25 23:00:32 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'FICHINTER_CREATE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
2012-07-29 12:54:19 +02:00
2020-11-19 20:50:58 +01:00
if ( ! $error ) {
2012-12-30 17:13:43 +01:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2012-12-30 17:13:43 +01:00
$this -> db -> rollback ();
2019-12-18 15:20:36 +01:00
$this -> error = join ( ',' , $this -> errors );
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2012-12-30 17:13:43 +01:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2007-09-22 03:47:43 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
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
*/
2020-10-31 14:32:18 +01:00
public function update ( $user , $notrigger = 0 )
2008-06-21 01:01:02 +02:00
{
2020-10-31 14:32:18 +01:00
global $conf ;
2019-12-12 20:25:36 +01:00
2021-02-25 23:00:32 +01:00
if ( ! is_numeric ( $this -> duration )) {
$this -> duration = 0 ;
}
if ( ! dol_strlen ( $this -> fk_project )) {
$this -> fk_project = 0 ;
}
2022-05-17 12:53:50 +02:00
if ( isset ( $this -> ref_client )) {
$this -> ref_client = trim ( $this -> ref_client );
}
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
$error = 0 ;
2018-08-12 16:29:26 +02:00
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 " ;
2019-12-18 15:20:36 +01:00
$sql .= " description = ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
2021-06-09 15:36:47 +02:00
$sql .= " , duree = " . (( int ) $this -> duration );
2022-05-17 12:53:50 +02:00
$sql .= " , ref_client = " . ( $this -> ref_client ? " ' " . $this -> db -> escape ( $this -> ref_client ) . " ' " : " null " );
2021-06-09 15:36:47 +02:00
$sql .= " , fk_projet = " . (( int ) $this -> fk_project );
2019-12-18 15:20:36 +01: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 " );
2021-08-23 19:33:24 +02:00
$sql .= " , fk_user_modif = " . (( int ) $user -> id );
$sql .= " WHERE rowid = " . (( int ) $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 );
2021-02-25 23:00:32 +01:00
if ( $this -> db -> query ( $sql )) {
if ( ! $error ) {
2019-12-18 15:20:36 +01:00
$result = $this -> insertExtraFields ();
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
2018-08-12 16:29:26 +02:00
$error ++ ;
}
}
2013-05-20 21:21:03 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'FICHINTER_MODIFY' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ; $this -> db -> rollback (); return - 1 ;
}
2017-10-07 13:09:31 +02:00
// 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 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01: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
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $rowid , $ref = '' )
2008-06-21 01:01:02 +02:00
{
2022-05-17 12:53:50 +02:00
$sql = " SELECT f.rowid, f.ref, f.ref_client, f.description, f.fk_soc, f.fk_statut, " ;
2019-12-18 15:20:36 +01:00
$sql .= " f.datec, f.dateo, f.datee, f.datet, f.fk_user_author, " ;
$sql .= " f.date_valid as datev, " ;
$sql .= " f.tms as datem, " ;
2022-04-14 12:08:06 +02:00
$sql .= " f.duree, f.fk_projet as fk_project, f.note_public, f.note_private, f.model_pdf, f.extraparams, fk_contrat, f.entity as entity " ;
2019-12-18 15:20:36 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as f " ;
2018-03-22 16:18:11 +01:00
if ( $ref ) {
2019-12-18 15:20:36 +01:00
$sql .= " WHERE f.entity IN ( " . getEntity ( 'intervention' ) . " ) " ;
2022-01-03 18:15:19 +01:00
$sql .= " AND f.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2021-02-25 23:00:32 +01:00
} else {
2022-01-03 18:15:19 +01:00
$sql .= " WHERE f.rowid = " . (( int ) $rowid );
2021-02-25 23:00:32 +01:00
}
2005-09-07 23:47:16 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2008-06-21 01:01:02 +02:00
$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 ;
2022-05-17 12:53:50 +02:00
$this -> ref_client = $obj -> ref_client ;
2008-06-21 01:01:02 +02:00
$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 );
2019-02-27 15:32:18 +01:00
$this -> fk_project = $obj -> fk_project ;
2008-06-21 01:01:02 +02:00
$this -> note_public = $obj -> note_public ;
$this -> note_private = $obj -> note_private ;
2020-08-18 14:48:38 +02:00
$this -> model_pdf = $obj -> model_pdf ;
2020-10-31 14:32:18 +01:00
$this -> modelpdf = $obj -> model_pdf ; // deprecated
2019-02-25 20:35:59 +01:00
$this -> fk_contrat = $obj -> fk_contrat ;
2022-04-14 12:08:06 +02:00
$this -> entity = $obj -> entity ;
2012-03-18 01:25:08 +01:00
2019-02-25 20:35:59 +01:00
$this -> user_creation = $obj -> fk_user_author ;
2017-07-25 21:22:22 +02:00
2019-02-25 20:35:59 +01:00
$this -> extraparams = ( array ) json_decode ( $obj -> extraparams , true );
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( $this -> statut == 0 ) {
$this -> brouillon = 1 ;
}
2011-07-02 18:48:31 +02:00
2019-02-10 12:59:09 +01:00
// Retrieve 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
*/
2019-12-18 15:20:36 +01:00
$result = $this -> fetch_lines ();
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
2011-07-01 18:24:44 +02:00
return - 3 ;
}
2008-06-21 01:01:02 +02:00
$this -> db -> free ( $resql );
return 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +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
2019-03-21 17:37:56 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-06-21 01:01:02 +02:00
*/
2020-10-31 14:32:18 +01:00
public 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
2019-12-18 15:20:36 +01:00
$error = 0 ;
2019-03-21 17:37:56 +01:00
// Protection
2021-02-25 23:00:32 +01:00
if ( $this -> statut <= self :: STATUS_DRAFT ) {
2020-10-31 14:32:18 +01:00
return 0 ;
2019-03-21 17:37:56 +01:00
}
2009-03-12 23:49:05 +01:00
2019-03-21 17:37:56 +01:00
dol_syslog ( get_class ( $this ) . " ::setDraft " , LOG_DEBUG );
2009-03-12 23:49:05 +01:00
2019-03-21 17:37:56 +01:00
$this -> db -> begin ();
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2019-12-18 15:20:36 +01:00
$sql .= " SET fk_statut = " . self :: STATUS_DRAFT ;
2021-08-23 19:33:24 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2019-03-21 17:37:56 +01:00
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2020-10-31 14:32:18 +01:00
if ( ! $error ) {
$this -> oldcopy = clone $this ;
}
2019-03-21 17:37:56 +01:00
2020-10-31 14:32:18 +01:00
if ( ! $error ) {
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_UNVALIDATE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
}
2019-03-21 17:37:56 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$this -> statut = self :: STATUS_DRAFT ;
$this -> db -> commit ();
return 1 ;
2019-03-21 17:37:56 +01:00
} else {
2020-10-31 14:32:18 +01:00
$this -> db -> rollback ();
return - 1 ;
2008-07-12 14:45:29 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-03-21 17:37:56 +01:00
$this -> db -> rollback ();
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> lasterror ();
2019-03-21 17:37:56 +01:00
return - 1 ;
}
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
*/
2020-10-31 14:32:18 +01:00
public 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
2019-12-18 15:20:36 +01:00
$error = 0 ;
2012-01-04 23:56:10 +01:00
2021-02-25 23:00:32 +01:00
if ( $this -> statut != 1 ) {
2008-11-02 23:11:34 +01:00
$this -> db -> begin ();
2012-07-29 12:54:19 +02:00
2019-12-18 15:20:36 +01:00
$now = dol_now ();
2013-05-20 21:21:03 +02:00
2013-03-10 13:58:32 +01:00
// Define new ref
2021-02-25 23:00:32 +01: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-05-12 17:54:30 +02:00
$num = $this -> getNextNumRef ( $this -> thirdparty );
2020-05-21 15:05:19 +02:00
} else {
2013-03-10 13:58:32 +01:00
$num = $this -> ref ;
}
2020-03-03 11:49:17 +01:00
$this -> newref = dol_sanitizeFileName ( $num );
2009-03-12 23:49:05 +01:00
2008-11-02 23:11:34 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2019-12-18 15:20:36 +01:00
$sql .= " SET fk_statut = 1 " ;
2021-08-23 19:33:24 +02:00
$sql .= " , ref = ' " . $this -> db -> escape ( $num ) . " ' " ;
2019-12-18 15:20:36 +01:00
$sql .= " , date_valid = ' " . $this -> db -> idate ( $now ) . " ' " ;
2022-07-03 12:54:58 +02:00
$sql .= " , fk_user_valid = " . ( $user -> id > 0 ? ( int ) $user -> id : " null " );
2021-08-23 19:33:24 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
$sql .= " AND entity = " . (( int ) $conf -> entity );
2019-12-18 15:20:36 +01:00
$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 );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
2013-12-01 21:03:12 +01:00
dol_print_error ( $this -> db );
$error ++ ;
}
2021-02-25 23:00:32 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'FICHINTER_VALIDATE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
2014-10-27 03:24:45 +01:00
}
2021-02-25 23:00:32 +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
2021-02-25 23:00:32 +01:00
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref )) {
2019-07-28 22:26:55 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
// Now we rename also files into index
2019-12-18 15:20:36 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " ecm_files set filename = CONCAT(' " . $this -> db -> escape ( $this -> newref ) . " ', SUBSTR(filename, " . ( strlen ( $this -> ref ) + 1 ) . " )), filepath = 'ficheinter/ " . $this -> db -> escape ( $this -> newref ) . " ' " ;
$sql .= " WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' AND filepath = 'ficheinter/ " . $this -> db -> escape ( $this -> ref ) . " ' and entity = " . $conf -> entity ;
2019-07-28 22:26:55 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
$error ++ ; $this -> error = $this -> db -> lasterror ();
}
2019-07-28 22:26:55 +02:00
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
2013-12-01 21:03:12 +01:00
$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 ;
2021-02-25 23:00:32 +01:00
if ( ! $error && 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
2021-02-25 23:00:32 +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
2019-12-18 15:20:36 +01:00
$listoffiles = dol_dir_list ( $conf -> ficheinter -> dir_output . '/' . $newref , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
2021-02-25 23:00:32 +01:00
foreach ( $listoffiles as $fileentry ) {
2019-12-18 15:20:36 +01:00
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
2017-10-07 13:09:31 +02:00
@ rename ( $dirsource , $dirdest );
}
2013-12-01 21:03:12 +01:00
}
}
}
}
// Set new ref and define current statut
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2013-12-01 21:03:12 +01:00
$this -> ref = $num ;
2019-12-18 15:20:36 +01:00
$this -> statut = 1 ;
$this -> brouillon = 0 ;
$this -> date_validation = $now ;
2013-12-01 21:03:12 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2008-11-02 23:11:34 +01:00
$this -> db -> rollback ();
2019-01-27 11:55:16 +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
*/
2019-02-25 20:35:59 +01:00
public function getAmount ()
2017-09-11 14:41:46 +02:00
{
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
2019-12-18 15:20:36 +01:00
foreach ( $this -> lines as $line ) {
2017-09-11 14:41:46 +02:00
$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
2020-10-31 14:32:18 +01: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
*/
2019-01-27 15:20:16 +01:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
2017-09-09 20:00:09 +02:00
{
2020-12-13 13:34:21 +01:00
global $conf ;
2017-09-09 20:00:09 +02:00
2020-12-13 13:34:21 +01:00
$outputlangs -> load ( " interventions " );
2017-09-09 20:00:09 +02:00
2019-12-18 15:20:36 +01:00
if ( ! dol_strlen ( $modele )) {
2017-09-11 14:50:00 +02:00
$modele = 'soleil' ;
2017-09-09 20:00:09 +02:00
2020-12-13 13:34:21 +01:00
if ( ! empty ( $this -> model_pdf )) {
$modele = $this -> model_pdf ;
} elseif ( ! empty ( $this -> modelpdf )) { // deprecated
2017-09-09 20:00:09 +02:00
$modele = $this -> modelpdf ;
2019-12-18 15:20:36 +01:00
} elseif ( ! empty ( $conf -> global -> FICHEINTER_ADDON_PDF )) {
2017-09-11 14:50:00 +02:00
$modele = $conf -> global -> FICHEINTER_ADDON_PDF ;
2017-09-09 20:00:09 +02:00
}
}
$modelpath = " core/modules/fichinter/doc/ " ;
2019-01-27 11:55:16 +01: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
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode = 0 )
2008-06-21 01:01:02 +02:00
{
2020-04-20 02:48:05 +02:00
return $this -> LibStatut (( isset ( $this -> statut ) ? $this -> statut : $this -> status ), $mode );
2008-06-21 01:01:02 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-06-21 01:01:02 +02:00
/**
2019-10-24 16:10:00 +02:00
* Returns the label of a status
2011-12-19 17:29:09 +01:00
*
2019-10-24 16:10:00 +02:00
* @ param int $status Id status
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
*/
2019-10-24 16:10:00 +02:00
public function LibStatut ( $status , $mode = 0 )
2008-06-21 01:01:02 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2018-04-21 15:46:55 +02:00
// Init/load array of translation of status
2021-02-25 23:00:32 +01:00
if ( empty ( $this -> statuts ) || empty ( $this -> statuts_short ) || empty ( $this -> statuts_logo )) {
2018-04-21 15:46:55 +02:00
global $langs ;
$langs -> load ( " fichinter " );
2020-10-25 14:11:09 +01:00
$this -> statuts [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> statuts [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Validated' );
$this -> statuts [ self :: STATUS_BILLED ] = $langs -> transnoentitiesnoconv ( 'StatusInterInvoiced' );
$this -> statuts [ self :: STATUS_CLOSED ] = $langs -> transnoentitiesnoconv ( 'Done' );
$this -> statuts_short [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> statuts_short [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Validated' );
$this -> statuts_short [ self :: STATUS_BILLED ] = $langs -> transnoentitiesnoconv ( 'StatusInterInvoiced' );
$this -> statuts_short [ self :: STATUS_CLOSED ] = $langs -> transnoentitiesnoconv ( 'Done' );
2019-12-18 15:20:36 +01:00
$this -> statuts_logo [ self :: STATUS_DRAFT ] = 'status0' ;
$this -> statuts_logo [ self :: STATUS_VALIDATED ] = 'status1' ;
$this -> statuts_logo [ self :: STATUS_BILLED ] = 'status6' ;
$this -> statuts_logo [ self :: STATUS_CLOSED ] = 'status6' ;
2018-04-21 15:46:55 +02:00
}
2010-01-04 22:45:59 +01:00
2019-10-24 16:10:00 +02:00
return dolGetStatus ( $this -> statuts [ $status ], $this -> statuts_short [ $status ], '' , $this -> statuts_logo [ $status ], $mode );
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
*/
2019-02-25 20:35:59 +01:00
public 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
2019-12-18 15:20:36 +01:00
$result = '' ;
2017-10-03 14:50:52 +02:00
2020-11-02 11:41:07 +01:00
$label = img_picto ( '' , $this -> picto ) . ' <u class="paddingrightonly">' . $langs -> trans ( " Intervention " ) . '</u>' ;
2020-04-20 02:48:05 +02:00
if ( isset ( $this -> status )) {
2020-11-02 11:41:07 +01:00
$label .= ' ' . $this -> getLibStatut ( 5 );
2020-02-24 05:23:34 +01:00
}
2020-11-02 11:41:07 +01:00
$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
2021-02-25 23:00:32 +01:00
if ( $option !== 'nolink' ) {
2017-10-07 13:09:31 +02:00
// Add param to save lastsearch_values or not
2019-12-18 15:20:36 +01:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-25 23:00:32 +01:00
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
2019-12-18 15:20:36 +01:00
$linkclose = '' ;
2021-02-25 23:00:32 +01:00
if ( empty ( $notooltip )) {
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER )) {
2019-12-18 15:20:36 +01:00
$label = $langs -> trans ( " ShowIntervention " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
2017-10-03 14:50:52 +02:00
}
2019-12-18 15:20:36 +01:00
$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 . '"' ;
2019-12-18 15:20:36 +01:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2010-02-27 14:37:13 +01:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2021-02-25 23:00:32 +01:00
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 ;
}
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
2018-06-18 13:31:29 +02:00
global $action ;
2020-09-30 15:17:44 +02:00
$hookmanager -> initHooks ( array ( 'interventiondao' ));
2021-11-22 19:57:26 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2019-12-18 15:20:36 +01:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-25 23:00:32 +01:00
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2018-06-18 13:31:29 +02:00
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
*/
2019-02-25 20:35:59 +01:00
public function getNextNumRef ( $soc )
2008-06-21 01:01:02 +02:00
{
2009-03-23 20:13:51 +01:00
global $conf , $db , $langs ;
2008-06-21 01:01:02 +02:00
$langs -> load ( " interventions " );
2021-02-25 23:00:32 +01:00
if ( ! empty ( $conf -> global -> FICHEINTER_ADDON )) {
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)
2019-12-18 15:20:36 +01:00
$mybool |= @ include_once $dir . $file ;
2009-03-23 20:13:51 +01:00
}
2009-04-29 20:02:50 +02:00
2018-10-27 21:19:19 +02:00
if ( $mybool === false ) {
dol_print_error ( '' , " Failed to include file " . $file );
2015-01-17 18:58:05 +01:00
return '' ;
}
2008-06-21 01:01:02 +02:00
$obj = new $classname ();
$numref = " " ;
2018-10-27 21:19:19 +02:00
$numref = $obj -> getNextValue ( $soc , $this );
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( $numref != " " ) {
2008-06-21 01:01:02 +02:00
return $numref ;
2020-05-21 15:05:19 +02:00
} else {
2019-01-27 11:55:16 +01:00
dol_print_error ( $db , " Fichinter::getNextNumRef " . $obj -> error );
2008-06-21 01:01:02 +02:00
return " " ;
}
2020-05-21 15:05:19 +02:00
} 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
*/
2019-02-25 20:35:59 +01:00
public function info ( $id )
2008-02-24 18:01:48 +01:00
{
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, " ;
2019-12-18 15:20:36 +01:00
$sql .= " f.datec, " ;
$sql .= " f.tms as date_modification, " ;
$sql .= " f.date_valid as datev, " ;
$sql .= " f.fk_user_author, " ;
$sql .= " f.fk_user_modif as fk_user_modification, " ;
$sql .= " f.fk_user_valid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter as f " ;
2021-03-14 11:48:39 +01:00
$sql .= " WHERE f.rowid = " . (( int ) $id );
2008-06-21 01:01:02 +02:00
2013-11-05 14:24:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
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 );
2019-12-18 15:20:36 +01:00
$this -> user_creation = $cuser ;
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( $obj -> fk_user_valid ) {
2010-04-28 09:31:34 +02:00
$vuser = new User ( $this -> db );
$vuser -> fetch ( $obj -> fk_user_valid );
2019-12-18 15:20:36 +01:00
$this -> user_validation = $vuser ;
2008-06-21 01:01:02 +02:00
}
2021-02-25 23:00:32 +01:00
if ( $obj -> fk_user_modification ) {
2014-10-20 11:16:51 +02:00
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modification );
2019-12-18 15:20:36 +01:00
$this -> user_modification = $muser ;
2014-10-20 11:16:51 +02:00
}
2008-06-21 01:01:02 +02:00
}
2013-11-05 14:24:37 +01:00
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +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
*/
2019-02-25 20:35:59 +01:00
public function delete ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2019-12-18 15:20:36 +01: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
2019-12-18 15:20:36 +01: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
2021-02-25 23:00:32 +01:00
if ( ! $error && ! $notrigger ) {
2017-11-06 19:27:10 +01:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'FICHINTER_DELETE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ; $this -> db -> rollback (); return - 1 ;
}
2017-11-06 19:27:10 +01:00
// End call triggers
}
2011-07-01 18:24:44 +02:00
// Delete linked object
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-11-06 19:27:10 +01:00
$res = $this -> deleteObjectLinked ();
2021-02-25 23:00:32 +01:00
if ( $res < 0 ) {
$error ++ ;
}
2017-11-06 19:27:10 +01:00
}
2011-07-02 18:48:31 +02:00
2011-07-01 18:24:44 +02:00
// Delete linked contacts
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-11-06 19:27:10 +01:00
$res = $this -> delete_linked_contact ();
2021-02-25 23:00:32 +01:00
if ( $res < 0 ) {
2019-12-18 15:20:36 +01:00
$this -> error = 'ErrorFailToDeleteLinkedContact' ;
2017-11-06 19:27:10 +01:00
$error ++ ;
}
2011-07-01 18:24:44 +02:00
}
2011-07-02 18:48:31 +02:00
2020-11-01 21:52:34 +01:00
if ( ! $error ) {
$main = MAIN_DB_PREFIX . 'fichinterdet' ;
$ef = $main . " _extrafields " ;
2021-08-23 19:33:24 +02:00
$sql = " DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = " . (( int ) $this -> id ) . " ) " ;
2020-04-23 18:52:45 +02:00
2020-11-01 21:52:34 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
$error ++ ;
}
2020-04-23 18:55:36 +02:00
}
2020-04-23 18:52:45 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-11-06 19:27:10 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fichinterdet " ;
2021-08-23 19:33:24 +02:00
$sql .= " WHERE fk_fichinter = " . (( int ) $this -> id );
2017-11-06 19:27:10 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
$error ++ ;
}
2010-04-30 07:44:19 +02:00
}
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-11-06 19:27:10 +01:00
// Remove extrafields
$res = $this -> deleteExtraFields ();
2021-02-25 23:00:32 +01:00
if ( $res < 0 ) {
$error ++ ;
}
2017-11-06 19:27:10 +01:00
}
2009-07-07 18:19:37 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
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 " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $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 );
2021-02-25 23:00:32 +01:00
if ( ! $resql ) {
$error ++ ;
}
2017-11-06 19:27:10 +01:00
}
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2020-09-12 04:25:54 +02:00
// Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
$this -> deleteEcmFiles ();
2020-09-03 12:43:38 +02:00
2017-11-06 19:27:10 +01:00
// Remove directory with files
$fichinterref = dol_sanitizeFileName ( $this -> ref );
2021-02-25 23:00:32 +01:00
if ( $conf -> ficheinter -> dir_output ) {
2019-12-18 15:20:36 +01:00
$dir = $conf -> ficheinter -> dir_output . " / " . $fichinterref ;
$file = $conf -> ficheinter -> dir_output . " / " . $fichinterref . " / " . $fichinterref . " .pdf " ;
2021-02-25 23:00:32 +01:00
if ( file_exists ( $file )) {
2017-11-06 19:27:10 +01:00
dol_delete_preview ( $this );
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( ! dol_delete_file ( $file , 0 , 0 , 0 , $this )) { // For triggers
2018-09-25 00:39:28 +02:00
$langs -> load ( " errors " );
2019-12-18 15:20:36 +01:00
$this -> error = $langs -> trans ( " ErrorFailToDeleteFile " , $file );
2017-11-06 19:27:10 +01:00
return 0 ;
2007-08-28 09:44:17 +02:00
}
}
2021-02-25 23:00:32 +01:00
if ( file_exists ( $dir )) {
if ( ! dol_delete_dir_recursive ( $dir )) {
2018-09-25 00:39:28 +02:00
$langs -> load ( " errors " );
2019-12-18 15:20:36 +01:00
$this -> error = $langs -> trans ( " ErrorFailToDeleteDir " , $dir );
2017-11-06 19:27:10 +01:00
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
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-11-06 19:27:10 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +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
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-06-21 01:01:02 +02:00
/**
2019-02-25 23:15:48 +01:00
* Defines a delivery date of intervention
2011-12-19 17:29:09 +01:00
*
2019-02-25 23:15:48 +01:00
* @ param User $user Object user who define
2019-04-04 18:33:12 +02:00
* @ param integer $date_delivery date of delivery
2019-02-25 23:15:48 +01:00
* @ return int < 0 if ko , > 0 if ok
2020-10-31 14:32:18 +01:00
*/
public function set_date_delivery ( $user , $date_delivery )
{
// phpcs:enable
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2021-02-25 23:00:32 +01:00
if ( $user -> rights -> ficheinter -> creer ) {
2008-06-21 01:01:02 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2019-12-18 15:20:36 +01:00
$sql .= " SET datei = ' " . $this -> db -> idate ( $date_delivery ) . " ' " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2019-12-18 15:20:36 +01:00
$sql .= " AND fk_statut = 0 " ;
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( $this -> db -> query ( $sql )) {
2008-06-21 01:01:02 +02:00
$this -> date_delivery = $date_delivery ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$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 ;
}
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-06-21 01:01:02 +02:00
/**
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
*/
2020-10-31 14:32:18 +01:00
public function set_description ( $user , $description )
2008-06-21 01:01:02 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2009-05-02 16:26:54 +02:00
global $conf ;
2009-07-07 18:19:37 +02:00
2021-02-25 23:00:32 +01:00
if ( $user -> rights -> ficheinter -> creer ) {
2008-06-21 01:01:02 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2019-12-18 15:20:36 +01:00
$sql .= " SET description = ' " . $this -> db -> escape ( $description ) . " ', " ;
$sql .= " fk_user_modif = " . $user -> id ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2008-06-21 01:01:02 +02:00
2021-02-25 23:00:32 +01:00
if ( $this -> db -> query ( $sql )) {
2008-06-21 01:01:02 +02:00
$this -> description = $description ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$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
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*/
2020-10-31 14:32:18 +01:00
public function set_contrat ( $user , $contractid )
{
// phpcs:enable
2013-06-03 01:43:12 +02:00
global $conf ;
2021-02-25 23:00:32 +01:00
if ( $user -> rights -> ficheinter -> creer ) {
2013-06-03 01:43:12 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinter " ;
2020-09-20 02:30:53 +02:00
$sql .= " SET fk_contrat = " . (( int ) $contractid );
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2013-06-03 01:43:12 +02:00
2021-02-25 23:00:32 +01:00
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 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2013-06-03 01:43:12 +02:00
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
*
2019-04-25 12:11:32 +02:00
* @ param User $user User making the clone
* @ param int $socid Id of thirdparty
* @ return int New id of clone
2017-10-07 13:09:31 +02:00
*/
2019-04-25 12:11:32 +02:00
public function createFromClone ( User $user , $socid = 0 )
2017-10-07 13:09:31 +02:00
{
2019-04-25 12:11:32 +02:00
global $hookmanager ;
2016-07-01 18:12:37 +02:00
2019-12-18 15:20:36 +01:00
$error = 0 ;
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
2021-02-25 23:00:32 +01:00
foreach ( $this -> lines as $line ) {
2020-03-27 16:02:58 +01:00
$line -> fetch_optionals ();
2021-02-25 23:00:32 +01:00
}
2016-07-01 18:12:37 +02:00
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
2021-02-25 23:00:32 +01:00
if ( ! empty ( $socid ) && $socid != $this -> socid ) {
2017-10-07 13:09:31 +02:00
$objsoc = new Societe ( $this -> db );
2016-07-01 18:12:37 +02:00
2021-02-25 23:00:32 +01:00
if ( $objsoc -> fetch ( $socid ) > 0 ) {
2019-12-18 15:20:36 +01:00
$this -> socid = $objsoc -> id ;
2017-10-07 13:09:31 +02:00
//$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);
2019-12-18 15:20:36 +01:00
$this -> fk_project = '' ;
$this -> fk_delivery_address = '' ;
2017-10-07 13:09:31 +02:00
}
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
2019-12-18 15:20:36 +01:00
$this -> id = 0 ;
2016-07-01 18:12:37 +02:00
$this -> ref = '' ;
2019-12-18 15:20:36 +01:00
$this -> statut = 0 ;
2017-10-07 13:09:31 +02:00
// Clear fields
$this -> user_author_id = $user -> id ;
2022-07-03 12:54:58 +02:00
$this -> user_valid = 0 ;
2017-10-07 13:09:31 +02:00
$this -> date_creation = '' ;
$this -> date_validation = '' ;
$this -> ref_client = '' ;
// Create clone
2019-01-19 12:46:38 +01:00
$this -> context [ 'createfromclone' ] = 'createfromclone' ;
2019-12-18 15:20:36 +01:00
$result = $this -> create ( $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
// Add lines because it is not included into create function
2021-02-25 23:00:32 +01:00
foreach ( $this -> lines as $line ) {
2022-09-04 14:48:05 +02:00
$this -> addline ( $user , $this -> id , $line -> desc , $line -> datei , $line -> duration , $line -> array_options );
2017-10-07 13:09:31 +02:00
}
// Hook of thirdparty module
2021-02-25 23:00:32 +01:00
if ( is_object ( $hookmanager )) {
2019-12-18 15:20:36 +01:00
$parameters = array ( 'objFrom' => $objFrom );
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'createFrom' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-25 23:00:32 +01:00
if ( $reshook < 0 ) {
2023-01-18 09:53:28 +01:00
$this -> errors += $hookmanager -> errors ;
$this -> error = $hookmanager -> error ;
2021-02-25 23:00:32 +01:00
$error ++ ;
}
2017-10-07 13:09:31 +02:00
}
}
unset ( $this -> context [ 'createfromclone' ]);
// End
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$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
2019-04-04 18:33:12 +02:00
* @ param integer $date_intervention Intervention date
2011-12-19 17:29:09 +01:00
* @ 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
*/
2019-02-25 20:35:59 +01:00
public 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
2021-02-25 23:00:32 +01:00
if ( $this -> statut == 0 ) {
2008-06-21 01:01:02 +02:00
$this -> db -> begin ();
2009-03-12 23:49:05 +01:00
2008-06-21 01:01:02 +02:00
// Insertion ligne
2019-12-18 15:20:36 +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
2019-12-18 15:20:36 +01:00
if ( is_array ( $array_options ) && count ( $array_options ) > 0 ) {
$line -> array_options = $array_options ;
2014-08-26 17:08:03 +02:00
}
2019-12-18 15:20:36 +01:00
$result = $line -> insert ( $user );
2014-08-28 14:48:16 +02:00
2021-02-25 23:00:32 +01:00
if ( $result >= 0 ) {
2008-06-21 01:01:02 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2008-06-21 01:01:02 +02:00
$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
*/
2019-02-25 20:35:59 +01:00
public function initAsSpecimen ()
2007-08-29 09:55:34 +02:00
{
2019-12-18 15:20:36 +01:00
global $user , $langs , $conf ;
2007-08-29 09:55:34 +02:00
2019-12-18 15:20:36 +01:00
$now = dol_now ();
2007-08-29 09:55:34 +02:00
2007-12-08 15:06:01 +01:00
// Initialise parametres
2019-12-18 15:20:36 +01:00
$this -> id = 0 ;
2007-08-29 09:55:34 +02:00
$this -> ref = 'SPECIMEN' ;
2022-05-17 12:53:50 +02:00
$this -> ref_client = 'SPECIMEN CLIENT' ;
2019-12-18 15:20:36 +01:00
$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 ;
2019-12-18 15:20:36 +01:00
$this -> note_private = 'Private note' ;
$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 ;
2021-02-25 23:00:32 +01:00
while ( $xnbp < $nbp ) {
2019-12-18 15:20:36 +01:00
$line = new FichinterLigne ( $this -> db );
$line -> desc = $langs -> trans ( " Description " ) . " " . $xnbp ;
$line -> datei = ( $now - 3600 * ( 1 + $xnbp ));
$line -> duration = 600 ;
$line -> fk_fichinter = 0 ;
$this -> lines [ $xnbp ] = $line ;
2007-08-29 09:55:34 +02:00
$xnbp ++ ;
2011-09-12 20:43:01 +02:00
2019-12-18 15:20:36 +01: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
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*/
2020-10-31 14:32:18 +01:00
public function fetch_lines ()
{
// phpcs:enable
2018-02-08 12:41:31 +01:00
$this -> lines = array ();
2022-03-28 13:50:47 +02:00
$sql = " SELECT rowid, fk_fichinter, description, duree, date, rang " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinterdet " ;
$sql .= " WHERE fk_fichinter = " . (( int ) $this -> id );
$sql .= " 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 );
2022-03-28 13:50:47 +02:00
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
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 ;
2021-02-25 23:00:32 +01:00
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 ;
2019-11-02 14:32:25 +01:00
$line -> fk_fichinter = $objp -> fk_fichinter ;
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
2019-12-18 15:20:36 +01:00
$line -> qty = round ( $objp -> duree / 3600 , 2 );
2011-07-01 18:24:44 +02:00
$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 ;
2020-10-27 22:57:06 +01:00
$line -> fetch_optionals ();
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 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2008-06-21 01:01:02 +02:00
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 );
}
2022-05-17 12:53:50 +02:00
/**
* Set customer reference number
*
* @ param User $user Object user that modify
* @ param string $ref_client Customer reference
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
* @ return int < 0 if ko , > 0 if ok
*/
public function setRefClient ( $user , $ref_client , $notrigger = 0 )
{
// phpcs:enable
if ( ! empty ( $user -> rights -> ficheinter -> creer )) {
$error = 0 ;
$this -> db -> begin ();
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET ref_client = " . ( empty ( $ref_client ) ? 'NULL' : " ' " . $this -> db -> escape ( $ref_client ) . " ' " );
$sql .= " WHERE rowid = " . (( int ) $this -> id );
dol_syslog ( __METHOD__ . ' $this->id=' . $this -> id . ', ref_client=' . $ref_client , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = $this -> db -> error ();
$error ++ ;
}
if ( ! $error ) {
$this -> oldcopy = clone $this ;
$this -> ref_client = $ref_client ;
}
if ( ! $notrigger && empty ( $error )) {
// Call trigger
$result = $this -> call_trigger ( 'FICHINTER_MODIFY' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
if ( ! $error ) {
$this -> db -> commit ();
return 1 ;
} else {
foreach ( $this -> errors as $errmsg ) {
dol_syslog ( __METHOD__ . ' Error: ' . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
$this -> db -> rollback ();
return - 1 * $error ;
}
} else {
return - 1 ;
}
}
2008-06-21 01:01:02 +02:00
}
/**
2019-07-05 21:50:35 +02:00
* Class to manage intervention lines
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
{
2018-08-22 11:11:59 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-02 23:25:10 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2019-12-18 15:20:36 +01:00
public $error = '' ;
2007-08-28 09:44:17 +02:00
2008-06-21 01:01:02 +02:00
// From llx_fichinterdet
2018-10-10 12:16:20 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-01 15:27:55 +02:00
public $fk_fichinter ;
2018-10-10 12:16:20 +02:00
2022-06-14 18:14:24 +02:00
public $desc ; // Description ligne
/**
* @ var int Date of intervention
*/
public $date ; // Date intervention
/**
* @ var int Date of intervention
* @ deprecated
*/
public $datei ; // Date intervention
public $duration ; // Duration of intervention
2018-09-01 15:27:55 +02:00
public $rang = 0 ;
2007-08-28 09:44:17 +02:00
2018-08-23 18:35:45 +02:00
/**
* @ var string ID to identify managed object
*/
2019-12-18 15:20:36 +01:00
public $element = 'fichinterdet' ;
2018-09-02 23:25:10 +02:00
2018-08-22 18:48:53 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-12-18 15:20:36 +01:00
public $table_element = 'fichinterdet' ;
2018-09-02 23:25:10 +02:00
2018-09-01 15:27:55 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Field with ID of parent key if this field has a parent
2018-09-01 15:27:55 +02:00
*/
2019-12-18 15:20:36 +01:00
public $fk_element = 'fk_fichinter' ;
2014-08-28 14:48:16 +02:00
2022-05-19 12:08:03 +02:00
2007-08-28 09:44:17 +02:00
/**
2018-09-04 20:48:35 +02:00
* Constructor
2012-01-08 00:52:07 +01:00
*
2018-09-04 20:48:35 +02:00
* @ param DoliDB $db Database handler
2007-08-28 09:44:17 +02:00
*/
2019-02-25 20:35:59 +01:00
public 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
*/
2019-02-25 20:35:59 +01:00
public function fetch ( $rowid )
2007-08-28 09:44:17 +02:00
{
2007-11-04 22:21:33 +01:00
$sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,' ;
2019-12-18 15:20:36 +01:00
$sql .= ' ft.date as datei' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'fichinterdet as ft' ;
2021-03-29 15:32:09 +02:00
$sql .= ' WHERE ft.rowid = ' . (( int ) $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 );
2021-02-25 23:00:32 +01:00
if ( $result ) {
2007-08-28 09:44:17 +02:00
$objp = $this -> db -> fetch_object ( $result );
2007-11-04 22:21:33 +01:00
$this -> rowid = $objp -> rowid ;
2019-12-18 15:20:36 +01:00
$this -> id = $objp -> rowid ;
2007-11-04 22:21:33 +01:00
$this -> fk_fichinter = $objp -> fk_fichinter ;
2019-12-18 15:20:36 +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 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
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
* 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
*/
2019-02-25 20:35:59 +01:00
public function insert ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2019-12-18 15:20:36 +01:00
global $langs , $conf ;
2013-05-19 11:18:45 +02:00
2020-10-31 14:32:18 +01:00
$error = 0 ;
2019-12-12 20:25:36 +01: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
2019-12-18 15:20:36 +01:00
$rangToUse = $this -> rang ;
2021-02-25 23:00:32 +01:00
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' ;
2021-08-28 01:45:53 +02:00
$sql .= ' WHERE fk_fichinter = ' . (( int ) $this -> fk_fichinter );
2007-08-28 09:44:17 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2007-08-28 09:44:17 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$rangToUse = $obj -> max + 1 ;
2020-05-21 15:05:19 +02:00
} 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' ;
2019-12-18 15:20:36 +01:00
$sql .= ' (fk_fichinter, description, date, duree, rang)' ;
2021-08-28 01:45:53 +02:00
$sql .= " VALUES ( " . (( int ) $this -> fk_fichinter ) . " , " ;
2019-12-18 15:20:36 +01:00
$sql .= " ' " . $this -> db -> escape ( $this -> desc ) . " ', " ;
$sql .= " ' " . $this -> db -> idate ( $this -> datei ) . " ', " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( int ) $this -> duration ) . " , " ;
$sql .= ' ' . (( int ) $rangToUse );
2019-12-18 15:20:36 +01:00
$sql .= ')' ;
2007-08-28 09:44:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::insert " , LOG_DEBUG );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2019-12-18 15:20:36 +01:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'fichinterdet' );
$this -> rowid = $this -> id ;
2014-08-28 14:48:16 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2019-12-18 15:20:36 +01:00
$result = $this -> insertExtraFields ();
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2019-12-18 15:20:36 +01:00
$result = $this -> update_total ();
2014-08-28 14:48:16 +02:00
2021-02-25 23:00:32 +01:00
if ( $result > 0 ) {
2019-12-18 15:20:36 +01:00
$this -> rang = $rangToUse ;
2013-05-20 21:21:03 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'LINEFICHINTER_CREATE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// 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 ;
2020-05-21 15:05:19 +02:00
} else {
2007-11-04 22:21:33 +01:00
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2007-08-28 09:44:17 +02:00
$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
*/
2019-02-25 20:35:59 +01:00
public function update ( $user , $notrigger = 0 )
2007-08-28 09:44:17 +02:00
{
2019-12-18 15:20:36 +01:00
global $langs , $conf ;
2013-05-20 21:21:03 +02:00
2020-10-31 14:32:18 +01:00
$error = 0 ;
2019-12-12 20:25:36 +01:00
2020-10-31 14:32:18 +01:00
$this -> db -> begin ();
2007-08-28 09:44:17 +02:00
// Mise a jour ligne en base
$sql = " UPDATE " . MAIN_DB_PREFIX . " fichinterdet SET " ;
2019-12-18 15:20:36 +01:00
$sql .= " description=' " . $this -> db -> escape ( $this -> desc ) . " ' " ;
$sql .= " ,date=' " . $this -> db -> idate ( $this -> datei ) . " ' " ;
$sql .= " ,duree= " . $this -> duration ;
$sql .= " ,rang=' " . $this -> db -> escape ( $this -> rang ) . " ' " ;
2021-08-23 19:33:24 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2007-08-28 09:44:17 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::update " , LOG_DEBUG );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
if ( ! $error ) {
2019-12-18 15:20:36 +01:00
$result = $this -> insertExtraFields ();
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
}
}
2014-08-28 14:48:16 +02:00
2019-12-18 15:20:36 +01:00
$result = $this -> update_total ();
2021-02-25 23:00:32 +01:00
if ( $result > 0 ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2022-04-02 10:31:58 +02:00
$result = $this -> call_trigger ( 'LINEFICHINTER_MODIFY' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
2013-05-19 18:28:27 +02:00
}
}
2013-05-20 21:21:03 +02:00
2021-02-25 23:00:32 +01:00
if ( ! $error ) {
2007-11-04 22:21:33 +01:00
$this -> db -> commit ();
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> lasterror ();
2007-11-04 22:21:33 +01:00
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +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
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*/
2019-02-25 20:35:59 +01:00
public function update_total ()
2007-11-04 22:21:33 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
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 " ;
2019-12-18 15:20:36 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinterdet " ;
2021-03-30 03:37:54 +02:00
$sql .= " WHERE fk_fichinter= " . (( int ) $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 );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2019-12-18 15:20:36 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$total_duration = 0 ;
2021-02-25 23:00:32 +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 " ;
2021-03-30 03:37:54 +02:00
$sql .= " SET duree = " . (( int ) $total_duration );
2019-12-18 15:20:36 +01:00
$sql .= " , dateo = " . ( ! empty ( $obj -> dateo ) ? " ' " . $this -> db -> idate ( $obj -> dateo ) . " ' " : " null " );
$sql .= " , datee = " . ( ! empty ( $obj -> datee ) ? " ' " . $this -> db -> idate ( $obj -> datee ) . " ' " : " null " );
2021-03-30 03:37:54 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> fk_fichinter );
2007-11-04 22:21:33 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " FichinterLigne::update_total " , LOG_DEBUG );
2019-12-18 15:20:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2007-11-04 22:21:33 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2007-11-04 22:21:33 +01:00
$this -> db -> rollback ();
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error ();
2007-08-28 09:44:17 +02:00
$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
*/
2019-02-25 20:35:59 +01:00
public function deleteline ( $user , $notrigger = 0 )
2008-06-21 01:01:02 +02:00
{
2019-12-18 15:20:36 +01:00
global $langs , $conf ;
2013-05-20 21:21:03 +02:00
2019-12-18 15:20:36 +01:00
$error = 0 ;
2017-10-07 20:21:47 +02:00
2021-02-25 23:00:32 +01: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
2020-10-31 14:32:18 +01:00
$result = $this -> deleteExtraFields ();
2020-04-23 18:55:36 +02:00
if ( $result < 0 ) {
$error ++ ;
$this -> db -> rollback ();
return - 1 ;
}
2020-04-23 18:52:45 +02:00
2021-08-23 19:33:24 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fichinterdet WHERE rowid = " . (( int ) $this -> id );
2008-06-21 01:01:02 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-25 23:00:32 +01:00
if ( $resql ) {
2008-06-21 01:01:02 +02:00
$result = $this -> update_total ();
2021-02-25 23:00:32 +01:00
if ( $result > 0 ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-12-18 15:20:36 +01:00
$result = $this -> call_trigger ( 'LINEFICHINTER_DELETE' , $user );
2021-02-25 23:00:32 +01:00
if ( $result < 0 ) {
$error ++ ; $this -> db -> rollback (); return - 1 ;
}
2017-10-07 13:09:31 +02:00
// 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 ;
2020-05-21 15:05:19 +02:00
} else {
2008-06-21 01:01:02 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-18 15:20:36 +01:00
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2008-06-21 01:01:02 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2008-06-21 01:01:02 +02:00
return - 2 ;
}
}
2007-08-28 09:44:17 +02:00
}