Passed
Push — master ( e643a0...1f75e2 )
by Michael
03:19
created

DiscountsHandler::getDiscountedPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Gestion des réductions
27
 */
28
29
30
/**
31
 * Class DiscountsHandler
32
 */
33
class DiscountsHandler extends OledrionPersistableObjectHandler
34
{
35
    /**
36
     * DiscountsHandler constructor.
37
     * @param \XoopsDatabase|null $db
38
     */
39
    public function __construct(\XoopsDatabase $db = null)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
    {
41
        //                        Table                   Classe              Id        Libellé
42
        parent::__construct($db, 'oledrion_discounts', Discounts::class, 'disc_id', 'disc_title');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Oledrion\Ol...tHandler::__construct() does only seem to accept XoopsDatabase, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'oledrion_discounts', Discounts::class, 'disc_id', 'disc_title');
Loading history...
43
    }
44
45
    /**
46
     * @param $price
47
     * @param $discount
48
     * @return mixed
49
     */
50
    private function getDiscountedPrice($price, $discount)
51
    {
52
        return $price - ($price * ($discount / 100));
53
    }
54
55
    /**
56
     * Retourne la liste des règles qui sont applicables sur la période courante
57
     * @param void
58
     * @return array objets de type Discounts
59
     */
60
    public function getRulesForThisPeriod()
61
    {
62
        static $buffer = [];
63
        if (is_array($buffer) && count($buffer) > 0) {
64
            return $buffer;
65
        }
66
67
        $critere = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        $critere->add(new \Criteria('disc_date_from', 0, '='));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
        $critere->add(new \Criteria('disc_date_to', 0, '='), 'OR');
70
71
        $critere2 = new \CriteriaCompo();
72
        $critere2->add(new \Criteria('disc_date_from', time(), '>='));
73
        $critere2->add(new \Criteria('disc_date_to', time(), '<='));
74
        $critere->add($critere2);
75
76
        $buffer = $this->getObjects($critere);
77
78
        return $buffer;
79
    }
80
81
    /**
82
     * @deprecated
83
     */
84
85
    /**
86
     * Renvoie la liste des règles à appliquer sur chaque produit (avec gestion de cache) pour l'utilisateur courant
87
     *
88
     * @return array Tableau d'objets de type Discounts
89
     */
90
    public function getRulesOnEachProduct()
91
    {
92
        static $buffer = [];
93
        if (is_array($buffer) && count($buffer) > 0) {
94
        } else {
95
            $groups  = Oledrion\Utility::getCurrentMemberGroups();
96
            $critere = new \CriteriaCompo();
97
            $critere->add(new \Criteria('disc_on_what', Constants::OLEDRION_DISCOUNT_ON3, '='));
98
            if (count($groups) > 0) {
99
                $critere->add(new \Criteria('disc_group', '(' . implode(',', $groups) . ')', 'IN'));
100
            }
101
            $buffer = $this->getObjects($critere);
102
        }
103
104
        return $buffer;
105
    }
106
107
    /**
108
     * Renvoie la liste des règles à appliquer sur tous les produits (avec gestion de cache) pour l'utilisateur courant
109
     *
110
     * @return array Tableau d'objets de type Discounts
111
     */
112
    public function getRulesOnAllProducts()
