Passed
Push — master ( 2196ce...ac647e )
by Aimeos
03:24
created

Standard::status()

Size

Total Lines 20
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
c 1
b 0
f 0
eloc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2024
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
	/** controller/jobs/order/email/voucher/name
25
	 * Class name of the used order email voucher scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Order\Email\Voucher\Myvoucher
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/order/email/voucher/name = Myvoucher
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyVoucher"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
	/** controller/jobs/order/email/voucher/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the order email voucher controllers
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/order/email/voucher/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to this job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2015.09
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/order/email/voucher/decorators/global
79
	 * @see controller/jobs/order/email/voucher/decorators/local
80
	 */
81
82
	/** controller/jobs/order/email/voucher/decorators/global
83
	 * Adds a list of globally available decorators only to the order email voucher controllers
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/order/email/voucher/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2015.09
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/order/email/voucher/decorators/excludes
102
	 * @see controller/jobs/order/email/voucher/decorators/local
103
	 */
104
105
	/** controller/jobs/order/email/voucher/decorators/local
106
	 * Adds a list of local decorators only to the order email voucher controllers
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Order\Email\Voucher\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/order/email/voucher/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Order\Email\Voucher\Decorator\Decorator2" only to this job
120
	 * controller.
121
	 *
122
	 * @param array List of decorator names
123
	 * @since 2015.09
124
	 * @see controller/jobs/common/decorators/default
125
	 * @see controller/jobs/order/email/voucher/decorators/excludes
126
	 * @see controller/jobs/order/email/voucher/decorators/global
127
	 */
128
129
130
	use \Aimeos\Controller\Jobs\Mail;
131
132
133
	private ?string $couponId = null;
134
135
136
	/**
137
	 * Returns the localized name of the job.
138
	 *
139
	 * @return string Name of the job
140
	 */
141
	public function getName() : string
142
	{
143
		return $this->context()->translate( 'controller/jobs', 'Voucher related e-mails' );
144
	}
145
146
147
	/**
148
	 * Returns the localized description of the job.
149
	 *
150
	 * @return string Description of the job
151
	 */
152
	public function getDescription() : string
153
	{
154
		return $this->context()->translate( 'controller/jobs', 'Sends the e-mail with the voucher to the customer' );
155
	}
156
157
158
	/**
159
	 * Executes the job.
160
	 *
161
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
162
	 */
163
	public function run()
164
	{
165
		$context = $this->context();
166
		$config = $context->config();
167
		$limitDate = date( 'Y-m-d H:i:s', time() - $this->limit() * 86400 );
168
169
		$manager = \Aimeos\MShop::create( $context, 'order' );
170
171
		$filter = $manager->filter();
172
		$func = $filter->make( 'order:status', [\Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER, '1'] );
173
		$filter->add( $filter->and( [
174
			$filter->compare( '>=', 'order.mtime', $limitDate ),
175
			$filter->compare( '==', 'order.statuspayment', $this->status() ),
176
			$filter->compare( '==', 'order.product.type', 'voucher' ),
177
			$filter->compare( '==', $func, 0 ),
178
		] ) );
179
180
		$start = 0;
181
182
		do
183
		{
184
			$items = $manager->search( $filter->slice( $start ), ['order/address', 'order/product'] );
185
186
			$this->notify( $items );
187
188
			$count = count( $items );
189
			$start += $count;
190
		}
191
		while( $count >= $filter->getLimit() );
192
	}
193
194
195
	/**
196
	 * Returns the delivery address item of the order
197
	 *
198
	 * @param \Aimeos\MShop\Order\Item\Iface $orderBaseItem Order including address items
199
	 * @return \Aimeos\MShop\Order\Item\Address\Iface Delivery or voucher address item
200
	 * @throws \Aimeos\Controller\Jobs\Exception If no address item is available
201
	 */
202
	protected function address( \Aimeos\MShop\Order\Item\Iface $orderBaseItem ) : \Aimeos\MShop\Order\Item\Address\Iface
