Intraface_modules_shop_Basket::getItemCount()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 20
ccs 14
cts 14
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Basket
4
 *
5
 * PHP version 5
6
 *
7
 * @package Intraface_Shop
8
 * @author Lars Olesen <[email protected]>
9
 */
10 1
require_once 'Intraface/functions.php';
11 1
require_once 'Intraface/modules/product/Product.php';
12
13
class Intraface_modules_shop_Basket
14
{
15
    /**
16
     * @var object
17
     */
18
    private $webshop;
19
20
    /**
21
     * @var object
22
     */
23
    private $coordinator;
24
25
    /**
26
     * @var object
27
     */
28
    private $intranet;
29
30
    /**
31
     * Variablen bruges, fordi webshop almindeligvis bruges uden for systemet.
32
     * For at kunne holde fx indk�bskurven intakt, s� skal den alts� kunne fastholde
33
     * session id'et. Det ville den ikke kunne, fordi hver kontakt over xml-rpc jo
34
     * er en ny foresp�rgsel og alts� en ny session p� serveren.
35
     *
36
     * @var string
37
     */
38
    private $session_id;
39
40
    /**
41
     * @var object
42
     */
43
    private $db;
44
45
    /**
46
     * @var array $items
47
     */
48
    private $items;
49
50
    const CLEAN_UP_AFTER = 2;
51
52
    /**
53
     * Constructor
54
     *
55
     * @param object $db         The webshop object
56
     * @param object $intranet   The webshop object
57
     * @param object $webshop    The webshop object
0 ignored issues
show
Bug introduced by
There is no parameter named $webshop. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
     * @param string $session_id A session id
59
     *
60
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
61
     */
62 21
    public function __construct($db, $intranet, $coordinator, $shop, $session_id)
63
    {
64 21
        $this->db          = $db;
65 21
        $this->intranet    = $intranet;
66 21
        $this->coordinator = $coordinator;
67 21
        $this->webshop     = $shop;
68 21
        $this->session_id  = $session_id;
69 21
        $this->resetItemCache();
70
71 21
        $this->conditions = array(
0 ignored issues
show
Bug introduced by
The property conditions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72 21
            'session_id = ' . $this->db->quote($this->session_id, 'text'),
73 21
            'shop_id = ' . $this->db->quote($this->webshop->getId(), 'integer'),
74 21
            'intranet_id = ' . $this->db->quote($this->intranet->getId(), 'integer'));
75
76 21
        $this->cleanUp();
77 21
    }
78
79 21
    private function cleanUp()
80
    {
81 21
        return $this->db->query("DELETE FROM basket WHERE DATE_ADD(date_changed, INTERVAL " . $this->db->quote(self::CLEAN_UP_AFTER, 'integer') . " HOUR) < NOW()");
82
    }
83
84
    /**
85
     * Adds a product to the basket
86
     *
87
     * @param integer $product_id Id of product
88
     * @param integer $quantity   Number of products to add
89
     *
90
     * @return boolean
91
     */
92 6
    public function add($product_id, $product_variation_id = 0, $quantity = 1, $text = '', $product_detail_id = 0)
93
    {
94 6
        $product_id = intval($product_id);
95 6
        $product_variation_id = intval($product_variation_id);
96 6
        $quantity = intval($quantity);
97 6
        $quantity = $this->getItemCount($product_id, $product_variation_id) + $quantity;
98 6
        return $this->change($product_id, $product_variation_id, $quantity, $text, $product_detail_id);
99
    }
100
101
    /**
102
     * Removes a product from the basket
103
     *
104
     * @param integer $product_id Product to remove
105
     * @param integer $quantity   How many should be removed
106
     *
107
     * @return boelean
108
     */
109 1
    public function remove($product_id, $product_variation_id = 0, $quantity = 1)
110
    {
111 1
        $product_id = intval($product_id);
112 1
        $product_variation_id = intval($product_variation_id);
113 1
        $quantity = intval($quantity);
114 1
        $quantity = $this->getItemCount($product_id, $product_variation_id) - $quantity;
115 1
        return $this->change($product_id, $product_variation_id, $quantity);
116
    }
117
118
    /**
119
     * Changes a product in the basket
120
     *
121
     * @param integer $product_id       Product id
122
     * @param integer $quantity         The quantity
123
     * @param string  $text             To add description to product, not yet implemented
124
     * @param integer $basketevaluation Wheter the product is from basketevaluation
125
     *
126
     * @return boolean
127
     */
128 10
    public function change($product_id, $product_variation_id, $quantity, $text = '', $product_detail_id = 0, $basketevaluation = 0)
129
    {
130 10
        $db = new DB_Sql;
131 10
        $product_id = (int)$product_id;
132 10
        $product_variation_id = intval($product_variation_id);
133 10
        $product_detail_id = (int)$product_detail_id;
134 10
        $quantity = (int)$quantity;
135
136 10
        $this->resetItemCache();
137
138 10
        $this->coordinator->kernel->useModule('product');
139
140 10
        $product = new Product($this->coordinator->kernel, $product_id, $product_detail_id);
141
142 10
        if ($product->get('id') == 0) {
143
            return false;
144
        }
145
146 10
        if ($product->get('stock')) {
147
            if ($product_variation_id != 0) {
148
                $variation = $product->getVariation($product_variation_id);
149
                if (!$variation->getId()) {
150
                    return false;
151
                }
152
                $stock = $variation->getStock($product);
153
            } else {
154
                $stock = $product->getStock();
155
            }
156
157
            if (is_object($stock) and $stock->get('for_sale') < $quantity and $quantity != 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
158
                return false;
159
            }
160
        }
161
162 10
        $sql_extra = implode(" AND ", $this->conditions);
163
164 10
        $db->query("SELECT id, quantity
165
                FROM basket
166 10
                WHERE product_id = ".$product_id."
167 10
                    AND product_variation_id = ".$product_variation_id."
168 10
                    AND product_detail_id = " . $product_detail_id . "
169 10
                    AND basketevaluation_product = " . $basketevaluation . "
170 10
                    AND " . $sql_extra);
171
172 10
        if ($db->nextRecord()) {
173 3
            if ($quantity == 0) {
174 1
                $db->query("DELETE FROM basket
175 1
                    WHERE id = ".$db->f('id') . "
176 1
                        AND basketevaluation_product = " . $basketevaluation . "
177 1
                        AND " . $sql_extra);
178 1
            } else {
179 2
                $db->query("UPDATE basket SET
180
                    quantity = $quantity,
181
                    date_changed = NOW(),
182 2
                    text = '".$text."'
183 2
                    WHERE id = ".$db->f('id') . "
184 2
                        AND basketevaluation_product = " . $basketevaluation . "
185 2
                        AND " . $sql_extra);
186
            }
187 3
            return true;
188
        } else {
189 10
            $sql_extra = implode(', ', $this->conditions);
190 10
            $db->query("INSERT INTO basket
191
                    SET
192
                        quantity = $quantity,
193
                        date_changed = NOW(),
194 10
                        text = '".$text."',
195 10
                        basketevaluation_product = " . $basketevaluation . ",
196 10
                        product_id = ".$product_id.",
197 10
                        product_variation_id = ".$product_variation_id.",
198 10
                        product_detail_id = ".$product_detail_id.",
199 10
                        " . $sql_extra);
200 10
            return true;
201
        }
202
    }
203
204
    /**
205
     * Save order details
206
     *
207
     * @param (array)input  array with buyer details
208
     *
209
     * @return boolean true or false
210
     */
211 2 View Code Duplication
    public function saveAddress($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213 2
        settype($input['name'], 'string');
214 2
        settype($input['contactperson'], 'string');
215 2
        settype($input['address'], 'string');
216 2
        settype($input['postcode'], 'string');
217 2
        settype($input['city'], 'string');
218 2
        settype($input['country'], 'string');
219 2
        settype($input['cvr'], 'string');
220 2
        settype($input['email'], 'string');
221 2
        settype($input['phone'], 'string');
222
223 2
        $sql = "name = \"".safeToDb($input['name'])."\"," .
224 2
            "contactperson = \"".safeToDb($input['contactperson'])."\", " .
225 2
            "address = \"".safeToDb($input['address'])."\", " .
226 2
            "postcode = \"".safeToDb($input['postcode'])."\", " .
227 2
            "city = \"".safeToDb($input['city'])."\", ".
228 2
            "country = \"".safeToDb($input['country'])."\", ".
229 2
            "cvr = \"".safeToDb($input['cvr'])."\", ".
230 2
            "email =\"".safeToDb($input['email'])."\", ".
231 2
            "phone = \"".safeToDb($input['phone'])."\"";
232
233 2
        return $this->saveToDb($sql);
234
    }
235
236
    /**
237
     * Save customer coupon
238
     *
239
     * @param string $customer_coupon customer coupon
240
     *
241
     * @return boolean true or false
242
     */
243 1
    public function saveCustomerCoupon($customer_coupon)
244
    {
245 1
        $sql = "customer_coupon = \"".$customer_coupon."\"";
246
247 1
        return $this->saveToDb($sql);
248
    }
249
250
    /**
251
     * Save customer EAN location number
252
     *
253
     * @param string $customer_ean customer coupon
254
     *
255
     * @return boolean true or false
256
     */
257 1
    public function saveCustomerEan($customer_ean)
258
    {
259 1
        $sql = "customer_ean = \"".$customer_ean."\"";
260
261 1
        return $this->saveToDb($sql);
262
    }
263
264
    /**
265
     * Save customer comment
266
     *
267
     * @param string $customer_comment comment
268
     *
269
     * @return boolean true or false
270
     */
271 1
    public function saveCustomerComment($customer_comment)
272
    {
273 1
        $sql = "customer_comment = \"".$customer_comment."\"";
274
275 1
        return $this->saveToDb($sql);
276
    }
277
278
    /**
279
     * Save Payment method
280
     *
281
     * @param string $payment_method
282
     *
283
     * @return boolean true or false
284
     */
285
    public function savePaymentMethod($payment_method)
286
    {
287
288
        $payment_method_key = $this->coordinator->getPaymentMethodKeyFromIdenfifier($payment_method);
289
290
        $sql = "payment_method_key = \"".$payment_method_key."\"";
291
292
        return $this->saveToDb($sql);
293
    }
294
295
    /**
296
     * @todo Is this really public
297
     * @todo Strange name
298
     *
299
     * @param string $sql Extra sql string to add
300
     */
301 5
    public function saveToDb($sql)
302
    {
303 5
        $sql_extra = implode(" AND ", $this->conditions);
304
305 5
        $db = new DB_Sql;
306 5
        $db->query("SELECT id FROM basket_details WHERE " . $sql_extra. "
307 5
                AND intranet_id = " . $this->intranet->getId());
308 5
        if ($db->nextRecord()) {
309
            $db->query("UPDATE basket_details SET ".$sql.",
310
                date_changed = NOW()
311
                WHERE id = ".$db->f('id') . "
312
                    AND " . $sql_extra . "
313
                    AND intranet_id = " . $this->intranet->getId());
314
            return true;
315
        } else {
316 5
            $sql_extra = implode(", ", $this->conditions);
317 5
            $db->query("INSERT INTO basket_details
318 5
                    SET ".$sql.",
319
                        date_changed = NOW(),
320
                        date_created = NOW(),
321 5
                        " . $sql_extra);
322
323 5
            return true;
324
        }
325
    }
326
327
    /**
328
     * Return buyer details
329
     *
330
     * @todo Should return an object
331
     *
332
     * @return array of buyer details.
333
     */
334 2
    public function getAddress()
335
    {
336 2
        $sql_extra = implode(" AND ", $this->conditions);
337 2
        $db = new DB_Sql;
338 2
        $db->query("SELECT *
339
            FROM basket_details
340 2
            WHERE " . $sql_extra . "
341 2
                AND intranet_id = " . $this->intranet->getId());
342 2
        if (!$db->nextRecord()) {
343
            return array();
344
        }
345
346 2
        return array('name' => $db->f('name'),
347 2
            'contactperson' => $db->f('contactperson'),
348 2
            'address' => $db->f('address'),
349 2
            'postcode' => $db->f('postcode'),
350 2
            'city' => $db->f('city'),
351 2
            'country' => $db->f('country'),
352 2
            'cvr' => $db->f('cvr'),
353 2
            'email' => $db->f('email'),
354 2
            'phone' => $db->f('phone'));
355
    }
356
357
    /**
358
     * Return customer coupon
359
     *
360
     * @todo Why return an array
361
     *
362
     * @return array with customer coupon
363
     */
364 1 View Code Duplication
    public function getCustomerCoupon()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
365
    {
366 1
        $sql_extra = implode(" AND ", $this->conditions);
367 1
        $db = new DB_Sql;
368 1
        $db->query("SELECT customer_coupon
369
            FROM basket_details
370 1
            WHERE " . $sql_extra . "
371 1
                AND intranet_id = " . $this->intranet->getId());
372 1
        if (!$db->nextRecord()) {
373
            return array();
374
        }
375
376 1
        return array('customer_coupon' => $db->f('customer_coupon'));
377
    }
378
379
    /**
380
     * Return customer EAN location number
381
     *
382
     * @todo Why return an array
383
     *
384
     * @return array with customer ean
385
     */
386 1 View Code Duplication
    public function getCustomerEan()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
387
    {
388 1
        $sql_extra = implode(" AND ", $this->conditions);
389 1
        $db = new DB_Sql;
390 1
        $db->query("SELECT customer_ean
391
            FROM basket_details
392 1
            WHERE " . $sql_extra . "
393 1
                AND intranet_id = " . $this->intranet->getId());
394 1
        if (!$db->nextRecord()) {
395
            return array();
396
        }
397
398 1
        return array('customer_ean' => $db->f('customer_ean'));
399
    }
400
401
    /**
402
     * Return customer coupon
403
     *
404
     * @todo Why return an array
405
     *
406
     * @return array with customer coupon
407
     */
408 1 View Code Duplication
    public function getCustomerComment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
409
    {
410 1
        $sql_extra = implode(" AND ", $this->conditions);
411 1
        $db = new DB_Sql;
412 1
        $db->query("SELECT customer_comment
413
            FROM basket_details
414 1
            WHERE " . $sql_extra . "
415 1
                AND intranet_id = " . $this->intranet->getId());
416 1
        if (!$db->nextRecord()) {
417
            return array();
418
        }
419
420 1
        return array('customer_comment' => $db->f('customer_comment'));
421
    }
422
423
    /**
424
     * Return payment method
425
     *
426
     * @todo Why return an array? Then it is possible to extend with more data later.
427
     *
428
     * @return array with customer coupon
429
     */
430
    public function getPaymentMethod()
431
    {
432
        $sql_extra = implode(" AND ", $this->conditions);
433
        $db = new DB_Sql;
434
        $db->query("SELECT payment_method_key
435
            FROM basket_details
436
            WHERE " . $sql_extra . "
437
                AND intranet_id = " . $this->intranet->getId());
438
        if (!$db->nextRecord() || $db->f('payment_method_key') == 0) {
439
            return array();
440
        }
441
442
        return array('payment_method' => $this->coordinator->getPaymentMethodFromKey($db->f('payment_method_key')));
443
    }
444
445
    /**
446
     * Counts the number of a certain product in the basket
447
     *
448
     * @param integer $product_id Product id of the product to count
449
     *
450
     * @return integer
451
     */
452 6
    public function getItemCount($product_id, $product_variation_id = 0)
453
    {
454 6
        $product_id = (int)$product_id;
455 6
        $product_variation_id = (int)$product_variation_id;
456
457 6
        $sql_extra = implode(" AND ", $this->conditions);
458 6
        $db = new DB_Sql;
459 6
        $db->query("SELECT *
460
            FROM basket
461 6
            WHERE " . $sql_extra . "
462 6
                AND product_id = " . $product_id . "
463 6
                AND product_variation_id = ".$product_variation_id."
464 6
                AND intranet_id = " . $this->intranet->getId() . "
465 6
      AND quantity > 0 LIMIT 1");
466
467 6
        if (!$db->nextRecord()) {
468 6
            return 0;
469
        }
470 1
        return $db->f("quantity");
471
    }
472
473
    /**
474
     * Gets the total price of the basket
475
     *
476
     * @return float
477
     */
478 2
    public function getTotalPrice($type = 'including_vat')
479
    {
480 2
        $price = 0;
481
482 2
        foreach ($this->getItems() as $item) {
483 2
            if ($type == 'exclusive_vat') {
484
                $price += $item['totalprice'];
485
            } else {
486 2
                $price += $item['totalprice_incl_vat'];
487
            }
488 2
        }
489
490 2
        return round($price, 2);
491
    }
492
493
    /**
494
     * Gets the total price of the basket in given currencies
495
     *
496
     * @param object $currencies Collection of currencies
497
     * @return float
498
     */
499
    public function getTotalPriceInCurrencies($currencies)
500
    {
501
502
        $total['DKK']['ex_vat'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$total was never initialized. Although not strictly required by PHP, it is generally a good practice to add $total = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
503
        $total['DKK']['incl_vat'] = 0;
504
505
        if (is_object($currencies) && $currencies->count() > 0) {
506
            foreach ($currencies as $currency) {
507
                $total[$currency->getType()->getIsoCode()]['ex_vat'] = 0;
508
                $total[$currency->getType()->getIsoCode()]['incl_vat'] = 0;
509
            }
510
        }
511
512
        foreach ($this->getItems($currencies) as $item) {
0 ignored issues
show
Documentation introduced by
$currencies is of type object, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
513
            $total['DKK']['ex_vat'] += $item['currency']['DKK']['price'];
514
            $total['DKK']['incl_vat'] += $item['currency']['DKK']['totalprice_incl_vat'];
515
516
            if (is_object($currencies) && $currencies->count() > 0) {
517
                foreach ($currencies as $currency) {
518
                    $total[$currency->getType()->getIsoCode()]['ex_vat'] += $item['currency'][$currency->getType()->getIsoCode()]['price'];
519
                    $total[$currency->getType()->getIsoCode()]['incl_vat'] += $item['currency'][$currency->getType()->getIsoCode()]['totalprice_incl_vat'];
520
                }
521
            }
522
        }
523
524
        return $total;
525
    }
526
527
    /**
528
     * Gets the total weight of the basket
529
     *
530
     * @return float
531
     */
532 1
    public function getTotalWeight()
533
    {
534 1
        $weight = 0;
535
536 1
        foreach ($this->getItems() as $item) {
537 1
            $weight += $item['totalweight'];
538 1
        }
539
540 1
        return $weight;
541
    }
542
543
    /**
544
     * Gets all items in the basket
545
     *
546
     * @return array
547
     */
548 14
    public function getItems($currencies = false)
549
    {
550
551
        // Local cache
552 14
        if ($this->items !== null && is_array($this->items)) {
553
            return $this->items;
554
        }
555
556 14
        $sql_extra = implode(" AND basket.", $this->conditions);
557 14
        $items = array();
558 14
        $db = new DB_Sql;
559
560 14
        $db->query("SELECT
561
                product.id,
562
                basket.product_id,
563
                basket.product_variation_id,
564
                basket.product_detail_id,
565
                product_detail.price,
566
                basket.quantity,
567
                basket.text,
568
                basket.basketevaluation_product
569
            FROM basket
570
            INNER JOIN product
571
                ON product.id = basket.product_id
572
            INNER JOIN product_detail
573
                ON product.id = product_detail.product_id
574 14
            WHERE " . $sql_extra . "
575
                AND product_detail.active = 1
576 14
                AND basket.intranet_id = " . $this->intranet->getId() . "
577 14
            ORDER BY product_detail.vat DESC, basket.basketevaluation_product");
578
579 14
        $i = 0;
580 14
        while ($db->nextRecord()) {
581 10
            $items[$i]['id'] = $db->f("id");
582 10
            $items[$i]['text'] = $db->f("text");
583 10
            $items[$i]['basketevaluation_product'] = $db->f("basketevaluation_product");
584 10
            $product = new Product($this->coordinator->kernel, $db->f("id"));
585 10
            $product->getPictures();
586 10
            $items[$i]['product_id'] = $product->get('id');
587 10
            $items[$i]['product_detail_id'] = $product->get('product_detail_id');
588 10
            $items[$i]['pictures'] = $product->get('pictures');
589
590
            //if ($db->f('product_variation_id') != 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
591 10
            if ($product->hasVariation()) {
592 3
                $variation = $product->getVariation($db->f('product_variation_id'));
593 3
                $detail = $variation->getDetail();
594 3
                $items[$i]['product_variation_id'] = $variation->getId();
595 3
                $items[$i]['name'] = $product->get('name').' - '.$variation->getName();
596 3
                $items[$i]['weight'] = $detail->getWeight($product)->getAsIso(0);
597 3
                $items[$i]['price'] = $detail->getPrice($product)->getAsIso(2);
598 3
                $items[$i]['price_incl_vat'] = $detail->getPriceIncludingVat($product)->getAsIso(2);
599
600 3
                $items[$i]['currency']['DKK']['price'] = $detail->getPrice($product)->getAsIso();
601 3
                $items[$i]['currency']['DKK']['price_incl_vat'] = $detail->getPriceIncludingVat($product)->getAsIso(4);
602 3 View Code Duplication
                if (is_object($currencies) && $currencies->count() > 0) {
603
                    foreach ($currencies as $currency) {
604
                        $items[$i]['currency'][$currency->getType()->getIsoCode()]['price'] = $detail->getPriceInCurrency($currency, 0, $product)->getAsIso();
605
                        $items[$i]['currency'][$currency->getType()->getIsoCode()]['price_incl_vat'] = $detail->getPriceIncludingVatInCurrency($currency, 0, $product)->getAsIso(4);
606
                    }
607
                }
608 3
            } else {
609 9
                $items[$i]['product_variation_id'] = 0;
610 9
                $items[$i]['name'] = $product->get('name');
611 9
                $items[$i]['weight'] = $product->get('weight');
612 9
                $items[$i]['price'] = $product->getDetails()->getPrice()->getAsIso(2);
613 9
                $items[$i]['price_incl_vat'] = $product->getDetails()->getPriceIncludingVat()->getAsIso(2);
614
615 9
                $items[$i]['currency']['DKK']['price'] = $product->getDetails()->getPrice()->getAsIso(2);
616 9
                $items[$i]['currency']['DKK']['price_incl_vat'] = $product->getDetails()->getPriceIncludingVat()->getAsIso(2);
617 9 View Code Duplication
                if (is_object($currencies) && $currencies->count() > 0) {
618
                    foreach ($currencies as $currency) {
619
                        $items[$i]['currency'][$currency->getType()->getIsoCode()]['price'] = $product->getDetails()->getPriceInCurrency($currency)->getAsIso(2);
620
                        $items[$i]['currency'][$currency->getType()->getIsoCode()]['price_incl_vat'] = $product->getDetails()->getPriceIncludingVatInCurrency($currency)->getAsIso(2);
621
                    }
622
                }
623
            }
624
625
            // basket specific
626 10
            $items[$i]['quantity'] = $db->f('quantity');
627 10
            $items[$i]['totalweight'] = $items[$i]['weight'] * $db->f('quantity');
628 10
            $items[$i]['totalprice'] = $db->f('quantity') * $items[$i]['price'];
629 10
            $items[$i]['totalprice_incl_vat'] = $db->f('quantity') * $items[$i]['price_incl_vat'];
630
631 10
            $items[$i]['currency']['DKK']['totalprice'] = $db->f('quantity') * $items[$i]['currency']['DKK']['price'];
632 10
            $items[$i]['currency']['DKK']['totalprice_incl_vat'] = $db->f('quantity') * $items[$i]['currency']['DKK']['price_incl_vat'];
633 10
            if (is_object($currencies) && $currencies->count() > 0) {
634
                foreach ($currencies as $currency) {
635
                    $items[$i]['currency'][$currency->getType()->getIsoCode()]['totalprice'] = $db->f('quantity') * $items[$i]['currency'][$currency->getType()->getIsoCode()]['price'];
636
                    $items[$i]['currency'][$currency->getType()->getIsoCode()]['totalprice_incl_vat'] = $db->f('quantity') * $items[$i]['currency'][$currency->getType()->getIsoCode()]['price_incl_vat'];
637
                }
638
            }
639
640 10
            $i++;
641 10
        }
642
643 14
        $this->items = $items;
644 14
        return $items;
645
    }
646
647
    /**
648
     * Resets the item cache
649
     */
650 21
    private function resetItemCache()
651
    {
652 21
        $this->items = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
653 21
    }
654
655
    /**
656
     * Helperfunction for BasketEvaluation. Removes all products from basket
657
     * placed by BasketEvaluation.
658
     *
659
     * @return boolean
660
     */
661 2
    public function removeEvaluationProducts()
662
    {
663 2
        $sql_extra = implode(" AND ", $this->conditions);
664 2
        $db = new DB_Sql;
665 2
        $db->query("DELETE FROM basket " .
666 2
                "WHERE basketevaluation_product = 1 " .
667 2
                    "AND " . $sql_extra . " " .
668 2
                    "AND intranet_id = " . $this->intranet->getId());
669
670 2
        $this->resetItemCache();
671
672 2
        return true;
673
    }
674
675
    /**
676
     * Resets the basket for a session
677
     *
678
     * @return boolean
679
     */
680 5
    public function reset()
681
    {
682 5
        $this->resetItemCache();
683
684 5
        $sql_extra = implode(" AND ", $this->conditions);
685 5
        $db = new DB_Sql;
686 5
        $db->query("UPDATE basket SET session_id = '' WHERE " . $sql_extra . " AND intranet_id = " . $this->intranet->getId());
687 5
        $db->query("UPDATE basket_details SET session_id = '' WHERE " . $sql_extra . " AND intranet_id = " . $this->intranet->getId());
688
689 5
        return true;
690
    }
691
}
692