Passed
Push — master ( faf2f5...510396 )
by Aimeos
03:53
created

Standard::fromArray()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 40
rs 8.9457
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Order\Transaction;
12
13
sprintf( 'transaction' ); // for translation
14
15
16
/**
17
 * Default implementation of order transaction JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/order/transaction/name
27
	 * Name of the transaction subpart used by the JQAdm order implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Order\Transaction\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2023.01
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string|null HTML output
42
	 */
43
	public function copy() : ?string
44
	{
45
		return $this->get();
46
	}
47
48
49
	/**
50
	 * Creates a new resource
51
	 *
52
	 * @return string|null HTML output
53
	 */
54
	public function create() : ?string
55
	{
56
		return $this->get();
57
	}
58
59
60
	/**
61
	 * Returns a single resource
62
	 *
63
	 * @return string|null HTML output
64
	 */
65
	public function get() : ?string
66
	{
67
		$view = $this->object()->data( $this->view() );
68
69
		return $this->render( $view );
70
	}
71
72
73
	/**
74
	 * Saves the data
75
	 *
76
	 * @return string|null HTML output
77
	 */
78
	public function save() : ?string
79
	{
80
		$view = $this->view();
81
82
		$this->fromArray( $view->item, $view->param( 'transaction', [] ) );
83
		$view->transactionBody = parent::save();
84
85
		return null;
86
	}
87
88
89
	/**
90
	 * Returns the sub-client given by its name.
91
	 *
92
	 * @param string $type Name of the client type
93
	 * @param string|null $name Name of the sub-client (Default if null)
94
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
95
	 */
96
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
97
	{
98
		/** admin/jqadm/order/transaction/decorators/excludes
99
		 * Excludes decorators added by the "common" option from the order JQAdm client
100
		 *
101
		 * Decorators extend the functionality of a class by adding new aspects
102
		 * (e.g. log what is currently done), executing the methods of the underlying
103
		 * class only in certain conditions (e.g. only for logged in users) or
104
		 * modify what is returned to the caller.
105
		 *
106
		 * This option allows you to remove a decorator added via
107
		 * "admin/jqadm/common/decorators/default" before they are wrapped
108
		 * around the JQAdm client.
109
		 *
110
		 *  admin/jqadm/order/transaction/decorators/excludes = array( 'decorator1' )
111
		 *
112
		 * This would remove the decorator named "decorator1" from the list of
113
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
114
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
115
		 *
116
		 * @param array List of decorator names
117
		 * @since 2023.01
118
		 * @category Developer
119
		 * @see admin/jqadm/common/decorators/default
120
		 * @see admin/jqadm/order/transaction/decorators/global
121
		 * @see admin/jqadm/order/transaction/decorators/local
122
		 */
123
124
		/** admin/jqadm/order/transaction/decorators/global
125
		 * Adds a list of globally available decorators only to the order JQAdm client
126
		 *
127
		 * Decorators extend the functionality of a class by adding new aspects
128
		 * (e.g. log what is currently done), executing the methods of the underlying
129
		 * class only in certain conditions (e.g. only for logged in users) or
130
		 * modify what is returned to the caller.
131
		 *
132
		 * This option allows you to wrap global decorators
133
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
134
		 *
135
		 *  admin/jqadm/order/transaction/decorators/global = array( 'decorator1' )
136
		 *
137
		 * This would add the decorator named "decorator1" defined by
138
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
139
		 *
140
		 * @param array List of decorator names
141
		 * @since 2023.01
142
		 * @category Developer
143
		 * @see admin/jqadm/common/decorators/default
144
		 * @see admin/jqadm/order/transaction/decorators/excludes
145
		 * @see admin/jqadm/order/transaction/decorators/local
146
		 */
147
148
		/** admin/jqadm/order/transaction/decorators/local
149
		 * Adds a list of local decorators only to the order JQAdm client
150
		 *
151
		 * Decorators extend the functionality of a class by adding new aspects
152
		 * (e.g. log what is currently done), executing the methods of the underlying
153
		 * class only in certain conditions (e.g. only for logged in users) or
154
		 * modify what is returned to the caller.
155
		 *
156
		 * This option allows you to wrap local decorators
157
		 * ("\Aimeos\Admin\JQAdm\Order\Decorator\*") around the JQAdm client.
158
		 *
159
		 *  admin/jqadm/order/transaction/decorators/local = array( 'decorator2' )
160
		 *
161
		 * This would add the decorator named "decorator2" defined by
162
		 * "\Aimeos\Admin\JQAdm\Order\Decorator\Decorator2" only to the JQAdm client.
163
		 *
164
		 * @param array List of decorator names
165
		 * @since 2023.01
166
		 * @category Developer
167
		 * @see admin/jqadm/common/decorators/default
168
		 * @see admin/jqadm/order/transaction/decorators/excludes
169
		 * @see admin/jqadm/order/transaction/decorators/global
170
		 */
171
		return $this->createSubClient( 'order/transaction/' . $type, $name );
172
	}
173
174
175
	/**
176
	 * Returns the list of sub-client names configured for the client.
177
	 *
178
	 * @return array List of JQAdm client names
179
	 */
180
	protected function getSubClientNames() : array
