dolibarr  13.0.2
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  * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20  use Luracast\Restler\RestException;
21 
22  require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
23 
31 {
32 
36  static $FIELDS = array(
37  'fk_user_author'
38  );
39 
43  public $expensereport;
44 
45 
49  public function __construct()
50  {
51  global $db, $conf;
52  $this->db = $db;
53  $this->expensereport = new ExpenseReport($this->db);
54  }
55 
66  public function get($id)
67  {
68  if (!DolibarrApiAccess::$user->rights->expensereport->lire) {
69  throw new RestException(401);
70  }
71 
72  $result = $this->expensereport->fetch($id);
73  if (!$result) {
74  throw new RestException(404, 'Expense report not found');
75  }
76 
77  if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport->id)) {
78  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79  }
80 
81  $this->expensereport->fetchObjectLinked();
82  return $this->_cleanObjectDatas($this->expensereport);
83  }
84 
98  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '')
99  {
100  global $db, $conf;
101 
102  $obj_ret = array();
103 
104  // case of external user, $societe param is ignored and replaced by user's socid
105  //$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $societe;
106 
107  $sql = "SELECT t.rowid";
108  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as t";
109  $sql .= ' WHERE t.entity IN ('.getEntity('expensereport').')';
110  if ($user_ids) $sql .= " AND t.fk_user_author IN (".$user_ids.")";
111 
112  // Add sql filters
113  if ($sqlfilters)
114  {
115  if (!DolibarrApi::_checkFilters($sqlfilters)) {
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 .= $this->db->order($sortfield, $sortorder);
123  if ($limit) {
124  if ($page < 0) {
125  $page = 0;
126  }
127  $offset = $limit * $page;
128 
129  $sql .= $this->db->plimit($limit + 1, $offset);
130  }
131 
132  $result = $this->db->query($sql);
133 
134  if ($result)
135  {
136  $num = $this->db->num_rows($result);
137  $min = min($num, ($limit <= 0 ? $num : $limit));
138  $i = 0;
139  while ($i < $min)
140  {
141  $obj = $this->db->fetch_object($result);
142  $expensereport_static = new ExpenseReport($this->db);
143  if ($expensereport_static->fetch($obj->rowid)) {
144  $obj_ret[] = $this->_cleanObjectDatas($expensereport_static);
145  }
146  $i++;
147  }
148  } else {
149  throw new RestException(503, 'Error when retrieve Expense Report list : '.$this->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  public 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  public function getLines($id)
199  {
200  if(! DolibarrApiAccess::$user->rights->expensereport->lire) {
201  throw new RestException(401);
202  }
203 
204  $result = $this->expensereport->fetch($id);
205  if( ! $result ) {
206  throw new RestException(404, 'expensereport not found');
207  }
208 
209  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
210  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
211  }
212  $this->expensereport->getLinesArray();
213  $result = array();
214  foreach ($this->expensereport->lines as $line) {
215  array_push($result,$this->_cleanObjectDatas($line));
216  }
217  return $result;
218  }
219  */
220 
231  /*
232  public function postLine($id, $request_data = null)
233  {
234  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
235  throw new RestException(401);
236  }
237 
238  $result = $this->expensereport->fetch($id);
239  if( ! $result ) {
240  throw new RestException(404, 'expensereport not found');
241  }
242 
243  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
244  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
245  }
246  $request_data = (object) $request_data;
247  $updateRes = $this->expensereport->addline(
248  $request_data->desc,
249  $request_data->subprice,
250  $request_data->qty,
251  $request_data->tva_tx,
252  $request_data->localtax1_tx,
253  $request_data->localtax2_tx,
254  $request_data->fk_product,
255  $request_data->remise_percent,
256  $request_data->info_bits,
257  $request_data->fk_remise_except,
258  'HT',
259  0,
260  $request_data->date_start,
261  $request_data->date_end,
262  $request_data->product_type,
263  $request_data->rang,
264  $request_data->special_code,
265  $fk_parent_line,
266  $request_data->fk_fournprice,
267  $request_data->pa_ht,
268  $request_data->label,
269  $request_data->array_options,
270  $request_data->fk_unit,
271  $this->element,
272  $request_data->id
273  );
274 
275  if ($updateRes > 0) {
276  return $updateRes;
277 
278  }
279  return false;
280  }
281  */
282 
294  /*
295  public function putLine($id, $lineid, $request_data = null)
296  {
297  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
298  throw new RestException(401);
299  }
300 
301  $result = $this->expensereport->fetch($id);
302  if( ! $result ) {
303  throw new RestException(404, 'expensereport not found');
304  }
305 
306  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
307  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
308  }
309  $request_data = (object) $request_data;
310  $updateRes = $this->expensereport->updateline(
311  $lineid,
312  $request_data->desc,
313  $request_data->subprice,
314  $request_data->qty,
315  $request_data->remise_percent,
316  $request_data->tva_tx,
317  $request_data->localtax1_tx,
318  $request_data->localtax2_tx,
319  'HT',
320  $request_data->info_bits,
321  $request_data->date_start,
322  $request_data->date_end,
323  $request_data->product_type,
324  $request_data->fk_parent_line,
325  0,
326  $request_data->fk_fournprice,
327  $request_data->pa_ht,
328  $request_data->label,
329  $request_data->special_code,
330  $request_data->array_options,
331  $request_data->fk_unit
332  );
333 
334  if ($updateRes > 0) {
335  $result = $this->get($id);
336  unset($result->line);
337  return $this->_cleanObjectDatas($result);
338  }
339  return false;
340  }
341  */
342 
353  /*
354  public function deleteLine($id, $lineid)
355  {
356  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
357  throw new RestException(401);
358  }
359 
360  $result = $this->expensereport->fetch($id);
361  if( ! $result ) {
362  throw new RestException(404, 'expensereport not found');
363  }
364 
365  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
366  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
367  }
368 
369  // TODO Check the lineid $lineid is a line of ojbect
370 
371  $updateRes = $this->expensereport->deleteline($lineid);
372  if ($updateRes == 1) {
373  return $this->get($id);
374  }
375  return false;
376  }
377  */
378 
391  public function put($id, $request_data = null)
392  {
393  if (!DolibarrApiAccess::$user->rights->expensereport->creer) {
394  throw new RestException(401);
395  }
396 
397  $result = $this->expensereport->fetch($id);
398  if (!$result) {
399  throw new RestException(404, 'expensereport not found');
400  }
401 
402  if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport->id)) {
403  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
404  }
405  foreach ($request_data as $field => $value) {
406  if ($field == 'id') continue;
407  $this->expensereport->$field = $value;
408  }
409 
410  if ($this->expensereport->update(DolibarrApiAccess::$user) > 0)
411  {
412  return $this->get($id);
413  } else {
414  throw new RestException(500, $this->expensereport->error);
415  }
416  }
417 
425  public function delete($id)
426  {
427  if (!DolibarrApiAccess::$user->rights->expensereport->supprimer) {
428  throw new RestException(401);
429  }
430  $result = $this->expensereport->fetch($id);
431  if (!$result) {
432  throw new RestException(404, 'Expense Report not found');
433  }
434 
435  if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport->id)) {
436  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
437  }
438 
439  if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
440  throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
441  }
442 
443  return array(
444  'success' => array(
445  'code' => 200,
446  'message' => 'Expense Report deleted'
447  )
448  );
449  }
450 
466  /*
467  public function validate($id, $idwarehouse=0)
468  {
469  if(! DolibarrApiAccess::$user->rights->expensereport->creer) {
470  throw new RestException(401);
471  }
472  $result = $this->expensereport->fetch($id);
473  if( ! $result ) {
474  throw new RestException(404, 'expensereport not found');
475  }
476 
477  if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
478  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
479  }
480 
481  if( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) {
482  throw new RestException(500, 'Error when validate expensereport');
483  }
484 
485  return array(
486  'success' => array(
487  'code' => 200,
488  'message' => 'expensereport validated'
489  )
490  );
491  }*/
492 
493  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
500  protected function _cleanObjectDatas($object)
501  {
502  // phpcs:enable
503  $object = parent::_cleanObjectDatas($object);
504 
505  unset($object->fk_statut);
506  unset($object->statut);
507  unset($object->user);
508  unset($object->thirdparty);
509 
510  unset($object->cond_reglement);
511  unset($object->shipping_method_id);
512 
513  unset($object->barcode_type);
514  unset($object->barcode_type_code);
515  unset($object->barcode_type_label);
516  unset($object->barcode_type_coder);
517 
518  unset($object->code_paiement);
519  unset($object->code_statut);
520  unset($object->fk_c_paiement);
521  unset($object->fk_incoterms);
522  unset($object->label_incoterms);
523  unset($object->location_incoterms);
524  unset($object->mode_reglement_id);
525  unset($object->cond_reglement_id);
526 
527  unset($object->name);
528  unset($object->lastname);
529  unset($object->firstname);
530  unset($object->civility_id);
531  unset($object->cond_reglement_id);
532  unset($object->contact);
533  unset($object->contact_id);
534 
535  unset($object->state);
536  unset($object->state_id);
537  unset($object->state_code);
538  unset($object->country);
539  unset($object->country_id);
540  unset($object->country_code);
541 
542  unset($object->note); // We already use note_public and note_pricate
543 
544  return $object;
545  }
546 
554  private function _validate($data)
555  {
556  $expensereport = array();
557  foreach (ExpenseReports::$FIELDS as $field) {
558  if (!isset($data[$field]))
559  throw new RestException(400, "$field field missing");
560  $expensereport[$field] = $data[$field];
561  }
562  return $expensereport;
563  }
564 }
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
post($request_data=null)
Create Expense Report object.
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:252
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters= '')
List Expense Reports.
put($id, $request_data=null)
Get lines of an Expense Report.
_cleanObjectDatas($object)
Validate an Expense Report.