From 62e305f7fa7800058ee4f6487c2d6a7cef2d89e9 Mon Sep 17 00:00:00 2001 From: ldestailleur Date: Sat, 10 May 2025 21:03:27 +0200 Subject: [PATCH] Debug the asset module. Can't delete. --- htdocs/asset/class/asset.class.php | 30 +++++ .../class/assetaccountancycodes.class.php | 63 +++++++++++ .../assetaccountancycodesfiscal.class.php | 103 ++++++++++++++++++ .../class/assetdepreciationoptions.class.php | 2 +- htdocs/core/class/commonobject.class.php | 24 +++- ...t_accountancy_codes_economic-asset.key.sql | 2 + .../template/class/myobject.class.php | 7 +- 7 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 htdocs/asset/class/assetaccountancycodesfiscal.class.php diff --git a/htdocs/asset/class/asset.class.php b/htdocs/asset/class/asset.class.php index 18eaa12bc63..ad261242ed2 100644 --- a/htdocs/asset/class/asset.class.php +++ b/htdocs/asset/class/asset.class.php @@ -52,6 +52,24 @@ class Asset extends CommonObject */ public $picto = 'asset'; + /** + * @var string|int Field with ID of parent key if this field has a parent (a string). For example 'fk_product'. + * ID of parent key itself (an int). For example in few classes like 'Comment', 'ActionComm' or 'AdvanceTargetingMailing'. + */ + public $fk_element = 'fk_asset'; + + /** + * @var array List of child tables. To know object to delete on cascade. + * If name matches '@ClassName:FilePathClass:ParentFkFieldName' (the recommended mode) it will + * call method ClassName->deleteByParentField(parentId, 'ParentFkFieldName') to fetch and delete child object. + * Using an array like childtables should not be implemented because a child may have other child, so we must only use the method that call deleteByParentField(). + */ + protected $childtablesoncascade = array( + '@AssetAccountancyCodes:/asset/class/assetaccountancycodes.class.php:fk_asset', + '@AssetAccountancyCodesFiscal:/asset/class/assetaccountancycodesfiscal.class.php:fk_asset' + ); + + const STATUS_DRAFT = 0; // Draft const STATUS_VALIDATED = 1; // In progress const STATUS_DISPOSED = 9; // Disposed @@ -246,15 +264,18 @@ class Asset extends CommonObject * @var AssetDepreciationOptions Used for computed fields of depreciation options class. */ public $asset_depreciation_options; + /** * @var AssetAccountancyCodes */ public $asset_accountancy_codes; + /** * @var array> List of depreciation lines for each mode (sort by depreciation date). */ public $depreciation_lines = array(); + /** * Constructor * @@ -569,6 +590,15 @@ class Asset extends CommonObject */ public function delete(User $user, $notrigger = 0) { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'asset_depreciation_options_fiscal WHERE fk_asset = '.((int) $this->id); + $this->db->query($sql); + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'asset_depreciation_options_economic WHERE fk_asset = '.((int) $this->id); + $this->db->query($sql); + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'asset_depreciation WHERE fk_asset = '.((int) $this->id); + $this->db->query($sql); + return $this->deleteCommon($user, $notrigger); //return $this->deleteCommon($user, $notrigger, 1); } diff --git a/htdocs/asset/class/assetaccountancycodes.class.php b/htdocs/asset/class/assetaccountancycodes.class.php index 7dd2ad2ef80..55688ee1f00 100644 --- a/htdocs/asset/class/assetaccountancycodes.class.php +++ b/htdocs/asset/class/assetaccountancycodes.class.php @@ -24,11 +24,41 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; + /** * Class for AssetAccountancyCodes */ class AssetAccountancyCodes extends CommonObject { + // TODO This class and table should not exists and should be properties of llx_asset_asset. + + /** + * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management (so extrafields know the link to the parent table). + */ + public $table_element = 'asset_accountancy_codes_economic'; + + /** + * @var string Field with ID of parent key if this object has a parent + */ + public $fk_element = 'fk_asset'; + + // BEGIN MODULEBUILDER PROPERTIES + /** + * @inheritdoc + * Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. + */ + 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'), + //... + ); + + /** + * @var int ID + */ + public $rowid; + // END MODULEBUILDER PROPERTIES + + /** * @var array>> Array with all accountancy codes info by mode. * Note : 'economic' mode is mandatory and is the primary accountancy codes @@ -64,6 +94,11 @@ class AssetAccountancyCodes extends CommonObject ), ); + /** + * @var int ID parent asset + */ + public $fk_asset; + /** * @var array> Array with all accountancy codes by mode. */ @@ -79,6 +114,34 @@ class AssetAccountancyCodes extends CommonObject $this->db = $db; } + /** + * Load object in memory from the database + * + * @param int $id Id object + * @param string $ref Ref + * @return int<-1,max> Return integer <0 if KO, 0 if not found, >0 if OK + */ + public function fetch($id, $ref = null) + { + $result = $this->fetchCommon($id, $ref); + + return $result; + } + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param int<0,1> $notrigger 0=launch triggers, 1=disable triggers + * @return int<-1,1> Return integer <0 if KO, >0 if OK + */ + public function delete(User $user, $notrigger = 0) + { + return $this->deleteCommon($user, $notrigger); + //return $this->deleteCommon($user, $notrigger, 1); + } + + /** * Fill accountancy_codes property of object (using for data sent by forms) * diff --git a/htdocs/asset/class/assetaccountancycodesfiscal.class.php b/htdocs/asset/class/assetaccountancycodesfiscal.class.php new file mode 100644 index 00000000000..948868ea445 --- /dev/null +++ b/htdocs/asset/class/assetaccountancycodesfiscal.class.php @@ -0,0 +1,103 @@ + + * Copyright (C) 2024 MDW + * + * 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 . + */ + +/** + * \file asset/class/assetaccountancycodesfiscal.class.php + * \ingroup asset + * \brief This file is a class file for AssetAccountancyCodesFiscal + */ + +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; + + +/** + * Class for AssetAccountancyCodes + */ +class AssetAccountancyCodesFiscal extends CommonObject +{ + // TODO This class and table should not exists and should be properties of llx_asset_asset. + + /** + * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management (so extrafields know the link to the parent table). + */ + public $table_element = 'asset_accountancy_codes_fiscal'; + + /** + * @var string Field with ID of parent key if this object has a parent + */ + public $fk_element = 'fk_asset'; + + // BEGIN MODULEBUILDER PROPERTIES + /** + * @inheritdoc + * Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. + */ + 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'), + //... + ); + + /** + * @var int ID + */ + public $rowid; + // END MODULEBUILDER PROPERTIES + + /** + * @var int ID parent asset + */ + public $fk_asset; + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct(DoliDB $db) + { + $this->db = $db; + } + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @param string $ref Ref + * @return int<-1,max> Return integer <0 if KO, 0 if not found, >0 if OK + */ + public function fetch($id, $ref = null) + { + $result = $this->fetchCommon($id, $ref); + + return $result; + } + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param int<0,1> $notrigger 0=launch triggers, 1=disable triggers + * @return int<-1,1> Return integer <0 if KO, >0 if OK + */ + public function delete(User $user, $notrigger = 0) + { + return $this->deleteCommon($user, $notrigger); + //return $this->deleteCommon($user, $notrigger, 1); + } +} diff --git a/htdocs/asset/class/assetdepreciationoptions.class.php b/htdocs/asset/class/assetdepreciationoptions.class.php index 16db99fcd02..ea2c287add2 100644 --- a/htdocs/asset/class/assetdepreciationoptions.class.php +++ b/htdocs/asset/class/assetdepreciationoptions.class.php @@ -107,7 +107,7 @@ class AssetDepreciationOptions extends CommonObject ), ); /** - * @var int + * @var int ID parent asset */ public $fk_asset; /** diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ef1fbe1248f..33eb37a76f8 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4910,8 +4910,6 @@ abstract class CommonObject */ public function getCanvas($id = 0, $ref = '') { - global $conf; - if (empty($id) && empty($ref)) { return 0; } @@ -10926,6 +10924,7 @@ abstract class CommonObject if (!empty($deleteFromObject[3])) { $filter = $deleteFromObject[3]; } + if (dol_include_once($filePath)) { $childObject = new $className($this->db); if (method_exists($childObject, 'deleteByParentField')) { @@ -11031,7 +11030,28 @@ abstract class CommonObject $error = 0; $deleted = 0; + //dol_syslog("deleteByParentField for ".$parentId.' '.$parentField); + if (!empty($parentId) && !empty($parentField)) { + if (empty($this->table_element)) { + $this->error = 'Property table_element for object is not defined'; + $this->errors[] = $this->error; + $error++; + return -1; + } + if (!method_exists($this, 'fetch')) { + $this->error = 'Method fetch for object is not defined'; + $this->errors[] = $this->error; + $error++; + return -1; + } + if (!method_exists($this, 'delete')) { + $this->error = 'Method delete for object is not defined'; + $this->errors[] = $this->error; + $error++; + return -1; + } + $this->db->begin(); $sql = "SELECT rowid FROM ".$this->db->prefix().$this->table_element; diff --git a/htdocs/install/mysql/tables/llx_asset_accountancy_codes_economic-asset.key.sql b/htdocs/install/mysql/tables/llx_asset_accountancy_codes_economic-asset.key.sql index 69bc6754f65..6e6a0efe0f8 100644 --- a/htdocs/install/mysql/tables/llx_asset_accountancy_codes_economic-asset.key.sql +++ b/htdocs/install/mysql/tables/llx_asset_accountancy_codes_economic-asset.key.sql @@ -15,6 +15,8 @@ -- along with this program. If not, see https://www.gnu.org/licenses/. -- ======================================================================== +-- TODO This table seems to contains properties of llx_asset so should not exists and fields should be into llx_asset. + ALTER TABLE llx_asset_accountancy_codes_economic ADD INDEX idx_asset_ace_rowid (rowid); ALTER TABLE llx_asset_accountancy_codes_economic ADD UNIQUE uk_asset_ace_fk_asset (fk_asset); ALTER TABLE llx_asset_accountancy_codes_economic ADD UNIQUE uk_asset_ace_fk_asset_model (fk_asset_model); diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index beb526cece7..011749b3590 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -201,7 +201,7 @@ class MyObject extends CommonObject // public $table_element_line = 'mymodule_myobjectline'; // /** - // * @var string Field with ID of parent key if this object has a parent + // * @var string Field name with ID of parent key if this object has a parent, Or Field name of in child tables to link to this record. // */ // public $fk_element = 'fk_myobject'; @@ -217,8 +217,9 @@ class MyObject extends CommonObject // /** // * @var array List of child tables. To know object to delete on cascade. - // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will - // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object + // * If name matches '@ClassName:FilePathClass:ParentFkFieldName' (the recommended mode) it will + // * call method ClassName->deleteByParentField(parentId, 'ParentFkFieldName') to fetch and delete child object. + // * Using an array like childtables should not be implemented because a child may have other child, so we must only use the method that call deleteByParentField(). // */ // protected $childtablesoncascade = array('mymodule_myobjectdet');