Skip to content

Commit

Permalink
Merge pull request #7804 from Oeris/develop-api
Browse files Browse the repository at this point in the history
NEW Update in the order REST API
  • Loading branch information
eldy committed Nov 14, 2017
2 parents 010e542 + 4212000 commit 765a209
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
14 changes: 14 additions & 0 deletions htdocs/api/class/api_documents.class.php
Expand Up @@ -119,6 +119,20 @@ public function index($module_part, $original_file='', $regeneratedoc=0)
throw new RestException(500, 'Error generating document');
}
}
if ($module_part == 'commande' || $module_part == 'order')
{
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
$this->order = new Commande($this->db);
$result = $this->order->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file)));
if( ! $result ) {
throw new RestException(404, 'Order not found');
}
$result = $this->order->generateDocument($this->order->modelpdf, $langs, $hidedetails, $hidedesc, $hideref);
if( $result <= 0 ) {
throw new RestException(500, 'Error generating document');
}
}

}

$filename = basename($original_file);
Expand Down
76 changes: 65 additions & 11 deletions htdocs/commande/class/api_orders.class.php
Expand Up @@ -396,24 +396,30 @@ function deleteLine($id, $lineid) {
* @return int
*/
function put($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
if (! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}

$result = $this->commande->fetch($id);
if( ! $result ) {
if (! $result) {
throw new RestException(404, 'Order not found');
}

if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
if (! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
foreach($request_data as $field => $value) {
if ($field == 'id') continue;
$this->commande->$field = $value;
}

if($this->commande->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
// Update availability
if (!empty($this->commande->availability_id)) {
if ($this->commande->availability($this->commande->availability_id) < 0)
throw new RestException(400, 'Error while updating availability');
}

if ($this->commande->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
return $this->get($id);

return false;
Expand Down Expand Up @@ -487,18 +493,22 @@ function validate($id, $idwarehouse=0, $notrigger=0)

$result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
if ($result == 0) {
throw new RestException(500, 'Error nothing done. May be object is already validated');
throw new RestException(304, 'Error nothing done. May be object is already validated');
}
if ($result < 0) {
throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}

return array(
'success' => array(
'code' => 200,
'message' => 'Order validated (Ref='.$this->commande->ref.')'
)
);
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$this->commande->fetchObjectLinked();
return $this->_cleanObjectDatas($this->commande);
}

/**
Expand Down Expand Up @@ -541,6 +551,50 @@ function close($id, $notrigger=0)
);
}

/**
* Set an order to draft
*
* @param int $id Order ID
*
* @url POST {id}/settodraft
*
* @return array
*/
function settodraft($id)
{
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}

if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$result = $this->commande->set_draft(DolibarrApiAccess::$user);
if ($result == 0) {
throw new RestException(304, 'Nothing done. May be object is already closed');
}
if ($result < 0) {
throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
}

$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}

if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$this->commande->fetchObjectLinked();
return $this->_cleanObjectDatas($this->commande);
}


/**
* Clean sensible object datas
Expand Down

0 comments on commit 765a209

Please sign in to comment.