Standardize code

This commit is contained in:
Laurent Destailleur
2017-10-07 14:11:01 +02:00
parent 66978a3bb4
commit af1d2bfffd
5 changed files with 96 additions and 90 deletions

View File

@@ -370,8 +370,10 @@ class Proposals extends DolibarrApi
* @url DELETE {id}/lines/{lineid} * @url DELETE {id}/lines/{lineid}
* *
* @return int * @return int
* @throws 401
* @throws 404
*/ */
function delLine($id, $lineid) { function deleteLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->propal->creer) { if(! DolibarrApiAccess::$user->rights->propal->creer) {
throw new RestException(401); throw new RestException(401);
} }
@@ -384,9 +386,10 @@ class Proposals extends DolibarrApi
if( ! DolibarrApi::_checkAccessToResource('propal',$this->propal->id)) { if( ! DolibarrApi::_checkAccessToResource('propal',$this->propal->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);
} }
$request_data = (object) $request_data;
$request_data = (object) $request_data;
$updateRes = $this->propal->deleteline($lineid); $updateRes = $this->propal->deleteline($lineid);
if ($updateRes == 1) { if ($updateRes > 0) {
return $this->get($id); return $this->get($id);
} }
return false; return false;

View File

@@ -389,7 +389,7 @@ class CommandeApi extends DolibarrApi
* *
* @return int * @return int
*/ */
function delLine($id, $lineid) { function deleteLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->commande->creer) { if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401); throw new RestException(401);
} }

View File

