Passed
Push — master ( 5b5d82...0b20e6 )
by Aimeos
05:27
created

Standard::search()   A

Complexity

Conditions 5
Paths 18

Size

Total Lines 154
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 154
rs 9.1768
c 0
b 0
f 0
cc 5
nc 18
nop 3

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\Service\Transaction;
13
14
15
/**
16
 * Order base service manager.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Order\Manager\Service\Transaction\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	private array $searchConfig = array(
26
		'order.service.transaction.id' => array(
27
			'code' => 'order.service.transaction.id',
28
			'internalcode' => 'mordsetx."id"',
29
			'internaldeps' => array( 'LEFT JOIN "mshop_order_service_tx" AS mordsetx ON ( mordse."id" = mordsetx."parentid" )' ),
30
			'label' => 'Service transaction ID',
31
			'type' => 'int',
32
			'public' => false,
33
		),
34
		'order.service.transaction.siteid' => array(
35
			'code' => 'order.service.transaction.siteid',
36
			'internalcode' => 'mordsetx."siteid"',
37
			'label' => 'Service transaction site ID',
38
			'public' => false,
39
		),
40
		'order.service.transaction.parentid' => array(
41
			'code' => 'order.service.transaction.parentid',
42
			'internalcode' => 'mordsetx."parentid"',
43
			'label' => 'Service ID',
44
			'type' => 'int',
45
			'public' => false,
46
		),
47
		'order.service.transaction.type' => array(
48
			'code' => 'order.service.transaction.type',
49
			'internalcode' => 'mordsetx."type"',
50
			'label' => 'Service transaction type',
51
		),
52
		'order.service.transaction.currencyid' => array(
53
			'code' => 'order.service.transaction.currencyid',
54
			'internalcode' => 'mordsetx."currencyid"',
55
			'label' => 'Service currencyid code',
56
		),
57
		'order.service.transaction.price' => array(
58
			'code' => 'order.service.transaction.price',
59
			'internalcode' => 'mordsetx."price"',
60
			'label' => 'Service price',
61
			'type' => 'decimal',
62
		),
63
		'order.service.transaction.costs' => array(
64
			'code' => 'order.service.transaction.costs',
65
			'internalcode' => 'mordsetx."costs"',
66
			'label' => 'Service shipping',
67
			'type' => 'decimal',
68
		),
69
		'order.service.transaction.rebate' => array(
70
			'code' => 'order.service.transaction.rebate',
71
			'internalcode' => 'mordsetx."rebate"',
72
			'label' => 'Service rebate',
73
			'type' => 'decimal',
74
		),
75
		'order.service.transaction.taxvalue' => array(
76
			'code' => 'order.service.transaction.taxvalue',
77
			'internalcode' => 'mordsetx."tax"',
78
			'label' => 'Service tax value',
79
			'type' => 'decimal',
80
		),
81
		'order.service.transaction.taxflag' => array(
82
			'code' => 'order.service.transaction.taxflag',
83
			'internalcode' => 'mordsetx."taxflag"',
84
			'label' => 'Service tax flag (0=net, 1=gross)',
85
			'type' => 'int',
86
		),
87
		'order.service.transaction.config' => array(
88
			'code' => 'order.service.transaction.config',
89
			'internalcode' => 'mordsetx."config"',
90
			'label' => 'Transaction data',
91
			'type' => 'json',
92
		),
93
		'order.service.transaction.status' => array(
94
			'code' => 'order.service.transaction.status',
95
			'internalcode' => 'mordsetx."status"',
96
			'label' => 'Transaction status',
97
			'type' => 'int',
98
		),
99
		'order.service.transaction.ctime' => array(
100
			'code' => 'order.service.transaction.ctime',
101
			'internalcode' => 'mordsetx."ctime"',
102
			'label' => 'Service transaction create date/time',
103
			'type' => 'datetime',
104
			'public' => false,
105
		),
106
		'order.service.transaction.mtime' => array(
107
			'code' => 'order.service.transaction.mtime',
108
			'internalcode' => 'mordsetx."mtime"',
109
			'label' => 'Service transaction modify date/time',
110
			'type' => 'datetime',
111
			'public' => false,
112
		),
113
		'order.service.transaction.editor' => array(
114
			'code' => 'order.service.transaction.editor',
115
			'internalcode' => 'mordsetx."editor"',
116
			'label' => 'Service transaction editor',
117
			'public' => false,
118
		),
119
	);
120
121
122
	/**
123
	 * Initializes the object.
124
	 *
125
	 * @param \Aimeos\MShop\ContextIface $context Context object
126
	 */
