Passed
Push — master ( 5c5569...ba6e3e )
by Aimeos
04:18
created

Base::getStatusItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item;
12
13
14
/**
15
 * Base order item class with common constants and methods.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
abstract class Base
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Order\Item\Iface, \Aimeos\Macro\Iface, \ArrayAccess, \JsonSerializable
23
{
24
	use \Aimeos\Macro\Macroable;
25
	use Publisher;
26
27
28
	/**
29
	 * Unfinished delivery.
30
	 * This is the default status after creating an order and this status
31
	 * should be also used as long as technical errors occurs.
32
	 */
33
	const STAT_UNFINISHED = -1;
34
35
	/**
36
	 * Delivery was deleted.
37
	 * The delivery of the order was deleted manually.
38
	 */
39
	const STAT_DELETED = 0;
40
41
	/**
42
	 * Delivery is pending.
43
	 * The order is not yet in the fulfillment process until further actions
44
	 * are taken.
45
	 */
46
	const STAT_PENDING = 1;
47
48
	/**
49
	 * Fulfillment in progress.
50
	 * The delivery of the order is in the (internal) fulfillment process and
51
	 * will be ready soon.
52
	 */
53
	const STAT_PROGRESS = 2;
54
55
	/**
56
	 * Parcel is dispatched.
57
	 * The parcel was given to the logistic partner for delivery to the
58
	 * customer.
59
	 */
60
	const STAT_DISPATCHED = 3;
61
62
	/**
63
	 * Parcel was delivered.
64
	 * The logistic partner delivered the parcel and the customer received it.
65
	 */
66
	const STAT_DELIVERED = 4;
67
68
	/**
69
	 * Parcel is lost.
70
	 * The parcel is lost during delivery by the logistic partner and haven't
71
	 * reached the customer nor it's returned to the merchant.
72
	 */
73
	const STAT_LOST = 5;
74
75
	/**
76
	 * Parcel was refused.
77
	 * The delivery of the parcel failed because the customer has refused to
78
	 * accept it or the address was invalid.
79
	 */
80
	const STAT_REFUSED = 6;
81
82
	/**
83
	 * Parcel was returned.
84
	 * The parcel was sent back by the customer.
85
	 */
86
	const STAT_RETURNED = 7;
87
88
89
	/**
90
	 * Unfinished payment.
91
	 * This is the default status after creating an order and this status
92
	 * should be also used as long as technical errors occurs.
93
	 */
94
	const PAY_UNFINISHED = -1;
95
96
	/**
97
	 * Payment was deleted.
98
	 * The payment for the order was deleted manually.
99
	 */
100
	const PAY_DELETED = 0;
101
102
	/**
103
	 * Payment was canceled.
104
	 * The customer canceled the payment process.
105
	 */
106
	const PAY_CANCELED = 1;
107
108
	/**
109
	 * Payment was refused.
110
	 * The customer didn't enter valid payment details.
111
	 */
112
	const PAY_REFUSED = 2;
113
114
	/**
115
	 * Payment was refund.
116
	 * The payment was OK but refund and the customer got his money back.
117
	 */
118
	const PAY_REFUND = 3;
119
120
	/**
121
	 * Payment is pending.
122
	 * The payment is not yet done until further actions are taken.
123
	 */
124
	const PAY_PENDING = 4;
125
126
	/**
127
	 * Payment is authorized.
128
	 * The customer authorized the merchant to invoice the amount but the money
129
	 * is not yet received. This is used for all post-paid orders.
130
	 */
131
	const PAY_AUTHORIZED = 5;
132
133
	/**
134
	 * Payment is received.
135
	 * The merchant received the money from the customer.
136
	 */
137
	const PAY_RECEIVED = 6;
138
139
	/**
140
	 * Payment is transferred.
141
	 * The vendor received the money from the platform.
142
	 */
143
	const PAY_TRANSFERRED = 7;
144
145
146
	// protected is a workaround for serialize problem
147
	protected ?\Aimeos\MShop\Customer\Item\Iface $customer;
148
	protected \Aimeos\MShop\Locale\Item\Iface $locale;
149
	protected \Aimeos\MShop\Price\Item\Iface $price;
150
	protected bool $recalc = false;
151
	protected array $coupons = [];
152
	protected array $products = [];
153
	protected array $services = [];
154
	protected array $statuses = [];
155
	protected array $addresses = [];
156
157
158
	/**
159
	 * Initializes the order object
160
	 *
161
	 * @param string $prefix Prefix for the keys in the associative array
162
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
163
	 */
164
	public function __construct( string $prefix, array $values = [] )
