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