Passed
Push — master ( 26e563...322e4f )
by Aimeos
05:33
created

Standard::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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