165
	{
166
		parent::__construct( $prefix, $values );
167
168
		$this->customer = $values['.customer'] ?? null;
169
		$this->locale = $values['.locale'];
170
		$this->price = $values['.price'];
171
172
		$products = $this->get( '.products', [] );
173
174
		foreach( $this->get( '.coupons', [] ) as $coupon )
175
		{
176
			if( !isset( $this->coupons[$coupon->getCode()] ) ) {
177
				$this->coupons[$coupon->getCode()] = [];
178
			}
179
180
			if( isset( $products[$coupon->getProductId()] ) ) {
181
				$this->coupons[$coupon->getCode()][] = $products[$coupon->getProductId()];
182
			}
183
		}
184
185
		foreach( $this->get( '.products', [] ) as $product ) {
186
			$this->products[$product->getPosition()] = $product;
187
		}
188
189
		foreach( $this->get( '.addresses', [] ) as $address ) {
190
			$this->addresses[$address->getType()][] = $address;
191
		}
192
193
		foreach( $this->get( '.services', [] ) as $service ) {
194
			$this->services[$service->getType()][] = $service;
195
		}
196
197
		foreach( $this->get( '.statuses', [] ) as $status ) {
198
			$this->statuses[$status->getType()][$status->getValue()] = $status;
199
		}
200
	}
201
202
203
	/**
204
	 * Clones internal objects of the order item.
205
	 */
206
	public function __clone()
207
	{
208
		parent::__clone();
209
210
		$this->price = clone $this->price;
211
		$this->locale = clone $this->locale;
212
	}
213
214
215
	/**
216
	 * Specifies the data which should be serialized to JSON by json_encode().
217
	 *
218
	 * @return array<string,mixed> Data to serialize to JSON
219
	 */
220
	#[\ReturnTypeWillChange]
221
	public function jsonSerialize()
222
	{
223
		return parent::jsonSerialize() + [
224
			'addresses' => $this->addresses,
225
			'products' => $this->products,
226
			'services' => $this->services,
227
			'coupons' => $this->coupons,
228
			'customer' => $this->customer,
229
			'locale' => $this->locale,
230
			'price' => $this->price,
231
		];
232
	}
233
234
235
	/**
236
	 * Prepares the object for serialization.
237
	 *
238
	 * @return array List of properties that should be serialized
239
	 */
240
	public function __sleep() : array
241
	{
242
		/*
243
		 * Workaround because database connections can't be serialized
244
		 * Listeners will be reattached on wakeup by the order base manager
245
		 */
246
		$this->off();
247
248
		return array_keys( get_object_vars( $this ) );
249
	}
250
251
252
	/**
253
	 * Returns the ID of the items
254
	 *
255
	 * @return string ID of the item or null
256
	 */
257
	public function __toString() : string
258
	{
259
		return (string) $this->getId();
260
	}
261
262
263
	/**
264
	 * Tests if all necessary items are available to create the order.
265
	 *
266
	 * @param array $what Type of data
267
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
268
	 * @throws \Aimeos\MShop\Order\Exception if there are no products in the basket
269
	 */
270
	public function check( array $what = ['order/address', 'order/coupon', 'order/product', 'order/service'] ) : \Aimeos\MShop\Order\Item\Iface
271
	{
272
		$this->notify( 'check.before', $what );
273
274
		if( in_array( 'order/product', $what ) && ( count( $this->getProducts() ) < 1 ) ) {
275
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Basket empty' ) );
276
		}
277
278
		$this->notify( 'check.after', $what );
279
280
		return $this;
281
	}
282
283
284
	/**
285
	 * Notifies listeners before the basket becomes an order.
286
	 *
287
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
288
	 */
289
	public function finish() : \Aimeos\MShop\Order\Item\Iface
290
	{
291
		$this->notify( 'setOrder.before' );
292
		return $this;
293
	}
294
295
296
	/**
297
	 * Adds the address of the given type to the basket
298
	 *
299
	 * @param \Aimeos\MShop\Order\Item\Address\Iface $address Order address item for the given type
300
	 * @param string $type Address type, usually "billing" or "delivery"
301
	 * @param int|null $position Position of the address in the list
302
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
303
	 */
304
	public function addAddress( \Aimeos\MShop\Order\Item\Address\Iface $address, string $type, int $position = null ) : \Aimeos\MShop\Order\Item\Iface
305
	{
306
		$address = $this->notify( 'addAddress.before', $address );
307
308
		$address = clone $address;
309
		$address = $address->setType( $type );
310
311
		if( $position !== null ) {
312
			$this->addresses[$type][$position] = $address;
313
		} else {
314
			$this->addresses[$type][] = $address;
315
		}
316
317
		$this->setModified();
318
319
		$this->notify( 'addAddress.after', $address );
320
321
		return $this;
322
	}
