api_mos.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2019 Maxime Kohlhaas <maxime@atm-consulting.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
  20. /**
  21. * \file htdocs/mrp/class/api_mos.class.php
  22. * \ingroup mrp
  23. * \brief File for API management of MO.
  24. */
  25. /**
  26. * API class for MO
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class Mos extends DolibarrApi
  32. {
  33. /**
  34. * @var Mo $mo {@type Mo}
  35. */
  36. public $mo;
  37. /**
  38. * Constructor
  39. */
  40. public function __construct()
  41. {
  42. global $db, $conf;
  43. $this->db = $db;
  44. $this->mo = new Mo($this->db);
  45. }
  46. /**
  47. * Get properties of a MO object
  48. *
  49. * Return an array with MO informations
  50. *
  51. * @param int $id ID of MO
  52. * @return Object Object with cleaned properties
  53. *
  54. * @url GET {id}
  55. * @throws RestException
  56. */
  57. public function get($id)
  58. {
  59. if (!DolibarrApiAccess::$user->rights->mrp->read) {
  60. throw new RestException(401);
  61. }
  62. $result = $this->mo->fetch($id);
  63. if (!$result) {
  64. throw new RestException(404, 'MO not found');
  65. }
  66. if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
  67. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  68. }
  69. return $this->_cleanObjectDatas($this->mo);
  70. }
  71. /**
  72. * List Mos
  73. *
  74. * Get a list of MOs
  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 string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  81. * @return array Array of order objects
  82. *
  83. * @throws RestException
  84. */
  85. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  86. {
  87. global $db, $conf;
  88. if (!DolibarrApiAccess::$user->rights->mrp->read) {
  89. throw new RestException(401);
  90. }
  91. $obj_ret = array();
  92. $tmpobject = new Mo($this->db);
  93. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  94. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  95. // If the internal user must only see his customers, force searching by him
  96. $search_sale = 0;
  97. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  98. $search_sale = DolibarrApiAccess::$user->id;
  99. }
  100. $sql = "SELECT t.rowid";
  101. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  102. $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)
  103. }
  104. $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
  105. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  106. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  107. }
  108. $sql .= " WHERE 1 = 1";
  109. // Example of use $mode
  110. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  111. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  112. if ($tmpobject->ismultientitymanaged) {
  113. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  114. }
  115. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  116. $sql .= " AND t.fk_soc = sc.fk_soc";
  117. }
  118. if ($restrictonsocid && $socid) {
  119. $sql .= " AND t.fk_soc = ".((int) $socid);
  120. }
  121. if ($restrictonsocid && $search_sale > 0) {
  122. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  123. }
  124. // Insert sale filter
  125. if ($restrictonsocid && $search_sale > 0) {
  126. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  127. }
  128. if ($sqlfilters) {
  129. $errormessage = '';
  130. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  131. if ($errormessage) {
  132. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  133. }
  134. }
  135. $sql .= $this->db->order($sortfield, $sortorder);
  136. if ($limit) {
  137. if ($page < 0) {
  138. $page = 0;
  139. }
  140. $offset = $limit * $page;
  141. $sql .= $this->db->plimit($limit + 1, $offset);
  142. }
  143. $result = $this->db->query($sql);
  144. if ($result) {
  145. $num = $this->db->num_rows($result);
  146. $i = 0;
  147. while ($i < $num) {
  148. $obj = $this->db->fetch_object($result);
  149. $tmp_object = new Mo($this->db);
  150. if ($tmp_object->fetch($obj->rowid)) {
  151. $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
  152. }
  153. $i++;
  154. }
  155. } else {
  156. throw new RestException(503, 'Error when retrieve MO list');
  157. }
  158. if (!count($obj_ret)) {
  159. throw new RestException(404, 'No MO found');
  160. }
  161. return $obj_ret;
  162. }
  163. /**
  164. * Create MO object
  165. *
  166. * @param array $request_data Request datas
  167. * @return int ID of MO
  168. */
  169. public function post($request_data = null)
  170. {
  171. if (!DolibarrApiAccess::$user->rights->mrp->write) {
  172. throw new RestException(401);
  173. }
  174. // Check mandatory fields
  175. $result = $this->_validate($request_data);
  176. foreach ($request_data as $field => $value) {
  177. $this->mo->$field = $value;
  178. }
  179. $this->checkRefNumbering();
  180. if (!$this->mo->create(DolibarrApiAccess::$user)) {
  181. throw new RestException(500, "Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
  182. }
  183. return $this->mo->id;
  184. }
  185. /**
  186. * Update MO
  187. *
  188. * @param int $id Id of MO to update
  189. * @param array $request_data Datas
  190. *
  191. * @return int
  192. */
  193. public function put($id, $request_data = null)
  194. {
  195. if (!DolibarrApiAccess::$user->rights->mrp->write) {
  196. throw new RestException(401);
  197. }
  198. $result = $this->mo->fetch($id);
  199. if (!$result) {
  200. throw new RestException(404, 'MO not found');
  201. }
  202. if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
  203. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  204. }
  205. foreach ($request_data as $field => $value) {
  206. if ($field == 'id') {
  207. continue;
  208. }
  209. $this->mo->$field = $value;
  210. }
  211. $this->checkRefNumbering();
  212. if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
  213. return $this->get($id);
  214. } else {
  215. throw new RestException(500, $this->mo->error);
  216. }
  217. }
  218. /**
  219. * Delete MO
  220. *
  221. * @param int $id MO ID
  222. * @return array
  223. */
  224. public function delete($id)
  225. {
  226. if (!DolibarrApiAccess::$user->rights->mrp->delete) {
  227. throw new RestException(401);
  228. }
  229. $result = $this->mo->fetch($id);
  230. if (!$result) {
  231. throw new RestException(404, 'MO not found');
  232. }
  233. if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
  234. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  235. }
  236. if (!$this->mo->delete(DolibarrApiAccess::$user)) {
  237. throw new RestException(500, 'Error when deleting MO : '.$this->mo->error);
  238. }
  239. return array(
  240. 'success' => array(
  241. 'code' => 200,
  242. 'message' => 'MO deleted'
  243. )
  244. );
  245. }
  246. /**
  247. * Produce and consume
  248. *
  249. * Example:
  250. * {
  251. * "inventorylabel": "Produce and consume using API",
  252. * "inventorycode": "PRODUCEAPI-YY-MM-DD",
  253. * "autoclose": 1,
  254. * "arraytoconsume": [],
  255. * "arraytoproduce": []
  256. * }
  257. *
  258. * @param int $id ID of state
  259. * @param array $request_data Request datas
  260. *
  261. * @url POST {id}/produceandconsume
  262. *
  263. * @return int ID of MO
  264. */
  265. public function produceAndConsume($id, $request_data = null)
  266. {
  267. global $langs;
  268. $error = 0;
  269. if (!DolibarrApiAccess::$user->rights->mrp->write) {
  270. throw new RestException(401, 'Not enough permission');
  271. }
  272. $result = $this->mo->fetch($id);
  273. if (!$result) {
  274. throw new RestException(404, 'MO not found');
  275. }
  276. if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
  277. throw new RestException(401, 'Error bad status of MO');
  278. }
  279. $labelmovement = '';
  280. $codemovement = '';
  281. $autoclose = 1;
  282. $arraytoconsume = array();
  283. $arraytoproduce = array();
  284. foreach ($request_data as $field => $value) {
  285. if ($field == 'inventorylabel') {
  286. $labelmovement = $value;
  287. }
  288. if ($field == 'inventorycode') {
  289. $codemovement = $value;
  290. }
  291. if ($field == 'autoclose') {
  292. $autoclose = $value;
  293. }
  294. if ($field == 'arraytoconsume') {
  295. $arraytoconsume = $value;
  296. }
  297. if ($field == 'arraytoproduce') {
  298. $arraytoproduce = $value;
  299. }
  300. }
  301. if (empty($labelmovement)) {
  302. throw new RestException(500, "Field inventorylabel not provided");
  303. }
  304. if (empty($codemovement)) {
  305. throw new RestException(500, "Field inventorycode not provided");
  306. }
  307. // Code for consume and produce...
  308. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  309. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  310. dol_include_once('/mrp/lib/mrp_mo.lib.php');
  311. $stockmove = new MouvementStock($this->db);
  312. $consumptioncomplete = true;
  313. $productioncomplete = true;
  314. if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
  315. $pos = 0;
  316. $arrayofarrayname = array("arraytoconsume","arraytoproduce");
  317. foreach ($arrayofarrayname as $arrayname) {
  318. foreach ($arrayname as $value) {
  319. $tmpproduct = new Product($this->db);
  320. if (empty($value["objectid"])) {
  321. throw new RestException(500, "Field objectid required in ".$arrayname);
  322. }
  323. $tmpproduct->fetch($value["qty"]);
  324. if (empty($value["qty"])) {
  325. throw new RestException(500, "Field qty required in ".$arrayname);
  326. }
  327. if ($value["qty"]!=0) {
  328. $qtytoprocess = $value["qty"];
  329. if (isset($value["fk_warehouse"])) { // If there is a warehouse to set
  330. if (!($value["fk_warehouse"] > 0)) { // If there is no warehouse set.
  331. $error++;
  332. throw new RestException(500, "Field fk_warehouse must be > 0 in ".$arrayname);
  333. }
  334. if ($tmpproduct->status_batch) {
  335. $error++;
  336. throw new RestException(500, "Product ".$tmpproduct->ref."must be in batch");
  337. }
  338. }
  339. $idstockmove = 0;
  340. if (!$error && $value["fk_warehouse"] > 0) {
  341. // Record stock movement
  342. $id_product_batch = 0;
  343. $stockmove->setOrigin($this->mo->element, $this->mo->id);
  344. if ($qtytoprocess >= 0) {
  345. $moline = new MoLine($this->db);
  346. $moline->fk_mo = $this->mo->id;
  347. $moline->position = $pos;
  348. $moline->fk_product = $value["objectid"];
  349. $moline->fk_warehouse = $value["fk_warehouse"];
  350. $moline->qty = $qtytoprocess;
  351. $moline->batch = $tmpproduct->status_batch;
  352. $moline->role = 'toproduce';
  353. $moline->fk_mrp_production = "";
  354. $moline->fk_stock_movement = $idstockmove;
  355. $moline->fk_user_creat = DolibarrApiAccess::$user->id;
  356. $resultmoline = $moline->create(DolibarrApiAccess::$user);
  357. if ($resultmoline <= 0) {
  358. $error++;
  359. throw new RestException(500, $moline->error);
  360. }
  361. $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  362. } else {
  363. $moline = new MoLine($this->db);
  364. $moline->fk_mo = $this->mo->id;
  365. $moline->position = $pos;
  366. $moline->fk_product = $value["objectid"];
  367. $moline->fk_warehouse = $value["fk_warehouse"];
  368. $moline->qty = $qtytoprocess;
  369. $moline->batch = $tmpproduct->status_batch;
  370. $moline->role = 'toconsume';
  371. $moline->fk_mrp_production = "";
  372. $moline->fk_stock_movement = $idstockmove;
  373. $moline->fk_user_creat = DolibarrApiAccess::$user->id;
  374. $resultmoline = $moline->create(DolibarrApiAccess::$user);
  375. if ($resultmoline <= 0) {
  376. $error++;
  377. throw new RestException(500, $moline->error);
  378. }
  379. $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  380. }
  381. if ($idstockmove < 0) {
  382. $error++;
  383. throw new RestException(500, $stockmove->error);
  384. }
  385. }
  386. if (!$error) {
  387. // Record consumption
  388. $moline = new MoLine($this->db);
  389. $moline->fk_mo = $this->mo->id;
  390. $moline->position = $pos;
  391. $moline->fk_product = $value["objectid"];
  392. $moline->fk_warehouse = $value["fk_warehouse"];
  393. $moline->qty = $qtytoprocess;
  394. $moline->batch = $tmpproduct->status_batch;
  395. if ($arrayname == "arraytoconsume") {
  396. $moline->role = 'consumed';
  397. } else {
  398. $moline->role = 'produced';
  399. }
  400. $moline->fk_mrp_production = "";
  401. $moline->fk_stock_movement = $idstockmove;
  402. $moline->fk_user_creat = DolibarrApiAccess::$user->id;
  403. $resultmoline = $moline->create(DolibarrApiAccess::$user);
  404. if ($resultmoline <= 0) {
  405. $error++;
  406. throw new RestException(500, $moline->error);
  407. }
  408. $pos++;
  409. }
  410. }
  411. }
  412. }
  413. if (!$error) {
  414. if ($autoclose <= 0) {
  415. $consumptioncomplete = false;
  416. $productioncomplete = false;
  417. }
  418. }
  419. } else {
  420. $pos = 0;
  421. foreach ($this->mo->lines as $line) {
  422. if ($line->role == 'toconsume') {
  423. $tmpproduct = new Product($this->db);
  424. $tmpproduct->fetch($line->fk_product);
  425. if ($line->qty != 0) {
  426. $qtytoprocess = $line->qty;
  427. if (isset($line->fk_warehouse)) { // If there is a warehouse to set
  428. if (!($line->fk_warehouse > 0)) { // If there is no warehouse set.
  429. $langs->load("errors");
  430. $error++;
  431. throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref));
  432. }
  433. if ($tmpproduct->status_batch) {
  434. $langs->load("errors");
  435. $error++;
  436. throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref));
  437. }
  438. }
  439. $idstockmove = 0;
  440. if (!$error && $line->fk_warehouse > 0) {
  441. // Record stock movement
  442. $id_product_batch = 0;
  443. $stockmove->origin_type = 'mo';
  444. $stockmove->origin_id = $this->mo->id;
  445. if ($qtytoprocess >= 0) {
  446. $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  447. } else {
  448. $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  449. }
  450. if ($idstockmove < 0) {
  451. $error++;
  452. throw new RestException(500, $stockmove->error);
  453. }
  454. }
  455. if (!$error) {
  456. // Record consumption
  457. $moline = new MoLine($this->db);
  458. $moline->fk_mo = $this->mo->id;
  459. $moline->position = $pos;
  460. $moline->fk_product = $line->fk_product;
  461. $moline->fk_warehouse = $line->fk_warehouse;
  462. $moline->qty = $qtytoprocess;
  463. $moline->batch = $tmpproduct->status_batch;
  464. $moline->role = 'consumed';
  465. $moline->fk_mrp_production = $line->id;
  466. $moline->fk_stock_movement = $idstockmove;
  467. $moline->fk_user_creat = DolibarrApiAccess::$user->id;
  468. $resultmoline = $moline->create(DolibarrApiAccess::$user);
  469. if ($resultmoline <= 0) {
  470. $error++;
  471. throw new RestException(500, $moline->error);
  472. }
  473. $pos++;
  474. }
  475. }
  476. }
  477. }
  478. $pos = 0;
  479. foreach ($this->mo->lines as $line) {
  480. if ($line->role == 'toproduce') {
  481. $tmpproduct = new Product($this->db);
  482. $tmpproduct->fetch($line->fk_product);
  483. if ($line->qty != 0) {
  484. $qtytoprocess = $line->qty;
  485. if (isset($line->fk_warehouse)) { // If there is a warehouse to set
  486. if (!($line->fk_warehouse > 0)) { // If there is no warehouse set.
  487. $langs->load("errors");
  488. $error++;
  489. throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref));
  490. }
  491. if ($tmpproduct->status_batch) {
  492. $langs->load("errors");
  493. $error++;
  494. throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref));
  495. }
  496. }
  497. $idstockmove = 0;
  498. if (!$error && $line->fk_warehouse > 0) {
  499. // Record stock movement
  500. $id_product_batch = 0;
  501. $stockmove->origin_type = 'mo';
  502. $stockmove->origin_id = $this->mo->id;
  503. if ($qtytoprocess >= 0) {
  504. $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  505. } else {
  506. $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement);
  507. }
  508. if ($idstockmove < 0) {
  509. $error++;
  510. throw new RestException(500, $stockmove->error);
  511. }
  512. }
  513. if (!$error) {
  514. // Record consumption
  515. $moline = new MoLine($this->db);
  516. $moline->fk_mo = $this->mo->id;
  517. $moline->position = $pos;
  518. $moline->fk_product = $line->fk_product;
  519. $moline->fk_warehouse = $line->fk_warehouse;
  520. $moline->qty = $qtytoprocess;
  521. $moline->batch = $tmpproduct->status_batch;
  522. $moline->role = 'produced';
  523. $moline->fk_mrp_production = $line->id;
  524. $moline->fk_stock_movement = $idstockmove;
  525. $moline->fk_user_creat = DolibarrApiAccess::$user->id;
  526. $resultmoline = $moline->create(DolibarrApiAccess::$user);
  527. if ($resultmoline <= 0) {
  528. $error++;
  529. throw new RestException(500, $moline->error);
  530. }
  531. $pos++;
  532. }
  533. }
  534. }
  535. }
  536. if (!$error) {
  537. if ($autoclose > 0) {
  538. foreach ($this->mo->lines as $line) {
  539. if ($line->role == 'toconsume') {
  540. $arrayoflines = $this->mo->fetchLinesLinked('consumed', $line->id);
  541. $alreadyconsumed = 0;
  542. foreach ($arrayoflines as $line2) {
  543. $alreadyconsumed += $line2['qty'];
  544. }
  545. if ($alreadyconsumed < $line->qty) {
  546. $consumptioncomplete = false;
  547. }
  548. }
  549. if ($line->role == 'toproduce') {
  550. $arrayoflines = $this->mo->fetchLinesLinked('produced', $line->id);
  551. $alreadyproduced = 0;
  552. foreach ($arrayoflines as $line2) {
  553. $alreadyproduced += $line2['qty'];
  554. }
  555. if ($alreadyproduced < $line->qty) {
  556. $productioncomplete = false;
  557. }
  558. }
  559. }
  560. } else {
  561. $consumptioncomplete = false;
  562. $productioncomplete = false;
  563. }
  564. }
  565. }
  566. // Update status of MO
  567. dol_syslog("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete);
  568. //var_dump("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete);
  569. if ($consumptioncomplete && $productioncomplete) {
  570. $result = $this->mo->setStatut(self::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED');
  571. } else {
  572. $result = $this->mo->setStatut(self::STATUS_INPROGRESS, 0, '', 'MRP_MO_PRODUCED');
  573. }
  574. if ($result <= 0) {
  575. throw new RestException(500, $this->mo->error);
  576. }
  577. return $this->mo->id;
  578. }
  579. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  580. /**
  581. * Clean sensible object datas
  582. *
  583. * @param Object $object Object to clean
  584. * @return Object Object with cleaned properties
  585. */
  586. protected function _cleanObjectDatas($object)
  587. {
  588. // phpcs:enable
  589. $object = parent::_cleanObjectDatas($object);
  590. unset($object->rowid);
  591. unset($object->canvas);
  592. unset($object->name);
  593. unset($object->lastname);
  594. unset($object->firstname);
  595. unset($object->civility_id);
  596. unset($object->statut);
  597. unset($object->state);
  598. unset($object->state_id);
  599. unset($object->state_code);
  600. unset($object->region);
  601. unset($object->region_code);
  602. unset($object->country);
  603. unset($object->country_id);
  604. unset($object->country_code);
  605. unset($object->barcode_type);
  606. unset($object->barcode_type_code);
  607. unset($object->barcode_type_label);
  608. unset($object->barcode_type_coder);
  609. unset($object->total_ht);
  610. unset($object->total_tva);
  611. unset($object->total_localtax1);
  612. unset($object->total_localtax2);
  613. unset($object->total_ttc);
  614. unset($object->fk_account);
  615. unset($object->comments);
  616. unset($object->note);
  617. unset($object->mode_reglement_id);
  618. unset($object->cond_reglement_id);
  619. unset($object->cond_reglement);
  620. unset($object->shipping_method_id);
  621. unset($object->fk_incoterms);
  622. unset($object->label_incoterms);
  623. unset($object->location_incoterms);
  624. // If object has lines, remove $db property
  625. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  626. $nboflines = count($object->lines);
  627. for ($i = 0; $i < $nboflines; $i++) {
  628. $this->_cleanObjectDatas($object->lines[$i]);
  629. unset($object->lines[$i]->lines);
  630. unset($object->lines[$i]->note);
  631. }
  632. }
  633. return $object;
  634. }
  635. /**
  636. * Validate fields before create or update object
  637. *
  638. * @param array $data Array of data to validate
  639. * @return array
  640. *
  641. * @throws RestException
  642. */
  643. private function _validate($data)
  644. {
  645. $myobject = array();
  646. foreach ($this->mo->fields as $field => $propfield) {
  647. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  648. continue; // Not a mandatory field
  649. }
  650. if (!isset($data[$field])) {
  651. throw new RestException(400, "$field field missing");
  652. }
  653. $myobject[$field] = $data[$field];
  654. }
  655. return $myobject;
  656. }
  657. /**
  658. * Validate the ref field and get the next Number if it's necessary.
  659. *
  660. * @return void
  661. */
  662. private function checkRefNumbering(): void
  663. {
  664. $ref = substr($this->mo->ref, 1, 4);
  665. if ($this->mo->status > 0 && $ref == 'PROV') {
  666. throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
  667. }
  668. if (strtolower($this->mo->ref) == 'auto') {
  669. if (empty($this->mo->id) && $this->mo->status == 0) {
  670. $this->mo->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
  671. } else {
  672. $this->mo->fetch_product();
  673. $numref = $this->mo->getNextNumRef($this->mo->product);
  674. $this->mo->ref = $numref;
  675. }
  676. }
  677. }
  678. }