Completed
Push — master ( 1db5db...8743f5 )
by Michael
06:44
created

BookshopBookshop_discountsHandler::applyDiscountOnShipping2()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 7
nop 4
dl 0
loc 23
rs 6.7272
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//  ------------------------------------------------------------------------ //
3
//                      BOOKSHOP - MODULE FOR XOOPS 2                        //
4
//                  Copyright (c) 2007, 2008 Instant Zero                    //
5
//                     <http://www.instant-zero.com/>                        //
6
// ------------------------------------------------------------------------- //
7
//  This program is free software; you can redistribute it and/or modify     //
8
//  it under the terms of the GNU General Public License as published by     //
9
//  the Free Software Foundation; either version 2 of the License, or        //
10
//  (at your option) any later version.                                      //
11
//                                                                           //
12
//  You may not change or alter any portion of this comment or credits       //
13
//  of supporting developers from this source code or any supporting         //
14
//  source code which is considered copyrighted (c) material of the          //
15
//  original comment or credit authors.                                      //
16
//                                                                           //
17
//  This program is distributed in the hope that it will be useful,          //
18
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
//  GNU General Public License for more details.                             //
21
//                                                                           //
22
//  You should have received a copy of the GNU General Public License        //
23
//  along with this program; if not, write to the Free Software              //
24
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
//  ------------------------------------------------------------------------ //
26
27
if (!defined('XOOPS_ROOT_PATH')) {
28
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
29
30
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
31
if (!class_exists('Bookshop_XoopsPersistableObjectHandler')) {
32
    include_once XOOPS_ROOT_PATH . '/modules/bookshop/class/PersistableObjectHandler.php';
33
}
34
35
define('DISCOUNT_TYPE1', 0);        // Pourcent
36
define('DISCOUNT_TYPE2', 1);        // Euros
37
38
define('DISCOUNT_ON1', 0);            // Le montant global de la commande
39
define('DISCOUNT_ON2', 1);            // Tous les livres
40
define('DISCOUNT_ON3', 2);            // Chaque livre
41
define('DISCOUNT_ON4', 3);            // Les frais de ports de tous les livres
42
define('DISCOUNT_ON5', 4);            // Les frais de ports de tous les livres
43
44
define('DISCOUNT_WHEN1', 0);            // Dans tous les cas
45
define('DISCOUNT_WHEN2', 1);            // Si c'est le premier achat de l'utilisateur sur le site
46
define('DISCOUNT_WHEN3', 2);            // Si le livre n'a jamais �t� achet�
47
define('DISCOUNT_WHEN4', 3);			// Sur la quantité
48
49
define('DISCOUNT_SHIPPING1', 0);			// Frais de port, A payer dans leur intégralité
50
define('DISCOUNT_SHIPPING2', 1);            // Frais de port, Totalement gratuit
51
define('DISCOUNT_SHIPPING3', 2);            // Sont de x euros pour le premier article puis de x euros par article
52
53
/**
54
 * Class bookshop_discounts
55
 */
56
class bookshop_discounts extends Bookshop_Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
57
{
58
    public function __construct()
59
    {
60
        $this->initVar('disc_id', XOBJ_DTYPE_INT, null, false);
61
        $this->initVar('disc_group', XOBJ_DTYPE_INT, null, false);
62
        $this->initVar('disc_amount', XOBJ_DTYPE_TXTBOX, null, false);
63
        $this->initVar('disc_percent_monney', XOBJ_DTYPE_INT, null, false);
64
        $this->initVar('disc_on_what', XOBJ_DTYPE_INT, null, false);
65
        $this->initVar('disc_when', XOBJ_DTYPE_INT, null, false);
66
        $this->initVar('disc_shipping', XOBJ_DTYPE_INT, null, false);
67
        $this->initVar('disc_shipping_amount', XOBJ_DTYPE_TXTBOX, null, false);
68
        $this->initVar('disc_shipping_amount_next', XOBJ_DTYPE_TXTBOX, null, false);
69
        $this->initVar('disc_if_amount', XOBJ_DTYPE_TXTBOX, null, false);
70
        $this->initVar('disc_description', XOBJ_DTYPE_TXTAREA, null, false);
71
        $this->initVar('disc_qty_criteria', XOBJ_DTYPE_INT, null, false);
72
        $this->initVar('disc_qty_value', XOBJ_DTYPE_INT, null, false);
73
        // Pour autoriser le html
74
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
75
    }
76
}
77
78
/**
79
 * Class BookshopBookshop_discountsHandler
80
 */
81
class BookshopBookshop_discountsHandler extends Bookshop_XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
82
{
83
    /**
84
     * @param $db
85
     */
86
    public function __construct($db)
87
    {    //                                             Table                   Classe              Id
88
        parent::__construct($db, 'bookshop_discounts', 'bookshop_discounts', 'disc_id');
89
    }
90
91
    /**
92
     * Renvoie la liste des groupes de l'utlisateur courant
93
     */
94
    public function getCurrentMemberGroups()
95
    {
96
        global $xoopsUser;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
97
        static $tblBuffer = array();
98
99
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
100
        } else {
101
            if (is_object($xoopsUser)) {
102
                $uid = $xoopsUser->getVar('uid');
103
            } else {
104
                $uid = 0;
105
            }
106
            if ($uid > 0) {
107
                $member_handler = xoops_getHandler('member');
108
                $tblBuffer      = $member_handler->getGroupsByUser($uid, false);    // Renvoie un tableau d'ID (de groupes)
109
            } else {
110
                $tblBuffer = array(XOOPS_GROUP_ANONYMOUS);
111
            }
112
        }
113
114
        return $tblBuffer;
115
    }
116
117
    /**
118
	 * Renvoie la liste des règles à appliquer sur chaque livre (avec gestion de cache) pour l'utilisateur courant
119
     *
120
     * @return array Tableau d'objets de type Discounts
121
     */
122
    public function getRulesOnEachBook()
123
    {
124
        static $tblBuffer = array();
125
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
126
        } else {
127
            $critere = new CriteriaCompo();
128
            $critere->add(new Criteria('disc_on_what', DISCOUNT_ON3, '='));
129
            $tblGroups = $this->getCurrentMemberGroups();
130
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
131
            $tblBuffer =& $this->getObjects($critere);
132
        }
133
134
        return $tblBuffer;
135
    }
