dolibarr  17.0.3
api_tasks.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 <https://www.gnu.org/licenses/>.
17  */
18 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
22  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
23 
30 class Tasks extends DolibarrApi
31 {
32 
36  public static $FIELDS = array(
37  'ref',
38  'label',
39  'fk_project'
40  );
41 
45  public $task;
46 
50  public function __construct()
51  {
52  global $db, $conf;
53  $this->db = $db;
54  $this->task = new Task($this->db);
55  }
56 
68  public function get($id, $includetimespent = 0)
69  {
70  if (!DolibarrApiAccess::$user->rights->projet->lire) {
71  throw new RestException(401);
72  }
73 
74  $result = $this->task->fetch($id);
75  if (!$result) {
76  throw new RestException(404, 'Task not found');
77  }
78 
79  if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
80  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81  }
82 
83  if ($includetimespent == 1) {
84  $timespent = $this->task->getSummaryOfTimeSpent(0);
85  }
86  if ($includetimespent == 2) {
87  $timespent = $this->task->fetchTimeSpentOnTask();
88  }
89 
90  return $this->_cleanObjectDatas($this->task);
91  }
92 
93 
94 
107  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
108  {
109  global $db, $conf;
110 
111  if (!DolibarrApiAccess::$user->rights->projet->lire) {
112  throw new RestException(401);
113  }
114 
115  $obj_ret = array();
116 
117  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
118  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
119 
120  // If the internal user must only see his customers, force searching by him
121  $search_sale = 0;
122  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
123  $search_sale = DolibarrApiAccess::$user->id;
124  }
125 
126  $sql = "SELECT t.rowid";
127  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128  $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)
129  }
130  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t";
131 
132  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
134  }
135 
136  $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
137  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
138  $sql .= " AND t.fk_soc = sc.fk_soc";
139  }
140  if ($socids) {
141  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
142  }
143  if ($search_sale > 0) {
144  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
145  }
146  // Insert sale filter
147  if ($search_sale > 0) {
148  $sql .= " AND sc.fk_user = ".((int) $search_sale);
149  }
150  // Add sql filters
151  if ($sqlfilters) {
152  $errormessage = '';
153  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
154  if ($errormessage) {
155  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
156  }
157  }
158 
159  $sql .= $this->db->order($sortfield, $sortorder);
160  if ($limit) {
161  if ($page < 0) {
162  $page = 0;
163  }
164  $offset = $limit * $page;
165 
166  $sql .= $this->db->plimit($limit + 1, $offset);
167  }
168 
169  dol_syslog("API Rest request");
170  $result = $this->db->query($sql);
171 
172  if ($result) {
173  $num = $this->db->num_rows($result);
174  $min = min($num, ($limit <= 0 ? $num : $limit));
175  $i = 0;
176  while ($i < $min) {
177  $obj = $this->db->fetch_object($result);
178  $task_static = new Task($this->db);
179  if ($task_static->fetch($obj->rowid)) {
180  $obj_ret[] = $this->_cleanObjectDatas($task_static);
181  }
182  $i++;
183  }
184  } else {
185  throw new RestException(503, 'Error when retrieve task list : '.$this->db->lasterror());
186  }
187  if (!count($obj_ret)) {
188  throw new RestException(404, 'No task found');
189  }
190  return $obj_ret;
191  }
192 
199  public function post($request_data = null)
200  {
201  if (!DolibarrApiAccess::$user->rights->projet->creer) {
202  throw new RestException(401, "Insuffisant rights");
203  }
204  // Check mandatory fields
205  $result = $this->_validate($request_data);
206 
207  foreach ($request_data as $field => $value) {
208  $this->task->$field = $value;
209  }
210  /*if (isset($request_data["lines"])) {
211  $lines = array();
212  foreach ($request_data["lines"] as $line) {
213  array_push($lines, (object) $line);
214  }
215  $this->project->lines = $lines;
216  }*/
217  if ($this->task->create(DolibarrApiAccess::$user) < 0) {
218  throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
219  }
220 
221  return $this->task->id;
222  }
223 
224  // /**
225  // * Get time spent of a task
226  // *
227  // * @param int $id Id of task
228  // * @return int
229  // *
230  // * @url GET {id}/tasks
231  // */
232  /*
233  public function getLines($id, $includetimespent=0)
234  {
235  if(! DolibarrApiAccess::$user->rights->projet->lire) {
236  throw new RestException(401);
237  }
238 
239  $result = $this->project->fetch($id);
240  if( ! $result ) {
241  throw new RestException(404, 'Project not found');
242  }
243 
244  if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
245  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
246  }
247  $this->project->getLinesArray(DolibarrApiAccess::$user);
248  $result = array();
249  foreach ($this->project->lines as $line) // $line is a task
250  {
251  if ($includetimespent == 1)
252  {
253  $timespent = $line->getSummaryOfTimeSpent(0);
254  }
255  if ($includetimespent == 1)
256  {
257  // TODO
258  // Add class for timespent records and loop and fill $line->lines with records of timespent
259  }
260  array_push($result,$this->_cleanObjectDatas($line));
261  }
262  return $result;
263  }
264  */
265 
276  public function getRoles($id, $userid = 0)
277  {
278  global $db;
279 
280  if (!DolibarrApiAccess::$user->rights->projet->lire) {
281  throw new RestException(401);
282  }
283 
284  $result = $this->task->fetch($id);
285  if (!$result) {
286  throw new RestException(404, 'Task not found');
287  }
288 
289  if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
290  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
291  }
292 
293  $usert = DolibarrApiAccess::$user;
294  if ($userid > 0) {
295  $usert = new User($this->db);
296  $usert->fetch($userid);
297  }
298  $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(0, $usert, 0, $id);
299  $result = array();
300  foreach ($this->task->roles as $line) {
301  array_push($result, $this->_cleanObjectDatas($line));
302  }
303  return $result;
304  }
305 
306 
307  // /**
308  // * Add a task to given project
309  // *
310  // * @param int $id Id of project to update
311  // * @param array $request_data Projectline data
312  // *
313  // * @url POST {id}/tasks
314  // *
315  // * @return int
316  // */
317  /*
318  public function postLine($id, $request_data = null)
319  {
320  if(! DolibarrApiAccess::$user->rights->projet->creer) {
321  throw new RestException(401);
322  }
323 
324  $result = $this->project->fetch($id);
325  if( ! $result ) {
326  throw new RestException(404, 'Project not found');
327  }
328 
329  if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
330  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
331  }
332 
333  $request_data = (object) $request_data;
334 
335  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
336 
337  $updateRes = $this->project->addline(
338  $request_data->desc,
339  $request_data->subprice,
340  $request_data->qty,
341  $request_data->tva_tx,
342  $request_data->localtax1_tx,
343  $request_data->localtax2_tx,
344  $request_data->fk_product,
345  $request_data->remise_percent,
346  $request_data->info_bits,
347  $request_data->fk_remise_except,
348  'HT',
349  0,
350  $request_data->date_start,
351  $request_data->date_end,
352  $request_data->product_type,
353  $request_data->rang,
354  $request_data->special_code,
355  $fk_parent_line,
356  $request_data->fk_fournprice,
357  $request_data->pa_ht,
358  $request_data->label,
359  $request_data->array_options,
360  $request_data->fk_unit,
361  $this->element,
362  $request_data->id
363  );
364 
365  if ($updateRes > 0) {
366  return $updateRes;
367 
368  }
369  return false;
370  }
371  */
372 
373  // /**
374  // * Update a task to given project
375  // *
376  // * @param int $id Id of project to update
377  // * @param int $taskid Id of task to update
378  // * @param array $request_data Projectline data
379  // *
380  // * @url PUT {id}/tasks/{taskid}
381  // *
382  // * @return object
383  // */
384  /*
385  public function putLine($id, $lineid, $request_data = null)
386  {
387  if(! DolibarrApiAccess::$user->rights->projet->creer) {
388  throw new RestException(401);
389  }
390 
391  $result = $this->project->fetch($id);
392  if( ! $result ) {
393  throw new RestException(404, 'Project not found');
394  }
395 
396  if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
397  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
398  }
399 
400  $request_data = (object) $request_data;
401 
402  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
403 
404  $updateRes = $this->project->updateline(
405  $lineid,
406  $request_data->desc,
407  $request_data->subprice,
408  $request_data->qty,
409  $request_data->remise_percent,
410  $request_data->tva_tx,
411  $request_data->localtax1_tx,
412  $request_data->localtax2_tx,
413  'HT',
414  $request_data->info_bits,
415  $request_data->date_start,
416  $request_data->date_end,
417  $request_data->product_type,
418  $request_data->fk_parent_line,
419  0,
420  $request_data->fk_fournprice,
421  $request_data->pa_ht,
422  $request_data->label,
423  $request_data->special_code,
424  $request_data->array_options,
425  $request_data->fk_unit
426  );
427 
428  if ($updateRes > 0) {
429  $result = $this->get($id);
430  unset($result->line);
431  return $this->_cleanObjectDatas($result);
432  }
433  return false;
434  }*/
435 
436 
445  public function put($id, $request_data = null)
446  {
447  if (!DolibarrApiAccess::$user->rights->projet->creer) {
448  throw new RestException(401);
449  }
450 
451  $result = $this->task->fetch($id);
452  if (!$result) {
453  throw new RestException(404, 'Task not found');
454  }
455 
456  if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
457  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
458  }
459  foreach ($request_data as $field => $value) {
460  if ($field == 'id') {
461  continue;
462  }
463  $this->task->$field = $value;
464  }
465 
466  if ($this->task->update(DolibarrApiAccess::$user) > 0) {
467  return $this->get($id);
468  } else {
469  throw new RestException(500, $this->task->error);
470  }
471  }
472 
480  public function delete($id)
481  {
482  if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
483  throw new RestException(401);
484  }
485  $result = $this->task->fetch($id);
486  if (!$result) {
487  throw new RestException(404, 'Task not found');
488  }
489 
490  if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
491  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
492  }
493 
494  if (!$this->task->delete(DolibarrApiAccess::$user)) {
495  throw new RestException(500, 'Error when delete task : '.$this->task->error);
496  }
497 
498  return array(
499  'success' => array(
500  'code' => 200,
501  'message' => 'Task deleted'
502  )
503  );
504  }
505 
506 
523  public function addTimeSpent($id, $date, $duration, $user_id = 0, $note = '')
524  {
525  if (!DolibarrApiAccess::$user->rights->projet->creer) {
526  throw new RestException(401);
527  }
528  $result = $this->task->fetch($id);
529  if ($result <= 0) {
530  throw new RestException(404, 'Task not found');
531  }
532 
533  if (!DolibarrApi::_checkAccessToResource('project', $this->task->fk_project)) {
534  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
535  }
536 
537  $uid = $user_id;
538  if (empty($uid)) {
539  $uid = DolibarrApiAccess::$user->id;
540  }
541 
542  $newdate = dol_stringtotime($date, 1);
543  $this->task->timespent_date = $newdate;
544  $this->task->timespent_datehour = $newdate;
545  $this->task->timespent_withhour = 1;
546  $this->task->timespent_duration = $duration;
547  $this->task->timespent_fk_user = $uid;
548  $this->task->timespent_note = $note;
549 
550  $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
551  if ($result == 0) {
552  throw new RestException(304, 'Error nothing done. May be object is already validated');
553  }
554  if ($result < 0) {
555  throw new RestException(500, 'Error when adding time: '.$this->task->error);
556  }
557 
558  return array(
559  'success' => array(
560  'code' => 200,
561  'message' => 'Time spent added'
562  )
563  );
564  }
565 
582  public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note = '')
583  {
584  if (!DolibarrApiAccess::$user->rights->projet->creer) {
585  throw new RestException(401);
586  }
587  $this->timespentRecordChecks($id, $timespent_id);
588 
589  if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
590  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
591  }
592 
593  $newdate = dol_stringtotime($date, 1);
594  $this->task->timespent_date = $newdate;
595  $this->task->timespent_datehour = $newdate;
596  $this->task->timespent_withhour = 1;
597  $this->task->timespent_duration = $duration;
598  $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
599  $this->task->timespent_note = $note;
600 
601  $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
602  if ($result == 0) {
603  throw new RestException(304, 'Error nothing done.');
604  }
605  if ($result < 0) {
606  throw new RestException(500, 'Error when updating time spent: '.$this->task->error);
607  }
608 
609  return array(
610  'success' => array(
611  'code' => 200,
612  'message' => 'Time spent updated'
613  )
614  );
615  }
616 
627  public function deleteTimeSpent($id, $timespent_id)
628  {
629  if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
630  throw new RestException(401);
631  }
632  $this->timespentRecordChecks($id, $timespent_id);
633 
634  if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
635  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
636  }
637 
638  if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
639  throw new RestException(500, 'Error when deleting time spent: '.$this->task->error);
640  }
641 
642  return array(
643  'success' => array(
644  'code' => 200,
645  'message' => 'Time spent deleted'
646  )
647  );
648  }
649 
659  protected function timespentRecordChecks($id, $timespent_id)
660  {
661  if ($this->task->fetch($id) <= 0) {
662  throw new RestException(404, 'Task not found');
663  }
664  if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
665  throw new RestException(404, 'Timespent not found');
666  } elseif ($this->task->id != $id) {
667  throw new RestException(404, 'Timespent not found in selected task');
668  }
669  }
670 
671  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
678  protected function _cleanObjectDatas($object)
679  {
680  // phpcs:enable
681  $object = parent::_cleanObjectDatas($object);
682 
683  unset($object->barcode_type);
684  unset($object->barcode_type_code);
685  unset($object->barcode_type_label);
686  unset($object->barcode_type_coder);
687  unset($object->cond_reglement_id);
688  unset($object->cond_reglement);
689  unset($object->fk_delivery_address);
690  unset($object->shipping_method_id);
691  unset($object->fk_account);
692  unset($object->note);
693  unset($object->fk_incoterms);
694  unset($object->label_incoterms);
695  unset($object->location_incoterms);
696  unset($object->name);
697  unset($object->lastname);
698  unset($object->firstname);
699  unset($object->civility_id);
700  unset($object->mode_reglement_id);
701  unset($object->country);
702  unset($object->country_id);
703  unset($object->country_code);
704 
705  unset($object->weekWorkLoad);
706  unset($object->weekWorkLoad);
707 
708  //unset($object->lines); // for task we use timespent_lines, but for project we use lines
709 
710  unset($object->total_ht);
711  unset($object->total_tva);
712  unset($object->total_localtax1);
713  unset($object->total_localtax2);
714  unset($object->total_ttc);
715 
716  unset($object->comments);
717 
718  return $object;
719  }
720 
728  private function _validate($data)
729  {
730  $object = array();
731  foreach (self::$FIELDS as $field) {
732  if (!isset($data[$field])) {
733  throw new RestException(400, "$field field missing");
734  }
735  $object[$field] = $data[$field];
736  }
737  return $object;
738  }
739 
740 
741  // \todo
742  // getSummaryOfTimeSpent
743 }
Tasks\timespentRecordChecks
timespentRecordChecks($id, $timespent_id)
Validate task & timespent IDs for timespent API methods.
Definition: api_tasks.class.php:659
Tasks
Definition: api_tasks.class.php:30
db
$conf db
API class for accounts.
Definition: inc.php:41
Tasks\put
put($id, $request_data=null)
Update task general fields (won't touch time spent of task)
Definition: api_tasks.class.php:445
Tasks\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_tasks.class.php:728
Tasks\putTimeSpent
putTimeSpent($id, $timespent_id, $date, $duration, $user_id=0, $note='')
Update time spent for a task of a project.
Definition: api_tasks.class.php:582
Tasks\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List tasks.
Definition: api_tasks.class.php:107
Task
Class to manage tasks.
Definition: task.class.php:37
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Tasks\addTimeSpent
addTimeSpent($id, $date, $duration, $user_id=0, $note='')
Add time spent to a task of a project.
Definition: api_tasks.class.php:523
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
forgeSQLFromUniversalSearchCriteria
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
Definition: functions.lib.php:11566
User
Class to manage Dolibarr users.
Definition: user.class.php:46
Tasks\getRoles
getRoles($id, $userid=0)
Get roles a user is assigned to a task with.
Definition: api_tasks.class.php:276
Tasks\deleteTimeSpent
deleteTimeSpent($id, $timespent_id)
Delete time spent for a task of a project.
Definition: api_tasks.class.php:627
Tasks\__construct
__construct()
Constructor.
Definition: api_tasks.class.php:50
Tasks\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_tasks.class.php:678
Tasks\post
post($request_data=null)
Create task object.
Definition: api_tasks.class.php:199
dol_stringtotime
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:407