Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

class/oledrion_discounts.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link http://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
/**
20
 * Gestion des réductions
21
 */
22
require __DIR__ . '/classheader.php';
23
24
// Les nouveaux define relatifs aux réductions ************************************************************************
25
define('OLEDRION_DISCOUNT_PRICE_TYPE0', 0); // Réduction non définie
26
define('OLEDRION_DISCOUNT_PRICE_TYPE1', 1); // Réduction dégressive
27
define('OLEDRION_DISCOUNT_PRICE_TYPE2', 2); // Réduction d'un montant ou pourcentage
28
29
define('OLEDRION_DISCOUNT_PRICE_REDUCE_PERCENT', 1); // Pourcent
30
define('OLEDRION_DISCOUNT_PRICE_REDUCE_MONEY', 2); // Euros
31
32
define('OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_PRODUCT', 1); // Réduction d'un montant ou d'un pourcentage sur le produit
33
define('OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_CART', 2); // Réduction d'un montant ou d'un pourcentage sur le panier
34
35
define('OLEDRION_DISCOUNT_PRICE_CASE_ALL', 1); // Dans tous les cas
36
define('OLEDRION_DISCOUNT_PRICE_CASE_FIRST_BUY', 2); // si c'est le premier achat du client sur le site
37
define('OLEDRION_DISCOUNT_PRICE_CASE_PRODUCT_NEVER', 3); // si le produit n'a jamais été acheté par le client
38
define('OLEDRION_DISCOUNT_PRICE_CASE_QTY_IS', 4); // si la quantité de produit est ...
39
40
define('OLEDRION_DISCOUNT_PRICE_QTY_COND1', 1); // si la quantité de produit est > à
41
define('OLEDRION_DISCOUNT_PRICE_QTY_COND2', 2); // si la quantité de produit est >= à
42
define('OLEDRION_DISCOUNT_PRICE_QTY_COND3', 3); // si la quantité de produit est < à
43
define('OLEDRION_DISCOUNT_PRICE_QTY_COND4', 4); // si la quantité de produit est <= à
44
define('OLEDRION_DISCOUNT_PRICE_QTY_COND5', 5); // si la quantité de produit est = à
45
46
define('OLEDRION_DISCOUNT_PRICE_QTY_COND1_TEXT', '>'); // si la quantité de produit est > à
47
define('OLEDRION_DISCOUNT_PRICE_QTY_COND2_TEXT', '>='); // si la quantité de produit est >= à
48
define('OLEDRION_DISCOUNT_PRICE_QTY_COND3_TEXT', '<'); // si la quantité de produit est < à
49
define('OLEDRION_DISCOUNT_PRICE_QTY_COND4_TEXT', '<='); // si la quantité de produit est <= à
50
define('OLEDRION_DISCOUNT_PRICE_QTY_COND5_TEXT', '='); // si la quantité de produit est = à
51
52
define('OLEDRION_DISCOUNT_SHIPPING_TYPE1', 1); // Les frais de port sont à payer dans leur intégralité
53
define('OLEDRION_DISCOUNT_SHIPPING_TYPE2', 2); // Les frais de port sont totalement gratuits
54
define('OLEDRION_DISCOUNT_SHIPPING_TYPE3', 3); // Les frais de port sont réduits de ...
55
define('OLEDRION_DISCOUNT_SHIPPING_TYPE4', 4); // Les frais de port sont dégressifs
56
57
define('OLEDRION_DISCOUNT_ON1', 1);
58
define('OLEDRION_DISCOUNT_ON2', 2);
59
define('OLEDRION_DISCOUNT_ON3', 3);
60
define('OLEDRION_DISCOUNT_ON4', 4);
61
define('OLEDRION_DISCOUNT_ON5', 5);
62
define('OLEDRION_DISCOUNT_ON6', 6);
63
64
define('OLEDRION_DISCOUNT_WHEN1', 1);
65
define('OLEDRION_DISCOUNT_WHEN2', 2);
66
define('OLEDRION_DISCOUNT_WHEN3', 3);
67
define('OLEDRION_DISCOUNT_WHEN4', 4);
68
69
define('OLEDRION_DISCOUNT_TYPE1', 1);
70
71
72
73
74
// ********************************************************************************************************************
75
76
/**
77
 * Class Oledrion_discounts
78
 */