323
324
325
	/**
326
	 * Deletes an order address from the basket
327
	 *
328
	 * @param string $type Address type defined in \Aimeos\MShop\Order\Item\Address\Base
329
	 * @param int|null $position Position of the address in the list
330
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
331
	 */
332
	public function deleteAddress( string $type, int $position = null ) : \Aimeos\MShop\Order\Item\Iface
333
	{
334
		if( $position === null && isset( $this->addresses[$type] ) || isset( $this->addresses[$type][$position] ) )
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($position === null && IssetNode) || IssetNode, Probably Intended Meaning: $position === null && (IssetNode || IssetNode)
Loading history...
335
		{
336
			$old = ( isset( $this->addresses[$type][$position] ) ? $this->addresses[$type][$position] : $this->addresses[$type] );
337
			$old = $this->notify( 'deleteAddress.before', $old );
338
339
			if( $position !== null ) {
340
				unset( $this->addresses[$type][$position] );
341
			} else {
342
				unset( $this->addresses[$type] );
343
			}
344
345
			$this->setModified();
346
347
			$this->notify( 'deleteAddress.after', $old );
348
		}
349
350
		return $this;
351
	}
352
353
354
	/**
355
	 * Returns the order address depending on the given type
356
	 *
357
	 * @param string $type Address type, usually "billing" or "delivery"
358
	 * @param int|null $position Address position in list of addresses
359
	 * @return \Aimeos\MShop\Order\Item\Address\Iface[]|\Aimeos\MShop\Order\Item\Address\Iface Order address item or list of
360
	 */
361
	public function getAddress( string $type, int $position = null )
362
	{
363
		if( $position !== null )
364
		{
365
			if( isset( $this->addresses[$type][$position] ) ) {
366
				return $this->addresses[$type][$position];
367
			}
368
369
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Address not available' ) );
370
		}
371
372
		return ( isset( $this->addresses[$type] ) ? $this->addresses[$type] : [] );
373
	}
374
375
376
	/**
377
	 * Returns all addresses that are part of the basket
378
	 *
379
	 * @return \Aimeos\Map Associative list of address items implementing
380
	 *  \Aimeos\MShop\Order\Item\Address\Iface with "billing" or "delivery" as key
381
	 */
382
	public function getAddresses() : \Aimeos\Map
383
	{
384
		return map( $this->addresses );
385
	}
386
387
388
	/**
389
	 * Replaces all addresses in the current basket with the new ones
390
	 *
391
	 * @param \Aimeos\Map|array $map Associative list of order addresses as returned by getAddresses()
392
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
393
	 */
394
	public function setAddresses( iterable $map ) : \Aimeos\MShop\Order\Item\Iface
395
	{
396
		$map = $this->notify( 'setAddresses.before', $map );
397
398
		foreach( $map as $type => $items ) {
399
			$this->checkAddresses( $items, $type );
400
		}
401
402
		$old = $this->addresses;
403
		$this->addresses = is_map( $map ) ? $map->toArray() : $map;
404
		$this->setModified();
405
406
		$this->notify( 'setAddresses.after', $old );
407
408
		return $this;
409
	}
410
411
412
	/**
413
	 * Adds a coupon code and the given product item to the basket
414
	 *
415
	 * @param string $code Coupon code
416
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
417
	 */
418
	public function addCoupon( string $code ) : \Aimeos\MShop\Order\Item\Iface
419
	{
420
		if( !isset( $this->coupons[$code] ) )
421
		{
422
			$code = $this->notify( 'addCoupon.before', $code );
423
424
			$this->coupons[$code] = [];
425
			$this->setModified();
426
427
			$this->notify( 'addCoupon.after', $code );
428
		}
429
430
		return $this;
431
	}
432
433
434
	/**
435
	 * Removes a coupon and the related product items from the basket
436
	 *
437
	 * @param string $code Coupon code
438
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
439
	 */
440
	public function deleteCoupon( string $code ) : \Aimeos\MShop\Order\Item\Iface
441
	{
442
		if( isset( $this->coupons[$code] ) )
443
		{
444
			$old = [$code => $this->coupons[$code]];
445
			$old = $this->notify( 'deleteCoupon.before', $old );
446
447
			foreach( $this->coupons[$code] as $product )
448
			{
449
				if( ( $key = array_search( $product, $this->products, true ) ) !== false ) {
450
					unset( $this->products[$key] );
451
				}
452
			}
453
454
			unset( $this->coupons[$code] );
455
			$this->setModified();
456
457
			$this->notify( 'deleteCoupon.after', $old );
458
		}
459
460
		return $this;
461
	}
