Passed
Push — master ( b35ffc...19a63c )
by Aimeos
04:50
created

Standard::setCustomerId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Item;
13
14
15
/**
16
 * Default implementation of an order invoice item.
17
 *
18
 * @property int oldPaymentStatus Last delivery status before it was changed by setDeliveryStatus()
19
 * @property int oldDeliveryStatus Last payment status before it was changed by setPaymentStatus()
20
 *
21
 * @package MShop
22
 * @subpackage Order
23
 */
24
class Standard
25
	extends \Aimeos\MShop\Order\Item\Base
26
	implements \Aimeos\MShop\Order\Item\Iface
27
{
28
	// protected is a workaround for serialize problem
29
	protected $price;
30
	protected $locale;
31
	protected $customer;
32
	protected $recalc = false;
33
34
35
	/**
36
	 * Initializes the shopping basket.
37
	 *
38
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
39
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
40
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
41
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of ordered product items
42
	 * @param \Aimeos\MShop\Order\Item\Address\Iface[] $addresses List of order address items
43
	 * @param \Aimeos\MShop\Order\Item\Service\Iface[] $services List of order service items
44
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $coupons Associative list of coupon codes as keys and order product items as values
45
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object
46
	 */
47
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
48
		array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
49
		?\Aimeos\MShop\Customer\Item\Iface $custItem = null )
50
	{
51
		parent::__construct( $price, $locale, $values, $products, $addresses, $services, $coupons );
52
53
		$this->price = $price;
54
		$this->locale = $locale;
55
		$this->customer = $custItem;
56
	}
57
58
59
	/**
60
	 * Clones internal objects of the order base item.
61
	 */
62
	public function __clone()
63
	{
64
		parent::__clone();
65
66
		$this->price = clone $this->price;
67
		$this->locale = clone $this->locale;
68
	}
69
70
71
	/**
72
	 * Specifies the data which should be serialized to JSON by json_encode().
73
	 *
74
	 * @return array<string,mixed> Data to serialize to JSON
75
	 */
76
	#[\ReturnTypeWillChange]
77
	public function jsonSerialize()
78
	{
79
		return parent::jsonSerialize() + [
80
			'price' => $this->price,
81
			'locale' => $this->locale,
82
			'customer' => $this->customer,
83
		];
84
	}
85
86
87
	/**
88
	 * Returns the order number
89
	 *
90
	 * @return string Order number
91
	 */
92
	public function getOrderNumber() : string
93
	{
94
		if( $fcn = self::macro( 'ordernumber' ) ) {
95
			return (string) $fcn( $this );
96
		}
97
98
		return (string) $this->getId();
99
	}
100
101
102
	/**
103
	 * Returns the number of the invoice.
104
	 *
105
	 * @return string Invoice number
106
	 */
107
	public function getInvoiceNumber() : string
108
	{
109
		return (string) $this->get( 'order.invoiceno', '' );
110
	}
111
112
113
	/**
114
	 * Sets the number of the invoice.
115
	 *
116
	 * @param string $value Invoice number
117
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
118
	 */
119
	public function setInvoiceNumber( string $value ) : \Aimeos\MShop\Common\Item\Iface
120
	{
121
		return $this->set( 'order.invoiceno', $value );
122
	}
123
124
125
	/**
126
	 * Returns the channel of the invoice (repeating, web, phone, etc).
127
	 *
128
	 * @return string Invoice channel
129
	 */
130
	public function getChannel() : string
131
	{
132
		return (string) $this->get( 'order.channel', '' );
133
	}
134
135
136
	/**
137
	 * Sets the channel of the invoice.
138
	 *
139
	 * @param string $channel Invoice channel
140
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
141
	 */
142
	public function setChannel( string $channel ) : \Aimeos\MShop\Common\Item\Iface
143
	{
144
		return $this->set( 'order.channel', $this->checkCode( $channel ) );
145
	}
