Passed
Push — master ( fc01fb...26d801 )
by Aimeos
28:48 queued 11:51
created

Standard::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 56
rs 9.344
cc 3
nc 3
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2023
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Manager;
13
14
15
/**
16
 * Default order manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard extends Base
22
	implements \Aimeos\MShop\Order\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	/** mshop/order/manager/name
25
	 * Class name of the used order manager implementation
26
	 *
27
	 * Each default manager can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the manager factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\MShop\Order\Manager\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\MShop\Order\Manager\Mymanager
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  mshop/order/manager/name = Mymanager
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyManager"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 * @category Developer
56
	 */
57
58
	/** mshop/order/manager/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the order manager
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "mshop/common/manager/decorators/default" before they are wrapped
68
	 * around the order manager.
69
	 *
70
	 *  mshop/order/manager/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
74
	 * "mshop/common/manager/decorators/default" for the order manager.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2014.03
78
	 * @category Developer
79
	 * @see mshop/common/manager/decorators/default
80
	 * @see mshop/order/manager/decorators/global
81
	 * @see mshop/order/manager/decorators/local
82
	 */
83
84
	/** mshop/order/manager/decorators/global
85
	 * Adds a list of globally available decorators only to the order manager
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the order manager.
94
	 *
95
	 *  mshop/order/manager/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the order
99
	 * manager.
100
	 *
101
	 * @param array List of decorator names
102
	 * @since 2014.03
103
	 * @category Developer
104
	 * @see mshop/common/manager/decorators/default
105
	 * @see mshop/order/manager/decorators/excludes
106
	 * @see mshop/order/manager/decorators/local
107
	 */
108
109
	/** mshop/order/manager/decorators/local
110
	 * Adds a list of local decorators only to the order manager
111
	 *
112
	 * Decorators extend the functionality of a class by adding new aspects
113
	 * (e.g. log what is currently done), executing the methods of the underlying
114
	 * class only in certain conditions (e.g. only for logged in users) or
115
	 * modify what is returned to the caller.
116
	 *
117
	 * This option allows you to wrap local decorators
118
	 * ("\Aimeos\MShop\Order\Manager\Decorator\*") around the order manager.
119
	 *
120
	 *  mshop/order/manager/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\MShop\Order\Manager\Decorator\Decorator2" only to the order
124
	 * manager.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2014.03
128
	 * @category Developer
129
	 * @see mshop/common/manager/decorators/default
130
	 * @see mshop/order/manager/decorators/excludes
131
	 * @see mshop/order/manager/decorators/global
132
	 */