462
463
464
	/**
465
	 * Returns the available coupon codes and the lists of affected product items
466
	 *
467
	 * @return \Aimeos\Map Associative array of codes and lists of product items
468
	 *  implementing \Aimeos\MShop\Order\Product\Iface
469
	 */
470
	public function getCoupons() : \Aimeos\Map
471
	{
472
		return map( $this->coupons );
473
	}
474
475
476
	/**
477
	 * Sets a coupon code and the given product items in the basket.
478
	 *
479
	 * @param string $code Coupon code
480
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of coupon products
481
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
482
	 */
483
	public function setCoupon( string $code, iterable $products = [] ) : \Aimeos\MShop\Order\Item\Iface
484
	{
485
		$new = $this->notify( 'setCoupon.before', [$code => $products] );
486
487
		$products = $this->checkProducts( map( $new )->first( [] ) );
488
489
		if( isset( $this->coupons[$code] ) )
490
		{
491
			foreach( $this->coupons[$code] as $product )
492
			{
493
				if( ( $key = array_search( $product, $this->products, true ) ) !== false ) {
494
					unset( $this->products[$key] );
495
				}
496
			}
497
		}
498
499
		foreach( $products as $product ) {
500
			$this->products[] = $product;
501
		}
502
503
		$old = isset( $this->coupons[$code] ) ? [$code => $this->coupons[$code]] : [];
504
		$this->coupons[$code] = is_map( $products ) ? $products->toArray() : $products;
505
		$this->setModified();
506
507
		$this->notify( 'setCoupon.after', $old );
508
509
		return $this;
510
	}
511
512
513
	/**
514
	 * Replaces all coupons in the current basket with the new ones
515
	 *
516
	 * @param iterable $map Associative list of order coupons as returned by getCoupons()
517
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
518
	 */
519
	public function setCoupons( iterable $map ) : \Aimeos\MShop\Order\Item\Iface
520
	{
521
		$map = $this->notify( 'setCoupons.before', $map );
522
523
		foreach( $map as $code => $products ) {
524
			$map[$code] = $this->checkProducts( $products );
525
		}
526
527
		foreach( $this->coupons as $code => $products )
528
		{
529
			foreach( $products as $product )
530
			{
531
				if( ( $key = array_search( $product, $this->products, true ) ) !== false ) {
532
					unset( $this->products[$key] );
533
				}
534
			}
535
		}
536
537
		foreach( $map as $code => $products )
538
		{
539
			foreach( $products as $product ) {
540
				$this->products[] = $product;
541
			}
542
		}
543
544
		$old = $this->coupons;
545
		$this->coupons = is_map( $map ) ? $map->toArray() : $map;
546
		$this->setModified();
547
548
		$this->notify( 'setCoupons.after', $old );
549
550
		return $this;
551
	}
552
553
554
	/**
555
	 * Adds an order product item to the basket
556
	 * If a similar item is found, only the quantity is increased.
557
	 *
558
	 * @param \Aimeos\MShop\Order\Item\Product\Iface $item Order product item to be added
559
	 * @param int|null $position position of the new order product item
560
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
561
	 */
562
	public function addProduct( \Aimeos\MShop\Order\Item\Product\Iface $item, int $position = null ) : \Aimeos\MShop\Order\Item\Iface
563
	{
564
		$item = $this->notify( 'addProduct.before', $item );
565
566
		$this->checkProducts( [$item] );
567
568
		if( $position !== null ) {
569
			$this->products[$position] = $item;
570
		} elseif( ( $pos = $this->getSameProduct( $item, $this->products ) ) !== null ) {
571
			$item = $this->products[$pos]->setQuantity( $this->products[$pos]->getQuantity() + $item->getQuantity() );
572
		} else {
573
			$this->products[] = $item;
574
		}
575
576
		ksort( $this->products );
577
		$this->setModified();
578
579
		$this->notify( 'addProduct.after', $item );
580
581
		return $this;
582
	}
583
584
585
	/**
586
	 * Deletes an order product item from the basket
587
	 *
588
	 * @param int $position Position of the order product item
589
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
590
	 */
591
	public function deleteProduct( int $position ) : \Aimeos\MShop\Order\Item\Iface
592
	{
593
		if( isset( $this->products[$position] ) )
594
		{
595
			$old = $this->products[$position];
596
			$old = $this->notify( 'deleteProduct.before', $old );
597
598
			unset( $this->products[$position] );
599
			$this->setModified();
600
601
			$this->notify( 'deleteProduct.after', $old );
602
		}
603
604
		return $this;
605
	}