127
	public function __construct( \Aimeos\MShop\ContextIface $context )
128
	{
129
		parent::__construct( $context );
130
131
		/** mshop/order/manager/resource
132
		 * Name of the database connection resource to use
133
		 *
134
		 * You can configure a different database connection for each data domain
135
		 * and if no such connection name exists, the "db" connection will be used.
136
		 * It's also possible to use the same database connection for different
137
		 * data domains by configuring the same connection name using this setting.
138
		 *
139
		 * @param string Database connection name
140
		 * @since 2023.04
141
		 */
142
		$this->setResourceName( $context->config()->get( 'mshop/order/manager/resource', 'db-order' ) );
143
	}
144
145
146
	/**
147
	 * Counts the number items that are available for the values of the given key.
148
	 *
149
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria
150
	 * @param array|string $key Search key or list of keys to aggregate items for
151
	 * @param string|null $value Search key for aggregating the value column
152
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
153
	 * @return \Aimeos\Map List of the search keys as key and the number of counted items as value
154
	 */
155
	public function aggregate( \Aimeos\Base\Criteria\Iface $search, $key, string $value = null, string $type = null ) : \Aimeos\Map
156
	{
157
		/** mshop/order/manager/service/transaction/aggregate/mysql
158
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
159
		 *
160
		 * @see mshop/order/manager/service/transaction/aggregate/ansi
161
		 */
162
163
		/** mshop/order/manager/service/transaction/aggregate/ansi
164
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
165
		 *
166
		 * Groups all records by the values in the key column and counts their
167
		 * occurence. The matched records can be limited by the given criteria
168
		 * from the order database. The records must be from one of the sites
169
		 * that are configured via the context item. If the current site is part
170
		 * of a tree of sites, the statement can count all records from the
171
		 * current site and the complete sub-tree of sites.
172
		 *
173
		 * As the records can normally be limited by criteria from sub-managers,
174
		 * their tables must be joined in the SQL context. This is done by
175
		 * using the "internaldeps" property from the definition of the ID
176
		 * column of the sub-managers. These internal dependencies specify
177
		 * the JOIN between the tables and the used columns for joining. The
178
		 * ":joins" placeholder is then replaced by the JOIN strings from
179
		 * the sub-managers.
180
		 *
181
		 * To limit the records matched, conditions can be added to the given
182
		 * criteria object. It can contain comparisons like column names that
183
		 * must match specific values which can be combined by AND, OR or NOT
184
		 * operators. The resulting string of SQL conditions replaces the
185
		 * ":cond" placeholder before the statement is sent to the database
186
		 * server.
187
		 *
188
		 * This statement doesn't return any records. Instead, it returns pairs
189
		 * of the different values found in the key column together with the
190
		 * number of records that have been found for that key values.
191
		 *
192
		 * The SQL statement should conform to the ANSI standard to be
193
		 * compatible with most relational database systems. This also
194
		 * includes using double quotes for table and column names.
195
		 *
196
		 * @param string SQL statement for aggregating order items
197
		 * @since 2023.01
198
		 * @see mshop/order/manager/service/transaction/insert/ansi
199
		 * @see mshop/order/manager/service/transaction/update/ansi
200
		 * @see mshop/order/manager/service/transaction/newid/ansi
201
		 * @see mshop/order/manager/service/transaction/delete/ansi
202
		 * @see mshop/order/manager/service/transaction/search/ansi
203
		 * @see mshop/order/manager/service/transaction/count/ansi
204
		 */
205
		$cfgkey = 'mshop/order/manager/service/transaction/aggregate';
206
		return $this->aggregateBase( $search, $key, $cfgkey, ['order.service.transaction'], $value, $type );
207
	}
