-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupplier.module
234 lines (201 loc) · 6.59 KB
/
supplier.module
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file
* Contains supplier.module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
const SUPPLIER_ROLE_ID = 'supplier';
const SUPPLIER_FINANCE_ACCOUNT_TYPE = 'supplier';
/**
* Implements hook_help().
*/
function supplier_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the supplier module.
case 'help.page.supplier':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('My Awesome Module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_install().
*/
function supplier_install() {
// 添加权限
user_role_grant_permissions(SUPPLIER_ROLE_ID, [
'access toolbar',
'apply withdraw for own finance account',
'update own platform commerce_store',
'update own supplier commerce_store',
'view account entities',
'view ledger entities',
'view platform commerce_store',
'view supplier commerce_store',
'view the administration theme',
'view supplier in toolbar',
'view withdraw entities'
]);
}
/**
* Implements hook_theme().
*/
function supplier_theme() {
return [
'supplier' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_entity_base_field_info_alter()
*
* 修改 commerce_store实体的默认字段设置
*/
function supplier_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
/* @var $fields \Drupal\Core\Field\BaseFieldDefinition[] */
if ($entity_type->id() == 'commerce_store') {
// 修改默认货币为 RMB
if (!empty($fields['default_currency'])) {
$fields['default_currency']
->setDefaultValue('CNY');
}
}
}
/**
* Implements hook_form_FORM_ID_alter()
*
* FORM_ID: commerce_store_supplier_add_form
*
* 在添加供应商时,显示创建登录帐号表单
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function supplier_form_commerce_store_supplier_add_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
_supplier_add_account_form_elements($form);
}
/**
* Implements hook_form_FORM_ID_alter()
*
* FORM_ID: commerce_store_platform_add_form
*
* 在添加自营商家时,显示创建登录帐号表单
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function supplier_form_commerce_store_platform_add_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
_supplier_add_account_form_elements($form);
}
function _supplier_add_account_form_elements(&$form) {
$form['account'] = array(
'#type' => 'fieldset',
'#title' => t('创建登录账号'),
);
// TODO: 检查用户名的可用性和唯一性
$form['account']['username'] = array(
'#type' => 'textfield',
'#title' => t('用户名'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => \Drupal\user\UserInterface::USERNAME_MAX_LENGTH,
'#description' => t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
'#required' => TRUE,
);
$form['account']['password'] = array(
'#type' => 'textfield',
'#title' => t('密码'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
);
$form['actions']['submit']['#submit'][] = 'supplier_create_supplier_user';
}
/**
* 创建一个供应商用户
* @param $form
* @param FormStateInterface $form_state
*/
function supplier_create_supplier_user($form, FormStateInterface $form_state) {
// 读取用户名密码
$form_values = $form_state->getValues();
$username = \Drupal\Component\Utility\NestedArray::getValue($form_values, $form['account']['username']['#parents']);
$password = \Drupal\Component\Utility\NestedArray::getValue($form_values, $form['account']['password']['#parents']);
$email = \Drupal\Component\Utility\NestedArray::getValue($form_values, $form['mail']['widget'][0]['#parents'])['value'];
$store = $form_state->getFormObject()->getEntity();
_supplier_create_supplier_resource($store, $username, $password, $email);
}
/**
* 为一个供应商创建相关资源:登录账号、记账账户
*
* @param $store
* @param $username
* @param $password
* @param $email
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _supplier_create_supplier_resource($store, $username, $password, $email) {
// 创建用户
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = \Drupal\user\Entity\User::create();
$user->setPassword($password);
$user->enforceIsNew();
$user->setEmail($email);
$user->setUsername($username); //This username must be unique and accept only a-Z,0-9, - _ @ .
$user->addRole(SUPPLIER_ROLE_ID);
$user->activate();
$user->save();
// 把用户 id保存到 供应商的 uid字段
$store->setOwner($user);
$store->save();
// 创建佣金管理账户(调用Finance模块)
$financeFinanceManager = \Drupal::getContainer()->get('finance.finance_manager');
$financeFinanceManager->createAccount($user, SUPPLIER_FINANCE_ACCOUNT_TYPE);
}
/**
* Implements hook_entity_base_field_info().
*/
function supplier_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() == 'commerce_product_variation') {
$fields['cost'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('成本价'))
->setDescription('用于与商家进行结算的价格,一般比平台实际销售价格要低。')
->setRequired(true)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'commerce_price_default'
])
->setDisplayOptions('form', [
'type' => 'commerce_price_default'
])
->setDisplayConfigurable('view', true)
->setDisplayConfigurable('form', true);
}
return $fields;
}
function supplier_entity_operation(Drupal\Core\Entity\EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() === 'commerce_store') {
$operations['owner'] = [
'title' => t('用户帐号'),
'url' => \Drupal\Core\Url::fromRoute('entity.commerce_store.owner', ['commerce_store' => $entity->id()]),
'weight' => 20,
];
$operations['finance_account'] = [
'title' => t('余额账户'),
'url' => \Drupal\Core\Url::fromRoute('entity.commerce_store.finance_account', ['commerce_store' => $entity->id()]),
'weight' => 20,
];
}
return $operations;
}