Passed
Push — master ( 51d5ad...84139c )
by Aimeos
04:55
created

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Stock
8
 */
9
10
11
namespace Aimeos\MShop\Stock\Manager;
12
13
14
/**
15
 * Default stock manager implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Stock
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Stock\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private array $searchConfig = [
25
		'stock.type' => [
26
			'label' => 'Type',
27
			'internalcode' => 'type',
28
		],
29
		'stock.productid' => [
30
			'label' => 'Product ID',
31
			'internalcode' => 'prodid',
32
		],
33
		'stock.stocklevel' => [
34
			'label' => 'Stock level',
35
			'internalcode' => 'stocklevel',
36
			'type' => 'float',
37
		],
38
		'stock.dateback' => [
39
			'label' => 'Back in stock date/time',
40
			'internalcode' => 'backdate',
41
			'type' => 'datetime',
42
		],
43
		'stock.timeframe' => [
44
			'label' => 'Delivery time frame',
45
			'internalcode' => 'timeframe',
46
		],
47
	];
48
49
50
	/**
51
	 * Creates a new empty item instance
52
	 *
53
	 * @param array $values Values the item should be initialized with
54
	 * @return \Aimeos\MShop\Stock\Item\Iface New stock item object
55
	 */
56
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
57
	{
58
		$values['stock.siteid'] = $values['stock.siteid'] ?? $this->context()->locale()->getSiteId();
59
		return new \Aimeos\MShop\Stock\Item\Standard( 'stock.', $values );
60
	}
61
62
63
64
	/**
65
	 * Decreases the stock level for the given product codes/quantity pairs and type
66
	 *
67
	 * @param array $pairs Associative list of product codes as keys and quantities as values
68
	 * @param string $type Unique code of the stock type
69
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
70
	 */
71
	public function decrease( iterable $pairs, string $type = 'default' ) : \Aimeos\MShop\Stock\Manager\Iface