606
607
608
	/**
609
	 * Returns the product item of an basket specified by its key
610
	 *
611
	 * @param int $key Key returned by getProducts() identifying the requested product
612
	 * @return \Aimeos\MShop\Order\Item\Product\Iface Product item of an order
613
	 */
614
	public function getProduct( int $key ) : \Aimeos\MShop\Order\Item\Product\Iface
615
	{
616
		if( !isset( $this->products[$key] ) ) {
617
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Product not available' ) );
618
		}
619
620
		return $this->products[$key];
621
	}
622
623
624
	/**
625
	 * Returns the product items that are or should be part of a basket
626
	 *
627
	 * @return \Aimeos\Map List of order product items implementing \Aimeos\MShop\Order\Item\Product\Iface
628
	 */
629
	public function getProducts() : \Aimeos\Map
630
	{
631
		return map( $this->products );
632
	}
633
634
635
	/**
636
	 * Replaces all products in the current basket with the new ones
637
	 *
638
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $map Associative list of ordered products as returned by getProducts()
639
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
640
	 */
641
	public function setProducts( iterable $map ) : \Aimeos\MShop\Order\Item\Iface
642
	{
643
		$map = $this->notify( 'setProducts.before', $map );
644
645
		$this->checkProducts( $map );
646
647
		$old = $this->products;
648
		$this->products = is_map( $map ) ? $map->toArray() : $map;
649
		$this->setModified();
650
651
		$this->notify( 'setProducts.after', $old );
652
653
		return $this;
654
	}
655
656
657
	/**
658
	 * Adds an order service to the basket
659
	 *
660
	 * @param \Aimeos\MShop\Order\Item\Service\Iface $service Order service item for the given domain
661
	 * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base
662
	 * @param int|null $position Position of the service in the list to overwrite
663
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
664
	 */
665
	public function addService( \Aimeos\MShop\Order\Item\Service\Iface $service, string $type, int $position = null ) : \Aimeos\MShop\Order\Item\Iface
666
	{
667
		$service = $this->notify( 'addService.before', $service );
668
669
		$this->checkPrice( $service->getPrice() );
670
671
		$service = clone $service;
672
		$service = $service->setType( $type );
673
674
		if( $position !== null ) {
675
			$this->services[$type][$position] = $service;
676
		} else {
677
			$this->services[$type][] = $service;
678
		}
679
680
		$this->setModified();
681
682
		$this->notify( 'addService.after', $service );
683
684
		return $this;
685
	}
686
687
688
	/**
689
	 * Deletes an order service from the basket
690
	 *
691
	 * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base
692
	 * @param int|null $position Position of the service in the list to delete
693
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
694
	 */
695
	public function deleteService( string $type, int $position = null ) : \Aimeos\MShop\Order\Item\Iface
696
	{
697
		if( $position === null && isset( $this->services[$type] ) || isset( $this->services[$type][$position] ) )
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($position === null && IssetNode) || IssetNode, Probably Intended Meaning: $position === null && (IssetNode || IssetNode)
Loading history...
698
		{
699
			$old = ( isset( $this->services[$type][$position] ) ? $this->services[$type][$position] : $this->services[$type] );
700
			$old = $this->notify( 'deleteService.before', $old );
701
702
			if( $position !== null ) {
703
				unset( $this->services[$type][$position] );
704
			} else {
705
				unset( $this->services[$type] );
706
			}
707
708
			$this->setModified();
709
710
			$this->notify( 'deleteService.after', $old );
711
		}
712
713
		return $this;
714
	}
715
716
717
	/**
718
	 * Returns the order services depending on the given type
719
	 *
720
	 * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base
721
	 * @param int|null $position Position of the service in the list to retrieve
722
	 * @return \Aimeos\MShop\Order\Item\Service\Iface[]|\Aimeos\MShop\Order\Item\Service\Iface
723
	 * 	Order service item or list of items for the requested type
724
	 * @throws \Aimeos\MShop\Order\Exception If no service for the given type and position is found
725
	 */
726
	public function getService( string $type, int $position = null )
727
	{
728
		if( $position !== null )
729
		{
730
			if( isset( $this->services[$type][$position] ) ) {
731
				return $this->services[$type][$position];
732
			}
733
734
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Service not available' ) );
735
		}
736
737
		return ( isset( $this->services[$type] ) ? $this->services[$type] : [] );
738
	}
