| Total Complexity | 172 |
| Total Lines | 952 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Mos often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mos, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Mos extends DolibarrApi |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var Mo $mo {@type Mo} |
||
| 48 | */ |
||
| 49 | public $mo; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | */ |
||
| 54 | public function __construct() |
||
| 55 | { |
||
| 56 | global $db, $conf; |
||
| 57 | $this->db = $db; |
||
| 58 | $this->mo = new Mo($this->db); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get properties of a MO object |
||
| 63 | * |
||
| 64 | * Return an array with MO information |
||
| 65 | * |
||
| 66 | * @param int $id ID of MO |
||
| 67 | * @return array Object with cleaned properties |
||
| 68 | * |
||
| 69 | * @url GET {id} |
||
| 70 | * @throws RestException |
||
| 71 | */ |
||
| 72 | public function get($id) |
||
| 73 | { |
||
| 74 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'read')) { |
||
| 75 | throw new RestException(403); |
||
| 76 | } |
||
| 77 | |||
| 78 | $result = $this->mo->fetch($id); |
||
| 79 | if (!$result) { |
||
| 80 | throw new RestException(404, 'MO not found'); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) { |
||
| 84 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $this->_cleanObjectDatas($this->mo); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * List Mos |
||
| 93 | * |
||
| 94 | * Get a list of MOs |
||
| 95 | * |
||
| 96 | * @param string $sortfield Sort field |
||
| 97 | * @param string $sortorder Sort order |
||
| 98 | * @param int $limit Limit for list |
||
| 99 | * @param int $page Page number |
||
| 100 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 101 | * @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
||
| 102 | * @return array Array of order objects |
||
| 103 | * |
||
| 104 | * @throws RestException |
||
| 105 | */ |
||
| 106 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '') |
||
| 107 | { |
||
| 108 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'read')) { |
||
| 109 | throw new RestException(403); |
||
| 110 | } |
||
| 111 | |||
| 112 | $obj_ret = array(); |
||
| 113 | $tmpobject = new Mo($this->db); |
||
| 114 | |||
| 115 | $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0; |
||
| 116 | |||
| 117 | $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object |
||
| 118 | |||
| 119 | // If the internal user must only see his customers, force searching by him |
||
| 120 | $search_sale = 0; |
||
| 121 | if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) { |
||
| 122 | $search_sale = DolibarrApiAccess::$user->id; |
||
| 123 | } |
||
| 124 | |||
| 125 | $sql = "SELECT t.rowid"; |
||
| 126 | $sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject->table_element . " AS t"; |
||
| 127 | $sql .= " 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 |
||
| 128 | $sql .= " WHERE 1 = 1"; |
||
| 129 | if ($tmpobject->ismultientitymanaged) { |
||
| 130 | $sql .= ' AND t.entity IN (' . getEntity($tmpobject->element) . ')'; |
||
| 131 | } |
||
| 132 | if ($restrictonsocid && $socid) { |
||
| 133 | $sql .= " AND t.fk_soc = " . ((int) $socid); |
||
| 134 | } |
||
| 135 | // Search on sale representative |
||
| 136 | if ($search_sale && $search_sale != '-1') { |
||
| 137 | if ($search_sale == -2) { |
||
| 138 | $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
||
| 139 | } elseif ($search_sale > 0) { |
||
| 140 | $sql .= " AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = " . ((int) $search_sale) . ")"; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | if ($sqlfilters) { |
||
| 144 | $errormessage = ''; |
||
| 145 | $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
||
| 146 | if ($errormessage) { |
||
| 147 | throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 152 | if ($limit) { |
||
| 153 | if ($page < 0) { |
||
| 154 | $page = 0; |
||
| 155 | } |
||
| 156 | $offset = $limit * $page; |
||
| 157 | |||
| 158 | $sql .= $this->db->plimit($limit + 1, $offset); |
||
| 159 | } |
||
| 160 | |||
| 161 | $result = $this->db->query($sql); |
||
| 162 | if ($result) { |
||
| 163 | $num = $this->db->num_rows($result); |
||
| 164 | $i = 0; |
||
| 165 | while ($i < $num) { |
||
| 166 | $obj = $this->db->fetch_object($result); |
||
| 167 | $tmp_object = new Mo($this->db); |
||
| 168 | if ($tmp_object->fetch($obj->rowid)) { |
||
| 169 | $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties); |
||
| 170 | } |
||
| 171 | $i++; |
||
| 172 | } |
||
| 173 | } else { |
||
| 174 | throw new RestException(503, 'Error when retrieve MO list'); |
||
| 175 | } |
||
| 176 | |||
| 177 | return $obj_ret; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Create MO object |
||
| 182 | * |
||
| 183 | * @param array $request_data Request datas |
||
| 184 | * @return int ID of MO |
||
| 185 | */ |
||
| 186 | public function post($request_data = null) |
||
| 187 | { |
||
| 188 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'write')) { |
||
| 189 | throw new RestException(403); |
||
| 190 | } |
||
| 191 | // Check mandatory fields |
||
| 192 | $result = $this->_validate($request_data); |
||
| 193 | |||
| 194 | foreach ($request_data as $field => $value) { |
||
| 195 | if ($field === 'caller') { |
||
| 196 | // 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 with the caller |
||
| 197 | $this->mo->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | $this->mo->$field = $this->_checkValForAPI($field, $value, $this->mo); |
||
| 202 | } |
||
| 203 | |||
| 204 | $this->checkRefNumbering(); |
||
| 205 | |||
| 206 | if (!$this->mo->create(DolibarrApiAccess::$user)) { |
||
| 207 | throw new RestException(500, "Error creating MO", array_merge(array($this->mo->error), $this->mo->errors)); |
||
| 208 | } |
||
| 209 | return $this->mo->id; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Update MO |
||
| 214 | * |
||
| 215 | * @param int $id Id of MO to update |
||
| 216 | * @param array $request_data Datas |
||
| 217 | * @return array Updated object |
||
| 218 | */ |
||
| 219 | public function put($id, $request_data = null) |
||
| 220 | { |
||
| 221 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'write')) { |
||
| 222 | throw new RestException(403); |
||
| 223 | } |
||
| 224 | |||
| 225 | $result = $this->mo->fetch($id); |
||
| 226 | if (!$result) { |
||
| 227 | throw new RestException(404, 'MO not found'); |
||
| 228 | } |
||
| 229 | |||
| 230 | if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) { |
||
| 231 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 232 | } |
||
| 233 | |||
| 234 | foreach ($request_data as $field => $value) { |
||
| 235 | if ($field == 'id') { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | if ($field === 'caller') { |
||
| 239 | // 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 with the caller |
||
| 240 | $this->mo->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->mo->$field = $this->_checkValForAPI($field, $value, $this->mo); |
||
| 245 | } |
||
| 246 | |||
| 247 | $this->checkRefNumbering(); |
||
| 248 | |||
| 249 | if ($this->mo->update(DolibarrApiAccess::$user) > 0) { |
||
| 250 | return $this->get($id); |
||
| 251 | } else { |
||
| 252 | throw new RestException(500, $this->mo->error); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Delete MO |
||
| 258 | * |
||
| 259 | * @param int $id MO ID |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function delete($id) |
||
| 263 | { |
||
| 264 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'delete')) { |
||
| 265 | throw new RestException(403); |
||
| 266 | } |
||
| 267 | $result = $this->mo->fetch($id); |
||
| 268 | if (!$result) { |
||
| 269 | throw new RestException(404, 'MO not found'); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) { |
||
| 273 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (!$this->mo->delete(DolibarrApiAccess::$user)) { |
||
| 277 | throw new RestException(500, 'Error when deleting MO : ' . $this->mo->error); |
||
| 278 | } |
||
| 279 | |||
| 280 | return array( |
||
| 281 | 'success' => array( |
||
| 282 | 'code' => 200, |
||
| 283 | 'message' => 'MO deleted' |
||
| 284 | ) |
||
| 285 | ); |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Produce and consume all |
||
| 291 | * |
||
| 292 | * - If arraytoconsume and arraytoproduce are both filled, this fill an empty MO with the lines to consume and produce and record the consumption and production. |
||
| 293 | * - If arraytoconsume and arraytoproduce are not provided, it consumes and produces all existing lines. |
||
| 294 | * |
||
| 295 | * Example: |
||
| 296 | * { |
||
| 297 | * "inventorylabel": "Produce and consume using API", |
||
| 298 | * "inventorycode": "PRODUCEAPI-YY-MM-DD", |
||
| 299 | * "autoclose": 1, |
||
| 300 | * "arraytoconsume": [ |
||
| 301 | * "objectid": 123, -- ID_of_product |
||
| 302 | * "qty": "2", |
||
| 303 | * "fk_warehouse": "789" |
||
| 304 | * ], |
||
| 305 | * "arraytoproduce": [ |
||
| 306 | * "objectid": 456, -- ID_of_product |
||
| 307 | * "qty": "1", |
||
| 308 | * "fk_warehouse": "789" |
||
| 309 | * ] |
||
| 310 | * } |
||
| 311 | * |
||
| 312 | * @param int $id ID of state |
||
| 313 | * @param array $request_data Request datas |
||
| 314 | * |
||
| 315 | * @url POST {id}/produceandconsumeall |
||
| 316 | * |
||
| 317 | * @return int ID of MO |
||
| 318 | */ |
||
| 319 | public function produceAndConsumeAll($id, $request_data = null) |
||
| 320 | { |
||
| 321 | global $langs; |
||
| 322 | |||
| 323 | $error = 0; |
||
| 324 | |||
| 325 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'write')) { |
||
| 326 | throw new RestException(403, 'Not enough permission'); |
||
| 327 | } |
||
| 328 | $result = $this->mo->fetch($id); |
||
| 329 | if (!$result) { |
||
| 330 | throw new RestException(404, 'MO not found'); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) { |
||
| 334 | throw new RestException(405, 'Error bad status of MO'); |
||
| 335 | } |
||
| 336 | |||
| 337 | // Code for consume and produce... |
||
| 338 | require_once constant('DOL_DOCUMENT_ROOT') . '/mrp/lib/mrp_mo.lib.php'; |
||
| 339 | |||
| 340 | $stockmove = new MouvementStock($this->db); |
||
| 341 | |||
| 342 | $labelmovement = ''; |
||
| 343 | $codemovement = ''; |
||
| 344 | $autoclose = 1; |
||
| 345 | $arraytoconsume = array(); |
||
| 346 | $arraytoproduce = array(); |
||
| 347 | |||
| 348 | foreach ($request_data as $field => $value) { |
||
| 349 | if ($field == 'inventorylabel') { |
||
| 350 | $labelmovement = $value; |
||
| 351 | } |
||
| 352 | if ($field == 'inventorycode') { |
||
| 353 | $codemovement = $value; |
||
| 354 | } |
||
| 355 | if ($field == 'autoclose') { |
||
| 356 | $autoclose = $value; |
||
| 357 | } |
||
| 358 | if ($field == 'arraytoconsume') { |
||
| 359 | $arraytoconsume = $value; |
||
| 360 | } |
||
| 361 | if ($field == 'arraytoproduce') { |
||
| 362 | $arraytoproduce = $value; |
||
| 363 | } |
||
| 364 | if ($field === 'caller') { |
||
| 365 | // 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 with the caller |
||
| 366 | $stockmove->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 367 | continue; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | if (empty($labelmovement)) { |
||
| 372 | throw new RestException(500, "Field inventorylabel not provided"); |
||
| 373 | } |
||
| 374 | if (empty($codemovement)) { |
||
| 375 | throw new RestException(500, "Field inventorycode not provided"); |
||
| 376 | } |
||
| 377 | |||
| 378 | $consumptioncomplete = true; |
||
| 379 | $productioncomplete = true; |
||
| 380 | |||
| 381 | if (!empty($arraytoconsume) && !empty($arraytoproduce)) { |
||
| 382 | $pos = 0; |
||
| 383 | $arrayofarrayname = array("arraytoconsume","arraytoproduce"); |
||
| 384 | foreach ($arrayofarrayname as $arrayname) { |
||
| 385 | foreach (${$arrayname} as $value) { |
||
| 386 | $tmpproduct = new Product($this->db); |
||
| 387 | if (empty($value["objectid"])) { |
||
| 388 | throw new RestException(500, "Field objectid required in " . $arrayname); |
||
| 389 | } |
||
| 390 | $tmpproduct->fetch($value["qty"]); |
||
| 391 | if (empty($value["qty"])) { |
||
| 392 | throw new RestException(500, "Field qty required in " . $arrayname); |
||
| 393 | } |
||
| 394 | if ($value["qty"] != 0) { |
||
| 395 | $qtytoprocess = $value["qty"]; |
||
| 396 | if (isset($value["fk_warehouse"])) { // If there is a warehouse to set |
||
| 397 | if (!($value["fk_warehouse"] > 0)) { // If there is no warehouse set. |
||
| 398 | $error++; |
||
| 399 | throw new RestException(500, "Field fk_warehouse must be > 0 in " . $arrayname); |
||
| 400 | } |
||
| 401 | if ($tmpproduct->status_batch) { |
||
| 402 | $error++; |
||
| 403 | throw new RestException(500, "Product " . $tmpproduct->ref . "must be in batch"); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | $idstockmove = 0; |
||
| 407 | if (!$error && $value["fk_warehouse"] > 0) { |
||
| 408 | // Record consumption to do and stock movement |
||
| 409 | $id_product_batch = 0; |
||
| 410 | |||
| 411 | $stockmove->setOrigin($this->mo->element, $this->mo->id); |
||
| 412 | |||
| 413 | if ($arrayname == 'arraytoconsume') { |
||
| 414 | $moline = new MoLine($this->db); |
||
| 415 | $moline->fk_mo = $this->mo->id; |
||
| 416 | $moline->position = $pos; |
||
| 417 | $moline->fk_product = $value["objectid"]; |
||
| 418 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
| 419 | $moline->qty = $qtytoprocess; |
||
| 420 | $moline->batch = $tmpproduct->status_batch; |
||
| 421 | $moline->role = 'toproduce'; |
||
| 422 | $moline->fk_mrp_production = ""; |
||
| 423 | $moline->fk_stock_movement = $idstockmove; |
||
| 424 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 425 | |||
| 426 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 427 | if ($resultmoline <= 0) { |
||
| 428 | $error++; |
||
| 429 | throw new RestException(500, $moline->error); |
||
| 430 | } |
||
| 431 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 432 | } else { |
||
| 433 | $moline = new MoLine($this->db); |
||
| 434 | $moline->fk_mo = $this->mo->id; |
||
| 435 | $moline->position = $pos; |
||
| 436 | $moline->fk_product = $value["objectid"]; |
||
| 437 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
| 438 | $moline->qty = $qtytoprocess; |
||
| 439 | $moline->batch = $tmpproduct->status_batch; |
||
| 440 | $moline->role = 'toconsume'; |
||
| 441 | $moline->fk_mrp_production = ""; |
||
| 442 | $moline->fk_stock_movement = $idstockmove; |
||
| 443 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 444 | |||
| 445 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 446 | if ($resultmoline <= 0) { |
||
| 447 | $error++; |
||
| 448 | throw new RestException(500, $moline->error); |
||
| 449 | } |
||
| 450 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 451 | } |
||
| 452 | if ($idstockmove < 0) { |
||
| 453 | $error++; |
||
| 454 | throw new RestException(500, $stockmove->error); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | if (!$error) { |
||
| 458 | // Record consumption done |
||
| 459 | $moline = new MoLine($this->db); |
||
| 460 | $moline->fk_mo = $this->mo->id; |
||
| 461 | $moline->position = $pos; |
||
| 462 | $moline->fk_product = $value["objectid"]; |
||
| 463 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
| 464 | $moline->qty = $qtytoprocess; |
||
| 465 | $moline->batch = $tmpproduct->status_batch; |
||
| 466 | if ($arrayname == "arraytoconsume") { |
||
| 467 | $moline->role = 'consumed'; |
||
| 468 | } else { |
||
| 469 | $moline->role = 'produced'; |
||
| 470 | } |
||
| 471 | $moline->fk_mrp_production = ""; |
||
| 472 | $moline->fk_stock_movement = $idstockmove; |
||
| 473 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 474 | |||
| 475 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 476 | if ($resultmoline <= 0) { |
||
| 477 | $error++; |
||
| 478 | throw new RestException(500, $moline->error); |
||
| 479 | } |
||
| 480 | |||
| 481 | $pos++; |
||
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 | } |
||
| 486 | if (!$error) { |
||
| 487 | if ($autoclose <= 0) { |
||
| 488 | $consumptioncomplete = false; |
||
| 489 | $productioncomplete = false; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | } else { |
||
| 493 | $pos = 0; |
||
| 494 | foreach ($this->mo->lines as $line) { |
||
| 495 | if ($line->role == 'toconsume') { |
||
| 496 | $tmpproduct = new Product($this->db); |
||
| 497 | $tmpproduct->fetch($line->fk_product); |
||
| 498 | if ($line->qty != 0) { |
||
| 499 | $qtytoprocess = $line->qty; |
||
| 500 | if (isset($line->fk_warehouse)) { // If there is a warehouse to set |
||
| 501 | if (!($line->fk_warehouse > 0)) { // If there is no warehouse set. |
||
| 502 | $langs->load("errors"); |
||
| 503 | $error++; |
||
| 504 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref)); |
||
| 505 | } |
||
| 506 | if ($tmpproduct->status_batch) { |
||
| 507 | $langs->load("errors"); |
||
| 508 | $error++; |
||
| 509 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref)); |
||
| 510 | } |
||
| 511 | } |
||
| 512 | $idstockmove = 0; |
||
| 513 | if (!$error && $line->fk_warehouse > 0) { |
||
| 514 | // Record stock movement |
||
| 515 | $id_product_batch = 0; |
||
| 516 | $stockmove->origin_type = 'mo'; |
||
| 517 | $stockmove->origin_id = $this->mo->id; |
||
| 518 | if ($qtytoprocess >= 0) { |
||
| 519 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 520 | } else { |
||
| 521 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 522 | } |
||
| 523 | if ($idstockmove < 0) { |
||
| 524 | $error++; |
||
| 525 | throw new RestException(500, $stockmove->error); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | if (!$error) { |
||
| 529 | // Record consumption |
||
| 530 | $moline = new MoLine($this->db); |
||
| 531 | $moline->fk_mo = $this->mo->id; |
||
| 532 | $moline->position = $pos; |
||
| 533 | $moline->fk_product = $line->fk_product; |
||
| 534 | $moline->fk_warehouse = $line->fk_warehouse; |
||
| 535 | $moline->qty = $qtytoprocess; |
||
| 536 | $moline->batch = $tmpproduct->status_batch; |
||
| 537 | $moline->role = 'consumed'; |
||
| 538 | $moline->fk_mrp_production = $line->id; |
||
| 539 | $moline->fk_stock_movement = $idstockmove; |
||
| 540 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 541 | |||
| 542 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 543 | if ($resultmoline <= 0) { |
||
| 544 | $error++; |
||
| 545 | throw new RestException(500, $moline->error); |
||
| 546 | } |
||
| 547 | |||
| 548 | $pos++; |
||
| 549 | } |
||
| 550 | } |
||
| 551 | } |
||
| 552 | } |
||
| 553 | $pos = 0; |
||
| 554 | foreach ($this->mo->lines as $line) { |
||
| 555 | if ($line->role == 'toproduce') { |
||
| 556 | $tmpproduct = new Product($this->db); |
||
| 557 | $tmpproduct->fetch($line->fk_product); |
||
| 558 | if ($line->qty != 0) { |
||
| 559 | $qtytoprocess = $line->qty; |
||
| 560 | if (isset($line->fk_warehouse)) { // If there is a warehouse to set |
||
| 561 | if (!($line->fk_warehouse > 0)) { // If there is no warehouse set. |
||
| 562 | $langs->load("errors"); |
||
| 563 | $error++; |
||
| 564 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref)); |
||
| 565 | } |
||
| 566 | if ($tmpproduct->status_batch) { |
||
| 567 | $langs->load("errors"); |
||
| 568 | $error++; |
||
| 569 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref)); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | $idstockmove = 0; |
||
| 573 | if (!$error && $line->fk_warehouse > 0) { |
||
| 574 | // Record stock movement |
||
| 575 | $id_product_batch = 0; |
||
| 576 | $stockmove->origin_type = 'mo'; |
||
| 577 | $stockmove->origin_id = $this->mo->id; |
||
| 578 | if ($qtytoprocess >= 0) { |
||
| 579 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 580 | } else { |
||
| 581 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 582 | } |
||
| 583 | if ($idstockmove < 0) { |
||
| 584 | $error++; |
||
| 585 | throw new RestException(500, $stockmove->error); |
||
| 586 | } |
||
| 587 | } |
||
| 588 | if (!$error) { |
||
| 589 | // Record consumption |
||
| 590 | $moline = new MoLine($this->db); |
||
| 591 | $moline->fk_mo = $this->mo->id; |
||
| 592 | $moline->position = $pos; |
||
| 593 | $moline->fk_product = $line->fk_product; |
||
| 594 | $moline->fk_warehouse = $line->fk_warehouse; |
||
| 595 | $moline->qty = $qtytoprocess; |
||
| 596 | $moline->batch = $tmpproduct->status_batch; |
||
| 597 | $moline->role = 'produced'; |
||
| 598 | $moline->fk_mrp_production = $line->id; |
||
| 599 | $moline->fk_stock_movement = $idstockmove; |
||
| 600 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 601 | |||
| 602 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 603 | if ($resultmoline <= 0) { |
||
| 604 | $error++; |
||
| 605 | throw new RestException(500, $moline->error); |
||
| 606 | } |
||
| 607 | |||
| 608 | $pos++; |
||
| 609 | } |
||
| 610 | } |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | if (!$error) { |
||
| 615 | if ($autoclose > 0) { |
||
| 616 | foreach ($this->mo->lines as $line) { |
||
| 617 | if ($line->role == 'toconsume') { |
||
| 618 | $arrayoflines = $this->mo->fetchLinesLinked('consumed', $line->id); |
||
| 619 | $alreadyconsumed = 0; |
||
| 620 | foreach ($arrayoflines as $line2) { |
||
| 621 | $alreadyconsumed += $line2['qty']; |
||
| 622 | } |
||
| 623 | |||
| 624 | if ($alreadyconsumed < $line->qty) { |
||
| 625 | $consumptioncomplete = false; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | if ($line->role == 'toproduce') { |
||
| 629 | $arrayoflines = $this->mo->fetchLinesLinked('produced', $line->id); |
||
| 630 | $alreadyproduced = 0; |
||
| 631 | foreach ($arrayoflines as $line2) { |
||
| 632 | $alreadyproduced += $line2['qty']; |
||
| 633 | } |
||
| 634 | |||
| 635 | if ($alreadyproduced < $line->qty) { |
||
| 636 | $productioncomplete = false; |
||
| 637 | } |
||
| 638 | } |
||
| 639 | } |
||
| 640 | } else { |
||
| 641 | $consumptioncomplete = false; |
||
| 642 | $productioncomplete = false; |
||
| 643 | } |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | // Update status of MO |
||
| 648 | dol_syslog("consumptioncomplete = " . json_encode($consumptioncomplete) . " productioncomplete = " . json_encode($productioncomplete)); |
||
| 649 | if ($consumptioncomplete && $productioncomplete) { |
||
| 650 | $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED'); |
||
| 651 | } else { |
||
| 652 | $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0, '', 'MRP_MO_PRODUCED'); |
||
| 653 | } |
||
| 654 | if ($result <= 0) { |
||
| 655 | throw new RestException(500, $this->mo->error); |
||
| 656 | } |
||
| 657 | |||
| 658 | return $this->mo->id; |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Produce and consume |
||
| 663 | * |
||
| 664 | * Example: |
||
| 665 | * { |
||
| 666 | * "inventorylabel": "Produce and consume using API", |
||
| 667 | * "inventorycode": "PRODUCEAPI-YY-MM-DD", |
||
| 668 | * "autoclose": 1, |
||
| 669 | * "arraytoconsume": [ |
||
| 670 | * { |
||
| 671 | * "objectid": "123", -- rowid of MoLine |
||
| 672 | * "qty": "2", |
||
| 673 | * "fk_warehouse": "789" |
||
| 674 | * } |
||
| 675 | * ], |
||
| 676 | * "arraytoproduce": [ |
||
| 677 | * { |
||
| 678 | * "objectid": "456", -- rowid of MoLine |
||
| 679 | * "qty": "1", |
||
| 680 | * "fk_warehouse": "789", |
||
| 681 | * "pricetoproduce": "12.3" -- optional |
||
| 682 | * } |
||
| 683 | * ] |
||
| 684 | * } |
||
| 685 | * |
||
| 686 | * @param int $id ID of state |
||
| 687 | * @param array $request_data Request datas |
||
| 688 | * |
||
| 689 | * @url POST {id}/produceandconsume |
||
| 690 | * |
||
| 691 | * @return int ID of MO |
||
| 692 | */ |
||
| 693 | public function produceAndConsume($id, $request_data = null) |
||
| 694 | { |
||
| 695 | if (!DolibarrApiAccess::$user->hasRight("mrp", "write")) { |
||
| 696 | throw new RestException(403, 'Not enough permission'); |
||
| 697 | } |
||
| 698 | $result = $this->mo->fetch($id); |
||
| 699 | if (!$result) { |
||
| 700 | throw new RestException(404, 'MO not found'); |
||
| 701 | } |
||
| 702 | |||
| 703 | if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) { |
||
| 704 | throw new RestException(405, 'Error bad status of MO'); |
||
| 705 | } |
||
| 706 | |||
| 707 | // Code for consume and produce... |
||
| 708 | require_once constant('DOL_DOCUMENT_ROOT') . '/mrp/lib/mrp_mo.lib.php'; |
||
| 709 | |||
| 710 | $stockmove = new MouvementStock($this->db); |
||
| 711 | |||
| 712 | $labelmovement = ''; |
||
| 713 | $codemovement = ''; |
||
| 714 | $autoclose = 1; |
||
| 715 | $arraytoconsume = array(); |
||
| 716 | $arraytoproduce = array(); |
||
| 717 | |||
| 718 | foreach ($request_data as $field => $value) { |
||
| 719 | if ($field == 'inventorylabel') { |
||
| 720 | $labelmovement = $value; |
||
| 721 | } |
||
| 722 | if ($field == 'inventorycode') { |
||
| 723 | $codemovement = $value; |
||
| 724 | } |
||
| 725 | if ($field == 'autoclose') { |
||
| 726 | $autoclose = $value; |
||
| 727 | } |
||
| 728 | if ($field == 'arraytoconsume') { |
||
| 729 | $arraytoconsume = $value; |
||
| 730 | } |
||
| 731 | if ($field == 'arraytoproduce') { |
||
| 732 | $arraytoproduce = $value; |
||
| 733 | } |
||
| 734 | if ($field === 'caller') { |
||
| 735 | // 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 with the caller |
||
| 736 | $stockmove->context['caller'] = $request_data['caller']; |
||
| 737 | continue; |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | if (empty($labelmovement)) { |
||
| 742 | throw new RestException(500, "Field inventorylabel not provided"); |
||
| 743 | } |
||
| 744 | if (empty($codemovement)) { |
||
| 745 | throw new RestException(500, "Field inventorycode not provided"); |
||
| 746 | } |
||
| 747 | |||
| 748 | $this->db->begin(); |
||
| 749 | |||
| 750 | $pos = 0; |
||
| 751 | $arrayofarrayname = array("arraytoconsume","arraytoproduce"); |
||
| 752 | foreach ($arrayofarrayname as $arrayname) { |
||
| 753 | foreach (${$arrayname} as $value) { |
||
| 754 | if (empty($value["objectid"])) { |
||
| 755 | throw new RestException(500, "Field objectid required in " . $arrayname); |
||
| 756 | } |
||
| 757 | |||
| 758 | $molinetoprocess = new MoLine($this->db); |
||
| 759 | $tmpmolineid = $molinetoprocess->fetch($value["objectid"]); |
||
| 760 | if ($tmpmolineid <= 0) { |
||
| 761 | throw new RestException(500, "MoLine with rowid " . $value["objectid"] . " not exist."); |
||
| 762 | } |
||
| 763 | |||
| 764 | $tmpproduct = new Product($this->db); |
||
| 765 | $tmpproduct->fetch($molinetoprocess->fk_product); |
||
| 766 | if ($tmpproduct->status_batch) { |
||
| 767 | throw new RestException(500, "Product " . $tmpproduct->ref . " must be in batch, this API can't handle it currently."); |
||
| 768 | } |
||
| 769 | |||
| 770 | if (empty($value["qty"]) && $value["qty"] != 0) { |
||
| 771 | throw new RestException(500, "Field qty with lower or higher then 0 required in " . $arrayname); |
||
| 772 | } |
||
| 773 | $qtytoprocess = $value["qty"]; |
||
| 774 | |||
| 775 | if (isset($value["fk_warehouse"])) { // If there is a warehouse to set |
||
| 776 | if (!($value["fk_warehouse"] > 0)) { // If there is no warehouse set. |
||
| 777 | throw new RestException(500, "Field fk_warehouse required in " . $arrayname); |
||
| 778 | } |
||
| 779 | } |
||
| 780 | $fk_warehousetoprocess = $value["fk_warehouse"]; |
||
| 781 | |||
| 782 | $pricetoproduce = 0; |
||
| 783 | if (isset($value["pricetoproduce"])) { // If there is a price to produce set. |
||
| 784 | if ($value["pricetoproduce"] > 0) { // Only use prices grater then 0. |
||
| 785 | $pricetoproduce = $value["pricetoproduce"]; |
||
| 786 | } |
||
| 787 | } |
||
| 788 | |||
| 789 | $idstockmove = 0; |
||
| 790 | |||
| 791 | // Record stock movement |
||
| 792 | $id_product_batch = 0; |
||
| 793 | $stockmove->origin_type = 'mo'; |
||
| 794 | $stockmove->origin_id = $this->mo->id; |
||
| 795 | if ($arrayname == "arraytoconsume") { |
||
| 796 | if ($qtytoprocess >= 0) { |
||
| 797 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 798 | } else { |
||
| 799 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 800 | } |
||
| 801 | } else { |
||
| 802 | if ($qtytoprocess >= 0) { |
||
| 803 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 804 | } else { |
||
| 805 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
| 806 | } |
||
| 807 | } |
||
| 808 | if ($idstockmove <= 0) { |
||
| 809 | throw new RestException(500, $stockmove->error); |
||
| 810 | } |
||
| 811 | |||
| 812 | // Record consumption |
||
| 813 | $moline = new MoLine($this->db); |
||
| 814 | $moline->fk_mo = $this->mo->id; |
||
| 815 | $moline->position = $pos; |
||
| 816 | $moline->fk_product = $tmpproduct->id; |
||
| 817 | $moline->fk_warehouse = $fk_warehousetoprocess; |
||
| 818 | $moline->qty = $qtytoprocess; |
||
| 819 | $moline->batch = ''; |
||
| 820 | $moline->fk_mrp_production = $molinetoprocess->id; |
||
| 821 | $moline->fk_stock_movement = $idstockmove; |
||
| 822 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
| 823 | |||
| 824 | if ($arrayname == "arraytoconsume") { |
||
| 825 | $moline->role = 'consumed'; |
||
| 826 | } else { |
||
| 827 | $moline->role = 'produced'; |
||
| 828 | } |
||
| 829 | |||
| 830 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
| 831 | if ($resultmoline <= 0) { |
||
| 832 | throw new RestException(500, $moline->error); |
||
| 833 | } |
||
| 834 | |||
| 835 | $pos++; |
||
| 836 | } |
||
| 837 | } |
||
| 838 | |||
| 839 | $consumptioncomplete = true; |
||
| 840 | $productioncomplete = true; |
||
| 841 | |||
| 842 | if ($autoclose > 0) { |
||
| 843 | // Refresh Lines after consumptions. |
||
| 844 | $this->mo->fetchLines(); |
||
| 845 | |||
| 846 | foreach ($this->mo->lines as $line) { |
||
| 847 | if ($line->role == 'toconsume') { |
||
| 848 | $arrayoflines = $this->mo->fetchLinesLinked('consumed', $line->id); |
||
| 849 | $alreadyconsumed = 0; |
||
| 850 | foreach ($arrayoflines as $line2) { |
||
| 851 | $alreadyconsumed += $line2['qty']; |
||
| 852 | } |
||
| 853 | |||
| 854 | if ($alreadyconsumed < $line->qty) { |
||
| 855 | $consumptioncomplete = false; |
||
| 856 | } |
||
| 857 | } |
||
| 858 | if ($line->role == 'toproduce') { |
||
| 859 | $arrayoflines = $this->mo->fetchLinesLinked('produced', $line->id); |
||
| 860 | $alreadyproduced = 0; |
||
| 861 | foreach ($arrayoflines as $line2) { |
||
| 862 | $alreadyproduced += $line2['qty']; |
||
| 863 | } |
||
| 864 | |||
| 865 | if ($alreadyproduced < $line->qty) { |
||
| 866 | $productioncomplete = false; |
||
| 867 | } |
||
| 868 | } |
||
| 869 | } |
||
| 870 | } else { |
||
| 871 | $consumptioncomplete = false; |
||
| 872 | $productioncomplete = false; |
||
| 873 | } |
||
| 874 | |||
| 875 | // Update status of MO |
||
| 876 | dol_syslog("consumptioncomplete = " . (string) $consumptioncomplete . " productioncomplete = " . (string) $productioncomplete); |
||
| 877 | //var_dump("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete); |
||
| 878 | if ($consumptioncomplete && $productioncomplete) { |
||
| 879 | $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED'); |
||
| 880 | } else { |
||
| 881 | $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0, '', 'MRP_MO_PRODUCED'); |
||
| 882 | } |
||
| 883 | if ($result <= 0) { |
||
| 884 | throw new RestException(500, $this->mo->error); |
||
| 885 | } |
||
| 886 | |||
| 887 | $this->db->commit(); |
||
| 888 | return $this->mo->id; |
||
| 889 | } |
||
| 890 | |||
| 891 | |||
| 892 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 893 | /** |
||
| 894 | * Clean sensible object datas |
||
| 895 | * |
||
| 896 | * @param Object $object Object to clean |
||
| 897 | * @return Object Object with cleaned properties |
||
| 898 | */ |
||
| 899 | protected function _cleanObjectDatas($object) |
||
| 900 | { |
||
| 901 | // phpcs:enable |
||
| 902 | $object = parent::_cleanObjectDatas($object); |
||
| 903 | |||
| 904 | unset($object->rowid); |
||
| 905 | unset($object->canvas); |
||
| 906 | |||
| 907 | unset($object->name); |
||
| 908 | unset($object->lastname); |
||
| 909 | unset($object->firstname); |
||
| 910 | unset($object->civility_id); |
||
| 911 | unset($object->statut); |
||
| 912 | unset($object->state); |
||
| 913 | unset($object->state_id); |
||
| 914 | unset($object->state_code); |
||
| 915 | unset($object->region); |
||
| 916 | unset($object->region_code); |
||
| 917 | unset($object->country); |
||
| 918 | unset($object->country_id); |
||
| 919 | unset($object->country_code); |
||
| 920 | unset($object->barcode_type); |
||
| 921 | unset($object->barcode_type_code); |
||
| 922 | unset($object->barcode_type_label); |
||
| 923 | unset($object->barcode_type_coder); |
||
| 924 | unset($object->total_ht); |
||
| 925 | unset($object->total_tva); |
||
| 926 | unset($object->total_localtax1); |
||
| 927 | unset($object->total_localtax2); |
||
| 928 | unset($object->total_ttc); |
||
| 929 | unset($object->fk_account); |
||
| 930 | unset($object->comments); |
||
| 931 | unset($object->note); |
||
| 932 | unset($object->mode_reglement_id); |
||
| 933 | unset($object->cond_reglement_id); |
||
| 934 | unset($object->cond_reglement); |
||
| 935 | unset($object->shipping_method_id); |
||
| 936 | unset($object->fk_incoterms); |
||
| 937 | unset($object->label_incoterms); |
||
| 938 | unset($object->location_incoterms); |
||
| 939 | |||
| 940 | // If object has lines, remove $db property |
||
| 941 | if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) { |
||
| 942 | $nboflines = count($object->lines); |
||
| 943 | for ($i = 0; $i < $nboflines; $i++) { |
||
| 944 | $this->_cleanObjectDatas($object->lines[$i]); |
||
| 945 | |||
| 946 | unset($object->lines[$i]->lines); |
||
| 947 | unset($object->lines[$i]->note); |
||
| 948 | } |
||
| 949 | } |
||
| 950 | |||
| 951 | return $object; |
||
|
|
|||
| 952 | } |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Validate fields before create or update object |
||
| 956 | * |
||
| 957 | * @param array $data Array of data to validate |
||
| 958 | * @return array |
||
| 959 | * |
||
| 960 | * @throws RestException |
||
| 961 | */ |
||
| 962 | private function _validate($data) |
||
| 975 | } |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Validate the ref field and get the next Number if it's necessary. |
||
| 979 | * |
||
| 980 | * @return void |
||
| 981 | */ |
||
| 982 | private function checkRefNumbering() |
||
| 996 | } |
||
| 997 | } |
||
| 998 | } |
||
| 999 | } |
||
| 1000 |