Passed
Push — master ( b98f34...b02400 )
by Aimeos
05:49 queued 01:38
created

Standard::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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