|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018-2022 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Order |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Order\Email\Voucher; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Order voucher e-mail job controller. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Order |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Controller\Jobs\Base |
|
22
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
use \Aimeos\Controller\Jobs\Mail; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
private $couponId; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns the localized name of the job. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string Name of the job |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getName() : string |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->context()->translate( 'controller/jobs', 'Voucher related e-mails' ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the localized description of the job. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string Description of the job |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getDescription() : string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->context()->translate( 'controller/jobs', 'Sends the e-mail with the voucher to the customer' ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Executes the job. |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
|
56
|
|
|
*/ |
|
57
|
|
|
public function run() |
|
58
|
|
|
{ |
|
59
|
|
|
$context = $this->context(); |
|
60
|
|
|
$config = $context->config(); |
|
61
|
|
|
|
|
62
|
|
|
/** controller/jobs/order/email/voucher/limit-days |
|
63
|
|
|
* Only send voucher e-mails of orders that were created in the past within the configured number of days |
|
64
|
|
|
* |
|
65
|
|
|
* The voucher e-mails are normally send immediately after the voucher |
|
66
|
|
|
* has been ordered. This option prevents e-mails for old orders from |
|
67
|
|
|
* being send in case anything went wrong or an update failed to avoid |
|
68
|
|
|
* confusion of customers. |
|
69
|
|
|
* |
|
70
|
|
|
* @param integer Number of days |
|
71
|
|
|
* @since 2018.07 |
|
72
|
|
|
* @category User |
|
73
|
|
|
* @category Developer |
|
74
|
|
|
* @see controller/jobs/order/email/voucher/status |
|
75
|
|
|
*/ |
|
76
|
|
|
$limit = $config->get( 'controller/jobs/order/email/voucher/limit-days', 30 ); |
|
77
|
|
|
$limitDate = date( 'Y-m-d H:i:s', time() - $limit * 86400 ); |
|
78
|
|
|
|
|
79
|
|
|
/** controller/jobs/order/email/voucher/status |
|
80
|
|
|
* Only send e-mails containing voucher for these payment status values |
|
81
|
|
|
* |
|
82
|
|
|
* E-mail containing vouchers can be sent for these payment status values: |
|
83
|
|
|
* |
|
84
|
|
|
* * 0: deleted |
|
85
|
|
|
* * 1: canceled |
|
86
|
|
|
* * 2: refused |
|
87
|
|
|
* * 3: refund |
|
88
|
|
|
* * 4: pending |
|
89
|
|
|
* * 5: authorized |
|
90
|
|
|
* * 6: received |
|
91
|
|
|
* |
|
92
|
|
|
* @param integer Payment status constant |
|
93
|
|
|
* @since 2018.07 |
|
94
|
|
|
* @category User |
|
95
|
|
|
* @category Developer |
|
96
|
|
|
* @see controller/jobs/order/email/voucher/limit-days |
|
97
|
|
|
*/ |
|
98
|
|
|
$status = $config->get( 'controller/jobs/order/email/voucher/status', \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED ); |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$manager = \Aimeos\MShop::create( $context, 'order' ); |
|
102
|
|
|
|
|
103
|
|
|
$filter = $manager->filter(); |
|
104
|
|
|
$func = $filter->make( 'order:status', [\Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER, '1'] ); |
|
105
|
|
|
$filter->add( [ |
|
106
|
|
|
$filter->compare( '>=', 'order.mtime', $limitDate ), |
|
107
|
|
|
$filter->compare( '==', 'order.statuspayment', $status ), |
|
108
|
|
|
$filter->compare( '==', 'order.base.product.type', 'voucher' ), |
|
109
|
|
|
$filter->compare( '==', $func, 0 ), |
|
110
|
|
|
] ); |
|
111
|
|
|
|
|
112
|
|
|
$start = 0; |
|
113
|
|
|
|
|
114
|
|
|
do |
|
115
|
|
|
{ |
|
116
|
|
|
$items = $manager->search( $filter->slice( $start ), ['order/base', 'order/base/addres', 'order/base/product'] ); |
|
117
|
|
|
|
|
118
|
|
|
$this->notify( $items ); |
|
119
|
|
|
|
|
120
|
|
|
$count = count( $items ); |
|
121
|
|
|
$start += $count; |
|
122
|
|
|
} |
|
123
|
|
|
while( $count >= $filter->getLimit() ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the delivery address item of the order |
|
129
|
|
|
* |
|
130
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem Order including address items |
|
131
|
|
|
* @return \Aimeos\MShop\Order\Item\Base\Address\Iface Delivery or voucher address item |
|
132
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If no address item is available |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function address( \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem ) : \Aimeos\MShop\Order\Item\Base\Address\Iface |
|
135
|
|
|
{ |
|
136
|
|
|
$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY; |
|
137
|
|
|
if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) { |
|
138
|
|
|
return $addr; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
|
142
|
|
|
if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) { |
|
143
|
|
|
return $addr; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$msg = sprintf( 'No e-mail address found in order base with ID "%1$s"', $orderBaseItem->getId() ); |
|
147
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Creates coupon codes for the bought vouchers |
|
153
|
|
|
* |
|
154
|
|
|
* @param \Aimeos\Map $orderProdItems Complete order including addresses, products, services |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function createCoupons( \Aimeos\Map $orderProdItems ) |
|
157
|
|
|
{ |
|
158
|
|
|
$map = []; |
|
159
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'order/base/product/attribute' ); |
|
160
|
|
|
|
|
161
|
|
|
foreach( $orderProdItems as $orderProductItem ) |
|
162
|
|
|
{ |
|
163
|
|
|
if( $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) { |
|
164
|
|
|
continue; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$codes = []; |
|
168
|
|
|
|
|
169
|
|
|
for( $i = 0; $i < $orderProductItem->getQuantity(); $i++ ) |
|
170
|
|
|
{ |
|
171
|
|
|
$str = $i . getmypid() . microtime( true ) . $orderProductItem->getId(); |
|
172
|
|
|
$code = substr( strtoupper( sha1( $str ) ), -8 ); |
|
173
|
|
|
$map[$code] = $orderProductItem->getId(); |
|
174
|
|
|
$codes[] = $code; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$item = $manager->create()->setCode( 'coupon-code' )->setType( 'coupon' )->setValue( $codes ); |
|
178
|
|
|
$orderProductItem->setAttributeItem( $item ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->saveCoupons( $map ); |
|
182
|
|
|
return $orderProdItems; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the coupon ID for the voucher coupon |
|
188
|
|
|
* |
|
189
|
|
|
* @return string Unique ID of the coupon item |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function couponId() : string |
|
192
|
|
|
{ |
|
193
|
|
|
if( !isset( $this->couponId ) ) |
|
194
|
|
|
{ |
|
195
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'coupon' ); |
|
196
|
|
|
$filter = $manager->filter()->add( 'coupon.provider', '=~', 'Voucher' )->slice( 0, 1 ); |
|
197
|
|
|
|
|
198
|
|
|
if( ( $item = $manager->search( $filter )->first() ) === null ) { |
|
199
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( 'No coupon provider "Voucher" available' ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->couponId = $item->getId(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $this->couponId; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Sends the voucher e-mail for the given orders |
|
211
|
|
|
* |
|
212
|
|
|
* @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function notify( \Aimeos\Map $items ) |
|
215
|
|
|
{ |
|
216
|
|
|
$context = $this->context(); |
|
217
|
|
|
$sites = $this->sites( $items->getBaseItem()->getSiteId()->unique() ); |
|
218
|
|
|
|
|
219
|
|
|
$couponManager = \Aimeos\MShop::create( $context, 'coupon' ); |
|
220
|
|
|
$orderProdManager = \Aimeos\MShop::create( $context, 'order/base/product' ); |
|
221
|
|
|
|
|
222
|
|
|
foreach( $items as $id => $item ) |
|
223
|
|
|
{ |
|
224
|
|
|
$couponManager->begin(); |
|
225
|
|
|
$orderProdManager->begin(); |
|
226
|
|
|
|
|
227
|
|
|
try |
|
228
|
|
|
{ |
|
229
|
|
|
$base = $item->getBaseItem(); |
|
230
|
|
|
$orderProdManager->save( $this->createCoupons( $this->products( $base ) ) ); |
|
231
|
|
|
|
|
232
|
|
|
$list = $sites->get( $base->getSiteId(), map() ); |
|
233
|
|
|
$view = $this->view( $base, $list->getTheme()->filter()->last() ); |
|
234
|
|
|
|
|
235
|
|
|
$this->send( $view, $this->products( $base ), $this->address( $base ), $list->getLogo()->filter()->last() ); |
|
236
|
|
|
$this->status( $id ); |
|
237
|
|
|
|
|
238
|
|
|
$orderProdManager->commit(); |
|
239
|
|
|
$couponManager->commit(); |
|
240
|
|
|
|
|
241
|
|
|
$str = sprintf( 'Sent voucher e-mails for order ID "%1$s"', $item->getId() ); |
|
242
|
|
|
$context->logger()->info( $str, 'email/order/voucher' ); |
|
243
|
|
|
} |
|
244
|
|
|
catch( \Exception $e ) |
|
245
|
|
|
{ |
|
246
|
|
|
$orderProdManager->rollback(); |
|
247
|
|
|
$couponManager->rollback(); |
|
248
|
|
|
|
|
249
|
|
|
$str = 'Error while trying to send voucher e-mails for order ID "%1$s": %2$s'; |
|
250
|
|
|
$msg = sprintf( $str, $item->getId(), $e->getMessage() . PHP_EOL . $e->getTraceAsString() ); |
|
251
|
|
|
$context->logger()->info( $msg, 'email/order/voucher' ); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Returns the ordered voucher products from the basket. |
|
259
|
|
|
* |
|
260
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem Basket object |
|
261
|
|
|
* @return \Aimeos\Map List of order product items for the voucher products |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function products( \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem ) : \Aimeos\Map |
|
264
|
|
|
{ |
|
265
|
|
|
$list = []; |
|
266
|
|
|
|
|
267
|
|
|
foreach( $orderBaseItem->getProducts() as $orderProductItem ) |
|
268
|
|
|
{ |
|
269
|
|
|
if( $orderProductItem->getType() === 'voucher' ) { |
|
270
|
|
|
$list[] = $orderProductItem; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
foreach( $orderProductItem->getProducts() as $subProductItem ) |
|
274
|
|
|
{ |
|
275
|
|
|
if( $subProductItem->getType() === 'voucher' ) { |
|
276
|
|
|
$list[] = $subProductItem; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return map( $list ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Saves the given coupon codes |
|
287
|
|
|
* |
|
288
|
|
|
* @param array $map Associative list of coupon codes as keys and reference Ids as values |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function saveCoupons( array $map ) |
|
291
|
|
|
{ |
|
292
|
|
|
$couponId = $this->couponId(); |
|
293
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' ); |
|
294
|
|
|
|
|
295
|
|
|
foreach( $map as $code => $ref ) |
|
296
|
|
|
{ |
|
297
|
|
|
$item = $manager->create()->setParentId( $couponId ) |
|
298
|
|
|
->setCode( $code )->setRef( $ref )->setCount( null ); // unlimited |
|
299
|
|
|
|
|
300
|
|
|
$manager->save( $item ); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Sends the voucher related e-mail for a single order |
|
307
|
|
|
* |
|
308
|
|
|
* @param \Aimeos\MW\View\Iface $view Populated view object |
|
309
|
|
|
* @param \Aimeos\Map $orderProducts List of ordered voucher products |
|
310
|
|
|
* @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item |
|
311
|
|
|
* @param string|null $logoPath Relative path to the logo in the fs-media file system |
|
312
|
|
|
*/ |
|
313
|
|
|
protected function send( \Aimeos\MW\View\Iface $view, \Aimeos\Map $orderProducts, |
|
314
|
|
|
\Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null ) |
|
315
|
|
|
{ |
|
316
|
|
|
$context = $this->context(); |
|
317
|
|
|
$config = $context->config(); |
|
318
|
|
|
$logo = $this->call( 'mailLogo', $logoPath ); |
|
319
|
|
|
|
|
320
|
|
|
foreach( $orderProducts as $orderProductItem ) |
|
321
|
|
|
{ |
|
322
|
|
|
if( !empty( $codes = $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) ) |
|
323
|
|
|
{ |
|
324
|
|
|
foreach( (array) $codes as $code ) |
|
325
|
|
|
{ |
|
326
|
|
|
$view->orderProductItem = $orderProductItem; |
|
327
|
|
|
$view->voucher = $code; |
|
328
|
|
|
|
|
329
|
|
|
$msg = $this->call( 'mailTo', $address ); |
|
330
|
|
|
$view->logo = $msg->embed( $logo, basename( (string) $logoPath ) ); |
|
331
|
|
|
|
|
332
|
|
|
$msg->subject( $context->translate( 'client', 'Your voucher' ) ) |
|
333
|
|
|
->html( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-html', 'order/email/voucher/html' ) ) ) |
|
334
|
|
|
->text( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-text', 'order/email/voucher/text' ) ) ) |
|
335
|
|
|
->send(); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Returns the site items for the given site codes |
|
344
|
|
|
* |
|
345
|
|
|
* @param iterable $siteIds List of site IDs |
|
346
|
|
|
* @return \Aimeos\Map Site items with codes as keys |
|
347
|
|
|
*/ |
|
348
|
|
|
protected function sites( iterable $siteIds ) : \Aimeos\Map |
|
349
|
|
|
{ |
|
350
|
|
|
$map = []; |
|
351
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' ); |
|
352
|
|
|
|
|
353
|
|
|
foreach( $siteIds as $siteId ) |
|
354
|
|
|
{ |
|
355
|
|
|
$list = explode( '.', trim( $siteId, '.' ) ); |
|
356
|
|
|
$map[$siteId] = $manager->getPath( end( $list ) ); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return map( $map ); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Adds the status of the delivered e-mail for the given order ID |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $orderId Unique order ID |
|
367
|
|
|
*/ |
|
368
|
|
|
protected function status( string $orderId ) |
|
369
|
|
|
{ |
|
370
|
|
|
$orderStatusManager = \Aimeos\MShop::create( $this->context(), 'order/status' ); |
|
371
|
|
|
|
|
372
|
|
|
$statusItem = $orderStatusManager->create()->setParentId( $orderId )->setValue( 1 ) |
|
373
|
|
|
->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER ); |
|
374
|
|
|
|
|
375
|
|
|
$orderStatusManager->save( $statusItem ); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Returns the view populated with common data |
|
381
|
|
|
* |
|
382
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $base Basket including addresses |
|
383
|
|
|
* @param string|null $theme Theme name |
|
384
|
|
|
* @return \Aimeos\MW\View\Iface View object |
|
385
|
|
|
*/ |
|
386
|
|
|
protected function view( \Aimeos\MShop\Order\Item\Base\Iface $base, string $theme = null ) : \Aimeos\MW\View\Iface |
|
387
|
|
|
{ |
|
388
|
|
|
$address = $this->address( $base ); |
|
389
|
|
|
$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId(); |
|
390
|
|
|
|
|
391
|
|
|
$view = $this->call( 'mailView', $langId ); |
|
392
|
|
|
$view->intro = $this->call( 'mailIntro', $address ); |
|
393
|
|
|
$view->css = $this->call( 'mailCss', $theme ); |
|
394
|
|
|
$view->address = $address; |
|
395
|
|
|
$view->urlparams = [ |
|
396
|
|
|
'currency' => $base->getPrice()->getCurrencyId(), |
|
397
|
|
|
'site' => $base->getSiteCode(), |
|
398
|
|
|
'locale' => $langId, |
|
399
|
|
|
]; |
|
400
|
|
|
|
|
401
|
|
|
return $view; |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
|