12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718 |
- <?php
- /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
- * Copyright (C) 2011-2020 Juanjo Menent <jmenent@2byte.es>
- * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
- * Copyright (C) 2015-2020 Charlene Benke <charlie@patas-monkey.com>
- * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
- * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
- /**
- * \file htdocs/fichinter/class/fichinter.class.php
- * \ingroup ficheinter
- * \brief Fichier de la classe des gestion des fiches interventions
- */
- require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
- require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
- /**
- * Class to manage interventions
- */
- class Fichinter extends CommonObject
- {
- public $fields = array(
- 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
- 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
- 'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
- 'fk_contrat' =>array('type'=>'integer', 'label'=>'Fk contrat', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
- '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),
- '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),
- 'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
- '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),
- '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),
- );
- /**
- * @var string ID to identify managed object
- */
- public $element = 'fichinter';
- /**
- * @var string Name of table without prefix where object is stored
- */
- public $table_element = 'fichinter';
- /**
- * @var string Field with ID of parent key if this field has a parent
- */
- public $fk_element = 'fk_fichinter';
- /**
- * @var int Name of subtable line
- */
- public $table_element_line = 'fichinterdet';
- /**
- * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
- */
- public $picto = 'intervention';
- /**
- * {@inheritdoc}
- */
- protected $table_ref_field = 'ref';
- /**
- * @var int Thirdparty Id
- */
- public $socid;
- public $author;
- /**
- * Date creation record (datec)
- *
- * @var integer
- */
- public $datec;
- public $datev;
- public $dateo;
- public $datee;
- public $datet;
- /**
- * Date modification record (tms)
- *
- * @var integer
- */
- public $datem;
- /**
- * @var int duration
- */
- public $duration;
- /**
- * @var int status
- */
- public $statut = 0; // 0=draft, 1=validated, 2=invoiced, 3=Terminate
- /**
- * @var string description
- */
- public $description;
- /**
- * @var int Contract ID
- */
- public $fk_contrat = 0;
- /**
- * @var int Project ID
- */
- public $fk_project = 0;
- /**
- * @var array extraparams
- */
- public $extraparams = array();
- /**
- * @var array lines
- */
- public $lines = array();
- /**
- * Draft status
- */
- const STATUS_DRAFT = 0;
- /**
- * Validated status
- */
- const STATUS_VALIDATED = 1;
- /**
- * Billed
- */
- const STATUS_BILLED = 2;
- /**
- * Closed
- */
- const STATUS_CLOSED = 3;
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Load indicators into this->nb for board
- *
- * @return int <0 if KO, >0 if OK
- */
- public function load_state_board()
- {
- // phpcs:enable
- 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 (empty($user->rights->societe->client->voir) && !$user->socid) {
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
- $sql .= " WHERE sc.fk_user = ".((int) $user->id);
- $clause = "AND";
- }
- $sql .= " ".$clause." fi.entity IN (".getEntity('intervention').")";
- $resql = $this->db->query($sql);
- if ($resql) {
- while ($obj = $this->db->fetch_object($resql)) {
- $this->nb["interventions"] = $obj->nb;
- }
- $this->db->free($resql);
- return 1;
- } else {
- dol_print_error($this->db);
- $this->error = $this->db->error();
- return -1;
- }
- }
- /**
- * Create an intervention into data base
- *
- * @param User $user Objet user that make creation
- * @param int $notrigger Disable all triggers
- * @return int <0 if KO, >0 if OK
- */
- public function create($user, $notrigger = 0)
- {
- global $conf, $langs;
- $error = 0;
- dol_syslog(get_class($this)."::create ref=".$this->ref);
- // Check parameters
- 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;
- }
- }
- if (!is_numeric($this->duration)) {
- $this->duration = 0;
- }
- if ($this->socid <= 0) {
- $this->error = 'ErrorFicheinterCompanyDoesNotExist';
- dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
- return -1;
- }
- $soc = new Societe($this->db);
- $result = $soc->fetch($this->socid);
- $now = dol_now();
- $this->db->begin();
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter (";
- $sql .= "fk_soc";
- $sql .= ", datec";
- $sql .= ", ref";
- $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)."'";
- $sql .= ", ".((int) $conf->entity);
- $sql .= ", ".((int) $user->id);
- $sql .= ", ".((int) $user->id);
- $sql .= ", ".($this->description ? "'".$this->db->escape($this->description)."'" : "null");
- $sql .= ", '".$this->db->escape($this->model_pdf)."'";
- $sql .= ", ".($this->fk_project ? ((int) $this->fk_project) : 0);
- $sql .= ", ".($this->fk_contrat ? ((int) $this->fk_contrat) : 0);
- $sql .= ", ".((int) $this->statut);
- $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
- $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
- $sql .= ")";
- dol_syslog(get_class($this)."::create", LOG_DEBUG);
- $result = $this->db->query($sql);
- if ($result) {
- $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."fichinter");
- if ($this->id) {
- $this->ref = '(PROV'.$this->id.')';
- $sql = 'UPDATE '.MAIN_DB_PREFIX."fichinter SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
- dol_syslog(get_class($this)."::create", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- }
- }
- if (!$error) {
- $result = $this->insertExtraFields();
- if ($result < 0) {
- $error++;
- }
- }
- // Add linked object
- if (!$error && $this->origin && $this->origin_id) {
- $ret = $this->add_object_linked();
- if (!$ret) {
- dol_print_error($this->db);
- }
- }
- if (!$error && !$notrigger) {
- // Call trigger
- $result = $this->call_trigger('FICHINTER_CREATE', $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- }
- 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;
- }
- } else {
- $this->error = $this->db->error();
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Update an intervention
- *
- * @param User $user Objet user that make creation
- * @param int $notrigger Disable all triggers
- * @return int <0 if KO, >0 if OK
- */
- public function update($user, $notrigger = 0)
- {
- global $conf;
- if (!is_numeric($this->duration)) {
- $this->duration = 0;
- }
- if (!dol_strlen($this->fk_project)) {
- $this->fk_project = 0;
- }
- $error = 0;
- $this->db->begin();
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter SET ";
- $sql .= "description = '".$this->db->escape($this->description)."'";
- $sql .= ", duree = ".((int) $this->duration);
- $sql .= ", fk_projet = ".((int) $this->fk_project);
- $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");
- $sql .= ", fk_user_modif = ".((int) $user->id);
- $sql .= " WHERE rowid = ".((int) $this->id);
- dol_syslog(get_class($this)."::update", LOG_DEBUG);
- if ($this->db->query($sql)) {
- if (!$error) {
- $result = $this->insertExtraFields();
- if ($result < 0) {
- $error++;
- }
- }
- if (!$error && !$notrigger) {
- // Call trigger
- $result = $this->call_trigger('FICHINTER_MODIFY', $user);
- if ($result < 0) {
- $error++; $this->db->rollback(); return -1;
- }
- // End call triggers
- }
- $this->db->commit();
- return 1;
- } else {
- $this->error = $this->db->error();
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Fetch a intervention
- *
- * @param int $rowid Id of intervention
- * @param string $ref Ref of intervention
- * @return int <0 if KO, >0 if OK
- */
- public function fetch($rowid, $ref = '')
- {
- $sql = "SELECT f.rowid, f.ref, f.description, f.fk_soc, f.fk_statut,";
- $sql .= " f.datec, f.dateo, f.datee, f.datet, f.fk_user_author,";
- $sql .= " f.date_valid as datev,";
- $sql .= " f.tms as datem,";
- $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";
- $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
- if ($ref) {
- $sql .= " WHERE f.entity IN (".getEntity('intervention').")";
- $sql .= " AND f.ref = '".$this->db->escape($ref)."'";
- } else {
- $sql .= " WHERE f.rowid = ".((int) $rowid);
- }
- dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- if ($this->db->num_rows($resql)) {
- $obj = $this->db->fetch_object($resql);
- $this->id = $obj->rowid;
- $this->ref = $obj->ref;
- $this->description = $obj->description;
- $this->socid = $obj->fk_soc;
- $this->statut = $obj->fk_statut;
- $this->duration = $obj->duree;
- $this->datec = $this->db->jdate($obj->datec);
- $this->dateo = $this->db->jdate($obj->dateo);
- $this->datee = $this->db->jdate($obj->datee);
- $this->datet = $this->db->jdate($obj->datet);
- $this->datev = $this->db->jdate($obj->datev);
- $this->datem = $this->db->jdate($obj->datem);
- $this->fk_project = $obj->fk_project;
- $this->note_public = $obj->note_public;
- $this->note_private = $obj->note_private;
- $this->model_pdf = $obj->model_pdf;
- $this->modelpdf = $obj->model_pdf; // deprecated
- $this->fk_contrat = $obj->fk_contrat;
- $this->entity = $obj->entity;
- $this->user_creation = $obj->fk_user_author;
- $this->extraparams = (array) json_decode($obj->extraparams, true);
- if ($this->statut == 0) {
- $this->brouillon = 1;
- }
- // Retrieve extrafields
- $this->fetch_optionals();
- /*
- * Lines
- */
- $result = $this->fetch_lines();
- if ($result < 0) {
- return -3;
- }
- $this->db->free($resql);
- return 1;
- }
- } else {
- $this->error = $this->db->lasterror();
- return -1;
- }
- }
- /**
- * Set status to draft
- *
- * @param User $user User that set draft
- * @return int <0 if KO, >0 if OK
- */
- public function setDraft($user)
- {
- global $langs, $conf;
- $error = 0;
- // Protection
- if ($this->statut <= self::STATUS_DRAFT) {
- return 0;
- }
- dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
- $this->db->begin();
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
- $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
- $sql .= " WHERE rowid = ".((int) $this->id);
- $resql = $this->db->query($sql);
- if ($resql) {
- if (!$error) {
- $this->oldcopy = clone $this;
- }
- if (!$error) {
- // Call trigger
- $result = $this->call_trigger('FICHINTER_UNVALIDATE', $user);
- if ($result < 0) {
- $error++;
- }
- }
- if (!$error) {
- $this->statut = self::STATUS_DRAFT;
- $this->db->commit();
- return 1;
- } else {
- $this->db->rollback();
- return -1;
- }
- } else {
- $this->db->rollback();
- $this->error = $this->db->lasterror();
- return -1;
- }
- }
- /**
- * Validate a intervention
- *
- * @param User $user User that validate
- * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
- * @return int <0 if KO, >0 if OK
- */
- public function setValid($user, $notrigger = 0)
- {
- global $conf;
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- $error = 0;
- if ($this->statut != 1) {
- $this->db->begin();
- $now = dol_now();
- // Define new ref
- if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
- $num = $this->getNextNumRef($this->thirdparty);
- } else {
- $num = $this->ref;
- }
- $this->newref = dol_sanitizeFileName($num);
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
- $sql .= " SET fk_statut = 1";
- $sql .= ", ref = '".$this->db->escape($num)."'";
- $sql .= ", date_valid = '".$this->db->idate($now)."'";
- $sql .= ", fk_user_valid = ".((int) $user->id);
- $sql .= " WHERE rowid = ".((int) $this->id);
- $sql .= " AND entity = ".((int) $conf->entity);
- $sql .= " AND fk_statut = 0";
- dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- dol_print_error($this->db);
- $error++;
- }
- if (!$error && !$notrigger) {
- // Call trigger
- $result = $this->call_trigger('FICHINTER_VALIDATE', $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- }
- if (!$error) {
- $this->oldref = $this->ref;
- // Rename directory if dir was a temporary ref
- if (preg_match('/^[\(]?PROV/i', $this->ref)) {
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- // Now we rename also files into index
- $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;
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++; $this->error = $this->db->lasterror();
- }
- // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
- $oldref = dol_sanitizeFileName($this->ref);
- $newref = dol_sanitizeFileName($num);
- $dirsource = $conf->ficheinter->dir_output.'/'.$oldref;
- $dirdest = $conf->ficheinter->dir_output.'/'.$newref;
- if (!$error && file_exists($dirsource)) {
- dol_syslog(get_class($this)."::setValid rename dir ".$dirsource." into ".$dirdest);
- if (@rename($dirsource, $dirdest)) {
- 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);
- }
- }
- }
- }
- }
- // Set new ref and define current statut
- if (!$error) {
- $this->ref = $num;
- $this->statut = 1;
- $this->brouillon = 0;
- $this->date_validation = $now;
- $this->db->commit();
- return 1;
- } else {
- $this->db->rollback();
- dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
- return -1;
- }
- }
- }
- /**
- * Returns amount based on user thm
- *
- * @return float Amount
- */
- public function getAmount()
- {
- global $db;
- $amount = 0;
- $this->author = new User($db);
- $this->author->fetch($this->user_creation);
- $thm = $this->author->thm;
- foreach ($this->lines as $line) {
- $amount += ($line->duration / 60 / 60 * $thm);
- }
- return price2num($amount, 'MT');
- }
- /**
- * 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
- * @param null|array $moreparams Array to provide more information
- * @return int 0 if KO, 1 if OK
- */
- public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
- {
- global $conf;
- $outputlangs->load("interventions");
- if (!dol_strlen($modele)) {
- $modele = 'soleil';
- if (!empty($this->model_pdf)) {
- $modele = $this->model_pdf;
- } elseif (!empty($this->modelpdf)) { // deprecated
- $modele = $this->modelpdf;
- } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
- $modele = $conf->global->FICHEINTER_ADDON_PDF;
- }
- }
- $modelpath = "core/modules/fichinter/doc/";
- return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
- }
- /**
- * Returns the label status
- *
- * @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
- */
- public function getLibStatut($mode = 0)
- {
- return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Returns the label of a status
- *
- * @param int $status Id status
- * @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
- * @return string Label
- */
- public function LibStatut($status, $mode = 0)
- {
- // phpcs:enable
- // Init/load array of translation of status
- if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) {
- global $langs;
- $langs->load("fichinter");
- $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');
- $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';
- }
- return dolGetStatus($this->statuts[$status], $this->statuts_short[$status], '', $this->statuts_logo[$status], $mode);
- }
- /**
- * Return clicable name (with picto eventually)
- *
- * @param int $withpicto 0=_No picto, 1=Includes the picto in the linkn, 2=Picto only
- * @param string $option Options
- * @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
- * @return string String with URL
- */
- public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1)
- {
- global $conf, $langs, $hookmanager;
- $result = '';
- $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
- if (isset($this->status)) {
- $label .= ' '.$this->getLibStatut(5);
- }
- $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
- $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
- 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';
- }
- }
- $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"';
- /*
- $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;
- */
- }
- $linkstart = '<a href="'.$url.'"';
- $linkstart .= $linkclose.'>';
- $linkend = '</a>';
- $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;
- global $action;
- $hookmanager->initHooks(array('interventiondao'));
- $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;
- }
- return $result;
- }
- /**
- * Returns the next non used reference of intervention
- * depending on the module numbering assets within FICHEINTER_ADDON
- *
- * @param Societe $soc Thirdparty object
- * @return string Free reference for intervention
- */
- public function getNextNumRef($soc)
- {
- global $conf, $db, $langs;
- $langs->load("interventions");
- if (!empty($conf->global->FICHEINTER_ADDON)) {
- $mybool = false;
- $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
- $classname = "mod_".$conf->global->FICHEINTER_ADDON;
- // 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;
- }
- if ($mybool === false) {
- dol_print_error('', "Failed to include file ".$file);
- return '';
- }
- $obj = new $classname();
- $numref = "";
- $numref = $obj->getNextValue($soc, $this);
- if ($numref != "") {
- return $numref;
- } else {
- dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
- return "";
- }
- } else {
- $langs->load("errors");
- print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
- return "";
- }
- }
- /**
- * Load information on object
- *
- * @param int $id Id of object
- * @return void
- */
- public function info($id)
- {
- global $conf;
- $sql = "SELECT f.rowid,";
- $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";
- $sql .= " WHERE f.rowid = ".((int) $id);
- $resql = $this->db->query($sql);
- if ($resql) {
- if ($this->db->num_rows($resql)) {
- $obj = $this->db->fetch_object($resql);
- $this->id = $obj->rowid;
- $this->date_creation = $this->db->jdate($obj->datec);
- $this->date_modification = $this->db->jdate($obj->date_modification);
- $this->date_validation = $this->db->jdate($obj->datev);
- $cuser = new User($this->db);
- $cuser->fetch($obj->fk_user_author);
- $this->user_creation = $cuser;
- if ($obj->fk_user_valid) {
- $vuser = new User($this->db);
- $vuser->fetch($obj->fk_user_valid);
- $this->user_validation = $vuser;
- }
- if ($obj->fk_user_modification) {
- $muser = new User($this->db);
- $muser->fetch($obj->fk_user_modification);
- $this->user_modification = $muser;
- }
- }
- $this->db->free($resql);
- } else {
- dol_print_error($this->db);
- }
- }
- /**
- * Delete intervetnion
- *
- * @param User $user Object user who delete
- * @param int $notrigger Disable trigger
- * @return int <0 if KO, >0 if OK
- */
- public function delete($user, $notrigger = 0)
- {
- global $conf, $langs;
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- $error = 0;
- $this->db->begin();
- if (!$error && !$notrigger) {
- // Call trigger
- $result = $this->call_trigger('FICHINTER_DELETE', $user);
- if ($result < 0) {
- $error++; $this->db->rollback(); return -1;
- }
- // End call triggers
- }
- // Delete linked object
- if (!$error) {
- $res = $this->deleteObjectLinked();
- if ($res < 0) {
- $error++;
- }
- }
- // Delete linked contacts
- if (!$error) {
- $res = $this->delete_linked_contact();
- if ($res < 0) {
- $this->error = 'ErrorFailToDeleteLinkedContact';
- $error++;
- }
- }
- if (!$error) {
- $main = MAIN_DB_PREFIX.'fichinterdet';
- $ef = $main."_extrafields";
- $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- }
- }
- if (!$error) {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
- $sql .= " WHERE fk_fichinter = ".((int) $this->id);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- }
- }
- if (!$error) {
- // Remove extrafields
- $res = $this->deleteExtraFields();
- if ($res < 0) {
- $error++;
- }
- }
- if (!$error) {
- // Delete object
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
- $sql .= " WHERE rowid = ".((int) $this->id);
- dol_syslog("Fichinter::delete", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- }
- }
- if (!$error) {
- // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
- $this->deleteEcmFiles();
- // 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)) {
- dol_delete_preview($this);
- if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
- $langs->load("errors");
- $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
- return 0;
- }
- }
- if (file_exists($dir)) {
- if (!dol_delete_dir_recursive($dir)) {
- $langs->load("errors");
- $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
- return 0;
- }
- }
- }
- }
- if (!$error) {
- $this->db->commit();
- return 1;
- } else {
- $this->db->rollback();
- return -1;
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Defines a delivery date of intervention
- *
- * @param User $user Object user who define
- * @param integer $date_delivery date of delivery
- * @return int <0 if ko, >0 if ok
- */
- public function set_date_delivery($user, $date_delivery)
- {
- // phpcs:enable
- global $conf;
- if ($user->rights->ficheinter->creer) {
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
- $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
- $sql .= " WHERE rowid = ".((int) $this->id);
- $sql .= " AND fk_statut = 0";
- if ($this->db->query($sql)) {
- $this->date_delivery = $date_delivery;
- return 1;
- } else {
- $this->error = $this->db->error();
- dol_syslog("Fichinter::set_date_delivery Erreur SQL");
- return -1;
- }
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Define the label of the intervention
- *
- * @param User $user Object user who modify
- * @param string $description description
- * @return int <0 if KO, >0 if OK
- */
- public function set_description($user, $description)
- {
- // phpcs:enable
- global $conf;
- if ($user->rights->ficheinter->creer) {
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
- $sql .= " SET description = '".$this->db->escape($description)."',";
- $sql .= " fk_user_modif = ".$user->id;
- $sql .= " WHERE rowid = ".((int) $this->id);
- if ($this->db->query($sql)) {
- $this->description = $description;
- return 1;
- } else {
- $this->error = $this->db->error();
- dol_syslog("Fichinter::set_description Erreur SQL");
- return -1;
- }
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Link intervention to a contract
- *
- * @param User $user Object user who modify
- * @param int $contractid Description
- * @return int <0 if ko, >0 if ok
- */
- public function set_contrat($user, $contractid)
- {
- // phpcs:enable
- global $conf;
- if ($user->rights->ficheinter->creer) {
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
- $sql .= " SET fk_contrat = ".((int) $contractid);
- $sql .= " WHERE rowid = ".((int) $this->id);
- if ($this->db->query($sql)) {
- $this->fk_contrat = $contractid;
- return 1;
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
- return -2;
- }
- /**
- * Load an object from its id and create a new one in database
- *
- * @param User $user User making the clone
- * @param int $socid Id of thirdparty
- * @return int New id of clone
- */
- public function createFromClone(User $user, $socid = 0)
- {
- global $hookmanager;
- $error = 0;
- $this->db->begin();
- // get extrafields so they will be clone
- foreach ($this->lines as $line) {
- $line->fetch_optionals();
- }
- // Load source object
- $objFrom = clone $this;
- // Change socid if needed
- if (!empty($socid) && $socid != $this->socid) {
- $objsoc = new Societe($this->db);
- 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 = '';
- }
- // TODO Change product price if multi-prices
- }
- $this->id = 0;
- $this->ref = '';
- $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
- $this->context['createfromclone'] = 'createfromclone';
- $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;
- }
- }
- /**
- * Adding a line of intervention into data base
- *
- * @param user $user User that do the action
- * @param int $fichinterid Id of intervention
- * @param string $desc Line description
- * @param integer $date_intervention Intervention date
- * @param int $duration Intervention duration
- * @param array $array_options Array option
- * @return int >0 if ok, <0 if ko
- */
- public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
- {
- dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
- if ($this->statut == 0) {
- $this->db->begin();
- // Insertion ligne
- $line = new FichinterLigne($this->db);
- $line->fk_fichinter = $fichinterid;
- $line->desc = $desc;
- $line->datei = $date_intervention;
- $line->duration = $duration;
- if (is_array($array_options) && count($array_options) > 0) {
- $line->array_options = $array_options;
- }
- $result = $line->insert($user);
- if ($result >= 0) {
- $this->db->commit();
- return 1;
- } else {
- $this->error = $this->db->error();
- $this->db->rollback();
- return -1;
- }
- }
- }
- /**
- * 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
- */
- public function initAsSpecimen()
- {
- global $user, $langs, $conf;
- $now = dol_now();
- // Initialise parametres
- $this->id = 0;
- $this->ref = 'SPECIMEN';
- $this->specimen = 1;
- $this->socid = 1;
- $this->datec = $now;
- $this->note_private = 'Private note';
- $this->note_public = 'SPECIMEN';
- $this->duration = 0;
- $nbp = 25;
- $xnbp = 0;
- while ($xnbp < $nbp) {
- $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;
- $xnbp++;
- $this->duration += $line->duration;
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Load array lines ->lines
- *
- * @return int <0 if KO, >0 if OK
- */
- public function fetch_lines()
- {
- // phpcs:enable
- $this->lines = array();
- $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";
- dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $objp = $this->db->fetch_object($resql);
- $line = new FichinterLigne($this->db);
- $line->id = $objp->rowid;
- $line->fk_fichinter = $objp->fk_fichinter;
- $line->desc = $objp->description;
- $line->duration = $objp->duree;
- //For invoicing we calculing hours
- $line->qty = round($objp->duree / 3600, 2);
- $line->date = $this->db->jdate($objp->date);
- $line->datei = $this->db->jdate($objp->date);
- $line->rang = $objp->rang;
- $line->product_type = 1;
- $line->fetch_optionals();
- $this->lines[$i] = $line;
- $i++;
- }
- $this->db->free($resql);
- return 1;
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
- /**
- * 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);
- }
- }
- /**
- * Class to manage intervention lines
- */
- class FichinterLigne extends CommonObjectLine
- {
- /**
- * @var DoliDB Database handler.
- */
- public $db;
- /**
- * @var string Error code (or message)
- */
- public $error = '';
- // From llx_fichinterdet
- /**
- * @var int ID
- */
- public $fk_fichinter;
- public $desc; // Description ligne
- public $datei; // Date intervention
- public $duration; // Duree de l'intervention
- public $rang = 0;
- /**
- * @var string ID to identify managed object
- */
- public $element = 'fichinterdet';
- /**
- * @var string Name of table without prefix where object is stored
- */
- public $table_element = 'fichinterdet';
- /**
- * @var string Field with ID of parent key if this field has a parent
- */
- public $fk_element = 'fk_fichinter';
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * Retrieve the line of intervention
- *
- * @param int $rowid Line id
- * @return int <0 if KO, >0 if OK
- */
- public function fetch($rowid)
- {
- $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,';
- $sql .= ' ft.date as datei';
- $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
- $sql .= ' WHERE ft.rowid = '.((int) $rowid);
- dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
- $result = $this->db->query($sql);
- if ($result) {
- $objp = $this->db->fetch_object($result);
- $this->rowid = $objp->rowid;
- $this->id = $objp->rowid;
- $this->fk_fichinter = $objp->fk_fichinter;
- $this->datei = $this->db->jdate($objp->datei);
- $this->desc = $objp->description;
- $this->duration = $objp->duree;
- $this->rang = $objp->rang;
- $this->db->free($result);
- return 1;
- } else {
- $this->error = $this->db->error().' sql='.$sql;
- return -1;
- }
- }
- /**
- * Insert the line into database
- *
- * @param User $user Objet user that make creation
- * @param int $notrigger Disable all triggers
- * @return int <0 if ko, >0 if ok
- */
- public function insert($user, $notrigger = 0)
- {
- global $langs, $conf;
- $error = 0;
- dol_syslog("FichinterLigne::insert rang=".$this->rang);
- $this->db->begin();
- $rangToUse = $this->rang;
- if ($rangToUse == -1) {
- // Recupere rang max de la ligne d'intervention dans $rangmax
- $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
- $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
- $resql = $this->db->query($sql);
- if ($resql) {
- $obj = $this->db->fetch_object($resql);
- $rangToUse = $obj->max + 1;
- } else {
- dol_print_error($this->db);
- $this->db->rollback();
- return -1;
- }
- }
- // Insertion dans base de la ligne
- $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
- $sql .= ' (fk_fichinter, description, date, duree, rang)';
- $sql .= " VALUES (".((int) $this->fk_fichinter).",";
- $sql .= " '".$this->db->escape($this->desc)."',";
- $sql .= " '".$this->db->idate($this->datei)."',";
- $sql .= " ".((int) $this->duration).",";
- $sql .= ' '.((int) $rangToUse);
- $sql .= ')';
- dol_syslog("FichinterLigne::insert", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
- $this->rowid = $this->id;
- if (!$error) {
- $result = $this->insertExtraFields();
- if ($result < 0) {
- $error++;
- }
- }
- $result = $this->update_total();
- if ($result > 0) {
- $this->rang = $rangToUse;
- if (!$notrigger) {
- // Call trigger
- $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- }
- }
- if (!$error) {
- $this->db->commit();
- return $result;
- } else {
- $this->db->rollback();
- return -1;
- }
- } else {
- $this->error = $this->db->error()." sql=".$sql;
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Update intervention into database
- *
- * @param User $user Objet user that make creation
- * @param int $notrigger Disable all triggers
- * @return int <0 if ko, >0 if ok
- */
- public function update($user, $notrigger = 0)
- {
- global $langs, $conf;
- $error = 0;
- $this->db->begin();
- // Mise a jour ligne en base
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
- $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)."'";
- $sql .= " WHERE rowid = ".((int) $this->id);
- dol_syslog("FichinterLigne::update", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- if (!$error) {
- $result = $this->insertExtraFields();
- if ($result < 0) {
- $error++;
- }
- }
- $result = $this->update_total();
- if ($result > 0) {
- if (!$notrigger) {
- // Call trigger
- $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- }
- }
- if (!$error) {
- $this->db->commit();
- return $result;
- } else {
- $this->error = $this->db->lasterror();
- $this->db->rollback();
- return -1;
- }
- } else {
- $this->error = $this->db->lasterror();
- $this->db->rollback();
- return -1;
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Update total duration into llx_fichinter
- *
- * @return int <0 si ko, >0 si ok
- */
- public function update_total()
- {
- // phpcs:enable
- global $conf;
- $this->db->begin();
- $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
- $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
- $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
- dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $obj = $this->db->fetch_object($resql);
- $total_duration = 0;
- if (!empty($obj->total_duration)) {
- $total_duration = $obj->total_duration;
- }
- $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
- $sql .= " SET duree = ".((int) $total_duration);
- $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->idate($obj->dateo)."'" : "null");
- $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->idate($obj->datee)."'" : "null");
- $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
- dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $this->db->commit();
- return 1;
- } else {
- $this->error = $this->db->error();
- $this->db->rollback();
- return -2;
- }
- } else {
- $this->error = $this->db->error();
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Delete a intervention line
- *
- * @param User $user Objet user that make creation
- * @param int $notrigger Disable all triggers
- * @return int >0 if ok, <0 if ko
- */
- public function deleteline($user, $notrigger = 0)
- {
- global $langs, $conf;
- $error = 0;
- if ($this->statut == 0) {
- dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
- $this->db->begin();
- $result = $this->deleteExtraFields();
- if ($result < 0) {
- $error++;
- $this->db->rollback();
- return -1;
- }
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
- $resql = $this->db->query($sql);
- if ($resql) {
- $result = $this->update_total();
- if ($result > 0) {
- if (!$notrigger) {
- // Call trigger
- $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
- if ($result < 0) {
- $error++; $this->db->rollback(); return -1;
- }
- // End call triggers
- }
- $this->db->commit();
- return $result;
- } else {
- $this->db->rollback();
- return -1;
- }
- } else {
- $this->error = $this->db->error()." sql=".$sql;
- $this->db->rollback();
- return -1;
- }
- } else {
- return -2;
- }
- }
- }
|