| Conditions | 63 |
| Paths | 0 |
| Total Lines | 283 |
| Code Lines | 163 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 53 | public function index(bool $executeActions = true): bool |
||
| 54 | { |
||
| 55 | global $conf; |
||
| 56 | global $db; |
||
| 57 | global $user; |
||
| 58 | global $hookmanager; |
||
| 59 | global $user; |
||
| 60 | global $menumanager; |
||
| 61 | global $langs; |
||
| 62 | global $mysoc; |
||
| 63 | |||
| 64 | // Load translation files required by the page |
||
| 65 | $langs->loadLangs(['mrp', 'other']); |
||
| 66 | |||
| 67 | // Get parameters |
||
| 68 | $id = GETPOSTINT('id'); |
||
| 69 | $lineid = GETPOSTINT('lineid'); |
||
| 70 | $ref = GETPOST('ref', 'alpha'); |
||
| 71 | $action = GETPOST('action', 'aZ09'); |
||
| 72 | $confirm = GETPOST('confirm', 'alpha'); |
||
| 73 | $cancel = GETPOST('cancel', 'aZ09'); |
||
| 74 | $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'bomcard'; // To manage different context of search |
||
| 75 | $backtopage = GETPOST('backtopage', 'alpha'); |
||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | $hidedetails = (GETPOSTINT('hidedetails') ? GETPOSTINT('hidedetails') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0)); |
||
| 80 | $hidedesc = (GETPOSTINT('hidedesc') ? GETPOSTINT('hidedesc') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0)); |
||
| 81 | $hideref = (GETPOSTINT('hideref') ? GETPOSTINT('hideref') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0)); |
||
| 82 | |||
| 83 | // Initialize technical objects |
||
| 84 | $object = new Bom($db); |
||
| 85 | $extrafields = new ExtraFields($db); |
||
| 86 | $diroutputmassaction = $conf->bom->dir_output . '/temp/massgeneration/' . $user->id; |
||
| 87 | $hookmanager->initHooks(['bomcard', 'globalcard']); // Note that conf->hooks_modules contains array |
||
| 88 | |||
| 89 | // Fetch optionals attributes and labels |
||
| 90 | $extrafields->fetch_name_optionals_label($object->table_element); |
||
| 91 | $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_'); |
||
| 92 | |||
| 93 | // Initialize array of search criteria |
||
| 94 | $search_all = GETPOST("search_all", 'alpha'); |
||
| 95 | $search = []; |
||
| 96 | foreach ($object->fields as $key => $val) { |
||
| 97 | if (GETPOST('search_' . $key, 'alpha')) { |
||
| 98 | $search[$key] = GETPOST('search_' . $key, 'alpha'); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | if (empty($action) && empty($id) && empty($ref)) { |
||
| 103 | $action = 'view'; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Load object |
||
| 107 | include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once. |
||
| 108 | if ($object->id > 0) { |
||
| 109 | $object->calculateCosts(); |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | // Security check - Protection if external user |
||
| 114 | //if ($user->socid > 0) accessforbidden(); |
||
| 115 | //if ($user->socid > 0) $socid = $user->socid; |
||
| 116 | $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0); |
||
| 117 | $result = restrictedArea($user, 'bom', $object->id, $object->table_element, '', '', 'rowid', $isdraft); |
||
| 118 | |||
| 119 | // Permissions |
||
| 120 | $permissionnote = $user->hasRight('bom', 'write'); // Used by the include of actions_setnotes.inc.php |
||
| 121 | $permissiondellink = $user->hasRight('bom', 'write'); // Used by the include of actions_dellink.inc.php |
||
| 122 | $permissiontoadd = $user->hasRight('bom', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php |
||
| 123 | $permissiontodelete = $user->hasRight('bom', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT); |
||
| 124 | $upload_dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1]; |
||
| 125 | |||
| 126 | |||
| 127 | /* |
||
| 128 | * Actions |
||
| 129 | */ |
||
| 130 | |||
| 131 | $parameters = []; |
||
| 132 | $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 133 | if ($reshook < 0) { |
||
| 134 | setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (empty($reshook)) { |
||
| 138 | $error = 0; |
||
| 139 | |||
| 140 | $backurlforlist = '/bom/bom_list.php'; |
||
| 141 | |||
| 142 | if (empty($backtopage) || ($cancel && empty($id))) { |
||
| 143 | if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { |
||
| 144 | if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { |
||
| 145 | $backtopage = $backurlforlist; |
||
| 146 | } else { |
||
| 147 | $backtopage = '/bom/bom_card.php?id=' . ($id > 0 ? $id : '__ID__'); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $triggermodname = 'BOM_MODIFY'; // Name of trigger action code to execute when we modify record |
||
| 153 | |||
| 154 | |||
| 155 | // Actions cancel, add, update, delete or clone |
||
| 156 | include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php'; |
||
| 157 | // The fetch/fetch_lines was redone into the inc.php so we must recall the calculateCosts() |
||
| 158 | if ($action == 'confirm_validate' && $object->id > 0) { |
||
| 159 | $object->calculateCosts(); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Actions when linking object each other |
||
| 163 | include DOL_DOCUMENT_ROOT . '/core/actions_dellink.inc.php'; |
||
| 164 | |||
| 165 | // Actions when printing a doc from card |
||
| 166 | include DOL_DOCUMENT_ROOT . '/core/actions_printing.inc.php'; |
||
| 167 | |||
| 168 | // Action to move up and down lines of object |
||
| 169 | //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; |
||
| 170 | |||
| 171 | // Action to build doc |
||
| 172 | include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php'; |
||
| 173 | |||
| 174 | // Actions to send emails |
||
| 175 | $triggersendname = 'BOM_SENTBYMAIL'; |
||
| 176 | $autocopy = 'MAIN_MAIL_AUTOCOPY_BOM_TO'; |
||
| 177 | $trackid = 'bom' . $object->id; |
||
| 178 | include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php'; |
||
| 179 | |||
| 180 | // Add line |
||
| 181 | if ($action == 'addline' && $user->hasRight('bom', 'write')) { |
||
| 182 | $langs->load('errors'); |
||
| 183 | $error = 0; |
||
| 184 | $predef = ''; |
||
| 185 | |||
| 186 | // Set if we used free entry or predefined product |
||
| 187 | $bom_child_id = GETPOSTINT('bom_id'); |
||
| 188 | if ($bom_child_id > 0) { |
||
| 189 | $bom_child = new Bom($db); |
||
| 190 | $res = $bom_child->fetch($bom_child_id); |
||
| 191 | if ($res) { |
||
| 192 | $idprod = $bom_child->fk_product; |
||
| 193 | } |
||
| 194 | } else { |
||
| 195 | $idprod = (!empty(GETPOSTINT('idprodservice')) ? GETPOSTINT('idprodservice') : GETPOSTINT('idprod')); |
||
| 196 | } |
||
| 197 | |||
| 198 | $qty = price2num(GETPOST('qty', 'alpha'), 'MS'); |
||
| 199 | $qty_frozen = price2num(GETPOST('qty_frozen', 'alpha'), 'MS'); |
||
| 200 | $disable_stock_change = GETPOSTINT('disable_stock_change'); |
||
| 201 | $efficiency = price2num(GETPOST('efficiency', 'alpha')); |
||
| 202 | $fk_unit = GETPOST('fk_unit', 'alphanohtml'); |
||
| 203 | |||
| 204 | $fk_default_workstation = 0; |
||
| 205 | if (!empty($idprod) && isModEnabled('workstation')) { |
||
| 206 | $product = new Product($db); |
||
| 207 | $res = $product->fetch($idprod); |
||
| 208 | if ($res > 0 && $product->type == Product::TYPE_SERVICE) { |
||
| 209 | $fk_default_workstation = $product->fk_default_workstation; |
||
| 210 | } |
||
| 211 | if (empty($fk_unit)) { |
||
| 212 | $fk_unit = $product->fk_unit; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | if ($qty == '') { |
||
| 217 | setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors'); |
||
| 218 | $error++; |
||
| 219 | } |
||
| 220 | if (!($idprod > 0)) { |
||
| 221 | setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Product')), null, 'errors'); |
||
| 222 | $error++; |
||
| 223 | } |
||
| 224 | |||
| 225 | if ($object->fk_product == $idprod) { |
||
| 226 | setEventMessages($langs->trans('TheProductXIsAlreadyTheProductToProduce'), null, 'errors'); |
||
| 227 | $error++; |
||
| 228 | } |
||
| 229 | |||
| 230 | // We check if we're allowed to add this bom |
||
| 231 | $TParentBom = []; |
||
| 232 | $object->getParentBomTreeRecursive($TParentBom); |
||
| 233 | if ($bom_child_id > 0 && !empty($TParentBom) && in_array($bom_child_id, $TParentBom)) { |
||
| 234 | $n_child = new Bom($db); |
||
| 235 | $n_child->fetch($bom_child_id); |
||
| 236 | setEventMessages($langs->transnoentities('BomCantAddChildBom', $n_child->getNomUrl(1), $object->getNomUrl(1)), null, 'errors'); |
||
| 237 | $error++; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (!$error) { |
||
| 241 | // Extrafields |
||
| 242 | $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line); |
||
| 243 | $array_options = $extrafields->getOptionalsFromPost($object->table_element_line, $predef); |
||
| 244 | // Unset extrafield |
||
| 245 | if (is_array($extralabelsline)) { |
||
| 246 | // Get extra fields |
||
| 247 | foreach ($extralabelsline as $key => $value) { |
||
| 248 | unset($_POST["options_" . $key]); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | $result = $object->addLine($idprod, $qty, $qty_frozen, $disable_stock_change, $efficiency, -1, $bom_child_id, null, $fk_unit, $array_options, $fk_default_workstation); |
||
| 253 | |||
| 254 | if ($result <= 0) { |
||
| 255 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 256 | $action = ''; |
||
| 257 | } else { |
||
| 258 | unset($_POST['idprod']); |
||
| 259 | unset($_POST['idprodservice']); |
||
| 260 | unset($_POST['qty']); |
||
| 261 | unset($_POST['qty_frozen']); |
||
| 262 | unset($_POST['disable_stock_change']); |
||
| 263 | } |
||
| 264 | |||
| 265 | $object->fetchLines(); |
||
| 266 | |||
| 267 | $object->calculateCosts(); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | // Update line |
||
| 272 | if ($action == 'updateline' && $user->hasRight('bom', 'write')) { |
||
| 273 | $langs->load('errors'); |
||
| 274 | $error = 0; |
||
| 275 | |||
| 276 | // Set if we used free entry or predefined product |
||
| 277 | $qty = price2num(GETPOST('qty', 'alpha'), 'MS'); |
||
| 278 | $qty_frozen = price2num(GETPOST('qty_frozen', 'alpha'), 'MS'); |
||
| 279 | $disable_stock_change = GETPOSTINT('disable_stock_change'); |
||
| 280 | $efficiency = price2num(GETPOST('efficiency', 'alpha')); |
||
| 281 | $fk_unit = GETPOST('fk_unit', 'alphanohtml'); |
||
| 282 | |||
| 283 | if ($qty == '') { |
||
| 284 | setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors'); |
||
| 285 | $error++; |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!$error) { |
||
| 289 | // Extrafields |
||
| 290 | $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line); |
||
| 291 | $array_options = $extrafields->getOptionalsFromPost($object->table_element_line); |
||
| 292 | // Unset extrafield |
||
| 293 | if (is_array($extralabelsline)) { |
||
| 294 | // Get extra fields |
||
| 295 | foreach ($extralabelsline as $key => $value) { |
||
| 296 | unset($_POST["options_" . $key]); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | $bomline = new BomLine($db); |
||
| 301 | $bomline->fetch($lineid); |
||
| 302 | |||
| 303 | $fk_default_workstation = $bomline->fk_default_workstation; |
||
| 304 | if (isModEnabled('workstation') && GETPOSTISSET('idworkstations')) { |
||
| 305 | $fk_default_workstation = GETPOSTINT('idworkstations'); |
||
| 306 | } |
||
| 307 | |||
| 308 | $result = $object->updateLine($lineid, $qty, (int) $qty_frozen, (int) $disable_stock_change, $efficiency, $bomline->position, $bomline->import_key, $fk_unit, $array_options, $fk_default_workstation); |
||
| 309 | |||
| 310 | if ($result <= 0) { |
||
| 311 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 312 | $action = ''; |
||
| 313 | } else { |
||
| 314 | unset($_POST['idprod']); |
||
| 315 | unset($_POST['idprodservice']); |
||
| 316 | unset($_POST['qty']); |
||
| 317 | unset($_POST['qty_frozen']); |
||
| 318 | unset($_POST['disable_stock_change']); |
||
| 319 | } |
||
| 320 | |||
| 321 | $object->fetchLines(); |
||
| 322 | |||
| 323 | $object->calculateCosts(); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /* |
||
| 329 | * View |
||
| 330 | */ |
||
| 331 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Bom/Views/bom_card.php'); |
||
| 332 | |||
| 333 | $db->close(); |
||
| 334 | |||
| 335 | return true; |
||
| 336 | } |
||
| 338 |