113
    {
114
        static $buffer = [];
115
        if (is_array($buffer) && count($buffer) > 0) {
116
        } else {
117
            $critere = new \CriteriaCompo();
118
            $critere->add(new \Criteria('disc_on_what', Constants::OLEDRION_DISCOUNT_ON2, '='));
119
            $tblGroups = Oledrion\Utility::getCurrentMemberGroups();
120
            $critere->add(new \Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
121
            $buffer = $this->getObjects($critere);
122
        }
123
124
        return $buffer;
125
    }
126
127
    /**
128
     * Renvoie la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
129
     *
130
     * @return array Tableau d'objets de type Discounts
131
     */
132
    public function getRulesOnShipping()
133
    {
134
        static $buffer = [];
135
        if (is_array($buffer) && count($buffer) > 0) {
136
        } else {
137
            $critere = new \CriteriaCompo();
138
            $critere->add(new \Criteria('disc_on_what', Constants::OLEDRION_DISCOUNT_ON4, '='));
139
            $tblGroups = Oledrion\Utility::getCurrentMemberGroups();
140
            $critere->add(new \Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
141
            $buffer = $this->getObjects($critere);
142
        }
143
144
        return $buffer;
145
    }
146
147
    /**
148
     * Renvoie la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
149
     *
150
     * @return array Tableau d'objets de type Discounts
151
     */
152
    public function getRulesOnShipping2()
153
    {
154
        static $buffer = [];
155
        if (is_array($buffer) && count($buffer) > 0) {
156
        } else {
157
            $critere = new \CriteriaCompo();
158
            $critere->add(new \Criteria('disc_on_what', Constants::OLEDRION_DISCOUNT_ON5, '='));
159
            $critere->add(new \Criteria('disc_shipping', Constants::OLEDRION_DISCOUNT_SHIPPING_TYPE2, '='));
160
            $tblGroups = Oledrion\Utility::getCurrentMemberGroups();
161
            $critere->add(new \Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
162
            $buffer = $this->getObjects($critere);
163
        }
164
165
        return $buffer;
166
    }
167
168
    /**
169
     * Renvoie la liste des règles à appliquer sur l'intégralité de la commande (avec gestion de cache) pour l'utilisateur courant
170
     *
171
     * @return array Tableau d'objets de type Discounts
172
     */
173
    public function getRulesOnCommand()
174
    {
175
        static $buffer = [];
176
        if (is_array($buffer) && count($buffer) > 0) {
177
        } else {
178
            $critere = new \CriteriaCompo();
179
            $critere->add(new \Criteria('disc_on_what', Constants::OLEDRION_DISCOUNT_ON1, '='));
180
            $tblGroups = Oledrion\Utility::getCurrentMemberGroups();
181
            $critere->add(new \Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
182
            $buffer = $this->getObjects($critere);
183
        }
184
185
        return $buffer;
186
    }
187
188
    /**
189
     * Deuxième lot de réductions, à appliquer sur les frais de port
190
     *
191
     * @param float $montantShipping      Montant des frais de port
192
     * @param float $commandAmount        Le montant total de la commande
193
     * @param array $discountsDescription Descriptions des réductions appliquées
194
     */
195
    public function applyDiscountOnShipping2(&$montantShipping, $commandAmount, &$discountsDescription)
196
    {
197
        $tblRules = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tblRules is dead and can be removed.
Loading history...
198
        $tblRules = $this->getRulesOnShipping2(); // Renvoie des objets Discounts
199
        if (count($tblRules) > 0) {
200
            foreach ($tblRules as $rule) {
201
                if ($commandAmount > (float)$rule->getVar('disc_if_amount')) {
202
                    $discountsDescription[] = $rule->getVar('disc_description');
203
                    $montantShipping        = 0.0;
204
                }
205
            }
206
        }
207
    }
208
209
    /**
210
     * Réductions à appliquer sur le montant global de la commande
211
     *
212
     * @param float $montantHT            Montant HT des produits
213
     * @param array $discountsDescription Descriptions des réductions appliquées
214
     */
215
    public function applyDiscountOnCommand(&$montantHT, &$discountsDescription)
216
    {
217
        global $commandsHandler;
218
        $tblRules = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tblRules is dead and can be removed.
Loading history...
219
        $tblRules = $this->getRulesOnCommand(); // Renvoie des objets Discounts
220
        if (count($tblRules) > 0) {
221
            $uid = Oledrion\Utility::getCurrentUserID();
222
            foreach ($tblRules as $rule) {
223
                switch ($rule->getVar('disc_when')) {
224
                    case Constants::OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
225
226
                        if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
227
                            // Réduction de x pourcent
228
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
229
                            if ($montantHT < 0) {
230
                                $montantHT = 0.0;
231
                            }
232
                        } else {
233
                            // Réduction de x euros
234
                            $montantHT -= $rule->getVar('disc_amount');
235
                            if ($montantHT < 0) {
236
                                $montantHT = 0.0;
237
                            }
238
                        }
239
                        $discountsDescription[] = $rule->getVar('disc_description');
240
241
                        break;
242
                    case Constants::OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
243
244
                        if ($commandsHandler->isFirstCommand($uid)) {
245
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
246
                                // Réduction de x pourcent
247
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
248
                                if ($montantHT < 0) {
249
                                    $montantHT = 0.0;
250
                                }
251
                            } else {
252
                                // Réduction de x euros
253
                                $montantHT -= $rule->getVar('disc_amount');
254
                                if ($montantHT < 0) {
255
                                    $montantHT = 0.0;
256
                                }
257
                            }
258
                            $discountsDescription[] = $rule->getVar('disc_description');
259
                        }
260
261
                        break;
262
                }
263
            }
264
        }
265
    }
266
267
    /**
268
     * Réductions à appliquer sur les frais de port de chaque produit
269
     *
270
     * @param float $montantHT            Montant HT des produits
271
     * @param array $discountsDescription Descriptions des réductions appliquées
272
     * @param int   $productQty           Quantité commandée du produit
273
     */
274
    public function applyDiscountOnShipping(&$montantHT, &$discountsDescription, $productQty)
275
    {
276
        global $commandsHandler;
277
        $tblRules = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tblRules is dead and can be removed.
Loading history...
278
        $tblRules = $this->getRulesOnShipping(); // Renvoie des objets Discounts
279
        if (count($tblRules) > 0) {
280
            $uid = Oledrion\Utility::getCurrentUserID();
281
            foreach ($tblRules as $rule) {
282
                switch ($rule->getVar('disc_when')) {
283
                    case Constants::OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
284
285
                        if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
286
                            // Réduction de x pourcent
287
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
288
                            if ($montantHT < 0) {
289
                                $montantHT = 0.0;
290
                            }
291
                        } else {
292
                            // Réduction de x euros
293
                            $montantHT -= $rule->getVar('disc_amount');
294
                            if ($montantHT < 0) {
295
                                $montantHT = 0.0;
296
                            }
297
                        }
298
                        $discountsDescription[] = $rule->getVar('disc_description');
299
300
                        break;
301
                    case Constants::OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
302
303
                        if ($commandsHandler->isFirstCommand($uid)) {
304
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
305
                                // Réduction de x pourcent
306
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
307
                                if ($montantHT < 0) {
308
                                    $montantHT = 0.0;
309
                                }
310
                            } else {
311
                                // Réduction de x euros
312
                                $montantHT -= $rule->getVar('disc_amount');
313
                                if ($montantHT < 0) {
314
                                    $montantHT = 0.0;
315
                                }
316
                            }
317
                            $discountsDescription[] = $rule->getVar('disc_description');
318
                        }
319
320
                        break;
321
                    case Constants::OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
322
323
                        $qtyDiscount = false;
324
                        switch ($rule->getVar('disc_qty_criteria')) {
325
                            case 0: // =
326
327
                                if ($productQty == $rule->getVar('disc_qty_value')) {
328
                                    $qtyDiscount = true;
329
                                }
330
331
                                break;
332
                            case 1: // >
333
334
                                if ($productQty > $rule->getVar('disc_qty_value')) {
335
                                    $qtyDiscount = true;
336
                                }
337
338
                                break;
339
                            case 2: // >=
340
341
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
342
                                    $qtyDiscount = true;
343
                                }
344
345
                                break;
346
                            case 3: // <
347
348
                                if ($productQty < $rule->getVar('disc_qty_value')) {
349
                                    $qtyDiscount = true;
350
                                }
351
352
                                break;
353
                            case 4: // <=
354
355
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
356
                                    $qtyDiscount = true;
357
                                }
358
359
                                break;
360
                        }
361
                        if ($qtyDiscount) {
362
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
363
                                // Réduction de x pourcents
364
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
365
                                if ($montantHT < 0) {
366
                                    $montantHT = 0.0;
367
                                }
368
                            } else {
369
                                // Réduction de x euros
370
                                $montantHT -= $rule->getVar('disc_amount');
371
                                if ($montantHT < 0) {
372
                                    $montantHT = 0.0;
373
                                }
374
                            }
375
                            $discountsDescription[] = $rule->getVar('disc_description');
376
                        }
377
378
                        break;
379
                }
