Standard::getDescription()   A
last analyzed

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-2025
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
		/** controller/jobs/order/service/transfer/name
25
		 * Class name of the used order service transfer 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\Service\Transfer\Standard
35
		 *
36
		 * and you want to replace it with your own version named
37
		 *
38
		 *  \Aimeos\Controller\Jobs\Order\Service\Transfer\Mytransfer
39
		 *
40
		 * then you have to set the this configuration option:
41
		 *
42
		 *  controller/jobs/order/service/transfer/name = Mytransfer
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 "MyTransfer"!
52
		 *
53
		 * @param string Last part of the class name
54
		 * @since 2021.10
55
		 */
56
57
		/** controller/jobs/order/service/transfer/decorators/excludes
58
		 * Excludes decorators added by the "common" option from the order service transfer 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/service/transfer/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 2021.10
77
		 * @see controller/jobs/common/decorators/default
78
		 * @see controller/jobs/order/service/transfer/decorators/global
79
		 * @see controller/jobs/order/service/transfer/decorators/local
80
		 */
81
82
		/** controller/jobs/order/service/transfer/decorators/global
83
		 * Adds a list of globally available decorators only to the order service transfer 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/service/transfer/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 2021.10
100
		 * @see controller/jobs/common/decorators/default
101
		 * @see controller/jobs/order/service/transfer/decorators/excludes
102
		 * @see controller/jobs/order/service/transfer/decorators/local
103
		 */
104
105
		/** controller/jobs/order/service/transfer/decorators/local
106
		 * Adds a list of local decorators only to the order service transfer 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\Service\Transfer\Decorator\*") around this job controller.
115
		 *
116
		 *  controller/jobs/order/service/transfer/decorators/local = array( 'decorator2' )
117
		 *
118
		 * This would add the decorator named "decorator2" defined by
119
		 * "\Aimeos\Controller\Jobs\Order\Service\Transfer\Decorator\Decorator2" only to this job
120
		 * controller.
121
		 *
122
		 * @param array List of decorator names
123
		 * @since 2021.10
124
		 * @see controller/jobs/common/decorators/default
125
		 * @see controller/jobs/order/service/transfer/decorators/excludes
126
		 * @see controller/jobs/order/service/transfer/decorators/global
127
		 */
128
129
130
	/**
131
	 * Returns the localized name of the job.
132
	 *
133
	 * @return string Name of the job
134
	 */
135
	public function getName() : string
136
	{
137
		return $this->context()->translate( 'controller/jobs', 'Transfers money to vendors' );
138
	}
139
140
141
	/**
142
	 * Returns the localized description of the job.
143
	 *
144
	 * @return string Description of the job
145
	 */
146
	public function getDescription() : string
147
	{
148
		return $this->context()->translate( 'controller/jobs', 'Transfers the price of ordered products to the vendors incl. commission handling' );
149
	}
150
151
152
	/**
153
	 * Executes the job.
154
	 */
155
	public function run()
156
	{
157
		$context = $this->context();
158
		$manager = \Aimeos\MShop::create( $context, 'service' );
159
160
		$filter = $manager->filter()->add( ['service.type' => 'payment'] );
161
		$cursor = $manager->cursor( $filter );
162
163
		while( $items = $manager->iterate( $cursor ) )
164
		{
165
			foreach( $items as $item )
166
			{
167
				try
168
				{
169
					$provider = $manager->getProvider( $item, $item->getType() );
170
171
					if( $provider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_TRANSFER ) ) {
172
						$this->orders( $provider );
173
					}
174
				}
175
				catch( \Exception $e )
176
				{
177
					$str = 'Error while transferring payment for service with ID "%1$s": %2$s';
178
					$msg = sprintf( $str, $item->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
179
					$context->logger()->error( $msg, 'order/service/transfer' );
180
				}
181
			}
182
		}
183
	}
184
185
186
	/**
187
	 * Returns the number of days to postpone transfers
188
	 *
189
	 * @return string Date/time in "YYYY-MM-DD HH:mm:ss" format
190
	 */
191
	protected function limit() : string
