Passed
Push — master ( 9142c7...2196ce )
by Aimeos
13:39
created

Standard::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 16
rs 10
c 1
b 0
f 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
168
		$limitDate = date( 'Y-m-d H:i:s', time() - $this->limit() * 86400 );
169
170
		/** controller/jobs/order/email/voucher/status
171
		 * Only send e-mails containing voucher for these payment status values
172
		 *
173
		 * E-mail containing vouchers can be sent for these payment status values:
174
		 *
175
		 * * 0: deleted
176
		 * * 1: canceled
177
		 * * 2: refused
178
		 * * 3: refund
179
		 * * 4: pending
180
		 * * 5: authorized
181
		 * * 6: received
182
		 *
183
		 * @param integer Payment status constant
184
		 * @since 2018.07
185
		 * @see controller/jobs/order/email/voucher/limit-days
186
		 */
187
		$status = $config->get( 'controller/jobs/order/email/voucher/status', \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED );
188
189
190
		$manager = \Aimeos\MShop::create( $context, 'order' );
191
192
		$filter = $manager->filter();
193
		$func = $filter->make( 'order:status', [\Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER, '1'] );
194
		$filter->add( $filter->and( [
195
			$filter->compare( '>=', 'order.mtime', $limitDate ),
196
			$filter->compare( '==', 'order.statuspayment', $status ),
197
			$filter->compare( '==', 'order.product.type', 'voucher' ),
198
			$filter->compare( '==', $func, 0 ),
199
		] ) );
200
201
		$start = 0;
202
203
		do
204
		{
205
			$items = $manager->search( $filter->slice( $start ), ['order/address', 'order/product'] );
206
207
			$this->notify( $items );
208
209
			$count = count( $items );
210
			$start += $count;
211
		}
212
		while( $count >= $filter->getLimit() );
213
	}
214
215
216
	/**
217
	 * Returns the delivery address item of the order
218
	 *
219
	 * @param \Aimeos\MShop\Order\Item\Iface $orderBaseItem Order including address items
220
	 * @return \Aimeos\MShop\Order\Item\Address\Iface Delivery or voucher address item
221
	 * @throws \Aimeos\Controller\Jobs\Exception If no address item is available
222
	 */
223
	protected function address( \Aimeos\MShop\Order\Item\Iface $orderBaseItem ) : \Aimeos\MShop\Order\Item\Address\Iface
224
	{
225
		$type = \Aimeos\MShop\Order\Item\Address\Base::TYPE_DELIVERY;
226
		if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) {
227
			return $addr;
228
		}
229
230
		$type = \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT;
231
		if( ( $addr = current( $orderBaseItem->getAddress( $type ) ) ) !== false && $addr->getEmail() !== '' ) {
232
			return $addr;
233
		}
234
235
		$msg = sprintf( 'No address with e-mail found in order base with ID "%1$s"', $orderBaseItem->getId() );
236
		throw new \Aimeos\Controller\Jobs\Exception( $msg );
237
	}
238
239
240
	/**
241
	 * Creates coupon codes for the bought vouchers
242
	 *
243
	 * @param \Aimeos\Map $orderProdItems Complete order including addresses, products, services
244
	 */
245
	protected function createCoupons( \Aimeos\Map $orderProdItems )
246
	{
247
		$map = [];
248
		$manager = \Aimeos\MShop::create( $this->context(), 'order/product/attribute' );
249
250
		foreach( $orderProdItems as $orderProductItem )
251
		{
252
			if( $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) {
253
				continue;
254
			}
255
256
			$codes = [];
257
258
			for( $i = 0; $i < $orderProductItem->getQuantity(); $i++ )
259
			{
260
				$str = $i . getmypid() . microtime( true ) . $orderProductItem->getId();
261
				$code = substr( strtoupper( sha1( $str ) ), -8 );
262
				$map[$code] = $orderProductItem->getId();
263
				$codes[] = $code;
264
			}
265
266
			$item = $manager->create()->setCode( 'coupon-code' )->setType( 'coupon' )->setValue( $codes );
267
			$orderProductItem->setAttributeItem( $item );
268
		}
269
270
		$this->saveCoupons( $map );
271
		return $orderProdItems;
272
	}
273
274
275
	/**
276
	 * Returns the coupon ID for the voucher coupon
277
	 *
278
	 * @return string Unique ID of the coupon item
279
	 */