146
147
148
	/**
149
	 * Returns the delivery date of the invoice.
150
	 *
151
	 * @return string|null ISO date in yyyy-mm-dd HH:ii:ss format
152
	 */
153
	public function getDateDelivery() : ?string
154
	{
155
		$value = $this->get( 'order.datedelivery' );
156
		return $value ? substr( $value, 0, 19 ) : null;
157
	}
158
159
160
	/**
161
	 * Sets the delivery date of the invoice.
162
	 *
163
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format
164
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
165
	 */
166
	public function setDateDelivery( ?string $date ) : \Aimeos\MShop\Order\Item\Iface
167
	{
168
		return $this->set( 'order.datedelivery', $this->checkDateFormat( $date ) );
169
	}
170
171
172
	/**
173
	 * Returns the purchase date of the invoice.
174
	 *
175
	 * @return string|null ISO date in yyyy-mm-dd HH:ii:ss format
176
	 */
177
	public function getDatePayment() : ?string
178
	{
179
		$value = $this->get( 'order.datepayment' );
180
		return $value ? substr( $value, 0, 19 ) : null;
181
	}
182
183
184
	/**
185
	 * Sets the purchase date of the invoice.
186
	 *
187
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format
188
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
189
	 */
190
	public function setDatePayment( ?string $date ) : \Aimeos\MShop\Order\Item\Iface
191
	{
192
		return $this->set( 'order.datepayment', $this->checkDateFormat( $date ) );
193
	}
194
195
196
	/**
197
	 * Returns the delivery status of the invoice.
198
	 *
199
	 * @return int Status code constant from \Aimeos\MShop\Order\Item\Base
200
	 */
201
	public function getStatusDelivery() : int
202
	{
203
		return $this->get( 'order.statusdelivery', -1 );
204
	}
205
206
207
	/**
208
	 * Sets the delivery status of the invoice.
209
	 *
210
	 * @param int $status Status code constant from \Aimeos\MShop\Order\Item\Base
211
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
212
	 */
213
	public function setStatusDelivery( int $status ) : \Aimeos\MShop\Order\Item\Iface
214
	{
215
		$this->set( '.statusdelivery', $this->get( 'order.statusdelivery' ) );
216
		return $this->set( 'order.statusdelivery', $status );
217
	}
218
219
220
	/**
221
	 * Returns the payment status of the invoice.
222
	 *
223
	 * @return int Payment constant from \Aimeos\MShop\Order\Item\Base
224
	 */
225
	public function getStatusPayment() : int
226
	{
227
		return $this->get( 'order.statuspayment', -1 );
228
	}
229
230
231
	/**
232
	 * Sets the payment status of the invoice.
233
	 *
234
	 * @param int $status Payment constant from \Aimeos\MShop\Order\Item\Base
235
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
236
	 */
237
	public function setStatusPayment( int $status ) : \Aimeos\MShop\Order\Item\Iface
238
	{
239
		if( $status !== $this->getStatusPayment() ) {
240
			$this->set( 'order.datepayment', date( 'Y-m-d H:i:s' ) );
241
		}
242
243
		$this->set( '.statuspayment', $this->get( 'order.statuspayment' ) );
244
		return $this->set( 'order.statuspayment', $status );
245
	}
246
247
248
	/**
249
	 * Returns the related invoice ID.
250
	 *
251
	 * @return string Related invoice ID
252
	 */
253
	public function getRelatedId() : string
254
	{
255
		return (string) $this->get( 'order.relatedid', '' );
256
	}
257
258
259
	/**
260
	 * Sets the related invoice ID.
261
	 *
262
	 * @param string|null $id Related invoice ID
263
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
264
	 * @throws \Aimeos\MShop\Order\Exception If ID is invalid
265
	 */
266
	public function setRelatedId( ?string $id ) : \Aimeos\MShop\Order\Item\Iface
267
	{
268
		return $this->set( 'order.relatedid', (string) $id );
269
	}
