api_interventions.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.'/fichinter/class/fichinter.class.php';
  20. /**
  21. * API class for Interventions
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Interventions extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'socid',
  33. 'fk_project',
  34. 'description',
  35. );
  36. /**
  37. * @var array $FIELDS Mandatory fields, checked when create and update object
  38. */
  39. public static $FIELDSLINE = array(
  40. 'description',
  41. 'date',
  42. 'duree',
  43. );
  44. /**
  45. * @var fichinter $fichinter {@type fichinter}
  46. */
  47. public $fichinter;
  48. /**
  49. * Constructor
  50. */
  51. public function __construct()
  52. {
  53. global $db, $conf;
  54. $this->db = $db;
  55. $this->fichinter = new Fichinter($this->db);
  56. }
  57. /**
  58. * Get properties of a Expense Report object
  59. *
  60. * Return an array with Expense Report information
  61. *
  62. * @param int $id ID of Expense Report
  63. * @return array|mixed Data without useless information
  64. *
  65. * @throws RestException
  66. */
  67. public function get($id)
  68. {
  69. if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
  70. throw new RestException(401);
  71. }
  72. $result = $this->fichinter->fetch($id);
  73. if (!$result) {
  74. throw new RestException(404, 'Intervention not found');
  75. }
  76. if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
  77. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  78. }
  79. $this->fichinter->fetchObjectLinked();
  80. return $this->_cleanObjectDatas($this->fichinter);
  81. }
  82. /**
  83. * List of interventions
  84. *
  85. * Return a list of interventions
  86. *
  87. * @param string $sortfield Sort field
  88. * @param string $sortorder Sort order
  89. * @param int $limit Limit for list
  90. * @param int $page Page number
  91. * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  92. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  93. * @return array Array of order objects
  94. *
  95. * @throws RestException
  96. */
  97. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  98. {
  99. global $db, $conf;
  100. if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
  101. throw new RestException(401);
  102. }
  103. $obj_ret = array();
  104. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  105. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  106. // If the internal user must only see his customers, force searching by him
  107. $search_sale = 0;
  108. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  109. $search_sale = DolibarrApiAccess::$user->id;
  110. }
  111. $sql = "SELECT t.rowid";
  112. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  113. $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)
  114. }
  115. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as t";
  116. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  117. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  118. }
  119. $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')';
  120. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  121. $sql .= " AND t.fk_soc = sc.fk_soc";
  122. }
  123. if ($socids) {
  124. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  125. }
  126. if ($search_sale > 0) {
  127. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  128. }
  129. // Insert sale filter
  130. if ($search_sale > 0) {
  131. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  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. $fichinter_static = new Fichinter($this->db);
  158. if ($fichinter_static->fetch($obj->rowid)) {
  159. $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
  160. }
  161. $i++;
  162. }
  163. } else {
  164. throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
  165. }
  166. if (!count($obj_ret)) {
  167. throw new RestException(404, 'No intervention found');
  168. }
  169. return $obj_ret;
  170. }
  171. /**
  172. * Create intervention object
  173. *
  174. * @param array $request_data Request data
  175. * @return int ID of intervention
  176. */
  177. public function post($request_data = null)
  178. {
  179. if (!DolibarrApiAccess::$user->rights->ficheinter->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->fichinter->$field = $value;
  186. }
  187. if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
  188. throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
  189. }
  190. return $this->fichinter->id;
  191. }
  192. /**
  193. * Get lines of an intervention
  194. *
  195. * @param int $id Id of intervention
  196. *
  197. * @url GET {id}/lines
  198. *
  199. * @return int
  200. */
  201. /* TODO
  202. public function getLines($id)
  203. {
  204. if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
  205. throw new RestException(401);
  206. }
  207. $result = $this->fichinter->fetch($id);
  208. if( ! $result ) {
  209. throw new RestException(404, 'Intervention not found');
  210. }
  211. if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
  212. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  213. }
  214. $this->fichinter->getLinesArray();
  215. $result = array();
  216. foreach ($this->fichinter->lines as $line) {
  217. array_push($result,$this->_cleanObjectDatas($line));
  218. }
  219. return $result;
  220. }
  221. */
  222. /**
  223. * Add a line to given intervention
  224. *
  225. * @param int $id Id of intervention to update
  226. * @param array $request_data Request data
  227. *
  228. * @url POST {id}/lines
  229. *
  230. * @return int
  231. */
  232. public function postLine($id, $request_data = null)
  233. {
  234. if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
  235. throw new RestException(401, "Insuffisant rights");
  236. }
  237. // Check mandatory fields
  238. $result = $this->_validateLine($request_data);
  239. foreach ($request_data as $field => $value) {
  240. $this->fichinter->$field = $value;
  241. }
  242. if (!$result) {
  243. throw new RestException(404, 'Intervention not found');
  244. }
  245. if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
  246. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  247. }
  248. $updateRes = $this->fichinter->addLine(
  249. DolibarrApiAccess::$user,
  250. $id,
  251. $this->fichinter->description,
  252. $this->fichinter->date,
  253. $this->fichinter->duree
  254. );
  255. if ($updateRes > 0) {
  256. return $updateRes;
  257. } else {
  258. throw new RestException(400, $this->fichinter->error);
  259. }
  260. }
  261. /**
  262. * Delete order
  263. *
  264. * @param int $id Order ID
  265. * @return array
  266. */
  267. public function delete($id)
  268. {
  269. if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
  270. throw new RestException(401);
  271. }
  272. $result = $this->fichinter->fetch($id);
  273. if (!$result) {
  274. throw new RestException(404, 'Intervention not found');
  275. }
  276. if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
  277. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  278. }
  279. if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
  280. throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
  281. }
  282. return array(
  283. 'success' => array(
  284. 'code' => 200,
  285. 'message' => 'Intervention deleted'
  286. )
  287. );
  288. }
  289. /**
  290. * Validate an intervention
  291. *
  292. * If you get a bad value for param notrigger check, provide this in body
  293. * {
  294. * "notrigger": 0
  295. * }
  296. *
  297. * @param int $id Intervention ID
  298. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  299. *
  300. * @url POST {id}/validate
  301. *
  302. * @return array
  303. */
  304. public function validate($id, $notrigger = 0)
  305. {
  306. if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
  307. throw new RestException(401, "Insuffisant rights");
  308. }
  309. $result = $this->fichinter->fetch($id);
  310. if (!$result) {
  311. throw new RestException(404, 'Intervention not found');
  312. }
  313. if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
  314. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  315. }
  316. $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
  317. if ($result == 0) {
  318. throw new RestException(304, 'Error nothing done. May be object is already validated');
  319. }
  320. if ($result < 0) {
  321. throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
  322. }
  323. $this->fichinter->fetchObjectLinked();
  324. return $this->_cleanObjectDatas($this->fichinter);
  325. }
  326. /**
  327. * Close an intervention
  328. *
  329. * @param int $id Intervention ID
  330. *
  331. * @url POST {id}/close
  332. *
  333. * @return array
  334. */
  335. public function closeFichinter($id)
  336. {
  337. if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
  338. throw new RestException(401, "Insuffisant rights");
  339. }
  340. $result = $this->fichinter->fetch($id);
  341. if (!$result) {
  342. throw new RestException(404, 'Intervention not found');
  343. }
  344. if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
  345. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  346. }
  347. $result = $this->fichinter->setStatut(3);
  348. if ($result == 0) {
  349. throw new RestException(304, 'Error nothing done. May be object is already closed');
  350. }
  351. if ($result < 0) {
  352. throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
  353. }
  354. $this->fichinter->fetchObjectLinked();
  355. return $this->_cleanObjectDatas($this->fichinter);
  356. }
  357. /**
  358. * Validate fields before create or update object
  359. *
  360. * @param array $data Data to validate
  361. * @return array
  362. *
  363. * @throws RestException
  364. */
  365. private function _validate($data)
  366. {
  367. $fichinter = array();
  368. foreach (Interventions::$FIELDS as $field) {
  369. if (!isset($data[$field])) {
  370. throw new RestException(400, "$field field missing");
  371. }
  372. $fichinter[$field] = $data[$field];
  373. }
  374. return $fichinter;
  375. }
  376. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  377. /**
  378. * Clean sensible object datas
  379. *
  380. * @param Object $object Object to clean
  381. * @return Object Object with cleaned properties
  382. */
  383. protected function _cleanObjectDatas($object)
  384. {
  385. // phpcs:enable
  386. $object = parent::_cleanObjectDatas($object);
  387. unset($object->statuts_short);
  388. unset($object->statuts_logo);
  389. unset($object->statuts);
  390. return $object;
  391. }
  392. /**
  393. * Validate fields before create or update object
  394. *
  395. * @param array $data Data to validate
  396. * @return array
  397. *
  398. * @throws RestException
  399. */
  400. private function _validateLine($data)
  401. {
  402. $fichinter = array();
  403. foreach (Interventions::$FIELDSLINE as $field) {
  404. if (!isset($data[$field])) {
  405. throw new RestException(400, "$field field missing");
  406. }
  407. $fichinter[$field] = $data[$field];
  408. }
  409. return $fichinter;
  410. }
  411. }