Passed
Push — master ( 7a15f5...abb755 )
by Aimeos
10:22 queued 06:39
created

Standard::save()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 22
rs 9.8666
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Order\Invoice;
12
13
sprintf( 'invoice' ); // for translation
14
15
16
/**
17
 * Default implementation of order invoice 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/invoice/name
27
	 * Name of the invoice subpart used by the JQAdm order implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Order\Invoice\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 2017.07
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->getObject()->addData( $this->getView() );
68
69
		$total = 0;
70
		$params = $this->storeFilter( $view->param( 'oi', [] ), 'orderinvoice' );
71
		$orderItems = $this->getOrderItems( $view->item, $params, $total );
72
73
		$view->invoiceData = $this->toArray( $orderItems );
74
		$view->invoiceTotal = parent::get();
75
		$view->invoiceTotal = $total;
76
77
		return $this->render( $view );
78
	}
79
80
81
	/**
82
	 * Saves the data
83
	 *
84
	 * @return string|null HTML output
85
	 */
86
	public function save() : ?string
87
	{
88
		$view = $this->getView();
89
90
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order' );
91
		$manager->begin();
92
93
		try
94
		{
95
			$this->storeFilter( $view->param( 'oi', [] ), 'orderinvoice' );
96
			$this->fromArray( $view->item, $view->param( 'invoice', [] ) );
97
			$view->invoiceBody = parent::save();
98
99
			$manager->commit();
100
		}
101
		catch( \Exception $e )
102
		{
103
			$manager->rollback();
104
			throw $e;
105
		}
106
107
		return null;
108
	}
109
110
111
	/**
112
	 * Returns the sub-client given by its name.
113
	 *
114
	 * @param string $type Name of the client type
115
	 * @param string|null $name Name of the sub-client (Default if null)
116
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
117
	 */
118
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
119
	{
120
		/** admin/jqadm/order/invoice/decorators/excludes
121
		 * Excludes decorators added by the "common" option from the order JQAdm client
122
		 *
123
		 * Decorators extend the functionality of a class by adding new aspects
124
		 * (e.g. log what is currently done), executing the methods of the underlying
125
		 * class only in certain conditions (e.g. only for logged in users) or
126
		 * modify what is returned to the caller.
127
		 *
128
		 * This option allows you to remove a decorator added via
129
		 * "admin/jqadm/common/decorators/default" before they are wrapped
130
		 * around the JQAdm client.
131
		 *
132
		 *  admin/jqadm/order/invoice/decorators/excludes = array( 'decorator1' )
133
		 *
134
		 * This would remove the decorator named "decorator1" from the list of
135
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
136
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
137
		 *
138
		 * @param array List of decorator names
139
		 * @since 2017.07
140
		 * @category Developer
141
		 * @see admin/jqadm/common/decorators/default
142
		 * @see admin/jqadm/order/invoice/decorators/global
143
		 * @see admin/jqadm/order/invoice/decorators/local
144
		 */
145
146
		/** admin/jqadm/order/invoice/decorators/global
147
		 * Adds a list of globally available decorators only to the order JQAdm client
148
		 *
149
		 * Decorators extend the functionality of a class by adding new aspects
150
		 * (e.g. log what is currently done), executing the methods of the underlying
151
		 * class only in certain conditions (e.g. only for logged in users) or
152
		 * modify what is returned to the caller.
153
		 *
154
		 * This option allows you to wrap global decorators
155
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
156
		 *
157
		 *  admin/jqadm/order/invoice/decorators/global = array( 'decorator1' )
158
		 *
159
		 * This would add the decorator named "decorator1" defined by
160
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
161
		 *
162
		 * @param array List of decorator names
163
		 * @since 2017.07
164
		 * @category Developer
165
		 * @see admin/jqadm/common/decorators/default
166
		 * @see admin/jqadm/order/invoice/decorators/excludes
167
		 * @see admin/jqadm/order/invoice/decorators/local
168
		 */
169
170
		/** admin/jqadm/order/invoice/decorators/local
171
		 * Adds a list of local decorators only to the order JQAdm client
172
		 *
173
		 * Decorators extend the functionality of a class by adding new aspects
174
		 * (e.g. log what is currently done), executing the methods of the underlying
175
		 * class only in certain conditions (e.g. only for logged in users) or
176
		 * modify what is returned to the caller.
177
		 *
178
		 * This option allows you to wrap local decorators
179
		 * ("\Aimeos\Admin\JQAdm\Order\Decorator\*") around the JQAdm client.
180
		 *
181
		 *  admin/jqadm/order/invoice/decorators/local = array( 'decorator2' )
182
		 *
183
		 * This would add the decorator named "decorator2" defined by
184
		 * "\Aimeos\Admin\JQAdm\Order\Decorator\Decorator2" only to the JQAdm client.
185
		 *
186
		 * @param array List of decorator names
187
		 * @since 2017.07
188
		 * @category Developer
189
		 * @see admin/jqadm/common/decorators/default
190
		 * @see admin/jqadm/order/invoice/decorators/excludes
191
		 * @see admin/jqadm/order/invoice/decorators/global
192
		 */
193
		return $this->createSubClient( 'order/invoice/' . $type, $name );
194
	}