136
137
    /**
138
	 * Renvoie la liste des règles à appliquer sur tous les livres (avec gestion de cache) pour l'utilisateur courant
139
     *
140
     * @return array Tableau d'objets de type Discounts
141
     */
142
    public function getRulesOnAllBooks()
143
    {
144
        static $tblBuffer = array();
145
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
146
        } else {
147
            $critere = new CriteriaCompo();
148
            $critere->add(new Criteria('disc_on_what', DISCOUNT_ON2, '='));
149
            $tblGroups = $this->getCurrentMemberGroups();
150
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
151
            $tblBuffer =& $this->getObjects($critere);
152
        }
153
154
        return $tblBuffer;
155
    }
156
157
    /**
158
	 * Renvoie la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
159
     *
160
     * @return array Tableau d'objets de type Discounts
161
     */
162
    public function getRulesOnShipping()
163
    {
164
        static $tblBuffer = array();
165
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
166
        } else {
167
            $critere = new CriteriaCompo();
168
            $critere->add(new Criteria('disc_on_what', DISCOUNT_ON4, '='));
169
            $tblGroups = $this->getCurrentMemberGroups();
170
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
171
            $tblBuffer =& $this->getObjects($critere);
172
        }
173
174
        return $tblBuffer;
175
    }
176
177
    /**
178
	 * Retourne la liste des règles à appliquer sur les frais de ports (avec gestion de cache) pour l'utilisateur courant
179
     *
180
     * @return array Tableau d'objets de type Discounts
181
     */
182
    public function getRulesOnShipping2()
183
    {
184
        static $tblBuffer = array();
185
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
186
            return $tblBuffer;
187
        } else {
188
            $critere = new CriteriaCompo();
189
            //$critere->add(new Criteria('disc_on_what', DISCOUNT_ON5, '='));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
190
            $critere2 = new CriteriaCompo();
191
            $critere2->add(new Criteria('disc_shipping', DISCOUNT_SHIPPING2, '='));
192
            $critere2->add(new Criteria('disc_shipping', DISCOUNT_SHIPPING3, '='), 'OR');
193
            $critere->add($critere2);
194
            $tblGroups = $this->getCurrentMemberGroups();
195
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
196
            $tblBuffer =& $this->getObjects($critere);
197
198
            return $tblBuffer;
199
        }
