--- /tmp/dsg/dolibarr/htdocs/fichinter/class/github_19.0.3_api_interventions.class.php +++ /tmp/dsg/dolibarr/htdocs/fichinter/class/client_api_interventions.class.php @@ -19,9 +19,3 @@ -/** - * \file htdocs/fichinter/class/api_interventions.class.php - * \ingroup fichinter - * \brief File of API to manage intervention - */ -use Luracast\Restler\RestException; - -require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php'; - + use Luracast\Restler\RestException; + + require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php'; @@ -37,444 +31,426 @@ - /** - * @var array $FIELDS Mandatory fields, checked when create and update object - */ - public static $FIELDS = array( - 'socid', - 'fk_project', - 'description', - ); - - /** - * @var array $FIELDS Mandatory fields, checked when create and update object - */ - public static $FIELDSLINE = array( - 'description', - 'date', - 'duree', - ); - - /** - * @var Fichinter $fichinter {@type fichinter} - */ - public $fichinter; - - /** - * Constructor - */ - public function __construct() - { - global $db, $conf; - $this->db = $db; - $this->fichinter = new Fichinter($this->db); - } - - /** - * Get properties of a Expense Report object - * Return an array with Expense Report information - * - * @param int $id ID of Expense Report - * @return Object Object with cleaned properties - * - * @throws RestException - */ - public function get($id) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->lire) { - throw new RestException(401); - } - - $result = $this->fichinter->fetch($id); - if (!$result) { - throw new RestException(404, 'Intervention not found'); - } - - if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $this->fichinter->fetchObjectLinked(); - return $this->_cleanObjectDatas($this->fichinter); - } - - /** - * List of interventions - * Return a list of interventions - * - * @param string $sortfield Sort field - * @param string $sortorder Sort order - * @param int $limit Limit for list - * @param int $page Page number - * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} - * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" - * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names - * @return array Array of order objects - * - * @throws RestException - */ - public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '') - { - global $db, $conf; - - if (!DolibarrApiAccess::$user->rights->ficheinter->lire) { - throw new RestException(401); - } - - $obj_ret = array(); - - // case of external user, $thirdparty_ids param is ignored and replaced by user's socid - $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; - - // If the internal user must only see his customers, force searching by him - $search_sale = 0; - if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) { - $search_sale = DolibarrApiAccess::$user->id; - } - - $sql = "SELECT t.rowid"; - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) - } - $sql .= " FROM ".MAIN_DB_PREFIX."fichinter AS t LEFT JOIN ".MAIN_DB_PREFIX."fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields - - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale - } - - $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')'; - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= " AND t.fk_soc = sc.fk_soc"; - } - if ($socids) { - $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")"; - } - if ($search_sale > 0) { - $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale - } - // Insert sale filter - if ($search_sale > 0) { - $sql .= " AND sc.fk_user = ".((int) $search_sale); - } - // Add sql filters - if ($sqlfilters) { - $errormessage = ''; - $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); - if ($errormessage) { - throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); - } - } - - $sql .= $this->db->order($sortfield, $sortorder); - if ($limit) { - if ($page < 0) { - $page = 0; - } - $offset = $limit * $page; - - $sql .= $this->db->plimit($limit + 1, $offset); - } - - dol_syslog("API Rest request"); - $result = $this->db->query($sql); - - if ($result) { - $num = $this->db->num_rows($result); - $min = min($num, ($limit <= 0 ? $num : $limit)); - $i = 0; - while ($i < $min) { - $obj = $this->db->fetch_object($result); - $fichinter_static = new Fichinter($this->db); - if ($fichinter_static->fetch($obj->rowid)) { - $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($fichinter_static), $properties); - } - $i++; - } - } else { - throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror()); - } - - return $obj_ret; - } - - /** - * Create intervention object - * - * @param array $request_data Request data - * @return int ID of intervention - */ - public function post($request_data = null) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { - throw new RestException(401, "Insuffisant rights"); - } - // Check mandatory fields - $result = $this->_validate($request_data); - foreach ($request_data as $field => $value) { - if ($field === 'caller') { - // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller - $this->fichinter->context['caller'] = $request_data['caller']; - continue; - } - - $this->fichinter->$field = $value; - } - - if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) { - throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors)); - } - - return $this->fichinter->id; - } - - - /** - * Get lines of an intervention - * - * @param int $id Id of intervention - * - * @url GET {id}/lines - * - * @return int - */ - /* TODO - public function getLines($id) - { - if(! DolibarrApiAccess::$user->rights->ficheinter->lire) { - throw new RestException(401); - } - - $result = $this->fichinter->fetch($id); - if( ! $result ) { - throw new RestException(404, 'Intervention not found'); - } - - if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - $this->fichinter->getLinesArray(); - $result = array(); - foreach ($this->fichinter->lines as $line) { - array_push($result,$this->_cleanObjectDatas($line)); - } - return $result; - } - */ - - /** - * Add a line to a given intervention - * - * @param int $id Id of intervention to update - * @param array $request_data Request data - * - * @url POST {id}/lines - * - * @return int - */ - public function postLine($id, $request_data = null) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { - throw new RestException(401, "Insuffisant rights"); - } - // Check mandatory fields - $result = $this->_validateLine($request_data); - - foreach ($request_data as $field => $value) { - if ($field === 'caller') { - // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller - $this->fichinter->context['caller'] = $request_data['caller']; - continue; - } - - $this->fichinter->$field = $value; - } - - if (!$result) { - throw new RestException(404, 'Intervention not found'); - } - - if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $updateRes = $this->fichinter->addLine( - DolibarrApiAccess::$user, - $id, - $this->fichinter->description, - $this->fichinter->date, - $this->fichinter->duree - ); - - if ($updateRes > 0) { - return $updateRes; - } else { - throw new RestException(400, $this->fichinter->error); - } - } - - /** - * Delete order - * - * @param int $id Order ID - * @return array - */ - public function delete($id) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) { - throw new RestException(401); - } - $result = $this->fichinter->fetch($id); - if (!$result) { - throw new RestException(404, 'Intervention not found'); - } - - if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - if (!$this->fichinter->delete(DolibarrApiAccess::$user)) { - throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error); - } - - return array( - 'success' => array( - 'code' => 200, - 'message' => 'Intervention deleted' - ) - ); - } - - /** - * Validate an intervention - * - * If you get a bad value for param notrigger check, provide this in body - * { - * "notrigger": 0 - * } - * - * @param int $id Intervention ID - * @param int $notrigger 1=Does not execute triggers, 0= execute triggers - * - * @url POST {id}/validate - * - * @return Object - */ - public function validate($id, $notrigger = 0) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { - throw new RestException(401, "Insuffisant rights"); - } - $result = $this->fichinter->fetch($id); - if (!$result) { - throw new RestException(404, 'Intervention not found'); - } - - if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger); - if ($result == 0) { - throw new RestException(304, 'Error nothing done. May be object is already validated'); - } - if ($result < 0) { - throw new RestException(500, 'Error when validating Intervention: '.$this->fichinter->error); - } - - $this->fichinter->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->fichinter); - } - - /** - * Close an intervention - * - * @param int $id Intervention ID - * - * @url POST {id}/close - * - * @return Object - */ - public function closeFichinter($id) - { - if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { - throw new RestException(401, "Insuffisant rights"); - } - $result = $this->fichinter->fetch($id); - if (!$result) { - throw new RestException(404, 'Intervention not found'); - } - - if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $result = $this->fichinter->setStatut(3); - - if ($result == 0) { - throw new RestException(304, 'Error nothing done. May be object is already closed'); - } - if ($result < 0) { - throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error); - } - - $this->fichinter->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->fichinter); - } - - /** - * Validate fields before create or update object - * - * @param array $data Data to validate - * @return array - * - * @throws RestException - */ - private function _validate($data) - { - $fichinter = array(); - foreach (Interventions::$FIELDS as $field) { - if (!isset($data[$field])) { - throw new RestException(400, "$field field missing"); - } - $fichinter[$field] = $data[$field]; - } - return $fichinter; - } - - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore - /** - * Clean sensible object datas - * - * @param Object $object Object to clean - * @return Object Object with cleaned properties - */ - protected function _cleanObjectDatas($object) - { - // phpcs:enable - $object = parent::_cleanObjectDatas($object); - - unset($object->labelStatus); - unset($object->labelStatusShort); - - return $object; - } - - /** - * Validate fields before create or update object - * - * @param array $data Data to validate - * @return array - * - * @throws RestException - */ - private function _validateLine($data) - { - $fichinter = array(); - foreach (Interventions::$FIELDSLINE as $field) { - if (!isset($data[$field])) { - throw new RestException(400, "$field field missing"); - } - $fichinter[$field] = $data[$field]; - } - return $fichinter; - } + + /** + * @var array $FIELDS Mandatory fields, checked when create and update object + */ + static $FIELDS = array( + 'socid', + 'fk_project', + 'description', + ); + + /** + * @var array $FIELDS Mandatory fields, checked when create and update object + */ + static $FIELDSLINE = array( + 'description', + 'date', + 'duree', + ); + + /** + * @var fichinter $fichinter {@type fichinter} + */ + public $fichinter; + + /** + * Constructor + */ + public function __construct() + { + global $db, $conf; + $this->db = $db; + $this->fichinter = new Fichinter($this->db); + } + + /** + * Get properties of a Expense Report object + * + * Return an array with Expense Report information + * + * @param int $id ID of Expense Report + * @return array|mixed Data without useless information + * + * @throws RestException + */ + public function get($id) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->lire) { + throw new RestException(401); + } + + $result = $this->fichinter->fetch($id); + if (!$result) { + throw new RestException(404, 'Intervention not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->fichinter->fetchObjectLinked(); + return $this->_cleanObjectDatas($this->fichinter); + } + + /** + * List of interventions + * + * Return a list of interventions + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit for list + * @param int $page Page number + * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" + * @return array Array of order objects + * + * @throws RestException + */ + public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') + { + global $db, $conf; + + $obj_ret = array(); + + // case of external user, $thirdparty_ids param is ignored and replaced by user's socid + $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; + + // If the internal user must only see his customers, force searching by him + $search_sale = 0; + if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; + + $sql = "SELECT t.rowid"; + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) + $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as t"; + + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale + + $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')'; + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc"; + if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")"; + if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale + // Insert sale filter + if ($search_sale > 0) { + $sql .= " AND sc.fk_user = ".$search_sale; + } + // Add sql filters + if ($sqlfilters) + { + if (!DolibarrApi::_checkFilters($sqlfilters)) + { + throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); + } + $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; + $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + + $sql .= $db->order($sortfield, $sortorder); + if ($limit) { + if ($page < 0) + { + $page = 0; + } + $offset = $limit * $page; + + $sql .= $db->plimit($limit + 1, $offset); + } + + dol_syslog("API Rest request"); + $result = $db->query($sql); + + if ($result) + { + $num = $db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + $i = 0; + while ($i < $min) + { + $obj = $db->fetch_object($result); + $fichinter_static = new Fichinter($db); + if ($fichinter_static->fetch($obj->rowid)) { + $obj_ret[] = $this->_cleanObjectDatas($fichinter_static); + } + $i++; + } + } + else { + throw new RestException(503, 'Error when retrieve intervention list : '.$db->lasterror()); + } + if (!count($obj_ret)) { + throw new RestException(404, 'No intervention found'); + } + return $obj_ret; + } + + /** + * Create intervention object + * + * @param array $request_data Request data + * @return int ID of intervention + */ + public function post($request_data = null) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { + throw new RestException(401, "Insuffisant rights"); + } + // Check mandatory fields + $result = $this->_validate($request_data); + foreach ($request_data as $field => $value) { + $this->fichinter->$field = $value; + } + + if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) { + throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors)); + } + + return $this->fichinter->id; + } + + + /** + * Get lines of an intervention + * + * @param int $id Id of intervention + * + * @url GET {id}/lines + * + * @return int + */ + /* TODO + public function getLines($id) + { + if(! DolibarrApiAccess::$user->rights->ficheinter->lire) { + throw new RestException(401); + } + + $result = $this->fichinter->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Intervention not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + $this->fichinter->getLinesArray(); + $result = array(); + foreach ($this->fichinter->lines as $line) { + array_push($result,$this->_cleanObjectDatas($line)); + } + return $result; + } + */ + + /** + * Add a line to given intervention + * + * @param int $id Id of intervention to update + * @param array $request_data Request data + * + * @url POST {id}/lines + * + * @return int + */ + public function postLine($id, $request_data = null) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { + throw new RestException(401, "Insuffisant rights"); + } + // Check mandatory fields + $result = $this->_validateLine($request_data); + + foreach ($request_data as $field => $value) { + $this->fichinter->$field = $value; + } + + if (!$result) { + throw new RestException(404, 'Intervention not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $updateRes = $this->fichinter->addLine( + DolibarrApiAccess::$user, + $id, + $this->fichinter->description, + $this->fichinter->date, + $this->fichinter->duree + ); + + if ($updateRes > 0) { + return $updateRes; + } else { + throw new RestException(400, $this->fichinter->error); + } + } + + /** + * Delete order + * + * @param int $id Order ID + * @return array + */ + public function delete($id) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) { + throw new RestException(401); + } + $result = $this->fichinter->fetch($id); + if (!$result) { + throw new RestException(404, 'Intervention not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if (!$this->fichinter->delete(DolibarrApiAccess::$user)) { + throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Intervention deleted' + ) + ); + } + + /** + * Validate an intervention + * + * If you get a bad value for param notrigger check, provide this in body + * { + * "notrigger": 0 + * } + * + * @param int $id Intervention ID + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers + * + * @url POST {id}/validate + * + * @return array + */ + public function validate($id, $notrigger = 0) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->creer) { + throw new RestException(401, "Insuffisant rights"); + } + $result = $this->fichinter->fetch($id); + if (!$result) { + throw new RestException(404, 'Intervention not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already validated'); + } + if ($result < 0) { + throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error); + } + + $this->fichinter->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->fichinter); + } + + /** + * Close an intervention + * + * @param int $id Intervention ID + * + * @url POST {id}/close + * + * @return array + */ + public function closeFichinter($id) + { + if (!DolibarrApiAccess::$user->rights->ficheinter->creer) + { + throw new RestException(401, "Insuffisant rights"); + } + $result = $this->fichinter->fetch($id); + if (!$result) { + throw new RestException(404, 'Intervention not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->fichinter->setStatut(3); + + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already closed'); + } + if ($result < 0) { + throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error); + } + + $this->fichinter->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->fichinter); + } + + /** + * Validate fields before create or update object + * + * @param array $data Data to validate + * @return array + * + * @throws RestException + */ + private function _validate($data) + { + $fichinter = array(); + foreach (Interventions::$FIELDS as $field) { + if (!isset($data[$field])) + throw new RestException(400, "$field field missing"); + $fichinter[$field] = $data[$field]; + } + return $fichinter; + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore + /** + * Clean sensible object datas + * + * @param object $object Object to clean + * @return array Array of cleaned object properties + */ + protected function _cleanObjectDatas($object) + { + // phpcs:enable + $object = parent::_cleanObjectDatas($object); + + unset($object->statuts_short); + unset($object->statuts_logo); + unset($object->statuts); + + return $object; + } + + /** + * Validate fields before create or update object + * + * @param array $data Data to validate + * @return array + * + * @throws RestException + */ + private function _validateLine($data) + { + $fichinter = array(); + foreach (Interventions::$FIELDSLINE as $field) { + if (!isset($data[$field])) + throw new RestException(400, "$field field missing"); + $fichinter[$field] = $data[$field]; + } + return $fichinter; + } --- /tmp/dsg/dolibarr/htdocs/fichinter/class/github_19.0.3_fichinter.class.php +++ /tmp/dsg/dolibarr/htdocs/fichinter/class/client_fichinter.class.php @@ -5 +5 @@ - * Copyright (C) 2011-2020 Juanjo Menent + * Copyright (C) 2011-2013 Juanjo Menent @@ -7 +7 @@ - * Copyright (C) 2015-2020 Charlene Benke + * Copyright (C) 2015 Charlie Benke @@ -9,2 +9 @@ - * Copyright (C) 2018-2020 Frédéric France - * Copyright (C) 2023 William Mead + * Copyright (C) 2018-2019 Frédéric France @@ -28 +27 @@ - * \ingroup fichinter + * \ingroup ficheinter @@ -39,0 +39 @@ + @@ -41,27 +41,26 @@ - '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'=>'isModEnabled("societe")', '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'=>'isModEnabled("project")', 'visible'=>-1, 'position'=>20), - 'fk_contrat' =>array('type'=>'integer', 'label'=>'Fk contrat', 'enabled'=>'$conf->contrat->enabled', '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), - 'ref_client' =>array('type'=>'varchar(255)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>36), - '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), - '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'=>'html', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2), - 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110), - 'note_public' =>array('type'=>'html', '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), - 'fk_statut' =>array('type'=>'integer', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500), + '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'=>1), + 'note_private' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>110), + 'note_public' =>array('type'=>'text', 'label'=>'NotePrivate', '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), @@ -69 +67,0 @@ - @@ -81 +79 @@ - * @var string Field with ID of parent key if this field has a parent + * @var int Field with ID of parent key if this field has a parent @@ -100,4 +98 @@ - /** - * @var int Thirdparty Id - */ - public $socid; + public $socid; // Id client @@ -108,5 +103,5 @@ - * Date creation record (datec) - * - * @var integer - */ - public $datec; + * Date creation record (datec) + * + * @var integer + */ + public $datec; @@ -120,9 +115,6 @@ - * Date modification record (tms) - * - * @var integer - */ - public $datem; - - /** - * @var int duration - */ + * Date modification record (tms) + * + * @var integer + */ + public $datem; + @@ -130,4 +121,0 @@ - - /** - * @var int status - */ @@ -142,2 +130,2 @@ - * @var int Contract ID - */ + * @var int ID + */ @@ -147,2 +135,2 @@ - * @var int Project ID - */ + * @var int ID + */ @@ -151,9 +138,0 @@ - /** - * Customer Ref - * @var string - */ - public $ref_client; - - /** - * @var array extraparams - */ @@ -162,3 +140,0 @@ - /** - * @var FichinterLigne[] lines - */ @@ -188,13 +163,0 @@ - * Date delivery - * @var string|int Delivery int - */ - public $date_delivery; - - /** - * Author Id - * @var int - */ - public $user_author_id; - - - /** @@ -205,2 +168,2 @@ - public function __construct($db) - { + public function __construct($db) + { @@ -208,3 +171,5 @@ - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + + $this->products = array(); + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -214,5 +179,5 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function load_state_board() - { - // phpcs:enable + * @return int <0 if KO, >0 if OK + */ + public function load_state_board() + { + // phpcs:enable @@ -227 +192,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) { + if (!$user->rights->societe->client->voir && !$user->socid) + { @@ -229 +195 @@ - $sql .= " WHERE sc.fk_user = ".((int) $user->id); + $sql .= " WHERE sc.fk_user = ".$user->id; @@ -235,2 +201,4 @@ - if ($resql) { - while ($obj = $this->db->fetch_object($resql)) { + if ($resql) + { + while ($obj = $this->db->fetch_object($resql)) + { @@ -241 +209,3 @@ - } else { + } + else + { @@ -253,3 +223,3 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function create($user, $notrigger = 0) + * @return int <0 if KO, >0 if OK + */ + public function create($user, $notrigger = 0) @@ -259 +229 @@ - $error = 0; + $error = 0; @@ -264 +234,2 @@ - if (!empty($this->ref)) { // We check that ref is not already used + if (!empty($this->ref)) // We check that ref is not already used + { @@ -266 +237,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -273,9 +245,5 @@ - if (!is_numeric($this->duration)) { - $this->duration = 0; - } - if (isset($this->ref_client)) { - $this->ref_client = trim($this->ref_client); - } - - if ($this->socid <= 0) { - $this->error = 'ErrorFicheinterCompanyDoesNotExist'; + if (!is_numeric($this->duration)) $this->duration = 0; + + if ($this->socid <= 0) + { + $this->error = 'ErrorBadParameterForFunc'; @@ -297 +264,0 @@ - $sql .= ", ref_client"; @@ -313,4 +280,3 @@ - $sql .= ", ".($this->ref_client ? "'".$this->db->escape($this->ref_client)."'" : "null"); - $sql .= ", ".((int) $conf->entity); - $sql .= ", ".((int) $user->id); - $sql .= ", ".((int) $user->id); + $sql .= ", ".$conf->entity; + $sql .= ", ".$user->id; + $sql .= ", ".$user->id; @@ -318,4 +284,4 @@ - $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->db->escape($this->modelpdf)."'"; + $sql .= ", ".($this->fk_project ? $this->fk_project : 0); + $sql .= ", ".($this->fk_contrat ? $this->fk_contrat : 0); + $sql .= ", ".$this->statut; @@ -328 +294,2 @@ - if ($result) { + if ($result) + { @@ -331 +298,2 @@ - if ($this->id) { + if ($this->id) + { @@ -333 +301 @@ - $sql = 'UPDATE '.MAIN_DB_PREFIX."fichinter SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id); + $sql = 'UPDATE '.MAIN_DB_PREFIX."fichinter SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".$this->id; @@ -337 +305,8 @@ - if (!$resql) { + if (!$resql) $error++; + } + + if (!$error) + { + $result = $this->insertExtraFields(); + if ($result < 0) + { @@ -342,7 +316,0 @@ - if (!$error) { - $result = $this->insertExtraFields(); - if ($result < 0) { - $error++; - } - } - @@ -350 +318,2 @@ - if (!$error && $this->origin && $this->origin_id) { + if (!$error && $this->origin && $this->origin_id) + { @@ -352,7 +321,6 @@ - if (!$ret) { - dol_print_error($this->db); - } - } - - - if (!$error && !$notrigger) { + if (!$ret) dol_print_error($this->db); + } + + + if (!$error && !$notrigger) + { @@ -361,3 +329 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) { $error++; } @@ -367 +333,2 @@ - if (!$error) { + if (!$error) + { @@ -370 +337,3 @@ - } else { + } + else + { @@ -376 +345,3 @@ - } else { + } + else + { @@ -388,17 +359,14 @@ - * @return int Return integer <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; - } - if (isset($this->ref_client)) { - $this->ref_client = trim($this->ref_client); - } - - $error = 0; + * @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; @@ -410,3 +378,2 @@ - $sql .= ", duree = ".((int) $this->duration); - $sql .= ", ref_client = ".($this->ref_client ? "'".$this->db->escape($this->ref_client)."'" : "null"); - $sql .= ", fk_projet = ".((int) $this->fk_project); + $sql .= ", duree = ".$this->duration; + $sql .= ", fk_projet = ".$this->fk_project; @@ -415,2 +382,2 @@ - $sql .= ", fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= ", fk_user_modif = ".$user->id; + $sql .= " WHERE rowid = ".$this->id; @@ -419,2 +386,4 @@ - if ($this->db->query($sql)) { - if (!$error) { + if ($this->db->query($sql)) + { + if (!$error) + { @@ -422 +391,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -427 +397,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -430,5 +401 @@ - if ($result < 0) { - $error++; - $this->db->rollback(); - return -1; - } + if ($result < 0) { $error++; $this->db->rollback(); return -1; } @@ -440 +407,3 @@ - } else { + } + else + { @@ -452,5 +421,5 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function fetch($rowid, $ref = '') - { - $sql = "SELECT f.rowid, f.ref, f.ref_client, f.description, f.fk_soc, f.fk_statut as status,"; + * @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,"; @@ -460 +429 @@ - $sql .= " f.duree, f.fk_projet as fk_project, f.note_public, f.note_private, f.model_pdf, f.last_main_doc, f.extraparams, fk_contrat, f.entity as entity"; + $sql .= " f.duree, f.fk_projet as fk_project, f.note_public, f.note_private, f.model_pdf, f.extraparams, fk_contrat"; @@ -464,4 +433,3 @@ - $sql .= " AND f.ref = '".$this->db->escape($ref)."'"; - } else { - $sql .= " WHERE f.rowid = ".((int) $rowid); - } + $sql .= " AND f.ref='".$this->db->escape($ref)."'"; + } + else $sql .= " WHERE f.rowid=".$rowid; @@ -471,2 +439,4 @@ - if ($resql) { - if ($this->db->num_rows($resql)) { + if ($resql) + { + if ($this->db->num_rows($resql)) + { @@ -477 +446,0 @@ - $this->ref_client = $obj->ref_client; @@ -480,2 +449 @@ - $this->status = $obj->status; - $this->statut = $obj->status; // deprecated + $this->statut = $obj->fk_statut; @@ -492 +460 @@ - $this->model_pdf = $obj->model_pdf; + $this->modelpdf = $obj->model_pdf; @@ -494,3 +462,2 @@ - $this->entity = $obj->entity; - - $this->user_creation_id = $obj->fk_user_author; + + $this->user_creation = $obj->fk_user_author; @@ -500 +467 @@ - $this->last_main_doc = $obj->last_main_doc; + if ($this->statut == 0) $this->brouillon = 1; @@ -509 +476,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -515,3 +483,3 @@ - - return 0; - } else { + } + else + { @@ -527,4 +495,6 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function setDraft($user) - { + * @return int <0 if KO, >0 if OK + */ + public function setDraft($user) + { + global $langs, $conf; + @@ -534,2 +504,3 @@ - if ($this->statut <= self::STATUS_DRAFT) { - return 0; + if ($this->statut <= self::STATUS_DRAFT) + { + return 0; @@ -539,2 +509,0 @@ - - $this->oldcopy = dol_clone($this, 2); @@ -546 +515 @@ - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= " WHERE rowid = ".$this->id; @@ -549 +518,12 @@ - if ($resql) { + if ($resql) + { + if (!$error) { + $this->oldcopy = clone $this; + } + + if (!$error) { + // Call trigger + $result = $this->call_trigger('FICHINTER_UNVALIDATE', $user); + if ($result < 0) $error++; + } + @@ -551,11 +531,3 @@ - // 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; + $this->statut = self::STATUS_DRAFT; + $this->db->commit(); + return 1; @@ -563,4 +535,6 @@ - $this->db->rollback(); - return -1; - } - } else { + $this->db->rollback(); + return -1; + } + } + else + { @@ -578,3 +552,3 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function setValid($user, $notrigger = 0) + * @return int <0 if KO, >0 if OK + */ + public function setValid($user, $notrigger = 0) @@ -587 +561,2 @@ - if ($this->status != self::STATUS_VALIDATED) { + if ($this->statut != 1) + { @@ -593 +568,2 @@ - if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life + if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life + { @@ -595 +571,3 @@ - } else { + } + else + { @@ -602 +580 @@ - $sql .= ", ref = '".$this->db->escape($num)."'"; + $sql .= ", ref = '".$num."'"; @@ -604,4 +582,3 @@ - $sql .= ", fk_user_valid = ".($user->id > 0 ? (int) $user->id : "null"); - $sql .= " WHERE rowid = ".((int) $this->id); - $sql .= " AND entity = ".((int) $this->entity); - + $sql .= ", fk_user_valid = ".$user->id; + $sql .= " WHERE rowid = ".$this->id; + $sql .= " AND entity = ".$conf->entity; @@ -612 +589,2 @@ - if (!$resql) { + if (!$resql) + { @@ -617 +595,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -620,3 +599 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) { $error++; } @@ -626 +603,2 @@ - if (!$error) { + if (!$error) + { @@ -630 +608,2 @@ - if (preg_match('/^[\(]?PROV/i', $this->ref)) { + if (preg_match('/^[\(]?PROV/i', $this->ref)) + { @@ -635 +614 @@ - $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".((int) $this->entity); + $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".$conf->entity; @@ -637,11 +616 @@ - if (!$resql) { - $error++; - $this->error = $this->db->lasterror(); - } - $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'ficheinter/".$this->db->escape($this->newref)."'"; - $sql .= " WHERE filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".$conf->entity; - $resql = $this->db->query($sql); - if (!$resql) { - $error++; - $this->error = $this->db->lasterror(); - } + if (!$resql) { $error++; $this->error = $this->db->lasterror(); } @@ -654 +623,2 @@ - if (!$error && file_exists($dirsource)) { + if (!$error && file_exists($dirsource)) + { @@ -657 +627,2 @@ - if (@rename($dirsource, $dirdest)) { + if (@rename($dirsource, $dirdest)) + { @@ -661 +632,2 @@ - foreach ($listoffiles as $fileentry) { + foreach ($listoffiles as $fileentry) + { @@ -674 +646,2 @@ - if (!$error) { + if (!$error) + { @@ -676,2 +649,2 @@ - $this->status = self::STATUS_VALIDATED; - $this->statut = self::STATUS_VALIDATED; // deprecated + $this->statut = 1; + $this->brouillon = 0; @@ -678,0 +652,4 @@ + } + + if (!$error) + { @@ -681 +658,3 @@ - } else { + } + else + { @@ -687,56 +665,0 @@ - - return 0; - } - - /** - * Close intervention - * - * @param User $user Objet user that close - * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers - * @return int Return integer <0 if KO, >0 if OK - */ - public function setClose($user, $notrigger = 0) - { - global $conf; - - $error = 0; - - if ($this->statut == self::STATUS_CLOSED) { - return 0; - } else { - $this->db->begin(); - - $now = dol_now(); - - $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element; - $sql .= ' SET fk_statut = ' . self::STATUS_CLOSED . ','; - $sql .= " datet = '" . $this->db->idate($now) . "',"; - $sql .= " fk_user_modif = " . ((int) $user->id); - $sql .= " WHERE rowid = " . ((int) $this->id); - $sql .= " AND fk_statut > " . self::STATUS_DRAFT; - $sql .= " AND entity = " . ((int) $conf->entity); - - if ($this->db->query($sql)) { - if (!$notrigger) { - // Call trigger - $result = $this->call_trigger('FICHINTER_CLOSE', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { - $this->statut = self::STATUS_CLOSED; - $this->db->commit(); - return 1; - } else { - $this->db->rollback(); - return -1; - } - } else { - $this->error = $this->db->lasterror(); - $this->db->rollback(); - return -1; - } - } @@ -751,0 +675,2 @@ + global $db; + @@ -754,2 +679,2 @@ - $this->author = new User($this->db); - $this->author->fetch($this->user_creation_id); + $this->author = new User($db); + $this->author->fetch($this->user_creation); @@ -775 +700 @@ - * @param null|array $moreparams Array to provide more information + * @param null|array $moreparams Array to provide more information @@ -780,3 +705,3 @@ - global $conf; - - $outputlangs->load("interventions"); + global $conf, $langs; + + $langs->load("interventions"); @@ -787,3 +712,3 @@ - if (!empty($this->model_pdf)) { - $modele = $this->model_pdf; - } elseif (getDolGlobalString('FICHEINTER_ADDON_PDF')) { + if ($this->modelpdf) { + $modele = $this->modelpdf; + } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) { @@ -810 +735 @@ - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -820 +745 @@ - // phpcs:enable + // phpcs:enable @@ -822 +747,2 @@ - if (empty($this->labelStatus) || empty($this->labelStatusShort)) { + if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) + { @@ -826,38 +752,15 @@ - $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft'); - $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated'); - $this->labelStatus[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced'); - $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done'); - $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft'); - $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated'); - $this->labelStatusShort[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced'); - $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done'); - } - - $statuscode = 'status'.$status; - if ($status == self::STATUS_BILLED || $status == self::STATUS_CLOSED) { - $statuscode = 'status6'; - } - return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statuscode, $mode); - } - - /** - * getTooltipContentArray - * - * @param array $params ex option, infologin - * @since v18 - * @return array - */ - public function getTooltipContentArray($params) - { - global $conf, $langs; - - $langs->load('fichinter'); - - $datas = []; - $datas['picto'] = img_picto('', $this->picto).' '.$langs->trans("Intervention").''; - if (isset($this->status)) { - $datas['picto'] .= ' '.$this->getLibStatut(5); - } - $datas['ref'] = '
'.$langs->trans('Ref').': '.$this->ref; - - return $datas; + $this->statuts[self::STATUS_DRAFT] = $langs->trans('Draft'); + $this->statuts[self::STATUS_VALIDATED] = $langs->trans('Validated'); + $this->statuts[self::STATUS_BILLED] = $langs->trans('StatusInterInvoiced'); + $this->statuts[self::STATUS_CLOSED] = $langs->trans('Done'); + $this->statuts_short[self::STATUS_DRAFT] = $langs->trans('Draft'); + $this->statuts_short[self::STATUS_VALIDATED] = $langs->trans('Validated'); + $this->statuts_short[self::STATUS_BILLED] = $langs->trans('StatusInterInvoiced'); + $this->statuts_short[self::STATUS_CLOSED] = $langs->trans('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); @@ -873 +775,0 @@ - * @param string $morecss Add more css on link @@ -876 +778 @@ - public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '') + public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1) @@ -880,4 +781,0 @@ - if (!empty($conf->dol_no_mouse_hover)) { - $notooltip = 1; // Force disable tooltips - } - @@ -885,13 +783,5 @@ - $params = [ - 'id' => $this->id, - 'objecttype' => $this->element, - 'option' => $option, - ]; - $classfortooltip = 'classfortooltip'; - $dataparams = ''; - if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { - $classfortooltip = 'classforajaxtooltip'; - $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"'; - $label = ''; - } else { - $label = implode($this->getTooltipContentArray($params)); + + $label = ''.$langs->trans("ShowIntervention").''; + $label .= '
'.$langs->trans('Ref').': '.$this->ref; + if (isset($this->status)) { + $label .= '
'.$langs->trans("Status").": ".$this->getLibStatut(5); @@ -902 +792,2 @@ - if ($option !== 'nolink') { + if ($option !== 'nolink') + { @@ -905,7 +796,3 @@ - if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { - $add_save_lastsearch_values = 1; - } - if ($add_save_lastsearch_values) { - $url .= '&save_lastsearch_values=1'; - } - } + 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'; + } @@ -914,2 +801,4 @@ - if (empty($notooltip)) { - if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { + if (empty($notooltip)) + { + if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) + { @@ -919,11 +808,12 @@ - $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"'); - $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"'; - } else { - $linkclose = ($morecss ? ' class="'.$morecss.'"' : ''); - } - - if ($option == 'nolink' || empty($url)) { - $linkstart = '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 = 'picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1); - } - - if ($withpicto != 2) { - $result .= $this->ref; - } - + 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; @@ -949,2 +829,2 @@ - $hookmanager->initHooks(array('interventiondao')); - $parameters = array('id'=>$this->id, 'getnomurl' => &$result); + $hookmanager->initHooks(array('intervnetiondao')); + $parameters = array('id'=>$this->id, 'getnomurl'=>$result); @@ -952,5 +832,2 @@ - if ($reshook > 0) { - $result = $hookmanager->resPrint; - } else { - $result .= $hookmanager->resPrint; - } + if ($reshook > 0) $result = $hookmanager->resPrint; + else $result .= $hookmanager->resPrint; @@ -974 +851,2 @@ - if (getDolGlobalString('FICHEINTER_ADDON')) { + if (!empty($conf->global->FICHEINTER_ADDON)) + { @@ -977,2 +855,2 @@ - $file = "mod_" . getDolGlobalString('FICHEINTER_ADDON').".php"; - $classname = "mod_" . getDolGlobalString('FICHEINTER_ADDON'); + $file = "mod_".$conf->global->FICHEINTER_ADDON.".php"; + $classname = "mod_".$conf->global->FICHEINTER_ADDON; @@ -999 +877,2 @@ - if ($numref != "") { + if ($numref != "") + { @@ -1001 +880,3 @@ - } else { + } + else + { @@ -1005 +886,3 @@ - } else { + } + else + { @@ -1019,0 +903,2 @@ + global $conf; + @@ -1028 +913 @@ - $sql .= " WHERE f.rowid = ".((int) $id); + $sql .= " WHERE f.rowid = ".$id; @@ -1031,2 +916,4 @@ - if ($resql) { - if ($this->db->num_rows($resql)) { + if ($resql) + { + if ($this->db->num_rows($resql)) + { @@ -1041,3 +928,16 @@ - $this->user_creation_id = $obj->fk_user_author; - $this->user_validation_id = $obj->fk_user_valid; - $this->user_modification_id = $obj->fk_user_modification; + $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; + } @@ -1046 +946,3 @@ - } else { + } + else + { @@ -1056,3 +958,3 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function delete(User $user, $notrigger = 0) + * @return int <0 if KO, >0 if OK + */ + public function delete($user, $notrigger = 0) @@ -1067 +969,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -1070,5 +973 @@ - if ($result < 0) { - $error++; - $this->db->rollback(); - return -1; - } + if ($result < 0) { $error++; $this->db->rollback(); return -1; } @@ -1079 +978,2 @@ - if (!$error) { + if (!$error) + { @@ -1081,3 +981 @@ - if ($res < 0) { - $error++; - } + if ($res < 0) $error++; @@ -1087 +985,2 @@ - if (!$error) { + if (!$error) + { @@ -1089 +988,2 @@ - if ($res < 0) { + if ($res < 0) + { @@ -1095,4 +995,14 @@ - 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).")"; + 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 = ".$this->id.")"; + + $resql = $this->db->query($sql); + if (!$resql) $error++; + } + + if (!$error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet"; + $sql .= " WHERE fk_fichinter = ".$this->id; @@ -1101,16 +1011,5 @@ - 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) { + if (!$resql) $error++; + } + + if (!$error) + { @@ -1119,6 +1018,5 @@ - if ($res < 0) { - $error++; - } - } - - if (!$error) { + if ($res < 0) $error++; + } + + if (!$error) + { @@ -1127 +1025 @@ - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= " WHERE rowid = ".$this->id; @@ -1131,10 +1029,5 @@ - 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(0); // Deleting files physically is done later with the dol_delete_dir_recursive - $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive - + if (!$resql) $error++; + } + + if (!$error) + { @@ -1143 +1036,2 @@ - if ($conf->ficheinter->dir_output) { + if ($conf->ficheinter->dir_output) + { @@ -1146 +1040,2 @@ - if (file_exists($file)) { + if (file_exists($file)) + { @@ -1149 +1044,2 @@ - if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers + if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers + { @@ -1155,2 +1051,4 @@ - if (file_exists($dir)) { - if (!dol_delete_dir_recursive($dir)) { + if (file_exists($dir)) + { + if (!dol_delete_dir_recursive($dir)) + { @@ -1165 +1063,2 @@ - if (!$error) { + if (!$error) + { @@ -1168 +1067,3 @@ - } else { + } + else + { @@ -1174 +1075 @@ - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -1180,6 +1081,9 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function set_date_delivery($user, $date_delivery) - { - // phpcs:enable - if ($user->hasRight('ficheinter', 'creer')) { + * @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) + { @@ -1188 +1092 @@ - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= " WHERE rowid = ".$this->id; @@ -1191 +1095,2 @@ - if ($this->db->query($sql)) { + if ($this->db->query($sql)) + { @@ -1194 +1099,3 @@ - } else { + } + else + { @@ -1200,5 +1107,3 @@ - - return 0; - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -1210,6 +1115,9 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function set_description($user, $description) - { - // phpcs:enable - if ($user->hasRight('ficheinter', 'creer')) { + * @return int <0 if KO, >0 if OK + */ + public function set_description($user, $description) + { + // phpcs:enable + global $conf; + + if ($user->rights->ficheinter->creer) + { @@ -1219,3 +1127,4 @@ - $sql .= " WHERE rowid = ".((int) $this->id); - - if ($this->db->query($sql)) { + $sql .= " WHERE rowid = ".$this->id; + + if ($this->db->query($sql)) + { @@ -1224 +1133,3 @@ - } else { + } + else + { @@ -1230,6 +1141,4 @@ - - return 0; - } - - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -1241,6 +1150,9 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function set_contrat($user, $contractid) - { - // phpcs:enable - if ($user->hasRight('ficheinter', 'creer')) { + * @return int <0 if ko, >0 if ok + */ + public function set_contrat($user, $contractid) + { + // phpcs:enable + global $conf; + + if ($user->rights->ficheinter->creer) + { @@ -1248,4 +1160,5 @@ - $sql .= " SET fk_contrat = ".((int) $contractid); - $sql .= " WHERE rowid = ".((int) $this->id); - - if ($this->db->query($sql)) { + $sql .= " SET fk_contrat = '".$contractid."'"; + $sql .= " WHERE rowid = ".$this->id; + + if ($this->db->query($sql)) + { @@ -1254 +1167,3 @@ - } else { + } + else + { @@ -1259 +1173,0 @@ - @@ -1281 +1195 @@ - foreach ($this->lines as $line) { + foreach ($this->lines as $line) @@ -1283 +1196,0 @@ - } @@ -1289 +1202,2 @@ - if (!empty($socid) && $socid != $this->socid) { + if (!empty($socid) && $socid != $this->socid) + { @@ -1292 +1206,2 @@ - if ($objsoc->fetch($socid) > 0) { + if ($objsoc->fetch($socid) > 0) + { @@ -1294,2 +1209,2 @@ - //$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->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); @@ -1305,2 +1220 @@ - $this->status = self::STATUS_DRAFT; - $this->statut = self::STATUS_DRAFT; // deprecated + $this->statut = 0; @@ -1310 +1224 @@ - $this->user_validation_id = 0; + $this->user_valid = ''; @@ -1313 +1226,0 @@ - @@ -1319,5 +1232,4 @@ - if ($result < 0) { - $error++; - } - - if (!$error) { + if ($result < 0) $error++; + + if (!$error) + { @@ -1325,2 +1237,3 @@ - foreach ($this->lines as $line) { - $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration, $line->array_options); + foreach ($this->lines as $line) + { + $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration); @@ -1330 +1243,2 @@ - if (is_object($hookmanager)) { + if (is_object($hookmanager)) + { @@ -1334,4 +1248 @@ - if ($reshook < 0) { - $this->setErrorsFromObject($hookmanager); - $error++; - } + if ($reshook < 0) $error++; @@ -1344 +1255,2 @@ - if (!$error) { + if (!$error) + { @@ -1347 +1259,3 @@ - } else { + } + else + { @@ -1365 +1279 @@ - public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = []) + public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '') @@ -1369 +1283,2 @@ - if ($this->status == self::STATUS_DRAFT) { + if ($this->statut == 0) + { @@ -1377,2 +1292 @@ - $line->date = $date_intervention; - $line->datei = $date_intervention; // For backward compatibility + $line->datei = $date_intervention; @@ -1387 +1301,2 @@ - if ($result >= 0) { + if ($result >= 0) + { @@ -1390 +1305,3 @@ - } else { + } + else + { @@ -1396,2 +1312,0 @@ - - return 0; @@ -1410 +1325 @@ - global $langs; + global $user, $langs, $conf; @@ -1417 +1331,0 @@ - $this->ref_client = 'SPECIMEN CLIENT'; @@ -1426 +1340,2 @@ - while ($xnbp < $nbp) { + while ($xnbp < $nbp) + { @@ -1429,2 +1344 @@ - $line->date = ($now - 3600 * (1 + $xnbp)); - $line->datei = ($now - 3600 * (1 + $xnbp)); // For backward compatibility + $line->datei = ($now - 3600 * (1 + $xnbp)); @@ -1440 +1354 @@ - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -1444,5 +1358,5 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function fetch_lines() - { - // phpcs:enable + * @return int <0 if KO, >0 if OK + */ + public function fetch_lines() + { + // phpcs:enable @@ -1451,4 +1365,3 @@ - $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"; + $sql = 'SELECT rowid, fk_fichinter, description, duree, date, rang'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet'; + $sql .= ' WHERE fk_fichinter = '.$this->id.' ORDER BY rang ASC, date ASC'; @@ -1457 +1369,0 @@ - @@ -1459 +1371,2 @@ - if ($resql) { + if ($resql) + { @@ -1462 +1375,2 @@ - while ($i < $num) { + while ($i < $num) + { @@ -1473 +1387 @@ - $line->datei = $this->db->jdate($objp->date); // For backward compatibility + $line->datei = $this->db->jdate($objp->date); @@ -1476 +1389,0 @@ - $line->fetch_optionals(); @@ -1478,0 +1392 @@ + @@ -1484 +1398,3 @@ - } else { + } + else + { @@ -1493,6 +1409,6 @@ - * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test - * @param int $origin_id Old thirdparty id - * @param int $dest_id New thirdparty id - * @return bool - */ - public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id) + * @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) @@ -1504,97 +1420 @@ - return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables); - } - - /** - * Set customer reference number - * - * @param User $user Object user that modify - * @param string $ref_client Customer reference - * @param int $notrigger 1=Does not execute triggers, 0= execute triggers - * @return int Return integer <0 if ko, >0 if ok - */ - public function setRefClient($user, $ref_client, $notrigger = 0) - { - // phpcs:enable - if ($user->hasRight('ficheinter', 'creer')) { - $error = 0; - - $this->db->begin(); - - $this->oldcopy = dol_clone($this); - - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'"); - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG); - $resql = $this->db->query($sql); - if (!$resql) { - $this->errors[] = $this->db->error(); - $error++; - } - - if (!$error) { - $this->ref_client = $ref_client; - } - - if (!$notrigger && empty($error)) { - // Call trigger - $result = $this->call_trigger('FICHINTER_MODIFY', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { - $this->db->commit(); - return 1; - } else { - foreach ($this->errors as $errmsg) { - dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } - } else { - return -1; - } - } - - /** - * Return clicable link of object (with eventually picto) - * - * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) - * @param array $arraydata Array of data - * @return string HTML Code for Kanban thumb. - */ - public function getKanbanView($option = '', $arraydata = null) - { - global $langs; - - $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']); - - $return = '
'; - $return .= '
'; - $return .= ''; - $return .= img_picto('', $this->picto); - $return .= ''; - $return .= '
'; - $return .= ''.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).''; - if ($selected >= 0) { - $return .= ''; - } - if (!empty($arraydata['thirdparty'])) { - $tmpthirdparty = $arraydata['thirdparty']; - $return .= '
'.$tmpthirdparty->getNomUrl(1).''; - } - if (property_exists($this, 'duration')) { - $return .= '
'.$langs->trans("Duration").' : '.convertSecondToTime($this->duration, 'allhourmin').''; - } - if (method_exists($this, 'getLibStatut')) { - $return .= '
'.$this->getLibStatut(3).'
'; - } - $return .= '
'; - $return .= '
'; - $return .= '
'; - return $return; + return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); @@ -1610,3 +1430,3 @@ - * @var DoliDB Database handler. - */ - public $db; + * @var DoliDB Database handler. + */ + public $db; @@ -1621,2 +1441,2 @@ - * @var int ID - */ + * @var int ID + */ @@ -1625,13 +1445,3 @@ - public $desc; // Description ligne - - /** - * @var int Date of intervention - */ - public $date; // Date intervention - /** - * @var int Date of intervention - * @deprecated - */ - public $datei; // Date intervention - - public $duration; // Duration of intervention + public $desc; // Description ligne + public $datei; // Date intervention + public $duration; // Duree de l'intervention @@ -1639,2 +1448,0 @@ - public $tva_tx; - public $subprice; @@ -1653 +1461 @@ - * @var string Field with ID of parent key if this field has a parent + * @var int Field with ID of parent key if this field has a parent @@ -1657,2 +1464,0 @@ - - @@ -1673 +1479 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -1676,0 +1483,5 @@ + $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 = '.$rowid; + @@ -1678,8 +1489,4 @@ - - $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft'; - $sql .= ' WHERE ft.rowid = '.((int) $rowid); - - $resql = $this->db->query($sql); - if ($resql) { - $objp = $this->db->fetch_object($resql); + $result = $this->db->query($sql); + if ($result) + { + $objp = $this->db->fetch_object($result); @@ -1687 +1494 @@ - $this->id = $objp->rowid; + $this->id = $objp->rowid; @@ -1689,2 +1496 @@ - $this->date = $this->db->jdate($objp->date); - $this->datei = $this->db->jdate($objp->date); // For backward compatibility + $this->datei = $this->db->jdate($objp->datei); @@ -1695,4 +1501 @@ - $this->db->free($resql); - - $this->fetch_optionals(); - + $this->db->free($result); @@ -1700 +1503,3 @@ - } else { + } + else + { @@ -1711 +1516 @@ - * @return int Return integer <0 if ko, >0 if ok + * @return int <0 if ko, >0 if ok @@ -1715 +1520,3 @@ - $error = 0; + global $langs, $conf; + + $error = 0; @@ -1719,4 +1525,0 @@ - if (empty($this->date) && !empty($this->datei)) { // For backward compatibility - $this->date = $this->datei; - } - @@ -1726 +1529,2 @@ - if ($rangToUse == -1) { + if ($rangToUse == -1) + { @@ -1729 +1533 @@ - $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter); + $sql .= ' WHERE fk_fichinter ='.$this->fk_fichinter; @@ -1731 +1535,2 @@ - if ($resql) { + if ($resql) + { @@ -1734 +1539,3 @@ - } else { + } + else + { @@ -1744 +1551 @@ - $sql .= " VALUES (".((int) $this->fk_fichinter).","; + $sql .= " VALUES (".$this->fk_fichinter.","; @@ -1746,3 +1553,3 @@ - $sql .= " '".$this->db->idate($this->date)."',"; - $sql .= " ".((int) $this->duration).","; - $sql .= ' '.((int) $rangToUse); + $sql .= " '".$this->db->idate($this->datei)."',"; + $sql .= " ".$this->duration.","; + $sql .= ' '.$rangToUse; @@ -1753 +1560,2 @@ - if ($resql) { + if ($resql) + { @@ -1757 +1565,2 @@ - if (!$error) { + if (!$error) + { @@ -1759 +1568,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -1767 +1577,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -1770 +1581,2 @@ - if (!$notrigger) { + if (!$notrigger) + { @@ -1773,3 +1585 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) { $error++; } @@ -1783 +1593,3 @@ - } else { + } + else + { @@ -1787 +1599,3 @@ - } else { + } + else + { @@ -1800 +1614 @@ - * @return int Return integer <0 if ko, >0 if ok + * @return int <0 if ko, >0 if ok @@ -1804,7 +1618,5 @@ - $error = 0; - - if (empty($this->date) && !empty($this->datei)) { // For backward compatibility - $this->date = $this->datei; - } - - $this->db->begin(); + global $langs, $conf; + + $error = 0; + + $this->db->begin(); @@ -1814,5 +1626,5 @@ - $sql .= " description = '".$this->db->escape($this->desc)."',"; - $sql .= " date = '".$this->db->idate($this->date)."',"; - $sql .= " duree = ".((int) $this->duration).","; - $sql .= " rang = ".((int) $this->rang); - $sql .= " WHERE rowid = ".((int) $this->id); + $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 = ".$this->id; @@ -1822,2 +1634,4 @@ - if ($resql) { - if (!$error) { + if ($resql) + { + if (!$error) + { @@ -1825 +1639,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -1831,2 +1646,4 @@ - if ($result > 0) { - if (!$notrigger) { + if ($result > 0) + { + if (!$notrigger) + { @@ -1834,4 +1651,2 @@ - $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user); - if ($result < 0) { - $error++; - } + $result = $this->call_trigger('LINEFICHINTER_UPDATE', $user); + if ($result < 0) { $error++; } @@ -1842 +1657,2 @@ - if (!$error) { + if (!$error) + { @@ -1845 +1661,3 @@ - } else { + } + else + { @@ -1850 +1668,3 @@ - } else { + } + else + { @@ -1857 +1677 @@ - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -1861 +1681 @@ - * @return int Return integer <0 si ko, >0 si ok + * @return int <0 si ko, >0 si ok @@ -1865 +1685 @@ - // phpcs:enable + // phpcs:enable @@ -1872 +1692 @@ - $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter); + $sql .= " WHERE fk_fichinter=".$this->fk_fichinter; @@ -1876 +1696,2 @@ - if ($resql) { + if ($resql) + { @@ -1879,3 +1700 @@ - if (!empty($obj->total_duration)) { - $total_duration = $obj->total_duration; - } + if (!empty($obj->total_duration)) $total_duration = $obj->total_duration; @@ -1884,4 +1703,4 @@ - $sql .= " SET duree = ".((int) $total_duration); - $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null"); - $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null"); - $sql .= " WHERE rowid = ".((int) $this->fk_fichinter); + $sql .= " SET duree = ".$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 = ".$this->fk_fichinter; @@ -1891 +1710,2 @@ - if ($resql) { + if ($resql) + { @@ -1894 +1714,3 @@ - } else { + } + else + { @@ -1899 +1721,3 @@ - } else { + } + else + { @@ -1914,0 +1739,2 @@ + global $langs, $conf; + @@ -1917,31 +1743,8 @@ - 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 { + if ($this->statut == 0) + { + dol_syslog(get_class($this)."::deleteline lineid=".$this->id); + $this->db->begin(); + + $result = $this->deleteExtraFields(); + if ($result < 0) { + $error++; @@ -1951,4 +1754,36 @@ - } else { - $this->error = $this->db->error()." sql=".$sql; - $this->db->rollback(); - return -1; + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".$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; --- /tmp/dsg/dolibarr/htdocs/fichinter/class/github_19.0.3_fichinterrec.class.php +++ /tmp/dsg/dolibarr/htdocs/fichinter/class/client_fichinterrec.class.php @@ -22 +22 @@ - * along with this program. If not, see . + * along with this program. If not, see . @@ -27 +27 @@ - * \ingroup fichinter + * \ingroup facture @@ -41,10 +41,6 @@ - public $element = 'fichinterrec'; - public $table_element = 'fichinter_rec'; - public $table_element_line = 'fichinterdet_rec'; - - /** - * @var string Fieldname with ID of parent key if this field has a parent - */ - public $fk_element = 'fk_fichinter'; - - /** + public $element = 'fichinterrec'; + public $table_element = 'fichinter_rec'; + public $table_element_line = 'fichinter_rec'; + public $fk_element = 'fk_fichinter'; + + /** @@ -53,831 +49,768 @@ - protected $table_ref_field = 'title'; - - /** - * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png - */ - public $picto = 'intervention'; - - - /** - * @var string title - */ - public $title; - public $number; - public $date; - public $amount; - public $tva; - public $total; - - /** - * @var int - */ - public $auto_validate; - - /** - * @var int Frequency - */ - public $frequency; - - public $id_origin; - - /** - * @var string Unit frequency - */ - public $unit_frequency; - - /** - * @var int Proposal Id - */ - public $propalid; - - public $date_last_gen; - public $date_when; - - /** - * @var int number of generation done - */ - public $nb_gen_done; - - /** - * @var int number of maximum generation - */ - public $nb_gen_max; - - /** - * int rank - */ - public $rang; - public $special_code; - - public $usenewprice = 0; - - /** - * Constructor - * - * @param DoliDB $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - - //status dans l'ordre de l'intervention - $this->labelStatus[0] = 'Draft'; - $this->labelStatus[1] = 'Closed'; - - $this->labelStatusShort[0] = 'Draft'; - $this->labelStatusShort[1] = 'Closed'; - } - - /** - * 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($this->status, $mode); - } - - - /** - * Create a predefined fichinter - * - * @param User $user User object - * @param int $notrigger no trigger - * @return int Return integer <0 if KO, id of fichinter if OK - */ - public function create($user, $notrigger = 0) - { - global $conf; - - $error = 0; - $now = dol_now(); - - // Clean parameters - $this->title = trim($this->title); - $this->description = trim($this->description); - - - $this->db->begin(); - - // Load fichinter model - $fichintsrc = new Fichinter($this->db); - - $result = $fichintsrc->fetch($this->id_origin); - if ($result > 0) { - $result = $fichintsrc->fetch_lines(); // to get all lines - } - - if ($result > 0) { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter_rec ("; - $sql .= "title"; - $sql .= ", fk_soc"; - $sql .= ", entity"; - $sql .= ", datec"; - $sql .= ", duree"; - $sql .= ", description"; - $sql .= ", note_private"; - $sql .= ", note_public"; - $sql .= ", fk_user_author"; - $sql .= ", fk_projet"; - $sql .= ", fk_contrat"; - $sql .= ", modelpdf"; - $sql .= ", frequency"; - $sql .= ", unit_frequency"; - $sql .= ", date_when"; - $sql .= ", date_last_gen"; - $sql .= ", nb_gen_done"; - $sql .= ", nb_gen_max"; - // $sql.= ", auto_validate"; - $sql .= ") VALUES ("; - $sql .= "'".$this->db->escape($this->title)."'"; - $sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : 'null'); - $sql .= ", ".((int) $conf->entity); - $sql .= ", '".$this->db->idate($now)."'"; - $sql .= ", ".(!empty($fichintsrc->duration) ? ((int) $fichintsrc->duration) : '0'); - $sql .= ", ".(!empty($this->description) ? ("'".$this->db->escape($this->description)."'") : "null"); - $sql .= ", ".(!empty($fichintsrc->note_private) ? ("'".$this->db->escape($fichintsrc->note_private)."'") : "null"); - $sql .= ", ".(!empty($fichintsrc->note_public) ? ("'".$this->db->escape($fichintsrc->note_public)."'") : "null"); - $sql .= ", ".((int) $user->id); - // si c'est la même société on conserve les liens vers le projet et le contrat - if ($this->socid == $fichintsrc->socid) { - $sql .= ", ".(!empty($fichintsrc->fk_project) ? ((int) $fichintsrc->fk_project) : "null"); - $sql .= ", ".(!empty($fichintsrc->fk_contrat) ? ((int) $fichintsrc->fk_contrat) : "null"); - } else { - $sql .= ", null, null"; - } - - $sql .= ", ".(!empty($fichintsrc->model_pdf) ? "'".$this->db->escape($fichintsrc->model_pdf)."'" : "''"); - - // Frequency - $sql .= ", ".(!empty($this->frequency) ? ((int) $this->frequency) : "null"); - $sql .= ", '".$this->db->escape($this->unit_frequency)."'"; - $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'null'); - $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'null'); - $sql .= ", 0"; // we start at 0 - $sql .= ", ".((int) $this->nb_gen_max); - // $sql.= ", ".$this->auto_validate; - $sql .= ")"; - - if ($this->db->query($sql)) { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); - - /* - * Lines - */ - $num = count($fichintsrc->lines); - for ($i = 0; $i < $num; $i++) { - //var_dump($fichintsrc->lines[$i]); - $result_insert = $this->addline( - $fichintsrc->lines[$i]->desc, - $fichintsrc->lines[$i]->duration, - $fichintsrc->lines[$i]->date, - $fichintsrc->lines[$i]->rang, - $fichintsrc->lines[$i]->subprice, - $fichintsrc->lines[$i]->qty, - $fichintsrc->lines[$i]->tva_tx, - $fichintsrc->lines[$i]->fk_product, - $fichintsrc->lines[$i]->remise_percent, - 'HT', - 0, - '', - 0, - $fichintsrc->lines[$i]->product_type, - $fichintsrc->lines[$i]->special_code, - !empty($fichintsrc->lines[$i]->label) ? $fichintsrc->lines[$i]->label : "", - $fichintsrc->lines[$i]->fk_unit - ); - - if ($result_insert < 0) { - $error++; - } - } - - if ($error) { - $this->db->rollback(); - return -1; - } else { - $this->db->commit(); - return $this->id; - } - } else { - $this->error = $this->db->error().' sql='.$sql; - $this->db->rollback(); - return -2; - } - } else { - $this->db->rollback(); - return -1; - } - } - - - /** - * Get the template of intervention object and lines - * - * @param int $rowid Id of object to load - * @param string $ref Reference of fichinter - * @param string $ref_ext External reference of fichinter - * @return int >0 if OK, <0 if KO, 0 if not found - */ - public function fetch($rowid = 0, $ref = '', $ref_ext = '') - { - $sql = 'SELECT f.title as title, f.fk_soc'; - $sql .= ', f.datec, f.duree, f.fk_projet, f.fk_contrat, f.description'; - $sql .= ', f.note_private, f.note_public, f.fk_user_author'; - $sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate'; - $sql .= ', f.note_private, f.note_public, f.fk_user_author'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinter_rec as f'; - if ($rowid > 0) { - $sql .= " WHERE f.rowid = ".((int) $rowid); - } elseif ($ref) { - $sql .= " WHERE f.title = '".$this->db->escape($ref)."'"; - } - - dol_syslog(get_class($this)."::fetch rowid=".$rowid, LOG_DEBUG); - - $result = $this->db->query($sql); - if ($result) { - if ($this->db->num_rows($result)) { - $obj = $this->db->fetch_object($result); - - $this->id = $rowid; - $this->title = $obj->title; - $this->ref = $obj->title; - $this->description = $obj->description; - $this->datec = $obj->datec; - $this->duration = $obj->duree; - $this->socid = $obj->fk_soc; - $this->status = 0; - $this->statut = 0; // deprecated - $this->fk_project = $obj->fk_projet; - $this->fk_contrat = $obj->fk_contrat; - $this->note_private = $obj->note_private; - $this->note_public = $obj->note_public; - $this->user_author = $obj->fk_user_author; - $this->model_pdf = empty($obj->model_pdf) ? "" : $obj->model_pdf; - $this->rang = !empty($obj->rang) ? $obj->rang : ""; - $this->special_code = !empty($obj->special_code) ? $obj->special_code : ""; - $this->frequency = $obj->frequency; - $this->unit_frequency = $obj->unit_frequency; - $this->date_when = $this->db->jdate($obj->date_when); - $this->date_last_gen = $this->db->jdate($obj->date_last_gen); - $this->nb_gen_done = $obj->nb_gen_done; - $this->nb_gen_max = $obj->nb_gen_max; - $this->auto_validate = $obj->auto_validate; - - // Lines - $result = $this->fetch_lines(); - if ($result < 0) { - $this->error = $this->db->error(); - return -3; - } - return 1; - } else { - $this->error = 'Interventional with id '.$rowid.' not found sql='.$sql; - dol_syslog(get_class($this).'::Fetch Error '.$this->error, LOG_ERR); - return -2; - } - } else { - $this->error = $this->db->error(); - return -1; - } - } - - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Load all lines of template of intervention into this->lines - * - * @param int $sall sall - * @return int 1 if OK, < 0 if KO - */ - public function fetch_lines($sall = 0) - { - // phpcs:enable - $this->lines = array(); - - $sql = 'SELECT l.rowid, l.fk_product, l.product_type as product_type, l.label as custom_label, l.description,'; - $sql .= ' l.price, l.qty, l.tva_tx, l.remise_percent, l.subprice, l.duree, l.date,'; - $sql .= ' l.total_ht, l.total_tva, l.total_ttc,'; - $sql .= ' l.rang, l.special_code,'; - $sql .= ' l.fk_unit, p.ref as product_ref, p.fk_product_type as fk_product_type,'; - $sql .= ' p.label as product_label, p.description as product_desc'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet_rec as l'; - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; - $sql .= ' WHERE l.fk_fichinter = '.((int) $this->id); - - dol_syslog('FichinterRec::fetch_lines', LOG_DEBUG); - - $result = $this->db->query($sql); - if ($result) { - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num) { - $objp = $this->db->fetch_object($result); - - $line = new FichinterLigne($this->db); - $line->id = $objp->rowid; - $line->label = $objp->custom_label; // Label line - $line->desc = $objp->description; // Description line - $line->product_type = $objp->product_type; // Type of line - $line->product_ref = $objp->product_ref; // Ref product - $line->product_label = $objp->product_label; // Label product - $line->product_desc = $objp->product_desc; // Description product - $line->fk_product_type = $objp->fk_product_type; // Type in product - $line->qty = $objp->qty; - $line->duree = $objp->duree; - $line->duration = $objp->duree; - $line->date = $objp->date; - $line->subprice = $objp->subprice; - $line->tva_tx = $objp->tva_tx; - $line->remise_percent = $objp->remise_percent; - $line->fk_remise_except = !empty($objp->fk_remise_except) ? $objp->fk_remise_except : ""; - $line->fk_product = $objp->fk_product; - $line->info_bits = !empty($objp->info_bits) ? $objp->info_bits : ""; - $line->total_ht = $objp->total_ht; - $line->total_tva = $objp->total_tva; - $line->total_ttc = $objp->total_ttc; - $line->rang = $objp->rang; - $line->special_code = $objp->special_code; - $line->fk_unit = $objp->fk_unit; - - $this->lines[$i] = $line; - - $i++; - } - - $this->db->free($result); - return 1; - } else { - $this->error = $this->db->error(); - return -3; - } - } - - - /** - * Delete template fichinter rec - * - * @param User $user Object user who delete - * @param int $notrigger Disable trigger - * @return int Return integer <0 if KO, >0 if OK - */ - public function delete(User $user, $notrigger = 0) - { - $rowid = $this->id; - - dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG); - - $error = 0; - $this->db->begin(); - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet_rec WHERE fk_fichinter = ".((int) $rowid); - dol_syslog($sql); - if ($this->db->query($sql)) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter_rec WHERE rowid = ".((int) $rowid); - dol_syslog($sql); - if (!$this->db->query($sql)) { - $this->error = $this->db->lasterror(); - $error = -1; - } - } else { - $this->error = $this->db->lasterror(); - $error = -2; - } - - if (!$error) { - $this->db->commit(); - return 1; - } else { - $this->db->rollback(); - return $error; - } - } - - - /** - * Add a line to fichinter rec - * - * @param string $desc Description de la ligne - * @param integer $duration Durée - * @param string $date Date - * @param int $rang Position of line - * @param double $pu_ht Unit price without tax (> 0 even for credit note) - * @param double $qty Quantity - * @param double $txtva Forced VAT rate, otherwise -1 - * @param int $fk_product Id of predefined product/service - * @param double $remise_percent Percentage of discount on line - * @param string $price_base_type HT or TTC - * @param int $info_bits Bits for type of lines - * @param int $fk_remise_except Id discount - * @param double $pu_ttc Unit price with tax (> 0 even for credit note) - * @param int $type Type of line (0=product, 1=service) - * @param int $special_code Special code - * @param string $label Label of the line - * @param string $fk_unit Unit - * @return int Return integer <0 if KO, Id of line if OK - */ - public function addline($desc, $duration, $date, $rang = -1, $pu_ht = 0, $qty = 0, $txtva = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = '', $pu_ttc = 0, $type = 0, $special_code = 0, $label = '', $fk_unit = null) - { - global $mysoc; - - include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; - - // Check parameters - if ($type < 0) { - $this->error = 'Bad value for parameter type'; - return -1; - } - - if ($this->status == self::STATUS_DRAFT) { - // Clean parameters - $remise_percent = price2num($remise_percent); - $qty = price2num($qty); - if (!$qty) { - $qty = 1; - } - if (!$info_bits) { - $info_bits = 0; - } - $pu_ht = price2num($pu_ht); - $pu_ttc = price2num($pu_ttc); - if (!preg_match('/\((.*)\)/', $txtva)) { - $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5' - } - - if ($price_base_type == 'HT') { - $pu = $pu_ht; - } else { - $pu = $pu_ttc; - } - - // Calcul du total TTC et de la TVA pour la ligne a partir de - // qty, pu, remise_percent et txtva - // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker - // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. - $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc); - - $total_ht = $tabprice[0]; - $total_tva = $tabprice[1]; - $total_ttc = $tabprice[2]; - - $pu_ht = $tabprice[3]; - - $product_type = $type; - if ($fk_product) { - $product = new Product($this->db); - $result = $product->fetch($fk_product); - $product_type = $product->type; - } - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinterdet_rec ("; - $sql .= "fk_fichinter"; - $sql .= ", label"; - $sql .= ", description"; - $sql .= ", date"; - $sql .= ", duree"; - //$sql.= ", price"; - //$sql.= ", qty"; - //$sql.= ", tva_tx"; - $sql .= ", fk_product"; - $sql .= ", product_type"; - $sql .= ", remise_percent"; - $sql .= ", subprice"; - $sql .= ", total_ht"; - $sql .= ", total_tva"; - $sql .= ", total_ttc"; - $sql .= ", rang"; - //$sql.= ", special_code"; - $sql .= ", fk_unit"; - $sql .= ") VALUES ("; - $sql .= (int) $this->id; - $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null"); - $sql .= ", ".(!empty($desc) ? "'".$this->db->escape($desc)."'" : "null"); - $sql .= ", ".(!empty($date) ? "'".$this->db->idate($date)."'" : "null"); - $sql .= ", ".$duration; - //$sql.= ", ".price2num($pu_ht); - //$sql.= ", ".(!empty($qty)? $qty :(!empty($duration)? $duration :"null")); - //$sql.= ", ".price2num($txtva); - $sql .= ", ".(!empty($fk_product) ? $fk_product : "null"); - $sql .= ", ".$product_type; - $sql .= ", ".(!empty($remise_percent) ? $remise_percent : "null"); - $sql.= ", '".price2num($pu_ht)."'"; - $sql .= ", '".price2num($total_ht)."'"; - $sql .= ", '".price2num($total_tva)."'"; - $sql .= ", '".price2num($total_ttc)."'"; - $sql .= ", ".(int) $rang; - //$sql.= ", ".$special_code; - $sql .= ", ".(!empty($fk_unit) ? $fk_unit : "null"); - $sql .= ")"; - - dol_syslog(get_class($this)."::addline", LOG_DEBUG); - if ($this->db->query($sql)) { - return 1; - } else { - $this->error = $this->db->lasterror(); - return -1; - } - } else { - $this->error = 'Bad status of recurring intervention. Must be draft status to allow addition of lines'; - return -1; - } - } - - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Rend la fichinter automatique - * - * @param User $user User object - * @param int $freq Freq - * @param string $courant Courant - * @return int 0 if OK, <0 if KO - */ - public function set_auto($user, $freq, $courant) - { - // phpcs:enable - if ($user->hasRight('fichinter', 'creer')) { - $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter_rec "; - $sql .= " SET frequency='".$this->db->escape($freq)."'"; - $sql .= ", date_last_gen='".$this->db->escape($courant)."'"; - $sql .= " WHERE rowid = ".((int) $this->id); - - $resql = $this->db->query($sql); - - if ($resql) { - $this->frequency = $freq; - $this->date_last_gen = $courant; - return 0; - } else { - dol_print_error($this->db); - return -1; - } - } else { - return -2; - } - } - - /** - * Return clicable name (with picto eventually) - * - * @param int $withpicto Add picto into link - * @param string $option Where point the link - * @param int $max Maxlength of ref - * @param int $short 1=Return just URL - * @param string $moretitle Add more text to title tooltip - * @return string String with URL - */ - public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '') - { - global $langs, $hookmanager; - - $result = ''; - $label = $langs->trans("ShowInterventionModel").': '.$this->ref; - - $url = DOL_URL_ROOT.'/fichinter/card-rec.php?id='.$this->id; - - if ($short) { - return $url; - } - - $picto = 'intervention'; - - $link = '
'; - $linkend = ''; - - if ($withpicto) { - $result .= $link.img_object($label, $picto, 'class="classfortooltip"').$linkend; - } - if ($withpicto && $withpicto != 2) { - $result .= ' '; - } - if ($withpicto != 2) { - $result .= $link.$this->ref.$linkend; - } - global $action; - $hookmanager->initHooks(array($this->element . 'dao')); - $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; - } - - - /** - * 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() - { - //$now = dol_now(); - //$arraynow = dol_getdate($now); - //$nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']); - - parent::initAsSpecimen(); - - $this->usenewprice = 1; - } - - /** - * Function used to replace a thirdparty id with another one. - * - * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test - * @param int $origin_id Old thirdparty id - * @param int $dest_id New thirdparty id - * @return bool - */ - public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id) - { - $tables = array('fichinter_rec'); - - return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables); - } - - /** - * Function used to replace a product id with another one. - * - * @param DoliDB $db Database handler - * @param int $origin_id Old product id - * @param int $dest_id New product id - * @return bool - */ - public static function replaceProduct(DoliDB $db, $origin_id, $dest_id) - { - $tables = array( - 'fichinterdet_rec' - ); - - return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables); - } - - /** - * Update frequency and unit - * - * @param int $frequency value of frequency - * @param string $unit unit of frequency (d, m, y) - * @return int Return integer <0 if KO, >0 if OK - */ - public function setFrequencyAndUnit($frequency, $unit) - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::setFrequencyAndUnit called with table_element not defined", LOG_ERR); - return -1; - } - - if (!empty($frequency) && empty($unit)) { - dol_syslog(get_class($this)."::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR); - return -2; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null'); - if (!empty($unit)) { - $sql .= ', unit_frequency = "'.$this->db->escape($unit).'"'; - } - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG); - if ($this->db->query($sql)) { - $this->frequency = $frequency; - if (!empty($unit)) { - $this->unit_frequency = $unit; - } - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } - - /** - * Update the next date of execution - * - * @param datetime $date date of execution - * @param int $increment_nb_gen_done 0 do nothing more, >0 increment nb_gen_done - * @return int Return integer <0 if KO, >0 if OK - */ - public function setNextDate($date, $increment_nb_gen_done = 0) - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR); - return -1; - } - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null"); - if ($increment_nb_gen_done > 0) { - $sql .= ', nb_gen_done = nb_gen_done + 1'; - } - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG); - if ($this->db->query($sql)) { - $this->date_when = $date; - if ($increment_nb_gen_done > 0) { - $this->nb_gen_done++; - } - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } - - /** - * Update the maximum period - * - * @param int $nb number of maximum period - * @return int Return integer <0 if KO, >0 if OK - */ - public function setMaxPeriod($nb) - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR); - return -1; - } - - if (empty($nb)) { - $nb = 0; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql .= ' SET nb_gen_max = '.((int) $nb); - $sql .= ' WHERE rowid = '.((int) $this->id); - - dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG); - if ($this->db->query($sql)) { - $this->nb_gen_max = $nb; - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } - - /** - * Update the auto validate fichinter - * - * @param int $validate 0 to create in draft, 1 to create and validate fichinter - * @return int Return integer <0 if KO, >0 if OK - */ - public function setAutoValidate($validate) - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::setAutoValidate called with property table_element not defined", LOG_ERR); - return -1; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql .= ' SET auto_validate = '.((int) $validate); - $sql .= ' WHERE rowid = '.((int) $this->id); - - dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG); - if ($this->db->query($sql)) { - $this->auto_validate = $validate; - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } - - /** - * Update the Number of Generation Done - * - * @return int Return integer <0 if KO, >0 if OK - */ - public function updateNbGenDone() - { - if (!$this->table_element) { - dol_syslog(get_class($this)."::updateNbGenDone called with property table_element not defined", LOG_ERR); - return -1; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql .= ' SET nb_gen_done = nb_gen_done + 1'; - $sql .= ' , date_last_gen = now()'; - // si on et arrivé à la fin des génération - if ($this->nb_gen_max == $this->nb_gen_done + 1) { - $sql .= ' , statut = 1'; - } - - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG); - if ($this->db->query($sql)) { - $this->nb_gen_done = $this->nb_gen_done + 1; - $this->nb_gen_done = dol_now(); - return 1; - } else { - dol_print_error($this->db); - return -1; - } - } + protected $table_ref_field = 'titre'; + + /** + * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png + */ + public $picto = 'intervention'; + + public $title; + public $number; + public $date; + public $amount; + public $remise; + public $tva; + public $total; + public $db_table; + public $propalid; + + public $date_last_gen; + public $date_when; + public $nb_gen_done; + public $nb_gen_max; + + public $rang; + public $special_code; + + public $usenewprice = 0; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + + //status dans l'ordre de l'intervention + $this->statuts[0] = 'Draft'; + $this->statuts[1] = 'Closed'; + + $this->statuts_short[0] = 'Draft'; + $this->statuts_short[1] = 'Closed'; + + $this->statuts_logo[0] = 'statut0'; + $this->statuts_logo[1] = 'statut1'; + } + + /** + * 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($this->statut, $mode); + } + + + /** + * Create a predefined fichinter + * + * @param User $user User object + * @param int $notrigger no trigger + * @return int <0 if KO, id of fichinter if OK + */ + public function create($user, $notrigger = 0) + { + global $conf; + + $error = 0; + $now = dol_now(); + + // Clean parameters + $this->title = trim($this->title); + $this->description = trim($this->description); + + + $this->db->begin(); + + // Load fichinter model + $fichintsrc = new Fichinter($this->db); + + $result = $fichintsrc->fetch($this->id_origin); + $result = $fichintsrc->fetch_lines(1); // to get all lines + + + if ($result > 0) { + // On positionne en mode brouillon la facture + $this->brouillon = 1; + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter_rec ("; + $sql .= "titre"; + $sql .= ", fk_soc"; + $sql .= ", entity"; + $sql .= ", datec"; + $sql .= ", duree"; + $sql .= ", description"; + $sql .= ", note_private"; + $sql .= ", note_public"; + $sql .= ", fk_user_author"; + $sql .= ", fk_projet"; + $sql .= ", fk_contrat"; + $sql .= ", modelpdf"; + + $sql .= ", frequency"; + $sql .= ", unit_frequency"; + $sql .= ", date_when"; + $sql .= ", date_last_gen"; + $sql .= ", nb_gen_done"; + $sql .= ", nb_gen_max"; + // $sql.= ", auto_validate"; + + $sql .= ") VALUES ("; + $sql .= "'".$this->db->escape($this->title)."'"; + $sql .= ", ".($this->socid > 0 ? $this->socid : 'null'); + $sql .= ", ".$conf->entity; + $sql .= ", '".$this->db->idate($now)."'"; + $sql .= ", ".(!empty($fichintsrc->duration) ? $fichintsrc->duration : '0'); + $sql .= ", ".(!empty($this->description) ? ("'".$this->db->escape($this->description)."'") : "null"); + $sql .= ", ".(!empty($fichintsrc->note_private) ? ("'".$this->db->escape($fichintsrc->note_private)."'") : "null"); + $sql .= ", ".(!empty($fichintsrc->note_public) ? ("'".$this->db->escape($fichintsrc->note_public)."'") : "null"); + $sql .= ", '".$user->id."'"; + // si c'est la même société on conserve les liens vers le projet et le contrat + if ($this->socid == $fichintsrc->socid) { + $sql .= ", ".(!empty($fichintsrc->fk_project) ? $fichintsrc->fk_project : "null"); + $sql .= ", ".(!empty($fichintsrc->fk_contrat) ? $fichintsrc->fk_contrat : "null"); + } else { + $sql .= ", null, null"; + } + + $sql .= ", ".(!empty($fichintsrc->modelpdf) ? "'".$this->db->escape($fichintsrc->modelpdf)."'" : "''"); + + // récurrence + $sql .= ", ".(!empty($this->frequency) ? $this->frequency : "null"); + $sql .= ", '".$this->db->escape($this->unit_frequency)."'"; + $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'null'); + $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'null'); + $sql .= ", 0"; // we start à 0 + $sql .= ", ".$this->nb_gen_max; + // $sql.= ", ".$this->auto_validate; + $sql .= ")"; + + if ($this->db->query($sql)) { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + + /* + * Lines + */ + $num = count($fichintsrc->lines); + for ($i = 0; $i < $num; $i++) { + //$result=$fichintlignesrc->fetch($fichintsrc->lines[$i]->id); + + //var_dump($fichintsrc->lines[$i]); + $result_insert = $this->addline( + $fichintsrc->lines[$i]->desc, + $fichintsrc->lines[$i]->duration, + $fichintsrc->lines[$i]->datei, + $fichintsrc->lines[$i]->rang, + $fichintsrc->lines[$i]->subprice, + $fichintsrc->lines[$i]->qty, + $fichintsrc->lines[$i]->tva_tx, + $fichintsrc->lines[$i]->fk_product, + $fichintsrc->lines[$i]->remise_percent, + 'HT', + 0, + '', + 0, + $fichintsrc->lines[$i]->product_type, + $fichintsrc->lines[$i]->special_code, + $fichintsrc->lines[$i]->label, + $fichintsrc->lines[$i]->fk_unit + ); + + if ($result_insert < 0) + $error++; + } + + if ($error) + $this->db->rollback(); + else { + $this->db->commit(); + return $this->id; + } + } else { + $this->error = $this->db->error().' sql='.$sql; + $this->db->rollback(); + return -2; + } + } else { + $this->db->rollback(); + return -1; + } + } + + + /** + * Recupere l'objet facture et ses lignes de factures + * + * @param int $rowid Id of object to load + * @param string $ref Reference of fichinter + * @param string $ref_ext External reference of fichinter + * @return int >0 if OK, <0 if KO, 0 if not found + */ + public function fetch($rowid = 0, $ref = '', $ref_ext = '') + { + $sql = 'SELECT f.titre, f.fk_soc'; + $sql .= ', f.datec, f.duree, f.fk_projet, f.fk_contrat, f.description'; + $sql .= ', f.note_private, f.note_public, f.fk_user_author'; + $sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate'; + $sql .= ', f.note_private, f.note_public, f.fk_user_author'; + + $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinter_rec as f'; + if ($rowid > 0) $sql .= ' WHERE f.rowid='.$rowid; + elseif ($ref) $sql .= " WHERE f.titre='".$this->db->escape($ref)."'"; + + dol_syslog(get_class($this)."::fetch rowid=".$rowid, LOG_DEBUG); + + $result = $this->db->query($sql); + if ($result) { + if ($this->db->num_rows($result)) { + $obj = $this->db->fetch_object($result); + + $this->id = $rowid; + $this->titre = $obj->titre; + $this->ref = $obj->titre; + $this->description = $obj->description; + $this->datec = $obj->datec; + $this->duration = $obj->duree; + $this->socid = $obj->fk_soc; + $this->statut = 0; + $this->fk_project = $obj->fk_projet; + $this->fk_contrat = $obj->fk_contrat; + $this->note_private = $obj->note_private; + $this->note_public = $obj->note_public; + $this->user_author = $obj->fk_user_author; + $this->modelpdf = $obj->model_pdf; + $this->rang = $obj->rang; + $this->special_code = $obj->special_code; + $this->frequency = $obj->frequency; + $this->unit_frequency = $obj->unit_frequency; + $this->date_when = $this->db->jdate($obj->date_when); + $this->date_last_gen = $this->db->jdate($obj->date_last_gen); + $this->nb_gen_done = $obj->nb_gen_done; + $this->nb_gen_max = $obj->nb_gen_max; + $this->auto_validate = $obj->auto_validate; + + $this->brouillon = 1; + + /* + * Lines + */ + $result = $this->fetch_lines(); + if ($result < 0) { + $this->error = $this->db->error(); + return -3; + } + return 1; + } else { + $this->error = 'Interventional with id '.$rowid.' not found sql='.$sql; + dol_syslog(get_class($this).'::Fetch Error '.$this->error, LOG_ERR); + return -2; + } + } else { + $this->error = $this->db->error(); + return -1; + } + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Recupere les lignes de factures predefinies dans this->lines + * @param int $sall sall + * + * @return int 1 if OK, < 0 if KO + */ + public function fetch_lines($sall = 0) + { + // phpcs:enable + $sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, '; + $sql .= ' l.price, l.qty, l.tva_tx, l.remise, l.remise_percent, l.subprice, l.duree, '; + $sql .= ' l.total_ht, l.total_tva, l.total_ttc,'; + $sql .= ' l.rang, l.special_code,'; + $sql .= ' l.fk_unit, p.ref as product_ref, p.fk_product_type as fk_product_type,'; + $sql .= ' p.label as product_label, p.description as product_desc'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet_rec as l'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; + $sql .= ' WHERE l.fk_fichinter = '.$this->id; + + dol_syslog('FichInter-rec::fetch_lines', LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) { + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num) { + $objp = $this->db->fetch_object($result); + $line = new FichinterLigne($this->db); + + $line->id = $objp->rowid; + $line->label = $objp->custom_label; // Label line + $line->desc = $objp->description; // Description line + $line->product_type = $objp->product_type; // Type of line + $line->product_ref = $objp->product_ref; // Ref product + $line->product_label = $objp->product_label; // Label product + $line->product_desc = $objp->product_desc; // Description product + $line->fk_product_type = $objp->fk_product_type; // Type of product + $line->qty = $objp->qty; + $line->duree = $objp->duree; + $line->duration = $objp->duree; + $line->datei = $objp->date; + $line->subprice = $objp->subprice; + $line->tva_tx = $objp->tva_tx; + $line->remise_percent = $objp->remise_percent; + $line->fk_remise_except = $objp->fk_remise_except; + $line->fk_product = $objp->fk_product; + $line->date_start = $objp->date_start; + $line->date_end = $objp->date_end; + $line->date_start = $objp->date_start; + $line->date_end = $objp->date_end; + $line->info_bits = $objp->info_bits; + $line->total_ht = $objp->total_ht; + $line->total_tva = $objp->total_tva; + $line->total_ttc = $objp->total_ttc; + $line->code_ventilation = $objp->fk_code_ventilation; + $line->rang = $objp->rang; + $line->special_code = $objp->special_code; + $line->fk_unit = $objp->fk_unit; + + // Ne plus utiliser + $line->price = $objp->price; + $line->remise = $objp->remise; + + $this->lines[$i] = $line; + + $i++; + } + + $this->db->free($result); + return 1; + } else { + $this->error = $this->db->error(); + return -3; + } + } + + + /** + * Delete template fichinter rec + * + * @param int $rowid Id of fichinter rec to delete. If empty, we delete current instance of fichinter rec + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers + * @param int $idwarehouse Id warehouse to use for stock change. + * @return int <0 if KO, >0 if OK + */ + public function delete($rowid = 0, $notrigger = 0, $idwarehouse = -1) + { + if (empty($rowid)) $rowid = $this->id; + + dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG); + + $error = 0; + $this->db->begin(); + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet_rec WHERE fk_fichinter = ".$rowid; + dol_syslog($sql); + if ($this->db->query($sql)) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter_rec WHERE rowid = ".$rowid; + dol_syslog($sql); + if (!$this->db->query($sql)) { + $this->error = $this->db->lasterror(); + $error = -1; + } + } else { + $this->error = $this->db->lasterror(); + $error = -2; + } + + if (!$error) { + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); + return $error; + } + } + + + /** + * Add a line to fichinter rec + * + * @param string $desc Description de la ligne + * @param integer $duration Durée + * @param string $datei Date + * @param int $rang Position of line + * @param double $pu_ht Prix unitaire HT (> 0 even for credit note) + * @param double $qty Quantite + * @param double $txtva Taux de tva force, sinon -1 + * @param int $fk_product Id du produit/service predefini + * @param double $remise_percent Pourcentage de remise de la ligne + * @param string $price_base_type HT or TTC + * @param int $info_bits Bits de type de lignes + * @param int $fk_remise_except Id remise + * @param double $pu_ttc Prix unitaire TTC (> 0 even for credit note) + * @param int $type Type of line (0=product, 1=service) + * @param int $special_code Special code + * @param string $label Label of the line + * @param string $fk_unit Unit + * @return int <0 if KO, Id of line if OK + */ + public function addline($desc, $duration, $datei, $rang = -1, $pu_ht = 0, $qty = 0, $txtva = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = '', $pu_ttc = 0, $type = 0, $special_code = 0, $label = '', $fk_unit = null) + { + global $mysoc; + + include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; + + // Check parameters + if ($type < 0) return -1; + + if ($this->brouillon) { + // Clean parameters + $remise_percent = price2num($remise_percent); + $qty = price2num($qty); + if (!$qty) $qty = 1; + if (!$info_bits) $info_bits = 0; + $pu_ht = price2num($pu_ht); + $pu_ttc = price2num($pu_ttc); + $txtva = price2num($txtva); + + if ($price_base_type == 'HT') { + $pu = $pu_ht; + } else { + $pu = $pu_ttc; + } + + + // Calcul du total TTC et de la TVA pour la ligne a partir de + // qty, pu, remise_percent et txtva + // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker + // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. + $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc); + + $total_ht = $tabprice[0]; + $total_tva = $tabprice[1]; + $total_ttc = $tabprice[2]; + + $product_type = $type; + if ($fk_product) { + $product = new Product($this->db); + $result = $product->fetch($fk_product); + $product_type = $product->type; + } + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinterdet_rec ("; + $sql .= "fk_fichinter"; + $sql .= ", label"; + $sql .= ", description"; + $sql .= ", date"; + $sql .= ", duree"; + //$sql.= ", price"; + //$sql.= ", qty"; + //$sql.= ", tva_tx"; + $sql .= ", fk_product"; + $sql .= ", product_type"; + $sql .= ", remise_percent"; + //$sql.= ", subprice"; + $sql .= ", remise"; + $sql .= ", total_ht"; + $sql .= ", total_tva"; + $sql .= ", total_ttc"; + $sql .= ", rang"; + //$sql.= ", special_code"; + $sql .= ", fk_unit"; + $sql .= ") VALUES ("; + $sql .= (int) $this->id; + $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null"); + $sql .= ", ".(!empty($desc) ? "'".$this->db->escape($desc)."'" : "null"); + $sql .= ", ".(!empty($datei) ? "'".$this->db->idate($datei)."'" : "null"); + $sql .= ", ".$duration; + //$sql.= ", ".price2num($pu_ht); + //$sql.= ", ".(!empty($qty)? $qty :(!empty($duration)? $duration :"null")); + //$sql.= ", ".price2num($txtva); + $sql .= ", ".(!empty($fk_product) ? $fk_product : "null"); + $sql .= ", ".$product_type; + $sql .= ", ".(!empty($remise_percent) ? $remise_percent : "null"); + //$sql.= ", '".price2num($pu_ht)."'"; + $sql .= ", null"; + $sql .= ", '".price2num($total_ht)."'"; + $sql .= ", '".price2num($total_tva)."'"; + $sql .= ", '".price2num($total_ttc)."'"; + $sql .= ", ".(int) $rang; + //$sql.= ", ".$special_code; + $sql .= ", ".(!empty($fk_unit) ? $fk_unit : "null"); + $sql .= ")"; + + dol_syslog(get_class($this)."::addline", LOG_DEBUG); + if ($this->db->query($sql)) { + return 1; + } else { + $this->error = $this->db->lasterror(); + return -1; + } + } + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Rend la fichinter automatique + * + * @param User $user User object + * @param int $freq Freq + * @param string $courant Courant + * @return int 0 if OK, <0 if KO + */ + public function set_auto($user, $freq, $courant) + { + // phpcs:enable + if ($user->rights->fichinter->creer) { + $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter_rec "; + $sql .= " SET frequency='".$this->db->escape($freq)."'"; + $sql .= ", last_gen='".$this->db->escape($courant)."'"; + $sql .= " WHERE rowid = ".$this->id; + + $resql = $this->db->query($sql); + + if ($resql) { + $this->frequency = $freq; + $this->last_gen = $courant; + return 0; + } else { + dol_print_error($this->db); + return -1; + } + } else { + return -2; + } + } + + /** + * Return clicable name (with picto eventually) + * + * @param int $withpicto Add picto into link + * @param string $option Where point the link + * @param int $max Maxlength of ref + * @param int $short 1=Return just URL + * @param string $moretitle Add more text to title tooltip + * @return string String with URL + */ + public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '') + { + global $langs; + + $result = ''; + $label = $langs->trans("ShowInterventionModel").': '.$this->ref; + + $url = DOL_URL_ROOT.'/fichinter/card-rec.php?id='.$this->id; + + if ($short) return $url; + + $picto = 'intervention'; + + $link = ''; + $linkend = ''; + + if ($withpicto) { + $result .= $link.img_object($label, $picto, 'class="classfortooltip"').$linkend; + } + if ($withpicto && $withpicto != 2) { + $result .= ' '; + } + if ($withpicto != 2) { + $result .= $link.$this->ref.$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. + * + * @param string $option ''=Create a specimen fichinter with lines, 'nolines'=No lines + * @return void + */ + public function initAsSpecimen($option = '') + { + global $user, $langs, $conf; + + $now = dol_now(); + $arraynow = dol_getdate($now); + $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']); + + parent::initAsSpecimen($option); + + $this->usenewprice = 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_rec'); + + return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); + } + + + /** + * Update frequency and unit + * + * @param int $frequency value of frequency + * @param string $unit unit of frequency (d, m, y) + * @return int <0 if KO, >0 if OK + */ + public function setFrequencyAndUnit($frequency, $unit) + { + if (!$this->table_element) { + dol_syslog(get_class($this)."::setFrequencyAndUnit called with table_element not defined", LOG_ERR); + return -1; + } + + if (!empty($frequency) && empty($unit)) { + dol_syslog(get_class($this)."::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR); + return -2; + } + + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null'); + if (!empty($unit)) { + $sql .= ', unit_frequency = "'.$this->db->escape($unit).'"'; + } + $sql .= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG); + if ($this->db->query($sql)) { + $this->frequency = $frequency; + if (!empty($unit)) $this->unit_frequency = $unit; + return 1; + } else { + dol_print_error($this->db); + return -1; + } + } + + /** + * Update the next date of execution + * + * @param datetime $date date of execution + * @param int $increment_nb_gen_done 0 do nothing more, >0 increment nb_gen_done + * @return int <0 if KO, >0 if OK + */ + public function setNextDate($date, $increment_nb_gen_done = 0) + { + if (!$this->table_element) { + dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR); + return -1; + } + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null"); + if ($increment_nb_gen_done > 0) $sql .= ', nb_gen_done = nb_gen_done + 1'; + $sql .= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG); + if ($this->db->query($sql)) { + $this->date_when = $date; + if ($increment_nb_gen_done > 0) $this->nb_gen_done++; + return 1; + } else { + dol_print_error($this->db); + return -1; + } + } + + /** + * Update the maximum period + * + * @param int $nb number of maximum period + * @return int <0 if KO, >0 if OK + */ + public function setMaxPeriod($nb) + { + if (!$this->table_element) { + dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR); + return -1; + } + + if (empty($nb)) $nb = 0; + + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql .= ' SET nb_gen_max = '.$nb; + $sql .= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG); + if ($this->db->query($sql)) { + $this->nb_gen_max = $nb; + return 1; + } else { + dol_print_error($this->db); + return -1; + } + } + + /** + * Update the auto validate fichinter + * + * @param int $validate 0 to create in draft, 1 to create and validate fichinter + * @return int <0 if KO, >0 if OK + */ + public function setAutoValidate($validate) + { + if (!$this->table_element) { + dol_syslog(get_class($this)."::setAutoValidate called with property table_element not defined", LOG_ERR); + return -1; + } + + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql .= ' SET auto_validate = '.$validate; + $sql .= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG); + if ($this->db->query($sql)) { + $this->auto_validate = $validate; + return 1; + } else { + dol_print_error($this->db); + return -1; + } + } + + /** + * Update the Number of Generation Done + * + * @return int <0 if KO, >0 if OK + */ + public function updateNbGenDone() + { + if (!$this->table_element) { + dol_syslog(get_class($this)."::updateNbGenDone called with property table_element not defined", LOG_ERR); + return -1; + } + + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql .= ' SET nb_gen_done = nb_gen_done + 1'; + $sql .= ' , date_last_gen = now()'; + // si on et arrivé à la fin des génération + if ($this->nb_gen_max == $this->nb_gen_done + 1) + $sql .= ' , statut = 1'; + + $sql .= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG); + if ($this->db->query($sql)) { + $this->nb_gen_done = $this->nb_gen_done + 1; + $this->nb_gen_done = dol_now(); + return 1; + } else { + dol_print_error($this->db); + return -1; + } + } --- /tmp/dsg/dolibarr/htdocs/fichinter/class/github_19.0.3_fichinterstats.class.php +++ /tmp/dsg/dolibarr/htdocs/fichinter/class/client_fichinterstats.class.php @@ -36,191 +36,178 @@ - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element; - - public $socid; - public $userid; - - public $from; - public $field; - public $where; - - - /** - * Constructor - * - * @param DoliDB $db Database handler - * @param int $socid Id third party for filter. This value must be forced during the new to external user company if user is an external user. - * @param string $mode Option ('customer', 'supplier') - * @param int $userid Id user for filter (creation user) - */ - public function __construct($db, $socid, $mode, $userid = 0) - { - global $user, $conf; - - $this->db = $db; - - $this->socid = ($socid > 0 ? $socid : 0); - $this->userid = $userid; - $this->cachefilesuffix = $mode; - - if ($mode == 'customer') { - $object = new Fichinter($this->db); - $this->from = MAIN_DB_PREFIX.$object->table_element." as c"; - $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl"; - $this->field = '0'; - $this->field_line = '0'; - //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled - } - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $this->where .= (!empty($this->where) ? ' AND ' : '')." c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('intervention').')'; - - if ($this->socid) { - $this->where .= " AND c.fk_soc = ".((int) $this->socid); - } - if ($this->userid > 0) { - $this->where .= ' AND c.fk_user_author = '.((int) $this->userid); - } - } - - /** - * Return intervention number by month for a year - * - * @param int $year Year to scan - * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month - * @return array Array with number by month - */ - public function getNbByMonth($year, $format = 0) - { - global $user; - - $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb"; - $sql .= " FROM ".$this->from; - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; - $sql .= " AND ".$this->where; - $sql .= " GROUP BY dm"; - $sql .= $this->db->order('dm', 'DESC'); - - $res = $this->_getNbByMonth($year, $sql, $format); - return $res; - } - - /** - * Return interventions number per year - * - * @return array Array with number by year - * - */ - public function getNbByYear() - { - global $user; - - $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0"; - $sql .= " FROM ".$this->from; - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE ".$this->where; - $sql .= " GROUP BY dm"; - $sql .= $this->db->order('dm', 'DESC'); - - return $this->_getNbByYear($sql); - } - - /** - * Return the intervention amount by month for a year - * - * @param int $year Year to scan - * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month - * @return array Array with amount by month - */ - public function getAmountByMonth($year, $format = 0) - { - global $user; - - $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0"; - $sql .= " FROM ".$this->from; - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; - $sql .= " AND ".$this->where; - $sql .= " GROUP BY dm"; - $sql .= $this->db->order('dm', 'DESC'); - - $res = $this->_getAmountByMonth($year, $sql, $format); - return $res; - } - - /** - * Return the intervention amount average by month for a year - * - * @param int $year year for stats - * @return array array with number by month - */ - public function getAverageByMonth($year) - { - global $user; - - $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0"; - $sql .= " FROM ".$this->from; - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; - $sql .= " AND ".$this->where; - $sql .= " GROUP BY dm"; - $sql .= $this->db->order('dm', 'DESC'); - - return $this->_getAverageByMonth($year, $sql); - } - - /** - * Return nb, total and average - * - * @return array Array of values - */ - public function getAllByYear() - { - global $user; - - $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg"; - $sql .= " FROM ".$this->from; - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE ".$this->where; - $sql .= " GROUP BY year"; - $sql .= $this->db->order('year', 'DESC'); - - return $this->_getAllByYear($sql); - } - - /** - * Return nb, amount of predefined product for year - * - * @param int $year Year to scan - * @param int $limit Limit - * @return array Array of values - */ - public function getAllByProduct($year, $limit = 0) - { - global $user; - - $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg"; - $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product"; - //if (empty($user->rights->societe->client->voir) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE ".$this->where; - $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid"; - $sql .= " AND c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'"; - $sql .= " GROUP BY product.ref"; - $sql .= $this->db->order('nb', 'DESC'); - //$sql.= $this->db->plimit(20); - - return $this->_getAllByProduct($sql, $limit); - } + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element; + + public $socid; + public $userid; + + public $from; + public $field; + public $where; + + + /** + * Constructor + * + * @param DoliDB $db Database handler + * @param int $socid Id third party for filter. This value must be forced during the new to external user company if user is an external user. + * @param string $mode Option ('customer', 'supplier') + * @param int $userid Id user for filter (creation user) + */ + public function __construct($db, $socid, $mode, $userid = 0) + { + global $user, $conf; + + $this->db = $db; + + $this->socid = ($socid > 0 ? $socid : 0); + $this->userid = $userid; + $this->cachefilesuffix = $mode; + + $this->where .= " c.entity = ".$conf->entity; + if ($mode == 'customer') + { + $object = new Fichinter($this->db); + $this->from = MAIN_DB_PREFIX.$object->table_element." as c"; + $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl"; + $this->field = '0'; + $this->field_line = '0'; + //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled + } + if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id; + if ($this->socid) + { + $this->where .= " AND c.fk_soc = ".$this->socid; + } + if ($this->userid > 0) $this->where .= ' AND c.fk_user_author = '.$this->userid; + } + + /** + * Return intervention number by month for a year + * + * @param int $year Year to scan + * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month + * @return array Array with number by month + */ + public function getNbByMonth($year, $format = 0) + { + global $user; + + $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb"; + $sql .= " FROM ".$this->from; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; + $sql .= " AND ".$this->where; + $sql .= " GROUP BY dm"; + $sql .= $this->db->order('dm', 'DESC'); + + $res = $this->_getNbByMonth($year, $sql, $format); + return $res; + } + + /** + * Return interventions number per year + * + * @return array Array with number by year + * + */ + public function getNbByYear() + { + global $user; + + $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0"; + $sql .= " FROM ".$this->from; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE ".$this->where; + $sql .= " GROUP BY dm"; + $sql .= $this->db->order('dm', 'DESC'); + + return $this->_getNbByYear($sql); + } + + /** + * Return the intervention amount by month for a year + * + * @param int $year Year to scan + * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month + * @return array Array with amount by month + */ + public function getAmountByMonth($year, $format = 0) + { + global $user; + + $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0"; + $sql .= " FROM ".$this->from; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; + $sql .= " AND ".$this->where; + $sql .= " GROUP BY dm"; + $sql .= $this->db->order('dm', 'DESC'); + + $res = $this->_getAmountByMonth($year, $sql, $format); + return $res; + } + + /** + * Return the intervention amount average by month for a year + * + * @param int $year year for stats + * @return array array with number by month + */ + public function getAverageByMonth($year) + { + global $user; + + $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0"; + $sql .= " FROM ".$this->from; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; + $sql .= " AND ".$this->where; + $sql .= " GROUP BY dm"; + $sql .= $this->db->order('dm', 'DESC'); + + return $this->_getAverageByMonth($year, $sql); + } + + /** + * Return nb, total and average + * + * @return array Array of values + */ + public function getAllByYear() + { + global $user; + + $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg"; + $sql .= " FROM ".$this->from; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE ".$this->where; + $sql .= " GROUP BY year"; + $sql .= $this->db->order('year', 'DESC'); + + return $this->_getAllByYear($sql); + } + + /** + * Return nb, amount of predefined product for year + * + * @param int $year Year to scan + * @param int $limit Limit + * @return array Array of values + */ + public function getAllByProduct($year, $limit = 0) + { + global $user; + + $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg"; + $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product"; + //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE ".$this->where; + $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid"; + $sql .= " AND c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'"; + $sql .= " GROUP BY product.ref"; + $sql .= $this->db->order('nb', 'DESC'); + //$sql.= $this->db->plimit(20); + + return $this->_getAllByProduct($sql, $limit); + }