Total Complexity | 70 |
Total Lines | 549 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ExpeditionLigne 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 ExpeditionLigne, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class ExpeditionLigne extends GenericDocumentLine |
||
53 | { |
||
54 | /** |
||
55 | * @var string ID to identify managed object |
||
56 | */ |
||
57 | public $element = 'expeditiondet'; |
||
58 | |||
59 | /** |
||
60 | * @var string Name of table without prefix where object is stored |
||
61 | */ |
||
62 | public $table_element = 'expeditiondet'; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Id of the line. Duplicate of $id. |
||
67 | * |
||
68 | * @var int |
||
69 | * @deprecated |
||
70 | */ |
||
71 | public $line_id; // deprecated |
||
72 | |||
73 | /** |
||
74 | * @deprecated |
||
75 | * @see $fk_origin_line |
||
76 | */ |
||
77 | public $origin_line_id; |
||
78 | |||
79 | /** |
||
80 | * Code of object line that is origin of the shipment line. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | public $fk_origin; // Example: 'orderline' |
||
85 | |||
86 | /** |
||
87 | * @var int ID |
||
88 | */ |
||
89 | public $fk_origin_line; |
||
90 | |||
91 | /** |
||
92 | * @var int Id of shipment |
||
93 | */ |
||
94 | public $fk_expedition; |
||
95 | |||
96 | /** |
||
97 | * @var DoliDB Database handler. |
||
98 | */ |
||
99 | public $db; |
||
100 | |||
101 | /** |
||
102 | * @var float qty asked From llx_expeditiondet |
||
103 | */ |
||
104 | public $qty; |
||
105 | |||
106 | /** |
||
107 | * @var float qty shipped |
||
108 | */ |
||
109 | public $qty_shipped; |
||
110 | |||
111 | /** |
||
112 | * @var int Id of product |
||
113 | */ |
||
114 | public $fk_product; |
||
115 | |||
116 | // detail of lot and qty = array(id in llx_expeditiondet_batch, fk_expeditiondet, batch, qty, fk_origin_stock) |
||
117 | // We can use this to know warehouse planned to be used for each lot. |
||
118 | public $detail_batch; |
||
119 | |||
120 | // detail of warehouses and qty |
||
121 | // We can use this to know warehouse when there is no lot. |
||
122 | public $details_entrepot; |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @var int Id of warehouse |
||
127 | */ |
||
128 | public $entrepot_id; |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @var float qty asked From llx_commandedet or llx_propaldet |
||
133 | */ |
||
134 | public $qty_asked; |
||
135 | |||
136 | /** |
||
137 | * @deprecated |
||
138 | * @see $product_ref |
||
139 | */ |
||
140 | public $ref; |
||
141 | |||
142 | /** |
||
143 | * @var string product ref |
||
144 | */ |
||
145 | public $product_ref; |
||
146 | |||
147 | /** |
||
148 | * @deprecated |
||
149 | * @see $product_label |
||
150 | */ |
||
151 | public $libelle; |
||
152 | |||
153 | /** |
||
154 | * @var string product label |
||
155 | */ |
||
156 | public $product_label; |
||
157 | |||
158 | /** |
||
159 | * @var string product description |
||
160 | * @deprecated |
||
161 | * @see $product_desc |
||
162 | */ |
||
163 | public $desc; |
||
164 | |||
165 | /** |
||
166 | * @var string product description |
||
167 | */ |
||
168 | public $product_desc; |
||
169 | |||
170 | /** |
||
171 | * Type of the product. 0 for product, 1 for service |
||
172 | * @var int |
||
173 | */ |
||
174 | public $product_type = 0; |
||
175 | |||
176 | /** |
||
177 | * @var int rang of line |
||
178 | */ |
||
179 | public $rang; |
||
180 | |||
181 | /** |
||
182 | * @var float weight |
||
183 | */ |
||
184 | public $weight; |
||
185 | public $weight_units; |
||
186 | |||
187 | /** |
||
188 | * @var float length |
||
189 | */ |
||
190 | public $length; |
||
191 | public $length_units; |
||
192 | |||
193 | /** |
||
194 | * @var float width |
||
195 | */ |
||
196 | public $width; |
||
197 | public $width_units; |
||
198 | |||
199 | /** |
||
200 | * @var float height |
||
201 | */ |
||
202 | public $height; |
||
203 | public $height_units; |
||
204 | |||
205 | /** |
||
206 | * @var float surface |
||
207 | */ |
||
208 | public $surface; |
||
209 | public $surface_units; |
||
210 | |||
211 | /** |
||
212 | * @var float volume |
||
213 | */ |
||
214 | public $volume; |
||
215 | public $volume_units; |
||
216 | |||
217 | // Invoicing |
||
218 | public $remise_percent; |
||
219 | public $tva_tx; |
||
220 | |||
221 | /** |
||
222 | * @var float total without tax |
||
223 | */ |
||
224 | public $total_ht; |
||
225 | |||
226 | /** |
||
227 | * @var float total with tax |
||
228 | */ |
||
229 | public $total_ttc; |
||
230 | |||
231 | /** |
||
232 | * @var float total vat |
||
233 | */ |
||
234 | public $total_tva; |
||
235 | |||
236 | /** |
||
237 | * @var float total localtax 1 |
||
238 | */ |
||
239 | public $total_localtax1; |
||
240 | |||
241 | /** |
||
242 | * @var float total localtax 2 |
||
243 | */ |
||
244 | public $total_localtax2; |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Constructor |
||
249 | * |
||
250 | * @param DoliDB $db Database handler |
||
251 | */ |
||
252 | public function __construct($db) |
||
253 | { |
||
254 | $this->db = $db; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Load line expedition |
||
259 | * |
||
260 | * @param int $rowid Id line order |
||
261 | * @return int Return integer <0 if KO, >0 if OK |
||
262 | */ |
||
263 | public function fetch($rowid) |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Insert line into database |
||
290 | * |
||
291 | * @param User $user User that modify |
||
292 | * @param int $notrigger 1 = disable triggers |
||
293 | * @return int Return integer <0 if KO, line id >0 if OK |
||
294 | */ |
||
295 | public function insert($user, $notrigger = 0) |
||
296 | { |
||
297 | $error = 0; |
||
298 | |||
299 | // Check parameters |
||
300 | if (empty($this->fk_expedition) || empty($this->fk_origin_line) || !is_numeric($this->qty)) { |
||
301 | $this->error = 'ErrorMandatoryParametersNotProvided'; |
||
302 | return -1; |
||
303 | } |
||
304 | |||
305 | $this->db->begin(); |
||
306 | |||
307 | if (empty($this->rang)) { |
||
308 | $this->rang = 0; |
||
309 | } |
||
310 | |||
311 | // Rank to use |
||
312 | $ranktouse = $this->rang; |
||
313 | if ($ranktouse == -1) { |
||
314 | $rangmax = $this->line_max($this->fk_expedition); |
||
315 | $ranktouse = $rangmax + 1; |
||
316 | } |
||
317 | |||
318 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "expeditiondet ("; |
||
319 | $sql .= "fk_expedition"; |
||
320 | $sql .= ", fk_entrepot"; |
||
321 | $sql .= ", fk_origin_line"; |
||
322 | $sql .= ", qty"; |
||
323 | $sql .= ", rang"; |
||
324 | $sql .= ") VALUES ("; |
||
325 | $sql .= $this->fk_expedition; |
||
326 | $sql .= ", " . (empty($this->entrepot_id) ? 'NULL' : $this->entrepot_id); |
||
327 | $sql .= ", " . ((int) $this->fk_origin_line); |
||
328 | $sql .= ", " . price2num($this->qty, 'MS'); |
||
329 | $sql .= ", " . ((int) $ranktouse); |
||
330 | $sql .= ")"; |
||
331 | |||
332 | dol_syslog(get_class($this) . "::insert", LOG_DEBUG); |
||
333 | $resql = $this->db->query($sql); |
||
334 | if ($resql) { |
||
335 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "expeditiondet"); |
||
336 | |||
337 | if (!$error) { |
||
338 | $result = $this->insertExtraFields(); |
||
339 | if ($result < 0) { |
||
340 | $error++; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | if (!$error && !$notrigger) { |
||
345 | // Call trigger |
||
346 | $result = $this->call_trigger('LINESHIPPING_INSERT', $user); |
||
347 | if ($result < 0) { |
||
348 | $error++; |
||
349 | } |
||
350 | // End call triggers |
||
351 | } |
||
352 | |||
353 | if ($error) { |
||
354 | foreach ($this->errors as $errmsg) { |
||
355 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
356 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
357 | } |
||
358 | } |
||
359 | } else { |
||
360 | $error++; |
||
361 | } |
||
362 | |||
363 | if ($error) { |
||
364 | $this->db->rollback(); |
||
365 | return -1; |
||
366 | } else { |
||
367 | $this->db->commit(); |
||
368 | return $this->id; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Delete shipment line. |
||
374 | * |
||
375 | * @param User $user User that modify |
||
376 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
377 | * @return int >0 if OK, <0 if KO |
||
378 | */ |
||
379 | public function delete($user = null, $notrigger = 0) |
||
380 | { |
||
381 | $error = 0; |
||
382 | |||
383 | $this->db->begin(); |
||
384 | |||
385 | // delete batch expedition line |
||
386 | if (isModEnabled('productbatch')) { |
||
387 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "expeditiondet_batch"; |
||
388 | $sql .= " WHERE fk_expeditiondet = " . ((int) $this->id); |
||
389 | |||
390 | if (!$this->db->query($sql)) { |
||
391 | $this->errors[] = $this->db->lasterror() . " - sql=$sql"; |
||
392 | $error++; |
||
393 | } |
||
394 | } |
||
395 | |||
396 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "expeditiondet"; |
||
397 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
398 | |||
399 | if (!$error && $this->db->query($sql)) { |
||
400 | // Remove extrafields |
||
401 | if (!$error) { |
||
402 | $result = $this->deleteExtraFields(); |
||
403 | if ($result < 0) { |
||
404 | $this->errors[] = $this->error; |
||
405 | $error++; |
||
406 | } |
||
407 | } |
||
408 | if (!$error && !$notrigger) { |
||
409 | // Call trigger |
||
410 | $result = $this->call_trigger('LINESHIPPING_DELETE', $user); |
||
411 | if ($result < 0) { |
||
412 | $this->errors[] = $this->error; |
||
413 | $error++; |
||
414 | } |
||
415 | // End call triggers |
||
416 | } |
||
417 | } else { |
||
418 | $this->errors[] = $this->db->lasterror() . " - sql=$sql"; |
||
419 | $error++; |
||
420 | } |
||
421 | |||
422 | if (!$error) { |
||
423 | $this->db->commit(); |
||
424 | return 1; |
||
425 | } else { |
||
426 | foreach ($this->errors as $errmsg) { |
||
427 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
428 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
429 | } |
||
430 | $this->db->rollback(); |
||
431 | return -1 * $error; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Update a line in database |
||
437 | * |
||
438 | * @param User $user User that modify |
||
439 | * @param int $notrigger 1 = disable triggers |
||
440 | * @return int Return integer < 0 if KO, > 0 if OK |
||
441 | */ |
||
442 | public function update($user = null, $notrigger = 0) |
||
601 | } |
||
602 | } |
||
603 | } |
||
604 |