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