api_expensereports.class.php 16 KB

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