Completed
Push — master ( 9d3fbd...af269e )
by Michael
09:48
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 https://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_once __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
 * Class Oledrion_discounts
75
 */
76
class Oledrion_discounts extends Oledrion_Object
77
{
78
    /**
79
     * constructor
80
     *
81
     * normally, this is called from child classes only
82
     *
83
     * @access public
84
     */
85
    public function __construct()
86
    {
87
        $this->initVar('disc_id', XOBJ_DTYPE_INT, null, false);
88
        $this->initVar('disc_title', XOBJ_DTYPE_TXTBOX, null, false);
89
        $this->initVar('disc_group', XOBJ_DTYPE_INT, null, false); // Groupe Xoops concerné par la remise (0=tous les groupes)
90
        $this->initVar('disc_cat_cid', XOBJ_DTYPE_INT, null, false);
91
        $this->initVar('disc_vendor_id', XOBJ_DTYPE_INT, null, false);
92
        $this->initVar('disc_product_id', XOBJ_DTYPE_INT, null, false);
93
        $this->initVar('disc_price_type', XOBJ_DTYPE_INT, null, false); // Type de réduction (dégressive, montant/pourcentage)
94
        $this->initVar('disc_price_degress_l1qty1', XOBJ_DTYPE_INT, null, false);
95
        $this->initVar('disc_price_degress_l1qty2', XOBJ_DTYPE_INT, null, false);
96
        $this->initVar('disc_price_degress_l1total', XOBJ_DTYPE_TXTBOX, null, false);
97
        $this->initVar('disc_price_degress_l2qty1', XOBJ_DTYPE_INT, null, false);
98
        $this->initVar('disc_price_degress_l2qty2', XOBJ_DTYPE_INT, null, false);
99
        $this->initVar('disc_price_degress_l2total', XOBJ_DTYPE_TXTBOX, null, false);
100
        $this->initVar('disc_price_degress_l3qty1', XOBJ_DTYPE_INT, null, false);
101
        $this->initVar('disc_price_degress_l3qty2', XOBJ_DTYPE_INT, null, false);
102
        $this->initVar('disc_price_degress_l3total', XOBJ_DTYPE_TXTBOX, null, false);
103
        $this->initVar('disc_price_degress_l4qty1', XOBJ_DTYPE_INT, null, false);
104
        $this->initVar('disc_price_degress_l4qty2', XOBJ_DTYPE_INT, null, false);
105
        $this->initVar('disc_price_degress_l4total', XOBJ_DTYPE_TXTBOX, null, false);
106
        $this->initVar('disc_price_degress_l5qty1', XOBJ_DTYPE_INT, null, false);
107
        $this->initVar('disc_price_degress_l5qty2', XOBJ_DTYPE_INT, null, false);
108
        $this->initVar('disc_price_degress_l5total', XOBJ_DTYPE_TXTBOX, null, false);
109
        $this->initVar('disc_price_amount_amount', XOBJ_DTYPE_TXTBOX, null, false); // Montant ou pourcentage de réduction sur le prix
110
        $this->initVar('disc_price_amount_type', XOBJ_DTYPE_INT, null, false); // Pourcent ou Euros ?
111
        $this->initVar('disc_price_amount_on', XOBJ_DTYPE_INT, null, false); // Produit ou panier ?
112
        $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)
113
        $this->initVar('disc_price_case_qty_cond', XOBJ_DTYPE_INT, null, false); // Supérieur, inférieur, égal
114
        $this->initVar('disc_price_case_qty_value', XOBJ_DTYPE_INT, null, false); // Quantité de produit à tester
115
        $this->initVar('disc_shipping_type', XOBJ_DTYPE_INT, null, false);
116
        $this->initVar('disc_shipping_free_morethan', XOBJ_DTYPE_TXTBOX, null, false);
117
        $this->initVar('disc_shipping_reduce_amount', XOBJ_DTYPE_TXTBOX, null, false);
118
        $this->initVar('disc_shipping_reduce_cartamount', XOBJ_DTYPE_TXTBOX, null, false);
119
        $this->initVar('disc_shipping_degress_l1qty1', XOBJ_DTYPE_INT, null, false);
120
        $this->initVar('disc_shipping_degress_l1qty2', XOBJ_DTYPE_INT, null, false);
121
        $this->initVar('disc_shipping_degress_l1total', XOBJ_DTYPE_TXTBOX, null, false);
122
        $this->initVar('disc_shipping_degress_l2qty1', XOBJ_DTYPE_INT, null, false);
123
        $this->initVar('disc_shipping_degress_l2qty2', XOBJ_DTYPE_INT, null, false);
124
        $this->initVar('disc_shipping_degress_l2total', XOBJ_DTYPE_TXTBOX, null, false);
125
        $this->initVar('disc_shipping_degress_l3qty1', XOBJ_DTYPE_INT, null, false);
126
        $this->initVar('disc_shipping_degress_l3qty2', XOBJ_DTYPE_INT, null, false);
127
        $this->initVar('disc_shipping_degress_l3total', XOBJ_DTYPE_TXTBOX, null, false);
128
        $this->initVar('disc_shipping_degress_l4qty1', XOBJ_DTYPE_INT, null, false);
129
        $this->initVar('disc_shipping_degress_l4qty2', XOBJ_DTYPE_INT, null, false);
130
        $this->initVar('disc_shipping_degress_l4total', XOBJ_DTYPE_TXTBOX, null, false);
131
        $this->initVar('disc_shipping_degress_l5qty1', XOBJ_DTYPE_INT, null, false);
132
        $this->initVar('disc_shipping_degress_l5qty2', XOBJ_DTYPE_INT, null, false);
133
        $this->initVar('disc_shipping_degress_l5total', XOBJ_DTYPE_TXTBOX, null, false);
134
        $this->initVar('disc_date_from', XOBJ_DTYPE_INT, null, false); // Date de début de la promo
135
        $this->initVar('disc_date_to', XOBJ_DTYPE_INT, null, false); // Date de fin de la promo
136
        $this->initVar('disc_description', XOBJ_DTYPE_TXTAREA, null, false);
137
138
        // Pour autoriser le html
139
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
140
    }
