api_warehouses.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  19. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  20. /**
  21. * API class for warehouses
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Warehouses extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'label',
  33. );
  34. /**
  35. * @var Entrepot $warehouse {@type Entrepot}
  36. */
  37. public $warehouse;
  38. /**
  39. * Constructor
  40. */
  41. public function __construct()
  42. {
  43. global $db, $conf;
  44. $this->db = $db;
  45. $this->warehouse = new Entrepot($this->db);
  46. }
  47. /**
  48. * Get properties of a warehouse object
  49. *
  50. * Return an array with warehouse informations
  51. *
  52. * @param int $id ID of warehouse
  53. * @return Object Object with cleaned properties
  54. *
  55. * @throws RestException
  56. */
  57. public function get($id)
  58. {
  59. if (!DolibarrApiAccess::$user->rights->stock->lire) {
  60. throw new RestException(401);
  61. }
  62. $result = $this->warehouse->fetch($id);
  63. if (!$result) {
  64. throw new RestException(404, 'warehouse not found');
  65. }
  66. if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id, 'entrepot')) {
  67. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  68. }
  69. return $this->_cleanObjectDatas($this->warehouse);
  70. }
  71. /**
  72. * List warehouses
  73. *
  74. * Get a list of warehouses
  75. *
  76. * @param string $sortfield Sort field
  77. * @param string $sortorder Sort order
  78. * @param int $limit Limit for list
  79. * @param int $page Page number
  80. * @param int $category Use this param to filter list by category
  81. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'WH-%') and (t.date_creation:<:'20160101')"
  82. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  83. * @return array Array of warehouse objects
  84. *
  85. * @throws RestException
  86. */
  87. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '')
  88. {
  89. global $db, $conf;
  90. $obj_ret = array();
  91. if (!DolibarrApiAccess::$user->rights->stock->lire) {
  92. throw new RestException(401);
  93. }
  94. $sql = "SELECT t.rowid";
  95. $sql .= " FROM ".MAIN_DB_PREFIX."entrepot AS t LEFT JOIN ".MAIN_DB_PREFIX."entrepot_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
  96. if ($category > 0) {
  97. $sql .= ", ".$this->db->prefix()."categorie_warehouse as c";
  98. }
  99. $sql .= ' WHERE t.entity IN ('.getEntity('stock').')';
  100. // Select warehouses of given category
  101. if ($category > 0) {
  102. $sql .= " AND c.fk_categorie = ".((int) $category);
  103. $sql .= " AND c.fk_warehouse = t.rowid ";
  104. }
  105. // Add sql filters
  106. if ($sqlfilters) {
  107. $errormessage = '';
  108. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  109. if ($errormessage) {
  110. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  111. }
  112. }
  113. $sql .= $this->db->order($sortfield, $sortorder);
  114. if ($limit) {
  115. if ($page < 0) {
  116. $page = 0;
  117. }
  118. $offset = $limit * $page;
  119. $sql .= $this->db->plimit($limit + 1, $offset);
  120. }
  121. $result = $this->db->query($sql);
  122. if ($result) {
  123. $i = 0;
  124. $num = $this->db->num_rows($result);
  125. $min = min($num, ($limit <= 0 ? $num : $limit));
  126. while ($i < $min) {
  127. $obj = $this->db->fetch_object($result);
  128. $warehouse_static = new Entrepot($this->db);
  129. if ($warehouse_static->fetch($obj->rowid)) {
  130. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($warehouse_static), $properties);
  131. }
  132. $i++;
  133. }
  134. } else {
  135. throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
  136. }
  137. return $obj_ret;
  138. }
  139. /**
  140. * Create warehouse object
  141. *
  142. * @param array $request_data Request data
  143. * @return int ID of warehouse
  144. */
  145. public function post($request_data = null)
  146. {
  147. if (!DolibarrApiAccess::$user->rights->stock->creer) {
  148. throw new RestException(401);
  149. }
  150. // Check mandatory fields
  151. $result = $this->_validate($request_data);
  152. foreach ($request_data as $field => $value) {
  153. if ($field === 'caller') {
  154. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  155. $this->warehouse->context['caller'] = $request_data['caller'];
  156. continue;
  157. }
  158. $this->warehouse->$field = $value;
  159. }
  160. if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
  161. throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
  162. }
  163. return $this->warehouse->id;
  164. }
  165. /**
  166. * Update warehouse
  167. *
  168. * @param int $id Id of warehouse to update
  169. * @param array $request_data Datas
  170. * @return int
  171. */
  172. public function put($id, $request_data = null)
  173. {
  174. if (!DolibarrApiAccess::$user->rights->stock->creer) {
  175. throw new RestException(401);
  176. }
  177. $result = $this->warehouse->fetch($id);
  178. if (!$result) {
  179. throw new RestException(404, 'warehouse not found');
  180. }
  181. if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
  182. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  183. }
  184. foreach ($request_data as $field => $value) {
  185. if ($field == 'id') {
  186. continue;
  187. }
  188. if ($field === 'caller') {
  189. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  190. $this->warehouse->context['caller'] = $request_data['caller'];
  191. continue;
  192. }
  193. $this->warehouse->$field = $value;
  194. }
  195. if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
  196. return $this->get($id);
  197. }
  198. return false;
  199. }
  200. /**
  201. * Delete warehouse
  202. *
  203. * @param int $id Warehouse ID
  204. * @return array
  205. */
  206. public function delete($id)
  207. {
  208. if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
  209. throw new RestException(401);
  210. }
  211. $result = $this->warehouse->fetch($id);
  212. if (!$result) {
  213. throw new RestException(404, 'warehouse not found');
  214. }
  215. if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
  216. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  217. }
  218. if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
  219. throw new RestException(401, 'error when delete warehouse');
  220. }
  221. return array(
  222. 'success' => array(
  223. 'code' => 200,
  224. 'message' => 'Warehouse deleted'
  225. )
  226. );
  227. }
  228. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  229. /**
  230. * Clean sensible object datas
  231. *
  232. * @param Entrepot $object Object to clean
  233. * @return Object Object with cleaned properties
  234. */
  235. protected function _cleanObjectDatas($object)
  236. {
  237. // phpcs:enable
  238. $object = parent::_cleanObjectDatas($object);
  239. // Remove the subscriptions because they are handled as a subresource.
  240. //unset($object->subscriptions);
  241. return $object;
  242. }
  243. /**
  244. * Validate fields before create or update object
  245. *
  246. * @param array|null $data Data to validate
  247. * @return array
  248. *
  249. * @throws RestException
  250. */
  251. private function _validate($data)
  252. {
  253. $warehouse = array();
  254. foreach (Warehouses::$FIELDS as $field) {
  255. if (!isset($data[$field])) {
  256. throw new RestException(400, "$field field missing");
  257. }
  258. $warehouse[$field] = $data[$field];
  259. }
  260. return $warehouse;
  261. }
  262. }