133
134
135
	private array $searchConfig = array(
136
		'order.id' => array(
137
			'code' => 'order.id',
138
			'internalcode' => 'mord."id"',
139
			'label' => 'Invoice ID',
140
			'type' => 'integer',
141
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
142
		),
143
		'order.siteid' => array(
144
			'code' => 'order.siteid',
145
			'internalcode' => 'mord."siteid"',
146
			'label' => 'Invoice site ID',
147
			'type' => 'string',
148
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
149
			'public' => false,
150
		),
151
		'order.invoiceno' => array(
152
			'code' => 'order.invoiceno',
153
			'internalcode' => 'mord."invoiceno"',
154
			'label' => 'Invoice number',
155
			'type' => 'string',
156
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
157
		),
158
		'order.relatedid' => array(
159
			'code' => 'order.relatedid',
160
			'internalcode' => 'mord."relatedid"',
161
			'label' => 'Related invoice ID',
162
			'type' => 'string',
163
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
164
		),
165
		'order.channel' => array(
166
			'code' => 'order.channel',
167
			'internalcode' => 'mord."channel"',
168
			'label' => 'Order channel',
169
			'type' => 'string',
170
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
171
		),
172
		'order.datepayment' => array(
173
			'code' => 'order.datepayment',
174
			'internalcode' => 'mord."datepayment"',
175
			'label' => 'Purchase date',
176
			'type' => 'datetime',
177
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
178
		),
179
		'order.datedelivery' => array(
180
			'code' => 'order.datedelivery',
181
			'internalcode' => 'mord."datedelivery"',
182
			'label' => 'Delivery date',
183
			'type' => 'datetime',
184
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
185
		),
186
		'order.statusdelivery' => array(
187
			'code' => 'order.statusdelivery',
188
			'internalcode' => 'mord."statusdelivery"',
189
			'label' => 'Delivery status',
190
			'type' => 'integer',
191
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
192
		),
193
		'order.statuspayment' => array(
194
			'code' => 'order.statuspayment',
195
			'internalcode' => 'mord."statuspayment"',
196
			'label' => 'Payment status',
197
			'type' => 'integer',
198
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
199
		),
200
		'order.sitecode' => array(
201
			'code' => 'order.sitecode',
202
			'internalcode' => 'mord."sitecode"',
203
			'label' => 'Order site code',
204
			'type' => 'string',
205
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
206
			'public' => false,
207
		),
208
		'order.customerid' => array(
209
			'code' => 'order.customerid',
210
			'internalcode' => 'mord."customerid"',
211
			'label' => 'Order customer ID',
212
			'type' => 'string',
213
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
214
		),
215
		'order.customerref' => array(
216
			'code' => 'order.customerref',
217
			'internalcode' => 'mord."customerref"',
218
			'label' => 'Order customer reference',
219
			'type' => 'string',
220
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
221
		),
222
		'order.languageid' => array(
223
			'code' => 'order.languageid',
224
			'internalcode' => 'mord."langid"',
225
			'label' => 'Order language code',
226
			'type' => 'string',
227
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
228
		),
229
		'order.currencyid' => array(
230
			'code' => 'order.currencyid',
231
			'internalcode' => 'mord."currencyid"',
232
			'label' => 'Order currencyid code',
233
			'type' => 'string',
234
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
235
		),
236
		'order.price' => array(
237
			'code' => 'order.price',
238
			'internalcode' => 'mord."price"',
239
			'label' => 'Order price amount',
240
			'type' => 'string',
241
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
242
		),
243
		'order.costs' => array(
244
			'code' => 'order.costs',
245
			'internalcode' => 'mord."costs"',
246
			'label' => 'Order shipping amount',
247
			'type' => 'string',
248
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
249
		),
250
		'order.rebate' => array(
251
			'code' => 'order.rebate',
252
			'internalcode' => 'mord."rebate"',
253
			'label' => 'Order rebate amount',
254
			'type' => 'string',
255
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
256
		),
257
		'order.taxvalue' => array(
258
			'code' => 'order.taxvalue',
259
			'internalcode' => 'mord."tax"',
260
			'label' => 'Order tax amount',
261
			'type' => 'string',
262
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
263
		),
264
		'order.taxflag' => array(
265
			'code' => 'order.taxflag',
266
			'internalcode' => 'mord."taxflag"',
267
			'label' => 'Order tax flag (0=net, 1=gross)',
268
			'type' => 'string',
269
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
270
		),
271
		'order.comment' => array(
272
			'code' => 'order.comment',
273
			'internalcode' => 'mord."comment"',
274
			'label' => 'Order comment',
275
			'type' => 'string',
276
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
277
		),
278
		'order.cdate' => array(
279
			'code' => 'order.cdate',
280
			'internalcode' => 'mord."cdate"',
281
			'label' => 'Create date',
282
			'type' => 'string',
283
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
284
			'public' => false,
285
		),
286
		'order.cmonth' => array(
287
			'code' => 'order.cmonth',
288
			'internalcode' => 'mord."cmonth"',
289
			'label' => 'Create month',
290
			'type' => 'string',
291
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
292
			'public' => false,
293
		),
294
		'order.cweek' => array(
295
			'code' => 'order.cweek',
296
			'internalcode' => 'mord."cweek"',
297
			'label' => 'Create week',
298
			'type' => 'string',
299
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
300
			'public' => false,
301
		),
302
		'order.cwday' => array(
303
			'code' => 'order.cwday',
304
			'internalcode' => 'mord."cwday"',
305
			'label' => 'Create weekday',
306
			'type' => 'string',
307
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
308
			'public' => false,
309
		),
310
		'order.chour' => array(
311
			'code' => 'order.chour',
312
			'internalcode' => 'mord."chour"',
313
			'label' => 'Create hour',
314
			'type' => 'string',
315
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
316
			'public' => false,
317
		),
318
		'order.ctime' => array(
319
			'code' => 'order.ctime',
320
			'internalcode' => 'mord."ctime"',
321
			'label' => 'Create date/time',
322
			'type' => 'datetime',
323
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
324
			'public' => false,
325
		),
326
		'order.mtime' => array(
327
			'code' => 'order.mtime',
328
			'internalcode' => 'mord."mtime"',
329
			'label' => 'Modify date/time',
330
			'type' => 'datetime',
331
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
332
			'public' => false,
333
		),
334
		'order.editor' => array(
335
			'code' => 'order.editor',
336
			'internalcode' => 'mord."editor"',
337
			'label' => 'Editor',
338
			'type' => 'string',
339
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
340
			'public' => false,
341
		),
342
		'order:status' => array(
343
			'code' => 'order:status()',
344
			'internalcode' => '( SELECT COUNT(mordst_cs."parentid")
345
				FROM "mshop_order_status" AS mordst_cs
346
				WHERE mord."id" = mordst_cs."parentid" AND :site
347
				AND mordst_cs."type" = $1 AND mordst_cs."value" IN ( $2 ) )',
348
			'label' => 'Number of order status items, parameter(<type>,<value>)',
349
			'type' => 'integer',
350
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
351
			'public' => false,
352
		),
353
	);
354
355
356
	/**
357
	 * Creates the manager that will use the given context object.
358
	 *
359
	 * @param \Aimeos\MShop\ContextIface $context Context object with required objects
360
	 */
361
	public function __construct( \Aimeos\MShop\ContextIface $context )
362
	{
363
		parent::__construct( $context );
364
365
		/** mshop/order/manager/resource
366
		 * Name of the database connection resource to use
367
		 *
368
		 * You can configure a different database connection for each data domain
369
		 * and if no such connection name exists, the "db" connection will be used.
370
		 * It's also possible to use the same database connection for different
371
		 * data domains by configuring the same connection name using this setting.
372
		 *
373
		 * @param string Database connection name
374
		 * @since 2023.04
375
		 */
376
		$this->setResourceName( $context->config()->get( 'mshop/order/manager/resource', 'db-order' ) );
377
378
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
379
		$level = $context->config()->get( 'mshop/order/manager/sitemode', $level );
380
381
		$name = 'order:status';
382
		$expr = $this->siteString( 'mordst_cs."siteid"', $level );
383
		$this->searchConfig[$name]['internalcode'] = str_replace( ':site', $expr, $this->searchConfig[$name]['internalcode'] );
384
	}
385
386
387
	/**
388
	 * Counts the number items that are available for the values of the given key.
389
	 *
390
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria
391
	 * @param array|string $key Search key or list of key to aggregate items for
392
	 * @param string|null $value Search key for aggregating the value column
393
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
394
	 * @return \Aimeos\Map List of the search keys as key and the number of counted items as value
395
	 */
396
	public function aggregate( \Aimeos\Base\Criteria\Iface $search, $key, string $value = null, string $type = null ) : \Aimeos\Map
397
	{
398
		/** mshop/order/manager/aggregate/mysql
399
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
400
		 *
401
		 * @see mshop/order/manager/aggregate/ansi
402
		 */
403
404
		/** mshop/order/manager/aggregate/ansi
405
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
406
		 *
407
		 * Groups all records by the values in the key column and counts their
408
		 * occurence. The matched records can be limited by the given criteria
409
		 * from the order database. The records must be from one of the sites
410
		 * that are configured via the context item. If the current site is part
411
		 * of a tree of sites, the statement can count all records from the
412
		 * current site and the complete sub-tree of sites.
413
		 *
414
		 * As the records can normally be limited by criteria from sub-managers,
415
		 * their tables must be joined in the SQL context. This is done by
416
		 * using the "internaldeps" property from the definition of the ID
417
		 * column of the sub-managers. These internal dependencies specify
418
		 * the JOIN between the tables and the used columns for joining. The
419
		 * ":joins" placeholder is then replaced by the JOIN strings from
420
		 * the sub-managers.
421
		 *
422
		 * To limit the records matched, conditions can be added to the given
423
		 * criteria object. It can contain comparisons like column names that
424
		 * must match specific values which can be combined by AND, OR or NOT
425
		 * operators. The resulting string of SQL conditions replaces the
426
		 * ":cond" placeholder before the statement is sent to the database
427
		 * server.
428
		 *
429
		 * This statement doesn't return any records. Instead, it returns pairs
430
		 * of the different values found in the key column together with the
431
		 * number of records that have been found for that key values.
432
		 *
433
		 * The SQL statement should conform to the ANSI standard to be
434
		 * compatible with most relational database systems. This also
435
		 * includes using double quotes for table and column names.
436
		 *
437
		 * @param string SQL statement for aggregating order items
438
		 * @since 2014.09
439
		 * @category Developer
440
		 * @see mshop/order/manager/insert/ansi
441
		 * @see mshop/order/manager/update/ansi
442
		 * @see mshop/order/manager/newid/ansi
443
		 * @see mshop/order/manager/delete/ansi
444
		 * @see mshop/order/manager/search/ansi
445
		 * @see mshop/order/manager/count/ansi
446
		 */
447
448
		/** mshop/order/manager/aggregateavg/mysql
449
		 * Computes the average of all values grouped by the key column and matched by the given criteria
450
		 *
451
		 * @param string SQL statement for aggregating the order items and computing the average value
452
		 * @since 2017.10
453
		 * @category Developer
454
		 * @see mshop/order/manager/aggregateavg/ansi
455
		 * @see mshop/order/manager/aggregate/mysql
456
		 */
457
458
		/** mshop/order/manager/aggregateavg/ansi
459
		 * Computes the average of all values grouped by the key column and matched by the given criteria
460
		 *
461
		 * @param string SQL statement for aggregating the order items and computing the average value
462
		 * @since 2017.10
463
		 * @category Developer
464
		 * @see mshop/order/manager/aggregate/ansi
465
		 */
466
467
		/** mshop/order/manager/aggregatesum/mysql
468
		 * Computes the sum of all values grouped by the key column and matched by the given criteria
469
		 *
470
		 * @param string SQL statement for aggregating the order items and computing the sum
471
		 * @since 2017.10
472
		 * @category Developer
473
		 * @see mshop/order/manager/aggregatesum/ansi
474
		 * @see mshop/order/manager/aggregate/mysql
475
		 */
476
477
		/** mshop/order/manager/aggregatesum/ansi
478
		 * Computes the sum of all values grouped by the key column and matched by the given criteria
479
		 *
480
		 * @param string SQL statement for aggregating the order items and computing the sum
481
		 * @since 2017.10
482
		 * @category Developer
483
		 * @see mshop/order/manager/aggregate/ansi
484
		 */
485
486
		$cfgkey = 'mshop/order/manager/aggregate';
487
		return $this->aggregateBase( $search, $key, $cfgkey, ['order'], $value, $type );
488
	}
489
490
491
	/**
492
	 * Removes old entries from the storage.
493
	 *
494
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
495
	 * @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
496
	 */
497
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
498
	{
499
		$path = 'mshop/order/manager/submanagers';
500
		$default = ['address', 'coupon', 'product', 'service', 'status'];
501
502
		foreach( $this->context()->config()->get( $path, $default ) as $domain ) {
503
			$this->object()->getSubManager( $domain )->clear( $siteids );
504
		}
505
506
		return $this->clearBase( $siteids, 'mshop/order/manager/delete' );
507
	}
508
509
510
	/**
511
	 * Creates a new empty item instance
512
	 *
513
	 * @param array $values Values the item should be initialized with
514
	 * @return \Aimeos\MShop\Order\Item\Iface New order item object
515
	 */
516
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
517
	{
518
		$context = $this->context();
519
		$locale = $context->locale();
520
521
		$price = \Aimeos\MShop::create( $context, 'price' )->create();
522
		$values['order.siteid'] = $values['order.siteid'] ?? $locale->getSiteId();
523
524
		$base = $this->createItemBase( $price, clone $locale, $values );
525
		\Aimeos\MShop::create( $context, 'plugin' )->register( $base, 'order' );
526
527
		return $base;
528
	}
529
530
531
	/**
532
	 * Removes multiple items.
533
	 *
534
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
535
	 * @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
536
	 */
