NEW Add API for contracts

This commit is contained in:
Laurent Destailleur 2017-10-16 17:56:59 +02:00
parent f7fe011e98
commit 45ac8367bd
12 changed files with 603 additions and 58 deletions

View File

@ -241,6 +241,7 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/resources.json'
else
{
$classfile = str_replace('_', '', $module);
if ($module == 'contracts') $moduledirforclass = 'contrat';
if ($module == 'supplierinvoices') $classfile = 'supplier_invoices';
if ($module == 'supplierorders') $classfile = 'supplier_orders';
$dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_'.$classfile.'.class.php');

View File

@ -287,7 +287,7 @@ class Proposals extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;

View File

@ -316,7 +316,7 @@ class CommandeApi extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;

View File

@ -286,7 +286,7 @@ class Orders extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;

View File

@ -1240,7 +1240,7 @@ class Commande extends CommonOrder
{
global $mysoc, $conf, $langs, $user;
dol_syslog(get_class($this)."::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit", LOG_DEBUG);
dol_syslog(get_class($this)."::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';

View File

@ -421,7 +421,7 @@ class Invoices extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
throw new RestException(400, 'Unable to insert the new line. Check your inputs.');

View File

@ -0,0 +1,532 @@
<?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
/**
* API class for contracts
*
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class Contracts extends DolibarrApi
{
/**
* @var array $FIELDS Mandatory fields, checked when create and update object
*/
static $FIELDS = array(
'socid',
'date_contrat',
'commercial_signature_id',
'commercial_suivi_id'
);
/**
* @var Contract $contract {@type Contrat}
*/
public $contract;
/**
* Constructor
*/
function __construct()
{
global $db, $conf;
$this->db = $db;
$this->contract = new Contrat($this->db);
}
/**
* Get properties of a contrat object
*
* Return an array with contrat informations
*
* @param int $id ID of contract
* @return array|mixed data without useless information
*
* @throws RestException
*/
function get($id)
{
if(! DolibarrApiAccess::$user->rights->contrat->lire) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contract not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->contract->fetchObjectLinked();
return $this->_cleanObjectDatas($this->contract);
}
/**
* List contracts
*
* Get a list of contracts
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @param string $thirdparty_ids Thirdparty ids to filter contracts of. {@example '1' or '1,2,3'} {@pattern /^[0-9,]*$/i}
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
* @return array Array of contract objects
*
* @throws RestException
*/
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') {
global $db, $conf;
$obj_ret = array();
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
$socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
// If the internal user must only see his customers, force searching by him
$search_sale = 0;
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
$sql = "SELECT t.rowid";
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
$sql.= " FROM ".MAIN_DB_PREFIX."contrat as t";
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
$sql.= ' WHERE t.entity IN ('.getEntity('contrat').')';
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
// Insert sale filter
if ($search_sale > 0)
{
$sql .= " AND sc.fk_user = ".$search_sale;
}
// Add sql filters
if ($sqlfilters)
{
if (! DolibarrApi::_checkFilters($sqlfilters))
{
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
}
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
}
$sql.= $db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
{
$page = 0;
}
$offset = $limit * $page;
$sql.= $db->plimit($limit + 1, $offset);
}
dol_syslog("API Rest request");
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
while ($i < $min)
{
$obj = $db->fetch_object($result);
$contrat_static = new Contrat($db);
if($contrat_static->fetch($obj->rowid)) {
$obj_ret[] = $this->_cleanObjectDatas($contrat_static);
}
$i++;
}
}
else {
throw new RestException(503, 'Error when retrieve contrat list : '.$db->lasterror());
}
if( ! count($obj_ret)) {
throw new RestException(404, 'No contract found');
}
return $obj_ret;
}
/**
* Create contract object
*
* @param array $request_data Request data
* @return int ID of contrat
*/
function post($request_data = NULL)
{
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401, "Insuffisant rights");
}
// Check mandatory fields
$result = $this->_validate($request_data);
foreach($request_data as $field => $value) {
$this->contract->$field = $value;
}
/*if (isset($request_data["lines"])) {
$lines = array();
foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line);
}
$this->contract->lines = $lines;
}*/
if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
}
return $this->contract->id;
}
/**
* Get lines of an contract
*
* @param int $id Id of contract
*
* @url GET {id}/lines
*
* @return int
*/
function getLines($id) {
if(! DolibarrApiAccess::$user->rights->contrat->lire) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contract not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->contract->getLinesArray();
$result = array();
foreach ($this->contract->lines as $line) {
array_push($result,$this->_cleanObjectDatas($line));
}
return $result;
}
/**
* Add a line to given contract
*
* @param int $id Id of contrat to update
* @param array $request_data Contractline data
*
* @url POST {id}/lines
*
* @return int
*/
function postLine($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contract not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->contract->addline(
$request_data->desc,
$request_data->subprice,
$request_data->qty,
$request_data->tva_tx,
$request_data->localtax1_tx,
$request_data->localtax2_tx,
$request_data->fk_product,
$request_data->remise_percent,
$request_data->date_start, // date ouverture = date_start_real
$request_data->date_end, // date_cloture = date_end_real
$request_data->HT,
$request_data->subprice_excl_tax,
$request_data->info_bits,
$request_data->fk_fournprice,
$request_data->pa_ht,
$request_data->array_options,
$request_data->fk_unit,
$request_data->rang
);
if ($updateRes > 0) {
return $updateRes;
}
return false;
}
/**
* Update a line to given contract
*
* @param int $id Id of contrat to update
* @param int $lineid Id of line to update
* @param array $request_data Contractline data
*
* @url PUT {id}/lines/{lineid}
*
* @return object
*/
function putLine($id, $lineid, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contrat not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->contract->updateline(
$lineid,
$request_data->desc,
$request_data->subprice,
$request_data->qty,
$request_data->remise_percent,
$request_data->date_ouveture_prevue,
$request_data->date_fin_validite,
$request_data->tva_tx,
$request_data->localtax1_tx,
$request_data->localtax2_tx,
$request_data->date_ouverture,
$request_data->date_cloture,
'HT',
$request_data->info_bits,
$request_data->fk_fourn_price,
$request_data->pa_ht,
$request_data->array_options,
$request_data->fk_unit
);
if ($updateRes > 0) {
$result = $this->get($id);
unset($result->line);
return $this->_cleanObjectDatas($result);
}
return false;
}
/**
* Delete a line to given contract
*
*
* @param int $id Id of contract to update
* @param int $lineid Id of line to delete
*
* @url DELETE {id}/lines/{lineid}
*
* @return int
* @throws 401
* @throws 404
*/
function deleteLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contrat not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
if ($updateRes > 0) {
return $this->get($id);
}
return false;
}
/**
* Update contract general fields (won't touch lines of contract)
*
* @param int $id Id of contrat to update
* @param array $request_data Datas
*
* @return int
*/
function put($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contrat not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
foreach($request_data as $field => $value) {
if ($field == 'id') continue;
$this->contract->$field = $value;
}
if($this->contract->update(DolibarrApiAccess::$user, 0))
return $this->get($id);
return false;
}
/**
* Delete contract
*
* @param int $id Contract ID
*
* @return array
*/
function delete($id)
{
if(! DolibarrApiAccess::$user->rights->contrat->supprimer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contract not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
if( ! $this->contract->delete(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
}
return array(
'success' => array(
'code' => 200,
'message' => 'Contract deleted'
)
);
}
/**
* Validate an contract
*
* @param int $id Contract ID
* @param int $idwarehouse Warehouse ID
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
*
* @url POST {id}/validate
*
* @return array
* FIXME An error 403 is returned if the request has an empty body.
* Error message: "Forbidden: Content type `text/plain` is not supported."
* Workaround: send this in the body
* {
* "idwarehouse": 0,
* "notrigger": 0
* }
*/
/*
function validate($id, $idwarehouse=0, $notrigger=0)
{
if(! DolibarrApiAccess::$user->rights->contrat->creer) {
throw new RestException(401);
}
$result = $this->contract->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Contract not found');
}
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->contract->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
if ($result == 0) {
throw new RestException(500, 'Error nothing done. May be object is already validated');
}
if ($result < 0) {
throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
}
return array(
'success' => array(
'code' => 200,
'message' => 'Contract validated (Ref='.$this->contract->ref.')'
)
);
}
*/
/**
* 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->address);
return $object;
}
/**
* Validate fields before create or update object
*
* @param array $data Array with data to verify
* @return array
* @throws RestException
*/
function _validate($data)
{
$contrat = array();
foreach (Contracts::$FIELDS as $field) {
if (!isset($data[$field]))
throw new RestException(400, "$field field missing");
$contrat[$field] = $data[$field];
}
return $contrat;
}
}

View File

@ -1400,13 +1400,14 @@ class Contrat extends CommonObject
* @param int $pa_ht Buying price HT
* @param array $array_options extrafields array
* @param string $fk_unit Code of the unit to use. Null to use the default one
* @return int <0 si erreur, >0 si ok
* @param string $rang Position
* @return int <0 if KO, >0 if OK
*/
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $fk_fournprice=null, $pa_ht = 0,$array_options=0, $fk_unit = null)
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $fk_fournprice=null, $pa_ht = 0,$array_options=0, $fk_unit = null, $rang=0)
{
global $user, $langs, $conf, $mysoc;
dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type, $pu_ttc, $info_bits");
dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type, $pu_ttc, $info_bits, $rang");
if ($this->statut >= 0)
{
@ -1616,7 +1617,7 @@ class Contrat extends CommonObject
{
global $user, $conf, $langs, $mysoc;
// Nettoyage parametres
// Clean parameters
$qty=trim($qty);
$desc=trim($desc);
$desc=trim($desc);
@ -1625,6 +1626,7 @@ class Contrat extends CommonObject
$localtax1tx = price2num($localtax1tx);
$localtax2tx = price2num($localtax2tx);
$pa_ht=price2num($pa_ht);
if (empty($fk_fournprice)) $fk_fournprice=0;
$subprice = $price;
$remise = 0;
@ -1701,7 +1703,7 @@ class Contrat extends CommonObject
$sql.= ", total_localtax1='".price2num($total_localtax1)."'";
$sql.= ", total_localtax2='".price2num($total_localtax2)."'";
$sql.= ", total_ttc='". price2num($total_ttc)."'";
$sql.= ", fk_product_fournisseur_price='".$fk_fournprice."'";
$sql.= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null");
$sql.= ", buy_price_ht='".price2num($pa_ht)."'";
if ($date_start > 0) { $sql.= ",date_ouverture_prevue='".$this->db->idate($date_start)."'"; }
else { $sql.=",date_ouverture_prevue=null"; }
@ -1772,7 +1774,7 @@ class Contrat extends CommonObject
* @param User $user User that delete
* @return int >0 if OK, <0 if KO
*/
function deleteline($idline,$user)
function deleteline($idline, User $user)
{
global $conf, $langs;
@ -2343,7 +2345,17 @@ class Contrat extends CommonObject
}
}
/**
/**
* Create an array of order lines
*
* @return int >0 if OK, <0 if KO
*/
function getLinesArray()
{
return $this->fetch_lines();
}
/**
* Create a document onto disk according to template module.
*
* @param string $modele Force model to use ('' to not force)
@ -3033,7 +3045,7 @@ class ContratLigne extends CommonObjectLine
$sql.= " info_bits,";
$sql.= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
if ($this->date_ouverture_prevue > 0) { $sql.= ",date_ouverture_prevue"; }
if ($this->date_fin_validite > 0) { $sql.= ",date_fin_validite"; }
if ($this->date_fin_validite > 0) { $sql.= ",date_fin_validite"; }
$sql.= ") VALUES ($this->fk_contrat, '', '" . $this->db->escape($this->description) . "',";
$sql.= ($this->fk_product>0 ? $this->fk_product : "null").",";
$sql.= " '".$this->db->escape($this->qty)."',";
@ -3051,8 +3063,8 @@ class ContratLigne extends CommonObjectLine
else $sql.= ' null,';
if ($this->pa_ht > 0) $sql.= ' '.price2num($this->pa_ht);
else $sql.= ' null';
if ($this->date_ouverture_prevue > 0) { $sql.= ",'".$this->db->idate($this->date_ouverture_prevue)."'"; }
if ($this->date_fin_validite > 0) { $sql.= ",'".$this->db->idate($this->date_fin_validite)."'"; }
if ($this->date_ouverture > 0) { $sql.= ",'".$this->db->idate($this->date_ouverture)."'"; }
if ($this->date_cloture > 0) { $sql.= ",'".$this->db->idate($this->date_cloture)."'"; }
$sql.= ")";
dol_syslog(get_class($this)."::insert", LOG_DEBUG);

View File

@ -271,7 +271,7 @@ class ExpenseReports extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;

View File

@ -231,7 +231,7 @@ class MyObjectApi extends DolibarrApi
}
if($this->myobject->update($id, DolibarrApiAccess::$user))
return $this->get ($id);
return $this->get($id);
return false;
}

View File

@ -342,7 +342,7 @@ class Projects extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;

View File

@ -20,7 +20,7 @@
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
/**
* API class for projects
*
@ -88,12 +88,12 @@ class Tasks extends DolibarrApi
// TODO
// Add class for timespent records and loop and fill $line->lines with records of timespent
}
return $this->_cleanObjectDatas($this->task);
}
/**
* List tasks
*
@ -110,7 +110,7 @@ class Tasks extends DolibarrApi
global $db, $conf;
$obj_ret = array();
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
$socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
@ -134,7 +134,7 @@ class Tasks extends DolibarrApi
$sql .= " AND sc.fk_user = ".$search_sale;
}
// Add sql filters
if ($sqlfilters)
if ($sqlfilters)
{
if (! DolibarrApi::_checkFilters($sqlfilters))
{
@ -143,7 +143,7 @@ class Tasks extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
}
$sql.= $db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
@ -236,7 +236,7 @@ class Tasks extends DolibarrApi
}
$this->project->getLinesArray(DolibarrApiAccess::$user);
$result = array();
foreach ($this->project->lines as $line) // $line is a task
foreach ($this->project->lines as $line) // $line is a task
{
if ($includetimespent == 1)
{
@ -252,7 +252,7 @@ class Tasks extends DolibarrApi
return $result;
}
*/
/**
* Get roles a user is assigned to a task with
*
@ -265,20 +265,20 @@ class Tasks extends DolibarrApi
*/
function getRoles($id, $userid=0) {
global $db;
if(! DolibarrApiAccess::$user->rights->projet->lire) {
throw new RestException(401);
}
$result = $this->task->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Task not found');
}
if( ! DolibarrApi::_checkAccessToResource('task',$this->task->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$usert = DolibarrApiAccess::$user;
if ($userid > 0)
{
@ -292,8 +292,8 @@ class Tasks extends DolibarrApi
}
return $result;
}
/**
* Add a task to given project
*
@ -348,13 +348,13 @@ class Tasks extends DolibarrApi
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
return $updateRes;
}
return false;
}
*/
/**
* Update a task to given project
*
@ -412,8 +412,8 @@ class Tasks extends DolibarrApi
}
return false;
}*/
/**
* Update task general fields (won't touch time spent of task)
*
@ -484,7 +484,7 @@ class Tasks extends DolibarrApi
}
/**
* Add time spent to a task of a project.
* You can test this API with the following input message
@ -495,15 +495,15 @@ class Tasks extends DolibarrApi
* @param int $duration Duration in seconds (3600 = 1h)
* @param int $user_id User (Use 0 for connected user)
* @param string $note Note
*
*
* @url POST {id}/addtimespent
*
* @return array
*/
function addTimeSpent($id, $date, $duration, $user_id=0, $note='')
{
if( ! DolibarrApiAccess::$user->rights->projet->creer) {
throw new RestException(401);
}
@ -511,22 +511,22 @@ class Tasks extends DolibarrApi
if ($result <= 0) {
throw new RestException(404, 'Task not found');
}
if( ! DolibarrApi::_checkAccessToResource('project', $this->task->fk_project)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$uid = $user_id;
if (empty($uid)) $uid = DolibarrApiAccess::$user->id;
$newdate = dol_stringtotime($date, 1);
$newdate = dol_stringtotime($date, 1);
$this->task->timespent_date = $newdate;
$this->task->timespent_datehour = $newdate;
$this->task->timespent_withhour = 1;
$this->task->timespent_duration = $duration;
$this->task->timespent_fk_user = $user_id;
$this->task->timespent_note = $note;
$this->task->timespent_note = $note;
$result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
if ($result == 0) {
throw new RestException(500, 'Error nothing done. May be object is already validated');
@ -534,7 +534,7 @@ class Tasks extends DolibarrApi
if ($result < 0) {
throw new RestException(500, 'Error when adding time: '.$this->task->error);
}
return array(
'success' => array(
'code' => 200,
@ -542,8 +542,8 @@ class Tasks extends DolibarrApi
)
);
}
/**
* Clean sensible object datas
*
@ -551,9 +551,9 @@ class Tasks extends DolibarrApi
* @return array Array of cleaned object properties
*/
function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object);
unset($object->barcode_type);
unset($object->barcode_type_code);
unset($object->barcode_type_label);
@ -575,21 +575,21 @@ class Tasks extends DolibarrApi
unset($object->country);
unset($object->country_id);
unset($object->country_code);
unset($object->weekWorkLoad);
unset($object->weekWorkLoad);
//unset($object->lines); // for task we use timespent_lines, but for project we use lines
unset($object->total_ht);
unset($object->total_tva);
unset($object->total_localtax1);
unset($object->total_localtax2);
unset($object->total_ttc);
return $object;
}
/**
* Validate fields before create or update object
*
@ -608,8 +608,8 @@ class Tasks extends DolibarrApi
}
return $object;
}
// TODO
// getSummaryOfTimeSpent
}