380
            }
381
        }
382
    }
383
384
    /**
385
     * Réductions à appliquer sur le montant HT de TOUS les produits
386
     *
387
     * @param float $montantHT            Montant HT des produits
388
     * @param array $discountsDescription Descriptions des réductions appliquées
389
     * @param int   $productQty           Quantité commandée du produit
390
     */
391
    public function applyDiscountOnAllProducts(&$montantHT, &$discountsDescription, $productQty)
392
    {
393
        global $commandsHandler;
394
        $tblRules = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tblRules is dead and can be removed.
Loading history...
395
        $tblRules = $this->getRulesOnAllProducts(); // Renvoie des objets Discounts
396
        if (count($tblRules) > 0) {
397
            $uid = Oledrion\Utility::getCurrentUserID();
398
            foreach ($tblRules as $rule) {
399
                switch ($rule->getVar('disc_when')) {
400
                    case Constants::OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
401
402
                        if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
403
                            // Réduction de x pourcent
404
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
405
                            if ($montantHT < 0) {
406
                                $montantHT = 0.0;
407
                            }
408
                        } else {
409
                            // Réduction de x euros
410
                            $montantHT -= $rule->getVar('disc_amount');
411
                            if ($montantHT < 0) {
412
                                $montantHT = 0.0;
413
                            }
414
                        }
415
                        $discountsDescription[] = $rule->getVar('disc_description');
416
417
                        break;
418
                    case Constants::OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
419
420
                        if ($commandsHandler->isFirstCommand($uid)) {
421
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
422
                                // Réduction de x pourcent
423
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
424
                                if ($montantHT < 0) {
425
                                    $montantHT = 0.0;
426
                                }
427
                            } else {
428
                                // Réduction de x euros
429
                                $montantHT -= $rule->getVar('disc_amount');
430
                                if ($montantHT < 0) {
431
                                    $montantHT = 0.0;
432
                                }
433
                            }
434
                            $discountsDescription[] = $rule->getVar('disc_description');
435
                        }
436
437
                        break;
438
                    case Constants::OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
439
440
                        $qtyDiscount = false;
441
                        switch ($rule->getVar('disc_qty_criteria')) {
442
                            case 0: // =
443
444
                                if ($productQty == $rule->getVar('disc_qty_value')) {
445
                                    $qtyDiscount = true;
446
                                }
447
448
                                break;
449
                            case 1: // >
450
451
                                if ($productQty > $rule->getVar('disc_qty_value')) {
452
                                    $qtyDiscount = true;
453
                                }
454
455
                                break;
456
                            case 2: // >=
457
458
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
459
                                    $qtyDiscount = true;
460
                                }
461
462
                                break;
463
                            case 3: // <
464
465
                                if ($productQty < $rule->getVar('disc_qty_value')) {
466
                                    $qtyDiscount = true;
467
                                }
468
469
                                break;
470
                            case 4: // <=
471
472
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
473
                                    $qtyDiscount = true;
474
                                }
475
476
                                break;
477
                        }
