/
Default.php
776 lines (623 loc) · 19.6 KB
/
Default.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2011
* @license LGPLv3, http://www.arcavias.com/en/license
* @package MShop
* @subpackage Order
*/
/**
* Default implementation of the shopping basket.
*
* @package MShop
* @subpackage Order
*/
class MShop_Order_Item_Base_Default extends MShop_Order_Item_Base_Abstract
{
private $_price;
private $_locale;
private $_values;
private $_products;
private $_addresses;
private $_services;
private $_coupons;
private $_modified = false;
/**
* Initializes the shopping cart.
*
* @param MShop_Price_Item_Interface $price Default price of the basket (usually 0.00)
* @param MShop_Locale_Item_Interface $locale Locale item containing the site, language and currency
* @param array $values Associative list of key/value pairs containing
* e.g. the order or user ID
*/
public function __construct( MShop_Price_Item_Interface $price, MShop_Locale_Item_Interface $locale,
array $values = array(), array $products = array(), array $addresses = array(),
array $services = array(), array $coupons = array() )
{
MW_Common_Abstract::checkClassList( 'MShop_Order_Item_Base_Product_Interface', $products );
MW_Common_Abstract::checkClassList( 'MShop_Order_Item_Base_Address_Interface', $addresses );
MW_Common_Abstract::checkClassList( 'MShop_Order_Item_Base_Service_Interface', $services );
foreach( $coupons as $couponProducts ) {
MW_Common_Abstract::checkClassList( 'MShop_Order_Item_Base_Product_Interface', $couponProducts );
}
$this->_price = $price;
$this->_locale = $locale;
$this->_values = $values;
$this->_products = $products;
$this->_addresses = $addresses;
$this->_services = $services;
$this->_coupons = $coupons;
$this->_modified = false;
}
/**
* Clones internal objects of the order base item.
*/
public function __clone()
{
$this->_price = clone $this->_price;
$this->_locale = clone $this->_locale;
foreach( $this->_products as $key => $value ) {
$this->_products[$key] = $value;
}
foreach( $this->_addresses as $key => $value ) {
$this->_addresses[$key] = $value;
}
foreach( $this->_services as $key => $value ) {
$this->_services[$key] = $value;
}
foreach( $this->_coupons as $key => $value ) {
$this->_coupons[$key] = $value;
}
}
/**
* Returns the ID of the order if already available.
*
* @return string|null ID of the order item
*/
public function getId()
{
return ( isset( $this->_values['id'] ) ? (string) $this->_values['id'] : null );
}
/**
* Sets the id of the order base object.
*
* @param string $id Unique ID of the order base object
*/
public function setId( $id )
{
if ( ( $this->_values['id'] = MShop_Common_Item_Abstract::checkId($this->getId(), $id) ) === null ) {
$this->_modified = true;
} else {
$this->_modified = false;
}
}
/**
* Returns the ID of the site the item is stored.
*
* @return integer|null Site ID (or null if not available)
*/
public function getSiteId()
{
return ( isset( $this->_values['siteid'] ) ? (int) $this->_values['siteid'] : null );
}
/**
* Returns the code of the site the item is stored.
*
* @return string Site code (or empty string if not available)
*/
public function getSiteCode()
{
return ( isset( $this->_values['sitecode'] ) ? (string) $this->_values['sitecode'] : '' );
}
/**
* Returns the comment field of the order item.
*
* @return string Comment for the order
*/
public function getComment()
{
return ( isset( $this->_values['comment'] ) ? (string) $this->_values['comment'] : '' );
}
/**
* Sets the comment field of the order item
*
* @param string $comment Comment for the order
*/
public function setComment( $comment )
{
if ( $comment == $this->getComment() ) { return; }
$this->_values['comment'] = (string) $comment;
$this->_modified = true;
}
/**
* Returns the customer ID of the customer who has ordered.
*
* @return string Unique ID of the customer
*/
public function getCustomerId()
{
return ( isset( $this->_values['customerid'] ) ? (string) $this->_values['customerid'] : '' );
}
/**
* Sets the customer ID of the customer who has ordered.
*
* @param string $customerid Unique ID of the customer
*/
public function setCustomerId( $customerid )
{
if ( $customerid === $this->getCustomerId() ) { return; }
$this->_notifyListeners( 'setCustomerId.before', $customerid );
$this->_values['customerid'] = (string) $customerid;
$this->_modified = true;
$this->_notifyListeners( 'setCustomerId.after', $customerid );
}
/**
* Returns the locales for the basic order item.
*
* @return MShop_Locale_Item_Interface Object containing information
* about site, language, country and currency
*/
public function getLocale()
{
return $this->_locale;
}
/**
* Sets the locales for the basic order item.
*
* @param MShop_Locale_Item_Interface $locale Object containing information
* about site, language, country and currency
*/
public function setLocale( MShop_Locale_Item_Interface $locale )
{
$this->_notifyListeners( 'setLocale.before', $locale );
$this->_locale = clone $locale;
$this->_modified = true;
$this->_notifyListeners( 'setLocale.after', $locale );
}
/**
* Returns the product items that are or should be part of an (future) order.
*
* @return array Array of order product items implementing MShop_Order_Item_Base_Product_Interface
*/
public function getProducts()
{
return $this->_products;
}
/**
* Returns the product item of an (future) order specified by its key.
*
* @param integer $key Key returned by getProducts() identifying the requested product
* @return MShop_Order_Item_Base_Product_Interface Product item of an order
*/
public function getProduct( $key )
{
if( !isset( $this->_products[$key] ) ) {
throw new MShop_Order_Exception( sprintf( 'Product with array key "%1$d" not available', $key ) );
}
return $this->_products[$key];
}
/**
* Adds an order product item to the (future) order.
* If a similar item is found, only the quantity is increased.
*
* @param MShop_Order_Item_Base_Product_Interface $item Order product item to be added
* @param integer|null $position position of the new order product item
* @return integer Position the product item was inserted at
*/
public function addProduct( MShop_Order_Item_Base_Product_Interface $item, $position = null )
{
$this->_checkProduct( $item );
$this->_checkPrice( $item->getPrice() );
$this->_notifyListeners( 'addProduct.before', $item );
if( ( $pos = $this->_getSameProduct( $item ) ) !== false )
{
$quantity = $item->getQuantity();
$item = $this->_products[$pos];
$item->setQuantity( $item->getQuantity() + $quantity );
}
else if( $position !== null )
{
if( isset( $this->_products[$position] ) )
{
$products = array();
foreach( $this->_products as $key => $product )
{
if( $key < $position ) {
$products[$key] = $product;
} else if( $key >= $position ) {
$products[$key+1] = $product;
}
}
$products[$position] = $item;
$this->_products = $products;
}
else
{
$this->_products[$position] = $item;
}
$pos = $position;
}
else
{
$this->_products[] = $item;
end( $this->_products );
$pos = key( $this->_products );
}
ksort( $this->_products );
$this->_modified = true;
$this->_notifyListeners( 'addProduct.after', $item );
return $pos;
}
/**
* Deletes an order product item from the (future) order.
*
* @param integer $position Position id of the order product item
*/
public function deleteProduct( $position )
{
if( !array_key_exists( $position, $this->_products ) ) {
throw new MShop_Order_Exception( sprintf( 'Product with array key "%1$d" not available', $position ) );
}
$this->_notifyListeners( 'deleteProduct.before', $position );
unset($this->_products[$position]);
$this->_modified = true;
$this->_notifyListeners( 'deleteProduct.after', $position );
}
/**
* Returns all addresses that are part of the basket.
*
* @return array Associative list of address items implementing
* MShop_Order_Item_Base_Address_Interface with "billing" or "delivery" as key
*/
public function getAddresses()
{
return $this->_addresses;
}
/**
* Returns the billing or delivery address depending on the given domain.
*
* @param string $domain Address domain, usually "billing" or "delivery"
* @return MShop_Order_Item_Base_Address_Interface Order address item for the requested domain
*/
public function getAddress( $domain = MShop_Order_Item_Base_Address_Abstract::TYPE_PAYMENT )
{
if(!isset($this->_addresses[$domain])) {
throw new MShop_Order_Exception( sprintf( 'Address for domain "%1$s" not available', $domain ) );
}
return $this->_addresses[$domain];
}
/**
* Sets a customer address as billing or delivery address for an order.
*
* @param MShop_Order_Item_Base_Address_Interface $address Order address item for the given domain
* @param string $domain Address domain, usually "billing" or "delivery"
* @return MShop_Order_Item_Base_Address_Interface Item that was really added to the basket
*/
public function setAddress( MShop_Order_Item_Base_Address_Interface $address,
$domain = MShop_Order_Item_Base_Address_Abstract::TYPE_PAYMENT )
{
if ( isset( $this->_addresses[ $domain ] ) && $this->_addresses[ $domain ] === $address ) { return; }
$this->_notifyListeners( 'setAddress.before', $address );
// enforce that the type is the same as the given one
$address->setType( $domain );
$this->_addresses[$domain] = clone $address;
$this->_modified = true;
$this->_notifyListeners( 'setAddress.after', $address );
return $this->_addresses[$domain];
}
/**
* Deleted a customer address for billing or delivery of an order.
*
* @param string $domain Address domain defined in MShop_Order_Item_Base_Address_Abstract
*/
public function deleteAddress( $domain = MShop_Order_Item_Base_Address_Abstract::TYPE_DELIVERY )
{
$this->_notifyListeners( 'deleteAddress.before', $domain );
if( isset( $this->_addresses[$domain] ) ) {
unset( $this->_addresses[$domain] );
$this->_modified = true;
}
$this->_notifyListeners( 'deleteAddress.after', $domain );
}
/**
* Returns all services that are part of the basket.
*
* @return array Associative list of service items implementing MShop_Order_Service_Interface
* with "delivery" or "payment" as key
*/
public function getServices()
{
return $this->_services;
}
/**
* Returns the delivery or payment service depending on the given type.
*
* @param string $type Service type code like 'payment', 'delivery', etc.
* @return MShop_Order_Item_Base_Serive_Interface Order service item for the requested type
*/
public function getService( $type )
{
if(!isset($this->_services[$type])) {
throw new MShop_Order_Exception( sprintf( 'Service of type "%1$s" not available', $type ) );
}
return $this->_services[$type];
}
/**
* Sets a service as delivery or payment service for an order.
*
* @param MShop_Order_Item_Base_Service_Interface $service Order service item for the given domain
* @param string $type Service type
* @return MShop_Order_Item_Base_Service_Interface Item that was really added to the basket
*/
public function setService( MShop_Order_Item_Base_Service_Interface $service, $type )
{
$this->_checkPrice( $service->getPrice() );
$this->_notifyListeners( 'setService.before', $service );
// enforce that the type is the same as the given one
$service->setType( $type );
$this->_services[$type] = clone $service;
$this->_modified = true;
$this->_notifyListeners( 'setService.after', $service );
return $this->_services[$type];
}
/**
* Deletes the delivery or payment service from the basket.
*/
public function deleteService( $type )
{
$this->_notifyListeners( 'deleteService.before', $type );
if( isset( $this->_services[$type] ) ) {
unset( $this->_services[$type] );
$this->_modified = true;
}
$this->_notifyListeners( 'deleteService.after', $type );
}
/**
* Returns the available coupon codes and the lists of affected product items.
*
* @return array Associative array of codes and lists of product items
* implementing MShop_Order_Product_Interface
*/
public function getCoupons()
{
return $this->_coupons;
}
/**
* Adds a coupon code entered by the customer and the given product item to the basket.
*
* @param string $code Coupon code
* @param MShop_Order_Item_Base_Product_Interface[] $products List of coupon products
*/
public function addCoupon( $code, array $products = array() )
{
if( isset( $this->_coupons[$code] ) ) {
throw new MShop_Order_Exception( sprintf( 'Duplicate coupon code "%1$s"', $code ) );
}
foreach( $products as $product )
{
$this->_checkProduct( $product );
$this->_checkPrice( $product->getPrice() );
}
$this->_notifyListeners( 'addCoupon.before', $products );
$this->_coupons[$code] = $products;
foreach( $products as $product ) {
$this->_products[] = $product;
}
$this->_modified = true;
$this->_notifyListeners( 'addCoupon.after', $code );
}
/**
* Removes a coupon and the related product items from the basket.
*
* @param string $code Coupon code
* @param boolean $removecode If the coupon code should also be removed
* @return array List of affected product items implementing MShop_Order_Item_Base_Product_Interface
* or an empty list if no products are affected by a coupon
*/
public function deleteCoupon( $code, $removecode = false )
{
$products = array();
if( isset( $this->_coupons[$code] ) )
{
$this->_notifyListeners( 'deleteCoupon.before', $code );
$products = $this->_coupons[$code];
foreach( $products as $product )
{
if( ( $key = array_search( $product, $this->_products, true ) ) !== false ) {
unset( $this->_products[$key] );
}
}
if( $removecode === true ) {
unset( $this->_coupons[$code] );
} else {
$this->_coupons[$code] = array();
}
$this->_modified = true;
$this->_notifyListeners( 'deleteCoupon.after' );
}
return $products;
}
/**
* Tests if all necessary items are available to create the order.
*
* @param integer $what Test for the specifice type of completeness
* @throws MShop_Order_Exception if there are no products in the basket
*/
public function check( $what = MShop_Order_Item_Base_Abstract::PARTS_ALL )
{
$this->_checkParts( $what );
$this->_notifyListeners( 'check.before', $what );
if( ( $what & MShop_Order_Item_Base_Abstract::PARTS_PRODUCT ) && ( count($this->_products) < 1 ) ) {
throw new MShop_Order_Exception( sprintf( 'Basket empty' ) );
}
$this->_notifyListeners( 'check.after', $what );
}
/**
* Tests if the order object was modified.
*
* @return bool True if modified, false if not
*/
public function isModified()
{
return $this->_modified;
}
/**
* Returns a price item with amounts calculated for the products, costs, etc.
*
* @return MShop_Price_Item_Interface Price item with price, costs and rebate the customer has to pay
*/
public function getPrice()
{
if( $this->_modified !== false )
{
$this->_price->setValue( '0.00' );
$this->_price->setCosts( '0.00' );
$this->_price->setRebate( '0.00' );
$this->_price->setTaxRate( '0.00' );
foreach( $this->getServices() as $service ) {
$this->_price->addItem( $service->getPrice() );
}
foreach( $this->getProducts() as $product ) {
$this->_price->addItem( $product->getPrice(), $product->getQuantity() );
}
}
return $this->_price;
}
/**
* Returns the current status of the order base item.
*
* @return integer Status of the item
*/
public function getStatus()
{
return ( isset( $this->_values['status'] ) ? (int) $this->_values['status'] : 0 );
}
/**
* Sets the new status of the order base item.
*
* @param integer $value Status of the item
*/
public function setStatus( $value )
{
$this->_values['status'] = (int) $value;
$this->_modified = true;
}
/**
* Returns modification time of the order item base product.
*
* @return string Returns modification time of the order base item
*/
public function getTimeModified()
{
return ( isset( $this->_values['mtime'] ) ? (string) $this->_values['mtime'] : null );
}
/**
* Returns the create date of the item.
*
* @return string ISO date in YYYY-MM-DD hh:mm:ss format
*/
public function getTimeCreated()
{
return ( isset( $this->_values['ctime'] ) ? (string) $this->_values['ctime'] : null );
}
/**
* Returns the editor code of editor who created/modified the item at last.
*
* @return string Editorcode of editor who created/modified the item at last
*/
public function getEditor()
{
return ( isset( $this->_values['editor'] ) ? (string) $this->_values['editor'] : '' );
}
/**
* Returns the item values as array.
*
* @return Associative list of item properties and their values
*/
public function toArray()
{
$price = $this->_price;
$locale = $this->_locale;
return array(
'order.base.id' => $this->getId(),
'order.base.siteid' => $this->getSiteId(),
'order.base.sitecode' => $this->getSiteCode(),
'order.base.languageid' => $locale->getLanguageId(),
'order.base.comment' => $this->getComment(),
'order.base.customerid' => $this->getCustomerId(),
'order.base.price' => $price->getValue(),
'order.base.costs' => $price->getCosts(),
'order.base.rebate' => $price->getRebate(),
'order.base.currencyid' => $price->getCurrencyId(),
'order.base.status' => $this->getStatus(),
'order.base.mtime' => $this->getTimeModified(),
'order.base.ctime' => $this->getTimeCreated(),
'order.base.editor' => $this->getEditor(),
);
}
/**
* Notifies listeners before the basket becomes an order.
*/
public function finish()
{
$this->_notifyListeners( 'setOrder.before' );
}
/**
* Prepares the object for serialization.
*
* @return array List of properties that should be serialized
*/
public function __sleep()
{
/*
* Workaround because database connections can't be serialized
* Listeners will be reattached on wakeup by the customer manager
*/
$this->_clearListeners();
return array_keys( get_object_vars( $this ) );
}
/**
* Checks if the price uses the same currency as the price in the basket.
*
* @param MShop_Price_Item_Interface $item Price item
*/
protected function _checkPrice( MShop_Price_Item_Interface $item )
{
$price = clone $this->_price;
$price->addItem( $item );
}
/**
* Tests if the given product is similar to an existing one.
* Similarity is described by the equality of properties so the quantity of
* the existing product can be updated.
*
* @param MShop_Order_Item_Base_Product_Interface $item Order product item
* @return integer Positon of the same product in the product list
* @throws MShop_Order_Exception If no similar item was found
*/
protected function _getSameProduct( MShop_Order_Item_Base_Product_Interface $item )
{
$attributeMap = array();
foreach( $item->getAttributes() as $attributeItem ) {
$attributeMap[ $attributeItem->getCode() ] = $attributeItem;
}
foreach( $this->_products as $position => $product )
{
if( $product->compare( $item ) === false ) {
continue;
}
$prodAttributes = $product->getAttributes();
if( count( $prodAttributes ) !== count( $attributeMap ) ) {
continue;
}
foreach( $prodAttributes as $attribute )
{
if( array_key_exists( $attribute->getCode(), $attributeMap ) === false
|| $attributeMap[ $attribute->getCode() ]->getValue() != $attribute->getValue() ) {
continue 2; // jump to outer loop
}
}
return $position;
}
return false;
}
}