79
class Oledrion_discounts extends Oledrion_Object
80
{
81
    /**
82
     * constructor
83
     *
84
     * normally, this is called from child classes only
85
     *
86
     * @access public
87
     */
88
    public function __construct()
89
    {
90
        $this->initVar('disc_id', XOBJ_DTYPE_INT, null, false);
91
        $this->initVar('disc_title', XOBJ_DTYPE_TXTBOX, null, false);
92
        $this->initVar('disc_group', XOBJ_DTYPE_INT, null, false); // Groupe Xoops concerné par la remise (0=tous les groupes)
93
        $this->initVar('disc_cat_cid', XOBJ_DTYPE_INT, null, false);
94
        $this->initVar('disc_vendor_id', XOBJ_DTYPE_INT, null, false);
95
        $this->initVar('disc_product_id', XOBJ_DTYPE_INT, null, false);
96
        $this->initVar('disc_price_type', XOBJ_DTYPE_INT, null, false); // Type de réduction (dégressive, montant/pourcentage)
97
        $this->initVar('disc_price_degress_l1qty1', XOBJ_DTYPE_INT, null, false);
98
        $this->initVar('disc_price_degress_l1qty2', XOBJ_DTYPE_INT, null, false);
99
        $this->initVar('disc_price_degress_l1total', XOBJ_DTYPE_TXTBOX, null, false);
100
        $this->initVar('disc_price_degress_l2qty1', XOBJ_DTYPE_INT, null, false);
101
        $this->initVar('disc_price_degress_l2qty2', XOBJ_DTYPE_INT, null, false);
102
        $this->initVar('disc_price_degress_l2total', XOBJ_DTYPE_TXTBOX, null, false);
103
        $this->initVar('disc_price_degress_l3qty1', XOBJ_DTYPE_INT, null, false);
104
        $this->initVar('disc_price_degress_l3qty2', XOBJ_DTYPE_INT, null, false);
105
        $this->initVar('disc_price_degress_l3total', XOBJ_DTYPE_TXTBOX, null, false);
106
        $this->initVar('disc_price_degress_l4qty1', XOBJ_DTYPE_INT, null, false);
107
        $this->initVar('disc_price_degress_l4qty2', XOBJ_DTYPE_INT, null, false);
108
        $this->initVar('disc_price_degress_l4total', XOBJ_DTYPE_TXTBOX, null, false);
109
        $this->initVar('disc_price_degress_l5qty1', XOBJ_DTYPE_INT, null, false);
110
        $this->initVar('disc_price_degress_l5qty2', XOBJ_DTYPE_INT, null, false);
111
        $this->initVar('disc_price_degress_l5total', XOBJ_DTYPE_TXTBOX, null, false);
112
        $this->initVar('disc_price_amount_amount', XOBJ_DTYPE_TXTBOX, null, false); // Montant ou pourcentage de réduction sur le prix
113
        $this->initVar('disc_price_amount_type', XOBJ_DTYPE_INT, null, false); // Pourcent ou Euros ?
114
        $this->initVar('disc_price_amount_on', XOBJ_DTYPE_INT, null, false); // Produit ou panier ?
115
        $this->initVar('disc_price_case', XOBJ_DTYPE_INT, null, false); // Dans quel cas ? (tous les cas, si c''est le premier achat, si le produit n''a jamais été acheté etc)
116
        $this->initVar('disc_price_case_qty_cond', XOBJ_DTYPE_INT, null, false); // Supérieur, inférieur, égal
117
        $this->initVar('disc_price_case_qty_value', XOBJ_DTYPE_INT, null, false); // Quantité de produit à tester
118
        $this->initVar('disc_shipping_type', XOBJ_DTYPE_INT, null, false);
119
        $this->initVar('disc_shipping_free_morethan', XOBJ_DTYPE_TXTBOX, null, false);
120
        $this->initVar('disc_shipping_reduce_amount', XOBJ_DTYPE_TXTBOX, null, false);
121
        $this->initVar('disc_shipping_reduce_cartamount', XOBJ_DTYPE_TXTBOX, null, false);
122
        $this->initVar('disc_shipping_degress_l1qty1', XOBJ_DTYPE_INT, null, false);
123
        $this->initVar('disc_shipping_degress_l1qty2', XOBJ_DTYPE_INT, null, false);
124
        $this->initVar('disc_shipping_degress_l1total', XOBJ_DTYPE_TXTBOX, null, false);
125
        $this->initVar('disc_shipping_degress_l2qty1', XOBJ_DTYPE_INT, null, false);
126
        $this->initVar('disc_shipping_degress_l2qty2', XOBJ_DTYPE_INT, null, false);
127
        $this->initVar('disc_shipping_degress_l2total', XOBJ_DTYPE_TXTBOX, null, false);
128
        $this->initVar('disc_shipping_degress_l3qty1', XOBJ_DTYPE_INT, null, false);
129
        $this->initVar('disc_shipping_degress_l3qty2', XOBJ_DTYPE_INT, null, false);
130
        $this->initVar('disc_shipping_degress_l3total', XOBJ_DTYPE_TXTBOX, null, false);
131
        $this->initVar('disc_shipping_degress_l4qty1', XOBJ_DTYPE_INT, null, false);
132
        $this->initVar('disc_shipping_degress_l4qty2', XOBJ_DTYPE_INT, null, false);
133
        $this->initVar('disc_shipping_degress_l4total', XOBJ_DTYPE_TXTBOX, null, false);
134
        $this->initVar('disc_shipping_degress_l5qty1', XOBJ_DTYPE_INT, null, false);
135
        $this->initVar('disc_shipping_degress_l5qty2', XOBJ_DTYPE_INT, null, false);
136
        $this->initVar('disc_shipping_degress_l5total', XOBJ_DTYPE_TXTBOX, null, false);
137
        $this->initVar('disc_date_from', XOBJ_DTYPE_INT, null, false); // Date de début de la promo
138
        $this->initVar('disc_date_to', XOBJ_DTYPE_INT, null, false); // Date de fin de la promo
139
        $this->initVar('disc_description', XOBJ_DTYPE_TXTAREA, null, false);
140
141
        // Pour autoriser le html
142
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
143
    }
144
}
145
146
/**
147
 * Class OledrionOledrion_discountsHandler
148
 */