195
196
197
	/**
198
	 * Returns the list of sub-client names configured for the client.
199
	 *
200
	 * @return array List of JQAdm client names
201
	 */
202
	protected function getSubClientNames() : array
203
	{
204
		/** admin/jqadm/order/invoice/subparts
205
		 * List of JQAdm sub-clients rendered within the order invoice section
206
		 *
207
		 * The output of the frontend is composed of the code generated by the JQAdm
208
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
209
		 * that are responsible for rendering certain sub-parts of the output. The
210
		 * sub-clients can contain JQAdm clients themselves and therefore a
211
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
212
		 * the output that is placed inside the container of its parent.
213
		 *
214
		 * At first, always the JQAdm code generated by the parent is printed, then
215
		 * the JQAdm code of its sub-clients. The invoice of the JQAdm sub-clients
216
		 * determines the invoice of the output of these sub-clients inside the parent
217
		 * container. If the configured list of clients is
218
		 *
219
		 *  array( "subclient1", "subclient2" )
220
		 *
221
		 * you can easily change the invoice of the output by reinvoiceing the subparts:
222
		 *
223
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
224
		 *
225
		 * You can also remove one or more parts if they shouldn't be rendered:
226
		 *
227
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
228
		 *
229
		 * As the clients only generates structural JQAdm, the layout defined via CSS
230
		 * should support adding, removing or reinvoiceing content by a fluid like
231
		 * design.
232
		 *
233
		 * @param array List of sub-client names
234
		 * @since 2017.07
235
		 * @category Developer
236
		 */
237
		return $this->getContext()->getConfig()->get( 'admin/jqadm/order/invoice/subparts', [] );
238
	}
239
240
241
	/**
242
	 * Returns the order items (invoice records) for the given order base (basket) ID
243
	 *
244
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $order Current order base item
245
	 * @param string $params GET/POST parameters containing the filter values
246
	 * @param integer $total Value/result parameter that will contain the item total afterwards
247
	 * @return \Aimeos\Map List of order IDs as keys and items implementing \Aimeos\MShop\Order\Item\Iface
248
	 */
249
	protected function getOrderItems( \Aimeos\MShop\Order\Item\Base\Iface $order, array $params, &$total ) : \Aimeos\Map