200
    }
201
202
    /**
203
	 * Renvoie la liste des règles à appliquer sur l'intégralité de la commande (avec gestion de cache) pour l'utilisateur courant
204
     *
205
     * @return array Tableau d'objets de type Discounts
206
     */
207
    public function getRulesOnCommand()
208
    {
209
        static $tblBuffer = array();
210
        if (is_array($tblBuffer) && count($tblBuffer) > 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
211
        } else {
212
            $critere = new CriteriaCompo();
213
            $critere->add(new Criteria('disc_on_what', DISCOUNT_ON1, '='));
214
            $tblGroups = $this->getCurrentMemberGroups();
215
            $critere->add(new Criteria('disc_group', '(' . implode(',', $tblGroups) . ')', 'IN'));
216
            $tblBuffer =& $this->getObjects($critere);
217
        }
218
219
        return $tblBuffer;
220
    }
221
222
    /**
223
	 * Deuxième lot de réductions, à appliquer sur les frais de port
224
     *
225
     * @param         $montantShipping
226
     * @param         $commandAmount
227
	 * @param array $discountsDescription Descriptions des réductions appliquées
228
     * @param integer $totalBooksQuantity   Le nombre total de livres
229
     *
230
     * @internal param float $montant Montant des frais de port
231
     */
232
    public function applyDiscountOnShipping2(&$montantShipping, &$commandAmount, &$discountsDescription, $totalBooksQuantity)
233
    {
234
        $tblRules = array();
235
        $tblRules = $this->getRulesOnShipping2();    // Renvoie des objets Discounts
236
        if (count($tblRules) > 0) {
237
            foreach ($tblRules as $rule) {
238
                switch ($rule->getVar('disc_shipping')) {
239
                    case DISCOUNT_SHIPPING2:    // Frais de ports totalement gratuits
240
                        if ($commandAmount > (float)$rule->getVar('disc_if_amount')) {
241
                            $discountsDescription[] = $rule->getVar('disc_description');
242
                            $montantShipping        = 0;
243
                        }
244
                        break;
245
                    case DISCOUNT_SHIPPING3:    // Les frais de ports sont de X euros pour le premier article puis de x euros pour les autres
246
						if($totalBooksQuantity > 1) {	// La règle n'est applicable que s'il y a plus d'un article
247
                            $discountsDescription[] = $rule->getVar('disc_description');
248
                            $montantShipping        = (float)$rule->getVar('disc_shipping_amount') + ((float)$rule->getVar('disc_shipping_amount_next') * ($totalBooksQuantity - 1));
249
                        }
250
                        break;
251
                }
252
            }
253
        }
254
    }
255
256
    /**
257
	 * Réductions à appliquer sur le montant global de la commande
258
     *
259
     * @param float $montantHT            Montant HT des livres
260
	 * @param array $discountsDescription Descriptions des réductions appliquées
261
     */
262
    public function applyDiscountOnCommand(&$montantHT, &$discountsDescription)
263
    {
264
        global $xoopsUser, $h_bookshop_commands;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
265
        $tblRules = array();
266
        $tblRules = $this->getRulesOnCommand();    // Renvoie des objets Discounts
267
        if (count($tblRules) > 0) {
268
            $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
269
            foreach ($tblRules as $rule) {
270
                switch ($rule->getVar('disc_when')) {
271
                    case DISCOUNT_WHEN1:    // Dans tous les cas
272
						if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
273
                            $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
274
                            if ($montantHT < 0) {
275
                                $montantHT = 0;
276
                            }
277
						} else {	// Réduction de x euros
278
                            $montantHT -= $rule->getVar('disc_amount');
279
                            if ($montantHT < 0) {
280
                                $montantHT = 0;
281
                            }
282
                        }
283
                        $discountsDescription[] = $rule->getVar('disc_description');
284
                        break;
285
286
                    case DISCOUNT_WHEN2:    // Si c'est le premier achat de l'utilisateur sur le site
287
                        if ($h_bookshop_commands->isFirstCommand($uid)) {
288
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
289
                                $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
290
                                if ($montantHT < 0) {
291
                                    $montantHT = 0;
292
                                }
293
							} else {	// Réduction de x euros
294
                                $montantHT -= $rule->getVar('disc_amount');
295
                                if ($montantHT < 0) {
296
                                    $montantHT = 0;
297
                                }
298
                            }
299
                            $discountsDescription[] = $rule->getVar('disc_description');
300
                        }
301
                        break;
302
                }
303
            }