149
class OledrionOledrion_discountsHandler extends Oledrion_XoopsPersistableObjectHandler
150
{
151
    /**
152
     * OledrionOledrion_discountsHandler constructor.
153
     * @param XoopsDatabase|null $db
154
     */
155
    public function __construct(XoopsDatabase $db)
156
    { //                        Table                   Classe              Id        Libellé
157
        parent::__construct($db, 'oledrion_discounts', 'oledrion_discounts', 'disc_id', 'disc_title');
158
    }
159
160
    /**
161
     * @param $price
162
     * @param $discount
163
     * @return mixed
164
     */
165
    private function getDiscountedPrice($price, $discount)
166
    {
167
        return $price - ($price * ($discount / 100));
168
    }
169
170
    /**
171
     * Retourne la liste des règles qui sont applicables sur la période courante
172
     * @param void
173
     * @return array objets de type oledrion_discounts
174
     */
175
    public function getRulesForThisPeriod()
176
    {
177
        static $buffer = array();
178
        if (is_array($buffer) && count($buffer) > 0) {
179
            return $buffer;
180
        } else {
181
            $critere = new CriteriaCompo();
182
            $critere->add(new Criteria('disc_date_from', 0, '='));
183
            $critere->add(new Criteria('disc_date_to', 0, '='), 'OR');
184
185
            $critere2 = new CriteriaCompo();
186
            $critere2->add(new Criteria('disc_date_from', time(), '>='));
187
            $critere2->add(new Criteria('disc_date_to', time(), '<='));
188
            $critere->add($critere2);
189
190
            $buffer = $this->getObjects($critere);
191
        }
192
193
        return $buffer;
194
    }
195
196
    /**
197
     *
198
     * @deprecated
199
     */
200
201
    /**
202
     * Renvoie la liste des règles à appliquer sur chaque produit (avec gestion de cache) pour l'utilisateur courant
203
     *
204
     * @return array Tableau d'objets de type Discounts
205
     */
206 View Code Duplication
    public function getRulesOnEachProduct()
207
    {
208
        static $buffer = array();
209
        if (is_array($buffer) && count($buffer) > 0) {
210
        } else {
211
            $groups  = Oledrion_utils::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<Oledrion_utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
            $critere = new CriteriaCompo();
213
            $critere->add(new Criteria('disc_on_what', OLEDRION_DISCOUNT_ON3, '='));
214
            if (count($groups) > 0) {
215
                $critere->add(new Criteria('disc_group', '(' . implode(',', $groups) . ')', 'IN'));
216
            }
217
            $buffer = $this->getObjects($critere);
218
        }
219
220
        return $buffer;
221
    }
222
223
    /**
224
     * Renvoie la liste des règles à appliquer sur tous les produits (avec gestion de cache) pour l'utilisateur courant
225
     *
226
     * @return array Tableau d'objets de type Discounts
227
     */
228 View Code Duplication
    public function getRulesOnAllProducts()
229
    {
230
        static $buffer = array();
231
        if (is_array($buffer) && count($buffer) > 0) {
232
        } else {
233
            $critere = new CriteriaCompo();
234
            $critere->add(new Criteria('disc_on_what',  OLEDRION_DISCOUNT_ON2, '='));
235
            $tblGroups = Oledrion_utils::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<Oledrion_utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
237
            $buffer = $this->getObjects($critere);
238
        }
239
240
        return $buffer;
241
    }
242
243
    /**
244
     * Renvoie la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
245
     *
246
     * @return array Tableau d'objets de type Discounts
247
     */
248 View Code Duplication
    public function getRulesOnShipping()
249
    {
250
        static $buffer = array();
251
        if (is_array($buffer) && count($buffer) > 0) {
252
        } else {
253
            $critere = new CriteriaCompo();
254
            $critere->add(new Criteria('disc_on_what',  OLEDRION_DISCOUNT_ON4, '='));
255
            $tblGroups = Oledrion_utils::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<Oledrion_utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
256
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
257
            $buffer = $this->getObjects($critere);
258
        }
259
260
        return $buffer;
261
    }
262
263
    /**
264
     * Renvoie la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
265
     *
266
     * @return array Tableau d'objets de type Discounts
267
     */
268 View Code Duplication
    public function getRulesOnShipping2()
269
    {
270
        static $buffer = array();
271
        if (is_array($buffer) && count($buffer) > 0) {
272
        } else {
273
            $critere = new CriteriaCompo();
274
            $critere->add(new Criteria('disc_on_what',  OLEDRION_DISCOUNT_ON5, '='));
275
            $critere->add(new Criteria('disc_shipping', OLEDRION_DISCOUNT_SHIPPING_TYPE2, '='));
276
            $tblGroups = Oledrion_utils::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<Oledrion_utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
277
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
278
            $buffer = $this->getObjects($critere);
279
        }
280
281
        return $buffer;
282
    }
283
284
    /**
285
     * Renvoie la liste des règles à appliquer sur l'intégralité de la commande (avec gestion de cache) pour l'utilisateur courant
286
     *
287
     * @return array Tableau d'objets de type Discounts
288
     */
289 View Code Duplication
    public function getRulesOnCommand()
290
    {
291
        static $buffer = array();
292
        if (is_array($buffer) && count($buffer) > 0) {
293
        } else {
294
            $critere = new CriteriaCompo();
295
            $critere->add(new Criteria('disc_on_what', OLEDRION_DISCOUNT_ON1, '='));
296
            $tblGroups = Oledrion_utils::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<Oledrion_utils>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
297
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
298
            $buffer = $this->getObjects($critere);
299
        }
300
301
        return $buffer;
302
    }
303
304
    /**
305
     * Deuxième lot de réductions, à appliquer sur les frais de port
306
     *
307
     * @param float $montantShipping      Montant des frais de port
308
     * @param float $commandAmount        Le montant total de la commande
309
     * @param array $discountsDescription Descriptions des réductions appliquées
310
     */
311
    public function applyDiscountOnShipping2(&$montantShipping, $commandAmount, &$discountsDescription)
312
    {
313
        $tblRules = array();
314
        $tblRules = $this->getRulesOnShipping2(); // Renvoie des objets Discounts
315
        if (count($tblRules) > 0) {
316
            foreach ($tblRules as $rule) {
317
                if ($commandAmount > (float)$rule->getVar('disc_if_amount')) {
318
                    $discountsDescription[] = $rule->getVar('disc_description');
319
                    $montantShipping        = 0;
320
                }
321
            }
322
        }
323
    }
324
325
    /**
326
     * Réductions à appliquer sur le montant global de la commande
327
     *
328
     * @param float $montantHT            Montant HT des produits
329
     * @param array $discountsDescription Descriptions des réductions appliquées
330
     */
331
    public function applyDiscountOnCommand(&$montantHT, &$discountsDescription)
332
    {
333
        global $h_oledrion_commands;
334
        $tblRules = array();
335
        $tblRules = $this->getRulesOnCommand(); // Renvoie des objets Discounts
336
        if (count($tblRules) > 0) {
337
            $uid = Oledrion_utils::getCurrentUserID();
338
            foreach ($tblRules as $rule) {
339
                switch ($rule->getVar('disc_when')) {
340
                    case OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
341
                        if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
342
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
343
                            if ($montantHT < 0) {
344
                                $montantHT = 0;
345
                            }
346
                        } else { // Réduction de x euros
347
                            $montantHT -= $rule->getVar('disc_amount');
348
                            if ($montantHT < 0) {
349
                                $montantHT = 0;
350
                            }
351
                        }
352
                        $discountsDescription[] = $rule->getVar('disc_description');
353
                        break;
354
355
                    case OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
356
                        if ($h_oledrion_commands->isFirstCommand($uid)) {
357
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
358
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
359
                                if ($montantHT < 0) {
360
                                    $montantHT = 0;
361
                                }
362
                            } else { // Réduction de x euros
363
                                $montantHT -= $rule->getVar('disc_amount');
364
                                if ($montantHT < 0) {
365
                                    $montantHT = 0;
366
                                }
367
                            }
368
                            $discountsDescription[] = $rule->getVar('disc_description');
369
                        }
370
                        break;
371
                }
372
            }
373
        }