739
740
741
	/**
742
	 * Returns all services that are part of the basket
743
	 *
744
	 * @return \Aimeos\Map Associative list of service types ("delivery" or "payment") as keys and list of
745
	 *	service items implementing \Aimeos\MShop\Order\Service\Iface as values
746
	 */
747
	public function getServices() : \Aimeos\Map
748
	{
749
		return map( $this->services );
750
	}
751
752
753
	/**
754
	 * Replaces all services in the current basket with the new ones
755
	 *
756
	 * @param \Aimeos\MShop\Order\Item\Service\Iface[] $map Associative list of order services as returned by getServices()
757
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for method chaining
758
	 */
759
	public function setServices( iterable $map ) : \Aimeos\MShop\Order\Item\Iface
760
	{
761
		$map = $this->notify( 'setServices.before', $map );
762
763
		foreach( $map as $type => $services ) {
764
			$map[$type] = $this->checkServices( $services, $type );
765
		}
766
767
		$old = $this->services;
768
		$this->services = is_map( $map ) ? $map->toArray() : $map;
769
		$this->setModified();
770
771
		$this->notify( 'setServices.after', $old );
772
773
		return $this;
774
	}
775
776
777
	/**
778
	 * Adds a status item to the order
779
	 *
780
	 * @param \Aimeos\MShop\Order\Item\Status\Iface $item Order status item
781
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for method chaining
782
	 */
783
	public function addStatus( \Aimeos\MShop\Order\Item\Status\Iface $item ) : \Aimeos\MShop\Order\Item\Iface
784
	{
785
		$type = $item->getType();
786
		$value = $item->getValue();
787
788
		if( isset( $this->statuses[$type][$value] ) ) {
789
			$this->statuses[$type][$value] = $item->setId( $this->statuses[$type][$value]->getId() );
790
		} else {
791
			$this->statuses[$type][$value] = $item;
792
		}
793
794
		return $this->setModified();
795
	}
796
797
798
	/**
799
	 * Returns the status item specified by its type and value
800
	 *
801
	 * @param string $type Status type
802
	 * @param string $value Status value
803
	 * @return \Aimeos\MShop\Order\Item\Status\Iface Status item of an order
804
	 * @throws \Aimeos\MShop\Order\Exception If status item is not available
805
	 */
806
	public function getStatus( string $type, string $value ) : \Aimeos\MShop\Order\Item\Status\Iface
807
	{
808
		if( !isset( $this->statuses[$type][$value] ) ) {
809
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Status not available' ) );
810
		}
811
812
		return $this->statuses[$type][$value];
813
	}
814
815
816
	/**
817
	 * Returns the status items
818
	 *
819
	 * @return \Aimeos\Map Associative list of status types as keys and list of
820
	 *	status value/item pairs implementing \Aimeos\MShop\Order\Status\Iface as values
821
	 */
822
	public function getStatuses() : \Aimeos\Map
823
	{
824
		return map( $this->statuses );
825
	}
826
827
828
	/**
829
	 * Returns the service costs
830
	 *
831
	 * @param string $type Service type like "delivery" or "payment"
832
	 * @return float Service costs value
833
	 */
834
	public function getCosts( string $type = 'delivery' ) : float
835
	{
836
		$costs = 0;
837
838
		if( $type === 'delivery' )
839
		{
840
			foreach( $this->getProducts() as $product ) {
841
				$costs += $product->getPrice()->getCosts() * $product->getQuantity();
842
			}
843
		}
844
845
		foreach( $this->getService( $type ) as $service ) {
846
			$costs += $service->getPrice()->getCosts();
847
		}
848
849
		return $costs;
850
	}
851
852
853
	/**
854
	 * Returns a price item with amounts calculated for the products, costs, etc.
855
	 *
856
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
857
	 */
858
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
859
	{
860
		if( $this->recalc )
861
		{
862
			$price = $this->price->clear();
863
864
			foreach( $this->getServices() as $list )
865
			{
866
				foreach( $list as $service ) {
867
					$price = $price->addItem( $service->getPrice() );
868
				}
869
			}
870
871
			foreach( $this->getProducts() as $product ) {
872
				$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
873
			}
874
875
			$this->price = $price;
876
			$this->recalc = false;
877
		}
878
879
		return $this->price;
880
	}
881
882
883
	/**
884
	 * Returns a list of tax names and values
885
	 *
886
	 * @return array Associative list of tax names as key and price items as value
887
	 */
888
	public function getTaxes() : array
