1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Finance module for HiPanel |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
7
|
|
|
* @package hipanel-module-finance |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\modules\finance\cart; |
13
|
|
|
|
14
|
|
|
use hipanel\modules\finance\logic\Calculator; |
15
|
|
|
use hipanel\modules\finance\models\Calculation; |
|
|
|
|
16
|
|
|
use hipanel\modules\finance\models\Value; |
17
|
|
|
use hiqdev\yii2\cart\ShoppingCart; |
18
|
|
|
use Yii; |
19
|
|
|
use yz\shoppingcart\CartActionEvent; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class CartCalculator provides API to calculate [[cart]] positions value |
23
|
|
|
* |
24
|
|
|
* Usage: |
25
|
|
|
* |
26
|
|
|
* ```php |
27
|
|
|
* $calculator = new CartCalculator([ |
28
|
|
|
* 'cart' => $this->cart |
29
|
|
|
* ]); |
30
|
|
|
* |
31
|
|
|
* $calculator->run(); // will calculate prices for all cart positions and update them |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* Also can be bound to some cart event as handler: |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* $cart->on(Cart::EVENT_UPDATE, [CartCalculator::class, 'handle']); |
38
|
|
|
* ``` |
39
|
|
|
* |
40
|
|
|
* @package hipanel\modules\finance\cart |
41
|
|
|
*/ |
42
|
|
|
class CartCalculator extends Calculator |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var AbstractCartPosition[] |
46
|
|
|
*/ |
47
|
|
|
protected $models; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ShoppingCart |
51
|
|
|
*/ |
52
|
|
|
public $cart; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var CartActionEvent |
56
|
|
|
*/ |
57
|
|
|
public $event; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates the instance of the object and runs the calculation. |
61
|
|
|
* |
62
|
|
|
* @param CartActionEvent $event The event |
63
|
|
|
* @void |
64
|
|
|
*/ |
65
|
|
|
public static function handle($event) |
66
|
|
|
{ |
67
|
|
|
/** @var ShoppingCart $cart */ |
68
|
|
|
$cart = $event->sender; |
69
|
|
|
|
70
|
|
|
$calculator = new static($cart); |
71
|
|
|
return $calculator->execute(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param ShoppingCart $cart |
76
|
|
|
*/ |
77
|
|
|
public function __construct(ShoppingCart $cart) |
78
|
|
|
{ |
79
|
|
|
$this->cart = $cart; |
80
|
|
|
|
81
|
|
|
parent::__construct($this->cart->positions); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function execute() |
88
|
|
|
{ |
89
|
|
|
parent::execute(); |
90
|
|
|
|
91
|
|
|
$this->applyCalculations(); |
92
|
|
|
|
93
|
|
|
return $this->calculations; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Updates positions using the calculations provided with [[getCalculation]] |
98
|
|
|
*/ |
99
|
|
|
private function applyCalculations() |
100
|
|
|
{ |
101
|
|
|
$currency = Yii::$app->params['currency']; |
102
|
|
|
|
103
|
|
|
foreach ($this->models as $position) { |
104
|
|
|
$id = $position->id; |
|
|
|
|
105
|
|
|
|
106
|
|
|
$calculation = $this->getCalculation($id); |
107
|
|
|
if (!$calculation instanceof Calculation) { |
108
|
|
|
Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because of failed value calculation. Normally this should never happen.', 'hipanel.cart'); |
109
|
|
|
$this->cart->removeById($position->id); |
|
|
|
|
110
|
|
|
break; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$value = $calculation->forCurrency($currency); |
114
|
|
|
if (!$value instanceof Value) { |
115
|
|
|
Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because calculation for currency "' . $currency . '" is not available', 'hipanel.cart'); |
116
|
|
|
$this->cart->removeById($position->id); |
|
|
|
|
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$position->setPrice($value->price); |
|
|
|
|
121
|
|
|
$position->setValue($value->value); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: