| Total Complexity | 65 |
| Total Lines | 549 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Facturation 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 Facturation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Facturation |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Attributs "volatiles" : reinitialises apres chaque traitement d'un article |
||
| 30 | * <p>Attributs "volatiles" : reinitialises apres chaque traitement d'un article</p> |
||
| 31 | * int $id => 'rowid' du produit dans llx_product |
||
| 32 | * string $ref => 'ref' du produit dans llx_product |
||
| 33 | * int $qte => Quantite pour le produit en cours de traitement |
||
| 34 | * int $stock => Stock theorique pour le produit en cours de traitement |
||
| 35 | * int $remise_percent => Remise en pourcent sur le produit en cours |
||
| 36 | * int $montant_remise => Remise en pourcent sur le produit en cours |
||
| 37 | * int $prix => Prix HT du produit en cours |
||
| 38 | * int $tva => 'rowid' du taux de tva dans llx_c_tva |
||
| 39 | */ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int ID |
||
| 43 | */ |
||
| 44 | public $id; |
||
| 45 | |||
| 46 | protected $ref; |
||
| 47 | protected $qte; |
||
| 48 | protected $stock; |
||
| 49 | protected $remise_percent; |
||
| 50 | protected $montant_remise; |
||
| 51 | protected $prix; |
||
| 52 | protected $tva; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Attributs persistants : utilises pour toute la duree de la vente (jusqu'a validation ou annulation) |
||
| 56 | * string $num_facture => Numero de la facture (de la forme FAYYMM-XXXX) |
||
| 57 | * string $mode_reglement => Mode de reglement (ESP, CB ou CHQ) |
||
| 58 | * int $montant_encaisse => Montant encaisse en cas de reglement en especes |
||
| 59 | * int $montant_rendu => Monnaie rendue en cas de reglement en especes |
||
| 60 | * int $paiement_le => Date de paiement en cas de paiement differe |
||
| 61 | * |
||
| 62 | * int $prix_total_ht => Prix total hors taxes |
||
| 63 | * int $montant_tva => Montant total de la TVA, tous taux confondus |
||
| 64 | * int $prix_total_ttc => Prix total TTC |
||
| 65 | */ |
||
| 66 | protected $num_facture; |
||
| 67 | protected $mode_reglement; |
||
| 68 | protected $montant_encaisse; |
||
| 69 | protected $montant_rendu; |
||
| 70 | protected $paiement_le; |
||
| 71 | |||
| 72 | protected $prix_total_ht; |
||
| 73 | protected $montant_tva; |
||
| 74 | protected $prix_total_ttc; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Constructor |
||
| 79 | */ |
||
| 80 | public function __construct() |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | // Data processing methods |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Add a product into cart |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function ajoutArticle() |
||
| 96 | { |
||
| 97 | global $conf, $db, $mysoc; |
||
| 98 | |||
| 99 | $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY']; |
||
| 100 | |||
| 101 | $societe = new Societe($db); |
||
| 102 | $societe->fetch($thirdpartyid); |
||
| 103 | |||
| 104 | $product = new Product($db); |
||
| 105 | $product->fetch($this->id); |
||
| 106 | |||
| 107 | |||
| 108 | $vatrowid = $this->tva(); |
||
| 109 | |||
| 110 | $tmp = getTaxesFromId($vatrowid); |
||
| 111 | $txtva = $tmp['rate'].(empty($tmp['code']) ? '' : ' ('.$tmp['code'].')'); |
||
| 112 | $vat_npr = $tmp['npr']; |
||
| 113 | |||
| 114 | $localtaxarray = getLocalTaxesFromRate($vatrowid, 0, $societe, $mysoc, 1); |
||
| 115 | |||
| 116 | // Clean vat code |
||
| 117 | $reg = array(); |
||
| 118 | $vat_src_code = ''; |
||
| 119 | if (preg_match('/\((.*)\)/', $txtva, $reg)) |
||
| 120 | { |
||
| 121 | $vat_src_code = $reg[1]; |
||
| 122 | $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. |
||
| 123 | } |
||
| 124 | |||
| 125 | // Define part of HT, VAT, TTC |
||
| 126 | $resultarray = calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray); |
||
| 127 | |||
| 128 | // Calculation of total HT without discount |
||
| 129 | $total_ht = $resultarray[0]; |
||
| 130 | $total_vat = $resultarray[1]; |
||
| 131 | $total_ttc = $resultarray[2]; |
||
| 132 | $total_localtax1 = $resultarray[9]; |
||
| 133 | $total_localtax2 = $resultarray[10]; |
||
| 134 | |||
| 135 | // Calculation of the discount amount |
||
| 136 | if ($this->remisePercent()) |
||
| 137 | { |
||
| 138 | $remise_percent = $this->remisePercent(); |
||
| 139 | } else { |
||
| 140 | $remise_percent = 0; |
||
| 141 | } |
||
| 142 | $montant_remise_ht = ($resultarray[6] - $resultarray[0]); |
||
| 143 | $this->amountDiscount($montant_remise_ht); |
||
| 144 | |||
| 145 | $newcartarray = $_SESSION['poscart']; |
||
| 146 | |||
| 147 | $i = 0; |
||
| 148 | if (!is_null($newcartarray) && !empty($newcartarray)) { |
||
| 149 | $i = count($newcartarray); |
||
| 150 | } |
||
| 151 | |||
| 152 | $newcartarray[$i]['id'] = $i; |
||
| 153 | $newcartarray[$i]['ref'] = $product->ref; |
||
| 154 | $newcartarray[$i]['label'] = $product->label; |
||
| 155 | $newcartarray[$i]['price'] = $product->price; |
||
| 156 | $newcartarray[$i]['price_ttc'] = $product->price_ttc; |
||
| 157 | |||
| 158 | if (!empty($conf->global->PRODUIT_MULTIPRICES)) |
||
| 159 | { |
||
| 160 | if (isset($product->multiprices[$societe->price_level])) |
||
| 161 | { |
||
| 162 | $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level]; |
||
| 163 | $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level]; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | $newcartarray[$i]['fk_article'] = $this->id; |
||
| 168 | $newcartarray[$i]['qte'] = $this->qte(); |
||
| 169 | $newcartarray[$i]['fk_tva'] = $this->tva(); // Vat rowid |
||
| 170 | $newcartarray[$i]['remise_percent'] = $remise_percent; |
||
| 171 | $newcartarray[$i]['remise'] = price2num($montant_remise_ht); |
||
| 172 | $newcartarray[$i]['total_ht'] = price2num($total_ht, 'MT'); |
||
| 173 | $newcartarray[$i]['total_ttc'] = price2num($total_ttc, 'MT'); |
||
| 174 | $newcartarray[$i]['total_vat'] = price2num($total_vat, 'MT'); |
||
| 175 | $newcartarray[$i]['total_localtax1'] = price2num($total_localtax1, 'MT'); |
||
| 176 | $newcartarray[$i]['total_localtax2'] = price2num($total_localtax2, 'MT'); |
||
| 177 | $_SESSION['poscart'] = $newcartarray; |
||
| 178 | |||
| 179 | $this->raz(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Remove a product from panel |
||
| 184 | * |
||
| 185 | * @param int $aArticle Id of line into cart to remove |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function supprArticle($aArticle) |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Calculation of total HT, total TTC and VAT amounts |
||
| 210 | * |
||
| 211 | * @return int Total |
||
| 212 | */ |
||
| 213 | public function calculTotaux() |
||
| 244 | //print 'total: '.$this->prix_total_ttc; exit; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Reset attributes |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | public function raz() |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Resetting persistent attributes |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | private function razPers() |
||
| 280 | } |
||
| 281 | |||
| 282 | |||
| 283 | // Methods for modifying protected attributes |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Getter for id |
||
| 287 | * |
||
| 288 | * @param int $aId Id |
||
| 289 | * @return int Id |
||
| 290 | */ |
||
| 291 | public function id($aId = null) |
||
| 292 | { |
||
| 293 | |||
| 294 | if (!$aId) |
||
| 295 | { |
||
| 296 | return $this->id; |
||
| 297 | } elseif ($aId == 'RESET') |
||
| 298 | { |
||
| 299 | $this->id = null; |
||
| 300 | } else { |
||
| 301 | $this->id = $aId; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Getter for ref |
||
| 307 | * |
||
| 308 | * @param string $aRef Ref |
||
| 309 | * @return string Ref |
||
| 310 | */ |
||
| 311 | public function ref($aRef = null) |
||
| 312 | { |
||
| 313 | |||
| 314 | if (is_null($aRef)) |
||
| 315 | { |
||
| 316 | return $this->ref; |
||
| 317 | } elseif ($aRef == 'RESET') |
||
| 318 | { |
||
| 319 | $this->ref = null; |
||
| 320 | } else { |
||
| 321 | $this->ref = $aRef; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Getter for qte |
||
| 327 | * |
||
| 328 | * @param int $aQte Qty |
||
| 329 | * @return int Qty |
||
| 330 | */ |
||
| 331 | public function qte($aQte = null) |
||
| 332 | { |
||
| 333 | if (is_null($aQte)) |
||
| 334 | { |
||
| 335 | return $this->qte; |
||
| 336 | } elseif ($aQte == 'RESET') |
||
| 337 | { |
||
| 338 | $this->qte = null; |
||
| 339 | } else { |
||
| 340 | $this->qte = $aQte; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Getter for stock |
||
| 346 | * |
||
| 347 | * @param string $aStock Stock |
||
| 348 | * @return string Stock |
||
| 349 | */ |
||
| 350 | public function stock($aStock = null) |
||
| 351 | { |
||
| 352 | |||
| 353 | if (is_null($aStock)) |
||
| 354 | { |
||
| 355 | return $this->stock; |
||
| 356 | } elseif ($aStock == 'RESET') |
||
| 357 | { |
||
| 358 | $this->stock = null; |
||
| 359 | } else { |
||
| 360 | $this->stock = $aStock; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Getter for remise_percent |
||
| 366 | * |
||
| 367 | * @param string $aRemisePercent Discount |
||
| 368 | * @return string Discount |
||
| 369 | */ |
||
| 370 | public function remisePercent($aRemisePercent = null) |
||
| 371 | { |
||
| 372 | |||
| 373 | if (is_null($aRemisePercent)) |
||
| 374 | { |
||
| 375 | return $this->remise_percent; |
||
| 376 | } elseif ($aRemisePercent == 'RESET') |
||
| 377 | { |
||
| 378 | $this->remise_percent = null; |
||
| 379 | } else { |
||
| 380 | $this->remise_percent = $aRemisePercent; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Getter for montant_remise |
||
| 386 | * |
||
| 387 | * @param int $aMontantRemise Amount |
||
| 388 | * @return string Amount |
||
| 389 | */ |
||
| 390 | public function amountDiscount($aMontantRemise = null) |
||
| 391 | { |
||
| 392 | |||
| 393 | if (is_null($aMontantRemise)) { |
||
| 394 | return $this->montant_remise; |
||
| 395 | } elseif ($aMontantRemise == 'RESET') { |
||
| 396 | $this->montant_remise = null; |
||
| 397 | } else { |
||
| 398 | $this->montant_remise = $aMontantRemise; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Getter for prix |
||
| 404 | * |
||
| 405 | * @param int $aPrix Price |
||
| 406 | * @return string Stock |
||
| 407 | */ |
||
| 408 | public function prix($aPrix = null) |
||
| 409 | { |
||
| 410 | |||
| 411 | if (is_null($aPrix)) { |
||
| 412 | return $this->prix; |
||
| 413 | } elseif ($aPrix == 'RESET') { |
||
| 414 | $this->prix = null; |
||
| 415 | } else { |
||
| 416 | $this->prix = $aPrix; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Getter for tva |
||
| 422 | * |
||
| 423 | * @param int $aTva Vat |
||
| 424 | * @return int Vat |
||
| 425 | */ |
||
| 426 | public function tva($aTva = null) |
||
| 427 | { |
||
| 428 | if (is_null($aTva)) { |
||
| 429 | return $this->tva; |
||
| 430 | } elseif ($aTva == 'RESET') { |
||
| 431 | $this->tva = null; |
||
| 432 | } else { |
||
| 433 | $this->tva = $aTva; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get num invoice |
||
| 439 | * |
||
| 440 | * @param string $aNumFacture Invoice ref |
||
| 441 | * @return string Invoice ref |
||
| 442 | */ |
||
| 443 | public function numInvoice($aNumFacture = null) |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get payment mode |
||
| 456 | * |
||
| 457 | * @param int $aModeReglement Payment mode |
||
| 458 | * @return int Payment mode |
||
| 459 | */ |
||
| 460 | public function getSetPaymentMode($aModeReglement = null) |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get amount |
||
| 474 | * |
||
| 475 | * @param int $aMontantEncaisse Amount |
||
| 476 | * @return int Amount |
||
| 477 | */ |
||
| 478 | public function amountCollected($aMontantEncaisse = null) |
||
| 479 | { |
||
| 480 | |||
| 481 | if (is_null($aMontantEncaisse)) { |
||
| 482 | return $this->montant_encaisse; |
||
| 483 | } elseif ($aMontantEncaisse == 'RESET') { |
||
| 484 | $this->montant_encaisse = null; |
||
| 485 | } else { |
||
| 486 | $this->montant_encaisse = $aMontantEncaisse; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get amount |
||
| 492 | * |
||
| 493 | * @param int $aMontantRendu Amount |
||
| 494 | * @return int Amount |
||
| 495 | */ |
||
| 496 | public function amountReturned($aMontantRendu = null) |
||
| 497 | { |
||
| 498 | |||
| 499 | if (is_null($aMontantRendu)) { |
||
| 500 | return $this->montant_rendu; |
||
| 501 | } elseif ($aMontantRendu == 'RESET') { |
||
| 502 | $this->montant_rendu = null; |
||
| 503 | } else { |
||
| 504 | $this->montant_rendu = $aMontantRendu; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get payment date |
||
| 510 | * |
||
| 511 | * @param integer $aPaiementLe Date |
||
| 512 | * @return integer Date |
||
| 513 | */ |
||
| 514 | public function paiementLe($aPaiementLe = null) |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get total HT |
||
| 527 | * |
||
| 528 | * @param int $aTotalHt Total amount |
||
| 529 | * @return int Total amount |
||
| 530 | */ |
||
| 531 | public function amountWithoutTax($aTotalHt = null) |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get amount vat |
||
| 544 | * |
||
| 545 | * @param int $aMontantTva Amount vat |
||
| 546 | * @return int Amount vat |
||
| 547 | */ |
||
| 548 | public function amountVat($aMontantTva = null) |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Get total TTC |
||
| 561 | * |
||
| 562 | * @param int $aTotalTtc Amount ttc |
||
| 563 | * @return int Amount ttc |
||
| 564 | */ |
||
| 565 | public function amountWithTax($aTotalTtc = null) |
||
| 578 |