Passed
Push — master ( a110b1...55cad9 )
by Aimeos
03:39
created

Standard::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Order\Service\Transfer;
12
13
14
/**
15
 * Transfers the money to the vendors
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/**
25
	 * Returns the localized name of the job.
26
	 *
27
	 * @return string Name of the job
28
	 */
29
	public function getName() : string
30
	{
31
		return $this->context()->translate( 'controller/jobs', 'Transfers money to vendors' );
32
	}
33
34
35
	/**
36
	 * Returns the localized description of the job.
37
	 *
38
	 * @return string Description of the job
39
	 */
40
	public function getDescription() : string
41
	{
42
		return $this->context()->translate( 'controller/jobs', 'Transfers the price of ordered products to the vendors incl. commission handling' );
43
	}
44
45
46
	/**
47
	 * Executes the job.
48
	 */
49
	public function run()
50
	{
51
		$context = $this->context();
52
53
		$serviceManager = \Aimeos\MShop::create( $context, 'service' );
54
		$serviceSearch = $serviceManager->filter()->add( ['service.type' => 'payment'] );
55
56
		$orderManager = \Aimeos\MShop::create( $context, 'order' );
57
		$orderSearch = $orderManager->filter();
58
		$start = 0;
59
60
		do
61
		{
62
			$serviceItems = $serviceManager->search( $serviceSearch );
63
64
			foreach( $serviceItems as $serviceItem )
65
			{
66
				try
67
				{
68
					$serviceProvider = $serviceManager->getProvider( $serviceItem, $serviceItem->getType() );
69
70
					if( !$serviceProvider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_TRANSFER ) ) {
71
						continue;
72
					}
73
74
					$orderSearch->setConditions( $orderSearch->and( [
75
						$orderSearch->compare( '<=', 'order.ctime', date( 'Y-m-d 00:00:00', time() - 86400 * $this->days() ) ),
76
						$orderSearch->compare( '==', 'order.statuspayment', \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED ),
77
						$orderSearch->compare( '==', 'order.base.service.code', $serviceItem->getCode() ),
78
						$orderSearch->compare( '==', 'order.base.service.type', 'payment' )
79
					] ) );
80
81
					$orderStart = 0;
82
83
					do
84
					{
85
						$orderItems = $orderManager->search( $orderSearch, $this->domains() );
86
87
						foreach( $orderItems as $orderItem )
88
						{
89
							try
90
							{
91
								$orderManager->save( $serviceProvider->transfer( $orderItem ) );
92
							}
93
							catch( \Exception $e )
94
							{
95
								$str = 'Error while transferring payment for order with ID "%1$s": %2$s';
96
								$msg = sprintf( $str, $serviceItem->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
97
								$context->logger()->error( $msg, 'order/service/transfer' );
98
							}
99
						}
100
101
						$orderCount = count( $orderItems );
102
						$orderStart += $orderCount;
103
						$orderSearch->slice( $orderStart );
104
					}
105
					while( $orderCount >= $orderSearch->getLimit() );
106
				}
107
				catch( \Exception $e )
108
				{
109
					$str = 'Error while transferring payment for service with ID "%1$s": %2$s';
110
					$msg = sprintf( $str, $serviceItem->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
111
					$context->logger()->error( $msg, 'order/service/transfer' );
112
				}
113
			}
114
115
			$count = count( $serviceItems );
116
			$start += $count;
117
			$serviceSearch->slice( $start );
118
		}
119
		while( $count >= $serviceSearch->getLimit() );
120
	}
121
122
123
	/**
124
	 * Returns the number of days to postpone transfers
125
	 *
126
	 * @return int Number of days
127
	 */
128
	protected function days() : int
129
	{
130
		/** controller/jobs/order/service/transfer/transfer-days
131
		 * Automatically transfers payments after the configured amount of days
132
		 *
133
		 * You can start transferring payments after the configured amount of days.
134
		 * Before, the money is hold back and not available to vendors.
135
		 *
136
		 * @param integer Number of days
137
		 * @since 2010.10
138
		 * @category User
139
		 * @category Developer
140
		 */
141
		return $this->context()->config()->get( 'controller/jobs/order/service/transfer/transfer-days', 0 );
142
	}
143
144
145
	/**
146
	 * Returns the domains used when fetching orders
147
	 *
148
	 * @return array List of data domain names
149
	 */
150
	protected function domains() : array
151
	{
152
		/** controller/jobs/order/service/transfer/domains
153
		 * Associated items that should be available too in the order
154
		 *
155
		 * Orders consist of address, coupons, products and services. They can be
156
		 * fetched together with the order items and passed to the payment service
157
		 * providers. Available domains for those items are:
158
		 *
159
		 * - order/base
160
		 * - order/base/address
161
		 * - order/base/coupon
162
		 * - order/base/product
163
		 * - order/base/service
164
		 *
165
		 * @param array Referenced domain names
166
		 * @since 2022.04
167
		 */
168
		$domains = ['order/base', 'order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
169
		return $this->context()->config()->get( 'controller/jobs/order/service/transfer/domains', $domains );
170
	}
171
}
172