270
271
272
	/**
273
	 * Returns the associated customer item
274
	 *
275
	 * @return \Aimeos\MShop\Customer\Item\Iface|null Customer item
276
	 */
277
	public function getCustomerItem() : ?\Aimeos\MShop\Customer\Item\Iface
278
	{
279
		return $this->customer;
280
	}
281
282
283
	/**
284
	 * Returns the code of the site the item is stored.
285
	 *
286
	 * @return string Site code (or empty string if not available)
287
	 */
288
	public function getSiteCode() : string
289
	{
290
		return $this->get( 'order.sitecode', '' );
291
	}
292
293
294
	/**
295
	 * Returns the comment field of the order item.
296
	 *
297
	 * @return string Comment for the order
298
	 */
299
	public function getComment() : string
300
	{
301
		return $this->get( 'order.comment', '' );
302
	}
303
304
305
	/**
306
	 * Sets the comment field of the order item
307
	 *
308
	 * @param string $comment Comment for the order
309
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
310
	 */
311
	public function setComment( ?string $comment ) : \Aimeos\MShop\Order\Item\Iface
312
	{
313
		return $this->set( 'order.comment', (string) $comment );
314
	}
315
316
317
	/**
318
	 * Returns the customer ID of the customer who has ordered.
319
	 *
320
	 * @return string Unique ID of the customer
321
	 */
322
	public function getCustomerId() : string
323
	{
324
		return $this->get( 'order.customerid', '' );
325
	}
326
327
328
	/**
329
	 * Sets the customer ID of the customer who has ordered.
330
	 *
331
	 * @param string $customerid Unique ID of the customer
332
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
333
	 */
334
	public function setCustomerId( ?string $customerid ) : \Aimeos\MShop\Order\Item\Iface
335
	{
336
		if( (string) $customerid !== $this->getCustomerId() )
337
		{
338
			$this->notify( 'setCustomerId.before', (string) $customerid );
339
			$this->set( 'order.customerid', (string) $customerid );
340
			$this->notify( 'setCustomerId.after', (string) $customerid );
341
		}
342
343
		return $this;
344
	}
345
346
347
	/**
348
	 * Returns the customer reference field of the order item
349
	 *
350
	 * @return string Customer reference for the order
351
	 */
352
	public function getCustomerReference() : string
353
	{
354
		return $this->get( 'order.customerref', '' );
355
	}
356
357
358
	/**
359
	 * Sets the customer reference field of the order item
360
	 *
361
	 * @param string $value Customer reference for the order
362
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
363
	 */
364
	public function setCustomerReference( ?string $value ) : \Aimeos\MShop\Order\Item\Iface
365
	{
366
		return $this->set( 'order.customerref', (string) $value );
367
	}
368
369
370
	/**
371
	 * Returns the locales for the basic order item.
372
	 *
373
	 * @return \Aimeos\MShop\Locale\Item\Iface Object containing information
374
	 *  about site, language, country and currency
375
	 */
376
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
377
	{
378
		return $this->locale;
379
	}
380
381
382
	/**
383
	 * Sets the locales for the basic order item.
384
	 *
385
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Object containing information
386
	 *  about site, language, country and currency
387
	 * @return \Aimeos\MShop\Order\Item\Iface Order base item for chaining method calls
388
	 */
389
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Order\Item\Iface
390
	{
391
		$this->notify( 'setLocale.before', $locale );
392
393
		$this->locale = clone $locale;
394
		$this->setModified();
395
396
		$this->notify( 'setLocale.after', $locale );
397
398
		return $this;
399
	}
400
401
402
	/**
403
	 * Returns a price item with amounts calculated for the products, costs, etc.
404
	 *
405
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate the customer has to pay
406
	 */