280
	protected function couponId() : string
281
	{
282
		if( !isset( $this->couponId ) )
283
		{
284
			$manager = \Aimeos\MShop::create( $this->context(), 'coupon' );
285
			$filter = $manager->filter()->add( 'coupon.provider', '=~', 'Voucher' )->slice( 0, 1 );
286
287
			if( ( $item = $manager->search( $filter )->first() ) === null ) {
288
				throw new \Aimeos\Controller\Jobs\Exception( 'No coupon provider "Voucher" available' );
289
			}
290
291
			$this->couponId = $item->getId();
292
		}
293
294
		return $this->couponId;
295
	}
296
297
298
	/**
299
	 * Returns the PDF file name
300
	 *
301
	 * @param string $code Voucher code
302
	 * @return string PDF file name
303
	 */
304
	protected function filename( string $code ) : string
305
	{
306
		return $this->context()->translate( 'controller/jobs', 'Voucher' ) . '-' . $code . '.pdf';
307
	}
308
309
310
	/**
311
	 * Returns the number of days after no e-mail will be sent anymore
312
	 *
313
	 * @return int Number of days
314
	 */
315
	protected function limit() : int
316
	{
317
		/** controller/jobs/order/email/voucher/limit-days
318
		 * Only send voucher e-mails of orders that were created in the past within the configured number of days
319
		 *
320
		 * The voucher e-mails are normally send immediately after the voucher
321
		 * status has changed. This option prevents e-mails for old order from
322
		 * being send in case anything went wrong or an update failed to avoid
323
		 * confusion of customers.
324
		 *
325
		 * @param integer Number of days
326
		 * @since 2014.03
327
		 * @see controller/jobs/order/email/delivery/limit-days
328
		 * @see controller/jobs/service/delivery/process/limit-days
329
		 */
330
		return (int) $this->context()->config()->get( 'controller/jobs/order/email/voucher/limit-days', 30 );
331
	}
332
333
334
	/**
335
	 * Sends the voucher e-mail for the given orders
336
	 *
337
	 * @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys
338
	 */
339
	protected function notify( \Aimeos\Map $items )
