dolibarr  7.0.0-beta
api_expensereports.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
22 
30 {
31 
35  static $FIELDS = array(
36  'socid'
37  );
38 
42  public $expensereport;
43 
44 
48  function __construct()
49  {
50  global $db, $conf;
51  $this->db = $db;
52  $this->expensereport = new ExpenseReport($this->db);
53  }
54 
65  function get($id)
66  {
67  if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
68  throw new RestException(401);
69  }
70 
71  $result = $this->expensereport->fetch($id);
72  if( ! $result ) {
73  throw new RestException(404, 'Expense report not found');
74  }
75 
76  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
77  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78  }
79 
80  $this->expensereport->fetchObjectLinked();
81  return $this->_cleanObjectDatas($this->expensereport);
82  }
83 
97  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '') {
98  global $db, $conf;
99 
100  $obj_ret = array();
101 
102  // case of external user, $societe param is ignored and replaced by user's socid
103  //$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe;
104 
105  $sql = "SELECT t.rowid";
106  $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as t";
107  $sql.= ' WHERE t.entity IN ('.getEntity('expensereport').')';
108  if ($user_ids) $sql.=" AND t.fk_user_author IN (".$user_ids.")";
109 
110  // Add sql filters
111  if ($sqlfilters)
112  {
113  if (! DolibarrApi::_checkFilters($sqlfilters))
114  {
115  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
116  }
117  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
118  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
119  }
120 
121  $sql.= $db->order($sortfield, $sortorder);
122  if ($limit) {
123  if ($page < 0)
124  {
125  $page = 0;
126  }
127  $offset = $limit * $page;
128 
129  $sql.= $db->plimit($limit + 1, $offset);
130  }
131 
132  $result = $db->query($sql);
133 
134  if ($result)
135  {
136  $num = $db->num_rows($result);
137  $min = min($num, ($limit <= 0 ? $num : $limit));
138  while ($i < $min)
139  {
140  $obj = $db->fetch_object($result);
141  $expensereport_static = new ExpenseReport($db);
142  if($expensereport_static->fetch($obj->rowid)) {
143  $obj_ret[] = $this->_cleanObjectDatas($expensereport_static);
144  }
145  $i++;
146  }
147  }
148  else {
149  throw new RestException(503, 'Error when retrieve Expense Report list : '.$db->lasterror());
150  }
151  if( ! count($obj_ret)) {
152  throw new RestException(404, 'No Expense Report found');
153  }
154  return $obj_ret;
155  }
156 
163  function post($request_data = NULL)
164  {
165  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
166  throw new RestException(401, "Insuffisant rights");
167  }
168  // Check mandatory fields
169  $result = $this->_validate($request_data);
170 
171  foreach($request_data as $field => $value) {
172  $this->expensereport->$field = $value;
173  }
174  /*if (isset($request_data["lines"])) {
175  $lines = array();
176  foreach ($request_data["lines"] as $line) {
177  array_push($lines, (object) $line);
178  }
179  $this->expensereport->lines = $lines;
180  }*/
181  if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
182  throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
183  }
184 
185  return $this->expensereport->id;
186  }
187 
197 /*
198  function getLines($id) {
199  if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
200  throw new RestException(401);
201  }
202 
203  $result = $this->expensereport->fetch($id);
204  if( ! $result ) {
205  throw new RestException(404, 'expensereport not found');
206  }
207 
208  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
209  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
210  }
211  $this->expensereport->getLinesArray();
212  $result = array();
213  foreach ($this->expensereport->lines as $line) {
214  array_push($result,$this->_cleanObjectDatas($line));
215  }
216  return $result;
217  }
218 */
219 
230 /*
231  function postLine($id, $request_data = NULL) {
232  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
233  throw new RestException(401);
234  }
235 
236  $result = $this->expensereport->fetch($id);
237  if( ! $result ) {
238  throw new RestException(404, 'expensereport not found');
239  }
240 
241  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
242  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
243  }
244  $request_data = (object) $request_data;
245  $updateRes = $this->expensereport->addline(
246  $request_data->desc,
247  $request_data->subprice,
248  $request_data->qty,
249  $request_data->tva_tx,
250  $request_data->localtax1_tx,
251  $request_data->localtax2_tx,
252  $request_data->fk_product,
253  $request_data->remise_percent,
254  $request_data->info_bits,
255  $request_data->fk_remise_except,
256  'HT',
257  0,
258  $request_data->date_start,
259  $request_data->date_end,
260  $request_data->product_type,
261  $request_data->rang,
262  $request_data->special_code,
263  $fk_parent_line,
264  $request_data->fk_fournprice,
265  $request_data->pa_ht,
266  $request_data->label,
267  $request_data->array_options,
268  $request_data->fk_unit,
269  $this->element,
270  $request_data->id
271  );
272 
273  if ($updateRes > 0) {
274  return $updateRes;
275 
276  }
277  return false;
278  }
279 */
280 
292  /*
293  function putLine($id, $lineid, $request_data = NULL) {
294  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
295  throw new RestException(401);
296  }
297 
298  $result = $this->expensereport->fetch($id);
299  if( ! $result ) {
300  throw new RestException(404, 'expensereport not found');
301  }
302 
303  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
304  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
305  }
306  $request_data = (object) $request_data;
307  $updateRes = $this->expensereport->updateline(
308  $lineid,
309  $request_data->desc,
310  $request_data->subprice,
311  $request_data->qty,
312  $request_data->remise_percent,
313  $request_data->tva_tx,
314  $request_data->localtax1_tx,
315  $request_data->localtax2_tx,
316  'HT',
317  $request_data->info_bits,
318  $request_data->date_start,
319  $request_data->date_end,
320  $request_data->product_type,
321  $request_data->fk_parent_line,
322  0,
323  $request_data->fk_fournprice,
324  $request_data->pa_ht,
325  $request_data->label,
326  $request_data->special_code,
327  $request_data->array_options,
328  $request_data->fk_unit
329  );
330 
331  if ($updateRes > 0) {
332  $result = $this->get($id);
333  unset($result->line);
334  return $this->_cleanObjectDatas($result);
335  }
336  return false;
337  }
338  */
339 
350  /*
351  function deleteLine($id, $lineid) {
352  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
353  throw new RestException(401);
354  }
355 
356  $result = $this->expensereport->fetch($id);
357  if( ! $result ) {
358  throw new RestException(404, 'expensereport not found');
359  }
360 
361  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
362  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
363  }
364 
365  // TODO Check the lineid $lineid is a line of ojbect
366 
367  $updateRes = $this->expensereport->deleteline($lineid);
368  if ($updateRes == 1) {
369  return $this->get($id);
370  }
371  return false;
372  }
373  */
374 
383  function put($id, $request_data = NULL) {
384  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
385  throw new RestException(401);
386  }
387 
388  $result = $this->expensereport->fetch($id);
389  if( ! $result ) {
390  throw new RestException(404, 'expensereport not found');
391  }
392 
393  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
394  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
395  }
396  foreach($request_data as $field => $value) {
397  if ($field == 'id') continue;
398  $this->expensereport->$field = $value;
399  }
400 
401  if ($this->expensereport->update(DolibarrApiAccess::$user) > 0)
402  {
403  return $this->get($id);
404  }
405  else
406  {
407  throw new RestException(500, $this->expensereport->error);
408  }
409  }
410 
418  function delete($id)
419  {
420  if(! DolibarrApiAccess::$user->rights->expensereport->supprimer) {
421  throw new RestException(401);
422  }
423  $result = $this->expensereport->fetch($id);
424  if( ! $result ) {
425  throw new RestException(404, 'Expense Report not found');
426  }
427 
428  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
429  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
430  }
431 
432  if( ! $this->expensereport->delete(DolibarrApiAccess::$user)) {
433  throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
434  }
435 
436  return array(
437  'success' => array(
438  'code' => 200,
439  'message' => 'Expense Report deleted'
440  )
441  );
442 
443  }
444 
460  /*
461  function validate($id, $idwarehouse=0)
462  {
463  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
464  throw new RestException(401);
465  }
466  $result = $this->expensereport->fetch($id);
467  if( ! $result ) {
468  throw new RestException(404, 'expensereport not found');
469  }
470 
471  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
472  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
473  }
474 
475  if( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) {
476  throw new RestException(500, 'Error when validate expensereport');
477  }
478 
479  return array(
480  'success' => array(
481  'code' => 200,
482  'message' => 'expensereport validated'
483  )
484  );
485  }*/
486 
493  function _cleanObjectDatas($object) {
494 
495  $object = parent::_cleanObjectDatas($object);
496 
497  unset($object->barcode_type);
498  unset($object->barcode_type_code);
499  unset($object->barcode_type_label);
500  unset($object->barcode_type_coder);
501 
502  return $object;
503  }
504 
512  function _validate($data)
513  {
514  $expensereport = array();
515  foreach (ExpenseReports::$FIELDS as $field) {
516  if (!isset($data[$field]))
517  throw new RestException(400, "$field field missing");
518  $expensereport[$field] = $data[$field];
519 
520  }
521  return $expensereport;
522  }
523 }
put($id, $request_data=NULL)
Get lines of an Expense Report.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
Class to manage Trips and Expenses.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
post($request_data=NULL)
Create Expense Report object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check user access to a resource.
Definition: api.class.php:221
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters= '')
List Expense Reports.
_cleanObjectDatas($object)
Validate an Expense Report.