FillDeductionLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace addons\CloudStock\common\logic\deduction;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  4. use addons\CloudStock\common\logic\reward\FillRewardLogic;
  5. use addons\CloudStock\common\models\CloudStockAgent;
  6. use addons\CloudStock\common\models\CloudStockGoods;
  7. use addons\CloudStock\common\models\CloudStockLevel;
  8. use common\models\user\UserPartitionRelationship;
  9. use common\enums\AddonsEnum;
  10. use common\enums\StatusEnum;
  11. use common\helpers\BcHelper;
  12. class FillDeductionLogic extends DeductionLogic
  13. {
  14. public function getUnitGoodsPrice()
  15. {
  16. if ($this->order->pay_money <= 0) return 0;
  17. $this->unit_goods_price = $this->getOrderGoodsUnitPrice();
  18. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  19. //队列触发的,不用再计算平级、越级奖
  20. //平级奖、越级奖扣除
  21. //获取补货者代理商数据
  22. $agent = CloudStockAgent::findOne(['user_id' => $this->user_id, 'mall_id' => $this->mall_id,'status'=>StatusEnum::ENABLED]);
  23. $stock_good = CloudStockGoods::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $this->order['goods_id']]);
  24. //紧凑平级奖
  25. if(!empty($stock_good['is_find_equal'])){
  26. $parent_id_arr = UserPartitionRelationship::getParentIdArr($this->user_id);
  27. $parent_id_arr = array_reverse($parent_id_arr);
  28. $reward_list = [];
  29. $is_find_equal = false;
  30. foreach ($parent_id_arr as $parent_id) {
  31. $parent_agent = CloudStockAgent::findOne(['user_id' => $parent_id, 'mall_id' => $this->mall_id,'status'=>StatusEnum::ENABLED]);
  32. //找平级
  33. if(!empty($parent_agent)){
  34. if($parent_agent['level'] < $agent['level']) {
  35. if (!empty($this->user['parent_id']) && $parent_id == $this->user['parent_id']) {
  36. $reward_list[] = [
  37. 'level' => 1,
  38. 'agent' => $parent_agent,
  39. ];
  40. }
  41. if (!empty($this->user['second_parent_id']) && $parent_id == $this->user['second_parent_id']) {
  42. $reward_list[] = [
  43. 'level' => 2,
  44. 'agent' => $parent_agent,
  45. ];
  46. }
  47. }elseif($parent_agent['level'] == $agent['level']){
  48. if(!$is_find_equal){
  49. $reward_list[] = [
  50. 'level' => 1,
  51. 'agent' => $parent_agent,
  52. ];
  53. $is_find_equal = true;
  54. }else{
  55. $reward_list[] = [
  56. 'level' => 2,
  57. 'agent' => $parent_agent,
  58. ];
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. if(!empty($reward_list)){
  65. foreach($reward_list as $reward){
  66. if($reward['agent']['level'] < $agent['level']){
  67. $over_desc = "补货越级奖发放,越级层数".$reward['level'];
  68. $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $reward['agent']['level'], 'status' => StatusEnum::ENABLED]);
  69. $this->sendOverReward($reward['agent'], $stock_level, $reward['level'], $over_desc, $this->order['num'], !empty($setting['is_by_buy_agent_level']), $agent['level']);
  70. }
  71. // 此处需要判断云库存用户是否开启了平级奖限制
  72. if($reward['agent']['level'] == $agent['level'] && empty($reward['agent']['is_equal_award_restriction'])){
  73. $equal_desc = "补货平级奖发放,平级层数".$reward['level'];
  74. $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $reward['agent']['level'], 'status' => StatusEnum::ENABLED]);
  75. $this->sendEqualReward($reward['agent'], $stock_level, $reward['level'], $equal_desc, $this->order['num']);
  76. }
  77. }
  78. }
  79. }else{
  80. //发放平一级对象,直推
  81. if (!empty($this->user['parent_id'])) {
  82. $direct_agent = CloudStockAgent::findOne(['user_id' => $this->user['parent_id'], 'mall_id' => $this->mall_id,'status'=>StatusEnum::ENABLED]);
  83. //获取平一级代理商数据
  84. if ($direct_agent && empty($direct_agent['is_equal_award_restriction'])) {
  85. $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $direct_agent['level'], 'status' => StatusEnum::ENABLED]);
  86. //发放平一级奖励
  87. $over_desc = "补货越级奖发放,越级层数1";
  88. $equal_desc = "补货平级奖发放,平级层数1";
  89. $this->equalOne($agent, $direct_agent, $stock_level, $over_desc, $equal_desc, $this->order['num'], $setting);
  90. }
  91. //发放平二级用户,间推
  92. if (!empty($this->user['second_parent_id'])) {
  93. //获取平二级代理商数据
  94. $second_parent_agent = CloudStockAgent::findOne(['user_id' => $this->user['second_parent_id'],'status'=>StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  95. if($second_parent_agent){
  96. switch (true) {
  97. case $agent['level'] > $second_parent_agent['level']:
  98. $over_level = 2;
  99. $over_desc = "补货越级奖发放,越级层数" . $over_level;
  100. $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $second_parent_agent['level'], 'status' => StatusEnum::ENABLED]);
  101. $this->sendOverReward($second_parent_agent, $stock_level, $over_level, $over_desc, $this->order['num'], !empty($setting['is_by_buy_agent_level']), $agent['level']);
  102. break;
  103. case $agent['level'] == $second_parent_agent['level']:
  104. if (empty($second_parent_agent['is_equal_award_restriction'])) {
  105. $equal_level = 2;
  106. $equal_desc = "补货平级奖发放,平级层数" . $equal_level;
  107. $stock_level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $second_parent_agent['level'], 'status' => StatusEnum::ENABLED]);
  108. $this->sendEqualReward($second_parent_agent, $stock_level, $equal_level, $equal_desc, $this->order['num']);
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. //补货奖励
  117. $level = CloudStockLevel::findOne(['mall_id' => $this->mall_id, 'level' => $agent->level, 'status' => StatusEnum::ENABLED]);
  118. if (!empty($level['is_fill'])) {
  119. //补货奖发放
  120. $stock_goods = CloudStockGoods::findOne(['mall_id' => $this->mall_id, 'goods_id' => $this->goods_id]);
  121. $fill_reward_logic = new FillRewardLogic($agent, $stock_goods, 0, $this->order['id'], $this->order['num'], 0,
  122. CloudStockEnum::CHANGE_TYPE_INCREASE_FILL_EXTRA_BONUS,
  123. '补货奖发放',
  124. 0
  125. );
  126. $reward_unit_price = $fill_reward_logic->addRewardLog();
  127. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  128. if(!empty($configs['fill_bonus'])){
  129. $this->unit_goods_price = $this->unit_goods_price-$reward_unit_price;
  130. if($this->unit_goods_price<0) $this->unit_goods_price = 0;
  131. }
  132. }
  133. return $this->unit_goods_price;
  134. }
  135. /**
  136. * 货款单价
  137. * @return float|int
  138. */
  139. public function getOrderGoodsUnitPrice()
  140. {
  141. return $this->order->pay_money / $this->order->num;
  142. }
  143. }