208
209
210
	/**
211
	 * Removes old entries from the storage.
212
	 *
213
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
214
	 * @return \Aimeos\MShop\Order\Manager\Service\Transaction\Iface Manager object for chaining method calls
215
	 */
216
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
217
	{
218
		$path = 'mshop/order/manager/service/transaction/submanagers';
219
		foreach( $this->context()->config()->get( $path, [] ) as $domain ) {
220
			$this->object()->getSubManager( $domain )->clear( $siteids );
221
		}
222
223
		return $this->clearBase( $siteids, 'mshop/order/manager/service/transaction/delete' );
224
	}
225
226
227
	/**
228
	 * Creates a new empty item instance
229
	 *
230
	 * @param array $values Values the item should be initialized with
231
	 * @return \Aimeos\MShop\Order\Item\Service\Transaction\Iface New order service transaction item object
232
	 */
233
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
234
	{
235
		$context = $this->context();
236
237
		$price = \Aimeos\MShop::create( $context, 'price' )->create();
238
		$values['order.service.transaction.siteid'] = $values['order.service.transaction.siteid'] ?? $context->locale()->getSiteId();
239
240
		return $this->createItemBase( $price, $values );
241
	}
242
243
244
	/**
245
	 * Removes multiple items.
246
	 *
247
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
248
	 * @return \Aimeos\MShop\Order\Manager\Service\Transaction\Iface Manager object for chaining method calls
249
	 */
250
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
251
	{
252
		/** mshop/order/manager/service/transaction/delete/mysql
253
		 * Deletes the items matched by the given IDs from the database
254
		 *
255
		 * @see mshop/order/manager/service/transaction/delete/ansi
256
		 */
257
258
		/** mshop/order/manager/service/transaction/delete/ansi
259
		 * Deletes the items matched by the given IDs from the database
260
		 *
261
		 * Removes the records specified by the given IDs from the order database.
262
		 * The records must be from the site that is configured via the
263
		 * context item.
264
		 *
265
		 * The ":cond" placeholder is replaced by the name of the ID column and
266
		 * the given ID or list of IDs while the site ID is bound to the question
267
		 * mark.
268
		 *
269
		 * The SQL statement should conform to the ANSI standard to be
270
		 * compatible with most relational database systems. This also
271
		 * includes using double quotes for table and column names.
272
		 *
273
		 * @param string SQL statement for deleting items
274
		 * @since 2023.01
275
		 * @see mshop/order/manager/service/transaction/insert/ansi
276
		 * @see mshop/order/manager/service/transaction/update/ansi
277
		 * @see mshop/order/manager/service/transaction/newid/ansi
278
		 * @see mshop/order/manager/service/transaction/search/ansi
279
		 * @see mshop/order/manager/service/transaction/count/ansi
280
		 */
281
		$path = 'mshop/order/manager/service/transaction/delete';
282
283
		return $this->deleteItemsBase( $itemIds, $path );
284
	}
285
286
287
	/**
288
	 * Returns the transaction object for the given ID.
289
	 *
290
	 * @param string $id Order service transaction ID
291
	 * @param string[] $ref List of domains to fetch list items and referenced items for
292
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
293
	 * @return \Aimeos\MShop\Order\Item\Service\Transaction\Iface Order base service transaction item of the given ID
294
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
295
	 */
296
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
297
	{
298
		return $this->getItemBase( 'order.service.transaction.id', $id, $ref, $default );
299
	}
300
301
302
	/**
303
	 * Returns the available manager types
304
	 *
305
	 * @param bool $withsub Return also the resource type of sub-managers if true
306
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
307
	 */
308
	public function getResourceType( bool $withsub = true ) : array
309
	{
310
		$path = 'mshop/order/manager/service/transaction/submanagers';
311
		return $this->getResourceTypeBase( 'order/service/transaction', $path, [], $withsub );
312
	}
313
314
315
	/**
316
	 * Returns the transactions that can be used for searching.
317
	 *
318
	 * @param bool $withsub Return also transactions of sub-managers if true
319
	 * @return \Aimeos\Base\Criteria\Transaction\Iface[] List of search transaction items
320
	 */
321
	public function getSearchAttributes( bool $withsub = true ) : array