889
	{
890
		$taxes = [];
891
892
		foreach( $this->getProducts() as $product )
893
		{
894
			$price = $product->getPrice();
895
896
			foreach( $price->getTaxrates() as $name => $taxrate )
897
			{
898
				$price = (clone $price)->setTaxRate( $taxrate );
899
900
				if( isset( $taxes[$name][$taxrate] ) ) {
901
					$taxes[$name][$taxrate]->addItem( $price, $product->getQuantity() );
902
				} else {
903
					$taxes[$name][$taxrate] = $price->addItem( $price, $product->getQuantity() - 1 );
904
				}
905
			}
906
		}
907
908
		foreach( $this->getServices() as $services )
909
		{
910
			foreach( $services as $service )
911
			{
912
				$price = $service->getPrice();
913
914
				foreach( $price->getTaxrates() as $name => $taxrate )
915
				{
916
					$price = (clone $price)->setTaxRate( $taxrate );
917
918
					if( isset( $taxes[$name][$taxrate] ) ) {
919
						$taxes[$name][$taxrate]->addItem( $price );
920
					} else {
921
						$taxes[$name][$taxrate] = $price;
922
					}
923
				}
924
			}
925
		}
926
927
		return $taxes;
928
	}
929
930
931
	/**
932
	 * Returns the locales for the basic order item.
933
	 *
934
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
935
	 *  about site, language, country and currency
936
	 */
937
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
938
	{
939
		return $this->get( '.locale' );
940
	}
941
942
943
	/**
944
	 * Sets the locales for the basic order item.
945
	 *
946
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
947
	 *  about site, language, country and currency
948
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
949
	 */
950
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Order\Item\Iface
951
	{
952
		$this->notify( 'setLocale.before', $locale );
953
		$this->set( '.locale', clone $locale );
954
		$this->notify( 'setLocale.after', $locale );
955
956
		return parent::setModified();
957
	}
958
959
960
	/**
961
	 * Sets the modified flag of the object.
962
	 *
963
	 * @return \Aimeos\MShop\Common\Item\Iface Order base item for method chaining
964
	 */
965
	public function setModified() : \Aimeos\MShop\Common\Item\Iface
966
	{
967
		$this->recalc = true;
968
		return parent::setModified();
969
	}
970
971
972
	/**
973
	 * Returns the item values as array.
974
	 *
975
	 * @param bool True to return private properties, false for public only
976
	 * @return array Associative list of item properties and their values
977
	 */
978
	public function toArray( bool $private = false ) : array
979
	{
980
		$price = $this->getPrice();
981
		$list = parent::toArray( $private );
982
983
		$list['order.currencyid'] = $price->getCurrencyId();
984
		$list['order.price'] = $price->getValue();
985
		$list['order.costs'] = $price->getCosts();
986
		$list['order.rebate'] = $price->getRebate();
987
		$list['order.taxflag'] = $price->getTaxFlag();
988
		$list['order.taxvalue'] = $price->getTaxValue();
989
990
		return $list;
991
	}
992
993
994
	/**
995
	 * Checks if the given delivery status is a valid constant.
996
	 *
997
	 * @param int $value Delivery status constant defined in \Aimeos\MShop\Order\Item\Base
998
	 * @return int Delivery status constant defined in \Aimeos\MShop\Order\Item\Base
999
	 * @throws \Aimeos\MShop\Order\Exception If delivery status is invalid
1000
	 */
1001
	protected function checkDeliveryStatus( int $value )
1002
	{
1003
		if( $value < \Aimeos\MShop\Order\Item\Base::STAT_UNFINISHED || $value > \Aimeos\MShop\Order\Item\Base::STAT_RETURNED ) {
1004
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order delivery status "%1$s" not within allowed range', $value ) );
1005
		}
1006
1007
		return $value;
1008
	}
1009
1010
1011
	/**
1012
	 * Checks the given payment status is a valid constant.
1013
	 *
1014
	 * @param int $value Payment status constant defined in \Aimeos\MShop\Order\Item\Base
1015
	 * @return int Payment status constant defined in \Aimeos\MShop\Order\Item\Base
1016
	 * @throws \Aimeos\MShop\Order\Exception If payment status is invalid
1017
	 */
1018
	protected function checkPaymentStatus( int $value )
1019
	{
1020
		if( $value < \Aimeos\MShop\Order\Item\Base::PAY_UNFINISHED || $value > \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED ) {
1021
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order payment status "%1$s" not within allowed range', $value ) );
1022
		}
1023
1024
		return $value;
1025
	}
1026
1027
1028
	/**
1029
	 * Checks if the price uses the same currency as the price in the basket.
1030
	 *
1031
	 * @param \Aimeos\MShop\Price\Item\Iface $item Price item
1032
	 */
1033
	protected function checkPrice( \Aimeos\MShop\Price\Item\Iface $item )
