Total Complexity | 106 |
Total Lines | 617 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like OrderLine 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 OrderLine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class OrderLine extends CommonOrderLine |
||
50 | { |
||
51 | /** |
||
52 | * @var string ID to identify managed object |
||
53 | */ |
||
54 | public $element = 'commandedet'; |
||
55 | |||
56 | public $table_element = 'commandedet'; |
||
57 | |||
58 | public $oldline; |
||
59 | |||
60 | /** |
||
61 | * Id of parent order |
||
62 | * @var int |
||
63 | */ |
||
64 | public $fk_commande; |
||
65 | |||
66 | /** |
||
67 | * Id of parent order |
||
68 | * @var int |
||
69 | * @deprecated Use fk_commande |
||
70 | * @see $fk_commande |
||
71 | */ |
||
72 | public $commande_id; |
||
73 | |||
74 | public $fk_parent_line; |
||
75 | |||
76 | /** |
||
77 | * @var int Id of invoice |
||
78 | */ |
||
79 | public $fk_facture; |
||
80 | |||
81 | /** |
||
82 | * @var string External ref |
||
83 | */ |
||
84 | public $ref_ext; |
||
85 | |||
86 | public $fk_remise_except; |
||
87 | |||
88 | /** |
||
89 | * @var int line rank |
||
90 | */ |
||
91 | public $rang = 0; |
||
92 | public $fk_fournprice; |
||
93 | |||
94 | /** |
||
95 | * Buy price without taxes |
||
96 | * @var float |
||
97 | */ |
||
98 | public $pa_ht; |
||
99 | public $marge_tx; |
||
100 | public $marque_tx; |
||
101 | |||
102 | /** |
||
103 | * @deprecated |
||
104 | * @see $remise_percent, $fk_remise_except |
||
105 | */ |
||
106 | public $remise; |
||
107 | |||
108 | // Start and end date of the line |
||
109 | public $date_start; |
||
110 | public $date_end; |
||
111 | |||
112 | public $skip_update_total; // Skip update price total for special lines |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Constructor |
||
117 | * |
||
118 | * @param DoliDB $db handler d'acces base de donnee |
||
119 | */ |
||
120 | public function __construct($db) |
||
121 | { |
||
122 | $this->db = $db; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Load line order |
||
127 | * |
||
128 | * @param int $rowid Id line order |
||
129 | * @return int Return integer <0 if KO, >0 if OK |
||
130 | */ |
||
131 | public function fetch($rowid) |
||
132 | { |
||
133 | $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,'; |
||
134 | $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,'; |
||
135 | $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,'; |
||
136 | $sql .= ' cd.fk_unit,'; |
||
137 | $sql .= ' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,'; |
||
138 | $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,'; |
||
139 | $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code'; |
||
140 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'commandedet as cd'; |
||
141 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON cd.fk_product = p.rowid'; |
||
142 | $sql .= ' WHERE cd.rowid = ' . ((int) $rowid); |
||
143 | $result = $this->db->query($sql); |
||
144 | if ($result) { |
||
145 | $objp = $this->db->fetch_object($result); |
||
146 | |||
147 | if (!$objp) { |
||
148 | $this->error = 'OrderLine with id ' . $rowid . ' not found sql=' . $sql; |
||
149 | return 0; |
||
150 | } |
||
151 | |||
152 | $this->rowid = $objp->rowid; |
||
|
|||
153 | $this->id = $objp->rowid; |
||
154 | $this->fk_commande = $objp->fk_commande; |
||
155 | $this->fk_parent_line = $objp->fk_parent_line; |
||
156 | $this->label = $objp->custom_label; |
||
157 | $this->desc = $objp->description; |
||
158 | $this->qty = $objp->qty; |
||
159 | $this->price = $objp->price; |
||
160 | $this->subprice = $objp->subprice; |
||
161 | $this->ref_ext = $objp->ref_ext; |
||
162 | $this->vat_src_code = $objp->vat_src_code; |
||
163 | $this->tva_tx = $objp->tva_tx; |
||
164 | $this->localtax1_tx = $objp->localtax1_tx; |
||
165 | $this->localtax2_tx = $objp->localtax2_tx; |
||
166 | $this->remise = $objp->remise; |
||
167 | $this->remise_percent = $objp->remise_percent; |
||
168 | $this->fk_remise_except = $objp->fk_remise_except; |
||
169 | $this->fk_product = $objp->fk_product; |
||
170 | $this->product_type = $objp->product_type; |
||
171 | $this->info_bits = $objp->info_bits; |
||
172 | $this->special_code = $objp->special_code; |
||
173 | $this->total_ht = $objp->total_ht; |
||
174 | $this->total_tva = $objp->total_tva; |
||
175 | $this->total_localtax1 = $objp->total_localtax1; |
||
176 | $this->total_localtax2 = $objp->total_localtax2; |
||
177 | $this->total_ttc = $objp->total_ttc; |
||
178 | $this->fk_fournprice = $objp->fk_fournprice; |
||
179 | $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht); |
||
180 | $this->pa_ht = $marginInfos[0]; |
||
181 | $this->marge_tx = $marginInfos[1]; |
||
182 | $this->marque_tx = $marginInfos[2]; |
||
183 | $this->special_code = $objp->special_code; |
||
184 | $this->rang = $objp->rang; |
||
185 | |||
186 | $this->ref = $objp->product_ref; // deprecated |
||
187 | |||
188 | $this->product_ref = $objp->product_ref; |
||
189 | $this->product_label = $objp->product_label; |
||
190 | $this->product_desc = $objp->product_desc; |
||
191 | $this->product_tobatch = $objp->product_tobatch; |
||
192 | $this->fk_unit = $objp->fk_unit; |
||
193 | |||
194 | $this->date_start = $this->db->jdate($objp->date_start); |
||
195 | $this->date_end = $this->db->jdate($objp->date_end); |
||
196 | |||
197 | $this->fk_multicurrency = $objp->fk_multicurrency; |
||
198 | $this->multicurrency_code = $objp->multicurrency_code; |
||
199 | $this->multicurrency_subprice = $objp->multicurrency_subprice; |
||
200 | $this->multicurrency_total_ht = $objp->multicurrency_total_ht; |
||
201 | $this->multicurrency_total_tva = $objp->multicurrency_total_tva; |
||
202 | $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; |
||
203 | |||
204 | $this->db->free($result); |
||
205 | |||
206 | return 1; |
||
207 | } else { |
||
208 | $this->error = $this->db->lasterror(); |
||
209 | return -1; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Delete line in database |
||
215 | * |
||
216 | * @param User $user User that modify |
||
217 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
218 | * @return int Return integer <0 si ko, >0 si ok |
||
219 | */ |
||
220 | public function delete(User $user, $notrigger = 0) |
||
221 | { |
||
222 | global $conf, $langs; |
||
223 | |||
224 | $error = 0; |
||
225 | |||
226 | if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility |
||
227 | $this->id = $this->rowid; |
||
228 | } |
||
229 | |||
230 | // check if order line is not in a shipment line before deleting |
||
231 | $sqlCheckShipmentLine = "SELECT"; |
||
232 | $sqlCheckShipmentLine .= " ed.rowid"; |
||
233 | $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed"; |
||
234 | $sqlCheckShipmentLine .= " WHERE ed.fk_origin_line = " . ((int) $this->id); |
||
235 | |||
236 | $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine); |
||
237 | if (!$resqlCheckShipmentLine) { |
||
238 | $error++; |
||
239 | $this->error = $this->db->lasterror(); |
||
240 | $this->errors[] = $this->error; |
||
241 | } else { |
||
242 | $langs->load('errors'); |
||
243 | $num = $this->db->num_rows($resqlCheckShipmentLine); |
||
244 | if ($num > 0) { |
||
245 | $error++; |
||
246 | $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine); |
||
247 | $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid; |
||
248 | $this->errors[] = $this->error; |
||
249 | } |
||
250 | $this->db->free($resqlCheckShipmentLine); |
||
251 | } |
||
252 | if ($error) { |
||
253 | dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR); |
||
254 | return -1; |
||
255 | } |
||
256 | |||
257 | $this->db->begin(); |
||
258 | |||
259 | $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id); |
||
260 | |||
261 | dol_syslog("OrderLine::delete", LOG_DEBUG); |
||
262 | $resql = $this->db->query($sql); |
||
263 | if ($resql) { |
||
264 | if (!$error && !$notrigger) { |
||
265 | // Call trigger |
||
266 | $result = $this->call_trigger('LINEORDER_DELETE', $user); |
||
267 | if ($result < 0) { |
||
268 | $error++; |
||
269 | } |
||
270 | // End call triggers |
||
271 | } |
||
272 | |||
273 | // Remove extrafields |
||
274 | if (!$error) { |
||
275 | $result = $this->deleteExtraFields(); |
||
276 | if ($result < 0) { |
||
277 | $error++; |
||
278 | dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | if (!$error) { |
||
283 | $this->db->commit(); |
||
284 | return 1; |
||
285 | } |
||
286 | |||
287 | foreach ($this->errors as $errmsg) { |
||
288 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
289 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
290 | } |
||
291 | $this->db->rollback(); |
||
292 | return -1 * $error; |
||
293 | } else { |
||
294 | $this->error = $this->db->lasterror(); |
||
295 | return -1; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Insert line into database |
||
301 | * |
||
302 | * @param User $user User that modify |
||
303 | * @param int $notrigger 1 = disable triggers |
||
304 | * @return int Return integer <0 if KO, >0 if OK |
||
305 | */ |
||
306 | public function insert($user = null, $notrigger = 0) |
||
307 | { |
||
308 | $error = 0; |
||
309 | |||
310 | $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'. |
||
311 | |||
312 | dol_syslog(get_class($this) . "::insert rang=" . $this->rang); |
||
313 | |||
314 | // Clean parameters |
||
315 | if (empty($this->tva_tx)) { |
||
316 | $this->tva_tx = 0; |
||
317 | } |
||
318 | if (empty($this->localtax1_tx)) { |
||
319 | $this->localtax1_tx = 0; |
||
320 | } |
||
321 | if (empty($this->localtax2_tx)) { |
||
322 | $this->localtax2_tx = 0; |
||
323 | } |
||
324 | if (empty($this->localtax1_type)) { |
||
325 | $this->localtax1_type = 0; |
||
326 | } |
||
327 | if (empty($this->localtax2_type)) { |
||
328 | $this->localtax2_type = 0; |
||
329 | } |
||
330 | if (empty($this->total_localtax1)) { |
||
331 | $this->total_localtax1 = 0; |
||
332 | } |
||
333 | if (empty($this->total_localtax2)) { |
||
334 | $this->total_localtax2 = 0; |
||
335 | } |
||
336 | if (empty($this->rang)) { |
||
337 | $this->rang = 0; |
||
338 | } |
||
339 | if (empty($this->remise_percent)) { |
||
340 | $this->remise_percent = 0; |
||
341 | } |
||
342 | if (empty($this->info_bits)) { |
||
343 | $this->info_bits = 0; |
||
344 | } |
||
345 | if (empty($this->special_code)) { |
||
346 | $this->special_code = 0; |
||
347 | } |
||
348 | if (empty($this->fk_parent_line)) { |
||
349 | $this->fk_parent_line = 0; |
||
350 | } |
||
351 | if (empty($this->pa_ht)) { |
||
352 | $this->pa_ht = 0; |
||
353 | } |
||
354 | if (empty($this->ref_ext)) { |
||
355 | $this->ref_ext = ''; |
||
356 | } |
||
357 | |||
358 | // if buy price not defined, define buyprice as configured in margin admin |
||
359 | if ($this->pa_ht == 0 && $pa_ht_isemptystring) { |
||
360 | $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product); |
||
361 | if ($result < 0) { |
||
362 | return $result; |
||
363 | } else { |
||
364 | $this->pa_ht = $result; |
||
365 | } |
||
366 | } |
||
367 | |||
368 | // Check parameters |
||
369 | if ($this->product_type < 0) { |
||
370 | return -1; |
||
371 | } |
||
372 | |||
373 | $this->db->begin(); |
||
374 | |||
375 | // Insertion dans base de la ligne |
||
376 | $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'commandedet'; |
||
377 | $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,'; |
||
378 | $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,'; |
||
379 | $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,'; |
||
380 | $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,'; |
||
381 | $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,'; |
||
382 | $sql .= ' fk_unit,'; |
||
383 | $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc'; |
||
384 | $sql .= ')'; |
||
385 | $sql .= " VALUES (" . $this->fk_commande . ","; |
||
386 | $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->db->escape($this->fk_parent_line) . "'" : "null") . ","; |
||
387 | $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ","; |
||
388 | $sql .= " '" . $this->db->escape($this->desc) . "',"; |
||
389 | $sql .= " '" . price2num($this->qty) . "',"; |
||
390 | $sql .= " '" . $this->db->escape($this->ref_ext) . "',"; |
||
391 | $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ","; |
||
392 | $sql .= " '" . price2num($this->tva_tx) . "',"; |
||
393 | $sql .= " '" . price2num($this->localtax1_tx) . "',"; |
||
394 | $sql .= " '" . price2num($this->localtax2_tx) . "',"; |
||
395 | $sql .= " '" . $this->db->escape($this->localtax1_type) . "',"; |
||
396 | $sql .= " '" . $this->db->escape($this->localtax2_type) . "',"; |
||
397 | $sql .= ' ' . ((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null") . ','; |
||
398 | $sql .= " '" . $this->db->escape($this->product_type) . "',"; |
||
399 | $sql .= " '" . price2num($this->remise_percent) . "',"; |
||
400 | $sql .= " " . (price2num($this->subprice) !== '' ? price2num($this->subprice) : "null") . ","; |
||
401 | $sql .= " " . ($this->price != '' ? "'" . price2num($this->price) . "'" : "null") . ","; |
||
402 | $sql .= ' ' . (!empty($this->fk_remise_except) ? $this->fk_remise_except : "null") . ','; |
||
403 | $sql .= ' ' . ((int) $this->special_code) . ','; |
||
404 | $sql .= ' ' . ((int) $this->rang) . ','; |
||
405 | $sql .= ' ' . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "null") . ','; |
||
406 | $sql .= ' ' . price2num($this->pa_ht) . ','; |
||
407 | $sql .= " " . ((int) $this->info_bits) . ","; |
||
408 | $sql .= " " . price2num($this->total_ht, 'MT') . ","; |
||
409 | $sql .= " " . price2num($this->total_tva, 'MT') . ","; |
||
410 | $sql .= " " . price2num($this->total_localtax1, 'MT') . ","; |
||
411 | $sql .= " " . price2num($this->total_localtax2, 'MT') . ","; |
||
412 | $sql .= " " . price2num($this->total_ttc, 'MT') . ","; |
||
413 | $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ','; |
||
414 | $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ','; |
||
415 | $sql .= ' ' . (!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit)); |
||
416 | $sql .= ", " . (!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL'); |
||
417 | $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'"; |
||
418 | $sql .= ", " . price2num($this->multicurrency_subprice, 'CU'); |
||
419 | $sql .= ", " . price2num($this->multicurrency_total_ht, 'CT'); |
||
420 | $sql .= ", " . price2num($this->multicurrency_total_tva, 'CT'); |
||
421 | $sql .= ", " . price2num($this->multicurrency_total_ttc, 'CT'); |
||
422 | $sql .= ')'; |
||
423 | |||
424 | dol_syslog(get_class($this) . "::insert", LOG_DEBUG); |
||
425 | $resql = $this->db->query($sql); |
||
426 | if ($resql) { |
||
427 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'commandedet'); |
||
428 | $this->rowid = $this->id; |
||
429 | |||
430 | if (!$error) { |
||
431 | $result = $this->insertExtraFields(); |
||
432 | if ($result < 0) { |
||
433 | $error++; |
||
434 | } |
||
435 | } |
||
436 | |||
437 | if (!$error && !$notrigger) { |
||
438 | // Call trigger |
||
439 | $result = $this->call_trigger('LINEORDER_INSERT', $user); |
||
440 | if ($result < 0) { |
||
441 | $error++; |
||
442 | } |
||
443 | // End call triggers |
||
444 | } |
||
445 | |||
446 | if (!$error) { |
||
447 | $this->db->commit(); |
||
448 | return 1; |
||
449 | } |
||
450 | |||
451 | foreach ($this->errors as $errmsg) { |
||
452 | dol_syslog(get_class($this) . "::insert " . $errmsg, LOG_ERR); |
||
453 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
454 | } |
||
455 | $this->db->rollback(); |
||
456 | return -1 * $error; |
||
457 | } else { |
||
458 | $this->error = $this->db->error(); |
||
459 | $this->db->rollback(); |
||
460 | return -2; |
||
461 | } |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Update the line object into db |
||
466 | * |
||
467 | * @param User $user User that modify |
||
468 | * @param int $notrigger 1 = disable triggers |
||
469 | * @return int Return integer <0 si ko, >0 si ok |
||
470 | */ |
||
471 | public function update(User $user, $notrigger = 0) |
||
624 | } |
||
625 | } |
||
626 | |||
627 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
628 | /** |
||
629 | * Update DB line fields total_xxx |
||
630 | * Used by migration |
||
631 | * |
||
632 | * @return int Return integer <0 if KO, >0 if OK |
||
633 | */ |
||
634 | public function update_total() |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.