Conditions | 88 |
Paths | > 20000 |
Total Lines | 388 |
Code Lines | 238 |
Lines | 4 |
Ratio | 1.03 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
389 | public function computeCart( |
||
390 | &$cartForTemplate, |
||
391 | &$emptyCart, |
||
392 | &$shippingAmount, |
||
393 | &$commandAmount, |
||
394 | &$vatAmount, |
||
395 | &$goOn, |
||
396 | &$commandAmountTTC, |
||
397 | &$discountsDescription, |
||
398 | &$discountsCount |
||
399 | ) { |
||
400 | $emptyCart = false; |
||
401 | $goOn = ''; |
||
402 | $vats = array(); |
||
403 | $cpt = 0; |
||
404 | $discountsCount = 0; |
||
405 | $this->cart = isset($_SESSION[OledrionOledrion_caddyHandler::CADDY_NAME]) ? $_SESSION[OledrionOledrion_caddyHandler::CADDY_NAME] : array(); |
||
406 | $cartCount = count($this->cart); |
||
407 | if ($cartCount == 0) { |
||
408 | $emptyCart = true; |
||
409 | |||
410 | return true; |
||
411 | } |
||
412 | |||
413 | // Réinitialisation des données privées |
||
414 | $this->initializePrivateData(); |
||
415 | // Chargement des objets produits associés aux produits du panier et calcul des quantités par catégorie |
||
416 | $this->loadProductsAssociatedToCart(); |
||
417 | // Chargement des TVA |
||
418 | if (!isset($_POST['cmd_country']) || empty($_POST['cmd_country'])) { |
||
419 | $_POST['cmd_country'] = OLEDRION_DEFAULT_COUNTRY; |
||
420 | } |
||
421 | $vats = $this->handlers->h_oledrion_vat->getCountryVats($_POST['cmd_country']); |
||
422 | $oledrion_Currency = &Oledrion_Currency::getInstance(); |
||
423 | $caddyCount = count($this->cart); |
||
424 | |||
425 | // Initialisation des totaux généraux (ht, tva et frais de port) |
||
426 | $totalHT = $totalVAT = $totalShipping = 0.0; |
||
427 | |||
428 | // Boucle sur tous les produits et sur chacune des règles pour calculer le prix du produit (et ses frais de port) et voir si on doit y appliquer une réduction |
||
429 | foreach ($this->cart as $cartProduct) { |
||
430 | if ((float)$cartProduct['product']->getVar('product_discount_price', 'n') > 0) { |
||
431 | $ht = (float)$cartProduct['product']->getVar('product_discount_price', 'n'); |
||
432 | } else { |
||
433 | $ht = (float)$cartProduct['product']->getVar('product_price', 'n'); |
||
434 | } |
||
435 | // S'il y a des options, on rajoute leur montant |
||
436 | $productAttributes = array(); |
||
437 | View Code Duplication | if (is_array($cartProduct['attributes']) && count($cartProduct['attributes']) > 0) { |
|
438 | $ht += $this->handlers->h_oledrion_attributes->getProductOptionsPrice($cartProduct['attributes'], $cartProduct['product']->getVar('product_vat_id'), $productAttributes); |
||
439 | } |
||
440 | |||
441 | $discountedPrice = $ht; |
||
442 | $quantity = (int)$cartProduct['qty']; |
||
443 | |||
444 | if (Oledrion_utils::getModuleOption('shipping_quantity')) { |
||
445 | $discountedShipping = (float)($cartProduct['product']->getVar('product_shipping_price', 'n') * $quantity); |
||
446 | } else { |
||
447 | $discountedShipping = (float)$cartProduct['product']->getVar('product_shipping_price', 'n'); |
||
448 | } |
||
449 | $totalPrice = 0.0; |
||
450 | $reduction = ''; |
||
451 | |||
452 | ++$cpt; |
||
453 | if ($cpt == $caddyCount) { |
||
454 | // On arrive sur le dernier produit |
||
455 | $category = null; |
||
456 | $category = $this->handlers->h_oledrion_cat->get($cartProduct['product']->getVar('product_cid')); |
||
457 | if (is_object($category)) { |
||
458 | $goOn = $category->getLink(); |
||
459 | } |
||
460 | } |
||
461 | |||
462 | // Boucle sur les règles |
||
463 | foreach ($this->allActiveRules as $rule) { |
||
464 | $applyRule = false; |
||
465 | if (($rule->disc_group != 0 && Oledrion_utils::isMemberOfGroup($rule->disc_group)) |
||
466 | || $rule->disc_group == 0 |
||
467 | ) { |
||
468 | if (($rule->disc_cat_cid != 0 |
||
469 | && $cartProduct['product']->getVar('product_cid') == $rule->disc_cat_cid) |
||
470 | || $rule->disc_cat_cid == 0 |
||
471 | ) { |
||
472 | if (($rule->disc_vendor_id != 0 |
||
473 | && $cartProduct['product']->getVar('disc_vendor_id') == $rule->disc_vendor_id) |
||
474 | || $rule->disc_vendor_id == 0 |
||
475 | ) { |
||
476 | if (($rule->disc_product_id != 0 |
||
477 | && $cartProduct['product']->getVar('product_id') == $rule->disc_product_id) |
||
478 | || $rule->disc_product_id == 0 |
||
479 | ) { |
||
480 | // Dans quel cas appliquer la réduction ? |
||
481 | switch ($rule->disc_price_case) { |
||
482 | case OLEDRION_DISCOUNT_PRICE_CASE_ALL : |
||
483 | // Dans tous les cas |
||
484 | $applyRule = true; |
||
485 | break; |
||
486 | case OLEDRION_DISCOUNT_PRICE_CASE_FIRST_BUY : |
||
487 | // Si c'est le premier achat de l'utilisateur sur le site |
||
488 | if ($this->handlers->h_oledrion_commands->isFirstCommand()) { |
||
489 | $applyRule = true; |
||
490 | } |
||
491 | break; |
||
492 | case OLEDRION_DISCOUNT_PRICE_CASE_PRODUCT_NEVER : |
||
493 | // Si le produit n'a jamais été acheté par le client |
||
494 | if (!$this->handlers->h_oledrion_commands->productAlreadyBought(0, $cartProduct['product']->getVar('product_id'))) { |
||
495 | $applyRule = true; |
||
496 | } |
||
497 | break; |
||
498 | case OLEDRION_DISCOUNT_PRICE_CASE_QTY_IS : |
||
499 | // Si la quantité de produit est ... à ... |
||
500 | switch ($rule->disc_price_case_qty_cond) { |
||
501 | case OLEDRION_DISCOUNT_PRICE_QTY_COND1 : |
||
502 | // > |
||
503 | if ($cartProduct['qty'] > $rule->disc_price_case_qty_value) { |
||
504 | $applyRule = true; |
||
505 | } |
||
506 | break; |
||
507 | case OLEDRION_DISCOUNT_PRICE_QTY_COND2 : |
||
508 | // >= |
||
509 | if ($cartProduct['qty'] >= $rule->disc_price_case_qty_value) { |
||
510 | $applyRule = true; |
||
511 | } |
||
512 | break; |
||
513 | case OLEDRION_DISCOUNT_PRICE_QTY_COND3 : |
||
514 | // < |
||
515 | if ($cartProduct['qty'] < $rule->disc_price_case_qty_value) { |
||
516 | $applyRule = true; |
||
517 | } |
||
518 | break; |
||
519 | case OLEDRION_DISCOUNT_PRICE_QTY_COND4 : |
||
520 | // <= |
||
521 | if ($cartProduct['qty'] <= $rule->disc_price_case_qty_value) { |
||
522 | $applyRule = true; |
||
523 | } |
||
524 | break; |
||
525 | case OLEDRION_DISCOUNT_PRICE_QTY_COND5 : |
||
526 | // == |
||
527 | if ($cartProduct['qty'] == $rule->disc_price_case_qty_value) { |
||
528 | $applyRule = true; |
||
529 | } |
||
530 | break; |
||
531 | } |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | } |
||
536 | } |
||
537 | if ($applyRule) { |
||
538 | // Il faut appliquer la règle |
||
539 | // On calcule le nouveau prix ht du produit |
||
540 | switch ($rule->disc_price_type) { |
||
541 | case OLEDRION_DISCOUNT_PRICE_TYPE1 : |
||
542 | // Montant dégressif selon les quantités |
||
543 | if ($quantity >= $rule->disc_price_degress_l1qty1 |
||
544 | && $quantity <= $rule->disc_price_degress_l1qty2 |
||
545 | ) { |
||
546 | $discountedPrice = (float)$rule->getVar('disc_price_degress_l1total', 'n'); |
||
547 | } |
||
548 | if ($quantity >= $rule->disc_price_degress_l2qty1 |
||
549 | && $quantity <= $rule->disc_price_degress_l2qty2 |
||
550 | ) { |
||
551 | $discountedPrice = (float)$rule->getVar('disc_price_degress_l2total', 'n'); |
||
552 | } |
||
553 | if ($quantity >= $rule->disc_price_degress_l3qty1 |
||
554 | && $quantity <= $rule->disc_price_degress_l3qty2 |
||
555 | ) { |
||
556 | $discountedPrice = (float)$rule->getVar('disc_price_degress_l3total', 'n'); |
||
557 | } |
||
558 | if ($quantity >= $rule->disc_price_degress_l4qty1 |
||
559 | && $quantity <= $rule->disc_price_degress_l4qty2 |
||
560 | ) { |
||
561 | $discountedPrice = (float)$rule->getVar('disc_price_degress_l4total', 'n'); |
||
562 | } |
||
563 | if ($quantity >= $rule->disc_price_degress_l5qty1 |
||
564 | && $quantity <= $rule->disc_price_degress_l5qty2 |
||
565 | ) { |
||
566 | $discountedPrice = (float)$rule->getVar('disc_price_degress_l5total', 'n'); |
||
567 | } |
||
568 | $reduction = $rule->disc_description; |
||
569 | ++$discountsCount; |
||
570 | break; |
||
571 | |||
572 | case OLEDRION_DISCOUNT_PRICE_TYPE2 : |
||
573 | // D'un montant ou d'un pourcentage |
||
574 | if ($rule->disc_price_amount_on == OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_PRODUCT) { |
||
575 | // Réduction sur le produit |
||
576 | if ($rule->disc_price_amount_type == OLEDRION_DISCOUNT_PRICE_REDUCE_PERCENT) { |
||
577 | // Réduction en pourcentage |
||
578 | $discountedPrice = $this->getDiscountedPrice($discountedPrice, $rule->getVar('disc_price_amount_amount', 'n')); |
||
579 | } elseif ($rule->disc_price_amount_type == OLEDRION_DISCOUNT_PRICE_REDUCE_MONEY) { |
||
580 | // Réduction d'un montant en euros |
||
581 | $discountedPrice -= (float)$rule->getVar('disc_price_amount_amount', 'n'); |
||
582 | } |
||
583 | |||
584 | // Pas de montants négatifs |
||
585 | Oledrion_utils::doNotAcceptNegativeAmounts($discountedPrice); |
||
586 | $reduction = $rule->disc_description; |
||
587 | ++$discountsCount; |
||
588 | } elseif ($rule->disc_price_amount_on == OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_CART) { |
||
589 | // Règle à appliquer sur le panier |
||
590 | if (!isset($this->rulesForTheWhole[$rule->disc_id])) { |
||
591 | $this->rulesForTheWhole[$rule->disc_id] = $rule; |
||
592 | } |
||
593 | } |
||
594 | break; |
||
595 | } |
||
596 | |||
597 | // On passe au montant des frais de port |
||
598 | switch ($rule->disc_shipping_type) { |
||
599 | case OLEDRION_DISCOUNT_SHIPPING_TYPE1 : |
||
600 | // A payer dans leur intégralité, rien à faire |
||
601 | break; |
||
602 | case OLEDRION_DISCOUNT_SHIPPING_TYPE2 : |
||
603 | // Totalement gratuits si le client commande plus de X euros d'achat |
||
604 | if ($this->totalAmountBeforeDiscounts > $rule->disc_shipping_free_morethan) { |
||
605 | $discountedShipping = 0.0; |
||
606 | } |
||
607 | break; |
||
608 | case OLEDRION_DISCOUNT_SHIPPING_TYPE3 : |
||
609 | // Frais de port réduits de X euros si la commande est > x |
||
610 | if ($this->totalAmountBeforeDiscounts > $rule->disc_shipping_reduce_cartamount) { |
||
611 | $discountedShipping -= (float)$rule->getVar('disc_shipping_reduce_amount', 'n'); |
||
612 | } |
||
613 | // Pas de montants négatifs |
||
614 | Oledrion_utils::doNotAcceptNegativeAmounts($discountedShipping); |
||
615 | break; |
||
616 | case OLEDRION_DISCOUNT_SHIPPING_TYPE4 : |
||
617 | // Frais de port dégressifs |
||
618 | if ($quantity >= $rule->disc_shipping_degress_l1qty1 |
||
619 | && $quantity <= $rule->disc_shipping_degress_l1qty2 |
||
620 | ) { |
||
621 | $discountedShipping = (float)$rule->getVar('disc_shipping_degress_l1total', 'n'); |
||
622 | } |
||
623 | if ($quantity >= $rule->disc_shipping_degress_l2qty1 |
||
624 | && $quantity <= $rule->disc_shipping_degress_l2qty2 |
||
625 | ) { |
||
626 | $discountedShipping = (float)$rule->getVar('disc_shipping_degress_l2total', 'n'); |
||
627 | } |
||
628 | if ($quantity >= $rule->disc_shipping_degress_l3qty1 |
||
629 | && $quantity <= $rule->disc_shipping_degress_l3qty2 |
||
630 | ) { |
||
631 | $discountedShipping = (float)$rule->getVar('disc_shipping_degress_l3total', 'n'); |
||
632 | } |
||
633 | if ($quantity >= $rule->disc_shipping_degress_l4qty1 |
||
634 | && $quantity <= $rule->disc_shipping_degress_l4qty2 |
||
635 | ) { |
||
636 | $discountedShipping = (float)$rule->getVar('disc_shipping_degress_l4total', 'n'); |
||
637 | } |
||
638 | if ($quantity >= $rule->disc_shipping_degress_l5qty1 |
||
639 | && $quantity <= $rule->disc_shipping_degress_l5qty2 |
||
640 | ) { |
||
641 | $discountedShipping = (float)$rule->getVar('disc_shipping_degress_l5total', 'n'); |
||
642 | } |
||
643 | break; |
||
644 | } // Sélection du type de réduction sur les frais de port |
||
645 | } // Il faut appliquer la règle de réduction |
||
646 | }// Boucle sur les réductions |
||
647 | |||
648 | // Calcul de la TVA du produit |
||
649 | $vatId = $cartProduct['product']->getVar('product_vat_id'); |
||
650 | if (is_array($vats) && isset($vats[$vatId])) { |
||
651 | $vatRate = (float)$vats[$vatId]->getVar('vat_rate', 'n'); |
||
652 | $vatAmount = Oledrion_utils::getVAT($discountedPrice * $quantity, $vatRate); |
||
653 | } else { |
||
654 | $vatRate = 0.0; |
||
655 | $vatAmount = 0.0; |
||
656 | } |
||
657 | |||
658 | // Calcul du TTC du produit ((ht * qte) + tva + frais de port) |
||
659 | $totalPrice = (float)(($discountedPrice * $quantity) + $vatAmount + $discountedShipping); |
||
660 | |||
661 | // Les totaux généraux |
||
662 | $totalHT += ($discountedPrice * $quantity); |
||
663 | $totalVAT += $vatAmount; |
||
664 | $totalShipping += $discountedShipping; |
||
665 | |||
666 | // Recherche des éléments associés au produit |
||
667 | $associatedVendor = $associatedCategory = $associatedManufacturers = array(); |
||
668 | $manufacturersJoinList = ''; |
||
669 | // Le vendeur |
||
670 | if (isset($this->associatedVendors[$cartProduct['product']->product_vendor_id])) { |
||
671 | $associatedVendor = $this->associatedVendors[$cartProduct['product']->product_vendor_id]->toArray(); |
||
672 | } |
||
673 | |||
674 | // La catégorie |
||
675 | if (isset($this->associatedCategories[$cartProduct['product']->product_cid])) { |
||
676 | $associatedCategory = $this->associatedCategories[$cartProduct['product']->product_cid]->toArray(); |
||
677 | } |
||
678 | |||
679 | // Les fabricants |
||
680 | $product_id = $cartProduct['product']->product_id; |
||
681 | if (isset($this->associatedManufacturersPerProduct[$product_id])) { |
||
682 | // Recherche de la liste des fabricants associés à ce produit |
||
683 | $manufacturers = $this->associatedManufacturersPerProduct[$product_id]; |
||
684 | $manufacturersList = array(); |
||
685 | foreach ($manufacturers as $manufacturer_id) { |
||
686 | if (isset($this->associatedManufacturers[$manufacturer_id])) { |
||
687 | $associatedManufacturers[] = $this->associatedManufacturers[$manufacturer_id]->toArray(); |
||
688 | } |
||
689 | $manufacturersList[] = $this->associatedManufacturers[$manufacturer_id]->manu_commercialname . ' ' . $this->associatedManufacturers[$manufacturer_id]->manu_name; |
||
690 | } |
||
691 | $manufacturersJoinList = implode(OLEDRION_STRING_TO_JOIN_MANUFACTURERS, $manufacturersList); |
||
692 | } |
||
693 | $productTemplate = array(); |
||
694 | $productTemplate = $cartProduct['product']->toArray(); |
||
695 | $productTemplate['attributes'] = $productAttributes; |
||
696 | $productTemplate['number'] = $cartProduct['number']; |
||
697 | $productTemplate['id'] = $cartProduct['id']; |
||
698 | $productTemplate['product_qty'] = $cartProduct['qty']; |
||
699 | |||
700 | $productTemplate['unitBasePrice'] = $ht; |
||
701 | // Prix unitaire HT SANS réduction |
||
702 | $productTemplate['discountedPrice'] = $discountedPrice; |
||
703 | // Prix unitaire HT AVEC réduction |
||
704 | $productTemplate['discountedPriceWithQuantity'] = $discountedPrice * $quantity; |
||
705 | // Prix HT AVEC réduction et la quantité |
||
706 | // Les même prix mais formatés |
||
707 | $productTemplate['unitBasePriceFormated'] = $oledrion_Currency->amountForDisplay($ht); |
||
708 | // Prix unitaire HT SANS réduction |
||
709 | $productTemplate['discountedPriceFormated'] = $oledrion_Currency->amountForDisplay($discountedPrice); |
||
710 | // Prix unitaire HT AVEC réduction |
||
711 | $productTemplate['discountedPriceWithQuantityFormated'] = $oledrion_Currency->amountForDisplay($discountedPrice * $quantity); |
||
712 | // Prix HT AVEC réduction et la quantité |
||
713 | |||
714 | // Add by voltan |
||
715 | $productTemplate['discountedPriceFormatedOrg'] = $oledrion_Currency->amountForDisplay($ht - $discountedPrice); |
||
716 | $productTemplate['discountedPriceOrg'] = $ht - $discountedPrice; |
||
717 | |||
718 | $productTemplate['vatRate'] = $oledrion_Currency->amountInCurrency($vatRate); |
||
719 | $productTemplate['vatAmount'] = $vatAmount; |
||
720 | $productTemplate['normalShipping'] = $cartProduct['product']->getVar('product_shipping_price', 'n'); |
||
721 | $productTemplate['discountedShipping'] = $discountedShipping; |
||
722 | $productTemplate['totalPrice'] = $totalPrice; |
||
723 | $productTemplate['reduction'] = $reduction; |
||
724 | $productTemplate['templateProduct'] = $cartProduct['product']->toArray(); |
||
725 | |||
726 | $productTemplate['vatAmountFormated'] = $oledrion_Currency->amountInCurrency($vatAmount); |
||
727 | $productTemplate['normalShippingFormated'] = $oledrion_Currency->amountForDisplay($cartProduct['product']->getVar('product_shipping_price', 'n')); |
||
728 | $productTemplate['discountedShippingFormated'] = $oledrion_Currency->amountForDisplay($discountedShipping); |
||
729 | $productTemplate['totalPriceFormated'] = $oledrion_Currency->amountInCurrency($totalPrice); |
||
730 | $productTemplate['templateCategory'] = $associatedCategory; |
||
731 | $productTemplate['templateVendor'] = $associatedVendor; |
||
732 | $productTemplate['templateManufacturers'] = $associatedManufacturers; |
||
733 | $productTemplate['manufacturersJoinList'] = $manufacturersJoinList; |
||
734 | $this->cartForTemplate[] = $productTemplate; |
||
735 | }// foreach sur les produits du panier |
||
736 | |||
737 | // Traitement des règles générales s'il y en a |
||
738 | if (count($this->rulesForTheWhole) > 0) { |
||
739 | // $discountsDescription |
||
740 | foreach ($this->rulesForTheWhole as $rule) { |
||
741 | switch ($rule->disc_price_type) { |
||
742 | case OLEDRION_DISCOUNT_PRICE_TYPE2 : |
||
743 | // D'un montant ou d'un pourcentage |
||
744 | if ($rule->disc_price_amount_on == OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_CART) { |
||
745 | // Règle à appliquer sur le panier |
||
746 | if ($rule->disc_price_amount_type == OLEDRION_DISCOUNT_PRICE_REDUCE_PERCENT) { |
||
747 | // Réduction en pourcentage |
||
748 | $totalHT = $this->getDiscountedPrice($totalHT, $rule->getVar('disc_price_amount_amount')); |
||
749 | $totalVAT = $this->getDiscountedPrice($totalVAT, $rule->getVar('disc_price_amount_amount')); |
||
750 | } elseif ($rule->disc_price_amount_type == OLEDRION_DISCOUNT_PRICE_REDUCE_MONEY) { |
||
751 | // Réduction d'un montant en euros |
||
752 | $totalHT -= (float)$rule->getVar('disc_price_amount_amount'); |
||
753 | $totalVAT -= (float)$rule->getVar('disc_price_amount_amount'); |
||
754 | } |
||
755 | |||
756 | // Pas de montants négatifs |
||
757 | Oledrion_utils::doNotAcceptNegativeAmounts($totalHT); |
||
758 | Oledrion_utils::doNotAcceptNegativeAmounts($totalVAT); |
||
759 | $discountsDescription[] = $rule->disc_description; |
||
760 | ++$discountsCount; |
||
761 | }// Règle à appliquer sur le panier |
||
762 | break; |
||
763 | } // Switch |
||
764 | } // Foreach |
||
765 | }// S'il y a des règles globales |
||
766 | // Les totaux "renvoyés" à l'appelant |
||
767 | $shippingAmount = $totalShipping; |
||
768 | $commandAmount = $totalHT; |
||
769 | |||
770 | $vatAmount = $totalVAT; |
||
771 | $commandAmountTTC = $totalHT + $totalVAT + $totalShipping; |
||
772 | |||
773 | $cartForTemplate = $this->cartForTemplate; |
||
774 | |||
775 | return true; |
||
776 | } |
||
777 | } |
||
778 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.