dolibarr  9.0.0
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 
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  {
99  global $db, $conf;
100 
101  $obj_ret = array();
102 
103  // case of external user, $societe param is ignored and replaced by user's socid
104  //$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe;
105 
106  $sql = "SELECT t.rowid";
107  $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as t";
108  $sql.= ' WHERE t.entity IN ('.getEntity('expensereport').')';
109  if ($user_ids) $sql.=" AND t.fk_user_author IN (".$user_ids.")";
110 
111  // Add sql filters
112  if ($sqlfilters)
113  {
114  if (! DolibarrApi::_checkFilters($sqlfilters))
115  {
116  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
117  }
118  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
119  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
120  }
121 
122  $sql.= $db->order($sortfield, $sortorder);
123  if ($limit) {
124  if ($page < 0)
125  {
126  $page = 0;
127  }
128  $offset = $limit * $page;
129 
130  $sql.= $db->plimit($limit + 1, $offset);
131  }
132 
133  $result = $db->query($sql);
134 
135  if ($result)
136  {
137  $num = $db->num_rows($result);
138  $min = min($num, ($limit <= 0 ? $num : $limit));
139  while ($i < $min)
140  {
141  $obj = $db->fetch_object($result);
142  $expensereport_static = new ExpenseReport($db);
143  if($expensereport_static->fetch($obj->rowid)) {
144  $obj_ret[] = $this->_cleanObjectDatas($expensereport_static);
145  }
146  $i++;
147  }
148  }
149  else {
150  throw new RestException(503, 'Error when retrieve Expense Report list : '.$db->lasterror());
151  }
152  if( ! count($obj_ret)) {
153  throw new RestException(404, 'No Expense Report found');
154  }
155  return $obj_ret;
156  }
157 
164  function post($request_data = null)
165  {
166  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
167  throw new RestException(401, "Insuffisant rights");
168  }
169  // Check mandatory fields
170  $result = $this->_validate($request_data);
171 
172  foreach($request_data as $field => $value) {
173  $this->expensereport->$field = $value;
174  }
175  /*if (isset($request_data["lines"])) {
176  $lines = array();
177  foreach ($request_data["lines"] as $line) {
178  array_push($lines, (object) $line);
179  }
180  $this->expensereport->lines = $lines;
181  }*/
182  if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
183  throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
184  }
185 
186  return $this->expensereport->id;
187  }
188 
198 /*
199  function getLines($id)
200  {
201  if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
202  throw new RestException(401);
203  }
204 
205  $result = $this->expensereport->fetch($id);
206  if( ! $result ) {
207  throw new RestException(404, 'expensereport not found');
208  }
209 
210  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
211  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
212  }
213  $this->expensereport->getLinesArray();
214  $result = array();
215  foreach ($this->expensereport->lines as $line) {
216  array_push($result,$this->_cleanObjectDatas($line));
217  }
218  return $result;
219  }
220 */
221 
232 /*
233  function postLine($id, $request_data = null)
234  {
235  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
236  throw new RestException(401);
237  }
238 
239  $result = $this->expensereport->fetch($id);
240  if( ! $result ) {
241  throw new RestException(404, 'expensereport not found');
242  }
243 
244  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
245  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
246  }
247  $request_data = (object) $request_data;
248  $updateRes = $this->expensereport->addline(
249  $request_data->desc,
250  $request_data->subprice,
251  $request_data->qty,
252  $request_data->tva_tx,
253  $request_data->localtax1_tx,
254  $request_data->localtax2_tx,
255  $request_data->fk_product,
256  $request_data->remise_percent,
257  $request_data->info_bits,
258  $request_data->fk_remise_except,
259  'HT',
260  0,
261  $request_data->date_start,
262  $request_data->date_end,
263  $request_data->product_type,
264  $request_data->rang,
265  $request_data->special_code,
266  $fk_parent_line,
267  $request_data->fk_fournprice,
268  $request_data->pa_ht,
269  $request_data->label,
270  $request_data->array_options,
271  $request_data->fk_unit,
272  $this->element,
273  $request_data->id
274  );
275 
276  if ($updateRes > 0) {
277  return $updateRes;
278 
279  }
280  return false;
281  }
282 */
283 
295  /*
296  function putLine($id, $lineid, $request_data = null)
297  {
298  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
299  throw new RestException(401);
300  }
301 
302  $result = $this->expensereport->fetch($id);
303  if( ! $result ) {
304  throw new RestException(404, 'expensereport not found');
305  }
306 
307  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
308  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
309  }
310  $request_data = (object) $request_data;
311  $updateRes = $this->expensereport->updateline(
312  $lineid,
313  $request_data->desc,
314  $request_data->subprice,
315  $request_data->qty,
316  $request_data->remise_percent,
317  $request_data->tva_tx,
318  $request_data->localtax1_tx,
319  $request_data->localtax2_tx,
320  'HT',
321  $request_data->info_bits,
322  $request_data->date_start,
323  $request_data->date_end,
324  $request_data->product_type,
325  $request_data->fk_parent_line,
326  0,
327  $request_data->fk_fournprice,
328  $request_data->pa_ht,
329  $request_data->label,
330  $request_data->special_code,
331  $request_data->array_options,
332  $request_data->fk_unit
333  );
334 
335  if ($updateRes > 0) {
336  $result = $this->get($id);
337  unset($result->line);
338  return $this->_cleanObjectDatas($result);
339  }
340  return false;
341  }
342  */
343 
354  /*
355  function deleteLine($id, $lineid)
356  {
357  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
358  throw new RestException(401);
359  }
360 
361  $result = $this->expensereport->fetch($id);
362  if( ! $result ) {
363  throw new RestException(404, 'expensereport not found');
364  }
365 
366  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
367  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
368  }
369 
370  // TODO Check the lineid $lineid is a line of ojbect
371 
372  $updateRes = $this->expensereport->deleteline($lineid);
373  if ($updateRes == 1) {
374  return $this->get($id);
375  }
376  return false;
377  }
378  */
379 
388  function put($id, $request_data = null)
389  {
390  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
391  throw new RestException(401);
392  }
393 
394  $result = $this->expensereport->fetch($id);
395  if( ! $result ) {
396  throw new RestException(404, 'expensereport not found');
397  }
398 
399  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
400  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
401  }
402  foreach($request_data as $field => $value) {
403  if ($field == 'id') continue;
404  $this->expensereport->$field = $value;
405  }
406 
407  if ($this->expensereport->update(DolibarrApiAccess::$user) > 0)
408  {
409  return $this->get($id);
410  }
411  else
412  {
413  throw new RestException(500, $this->expensereport->error);
414  }
415  }
416 
424  function delete($id)
425  {
426  if(! DolibarrApiAccess::$user->rights->expensereport->supprimer) {
427  throw new RestException(401);
428  }
429  $result = $this->expensereport->fetch($id);
430  if( ! $result ) {
431  throw new RestException(404, 'Expense Report not found');
432  }
433 
434  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
435  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
436  }
437 
438  if( ! $this->expensereport->delete(DolibarrApiAccess::$user)) {
439  throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
440  }
441 
442  return array(
443  'success' => array(
444  'code' => 200,
445  'message' => 'Expense Report deleted'
446  )
447  );
448  }
449 
465  /*
466  function validate($id, $idwarehouse=0)
467  {
468  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
469  throw new RestException(401);
470  }
471  $result = $this->expensereport->fetch($id);
472  if( ! $result ) {
473  throw new RestException(404, 'expensereport not found');
474  }
475 
476  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
477  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
478  }
479 
480  if( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) {
481  throw new RestException(500, 'Error when validate expensereport');
482  }
483 
484  return array(
485  'success' => array(
486  'code' => 200,
487  'message' => 'expensereport validated'
488  )
489  );
490  }*/
491 
498  function _cleanObjectDatas($object)
499  {
500 
501  $object = parent::_cleanObjectDatas($object);
502 
503  unset($object->barcode_type);
504  unset($object->barcode_type_code);
505  unset($object->barcode_type_label);
506  unset($object->barcode_type_coder);
507 
508  return $object;
509  }
510 
518  function _validate($data)
519  {
520  $expensereport = array();
521  foreach (ExpenseReports::$FIELDS as $field) {
522  if (!isset($data[$field]))
523  throw new RestException(400, "$field field missing");
524  $expensereport[$field] = $data[$field];
525  }
526  return $expensereport;
527  }
528 }
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
post($request_data=null)
Create Expense Report object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters='')
List Expense Reports.
Class to manage Trips and Expenses.
__construct()
Constructor.
_validate($data)
Validate fields before create or update 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:225
put($id, $request_data=null)
Get lines of an Expense Report.
_cleanObjectDatas($object)
Validate an Expense Report.