407
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
408
	{
409
		if( $this->recalc )
410
		{
411
			$price = $this->price->clear();
412
413
			foreach( $this->getServices() as $list )
414
			{
415
				foreach( $list as $service ) {
416
					$price = $price->addItem( $service->getPrice() );
417
				}
418
			}
419
420
			foreach( $this->getProducts() as $product ) {
421
				$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
422
			}
423
424
			$this->price = $price;
425
			$this->recalc = false;
426
		}
427
428
		return $this->price;
429
	}
430
431
432
	/**
433
	 * Sets the modified flag of the object.
434
	 *
435
	 * @return \Aimeos\MShop\Common\Item\Iface Order base item for method chaining
436
	 */
437
	public function setModified() : \Aimeos\MShop\Common\Item\Iface
438
	{
439
		$this->recalc = true;
440
		return parent::setModified();
441
	}
442
443
444
	/*
445
	 * Sets the item values from the given array and removes that entries from the list
446
	 *
447
	 * @param array &$list Associative list of item keys and their values
448
	 * @param bool True to set private properties too, false for public only
449
	 * @return \Aimeos\MShop\Order\Item\Iface Order item for chaining method calls
450
	 */
451
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
452
	{
453
		$item = parent::fromArray( $list, $private );
454
		$locale = $item->locale();
455
456
		foreach( $list as $key => $value )
457
		{
458
			switch( $key )
459
			{
460
				case 'order.channel': $item = $item->setChannel( $value ); break;
461
				case 'order.invoiceno': !$private ?: $item = $item->setInvoiceNumber( $value ); break;
462
				case 'order.statusdelivery': $item = $item->setStatusDelivery( (int) $value ); break;
463
				case 'order.statuspayment': $item = $item->setStatusPayment( (int) $value ); break;
464
				case 'order.datedelivery': $item = $item->setDateDelivery( $value ); break;
465
				case 'order.datepayment': $item = $item->setDatePayment( $value ); break;
466
				case 'order.relatedid': $item = $item->setRelatedId( $value ); break;
467
				case 'order.customerid': $item = $item->setCustomerId( $value ); break;
468
				case 'order.languageid': $locale = $locale->setLanguageId( $value ); break;
469
				case 'order.customerref': $item = $item->setCustomerReference( $value ); break;
470
				case 'order.comment': $item = $item->setComment( $value ); break;
471
				default: continue 2;
472
			}
473
474
			unset( $list[$key] );
475
		}
476
477
		return $item->setLocale( $locale );
478
	}
479
480
481
	/**
482
	 * Returns the item values as array.
483
	 *
484
	 * @param bool True to return private properties, false for public only
485
	 * @return array Associative list of item properties and their values
486
	 */
487
	public function toArray( bool $private = false ) : array
488
	{
489
		$price = $this->getPrice();
490
		$locale = $this->locale();
491
492
		$list = parent::toArray( $private );
493
494
		$list['order.channel'] = $this->getChannel();
495
		$list['order.invoiceno'] = $this->getInvoiceNumber();
496
		$list['order.statusdelivery'] = $this->getStatusDelivery();
497
		$list['order.statuspayment'] = $this->getStatusPayment();
498
		$list['order.datedelivery'] = $this->getDateDelivery();
499
		$list['order.datepayment'] = $this->getDatePayment();
500
		$list['order.relatedid'] = $this->getRelatedId();
501
		$list['order.sitecode'] = $this->getSiteCode();
502
		$list['order.customerid'] = $this->getCustomerId();
503
		$list['order.languageid'] = $locale->getLanguageId();
504
		$list['order.currencyid'] = $price->getCurrencyId();
505
		$list['order.price'] = $price->getValue();
506
		$list['order.costs'] = $price->getCosts();
507
		$list['order.rebate'] = $price->getRebate();
508
		$list['order.taxflag'] = $price->getTaxFlag();
509
		$list['order.taxvalue'] = $price->getTaxValue();
510
		$list['order.customerref'] = $this->getCustomerReference();
511
		$list['order.comment'] = $this->getComment();
512
513
		return $list;
514
	}
515
}
516