forked from magento/magento2
/
Group.php
178 lines (161 loc) · 4.62 KB
/
Group.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;
/**
* Customer group model
*
* @method \Magento\Customer\Model\ResourceModel\Group _getResource()
* @method \Magento\Customer\Model\ResourceModel\Group getResource()
* @method string getCustomerGroupCode()
* @method \Magento\Customer\Model\Group setCustomerGroupCode(string $value)
* @method \Magento\Customer\Model\Group setTaxClassId(int $value)
* @method Group setTaxClassName(string $value)
*/
class Group extends \Magento\Framework\Model\AbstractModel
{
const NOT_LOGGED_IN_ID = 0;
const CUST_GROUP_ALL = 32000;
const ENTITY = 'customer_group';
const GROUP_CODE_MAX_LENGTH = 32;
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'customer_group';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getObject() in this case
*
* @var string
*/
protected $_eventObject = 'object';
/**
* @var \Magento\Store\Model\StoresConfig
*/
protected $_storesConfig;
/**
* @var \Magento\Framework\Reflection\DataObjectProcessor
*/
protected $dataObjectProcessor;
/**
* @var \Magento\Tax\Model\ClassModelFactory
*/
protected $classModelFactory;
/**
* Constructor
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Store\Model\StoresConfig $storesConfig
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
* @param \Magento\Tax\Model\ClassModelFactory $classModelFactory
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Store\Model\StoresConfig $storesConfig,
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
\Magento\Tax\Model\ClassModelFactory $classModelFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->_storesConfig = $storesConfig;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->classModelFactory = $classModelFactory;
parent::__construct(
$context,
$registry,
$resource,
$resourceCollection,
$data
);
}
/**
* @return void
*/
protected function _construct()
{
$this->_init(\Magento\Customer\Model\ResourceModel\Group::class);
}
/**
* Alias for setCustomerGroupCode
*
* @param string $value
* @return $this
*/
public function setCode($value)
{
return $this->setCustomerGroupCode($value);
}
/**
* Alias for getCustomerGroupCode
*
* @return string
*/
public function getCode()
{
return $this->getCustomerGroupCode();
}
/**
* Get tax class name
*
* @return string
*/
public function getTaxClassName()
{
$taxClassName = $this->getData('tax_class_name');
if ($taxClassName) {
return $taxClassName;
}
$classModel = $this->classModelFactory->create();
$classModel->load($this->getTaxClassId());
$taxClassName = $classModel->getClassName();
$this->setData('tax_class_name', $taxClassName);
return $taxClassName;
}
/**
* Determine if this group is used as the create account default group
*
* @return bool
*/
public function usesAsDefault()
{
$data = $this->_storesConfig->getStoresConfigByPath(
GroupManagement::XML_PATH_DEFAULT_ID
);
if (in_array($this->getId(), $data)) {
return true;
}
return false;
}
/**
* Prepare data before save
*
* @return $this
*/
public function beforeSave()
{
$this->_prepareData();
return parent::beforeSave();
}
/**
* Prepare customer group data
*
* @return $this
*/
protected function _prepareData()
{
$this->setCode(substr($this->getCode(), 0, self::GROUP_CODE_MAX_LENGTH));
return $this;
}
}