This commit is contained in:
Frédéric FRANCE 2020-10-31 15:59:33 +01:00
parent a51d135c7b
commit 8bf2b1797c
No known key found for this signature in database
GPG Key ID: 06809324E4B2ABC1
3 changed files with 317 additions and 317 deletions

View File

@ -30,206 +30,206 @@ require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
class DolibarrApi class DolibarrApi
{ {
/** /**
* @var DoliDb $db Database object * @var DoliDb $db Database object
*/ */
protected $db; protected $db;
/** /**
* @var Restler $r Restler object * @var Restler $r Restler object
*/ */
public $r; public $r;
/** /**
* Constructor * Constructor
* *
* @param DoliDb $db Database handler * @param DoliDb $db Database handler
* @param string $cachedir Cache dir * @param string $cachedir Cache dir
* @param boolean $refreshCache Update cache * @param boolean $refreshCache Update cache
*/ */
public function __construct($db, $cachedir = '', $refreshCache = false) public function __construct($db, $cachedir = '', $refreshCache = false)
{ {
global $conf, $dolibarr_main_url_root; global $conf, $dolibarr_main_url_root;
if (empty($cachedir)) $cachedir = $conf->api->dir_temp; if (empty($cachedir)) $cachedir = $conf->api->dir_temp;
Defaults::$cacheDirectory = $cachedir; Defaults::$cacheDirectory = $cachedir;
$this->db = $db; $this->db = $db;
$production_mode = (empty($conf->global->API_PRODUCTION_MODE) ? false : true); $production_mode = (empty($conf->global->API_PRODUCTION_MODE) ? false : true);
$this->r = new Restler($production_mode, $refreshCache); $this->r = new Restler($production_mode, $refreshCache);
$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root)); $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
$urlwithouturlrootautodetect = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim(DOL_MAIN_URL_ROOT)); $urlwithouturlrootautodetect = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim(DOL_MAIN_URL_ROOT));
$urlwithrootautodetect = $urlwithouturlroot.DOL_URL_ROOT; // This is to use local domain autodetected by dolibarr from url $urlwithrootautodetect = $urlwithouturlroot.DOL_URL_ROOT; // This is to use local domain autodetected by dolibarr from url
$this->r->setBaseUrls($urlwithouturlroot, $urlwithouturlrootautodetect); $this->r->setBaseUrls($urlwithouturlroot, $urlwithouturlrootautodetect);
$this->r->setAPIVersion(1); $this->r->setAPIVersion(1);
//$this->r->setSupportedFormats('json'); //$this->r->setSupportedFormats('json');
//$this->r->setSupportedFormats('jsonFormat'); //$this->r->setSupportedFormats('jsonFormat');
} }
/** /**
* Executed method when API is called without parameter * Executed method when API is called without parameter
* *
* Display a short message an return a http code 200 * Display a short message an return a http code 200
* *
* @return array * @return array
*/ */
/* Disabled, most APIs does not share same signature for method index /* Disabled, most APIs does not share same signature for method index
function index() function index()
{ {
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
'message' => __class__.' is up and running!' 'message' => __class__.' is up and running!'
) )
); );
}*/ }*/
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
* @param Object $object Object to clean * @param Object $object Object to clean
* @return Object Object with cleaned properties * @return Object Object with cleaned properties
*/ */
protected function _cleanObjectDatas($object) protected function _cleanObjectDatas($object)
{ {
// phpcs:enable // phpcs:enable
// Remove $db object property for object // Remove $db object property for object
unset($object->db); unset($object->db);
unset($object->isextrafieldmanaged); unset($object->isextrafieldmanaged);
unset($object->ismultientitymanaged); unset($object->ismultientitymanaged);
unset($object->restrictiononfksoc); unset($object->restrictiononfksoc);
unset($object->table_rowid); unset($object->table_rowid);
// Remove linkedObjects. We should already have linkedObjectsIds that avoid huge responses // Remove linkedObjects. We should already have linkedObjectsIds that avoid huge responses
unset($object->linkedObjects); unset($object->linkedObjects);
unset($object->fields); unset($object->fields);
unset($object->oldline); unset($object->oldline);
unset($object->error); unset($object->error);
unset($object->errors); unset($object->errors);
unset($object->ref_previous); unset($object->ref_previous);
unset($object->ref_next); unset($object->ref_next);
unset($object->ref_int); unset($object->ref_int);
unset($object->projet); // Should be fk_project unset($object->projet); // Should be fk_project
unset($object->project); // Should be fk_project unset($object->project); // Should be fk_project
unset($object->author); // Should be fk_user_author unset($object->author); // Should be fk_user_author
unset($object->timespent_old_duration); unset($object->timespent_old_duration);
unset($object->timespent_id); unset($object->timespent_id);
unset($object->timespent_duration); unset($object->timespent_duration);
unset($object->timespent_date); unset($object->timespent_date);
unset($object->timespent_datehour); unset($object->timespent_datehour);
unset($object->timespent_withhour); unset($object->timespent_withhour);
unset($object->timespent_fk_user); unset($object->timespent_fk_user);
unset($object->timespent_note); unset($object->timespent_note);
unset($object->fk_delivery_address); unset($object->fk_delivery_address);
unset($object->statuts); unset($object->statuts);
unset($object->statuts_short); unset($object->statuts_short);
unset($object->statuts_logo); unset($object->statuts_logo);
unset($object->statuts_long); unset($object->statuts_long);
unset($object->labelStatus); unset($object->labelStatus);
unset($object->labelStatusShort); unset($object->labelStatusShort);
unset($object->stats_propale); unset($object->stats_propale);
unset($object->stats_commande); unset($object->stats_commande);
unset($object->stats_contrat); unset($object->stats_contrat);
unset($object->stats_facture); unset($object->stats_facture);
unset($object->stats_commande_fournisseur); unset($object->stats_commande_fournisseur);
unset($object->stats_reception); unset($object->stats_reception);
unset($object->stats_mrptoconsume); unset($object->stats_mrptoconsume);
unset($object->stats_mrptoproduce); unset($object->stats_mrptoproduce);
unset($object->element); unset($object->element);
unset($object->fk_element); unset($object->fk_element);
unset($object->table_element); unset($object->table_element);
unset($object->table_element_line); unset($object->table_element_line);
unset($object->class_element_line); unset($object->class_element_line);
unset($object->picto); unset($object->picto);
unset($object->fieldsforcombobox); unset($object->fieldsforcombobox);
unset($object->skip_update_total); unset($object->skip_update_total);
unset($object->context); unset($object->context);
unset($object->next_prev_filter); unset($object->next_prev_filter);
unset($object->region); unset($object->region);
unset($object->region_code); unset($object->region_code);
unset($object->libelle_statut); unset($object->libelle_statut);
unset($object->libelle_paiement); unset($object->libelle_paiement);
if ($object->table_element != 'ticket') { if ($object->table_element != 'ticket') {
unset($object->comments); unset($object->comments);
} }
// Remove the $oldcopy property because it is not supported by the JSON // Remove the $oldcopy property because it is not supported by the JSON
// encoder. The following error is generated when trying to serialize // encoder. The following error is generated when trying to serialize
// it: "Error encoding/decoding JSON: Type is not supported" // it: "Error encoding/decoding JSON: Type is not supported"
// Note: Event if this property was correctly handled by the JSON // Note: Event if this property was correctly handled by the JSON
// encoder, it should be ignored because keeping it would let the API // encoder, it should be ignored because keeping it would let the API
// have a very strange behavior: calling PUT and then GET on the same // have a very strange behavior: calling PUT and then GET on the same
// resource would give different results: // resource would give different results:
// PUT /objects/{id} -> returns object with oldcopy = previous version of the object // PUT /objects/{id} -> returns object with oldcopy = previous version of the object
// GET /objects/{id} -> returns object with oldcopy empty // GET /objects/{id} -> returns object with oldcopy empty
unset($object->oldcopy); unset($object->oldcopy);
// If object has lines, remove $db property // If object has lines, remove $db property
if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) { if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
$nboflines = count($object->lines); $nboflines = count($object->lines);
for ($i = 0; $i < $nboflines; $i++) for ($i = 0; $i < $nboflines; $i++)
{ {
$this->_cleanObjectDatas($object->lines[$i]); $this->_cleanObjectDatas($object->lines[$i]);
unset($object->lines[$i]->contact); unset($object->lines[$i]->contact);
unset($object->lines[$i]->contact_id); unset($object->lines[$i]->contact_id);
unset($object->lines[$i]->country); unset($object->lines[$i]->country);
unset($object->lines[$i]->country_id); unset($object->lines[$i]->country_id);
unset($object->lines[$i]->country_code); unset($object->lines[$i]->country_code);
unset($object->lines[$i]->mode_reglement_id); unset($object->lines[$i]->mode_reglement_id);
unset($object->lines[$i]->mode_reglement_code); unset($object->lines[$i]->mode_reglement_code);
unset($object->lines[$i]->mode_reglement); unset($object->lines[$i]->mode_reglement);
unset($object->lines[$i]->cond_reglement_id); unset($object->lines[$i]->cond_reglement_id);
unset($object->lines[$i]->cond_reglement_code); unset($object->lines[$i]->cond_reglement_code);
unset($object->lines[$i]->cond_reglement); unset($object->lines[$i]->cond_reglement);
unset($object->lines[$i]->fk_delivery_address); unset($object->lines[$i]->fk_delivery_address);
unset($object->lines[$i]->fk_projet); unset($object->lines[$i]->fk_projet);
unset($object->lines[$i]->fk_project); unset($object->lines[$i]->fk_project);
unset($object->lines[$i]->thirdparty); unset($object->lines[$i]->thirdparty);
unset($object->lines[$i]->user); unset($object->lines[$i]->user);
unset($object->lines[$i]->model_pdf); unset($object->lines[$i]->model_pdf);
unset($object->lines[$i]->modelpdf); unset($object->lines[$i]->modelpdf);
unset($object->lines[$i]->note_public); unset($object->lines[$i]->note_public);
unset($object->lines[$i]->note_private); unset($object->lines[$i]->note_private);
unset($object->lines[$i]->fk_incoterms); unset($object->lines[$i]->fk_incoterms);
unset($object->lines[$i]->label_incoterms); unset($object->lines[$i]->label_incoterms);
unset($object->lines[$i]->location_incoterms); unset($object->lines[$i]->location_incoterms);
unset($object->lines[$i]->name); unset($object->lines[$i]->name);
unset($object->lines[$i]->lastname); unset($object->lines[$i]->lastname);
unset($object->lines[$i]->firstname); unset($object->lines[$i]->firstname);
unset($object->lines[$i]->civility_id); unset($object->lines[$i]->civility_id);
unset($object->lines[$i]->fk_multicurrency); unset($object->lines[$i]->fk_multicurrency);
unset($object->lines[$i]->multicurrency_code); unset($object->lines[$i]->multicurrency_code);
unset($object->lines[$i]->shipping_method_id); unset($object->lines[$i]->shipping_method_id);
} }
} }
if (!empty($object->thirdparty) && is_object($object->thirdparty)) if (!empty($object->thirdparty) && is_object($object->thirdparty))
{ {
$this->_cleanObjectDatas($object->thirdparty); $this->_cleanObjectDatas($object->thirdparty);
} }
dol_syslog(print_r($object, true), LOG_NOTICE); dol_syslog(print_r($object, true), LOG_NOTICE);
return $object; return $object;
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Check user access to a resource * Check user access to a resource
* *
@ -241,12 +241,12 @@ class DolibarrApi
* @param string $feature2 Feature to check, second level of permission (optional). Can be or check with 'level1|level2'. * @param string $feature2 Feature to check, second level of permission (optional). Can be or check with 'level1|level2'.
* @param string $dbt_keyfield Field name for socid foreign key if not fk_soc. Not used if objectid is null (optional) * @param string $dbt_keyfield Field name for socid foreign key if not fk_soc. Not used if objectid is null (optional)
* @param string $dbt_select Field name for select if not rowid. Not used if objectid is null (optional) * @param string $dbt_select Field name for select if not rowid. Not used if objectid is null (optional)
* @return bool * @return bool
* @throws RestException * @throws RestException
*/ */
protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid') protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid')
{ {
// phpcs:enable // phpcs:enable
// Features/modules to check // Features/modules to check
$featuresarray = array($resource); $featuresarray = array($resource);
if (preg_match('/&/', $resource)) { if (preg_match('/&/', $resource)) {
@ -261,9 +261,9 @@ class DolibarrApi
} }
return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select); return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select);
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Return if a $sqlfilters parameter is valid * Return if a $sqlfilters parameter is valid
* *
@ -272,30 +272,30 @@ class DolibarrApi
*/ */
protected function _checkFilters($sqlfilters) protected function _checkFilters($sqlfilters)
{ {
// phpcs:enable // phpcs:enable
//$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; //$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
//$tmp=preg_replace_all('/'.$regexstring.'/', '', $sqlfilters); //$tmp=preg_replace_all('/'.$regexstring.'/', '', $sqlfilters);
$tmp = $sqlfilters; $tmp = $sqlfilters;
$ok = 0; $ok = 0;
$i = 0; $nb = strlen($tmp); $i = 0; $nb = strlen($tmp);
$counter = 0; $counter = 0;
while ($i < $nb) while ($i < $nb)
{ {
if ($tmp[$i] == '(') $counter++; if ($tmp[$i] == '(') $counter++;
if ($tmp[$i] == ')') $counter--; if ($tmp[$i] == ')') $counter--;
if ($counter < 0) if ($counter < 0)
{ {
$error = "Bad sqlfilters=".$sqlfilters; $error = "Bad sqlfilters=".$sqlfilters;
dol_syslog($error, LOG_WARNING); dol_syslog($error, LOG_WARNING);
return false; return false;
} }
$i++; $i++;
} }
return true; return true;
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Function to forge a SQL criteria * Function to forge a SQL criteria
* *
@ -304,22 +304,22 @@ class DolibarrApi
*/ */
protected static function _forge_criteria_callback($matches) protected static function _forge_criteria_callback($matches)
{ {
// phpcs:enable // phpcs:enable
global $db; global $db;
//dol_syslog("Convert matches ".$matches[1]); //dol_syslog("Convert matches ".$matches[1]);
if (empty($matches[1])) return ''; if (empty($matches[1])) return '';
$tmp = explode(':', $matches[1]); $tmp = explode(':', $matches[1]);
if (count($tmp) < 3) return ''; if (count($tmp) < 3) return '';
$tmpescaped = $tmp[2]; $tmpescaped = $tmp[2];
$regbis = array(); $regbis = array();
if (preg_match('/^\'(.*)\'$/', $tmpescaped, $regbis)) if (preg_match('/^\'(.*)\'$/', $tmpescaped, $regbis))
{ {
$tmpescaped = "'".$db->escape($regbis[1])."'"; $tmpescaped = "'".$db->escape($regbis[1])."'";
} else { } else {
$tmpescaped = $db->escape($tmpescaped); $tmpescaped = $db->escape($tmpescaped);
} }
return $db->escape($tmp[0]).' '.strtoupper($db->escape($tmp[1]))." ".$tmpescaped; return $db->escape($tmp[0]).' '.strtoupper($db->escape($tmp[1]))." ".$tmpescaped;
} }
} }

