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
|
|
|
$siteItems = $this->sites( $items->getBaseItem()->getSiteCode()->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
|
|
|
$orderBaseItem = $item->getBaseItem(); |
230
|
|
|
$orderProdManager->save( $this->createCoupons( $this->products( $orderBaseItem ) ) ); |
231
|
|
|
|
232
|
|
|
$this->status( $id ); |
233
|
|
|
$this->send( $orderBaseItem, $siteItems->get( $orderBaseItem->getSiteCode() ) ); |
|
|
|
|
234
|
|
|
|
235
|
|
|
$orderProdManager->commit(); |
236
|
|
|
$couponManager->commit(); |
237
|
|
|
|
238
|
|
|
$str = sprintf( 'Sent voucher e-mails for order ID "%1$s"', $item->getId() ); |
239
|
|
|
$context->logger()->info( $str, 'email/order/voucher' ); |
240
|
|
|
} |
241
|
|
|
catch( \Exception $e ) |
242
|
|
|
{ |
243
|
|
|
$orderProdManager->rollback(); |
244
|
|
|
$couponManager->rollback(); |
245
|
|
|
|
246
|
|
|
$str = 'Error while trying to send voucher e-mails for order ID "%1$s": %2$s'; |
247
|
|
|
$msg = sprintf( $str, $item->getId(), $e->getMessage() . PHP_EOL . $e->getTraceAsString() ); |
248
|
|
|
$context->logger()->info( $msg, 'email/order/voucher' ); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns the ordered voucher products from the basket. |
256
|
|
|
* |
257
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem Basket object |
258
|
|
|
* @return \Aimeos\Map List of order product items for the voucher products |
259
|
|
|
*/ |
260
|
|
|
protected function products( \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem ) : \Aimeos\Map |
261
|
|
|
{ |
262
|
|
|
$list = []; |
263
|
|
|
|
264
|
|
|
foreach( $orderBaseItem->getProducts() as $orderProductItem ) |
265
|
|
|
{ |
266
|
|
|
if( $orderProductItem->getType() === 'voucher' ) { |
267
|
|
|
$list[] = $orderProductItem; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
foreach( $orderProductItem->getProducts() as $subProductItem ) |
271
|
|
|
{ |
272
|
|
|
if( $subProductItem->getType() === 'voucher' ) { |
273
|
|
|
$list[] = $subProductItem; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return map( $list ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Saves the given coupon codes |
284
|
|
|
* |
285
|
|
|
* @param array $map Associative list of coupon codes as keys and reference Ids as values |
286
|
|
|
*/ |
287
|
|
|
protected function saveCoupons( array $map ) |
288
|
|
|
{ |
289
|
|
|
$couponId = $this->couponId(); |
290
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' ); |
291
|
|
|
|
292
|
|
|
foreach( $map as $code => $ref ) |
293
|
|
|
{ |
294
|
|
|
$item = $manager->create()->setParentId( $couponId ) |
295
|
|
|
->setCode( $code )->setRef( $ref )->setCount( null ); // unlimited |
296
|
|
|
|
297
|
|
|
$manager->save( $item ); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Sends the voucher related e-mail for a single order |
304
|
|
|
* |
305
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem Order including addresses and products |
306
|
|
|
* @param \Aimeos\MShop\Locale\Item\Site\Iface $site Site item |
307
|
|
|
*/ |
308
|
|
|
protected function send( \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem, \Aimeos\MShop\Locale\Item\Site\Iface $site ) |
309
|
|
|
{ |
310
|
|
|
$context = $this->context(); |
311
|
|
|
$config = $context->config(); |
312
|
|
|
$address = $this->address( $orderBaseItem ); |
313
|
|
|
$view = $this->view( $orderBaseItem, $address, $site->getTheme() ); |
314
|
|
|
|
315
|
|
|
$logoPath = $site->getLogo(); |
316
|
|
|
$fs = $context->fs( 'fs-media' ); |
317
|
|
|
$logo = $fs->has( $logoPath ) ? $fs->read( $logoPath ) : ''; |
318
|
|
|
|
319
|
|
|
foreach( $this->products( $orderBaseItem ) as $orderProductItem ) |
320
|
|
|
{ |
321
|
|
|
if( !empty( $codes = $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) ) |
322
|
|
|
{ |
323
|
|
|
foreach( (array) $codes as $code ) |
324
|
|
|
{ |
325
|
|
|
$view->orderProductItem = $orderProductItem; |
326
|
|
|
$view->voucher = $code; |
327
|
|
|
|
328
|
|
|
$msg = $this->call( 'mailTo', $address ); |
329
|
|
|
$view->logo = $msg->embed( $logo, '', basename( $logoPath ) ); |
330
|
|
|
|
331
|
|
|
$msg->subject( $context->translate( 'client', 'Your voucher' ) ) |
332
|
|
|
->html( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-html', 'order/email/voucher/html' ) ) ) |
333
|
|
|
->text( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-text', 'order/email/voucher/text' ) ) ) |
334
|
|
|
->send(); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Returns the site items for the given site codes |
343
|
|
|
* |
344
|
|
|
* @param \Aimeos\Map $codes Unique site codes |
345
|
|
|
* @return \Aimeos\Map Site items with codes as keys |
346
|
|
|
*/ |
347
|
|
|
protected function sites( \Aimeos\Map $codes ) : \Aimeos\Map |
348
|
|
|
{ |
349
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' ); |
350
|
|
|
|
351
|
|
|
$filter = $manager->filter() |
352
|
|
|
->add( ['locale.site.code' => $codes] ) |
353
|
|
|
->slice( 0, $codes->count() ); |
354
|
|
|
|
355
|
|
|
return $manager->search( $filter )->col( null, 'code' ); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Adds the status of the delivered e-mail for the given order ID |
361
|
|
|
* |
362
|
|
|
* @param string $orderId Unique order ID |
363
|
|
|
*/ |
364
|
|
|
protected function status( string $orderId ) |
365
|
|
|
{ |
366
|
|
|
$orderStatusManager = \Aimeos\MShop::create( $this->context(), 'order/status' ); |
367
|
|
|
|
368
|
|
|
$statusItem = $orderStatusManager->create()->setParentId( $orderId )->setValue( 1 ) |
369
|
|
|
->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER ); |
370
|
|
|
|
371
|
|
|
$orderStatusManager->save( $statusItem ); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Returns the view populated with common data |
377
|
|
|
* |
378
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem Order including addresses and products |
379
|
|
|
* @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item |
380
|
|
|
* @return \Aimeos\MW\View\Iface View object |
381
|
|
|
*/ |
382
|
|
|
protected function view( \Aimeos\MShop\Order\Item\Base\Iface $orderBaseItem, |
383
|
|
|
\Aimeos\MShop\Common\Item\Address\Iface $address, string $theme = null ) : \Aimeos\MW\View\Iface |
384
|
|
|
{ |
385
|
|
|
$theme = $theme ?: 'default'; |
386
|
|
|
$fs = $this->context()->fs( 'fs-theme' ); |
387
|
|
|
$css = $fs->has( $theme . '/email.css' ) ? $fs->read( $theme . '/email.css' ) : null; |
388
|
|
|
|
389
|
|
|
$view = $this->call( 'mailView', $address->getLanguageId() ); |
390
|
|
|
$view->intro = $this->call( 'mailIntro', $address ); |
391
|
|
|
$view->addressItem = $address; |
392
|
|
|
$view->css = $css; |
393
|
|
|
$view->urlparams = [ |
394
|
|
|
'locale' => $address->getLanguageId() ?: $orderBaseItem->locale()->getLanguageId(), |
395
|
|
|
'currency' => $orderBaseItem->getPrice()->getCurrencyId(), |
396
|
|
|
'site' => $orderBaseItem->getSiteCode(), |
397
|
|
|
]; |
398
|
|
|
|
399
|
|
|
return $view; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|