2
0
forked from Wavyzz/dolibarr

Api interventions

This commit is contained in:
Laurent Destailleur
2018-02-03 20:58:26 +01:00
parent 3fd0105512
commit c6fba7273d
4 changed files with 162 additions and 62 deletions

View File

@@ -145,6 +145,7 @@ if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/swagger.json' ||
$modulenameforenabled = $module; $modulenameforenabled = $module;
if ($module == 'propale') { $modulenameforenabled='propal'; } if ($module == 'propale') { $modulenameforenabled='propal'; }
if ($module == 'supplierproposal') { $modulenameforenabled='supplier_proposal'; } if ($module == 'supplierproposal') { $modulenameforenabled='supplier_proposal'; }
if ($module == 'ficheinter') { $modulenameforenabled='ficheinter'; }
dol_syslog("Found module file ".$file." - module=".$module." - modulenameforenabled=".$modulenameforenabled." - moduledirforclass=".$moduledirforclass); dol_syslog("Found module file ".$file." - module=".$module." - modulenameforenabled=".$modulenameforenabled." - moduledirforclass=".$moduledirforclass);
@@ -217,7 +218,7 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' &&
$moduledirforclass = getModuleDirForApiClass($module); $moduledirforclass = getModuleDirForApiClass($module);
// Load a dedicated API file // Load a dedicated API file
dol_syslog("Load a dedicated API file moduledirforclass=".$moduledirforclass); dol_syslog("Load a dedicated API file module=".$module." moduledirforclass=".$moduledirforclass);
$tmpmodule = $module; $tmpmodule = $module;
if ($tmpmodule != 'api') if ($tmpmodule != 'api')
@@ -229,6 +230,11 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' &&
$classfile = 'supplier_orders'; $classfile = 'supplier_orders';
if ($module == 'supplierinvoices') if ($module == 'supplierinvoices')
$classfile = 'supplier_invoices'; $classfile = 'supplier_invoices';
if ($module == 'ficheinter')
$classfile = 'interventions';
if ($module == 'interventions')
$classfile = 'interventions';
$dir_part_file = dol_buildpath('/' . $moduledirforclass . '/class/api_' . $classfile . '.class.php', 0, 2); $dir_part_file = dol_buildpath('/' . $moduledirforclass . '/class/api_' . $classfile . '.class.php', 0, 2);
$classname = ucwords($module); $classname = ucwords($module);

View File

