api_boms.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2019 Maxime Kohlhaas <maxime@atm-consulting.fr>
  4. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2022 Christian Humpel <christian.humpel@live.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. use Luracast\Restler\RestException;
  21. require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
  22. /**
  23. * \file bom/class/api_boms.class.php
  24. * \ingroup bom
  25. * \brief File for API management of BOM.
  26. */
  27. /**
  28. * API class for BOM
  29. *
  30. * @access protected
  31. * @class DolibarrApiAccess {@requires user,external}
  32. */
  33. class Boms extends DolibarrApi
  34. {
  35. /**
  36. * @var BOM $bom {@type BOM}
  37. */
  38. public $bom;
  39. /**
  40. * Constructor
  41. */
  42. public function __construct()
  43. {
  44. global $db, $conf;
  45. $this->db = $db;
  46. $this->bom = new BOM($this->db);
  47. }
  48. /**
  49. * Get properties of a bom object
  50. *
  51. * Return an array with bom informations
  52. *
  53. * @param int $id ID of bom
  54. * @return Object Object with cleaned properties
  55. *
  56. * @url GET {id}
  57. * @throws RestException
  58. */
  59. public function get($id)
  60. {
  61. if (!DolibarrApiAccess::$user->rights->bom->read) {
  62. throw new RestException(401);
  63. }
  64. $result = $this->bom->fetch($id);
  65. if (!$result) {
  66. throw new RestException(404, 'BOM not found');
  67. }
  68. if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
  69. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  70. }
  71. return $this->_cleanObjectDatas($this->bom);
  72. }
  73. /**
  74. * List boms
  75. *
  76. * Get a list of boms
  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 $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  83. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  84. * @return array Array of order objects
  85. *
  86. * @throws RestException
  87. */
  88. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
  89. {
  90. global $db, $conf;
  91. if (!DolibarrApiAccess::$user->rights->bom->read) {
  92. throw new RestException(401);
  93. }
  94. $obj_ret = array();
  95. $tmpobject = new BOM($this->db);
  96. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  97. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  98. // If the internal user must only see his customers, force searching by him
  99. $search_sale = 0;
  100. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  101. $search_sale = DolibarrApiAccess::$user->id;
  102. }
  103. $sql = "SELECT t.rowid";
  104. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $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.$tmpobject->table_element." AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_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
  108. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  109. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  110. }
  111. $sql .= " WHERE 1 = 1";
  112. // Example of use $mode
  113. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  114. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  115. if ($tmpobject->ismultientitymanaged) {
  116. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  117. }
  118. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  119. $sql .= " AND t.fk_soc = sc.fk_soc";
  120. }
  121. if ($restrictonsocid && $socid) {
  122. $sql .= " AND t.fk_soc = ".((int) $socid);
  123. }
  124. if ($restrictonsocid && $search_sale > 0) {
  125. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  126. }
  127. // Insert sale filter
  128. if ($restrictonsocid && $search_sale > 0) {
  129. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  130. }
  131. if ($sqlfilters) {
  132. $errormessage = '';
  133. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  134. if ($errormessage) {
  135. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  136. }
  137. }
  138. $sql .= $this->db->order($sortfield, $sortorder);
  139. if ($limit) {
  140. if ($page < 0) {
  141. $page = 0;
  142. }
  143. $offset = $limit * $page;
  144. $sql .= $this->db->plimit($limit + 1, $offset);
  145. }
  146. $result = $this->db->query($sql);
  147. if ($result) {
  148. $num = $this->db->num_rows($result);
  149. $i = 0;
  150. while ($i < $num) {
  151. $obj = $this->db->fetch_object($result);
  152. $bom_static = new BOM($this->db);
  153. if ($bom_static->fetch($obj->rowid)) {
  154. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties);
  155. }
  156. $i++;
  157. }
  158. } else {
  159. throw new RestException(503, 'Error when retrieve bom list');
  160. }
  161. if (!count($obj_ret)) {
  162. throw new RestException(404, 'No bom found');
  163. }
  164. return $obj_ret;
  165. }
  166. /**
  167. * Create bom object
  168. *
  169. * @param array $request_data Request datas
  170. * @return int ID of bom
  171. */
  172. public function post($request_data = null)
  173. {
  174. if (!DolibarrApiAccess::$user->rights->bom->write) {
  175. throw new RestException(401);
  176. }
  177. // Check mandatory fields
  178. $result = $this->_validate($request_data);
  179. foreach ($request_data as $field => $value) {
  180. $this->bom->$field = $value;
  181. }
  182. $this->checkRefNumbering();
  183. if (!$this->bom->create(DolibarrApiAccess::$user)) {
  184. throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
  185. }
  186. return $this->bom->id;
  187. }
  188. /**
  189. * Update bom
  190. *
  191. * @param int $id Id of bom to update
  192. * @param array $request_data Datas
  193. *
  194. * @return int
  195. */
  196. public function put($id, $request_data = null)
  197. {
  198. if (!DolibarrApiAccess::$user->rights->bom->write) {
  199. throw new RestException(401);
  200. }
  201. $result = $this->bom->fetch($id);
  202. if (!$result) {
  203. throw new RestException(404, 'BOM not found');
  204. }
  205. if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
  206. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  207. }
  208. foreach ($request_data as $field => $value) {
  209. if ($field == 'id') {
  210. continue;
  211. }
  212. $this->bom->$field = $value;
  213. }
  214. $this->checkRefNumbering();
  215. if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
  216. return $this->get($id);
  217. } else {
  218. throw new RestException(500, $this->bom->error);
  219. }
  220. }
  221. /**
  222. * Delete bom
  223. *
  224. * @param int $id BOM ID
  225. * @return array
  226. */
  227. public function delete($id)
  228. {
  229. if (!DolibarrApiAccess::$user->rights->bom->delete) {
  230. throw new RestException(401);
  231. }
  232. $result = $this->bom->fetch($id);
  233. if (!$result) {
  234. throw new RestException(404, 'BOM not found');
  235. }
  236. if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
  237. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  238. }
  239. if (!$this->bom->delete(DolibarrApiAccess::$user)) {
  240. throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
  241. }
  242. return array(
  243. 'success' => array(
  244. 'code' => 200,
  245. 'message' => 'BOM deleted'
  246. )
  247. );
  248. }
  249. /**
  250. * Get lines of an BOM
  251. *
  252. * @param int $id Id of BOM
  253. *
  254. * @url GET {id}/lines
  255. *
  256. * @return array
  257. */
  258. public function getLines($id)
  259. {
  260. if (!DolibarrApiAccess::$user->rights->bom->read) {
  261. throw new RestException(401);
  262. }
  263. $result = $this->bom->fetch($id);
  264. if (!$result) {
  265. throw new RestException(404, 'BOM not found');
  266. }
  267. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  268. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  269. }
  270. $this->bom->getLinesArray();
  271. $result = array();
  272. foreach ($this->bom->lines as $line) {
  273. array_push($result, $this->_cleanObjectDatas($line));
  274. }
  275. return $result;
  276. }
  277. /**
  278. * Add a line to given BOM
  279. *
  280. * @param int $id Id of BOM to update
  281. * @param array $request_data BOMLine data
  282. *
  283. * @url POST {id}/lines
  284. *
  285. * @return int
  286. */
  287. public function postLine($id, $request_data = null)
  288. {
  289. if (!DolibarrApiAccess::$user->rights->bom->write) {
  290. throw new RestException(401);
  291. }
  292. $result = $this->bom->fetch($id);
  293. if (!$result) {
  294. throw new RestException(404, 'BOM not found');
  295. }
  296. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  297. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  298. }
  299. $request_data = (object) $request_data;
  300. $updateRes = $this->bom->addLine(
  301. $request_data->fk_product,
  302. $request_data->qty,
  303. $request_data->qty_frozen,
  304. $request_data->disable_stock_change,
  305. $request_data->efficiency,
  306. $request_data->position,
  307. $request_data->fk_bom_child,
  308. $request_data->import_key
  309. );
  310. if ($updateRes > 0) {
  311. return $updateRes;
  312. } else {
  313. throw new RestException(400, $this->bom->error);
  314. }
  315. }
  316. /**
  317. * Update a line to given BOM
  318. *
  319. * @param int $id Id of BOM to update
  320. * @param int $lineid Id of line to update
  321. * @param array $request_data BOMLine data
  322. *
  323. * @url PUT {id}/lines/{lineid}
  324. *
  325. * @return object|bool
  326. */
  327. public function putLine($id, $lineid, $request_data = null)
  328. {
  329. if (!DolibarrApiAccess::$user->rights->bom->write) {
  330. throw new RestException(401);
  331. }
  332. $result = $this->bom->fetch($id);
  333. if (!$result) {
  334. throw new RestException(404, 'BOM not found');
  335. }
  336. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  337. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  338. }
  339. $request_data = (object) $request_data;
  340. $updateRes = $this->bom->updateLine(
  341. $lineid,
  342. $request_data->qty,
  343. $request_data->qty_frozen,
  344. $request_data->disable_stock_change,
  345. $request_data->efficiency,
  346. $request_data->position,
  347. $request_data->import_key
  348. );
  349. if ($updateRes > 0) {
  350. $result = $this->get($id);
  351. unset($result->line);
  352. return $this->_cleanObjectDatas($result);
  353. }
  354. return false;
  355. }
  356. /**
  357. * Delete a line to given BOM
  358. *
  359. *
  360. * @param int $id Id of BOM to update
  361. * @param int $lineid Id of line to delete
  362. *
  363. * @url DELETE {id}/lines/{lineid}
  364. *
  365. * @return int
  366. *
  367. * @throws RestException 401
  368. * @throws RestException 404
  369. * @throws RestException 500
  370. */
  371. public function deleteLine($id, $lineid)
  372. {
  373. if (!DolibarrApiAccess::$user->rights->bom->write) {
  374. throw new RestException(401);
  375. }
  376. $result = $this->bom->fetch($id);
  377. if (!$result) {
  378. throw new RestException(404, 'BOM not found');
  379. }
  380. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  381. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  382. }
  383. //Check the rowid is a line of current bom object
  384. $lineIdIsFromObject = false;
  385. foreach ($this->bom->lines as $bl) {
  386. if ($bl->id == $lineid) {
  387. $lineIdIsFromObject = true;
  388. break;
  389. }
  390. }
  391. if (!$lineIdIsFromObject) {
  392. throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
  393. }
  394. $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
  395. if ($updateRes > 0) {
  396. return $this->get($id);
  397. } else {
  398. throw new RestException(405, $this->bom->error);
  399. }
  400. }
  401. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  402. /**
  403. * Clean sensible object datas
  404. *
  405. * @param Object $object Object to clean
  406. * @return Object Object with cleaned properties
  407. */
  408. protected function _cleanObjectDatas($object)
  409. {
  410. // phpcs:enable
  411. $object = parent::_cleanObjectDatas($object);
  412. unset($object->rowid);
  413. unset($object->canvas);
  414. unset($object->name);
  415. unset($object->lastname);
  416. unset($object->firstname);
  417. unset($object->civility_id);
  418. unset($object->statut);
  419. unset($object->state);
  420. unset($object->state_id);
  421. unset($object->state_code);
  422. unset($object->region);
  423. unset($object->region_code);
  424. unset($object->country);
  425. unset($object->country_id);
  426. unset($object->country_code);
  427. unset($object->barcode_type);
  428. unset($object->barcode_type_code);
  429. unset($object->barcode_type_label);
  430. unset($object->barcode_type_coder);
  431. unset($object->total_ht);
  432. unset($object->total_tva);
  433. unset($object->total_localtax1);
  434. unset($object->total_localtax2);
  435. unset($object->total_ttc);
  436. unset($object->fk_account);
  437. unset($object->comments);
  438. unset($object->note);
  439. unset($object->mode_reglement_id);
  440. unset($object->cond_reglement_id);
  441. unset($object->cond_reglement);
  442. unset($object->shipping_method_id);
  443. unset($object->fk_incoterms);
  444. unset($object->label_incoterms);
  445. unset($object->location_incoterms);
  446. // If object has lines, remove $db property
  447. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  448. $nboflines = count($object->lines);
  449. for ($i = 0; $i < $nboflines; $i++) {
  450. $this->_cleanObjectDatas($object->lines[$i]);
  451. unset($object->lines[$i]->lines);
  452. unset($object->lines[$i]->note);
  453. }
  454. }
  455. return $object;
  456. }
  457. /**
  458. * Validate fields before create or update object
  459. *
  460. * @param array $data Array of data to validate
  461. * @return array
  462. *
  463. * @throws RestException
  464. */
  465. private function _validate($data)
  466. {
  467. $myobject = array();
  468. foreach ($this->bom->fields as $field => $propfield) {
  469. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  470. continue; // Not a mandatory field
  471. }
  472. if (!isset($data[$field])) {
  473. throw new RestException(400, "$field field missing");
  474. }
  475. $myobject[$field] = $data[$field];
  476. }
  477. return $myobject;
  478. }
  479. /**
  480. * Validate the ref field and get the next Number if it's necessary.
  481. *
  482. * @return void
  483. */
  484. private function checkRefNumbering(): void
  485. {
  486. $ref = substr($this->bom->ref, 1, 4);
  487. if ($this->bom->status > 0 && $ref == 'PROV') {
  488. throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
  489. }
  490. if (strtolower($this->bom->ref) == 'auto') {
  491. if (empty($this->bom->id) && $this->bom->status == 0) {
  492. $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
  493. } else {
  494. $this->bom->fetch_product();
  495. $numref = $this->bom->getNextNumRef($this->bom->product);
  496. $this->bom->ref = $numref;
  497. }
  498. }
  499. }
  500. }