478
                        if ($qtyDiscount) {
479
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
480
                                // Réduction de x pourcent
481
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
482
                                if ($montantHT < 0) {
483
                                    $montantHT = 0.0;
484
                                }
485
                            } else {
486
                                // Réduction de x euros
487
                                $montantHT -= $rule->getVar('disc_amount');
488
                                if ($montantHT < 0) {
489
                                    $montantHT = 0.0;
490
                                }
491
                            }
492
                            $discountsDescription[] = $rule->getVar('disc_description');
493
                        }
494
495
                        break;
496
                }
497
            }
498
        }
499
    }
500
501
    /**
502
     * Recalcul du prix HT du produit en appliquant les réductions, s'il y a lieu
503
     *
504
     * @param int   $productId            Identifiant du produit
505
     * @param float $prixHT               Prix HT du produit
506
     * @param array $discountsDescription Descriptions des réductions appliquées
507
     * @param int   $productQty           Quantité commandée du produit
508
     */
509
    public function applyDiscountOnEachProduct($productId, &$prixHT, &$discountsDescription, $productQty)
510
    {
511
        global $commandsHandler;
512
        $rules = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $rules is dead and can be removed.
Loading history...
513
        $rules = $this->getRulesOnEachProduct(); // Renvoie des objets Discounts
514
        if (count($rules) > 0) {
515
            $uid = Oledrion\Utility::getCurrentUserID();
516
            foreach ($rules as $rule) {
517
                switch ($rule->getVar('disc_when')) {
518
                    case Constants::OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
519
520
                        if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
521
                            // Réduction de x pourcent
522
                            $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
523
                            if ($prixHT < 0) {
524
                                $prixHT = 0.0;
525
                            }
526
                        } else {
527
                            // Réduction de x euros
528
                            $prixHT -= $rule->getVar('disc_amount');
529
                            if ($prixHT < 0) {
530
                                $prixHT = 0.0;
531
                            }
532
                        }
533
                        $discountsDescription[] = $rule->getVar('disc_description');
534
535
                        break;
536
                    case Constants::OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
537
538
                        if ($commandsHandler->isFirstCommand($uid)) {
539
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
540
                                // Réduction de x pourcent
541
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
542
                                if ($prixHT < 0) {
543
                                    $prixHT = 0.0;
544
                                }
545
                            } else {
546
                                // Réduction de x euros
547
                                $prixHT -= $rule->getVar('disc_amount');
548
                                if ($prixHT < 0) {
549
                                    $prixHT = 0.0;
550
                                }
551
                            }
552
                            $discountsDescription[] = $rule->getVar('disc_description');
553
                        }
554
555
                        break;
556
                    case Constants::OLEDRION_DISCOUNT_WHEN3: // Si le produit n'a jamais été acheté
557
558
                        if (!$commandsHandler->productAlreadyBought($uid, $productId)) {
559
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
560
                                // Réduction de x pourcent
561
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
562
                                if ($prixHT < 0) {
563
                                    $prixHT = 0.0;
564
                                }
565
                            } else {
566
                                // Réduction de x euros
567
                                $prixHT -= $rule->getVar('disc_amount');
568
                                if ($prixHT < 0) {
569
                                    $prixHT = 0.0;
570
                                }
571
                            }
572
                            $discountsDescription[] = $rule->getVar('disc_description');
573
                        }
574
575
                        break;
576
                    case Constants::OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
577
578
                        $qtyDiscount = false;
579
                        switch ($rule->getVar('disc_qty_criteria')) {
580
                            case 0: // =
581
582
                                if ($productQty == $rule->getVar('disc_qty_value')) {
583
                                    $qtyDiscount = true;
584
                                }
585
586
                                break;
587
                            case 1: // >
588
589
                                if ($productQty > $rule->getVar('disc_qty_value')) {
590
                                    $qtyDiscount = true;
591
                                }
592
593
                                break;
594
                            case 2: // >=
595
596
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
597
                                    $qtyDiscount = true;
598
                                }
599
600
                                break;
601
                            case 3: // <
602
603
                                if ($productQty < $rule->getVar('disc_qty_value')) {
604
                                    $qtyDiscount = true;
605
                                }
606
607
                                break;
608
                            case 4: // <=
609
610
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
611
                                    $qtyDiscount = true;
612
                                }
613
614
                                break;
615
                        }
616
                        if ($qtyDiscount) {
617
                            if (Constants::OLEDRION_DISCOUNT_PRICE_TYPE1 == $rule->getVar('disc_percent_monney')) {
618
                                // Réduction de x pourcent
619
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
620
                                if ($prixHT < 0) {
621
                                    $prixHT = 0.0;
622
                                }
623
                            } else {
624
                                // Réduction de x euros
625
                                $prixHT -= $rule->getVar('disc_amount');
626
                                if ($prixHT < 0) {
627
                                    $prixHT = 0.0;
628
                                }
629
                            }
630
                            $discountsDescription[] = $rule->getVar('disc_description');
631
                        }
632
633
                        break;
634
                }
635
            }
636
        }
637
    }
638
639
    /**
640
     * Supprime les remises associées à un produit
641
     *
642
     * @param int $disc_product_id
643
     * @return bool
644
     */
645
    public function removeProductFromDiscounts($disc_product_id)
646
    {
647
        $disc_product_id = (int)$disc_product_id;
648
649
        return $this->deleteAll(new \Criteria('disc_product_id', $disc_product_id, '='));
650
    }
651
}
652