Completed
Push — master ( d935a1...b40d8d )
by Aimeos
09:03
created

Quantity::calcPrice()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 1
dl 0
loc 28
rs 8.5806
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), 2017
6
 * @package MShop
7
 * @subpackage Service
8
 */
9
10
11
namespace Aimeos\MShop\Service\Provider\Decorator;
12
13
14
/**
15
 * Decorator for adding quantity based costs
16
 *
17
 * This decorator interacts with the ServiceUpdate and Autofill basket plugins!
18
 * If the delivery/payment option isn't available any more, the ServiceUpdate
19
 * plugin will remove it from the basket and the Autofill plugin will add one
20
 * of the available options again.
21
 *
22
 * @package MShop
23
 * @subpackage Service
24
 */
25
class Quantity
26
	extends \Aimeos\MShop\Service\Provider\Decorator\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
27
	implements \Aimeos\MShop\Service\Provider\Decorator\Iface
28
{
29
	private $beConfig = array(
30
		'quantity.packagesize' => array(
31
			'code' => 'quantity.packagesize',
32
			'internalcode' => 'quantity.packagesize',
33
			'label' => 'Number of products in the package',
34
			'type' => 'number',
35
			'internaltype' => 'integer',
36
			'default' => '1',
37
			'required' => false,
38
		),
39
		'quantity.packagecosts' => array(
40
			'code' => 'quantity.packagecosts',
41
			'internalcode' => 'quantity.packagecosts',
42
			'label' => 'Costs per the package',
43
			'type' => 'number',
44
			'internaltype' => 'float',
45
			'default' => '',
46
			'required' => true,
47
		),
48
	);
49
50
51
	/**
52
	 * Checks the backend configuration attributes for validity.
53
	 *
54
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
55
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
56
	 *    known by the provider but aren't valid
57
	 */
58
	public function checkConfigBE( array $attributes )
59
	{
60
		$error = $this->getProvider()->checkConfigBE( $attributes );
61
		$error += $this->checkConfig( $this->beConfig, $attributes );
62
63
		return $error;
64
	}
65
66
67
	/**
68
	 * Returns the configuration attribute definitions of the provider
69
	 *
70
	 * This will generate a list of available fields and rules for the value of
71
	 * each field in the administration interface.
72
	 *
73
	 * @return array List of attribute definitions implementing MW_Common_Critera_Attribute_Interface
74
	 */
75
	public function getConfigBE()
76
	{
77
		return array_merge( $this->getProvider()->getConfigBE(), $this->getConfigItems( $this->beConfig ) );
78
	}
79
80
81
	/**
82
	 * Returns the price when using the provider.
83
	 *
84
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
85
	 * @return \Aimeos\MShop\Price\Item\Iface Price item containing the price, shipping, rebate
86
	 */
87
	public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $basket )
88
	{
89
		$sum = 0;
90
		$price = $this->getProvider()->calcPrice( $basket );
91
92
		foreach( $basket->getProducts() as $basketItem )
93
		{
94
			$qty = $basketItem->getQuantity();
95
96
			if( ( $products = $basketItem->getProducts() ) !== [] )
97
			{
98
				foreach( $products as $prodItem ) { // calculate bundled products
99
					$sum += $qty * $prodItem->getQuantity();
100
				}
101
			}
102
			else
103
			{
104
				$sum += $qty;
105
			}
106
		}
107
108
		$size = $this->getConfigValue( array( 'quantity.packagesize' ), 1 );
109
		$costs = $this->getConfigValue( array( 'quantity.packagecosts' ), 0.00 );
110
111
		$value = ceil( $sum / $size ) * $costs;
112
113
		return $price->setCosts( $price->getCosts() + $value );
114
	}
115
}
116