304
        }
305
    }
306
307
    /**
308
	 * Réductions à appliquer sur les frais de port
309
     *
310
     * @param float   $montantHT            Montant HT des livres
311
	 * @param array $discountsDescription Descriptions des réductions appliquées
312
 	 * @param integer $bookQty Quantité commandée du livre
313
     */
314
    public function applyDiscountOnShipping(&$montantHT, &$discountsDescription, $bookQty)
315
    {
316
        global $xoopsUser, $h_bookshop_commands;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
317
        $tblRules = array();
318
        $tblRules = $this->getRulesOnShipping();    // Renvoie des objets Discounts
319
        if (count($tblRules) > 0) {
320
            $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
321
            foreach ($tblRules as $rule) {
322
                switch ($rule->getVar('disc_when')) {
323
                    case DISCOUNT_WHEN1:    // Dans tous les cas
324
						if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
325
                            $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
326
                            if ($montantHT < 0) {
327
                                $montantHT = 0;
328
                            }
329
						} else {	// Réduction de x euros
330
                            $montantHT -= $rule->getVar('disc_amount');
331
                            if ($montantHT < 0) {
332
                                $montantHT = 0;
333
                            }
334
                        }
335
                        $discountsDescription[] = $rule->getVar('disc_description');
336
                        break;
337
338
                    case DISCOUNT_WHEN2:    // Si c'est le premier achat de l'utilisateur sur le site
339
                        if ($h_bookshop_commands->isFirstCommand($uid)) {
340
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
341
                                $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
342
                                if ($montantHT < 0) {
343
                                    $montantHT = 0;
344
                                }
345
							} else {	// Réduction de x euros
346
                                $montantHT -= $rule->getVar('disc_amount');
347
                                if ($montantHT < 0) {
348
                                    $montantHT = 0;
349
                                }
350
                            }
351
                            $discountsDescription[] = $rule->getVar('disc_description');
352
                        }
353
                        break;
354
355
					case DISCOUNT_WHEN4:	// Si la quantité est =, >, >=, <, <= à ...
356
                        $qtyDiscount = false;
357
                        switch ($rule->getVar('disc_qty_criteria')) {
358
                            case 0:    // =
359
                                if ($bookQty == $rule->getVar('disc_qty_value')) {
360
                                    $qtyDiscount = true;
361
                                }
362
                                break;
363
364
                            case 1:    // >
365
                                if ($bookQty > $rule->getVar('disc_qty_value')) {
366
                                    $qtyDiscount = true;
367
                                }
368
                                break;
369
370
                            case 2:    // >=
371
                                if ($bookQty >= $rule->getVar('disc_qty_value')) {
372
                                    $qtyDiscount = true;
373
                                }
374
                                break;
375
376
                            case 3:    // <
377
                                if ($bookQty < $rule->getVar('disc_qty_value')) {
378
                                    $qtyDiscount = true;
379
                                }
380
                                break;
381
382
                            case 4:    // <=
383
                                if ($bookQty <= $rule->getVar('disc_qty_value')) {
384
                                    $qtyDiscount = true;
385
                                }
386
                                break;
387
388
                        }
389
                        if ($qtyDiscount) {
390
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
391
                                $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
392
                                if ($prixHT < 0) {
393
                                    $prixHT = 0;
394
                                }
395
							} else {	// Réduction de x euros
396
                                $prixHT -= $rule->getVar('disc_amount');
397
                                if ($prixHT < 0) {
398
                                    $prixHT = 0;
399
                                }
400
                            }
401
                            $discountsDescription[] = $rule->getVar('disc_description');
402
                        }
403
                        break;
404
                }