537
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
538
	{
539
		/** mshop/order/manager/delete/mysql
540
		 * Deletes the items matched by the given IDs from the database
541
		 *
542
		 * @see mshop/order/manager/delete/ansi
543
		 */
544
545
		/** mshop/order/manager/delete/ansi
546
		 * Deletes the items matched by the given IDs from the database
547
		 *
548
		 * Removes the records specified by the given IDs from the order database.
549
		 * The records must be from the site that is configured via the
550
		 * context item.
551
		 *
552
		 * The ":cond" placeholder is replaced by the name of the ID column and
553
		 * the given ID or list of IDs while the site ID is bound to the question
554
		 * mark.
555
		 *
556
		 * The SQL statement should conform to the ANSI standard to be
557
		 * compatible with most relational database systems. This also
558
		 * includes using double quotes for table and column names.
559
		 *
560
		 * @param string SQL statement for deleting items
561
		 * @since 2014.03
562
		 * @category Developer
563
		 * @see mshop/order/manager/insert/ansi
564
		 * @see mshop/order/manager/update/ansi
565
		 * @see mshop/order/manager/newid/ansi
566
		 * @see mshop/order/manager/search/ansi
567
		 * @see mshop/order/manager/count/ansi
568
		 */
569
		$path = 'mshop/order/manager/delete';
570
571
		return $this->deleteItemsBase( $itemIds, $path );
572
	}
573
574
575
	/**
576
	 * Creates a search critera object
577
	 *
578
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
579
	 * @param bool $site TRUE to add site criteria to show orders with available products only
580
	 * @return \Aimeos\Base\Criteria\Iface New search criteria object
581
	 */
582
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
583
	{
584
		$search = parent::filter( $default );
585
		$context = $this->context();
586
587
		if( $default !== false ) {
588
			$search->add( ['order.customerid' => $context->user()] );
589
		}
590
591
		if( $site === true )
592
		{
593
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE;
594
			$search->add( $this->siteCondition( 'order.product.siteid', $level ) );
595
		}
596
597
		return $search;
598
	}
599
600
601
	/**
602
	 * Returns an order invoice item built from database values.
603
	 *
604
	 * @param string $id Unique id of the order invoice
605
	 * @param string[] $ref List of domains to fetch list items and referenced items for
606
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
607
	 * @return \Aimeos\MShop\Order\Item\Iface Returns order invoice item of the given id
608
	 * @throws \Aimeos\MShop\Order\Exception If item couldn't be found
609
	 */
610
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
611
	{
612
		return $this->getItemBase( 'order.id', $id, $ref, $default );
613
	}
614
615
616
	/**
617
	 * Returns the available manager types
618
	 *
619
	 * @param bool $withsub Return also the resource type of sub-managers if true
620
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
621
	 */
622
	public function getResourceType( bool $withsub = true ) : array
623
	{
624
		$path = 'mshop/order/manager/submanagers';
625
		$default = ['address', 'coupon', 'product', 'service', 'status'];
626
627
		return $this->getResourceTypeBase( 'order', $path, $default, $withsub );
628
	}
629
630
631
	/**
632
	 * Returns the attributes that can be used for searching.
633
	 *
634
	 * @param bool $withsub Return also attributes of sub-managers if true
635
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
636
	 */
637
	public function getSearchAttributes( bool $withsub = true ) : array
638
	{
639
		/** mshop/order/manager/submanagers
640
		 * List of manager names that can be instantiated by the order manager
641
		 *
642
		 * Managers provide a generic interface to the underlying storage.
643
		 * Each manager has or can have sub-managers caring about particular
644
		 * aspects. Each of these sub-managers can be instantiated by its
645
		 * parent manager using the getSubManager() method.
646
		 *
647
		 * The search keys from sub-managers can be normally used in the
648
		 * manager as well. It allows you to search for items of the manager
649
		 * using the search keys of the sub-managers to further limit the
650
		 * retrieved list of items.
651
		 *
652
		 * @param array List of sub-manager names
653
		 * @since 2014.03
654
		 * @category Developer
655
		 */
656
		$path = 'mshop/order/manager/submanagers';
657
		$default = ['address', 'coupon', 'product', 'service', 'status'];
658
659
		return $this->getSearchAttributesBase( $this->searchConfig, $path, $default, $withsub );
660
	}
661
662
663
	/**
664
	 * Creates a one-time order in the storage from the given invoice object.
665
	 *
666
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item with necessary values
667
	 * @param bool $fetch True if the new ID should be returned in the item
668
	 * @return \Aimeos\MShop\Order\Item\Iface $item Updated item including the generated ID
669
	 */
