1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
7
|
|
|
* @package Controller |
8
|
|
|
* @subpackage Frontend |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\Controller\Frontend\Order; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default implementation of the order frontend controller. |
17
|
|
|
* |
18
|
|
|
* @package Controller |
19
|
|
|
* @subpackage Frontend |
20
|
|
|
*/ |
21
|
|
|
class Standard |
22
|
|
|
extends \Aimeos\Controller\Frontend\Base |
23
|
|
|
implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Creates and adds a new order for the given order base ID |
27
|
|
|
* |
28
|
|
|
* @param string $baseId Unique ID of the saved basket |
29
|
|
|
* @param string $type Arbitrary order type (max. eight chars) |
30
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Created order object |
31
|
|
|
*/ |
32
|
|
|
public function addItem( $baseId, $type ) |
33
|
|
|
{ |
34
|
|
|
$total = 0; |
35
|
|
|
$context = $this->getContext(); |
36
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
37
|
|
|
|
38
|
|
|
/** controller/frontend/order/limit-seconds |
39
|
|
|
* Order limitation time frame in seconds |
40
|
|
|
* |
41
|
|
|
* Creating new orders is limited to avoid abuse and mitigate denial of |
42
|
|
|
* service attacks. Within the configured time frame, only one order |
43
|
|
|
* item can be created per order base item. All orders for the order |
44
|
|
|
* base item within the last X seconds are counted. If there's already |
45
|
|
|
* one available, an error message will be shown to the user instead of |
46
|
|
|
* creating the new order item. |
47
|
|
|
* |
48
|
|
|
* @param integer Number of seconds to check order items within |
49
|
|
|
* @since 2017.05 |
50
|
|
|
* @category Developer |
51
|
|
|
* @see controller/frontend/basket/limit-count |
52
|
|
|
* @see controller/frontend/basket/limit-seconds |
53
|
|
|
*/ |
54
|
|
|
$seconds = $context->getConfig()->get( 'controller/frontend/order/limit-seconds', 300 ); |
55
|
|
|
|
56
|
|
|
$search = $manager->createSearch(); |
57
|
|
|
$expr = [ |
58
|
|
|
$search->compare( '==', 'order.baseid', $baseId ), |
59
|
|
|
$search->compare( '>=', 'order.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ), |
60
|
|
|
]; |
61
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
62
|
|
|
$search->setSlice( 0, 0 ); |
63
|
|
|
|
64
|
|
|
$manager->searchItems( $search, [], $total ); |
65
|
|
|
|
66
|
|
|
if( $total > 0 ) { |
67
|
|
|
throw new \Aimeos\Controller\Frontend\Order\Exception( sprintf( 'The order has already been created' ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$item = $manager->createItem()->setBaseId( $baseId )->setType( $type ); |
71
|
|
|
return $manager->saveItem( $item ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the filter for searching items |
77
|
|
|
* |
78
|
|
|
* @return \Aimeos\MW\Criteria\Iface Filter object |
79
|
|
|
*/ |
80
|
|
|
public function createFilter() |
81
|
|
|
{ |
82
|
|
|
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order' )->createSearch( true ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the order item for the given ID |
88
|
|
|
* |
89
|
|
|
* @param string $id Unique order ID |
90
|
|
|
* @param boolean $default Use default criteria to limit orders |
91
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Order object |
92
|
|
|
*/ |
93
|
|
|
public function getItem( $id, $default = true ) |
94
|
|
|
{ |
95
|
|
|
$context = $this->getContext(); |
96
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
97
|
|
|
|
98
|
|
|
$search = $manager->createSearch( true ); |
99
|
|
|
$expr = [ |
100
|
|
|
$search->compare( '==', 'order.id', $id ), |
101
|
|
|
$search->getConditions(), |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
if( $default !== false ) { |
105
|
|
|
$expr[] = $search->compare( '==', 'order.editor', $context->getEditor() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
109
|
|
|
|
110
|
|
|
$items = $manager->searchItems( $search ); |
111
|
|
|
|
112
|
|
|
if( ( $item = reset( $items ) ) !== false ) { |
113
|
|
|
return $item; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw new \Aimeos\Controller\Frontend\Order\Exception( sprintf( 'No order item for ID "%1$s" found', $id ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Saves the modified order item |
122
|
|
|
* |
123
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $item Order object |
124
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Saved order item |
125
|
|
|
*/ |
126
|
|
|
public function saveItem( \Aimeos\MShop\Order\Item\Iface $item ) |
127
|
|
|
{ |
128
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order' ); |
129
|
|
|
return $manager->saveItem( $item ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the order items based on the given filter that belong to the current user |
135
|
|
|
* |
136
|
|
|
* @param \Aimeos\MW\Criteria\Iface Filter object |
137
|
|
|
* @param integer|null &$total Variable that will contain the total number of available items |
138
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface[] Associative list of IDs as keys and order objects as values |
139
|
|
|
*/ |
140
|
|
|
public function searchItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null ) |
141
|
|
|
{ |
142
|
|
|
$context = $this->getContext(); |
143
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
144
|
|
|
|
145
|
|
|
$expr = [ |
146
|
|
|
$filter->getConditions(), |
147
|
|
|
$filter->compare( '==', 'order.base.customerid', $context->getUserId() ), |
148
|
|
|
]; |
149
|
|
|
$filter->setConditions( $filter->combine( '&&', $expr ) ); |
150
|
|
|
|
151
|
|
|
return $manager->searchItems( $filter, [], $total ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Blocks the resources listed in the order. |
157
|
|
|
* |
158
|
|
|
* Every order contains resources like products or redeemed coupon codes |
159
|
|
|
* that must be blocked so they can't be used by another customer in a |
160
|
|
|
* later order. This method reduces the the stock level of products, the |
161
|
|
|
* counts of coupon codes and others. |
162
|
|
|
* |
163
|
|
|
* It's save to call this method multiple times for one order. In this case, |
164
|
|
|
* the actions will be executed only once. All subsequent calls will do |
165
|
|
|
* nothing as long as the resources haven't been unblocked in the meantime. |
166
|
|
|
* |
167
|
|
|
* You can also block and unblock resources several times. Please keep in |
168
|
|
|
* mind that unblocked resources may be reused by other orders in the |
169
|
|
|
* meantime. This can lead to an oversell of products! |
170
|
|
|
* |
171
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
172
|
|
|
*/ |
173
|
|
|
public function block( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
174
|
|
|
{ |
175
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->block( $orderItem ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Frees the resources listed in the order. |
181
|
|
|
* |
182
|
|
|
* If customers created orders but didn't pay for them, the blocked resources |
183
|
|
|
* like products and redeemed coupon codes must be unblocked so they can be |
184
|
|
|
* ordered again or used by other customers. This method increased the stock |
185
|
|
|
* level of products, the counts of coupon codes and others. |
186
|
|
|
* |
187
|
|
|
* It's save to call this method multiple times for one order. In this case, |
188
|
|
|
* the actions will be executed only once. All subsequent calls will do |
189
|
|
|
* nothing as long as the resources haven't been blocked in the meantime. |
190
|
|
|
* |
191
|
|
|
* You can also unblock and block resources several times. Please keep in |
192
|
|
|
* mind that unblocked resources may be reused by other orders in the |
193
|
|
|
* meantime. This can lead to an oversell of products! |
194
|
|
|
* |
195
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
196
|
|
|
*/ |
197
|
|
|
public function unblock( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
198
|
|
|
{ |
199
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->unblock( $orderItem ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Blocks or frees the resources listed in the order if necessary. |
205
|
|
|
* |
206
|
|
|
* After payment status updates, the resources like products or coupon |
207
|
|
|
* codes listed in the order must be blocked or unblocked. This method |
208
|
|
|
* cares about executing the appropriate action depending on the payment |
209
|
|
|
* status. |
210
|
|
|
* |
211
|
|
|
* It's save to call this method multiple times for one order. In this case, |
212
|
|
|
* the actions will be executed only once. All subsequent calls will do |
213
|
|
|
* nothing as long as the payment status hasn't changed in the meantime. |
214
|
|
|
* |
215
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
216
|
|
|
*/ |
217
|
|
|
public function update( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
218
|
|
|
{ |
219
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->update( $orderItem ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Creates a new order from the given basket. |
225
|
|
|
* |
226
|
|
|
* Saves the given basket to the storage including the addresses, coupons, |
227
|
|
|
* products, services, etc. and creates/stores a new order item for that |
228
|
|
|
* order. |
229
|
|
|
* |
230
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object to be stored |
231
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Order item that belongs to the stored basket |
232
|
|
|
* @deprecated 2017.04 Use store() from basket controller instead |
233
|
|
|
*/ |
234
|
|
|
public function store( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
235
|
|
|
{ |
236
|
|
|
$context = $this->getContext(); |
237
|
|
|
|
238
|
|
|
$orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
239
|
|
|
$orderBaseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' ); |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
$orderBaseManager->begin(); |
243
|
|
|
$orderBaseManager->store( $basket ); |
244
|
|
|
$orderBaseManager->commit(); |
245
|
|
|
|
246
|
|
|
$orderItem = $orderManager->createItem(); |
247
|
|
|
$orderItem->setBaseId( $basket->getId() ); |
248
|
|
|
$orderItem->setType( \Aimeos\MShop\Order\Item\Base::TYPE_WEB ); |
249
|
|
|
|
250
|
|
|
return $orderManager->saveItem( $orderItem ); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|