405
            }
406
        }
407
    }
408
409
    /**
410
	 * Réductions à appliquer sur le montant HT de TOUS les livres
411
     *
412
     * @param float   $montantHT            Montant HT des livres
413
	 * @param array $discountsDescription Descriptions des réductions appliquées
414
	 * @param integer $bookQty Quantité commandée du livre
415
     */
416
    public function applyDiscountOnAllBooks(&$montantHT, &$discountsDescription, $bookQty)
417
    {
418
        global $xoopsUser, $h_bookshop_commands;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
419
        $tblRules = array();
420
        $tblRules = $this->getRulesOnAllBooks();    // Renvoie des objets Discounts
421
        if (count($tblRules) > 0) {
422
            $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
423
            foreach ($tblRules as $rule) {
424
                switch ($rule->getVar('disc_when')) {
425
                    case DISCOUNT_WHEN1:    // Dans tous les cas
426
						if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
427
                            $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
428
                            if ($montantHT < 0) {
429
                                $montantHT = 0;
430
                            }
431
						} else {	// Réduction de x euros
432
                            $montantHT -= $rule->getVar('disc_amount');
433
                            if ($montantHT < 0) {
434
                                $montantHT = 0;
435
                            }
436
                        }
437
                        $discountsDescription[] = $rule->getVar('disc_description');
438
                        break;
439
440
                    case DISCOUNT_WHEN2:    // Si c'est le premier achat de l'utilisateur sur le site
441
                        if ($h_bookshop_commands->isFirstCommand($uid)) {
442
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
443
                                $montantHT = bookshop_getDiscountedPrice($montantHT, $rule->getVar('disc_amount'));
444
                                if ($montantHT < 0) {
445
                                    $montantHT = 0;
446
                                }
447
							} else {	// Réduction de x euros
448
                                $montantHT -= $rule->getVar('disc_amount');
449
                                if ($montantHT < 0) {
450
                                    $montantHT = 0;
451
                                }
452
                            }
453
                            $discountsDescription[] = $rule->getVar('disc_description');
454
                        }
455
                        break;
456
457
					case DISCOUNT_WHEN4:	// Si la quantité est =, >, >=, <, <= à ...
458
                        $qtyDiscount = false;
459
                        switch ($rule->getVar('disc_qty_criteria')) {
460
                            case 0:    // =
461
                                if ($bookQty == $rule->getVar('disc_qty_value')) {
462
                                    $qtyDiscount = true;
463
                                }
464
                                break;
465
466
                            case 1:    // >
467
                                if ($bookQty > $rule->getVar('disc_qty_value')) {
468
                                    $qtyDiscount = true;
469
                                }
470
                                break;
471
472
                            case 2:    // >=
473
                                if ($bookQty >= $rule->getVar('disc_qty_value')) {
474
                                    $qtyDiscount = true;
475
                                }
476
                                break;
477
478
                            case 3:    // <
479
                                if ($bookQty < $rule->getVar('disc_qty_value')) {
480
                                    $qtyDiscount = true;
481
                                }
482
                                break;
483
484
                            case 4:    // <=
485
                                if ($bookQty <= $rule->getVar('disc_qty_value')) {
486
                                    $qtyDiscount = true;
487
                                }
488
                                break;
489
490
                        }
491
                        if ($qtyDiscount) {
492
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
493
                                $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
494
                                if ($prixHT < 0) {
495
                                    $prixHT = 0;
496
                                }
497
							} else {	// Réduction de x euros
498
                                $prixHT -= $rule->getVar('disc_amount');
499
                                if ($prixHT < 0) {
500
                                    $prixHT = 0;
501
                                }
502
                            }
503
                            $discountsDescription[] = $rule->getVar('disc_description');
504
                        }
505
                        break;
506
                }
507
            }
508
        }
509
    }
510
511
    /**
512
	 * Recalcul du prix HT du livre en appliquant les réductions, s'il y a lieu
513
     *
514
     * @param integer $bookId               Identifiant du livre
515
     * @param float   $prixHT               Prix HT du livre
516
	 * @param array $discountsDescription Descriptions des réductions appliquées
517
	 * @param integer $bookQty Quantité commandée du livre
518
     */