181
	{
182
		/** admin/jqadm/order/transaction/subparts
183
		 * List of JQAdm sub-clients rendered within the order transaction section
184
		 *
185
		 * The output of the frontend is composed of the code generated by the JQAdm
186
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
187
		 * that are responsible for rendering certain sub-parts of the output. The
188
		 * sub-clients can contain JQAdm clients themselves and therefore a
189
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
190
		 * the output that is placed inside the container of its parent.
191
		 *
192
		 * At first, always the JQAdm code generated by the parent is printed, then
193
		 * the JQAdm code of its sub-clients. The transaction of the JQAdm sub-clients
194
		 * determines the transaction of the output of these sub-clients inside the parent
195
		 * container. If the configured list of clients is
196
		 *
197
		 *  array( "subclient1", "subclient2" )
198
		 *
199
		 * you can easily change the transaction of the output by retransactioning the subparts:
200
		 *
201
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
202
		 *
203
		 * You can also remove one or more parts if they shouldn't be rendered:
204
		 *
205
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
206
		 *
207
		 * As the clients only generates structural JQAdm, the layout defined via CSS
208
		 * should support adding, removing or retransactioning content by a fluid like
209
		 * design.
210
		 *
211
		 * @param array List of sub-client names
212
		 * @since 2023.01
213
		 * @category Developer
214
		 */
215
		return $this->context()->config()->get( 'admin/jqadm/order/transaction/subparts', [] );
216
	}
217
218
219
	/**
220
	 * Creates new and updates existing items using the data array
221
	 *
222
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $order Order base item object
223
	 * @param array $data Data array
224
	 */
225
	protected function fromArray( \Aimeos\MShop\Order\Item\Base\Iface $order, array $data )
226
	{
227
		$services = map( $order->getService( 'payment' ) )->col( null, 'order.base.service.id' );
228
229
		foreach( $data as $serviceId => $entry )
230
		{
231
			if( array_sum( $entry ) === 0 || !isset( $services[$serviceId] ) ) {
232
				continue;
233
			}
234
235
			$context = $this->context();
236
			$service = $services[$serviceId];
237
238
			$price = \Aimeos\MShop::create( $context, 'price' )->create()
239
				->setValue( -$entry['order.base.service.transaction.value'] ?? 0 )
240
				->setCosts( -$entry['order.base.service.transaction.costs'] ?? 0 )
241
				->setCurrencyId( $service->getPrice()->getCurrencyId() );
0 ignored issues
show
Bug introduced by
The method getPrice() does not exist on null. ( Ignorable by Annotation )

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

241
				->setCurrencyId( $service->/** @scrutinizer ignore-call */ getPrice()->getCurrencyId() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
242
243
			$txItem = \Aimeos\MShop::create( $context, 'order/base/service' )->createTransaction()
0 ignored issues
show
Bug introduced by
The method createTransaction() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

243
			$txItem = \Aimeos\MShop::create( $context, 'order/base/service' )->/** @scrutinizer ignore-call */ createTransaction()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
244
				->setType( 'payment' )->setPrice( $price )->setStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUND );
245
246
			$serviceItem = \Aimeos\MShop::create( $context, 'service' )->get( $service->getServiceId() );
247
248
			$provider = \Aimeos\MShop::create( $context, 'service' )->getProvider( $serviceItem, 'payment' );
249
250
			if( $provider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_REFUND ) )
251
			{
252
				$manager = \Aimeos\MShop::create( $context, 'order' );
253
				$filter = $manager->filter()->add( 'order.baseid', '==', $order->getId() );
254
255
				if( $order = $manager->search( $filter )->first() ) {
256
					$manager->save( $provider->refund( $order, $price ) );
257
				}
258
			}
259
			else
260
			{
261
				$txItem->setConfigValue( 'info', $context->translate( 'admin', 'Manual transfer' ) );
262
			}
263
264
			$service->addTransaction( $txItem );
265
		}
266
	}
267
268
269
	/**
270
	 * Returns the rendered template including the view data
271
	 *
272
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
273
	 * @return string HTML output
274
	 */
275
	protected function render( \Aimeos\Base\View\Iface $view ) : string
276
	{
277
		/** admin/jqadm/order/transaction/template-item
278
		 * Relative path to the HTML body template of the transaction subpart for orders.
279
		 *
280
		 * The template file contains the HTML code and processing instructions
281
		 * to generate the result shown in the body of the frontend. The
282
		 * configuration string is the path to the template file relative
283
		 * to the templates directory (usually in templates/admin/jqadm).
284
		 *
285
		 * You can overwrite the template file configuration in extensions and
286
		 * provide alternative templates. These alternative templates should be
287
		 * named like the default one but with the string "default" replaced by
288
		 * an unique name. You may use the name of your project for this. If
289
		 * you've implemented an alternative client class as well, "default"
290
		 * should be replaced by the name of the new class.
291
		 *
292
		 * @param string Relative path to the template creating the HTML code
293
		 * @since 2016.04
294
		 * @category Developer
295
		 */
296
		$tplconf = 'admin/jqadm/order/transaction/template-item';
297
		$default = 'order/item-transaction';
298
299
		return $view->render( $view->config( $tplconf, $default ) );
300
	}
301
}
302