@@ -472,7 +472,7 @@ class Orders extends DolibarrApi
/** /**
* Validate an order * Validate an order
* *
* If you get a bad value for param notrigger check that ou provide this in body * If you get a bad value for param notrigger check, provide this in body
* { * {
* "idwarehouse": 0, * "idwarehouse": 0,
* "notrigger": 0 * "notrigger": 0

View File

@@ -2258,6 +2258,9 @@ function getModuleDirForApiClass($module)
elseif ($module == 'users') { elseif ($module == 'users') {
$moduledirforclass = 'user'; $moduledirforclass = 'user';
} }
elseif ($module == 'ficheinter' || $module == 'interventions') {
$moduledirforclass = 'fichinter';
}
return $moduledirforclass; return $moduledirforclass;
} }

View File

@@ -26,7 +26,7 @@
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class Fichinters extends DolibarrApi class Interventions extends DolibarrApi
{ {
/** /**
@@ -42,7 +42,6 @@ class Fichinters 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 $FIELDSLINE = array( static $FIELDSLINE = array(
'fk_fichinter',
'description', 'description',
'date', 'date',
'duree' 'duree'
@@ -64,9 +63,38 @@ class Fichinters extends DolibarrApi
} }
/** /**
* List fichinters * Get properties of a Expense Report object
* *
* Get a list of fichinters * Return an array with Expense Report informations
*
* @param int $id ID of Expense Report
* @return array|mixed Data without useless information
*
* @throws RestException
*/
function get($id)
{
if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
throw new RestException(401);
}
$result = $this->fichinter->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Intervention report not found');
}
if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->fichinter->fetchObjectLinked();
return $this->_cleanObjectDatas($this->fichinter);
}
/**
* List of interventions
*
* Return a list of interventions
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
@@ -139,7 +167,7 @@ class Fichinters extends DolibarrApi
$obj = $db->fetch_object($result); $obj = $db->fetch_object($result);
$fichinter_static = new Fichinter($db); $fichinter_static = new Fichinter($db);
if($fichinter_static->fetch($obj->rowid)) { if($fichinter_static->fetch($obj->rowid)) {
$obj_ret[] = $fichinter_static; $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
} }
$i++; $i++;
} }
@@ -154,10 +182,10 @@ class Fichinters extends DolibarrApi
} }
/** /**
* Create fichinter object * Create intervention object
* *
* @param array $request_data Request data * @param array $request_data Request data
* @return int ID of fichinter * @return int ID of intervention
*/ */
function post($request_data = NULL) function post($request_data = NULL)
{ {
@@ -177,16 +205,50 @@ class Fichinters extends DolibarrApi
return $this->fichinter->id; return $this->fichinter->id;
} }
/** /**
* Create fichinter line object * Get lines of an intervention
* *
* @param int $id Id of intervention
*
* @url GET {id}/lines
*
* @return int
*/
/* TODO
function getLines($id) {
if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
throw new RestException(401);
}
$result = $this->fichinter->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Intervention not found');
}
if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->fichinter->getLinesArray();
$result = array();
foreach ($this->fichinter->lines as $line) {
array_push($result,$this->_cleanObjectDatas($line));
}
return $result;
}
*/
/**
* Add a line to given intervention
*
* @param int $id Id of intervention to update
* @param array $request_data Request data * @param array $request_data Request data
* *
* @url POST /line * @url POST {id}/lines
* *
* @return boolean Create line fichinter work * @return int
*/ */
function postLine($request_data = NULL) function postLine($id, $request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->ficheinter->creer) { if(! DolibarrApiAccess::$user->rights->ficheinter->creer) {
throw new RestException(401, "Insuffisant rights"); throw new RestException(401, "Insuffisant rights");
@@ -199,96 +261,107 @@ class Fichinters extends DolibarrApi
} }
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Fichinter not found'); throw new RestException(404, 'Intervention not found');
} }
if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) { if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->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->fichinter->addLine( $updateRes = $this->fichinter->addLine(
DolibarrApiAccess::$user, DolibarrApiAccess::$user,
$this->fichinter->fk_fichinter, $id,
$this->fichinter->description, $this->fichinter->description,
$this->fichinter->date, $this->fichinter->date,
$this->fichinter->duree) < 0) { $this->fichinter->duree
throw new RestException(500, "Error creating fichinter line", array_merge(array($this->fichinter->error), $this->fichinter->errors)); );
}
return $this->fichinter; if ($updateRes > 0) {
return $updateRes;
}
else {
throw new RestException(400, $this->fichinter->error);
}
} }
/** /**
* Validate a fichinter * Validate an intervention
* *
* @param int $id fichinter ID * If you get a bad value for param notrigger check, provide this in body
* {
* "notrigger": 0
* }
* *
* @url POST validate * @param int $id Intervention ID
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
*
* @url POST {id}/validate
* *
* @return array * @return array
*
*/ */
function validFichinter($id) function validate($id, $notrigger=0)
{ {
if(! DolibarrApiAccess::$user->rights->ficheinter->creer) { if(! DolibarrApiAccess::$user->rights->ficheinter->creer) {
throw new RestException(401, "Insuffisant rights"); throw new RestException(401, "Insuffisant rights");
} }
$result = $this->fichinter->fetch($id); $result = $this->fichinter->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Fichinter not found'); throw new RestException(404, 'Intervention not found');
} }
if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) { if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->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->fichinter->setValid(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when validate fichinter');
} }
return array( $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
'success' => array( if ($result == 0) {
'code' => 200, throw new RestException(304, 'Error nothing done. May be object is already validated');
'message' => 'Fichinter validated' }
) if ($result < 0) {
); throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
}
$this->fichinter->fetchObjectLinked();
return $this->_cleanObjectDatas($this->fichinter);
} }
/** /**
* Close a fichinter * Close an intervention
* *
* @param int $id fichinter ID * @param int $id Intervention ID
* *
* @url POST close * @url POST {id}/close
* *
* @return array * @return array
*
*/ */
function closeFichinter($id) function closeFichinter($id)
{ {
if(! DolibarrApiAccess::$user->rights->ficheinter->creer) { if(! DolibarrApiAccess::$user->rights->ficheinter->creer)
throw new RestException(401, "Insuffisant rights"); {
} throw new RestException(401, "Insuffisant rights");
}
$result = $this->fichinter->fetch($id); $result = $this->fichinter->fetch($id);
if( ! $result ) { if (! $result) {
throw new RestException(404, 'Fichinter not found'); throw new RestException(404, 'Intervention not found');
} }
if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) { if (! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->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->fichinter->setStatut(3) ) {
throw new RestException(500, 'Error when closed fichinter');
} }
return array( $result = $this->fichinter->setStatut(3);
'success' => array(
'code' => 200, if ($result == 0) {
'message' => 'Fichinter closed' throw new RestException(304, 'Error nothing done. May be object is already closed');
) }
); if ($result < 0) {
throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
}
$this->fichinter->fetchObjectLinked();
return $this->_cleanObjectDatas($this->fichinter);
} }
/** /**
@@ -302,7 +375,7 @@ class Fichinters extends DolibarrApi
function _validate($data) function _validate($data)
{ {
$fichinter = array(); $fichinter = array();
foreach (Fichinters::$FIELDS as $field) { foreach (Interventions::$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");
$fichinter[$field] = $data[$field]; $fichinter[$field] = $data[$field];
@@ -310,6 +383,24 @@ class Fichinters extends DolibarrApi
return $fichinter; return $fichinter;
} }
/**
* Clean sensible object datas
*
* @param object $object Object to clean
* @return array Array of cleaned object properties
*/
function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object);
unset($object->statuts_short);
unset($object->statuts_logo);
unset($object->statuts);
return $object;
}
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
@@ -321,7 +412,7 @@ class Fichinters extends DolibarrApi
function _validateLine($data) function _validateLine($data)
{ {
$fichinter = array(); $fichinter = array();
foreach (Fichinters::$FIELDSLINE as $field) { foreach (Interventions::$FIELDSLINE as $field) {
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$fichinter[$field] = $data[$field]; $fichinter[$field] = $data[$field];