250
	{
251
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order' );
252
253
		$search = $manager->filter( false, true );
254
		$search->setSortations( [$search->sort( '-', 'order.ctime' )] );
255
256
		$search = $this->initCriteria( $search, $params, 'orderinvoice' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Base::initCriteria() has too many arguments starting with 'orderinvoice'. ( Ignorable by Annotation )

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

256
		/** @scrutinizer ignore-call */ 
257
  $search = $this->initCriteria( $search, $params, 'orderinvoice' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
257
		$expr = [
258
			$search->compare( '==', 'order.baseid', $order->getId() ),
259
			$search->getConditions(),
260
		];
261
		$search->setConditions( $search->and( $expr ) );
262
263
		return $manager->search( $search, [], $total );
264
	}
265
266
267
	/**
268
	 * Creates new and updates existing items using the data array
269
	 *
270
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $order Order base item object
271
	 * @param array $data Data array
272
	 */
273
	protected function fromArray( \Aimeos\MShop\Order\Item\Base\Iface $order, array $data )
274
	{
275
		$invoiceIds = $this->getValue( $data, 'order.id', [] );
276
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order' );
277
278
		$search = $manager->filter()->slice( 0, count( $invoiceIds ) );
279
		$search->setConditions( $search->compare( '==', 'order.id', $invoiceIds ) );
280
281
		$items = $manager->search( $search );
282
283
284
		foreach( $invoiceIds as $idx => $id )
285
		{
286
			if( !isset( $items[$id] ) ) {
287
				$item = $manager->create();
288
			} else {
289
				$item = $items[$id];
290
			}
291
292
			$item->setType( $this->getValue( $data, 'order.type/' . $idx, $item->getType() ) );
293
			$item->setStatusDelivery( $this->getValue( $data, 'order.statusdelivery/' . $idx ) );
294
			$item->setStatusPayment( $this->getValue( $data, 'order.statuspayment/' . $idx ) );
295
			$item->setDateDelivery( $this->getValue( $data, 'order.datedelivery/' . $idx ) );
296
			$item->setDatePayment( $this->getValue( $data, 'order.datepayment/' . $idx ) );
297
			$item->setRelatedId( $this->getValue( $data, 'order.relatedid/' . $idx ) );
298
			$item->setBaseId( $order->getId() );
299
300
			$manager->save( $item );
301
		}
302
	}
303
304
305
	/**
306
	 * Constructs the data array for the view from the given item
307
	 *
308
	 * @param \Aimeos\Map $invoices List of invoices implementing \Aimeos\MShop\Order\Item\Iface belonging to the order
309
	 * @return array Multi-dimensional associative list of item data
310
	 */
311
	protected function toArray( \Aimeos\Map $invoices ) : array
312
	{
313
		$data = [];
314
315
		foreach( $invoices as $item )
316
		{
317
			foreach( $item->toArray( true ) as $key => $value ) {
318
				$data[$key][] = $value;
319
			}
320
		}
321
322
		return $data;
323
	}
324
325
326
	/**
327
	 * Returns the rendered template including the view data
328
	 *
329
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
330
	 * @return string HTML output
331
	 */
332
	protected function render( \Aimeos\MW\View\Iface $view ) : string
333
	{
334
		/** admin/jqadm/order/invoice/template-item
335
		 * Relative path to the HTML body template of the invoice subpart for orders.
336
		 *
337
		 * The template file contains the HTML code and processing instructions
338
		 * to generate the result shown in the body of the frontend. The
339
		 * configuration string is the path to the template file relative
340
		 * to the templates directory (usually in admin/jqadm/templates).
341
		 *
342
		 * You can overwrite the template file configuration in extensions and
343
		 * provide alternative templates. These alternative templates should be
344
		 * named like the default one but with the string "default" replaced by
345
		 * an unique name. You may use the name of your project for this. If
346
		 * you've implemented an alternative client class as well, "default"
347
		 * should be replaced by the name of the new class.
348
		 *
349
		 * @param string Relative path to the template creating the HTML code
350
		 * @since 2016.04
351
		 * @category Developer
352
		 */
353
		$tplconf = 'admin/jqadm/order/invoice/template-item';
354
		$default = 'order/item-invoice-standard';
355
356
		return $view->render( $view->config( $tplconf, $default ) );
357
	}
358
}
359