322
	{
323
		/** mshop/order/manager/service/transaction/submanagers
324
		 * List of manager names that can be instantiated by the order base service transaction manager
325
		 *
326
		 * Managers provide a generic interface to the underlying storage.
327
		 * Each manager has or can have sub-managers caring about particular
328
		 * aspects. Each of these sub-managers can be instantiated by its
329
		 * parent manager using the getSubManager() method.
330
		 *
331
		 * The search keys from sub-managers can be normally used in the
332
		 * manager as well. It allows you to search for items of the manager
333
		 * using the search keys of the sub-managers to further limit the
334
		 * retrieved list of items.
335
		 *
336
		 * @param array List of sub-manager names
337
		 * @since 2023.01
338
		 */
339
		$path = 'mshop/order/manager/service/transaction/submanagers';
340
341
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
342
	}
343
344
345
	/**
346
	 * Returns a new manager for order service extensions.
347
	 *
348
	 * @param string $manager Name of the sub manager type in lower case
349
	 * @param string|null $name Name of the implementation (from configuration or "Standard" if null)
350
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g transaction
351
	 */
352
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
353
	{
354
		/** mshop/order/manager/service/transaction/name
355
		 * Class name of the used order base service transaction manager implementation
356
		 *
357
		 * Each default order base service transaction manager can be replaced by an alternative imlementation.
358
		 * To use this implementation, you have to set the last part of the class
359
		 * name as configuration value so the manager factory knows which class it
360
		 * has to instantiate.
361
		 *
362
		 * For example, if the name of the default class is
363
		 *
364
		 *  \Aimeos\MShop\Order\Manager\Service\Transaction\Standard
365
		 *
366
		 * and you want to replace it with your own version named
367
		 *
368
		 *  \Aimeos\MShop\Order\Manager\Service\Transaction\Mytransaction
369
		 *
370
		 * then you have to set the this configuration option:
371
		 *
372
		 *  mshop/order/manager/service/transaction/name = Mytransaction
373
		 *
374
		 * The value is the last part of your own class name and it's case sensitive,
375
		 * so take care that the configuration value is exactly named like the last
376
		 * part of the class name.
377
		 *
378
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
379
		 * characters are possible! You should always start the last part of the class
380
		 * name with an upper case character and continue only with lower case characters
381
		 * or numbers. Avoid chamel case names like "MyTransaction"!
382
		 *
383
		 * @param string Last part of the class name
384
		 * @since 2023.01
385
		 */
386
387
		/** mshop/order/manager/service/transaction/decorators/excludes
388
		 * Excludes decorators added by the "common" option from the order base service transaction manager
389
		 *
390
		 * Decorators extend the functionality of a class by adding new aspects
391
		 * (e.g. log what is currently done), executing the methods of the underlying
392
		 * class only in certain conditions (e.g. only for logged in users) or
393
		 * modify what is returned to the caller.
394
		 *
395
		 * This option allows you to remove a decorator added via
396
		 * "mshop/common/manager/decorators/default" before they are wrapped
397
		 * around the order base service transaction manager.
398
		 *
399
		 *  mshop/order/manager/service/transaction/decorators/excludes = array( 'decorator1' )
400
		 *
401
		 * This would remove the decorator named "decorator1" from the list of
402
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
403
		 * "mshop/common/manager/decorators/default" for the order base service transaction manager.
404
		 *
405
		 * @param array List of decorator names
406
		 * @since 2023.01
407
		 * @see mshop/common/manager/decorators/default
408
		 * @see mshop/order/manager/service/transaction/decorators/global
409
		 * @see mshop/order/manager/service/transaction/decorators/local
410
		 */
411
412
		/** mshop/order/manager/service/transaction/decorators/global
413
		 * Adds a list of globally available decorators only to the order base service transaction manager
414
		 *
415
		 * Decorators extend the functionality of a class by adding new aspects
416
		 * (e.g. log what is currently done), executing the methods of the underlying
417
		 * class only in certain conditions (e.g. only for logged in users) or
418
		 * modify what is returned to the caller.
419
		 *
420
		 * This option allows you to wrap global decorators
421
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the order base
422
		 * service transaction manager.
423
		 *
424
		 *  mshop/order/manager/service/transaction/decorators/global = array( 'decorator1' )
425
		 *
426
		 * This would add the decorator named "decorator1" defined by
427
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the order
428
		 * base service transaction manager.
429
		 *
430
		 * @param array List of decorator names
431
		 * @since 2023.01
432
		 * @see mshop/common/manager/decorators/default
433
		 * @see mshop/order/manager/service/transaction/decorators/excludes
434
		 * @see mshop/order/manager/service/transaction/decorators/local
435
		 */
436
437
		/** mshop/order/manager/service/transaction/decorators/local
438
		 * Adds a list of local decorators only to the order base service transaction manager
439
		 *
440
		 * Decorators extend the functionality of a class by adding new aspects
441
		 * (e.g. log what is currently done), executing the methods of the underlying
442
		 * class only in certain conditions (e.g. only for logged in users) or
443
		 * modify what is returned to the caller.
444
		 *
445
		 * This option allows you to wrap local decorators
446
		 * ("\Aimeos\MShop\Order\Manager\Service\Transaction\Decorator\*")
447
		 * around the order base service transaction manager.
448
		 *
449
		 *  mshop/order/manager/service/transaction/decorators/local = array( 'decorator2' )
450
		 *
451
		 * This would add the decorator named "decorator2" defined by
452
		 * "\Aimeos\MShop\Order\Manager\Service\Transaction\Decorator\Decorator2"
453
		 * only to the order base service transaction manager.
454
		 *
455
		 * @param array List of decorator names
456
		 * @since 2023.01
457
		 * @see mshop/common/manager/decorators/default
458
		 * @see mshop/order/manager/service/transaction/decorators/excludes
459
		 * @see mshop/order/manager/service/transaction/decorators/global
460
		 */
461
462
		return $this->getSubManagerBase( 'order', 'service/transaction/' . $manager, $name );
463
	}