374
    }
375
376
    /**
377
     * Réductions à appliquer sur les frais de port de chaque produit
378
     *
379
     * @param float   $montantHT            Montant HT des produits
380
     * @param array   $discountsDescription Descriptions des réductions appliquées
381
     * @param integer $productQty           Quantité commandée du produit
382
     */
383 View Code Duplication
    public function applyDiscountOnShipping(&$montantHT, &$discountsDescription, $productQty)
384
    {
385
        global $h_oledrion_commands;
386
        $tblRules = array();
387
        $tblRules = $this->getRulesOnShipping(); // Renvoie des objets Discounts
388
        if (count($tblRules) > 0) {
389
            $uid = Oledrion_utils::getCurrentUserID();
390
            foreach ($tblRules as $rule) {
391
                switch ($rule->getVar('disc_when')) {
392
                    case OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
393
                        if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
394
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
395
                            if ($montantHT < 0) {
396
                                $montantHT = 0;
397
                            }
398
                        } else { // Réduction de x euros
399
                            $montantHT -= $rule->getVar('disc_amount');
400
                            if ($montantHT < 0) {
401
                                $montantHT = 0;
402
                            }
403
                        }
404
                        $discountsDescription[] = $rule->getVar('disc_description');
405
                        break;
406
407
                    case OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
408
                        if ($h_oledrion_commands->isFirstCommand($uid)) {
409
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
410
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
411
                                if ($montantHT < 0) {
412
                                    $montantHT = 0;
413
                                }
414
                            } else { // Réduction de x euros
415
                                $montantHT -= $rule->getVar('disc_amount');
416
                                if ($montantHT < 0) {
417
                                    $montantHT = 0;
418
                                }
419
                            }
420
                            $discountsDescription[] = $rule->getVar('disc_description');
421
                        }
422
                        break;
423
424
                    case OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
425
                        $qtyDiscount = false;
426
                        switch ($rule->getVar('disc_qty_criteria')) {
427
                            case 0: // =
428
                                if ($productQty == $rule->getVar('disc_qty_value')) {
429
                                    $qtyDiscount = true;
430
                                }
431
                                break;
432
433
                            case 1: // >
434
                                if ($productQty > $rule->getVar('disc_qty_value')) {
435
                                    $qtyDiscount = true;
436
                                }
437
                                break;
438
439
                            case 2: // >=
440
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
441
                                    $qtyDiscount = true;
442
                                }
443
                                break;
444
445
                            case 3: // <
446
                                if ($productQty < $rule->getVar('disc_qty_value')) {
447
                                    $qtyDiscount = true;
448
                                }
449
                                break;
450
451
                            case 4: // <=
452
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
453
                                    $qtyDiscount = true;
454
                                }
455
                                break;
456
457
                        }