72
	{
73
		$context = $this->context();
74
		$translations = ['stock.siteid' => '"siteid"'];
75
		$types = ['stock.siteid' => \Aimeos\Base\Criteria\SQL::type( 'string' )];
76
77
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
78
		$level = $context->config()->get( 'mshop/stock/manager/sitemode', $level );
79
80
		$search = $this->object()->filter();
81
		$search->setConditions( $this->siteCondition( 'stock.siteid', $level ) );
82
		$conditions = $search->getConditionSource( $types, $translations );
83
84
		$conn = $context->db( $this->getResourceName() );
85
86
		/** mshop/stock/manager/stocklevel/mysql
87
		 * Increases or decreases the stock level for the given product and type code
88
		 *
89
		 * @see mshop/stock/manager/stocklevel/ansi
90
		 */
91
92
		/** mshop/stock/manager/stocklevel/ansi
93
		 * Increases or decreases the stock level for the given product and type code
94
		 *
95
		 * The stock level is decreased for the ordered products each time
96
		 * an order is placed by a customer successfully. Also, updates
97
		 * from external sources like ERP systems can increase the stock
98
		 * level of a product if no absolute values are set via save()
99
		 * instead.
100
		 *
101
		 * The stock level must be from one of the sites that are configured
102
		 * via the context item. If the current site is part of a tree of
103
		 * sites, the statement can increase or decrease stock levels from
104
		 * the current site and all parent sites if the stock level is
105
		 * inherited by one of the parent sites.
106
		 *
107
		 * Each time the stock level is updated, the modify date/time is
108
		 * set to the current timestamp and the editor field is updated.
109
		 *
110
		 * @param string SQL statement for increasing/decreasing the stock level
111
		 * @since 2017.01
112
		 * @see mshop/stock/manager/insert/ansi
113
		 * @see mshop/stock/manager/update/ansi
114
		 * @see mshop/stock/manager/newid/ansi
115
		 * @see mshop/stock/manager/delete/ansi
116
		 * @see mshop/stock/manager/search/ansi
117
		 * @see mshop/stock/manager/count/ansi
118
		 */
119
		$path = 'mshop/stock/manager/stocklevel';
120
121
		foreach( $pairs as $prodid => $qty )
122
		{
123
			$stmt = $conn->create( str_replace( ':cond', $conditions, $this->getSqlConfig( $path ) ) );
124
125
			$stmt->bind( 1, $qty, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
126
			$stmt->bind( 2, $context->datetime() ); //mtime
127
			$stmt->bind( 3, $context->editor() );
128
			$stmt->bind( 4, $prodid );
129
			$stmt->bind( 5, $type );
130
131
			$stmt->execute()->finish();
132
		}
133
134
		return $this;
135
	}
136
137
138
	/**
139
	 * Returns the additional column/search definitions
140
	 *
141
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
142
	 */
143
	public function getSaveAttributes() : array
144
	{
145
		return $this->createAttributes( $this->searchConfig );
146
	}
147
148
149
	/**
150
	 * Increases the stock level for the given product codes/quantity pairs and type
151
	 *
152
	 * @param array $pairs Associative list of product codes as keys and quantities as values
153
	 * @param string $type Unique code of the type
154
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
155
	 */
156
	public function increase( iterable $pairs, string $type = 'default' ) : \Aimeos\MShop\Stock\Manager\Iface
157
	{
158
		foreach( $pairs as $prodid => $qty ) {
159
			$pairs[$prodid] = -$qty;
160
		}
161
162
		return $this->object()->decrease( $pairs, $type );
163
	}
164
165
166
	/**
167
	 * Returns the prefix for the item properties and search keys.
168
	 *
169
	 * @return string Prefix for the item properties and search keys
170
	 */
171
	protected function prefix() : string
172
	{
173
		return 'stock.';
174
	}
175
176
177
	/** mshop/stock/manager/name
178
	 * Class name of the used stock manager implementation
179
	 *
180
	 * Each default manager can be replace by an alternative imlementation.
181
	 * To use this implementation, you have to set the last part of the class
182
	 * name as configuration value so the manager factory knows which class it
183
	 * has to instantiate.
184
	 *
185
	 * For example, if the name of the default class is
186
	 *
187
	 *  \Aimeos\MShop\Stock\Manager\Standard
188
	 *
189
	 * and you want to replace it with your own version named
190
	 *
191
	 *  \Aimeos\MShop\Stock\Manager\Mymanager
192
	 *
193
	 * then you have to set the this configuration option:
194
	 *
195
	 *  mshop/stock/manager/name = Mymanager
196
	 *
197
	 * The value is the last part of your own class name and it's case sensitive,
198
	 * so take care that the configuration value is exactly named like the last
199
	 * part of the class name.
200
	 *
201
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
202
	 * characters are possible! You should always start the last part of the class
203
	 * name with an upper case character and continue only with lower case characters
204
	 * or numbers. Avoid chamel case names like "MyManager"!
205
	 *
206
	 * @param string Last part of the class name
207
	 * @since 2020.10
208
	 */
209
210
	/** mshop/stock/manager/decorators/excludes
211
	 * Excludes decorators added by the "common" option from the stock manager
212
	 *
213
	 * Decorators extend the functionality of a class by adding new aspects
214
	 * (e.g. log what is currently done), executing the methods of the underlying
215
	 * class only in certain conditions (e.g. only for logged in users) or
216
	 * modify what is returned to the caller.
217
	 *
218
	 * This option allows you to remove a decorator added via
219
	 * "mshop/common/manager/decorators/default" before they are wrapped
220
	 * around the stock manager.
221
	 *
222
	 *  mshop/stock/manager/decorators/excludes = array( 'decorator1' )
223
	 *
224
	 * This would remove the decorator named "decorator1" from the list of
225
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
226
	 * "mshop/common/manager/decorators/default" for the stock manager.
227
	 *
228
	 * @param array List of decorator names
229
	 * @since 2020.10
230
	 * @see mshop/common/manager/decorators/default
231
	 * @see mshop/stock/manager/decorators/global
232
	 * @see mshop/stock/manager/decorators/local
233
	 */
234
235
	/** mshop/stock/manager/decorators/global
236
	 * Adds a list of globally available decorators only to the stock manager
237
	 *
238
	 * Decorators extend the functionality of a class by adding new aspects
239
	 * (e.g. log what is currently done), executing the methods of the underlying
240
	 * class only in certain conditions (e.g. only for logged in users) or
241
	 * modify what is returned to the caller.
242
	 *
243
	 * This option allows you to wrap global decorators
244
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the stock manager.
245
	 *
246
	 *  mshop/stock/manager/decorators/global = array( 'decorator1' )
247
	 *
248
	 * This would add the decorator named "decorator1" defined by
249
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the stock
250
	 * manager.
251
	 *
252
	 * @param array List of decorator names
253
	 * @since 2020.10
254
	 * @see mshop/common/manager/decorators/default
255
	 * @see mshop/stock/manager/decorators/excludes
256
	 * @see mshop/stock/manager/decorators/local
257
	 */
258
259
	/** mshop/stock/manager/decorators/local
260
	 * Adds a list of local decorators only to the stock manager
261
	 *
262
	 * Decorators extend the functionality of a class by adding new aspects
263
	 * (e.g. log what is currently done), executing the methods of the underlying
264
	 * class only in certain conditions (e.g. only for logged in users) or
265
	 * modify what is returned to the caller.
266
	 *
267
	 * This option allows you to wrap local decorators
268
	 * ("\Aimeos\MShop\Stock\Manager\Decorator\*") around the stock manager.
269
	 *
270
	 *  mshop/stock/manager/decorators/local = array( 'decorator2' )
271
	 *
272
	 * This would add the decorator named "decorator2" defined by
273
	 * "\Aimeos\MShop\Stock\Manager\Decorator\Decorator2" only to the stock
274
	 * manager.
275
	 *
276
	 * @param array List of decorator names
277
	 * @since 2020.10
278
	 * @see mshop/common/manager/decorators/default
279
	 * @see mshop/stock/manager/decorators/excludes
280
	 * @see mshop/stock/manager/decorators/global
281
	 */
282
283
	/** mshop/stock/manager/resource
284
	 * Name of the database connection resource to use
285
	 *
286
	 * You can configure a different database connection for each data domain
287
	 * and if no such connection name exists, the "db" connection will be used.
288
	 * It's also possible to use the same database connection for different
289
	 * data domains by configuring the same connection name using this setting.
290
	 *
291
	 * @param string Database connection name
292
	 * @since 2023.04
293
	 */
294
295
296
	/** mshop/stock/manager/delete/mysql
297
	 * Deletes the items matched by the given IDs from the database
298
	 *
299
	 * @see mshop/stock/manager/delete/ansi
300
	 */
301
302
	/** mshop/stock/manager/delete/ansi
303
	 * Deletes the items matched by the given IDs from the database
304
	 *
305
	 * Removes the records specified by the given IDs from the stock database.
306
	 * The records must be from the site that is configured via the
307
	 * context item.
308
	 *
309
	 * The ":cond" placeholder is replaced by the name of the ID column and
310
	 * the given ID or list of IDs while the site ID is bound to the question
311
	 * mark.
312
	 *
313
	 * The SQL statement should conform to the ANSI standard to be
314
	 * compatible with most relational database systems. This also
315
	 * includes using double quotes for table and column names.
316
	 *
317
	 * @param string SQL statement for deleting items
318
	 * @since 2020.10
319
	 * @see mshop/stock/manager/insert/ansi
320
	 * @see mshop/stock/manager/update/ansi
321
	 * @see mshop/stock/manager/newid/ansi
322
	 * @see mshop/stock/manager/search/ansi
323
	 * @see mshop/stock/manager/count/ansi
324
	 */
325
326
	 /** mshop/stock/manager/submanagers
327
	 * List of manager names that can be instantiated by the stock manager
328
	 *
329
	 * Managers provide a generic interface to the underlying storage.
330
	 * Each manager has or can have sub-managers caring about particular
331
	 * aspects. Each of these sub-managers can be instantiated by its
332
	 * parent manager using the getSubManager() method.
333
	 *
334
	 * The search keys from sub-managers can be normally used in the
335
	 * manager as well. It allows you to search for items of the manager
336
	 * using the search keys of the sub-managers to further limit the
337
	 * retrieved list of items.
338
	 *
339
	 * @param array List of sub-manager names
340
	 * @since 2020.10
341
	 */
342
343
	 /** mshop/stock/manager/insert/mysql
344
	 * Inserts a new stock record into the database table
345
	 *
346
	 * @see mshop/stock/manager/insert/ansi
347
	 */
348
349
	/** mshop/stock/manager/insert/ansi
350
	 * Inserts a new stock record into the database table
351
	 *
352
	 * Items with no ID yet (i.e. the ID is NULL) will be created in
353
	 * the database and the newly created ID retrieved afterwards
354
	 * using the "newid" SQL statement.
355
	 *
356
	 * The SQL statement must be a string suitable for being used as
357
	 * prepared statement. It must include question marks for binding
358
	 * the values from the stock item to the statement before they are
359
	 * sent to the database server. The number of question marks must
360
	 * be the same as the number of columns listed in the INSERT
361
	 * statement. The catalog of the columns must correspond to the
362
	 * catalog in the save() method, so the correct values are
363
	 * bound to the columns.
364
	 *
365
	 * The SQL statement should conform to the ANSI standard to be
366
	 * compatible with most relational database systems. This also
367
	 * includes using double quotes for table and column names.
368
	 *
369
	 * @param string SQL statement for inserting records
370
	 * @since 2020.10
371
	 * @see mshop/stock/manager/update/ansi
372
	 * @see mshop/stock/manager/newid/ansi
373
	 * @see mshop/stock/manager/delete/ansi
374
	 * @see mshop/stock/manager/search/ansi
375
	 * @see mshop/stock/manager/count/ansi
376
	 */
377
378
	 /** mshop/stock/manager/update/mysql
379
	 * Updates an existing stock record in the database
380
	 *
381
	 * @see mshop/stock/manager/update/ansi
382
	 */
383
384
	/** mshop/stock/manager/update/ansi
385
	 * Updates an existing stock record in the database
386
	 *
387
	 * Items which already have an ID (i.e. the ID is not NULL) will
388
	 * be updated in the database.
389
	 *
390
	 * The SQL statement must be a string suitable for being used as
391
	 * prepared statement. It must include question marks for binding
392
	 * the values from the stock item to the statement before they are
393
	 * sent to the database server. The catalog of the columns must
394
	 * correspond to the catalog in the save() method, so the
395
	 * correct values are bound to the columns.
396
	 *
397
	 * The SQL statement should conform to the ANSI standard to be
398
	 * compatible with most relational database systems. This also
399
	 * includes using double quotes for table and column names.
400
	 *
401
	 * @param string SQL statement for updating records
402
	 * @since 2020.10
403
	 * @see mshop/stock/manager/insert/ansi
404
	 * @see mshop/stock/manager/newid/ansi
405
	 * @see mshop/stock/manager/delete/ansi
406
	 * @see mshop/stock/manager/search/ansi
407
	 * @see mshop/stock/manager/count/ansi
408
	 */
409
410
	/** mshop/stock/manager/newid/mysql
411
	 * Retrieves the ID generated by the database when inserting a new record
412
	 *
413
	 * @see mshop/stock/manager/newid/ansi
414
	 */
415
416
	/** mshop/stock/manager/newid/ansi
417
	 * Retrieves the ID generated by the database when inserting a new record
418
	 *
419
	 * As soon as a new record is inserted into the database table,
420
	 * the database server generates a new and unique identifier for
421
	 * that record. This ID can be used for retrieving, updating and
422
	 * deleting that specific record from the table again.
423
	 *
424
	 * For MySQL:
425
	 *  SELECT LAST_INSERT_ID()
426
	 * For PostgreSQL:
427
	 *  SELECT currval('seq_mrul_id')
428
	 * For SQL Server:
429
	 *  SELECT SCOPE_IDENTITY()
430
	 * For Oracle:
431
	 *  SELECT "seq_mrul_id".CURRVAL FROM DUAL
432
	 *
433
	 * There's no way to retrive the new ID by a SQL statements that
434
	 * fits for most database servers as they implement their own
435
	 * specific way.
436
	 *
437
	 * @param string SQL statement for retrieving the last inserted record ID
438
	 * @since 2020.10
439
	 * @see mshop/stock/manager/insert/ansi
440
	 * @see mshop/stock/manager/update/ansi
441
	 * @see mshop/stock/manager/delete/ansi
442
	 * @see mshop/stock/manager/search/ansi
443
	 * @see mshop/stock/manager/count/ansi
444
	 */
445
446
	/** mshop/stock/manager/sitemode
447
	 * Mode how items from levels below or above in the site tree are handled
448
	 *
449
	 * By default, only items from the current site are fetched from the
450
	 * storage. If the ai-sites extension is installed, you can create a
451
	 * tree of sites. Then, this setting allows you to define for the
452
	 * whole stock domain if items from parent sites are inherited,
453
	 * sites from child sites are aggregated or both.
454
	 *
455
	 * Available constants for the site mode are:
456
	 * * 0 = only items from the current site
457
	 * * 1 = inherit items from parent sites
458
	 * * 2 = aggregate items from child sites
459
	 * * 3 = inherit and aggregate items at the same time
460
	 *
461
	 * You also need to set the mode in the locale manager
462
	 * (mshop/locale/manager/sitelevel) to one of the constants.
463
	 * If you set it to the same value, it will work as described but you
464
	 * can also use different modes. For example, if inheritance and
465
	 * aggregation is configured the locale manager but only inheritance
466
	 * in the domain manager because aggregating items makes no sense in
467
	 * this domain, then items wil be only inherited. Thus, you have full
468
	 * control over inheritance and aggregation in each domain.
469
	 *
470
	 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
471
	 * @since 2018.01
472
	 * @see mshop/locale/manager/sitelevel
473
	 */
474
475
	/** mshop/stock/manager/search/mysql
476
	 * Retrieves the records matched by the given criteria in the database
477
	 *
478
	 * @see mshop/stock/manager/search/ansi
479
	 */
480
481
	/** mshop/stock/manager/search/ansi
482
	 * Retrieves the records matched by the given criteria in the database
483
	 *
484
	 * Fetches the records matched by the given criteria from the stock
485
	 * database. The records must be from one of the sites that are
486
	 * configured via the context item. If the current site is part of
487
	 * a tree of sites, the SELECT statement can retrieve all records
488
	 * from the current site and the complete sub-tree of sites.
489
	 *
490
	 * As the records can normally be limited by criteria from sub-managers,
491
	 * their tables must be joined in the SQL context. This is done by
492
	 * using the "internaldeps" property from the definition of the ID
493
	 * column of the sub-managers. These internal dependencies specify
494
	 * the JOIN between the tables and the used columns for joining. The
495
	 * ":joins" placeholder is then replaced by the JOIN strings from
496
	 * the sub-managers.
497
	 *
498
	 * To limit the records matched, conditions can be added to the given
499
	 * criteria object. It can contain comparisons like column names that
500
	 * must match specific values which can be combined by AND, OR or NOT
501
	 * operators. The resulting string of SQL conditions replaces the
502
	 * ":cond" placeholder before the statement is sent to the database
503
	 * server.
504
	 *
505
	 * If the records that are retrieved should be cataloged by one or more
506
	 * columns, the generated string of column / sort direction pairs
507
	 * replaces the ":catalog" placeholder. In case no cataloging is required,
508
	 * the complete ORDER BY part including the "\/*-catalogby*\/...\/*catalogby-*\/"
509
	 * markers is removed to speed up retrieving the records. Columns of
510
	 * sub-managers can also be used for cataloging the result set but then
511
	 * no index can be used.
512
	 *
513
	 * The number of returned records can be limited and can start at any
514
	 * number between the begining and the end of the result set. For that
515
	 * the ":size" and ":start" placeholders are replaced by the
516
	 * corresponding values from the criteria object. The default values
517
	 * are 0 for the start and 100 for the size value.
518
	 *
519
	 * The SQL statement should conform to the ANSI standard to be
520
	 * compatible with most relational database systems. This also
521
	 * includes using double quotes for table and column names.
522
	 *
523
	 * @param string SQL statement for searching items
524
	 * @since 2020.10
525
	 * @see mshop/stock/manager/insert/ansi
526
	 * @see mshop/stock/manager/update/ansi
527
	 * @see mshop/stock/manager/newid/ansi
528
	 * @see mshop/stock/manager/delete/ansi
529
	 * @see mshop/stock/manager/count/ansi
530
	 */
531
532
	/** mshop/stock/manager/count/mysql
533
	 * Counts the number of records matched by the given criteria in the database
534
	 *
535
	 * @see mshop/stock/manager/count/ansi
536
	 */
537
538
	/** mshop/stock/manager/count/ansi
539
	 * Counts the number of records matched by the given criteria in the database
540
	 *
541
	 * Counts all records matched by the given criteria from the stock
542
	 * database. The records must be from one of the sites that are
543
	 * configured via the context item. If the current site is part of
544
	 * a tree of sites, the statement can count all records from the
545
	 * current site and the complete sub-tree of sites.
546
	 *
547
	 * As the records can normally be limited by criteria from sub-managers,
548
	 * their tables must be joined in the SQL context. This is done by
549
	 * using the "internaldeps" property from the definition of the ID
550
	 * column of the sub-managers. These internal dependencies specify
551
	 * the JOIN between the tables and the used columns for joining. The
552
	 * ":joins" placeholder is then replaced by the JOIN strings from
553
	 * the sub-managers.
554
	 *
555
	 * To limit the records matched, conditions can be added to the given
556
	 * criteria object. It can contain comparisons like column names that
557
	 * must match specific values which can be combined by AND, OR or NOT
558
	 * operators. The resulting string of SQL conditions replaces the
559
	 * ":cond" placeholder before the statement is sent to the database
560
	 * server.
561
	 *
562
	 * Both, the strings for ":joins" and for ":cond" are the same as for
563
	 * the "search" SQL statement.
564
	 *
565
	 * Contrary to the "search" statement, it doesn't return any records
566
	 * but instead the number of records that have been found. As counting
567
	 * thousands of records can be a long running task, the maximum number
568
	 * of counted records is limited for performance reasons.
569
	 *
570
	 * The SQL statement should conform to the ANSI standard to be
571
	 * compatible with most relational database systems. This also
572
	 * includes using double quotes for table and column names.
573
	 *
574
	 * @param string SQL statement for counting items
575
	 * @since 2020.10
576
	 * @see mshop/stock/manager/insert/ansi
577
	 * @see mshop/stock/manager/update/ansi
578
	 * @see mshop/stock/manager/newid/ansi
579
	 * @see mshop/stock/manager/delete/ansi
580
	 * @see mshop/stock/manager/search/ansi
581
	 */
582
}
583