|
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 |
|
|
|
|
|
|
58
|
|
|
* @param string $session_id A session id |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.