670
	protected function saveItem( \Aimeos\MShop\Order\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Order\Item\Iface
671
	{
672
		if( !$item->isModified() ) {
673
			return $this->saveBasket( $item );
674
		}
675
676
		if( empty( $item->getInvoiceNumber() ) && $item->getStatusPayment() >= \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED )
677
		{
678
			try {
679
				$item->setInvoiceNumber( $this->createInvoiceNumber( $item ) );
680
			} catch( \Exception $e ) { // redo on transaction deadlock
681
				$item->setInvoiceNumber( $this->createInvoiceNumber( $item ) );
682
			}
683
		}
684
685
		$context = $this->context();
686
		$conn = $context->db( $this->getResourceName() );
687
688
		$id = $item->getId();
689
		$date = date( 'Y-m-d H:i:s' );
690
		$columns = $this->object()->getSaveAttributes();
691
692
		if( $id === null )
693
		{
694
			/** mshop/order/manager/insert/mysql
695
			 * Inserts a new order record into the database table
696
			 *
697
			 * @see mshop/order/manager/insert/ansi
698
			 */
699
700
			/** mshop/order/manager/insert/ansi
701
			 * Inserts a new order record into the database table
702
			 *
703
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
704
			 * the database and the newly created ID retrieved afterwards
705
			 * using the "newid" SQL statement.
706
			 *
707
			 * The SQL statement must be a string suitable for being used as
708
			 * prepared statement. It must include question marks for binding
709
			 * the values from the order item to the statement before they are
710
			 * sent to the database server. The number of question marks must
711
			 * be the same as the number of columns listed in the INSERT
712
			 * statement. The order of the columns must correspond to the
713
			 * order in the save() method, so the correct values are
714
			 * bound to the columns.
715
			 *
716
			 * The SQL statement should conform to the ANSI standard to be
717
			 * compatible with most relational database systems. This also
718
			 * includes using double quotes for table and column names.
719
			 *
720
			 * @param string SQL statement for inserting records
721
			 * @since 2014.03
722
			 * @category Developer
723
			 * @see mshop/order/manager/update/ansi
724
			 * @see mshop/order/manager/newid/ansi
725
			 * @see mshop/order/manager/delete/ansi
726
			 * @see mshop/order/manager/search/ansi
727
			 * @see mshop/order/manager/count/ansi
728
			 */
729
			$path = 'mshop/order/manager/insert';
730
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig($path) can also be of type array; however, parameter $sql of Aimeos\MShop\Common\Manager\Base::addSqlColumns() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

730
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
731
		}
732
		else
733
		{
734
			/** mshop/order/manager/update/mysql
735
			 * Updates an existing order record in the database
736
			 *
737
			 * @see mshop/order/manager/update/ansi
738
			 */
739
740
			/** mshop/order/manager/update/ansi
741
			 * Updates an existing order record in the database
742
			 *
743
			 * Items which already have an ID (i.e. the ID is not NULL) will
744
			 * be updated in the database.
745
			 *
746
			 * The SQL statement must be a string suitable for being used as
747
			 * prepared statement. It must include question marks for binding
748
			 * the values from the order item to the statement before they are
749
			 * sent to the database server. The order of the columns must
750
			 * correspond to the order in the save() method, so the
751
			 * correct values are bound to the columns.
752
			 *
753
			 * The SQL statement should conform to the ANSI standard to be
754
			 * compatible with most relational database systems. This also
755
			 * includes using double quotes for table and column names.
756
			 *
757
			 * @param string SQL statement for updating records
758
			 * @since 2014.03
759
			 * @category Developer
760
			 * @see mshop/order/manager/insert/ansi
761
			 * @see mshop/order/manager/newid/ansi
762
			 * @see mshop/order/manager/delete/ansi
763
			 * @see mshop/order/manager/search/ansi
764
			 * @see mshop/order/manager/count/ansi
765
			 */
766
			$path = 'mshop/order/manager/update';
767
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
768
		}
769
770
		$idx = 1;
771
		$priceItem = $item->getPrice();
772
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
773
774
		foreach( $columns as $name => $entry ) {
775
			$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
776
		}
777
778
		$stmt->bind( $idx++, $item->getInvoiceNumber() );
779
		$stmt->bind( $idx++, $item->getChannel() );
780
		$stmt->bind( $idx++, $item->getDatePayment() );
781
		$stmt->bind( $idx++, $item->getDateDelivery() );
782
		$stmt->bind( $idx++, $item->getStatusDelivery(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
783
		$stmt->bind( $idx++, $item->getStatusPayment(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
784
		$stmt->bind( $idx++, $item->getRelatedId(), \Aimeos\Base\DB\Statement\Base::PARAM_STR );
785
		$stmt->bind( $idx++, $item->getCustomerId() );
786
		$stmt->bind( $idx++, $context->locale()->getSiteItem()->getCode() );
787
		$stmt->bind( $idx++, $item->locale()->getLanguageId() );
788
		$stmt->bind( $idx++, $priceItem->getCurrencyId() );
789
		$stmt->bind( $idx++, $priceItem->getValue() );
790
		$stmt->bind( $idx++, $priceItem->getCosts() );
791
		$stmt->bind( $idx++, $priceItem->getRebate() );
792
		$stmt->bind( $idx++, $priceItem->getTaxValue() );
793
		$stmt->bind( $idx++, $priceItem->getTaxFlag(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
794
		$stmt->bind( $idx++, $item->getCustomerReference() );
795
		$stmt->bind( $idx++, $item->getComment() );
796
		$stmt->bind( $idx++, $date ); // mtime
797
		$stmt->bind( $idx++, $context->editor() );
798
799
		if( $id !== null ) {
800
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
801
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
802
		} else {
803
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
804
			$stmt->bind( $idx++, $date ); // ctime
805
			$stmt->bind( $idx++, date( 'Y-m-d' ) ); // cdate
806
			$stmt->bind( $idx++, date( 'Y-m' ) ); // cmonth
807
			$stmt->bind( $idx++, date( 'Y-W' ) ); // cweek
808
			$stmt->bind( $idx++, date( 'w' ) ); // cwday
809
			$stmt->bind( $idx++, date( 'H' ) ); // chour
810
		}
811
812
		$stmt->execute()->finish();
813
814
		if( $id === null && $fetch === true )
815
		{
816
			/** mshop/order/manager/newid/mysql
817
			 * Retrieves the ID generated by the database when inserting a new record
818
			 *
819
			 * @see mshop/order/manager/newid/ansi
820
			 */
821
822
			/** mshop/order/manager/newid/ansi
823
			 * Retrieves the ID generated by the database when inserting a new record
824
			 *
825
			 * As soon as a new record is inserted into the database table,
826
			 * the database server generates a new and unique identifier for
827
			 * that record. This ID can be used for retrieving, updating and
828
			 * deleting that specific record from the table again.
829
			 *
830
			 * For MySQL:
831
			 *  SELECT LAST_INSERT_ID()
832
			 * For PostgreSQL:
833
			 *  SELECT currval('seq_mord_id')
834
			 * For SQL Server:
835
			 *  SELECT SCOPE_IDENTITY()
836
			 * For Oracle:
837
			 *  SELECT "seq_mord_id".CURRVAL FROM DUAL
838
			 *
839
			 * There's no way to retrive the new ID by a SQL statements that
840
			 * fits for most database servers as they implement their own
841
			 * specific way.
842
			 *
843
			 * @param string SQL statement for retrieving the last inserted record ID
844
			 * @since 2014.03
845
			 * @category Developer
846
			 * @see mshop/order/manager/insert/ansi
847
			 * @see mshop/order/manager/update/ansi
848
			 * @see mshop/order/manager/delete/ansi
849
			 * @see mshop/order/manager/search/ansi
850
			 * @see mshop/order/manager/count/ansi
851
			 */
852
			$path = 'mshop/order/manager/newid';
853
			$id = $this->newId( $conn, $path );
854
		}
855
856
		$item->setId( $id );
857
858
		$this->addStatus( $item );
859
860
		return $this->saveBasket( $item );
861
	}
862
863
864
	/**
865
	 * Searches for orders based on the given criteria.
866
	 *
867
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
868
	 * @param string[] $ref List of domains to fetch list items and referenced items for
869
	 * @param int|null &$total Number of items that are available in total
870
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Iface with ids as keys
871
	 */
872
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
873
	{
874
		$context = $this->context();
875
		$conn = $context->db( $this->getResourceName() );
876
877
		$priceManager = \Aimeos\MShop::create( $context, 'price' );
878
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
879
880
		$map = $custItems = [];
881
		$required = ['order'];
882
883
		/** mshop/order/manager/sitemode
884
		 * Mode how items from levels below or above in the site tree are handled
885
		 *
886
		 * By default, only items from the current site are fetched from the
887
		 * storage. If the ai-sites extension is installed, you can create a
888
		 * tree of sites. Then, this setting allows you to define for the
889
		 * whole order domain if items from parent sites are inherited,
890
		 * sites from child sites are aggregated or both.
891
		 *
892
		 * Available constants for the site mode are:
893
		 * * 0 = only items from the current site
894
		 * * 1 = inherit items from parent sites
895
		 * * 2 = aggregate items from child sites
896
		 * * 3 = inherit and aggregate items at the same time
897
		 *
898
		 * You also need to set the mode in the locale manager
899
		 * (mshop/locale/manager/sitelevel) to one of the constants.
900
		 * If you set it to the same value, it will work as described but you
901
		 * can also use different modes. For example, if inheritance and
902
		 * aggregation is configured the locale manager but only inheritance
903
		 * in the domain manager because aggregating items makes no sense in
904
		 * this domain, then items wil be only inherited. Thus, you have full
905
		 * control over inheritance and aggregation in each domain.
906
		 *
907
		 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
908
		 * @category Developer
909
		 * @since 2018.01
910
		 * @see mshop/locale/manager/sitelevel
911
		 */
912
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
913
		$level = $context->config()->get( 'mshop/order/manager/sitemode', $level );
914
915
		/** mshop/order/manager/search/mysql
916
		 * Retrieves the records matched by the given criteria in the database
917
		 *
918
		 * @see mshop/order/manager/search/ansi
919
		 */
920
921
		/** mshop/order/manager/search/ansi
922
		 * Retrieves the records matched by the given criteria in the database
923
		 *
924
		 * Fetches the records matched by the given criteria from the order
925
		 * database. The records must be from one of the sites that are
926
		 * configured via the context item. If the current site is part of
927
		 * a tree of sites, the SELECT statement can retrieve all records
928
		 * from the current site and the complete sub-tree of sites.
929
		 *
930
		 * As the records can normally be limited by criteria from sub-managers,
931
		 * their tables must be joined in the SQL context. This is done by
932
		 * using the "internaldeps" property from the definition of the ID
933
		 * column of the sub-managers. These internal dependencies specify
934
		 * the JOIN between the tables and the used columns for joining. The
935
		 * ":joins" placeholder is then replaced by the JOIN strings from
936
		 * the sub-managers.
937
		 *
938
		 * To limit the records matched, conditions can be added to the given
939
		 * criteria object. It can contain comparisons like column names that
940
		 * must match specific values which can be combined by AND, OR or NOT
941
		 * operators. The resulting string of SQL conditions replaces the
942
		 * ":cond" placeholder before the statement is sent to the database
943
		 * server.
944
		 *
945
		 * If the records that are retrieved should be ordered by one or more
946
		 * columns, the generated string of column / sort direction pairs
947
		 * replaces the ":order" placeholder. In case no ordering is required,
948
		 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
949
		 * markers is removed to speed up retrieving the records. Columns of
950
		 * sub-managers can also be used for ordering the result set but then
951
		 * no index can be used.
952
		 *
953
		 * The number of returned records can be limited and can start at any
954
		 * number between the begining and the end of the result set. For that
955
		 * the ":size" and ":start" placeholders are replaced by the
956
		 * corresponding values from the criteria object. The default values
957
		 * are 0 for the start and 100 for the size value.
958
		 *
959
		 * The SQL statement should conform to the ANSI standard to be
960
		 * compatible with most relational database systems. This also
961
		 * includes using double quotes for table and column names.
962
		 *
963
		 * @param string SQL statement for searching items
964
		 * @since 2014.03
965
		 * @category Developer
966
		 * @see mshop/order/manager/insert/ansi
967
		 * @see mshop/order/manager/update/ansi
968
		 * @see mshop/order/manager/newid/ansi
969
		 * @see mshop/order/manager/delete/ansi
970
		 * @see mshop/order/manager/count/ansi
971
		 */
972
		$cfgPathSearch = 'mshop/order/manager/search';
973
974
		/** mshop/order/manager/count/mysql
975
		 * Counts the number of records matched by the given criteria in the database
976
		 *
977
		 * @see mshop/order/manager/count/ansi
978
		 */
979
980
		/** mshop/order/manager/count/ansi
981
		 * Counts the number of records matched by the given criteria in the database
982
		 *
983
		 * Counts all records matched by the given criteria from the order
984
		 * database. The records must be from one of the sites that are
985
		 * configured via the context item. If the current site is part of
986
		 * a tree of sites, the statement can count all records from the
987
		 * current site and the complete sub-tree of sites.
988
		 *
989
		 * As the records can normally be limited by criteria from sub-managers,
990
		 * their tables must be joined in the SQL context. This is done by
991
		 * using the "internaldeps" property from the definition of the ID
992
		 * column of the sub-managers. These internal dependencies specify
993
		 * the JOIN between the tables and the used columns for joining. The
994
		 * ":joins" placeholder is then replaced by the JOIN strings from
995
		 * the sub-managers.
996
		 *
997
		 * To limit the records matched, conditions can be added to the given
998
		 * criteria object. It can contain comparisons like column names that
999
		 * must match specific values which can be combined by AND, OR or NOT
1000
		 * operators. The resulting string of SQL conditions replaces the
1001
		 * ":cond" placeholder before the statement is sent to the database
1002
		 * server.
1003
		 *
1004
		 * Both, the strings for ":joins" and for ":cond" are the same as for
1005
		 * the "search" SQL statement.
1006
		 *
1007
		 * Contrary to the "search" statement, it doesn't return any records
1008
		 * but instead the number of records that have been found. As counting
1009
		 * thousands of records can be a long running task, the maximum number
1010
		 * of counted records is limited for performance reasons.
1011
		 *
1012
		 * The SQL statement should conform to the ANSI standard to be
1013
		 * compatible with most relational database systems. This also
1014
		 * includes using double quotes for table and column names.
1015
		 *
1016
		 * @param string SQL statement for counting items
1017
		 * @since 2014.03
1018
		 * @category Developer
1019
		 * @see mshop/order/manager/insert/ansi
1020
		 * @see mshop/order/manager/update/ansi
1021
		 * @see mshop/order/manager/newid/ansi
1022
		 * @see mshop/order/manager/delete/ansi
1023
		 * @see mshop/order/manager/search/ansi
1024
		 */
1025
		$cfgPathCount = 'mshop/order/manager/count';
1026
1027
		$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount,
1028
			$required, $total, $level );
1029
1030
		try
1031
		{
1032
			while( ( $row = $results->fetch() ) !== null ) {
1033
				$map[$row['order.id']] = $row;
1034
			}
1035
		}
1036
		catch( \Exception $e )
1037
		{
1038
			$results->finish();
1039
			throw $e;
1040
		}
1041
1042
1043
		if( ( isset( $ref['customer'] ) || in_array( 'customer', $ref ) )
1044
			&& !( $ids = map( $map )->col( 'order.customerid' )->filter() )->empty()
1045
		) {
1046
			$manager = \Aimeos\MShop::create( $context, 'customer' );
1047
			$search = $manager->filter()->slice( 0, count( $ids ) )->add( ['customer.id' => $ids] );
1048
			$custItems = $manager->search( $search, $ref )->all();
1049
		}
1050
1051
		foreach( $map as $id => $row )
1052
		{
1053
			// don't use fromArray() or set*() methods to avoid recalculation of tax value
1054
			$price = $priceManager->create( [
1055
				'price.currencyid' => $row['order.currencyid'],
1056
				'price.value' => $row['order.price'],
1057
				'price.costs' => $row['order.costs'],
1058
				'price.rebate' => $row['order.rebate'],
1059
				'price.taxflag' => $row['order.taxflag'],
1060
				'price.taxvalue' => $row['order.taxvalue'],
1061
			] );
1062
1063
			// you may need the site object! take care!
1064
			$localeItem = $localeManager->create( [
1065
				'locale.currencyid' => $row['order.currencyid'],
1066
				'locale.languageid' => $row['order.languageid'],
1067
				'locale.siteid' => $row['order.siteid'],
1068
			] );
1069
1070
			$map[$id] = [$price, $localeItem, $row, $custItems[$row['order.customerid'] ?? null] ?? null];
1071
		}
1072
1073
		return $this->buildItems( $map, $ref );
1074
	}
1075
1076
1077
	/**
1078
	 * Returns a new manager for order extensions
1079
	 *
1080
	 * @param string $manager Name of the sub manager type in lower case
1081
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
1082
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g base, etc.
1083
	 */
1084
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
1085
	{
1086
		return $this->getSubManagerBase( 'order', $manager, $name );
1087
	}
1088
1089
1090
	/**
1091
	 * Adds the new payment and delivery values to the order status log.
1092
	 *
1093
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item object
1094
	 */
1095
	protected function addStatus( \Aimeos\MShop\Order\Item\Iface $item )
1096
	{
1097
		$statusManager = \Aimeos\MShop::create( $this->context(), 'order/status' );
1098
1099
		$statusItem = $statusManager->create();
1100
		$statusItem->setParentId( $item->getId() );
1101
1102
		if( ( $status = $item->get( '.statuspayment' ) ) !== null && $status != $item->getStatusPayment() )
1103
		{
1104
			$statusItem->setId( null )->setValue( $item->getStatusPayment() )
1105
				->setType( \Aimeos\MShop\Order\Item\Status\Base::STATUS_PAYMENT );
1106
1107
			$statusManager->save( $statusItem, false );
1108
		}
1109
1110
		if( ( $status = $item->get( '.statusdelivery' ) ) !== null && $status != $item->getStatusDelivery() )
1111
		{
1112
			$statusItem->setId( null )->setValue( $item->getStatusDelivery() )
1113
				->setType( \Aimeos\MShop\Order\Item\Status\Base::STATUS_DELIVERY );
1114
1115
			$statusManager->save( $statusItem, false );
1116
		}
1117
	}
1118
1119
1120
	/**
1121
	 * Creates the order base item objects from the map and adds the referenced items
1122
	 *
1123
	 * @param array $map Associative list of order base IDs as keys and list of price/locale/row as values
1124
	 * @param string[] $ref Domain items that should be added as well, e.g.
1125
	 *	"order/address", "order/coupon", "order/product", "order/service"
1126
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Iface with IDs as keys
1127
	 */
1128
	protected function buildItems( array $map, array $ref ) : \Aimeos\Map
1129
	{
1130
		$items = [];
1131
		$baseIds = array_keys( $map );
1132
		$addressMap = $couponMap = $productMap = $serviceMap = [];
1133
1134
		if( in_array( 'order/address', $ref ) ) {
1135
			$addressMap = $this->getAddresses( $baseIds );
1136
		}
1137
1138
		if( in_array( 'order/product', $ref ) || in_array( 'order/coupon', $ref ) ) {
1139
			$productMap = $this->getProducts( $baseIds );
1140
		}
1141
1142
		if( in_array( 'order/coupon', $ref ) ) {
1143
			$couponMap = $this->getCoupons( $baseIds, $productMap );
1144
		}
1145
1146
		if( in_array( 'order/service', $ref ) ) {
1147
			$serviceMap = $this->getServices( $baseIds );
1148
		}
1149
1150
		foreach( $map as $id => $list )
1151
		{
1152
			list( $price, $locale, $row, $custItem ) = $list;
1153
1154
			$addresses = $addressMap[$id] ?? [];
1155
			$coupons = $couponMap[$id] ?? [];
1156
			$products = $productMap[$id] ?? [];
1157
			$services = $serviceMap[$id] ?? [];
1158
1159
			$item = $this->createItemBase( $price, $locale, $row, $products, $addresses, $services, $coupons, $custItem );
1160
1161
			if( $item = $this->applyFilter( $item ) ) {
1162
				$items[$id] = $item;
1163
			}
1164
		}
1165
1166
		return map( $items );
1167
	}
1168
1169
1170
	/**
1171
	 * Creates a new invoice number for the passed order and site.
1172
	 *
1173
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item with necessary values
1174
	 * @return string Unique invoice number for the current site
1175
	 */
1176
	public function createInvoiceNumber( \Aimeos\MShop\Order\Item\Iface $item ) : string
1177
	{
1178
		$context = $this->context();
1179
		$siteId = $context->locale()->getSiteId();
1180
		$conn = $context->db( 'db-locale', true );
1181
1182
		try
1183
		{
1184
			$conn->query( 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' )->finish();
1185
			$conn->query( 'START TRANSACTION' )->finish();
1186
1187
			$result = $conn->query( 'SELECT "invoiceno" FROM "mshop_locale_site" where "siteid" = ?', [$siteId] );
1188
			$row = $result->fetch();
1189
			$result->finish();
1190
1191
			$conn->create( 'UPDATE "mshop_locale_site" SET "invoiceno" = "invoiceno" + 1 WHERE "siteid" = ?' )
1192
				->bind( 1, $siteId )->execute()->finish();
1193
1194
			$conn->query( 'COMMIT' )->finish();
1195
		}
1196
		catch( \Exception $e )
1197
		{
1198
			$conn->close();
1199
			throw $e;
1200
		}
1201
1202
		return $row['invoiceno'] ?? '';
1203
	}
1204
1205
1206
	/**
1207
	 * Returns a new and empty order base item (shopping basket).
1208
	 *
1209
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
1210
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
1211
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
1212
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of ordered product items
1213
	 * @param \Aimeos\MShop\Order\Item\Address\Iface[] $addresses List of order address items
1214
	 * @param \Aimeos\MShop\Order\Item\Service\Iface[] $services List of order serviceitems
1215
	 * @param \Aimeos\MShop\Order\Item\Product\Iface[] $coupons Associative list of coupon codes as keys and items as values
1216
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object if requested
1217
	 * @return \Aimeos\MShop\Order\Item\Iface Order base object
1218
	 */
1219
	protected function createItemBase( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
1220
		array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
1221
		?\Aimeos\MShop\Customer\Item\Iface $custItem = null ) : \Aimeos\MShop\Order\Item\Iface
1222
	{
1223
		return new \Aimeos\MShop\Order\Item\Standard( $price, $locale,
1224
			$values, $products, $addresses, $services, $coupons, $custItem );
1225
	}
1226
1227
1228
	/**
1229
	 * Saves the modified basket content
1230
	 *
1231
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Basket content
1232
	 * @return \Aimeos\MShop\Order\Item\Iface Saved basket content
1233
	 */
1234
	protected function saveBasket( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Order\Item\Iface
1235
	{
1236
		$this->saveAddresses( $basket );
1237
		$this->saveServices( $basket );
1238
		$this->saveProducts( $basket );
1239
		$this->saveCoupons( $basket );
1240
1241
		return $basket;
1242
	}
1243
}
1244