api_projects.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  20. require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  21. /**
  22. * API class for projects
  23. *
  24. * @access protected
  25. * @class DolibarrApiAccess {@requires user,external}
  26. */
  27. class Projects extends DolibarrApi
  28. {
  29. /**
  30. * @var array $FIELDS Mandatory fields, checked when create and update object
  31. */
  32. public static $FIELDS = array(
  33. 'ref',
  34. 'title'
  35. );
  36. /**
  37. * @var Project $project {@type Project}
  38. */
  39. public $project;
  40. /**
  41. * Constructor
  42. */
  43. public function __construct()
  44. {
  45. global $db, $conf;
  46. $this->db = $db;
  47. $this->project = new Project($this->db);
  48. $this->task = new Task($this->db);
  49. }
  50. /**
  51. * Get properties of a project object
  52. *
  53. * Return an array with project informations
  54. *
  55. * @param int $id ID of project
  56. * @return Object Object with cleaned properties
  57. *
  58. * @throws RestException
  59. */
  60. public function get($id)
  61. {
  62. if (!DolibarrApiAccess::$user->rights->projet->lire) {
  63. throw new RestException(401);
  64. }
  65. $result = $this->project->fetch($id);
  66. if (!$result) {
  67. throw new RestException(404, 'Project not found');
  68. }
  69. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  70. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  71. }
  72. $this->project->fetchObjectLinked();
  73. return $this->_cleanObjectDatas($this->project);
  74. }
  75. /**
  76. * List projects
  77. *
  78. * Get a list of projects
  79. *
  80. * @param string $sortfield Sort field
  81. * @param string $sortorder Sort order
  82. * @param int $limit Limit for list
  83. * @param int $page Page number
  84. * @param string $thirdparty_ids Thirdparty ids to filter projects of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  85. * @param int $category Use this param to filter list by category
  86. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  87. * @return array Array of project objects
  88. */
  89. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '')
  90. {
  91. global $db, $conf;
  92. if (!DolibarrApiAccess::$user->rights->projet->lire) {
  93. throw new RestException(401);
  94. }
  95. $obj_ret = array();
  96. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  97. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  98. // If the internal user must only see his customers, force searching by him
  99. $search_sale = 0;
  100. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  101. $search_sale = DolibarrApiAccess::$user->id;
  102. }
  103. $sql = "SELECT t.rowid";
  104. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  105. $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)
  106. }
  107. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  108. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields
  109. if ($category > 0) {
  110. $sql .= ", ".MAIN_DB_PREFIX."categorie_project as c";
  111. }
  112. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  113. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  114. }
  115. $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
  116. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  117. $sql .= " AND t.fk_soc = sc.fk_soc";
  118. }
  119. if ($socids) {
  120. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  121. }
  122. if ($search_sale > 0) {
  123. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  124. }
  125. // Insert sale filter
  126. if ($search_sale > 0) {
  127. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  128. }
  129. // Select projects of given category
  130. if ($category > 0) {
  131. $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
  132. }
  133. // Add sql filters
  134. if ($sqlfilters) {
  135. $errormessage = '';
  136. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  137. if ($errormessage) {
  138. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  139. }
  140. }
  141. $sql .= $this->db->order($sortfield, $sortorder);
  142. if ($limit) {
  143. if ($page < 0) {
  144. $page = 0;
  145. }
  146. $offset = $limit * $page;
  147. $sql .= $this->db->plimit($limit + 1, $offset);
  148. }
  149. dol_syslog("API Rest request");
  150. $result = $this->db->query($sql);
  151. if ($result) {
  152. $num = $this->db->num_rows($result);
  153. $min = min($num, ($limit <= 0 ? $num : $limit));
  154. $i = 0;
  155. while ($i < $min) {
  156. $obj = $this->db->fetch_object($result);
  157. $project_static = new Project($this->db);
  158. if ($project_static->fetch($obj->rowid)) {
  159. $obj_ret[] = $this->_cleanObjectDatas($project_static);
  160. }
  161. $i++;
  162. }
  163. } else {
  164. throw new RestException(503, 'Error when retrieve project list : '.$this->db->lasterror());
  165. }
  166. if (!count($obj_ret)) {
  167. throw new RestException(404, 'No project found');
  168. }
  169. return $obj_ret;
  170. }
  171. /**
  172. * Create project object
  173. *
  174. * @param array $request_data Request data
  175. * @return int ID of project
  176. */
  177. public function post($request_data = null)
  178. {
  179. if (!DolibarrApiAccess::$user->rights->projet->creer) {
  180. throw new RestException(401, "Insuffisant rights");
  181. }
  182. // Check mandatory fields
  183. $result = $this->_validate($request_data);
  184. foreach ($request_data as $field => $value) {
  185. $this->project->$field = $value;
  186. }
  187. /*if (isset($request_data["lines"])) {
  188. $lines = array();
  189. foreach ($request_data["lines"] as $line) {
  190. array_push($lines, (object) $line);
  191. }
  192. $this->project->lines = $lines;
  193. }*/
  194. if ($this->project->create(DolibarrApiAccess::$user) < 0) {
  195. throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
  196. }
  197. return $this->project->id;
  198. }
  199. /**
  200. * Get tasks of a project.
  201. * See also API /tasks
  202. *
  203. * @param int $id Id of project
  204. * @param int $includetimespent 0=Return only list of tasks. 1=Include a summary of time spent, 2=Include details of time spent lines
  205. * @return array
  206. *
  207. * @url GET {id}/tasks
  208. */
  209. public function getLines($id, $includetimespent = 0)
  210. {
  211. if (!DolibarrApiAccess::$user->rights->projet->lire) {
  212. throw new RestException(401);
  213. }
  214. $result = $this->project->fetch($id);
  215. if (!$result) {
  216. throw new RestException(404, 'Project not found');
  217. }
  218. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  219. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  220. }
  221. $this->project->getLinesArray(DolibarrApiAccess::$user);
  222. $result = array();
  223. foreach ($this->project->lines as $line) { // $line is a task
  224. if ($includetimespent == 1) {
  225. $timespent = $line->getSummaryOfTimeSpent(0);
  226. }
  227. if ($includetimespent == 2) {
  228. $timespent = $line->fetchTimeSpentOnTask();
  229. }
  230. array_push($result, $this->_cleanObjectDatas($line));
  231. }
  232. return $result;
  233. }
  234. /**
  235. * Get roles a user is assigned to a project with
  236. *
  237. * @param int $id Id of project
  238. * @param int $userid Id of user (0 = connected user)
  239. * @return array
  240. *
  241. * @url GET {id}/roles
  242. */
  243. public function getRoles($id, $userid = 0)
  244. {
  245. global $db;
  246. if (!DolibarrApiAccess::$user->rights->projet->lire) {
  247. throw new RestException(401);
  248. }
  249. $result = $this->project->fetch($id);
  250. if (!$result) {
  251. throw new RestException(404, 'Project not found');
  252. }
  253. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  254. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  255. }
  256. require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  257. $taskstatic = new Task($this->db);
  258. $userp = DolibarrApiAccess::$user;
  259. if ($userid > 0) {
  260. $userp = new User($this->db);
  261. $userp->fetch($userid);
  262. }
  263. $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0);
  264. $result = array();
  265. foreach ($this->project->roles as $line) {
  266. array_push($result, $this->_cleanObjectDatas($line));
  267. }
  268. return $result;
  269. }
  270. /**
  271. * Add a task to given project
  272. *
  273. * @param int $id Id of project to update
  274. * @param array $request_data Projectline data
  275. *
  276. * @url POST {id}/tasks
  277. *
  278. * @return int
  279. */
  280. /*
  281. public function postLine($id, $request_data = null)
  282. {
  283. if(! DolibarrApiAccess::$user->rights->projet->creer) {
  284. throw new RestException(401);
  285. }
  286. $result = $this->project->fetch($id);
  287. if( ! $result ) {
  288. throw new RestException(404, 'Project not found');
  289. }
  290. if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
  291. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  292. }
  293. $request_data = (object) $request_data;
  294. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  295. $updateRes = $this->project->addline(
  296. $request_data->desc,
  297. $request_data->subprice,
  298. $request_data->qty,
  299. $request_data->tva_tx,
  300. $request_data->localtax1_tx,
  301. $request_data->localtax2_tx,
  302. $request_data->fk_product,
  303. $request_data->remise_percent,
  304. $request_data->info_bits,
  305. $request_data->fk_remise_except,
  306. 'HT',
  307. 0,
  308. $request_data->date_start,
  309. $request_data->date_end,
  310. $request_data->product_type,
  311. $request_data->rang,
  312. $request_data->special_code,
  313. $fk_parent_line,
  314. $request_data->fk_fournprice,
  315. $request_data->pa_ht,
  316. $request_data->label,
  317. $request_data->array_options,
  318. $request_data->fk_unit,
  319. $this->element,
  320. $request_data->id
  321. );
  322. if ($updateRes > 0) {
  323. return $updateRes;
  324. }
  325. return false;
  326. }
  327. */
  328. /**
  329. * Update a task to given project
  330. *
  331. * @param int $id Id of project to update
  332. * @param int $taskid Id of task to update
  333. * @param array $request_data Projectline data
  334. *
  335. * @url PUT {id}/tasks/{taskid}
  336. *
  337. * @return object
  338. */
  339. /*
  340. public function putLine($id, $lineid, $request_data = null)
  341. {
  342. if(! DolibarrApiAccess::$user->rights->projet->creer) {
  343. throw new RestException(401);
  344. }
  345. $result = $this->project->fetch($id);
  346. if( ! $result ) {
  347. throw new RestException(404, 'Project not found');
  348. }
  349. if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
  350. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  351. }
  352. $request_data = (object) $request_data;
  353. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  354. $updateRes = $this->project->updateline(
  355. $lineid,
  356. $request_data->desc,
  357. $request_data->subprice,
  358. $request_data->qty,
  359. $request_data->remise_percent,
  360. $request_data->tva_tx,
  361. $request_data->localtax1_tx,
  362. $request_data->localtax2_tx,
  363. 'HT',
  364. $request_data->info_bits,
  365. $request_data->date_start,
  366. $request_data->date_end,
  367. $request_data->product_type,
  368. $request_data->fk_parent_line,
  369. 0,
  370. $request_data->fk_fournprice,
  371. $request_data->pa_ht,
  372. $request_data->label,
  373. $request_data->special_code,
  374. $request_data->array_options,
  375. $request_data->fk_unit
  376. );
  377. if ($updateRes > 0) {
  378. $result = $this->get($id);
  379. unset($result->line);
  380. return $this->_cleanObjectDatas($result);
  381. }
  382. return false;
  383. }*/
  384. /**
  385. * Update project general fields (won't touch lines of project)
  386. *
  387. * @param int $id Id of project to update
  388. * @param array $request_data Datas
  389. *
  390. * @return int
  391. */
  392. public function put($id, $request_data = null)
  393. {
  394. if (!DolibarrApiAccess::$user->rights->projet->creer) {
  395. throw new RestException(401);
  396. }
  397. $result = $this->project->fetch($id);
  398. if ($result <= 0) {
  399. throw new RestException(404, 'Project not found');
  400. }
  401. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  402. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  403. }
  404. foreach ($request_data as $field => $value) {
  405. if ($field == 'id') {
  406. continue;
  407. }
  408. $this->project->$field = $value;
  409. }
  410. if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
  411. return $this->get($id);
  412. } else {
  413. throw new RestException(500, $this->project->error);
  414. }
  415. }
  416. /**
  417. * Delete project
  418. *
  419. * @param int $id Project ID
  420. *
  421. * @return array
  422. */
  423. public function delete($id)
  424. {
  425. if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
  426. throw new RestException(401);
  427. }
  428. $result = $this->project->fetch($id);
  429. if (!$result) {
  430. throw new RestException(404, 'Project not found');
  431. }
  432. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  433. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  434. }
  435. if (!$this->project->delete(DolibarrApiAccess::$user)) {
  436. throw new RestException(500, 'Error when delete project : '.$this->project->error);
  437. }
  438. return array(
  439. 'success' => array(
  440. 'code' => 200,
  441. 'message' => 'Project deleted'
  442. )
  443. );
  444. }
  445. /**
  446. * Validate a project.
  447. * You can test this API with the following input message
  448. * { "notrigger": 0 }
  449. *
  450. * @param int $id Project ID
  451. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  452. *
  453. * @url POST {id}/validate
  454. *
  455. * @return array
  456. * FIXME An error 403 is returned if the request has an empty body.
  457. * Error message: "Forbidden: Content type `text/plain` is not supported."
  458. * Workaround: send this in the body
  459. * {
  460. * "notrigger": 0
  461. * }
  462. */
  463. public function validate($id, $notrigger = 0)
  464. {
  465. if (!DolibarrApiAccess::$user->rights->projet->creer) {
  466. throw new RestException(401);
  467. }
  468. $result = $this->project->fetch($id);
  469. if (!$result) {
  470. throw new RestException(404, 'Project not found');
  471. }
  472. if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
  473. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  474. }
  475. $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
  476. if ($result == 0) {
  477. throw new RestException(304, 'Error nothing done. May be object is already validated');
  478. }
  479. if ($result < 0) {
  480. throw new RestException(500, 'Error when validating Project: '.$this->project->error);
  481. }
  482. return array(
  483. 'success' => array(
  484. 'code' => 200,
  485. 'message' => 'Project validated'
  486. )
  487. );
  488. }
  489. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  490. /**
  491. * Clean sensible object datas
  492. *
  493. * @param Object $object Object to clean
  494. * @return Object Object with cleaned properties
  495. */
  496. protected function _cleanObjectDatas($object)
  497. {
  498. // phpcs:enable
  499. $object = parent::_cleanObjectDatas($object);
  500. unset($object->datec);
  501. unset($object->datem);
  502. unset($object->barcode_type);
  503. unset($object->barcode_type_code);
  504. unset($object->barcode_type_label);
  505. unset($object->barcode_type_coder);
  506. unset($object->cond_reglement_id);
  507. unset($object->cond_reglement);
  508. unset($object->fk_delivery_address);
  509. unset($object->shipping_method_id);
  510. unset($object->fk_account);
  511. unset($object->note);
  512. unset($object->fk_incoterms);
  513. unset($object->label_incoterms);
  514. unset($object->location_incoterms);
  515. unset($object->name);
  516. unset($object->lastname);
  517. unset($object->firstname);
  518. unset($object->civility_id);
  519. unset($object->mode_reglement_id);
  520. unset($object->country);
  521. unset($object->country_id);
  522. unset($object->country_code);
  523. unset($object->weekWorkLoad);
  524. unset($object->weekWorkLoad);
  525. //unset($object->lines); // for task we use timespent_lines, but for project we use lines
  526. unset($object->total_ht);
  527. unset($object->total_tva);
  528. unset($object->total_localtax1);
  529. unset($object->total_localtax2);
  530. unset($object->total_ttc);
  531. unset($object->comments);
  532. return $object;
  533. }
  534. /**
  535. * Validate fields before create or update object
  536. *
  537. * @param array $data Array with data to verify
  538. * @return array
  539. * @throws RestException
  540. */
  541. private function _validate($data)
  542. {
  543. $object = array();
  544. foreach (self::$FIELDS as $field) {
  545. if (!isset($data[$field])) {
  546. throw new RestException(400, "$field field missing");
  547. }
  548. $object[$field] = $data[$field];
  549. }
  550. return $object;
  551. }
  552. // TODO
  553. // getSummaryOfTimeSpent
  554. }