From 619805c7a5cd8fd8a382f776e92b129f7ea12824 Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Thu, 5 Apr 2018 20:40:41 +0200 Subject: [PATCH] Update works --- htdocs/assets/class/assets_type.class.php | 421 ++++++++++++++++++ htdocs/assets/type.php | 102 ++--- htdocs/core/menus/standard/eldy.lib.php | 4 +- .../install/mysql/tables/llx_assets_type.sql | 1 - 4 files changed, 472 insertions(+), 56 deletions(-) create mode 100644 htdocs/assets/class/assets_type.class.php diff --git a/htdocs/assets/class/assets_type.class.php b/htdocs/assets/class/assets_type.class.php new file mode 100644 index 00000000000..5dd727ee950 --- /dev/null +++ b/htdocs/assets/class/assets_type.class.php @@ -0,0 +1,421 @@ + + * + * 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 htdocs/assets/class/assets_type.class.php + * \ingroup assets + * \brief File of class to manage assets types + */ + +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; + + +/** + * Class to manage assets type + */ +class AssetsType extends CommonObject +{ + public $table_element = 'assets_type'; + public $element = 'assets_type'; + public $picto = 'group'; + public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe + + /** @var string Label */ + public $label; + /** @var string Accountancy code asset */ + public $accountancy_code_asset; + /** @var string Accountancy code depreciation asset */ + public $accountancy_code_depreciation_asset; + /** @var string Accountancy code depreciation expense */ + public $accountancy_code_depreciation_expense; + /** @var string Public note */ + public $note; + /** @var array Array of assets */ + public $assets=array(); + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + $this->db = $db; + } + + + /** + * Fonction qui permet de creer le type d'immobilisation + * + * @param User $user User making creation + * @param int $notrigger 1=do not execute triggers, 0 otherwise + * @return int >0 if OK, < 0 if KO + */ + function create($user,$notrigger=0) + { + global $conf; + + $error=0; + + $this->label=trim($this->label); + + $this->db->begin(); + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."assets_type ("; + $sql.= "label"; + $sql.= ", entity"; + $sql.= ") VALUES ("; + $sql.= "'".$this->db->escape($this->label)."'"; + $sql.= ", ".$conf->entity; + $sql.= ")"; + + dol_syslog("Assets_type::create", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."assets_type"); + + $result = $this->update($user,1); + if ($result < 0) + { + $this->db->rollback(); + return -3; + } + + if (! $notrigger) + { + // Call trigger + $result=$this->call_trigger('ASSETS_TYPE_CREATE',$user); + if ($result < 0) { $error++; } + // End call triggers + } + + if (! $error) + { + $this->db->commit(); + return $this->id; + } + else + { + dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } + } + else + { + $this->error=$this->db->lasterror(); + $this->db->rollback(); + return -1; + } + } + + /** + * Met a jour en base donnees du type + * + * @param User $user Object user making change + * @param int $notrigger 1=do not execute triggers, 0 otherwise + * @return int >0 if OK, < 0 if KO + */ + function update($user,$notrigger=0) + { + global $conf, $hookmanager; + + $error=0; + + $this->label=trim($this->label); + + $this->db->begin(); + + $sql = "UPDATE ".MAIN_DB_PREFIX."assets_type "; + $sql.= "SET "; + $sql.= "label = '".$this->db->escape($this->label) ."',"; + $sql.= "accountancy_code_asset = '".$this->db->escape($this->accountancy_code_asset)."',"; + $sql.= "accountancy_code_depreciation_asset = '".$this->db->escape($this->accountancy_code_depreciation_asset)."',"; + $sql.= "accountancy_code_depreciation_expense = '".$this->db->escape($this->accountancy_code_depreciation_expense)."'"; + $sql.= " WHERE rowid =".$this->id; + + $result = $this->db->query($sql); + if ($result) + { + $action='update'; + + // Actions on extra fields (by external module or standard code) + $hookmanager->initHooks(array('assetstypedao')); + $parameters=array('assetstype'=>$this->id); + $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks + if (empty($reshook)) + { + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used + { + $result=$this->insertExtraFields(); + if ($result < 0) + { + $error++; + } + } + } + else if ($reshook < 0) $error++; + + if (! $error && ! $notrigger) + { + // Call trigger + $result=$this->call_trigger('ASSETS_TYPE_MODIFY',$user); + if ($result < 0) { $error++; } + // End call triggers + } + + if (! $error) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR); + return -$error; + } + } + else + { + $this->error=$this->db->lasterror(); + $this->db->rollback(); + return -1; + } + } + + /** + * Fonction qui permet de supprimer le status de l'adherent + * + * @return int >0 if OK, 0 if not found, < 0 if KO + */ + function delete() + { + global $user; + + $error = 0; + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."assets_type"; + $sql.= " WHERE rowid = ".$this->id; + + $resql=$this->db->query($sql); + if ($resql) + { + // Call trigger + $result=$this->call_trigger('ASSETS_TYPE_DELETE',$user); + if ($result < 0) { $error++; $this->db->rollback(); return -2; } + // End call triggers + + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + $this->error=$this->db->lasterror(); + return -1; + } + } + + /** + * Fonction qui permet de recuperer le status de l'adherent + * + * @param int $rowid Id of member type to load + * @return int <0 if KO, >0 if OK + */ + function fetch($rowid) + { + $sql = "SELECT d.rowid, d.label as label, d.accountancy_code_asset, d.accountancy_code_depreciation_asset, d.accountancy_code_depreciation_expense, d.note"; + $sql .= " FROM ".MAIN_DB_PREFIX."assets_type as d"; + $sql .= " WHERE d.rowid = ".(int) $rowid; + + dol_syslog("Assets_type::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->rowid; + $this->label = $obj->label; + $this->accountancy_code_asset = $obj->accountancy_code_asset; + $this->accountancy_code_depreciation_asset = $obj->accountancy_code_depreciation_asset; + $this->accountancy_code_depreciation_expense = $obj->accountancy_code_depreciation_expense; + $this->note = $obj->note; + } + + return 1; + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } + + /** + * Return list of assets' type + * + * @return array List of types of members + */ + function liste_array() + { + global $conf,$langs; + + $assetstypes = array(); + + $sql = "SELECT rowid, label as label"; + $sql.= " FROM ".MAIN_DB_PREFIX."assets_type"; + $sql.= " WHERE entity IN (".getEntity('assets_type').")"; + + $resql=$this->db->query($sql); + if ($resql) + { + $nump = $this->db->num_rows($resql); + + if ($nump) + { + $i = 0; + while ($i < $nump) + { + $obj = $this->db->fetch_object($resql); + + $assetstypes[$obj->rowid] = $langs->trans($obj->label); + $i++; + } + } + } + else + { + print $this->db->error(); + } + return $assetstypes; + } + + /** + * Return array of Asset objects for asset type this->id (or all if this->id not defined) + * + * @param string $excludefilter Filter to exclude + * @param int $mode 0=Return array of asset instance + * 1=Return array of asset instance without extra data + * 2=Return array of assets id only + * @return mixed Array of assets or -1 on error + */ + function listAssetsForAssetsType($excludefilter='', $mode=0) + { + global $conf, $user; + + $ret=array(); + + $sql = "SELECT a.rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."assets as a"; + $sql.= " WHERE a.entity IN (".getEntity('assets').")"; + $sql.= " AND a.fk_assets_type = ".$this->id; + if (! empty($excludefilter)) $sql.=' AND ('.$excludefilter.')'; + + dol_syslog(get_class($this)."::listAssetsForGroup", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + while ($obj = $this->db->fetch_object($resql)) + { + if (! array_key_exists($obj->rowid, $ret)) + { + if ($mode < 2) + { + $assetsstatic=new Assets($this->db); + if ($mode == 1) { + $assetsstatic->fetch($obj->rowid,'','','',false, false); + } else { + $assetsstatic->fetch($obj->rowid); + } + $ret[$obj->rowid]=$assetsstatic; + } + else $ret[$obj->rowid]=$obj->rowid; + } + } + + $this->db->free($resql); + + $this->assets=$ret; + + return $ret; + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } + + /** + * Return clicable name (with picto eventually) + * + * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto + * @param int $maxlen length max label + * @param int $notooltip 1=Disable tooltip + * @return string String with URL + */ + function getNomUrl($withpicto=0, $maxlen=0, $notooltip=0) + { + global $langs; + + $result=''; + $label=$langs->trans("ShowTypeCard",$this->label); + + $linkstart = ''; + $linkend=''; + + $result .= $linkstart; + if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); + if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->label,$maxlen):$this->label); + $result .= $linkend; + + return $result; + } + + /** + * 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 + */ + function initAsSpecimen() + { + global $conf, $user, $langs; + + // Initialize parameters + $this->id = 0; + $this->ref = 'ATSPEC'; + $this->specimen=1; + + $this->label='ASSETS TYPE SPECIMEN'; + $this->note='This is a note'; + + // Assets of this asset type is just me + $this->assets=array( + $user->id => $user + ); + } + +} diff --git a/htdocs/assets/type.php b/htdocs/assets/type.php index 48df382181d..e5326029cfc 100644 --- a/htdocs/assets/type.php +++ b/htdocs/assets/type.php @@ -1,10 +1,5 @@ - * Copyright (C) 2003 Jean-Louis Bergamo - * Copyright (C) 2004-2011 Laurent Destailleur - * Copyright (C) 2005-2017 Regis Houssin - * Copyright (C) 2013 Florian Henry - * Copyright (C) 2015 Alexandre Spangaro +/* Copyright (C) 2018 Alexandre Spangaro * * 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 @@ -21,18 +16,18 @@ */ /** - * \file htdocs/adherents/type.php - * \ingroup member - * \brief Member's type setup + * \file htdocs/assets/type.php + * \ingroup assets + * \brief Asset's type setup */ require '../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php'; -require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; -require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/assets.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/assets/class/assets.class.php'; +require_once DOL_DOCUMENT_ROOT.'/assets/class/assets_type.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; -$langs->load("members"); +$langs->load("assets"); $rowid = GETPOST('rowid','int'); $action = GETPOST('action','alpha'); @@ -43,7 +38,6 @@ $search_lastname = GETPOST('search_lastname','alpha'); $search_login = GETPOST('search_login','alpha'); $search_email = GETPOST('search_email','alpha'); $type = GETPOST('type','alpha'); -$status = GETPOST('status','alpha'); $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit; $sortfield = GETPOST("sortfield",'alpha'); @@ -63,14 +57,14 @@ $comment=GETPOST("comment"); $mail_valid=GETPOST("mail_valid"); // Security check -$result=restrictedArea($user,'adherent',$rowid,'adherent_type'); +$result=restrictedArea($user,'assets',$rowid,'assets_type'); -$object = new AdherentType($db); +$object = new AssetsType($db); $extrafields = new ExtraFields($db); // fetch optionals attributes and labels -$extralabels=$extrafields->fetch_name_optionals_label('adherent_type'); +$extralabels=$extrafields->fetch_name_optionals_label('assets_type'); if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter','alpha')) // All tests are required to be compatible with all browsers { @@ -83,7 +77,7 @@ if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter_x', // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context -$hookmanager->initHooks(array('membertypecard','globalcard')); +$hookmanager->initHooks(array('assetstypecard','globalcard')); /* @@ -101,13 +95,13 @@ if ($cancel) { } } -if ($action == 'add' && $user->rights->adherent->configurer) +if ($action == 'add' && $user->rights->assets->write) { - $object->label = trim($label); - $object->subscription = (int) trim($subscription); - $object->note = trim($comment); - $object->mail_valid = trim($mail_valid); - $object->vote = (boolean) trim($vote); + $object->label = trim($label); + $object->accountancy_code_asset = trim($accountancy_code_asset); + $object->accountancy_code_depreciation_asset = trim($accountancy_code_depreciation_asset); + $object->accountancy_code_depreciation_expense = trim($accountancy_code_depreciation_expense); + $object->note = trim($comment); // Fill array 'array_options' with data from add form $ret = $extrafields->setOptionalsFromPost($extralabels,$object); @@ -118,7 +112,7 @@ if ($action == 'add' && $user->rights->adherent->configurer) setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")), null, 'errors'); } else { - $sql = "SELECT libelle FROM ".MAIN_DB_PREFIX."adherent_type WHERE libelle='".$db->escape($object->label)."'"; + $sql = "SELECT label FROM ".MAIN_DB_PREFIX."assets_type WHERE label='".$db->escape($object->label)."'"; $result = $db->query($sql); if ($result) { $num = $db->num_rows($result); @@ -150,17 +144,17 @@ if ($action == 'add' && $user->rights->adherent->configurer) } } -if ($action == 'update' && $user->rights->adherent->configurer) +if ($action == 'update' && $user->rights->assets->write) { $object->fetch($rowid); $object->oldcopy = clone $object; - $object->label = trim($label); - $object->subscription = (int) trim($subscription); - $object->note = trim($comment); - $object->mail_valid = trim($mail_valid); - $object->vote = (boolean) trim($vote); + $object->label = trim($label); + $object->accountancy_code_asset = trim($accountancy_code_asset); + $object->accountancy_code_depreciation_asset = trim($accountancy_code_depreciation_asset); + $object->accountancy_code_depreciation_expense = trim($accountancy_code_depreciation_expense); + $object->note = trim($comment); // Fill array 'array_options' with data from add form $ret = $extrafields->setOptionalsFromPost($extralabels,$object); @@ -170,7 +164,7 @@ if ($action == 'update' && $user->rights->adherent->configurer) if ($ret >= 0 && ! count($object->errors)) { - setEventMessages($langs->trans("MemberTypeModified"), null, 'mesgs'); + setEventMessages($langs->trans("AssetsTypeModified"), null, 'mesgs'); } else { @@ -181,20 +175,20 @@ if ($action == 'update' && $user->rights->adherent->configurer) exit; } -if ($action == 'confirm_delete' && $user->rights->adherent->configurer) +if ($action == 'confirm_delete' && $user->rights->assets->write) { $object->fetch($rowid); $res=$object->delete(); if ($res > 0) { - setEventMessages($langs->trans("MemberTypeDeleted"), null, 'mesgs'); + setEventMessages($langs->trans("AssetsTypeDeleted"), null, 'mesgs'); header("Location: ".$_SERVER["PHP_SELF"]); exit; } else { - setEventMessages($langs->trans("MemberTypeCanNotBeDeleted"), null, 'errors'); + setEventMessages($langs->trans("AssetsTypeCanNotBeDeleted"), null, 'errors'); $action=''; } } @@ -205,18 +199,18 @@ if ($action == 'confirm_delete' && $user->rights->adherent->configurer) */ $form=new Form($db); - -llxHeader('',$langs->trans("MembersTypeSetup"),'EN:Module_Foundations|FR:Module_Adhérents|ES:Módulo_Miembros'); +$helpurl=''; +llxHeader('',$langs->trans("AssetsTypeSetup"),$helpurl); -// List of members type +// List of assets type if (! $rowid && $action != 'create' && $action != 'edit') { //dol_fiche_head(''); - $sql = "SELECT d.rowid, d.libelle as label, d.subscription, d.vote"; - $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as d"; - $sql.= " WHERE d.entity IN (".getEntity('member_type').")"; + $sql = "SELECT d.rowid, d.label as label, d.accountancy_code_asset, d.accountancy_code_depreciation_asset, d.accountancy_code_depreciation_expense, d.note"; + $sql.= " FROM ".MAIN_DB_PREFIX."assets_type as d"; + $sql.= " WHERE d.entity IN (".getEntity('assets_type').")"; $result = $db->query($sql); if ($result) @@ -237,7 +231,7 @@ if (! $rowid && $action != 'create' && $action != 'edit') print ''; print ''; - print_barre_liste($langs->trans("MembersTypes"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_generic.png', 0, '', '', $limit); + print_barre_liste($langs->trans("AssetsTypes"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_generic.png', 0, '', '', $limit); $moreforfilter = ''; @@ -252,19 +246,19 @@ if (! $rowid && $action != 'create' && $action != 'edit') print ' '; print "\n"; - $membertype = new AdherentType($db); + $assetstype = new AssetsType($db); while ($i < $num) { $objp = $db->fetch_object($result); - $membertype->id = $objp->rowid; - $membertype->ref = $objp->rowid; - $membertype->label = $objp->rowid; + $assetstype->id = $objp->rowid; + $assetstype->ref = $objp->rowid; + $assetstype->label = $objp->rowid; print ''; print ''; - print $membertype->getNomUrl(1); + print $assetstype->getNomUrl(1); //'.img_object($langs->trans("ShowType"),'group').' '.$objp->rowid.' print ''; print ''.dol_escape_htmltag($objp->label).''; @@ -432,7 +426,7 @@ if ($rowid > 0) // Show list of members (nearly same code than in page list.php) - $membertypestatic=new AdherentType($db); + $assetstypestatic=new AdherentType($db); $now=dol_now(); @@ -514,9 +508,9 @@ if ($rowid > 0) if ($type > 0) { - $membertype=new AdherentType($db); - $result=$membertype->fetch($type); - $titre.=" (".$membertype->label.")"; + $assetstype=new AdherentType($db); + $result=$assetstype->fetch($type); + $titre.=" (".$assetstype->label.")"; } $param="&rowid=".$object->id; @@ -603,9 +597,9 @@ if ($rowid > 0) // Type /*print ''; - $membertypestatic->id=$objp->type_id; - $membertypestatic->label=$objp->type; - print $membertypestatic->getNomUrl(1,12); + $assetstypestatic->id=$objp->type_id; + $assetstypestatic->label=$objp->type; + print $assetstypestatic->getNomUrl(1,12); print ''; */ diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index d832f57ec9a..6d9d78e656b 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -1143,7 +1143,9 @@ function print_left_eldy_menu($db,$menu_array_before,$menu_array_after,&$tabMenu $langs->load("assets"); $newmenu->add("/assets/list.php?leftmenu=assets&mainmenu=accountancy",$langs->trans("MenuAssets"), 0, $user->rights->assets->read, '', $mainmenu, 'assets'); $newmenu->add("/assets/card.php?leftmenu=assets&action=create",$langs->trans("MenuNewAsset"), 1, $user->rights->assets->write); - $newmenu->add("/assets/type.php?leftmenu=assets&action=create",$langs->trans("MenuTypeAssets"), 1, $user->rights->assets->write); + $newmenu->add("/assets/type.php?leftmenu=assets",$langs->trans("MenuTypeAssets"), 1, $user->rights->assets->read, '', $mainmenu, 'assets_type'); + $newmenu->add("/assets/type.php?leftmenu=assets_type&action=create",$langs->trans("MenuNewTypeAssets"), 1, $user->rights->assets->write); + $newmenu->add("/assets/type.php?leftmenu=assets_type",$langs->trans("MenuListTypeAssets"), 1, $user->rights->assets->read); $newmenu->add("/assets/list.php?leftmenu=assets",$langs->trans("MenuListAssets"), 1, $user->rights->assets->read); } } diff --git a/htdocs/install/mysql/tables/llx_assets_type.sql b/htdocs/install/mysql/tables/llx_assets_type.sql index fe035083ad6..d637085488b 100644 --- a/htdocs/install/mysql/tables/llx_assets_type.sql +++ b/htdocs/install/mysql/tables/llx_assets_type.sql @@ -18,7 +18,6 @@ create table llx_assets_type rowid integer AUTO_INCREMENT PRIMARY KEY, entity integer DEFAULT 1 NOT NULL, -- multi company id tms timestamp, - statut smallint NOT NULL DEFAULT 0, label varchar(50) NOT NULL, accountancy_code_asset varchar(32), accountancy_code_depreciation_asset varchar(32),