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
|
|
|
$manager->saveItem( $item ); |
72
|
|
|
|
73
|
|
|
return $item; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the filter for searching items |
79
|
|
|
* |
80
|
|
|
* @return \Aimeos\MW\Criteria\Iface Filter object |
81
|
|
|
*/ |
82
|
|
|
public function createFilter() |
83
|
|
|
{ |
84
|
|
|
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order' )->createSearch( true ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the order item for the given ID |
90
|
|
|
* |
91
|
|
|
* @param string $id Unique order ID |
92
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Order object |
93
|
|
|
*/ |
94
|
|
|
public function getItem( $id ) |
95
|
|
|
{ |
96
|
|
|
$context = $this->getContext(); |
97
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
98
|
|
|
|
99
|
|
|
$search = $manager->createSearch( true ); |
100
|
|
|
$expr = [ |
101
|
|
|
$search->compare( '==', 'order.id', $id ), |
102
|
|
|
$search->compare( '==', 'order.base.customerid', (string) $context->getUserId() ), |
103
|
|
|
$search->getConditions(), |
104
|
|
|
]; |
105
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
106
|
|
|
|
107
|
|
|
$items = $manager->searchItems( $search ); |
108
|
|
|
|
109
|
|
|
if( ( $item = reset( $items ) ) !== false ) { |
110
|
|
|
return $item; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new \Aimeos\Controller\Frontend\Order\Exception( sprintf( 'No order item for ID "%1$s" found', $id ) ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the order items based on the given filter that belong to the current user |
119
|
|
|
* |
120
|
|
|
* @param \Aimeos\MW\Criteria\Iface Filter object |
121
|
|
|
* @param integer|null &$total Variable that will contain the total number of available items |
122
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface[] Associative list of IDs as keys and order objects as values |
123
|
|
|
*/ |
124
|
|
|
public function searchItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null ) |
125
|
|
|
{ |
126
|
|
|
$context = $this->getContext(); |
127
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
128
|
|
|
|
129
|
|
|
$expr = [ |
130
|
|
|
$filter->getConditions(), |
131
|
|
|
$filter->compare( '==', 'order.base.customerid', $context->getUserId() ), |
132
|
|
|
]; |
133
|
|
|
$filter->setConditions( $filter->combine( '&&', $expr ) ); |
134
|
|
|
|
135
|
|
|
return $manager->searchItems( $filter, [], $total ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Blocks the resources listed in the order. |
141
|
|
|
* |
142
|
|
|
* Every order contains resources like products or redeemed coupon codes |
143
|
|
|
* that must be blocked so they can't be used by another customer in a |
144
|
|
|
* later order. This method reduces the the stock level of products, the |
145
|
|
|
* counts of coupon codes and others. |
146
|
|
|
* |
147
|
|
|
* It's save to call this method multiple times for one order. In this case, |
148
|
|
|
* the actions will be executed only once. All subsequent calls will do |
149
|
|
|
* nothing as long as the resources haven't been unblocked in the meantime. |
150
|
|
|
* |
151
|
|
|
* You can also block and unblock resources several times. Please keep in |
152
|
|
|
* mind that unblocked resources may be reused by other orders in the |
153
|
|
|
* meantime. This can lead to an oversell of products! |
154
|
|
|
* |
155
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
156
|
|
|
*/ |
157
|
|
|
public function block( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
158
|
|
|
{ |
159
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->block( $orderItem ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Frees the resources listed in the order. |
165
|
|
|
* |
166
|
|
|
* If customers created orders but didn't pay for them, the blocked resources |
167
|
|
|
* like products and redeemed coupon codes must be unblocked so they can be |
168
|
|
|
* ordered again or used by other customers. This method increased the stock |
169
|
|
|
* level of products, the counts of coupon codes and others. |
170
|
|
|
* |
171
|
|
|
* It's save to call this method multiple times for one order. In this case, |
172
|
|
|
* the actions will be executed only once. All subsequent calls will do |
173
|
|
|
* nothing as long as the resources haven't been blocked in the meantime. |
174
|
|
|
* |
175
|
|
|
* You can also unblock and block resources several times. Please keep in |
176
|
|
|
* mind that unblocked resources may be reused by other orders in the |
177
|
|
|
* meantime. This can lead to an oversell of products! |
178
|
|
|
* |
179
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
180
|
|
|
*/ |
181
|
|
|
public function unblock( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
182
|
|
|
{ |
183
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->unblock( $orderItem ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Blocks or frees the resources listed in the order if necessary. |
189
|
|
|
* |
190
|
|
|
* After payment status updates, the resources like products or coupon |
191
|
|
|
* codes listed in the order must be blocked or unblocked. This method |
192
|
|
|
* cares about executing the appropriate action depending on the payment |
193
|
|
|
* status. |
194
|
|
|
* |
195
|
|
|
* It's save to call this method multiple times for one order. In this case, |
196
|
|
|
* the actions will be executed only once. All subsequent calls will do |
197
|
|
|
* nothing as long as the payment status hasn't changed in the meantime. |
198
|
|
|
* |
199
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
200
|
|
|
*/ |
201
|
|
|
public function update( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
202
|
|
|
{ |
203
|
|
|
\Aimeos\Controller\Common\Order\Factory::createController( $this->getContext() )->update( $orderItem ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Creates a new order from the given basket. |
209
|
|
|
* |
210
|
|
|
* Saves the given basket to the storage including the addresses, coupons, |
211
|
|
|
* products, services, etc. and creates/stores a new order item for that |
212
|
|
|
* order. |
213
|
|
|
* |
214
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object to be stored |
215
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Order item that belongs to the stored basket |
216
|
|
|
* @deprecated 2017.04 Use store() from basket controller instead |
217
|
|
|
*/ |
218
|
|
|
public function store( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
219
|
|
|
{ |
220
|
|
|
$context = $this->getContext(); |
221
|
|
|
|
222
|
|
|
$orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
223
|
|
|
$orderBaseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' ); |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
$orderBaseManager->begin(); |
227
|
|
|
$orderBaseManager->store( $basket ); |
228
|
|
|
$orderBaseManager->commit(); |
229
|
|
|
|
230
|
|
|
$orderItem = $orderManager->createItem(); |
231
|
|
|
$orderItem->setBaseId( $basket->getId() ); |
232
|
|
|
$orderItem->setType( \Aimeos\MShop\Order\Item\Base::TYPE_WEB ); |
233
|
|
|
$orderManager->saveItem( $orderItem ); |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
return $orderItem; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|