| Total Complexity | 95 |
| Total Lines | 777 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SupplierInvoiceLine 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 SupplierInvoiceLine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class SupplierInvoiceLine extends CommonObjectLine |
||
| 57 | { |
||
| 58 | /** |
||
| 59 | * @var string ID to identify managed object |
||
| 60 | */ |
||
| 61 | public $element = 'facture_fourn_det'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string Name of table without prefix where object is stored |
||
| 65 | */ |
||
| 66 | public $table_element = 'facture_fourn_det'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @see CommonObjectLine |
||
| 70 | */ |
||
| 71 | public $parent_element = 'facture_fourn'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @see CommonObjectLine |
||
| 75 | */ |
||
| 76 | public $fk_parent_attribute = 'fk_facture_fourn'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var static |
||
| 80 | */ |
||
| 81 | public $oldline; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @deprecated |
||
| 85 | * @see $product_ref |
||
| 86 | */ |
||
| 87 | public $ref; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Internal ref |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | public $product_ref; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Supplier reference of price when we added the line. May have been changed after line was added. |
||
| 97 | * TODO Rename field ref to ref_supplier into table llx_facture_fourn_det and llx_commande_fournisseurdet and update fields into updateline |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | public $ref_supplier; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Product description |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | public $product_desc; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Unit price before taxes |
||
| 110 | * @var float |
||
| 111 | * @deprecated Use $subprice |
||
| 112 | * @see $subprice |
||
| 113 | */ |
||
| 114 | public $pu_ht; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Unit price excluded taxes |
||
| 118 | * @var float |
||
| 119 | */ |
||
| 120 | public $subprice; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Unit price included taxes |
||
| 124 | * @var float |
||
| 125 | */ |
||
| 126 | public $pu_ttc; |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Id of the corresponding supplier invoice |
||
| 131 | * @var int |
||
| 132 | */ |
||
| 133 | public $fk_facture_fourn; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * This field may contains label of line (when invoice create from order) |
||
| 137 | * @var string |
||
| 138 | * @deprecated Use $product_label |
||
| 139 | */ |
||
| 140 | public $label; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Description of the line |
||
| 144 | * @var string |
||
| 145 | * @deprecated Use $desc |
||
| 146 | */ |
||
| 147 | public $description; |
||
| 148 | |||
| 149 | public $date_start; |
||
| 150 | public $date_end; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var int |
||
| 154 | */ |
||
| 155 | public $fk_code_ventilation; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var int<0,1> |
||
| 159 | */ |
||
| 160 | public $skip_update_total; // Skip update price total for special lines |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var float Situation progress percentage |
||
| 164 | */ |
||
| 165 | public $situation_percent; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var int Previous situation line id reference |
||
| 169 | */ |
||
| 170 | public $fk_prev_id; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * VAT code |
||
| 174 | * @var string |
||
| 175 | */ |
||
| 176 | public $vat_src_code; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * VAT % |
||
| 180 | * @var float |
||
| 181 | */ |
||
| 182 | public $tva_tx; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Local tax 1 % |
||
| 186 | * @var float |
||
| 187 | */ |
||
| 188 | public $localtax1_tx; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Local tax 2 % |
||
| 192 | * @var float |
||
| 193 | */ |
||
| 194 | public $localtax2_tx; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Quantity |
||
| 198 | * @var float |
||
| 199 | */ |
||
| 200 | public $qty; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Percent of discount |
||
| 204 | * @var float |
||
| 205 | */ |
||
| 206 | public $remise_percent; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Buying price value |
||
| 210 | * @var float |
||
| 211 | */ |
||
| 212 | public $pa_ht; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Total amount without taxes |
||
| 216 | * @var float |
||
| 217 | */ |
||
| 218 | public $total_ht; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Total amount with taxes |
||
| 222 | * @var float |
||
| 223 | */ |
||
| 224 | public $total_ttc; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Total amount of taxes |
||
| 228 | * @var float |
||
| 229 | */ |
||
| 230 | public $total_tva; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Total local tax 1 amount |
||
| 234 | * @var float |
||
| 235 | */ |
||
| 236 | public $total_localtax1; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Total local tax 2 amount |
||
| 240 | * @var float |
||
| 241 | */ |
||
| 242 | public $total_localtax2; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @var int ID |
||
| 246 | */ |
||
| 247 | public $fk_product; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Type of the product. 0 for product 1 for service |
||
| 251 | * @var int |
||
| 252 | */ |
||
| 253 | public $product_type; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Label of the product |
||
| 257 | * @var string |
||
| 258 | */ |
||
| 259 | public $product_label; |
||
| 260 | |||
| 261 | /** |
||
| 262 | * List of cumulative options: |
||
| 263 | * Bit 0: 0 si TVA normal - 1 si TVA NPR |
||
| 264 | * Bit 1: 0 si ligne normal - 1 si bit discount (link to line into llx_remise_except) |
||
| 265 | * @var int |
||
| 266 | */ |
||
| 267 | public $info_bits; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Link to line into llx_remise_except |
||
| 271 | * @var int |
||
| 272 | */ |
||
| 273 | public $fk_remise_except; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @var int ID |
||
| 277 | */ |
||
| 278 | public $fk_parent_line; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @var int special code |
||
| 282 | */ |
||
| 283 | public $special_code; |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @var int rank of line |
||
| 287 | */ |
||
| 288 | public $rang; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Total local tax 1 amount |
||
| 292 | * @var float |
||
| 293 | */ |
||
| 294 | public $localtax1_type; |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Total local tax 2 amount |
||
| 298 | * @var float |
||
| 299 | */ |
||
| 300 | public $localtax2_type; |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Constructor |
||
| 305 | * |
||
| 306 | * @param DoliDB $db Database handler |
||
|
|
|||
| 307 | */ |
||
| 308 | public function __construct($db) |
||
| 309 | { |
||
| 310 | $this->db = $db; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Retrieves a supplier invoice line |
||
| 315 | * |
||
| 316 | * @param int $rowid Line id |
||
| 317 | * @return int Return integer <0 KO; 0 NOT FOUND; 1 OK |
||
| 318 | */ |
||
| 319 | public function fetch($rowid) |
||
| 320 | { |
||
| 321 | $sql = 'SELECT f.rowid, f.ref as ref_supplier, f.description as line_desc, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx'; |
||
| 322 | $sql .= ', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except'; |
||
| 323 | $sql .= ', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_facture_fourn, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit'; |
||
| 324 | $sql .= ', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc'; |
||
| 325 | $sql .= ', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc'; |
||
| 326 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture_fourn_det as f'; |
||
| 327 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON f.fk_product = p.rowid'; |
||
| 328 | $sql .= ' WHERE f.rowid = ' . ((int) $rowid); |
||
| 329 | $sql .= ' ORDER BY f.rang, f.rowid'; |
||
| 330 | |||
| 331 | $query = $this->db->query($sql); |
||
| 332 | |||
| 333 | if (!$query) { |
||
| 334 | $this->errors[] = $this->db->error(); |
||
| 335 | return -1; |
||
| 336 | } |
||
| 337 | |||
| 338 | if (!$this->db->num_rows($query)) { |
||
| 339 | return 0; |
||
| 340 | } |
||
| 341 | |||
| 342 | $obj = $this->db->fetch_object($query); |
||
| 343 | |||
| 344 | $this->id = $obj->rowid; |
||
| 345 | $this->rowid = $obj->rowid; |
||
| 346 | $this->fk_facture_fourn = $obj->fk_facture_fourn; |
||
| 347 | $this->description = $obj->line_desc; |
||
| 348 | $this->desc = $obj->line_desc; |
||
| 349 | $this->date_start = $obj->date_start; |
||
| 350 | $this->date_end = $obj->date_end; |
||
| 351 | $this->product_ref = $obj->product_ref; |
||
| 352 | $this->ref_supplier = $obj->ref_supplier; |
||
| 353 | $this->product_desc = $obj->product_desc; |
||
| 354 | |||
| 355 | $this->subprice = $obj->pu_ht; |
||
| 356 | $this->pu_ht = $this->subprice; |
||
| 357 | $this->pu_ttc = $obj->pu_ttc; |
||
| 358 | $this->tva_tx = $obj->tva_tx; |
||
| 359 | $this->localtax1_tx = $obj->localtax1_tx; |
||
| 360 | $this->localtax2_tx = $obj->localtax2_tx; |
||
| 361 | $this->localtax1_type = $obj->localtax1_type; |
||
| 362 | $this->localtax2_type = $obj->localtax2_type; |
||
| 363 | |||
| 364 | $this->qty = $obj->qty; |
||
| 365 | $this->remise_percent = $obj->remise_percent; |
||
| 366 | $this->fk_remise_except = $obj->fk_remise_except; |
||
| 367 | //$this->tva = $obj->total_tva; // deprecated |
||
| 368 | $this->total_ht = $obj->total_ht; |
||
| 369 | $this->total_tva = $obj->total_tva; |
||
| 370 | $this->total_localtax1 = $obj->total_localtax1; |
||
| 371 | $this->total_localtax2 = $obj->total_localtax2; |
||
| 372 | $this->total_ttc = $obj->total_ttc; |
||
| 373 | $this->fk_product = $obj->fk_product; |
||
| 374 | $this->product_type = $obj->product_type; |
||
| 375 | $this->product_label = $obj->product_label; |
||
| 376 | $this->label = $obj->product_label; |
||
| 377 | $this->info_bits = $obj->info_bits; |
||
| 378 | $this->fk_parent_line = $obj->fk_parent_line; |
||
| 379 | $this->special_code = $obj->special_code; |
||
| 380 | $this->rang = $obj->rang; |
||
| 381 | $this->fk_unit = $obj->fk_unit; |
||
| 382 | |||
| 383 | $this->multicurrency_subprice = $obj->multicurrency_subprice; |
||
| 384 | $this->multicurrency_total_ht = $obj->multicurrency_total_ht; |
||
| 385 | $this->multicurrency_total_tva = $obj->multicurrency_total_tva; |
||
| 386 | $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc; |
||
| 387 | |||
| 388 | $this->fetch_optionals(); |
||
| 389 | |||
| 390 | return 1; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Deletes a line |
||
| 395 | * |
||
| 396 | * @param int $notrigger 1=Does not execute triggers, 0=execute triggers |
||
| 397 | * @return int 0 if KO, 1 if OK |
||
| 398 | */ |
||
| 399 | public function delete($notrigger = 0) |
||
| 400 | { |
||
| 401 | global $user; |
||
| 402 | |||
| 403 | dol_syslog(get_class($this) . "::deleteline rowid=" . ((int) $this->id), LOG_DEBUG); |
||
| 404 | |||
| 405 | $error = 0; |
||
| 406 | |||
| 407 | $this->db->begin(); |
||
| 408 | |||
| 409 | if (!$notrigger) { |
||
| 410 | if ($this->call_trigger('LINEBILL_SUPPLIER_DELETE', $user) < 0) { |
||
| 411 | $error++; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | $this->deleteObjectLinked(); |
||
| 416 | |||
| 417 | // Remove extrafields |
||
| 418 | if (!$error) { |
||
| 419 | $result = $this->deleteExtraFields(); |
||
| 420 | if ($result < 0) { |
||
| 421 | $error++; |
||
| 422 | dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | if (!$error) { |
||
| 427 | // Supprime ligne |
||
| 428 | $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture_fourn_det '; |
||
| 429 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 430 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
| 431 | $resql = $this->db->query($sql); |
||
| 432 | if (!$resql) { |
||
| 433 | $error++; |
||
| 434 | $this->error = $this->db->lasterror(); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | if (!$error) { |
||
| 439 | $this->db->commit(); |
||
| 440 | return 1; |
||
| 441 | } else { |
||
| 442 | $this->db->rollback(); |
||
| 443 | return -1; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Update a supplier invoice line |
||
| 449 | * |
||
| 450 | * @param int $notrigger Disable triggers |
||
| 451 | * @return int Return integer <0 if KO, >0 if OK |
||
| 452 | */ |
||
| 453 | public function update($notrigger = 0) |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Insert line into database |
||
| 587 | * |
||
| 588 | * @param int $notrigger 1 no triggers |
||
| 589 | * @param int $noerrorifdiscountalreadylinked 1=Do not make error if lines is linked to a discount and discount already linked to another |
||
| 590 | * @return int Return integer <0 if KO, >0 if OK |
||
| 591 | */ |
||
| 592 | public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0) |
||
| 800 | } |
||
| 801 | } |
||
| 802 | |||
| 803 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 804 | /** |
||
| 805 | * Mise a jour de l'objet ligne de commande en base |
||
| 806 | * |
||
| 807 | * @return int Return integer <0 si ko, >0 si ok |
||
| 808 | */ |
||
| 809 | public function update_total() |
||
| 836 |