203
	{
204
		$type = \Aimeos\MShop\Order\Item\Address\Base::TYPE_DELIVERY;
205
		if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) {
206
			return $addr;
207
		}
208
209
		$type = \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT;
210
		if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) {
211
			return $addr;
212
		}
213
214
		$msg = sprintf( 'No address with e-mail found in order base with ID "%1$s"', $orderBaseItem->getId() );
215
		throw new \Aimeos\Controller\Jobs\Exception( $msg );
216
	}
217
218
219
	/**
220
	 * Creates coupon codes for the bought vouchers
221
	 *
222
	 * @param \Aimeos\Map $orderProdItems Complete order including addresses, products, services
223
	 */
224
	protected function createCoupons( \Aimeos\Map $orderProdItems )
225
	{
226
		$map = [];
227
		$manager = \Aimeos\MShop::create( $this->context(), 'order/product/attribute' );
228
229
		foreach( $orderProdItems as $orderProductItem )
230
		{
231
			if( $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) {
232
				continue;
233
			}
234
235
			$codes = [];
236
237
			for( $i = 0; $i < $orderProductItem->getQuantity(); $i++ )
238
			{
239
				$str = $i . getmypid() . microtime( true ) . $orderProductItem->getId();
240
				$code = substr( strtoupper( sha1( $str ) ), -8 );
241
				$map[$code] = $orderProductItem->getId();
242
				$codes[] = $code;
243
			}
244
245
			$item = $manager->create()->setCode( 'coupon-code' )->setType( 'coupon' )->setValue( $codes );
246
			$orderProductItem->setAttributeItem( $item );
247
		}
248
249
		$this->saveCoupons( $map );
250
		return $orderProdItems;
251
	}
252
253
254
	/**
255
	 * Returns the coupon ID for the voucher coupon
256
	 *
257
	 * @return string Unique ID of the coupon item
258
	 */
259
	protected function couponId() : string
260
	{
261
		if( !isset( $this->couponId ) )
262
		{
263
			$manager = \Aimeos\MShop::create( $this->context(), 'coupon' );
264
			$filter = $manager->filter()->add( 'coupon.provider', '=~', 'Voucher' )->slice( 0, 1 );
265
266
			if( ( $item = $manager->search( $filter )->first() ) === null ) {
267
				throw new \Aimeos\Controller\Jobs\Exception( 'No coupon provider "Voucher" available' );
268
			}
269
270
			$this->couponId = $item->getId();
271
		}
272
273
		return $this->couponId;
274
	}
275
276
277
	/**
278
	 * Returns the PDF file name
279
	 *
280
	 * @param string $code Voucher code
281
	 * @return string PDF file name
282
	 */
283
	protected function filename( string $code ) : string
284
	{
285
		return $this->context()->translate( 'controller/jobs', 'Voucher' ) . '-' . $code . '.pdf';
286
	}
287
288
289
	/**
290
	 * Returns the number of days after no e-mail will be sent anymore
291
	 *
292
	 * @return int Number of days
293
	 */
294
	protected function limit() : int
295
	{
296
		/** controller/jobs/order/email/voucher/limit-days
297
		 * Only send voucher e-mails of orders that were created in the past within the configured number of days
298
		 *
299
		 * The voucher e-mails are normally send immediately after the voucher
300
		 * status has changed. This option prevents e-mails for old order from
301
		 * being send in case anything went wrong or an update failed to avoid
302
		 * confusion of customers.
303
		 *
304
		 * @param integer Number of days
305
		 * @since 2014.03
306
		 * @see controller/jobs/order/email/delivery/limit-days
307
		 * @see controller/jobs/service/delivery/process/limit-days
308
		 */
309
		return (int) $this->context()->config()->get( 'controller/jobs/order/email/voucher/limit-days', 30 );
310
	}
311
312
313
	/**
314
	 * Sends the voucher e-mail for the given orders
315
	 *
316
	 * @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys
317
	 */
