1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
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 $searchConfig = array( |
26
|
|
|
'price.id' => array( |
27
|
|
|
'code' => 'price.id', |
28
|
|
|
'internalcode' => 'mpri."id"', |
29
|
|
|
'label' => 'Price ID', |
30
|
|
|
'type' => 'integer', |
31
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
32
|
|
|
), |
33
|
|
|
'price.siteid' => array( |
34
|
|
|
'code' => 'price.siteid', |
35
|
|
|
'internalcode' => 'mpri."siteid"', |
36
|
|
|
'label' => 'Price site ID', |
37
|
|
|
'type' => 'integer', |
38
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
39
|
|
|
'public' => false, |
40
|
|
|
), |
41
|
|
|
'price.type' => array( |
42
|
|
|
'label' => 'Price type ID', |
43
|
|
|
'code' => 'price.type', |
44
|
|
|
'internalcode' => 'mpri."type"', |
45
|
|
|
'type' => 'string', |
46
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
47
|
|
|
), |
48
|
|
|
'price.currencyid' => array( |
49
|
|
|
'code' => 'price.currencyid', |
50
|
|
|
'internalcode' => 'mpri."currencyid"', |
51
|
|
|
'label' => 'Price currency code', |
52
|
|
|
'type' => 'string', |
53
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
54
|
|
|
), |
55
|
|
|
'price.domain' => array( |
56
|
|
|
'code' => 'price.domain', |
57
|
|
|
'internalcode' => 'mpri."domain"', |
58
|
|
|
'label' => 'Price domain', |
59
|
|
|
'type' => 'string', |
60
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
61
|
|
|
), |
62
|
|
|
'price.label' => array( |
63
|
|
|
'code' => 'price.label', |
64
|
|
|
'internalcode' => 'mpri."label"', |
65
|
|
|
'label' => 'Price label', |
66
|
|
|
'type' => 'string', |
67
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
68
|
|
|
), |
69
|
|
|
'price.quantity' => array( |
70
|
|
|
'code' => 'price.quantity', |
71
|
|
|
'internalcode' => 'mpri."quantity"', |
72
|
|
|
'label' => 'Price quantity', |
73
|
|
|
'type' => 'integer', |
74
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
75
|
|
|
), |
76
|
|
|
'price.value' => array( |
77
|
|
|
'code' => 'price.value', |
78
|
|
|
'internalcode' => 'mpri."value"', |
79
|
|
|
'label' => 'Price regular value', |
80
|
|
|
'type' => 'decimal', |
81
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
82
|
|
|
), |
83
|
|
|
'price.costs' => array( |
84
|
|
|
'code' => 'price.costs', |
85
|
|
|
'internalcode' => 'mpri."costs"', |
86
|
|
|
'label' => 'Price shipping costs', |
87
|
|
|
'type' => 'decimal', |
88
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
89
|
|
|
), |
90
|
|
|
'price.rebate' => array( |
91
|
|
|
'code' => 'price.rebate', |
92
|
|
|
'internalcode' => 'mpri."rebate"', |
93
|
|
|
'label' => 'Price rebate amount', |
94
|
|
|
'type' => 'decimal', |
95
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
96
|
|
|
), |
97
|
|
|
'price.taxrate' => array( |
98
|
|
|
'code' => 'price.taxrate', |
99
|
|
|
'internalcode' => 'mpri."taxrate"', |
100
|
|
|
'label' => 'Price tax in percent', |
101
|
|
|
'type' => 'decimal', |
102
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
103
|
|
|
), |
104
|
|
|
'price.status' => array( |
105
|
|
|
'code' => 'price.status', |
106
|
|
|
'internalcode' => 'mpri."status"', |
107
|
|
|
'label' => 'Price status', |
108
|
|
|
'type' => 'integer', |
109
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
110
|
|
|
), |
111
|
|
|
'price.mtime' => array( |
112
|
|
|
'code' => 'price.mtime', |
113
|
|
|
'internalcode' => 'mpri."mtime"', |
114
|
|
|
'label' => 'Price modify date', |
115
|
|
|
'type' => 'datetime', |
116
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
117
|
|
|
), |
118
|
|
|
'price.ctime' => array( |
119
|
|
|
'code' => 'price.ctime', |
120
|
|
|
'internalcode' => 'mpri."ctime"', |
121
|
|
|
'label' => 'Price create date/time', |
122
|
|
|
'type' => 'datetime', |
123
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
124
|
|
|
), |
125
|
|
|
'price.editor' => array( |
126
|
|
|
'code' => 'price.editor', |
127
|
|
|
'internalcode' => 'mpri."editor"', |
128
|
|
|
'label' => 'Price editor', |
129
|
|
|
'type' => 'string', |
130
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
131
|
|
|
), |
132
|
|
|
'price:has' => array( |
133
|
|
|
'code' => 'price:has()', |
134
|
|
|
'internalcode' => '( |
135
|
|
|
SELECT mprili_has."id" FROM mshop_price_list AS mprili_has |
136
|
|
|
WHERE mpri."id" = mprili_has."parentid" AND :site AND :key LIMIT 1 |
137
|
|
|
)', |
138
|
|
|
'label' => 'Price has list item, parameter(<domain>[,<list type>[,<reference ID>)]]', |
139
|
|
|
'type' => 'null', |
140
|
|
|
'internaltype' => 'null', |
141
|
|
|
'public' => false, |
142
|
|
|
), |
143
|
|
|
'price:prop' => array( |
144
|
|
|
'code' => 'price:prop()', |
145
|
|
|
'internalcode' => '( |
146
|
|
|
SELECT mpripr_prop."id" FROM mshop_price_property AS mpripr_prop |
147
|
|
|
WHERE mpri."id" = mpripr_prop."parentid" AND :site AND :key LIMIT 1 |
148
|
|
|
)', |
149
|
|
|
'label' => 'Price has property item, parameter(<property type>[,<language code>[,<property value>]])', |
150
|
|
|
'type' => 'null', |
151
|
|
|
'internaltype' => 'null', |
152
|
|
|
'public' => false, |
153
|
|
|
), |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
private $currencyId; |
157
|
|
|
private $precision; |
158
|
|
|
private $taxflag; |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Initializes the object. |
163
|
|
|
* |
164
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
165
|
|
|
*/ |
166
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
167
|
|
|
{ |
168
|
|
|
parent::__construct( $context ); |
169
|
|
|
|
170
|
|
|
$this->setResourceName( 'db-price' ); |
171
|
|
|
$this->currencyId = $context->getLocale()->getCurrencyId(); |
172
|
|
|
|
173
|
|
|
/** mshop/price/taxflag |
174
|
|
|
* Configuration setting if prices are inclusive or exclusive tax |
175
|
|
|
* |
176
|
|
|
* In Aimeos, prices can be entered either completely with or without tax. The |
177
|
|
|
* default is that prices contains tax. You must specifiy the tax rate for each |
178
|
|
|
* prices to prevent wrong calculations. |
179
|
|
|
* |
180
|
|
|
* @param boolean True if gross prices are used, false for net prices |
181
|
|
|
* @category Developer |
182
|
|
|
* @category User |
183
|
|
|
* @since 2016.02 |
184
|
|
|
*/ |
185
|
|
|
$this->taxflag = $context->getConfig()->get( 'mshop/price/taxflag', true ); |
186
|
|
|
|
187
|
|
|
/** mshop/price/precision |
188
|
|
|
* Number of decimal digits prices contain |
189
|
|
|
* |
190
|
|
|
* Sets the number of decimal digits price values will contain. Internally, |
191
|
|
|
* prices are calculated as double values with high precision but these |
192
|
|
|
* values will be rounded after calculation to the configured number of digits. |
193
|
|
|
* |
194
|
|
|
* @param integer Positive number of digits |
195
|
|
|
* @category Developer |
196
|
|
|
* @since 2019.04 |
197
|
|
|
*/ |
198
|
|
|
$this->precision = $context->getConfig()->get( 'mshop/price/precision', 2 ); |
199
|
|
|
|
200
|
|
|
$self = $this; |
201
|
|
|
$locale = $context->getLocale(); |
202
|
|
|
|
203
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
204
|
|
|
$level = $context->getConfig()->get( 'mshop/price/manager/sitemode', $level ); |
205
|
|
|
|
206
|
|
|
$siteIds = [$locale->getSiteId()]; |
207
|
|
|
|
208
|
|
|
if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_PATH ) { |
209
|
|
|
$siteIds = array_merge( $siteIds, $locale->getSitePath() ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) { |
213
|
|
|
$siteIds = array_merge( $siteIds, $locale->getSiteSubTree() ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
$this->searchConfig['price:has']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) { |
218
|
|
|
|
219
|
|
|
foreach( $params as $key => $param ) { |
220
|
|
|
$params[$key] = trim( $param, '\'' ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$source = str_replace( ':site', $self->toExpression( 'mprili_has."siteid"', $siteIds ), $source ); |
224
|
|
|
$str = $self->toExpression( 'mprili_has."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' ); |
225
|
|
|
$source = str_replace( ':key', $str, $source ); |
226
|
|
|
|
227
|
|
|
return $params; |
228
|
|
|
}; |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
$this->searchConfig['price:prop']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) { |
232
|
|
|
|
233
|
|
|
foreach( $params as $key => $param ) { |
234
|
|
|
$params[$key] = trim( $param, '\'' ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$source = str_replace( ':site', $self->toExpression( 'mpripr_prop."siteid"', $siteIds ), $source ); |
238
|
|
|
$str = $self->toExpression( 'mpripr_prop."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' ); |
239
|
|
|
$source = str_replace( ':key', $str, $source ); |
240
|
|
|
|
241
|
|
|
return $params; |
242
|
|
|
}; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Removes old entries from the storage. |
248
|
|
|
* |
249
|
|
|
* @param string[] $siteids List of IDs for sites whose entries should be deleted |
250
|
|
|
* @return \Aimeos\MShop\Price\Manager\Iface Manager object for chaining method calls |
251
|
|
|
*/ |
252
|
|
|
public function cleanup( array $siteids ) |
253
|
|
|
{ |
254
|
|
|
$path = 'mshop/price/manager/submanagers'; |
255
|
|
|
foreach( $this->getContext()->getConfig()->get( $path, ['type', 'property', 'lists'] ) as $domain ) { |
256
|
|
|
$this->getObject()->getSubManager( $domain )->cleanup( $siteids ); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $this->cleanupBase( $siteids, 'mshop/price/manager/standard/delete' ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Creates a new empty item instance |
265
|
|
|
* |
266
|
|
|
* @param array $values Values the item should be initialized with |
267
|
|
|
* @return \Aimeos\MShop\Price\Item\Iface New price item object |
268
|
|
|
*/ |
269
|
|
|
public function createItem( array $values = [] ) |
270
|
|
|
{ |
271
|
|
|
$locale = $this->getContext()->getLocale(); |
272
|
|
|
$values['price.siteid'] = $locale->getSiteId(); |
273
|
|
|
|
274
|
|
|
if( $locale->getCurrencyId() !== null ) { |
275
|
|
|
$values['price.currencyid'] = $locale->getCurrencyId(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this->createItemBase( $values ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Returns the available manager types |
285
|
|
|
* |
286
|
|
|
* @param boolean $withsub Return also the resource type of sub-managers if true |
287
|
|
|
* @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
288
|
|
|
*/ |
289
|
|
|
public function getResourceType( $withsub = true ) |
290
|
|
|
{ |
291
|
|
|
$path = 'mshop/price/manager/submanagers'; |
292
|
|
|
return $this->getResourceTypeBase( 'price', $path, ['property', 'lists'], $withsub ); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns the attributes that can be used for searching. |
298
|
|
|
* |
299
|
|
|
* @param boolean $withsub Return also attributes of sub-managers if true |
300
|
|
|
* @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items |
301
|
|
|
*/ |
302
|
|
|
public function getSearchAttributes( $withsub = true ) |
303
|
|
|
{ |
304
|
|
|
/** mshop/price/manager/submanagers |
305
|
|
|
* List of manager names that can be instantiated by the price manager |
306
|
|
|
* |
307
|
|
|
* Managers provide a generic interface to the underlying storage. |
308
|
|
|
* Each manager has or can have sub-managers caring about particular |
309
|
|
|
* aspects. Each of these sub-managers can be instantiated by its |
310
|
|
|
* parent manager using the getSubManager() method. |
311
|
|
|
* |
312
|
|
|
* The search keys from sub-managers can be normally used in the |
313
|
|
|
* manager as well. It allows you to search for items of the manager |
314
|
|
|
* using the search keys of the sub-managers to further limit the |
315
|
|
|
* retrieved list of items. |
316
|
|
|
* |
317
|
|
|
* @param array List of sub-manager names |
318
|
|
|
* @since 2014.03 |
319
|
|
|
* @category Developer |
320
|
|
|
*/ |
321
|
|
|
$path = 'mshop/price/manager/submanagers'; |
322
|
|
|
|
323
|
|
|
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Removes multiple items specified by ids in the array. |
329
|
|
|
* |
330
|
|
|
* @param string[] $ids List of IDs |
331
|
|
|
* @return \Aimeos\MShop\Price\Manager\Iface Manager object for chaining method calls |
332
|
|
|
*/ |
333
|
|
|
public function deleteItems( array $ids ) |
334
|
|
|
{ |
335
|
|
|
/** mshop/price/manager/standard/delete/mysql |
336
|
|
|
* Deletes the items matched by the given IDs from the database |
337
|
|
|
* |
338
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
339
|
|
|
*/ |
340
|
|
|
|
341
|
|
|
/** mshop/price/manager/standard/delete/ansi |
342
|
|
|
* Deletes the items matched by the given IDs from the database |
343
|
|
|
* |
344
|
|
|
* Removes the records specified by the given IDs from the price database. |
345
|
|
|
* The records must be from the site that is configured via the |
346
|
|
|
* context item. |
347
|
|
|
* |
348
|
|
|
* The ":cond" placeholder is replaced by the name of the ID column and |
349
|
|
|
* the given ID or list of IDs while the site ID is bound to the question |
350
|
|
|
* mark. |
351
|
|
|
* |
352
|
|
|
* The SQL statement should conform to the ANSI standard to be |
353
|
|
|
* compatible with most relational database systems. This also |
354
|
|
|
* includes using double quotes for table and column names. |
355
|
|
|
* |
356
|
|
|
* @param string SQL statement for deleting items |
357
|
|
|
* @since 2014.03 |
358
|
|
|
* @category Developer |
359
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
360
|
|
|
* @see mshop/price/manager/standard/update/ansi |
361
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
362
|
|
|
* @see mshop/price/manager/standard/search/ansi |
363
|
|
|
* @see mshop/price/manager/standard/count/ansi |
364
|
|
|
*/ |
365
|
|
|
$path = 'mshop/price/manager/standard/delete'; |
366
|
|
|
|
367
|
|
|
return $this->deleteItemsBase( $ids, $path ); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Returns the price item object specificed by its ID. |
373
|
|
|
* |
374
|
|
|
* @param string $id Unique price ID referencing an existing price |
375
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
376
|
|
|
* @param boolean $default Add default criteria |
377
|
|
|
* @return \Aimeos\MShop\Price\Item\Iface $item Returns the price item of the given id |
378
|
|
|
* @throws \Aimeos\MShop\Exception If item couldn't be found |
379
|
|
|
*/ |
380
|
|
|
public function getItem( $id, array $ref = [], $default = false ) |
381
|
|
|
{ |
382
|
|
|
return $this->getItemBase( 'price.id', $id, $ref, $default ); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Saves a price item object. |
388
|
|
|
* |
389
|
|
|
* @param \Aimeos\MShop\Price\Item\Iface $item Price item object |
390
|
|
|
* @param boolean $fetch True if the new ID should be returned in the item |
391
|
|
|
* @return \Aimeos\MShop\Price\Item\Iface Updated item including the generated ID |
392
|
|
|
* @throws \Aimeos\MShop\Price\Exception If price couldn't be saved |
393
|
|
|
*/ |
394
|
|
|
public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true ) |
395
|
|
|
{ |
396
|
|
|
self::checkClass( \Aimeos\MShop\Price\Item\Iface::class, $item ); |
397
|
|
|
|
398
|
|
|
if( !$item->isModified() ) |
399
|
|
|
{ |
400
|
|
|
$item = $this->savePropertyItems( $item, 'price', $fetch ); |
401
|
|
|
return $this->saveListItems( $item, 'price', $fetch ); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$context = $this->getContext(); |
405
|
|
|
|
406
|
|
|
$dbm = $context->getDatabaseManager(); |
407
|
|
|
$dbname = $this->getResourceName(); |
408
|
|
|
$conn = $dbm->acquire( $dbname ); |
409
|
|
|
|
410
|
|
|
try |
411
|
|
|
{ |
412
|
|
|
$id = $item->getId(); |
413
|
|
|
$date = date( 'Y-m-d H:i:s' ); |
414
|
|
|
|
415
|
|
|
if( $id === null ) |
416
|
|
|
{ |
417
|
|
|
/** mshop/price/manager/standard/insert/mysql |
418
|
|
|
* Inserts a new price record into the database table |
419
|
|
|
* |
420
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
421
|
|
|
*/ |
422
|
|
|
|
423
|
|
|
/** mshop/price/manager/standard/insert/ansi |
424
|
|
|
* Inserts a new price record into the database table |
425
|
|
|
* |
426
|
|
|
* Items with no ID yet (i.e. the ID is NULL) will be created in |
427
|
|
|
* the database and the newly created ID retrieved afterwards |
428
|
|
|
* using the "newid" SQL statement. |
429
|
|
|
* |
430
|
|
|
* The SQL statement must be a string suitable for being used as |
431
|
|
|
* prepared statement. It must include question marks for binding |
432
|
|
|
* the values from the price item to the statement before they are |
433
|
|
|
* sent to the database server. The number of question marks must |
434
|
|
|
* be the same as the number of columns listed in the INSERT |
435
|
|
|
* statement. The order of the columns must correspond to the |
436
|
|
|
* order in the saveItems() method, so the correct values are |
437
|
|
|
* bound to the columns. |
438
|
|
|
* |
439
|
|
|
* The SQL statement should conform to the ANSI standard to be |
440
|
|
|
* compatible with most relational database systems. This also |
441
|
|
|
* includes using double quotes for table and column names. |
442
|
|
|
* |
443
|
|
|
* @param string SQL statement for inserting records |
444
|
|
|
* @since 2014.03 |
445
|
|
|
* @category Developer |
446
|
|
|
* @see mshop/price/manager/standard/update/ansi |
447
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
448
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
449
|
|
|
* @see mshop/price/manager/standard/search/ansi |
450
|
|
|
* @see mshop/price/manager/standard/count/ansi |
451
|
|
|
*/ |
452
|
|
|
$path = 'mshop/price/manager/standard/insert'; |
453
|
|
|
} |
454
|
|
|
else |
455
|
|
|
{ |
456
|
|
|
/** mshop/price/manager/standard/update/mysql |
457
|
|
|
* Updates an existing price record in the database |
458
|
|
|
* |
459
|
|
|
* @see mshop/price/manager/standard/update/ansi |
460
|
|
|
*/ |
461
|
|
|
|
462
|
|
|
/** mshop/price/manager/standard/update/ansi |
463
|
|
|
* Updates an existing price record in the database |
464
|
|
|
* |
465
|
|
|
* Items which already have an ID (i.e. the ID is not NULL) will |
466
|
|
|
* be updated in the database. |
467
|
|
|
* |
468
|
|
|
* The SQL statement must be a string suitable for being used as |
469
|
|
|
* prepared statement. It must include question marks for binding |
470
|
|
|
* the values from the price item to the statement before they are |
471
|
|
|
* sent to the database server. The order of the columns must |
472
|
|
|
* correspond to the order in the saveItems() method, so the |
473
|
|
|
* correct values are bound to the columns. |
474
|
|
|
* |
475
|
|
|
* The SQL statement should conform to the ANSI standard to be |
476
|
|
|
* compatible with most relational database systems. This also |
477
|
|
|
* includes using double quotes for table and column names. |
478
|
|
|
* |
479
|
|
|
* @param string SQL statement for updating records |
480
|
|
|
* @since 2014.03 |
481
|
|
|
* @category Developer |
482
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
483
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
484
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
485
|
|
|
* @see mshop/price/manager/standard/search/ansi |
486
|
|
|
* @see mshop/price/manager/standard/count/ansi |
487
|
|
|
*/ |
488
|
|
|
$path = 'mshop/price/manager/standard/update'; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
$stmt = $this->getCachedStatement( $conn, $path ); |
492
|
|
|
|
493
|
|
|
$stmt->bind( 1, $item->getType() ); |
494
|
|
|
$stmt->bind( 2, $item->getCurrencyId() ); |
495
|
|
|
$stmt->bind( 3, $item->getDomain() ); |
496
|
|
|
$stmt->bind( 4, $item->getLabel() ); |
497
|
|
|
$stmt->bind( 5, $item->getQuantity(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
498
|
|
|
$stmt->bind( 6, $item->getValue() ); |
499
|
|
|
$stmt->bind( 7, $item->getCosts() ); |
500
|
|
|
$stmt->bind( 8, $item->getRebate() ); |
501
|
|
|
$stmt->bind( 9, $item->getTaxRate() ); |
502
|
|
|
$stmt->bind( 10, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
503
|
|
|
$stmt->bind( 11, $date ); //mtime |
504
|
|
|
$stmt->bind( 12, $context->getEditor() ); |
505
|
|
|
$stmt->bind( 13, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
506
|
|
|
|
507
|
|
|
if( $id !== null ) { |
508
|
|
|
$stmt->bind( 14, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
509
|
|
|
$item->setId( $id ); |
510
|
|
|
} else { |
511
|
|
|
$stmt->bind( 14, $date ); //ctime |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
$stmt->execute()->finish(); |
515
|
|
|
|
516
|
|
|
if( $id === null ) |
517
|
|
|
{ |
518
|
|
|
/** mshop/price/manager/standard/newid/mysql |
519
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
520
|
|
|
* |
521
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
522
|
|
|
*/ |
523
|
|
|
|
524
|
|
|
/** mshop/price/manager/standard/newid/ansi |
525
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
526
|
|
|
* |
527
|
|
|
* As soon as a new record is inserted into the database table, |
528
|
|
|
* the database server generates a new and unique identifier for |
529
|
|
|
* that record. This ID can be used for retrieving, updating and |
530
|
|
|
* deleting that specific record from the table again. |
531
|
|
|
* |
532
|
|
|
* For MySQL: |
533
|
|
|
* SELECT LAST_INSERT_ID() |
534
|
|
|
* For PostgreSQL: |
535
|
|
|
* SELECT currval('seq_mpri_id') |
536
|
|
|
* For SQL Server: |
537
|
|
|
* SELECT SCOPE_IDENTITY() |
538
|
|
|
* For Oracle: |
539
|
|
|
* SELECT "seq_mpri_id".CURRVAL FROM DUAL |
540
|
|
|
* |
541
|
|
|
* There's no way to retrive the new ID by a SQL statements that |
542
|
|
|
* fits for most database servers as they implement their own |
543
|
|
|
* specific way. |
544
|
|
|
* |
545
|
|
|
* @param string SQL statement for retrieving the last inserted record ID |
546
|
|
|
* @since 2014.03 |
547
|
|
|
* @category Developer |
548
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
549
|
|
|
* @see mshop/price/manager/standard/update/ansi |
550
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
551
|
|
|
* @see mshop/price/manager/standard/search/ansi |
552
|
|
|
* @see mshop/price/manager/standard/count/ansi |
553
|
|
|
*/ |
554
|
|
|
$path = 'mshop/price/manager/standard/newid'; |
555
|
|
|
$item->setId( $this->newId( $conn, $path ) ); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
$dbm->release( $conn, $dbname ); |
559
|
|
|
} |
560
|
|
|
catch( \Exception $e ) |
561
|
|
|
{ |
562
|
|
|
$dbm->release( $conn, $dbname ); |
563
|
|
|
throw $e; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
$item = $this->savePropertyItems( $item, 'price', $fetch ); |
567
|
|
|
return $this->saveListItems( $item, 'price', $fetch ); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Returns the item objects matched by the given search criteria. |
573
|
|
|
* |
574
|
|
|
* @param \Aimeos\MW\Criteria\Iface $search Search criteria object |
575
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
576
|
|
|
* @param integer|null &$total Number of items that are available in total |
577
|
|
|
* @return array List of items implementing \Aimeos\MShop\Price\Item\Iface |
578
|
|
|
*/ |
579
|
|
|
public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null ) |
580
|
|
|
{ |
581
|
|
|
$map = []; |
582
|
|
|
$context = $this->getContext(); |
583
|
|
|
|
584
|
|
|
$dbm = $context->getDatabaseManager(); |
585
|
|
|
$dbname = $this->getResourceName(); |
586
|
|
|
$conn = $dbm->acquire( $dbname ); |
587
|
|
|
|
588
|
|
|
try |
589
|
|
|
{ |
590
|
|
|
$required = array( 'price' ); |
591
|
|
|
|
592
|
|
|
/** mshop/price/manager/sitemode |
593
|
|
|
* Mode how items from levels below or above in the site tree are handled |
594
|
|
|
* |
595
|
|
|
* By default, only items from the current site are fetched from the |
596
|
|
|
* storage. If the ai-sites extension is installed, you can create a |
597
|
|
|
* tree of sites. Then, this setting allows you to define for the |
598
|
|
|
* whole price domain if items from parent sites are inherited, |
599
|
|
|
* sites from child sites are aggregated or both. |
600
|
|
|
* |
601
|
|
|
* Available constants for the site mode are: |
602
|
|
|
* * 0 = only items from the current site |
603
|
|
|
* * 1 = inherit items from parent sites |
604
|
|
|
* * 2 = aggregate items from child sites |
605
|
|
|
* * 3 = inherit and aggregate items at the same time |
606
|
|
|
* |
607
|
|
|
* You also need to set the mode in the locale manager |
608
|
|
|
* (mshop/locale/manager/standard/sitelevel) to one of the constants. |
609
|
|
|
* If you set it to the same value, it will work as described but you |
610
|
|
|
* can also use different modes. For example, if inheritance and |
611
|
|
|
* aggregation is configured the locale manager but only inheritance |
612
|
|
|
* in the domain manager because aggregating items makes no sense in |
613
|
|
|
* this domain, then items wil be only inherited. Thus, you have full |
614
|
|
|
* control over inheritance and aggregation in each domain. |
615
|
|
|
* |
616
|
|
|
* @param integer Constant from Aimeos\MShop\Locale\Manager\Base class |
617
|
|
|
* @category Developer |
618
|
|
|
* @since 2018.01 |
619
|
|
|
* @see mshop/locale/manager/standard/sitelevel |
620
|
|
|
*/ |
621
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
622
|
|
|
$level = $context->getConfig()->get( 'mshop/price/manager/sitemode', $level ); |
623
|
|
|
|
624
|
|
|
/** mshop/price/manager/standard/search/mysql |
625
|
|
|
* Retrieves the records matched by the given criteria in the database |
626
|
|
|
* |
627
|
|
|
* @see mshop/price/manager/standard/search/ansi |
628
|
|
|
*/ |
629
|
|
|
|
630
|
|
|
/** mshop/price/manager/standard/search/ansi |
631
|
|
|
* Retrieves the records matched by the given criteria in the database |
632
|
|
|
* |
633
|
|
|
* Fetches the records matched by the given criteria from the price |
634
|
|
|
* database. The records must be from one of the sites that are |
635
|
|
|
* configured via the context item. If the current site is part of |
636
|
|
|
* a tree of sites, the SELECT statement can retrieve all records |
637
|
|
|
* from the current site and the complete sub-tree of sites. |
638
|
|
|
* |
639
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
640
|
|
|
* their tables must be joined in the SQL context. This is done by |
641
|
|
|
* using the "internaldeps" property from the definition of the ID |
642
|
|
|
* column of the sub-managers. These internal dependencies specify |
643
|
|
|
* the JOIN between the tables and the used columns for joining. The |
644
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
645
|
|
|
* the sub-managers. |
646
|
|
|
* |
647
|
|
|
* To limit the records matched, conditions can be added to the given |
648
|
|
|
* criteria object. It can contain comparisons like column names that |
649
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
650
|
|
|
* operators. The resulting string of SQL conditions replaces the |
651
|
|
|
* ":cond" placeholder before the statement is sent to the database |
652
|
|
|
* server. |
653
|
|
|
* |
654
|
|
|
* If the records that are retrieved should be ordered by one or more |
655
|
|
|
* columns, the generated string of column / sort direction pairs |
656
|
|
|
* replaces the ":order" placeholder. In case no ordering is required, |
657
|
|
|
* the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/" |
658
|
|
|
* markers is removed to speed up retrieving the records. Columns of |
659
|
|
|
* sub-managers can also be used for ordering the result set but then |
660
|
|
|
* no index can be used. |
661
|
|
|
* |
662
|
|
|
* The number of returned records can be limited and can start at any |
663
|
|
|
* number between the begining and the end of the result set. For that |
664
|
|
|
* the ":size" and ":start" placeholders are replaced by the |
665
|
|
|
* corresponding values from the criteria object. The default values |
666
|
|
|
* are 0 for the start and 100 for the size value. |
667
|
|
|
* |
668
|
|
|
* The SQL statement should conform to the ANSI standard to be |
669
|
|
|
* compatible with most relational database systems. This also |
670
|
|
|
* includes using double quotes for table and column names. |
671
|
|
|
* |
672
|
|
|
* @param string SQL statement for searching items |
673
|
|
|
* @since 2014.03 |
674
|
|
|
* @category Developer |
675
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
676
|
|
|
* @see mshop/price/manager/standard/update/ansi |
677
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
678
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
679
|
|
|
* @see mshop/price/manager/standard/count/ansi |
680
|
|
|
*/ |
681
|
|
|
$cfgPathSearch = 'mshop/price/manager/standard/search'; |
682
|
|
|
|
683
|
|
|
/** mshop/price/manager/standard/count/mysql |
684
|
|
|
* Counts the number of records matched by the given criteria in the database |
685
|
|
|
* |
686
|
|
|
* @see mshop/price/manager/standard/count/ansi |
687
|
|
|
*/ |
688
|
|
|
|
689
|
|
|
/** mshop/price/manager/standard/count/ansi |
690
|
|
|
* Counts the number of records matched by the given criteria in the database |
691
|
|
|
* |
692
|
|
|
* Counts all records matched by the given criteria from the price |
693
|
|
|
* database. The records must be from one of the sites that are |
694
|
|
|
* configured via the context item. If the current site is part of |
695
|
|
|
* a tree of sites, the statement can count all records from the |
696
|
|
|
* current site and the complete sub-tree of sites. |
697
|
|
|
* |
698
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
699
|
|
|
* their tables must be joined in the SQL context. This is done by |
700
|
|
|
* using the "internaldeps" property from the definition of the ID |
701
|
|
|
* column of the sub-managers. These internal dependencies specify |
702
|
|
|
* the JOIN between the tables and the used columns for joining. The |
703
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
704
|
|
|
* the sub-managers. |
705
|
|
|
* |
706
|
|
|
* To limit the records matched, conditions can be added to the given |
707
|
|
|
* criteria object. It can contain comparisons like column names that |
708
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
709
|
|
|
* operators. The resulting string of SQL conditions replaces the |
710
|
|
|
* ":cond" placeholder before the statement is sent to the database |
711
|
|
|
* server. |
712
|
|
|
* |
713
|
|
|
* Both, the strings for ":joins" and for ":cond" are the same as for |
714
|
|
|
* the "search" SQL statement. |
715
|
|
|
* |
716
|
|
|
* Contrary to the "search" statement, it doesn't return any records |
717
|
|
|
* but instead the number of records that have been found. As counting |
718
|
|
|
* thousands of records can be a long running task, the maximum number |
719
|
|
|
* of counted records is limited for performance reasons. |
720
|
|
|
* |
721
|
|
|
* The SQL statement should conform to the ANSI standard to be |
722
|
|
|
* compatible with most relational database systems. This also |
723
|
|
|
* includes using double quotes for table and column names. |
724
|
|
|
* |
725
|
|
|
* @param string SQL statement for counting items |
726
|
|
|
* @since 2014.03 |
727
|
|
|
* @category Developer |
728
|
|
|
* @see mshop/price/manager/standard/insert/ansi |
729
|
|
|
* @see mshop/price/manager/standard/update/ansi |
730
|
|
|
* @see mshop/price/manager/standard/newid/ansi |
731
|
|
|
* @see mshop/price/manager/standard/delete/ansi |
732
|
|
|
* @see mshop/price/manager/standard/search/ansi |
733
|
|
|
*/ |
734
|
|
|
$cfgPathCount = 'mshop/price/manager/standard/count'; |
735
|
|
|
|
736
|
|
|
$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
737
|
|
|
|
738
|
|
|
while( ( $row = $results->fetch() ) !== false ) { |
739
|
|
|
$map[$row['price.id']] = $row; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
$dbm->release( $conn, $dbname ); |
743
|
|
|
} |
744
|
|
|
catch( \Exception $e ) |
745
|
|
|
{ |
746
|
|
|
$dbm->release( $conn, $dbname ); |
747
|
|
|
throw $e; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
return $this->buildItems( $map, null, 'price' ); |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Creates a search critera object |
756
|
|
|
* |
757
|
|
|
* @param boolean $default Add default criteria (optional) |
758
|
|
|
* @return \Aimeos\MW\Criteria\Iface New search criteria object |
759
|
|
|
*/ |
760
|
|
|
public function createSearch( $default = false ) |
761
|
|
|
{ |
762
|
|
|
if( $default === true ) |
763
|
|
|
{ |
764
|
|
|
$object = $this->createSearchBase( 'price' ); |
765
|
|
|
$currencyid = $this->getContext()->getLocale()->getCurrencyId(); |
766
|
|
|
|
767
|
|
|
if( $currencyid !== null ) |
768
|
|
|
{ |
769
|
|
|
$expr = array( |
770
|
|
|
$object->compare( '==', 'price.currencyid', $currencyid ), |
771
|
|
|
$object->getConditions(), |
772
|
|
|
); |
773
|
|
|
|
774
|
|
|
$object->setConditions( $object->combine( '&&', $expr ) ); |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
return $object; |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
return parent::createSearch(); |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* Returns a new manager for price extensions. |
786
|
|
|
* |
787
|
|
|
* @param string $manager Name of the sub manager type in lower case |
788
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
789
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g type, etc. |
790
|
|
|
*/ |
791
|
|
|
public function getSubManager( $manager, $name = null ) |
792
|
|
|
{ |
793
|
|
|
return $this->getSubManagerBase( 'price', $manager, $name ); |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
|
797
|
|
|
/** |
798
|
|
|
* Creates a new price item |
799
|
|
|
* |
800
|
|
|
* @param array $values List of attributes for price item |
801
|
|
|
* @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items |
802
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items |
803
|
|
|
* @return \Aimeos\MShop\Price\Item\Iface New price item |
804
|
|
|
*/ |
805
|
|
|
protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [] ) |
806
|
|
|
{ |
807
|
|
|
$values['currencyid'] = $this->currencyId; |
808
|
|
|
|
809
|
|
|
if( !isset( $values['price.taxflag'] ) ) { |
810
|
|
|
$values['price.taxflag'] = $this->taxflag; |
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
return new \Aimeos\MShop\Price\Item\Standard( $values, $listItems, $refItems, $this->precision ); |
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
|