Merge pull request #7965 from Oeris/develop-api

NEW improvements of invoices, orders and proposals in the REST API
This commit is contained in:
Laurent Destailleur
2017-12-19 18:17:48 +01:00
committed by GitHub
3 changed files with 252 additions and 7 deletions

View File

@@ -290,8 +290,9 @@ class Proposals extends DolibarrApi
if ($updateRes > 0) { if ($updateRes > 0) {
return $updateRes; return $updateRes;
} }
else {
return false; throw new RestException(400, $this->propal->error);
}
} }
/** /**
@@ -422,6 +423,19 @@ class Proposals extends DolibarrApi
$this->propal->$field = $value; $this->propal->$field = $value;
} }
// update end of validity date
if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation))
{
$this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
}
if (!empty($this->propal->fin_validite))
{
if($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite)<0)
{
throw new RestException(500, $this->propal->error);
}
}
if ($this->propal->update(DolibarrApiAccess::$user) > 0) if ($this->propal->update(DolibarrApiAccess::$user) > 0)
{ {
return $this->get($id); return $this->get($id);
@@ -466,6 +480,51 @@ class Proposals extends DolibarrApi
} }
/**
* Set a proposal to draft
*
* @param int $id Order ID
*
* @url POST {id}/settodraft
*
* @return array
*/
function settodraft($id)
{
if(! DolibarrApiAccess::$user->rights->propal->creer) {
throw new RestException(401);
}
$result = $this->propal->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Proposal not found');
}
if( ! DolibarrApi::_checkAccessToResource('propal',$this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->propal->set_draft(DolibarrApiAccess::$user);
if ($result == 0) {
throw new RestException(304, 'Nothing done. May be object is already draft');
}
if ($result < 0) {
throw new RestException(500, 'Error : '.$this->propal->error);
}
$result = $this->propal->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Proposal not found');
}
if( ! DolibarrApi::_checkAccessToResource('propal',$this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->propal->fetchObjectLinked();
return $this->_cleanObjectDatas($this->propal);
}
/** /**
* Validate a commercial proposal * Validate a commercial proposal
* *

View File

@@ -418,6 +418,15 @@ class Orders extends DolibarrApi
if ($this->commande->availability($this->commande->availability_id) < 0) if ($this->commande->availability($this->commande->availability_id) < 0)
throw new RestException(400, 'Error while updating availability'); throw new RestException(400, 'Error while updating availability');
} }
// update bank account
if(!empty($this->commande->fk_account))
{
if($this->commande->setBankAccount($this->commande->fk_account) == 0)
{
throw new RestException(400,$this->commande->error);
}
}
if ($this->commande->update(DolibarrApiAccess::$user) > 0) if ($this->commande->update(DolibarrApiAccess::$user) > 0)
{ {
@@ -515,6 +524,80 @@ class Orders extends DolibarrApi
return $this->_cleanObjectDatas($this->commande); return $this->_cleanObjectDatas($this->commande);
} }
/**
* Tag the order as validated (opened)
*
* Function used when order is reopend after being closed.
*
* @param int $id Id of the order
*
* @url POST {id}/reopen
*
* @return int
*
* @throws 304
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
function reopen($id) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
if(empty($id)) {
throw new RestException(400, 'Order ID is mandatory');
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}
$result = $this->commande->set_reopen(DolibarrApiAccess::$user);
if( $result < 0) {
throw new RestException(405, $this->commande->error);
}else if( $result == 0) {
throw new RestException(304);
}
return $result;
}
/**
* Classify the order as invoiced
*
* @param int $id Id of the order
*
* @url POST {id}/setinvoiced
*
* @return int
*
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
function setinvoiced($id) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
if(empty($id)) {
throw new RestException(400, 'Order ID is mandatory');
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}
$result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
if( $result < 0) {
throw new RestException(400, $this->commande->error);
}
return $result;
}
/** /**
* Close an order (Classify it as "Delivered") * Close an order (Classify it as "Delivered")
* *
@@ -601,6 +684,49 @@ class Orders extends DolibarrApi
} }
/**
* Create an order using an existing proposal.
*
*
* @param int $proposalid Id of the proposal
*
* @url POST /createfromproposal/{proposalid}
*
* @return int
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
function createOrderFromProposal($proposalid) {
require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
if(! DolibarrApiAccess::$user->rights->propal->lire) {
throw new RestException(401);
}
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
if(empty($proposalid)) {
throw new RestException(400, 'Proposal ID is mandatory');
}
$propal = new Propal($this->db);
$result = $propal->fetch($proposalid);
if( ! $result ) {
throw new RestException(404, 'Proposal not found');
}
$result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
if( $result < 0) {
throw new RestException(405, $this->commande->error);
}
$this->commande->fetchObjectLinked();
return $this->_cleanObjectDatas($this->commande);
}
/** /**
* Clean sensible object datas * Clean sensible object datas
* *

View File

@@ -217,6 +217,48 @@ class Invoices extends DolibarrApi
return $this->invoice->id; return $this->invoice->id;
} }
/**
* Create an invoice using an existing order.
*
*
* @param int $orderid Id of the order
*
* @url POST /createfromorder/{orderid}
*
* @return int
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
function createInvoiceFromOrder($orderid) {
require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
if(! DolibarrApiAccess::$user->rights->commande->lire) {
throw new RestException(401);
}
if(! DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401);
}
if(empty($orderid)) {
throw new RestException(400, 'Order ID is mandatory');
}
$order = new Commande($this->db);
$result = $order->fetch($orderid);
if( ! $result ) {
throw new RestException(404, 'Order not found');
}
$result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
if( $result < 0) {
throw new RestException(405, $this->invoice->error);
}
$this->invoice->fetchObjectLinked();
return $this->_cleanObjectDatas($this->invoice);
}
/** /**
* Get lines of an invoice * Get lines of an invoice
* *
@@ -380,6 +422,15 @@ class Invoices extends DolibarrApi
$this->invoice->$field = $value; $this->invoice->$field = $value;
} }
// update bank account
if(!empty($this->invoice->fk_account))
{
if($this->invoice->setBankAccount($this->invoice->fk_account) == 0)
{
throw new RestException(400,$this->invoice->error);
}
}
if($this->invoice->update($id, DolibarrApiAccess::$user)) if($this->invoice->update($id, DolibarrApiAccess::$user))
return $this->get ($id); return $this->get ($id);
@@ -430,6 +481,11 @@ class Invoices extends DolibarrApi
* @url POST {id}/lines * @url POST {id}/lines
* *
* @return int * @return int
*
* @throws 200
* @throws 401
* @throws 404
* @throws 400
*/ */
function postLine($id, $request_data = NULL) { function postLine($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->facture->creer) { if(! DolibarrApiAccess::$user->rights->facture->creer) {
@@ -452,6 +508,10 @@ class Invoices extends DolibarrApi
$request_data->fk_parent_line = 0; $request_data->fk_parent_line = 0;
} }
// calculate pa_ht
$marginInfos = getMarginInfos($request_data->subprice, $request_data->remise_percent, $request_data->tva_tx, $request_data->localtax1_tx, $request_data->localtax2_tx, $request_data->fk_fournprice, $request_data->pa_ht);
$pa_ht = $marginInfos[0];
$updateRes = $this->invoice->addline( $updateRes = $this->invoice->addline(
$request_data->desc, $request_data->desc,
$request_data->subprice, $request_data->subprice,
@@ -475,7 +535,7 @@ class Invoices extends DolibarrApi
$id, $id,
$request_data->fk_parent_line, $request_data->fk_parent_line,
$request_data->fk_fournprice, $request_data->fk_fournprice,
$request_data->pa_ht, $pa_ht,
$request_data->label, $request_data->label,
$request_data->array_options, $request_data->array_options,
$request_data->situation_percent, $request_data->situation_percent,
@@ -483,11 +543,11 @@ class Invoices extends DolibarrApi
$request_data->fk_unit $request_data->fk_unit
); );
if ($updateRes > 0) { if ($updateRes < 0) {
return $updateRes; throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
} }
throw new RestException(400, 'Unable to insert the new line. Check your inputs.');
return $updateRes;
} }
/** /**