api_boms.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * @return array Array of order objects
  84. *
  85. * @throws RestException
  86. */
  87. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  88. {
  89. global $db, $conf;
  90. if (!DolibarrApiAccess::$user->rights->bom->read) {
  91. throw new RestException(401);
  92. }
  93. $obj_ret = array();
  94. $tmpobject = new BOM($this->db);
  95. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  96. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  97. // If the internal user must only see his customers, force searching by him
  98. $search_sale = 0;
  99. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  100. $search_sale = DolibarrApiAccess::$user->id;
  101. }
  102. $sql = "SELECT t.rowid";
  103. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  104. $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)
  105. }
  106. $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
  107. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  108. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  109. }
  110. $sql .= " WHERE 1 = 1";
  111. // Example of use $mode
  112. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  113. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  114. if ($tmpobject->ismultientitymanaged) {
  115. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  116. }
  117. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  118. $sql .= " AND t.fk_soc = sc.fk_soc";
  119. }
  120. if ($restrictonsocid && $socid) {
  121. $sql .= " AND t.fk_soc = ".((int) $socid);
  122. }
  123. if ($restrictonsocid && $search_sale > 0) {
  124. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  125. }
  126. // Insert sale filter
  127. if ($restrictonsocid && $search_sale > 0) {
  128. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  129. }
  130. if ($sqlfilters) {
  131. $errormessage = '';
  132. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  133. if ($errormessage) {
  134. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  135. }
  136. }
  137. $sql .= $this->db->order($sortfield, $sortorder);
  138. if ($limit) {
  139. if ($page < 0) {
  140. $page = 0;
  141. }
  142. $offset = $limit * $page;
  143. $sql .= $this->db->plimit($limit + 1, $offset);
  144. }
  145. $result = $this->db->query($sql);
  146. if ($result) {
  147. $num = $this->db->num_rows($result);
  148. $i = 0;
  149. while ($i < $num) {
  150. $obj = $this->db->fetch_object($result);
  151. $bom_static = new BOM($this->db);
  152. if ($bom_static->fetch($obj->rowid)) {
  153. $obj_ret[] = $this->_cleanObjectDatas($bom_static);
  154. }
  155. $i++;
  156. }
  157. } else {
  158. throw new RestException(503, 'Error when retrieve bom list');
  159. }
  160. if (!count($obj_ret)) {
  161. throw new RestException(404, 'No bom found');
  162. }
  163. return $obj_ret;
  164. }
  165. /**
  166. * Create bom object
  167. *
  168. * @param array $request_data Request datas
  169. * @return int ID of bom
  170. */
  171. public function post($request_data = null)
  172. {
  173. if (!DolibarrApiAccess::$user->rights->bom->write) {
  174. throw new RestException(401);
  175. }
  176. // Check mandatory fields
  177. $result = $this->_validate($request_data);
  178. foreach ($request_data as $field => $value) {
  179. $this->bom->$field = $value;
  180. }
  181. $this->checkRefNumbering();
  182. if (!$this->bom->create(DolibarrApiAccess::$user)) {
  183. throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
  184. }
  185. return $this->bom->id;
  186. }
  187. /**
  188. * Update bom
  189. *
  190. * @param int $id Id of bom to update
  191. * @param array $request_data Datas
  192. *
  193. * @return int
  194. */
  195. public function put($id, $request_data = null)
  196. {
  197. if (!DolibarrApiAccess::$user->rights->bom->write) {
  198. throw new RestException(401);
  199. }
  200. $result = $this->bom->fetch($id);
  201. if (!$result) {
  202. throw new RestException(404, 'BOM not found');
  203. }
  204. if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
  205. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  206. }
  207. foreach ($request_data as $field => $value) {
  208. if ($field == 'id') {
  209. continue;
  210. }
  211. $this->bom->$field = $value;
  212. }
  213. $this->checkRefNumbering();
  214. if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
  215. return $this->get($id);
  216. } else {
  217. throw new RestException(500, $this->bom->error);
  218. }
  219. }
  220. /**
  221. * Delete bom
  222. *
  223. * @param int $id BOM ID
  224. * @return array
  225. */
  226. public function delete($id)
  227. {
  228. if (!DolibarrApiAccess::$user->rights->bom->delete) {
  229. throw new RestException(401);
  230. }
  231. $result = $this->bom->fetch($id);
  232. if (!$result) {
  233. throw new RestException(404, 'BOM not found');
  234. }
  235. if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
  236. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  237. }
  238. if (!$this->bom->delete(DolibarrApiAccess::$user)) {
  239. throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
  240. }
  241. return array(
  242. 'success' => array(
  243. 'code' => 200,
  244. 'message' => 'BOM deleted'
  245. )
  246. );
  247. }
  248. /**
  249. * Get lines of an BOM
  250. *
  251. * @param int $id Id of BOM
  252. *
  253. * @url GET {id}/lines
  254. *
  255. * @return array
  256. */
  257. public function getLines($id)
  258. {
  259. if (!DolibarrApiAccess::$user->rights->bom->read) {
  260. throw new RestException(401);
  261. }
  262. $result = $this->bom->fetch($id);
  263. if (!$result) {
  264. throw new RestException(404, 'BOM not found');
  265. }
  266. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  267. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  268. }
  269. $this->bom->getLinesArray();
  270. $result = array();
  271. foreach ($this->bom->lines as $line) {
  272. array_push($result, $this->_cleanObjectDatas($line));
  273. }
  274. return $result;
  275. }
  276. /**
  277. * Add a line to given BOM
  278. *
  279. * @param int $id Id of BOM to update
  280. * @param array $request_data BOMLine data
  281. *
  282. * @url POST {id}/lines
  283. *
  284. * @return int
  285. */
  286. public function postLine($id, $request_data = null)
  287. {
  288. if (!DolibarrApiAccess::$user->rights->bom->write) {
  289. throw new RestException(401);
  290. }
  291. $result = $this->bom->fetch($id);
  292. if (!$result) {
  293. throw new RestException(404, 'BOM not found');
  294. }
  295. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  296. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  297. }
  298. $request_data = (object) $request_data;
  299. $updateRes = $this->bom->addLine(
  300. $request_data->fk_product,
  301. $request_data->qty,
  302. $request_data->qty_frozen,
  303. $request_data->disable_stock_change,
  304. $request_data->efficiency,
  305. $request_data->position,
  306. $request_data->fk_bom_child,
  307. $request_data->import_key
  308. );
  309. if ($updateRes > 0) {
  310. return $updateRes;
  311. } else {
  312. throw new RestException(400, $this->bom->error);
  313. }
  314. }
  315. /**
  316. * Update a line to given BOM
  317. *
  318. * @param int $id Id of BOM to update
  319. * @param int $lineid Id of line to update
  320. * @param array $request_data BOMLine data
  321. *
  322. * @url PUT {id}/lines/{lineid}
  323. *
  324. * @return object|bool
  325. */
  326. public function putLine($id, $lineid, $request_data = null)
  327. {
  328. if (!DolibarrApiAccess::$user->rights->bom->write) {
  329. throw new RestException(401);
  330. }
  331. $result = $this->bom->fetch($id);
  332. if (!$result) {
  333. throw new RestException(404, 'BOM not found');
  334. }
  335. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  336. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  337. }
  338. $request_data = (object) $request_data;
  339. $updateRes = $this->bom->updateLine(
  340. $lineid,
  341. $request_data->qty,
  342. $request_data->qty_frozen,
  343. $request_data->disable_stock_change,
  344. $request_data->efficiency,
  345. $request_data->position,
  346. $request_data->import_key
  347. );
  348. if ($updateRes > 0) {
  349. $result = $this->get($id);
  350. unset($result->line);
  351. return $this->_cleanObjectDatas($result);
  352. }
  353. return false;
  354. }
  355. /**
  356. * Delete a line to given BOM
  357. *
  358. *
  359. * @param int $id Id of BOM to update
  360. * @param int $lineid Id of line to delete
  361. *
  362. * @url DELETE {id}/lines/{lineid}
  363. *
  364. * @return int
  365. *
  366. * @throws RestException 401
  367. * @throws RestException 404
  368. * @throws RestException 500
  369. */
  370. public function deleteLine($id, $lineid)
  371. {
  372. if (!DolibarrApiAccess::$user->rights->bom->write) {
  373. throw new RestException(401);
  374. }
  375. $result = $this->bom->fetch($id);
  376. if (!$result) {
  377. throw new RestException(404, 'BOM not found');
  378. }
  379. if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
  380. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  381. }
  382. //Check the rowid is a line of current bom object
  383. $lineIdIsFromObject = false;
  384. foreach ($this->bom->lines as $bl) {
  385. if ($bl->id == $lineid) {
  386. $lineIdIsFromObject = true;
  387. break;
  388. }
  389. }
  390. if (!$lineIdIsFromObject) {
  391. throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
  392. }
  393. $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
  394. if ($updateRes > 0) {
  395. return $this->get($id);
  396. } else {
  397. throw new RestException(405, $this->bom->error);
  398. }
  399. }
  400. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  401. /**
  402. * Clean sensible object datas
  403. *
  404. * @param Object $object Object to clean
  405. * @return Object Object with cleaned properties
  406. */
  407. protected function _cleanObjectDatas($object)
  408. {
  409. // phpcs:enable
  410. $object = parent::_cleanObjectDatas($object);
  411. unset($object->rowid);
  412. unset($object->canvas);
  413. unset($object->name);
  414. unset($object->lastname);
  415. unset($object->firstname);
  416. unset($object->civility_id);
  417. unset($object->statut);
  418. unset($object->state);
  419. unset($object->state_id);
  420. unset($object->state_code);
  421. unset($object->region);
  422. unset($object->region_code);
  423. unset($object->country);
  424. unset($object->country_id);
  425. unset($object->country_code);
  426. unset($object->barcode_type);
  427. unset($object->barcode_type_code);
  428. unset($object->barcode_type_label);
  429. unset($object->barcode_type_coder);
  430. unset($object->total_ht);
  431. unset($object->total_tva);
  432. unset($object->total_localtax1);
  433. unset($object->total_localtax2);
  434. unset($object->total_ttc);
  435. unset($object->fk_account);
  436. unset($object->comments);
  437. unset($object->note);
  438. unset($object->mode_reglement_id);
  439. unset($object->cond_reglement_id);
  440. unset($object->cond_reglement);
  441. unset($object->shipping_method_id);
  442. unset($object->fk_incoterms);
  443. unset($object->label_incoterms);
  444. unset($object->location_incoterms);
  445. // If object has lines, remove $db property
  446. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  447. $nboflines = count($object->lines);
  448. for ($i = 0; $i < $nboflines; $i++) {
  449. $this->_cleanObjectDatas($object->lines[$i]);
  450. unset($object->lines[$i]->lines);
  451. unset($object->lines[$i]->note);
  452. }
  453. }
  454. return $object;
  455. }
  456. /**
  457. * Validate fields before create or update object
  458. *
  459. * @param array $data Array of data to validate
  460. * @return array
  461. *
  462. * @throws RestException
  463. */
  464. private function _validate($data)
  465. {
  466. $myobject = array();
  467. foreach ($this->bom->fields as $field => $propfield) {
  468. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  469. continue; // Not a mandatory field
  470. }
  471. if (!isset($data[$field])) {
  472. throw new RestException(400, "$field field missing");
  473. }
  474. $myobject[$field] = $data[$field];
  475. }
  476. return $myobject;
  477. }
  478. /**
  479. * Validate the ref field and get the next Number if it's necessary.
  480. *
  481. * @return void
  482. */
  483. private function checkRefNumbering(): void
  484. {
  485. $ref = substr($this->bom->ref, 1, 4);
  486. if ($this->bom->status > 0 && $ref == 'PROV') {
  487. throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
  488. }
  489. if (strtolower($this->bom->ref) == 'auto') {
  490. if (empty($this->bom->id) && $this->bom->status == 0) {
  491. $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
  492. } else {
  493. $this->bom->fetch_product();
  494. $numref = $this->bom->getNextNumRef($this->bom->product);
  495. $this->bom->ref = $numref;
  496. }
  497. }
  498. }
  499. }