458
                        if ($qtyDiscount) {
459
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcents
460
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
461
                                if ($montantHT < 0) {
462
                                    $montantHT = 0;
463
                                }
464
                            } else { // Réduction de x euros
465
                                $montantHT -= $rule->getVar('disc_amount');
466
                                if ($montantHT < 0) {
467
                                    $montantHT = 0;
468
                                }
469
                            }
470
                            $discountsDescription[] = $rule->getVar('disc_description');
471
                        }
472
                        break;
473
                }
474
            }
475
        }
476
    }
477
478
    /**
479
     * Réductions à appliquer sur le montant HT de TOUS les produits
480
     *
481
     * @param float   $montantHT            Montant HT des produits
482
     * @param array   $discountsDescription Descriptions des réductions appliquées
483
     * @param integer $productQty           Quantité commandée du produit
484
     */
485 View Code Duplication
    public function applyDiscountOnAllProducts(&$montantHT, &$discountsDescription, $productQty)
486
    {
487
        global $h_oledrion_commands;
488
        $tblRules = array();
489
        $tblRules = $this->getRulesOnAllProducts(); // Renvoie des objets Discounts
490
        if (count($tblRules) > 0) {
491
            $uid = Oledrion_utils::getCurrentUserID();
492
            foreach ($tblRules as $rule) {
493
                switch ($rule->getVar('disc_when')) {
494
                    case OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
495
                        if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
496
                            $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
497
                            if ($montantHT < 0) {
498
                                $montantHT = 0;
499
                            }
500
                        } else { // Réduction de x euros
501
                            $montantHT -= $rule->getVar('disc_amount');
502
                            if ($montantHT < 0) {
503
                                $montantHT = 0;
504
                            }
505
                        }
506
                        $discountsDescription[] = $rule->getVar('disc_description');
507
                        break;
508
509
                    case OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
510
                        if ($h_oledrion_commands->isFirstCommand($uid)) {
511
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
512
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
513
                                if ($montantHT < 0) {
514
                                    $montantHT = 0;
515
                                }
516
                            } else { // Réduction de x euros
517
                                $montantHT -= $rule->getVar('disc_amount');
518
                                if ($montantHT < 0) {
519
                                    $montantHT = 0;
520
                                }
521
                            }
522
                            $discountsDescription[] = $rule->getVar('disc_description');
523
                        }
524
                        break;
525
526
                    case OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
527
                        $qtyDiscount = false;
528
                        switch ($rule->getVar('disc_qty_criteria')) {
529
                            case 0: // =
530
                                if ($productQty == $rule->getVar('disc_qty_value')) {
531
                                    $qtyDiscount = true;
532
                                }
533
                                break;
534
535
                            case 1: // >
536
                                if ($productQty > $rule->getVar('disc_qty_value')) {
537
                                    $qtyDiscount = true;
538
                                }
539
                                break;
540
541
                            case 2: // >=
542
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
543
                                    $qtyDiscount = true;
544
                                }
545
                                break;
546
547
                            case 3: // <
548
                                if ($productQty < $rule->getVar('disc_qty_value')) {
549
                                    $qtyDiscount = true;
550
                                }
551
                                break;
552
553
                            case 4: // <=
554
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
555
                                    $qtyDiscount = true;
556
                                }
557
                                break;
558
559
                        }
