|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2022 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Order |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Order\Email\Delivery; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Order delivery 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
|
|
|
/** |
|
28
|
|
|
* Returns the localized name of the job. |
|
29
|
|
|
* |
|
30
|
|
|
* @return string Name of the job |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getName() : string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->context()->translate( 'controller/jobs', 'Order delivery related e-mails' ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the localized description of the job. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string Description of the job |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getDescription() : string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->context()->translate( 'controller/jobs', 'Sends order delivery status update e-mails' ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Executes the job. |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
|
53
|
|
|
*/ |
|
54
|
|
|
public function run() |
|
55
|
|
|
{ |
|
56
|
|
|
$context = $this->context(); |
|
57
|
|
|
$config = $context->config(); |
|
58
|
|
|
|
|
59
|
|
|
$orderManager = \Aimeos\MShop::create( $context, 'order' ); |
|
60
|
|
|
|
|
61
|
|
|
/** controller/jobs/order/email/delivery/limit-days |
|
62
|
|
|
* Only send delivery e-mails of orders that were created in the past within the configured number of days |
|
63
|
|
|
* |
|
64
|
|
|
* The delivery e-mails are normally send immediately after the delivery |
|
65
|
|
|
* status has changed. This option prevents e-mails for old order from |
|
66
|
|
|
* being send in case anything went wrong or an update failed to avoid |
|
67
|
|
|
* confusion of customers. |
|
68
|
|
|
* |
|
69
|
|
|
* @param integer Number of days |
|
70
|
|
|
* @since 2014.03 |
|
71
|
|
|
* @category User |
|
72
|
|
|
* @category Developer |
|
73
|
|
|
* @see controller/jobs/order/email/delivery/status |
|
74
|
|
|
* @see controller/jobs/order/email/payment/limit-days |
|
75
|
|
|
* @see controller/jobs/service/delivery/process/limit-days |
|
76
|
|
|
*/ |
|
77
|
|
|
$limit = $config->get( 'controller/jobs/order/email/delivery/limit-days', 90 ); |
|
78
|
|
|
$limitDate = date( 'Y-m-d H:i:s', time() - $limit * 86400 ); |
|
79
|
|
|
|
|
80
|
|
|
$default = [ |
|
81
|
|
|
\Aimeos\MShop\Order\Item\Base::STAT_PROGRESS, |
|
82
|
|
|
\Aimeos\MShop\Order\Item\Base::STAT_DISPATCHED, |
|
83
|
|
|
\Aimeos\MShop\Order\Item\Base::STAT_REFUSED, |
|
84
|
|
|
\Aimeos\MShop\Order\Item\Base::STAT_RETURNED, |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
/** controller/jobs/order/email/delivery/status |
|
88
|
|
|
* Only send order delivery notification e-mails for these delivery status values |
|
89
|
|
|
* |
|
90
|
|
|
* Notification e-mail about delivery status changes can be sent for these |
|
91
|
|
|
* status values: |
|
92
|
|
|
* |
|
93
|
|
|
* * 0: deleted |
|
94
|
|
|
* * 1: pending |
|
95
|
|
|
* * 2: progress |
|
96
|
|
|
* * 3: dispatched |
|
97
|
|
|
* * 4: delivered |
|
98
|
|
|
* * 5: lost |
|
99
|
|
|
* * 6: refused |
|
100
|
|
|
* * 7: returned |
|
101
|
|
|
* |
|
102
|
|
|
* User-defined status values are possible but should be in the private |
|
103
|
|
|
* block of values between 30000 and 32767. |
|
104
|
|
|
* |
|
105
|
|
|
* @param integer Delivery status constant |
|
106
|
|
|
* @since 2014.03 |
|
107
|
|
|
* @category User |
|
108
|
|
|
* @category Developer |
|
109
|
|
|
* @see controller/jobs/order/email/payment/status |
|
110
|
|
|
* @see controller/jobs/order/email/delivery/limit-days |
|
111
|
|
|
*/ |
|
112
|
|
|
foreach( (array) $config->get( 'controller/jobs/order/email/delivery/status', $default ) as $status ) |
|
113
|
|
|
{ |
|
114
|
|
|
$param = array( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_DELIVERY, (string) $status ); |
|
115
|
|
|
$filter = $orderManager->filter(); |
|
116
|
|
|
$filter->add( [ |
|
117
|
|
|
$filter->compare( '>=', 'order.mtime', $limitDate ), |
|
118
|
|
|
$filter->compare( '==', 'order.statusdelivery', $status ), |
|
119
|
|
|
$filter->compare( '==', $filter->make( 'order:status', $param ), 0 ), |
|
120
|
|
|
] ); |
|
121
|
|
|
|
|
122
|
|
|
$start = 0; |
|
123
|
|
|
$domains = ['order/base', 'order/base/address', 'order/base/product', 'order/base/service']; |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
do |
|
126
|
|
|
{ |
|
127
|
|
|
$items = $orderManager->search( $filter->slice( $start ) ); |
|
128
|
|
|
|
|
129
|
|
|
$this->notify( $items, $status ); |
|
130
|
|
|
|
|
131
|
|
|
$count = count( $items ); |
|
132
|
|
|
$start += $count; |
|
133
|
|
|
} |
|
134
|
|
|
while( $count >= $filter->getLimit() ); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the address item from the order |
|
141
|
|
|
* |
|
142
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Order including address items |
|
143
|
|
|
* @return \Aimeos\MShop\Common\Item\Address\Iface Address item |
|
144
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If no suitable address item is available |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function address( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : \Aimeos\MShop\Common\Item\Address\Iface |
|
147
|
|
|
{ |
|
148
|
|
|
if( ( $addr = current( $basket->getAddress( 'delivery' ) ) ) !== false && $addr->getEmail() ) { |
|
149
|
|
|
return $addr; |
|
150
|
|
|
}; |
|
151
|
|
|
|
|
152
|
|
|
if( ( $addr = current( $basket->getAddress( 'payment' ) ) ) !== false && $addr->getEmail() ) { |
|
153
|
|
|
return $addr; |
|
154
|
|
|
}; |
|
155
|
|
|
|
|
156
|
|
|
$msg = sprintf( 'No address with e-mail found in order base with ID "%1$s"', $basket->getId() ); |
|
157
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Adds the given list of files as attachments to the mail message object |
|
163
|
|
|
* |
|
164
|
|
|
* @param \Aimeos\Base\Mail\Message\Iface $msg Mail message |
|
165
|
|
|
* @param array $files List of absolute file paths |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function attachments( \Aimeos\Base\Mail\Message\Iface $msg ) : \Aimeos\Base\Mail\Message\Iface |
|
168
|
|
|
{ |
|
169
|
|
|
$context = $this->context(); |
|
170
|
|
|
$fs = $context->fs(); |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** client/html/email/delivery/attachments |
|
173
|
|
|
* List of file paths whose content should be attached to all delivery e-mails |
|
174
|
|
|
* |
|
175
|
|
|
* This configuration option allows you to add files to the e-mails that are |
|
176
|
|
|
* sent to the customer when the delivery status changes, e.g. for the order |
|
177
|
|
|
* confirmation e-mail. These files can't be customer specific. |
|
178
|
|
|
* |
|
179
|
|
|
* @param array List of absolute file paths |
|
180
|
|
|
* @since 2016.10 |
|
181
|
|
|
* @see client/html/email/delivery/attachments |
|
182
|
|
|
*/ |
|
183
|
|
|
$files = $context->config()->get( 'client/html/email/delivery/attachments', [] ); |
|
184
|
|
|
|
|
185
|
|
|
foreach( $files as $filepath ) |
|
186
|
|
|
{ |
|
187
|
|
|
if( $fs->has( $filepath ) ) { |
|
188
|
|
|
$msg->attach( $fs->read( $filepath ), basename( $filename ) ); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $msg; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Sends the delivery e-mail for the given orders |
|
198
|
|
|
* |
|
199
|
|
|
* @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys |
|
200
|
|
|
* @param int $status Delivery status value |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function notify( \Aimeos\Map $items, int $status ) |
|
203
|
|
|
{ |
|
204
|
|
|
$context = $this->context(); |
|
205
|
|
|
$sites = $this->sites( $items->getBaseItem()->getSiteId()->unique() ); |
|
206
|
|
|
|
|
207
|
|
|
foreach( $items as $id => $item ) |
|
208
|
|
|
{ |
|
209
|
|
|
try |
|
210
|
|
|
{ |
|
211
|
|
|
$basket = $item->getBaseItem(); |
|
212
|
|
|
$list = $sites->get( $basket->getSiteId(), map() ); |
|
213
|
|
|
|
|
214
|
|
|
$view = $this->view( $basket, $list->getTheme()->filter()->last() ); |
|
215
|
|
|
$view->summaryBasket = $basket; |
|
216
|
|
|
$view->orderItem = $item; |
|
217
|
|
|
|
|
218
|
|
|
$this->send( $view, $list->getLogo()->filter()->last() ); |
|
219
|
|
|
$this->status( $id, $status ); |
|
220
|
|
|
|
|
221
|
|
|
$email = $this->address( $basket )->getEmail(); |
|
222
|
|
|
$str = sprintf( 'Sent order delivery e-mail for status "%1$s" to "%2$s"', $status, $email ); |
|
223
|
|
|
$context->logger()->info( $str, 'email/order/delivery' ); |
|
224
|
|
|
} |
|
225
|
|
|
catch( \Exception $e ) |
|
226
|
|
|
{ |
|
227
|
|
|
$str = 'Error while trying to send delivery e-mail for order ID "%1$s" and status "%2$s": %3$s'; |
|
228
|
|
|
$msg = sprintf( $str, $item->getId(), $item->getStatusPayment(), $e->getMessage() ); |
|
229
|
|
|
$context->logger()->error( $msg . PHP_EOL . $e->getTraceAsString(), 'email/order/delivery' ); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Sends the delivery related e-mail for a single order |
|
237
|
|
|
* |
|
238
|
|
|
* @param \Aimeos\MW\View\Iface $view Populated view object |
|
239
|
|
|
* @param string|null $logoPath Relative path to the logo in the fs-media file system |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function send( \Aimeos\MW\View\Iface $view, string $logoPath = null ) |
|
242
|
|
|
{ |
|
243
|
|
|
$context = $this->context(); |
|
244
|
|
|
$config = $context->config(); |
|
245
|
|
|
$filename = $context->translate( 'client', 'Order' ) . '-' . $view->orderItem->getOrderNumber() . '.pdf'; |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
$msg = $this->call( 'mailTo', $view->addressItem ); |
|
248
|
|
|
$msg = $this->attachments( $msg ); |
|
249
|
|
|
$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) ); |
|
250
|
|
|
|
|
251
|
|
|
/** client/html/email/delivery/bcc-email |
|
252
|
|
|
* E-Mail address all delivery e-mails should be also sent to |
|
253
|
|
|
* |
|
254
|
|
|
* Using this option you can send a copy of all delivery related e-mails |
|
255
|
|
|
* to a second e-mail account. This can be handy for testing and checking |
|
256
|
|
|
* the e-mails sent to customers. |
|
257
|
|
|
* |
|
258
|
|
|
* It also allows shop owners with a very small volume of orders to be |
|
259
|
|
|
* notified about delivery changes. Be aware that this isn't useful if the |
|
260
|
|
|
* order volumne is high or has peeks! |
|
261
|
|
|
* |
|
262
|
|
|
* @param string|array E-mail address or list of e-mail addresses |
|
263
|
|
|
* @since 2014.03 |
|
264
|
|
|
*/ |
|
265
|
|
|
$msg->bcc( $config->get( 'client/html/email/delivery/bcc-email', [] ) ); |
|
266
|
|
|
|
|
267
|
|
|
$msg->subject( sprintf( $context->translate( 'client', 'Your order %1$s' ), $view->orderItem->getOrderNumber() ) ) |
|
268
|
|
|
->html( $view->render( $config->get( 'controller/jobs/order/email/delivery/template-html', 'order/email/delivery/html' ) ) ) |
|
269
|
|
|
->text( $view->render( $config->get( 'controller/jobs/order/email/delivery/template-text', 'order/email/delivery/text' ) ) ) |
|
270
|
|
|
->send(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Adds the status of the delivered e-mail for the given order ID |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $orderId Unique order ID |
|
278
|
|
|
* @param int $value Status value |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function status( string $orderId, int $value ) |
|
281
|
|
|
{ |
|
282
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'order/status' ); |
|
283
|
|
|
|
|
284
|
|
|
$item = $manager->create() |
|
285
|
|
|
->setParentId( $orderId ) |
|
286
|
|
|
->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_DELIVERY ) |
|
287
|
|
|
->setValue( $value ); |
|
288
|
|
|
|
|
289
|
|
|
$manager->save( $item ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Returns the site items for the given site codes |
|
295
|
|
|
* |
|
296
|
|
|
* @param iterable $siteIds List of site IDs |
|
297
|
|
|
* @return \Aimeos\Map Site items with codes as keys |
|
298
|
|
|
*/ |
|
299
|
|
|
protected function sites( iterable $siteIds ) : \Aimeos\Map |
|
300
|
|
|
{ |
|
301
|
|
|
$map = []; |
|
302
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' ); |
|
303
|
|
|
|
|
304
|
|
|
foreach( $siteIds as $siteId ) |
|
305
|
|
|
{ |
|
306
|
|
|
$list = explode( '.', trim( $siteId, '.' ) ); |
|
307
|
|
|
$map[$siteId] = $manager->getPath( end( $list ) ); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return map( $map ); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Returns the view populated with common data |
|
316
|
|
|
* |
|
317
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface $base Basket including addresses |
|
318
|
|
|
* @param string|null $theme Theme name |
|
319
|
|
|
* @return \Aimeos\MW\View\Iface View object |
|
320
|
|
|
*/ |
|
321
|
|
|
protected function view( \Aimeos\MShop\Order\Item\Base\Iface $base, string $theme = null ) : \Aimeos\MW\View\Iface |
|
322
|
|
|
{ |
|
323
|
|
|
$address = $this->address( $base ); |
|
324
|
|
|
$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId(); |
|
325
|
|
|
|
|
326
|
|
|
$view = $this->call( 'mailView', $langId ); |
|
327
|
|
|
$view->intro = $this->call( 'mailIntro', $address ); |
|
328
|
|
|
$view->css = $this->call( 'mailCss', $theme ); |
|
329
|
|
|
$view->address = $address; |
|
330
|
|
|
$view->urlparams = [ |
|
331
|
|
|
'currency' => $base->getPrice()->getCurrencyId(), |
|
332
|
|
|
'site' => $base->getSiteCode(), |
|
333
|
|
|
'locale' => $langId, |
|
334
|
|
|
]; |
|
335
|
|
|
|
|
336
|
|
|
return $view; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|