api_warehouses.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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('warehouse', $this->warehouse->id)) {
  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. * @return array Array of warehouse objects
  83. *
  84. * @throws RestException
  85. */
  86. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '')
  87. {
  88. global $db, $conf;
  89. $obj_ret = array();
  90. if (!DolibarrApiAccess::$user->rights->stock->lire) {
  91. throw new RestException(401);
  92. }
  93. $sql = "SELECT t.rowid";
  94. $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
  95. if ($category > 0) {
  96. $sql .= ", ".$this->db->prefix()."categorie_societe as c";
  97. }
  98. $sql .= ' WHERE t.entity IN ('.getEntity('stock').')';
  99. // Select warehouses of given category
  100. if ($category > 0) {
  101. $sql .= " AND c.fk_categorie = ".((int) $category);
  102. $sql .= " AND c.fk_warehouse = t.rowid ";
  103. }
  104. // Add sql filters
  105. if ($sqlfilters) {
  106. $errormessage = '';
  107. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  108. if ($errormessage) {
  109. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  110. }
  111. }
  112. $sql .= $this->db->order($sortfield, $sortorder);
  113. if ($limit) {
  114. if ($page < 0) {
  115. $page = 0;
  116. }
  117. $offset = $limit * $page;
  118. $sql .= $this->db->plimit($limit + 1, $offset);
  119. }
  120. $result = $this->db->query($sql);
  121. if ($result) {
  122. $i = 0;
  123. $num = $this->db->num_rows($result);
  124. $min = min($num, ($limit <= 0 ? $num : $limit));
  125. while ($i < $min) {
  126. $obj = $this->db->fetch_object($result);
  127. $warehouse_static = new Entrepot($this->db);
  128. if ($warehouse_static->fetch($obj->rowid)) {
  129. $obj_ret[] = $this->_cleanObjectDatas($warehouse_static);
  130. }
  131. $i++;
  132. }
  133. } else {
  134. throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
  135. }
  136. if (!count($obj_ret)) {
  137. throw new RestException(404, 'No warehouse found');
  138. }
  139. return $obj_ret;
  140. }
  141. /**
  142. * Create warehouse object
  143. *
  144. * @param array $request_data Request data
  145. * @return int ID of warehouse
  146. */
  147. public function post($request_data = null)
  148. {
  149. if (!DolibarrApiAccess::$user->rights->stock->creer) {
  150. throw new RestException(401);
  151. }
  152. // Check mandatory fields
  153. $result = $this->_validate($request_data);
  154. foreach ($request_data as $field => $value) {
  155. $this->warehouse->$field = $value;
  156. }
  157. if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
  158. throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
  159. }
  160. return $this->warehouse->id;
  161. }
  162. /**
  163. * Update warehouse
  164. *
  165. * @param int $id Id of warehouse to update
  166. * @param array $request_data Datas
  167. * @return int
  168. */
  169. public function put($id, $request_data = null)
  170. {
  171. if (!DolibarrApiAccess::$user->rights->stock->creer) {
  172. throw new RestException(401);
  173. }
  174. $result = $this->warehouse->fetch($id);
  175. if (!$result) {
  176. throw new RestException(404, 'warehouse not found');
  177. }
  178. if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
  179. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  180. }
  181. foreach ($request_data as $field => $value) {
  182. if ($field == 'id') {
  183. continue;
  184. }
  185. $this->warehouse->$field = $value;
  186. }
  187. if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
  188. return $this->get($id);
  189. }
  190. return false;
  191. }
  192. /**
  193. * Delete warehouse
  194. *
  195. * @param int $id Warehouse ID
  196. * @return array
  197. */
  198. public function delete($id)
  199. {
  200. if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
  201. throw new RestException(401);
  202. }
  203. $result = $this->warehouse->fetch($id);
  204. if (!$result) {
  205. throw new RestException(404, 'warehouse not found');
  206. }
  207. if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
  208. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  209. }
  210. if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
  211. throw new RestException(401, 'error when delete warehouse');
  212. }
  213. return array(
  214. 'success' => array(
  215. 'code' => 200,
  216. 'message' => 'Warehouse deleted'
  217. )
  218. );
  219. }
  220. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  221. /**
  222. * Clean sensible object datas
  223. *
  224. * @param Entrepot $object Object to clean
  225. * @return Object Object with cleaned properties
  226. */
  227. protected function _cleanObjectDatas($object)
  228. {
  229. // phpcs:enable
  230. $object = parent::_cleanObjectDatas($object);
  231. // Remove the subscriptions because they are handled as a subresource.
  232. //unset($object->subscriptions);
  233. return $object;
  234. }
  235. /**
  236. * Validate fields before create or update object
  237. *
  238. * @param array|null $data Data to validate
  239. * @return array
  240. *
  241. * @throws RestException
  242. */
  243. private function _validate($data)
  244. {
  245. $warehouse = array();
  246. foreach (Warehouses::$FIELDS as $field) {
  247. if (!isset($data[$field])) {
  248. throw new RestException(400, "$field field missing");
  249. }
  250. $warehouse[$field] = $data[$field];
  251. }
  252. return $warehouse;
  253. }
  254. }