| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 95 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 181 | public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0) |
||
| 182 | { |
||
| 183 | global $langs, $hookmanager; |
||
| 184 | dol_syslog(__METHOD__ . " user_id=" . $user->id . ", asset_id=" . $asset_id . ", asset_model_id=" . $asset_model_id . ", notrigger=" . $notrigger); |
||
| 185 | |||
| 186 | $error = 0; |
||
| 187 | $this->errors = array(); |
||
| 188 | |||
| 189 | // Clean parameters |
||
| 190 | $asset_id = $asset_id > 0 ? $asset_id : 0; |
||
| 191 | $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0; |
||
| 192 | |||
| 193 | $hookmanager->initHooks(array('assetaccountancycodesdao')); |
||
| 194 | $parameters = array('user' => $user, 'asset_id' => $asset_id, 'asset_model_id' => $asset_model_id); |
||
| 195 | $reshook = $hookmanager->executeHooks('updateAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks |
||
| 196 | if (!empty($reshook)) { |
||
| 197 | return $reshook; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Check parameters |
||
| 201 | if (empty($asset_id) && empty($asset_model_id)) { |
||
| 202 | $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide'); |
||
| 203 | $error++; |
||
| 204 | } |
||
| 205 | if ($error) { |
||
| 206 | dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR); |
||
| 207 | return -1; |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->db->begin(); |
||
| 211 | $now = dol_now(); |
||
| 212 | |||
| 213 | foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) { |
||
| 214 | // Delete old accountancy codes |
||
| 215 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . $mode_info['table']; |
||
| 216 | $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id); |
||
| 217 | $resql = $this->db->query($sql); |
||
| 218 | if (!$resql) { |
||
| 219 | $this->errors[] = $langs->trans('AssetErrorDeleteAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror(); |
||
| 220 | $error++; |
||
| 221 | } |
||
| 222 | |||
| 223 | if (!$error && !empty($this->accountancy_codes[$mode_key])) { |
||
| 224 | // Insert accountancy codes |
||
| 225 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . $mode_info['table'] . "("; |
||
| 226 | $sql .= $asset_id > 0 ? "fk_asset," : "fk_asset_model,"; |
||
| 227 | $sql .= implode(',', array_keys($mode_info['fields'])); |
||
| 228 | $sql .= ", tms, fk_user_modif"; |
||
| 229 | $sql .= ") VALUES("; |
||
| 230 | $sql .= $asset_id > 0 ? $asset_id : $asset_model_id; |
||
| 231 | foreach ($mode_info['fields'] as $field_key => $field_info) { |
||
| 232 | $sql .= ', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ? 'NULL' : "'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) . "'"); |
||
| 233 | } |
||
| 234 | $sql .= ", '" . $this->db->idate($now) . "'"; |
||
| 235 | $sql .= ", " . $user->id; |
||
| 236 | $sql .= ")"; |
||
| 237 | |||
| 238 | $resql = $this->db->query($sql); |
||
| 239 | if (!$resql) { |
||
| 240 | $this->errors[] = $langs->trans('AssetErrorInsertAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror(); |
||
| 241 | $error++; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | if (!$error && $asset_id > 0) { |
||
| 247 | // Calculation of depreciation lines (reversal and future) |
||
| 248 | require_once constant('DOL_DOCUMENT_ROOT') . '/asset/class/asset.class.php'; |
||
| 249 | $asset = new Asset($this->db); |
||
| 250 | $result = $asset->fetch($asset_id); |
||
| 251 | if ($result > 0) { |
||
| 252 | $result = $asset->calculationDepreciation(); |
||
| 253 | } |
||
| 254 | if ($result < 0) { |
||
| 255 | $this->errors[] = $langs->trans('AssetErrorCalculationDepreciationLines'); |
||
| 256 | $this->errors[] = $asset->errorsToString(); |
||
| 257 | $error++; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | if (!$error && !$notrigger) { |
||
| 262 | // Call trigger |
||
| 263 | $result = $this->call_trigger('ASSET_ACCOUNTANCY_CODES_MODIFY', $user); |
||
| 264 | if ($result < 0) { |
||
| 265 | $error++; |
||
| 266 | } |
||
| 267 | // End call triggers |
||
| 268 | } |
||
| 269 | |||
| 270 | if (!$error) { |
||
| 271 | $this->db->commit(); |
||
| 272 | return 1; |
||
| 273 | } else { |
||
| 274 | $this->db->rollback(); |
||
| 275 | return -1; |
||
| 276 | } |
||
| 279 |