464
465
466
	/**
467
	 * Adds or updates an order service transaction item to the storage.
468
	 *
469
	 * @param \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item Order service transaction object
470
	 * @param bool $fetch True if the new ID should be returned in the item
471
	 * @return \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item Updated item including the generated ID
472
	 */
473
	protected function saveItem( \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Order\Item\Service\Transaction\Iface
474
	{
475
		if( !$item->isModified() ) {
476
			return $item;
477
		}
478
479
		$context = $this->context();
480
		$conn = $context->db( $this->getResourceName() );
481
482
		$id = $item->getId();
483
		$date = date( 'Y-m-d H:i:s' );
484
		$columns = $this->object()->getSaveAttributes();
485
486
		if( $id === null )
487
		{
488
			/** mshop/order/manager/service/transaction/insert/mysql
489
			 * Inserts a new order record into the database table
490
			 *
491
			 * @see mshop/order/manager/service/transaction/insert/ansi
492
			 */
493
494
			/** mshop/order/manager/service/transaction/insert/ansi
495
			 * Inserts a new order record into the database table
496
			 *
497
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
498
			 * the database and the newly created ID retrieved afterwards
499
			 * using the "newid" SQL statement.
500
			 *
501
			 * The SQL statement must be a string suitable for being used as
502
			 * prepared statement. It must include question marks for binding
503
			 * the values from the order item to the statement before they are
504
			 * sent to the database server. The number of question marks must
505
			 * be the same as the number of columns listed in the INSERT
506
			 * statement. The order of the columns must correspond to the
507
			 * order in the save() method, so the correct values are
508
			 * bound to the columns.
509
			 *
510
			 * The SQL statement should conform to the ANSI standard to be
511
			 * compatible with most relational database systems. This also
512
			 * includes using double quotes for table and column names.
513
			 *
514
			 * @param string SQL statement for inserting records
515
			 * @since 2023.01
516
			 * @see mshop/order/manager/service/transaction/update/ansi
517
			 * @see mshop/order/manager/service/transaction/newid/ansi
518
			 * @see mshop/order/manager/service/transaction/delete/ansi
519
			 * @see mshop/order/manager/service/transaction/search/ansi
520
			 * @see mshop/order/manager/service/transaction/count/ansi
521
			 */
522
			$path = 'mshop/order/manager/service/transaction/insert';
523
			$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

523
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
524
		}
525
		else
526
		{
527
			/** mshop/order/manager/service/transaction/update/mysql
528
			 * Updates an existing order record in the database
529
			 *
530
			 * @see mshop/order/manager/service/transaction/update/ansi
531
			 */
532
533
			/** mshop/order/manager/service/transaction/update/ansi
534
			 * Updates an existing order record in the database
535
			 *
536
			 * Items which already have an ID (i.e. the ID is not NULL) will
537
			 * be updated in the database.
538
			 *
539
			 * The SQL statement must be a string suitable for being used as
540
			 * prepared statement. It must include question marks for binding
541
			 * the values from the order item to the statement before they are
542
			 * sent to the database server. The order of the columns must
543
			 * correspond to the order in the save() method, so the
544
			 * correct values are bound to the columns.
545
			 *
546
			 * The SQL statement should conform to the ANSI standard to be
547
			 * compatible with most relational database systems. This also
548
			 * includes using double quotes for table and column names.
549
			 *
550
			 * @param string SQL statement for updating records
551
			 * @since 2023.01
552
			 * @see mshop/order/manager/service/transaction/insert/ansi
553
			 * @see mshop/order/manager/service/transaction/newid/ansi
554
			 * @see mshop/order/manager/service/transaction/delete/ansi
555
			 * @see mshop/order/manager/service/transaction/search/ansi
556
			 * @see mshop/order/manager/service/transaction/count/ansi
557
			 */
558
			$path = 'mshop/order/manager/service/transaction/update';
559
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
560
		}
561
562
		$idx = 1;
563
		$price = $item->getPrice();
564
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
565
566
		foreach( $columns as $name => $entry ) {
567
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
568
		}
569
570
		$stmt->bind( $idx++, $item->getParentId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
571
		$stmt->bind( $idx++, $item->getType() );
572
		$stmt->bind( $idx++, $price->getCurrencyId() );
573
		$stmt->bind( $idx++, $price->getValue() );
574
		$stmt->bind( $idx++, $price->getCosts() );
575
		$stmt->bind( $idx++, $price->getRebate() );
576
		$stmt->bind( $idx++, $price->getTaxValue() );
577
		$stmt->bind( $idx++, $price->getTaxFlag(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
578
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
579
		$stmt->bind( $idx++, json_encode( $item->getConfig(), JSON_FORCE_OBJECT ) );
580
		$stmt->bind( $idx++, $date ); // mtime
581
		$stmt->bind( $idx++, $context->editor() );
582
583
		if( $id !== null ) {
584
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
585
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
586
		} else {
587
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
588
			$stmt->bind( $idx++, $date ); // ctime
589
		}
590
591
		$stmt->execute()->finish();
592
593
		if( $id === null && $fetch === true )
594
		{
595
			/** mshop/order/manager/service/transaction/newid/mysql
596
			 * Retrieves the ID generated by the database when inserting a new record
597
			 *
598
			 * @see mshop/order/manager/service/transaction/newid/ansi
599
			 */
600
601
			/** mshop/order/manager/service/transaction/newid/ansi
602
			 * Retrieves the ID generated by the database when inserting a new record
603
			 *
604
			 * As soon as a new record is inserted into the database table,
605
			 * the database server generates a new and unique identifier for
606
			 * that record. This ID can be used for retrieving, updating and
607
			 * deleting that specific record from the table again.
608
			 *
609
			 * For MySQL:
610
			 *  SELECT LAST_INSERT_ID()
611
			 * For PostgreSQL:
612
			 *  SELECT currval('seq_mord_id')
613
			 * For SQL Server:
614
			 *  SELECT SCOPE_IDENTITY()
615
			 * For Oracle:
616
			 *  SELECT "seq_mord_id".CURRVAL FROM DUAL
617
			 *
618
			 * There's no way to retrive the new ID by a SQL statements that
619
			 * fits for most database servers as they implement their own
620
			 * specific way.
621
			 *
622
			 * @param string SQL statement for retrieving the last inserted record ID
623
			 * @since 2023.01
624
			 * @see mshop/order/manager/service/transaction/insert/ansi
625
			 * @see mshop/order/manager/service/transaction/update/ansi
626
			 * @see mshop/order/manager/service/transaction/delete/ansi
627
			 * @see mshop/order/manager/service/transaction/search/ansi
628
			 * @see mshop/order/manager/service/transaction/count/ansi
629
			 */
630
			$path = 'mshop/order/manager/service/transaction/newid';
631
			$id = $this->newId( $conn, $path );
632
		}
633
634
		$item->setId( $id );
635
636
		return $item;
637
	}
638
639
640
	/**
641
	 * Searches for order service transaction items based on the given criteria.
642
	 *
643
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
644
	 * @param string[] $ref List of domains to fetch list items and referenced items for
645
	 * @param int|null &$total Number of items that are available in total
646
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Service\Transaction\Iface with ids as keys
647
	 */
648
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
649
	{
650
		$context = $this->context();
651
		$priceManager = \Aimeos\MShop::create( $context, 'price' );
652
653
		$items = [];
654
		$conn = $context->db( $this->getResourceName() );
655
		$required = array( 'order.service.transaction' );
656
657
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
658
		$level = $context->config()->get( 'mshop/order/manager/sitemode', $level );
659
660
		/** mshop/order/manager/service/transaction/search/mysql
661
		 * Retrieves the records matched by the given criteria in the database
662
		 *
663
		 * @see mshop/order/manager/service/transaction/search/ansi
664
		 */
665
666
		/** mshop/order/manager/service/transaction/search/ansi
667
		 * Retrieves the records matched by the given criteria in the database
668
		 *
669
		 * Fetches the records matched by the given criteria from the order
670
		 * database. The records must be from one of the sites that are
671
		 * configured via the context item. If the current site is part of
672
		 * a tree of sites, the SELECT statement can retrieve all records
673
		 * from the current site and the complete sub-tree of sites.
674
		 *
675
		 * As the records can normally be limited by criteria from sub-managers,
676
		 * their tables must be joined in the SQL context. This is done by
677
		 * using the "internaldeps" property from the definition of the ID
678
		 * column of the sub-managers. These internal dependencies specify
679
		 * the JOIN between the tables and the used columns for joining. The
680
		 * ":joins" placeholder is then replaced by the JOIN strings from
681
		 * the sub-managers.
682
		 *
683
		 * To limit the records matched, conditions can be added to the given
684
		 * criteria object. It can contain comparisons like column names that
685
		 * must match specific values which can be combined by AND, OR or NOT
686
		 * operators. The resulting string of SQL conditions replaces the
687
		 * ":cond" placeholder before the statement is sent to the database
688
		 * server.
689
		 *
690
		 * If the records that are retrieved should be ordered by one or more
691
		 * columns, the generated string of column / sort direction pairs
692
		 * replaces the ":order" placeholder. In case no ordering is required,
693
		 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
694
		 * markers is removed to speed up retrieving the records. Columns of
695
		 * sub-managers can also be used for ordering the result set but then
696
		 * no index can be used.
697
		 *
698
		 * The number of returned records can be limited and can start at any
699
		 * number between the begining and the end of the result set. For that
700
		 * the ":size" and ":start" placeholders are replaced by the
701
		 * corresponding values from the criteria object. The default values
702
		 * are 0 for the start and 100 for the size value.
703
		 *
704
		 * The SQL statement should conform to the ANSI standard to be
705
		 * compatible with most relational database systems. This also
706
		 * includes using double quotes for table and column names.
707
		 *
708
		 * @param string SQL statement for searching items
709
		 * @since 2023.01
710
		 * @see mshop/order/manager/service/transaction/insert/ansi
711
		 * @see mshop/order/manager/service/transaction/update/ansi
712
		 * @see mshop/order/manager/service/transaction/newid/ansi
713
		 * @see mshop/order/manager/service/transaction/delete/ansi
714
		 * @see mshop/order/manager/service/transaction/count/ansi
715
		 */
716
		$cfgPathSearch = 'mshop/order/manager/service/transaction/search';
717
718
		/** mshop/order/manager/service/transaction/count/mysql
719
		 * Counts the number of records matched by the given criteria in the database
720
		 *
721
		 * @see mshop/order/manager/service/transaction/count/ansi
722
		 */
723
724
		/** mshop/order/manager/service/transaction/count/ansi
725
		 * Counts the number of records matched by the given criteria in the database
726
		 *
727
		 * Counts all records matched by the given criteria from the order
728
		 * database. The records must be from one of the sites that are
729
		 * configured via the context item. If the current site is part of
730
		 * a tree of sites, the statement can count all records from the
731
		 * current site and the complete sub-tree of sites.
732
		 *
733
		 * As the records can normally be limited by criteria from sub-managers,
734
		 * their tables must be joined in the SQL context. This is done by
735
		 * using the "internaldeps" property from the definition of the ID
736
		 * column of the sub-managers. These internal dependencies specify
737
		 * the JOIN between the tables and the used columns for joining. The
738
		 * ":joins" placeholder is then replaced by the JOIN strings from
739
		 * the sub-managers.
740
		 *
741
		 * To limit the records matched, conditions can be added to the given
742
		 * criteria object. It can contain comparisons like column names that
743
		 * must match specific values which can be combined by AND, OR or NOT
744
		 * operators. The resulting string of SQL conditions replaces the
745
		 * ":cond" placeholder before the statement is sent to the database
746
		 * server.
747
		 *
748
		 * Both, the strings for ":joins" and for ":cond" are the same as for
749
		 * the "search" SQL statement.
750
		 *
751
		 * Contrary to the "search" statement, it doesn't return any records
752
		 * but instead the number of records that have been found. As counting
753
		 * thousands of records can be a long running task, the maximum number
754
		 * of counted records is limited for performance reasons.
755
		 *
756
		 * The SQL statement should conform to the ANSI standard to be
757
		 * compatible with most relational database systems. This also
758
		 * includes using double quotes for table and column names.
759
		 *
760
		 * @param string SQL statement for counting items
761
		 * @since 2023.01
762
		 * @see mshop/order/manager/service/transaction/insert/ansi
763
		 * @see mshop/order/manager/service/transaction/update/ansi
764
		 * @see mshop/order/manager/service/transaction/newid/ansi
765
		 * @see mshop/order/manager/service/transaction/delete/ansi
766
		 * @see mshop/order/manager/service/transaction/search/ansi
767
		 */
768
		$cfgPathCount = 'mshop/order/manager/service/transaction/count';
769
770
		$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
771
772
		try
773
		{
774
			while( $row = $results->fetch() )
775
			{
776
				if( ( $row['order.service.transaction.config'] = json_decode( $row['order.service.transaction.config'], true ) ) === null ) {
777
					$row['order.service.transaction.config'] = [];
778
				}
779
780
				// don't use fromArray() or set*() methods to avoid recalculation of tax value
781
				$price = $priceManager->create( [
782
					'price.currencyid' => $row['order.service.transaction.currencyid'],
783
					'price.value' => $row['order.service.transaction.price'],
784
					'price.costs' => $row['order.service.transaction.costs'],
785
					'price.rebate' => $row['order.service.transaction.rebate'],
786
					'price.taxflag' => $row['order.service.transaction.taxflag'],
787
					'price.taxvalue' => $row['order.service.transaction.taxvalue'],
788
				] );
789
790
				if( $item = $this->applyFilter( $this->createItemBase( $price, $row ) ) ) {
791
					$items[$row['order.service.transaction.id']] = $item;
792
				}
793
			}
794
		}
795
		catch( \Exception $e )
796
		{
797
			$results->finish();
798
			throw $e;
799
		}
800
801
		return map( $items );
802
	}
803
804
805
	/**
806
	 * Creates a new order service transaction item object initialized with given parameters.
807
	 *
808
	 * @param \Aimeos\MShop\Price\Item\Iface $price Transaction price item
809
	 * @param array $values Associative list of order service transaction key/value pairs
810
	 * @return \Aimeos\MShop\Order\Item\Service\Transaction\Standard New order service transaction item
811
	 */
812
	protected function createItemBase( \Aimeos\MShop\Price\Item\Iface $price, array $values = []
813
		) : \Aimeos\MShop\Order\Item\Service\Transaction\Iface
814
	{
815
		return new \Aimeos\MShop\Order\Item\Service\Transaction\Standard( $price, $values );
816
	}
817
}
818