Review of module asset (#33847)

* Fixed Asset - Update translation of the module

* Fix asset create name

* Fix ref field

* Update module Asset

* Fix CI

* Fix CI

* Fix comment
This commit is contained in:
Alexandre SPANGARO
2025-04-15 17:21:45 +02:00
committed by GitHub
parent f0bd86c8a4
commit ab0ce1d645
10 changed files with 276 additions and 48 deletions

View File

@@ -1,9 +1,9 @@
<?php
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2018-2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
* Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Jose MARTINEZ <jose.martinez@pichinov.com>
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2018-2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
* Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Jose MARTINEZ <jose.martinez@pichinov.com>
*
* 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
@@ -52,7 +52,8 @@ class Asset extends CommonObject
*/
public $picto = 'asset';
const STATUS_DRAFT = 0; // In progress
const STATUS_DRAFT = 0; // Draft
const STATUS_VALIDATED = 1; // In progress
const STATUS_DISPOSED = 9; // Disposed
/**
@@ -88,7 +89,7 @@ class Asset extends CommonObject
*/
public $fields = array(
'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id"),
'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 1, 'noteditable' => 0, 'index' => 1, 'searchall' => 1, 'showoncombobox' => 1, 'validate' => 1, 'comment' => "Reference of object", "css" => "maxwidth150"),
'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 4, 'noteditable' => 0, 'default' => '(PROV)', 'index' => 1, 'searchall' => 1, 'showoncombobox' => 1, 'validate' => 1, 'comment' => "Reference of object", "css" => "maxwidth150"),
'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'searchall' => 1, 'csslist' => 'tdoverflowmax125', 'css' => 'minwidth300', 'cssview' => 'wordbreak', 'showoncombobox' => 2, 'validate' => 1,),
'fk_asset_model' => array('type' => 'integer:AssetModel:asset/class/assetmodel.class.php:1:((status:=:1) and (entity:IN:__SHARED_ENTITIES__))', 'label' => 'AssetModel', 'enabled' => 1, 'position' => 40, 'notnull' => 0, 'visible' => 1, 'index' => 1, 'validate' => 1, 'csslist' => 'tdoverflowmax75', 'css' => 'maxwidth300'),
'qty' => array('type' => 'real', 'label' => 'Qty', 'enabled' => 1, 'position' => 50, 'notnull' => 1, 'visible' => 0, 'default' => '1', 'isameasure' => 1, 'css' => 'maxwidth75imp', 'validate' => 1,),
@@ -110,8 +111,10 @@ class Asset extends CommonObject
'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'position' => 301, 'notnull' => 0, 'visible' => 0, 'validate' => 1,),
'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 500, 'notnull' => 1, 'visible' => -2,),
'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 501, 'notnull' => 0, 'visible' => -2,),
'date_valid' => array('type' => 'datetime', 'label' => 'DateValidation', 'enabled' => 1, 'visible' => -2, 'position' => 502, 'notnull' => 0,),
'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'position' => 510, 'notnull' => 1, 'visible' => -2, 'foreignkey' => 'user.rowid',),
'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 511, 'notnull' => -1, 'visible' => -2,),
'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'picto' => 'user', 'enabled' => 1, 'visible' => -2, 'position' => 512, 'notnull' => 0, 'csslist' => 'tdoverflowmax100'),
'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'LastMainDoc', 'enabled' => 1, 'position' => 600, 'notnull' => 0, 'visible' => 0,),
'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 1000, 'notnull' => -1, 'visible' => -2,),
'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'position' => 1010, 'notnull' => -1, 'visible' => 0,),
@@ -206,6 +209,10 @@ class Asset extends CommonObject
* @var string
*/
public $note_private;
/**
* @var int|string date_valid
*/
public $date_valid;
/**
* @var int
*/
@@ -214,6 +221,10 @@ class Asset extends CommonObject
* @var int
*/
public $fk_user_modif;
/**
* @var int Id User modifying
*/
public $fk_user_valid;
/**
* @var string
*/
@@ -1297,7 +1308,7 @@ class Asset extends CommonObject
}
/**
* Set back to validated status
* Set back to validated status if disposed status
*
* @param User $user Object user that modify
* @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
@@ -1308,7 +1319,7 @@ class Asset extends CommonObject
global $conf, $langs;
// Protection
if ($this->status != self::STATUS_DISPOSED || $this->status == self::STATUS_DRAFT) {
if ($this->status != self::STATUS_DISPOSED) {
return 0;
}
@@ -1323,7 +1334,7 @@ class Asset extends CommonObject
$result = $this->update($user, 1);
if ($result > 0) {
$this->deleteObjectLinked(null, 'facture');
$result = $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'ASSET_REOPEN');
$result = $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'ASSET_REOPEN');
}
if ($result > 0) {
$result = $this->calculationDepreciation();
@@ -1520,13 +1531,18 @@ class Asset extends CommonObject
if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
global $langs;
//$langs->load("assets");
$this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('AssetInProgress');
$this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('AssetInDraft');
$this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('AssetInProgress');
$this->labelStatus[self::STATUS_DISPOSED] = $langs->transnoentitiesnoconv('AssetDisposed');
$this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('AssetInProgress');
$this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('AssetInDraft');
$this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('AssetInProgress');
$this->labelStatusShort[self::STATUS_DISPOSED] = $langs->transnoentitiesnoconv('AssetDisposed');
}
$statusType = 'status4';
$statusType = 'status'.$status;
if ($status == self::STATUS_VALIDATED) {
$statusType = 'status4';
}
if ($status == self::STATUS_DISPOSED) {
$statusType = 'status6';
}
@@ -1542,8 +1558,8 @@ class Asset extends CommonObject
*/
public function info($id)
{
$sql = "SELECT rowid, date_creation as datec, tms as datem,";
$sql .= " fk_user_creat, fk_user_modif";
$sql = "SELECT rowid, date_creation as datec, tms as datem, date_valid as datev,";
$sql .= " fk_user_creat, fk_user_modif, fk_user_valid";
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
$sql .= " WHERE t.rowid = ".((int) $id);
@@ -1553,10 +1569,12 @@ class Asset extends CommonObject
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
$this->user_creation_id = $obj->fk_user_creat;
$this->user_modification_id = $obj->fk_user_modif;
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->datem);
$this->user_creation_id = $obj->fk_user_creat;
$this->user_modification_id = $obj->fk_user_modif;
$this->user_validation_id = $obj->fk_user_valid;
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->datem);
$this->date_validation = $this->db->jdate($obj->datev);
}
$this->db->free($result);
@@ -1637,4 +1655,139 @@ class Asset extends CommonObject
return "";
}
}
/**
* Validate asset
*
* @param User $user User making status change
* @param int<0,1> $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int<-1,1> Return integer <=0 if OK, 0=Nothing done, >0 if KO
*/
public function validate($user, $notrigger = 0)
{
global $conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error = 0;
// Protection
if ($this->status == self::STATUS_VALIDATED) {
dol_syslog(get_class($this)."::validate action abandoned: already validated", LOG_WARNING);
return 0;
}
$now = dol_now();
$this->db->begin();
// 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();
} else {
$num = $this->ref;
}
$this->newref = dol_sanitizeFileName($num);
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " SET ref = '".$this->db->escape($num)."',";
$sql .= " status = ".self::STATUS_VALIDATED.",";
$sql .= " date_valid='".$this->db->idate($now)."',";
$sql .= " fk_user_valid = ".((int) $user->id);
$sql .= " WHERE rowid = ".((int) $this->id);
dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
dol_print_error($this->db);
$this->error = $this->db->lasterror();
$error++;
}
if (!$error && !$notrigger) {
// Call trigger
$result = $this->call_trigger('ASSET_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)) {
// 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 = 'asset/".$this->db->escape($this->newref)."'";
$sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'bom/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->error = $this->db->lasterror();
}
$sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'asset/".$this->db->escape($this->newref)."'";
$sql .= " WHERE filepath = 'asset/".$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->asset->dir_output.'/'.$oldref;
$dirdest = $conf->asset->dir_output.'/'.$newref;
if (!$error && file_exists($dirsource)) {
dol_syslog(get_class($this)."::validate() 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->asset->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 current status
if (!$error) {
$this->ref = $num;
$this->status = self::STATUS_VALIDATED;
}
if (!$error) {
$this->db->commit();
return 1;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Set draft status
*
* @param User $user Object user that modify
* @param int<0,1> $notrigger 1=Does not execute triggers, 0=Execute triggers
* @return int<-1,1> Return integer <0 if KO, 0=Nothing done, >0 if OK
*/
public function setDraft($user, $notrigger = 0)
{
// Protection
if ($this->status <= self::STATUS_DRAFT) {
return 0;
}
return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'ASSET_UNVALIDATE');
}
}