Merge pull request #14328 from marc-dll/NEW_api_thirdparty_price_level

NEW: thirdparty REST API: endpoint to set price level
This commit is contained in:
Laurent Destailleur 2020-08-02 01:20:05 +02:00 committed by GitHub
commit 75d8455789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -490,6 +490,66 @@ class Thirdparties extends DolibarrApi
}
$this->company->oldcopy = clone $this->company;
return $this->company->delete($id);
}
/**
* Set new price level for the given thirdparty
*
* @param int $id ID of thirdparty
* @param int $priceLevel Price level to apply to thirdparty
* @return object Thirdparty data without useless information
*
* @url PUT {id}/setpricelevel
*
* @throws RestException 400 Price level out of bounds
* @throws RestException 401 Access not allowed for your login
* @throws RestException 404 Thirdparty not found
* @throws RestException 500 Error fetching/setting price level
* @throws RestException 501 Request needs modules "Thirdparties" and "Products" and setting Multiprices activated
*/
public function setThirdpartyPriceLevel($id, $priceLevel)
{
global $conf;
if (empty($conf->societe->enabled)) {
throw new RestException(501, 'Module "Thirdparties" needed for this request');
}
if (empty($conf->product->enabled)) {
throw new RestException(501, 'Module "Products" needed for this request');
}
if (empty($conf->global->PRODUIT_MULTIPRICES)) {
throw new RestException(501, 'Multiprices features activation needed for this request');
}
if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
throw new RestException(400, 'Price level must be between 1 and ' . $conf->global->PRODUIT_MULTIPRICES_LIMIT);
}
if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
throw new RestException(401, 'Access to thirdparty ' . $id . ' not allowed for login '. DolibarrApiAccess::$user->login);
}
$result = $this->company->fetch($id);
if ($result < 0) {
throw new RestException(404, 'Thirdparty ' . $id . ' not found');
}
if (empty($result)) {
throw new RestException(500, 'Error fetching thirdparty ' . $id, array_merge(array($this->company->error), $this->company->errors));
}
if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
throw new RestException(401, 'Access to thirdparty ' . $id . ' not allowed for login ' . DolibarrApiAccess::$user->login);
}
$result = $this->company->set_price_level($priceLevel, DolibarrApiAccess::$user);
if ($result <= 0) {
throw new RestException(500, 'Error setting new price level for thirdparty ' . $id, array($this->company->db->lasterror()));
}
return $this->_cleanObjectDatas($this->company);
}
/**