| Total Complexity | 124 |
| Total Lines | 752 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Reductions 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 Reductions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 71 | class Reductions |
||
| 72 | { |
||
| 73 | // Ne contient que la liste des règles actives au moment du calcul |
||
| 74 | private $allActiveRules = []; |
||
| 75 | |||
| 76 | // Nombre de produits par catégorie |
||
| 77 | private $categoriesProductsCount = []; |
||
| 78 | |||
| 79 | // Quantité de produits par catégorie |
||
| 80 | private $categoriesProductsQuantities = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * le caddy en mémoire |
||
| 84 | * $cart['number'] = Indice du produit |
||
| 85 | * $cart['id'] = Identifiant du produit |
||
| 86 | * $cart['qty'] = Quantité voulue |
||
| 87 | * $cart['product'] = L'objet produit correspondant au panier |
||
| 88 | */ |
||
| 89 | private $cart = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Le caddy pour le template. Consulter les détails du caddy dans la métode ComputeCart |
||
| 93 | */ |
||
| 94 | private $cartForTemplate = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Les règles à appliquer à la fin, sur l'intégralité du panier |
||
| 98 | */ |
||
| 99 | private $rulesForTheWhole = []; |
||
| 100 | |||
| 101 | // Le total des quantités de produits avant les réductions |
||
| 102 | private $totalProductsQuantities = 0; |
||
| 103 | // Montant total de la commande avant les réductions |
||
| 104 | private $totalAmountBeforeDiscounts = 0; |
||
| 105 | |||
| 106 | // Handlers vers les tables du module |
||
| 107 | // private $handlers; |
||
| 108 | |||
| 109 | // Les fabricants associés aux produits du panier |
||
| 110 | private $associatedManufacturers = []; |
||
| 111 | |||
| 112 | // Les vendeur associés aux produits du panier |
||
| 113 | private $associatedVendors = []; |
||
| 114 | |||
| 115 | // Les catégories associées aux produits du panier |
||
| 116 | private $associatedCategories = []; |
||
| 117 | |||
| 118 | // Fabricants associés par produit du panier |
||
| 119 | private $associatedManufacturersPerProduct = []; |
||
| 120 | |||
| 121 | // Attributs par produit du panier |
||
| 122 | private $associatedAttributesPerProduct = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Chargement des handlers et des règles actives |
||
| 126 | */ |
||
| 127 | public function __construct() |
||
| 128 | { |
||
| 129 | $this->initHandlers(); |
||
| 130 | $this->loadAllActiveRules(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Chargement des handlers |
||
| 135 | */ |
||
| 136 | private function initHandlers() |
||
| 137 | { |
||
| 138 | // $this->handlers = HandlerManager::getInstance(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Chargement de toutes les règles actives de réductions (sans date définie ou avec une période correspondante à aujourd'hui) |
||
| 143 | */ |
||
| 144 | public function loadAllActiveRules() |
||
| 145 | { |
||
| 146 | global $xoopsDB; |
||
| 147 | require_once dirname(__DIR__) . '/include/common.php'; |
||
| 148 | $critere = new \CriteriaCompo(); |
||
| 149 | $critere1 = new \CriteriaCompo(); |
||
| 150 | $critere1->add(new \Criteria('disc_date_from', 0, '=')); |
||
| 151 | $critere1->add(new \Criteria('disc_date_to', 0, '=')); |
||
| 152 | $critere->add($critere1); |
||
| 153 | |||
| 154 | $critere2 = new \CriteriaCompo(); |
||
| 155 | $critere2->add(new \Criteria('disc_date_from', time(), '<=')); |
||
| 156 | $critere2->add(new \Criteria('disc_date_to', time(), '>=')); |
||
| 157 | $critere->add($critere2, 'OR'); |
||
| 158 | |||
| 159 | // $this->allActiveRules = $this->handlers->h_oledrion_discounts->getObjects($critere); |
||
| 160 | // $this->allActiveRules = $this->handlers->DiscountsHandler->getObjects($critere); |
||
| 161 | $discountsHandler = new Oledrion\DiscountsHandler($xoopsDB); |
||
| 162 | $this->allActiveRules = $discountsHandler->getObjects($critere); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Calcul des quantités de produits par catégorie et du nombre de produits par catégorie |
||
| 167 | * |
||
| 168 | * @param Products $product |
||
| 169 | * @param int $quantity |
||
| 170 | */ |
||
| 171 | public function computePerCategories(Products $product, $quantity) |
||
| 172 | { |
||
| 173 | // Nombre de produits par catégories |
||
| 174 | if (isset($this->categoriesProductsCount[$product->product_cid])) { |
||
|
|
|||
| 175 | ++$this->categoriesProductsCount[$product->product_cid]; |
||
| 176 | } else { |
||
| 177 | $this->categoriesProductsCount[$product->product_cid] = 1; |
||
| 178 | } |
||
| 179 | |||
| 180 | // Mise à jour des quantités par catégories |
||
| 181 | if (isset($this->categoriesProductsQuantities[$product->product_cid])) { |
||
| 182 | $this->categoriesProductsQuantities[$product->product_cid] += $quantity; |
||
| 183 | } else { |
||
| 184 | $this->categoriesProductsQuantities[$product->product_cid] = $quantity; |
||
| 185 | } |
||
| 186 | $this->totalProductsQuantities += $quantity; |
||
| 187 | // Quantité totale de tous les produits |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Ajoute à un tableau interne, le fabricant associé à un produit |
||
| 192 | * |
||
| 193 | * @param Products $product |
||
| 194 | */ |
||
| 195 | private function addAssociatedManufacturers(Products $product) |
||
| 196 | { |
||
| 197 | if (!isset($this->associatedManufacturers[$product->product_id])) { |
||
| 198 | $this->associatedManufacturers[$product->product_id] = $product->product_id; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Recherche des attributs associés à chaque produit |
||
| 204 | * |
||
| 205 | * @param Products $product |
||
| 206 | * @param array $attributes |
||
| 207 | * @since 2.3 |
||
| 208 | */ |
||
| 209 | private function addAssociatedAttributes(Products $product, $attributes) |
||
| 210 | { |
||
| 211 | if (!isset($this->associatedAttributesPerProduct[$product->product_id])) { |
||
| 212 | $this->associatedAttributesPerProduct[$product->product_id] = $product->getProductsAttributesList($attributes); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Ajoute à un tableau interne, le vendeur associé à un produit |
||
| 218 | * |
||
| 219 | * @param Products $product |
||
| 220 | */ |
||
| 221 | private function addAssociatedVendors(Products $product) |
||
| 222 | { |
||
| 223 | if (!isset($this->associatedVendors[$product->product_vendor_id])) { |
||
| 224 | $this->associatedVendors[$product->product_vendor_id] = $product->product_vendor_id; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Ajoute à un tableau interne, la catégorie associée à un produit |
||
| 230 | * |
||
| 231 | * @param Products $product |
||
| 232 | */ |
||
| 233 | private function addAssociatedCategories(Products $product) |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Charge les fabricants associés aux produits du panier |
||
| 242 | */ |
||
| 243 | private function loadAssociatedManufacturers() |
||
| 244 | { |
||
| 245 | if (count($this->associatedManufacturers) > 0) { |
||
| 246 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 247 | $manufacturerHandler = new Oledrion\ManufacturerHandler($db); |
||
| 248 | $productsmanuHandler = new Oledrion\ProductsmanuHandler($db); |
||
| 249 | sort($this->associatedManufacturers); |
||
| 250 | $productsIds = $this->associatedManufacturers; |
||
| 251 | $this->associatedManufacturers = []; |
||
| 252 | // au cas où cela échouerait |
||
| 253 | $productsManufacturers = $manufacturersIds = []; |
||
| 254 | $productsManufacturers = $productsmanuHandler->getFromProductsIds($productsIds); |
||
| 255 | if (count($productsManufacturers) > 0) { |
||
| 256 | foreach ($productsManufacturers as $productManufacturer) { |
||
| 257 | if (!isset($manufacturersIds[$productManufacturer->pm_manu_id])) { |
||
| 258 | $manufacturersIds[$productManufacturer->pm_manu_id] = $productManufacturer->pm_manu_id; |
||
| 259 | } |
||
| 260 | $this->associatedManufacturersPerProduct[$productManufacturer->pm_product_id][] = $productManufacturer->pm_manu_id; |
||
| 261 | } |
||
| 262 | if (count($manufacturersIds) > 0) { |
||
| 263 | sort($manufacturersIds); |
||
| 264 | $this->associatedManufacturers = $manufacturerHandler->getManufacturersFromIds($manufacturersIds); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Charge la liste des vendeurs associés aux produits |
||
| 272 | */ |
||
| 273 | private function loadAssociatedVendors() |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Charge les catégories associées aux produits du panier |
||
| 287 | */ |
||
| 288 | private function loadAssociatedCategories() |
||
| 289 | { |
||
| 290 | if (count($this->associatedCategories) > 0) { |
||
| 291 | sort($this->associatedCategories); |
||
| 292 | $ids = $this->associatedCategories; |
||
| 293 | //mb $this->associatedCategories = $this->handlers->h_oledrion_cat->getCategoriesFromIds($ids); |
||
| 294 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 295 | $categoryHandler = new Oledrion\CategoryHandler($db); |
||
| 296 | $this->associatedCategories = $categoryHandler->getCategoriesFromIds($ids); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Recherche les fabricants, catégories et vendeurs associés à chaque produit |
||
| 302 | */ |
||
| 303 | public function loadElementsAssociatedToProducts() |
||
| 304 | { |
||
| 305 | $this->loadAssociatedManufacturers(); |
||
| 306 | $this->loadAssociatedVendors(); |
||
| 307 | $this->loadAssociatedCategories(); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Recherche les (objets) produits associés à chaque produit du panier (et lance le calcul des quantités) |
||
| 312 | */ |
||
| 313 | public function loadProductsAssociatedToCart() |
||
| 314 | { |
||
| 315 | $newCart = []; |
||
| 316 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 317 | $productsHandler = new Oledrion\ProductsHandler($db); |
||
| 318 | $attributesHandler = new Oledrion\AttributesHandler($db); |
||
| 319 | foreach ($this->cart as $cartProduct) { |
||
| 320 | $data = []; |
||
| 321 | $data['id'] = $cartProduct['id']; |
||
| 322 | $data['number'] = $cartProduct['number']; |
||
| 323 | $data['qty'] = $cartProduct['qty']; |
||
| 324 | $data['attributes'] = $cartProduct['attributes']; |
||
| 325 | |||
| 326 | $product = null; |
||
| 327 | $product = $productsHandler->get($data['id']); |
||
| 328 | if (!is_object($product)) { |
||
| 329 | trigger_error(_OLEDRION_ERROR9); |
||
| 330 | continue; |
||
| 331 | // Pour éviter le cas de la suppression d'un produit (dans l'admin) alors qu'un client l'a toujours dans son panier (et donc en session) |
||
| 332 | } |
||
| 333 | $data['product'] = $product; |
||
| 334 | // Mise à jour des calculs par catégorie |
||
| 335 | $this->computePerCategories($product, $data['qty']); |
||
| 336 | // Recherche des éléments associés à chaque produit |
||
| 337 | $this->addAssociatedManufacturers($product); |
||
| 338 | $this->addAssociatedVendors($product); |
||
| 339 | $this->addAssociatedAttributes($product, $data['attributes']); |
||
| 340 | $this->addAssociatedCategories($product); |
||
| 341 | |||
| 342 | // Calcul du total de la commande avant réductions |
||
| 343 | if ((float)$product->getVar('product_discount_price', 'n') > 0) { |
||
| 344 | $ht = (float)$product->getVar('product_discount_price', 'n'); |
||
| 345 | } else { |
||
| 346 | $ht = (float)$product->getVar('product_price', 'n'); |
||
| 347 | } |
||
| 348 | // S'il y a des options, on rajoute leur montant |
||
| 349 | if (is_array($data['attributes']) && count($data['attributes']) > 0) { |
||
| 350 | $ht += $attributesHandler->getProductOptionsPrice($data['attributes'], $product->getVar('product_vat_id')); |
||
| 351 | } |
||
| 352 | |||
| 353 | $this->totalAmountBeforeDiscounts += ($data['qty'] * $ht); |
||
| 354 | |||
| 355 | $newCart[] = $data; |
||
| 356 | } |
||
| 357 | $this->loadElementsAssociatedToProducts(); |
||
| 358 | $this->cart = $newCart; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Calcul du montant HT auquel on applique un pourcentage de réduction |
||
| 363 | * |
||
| 364 | * @param float $price Le prix auquel appliquer la réduction |
||
| 365 | * @param int $discount Le pourcentage de réduction |
||
| 366 | * @return float Le montant réduit |
||
| 367 | */ |
||
| 368 | private function getDiscountedPrice($price, $discount) |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Remise à zéro des membres internes |
||
| 375 | */ |
||
| 376 | private function initializePrivateData() |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Calcul de la facture en fonction du panier |
||
| 391 | * Contenu du panier en session : |
||
| 392 | * |
||
| 393 | * $datas['number'] = Indice du produit dans le panier |
||
| 394 | * $datas['id'] = Identifiant du produit dans la base |
||
| 395 | * $datas['qty'] = Quantité voulue |
||
| 396 | * $datas['attributes'] = Attributs produit array('attr_id' => id attribut, 'values' => array(valueId1, valueId2 ...)) |
||
| 397 | * |
||
| 398 | * En variable privé, le panier (dans $cart) contient la même chose + un objet 'oledrion_products' dans la clé 'product' |
||
| 399 | * |
||
| 400 | * @param array $cartForTemplate Contenu du caddy à passer au template (en fait la liste des produits) |
||
| 401 | * @param bool emptyCart Indique si le panier est vide ou pas |
||
| 402 | * @param float $shippingAmount Montant des frais de port |
||
| 403 | * @param float $commandAmount Montant HT de la commande |
||
| 404 | * @param float $vatAmount Montant de la TVA |
||
| 405 | * @param string $goOn Adresse vers laquelle renvoyer le visiteur après qu'il ait ajouté un produit dans son panier (cela correspond en fait à la catégorie du dernier produit ajouté dans le panier) |
||
| 406 | * @param float $commandAmountTTC Montant TTC de la commande |
||
| 407 | * @param array $discountsDescription Descriptions des remises GLOBALES appliquées (et pas les remises par produit !) |
||
| 408 | * @param int $discountsCount Le nombre TOTAL de réductions appliquées (individuellement ou sur la globalité du panier) |
||
| 409 | * B.R. @param array $checkoutAttributes |
||
| 410 | * TODO: Passer les paramètres sous forme d'objet |
||
| 411 | * @param mixed $emptyCart |
||
| 412 | * @param mixed $checkoutAttributes |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | |||
| 416 | // B.R. Added: $checkoutAttributes |
||
| 417 | public function computeCart( |
||
| 825 |