1034
	{
1035
		$price = clone $this->getPrice();
1036
		$price->addItem( $item );
1037
	}
1038
1039
1040
	/**
1041
	 * Checks if all order addresses are valid
1042
	 *
1043
	 * @param \Aimeos\MShop\Order\Item\Address\Iface[] $items Order address items
1044
	 * @param string $type Address type constant from \Aimeos\MShop\Order\Item\Address\Base
1045
	 * @return \Aimeos\MShop\Order\Item\Address\Iface[] List of checked items
1046
	 * @throws \Aimeos\MShop\Exception If one of the order addresses is invalid
1047
	 */
1048
	protected function checkAddresses( iterable $items, string $type ) : iterable
1049
	{
1050
		map( $items )->implements( \Aimeos\MShop\Order\Item\Address\Iface::class, true );
1051
1052
		foreach( $items as $key => $item ) {
1053
			$items[$key] = $item->setType( $type );
1054
		}
1055
1056
		return $items;
1057
	}
1058
1059
1060
	/**
1061
	 * Checks if all order products are valid
1062
	 *
1063
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $items Order product items
1064
	 * @return \Aimeos\MShop\Order\Item\Product\Iface[] List of checked items
1065
	 * @throws \Aimeos\MShop\Exception If one of the order products is invalid
1066
	 */
1067
	protected function checkProducts( iterable $items ) : \Aimeos\Map
1068
	{
1069
		map( $items )->implements( \Aimeos\MShop\Order\Item\Product\Iface::class, true );
1070
1071
		foreach( $items as $key => $item )
1072
		{
1073
			if( $item->getProductCode() === '' ) {
1074
				throw new \Aimeos\MShop\Order\Exception( sprintf( 'Product does not contain the SKU code' ) );
1075
			}
1076
1077
			$this->checkPrice( $item->getPrice() );
1078
		}
1079
1080
		return map( $items );
1081
	}
1082
1083
1084
	/**
1085
	 * Checks if all order services are valid
1086
	 *
1087
	 * @param \Aimeos\MShop\Order\Item\Service\Iface[] $items Order service items
1088
	 * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base
1089
	 * @return \Aimeos\MShop\Order\Item\Service\Iface[] List of checked items
1090
	 * @throws \Aimeos\MShop\Exception If one of the order services is invalid
1091
	 */
1092
	protected function checkServices( iterable $items, string $type ) : iterable
1093
	{
1094
		map( $items )->implements( \Aimeos\MShop\Order\Item\Service\Iface::class, true );
1095
1096
		foreach( $items as $key => $item )
1097
		{
1098
			$this->checkPrice( $item->getPrice() );
1099
			$items[$key] = $item->setType( $type );
1100
		}
1101
1102
		return $items;
1103
	}
1104
1105
1106
	/**
1107
	 * Tests if the given product is similar to an existing one.
1108
	 * Similarity is described by the equality of properties so the quantity of
1109
	 * the existing product can be updated.
1110
	 *
1111
	 * @param \Aimeos\MShop\Order\Item\Product\Iface $item Order product item
1112
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of order product items to check against
1113
	 * @return int|null Positon of the same product in the product list of false if product is unique
1114
	 * @throws \Aimeos\MShop\Order\Exception If no similar item was found
1115
	 */
1116
	protected function getSameProduct( \Aimeos\MShop\Order\Item\Product\Iface $item, iterable $products ) : ?int
1117
	{
1118
		$map = [];
1119
		$count = 0;
1120
1121
		foreach( $item->getAttributeItems() as $attributeItem )
1122
		{
1123
			$key = md5( $attributeItem->getCode() . json_encode( $attributeItem->getValue() ) );
1124
			$map[$key] = $attributeItem;
1125
			$count++;
1126
		}
1127
1128
		foreach( $products as $position => $product )
1129
		{
1130
			if( $product->compare( $item ) === false ) {
1131
				continue;
1132
			}
1133
1134
			$prodAttributes = $product->getAttributeItems();
1135
1136
			if( count( $prodAttributes ) !== $count ) {
1137
				continue;
1138
			}
1139
1140
			foreach( $prodAttributes as $attribute )
1141
			{
1142
				$key = md5( $attribute->getCode() . json_encode( $attribute->getValue() ) );
1143
1144
				if( isset( $map[$key] ) === false || $map[$key]->getQuantity() != $attribute->getQuantity() ) {
1145
					continue 2; // jump to outer loop
1146
				}
1147
			}
1148
1149
			return $position;
1150
		}
1151
1152
		return null;
1153
	}
1154
}
1155