519
    public function applyDiscountOnEachBook($bookId, &$prixHT, &$discountsDescription, $bookQty)
520
    {
521
        global $xoopsUser, $h_bookshop_commands;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
522
        $tblRules = array();
523
        $tblRules = $this->getRulesOnEachBook();    // Renvoie des objets Discounts
524
        if (count($tblRules) > 0) {
525
            $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
526
            foreach ($tblRules as $rule) {
527
                switch ($rule->getVar('disc_when')) {
528
                    case DISCOUNT_WHEN1:    // Dans tous les cas
529
						if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
530
                            $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
531
						} else {	// Réduction de x euros
532
                            $prixHT -= $rule->getVar('disc_amount');
533
                        }
534
                        if ($prixHT < 0) {
535
                            $prixHT = 0;
536
                        }
537
                        $discountsDescription[] = $rule->getVar('disc_description');
538
                        break;
539
540
                    case DISCOUNT_WHEN2:    // Si c'est le premier achat de l'utilisateur sur le site
541
                        if ($h_bookshop_commands->isFirstCommand($uid)) {
542
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
543
                                $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
544
							} else {	// Réduction de x euros
545
                                $prixHT -= $rule->getVar('disc_amount');
546
                            }
547
                            if ($prixHT < 0) {
548
                                $prixHT = 0;
549
                            }
550
                            $discountsDescription[] = $rule->getVar('disc_description');
551
                        }
552
                        break;
553
554
					case DISCOUNT_WHEN3:	// Si le livre n'a jamais été acheté
555
                        if (!$h_bookshop_commands->BookAlreadyBought($uid, $bookId)) {
556
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
557
                                $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
558
							} else {	// Réduction de x euros
559
                                $prixHT -= $rule->getVar('disc_amount');
560
                            }
561
                            if ($prixHT < 0) {
562
                                $prixHT = 0;
563
                            }
564
                            $discountsDescription[] = $rule->getVar('disc_description');
565
                        }
566
                        break;
567
568
					case DISCOUNT_WHEN4:	// Si la quantité est =, >, >=, <, <= à ...
569
                        $qtyDiscount = false;
570
                        switch ($rule->getVar('disc_qty_criteria')) {
571
                            case 0:    // =
572
                                if ($bookQty == $rule->getVar('disc_qty_value')) {
573
                                    $qtyDiscount = true;
574
                                }
575
                                break;
576
577
                            case 1:    // >
578
                                if ($bookQty > $rule->getVar('disc_qty_value')) {
579
                                    $qtyDiscount = true;
580
                                }
581
                                break;
582
583
                            case 2:    // >=
584
                                if ($bookQty >= $rule->getVar('disc_qty_value')) {
585
                                    $qtyDiscount = true;
586
                                }
587
                                break;
588
589
                            case 3:    // <
590
                                if ($bookQty < $rule->getVar('disc_qty_value')) {
591
                                    $qtyDiscount = true;
592
                                }
593
                                break;
594
595
                            case 4:    // <=
596
                                if ($bookQty <= $rule->getVar('disc_qty_value')) {
597
                                    $qtyDiscount = true;
598
                                }
599
                                break;
600
601
                        }
602
                        if ($qtyDiscount) {
603
							if($rule->getVar('disc_percent_monney') == DISCOUNT_TYPE1) {	// Réduction de x pourcent
604
                                $prixHT = bookshop_getDiscountedPrice($prixHT, $rule->getVar('disc_amount'));
605
                                if ($prixHT < 0) {
606
                                    $prixHT = 0;
607
                                }
608
							} else {	// Réduction de x euros
609
                                $prixHT -= $rule->getVar('disc_amount');
610
                                if ($prixHT < 0) {
611
                                    $prixHT = 0;
612
                                }
613
                            }
614
                            $discountsDescription[] = $rule->getVar('disc_description');
615
                        }
616
                        break;
617
                }
618
            }
619
        }
620
    }
621
}
622
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected EOF
Loading history...