560
                        if ($qtyDiscount) {
561
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
562
                                $montantHT = $this->getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
563
                                if ($montantHT < 0) {
564
                                    $montantHT = 0;
565
                                }
566
                            } else { // Réduction de x euros
567
                                $montantHT -= $rule->getVar('disc_amount');
568
                                if ($montantHT < 0) {
569
                                    $montantHT = 0;
570
                                }
571
                            }
572
                            $discountsDescription[] = $rule->getVar('disc_description');
573
                        }
574
                        break;
575
                }
576
            }
577
        }
578
    }
579
580
    /**
581
     * Recalcul du prix HT du produit en appliquant les réductions, s'il y a lieu
582
     *
583
     * @param integer $productId            Identifiant du produit
584
     * @param float   $prixHT               Prix HT du produit
585
     * @param array   $discountsDescription Descriptions des réductions appliquées
586
     * @param integer $productQty           Quantité commandée du produit
587
     */
588
    public function applyDiscountOnEachProduct($productId, &$prixHT, &$discountsDescription, $productQty)
589
    {
590
        global $h_oledrion_commands;
591
        $rules = array();
592
        $rules = $this->getRulesOnEachProduct(); // Renvoie des objets Discounts
593
        if (count($rules) > 0) {
594
            $uid = Oledrion_utils::getCurrentUserID();
595
            foreach ($rules as $rule) {
596
                switch ($rule->getVar('disc_when')) {
597
                    case OLEDRION_DISCOUNT_WHEN1: // Dans tous les cas
598
                        if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
599
                            $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
600
                            if ($prixHT < 0) {
601
                                $prixHT = 0;
602
                            }
603
                        } else { // Réduction de x euros
604
                            $prixHT -= $rule->getVar('disc_amount');
605
                            if ($prixHT < 0) {
606
                                $prixHT = 0;
607
                            }
608
                        }
609
                        $discountsDescription[] = $rule->getVar('disc_description');
610
                        break;
611
612
                    case OLEDRION_DISCOUNT_WHEN2: // Si c'est le premier achat de l'utilisateur sur le site
613
                        if ($h_oledrion_commands->isFirstCommand($uid)) {
614
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
615
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
616
                                if ($prixHT < 0) {
617
                                    $prixHT = 0;
618
                                }
619
                            } else { // Réduction de x euros
620
                                $prixHT -= $rule->getVar('disc_amount');
621
                                if ($prixHT < 0) {
622
                                    $prixHT = 0;
623
                                }
624
                            }
625
                            $discountsDescription[] = $rule->getVar('disc_description');
626
                        }
627
                        break;
628
629
                    case OLEDRION_DISCOUNT_WHEN3: // Si le produit n'a jamais été acheté
630
                        if (!$h_oledrion_commands->productAlreadyBought($uid, $productId)) {
631
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
632
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
633
                                if ($prixHT < 0) {
634
                                    $prixHT = 0;
635
                                }
636
                            } else { // Réduction de x euros
637
                                $prixHT -= $rule->getVar('disc_amount');
638
                                if ($prixHT < 0) {
639
                                    $prixHT = 0;
640
                                }
641
                            }
642
                            $discountsDescription[] = $rule->getVar('disc_description');
643
                        }
644
                        break;
645
646
                    case OLEDRION_DISCOUNT_WHEN4: // Si la quantité est =, >, >=, <, <= à ...
647
                        $qtyDiscount = false;
648
                        switch ($rule->getVar('disc_qty_criteria')) {
649
                            case 0: // =
650
                                if ($productQty == $rule->getVar('disc_qty_value')) {
651
                                    $qtyDiscount = true;
652
                                }
653
                                break;
654
655
                            case 1: // >
656
                                if ($productQty > $rule->getVar('disc_qty_value')) {
657
                                    $qtyDiscount = true;
658
                                }
659
                                break;
660
661
                            case 2: // >=
662
                                if ($productQty >= $rule->getVar('disc_qty_value')) {
663
                                    $qtyDiscount = true;
664
                                }
665
                                break;
666
667
                            case 3: // <
668
                                if ($productQty < $rule->getVar('disc_qty_value')) {
669
                                    $qtyDiscount = true;
670
                                }
671
                                break;
672
673
                            case 4: // <=
674
                                if ($productQty <= $rule->getVar('disc_qty_value')) {
675
                                    $qtyDiscount = true;
676
                                }
677
                                break;
678
679
                        }
680
                        if ($qtyDiscount) {
681
                            if ($rule->getVar('disc_percent_monney') == OLEDRION_DISCOUNT_TYPE1) { // Réduction de x pourcent
682
                                $prixHT = $this->getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
683
                                if ($prixHT < 0) {
684
                                    $prixHT = 0;
685
                                }
686
                            } else { // Réduction de x euros
687
                                $prixHT -= $rule->getVar('disc_amount');
688
                                if ($prixHT < 0) {
689
                                    $prixHT = 0;
690
                                }
691
                            }
692
                            $discountsDescription[] = $rule->getVar('disc_description');
693
                        }
694
                        break;
695
                }
696
            }
697
        }
698
    }
699
700
    /**
701
     * Supprime les remises associées à un produit
702
     *
703
     * @param  integer $disc_product_id
704
     * @return boolean
705
     */
706
    public function removeProductFromDiscounts($disc_product_id)
707
    {
708
        $disc_product_id = (int)$disc_product_id;
709
710
        return $this->deleteAll(new Criteria('disc_product_id', $disc_product_id, '='));
711
    }
712
}
713