-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistribution.module
527 lines (457 loc) · 18.5 KB
/
distribution.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/**
* @file
* Contains distribution.module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\RoleInterface;
const DISTRIBUTOR_ROLE_ID = 'distributor';
/**
* Implements hook_help().
*/
function distribution_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the distribution module.
case 'help.page.distribution':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides distribution functionality.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_install().
*/
function distribution_install() {
// 创建分销用户角色
$role = \Drupal\user\Entity\Role::create([
'langcode' => \Drupal::languageManager()->getCurrentLanguage()->getId(),
'id' => DISTRIBUTOR_ROLE_ID,
'label' => '分销用户'
]);
$role->save();
// 为已认证用户添加权限
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'restful post distribution_apply_promoter',
'restful post distribution_apply_for_distributor',
'restful post distribution_apply_for_leader',
'restful post distribution_compute_commission_amount',
'restful post distribution_upload_distributor_logo',
'restful post distribution_update_distributor_setting',
'restful get distribution_last_promoter',
'restful get distribution_distributor_report',
'view commission entities',
'view distributor entities'
]);
}
/**
* Implements hook_theme().
*/
function distribution_theme() {
$theme = [
'distribution' => [
'render element' => 'children',
],
];
$theme['distribution_task'] = [
'render element' => 'elements',
'file' => 'distribution_task.page.inc',
'template' => 'distribution_task',
];
$theme['distribution_task_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'distribution_task.page.inc',
];
return $theme;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function distribution_theme_suggestions_distribution_task(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#distribution_task'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'distribution_task__' . $sanitized_view_mode;
$suggestions[] = 'distribution_task__' . $entity->bundle();
$suggestions[] = 'distribution_task__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'distribution_task__' . $entity->id();
$suggestions[] = 'distribution_task__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_entity_base_field_info().
*/
function distribution_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() == 'commerce_order') {
$fields['distributor'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('成交订单的所属分销商'))
->setSetting('target_type', 'distribution_distributor')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label'
])
->setDisplayOptions('form', [
'type' => 'readonly_field_widget'
])
->setDisplayConfigurable('view', true)
->setDisplayConfigurable('form', true);
}
return $fields;
}
function distribution_enhanced_user_user_info_alter(array &$data) {
if (in_array('distributor', $data['roles'])) {
/** @var \Drupal\distribution\DistributionManager $distribution_manager */
$distribution_manager = \Drupal::getContainer()->get('distribution.distribution_manager');
$distributor = $distribution_manager->getDistributor($data['base']);
if ($distributor) {
$data['distributor'] = $distributor;
$leader = $distribution_manager::getLeader($distributor);
if ($leader instanceof \Drupal\distribution\Entity\LeaderInterface) {
$data['leader'] = $leader;
}
}
}
}
/**
* Implements hook_views_data_alter().
*/
function distribution_views_data_alter(array &$data) {
$data['distribution_distributor']['state']['filter']['id'] = 'state_machine_state';
}
/**
* Implements hook_form_alter().
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function distribution_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$id = $form_id;
}
/**
* Implements hook_inline_entity_form_entity_form_alter
*
* @param $entity_form
* @param $form_state
*/
function distribution_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] === 'commerce_product_variation') {
// TODO: 排除特定 bundle,写成配置
if ($entity_form['#entity']->bundle() === 'service_booking_online' || $entity_form['#entity']->bundle() === 'service_booking_sms') return;
if (_distribution_variation_form_alter($entity_form, $form_state))
$entity_form['#ief_element_submit'][] = 'distribution_save_variation_distribution_target';
}
}
function distribution_form_commerce_product_variation_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (_distribution_variation_form_alter($form, $form_state))
$form['actions']['submit']['#submit'][] = 'distribution_save_variation_distribution_target';
}
function _distribution_variation_form_alter(&$entity_form, \Drupal\Core\Form\FormStateInterface &$form_state) {
$config = \Drupal::config('distribution.settings');
// 没有开启分销系统
if (!$config->get('enable')) return false;
$entity_form['distribution'] = [
'#type' => 'fieldset',
'#title' => t('分销佣金设置')
];
/** @var \Drupal\commerce_product\Form\ProductVariationForm $entity_form_object */
$entity_form_object = $form_state->getBuildInfo()['callback_object'];
$entity = $entity_form_object->getEntity();
// 由于commerce_variation entity form被改进为非inline entity form,最新测试使得额外的表单回调可以拿到 entity id了
// 所以分销设置现在可以同步编辑保存
/*
if ($entity->isNew()) {
$entity_form['distribution']['#description'] = t('系统已开启了分销功能,保存产品数据后,方可在此设置佣金。');
return false;
}*/
$price = new \Drupal\commerce_price\Price('0.00', 'CNY');
// 读取已设置的数据
/** @var \Drupal\distribution\DistributionManager $distribution_manager */
$distribution_manager = \Drupal::getContainer()->get('distribution.distribution_manager');
/** @var \Drupal\distribution\Entity\Target $target */
$target = $distribution_manager->getTarget($entity);
$data = [
'amount_off' => $price->toArray(),
'amount_promotion' => $price->toArray(),
'amount_chain' => $price->toArray(),
'amount_chain_senior' => $price->toArray(),
'amount_leader' => $price->toArray(),
'amount_monthly_reward' => $price->toArray(),
'percentage_promotion' => 0.00,
'percentage_chain' => 0.00,
'percentage_chain_senior' => 0.00,
'percentage_leader' => 0.00,
'percentage_monthly_reward' => 0.00
];
if ($target) {
if ($target->getAmountOff()) $data['amount_off'] = $target->getAmountOff()->toArray();
if ($target->getAmountPromotion()) $data['amount_promotion'] = $target->getAmountPromotion()->toArray();
if ($target->getAmountChain()) $data['amount_chain'] = $target->getAmountChain()->toArray();
if ($target->getAmountChainSenior()) $data['amount_chain_senior'] = $target->getAmountChainSenior()->toArray();
if ($target->getAmountLeader()) $data['amount_leader'] = $target->getAmountLeader()->toArray();
if ($target->getAmountMonthlyReward()) $data['amount_monthly_reward'] = $target->getAmountMonthlyReward()->toArray();
if ($target->getPercentagePromotion()) $data['percentage_promotion'] = $target->getPercentagePromotion();
if ($target->getPercentageChain()) $data['percentage_chain'] = $target->getPercentageChain();
if ($target->getPercentageChainSenior()) $data['percentage_chain_senior'] = $target->getPercentageChainSenior();
if ($target->getPercentageLeader()) $data['percentage_leader'] = $target->getPercentageLeader();
if ($target->getPercentageMonthlyReward()) $data['percentage_monthly_reward'] = $target->getPercentageMonthlyReward();
}
if ($config->get('enable_amount_off')) {
// 设置分销优惠金额
$entity_form['distribution']['amount_off'] = [
'#type' => 'commerce_price',
'#title' => t('分销优惠金额'),
'#description' => t('普通用户通过推广链接购买,或分销用户购买时,将减免此金额。'),
'#default_value' => $data['amount_off'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
}
if ($config->get('commission.compute_mode') === 'fixed_amount') {
// 填写固定金额
// 推广佣金设置
if ($config->get('commission.promotion')) {
$entity_form['distribution']['amount_promotion'] = [
'#type' => 'commerce_price',
'#title' => t('推广佣金'),
'#default_value' => $data['amount_promotion'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
}
// 链级佣金设置
if ($config->get('commission.chain')) {
$entity_form['distribution']['chain'] = [
'#type' => 'fieldset',
'#title' => t('链级佣金')
];
$entity_form['distribution']['chain']['amount_chain'] = [
'#type' => 'commerce_price',
'#title' => t('普通分销商'),
'#default_value' => $data['amount_chain'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
// 高级分销
if ($config->get('chain_commission.enable_senior_distributor')) {
$entity_form['distribution']['chain']['amount_chain_senior'] = [
'#type' => 'commerce_price',
'#title' => t('高级分销商'),
'#default_value' => $data['amount_chain_senior'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
}
}
// 团队领导佣金设置
if ($config->get('commission.leader')) {
$entity_form['distribution']['amount_leader'] = [
'#type' => 'commerce_price',
'#title' => t('团队领导佣金'),
'#default_value' => $data['amount_leader'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
}
// 月度奖金设置
if ($config->get('commission.monthly_reward')) {
$entity_form['distribution']['amount_monthly_reward'] = [
'#type' => 'commerce_price',
'#title' => t('月度奖金'),
'#default_value' => $data['amount_monthly_reward'],
'#allow_negative' => FALSE,
'#maxlength' => 128,
'#required' => TRUE
];
}
} elseif ($config->get('commission.compute_mode') === 'dynamic_percentage') {
// 填写百分比
// 推广佣金设置
if ($config->get('commission.promotion')) {
$entity_form['distribution']['percentage_promotion'] = array(
'#type' => 'number',
'#title' => t('推广佣金比例'),
'#default_value' => $data['percentage_promotion'],
'#min' => 0.00,
'#max' => 100.00,
'#step' => 0.01,
'#field_suffix' => '%',
'#required' => TRUE
);
}
// 链级佣金设置
if ($config->get('commission.chain')) {
$entity_form['distribution']['chain'] = [
'#type' => 'fieldset',
'#title' => t('链级佣金')
];
$entity_form['distribution']['chain']['percentage_chain'] = array(
'#type' => 'number',
'#title' => t('普通分销商比例'),
'#default_value' => $data['percentage_chain'],
'#min' => 0.00,
'#max' => 100.00,
'#step' => 0.01,
'#field_suffix' => '%',
'#required' => TRUE
);
// 高级分销
if ($config->get('chain_commission.enable_senior_distributor')) {
$entity_form['distribution']['chain']['percentage_chain_senior'] = array(
'#type' => 'number',
'#title' => t('高级分销商比例'),
'#default_value' => $data['percentage_chain_senior'],
'#min' => 0.00,
'#max' => 100.00,
'#step' => 0.01,
'#field_suffix' => '%',
'#required' => TRUE
);
}
}
// 团队领导佣金设置
if ($config->get('commission.leader')) {
$entity_form['distribution']['percentage_leader'] = array(
'#type' => 'number',
'#title' => t('团队领导佣金比例'),
'#default_value' => $data['percentage_leader'],
'#min' => 0.00,
'#max' => 100.00,
'#step' => 0.01,
'#field_suffix' => '%',
'#required' => TRUE
);
}
// 月度奖金设置
if ($config->get('commission.monthly_reward')) {
$entity_form['distribution']['percentage_monthly_reward'] = array(
'#type' => 'number',
'#title' => t('月度奖金比例'),
'#default_value' => $data['percentage_monthly_reward'],
'#min' => 0.00,
'#max' => 100.00,
'#step' => 0.01,
'#field_suffix' => '%',
'#required' => TRUE
);
}
}
return true;
}
/**
* @param $form
* @param FormStateInterface $form_state
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function distribution_save_variation_distribution_target($form, FormStateInterface $form_state) {
/** @var \Drupal\commerce\PurchasableEntityInterface $entity */
$entity = isset($form['#entity']) ? $form['#entity'] : null;
if (!$entity) {
/** @var \Drupal\commerce_product\Form\ProductVariationForm $entity_form_object */
$entity_form_object = $form_state->getBuildInfo()['callback_object'];
$entity = $entity_form_object->getEntity();
}
$data = [
'name' => $entity->getOrderItemTitle()
];
$config = \Drupal::config('distribution.settings');
if ($config->get('enable_amount_off')) {
// 设置分销优惠金额
$data['amount_off'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['amount_off']['#parents']);
}
if ($config->get('commission.compute_mode') === 'fixed_amount') {
// 推广佣金设置
if ($config->get('commission.promotion')) {
$data['amount_promotion'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['amount_promotion']['#parents']);
}
// 链级佣金设置
if ($config->get('commission.chain')) {
$data['amount_chain'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['chain']['amount_chain']['#parents']);
if ($config->get('chain_commission.enable_senior_distributor')) {
$data['amount_chain_senior'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['chain']['amount_chain_senior']['#parents']);
}
}
// 团队领导佣金设置
if ($config->get('commission.leader')) {
$data['amount_leader'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['amount_leader']['#parents']);
}
// 月度奖金设置
if ($config->get('commission.monthly_reward')) {
$data['amount_monthly_reward'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['amount_monthly_reward']['#parents']);
}
} elseif ($config->get('commission.compute_mode') === 'dynamic_percentage') {
// 填写百分比
// 推广佣金设置
if ($config->get('commission.promotion')) {
$data['percentage_promotion'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['percentage_promotion']['#parents']);
}
// 链级佣金设置
if ($config->get('commission.chain')) {
$data['percentage_chain'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['chain']['percentage_chain']['#parents']);
if ($config->get('chain_commission.enable_senior_distributor')) {
$data['percentage_chain_senior'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['chain']['percentage_chain_senior']['#parents']);
}
}
// 团队领导佣金设置
if ($config->get('commission.leader')) {
$data['percentage_leader'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['percentage_leader']['#parents']);
}
// 月度奖金设置
if ($config->get('commission.monthly_reward')) {
$data['percentage_monthly_reward'] = NestedArray::getValue($form_state->getValues(), $form['distribution']['percentage_monthly_reward']['#parents']);
}
}
// 设置商品的分销标的 distribution_target
/** @var \Drupal\distribution\DistributionManager $distribution_manager */
$distribution_manager = \Drupal::getContainer()->get('distribution.distribution_manager');
$distribution_manager->setTarget($entity, $data);
}
/**
* Implements hook_cron().
*/
function distribution_cron() {
// 检查是否需要生成月度奖励报告
/** @var \Drupal\distribution\MonthlyRewardManagerInterface $monthly_reward_manager */
$monthly_reward_manager = \Drupal::getContainer()->get('distribution.monthly_reward_manager');
\Drupal::logger('distribution')->notice('正在检查是否需要生成月度奖励报告');
$monthly_reward_manager->generateMonthlyCommissionStatement();
}
function distribution_entity_operation(Drupal\Core\Entity\EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() === 'distribution_distributor') {
$operations['owner'] = [
'title' => t('用户帐号'),
'url' => \Drupal\Core\Url::fromRoute('entity.distribution_distributor.owner', ['distribution_distributor' => $entity->id()]),
'weight' => 20,
];
$operations['account'] = [
'title' => t('余额账户'),
'url' => \Drupal\Core\Url::fromRoute('entity.distribution_distributor.account', ['distribution_distributor' => $entity->id()]),
'weight' => 20,
];
}
return $operations;
}
function distribution_jsonapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
// For every entity type that has an admin permission, allow access to filter
// by all entities of that type to users with that permission.
if ($account->isAuthenticated()) {
return [
JSONAPI_FILTER_AMONG_ALL => AccessResult::allowed()
];
}
}