Total Complexity | 98 |
Total Lines | 655 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like PropaleLigne 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 PropaleLigne, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class PropaleLigne extends CommonObjectLine |
||
60 | { |
||
61 | /** |
||
62 | * @var string ID to identify managed object |
||
63 | */ |
||
64 | public $element = 'propaldet'; |
||
65 | |||
66 | /** |
||
67 | * @var string Name of table without prefix where object is stored |
||
68 | */ |
||
69 | public $table_element = 'propaldet'; |
||
70 | |||
71 | /** |
||
72 | * @see CommonObjectLine |
||
73 | */ |
||
74 | public $parent_element = 'propal'; |
||
75 | |||
76 | /** |
||
77 | * @see CommonObjectLine |
||
78 | */ |
||
79 | public $fk_parent_attribute = 'fk_propal'; |
||
80 | |||
81 | public $oldline; |
||
82 | |||
83 | // From llx_propaldet |
||
84 | public $fk_propal; |
||
85 | public $fk_parent_line; |
||
86 | public $desc; // Description ligne |
||
87 | public $fk_product; // Id produit predefini |
||
88 | /** |
||
89 | * @deprecated |
||
90 | * @see $product_type |
||
91 | */ |
||
92 | public $fk_product_type; |
||
93 | /** |
||
94 | * Product type. |
||
95 | * @var int |
||
96 | * @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE |
||
97 | */ |
||
98 | public $product_type = Product::TYPE_PRODUCT; |
||
|
|||
99 | |||
100 | public $qty; |
||
101 | |||
102 | public $tva_tx; |
||
103 | public $vat_src_code; |
||
104 | |||
105 | /** |
||
106 | * Unit price before taxes |
||
107 | * @var float |
||
108 | */ |
||
109 | public $subprice; |
||
110 | public $remise_percent; |
||
111 | public $fk_remise_except; |
||
112 | |||
113 | public $rang = 0; |
||
114 | |||
115 | public $fk_fournprice; |
||
116 | public $pa_ht; |
||
117 | public $marge_tx; |
||
118 | public $marque_tx; |
||
119 | |||
120 | /** |
||
121 | * 1: frais de port |
||
122 | * 2: ecotaxe |
||
123 | * 3: option line (when qty = 0) |
||
124 | * @var int special code |
||
125 | */ |
||
126 | public $special_code; // Tag for special lines (exclusive tags) |
||
127 | |||
128 | public $info_bits = 0; // Some other info: |
||
129 | // Bit 0: 0 si TVA normal - 1 if TVA NPR |
||
130 | // Bit 1: 0 ligne normal - 1 if line with fixed discount |
||
131 | |||
132 | public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne |
||
133 | public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne |
||
134 | public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne |
||
135 | |||
136 | /** |
||
137 | * @deprecated |
||
138 | * @see $remise_percent, $fk_remise_except |
||
139 | */ |
||
140 | public $remise; |
||
141 | /** |
||
142 | * @deprecated |
||
143 | * @see $subprice |
||
144 | */ |
||
145 | public $price; |
||
146 | |||
147 | // From llx_product |
||
148 | /** |
||
149 | * @deprecated |
||
150 | * @see $product_ref |
||
151 | */ |
||
152 | public $ref; |
||
153 | /** |
||
154 | * Product reference |
||
155 | * @var string |
||
156 | */ |
||
157 | public $product_ref; |
||
158 | /** |
||
159 | * @deprecated |
||
160 | * @see $product_label |
||
161 | */ |
||
162 | public $libelle; |
||
163 | /** |
||
164 | * @deprecated |
||
165 | * @see $product_label |
||
166 | */ |
||
167 | public $label; |
||
168 | /** |
||
169 | * Product label |
||
170 | * @var string |
||
171 | */ |
||
172 | public $product_label; |
||
173 | /** |
||
174 | * Product description |
||
175 | * @var string |
||
176 | */ |
||
177 | public $product_desc; |
||
178 | |||
179 | /** |
||
180 | * Product use lot |
||
181 | * @var string |
||
182 | */ |
||
183 | public $product_tobatch; |
||
184 | |||
185 | /** |
||
186 | * Product barcode |
||
187 | * @var string |
||
188 | */ |
||
189 | public $product_barcode; |
||
190 | |||
191 | public $localtax1_tx; // Local tax 1 |
||
192 | public $localtax2_tx; // Local tax 2 |
||
193 | public $localtax1_type; // Local tax 1 type |
||
194 | public $localtax2_type; // Local tax 2 type |
||
195 | public $total_localtax1; // Line total local tax 1 |
||
196 | public $total_localtax2; // Line total local tax 2 |
||
197 | |||
198 | public $date_start; |
||
199 | public $date_end; |
||
200 | |||
201 | public $skip_update_total; // Skip update price total for special lines |
||
202 | |||
203 | // Multicurrency |
||
204 | public $fk_multicurrency; |
||
205 | public $multicurrency_code; |
||
206 | public $multicurrency_subprice; |
||
207 | public $multicurrency_total_ht; |
||
208 | public $multicurrency_total_tva; |
||
209 | public $multicurrency_total_ttc; |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Class line Constructor |
||
214 | * |
||
215 | * @param DoliDB $db Database handler |
||
216 | */ |
||
217 | public function __construct($db) |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Retrieve the propal line object |
||
224 | * |
||
225 | * @param int $rowid Propal line id |
||
226 | * @return int Return integer <0 if KO, >0 if OK |
||
227 | */ |
||
228 | public function fetch($rowid) |
||
229 | { |
||
230 | $sql = 'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,'; |
||
231 | $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,'; |
||
232 | $sql .= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,'; |
||
233 | $sql .= ' pd.fk_unit,'; |
||
234 | $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,'; |
||
235 | $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,'; |
||
236 | $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,'; |
||
237 | $sql .= ' pd.date_start, pd.date_end, pd.product_type'; |
||
238 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'propaldet as pd'; |
||
239 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON pd.fk_product = p.rowid'; |
||
240 | $sql .= ' WHERE pd.rowid = ' . ((int) $rowid); |
||
241 | |||
242 | $result = $this->db->query($sql); |
||
243 | if ($result) { |
||
244 | $objp = $this->db->fetch_object($result); |
||
245 | |||
246 | if ($objp) { |
||
247 | $this->id = $objp->rowid; |
||
248 | $this->rowid = $objp->rowid; // deprecated |
||
249 | $this->fk_propal = $objp->fk_propal; |
||
250 | $this->fk_parent_line = $objp->fk_parent_line; |
||
251 | $this->label = $objp->custom_label; |
||
252 | $this->desc = $objp->description; |
||
253 | $this->qty = $objp->qty; |
||
254 | $this->price = $objp->price; // deprecated |
||
255 | $this->subprice = $objp->subprice; |
||
256 | $this->vat_src_code = $objp->vat_src_code; |
||
257 | $this->tva_tx = $objp->tva_tx; |
||
258 | $this->remise = $objp->remise; // deprecated |
||
259 | $this->remise_percent = $objp->remise_percent; |
||
260 | $this->fk_remise_except = $objp->fk_remise_except; |
||
261 | $this->fk_product = $objp->fk_product; |
||
262 | $this->info_bits = $objp->info_bits; |
||
263 | |||
264 | $this->total_ht = $objp->total_ht; |
||
265 | $this->total_tva = $objp->total_tva; |
||
266 | $this->total_ttc = $objp->total_ttc; |
||
267 | |||
268 | $this->fk_fournprice = $objp->fk_fournprice; |
||
269 | |||
270 | $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht); |
||
271 | $this->pa_ht = $marginInfos[0]; |
||
272 | $this->marge_tx = $marginInfos[1]; |
||
273 | $this->marque_tx = $marginInfos[2]; |
||
274 | |||
275 | $this->special_code = $objp->special_code; |
||
276 | $this->product_type = $objp->product_type; |
||
277 | $this->rang = $objp->rang; |
||
278 | |||
279 | $this->ref = $objp->product_ref; // deprecated |
||
280 | $this->product_ref = $objp->product_ref; |
||
281 | $this->libelle = $objp->product_label; // deprecated |
||
282 | $this->product_label = $objp->product_label; |
||
283 | $this->product_desc = $objp->product_desc; |
||
284 | $this->fk_unit = $objp->fk_unit; |
||
285 | |||
286 | $this->date_start = $this->db->jdate($objp->date_start); |
||
287 | $this->date_end = $this->db->jdate($objp->date_end); |
||
288 | |||
289 | // Multicurrency |
||
290 | $this->fk_multicurrency = $objp->fk_multicurrency; |
||
291 | $this->multicurrency_code = $objp->multicurrency_code; |
||
292 | $this->multicurrency_subprice = $objp->multicurrency_subprice; |
||
293 | $this->multicurrency_total_ht = $objp->multicurrency_total_ht; |
||
294 | $this->multicurrency_total_tva = $objp->multicurrency_total_tva; |
||
295 | $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; |
||
296 | |||
297 | $this->fetch_optionals(); |
||
298 | |||
299 | $this->db->free($result); |
||
300 | |||
301 | return 1; |
||
302 | } else { |
||
303 | return 0; |
||
304 | } |
||
305 | } else { |
||
306 | return -1; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Insert object line propal in database |
||
312 | * |
||
313 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
314 | * @return int Return integer <0 if KO, >0 if OK |
||
315 | */ |
||
316 | public function insert($notrigger = 0) |
||
317 | { |
||
318 | global $conf, $user; |
||
319 | |||
320 | $error = 0; |
||
321 | |||
322 | dol_syslog(get_class($this) . "::insert rang=" . $this->rang); |
||
323 | |||
324 | $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'. |
||
325 | |||
326 | // Clean parameters |
||
327 | if (empty($this->tva_tx)) { |
||
328 | $this->tva_tx = 0; |
||
329 | } |
||
330 | if (empty($this->localtax1_tx)) { |
||
331 | $this->localtax1_tx = 0; |
||
332 | } |
||
333 | if (empty($this->localtax2_tx)) { |
||
334 | $this->localtax2_tx = 0; |
||
335 | } |
||
336 | if (empty($this->localtax1_type)) { |
||
337 | $this->localtax1_type = 0; |
||
338 | } |
||
339 | if (empty($this->localtax2_type)) { |
||
340 | $this->localtax2_type = 0; |
||
341 | } |
||
342 | if (empty($this->total_localtax1)) { |
||
343 | $this->total_localtax1 = 0; |
||
344 | } |
||
345 | if (empty($this->total_localtax2)) { |
||
346 | $this->total_localtax2 = 0; |
||
347 | } |
||
348 | if (empty($this->rang)) { |
||
349 | $this->rang = 0; |
||
350 | } |
||
351 | if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) { |
||
352 | $this->remise_percent = 0; |
||
353 | } |
||
354 | if (empty($this->info_bits)) { |
||
355 | $this->info_bits = 0; |
||
356 | } |
||
357 | if (empty($this->special_code)) { |
||
358 | $this->special_code = 0; |
||
359 | } |
||
360 | if (empty($this->fk_parent_line)) { |
||
361 | $this->fk_parent_line = 0; |
||
362 | } |
||
363 | if (empty($this->fk_fournprice)) { |
||
364 | $this->fk_fournprice = 0; |
||
365 | } |
||
366 | if (!is_numeric($this->qty)) { |
||
367 | $this->qty = 0; |
||
368 | } |
||
369 | if (empty($this->pa_ht)) { |
||
370 | $this->pa_ht = 0; |
||
371 | } |
||
372 | if (empty($this->multicurrency_subprice)) { |
||
373 | $this->multicurrency_subprice = 0; |
||
374 | } |
||
375 | if (empty($this->multicurrency_total_ht)) { |
||
376 | $this->multicurrency_total_ht = 0; |
||
377 | } |
||
378 | if (empty($this->multicurrency_total_tva)) { |
||
379 | $this->multicurrency_total_tva = 0; |
||
380 | } |
||
381 | if (empty($this->multicurrency_total_ttc)) { |
||
382 | $this->multicurrency_total_ttc = 0; |
||
383 | } |
||
384 | |||
385 | // if buy price not defined, define buyprice as configured in margin admin |
||
386 | if ($this->pa_ht == 0 && $pa_ht_isemptystring) { |
||
387 | if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) { |
||
388 | return $result; |
||
389 | } else { |
||
390 | $this->pa_ht = $result; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | // Check parameters |
||
395 | if ($this->product_type < 0) { |
||
396 | return -1; |
||
397 | } |
||
398 | |||
399 | $this->db->begin(); |
||
400 | |||
401 | // Insert line into database |
||
402 | $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'propaldet'; |
||
403 | $sql .= ' (fk_propal, fk_parent_line, label, description, fk_product, product_type,'; |
||
404 | $sql .= ' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,'; |
||
405 | $sql .= ' subprice, remise_percent, '; |
||
406 | $sql .= ' info_bits, '; |
||
407 | $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,'; |
||
408 | $sql .= ' fk_unit,'; |
||
409 | $sql .= ' date_start, date_end'; |
||
410 | $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)'; |
||
411 | $sql .= " VALUES (" . $this->fk_propal . ","; |
||
412 | $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->db->escape($this->fk_parent_line) . "'" : "null") . ","; |
||
413 | $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ","; |
||
414 | $sql .= " '" . $this->db->escape($this->desc) . "',"; |
||
415 | $sql .= " " . ($this->fk_product ? "'" . $this->db->escape($this->fk_product) . "'" : "null") . ","; |
||
416 | $sql .= " '" . $this->db->escape($this->product_type) . "',"; |
||
417 | $sql .= " " . ($this->fk_remise_except ? "'" . $this->db->escape($this->fk_remise_except) . "'" : "null") . ","; |
||
418 | $sql .= " " . price2num($this->qty, 'MS') . ","; |
||
419 | $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ","; |
||
420 | $sql .= " " . price2num($this->tva_tx) . ","; |
||
421 | $sql .= " " . price2num($this->localtax1_tx) . ","; |
||
422 | $sql .= " " . price2num($this->localtax2_tx) . ","; |
||
423 | $sql .= " '" . $this->db->escape($this->localtax1_type) . "',"; |
||
424 | $sql .= " '" . $this->db->escape($this->localtax2_type) . "',"; |
||
425 | $sql .= " " . (price2num($this->subprice) !== '' ? price2num($this->subprice, 'MU') : "null") . ","; |
||
426 | $sql .= " " . price2num($this->remise_percent) . ","; |
||
427 | $sql .= " " . (isset($this->info_bits) ? ((int) $this->info_bits) : "null") . ","; |
||
428 | $sql .= " " . price2num($this->total_ht, 'MT') . ","; |
||
429 | $sql .= " " . price2num($this->total_tva, 'MT') . ","; |
||
430 | $sql .= " " . price2num($this->total_localtax1, 'MT') . ","; |
||
431 | $sql .= " " . price2num($this->total_localtax2, 'MT') . ","; |
||
432 | $sql .= " " . price2num($this->total_ttc, 'MT') . ","; |
||
433 | $sql .= " " . (!empty($this->fk_fournprice) ? "'" . $this->db->escape($this->fk_fournprice) . "'" : "null") . ","; |
||
434 | $sql .= " " . (isset($this->pa_ht) ? "'" . price2num($this->pa_ht) . "'" : "null") . ","; |
||
435 | $sql .= ' ' . ((int) $this->special_code) . ','; |
||
436 | $sql .= ' ' . ((int) $this->rang) . ','; |
||
437 | $sql .= ' ' . (empty($this->fk_unit) ? 'NULL' : ((int) $this->fk_unit)) . ','; |
||
438 | $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ','; |
||
439 | $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null"); |
||
440 | $sql .= ", " . ($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null'); |
||
441 | $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'"; |
||
442 | $sql .= ", " . price2num($this->multicurrency_subprice, 'CU'); |
||
443 | $sql .= ", " . price2num($this->multicurrency_total_ht, 'CT'); |
||
444 | $sql .= ", " . price2num($this->multicurrency_total_tva, 'CT'); |
||
445 | $sql .= ", " . price2num($this->multicurrency_total_ttc, 'CT'); |
||
446 | $sql .= ')'; |
||
447 | |||
448 | dol_syslog(get_class($this) . '::insert', LOG_DEBUG); |
||
449 | $resql = $this->db->query($sql); |
||
450 | if ($resql) { |
||
451 | $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'propaldet'); |
||
452 | |||
453 | if (!$error) { |
||
454 | $this->id = $this->rowid; |
||
455 | $result = $this->insertExtraFields(); |
||
456 | if ($result < 0) { |
||
457 | $error++; |
||
458 | } |
||
459 | } |
||
460 | |||
461 | if (!$error && !$notrigger) { |
||
462 | // Call trigger |
||
463 | $result = $this->call_trigger('LINEPROPAL_INSERT', $user); |
||
464 | if ($result < 0) { |
||
465 | $this->db->rollback(); |
||
466 | return -1; |
||
467 | } |
||
468 | // End call triggers |
||
469 | } |
||
470 | |||
471 | $this->db->commit(); |
||
472 | return 1; |
||
473 | } else { |
||
474 | $this->error = $this->db->error() . " sql=" . $sql; |
||
475 | $this->db->rollback(); |
||
476 | return -1; |
||
477 | } |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Delete line in database |
||
482 | * |
||
483 | * @param User $user Object user |
||
484 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
485 | * @return int Return integer <0 if ko, >0 if ok |
||
486 | */ |
||
487 | public function delete(User $user, $notrigger = 0) |
||
528 | } |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Update propal line object into DB |
||
533 | * |
||
534 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
535 | * @return int Return integer <0 if ko, >0 if ok |
||
536 | */ |
||
537 | public function update($notrigger = 0) |
||
682 | } |
||
683 | } |
||
684 | |||
685 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
686 | /** |
||
687 | * Update DB line fields total_xxx |
||
688 | * Used by migration |
||
689 | * |
||
690 | * @return int Return integer <0 if KO, >0 if OK |
||
691 | */ |
||
692 | public function update_total() |
||
717 |