192
	{
193
		/** controller/jobs/order/service/transfer/transfer-days
194
		 * Automatically transfers payments after the configured amount of days
195
		 *
196
		 * You can start transferring payments after the configured amount of days.
197
		 * Before, the money is hold back and not available to vendors.
198
		 *
199
		 * @param integer Number of days
200
		 * @since 2010.10
201
		 */
202
		$days = $this->context()->config()->get( 'controller/jobs/order/service/transfer/transfer-days', 0 );
203
		return date( 'Y-m-d H:i:s', time() - 86400 * $days );
204
	}
205
206
207
	/**
208
	 * Returns the domains used when fetching orders
209
	 *
210
	 * @return array List of data domain names
211
	 */
212
	protected function domains() : array
213
	{
214
		$config = $this->context()->config();
215
216
		/** controller/jobs/order/service/transfer/domains
217
		 * Associated items that should be available too in the order
218
		 *
219
		 * Orders consist of address, coupons, products and services. They can be
220
		 * fetched together with the order items and passed to the payment service
221
		 * providers. Available domains for those items are:
222
		 *
223
		 * - order/address
224
		 * - order/coupon
225
		 * - order/product
226
		 * - order/service
227
		 *
228
		 * @param array Referenced domain names
229
		 * @since 2022.04
230
		 */
231
		$ref = $config->get( 'mshop/order/manager/subdomains', [] );
232
		return $config->get( 'controller/jobs/order/service/transfer/domains', $ref );
233
	}
234
235
236
	/**
237
	 * Returns the maximum number of orders processed at once
238
	 *
239
	 * @return int Maximum number of items
240
	 */
241
	protected function max() : int
242
	{
243
		/** controller/jobs/order/service/transfer/batch-max
244
		 * Maximum number of orders processed at once by the payment service provider
245
		 *
246
		 * Orders are sent in batches if the payment service provider supports it.
247
		 * This setting configures the maximum orders that will be handed over to
248
		 * the payment service provider at once. Bigger batches an improve the
249
		 * performance but requires more memory.
250
		 *
251
		 * @param integer Number of orders
252
		 * @since 2023.04
253
		 * @see controller/jobs/order/service/transfer/domains
254
		 * @see controller/jobs/order/service/transfer/limit-days
255
		 */
256
		return $this->context()->config()->get( 'controller/jobs/order/service/transfer/batch-max', 100 );
257
	}
258
259
260
	/**
261
	 * Fetches and processes the order items
262
	 *
263
	 * @param \Aimeos\MShop\Service\Provider\Iface $provider Service provider for processing the orders
264
	 */
265
	protected function orders( \Aimeos\MShop\Service\Provider\Iface $provider )
266
	{
267
		$context = $this->context();
268
		$domains = $this->domains();
269
270
		$serviceItem = $provider->getServiceItem();
271
		$manager = \Aimeos\MShop::create( $context, 'order' );
272
273
		$filter = $manager->filter()->slice( 0, $this->max() );
274
		$filter->setConditions( $filter->and( [
275
			$filter->compare( '<=', 'order.ctime', $this->limit() ),
276
			$filter->compare( '==', 'order.statuspayment', \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED ),
277
			$filter->compare( '==', 'order.service.code', $serviceItem->getCode() ),
278
			$filter->compare( '==', 'order.service.type', 'payment' )
279
		] ) );
280
		$cursor = $manager->cursor( $filter );
281
282
		while( $items = $manager->iterate( $cursor, $domains ) )
283
		{
284
			foreach( $items as $item )
285
			{
286
				try
287
				{
288
					$manager->save( $provider->transfer( $item ) );
0 ignored issues
show
Bug introduced by
The method transfer() does not exist on Aimeos\MShop\Service\Provider\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Service\Provider\Delivery\Iface or Aimeos\MShop\Service\Provider\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

288
					$manager->save( $provider->/** @scrutinizer ignore-call */ transfer( $item ) );
Loading history...
289
				}
290
				catch( \Exception $e )
291
				{
292
					$str = 'Error while transferring payment for order with ID "%1$s": %2$s';
293
					$msg = sprintf( $str, $item->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
294
					$context->logger()->error( $msg, 'order/service/transfer' );
295
				}
296
			}
297
		}
298
	}
299
}
300