-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cart.php
executable file
·357 lines (318 loc) · 9.49 KB
/
Cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Class Cart
*/
class Cart
{
/**
* @var object $instance
*/
public static $instance;
/**
* @var http_session $session
*/
protected $session;
/**
* @var string $cart_name session name of the cart
* @var string $key key id of the cart
*/
protected
$cart_name,
$key;
/**
* @var int $nb_items Total number of items in the cart
*/
private $nb_items = 0;
/**
* @var array $items Items in the cart
* @var array $fees Optional fees to add
* @var array $total Total prices and taxes
*/
private
$items = [],
$fees = [],
$total = [
'exc' => 0,
'inc' => 0,
'vat' => []
];
/**
* Cart constructor.
*/
public function __construct() {
$settingComp = new component_collections_setting();
$settings = $settingComp->getSetting();
$this->session = new http_session($settings['ssl']);//['value']);
$this->newCart();
}
/**
* Return the key id of the current cart
* @return string
*/
public function getKey(): string {
return $this->key;
}
/**
* @param object|CartItem $newitem
* @return int
*/
public function inCart(object $newitem): int {
foreach ($this->items as $i => $item) {
if($item['item'] == $newitem) return $i;
}
return -1;
}
/**
* @param int|string $id
* @param int $quantity
* @param float $price
* @param float $vat
* @param array $params
* @return bool
*/
public function addItem($id, int $quantity, $price = 0, $vat = 0, array $params = []): bool {
if($quantity < 1) return false;
$item = new CartItem($id, $price, $vat, $params);
$key = $this->inCart($item);
if($key > -1) {
$this->items[$key]['q'] = $quantity + $this->items[$key]['q'];
}
else {
$this->items[] = [
'q' => $quantity,
'item' => $item
];
}
$this->nb_items += $quantity;
$this->saveCart();
return $key > -1;
}
/**
* @param int|string $id
* @param int|null $quantity
* @param float|null $price
* @param float|null $vat
* @param array $params
* @return array|null
*/
public function updItem($id, int $quantity = null, float $price = null, float $vat = null, array $params = []) {
$item = new CartItem($id, $price, $vat, $params);
$key = $this->inCart($item);
if($key === -1) return null;
if($price !== null) $this->items[$key]['item']->unit_price = $price;
if($vat !== null) $this->items[$key]['item']->vat = $vat;
if(!empty($params)) $this->items[$key]['item']->params = $params;
if($quantity !== null) {
if($quantity <= $this->items[$key]['q']) {
$this->removeItem($key,$this->items[$key]['q'] - $quantity);
}
else {
$this->nb_items -= ($this->items[$key]['q'] - $quantity);
$this->items[$key]['q'] = $quantity;
}
}
$this->saveCart();
return $this->items[$key] ?? null;
}
/**
* @param int $index
* @param int|string $quantity
* @return void|array
*/
public function removeItem(int $index, $quantity) {
if(is_int($quantity) && $quantity < 0) return;
if(is_string($quantity) && $quantity !== 'all') return;
$this->nb_items -= $quantity;
if($quantity === $this->items[$index]['q'] || $quantity === 'all') unset($this->items[$index]);
else $this->items[$index]['q'] = ($this->items[$index]['q'] - $quantity);
$this->saveCart();
return $this->items[$index] ?? null;
}
/**
* @param int|string $id
* @return boolean
*/
public function inCartFee($id): bool {
return key_exists($id, $this->fees);
}
/**
* @param int|string $fee
* @param float|null $price
* @param float|null $vat
*/
public function addFee($fee, float $price = null, float $vat = null) {
if($this->inCartFee($fee)) $this->updFee($fee, $price, $vat);
$this->fees[$fee] = [
'price' => (float)$price,
'vat' => (float)$vat
];
$this->saveCart();
}
/**
* @param int|string $fee
* @param float|null $price
* @param float|null $vat
*/
public function updFee($fee, float $price = null, float $vat = null) {
if(!$this->inCartFee($fee)) $this->addFee($fee, $price, $vat);
$this->fees[$fee]['price'] = $price === null ? $this->fees[$fee]['price'] : $price;
$this->fees[$fee]['vat'] = $vat === null ? $this->fees[$fee]['vat'] : $vat;
$this->saveCart();
}
/**
* Calculate the product total price, tax excluded, tax included and tax amount
* @return array
*/
public function getTotalProduct(): array {
if(!empty($this->items)) {
foreach ($this->items as $item) {
$rate = 1 + (floatval($item['item']->vat) / 100);
$exc = intval($item['q']) * floatval($item['item']->unit_price);
$inc = $exc * $rate;
$vat = $inc - $exc;
$this->total['exc'] += $exc;
$this->total['inc'] += $inc;
if(isset($this->total['vat'][$item['item']->vat]))
$this->total['vat'][$item['item']->vat] += $vat;
else
$this->total['vat'][$item['item']->vat] = $vat;
if(!empty($item['item']->params)) {
foreach ($item['item']->params as $param) {
if(isset($param['price']) && !empty($param['price'])) {
$rate = 1 + (floatval($param['price']['vat']) / 100);
$exc = $param['price']['price'];
$inc = $exc * $rate;
$vat = $inc - $exc;
$this->total['exc'] += $exc;
$this->total['inc'] += $inc;
if(isset($this->total['vat'][$param['price']['vat']]))
$this->total['vat'][$param['price']['vat']] += $vat;
else
$this->total['vat'][$param['price']['vat']] = $vat;
}
}
}
}
}
return $this->total;
}
/**
* Calculate the total price, tax excluded, tax included and tax amount
* @return array
*/
public function getTotal(): array {
$this->getTotalProduct();
if(!empty($this->fees)) {
foreach ($this->fees as $fee) {
$rate = 1 + (floatval($fee['vat']) / 100);
$exc = floatval($fee['price']);
$inc = $exc * $rate;
$vat = $inc - $exc;
$this->total['exc'] += $exc;
$this->total['inc'] += $inc;
if(isset($this->total['vat'][$fee['vat']]))
$this->total['vat'][$fee['vat']] += $vat;
else
$this->total['vat'][$fee['vat']] = $vat;
}
}
return $this->total;
}
/**
* Return all cart data
* @return array
*/
public function getCartData(): array {
return [
'items' => $this->items,
'nb_items' => $this->nb_items,
'fees' => $this->fees,
'total' => $this->getTotal()
];
}
/**
* Create a new Cart, if there was an opened Cart, destroy it then create a new one
* If a key is given, the new Cart will use it as id
* @param string $key (optional) id of the cart
* @param string $session_name (optional) name of the session
*/
public function newCart(?string $key = '', string $session_name = 'mc_cart') {
$params = [];
if($key !== '') $params['ssid'] = $key;
if($this->key !== null) {
$this->session->close($this->cart_name);
session_write_close();
}
else {
session_write_close();
}
$this->session->start($session_name,$params);
if($key === null) session_regenerate_id();
$this->cart_name = $session_name;
//$this->key = $key === '' ? (http_request::isSession('session_key_cart') ? form_inputEscape::simpleClean($_SESSION['session_key_cart']) : session_id()) : $key;
$this->key = empty($key) ? session_id() : $key;
$this->session->run(['session_key_cart' => $this->key]);
}
/**
* Open the active cart
*/
public function openCart() {
$this->session->start($this->cart_name);
if(http_request::isSession('cart')) {
$this->items = $_SESSION['cart']['items'];
$this->nb_items = $_SESSION['cart']['nb_items'];
$this->fees = $_SESSION['cart']['fees'];
}
}
/**
* Save cart in session var
*/
private function saveCart() {
$current_sess = session_name();
if($current_sess !== $this->cart_name) $this->session->start($this->cart_name);
$this->session->run(['cart' => [
'items' => $this->items,
'nb_items' => $this->nb_items,
'fees' => $this->fees
]]);
if($current_sess !== $this->cart_name) $this->session->start($current_sess);
}
/**
* Empty the current cart
*/
public function emptyCart() {
$this->items = [];
$this->nb_items = 0;
$this->fees = [];
$this->saveCart();
}
/**
* @return object Cart
*/
public static function getInstance()
{
if(!self::$instance instanceof self) self::$instance = new self();
else self::$instance->openCart();
return self::$instance;
}
}
Class CartItem {
public
$id,
$unit_price,
$vat,
$params;
/**
* CartItem constructor.
* @param int $id Id
* @param float $u Unit Price
* @param float $v Rate VAT
* @param array $p Additionnal Parameters
*/
public function __construct(int $id, $u = 0, $v = 0, array $p = []) {
$this->id = $id;
$this->unit_price = $u;
$this->vat = $v;
$this->params = $p;
}
}