/
Base.php
310 lines (249 loc) · 9.64 KB
/
Base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Metaways Infosystems GmbH, 2011
* @copyright Aimeos (aimeos.org), 2015-2024
* @package MShop
* @subpackage Order
*/
namespace Aimeos\MShop\Order\Manager;
/**
* Basic methods and constants for order items (shopping basket).
*
* @package MShop
* @subpackage Order
*/
abstract class Base extends \Aimeos\MShop\Common\Manager\Base
{
/**
* Returns a new and empty order item (shopping basket).
*
* @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
* @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
* @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
* @param \Aimeos\MShop\Order\Item\Product\Iface[] $products List of ordered product items
* @param \Aimeos\MShop\Order\Item\Address\Iface[] $addresses List of order address items
* @param \Aimeos\MShop\Order\Item\Service\Iface[] $services List of order serviceitems
* @param \Aimeos\MShop\Order\Item\Product\Iface[] $coupons Associative list of coupon codes as keys and items as values
* @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object if requested
* @return \Aimeos\MShop\Order\Item\Iface Order object
*/
abstract protected function createItemBase( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
array $values = [], array $products = [], array $addresses = [], array $services = [], array $coupons = [],
?\Aimeos\MShop\Customer\Item\Iface $custItem = null ) : \Aimeos\MShop\Order\Item\Iface;
/**
* Returns the address item map for the given order IDs
*
* @param string[] $ids List of order IDs
* @param array $ref List of referenced domains that should be fetched too
* @return array Multi-dimensional associative list of order IDs as keys and order address type/item pairs as values
*/
protected function getAddresses( array $ids, array $ref ) : array
{
$items = [];
$manager = $this->object()->getSubManager( 'address' );
$criteria = $manager->filter()->slice( 0, 0x7fffffff );
$criteria->setConditions( $criteria->compare( '==', 'order.address.parentid', $ids ) );
foreach( $manager->search( $criteria, $ref ) as $item ) {
$items[$item->getParentId()][] = $item;
}
return $items;
}
/**
* Returns the coupon map for the given order IDs
*
* @param string[] $ids List of order IDs
* @param array $products Associative list of IDs and order product ID/item pairs as values
* @return array Multi-dimensional associative list of order IDs as keys and coupons with product items as values
*/
protected function getCoupons( array $ids, array $products = [] ) : array
{
$map = $productMap = [];
$manager = $this->object()->getSubManager( 'coupon' );
foreach( $products as $id => $list )
{
if( !isset( $productMap[$id] ) ) {
$productMap[$id] = [];
}
foreach( $list as $key => $product ) {
$productMap[$id][$product->getId()] = $product;
}
}
$criteria = $manager->filter()->slice( 0, 0x7fffffff );
$criteria->setConditions( $criteria->compare( '==', 'order.coupon.parentid', $ids ) );
foreach( $manager->search( $criteria ) as $item )
{
if( !isset( $map[$item->getParentId()][$item->getCode()] ) ) {
$map[$item->getParentId()][$item->getCode()] = [];
}
if( $item->getProductId() !== null && isset( $productMap[$item->getParentId()][$item->getProductId()] ) ) {
$map[$item->getParentId()][$item->getCode()][] = $productMap[$item->getParentId()][$item->getProductId()];
}
}
return $map;
}
/**
* Retrieves the ordered products from the storage.
*
* @param string[] $ids List of order IDs
* @param array $ref List of referenced domains that should be fetched too
* @return array Multi-dimensional associative list of order IDs as keys and order product
* IDs/items pairs in reversed order as values
*/
protected function getProducts( array $ids, array $ref ) : array
{
$map = $attributes = $subProducts = [];
$manager = $this->object()->getSubManager( 'product' );
$attrManager = $manager->getSubManager( 'attribute' );
$criteria = $manager->filter()->slice( 0, 0x7fffffff );
$criteria->setConditions( $criteria->compare( '==', 'order.product.parentid', $ids ) );
$items = $manager->search( $criteria, $ref )->reverse();
$search = $attrManager->filter()->slice( 0, 0x7fffffff );
$search->setConditions( $search->compare( '==', 'order.product.attribute.parentid', $items->keys()->toArray() ) );
foreach( $attrManager->search( $search, $ref ) as $id => $attribute ) {
$attributes[$attribute->getParentId()][$id] = $attribute;
}
foreach( $items as $id => $item )
{
if( isset( $attributes[$id] ) ) {
$item->setAttributeItems( $attributes[$id] );
}
if( $item->getOrderProductId() === null )
{
ksort( $subProducts ); // bring the array into the right order because it's reversed
$item->setProducts( $subProducts );
$map[$item->getParentId()][$item->getPosition()] = $item;
$subProducts = [];
}
else
{ // in case it's a sub-product
$subProducts[$item->getPosition()] = $item;
}
}
foreach( $map as $key => $list ) {
ksort( $map[$key] );
}
return $map;
}
/**
* Retrieves the order services from the storage.
*
* @param string[] $ids List of order IDs
* @param array $ref List of referenced domains that should be fetched too
* @return array Multi-dimensional associative list of order IDs as keys and service type/items pairs as values
*/
protected function getServices( array $ids, array $ref ) : array
{
$map = [];
$manager = $this->object()->getSubManager( 'service' );
$criteria = $manager->filter()->slice( 0, 0x7fffffff );
$criteria->setConditions( $criteria->compare( '==', 'order.service.parentid', $ids ) );
foreach( $manager->search( $criteria, $ref ) as $item ) {
$map[$item->getParentId()][] = $item;
}
return $map;
}
/**
* Saves the addresses of the order to the storage.
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket containing address items
* @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
*/
protected function saveAddresses( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Order\Manager\Iface
{
$addresses = $basket->getAddresses()->flat( 1 );
foreach( $addresses as $address )
{
if( $address->getParentId() != $basket->getId() ) {
$address->setId( null ); // create new item if copied
}
$address->setParentId( $basket->getId() );
}
$this->object()->getSubManager( 'address' )->save( $addresses );
return $this;
}
/**
* Saves the coupons of the order to the storage.
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket containing coupon items
* @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
*/
protected function saveCoupons( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Order\Manager\Iface
{
$manager = $this->object()->getSubManager( 'coupon' );
$filter = $manager->filter()->add( 'order.coupon.parentid', '==', $basket->getId() )->slice( 0, 0x7fffffff );
$items = $manager->search( $filter )->groupBy( 'order.coupon.code' );
foreach( $basket->getCoupons() as $code => $products )
{
if( empty( $products ) )
{
$item = !empty( $items[$code] ) ? current( $items[$code] ) : $manager->create()->setParentId( $basket->getId() );
$manager->save( $item->setCode( $code ) );
continue;
}
foreach( $products as $product )
{
foreach( $items[$code] ?? [] as $prodItem )
{
if( $product->getId() === $prodItem->getId() ) {
continue 2;
}
}
$manager->save( $manager->create()->setParentId( $basket->getId() )->setCode( $code )->setProductId( $product->getId() ) );
}
}
return $this;
}
/**
* Saves the ordered products to the storage.
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket containing ordered products or bundles
* @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
*/
protected function saveProducts( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Order\Manager\Iface
{
$products = $basket->getProducts();
$pos = (int) $products->merge( $products->getProducts()->flat( 1 ) )->max( 'order.product.position' );
foreach( $products as $product )
{
if( $product->getParentId() != $basket->getId() ) {
$product->setId( null ); // create new item if copied
}
if( !$product->getPosition() ) {
$product->setPosition( ++$pos );
}
$product->setParentId( $basket->getId() );
foreach( $product->getProducts() as $subProduct )
{
if( $subProduct->getParentId() != $basket->getId() ) {
$subProduct->setId( null ); // create new item if copied
}
if( !$subProduct->getPosition() ) {
$subProduct->setPosition( ++$pos );
}
$subProduct->setParentId( $basket->getId() );
}
}
$this->object()->getSubManager( 'product' )->save( $products );
return $this;
}
/**
* Saves the services of the order to the storage.
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket containing service items
* @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
*/
protected function saveServices( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Order\Manager\Iface
{
$services = $basket->getServices()->flat( 1 );
foreach( $services as $service )
{
if( $service->getParentId() != $basket->getId() ) {
$service->setId( null ); // create new item if copied
}
$service->setParentId( $basket->getId() );
}
$this->object()->getSubManager( 'service' )->save( $services );
return $this;
}
}