141
}
142
143
/**
144
 * Class OledrionOledrion_discountsHandler
145
 */
146
class OledrionOledrion_discountsHandler extends Oledrion_XoopsPersistableObjectHandler
147
{
148
    /**
149
     * OledrionOledrion_discountsHandler constructor.
150
     * @param XoopsDatabase|null $db
151
     */
152
    public function __construct(XoopsDatabase $db)
153
    { //                        Table                   Classe              Id        Libellé
154
        parent::__construct($db, 'oledrion_discounts', 'oledrion_discounts', 'disc_id', 'disc_title');
155
    }
156
157
    /**
158
     * @param $price
159
     * @param $discount
160
     * @return mixed
161
     */
162
    private function getDiscountedPrice($price, $discount)
163
    {
164
        return $price - ($price * ($discount / 100));
165
    }
166
167
    /**
168
     * Retourne la liste des règles qui sont applicables sur la période courante
169
     * @param void
170
     * @return array objets de type oledrion_discounts
171
     */
172
    public function getRulesForThisPeriod()
173
    {
174
        static $buffer = array();
175
        if (is_array($buffer) && count($buffer) > 0) {
176
            return $buffer;
177
        } else {
178
            $critere = new CriteriaCompo();
179
            $critere->add(new Criteria('disc_date_from', 0, '='));
180
            $critere->add(new Criteria('disc_date_to', 0, '='), 'OR');
181
182
            $critere2 = new CriteriaCompo();
183
            $critere2->add(new Criteria('disc_date_from', time(), '>='));
184
            $critere2->add(new Criteria('disc_date_to', time(), '<='));
185
            $critere->add($critere2);
186
187
            $buffer = $this->getObjects($critere);
188
        }
189
190
        return $buffer;
191
    }
192
193
    /**
194
     *
195
     * @deprecated
196
     */
197
198
    /**
199
     * Renvoie la liste des règles à appliquer sur chaque produit (avec gestion de cache) pour l'utilisateur courant
200
     *
201
     * @return array Tableau d'objets de type Discounts
202
     */
203 View Code Duplication
    public function getRulesOnEachProduct()
204
    {
205
        static $buffer = array();
206
        if (is_array($buffer) && count($buffer) > 0) {
207
        } else {
208
            $groups  = OledrionUtility::getCurrentMemberGroups();
0 ignored issues
show
The method getCurrentMemberGroups() does not seem to exist on object<OledrionUtility>.

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

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

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

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

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