340
	{
341
		$context = $this->context();
342
		$sites = $this->sites( $items->getSiteId()->unique() );
343
344
		$couponManager = \Aimeos\MShop::create( $context, 'coupon' );
345
		$orderProdManager = \Aimeos\MShop::create( $context, 'order/product' );
346
347
		foreach( $items as $id => $item )
348
		{
349
			$couponManager->begin();
350
			$orderProdManager->begin();
351
352
			try
353
			{
354
				$products = $this->products( $item );
355
				$orderProdManager->save( $this->createCoupons( $products ) );
356
357
				$addr = $this->address( $item );
358
				$context->locale()->setLanguageId( $addr->getLanguageId() );
359
360
				$list = $sites->get( $item->getSiteId(), map() );
361
				$view = $this->view( $item, $list->getTheme()->filter()->last() );
362
363
				$this->send( $view, $products, $addr, $list->getLogo()->filter()->last() );
364
				$this->status( $id );
365
366
				$orderProdManager->commit();
367
				$couponManager->commit();
368
369
				$str = sprintf( 'Sent voucher e-mails for order ID "%1$s"', $item->getId() );
370
				$context->logger()->info( $str, 'email/order/voucher' );
371
			}
372
			catch( \Exception $e )
373
			{
374
				$orderProdManager->rollback();
375
				$couponManager->rollback();
376
377
				$str = 'Error while trying to send voucher e-mails for order ID "%1$s": %2$s';
378
				$msg = sprintf( $str, $item->getId(), $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
379
				$context->logger()->info( $msg, 'email/order/voucher' );
380
			}
381
		}
382
	}
383
384
385
	/**
386
	 * Returns the generated PDF file for the order
387
	 *
388
	 * @param \Aimeos\Base\View\Iface $view View object with address and order item assigned
389
	 * @return string|null PDF content or NULL for no PDF file
390
	 */
391
	protected function pdf( \Aimeos\Base\View\Iface $view ) : ?string
392
	{
393
		$config = $this->context()->config();
394
395
		/** controller/jobs/order/email/voucher/pdf
396
		 * Enables attaching a PDF to the voucher e-mail
397
		 *
398
		 * The voucher PDF contains the same information like the HTML e-mail.
399
		 *
400
		 * @param bool TRUE to enable attaching the PDF, FALSE to skip the PDF
401
		 * @since 2022.10
402
		 */
403
		if( !$config->get( 'controller/jobs/order/email/voucher/pdf', true ) ) {
404
			return null;
405
		}
406
407
		$pdf = new class( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false ) extends \TCPDF {
408
			private ?\Closure $headerFcn = null;
409
			private ?\Closure $footerFcn = null;
410
411
			public function Footer() { return ( $fcn = $this->footerFcn ) ? $fcn( $this ) : null; }
412
			public function Header() { return ( $fcn = $this->headerFcn ) ? $fcn( $this ) : null; }
413
			public function setFooterFunction( \Closure $fcn ) { $this->footerFcn = $fcn; }
414
			public function setHeaderFunction( \Closure $fcn ) { $this->headerFcn = $fcn; }
415
		};
416
		$pdf->setCreator( PDF_CREATOR );
417
		$pdf->setAuthor( 'Aimeos' );
418
419
		/** controller/jobs/order/email/voucher/template-pdf
420
		 * Relative path to the template for the PDF part of the voucher emails.
421
		 *
422
		 * The template file contains the text and processing instructions
423
		 * to generate the result shown in the body of the frontend. The
424
		 * configuration string is the path to the template file relative
425
		 * to the templates directory (usually in templates/controller/jobs).
426
		 * You can overwrite the template file configuration in extensions and
427
		 * provide alternative templates.
428
		 *
429
		 * @param string Relative path to the template
430
		 * @since 2022.10
431
		 * @see controller/jobs/order/email/voucher/template-html
432
		 * @see controller/jobs/order/email/voucher/template-text
433
		 */
434
		$template = $config->get( 'controller/jobs/order/email/voucher/template-pdf', 'order/email/voucher/pdf' );
435
436
		// Generate HTML before creating first PDF page to include header added in template
437
		$content = $view->set( 'pdf', $pdf )->render( $template );
438
439
		$pdf->addPage();
440
		$pdf->writeHtml( $content );
441
		$pdf->lastPage();
442
443
		return $pdf->output( '', 'S' );
444
	}
445
446
447
	/**
448
	 * Returns the ordered voucher products from the basket.
449
	 *
450
	 * @param \Aimeos\MShop\Order\Item\Iface $orderBaseItem Basket object
451
	 * @return \Aimeos\Map List of order product items for the voucher products
452
	 */
453
	protected function products( \Aimeos\MShop\Order\Item\Iface $orderBaseItem ) : \Aimeos\Map
454
	{
455
		$list = [];
456
457
		foreach( $orderBaseItem->getProducts() as $orderProductItem )
458
		{
459
			if( $orderProductItem->getType() === 'voucher' ) {
460
				$list[] = $orderProductItem;
461
			}
462
463
			foreach( $orderProductItem->getProducts() as $subProductItem )
464
			{
465
				if( $subProductItem->getType() === 'voucher' ) {
466
					$list[] = $subProductItem;
467
				}
468
			}
469
		}
470
471
		return map( $list );
472
	}
473
474
475
	/**
476
	 * Saves the given coupon codes
477
	 *
478
	 * @param array $map Associative list of coupon codes as keys and reference Ids as values
479
	 */
480
	protected function saveCoupons( array $map )
481
	{
482
		$couponId = $this->couponId();
483
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
484
485
		foreach( $map as $code => $ref )
486
		{
487
			$item = $manager->create()->setParentId( $couponId )
488
				->setCode( $code )->setRef( $ref )->setCount( null ); // unlimited
489
490
			$manager->save( $item );
491
		}
492
	}
493
494
495
	/**
496
	 * Sends the voucher related e-mail for a single order
497
	 *
498
	 * @param \Aimeos\Base\View\Iface $view Populated view object
499
	 * @param \Aimeos\Map $orderProducts List of ordered voucher products
500
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
501
	 * @param string|null $logoPath Relative path to the logo in the fs-media file system
502
	 */
503
	protected function send( \Aimeos\Base\View\Iface $view, \Aimeos\Map $orderProducts,
504
		\Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null )
505
	{
506
		/** controller/jobs/order/email/voucher/template-html
507
		 * Relative path to the template for the HTML part of the voucher emails.
508
		 *
509
		 * The template file contains the HTML code and processing instructions
510
		 * to generate the result shown in the body of the frontend. The
511
		 * configuration string is the path to the template file relative
512
		 * to the templates directory (usually in templates/controller/jobs).
513
		 * You can overwrite the template file configuration in extensions and
514
		 * provide alternative templates.
515
		 *
516
		 * @param string Relative path to the template
517
		 * @since 2022.04
518
		 * @see controller/jobs/order/email/voucher/template-text
519
		 */
520
521
		/** controller/jobs/order/email/voucher/template-text
522
		 * Relative path to the template for the text part of the voucher emails.
523
		 *
524
		 * The template file contains the text and processing instructions
525
		 * to generate the result shown in the body of the frontend. The
526
		 * configuration string is the path to the template file relative
527
		 * to the templates directory (usually in templates/controller/jobs).
528
		 * You can overwrite the template file configuration in extensions and
529
		 * provide alternative templates.
530
		 *
531
		 * @param string Relative path to the template
532
		 * @since 2022.04
533
		 * @see controller/jobs/order/email/voucher/template-html
534
		 */
535
536
		$context = $this->context();
537
		$config = $context->config();
538
		$logo = $this->call( 'mailLogo', $logoPath );
539
		$view->orderAddressItem = $address;
540
541
		foreach( $orderProducts as $orderProductItem )
542
		{
543
			if( !empty( $codes = $orderProductItem->getAttribute( 'coupon-code', 'coupon' ) ) )
544
			{
545
				foreach( (array) $codes as $code )
546
				{
547
					$view->orderProductItem = $orderProductItem;
548
					$view->voucher = $code;
549
550
					$msg = $this->call( 'mailTo', $address );
551
					$view->logo = $msg->embed( $logo, basename( (string) $logoPath ) );
552
553
					$msg->subject( $context->translate( 'controller/jobs', 'Your voucher' ) )
554
						->html( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-html', 'order/email/voucher/html' ) ) )
555
						->text( $view->render( $config->get( 'controller/jobs/order/email/voucher/template-text', 'order/email/voucher/text' ) ) )
556
						->attach( $this->pdf( $view ), $this->call( 'filename', $code ), 'application/pdf' )
557
						->send();
558
				}
559
			}
560
		}
561
	}
562
563
564
	/**
565
	 * Returns the site items for the given site codes
566
	 *
567
	 * @param iterable $siteIds List of site IDs
568
	 * @return \Aimeos\Map Site items with codes as keys
569
	 */
570
	protected function sites( iterable $siteIds ) : \Aimeos\Map
571
	{
572
		$map = [];
573
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
574
575
		foreach( $siteIds as $siteId )
576
		{
577
			$list = explode( '.', trim( $siteId, '.' ) );
578
			$map[$siteId] = $manager->getPath( end( $list ) );
579
		}
580
581
		return map( $map );
582
	}
583
584
585
	/**
586
	 * Adds the status of the delivered e-mail for the given order ID
587
	 *
588
	 * @param string $orderId Unique order ID
589
	 */
590
	protected function status( string $orderId )
591
	{
592
		$orderStatusManager = \Aimeos\MShop::create( $this->context(), 'order/status' );
593
594
		$statusItem = $orderStatusManager->create()->setParentId( $orderId )->setValue( 1 )
595
			->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_VOUCHER );
596
597
		$orderStatusManager->save( $statusItem );
598
	}
599
600
601
	/**
602
	 * Returns the view populated with common data
603
	 *
604
	 * @param \Aimeos\MShop\Order\Item\Iface $base Basket including addresses
605
	 * @param string|null $theme Theme name
606
	 * @return \Aimeos\Base\View\Iface View object
607
	 */
608
	protected function view( \Aimeos\MShop\Order\Item\Iface $base, string $theme = null ) : \Aimeos\Base\View\Iface
609
	{
610
		$address = $this->address( $base );
611
		$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId();
612
613
		$view = $this->call( 'mailView', $langId );
614
		$view->intro = $this->call( 'mailIntro', $address );
615
		$view->css = $this->call( 'mailCss', $theme );
616
		$view->address = $address;
617
		$view->urlparams = [
618
			'currency' => $base->getPrice()->getCurrencyId(),
619
			'site' => $base->getSiteCode(),
620
			'locale' => $langId,
621
		];
622
623
		return $view;
624
	}
625
}
626