| Total Complexity | 85 |
| Total Lines | 568 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Boms 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 Boms, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Boms extends DolibarrApi |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var BOM $bom {@type BOM} |
||
| 46 | */ |
||
| 47 | public $bom; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor |
||
| 51 | */ |
||
| 52 | public function __construct() |
||
| 53 | { |
||
| 54 | global $db; |
||
| 55 | |||
| 56 | $this->db = $db; |
||
| 57 | $this->bom = new BOM($this->db); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get properties of a bom object |
||
| 62 | * |
||
| 63 | * Return an array with bom information |
||
| 64 | * |
||
| 65 | * @param int $id ID of bom |
||
| 66 | * @return array Object with cleaned properties |
||
| 67 | * |
||
| 68 | * @url GET {id} |
||
| 69 | * |
||
| 70 | * @throws RestException 403 Access denied |
||
| 71 | * @throws RestException 404 BOM not found |
||
| 72 | */ |
||
| 73 | public function get($id) |
||
| 74 | { |
||
| 75 | if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) { |
||
| 76 | throw new RestException(403); |
||
| 77 | } |
||
| 78 | |||
| 79 | $result = $this->bom->fetch($id); |
||
| 80 | if (!$result) { |
||
| 81 | throw new RestException(404, 'BOM not found'); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
||
| 85 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $this->_cleanObjectDatas($this->bom); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * List boms |
||
| 94 | * |
||
| 95 | * Get a list of boms |
||
| 96 | * |
||
| 97 | * @param string $sortfield Sort field |
||
| 98 | * @param string $sortorder Sort order |
||
| 99 | * @param int $limit Limit for list |
||
| 100 | * @param int $page Page number |
||
| 101 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 102 | * @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
||
| 103 | * @return array Array of order objects |
||
| 104 | * |
||
| 105 | * @throws RestException 400 Bad sqlfilters |
||
| 106 | * @throws RestException 403 Access denied |
||
| 107 | * @throws RestException 503 Error retrieving list of boms |
||
| 108 | */ |
||
| 109 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '') |
||
| 110 | { |
||
| 111 | if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) { |
||
| 112 | throw new RestException(403); |
||
| 113 | } |
||
| 114 | |||
| 115 | $obj_ret = array(); |
||
| 116 | $tmpobject = new BOM($this->db); |
||
| 117 | |||
| 118 | $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : ''; |
||
| 119 | |||
| 120 | $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object |
||
| 121 | |||
| 122 | // If the internal user must only see his customers, force searching by him |
||
| 123 | $search_sale = 0; |
||
| 124 | if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) { |
||
| 125 | $search_sale = DolibarrApiAccess::$user->id; |
||
| 126 | } |
||
| 127 | |||
| 128 | $sql = "SELECT t.rowid"; |
||
| 129 | $sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject->table_element . " AS t"; |
||
| 130 | $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 |
||
| 131 | $sql .= " WHERE 1 = 1"; |
||
| 132 | if ($tmpobject->ismultientitymanaged) { |
||
| 133 | $sql .= ' AND t.entity IN (' . getEntity($tmpobject->element) . ')'; |
||
| 134 | } |
||
| 135 | if ($restrictonsocid && $socid) { |
||
| 136 | $sql .= " AND t.fk_soc = " . ((int) $socid); |
||
| 137 | } |
||
| 138 | // Search on sale representative |
||
| 139 | if ($search_sale && $search_sale != '-1') { |
||
| 140 | if ($search_sale == -2) { |
||
| 141 | $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
||
| 142 | } elseif ($search_sale > 0) { |
||
| 143 | $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) . ")"; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | if ($sqlfilters) { |
||
| 147 | $errormessage = ''; |
||
| 148 | $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
||
| 149 | if ($errormessage) { |
||
| 150 | throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 155 | if ($limit) { |
||
| 156 | if ($page < 0) { |
||
| 157 | $page = 0; |
||
| 158 | } |
||
| 159 | $offset = $limit * $page; |
||
| 160 | |||
| 161 | $sql .= $this->db->plimit($limit + 1, $offset); |
||
| 162 | } |
||
| 163 | |||
| 164 | $result = $this->db->query($sql); |
||
| 165 | if ($result) { |
||
| 166 | $num = $this->db->num_rows($result); |
||
| 167 | $i = 0; |
||
| 168 | while ($i < $num) { |
||
| 169 | $obj = $this->db->fetch_object($result); |
||
| 170 | $bom_static = new BOM($this->db); |
||
| 171 | if ($bom_static->fetch($obj->rowid)) { |
||
| 172 | $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties); |
||
| 173 | } |
||
| 174 | $i++; |
||
| 175 | } |
||
| 176 | } else { |
||
| 177 | throw new RestException(503, 'Error when retrieve bom list'); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $obj_ret; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Create bom object |
||
| 185 | * |
||
| 186 | * @param array $request_data Request datas |
||
| 187 | * @return int ID of bom |
||
| 188 | * |
||
| 189 | * @throws RestException 403 Access denied |
||
| 190 | * @throws RestException 500 Error retrieving list of boms |
||
| 191 | */ |
||
| 192 | public function post($request_data = null) |
||
| 193 | { |
||
| 194 | if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
||
| 195 | throw new RestException(403); |
||
| 196 | } |
||
| 197 | // Check mandatory fields |
||
| 198 | $result = $this->_validate($request_data); |
||
| 199 | |||
| 200 | foreach ($request_data as $field => $value) { |
||
| 201 | if ($field === 'caller') { |
||
| 202 | // 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 |
||
| 203 | $this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom); |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->checkRefNumbering(); |
||
| 211 | |||
| 212 | if (!$this->bom->create(DolibarrApiAccess::$user)) { |
||
| 213 | throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors)); |
||
| 214 | } |
||
| 215 | return $this->bom->id; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Update bom |
||
| 220 | * |
||
| 221 | * @param int $id Id of bom to update |
||
| 222 | * @param array $request_data Datas |
||
| 223 | * @return array Object after update |
||
| 224 | * |
||
| 225 | * @throws RestException 403 Access denied |
||
| 226 | * @throws RestException 404 BOM not found |
||
| 227 | * @throws RestException 500 Error updating bom |
||
| 228 | */ |
||
| 229 | public function put($id, $request_data = null) |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Delete bom |
||
| 275 | * |
||
| 276 | * @param int $id BOM ID |
||
| 277 | * @return array |
||
| 278 | * |
||
| 279 | * @throws RestException 403 Access denied |
||
| 280 | * @throws RestException 404 BOM not found |
||
| 281 | * @throws RestException 500 Error deleting bom |
||
| 282 | */ |
||
| 283 | public function delete($id) |
||
| 284 | { |
||
| 285 | if (!DolibarrApiAccess::$user->hasRight('bom', 'delete')) { |
||
| 286 | throw new RestException(403); |
||
| 287 | } |
||
| 288 | $result = $this->bom->fetch($id); |
||
| 289 | if (!$result) { |
||
| 290 | throw new RestException(404, 'BOM not found'); |
||
| 291 | } |
||
| 292 | |||
| 293 | if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
||
| 294 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 295 | } |
||
| 296 | |||
| 297 | if (!$this->bom->delete(DolibarrApiAccess::$user)) { |
||
| 298 | throw new RestException(500, 'Error when deleting BOM : ' . $this->bom->error); |
||
| 299 | } |
||
| 300 | |||
| 301 | return array( |
||
| 302 | 'success' => array( |
||
| 303 | 'code' => 200, |
||
| 304 | 'message' => 'BOM deleted' |
||
| 305 | ) |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get lines of an BOM |
||
| 311 | * |
||
| 312 | * @param int $id Id of BOM |
||
| 313 | * |
||
| 314 | * @url GET {id}/lines |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | * |
||
| 318 | * @throws RestException 403 Access denied |
||
| 319 | * @throws RestException 404 BOM not found |
||
| 320 | */ |
||
| 321 | public function getLines($id) |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Add a line to given BOM |
||
| 345 | * |
||
| 346 | * @param int $id Id of BOM to update |
||
| 347 | * @param array $request_data BOMLine data |
||
| 348 | * |
||
| 349 | * @url POST {id}/lines |
||
| 350 | * |
||
| 351 | * @return int |
||
| 352 | * |
||
| 353 | * @throws RestException 403 Access denied |
||
| 354 | * @throws RestException 404 BOM not found |
||
| 355 | * @throws RestException 500 Error adding bom line |
||
| 356 | */ |
||
| 357 | public function postLine($id, $request_data = null) |
||
| 358 | { |
||
| 359 | if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
||
| 360 | throw new RestException(403); |
||
| 361 | } |
||
| 362 | |||
| 363 | $result = $this->bom->fetch($id); |
||
| 364 | if (!$result) { |
||
| 365 | throw new RestException(404, 'BOM not found'); |
||
| 366 | } |
||
| 367 | |||
| 368 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 369 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 370 | } |
||
| 371 | |||
| 372 | $request_data = (object) $request_data; |
||
| 373 | |||
| 374 | $updateRes = $this->bom->addLine( |
||
| 375 | $request_data->fk_product, |
||
| 376 | $request_data->qty, |
||
| 377 | $request_data->qty_frozen, |
||
| 378 | $request_data->disable_stock_change, |
||
| 379 | $request_data->efficiency, |
||
| 380 | $request_data->position, |
||
| 381 | $request_data->fk_bom_child, |
||
| 382 | $request_data->import_key, |
||
| 383 | $request_data->fk_unit |
||
| 384 | ); |
||
| 385 | |||
| 386 | if ($updateRes > 0) { |
||
| 387 | return $updateRes; |
||
| 388 | } else { |
||
| 389 | throw new RestException(500, $this->bom->error); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Update a line to given BOM |
||
| 395 | * |
||
| 396 | * @param int $id Id of BOM to update |
||
| 397 | * @param int $lineid Id of line to update |
||
| 398 | * @param array $request_data BOMLine data |
||
| 399 | * |
||
| 400 | * @url PUT {id}/lines/{lineid} |
||
| 401 | * |
||
| 402 | * @return object|bool |
||
| 403 | * |
||
| 404 | * @throws RestException 403 Access denied |
||
| 405 | * @throws RestException 404 BOM not found |
||
| 406 | */ |
||
| 407 | public function putLine($id, $lineid, $request_data = null) |
||
| 408 | { |
||
| 409 | if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
||
| 410 | throw new RestException(403); |
||
| 411 | } |
||
| 412 | |||
| 413 | $result = $this->bom->fetch($id); |
||
| 414 | if (!$result) { |
||
| 415 | throw new RestException(404, 'BOM not found'); |
||
| 416 | } |
||
| 417 | |||
| 418 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 419 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 420 | } |
||
| 421 | |||
| 422 | $request_data = (object) $request_data; |
||
| 423 | |||
| 424 | $updateRes = $this->bom->updateLine( |
||
| 425 | $lineid, |
||
| 426 | $request_data->qty, |
||
| 427 | $request_data->qty_frozen, |
||
| 428 | $request_data->disable_stock_change, |
||
| 429 | $request_data->efficiency, |
||
| 430 | $request_data->position, |
||
| 431 | $request_data->import_key, |
||
| 432 | $request_data->fk_unit |
||
| 433 | ); |
||
| 434 | |||
| 435 | if ($updateRes > 0) { |
||
| 436 | $result = $this->get($id); |
||
| 437 | unset($result->line); |
||
| 438 | return $this->_cleanObjectDatas($result); |
||
| 439 | } |
||
| 440 | return false; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Delete a line to given BOM |
||
| 445 | * |
||
| 446 | * |
||
| 447 | * @param int $id Id of BOM to update |
||
| 448 | * @param int $lineid Id of line to delete |
||
| 449 | * |
||
| 450 | * @url DELETE {id}/lines/{lineid} |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | * |
||
| 454 | * @throws RestException 403 Access denied |
||
| 455 | * @throws RestException 404 BOM not found |
||
| 456 | * @throws RestException 500 Error deleting bom line |
||
| 457 | */ |
||
| 458 | public function deleteLine($id, $lineid) |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 499 | /** |
||
| 500 | * Clean sensible object datas |
||
| 501 | * |
||
| 502 | * @param Object $object Object to clean |
||
| 503 | * @return Object Object with cleaned properties |
||
| 504 | */ |
||
| 505 | protected function _cleanObjectDatas($object) |
||
| 506 | { |
||
| 507 | // phpcs:enable |
||
| 508 | $object = parent::_cleanObjectDatas($object); |
||
| 509 | |||
| 510 | unset($object->rowid); |
||
| 511 | unset($object->canvas); |
||
| 512 | |||
| 513 | unset($object->name); |
||
| 514 | unset($object->lastname); |
||
| 515 | unset($object->firstname); |
||
| 516 | unset($object->civility_id); |
||
| 517 | unset($object->statut); |
||
| 518 | unset($object->state); |
||
| 519 | unset($object->state_id); |
||
| 520 | unset($object->state_code); |
||
| 521 | unset($object->region); |
||
| 522 | unset($object->region_code); |
||
| 523 | unset($object->country); |
||
| 524 | unset($object->country_id); |
||
| 525 | unset($object->country_code); |
||
| 526 | unset($object->barcode_type); |
||
| 527 | unset($object->barcode_type_code); |
||
| 528 | unset($object->barcode_type_label); |
||
| 529 | unset($object->barcode_type_coder); |
||
| 530 | unset($object->total_ht); |
||
| 531 | unset($object->total_tva); |
||
| 532 | unset($object->total_localtax1); |
||
| 533 | unset($object->total_localtax2); |
||
| 534 | unset($object->total_ttc); |
||
| 535 | unset($object->fk_account); |
||
| 536 | unset($object->comments); |
||
| 537 | unset($object->note); |
||
| 538 | unset($object->mode_reglement_id); |
||
| 539 | unset($object->cond_reglement_id); |
||
| 540 | unset($object->cond_reglement); |
||
| 541 | unset($object->shipping_method_id); |
||
| 542 | unset($object->fk_incoterms); |
||
| 543 | unset($object->label_incoterms); |
||
| 544 | unset($object->location_incoterms); |
||
| 545 | unset($object->multicurrency_code); |
||
| 546 | unset($object->multicurrency_tx); |
||
| 547 | unset($object->multicurrency_total_ht); |
||
| 548 | unset($object->multicurrency_total_ttc); |
||
| 549 | unset($object->multicurrency_total_tva); |
||
| 550 | unset($object->multicurrency_total_localtax1); |
||
| 551 | unset($object->multicurrency_total_localtax2); |
||
| 552 | |||
| 553 | |||
| 554 | // If object has lines, remove $db property |
||
| 555 | if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) { |
||
| 556 | $nboflines = count($object->lines); |
||
| 557 | for ($i = 0; $i < $nboflines; $i++) { |
||
| 558 | $this->_cleanObjectDatas($object->lines[$i]); |
||
| 559 | |||
| 560 | unset($object->lines[$i]->lines); |
||
| 561 | unset($object->lines[$i]->note); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | return $object; |
||
|
|
|||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Validate fields before create or update object |
||
| 570 | * |
||
| 571 | * @param array $data Array of data to validate |
||
| 572 | * @return array |
||
| 573 | * |
||
| 574 | * @throws RestException |
||
| 575 | */ |
||
| 576 | private function _validate($data) |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Validate the ref field and get the next Number if it's necessary. |
||
| 593 | * |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | private function checkRefNumbering() |
||
| 610 | } |
||
| 611 | } |
||
| 614 |