@@ -353,14 +353,16 @@ class Orders extends DolibarrApi
* Delete a line to given order * Delete a line to given order
* *
* *
* @param int $id Id of commande to update * @param int $id Id of order to update
* @param int $lineid Id of line to delete * @param int $lineid Id of line to delete
* *
* @url DELETE {id}/lines/{lineid} * @url DELETE {id}/lines/{lineid}
* *
* @return int * @return int
* @throws 401
* @throws 404
*/ */
function delLine($id, $lineid) { function deleteLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->commande->creer) { if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401); throw new RestException(401);
} }
@@ -373,7 +375,8 @@ class Orders extends DolibarrApi
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); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$request_data = (object) $request_data;
$request_data = (object) $request_data;
$updateRes = $this->commande->deleteline(DolibarrApiAccess::$user,$lineid); $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user,$lineid);
if ($updateRes > 0) { if ($updateRes > 0) {
return $this->get($id); return $this->get($id);

View File

@@ -312,13 +312,13 @@ class Invoices extends DolibarrApi
* @param int $id Id of invoice * @param int $id Id of invoice
* @param int $lineid Id of the line to delete * @param int $lineid Id of the line to delete
* *
* @url DELETE {id}/deleteline/{lineid} * @url DELETE {id}/lines/{lineid}
* *
* @return array * @return array
* @throws 304
* @throws 400 * @throws 400
* @throws 401 * @throws 401
* @throws 404 * @throws 404
* @throws 405
*/ */
function deleteLine($id, $lineid) { function deleteLine($id, $lineid) {
@@ -340,7 +340,7 @@ class Invoices extends DolibarrApi
$result = $this->invoice->deleteline($lineid); $result = $this->invoice->deleteline($lineid);
if( $result < 0) { if( $result < 0) {
throw new RestException(304); throw new RestException(405, $this->invoice->error);
} }
$result = $this->invoice->fetch($id); $result = $this->invoice->fetch($id);
@@ -355,11 +355,11 @@ class Invoices extends DolibarrApi
/** /**
* Add a line to a given invoice * Add a line to a given invoice
* *
* Exemple of POST query : { "desc": "Desc", "subprice": "1.00000000", "qty": "1", "tva_tx": "20.000", "localtax1_tx": "0.000", "localtax2_tx": "0.000", "fk_product": "1", "remise_percent": "0", "date_start": "", "date_end": "", "fk_code_ventilation": 0, "info_bits": "0", "fk_remise_except": null, "product_type": "1", "rang": "-1", "special_code": "0", "fk_parent_line": null, "fk_fournprice": null, "pa_ht": "0.00000000", "label": "", "array_options": [], "situation_percent": "100", "fk_prev_id": null, "fk_unit": null } * Exemple of POST query : { "desc": "Desc", "subprice": "1.00000000", "qty": "1", "tva_tx": "20.000", "localtax1_tx": "0.000", "localtax2_tx": "0.000", "fk_product": "1", "remise_percent": "0", "date_start": "", "date_end": "", "fk_code_ventilation": 0, "info_bits": "0", "fk_remise_except": null, "product_type": "1", "rang": "-1", "special_code": "0", "fk_parent_line": null, "fk_fournprice": null, "pa_ht": "0.00000000", "label": "", "array_options": [], "situation_percent": "100", "fk_prev_id": null, "fk_unit": null }
* *
* @param int $id Id of invoice * @param int $id Id of invoice
* @param array $request_data Invoiceline data * @param array $request_data Invoiceline data
@@ -381,14 +381,14 @@ class Invoices extends DolibarrApi
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->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);
} }
$request_data = (object) $request_data; $request_data = (object) $request_data;
// Reset fk_parent_line for no child products and special product // Reset fk_parent_line for no child products and special product
if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) { if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
$request_data->fk_parent_line = 0; $request_data->fk_parent_line = 0;
} }
$updateRes = $this->invoice->addline( $updateRes = $this->invoice->addline(
$request_data->desc, $request_data->desc,
$request_data->subprice, $request_data->subprice,
@@ -436,13 +436,13 @@ class Invoices extends DolibarrApi
* @url POST {id}/settodraft * @url POST {id}/settodraft
* *
* @return array * @return array
* *
* @throws 200 * @throws 200
* @throws 304 * @throws 304
* @throws 401 * @throws 401
* @throws 404 * @throws 404
* @throws 500 * @throws 500
* *
*/ */
function settodraft($id, $idwarehouse=-1) function settodraft($id, $idwarehouse=-1)
{ {

View File

@@ -1,7 +1,7 @@
<?php <?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr> /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
@@ -23,14 +23,14 @@
/** /**
* API class for Expense Reports * API class for Expense Reports
* *
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class ExpenseReports extends DolibarrApi class ExpenseReports extends DolibarrApi
{ {
/** /**
* @var array $FIELDS Mandatory fields, checked when create and update object * @var array $FIELDS Mandatory fields, checked when create and update object
*/ */
static $FIELDS = array( static $FIELDS = array(
'socid' 'socid'
@@ -41,7 +41,7 @@ class ExpenseReports extends DolibarrApi
*/ */
public $expensereport; public $expensereport;
/** /**
* Constructor * Constructor
*/ */
@@ -56,36 +56,36 @@ class ExpenseReports extends DolibarrApi
* Get properties of a Expense Report object * Get properties of a Expense Report object
* *
* Return an array with Expense Report informations * Return an array with Expense Report informations
* *
* @param int $id ID of Expense Report * @param int $id ID of Expense Report
* @return array|mixed Data without useless information * @return array|mixed Data without useless information
* *
* @throws RestException * @throws RestException
*/ */
function get($id) function get($id)
{ {
if(! DolibarrApiAccess::$user->rights->expensereport->lire) { if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Expense report not found'); throw new RestException(404, 'Expense report not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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->expensereport->fetchObjectLinked(); $this->expensereport->fetchObjectLinked();
return $this->_cleanObjectDatas($this->expensereport); return $this->_cleanObjectDatas($this->expensereport);
} }
/** /**
* List Expense Reports * List Expense Reports
* *
* Get a list of Expense Reports * Get a list of Expense Reports
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
@@ -96,19 +96,19 @@ class ExpenseReports extends DolibarrApi
*/ */
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') { function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
// case of external user, $societe param is ignored and replaced by user's socid // case of external user, $societe param is ignored and replaced by user's socid
//$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe; //$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe;
$sql = "SELECT t.rowid"; $sql = "SELECT t.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport as t"; $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as t";
$sql.= ' WHERE t.entity IN ('.getEntity('expensereport').')'; $sql.= ' WHERE t.entity IN ('.getEntity('expensereport').')';
if ($user_ids) $sql.=" AND t.fk_user_author IN (".$user_ids.")"; if ($user_ids) $sql.=" AND t.fk_user_author IN (".$user_ids.")";
// Add sql filters // Add sql filters
if ($sqlfilters) if ($sqlfilters)
{ {
if (! DolibarrApi::_checkFilters($sqlfilters)) if (! DolibarrApi::_checkFilters($sqlfilters))
{ {
@@ -117,7 +117,7 @@ class ExpenseReports extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
} }
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
if ($page < 0) if ($page < 0)
@@ -130,7 +130,7 @@ class ExpenseReports extends DolibarrApi
} }
$result = $db->query($sql); $result = $db->query($sql);
if ($result) if ($result)
{ {
$num = $db->num_rows($result); $num = $db->num_rows($result);
@@ -181,7 +181,7 @@ class ExpenseReports extends DolibarrApi
if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) { if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors)); throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
} }
return $this->expensereport->id; return $this->expensereport->id;
} }
@@ -189,22 +189,22 @@ class ExpenseReports extends DolibarrApi
* Get lines of an Expense Report * Get lines of an Expense Report
* *
* @param int $id Id of Expense Report * @param int $id Id of Expense Report
* *
* @url GET {id}/lines * @url GET {id}/lines
* *
* @return int * @return int
*/ */
/* /*
function getLines($id) { function getLines($id) {
if(! DolibarrApiAccess::$user->rights->expensereport->lire) { if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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);
} }
@@ -216,28 +216,28 @@ class ExpenseReports extends DolibarrApi
return $result; return $result;
} }
*/ */
/** /**
* Add a line to given Expense Report * Add a line to given Expense Report
* *
* @param int $id Id of Expense Report to update * @param int $id Id of Expense Report to update
* @param array $request_data Expense Report data * @param array $request_data Expense Report data
* *
* @url POST {id}/lines * @url POST {id}/lines
* *
* @return int * @return int
*/ */
/* /*
function postLine($id, $request_data = NULL) { function postLine($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->expensereport->creer) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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);
} }
@@ -277,29 +277,29 @@ class ExpenseReports extends DolibarrApi
return false; return false;
} }
*/ */
/** /**
* Update a line to given Expense Report * Update a line to given Expense Report
* *
* @param int $id Id of Expense Report to update * @param int $id Id of Expense Report to update
* @param int $lineid Id of line to update * @param int $lineid Id of line to update
* @param array $request_data Expense Report data * @param array $request_data Expense Report data
* *
* @url PUT {id}/lines/{lineid} * @url PUT {id}/lines/{lineid}
* *
* @return object * @return object
*/ */
/* /*
function putLine($id, $lineid, $request_data = NULL) { function putLine($id, $lineid, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->expensereport->creer) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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);
} }
@@ -342,22 +342,22 @@ class ExpenseReports extends DolibarrApi
* *
* @param int $id Id of Expense Report to update * @param int $id Id of Expense Report to update
* @param int $lineid Id of line to delete * @param int $lineid Id of line to delete
* *
* @url DELETE {id}/lines/{lineid} * @url DELETE {id}/lines/{lineid}
* *
* @return int * @return int
*/ */
/* /*
function delLine($id, $lineid) { function deleteLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->expensereport->creer) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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);
} }
@@ -369,26 +369,26 @@ class ExpenseReports extends DolibarrApi
return false; return false;
} }
*/ */
/** /**
* Update Expense Report general fields (won't touch lines of expensereport) * Update Expense Report general fields (won't touch lines of expensereport)
* *
* @param int $id Id of Expense Report to update * @param int $id Id of Expense Report to update
* @param array $request_data Datas * @param array $request_data Datas
* *
* @return int * @return int
*/ */
/* /*
function put($id, $request_data = NULL) { function put($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->expensereport->creer) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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);
} }
@@ -396,19 +396,19 @@ class ExpenseReports extends DolibarrApi
if ($field == 'id') continue; if ($field == 'id') continue;
$this->expensereport->$field = $value; $this->expensereport->$field = $value;
} }
if($this->expensereport->update(DolibarrApiAccess::$user)) if($this->expensereport->update(DolibarrApiAccess::$user))
return $this->get($id); return $this->get($id);
return false; return false;
} }
*/ */
/** /**
* Delete Expense Report * Delete Expense Report
* *
* @param int $id Expense Report ID * @param int $id Expense Report ID
* *
* @return array * @return array
*/ */
function delete($id) function delete($id)
@@ -420,31 +420,31 @@ class ExpenseReports extends DolibarrApi
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Expense Report not found'); throw new RestException(404, 'Expense Report not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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( ! $this->expensereport->delete(DolibarrApiAccess::$user)) { if( ! $this->expensereport->delete(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error); throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
} }
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
'message' => 'Expense Report deleted' 'message' => 'Expense Report deleted'
) )
); );
} }
/** /**
* Validate an Expense Report * Validate an Expense Report
* *
* @param int $id Expense Report ID * @param int $id Expense Report ID
* *
* @url POST {id}/validate * @url POST {id}/validate
* *
* @return array * @return array
* FIXME An error 403 is returned if the request has an empty body. * FIXME An error 403 is returned if the request has an empty body.
* Error message: "Forbidden: Content type `text/plain` is not supported." * Error message: "Forbidden: Content type `text/plain` is not supported."
@@ -463,15 +463,15 @@ class ExpenseReports extends DolibarrApi
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->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( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) { if( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) {
throw new RestException(500, 'Error when validate expensereport'); throw new RestException(500, 'Error when validate expensereport');
} }
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
@@ -479,12 +479,12 @@ class ExpenseReports extends DolibarrApi
) )
); );
}*/ }*/
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Array with data to verify * @param array $data Array with data to verify
* @return array * @return array
* @throws RestException * @throws RestException
*/ */
function _validate($data) function _validate($data)
@@ -494,7 +494,7 @@ class ExpenseReports extends DolibarrApi
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$expensereport[$field] = $data[$field]; $expensereport[$field] = $data[$field];
} }
return $expensereport; return $expensereport;
} }