View File

@ -45,7 +45,7 @@ class Thirdparties extends DolibarrApi
/** /**
* Constructor * Constructor
*/ */
public function __construct() public function __construct()
{ {
global $db, $conf; global $db, $conf;
$this->db = $db; $this->db = $db;
@ -72,10 +72,10 @@ class Thirdparties extends DolibarrApi
* *
* @throws RestException * @throws RestException
*/ */
public function get($id) public function get($id)
{ {
return $this->_fetch($id); return $this->_fetch($id);
} }
/** /**
* Get properties of a thirdparty object by email. * Get properties of a thirdparty object by email.
@ -91,7 +91,7 @@ class Thirdparties extends DolibarrApi
*/ */
public function getByEmail($email) public function getByEmail($email)
{ {
return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email); return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
} }
/** /**
@ -108,7 +108,7 @@ class Thirdparties extends DolibarrApi
*/ */
public function getByBarcode($barcode) public function getByBarcode($barcode)
{ {
return $this->_fetch('', '', '', $barcode); return $this->_fetch('', '', '', $barcode);
} }
/** /**
@ -128,8 +128,8 @@ class Thirdparties extends DolibarrApi
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.nom:like:'TheCompany%') and (t.date_creation:<:'20160101')" * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.nom:like:'TheCompany%') and (t.date_creation:<:'20160101')"
* @return array Array of thirdparty objects * @return array Array of thirdparty objects
*/ */
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '') public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '')
{ {
global $db; global $db;
$obj_ret = array(); $obj_ret = array();
@ -144,10 +144,10 @@ class Thirdparties extends DolibarrApi
$sql = "SELECT t.rowid"; $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) 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."societe as t"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
if ($category > 0) { if ($category > 0) {
if ($mode != 4) $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c"; if ($mode != 4) $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
if (!in_array($mode, array(1, 2, 3))) $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc"; if (!in_array($mode, array(1, 2, 3))) $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
} }
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 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 .= ", ".MAIN_DB_PREFIX."c_stcomm as st"; $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
$sql .= " WHERE t.entity IN (".getEntity('societe').")"; $sql .= " WHERE t.entity IN (".getEntity('societe').")";
@ -158,12 +158,12 @@ class Thirdparties extends DolibarrApi
if ($mode == 3) $sql .= " AND t.client IN (0)"; if ($mode == 3) $sql .= " AND t.client IN (0)";
if ($mode == 4) $sql .= " AND t.fournisseur IN (1)"; if ($mode == 4) $sql .= " AND t.fournisseur IN (1)";
// Select thirdparties of given category // Select thirdparties of given category
if ($category > 0) { if ($category > 0) {
if (!empty($mode) && $mode != 4) { $sql .= " AND c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid"; } if (!empty($mode) && $mode != 4) { $sql .= " AND c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid"; }
elseif (!empty($mode) && $mode == 4) { $sql .= " AND cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid"; } elseif (!empty($mode) && $mode == 4) { $sql .= " AND cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid"; }
else { $sql .= " AND ((c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid))"; } else { $sql .= " AND ((c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid))"; }
} }
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc";
//if ($email != NULL) $sql.= " AND s.email = \"".$email."\""; //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
@ -202,7 +202,7 @@ class Thirdparties extends DolibarrApi
{ {
$num = $this->db->num_rows($result); $num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit)); $min = min($num, ($limit <= 0 ? $num : $limit));
$i = 0; $i = 0;
while ($i < $min) while ($i < $min)
{ {
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
@ -227,7 +227,7 @@ class Thirdparties extends DolibarrApi
* @param array $request_data Request datas * @param array $request_data Request datas
* @return int ID of thirdparty * @return int ID of thirdparty
*/ */
public function post($request_data = null) public function post($request_data = null)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -251,7 +251,7 @@ class Thirdparties extends DolibarrApi
* @param array $request_data Datas * @param array $request_data Datas
* @return int * @return int
*/ */
public function put($id, $request_data = null) public function put($id, $request_data = null)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -291,7 +291,7 @@ class Thirdparties extends DolibarrApi
* *
* @url PUT {id}/merge/{idtodelete} * @url PUT {id}/merge/{idtodelete}
*/ */
public function merge($id, $idtodelete) public function merge($id, $idtodelete)
{ {
global $db, $hookmanager; global $db, $hookmanager;
@ -443,7 +443,7 @@ class Thirdparties extends DolibarrApi
// External modules should update their ones too // External modules should update their ones too
if (!$errors) if (!$errors)
{ {
$reshook = $hookmanager->executeHooks('replaceThirdparty', array( $reshook = $hookmanager->executeHooks('replaceThirdparty', array(
'soc_origin' => $soc_origin->id, 'soc_origin' => $soc_origin->id,
'soc_dest' => $object->id 'soc_dest' => $object->id
), $soc_dest, $action); ), $soc_dest, $action);
@ -499,7 +499,7 @@ class Thirdparties extends DolibarrApi
* @param int $id Thirparty ID * @param int $id Thirparty ID
* @return integer * @return integer
*/ */
public function delete($id) public function delete($id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->supprimer) { if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
throw new RestException(401); throw new RestException(401);
@ -511,9 +511,9 @@ class Thirdparties extends DolibarrApi
if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$this->company->oldcopy = clone $this->company; $this->company->oldcopy = clone $this->company;
return $this->company->delete($id); return $this->company->delete($id);
} }
/** /**
* Set new price level for the given thirdparty * Set new price level for the given thirdparty
@ -588,7 +588,7 @@ class Thirdparties extends DolibarrApi
* *
* @url GET {id}/categories * @url GET {id}/categories
*/ */
public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
{ {
if (!DolibarrApiAccess::$user->rights->categorie->lire) { if (!DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401); throw new RestException(401);
@ -627,7 +627,7 @@ class Thirdparties extends DolibarrApi
* *
* @url POST {id}/categories/{category_id} * @url POST {id}/categories/{category_id}
*/ */
public function addCategory($id, $category_id) public function addCategory($id, $category_id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -665,7 +665,7 @@ class Thirdparties extends DolibarrApi
* *
* @url DELETE {id}/categories/{category_id} * @url DELETE {id}/categories/{category_id}
*/ */
public function deleteCategory($id, $category_id) public function deleteCategory($id, $category_id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -706,7 +706,7 @@ class Thirdparties extends DolibarrApi
* *
* @url GET {id}/supplier_categories * @url GET {id}/supplier_categories
*/ */
public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
{ {
if (!DolibarrApiAccess::$user->rights->categorie->lire) { if (!DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401); throw new RestException(401);
@ -745,7 +745,7 @@ class Thirdparties extends DolibarrApi
* *
* @url POST {id}/supplier_categories/{category_id} * @url POST {id}/supplier_categories/{category_id}
*/ */
public function addSupplierCategory($id, $category_id) public function addSupplierCategory($id, $category_id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -783,7 +783,7 @@ class Thirdparties extends DolibarrApi
* *
* @url DELETE {id}/supplier_categories/{category_id} * @url DELETE {id}/supplier_categories/{category_id}
*/ */
public function deleteSupplierCategory($id, $category_id) public function deleteSupplierCategory($id, $category_id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -826,7 +826,7 @@ class Thirdparties extends DolibarrApi
* @throws RestException 401 * @throws RestException 401
* @throws RestException 404 * @throws RestException 404
*/ */
public function getOutStandingProposals($id, $mode = 'customer') public function getOutStandingProposals($id, $mode = 'customer')
{ {
$obj_ret = array(); $obj_ret = array();
@ -870,7 +870,7 @@ class Thirdparties extends DolibarrApi
* @throws RestException 401 * @throws RestException 401
* @throws RestException 404 * @throws RestException 404
*/ */
public function getOutStandingOrder($id, $mode = 'customer') public function getOutStandingOrder($id, $mode = 'customer')
{ {
$obj_ret = array(); $obj_ret = array();
@ -913,7 +913,7 @@ class Thirdparties extends DolibarrApi
* @throws RestException 401 * @throws RestException 401
* @throws RestException 404 * @throws RestException 404
*/ */
public function getOutStandingInvoices($id, $mode = 'customer') public function getOutStandingInvoices($id, $mode = 'customer')
{ {
$obj_ret = array(); $obj_ret = array();
@ -956,7 +956,7 @@ class Thirdparties extends DolibarrApi
* @throws RestException 401 * @throws RestException 401
* @throws RestException 404 * @throws RestException 404
*/ */
public function getSalesRepresentatives($id, $mode = 0) public function getSalesRepresentatives($id, $mode = 0)
{ {
$obj_ret = array(); $obj_ret = array();
@ -999,7 +999,7 @@ class Thirdparties extends DolibarrApi
* @throws RestException 404 * @throws RestException 404
* @throws RestException 503 * @throws RestException 503
*/ */
public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC') public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
{ {
$obj_ret = array(); $obj_ret = array();
@ -1057,8 +1057,8 @@ class Thirdparties extends DolibarrApi
* @throws RestException 404 * @throws RestException 404
* @throws RestException 405 * @throws RestException 405
*/ */
public function getInvoicesQualifiedForReplacement($id) public function getInvoicesQualifiedForReplacement($id)
{ {
if (!DolibarrApiAccess::$user->rights->facture->lire) { if (!DolibarrApiAccess::$user->rights->facture->lire) {
throw new RestException(401); throw new RestException(401);
} }
@ -1100,8 +1100,8 @@ class Thirdparties extends DolibarrApi
* @throws RestException 404 * @throws RestException 404
* @throws RestException 405 * @throws RestException 405
*/ */
public function getInvoicesQualifiedForCreditNote($id) public function getInvoicesQualifiedForCreditNote($id)
{ {
if (!DolibarrApiAccess::$user->rights->facture->lire) { if (!DolibarrApiAccess::$user->rights->facture->lire) {
throw new RestException(401); throw new RestException(401);
} }
@ -1136,8 +1136,8 @@ class Thirdparties extends DolibarrApi
* *
* @url GET {id}/bankaccounts * @url GET {id}/bankaccounts
*/ */
public function getCompanyBankAccount($id) public function getCompanyBankAccount($id)
{ {
global $db, $conf; global $db, $conf;
if (!DolibarrApiAccess::$user->rights->facture->lire) { if (!DolibarrApiAccess::$user->rights->facture->lire) {
@ -1214,7 +1214,7 @@ class Thirdparties extends DolibarrApi
* *
* @url POST {id}/bankaccounts * @url POST {id}/bankaccounts
*/ */
public function createCompanyBankAccount($id, $request_data = null) public function createCompanyBankAccount($id, $request_data = null)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -1257,7 +1257,7 @@ class Thirdparties extends DolibarrApi
* *
* @url PUT {id}/bankaccounts/{bankaccount_id} * @url PUT {id}/bankaccounts/{bankaccount_id}
*/ */
public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null) public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
@ -1301,8 +1301,8 @@ class Thirdparties extends DolibarrApi
* *
* @url DELETE {id}/bankaccounts/{bankaccount_id} * @url DELETE {id}/bankaccounts/{bankaccount_id}
*/ */
public function deleteCompanyBankAccount($id, $bankaccount_id) public function deleteCompanyBankAccount($id, $bankaccount_id)
{ {
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
} }
@ -1408,9 +1408,9 @@ class Thirdparties extends DolibarrApi
} else { } else {
throw new RestException(500); throw new RestException(500);
} }
} }
/** /**
* Get a specific gateway attached to a thirdparty (by specifying the site key) * Get a specific gateway attached to a thirdparty (by specifying the site key)
* *
* @param int $id ID of thirdparty * @param int $id ID of thirdparty
@ -1422,8 +1422,8 @@ class Thirdparties extends DolibarrApi
* *
* @url GET {id}/gateways/ * @url GET {id}/gateways/
*/ */
public function getSocieteAccounts($id, $site = null) public function getSocieteAccounts($id, $site = null)
{ {
global $db, $conf; global $db, $conf;
if (!DolibarrApiAccess::$user->rights->societe->lire) { if (!DolibarrApiAccess::$user->rights->societe->lire) {
@ -1499,7 +1499,7 @@ class Thirdparties extends DolibarrApi
* *
* @url POST {id}/gateways * @url POST {id}/gateways
*/ */
public function createSocieteAccount($id, $request_data = null) public function createSocieteAccount($id, $request_data = null)
{ {
global $db; global $db;
@ -1558,7 +1558,7 @@ class Thirdparties extends DolibarrApi
* *
* @url PUT {id}/gateways/{site} * @url PUT {id}/gateways/{site}
*/ */
public function putSocieteAccount($id, $site, $request_data = null) public function putSocieteAccount($id, $site, $request_data = null)
{ {
global $db; global $db;
@ -1641,7 +1641,7 @@ class Thirdparties extends DolibarrApi
* *
* @url PATCH {id}/gateways/{site} * @url PATCH {id}/gateways/{site}
*/ */
public function patchSocieteAccount($id, $site, $request_data = null) public function patchSocieteAccount($id, $site, $request_data = null)
{ {
global $db; global $db;
@ -1694,8 +1694,8 @@ class Thirdparties extends DolibarrApi
* *
* @url DELETE {id}/gateways/{site} * @url DELETE {id}/gateways/{site}
*/ */
public function deleteSocieteAccount($id, $site) public function deleteSocieteAccount($id, $site)
{ {
global $db; global $db;
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
@ -1730,8 +1730,8 @@ class Thirdparties extends DolibarrApi
* *
* @url DELETE {id}/gateways * @url DELETE {id}/gateways
*/ */
public function deleteSocieteAccounts($id) public function deleteSocieteAccounts($id)
{ {
global $db; global $db;
if (!DolibarrApiAccess::$user->rights->societe->creer) { if (!DolibarrApiAccess::$user->rights->societe->creer) {
@ -1767,7 +1767,7 @@ class Thirdparties extends DolibarrApi
} }
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
@ -1775,8 +1775,8 @@ class Thirdparties extends DolibarrApi
* @return Object Object with cleaned properties * @return Object Object with cleaned properties
*/ */
protected function _cleanObjectDatas($object) protected function _cleanObjectDatas($object)
{ {
// phpcs:enable // phpcs:enable
$object = parent::_cleanObjectDatas($object); $object = parent::_cleanObjectDatas($object);
unset($object->nom); // ->name already defined and nom deprecated unset($object->nom); // ->name already defined and nom deprecated
@ -1812,23 +1812,23 @@ class Thirdparties extends DolibarrApi
* *
* @throws RestException * @throws RestException
*/ */
private function _validate($data) private function _validate($data)
{ {
$thirdparty = array(); $thirdparty = array();
foreach (Thirdparties::$FIELDS as $field) { foreach (Thirdparties::$FIELDS as $field) {
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$thirdparty[$field] = $data[$field]; $thirdparty[$field] = $data[$field];
} }
return $thirdparty; return $thirdparty;
} }
/** /**
* Fetch properties of a thirdparty object. * Fetch properties of a thirdparty object.
* *
* Return an array with thirdparty informations * Return an array with thirdparty informations
* *
* @param int $rowid Id of third party to load * @param int $rowid Id of third party to load
* @param string $ref Reference of third party, name (Warning, this can return several records) * @param string $ref Reference of third party, name (Warning, this can return several records)
* @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr) * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr)
* @param string $barcode Barcode of third party to load * @param string $barcode Barcode of third party to load
@ -1840,42 +1840,42 @@ class Thirdparties extends DolibarrApi
* @param string $idprof6 Prof id 6 of third party (Warning, this can return several records) * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records)
* @param string $email Email of third party (Warning, this can return several records) * @param string $email Email of third party (Warning, this can return several records)
* @param string $ref_alias Name_alias of third party (Warning, this can return several records) * @param string $ref_alias Name_alias of third party (Warning, this can return several records)
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @throws RestException * @throws RestException
*/ */
private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '') private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
{ {
global $conf; global $conf;
if (!DolibarrApiAccess::$user->rights->societe->lire) { if (!DolibarrApiAccess::$user->rights->societe->lire) {
throw new RestException(401); throw new RestException(401);
} }
if ($rowid == 0) { if ($rowid == 0) {
$result = $this->company->initAsSpecimen(); $result = $this->company->initAsSpecimen();
} else { } else {
$result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias); $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
} }
if (!$result) { if (!$result) {
throw new RestException(404, 'Thirdparty not found'); throw new RestException(404, 'Thirdparty not found');
} }
if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
$filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
$filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
} else { } else {
$filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')"; $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
$filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')"; $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
} }
$absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount); $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
$absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote); $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
$this->company->absolute_discount = price2num($absolute_discount, 'MT'); $this->company->absolute_discount = price2num($absolute_discount, 'MT');
$this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT'); $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
return $this->_cleanObjectDatas($this->company); return $this->_cleanObjectDatas($this->company);
} }
} }

View File

@ -562,7 +562,7 @@ class Users extends DolibarrApi
return $this->useraccount->delete(DolibarrApiAccess::$user); return $this->useraccount->delete(DolibarrApiAccess::$user);
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
@ -571,7 +571,7 @@ class Users extends DolibarrApi
*/ */
protected function _cleanObjectDatas($object) protected function _cleanObjectDatas($object)
{ {
// phpcs:enable // phpcs:enable
global $conf; global $conf;
$object = parent::_cleanObjectDatas($object); $object = parent::_cleanObjectDatas($object);