dolibarr/htdocs/expensereport/class/expensereport_rule.class.php

288 lines
8.1 KiB
PHP
Raw Permalink Normal View History

<?php
/* Copyright (C) 2017 ATM Consulting <support@atm-consulting.fr>
* Copyright (C) 2017 Pierre-Henry Favre <phf@atm-consulting.fr>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Frédéric France <frederic.france@free.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
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/expensereport/class/expensereport_ik.class.php
* \ingroup expenseik
* \brief File of class to manage expense ik
*/
2017-10-13 13:10:36 +02:00
2022-05-18 11:00:43 +02:00
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/**
* Class to manage inventories
*/
2022-05-18 11:00:43 +02:00
class ExpenseReportRule extends CommonObject
{
2018-08-23 18:35:45 +02:00
/**
* @var string ID to identify managed object
*/
public $element = 'expenserule';
2018-10-06 12:01:00 +02:00
2018-08-22 18:48:53 +02:00
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'expensereport_rules';
2018-10-06 12:01:00 +02:00
/**
2020-12-05 23:53:55 +01:00
* @var string Fieldname with ID of parent key if this field has a parent
2018-10-06 12:01:00 +02:00
*/
public $fk_element = 'fk_expense_rule';
2017-10-13 13:10:36 +02:00
/**
* date start
2017-10-13 13:10:36 +02:00
* @var int|string
*/
public $dates;
2017-10-13 13:10:36 +02:00
/**
* date end
2017-10-13 13:10:36 +02:00
* @var int|string
*/
public $datee;
2017-10-13 13:10:36 +02:00
/**
* amount
* @var double
*/
public $amount;
2017-10-13 13:10:36 +02:00
/**
* restrective
* @var int
*/
public $restrictive;
2017-10-13 13:10:36 +02:00
/**
* rule for user
* @var int
*/
public $fk_user;
2017-10-13 13:10:36 +02:00
/**
* rule for group
* @var int
*/
public $fk_usergroup;
2017-10-13 13:10:36 +02:00
/**
* c_type_fees id
* @var int
*/
public $fk_c_type_fees;
2017-10-13 13:10:36 +02:00
/**
* code type of expense report
* @var string
*/
public $code_expense_rules_type;
2017-10-13 13:10:36 +02:00
/**
* rule for all
* @var int
*/
public $is_for_all;
2017-10-13 13:10:36 +02:00
/**
* entity
* @var int
*/
public $entity;
2017-10-13 13:10:36 +02:00
/**
* Attribute object linked with database
* @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int<-6,6>|string,alwayseditable?:int<0,1>,noteditable?:int<0,1>,default?:string,index?:int,foreignkey?:string,searchall?:int<0,1>,isameasure?:int<0,1>,css?:string,csslist?:string,help?:string,showoncombobox?:int<0,4>,disabled?:int<0,1>,arrayofkeyval?:array<int|string,string>,autofocusoncreate?:int<0,1>,comment?:string,copytoclipboard?:int<1,2>,validate?:int<0,1>,showonheader?:int<0,1>}> 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', 'index' => 1, 'label' => 'ID', 'enabled' => 1, 'visible' => -1, 'position' => 10),
'dates' => array('type' => 'date', 'label' => 'Dates', 'enabled' => 1, 'visible' => -1, 'position' => 20),
'datee' => array('type' => 'date', 'label' => 'Datee', 'enabled' => 1, 'visible' => -1, 'position' => 30),
'amount' => array('type' => 'double', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40),
'restrictive' => array('type' => 'integer', 'label' => 'Restrictive', 'enabled' => 1, 'visible' => -1, 'position' => 50),
'fk_user' => array('type' => 'integer', 'label' => 'User', 'enabled' => 1, 'visible' => -1, 'position' => 60),
'fk_usergroup' => array('type' => 'integer', 'label' => 'Usergroup', 'enabled' => 1, 'visible' => -1, 'position' => 70),
'fk_c_type_fees' => array('type' => 'integer', 'label' => 'Type fees', 'enabled' => 1, 'visible' => -1, 'position' => 80),
'code_expense_rules_type' => array('type' => 'string', 'label' => 'Expense rule code', 'enabled' => 1, 'visible' => -1, 'position' => 90),
'is_for_all' => array('type' => 'integer', 'label' => 'IsForAll', 'enabled' => 1, 'visible' => -1, 'position' => 100),
'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => -2, 'position' => 110),
);
2022-05-18 11:00:43 +02:00
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
2022-05-18 11:00:43 +02:00
public function __construct(DoliDB $db)
{
$this->db = $db;
}
/**
* Create object into database
*
* @param User $user User that creates
2024-01-12 19:48:18 +01:00
* @param int $notrigger 0=launch triggers after, 1=disable triggers
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, Id of created object if OK
2022-05-18 11:00:43 +02:00
*/
2024-01-12 18:07:53 +01:00
public function create(User $user, $notrigger = 0)
{
2022-05-18 11:00:43 +02:00
$resultcreate = $this->createCommon($user, $notrigger);
2022-05-18 11:00:43 +02:00
//$resultvalidate = $this->validate($user, $notrigger);
2017-10-13 13:10:36 +02:00
2022-05-18 11:00:43 +02:00
return $resultcreate;
}
/**
* Load object in memory from the database
*
* @param int $id Id object
* @param string $ref Ref
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, 0 if not found, >0 if OK
2022-05-18 11:00:43 +02:00
*/
public function fetch($id, $ref = null)
{
return $this->fetchCommon($id, $ref);
}
2022-05-18 11:00:43 +02:00
/**
* Update object into database
*
* @param User $user User that modifies
2024-01-12 18:07:53 +01:00
* @param int $notrigger 0=launch triggers after, 1=disable triggers
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2022-05-18 11:00:43 +02:00
*/
2024-01-12 18:07:53 +01:00
public function update(User $user, $notrigger = 0)
2022-05-18 11:00:43 +02:00
{
return $this->updateCommon($user, $notrigger);
}
/**
* Delete object in database
*
2024-01-12 18:07:53 +01:00
* @param User $user User that deletes
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, >0 if OK
2022-05-18 11:00:43 +02:00
*/
2024-01-12 18:07:53 +01:00
public function delete(User $user, $notrigger = 0)
2022-05-18 11:00:43 +02:00
{
return $this->deleteCommon($user, $notrigger);
//return $this->deleteCommon($user, $notrigger, 1);
}
/**
* Return all rules or filtered by something
2017-10-13 13:10:36 +02:00
*
2019-04-04 18:33:12 +02:00
* @param int $fk_c_type_fees type of expense
2024-01-14 21:12:52 +01:00
* @param int|string $date date of expense
2019-04-04 18:33:12 +02:00
* @param int $fk_user user of expense
Qual: Fix PhanPluginUnknownObjectMethodCall ("part 1") (#30563) * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add typing hint to fix phan notice. * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add typing hint in phan config to fix phan notice. * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint to fix phan notice. * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for linkedObjectBlock * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for linkedObjectBlock * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for linkedObjects * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) # Qual: Fix PhanPluginUnknownObjectMethodCall (add typing hints) Add/improve typing hint for PhanPluginUnknownObjectMethodCall * Fix: MemberNumRefNumbers getExample does not take any arguments Found thanks to adding the typing for phan! * Qual: Correct forced type for $module * Qual: Ignore phan false positive * Qual: phan/Correct module type and ignore false positive * Qual: phan/Ignore false positive * Qual: phan/Ignore false positive, replace depr.prop with method * Qual: Fix typing for count by adding is_array check * Qual: replace depr.prop `nom` with method * Qual: phan/Correct module type and ignore false positive * Qual: phan/Correct module type and ignore false positive * Qual: phan/Correct module type and ignore false positive * Qual: phan/Fix depr.prop with getter and ignore false positive * Qual: phan/Correct module type and ignore false positive * Qual: phan/Correct module type and ignore false positive * Qual: phan/Correct module type and ignore false positive * Qual: Improve typing for mymodule template * Make info method arguments match calls * Make PrintingDriver abstract class because we added abstract methods * Add phpdoc typing for phan * Update getExample function signature for compatibility * Update getExample function signature for compatibility * Update getExample function signature for compatibility * Update header wit correct information * Update getExample method signature * Update getExample method signature * Correct reference to class in comment * Force php phan typing for numbering modules * Correct $_GLOBALS to $GLOBALS * Add phpdoc to indicat type of properties * Correct default to null * Improve getNextValue typehint * Improve getNextValue typehint * Qual: Improve ModeleNumRefTakepos typing * Qual: Improve ModeleNumRefRecruitmentJobPosition typing * Fix default for $langs * Do not use non-existing ModeleNumRefTakepos::nom * Allow getNextValue argument to be null * Correct $_GLOBAL into $GLOBALS * Improve typing for ldap functions * Allow null for outputlangs argument * Improve typing for printOriginLine * Improve typing for linkedObject access * Fix PhanTypeMismatchArgumentNullableInternal and optimize * Fix PhanPossiblyUndeclaredVariable by setting default value * Ignore PhanPluginDuplicateExpressionAssignmentOperation - needs PHP7.4 * getToolTip does not accept null, changed to '' * Improve getNextValue typing * Change PrintingDriver back to normal class (instantiated) And add errors for functions that should be overloaded * Adjust pringOriginLine typing to match parent * Fix phpdoc for getNextValue * Fix phan typing * Add/Improve phpdoc typing hints * Qual: Adjustments to match typing * Update typing for unit, use GETPOSTINT * ModeleNumRefTask needs Project * Fix several notices appearing after update from develop * Index for choices is int, use GETPOSTINT * Qual: Ignore empty foreach * Force BOMLine on object line * phan typing to indicate that linked objects are BOM * Type the correct variable name (phan) * Add typing for $langs * Type to CommonNumRefGenerator (no class for availabilities) * Resolve several phan notices after update from dev branch * Extra typing fixes * Move common attributes to parent class (phan) * Add typing to Calendar class * Improve typing hints * Add typing to pdf_eagle_proforma * Qual: Add typing for token in generic_oauthcallback.php * Add typing for objMod * Correct getNextValue function signature (phpstan) * Fix typing (phpstan) * Update version declarations (fix phpstan) * Fix phpstan typing issues * Adjust typing (phpstan) * Qual: Update baseline & conf to detect 25% of PhanPluginUnknownObjectMethodCall
2024-08-17 19:32:52 +02:00
* @return ExpenseReportRule[] Array with ExpenseReportRule
*/
2024-01-14 21:12:52 +01:00
public function getAllRule($fk_c_type_fees = 0, $date = '', $fk_user = 0)
{
$rules = array();
2021-09-27 15:41:58 +02:00
$sql = 'SELECT er.rowid';
$sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
2021-09-27 15:41:58 +02:00
$sql .= ' WHERE er.entity IN (0,'.getEntity($this->element).')';
2021-02-25 22:45:02 +01:00
if (!empty($fk_c_type_fees)) {
2021-08-28 01:45:53 +02:00
$sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
}
2021-02-25 22:45:02 +01:00
if (!empty($date)) {
2021-09-27 15:41:58 +02:00
$sql .= " AND er.dates <= '".$this->db->idate($date)."'";
$sql .= " AND er.datee >= '".$this->db->idate($date)."'";
}
2021-02-25 22:45:02 +01:00
if ($fk_user > 0) {
$sql .= ' AND (er.is_for_all = 1';
2021-04-26 19:12:23 +02:00
$sql .= ' OR er.fk_user = '.((int) $fk_user);
2021-03-30 11:36:50 +02:00
$sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
}
$sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
2017-10-13 13:10:36 +02:00
2021-08-28 01:45:53 +02:00
dol_syslog("ExpenseReportRule::getAllRule");
2017-10-13 13:10:36 +02:00
2021-09-27 15:41:58 +02:00
$resql = $this->db->query($sql);
2021-02-25 22:45:02 +01:00
if ($resql) {
2021-09-27 15:41:58 +02:00
while ($obj = $this->db->fetch_object($resql)) {
$rule = new ExpenseReportRule($this->db);
2021-02-25 22:45:02 +01:00
if ($rule->fetch($obj->rowid) > 0) {
$rules[$rule->id] = $rule;
} else {
2021-09-27 15:41:58 +02:00
dol_print_error($this->db);
2021-02-25 22:45:02 +01:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2021-09-27 15:41:58 +02:00
dol_print_error($this->db);
}
2017-10-13 13:10:36 +02:00
return $rules;
}
2017-10-13 13:10:36 +02:00
/**
* Return the label of group for the current object
2017-10-13 13:10:36 +02:00
*
* @return string
*/
public function getGroupLabel()
{
include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
2017-10-13 13:10:36 +02:00
2021-02-25 22:45:02 +01:00
if ($this->fk_usergroup > 0) {
$group = new UserGroup($this->db);
2021-02-25 22:45:02 +01:00
if ($group->fetch($this->fk_usergroup) > 0) {
2021-09-27 15:41:58 +02:00
return $group->name;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $group->error;
$this->errors[] = $this->error;
}
}
2017-10-13 13:10:36 +02:00
return '';
}
2017-10-13 13:10:36 +02:00
/**
* Return the name of user for the current object
2017-10-13 13:10:36 +02:00
*
* @return string
*/
public function getUserName()
{
include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
2017-10-13 13:10:36 +02:00
2021-02-25 22:45:02 +01:00
if ($this->fk_user > 0) {
$u = new User($this->db);
2021-02-25 22:45:02 +01:00
if ($u->fetch($this->fk_user) > 0) {
return dolGetFirstLastname($u->firstname, $u->lastname);
2020-05-21 15:05:19 +02:00
} else {
$this->error = $u->error;
$this->errors[] = $this->error;
}
}
2017-10-13 13:10:36 +02:00
return '';
}
}