318
	protected function notify( \Aimeos\Map $items )
319
	{
320
		$context = $this->context();
321
		$sites = $this->sites( $items->getSiteId()->unique() );
322
323
		$couponManager = \Aimeos\MShop::create( $context, 'coupon' );
324
		$orderProdManager = \Aimeos\MShop::create( $context, 'order/product' );
325
326
		foreach( $items as $id => $item )
327
		{
328
			$couponManager->begin();
329
			$orderProdManager->begin();
330
331
			try
332
			{
333
				$products = $this->products( $item );
334
				$orderProdManager->save( $this->createCoupons( $products ) );
335
336
				$addr = $this->address( $item );
337
				$context->locale()->setLanguageId( $addr->getLanguageId() );
338
339
				$list = $sites->get( $item->getSiteId(), map() );
340
				$view = $this->view( $item, $list->getTheme()->filter()->last() );
341
342
				$this->send( $view, $products, $addr, $list->getLogo()->filter()->last() );
343
				$this->update( $id );
344
345
				$orderProdManager->commit();
346
				$couponManager->commit();
347
348
				$str = sprintf( 'Sent voucher e-mails for order ID "%1$s"', $item->getId() );
349
				$context->logger()->info( $str, 'email/order/voucher' );
350
			}
351
			catch( \Exception $e )
352
			{
353
				$orderProdManager->rollback();
354
				$couponManager->rollback();
355
356
				$str = 'Error while trying to send voucher e-mails for order ID "%1$s": %2$s';
357
				$msg = sprintf( $str, $item->getId(), $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
358
				$context->logger()->info( $msg, 'email/order/voucher' );
359
			}
360
		}
361
	}
362
363
364
	/**
365
	 * Returns the generated PDF file for the order
366
	 *
367
	 * @param \Aimeos\Base\View\Iface $view View object with address and order item assigned
368
	 * @return string|null PDF content or NULL for no PDF file
369
	 */
370
	protected function pdf( \Aimeos\Base\View\Iface $view ) : ?string
371
	{
372
		$config = $this->context()->config();
373
374
		/** controller/jobs/order/email/voucher/pdf
375
		 * Enables attaching a PDF to the voucher e-mail
376
		 *
377
		 * The voucher PDF contains the same information like the HTML e-mail.
378
		 *
379
		 * @param bool TRUE to enable attaching the PDF, FALSE to skip the PDF
380
		 * @since 2022.10
381
		 */
382
		if( !$config->get( 'controller/jobs/order/email/voucher/pdf', true ) ) {
383
			return null;
384
		}
385
386
		$pdf = new class( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false ) extends \TCPDF {
387
			private ?\Closure $headerFcn = null;
388
			private ?\Closure $footerFcn = null;
389
390
			public function Footer() { return ( $fcn = $this->footerFcn ) ? $fcn( $this ) : null; }
391
			public function Header() { return ( $fcn = $this->headerFcn ) ? $fcn( $this ) : null; }
392
			public function setFooterFunction( \Closure $fcn ) { $this->footerFcn = $fcn; }
393
			public function setHeaderFunction( \Closure $fcn ) { $this->headerFcn = $fcn; }
394
		};
395
		$pdf->setCreator( PDF_CREATOR );
396
		$pdf->setAuthor( 'Aimeos' );
397
398
		/** controller/jobs/order/email/voucher/template-pdf
399
		 * Relative path to the template for the PDF part of the voucher emails.
400
		 *
401
		 * The template file contains the text and processing instructions
402
		 * to generate the result shown in the body of the frontend. The
403
		 * configuration string is the path to the template file relative
404
		 * to the templates directory (usually in templates/controller/jobs).
405
		 * You can overwrite the template file configuration in extensions and
406
		 * provide alternative templates.
407
		 *
408
		 * @param string Relative path to the template
409
		 * @since 2022.10
410
		 * @see controller/jobs/order/email/voucher/template-html
411
		 * @see controller/jobs/order/email/voucher/template-text
412
		 */
413
		$template = $config->get( 'controller/jobs/order/email/voucher/template-pdf', 'order/email/voucher/pdf' );
414
415
		// Generate HTML before creating first PDF page to include header added in template
416
		$content = $view->set( 'pdf', $pdf )->render( $template );
417
418
		$pdf->addPage();
419
		$pdf->writeHtml( $content );
420
		$pdf->lastPage();
421
422
		return $pdf->output( '', 'S' );
423
	}
424
425
426
	/**
427
	 * Returns the ordered voucher products from the basket.
428
	 *
429
	 * @param \Aimeos\MShop\Order\Item\Iface $orderBaseItem Basket object
430
	 * @return \Aimeos\Map List of order product items for the voucher products
431
	 */
432
	protected function products( \Aimeos\MShop\Order\Item\Iface $orderBaseItem ) : \Aimeos\Map
433
	{
434
		$list = [];
435
436
		foreach( $orderBaseItem->getProducts() as $orderProductItem )
437
		{
438
			if( $orderProductItem->getType() === 'voucher' ) {
439
				$list[] = $orderProductItem;
440
			}
441
442
			foreach( $orderProductItem->getProducts() as $subProductItem )
443
			{
444
				if( $subProductItem->getType() === 'voucher' ) {
445
					$list[] = $subProductItem;
446
				}
447
			}
448
		}
449
450
		return map( $list );
451
	}
452
453
454
	/**
455
	 * Saves the given coupon codes
456
	 *
457
	 * @param array $map Associative list of coupon codes as keys and reference Ids as values
458
	 */
459
	protected function saveCoupons( array $map )
460
	{
461
		$couponId = $this->couponId();
462
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
463
464
		foreach( $map as $code => $ref )
465
		{
466
			$item = $manager->create()->setParentId( $couponId )
467
				->setCode( $code )->setRef( $ref )->setCount( null ); // unlimited
468
469
			$manager->save( $item );
470
		}
471
	}
472
473
474
	/**
475
	 * Sends the voucher related e-mail for a single order
476
	 *
477
	 * @param \Aimeos\Base\View\Iface $view Populated view object
478
	 * @param \Aimeos\Map $orderProducts List of ordered voucher products
479
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
480
	 * @param string|null $logoPath Relative path to the logo in the fs-media file system
481
	 */
482
	protected function send( \Aimeos\Base\View\Iface $view, \Aimeos\Map $orderProducts,
483
		\Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null )
484
	{
485
		/** controller/jobs/order/email/voucher/template-html
486
		 * Relative path to the template for the HTML part of the voucher emails.
487
		 *
488
		 * The template file contains the HTML code and processing instructions
489
		 * to generate the result shown in the body of the frontend. The
490
		 * configuration string is the path to the template file relative
491
		 * to the templates directory (usually in templates/controller/jobs).
492
		 * You can overwrite the template file configuration in extensions and
493
		 * provide alternative templates.
494
		 *
495
		 * @param string Relative path to the template
496
		 * @since 2022.04
497
		 * @see controller/jobs/order/email/voucher/template-text
498
		 */
499
500
		/** controller/jobs/order/email/voucher/template-text
501
		 * Relative path to the template for the text part of the voucher emails.
502
		 *
503
		 * The template file contains the text and processing instructions
504
		 * to generate the result shown in the body of the frontend. The
505
		 * configuration string is the path to the template file relative
506
		 * to the templates directory (usually in templates/controller/jobs).
507
		 * You can overwrite the template file configuration in extensions and
508
		 * provide alternative templates.
509
		 *
510
		 * @param string Relative path to the template
511
		 * @since 2022.04
512
		 * @see controller/jobs/order/email/voucher/template-html
513
		 */
514
515
		$context = $this->context();
516
		$config = $context->config();
517
		$logo = $this->call( 'mailLogo', $logoPath );
518
		$view->orderAddressItem = $address;
519
520
		foreach( $orderProducts as $orderProductItem )
521
		{
522
			if( !empty( $codes = $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) )
523
			{
524
				foreach( (array) $codes as $code )
525
				{
526
					$view->orderProductItem = $orderProductItem;
527
					$view->voucher = $code;
528
529
					$msg = $this->call( 'mailTo', $address );
530
					$view->logo = $msg->embed( $logo, basename( (string) $logoPath ) );
531
532
					$msg->subject( $context->translate( 'controller/jobs', 'Your voucher' ) )
533
						->html( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-html', 'order/email/voucher/html' ) ) )
534
						->text( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-text', 'order/email/voucher/text' ) ) )
535
						->attach( $this->pdf( $view ), $this->call( 'filename', $code ), 'application/pdf' )
536
						->send();
537
				}
538
			}
539
		}
540
	}
541
542
543
	/**
544
	 * Returns the site items for the given site codes
545
	 *
546
	 * @param iterable $siteIds List of site IDs
547
	 * @return \Aimeos\Map Site items with codes as keys
548
	 */
549
	protected function sites( iterable $siteIds ) : \Aimeos\Map
550
	{
551
		$map = [];
552
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
553
554
		foreach( $siteIds as $siteId )
555
		{
556
			$list = explode( '.', trim( $siteId, '.' ) );
557
			$map[$siteId] = $manager->getPath( end( $list ) );
558
		}
559
560
		return map( $map );
561
	}
562
563
564
	/**
565
	 * Returns the payment status for which the e-mails should be sent
566
	 *
567
	 * @return int Payment status
568
	 */
569
	protected function status() : int
570
	{
571
		/** controller/jobs/order/email/voucher/status
572
		 * Only send e-mails containing voucher for these payment status values
573
		 *
574
		 * E-mail containing vouchers can be sent for these payment status values:
575
		 *
576
		 * * 0: deleted
577
		 * * 1: canceled
578
		 * * 2: refused
579
		 * * 3: refund
580
		 * * 4: pending
581
		 * * 5: authorized
582
		 * * 6: received
583
		 *
584
		 * @param integer Payment status constant
585
		 * @since 2018.07
586
		 * @see controller/jobs/order/email/voucher/limit-days
587
		 */
588
		return (int) $this->context()->config()->get( 'controller/jobs/order/email/voucher/status', \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED );
589
	}
590
591
592
	/**
593
	 * Adds the status of the delivered e-mail for the given order ID
594
	 *
595
	 * @param string $orderId Unique order ID
596
	 */
597
	protected function update( string $orderId )
598
	{
599
		$orderStatusManager = \Aimeos\MShop::create( $this->context(), 'order/status' );
600
601
		$statusItem = $orderStatusManager->create()->setParentId( $orderId )->setValue( 1 )
602
			->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER );
603
604
		$orderStatusManager->save( $statusItem );
605
	}
606
607
608
	/**
609
	 * Returns the view populated with common data
610
	 *
611
	 * @param \Aimeos\MShop\Order\Item\Iface $base Basket including addresses
612
	 * @param string|null $theme Theme name
613
	 * @return \Aimeos\Base\View\Iface View object
614
	 */
615
	protected function view( \Aimeos\MShop\Order\Item\Iface $base, string $theme = null ) : \Aimeos\Base\View\Iface
616
	{
617
		$address = $this->address( $base );
618
		$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId();
619
620
		$view = $this->call( 'mailView', $langId );
621
		$view->intro = $this->call( 'mailIntro', $address );
622
		$view->css = $this->call( 'mailCss', $theme );
623
		$view->address = $address;
624
		$view->urlparams = [
625
			'currency' => $base->getPrice()->getCurrencyId(),
626
			'site' => $base->getSiteCode(),
627
			'locale' => $langId,
628
		];
629
630
		return $view;
631
	}
632
}
633