HigoMchMarketing.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * 嗨值,嗨贝抵扣计算
  4. */
  5. namespace addons\HiGo\common\logic\order;
  6. use addons\HiGo\common\enums\HiGoEnum;
  7. use addons\HiGo\common\models\HigoGoodsSetting;
  8. use addons\HiGo\common\models\HigoWallet;
  9. use common\enums\AddonsEnum;
  10. use common\enums\AppEnum;
  11. use common\enums\StatusEnum;
  12. use common\traits\ErrorTrait;
  13. class HigoMchMarketing
  14. {
  15. use ErrorTrait;
  16. public static function compute($item, &$form, array &$extra = [])
  17. {
  18. try {
  19. $mall_id = $item['mch_order_info']['mall_id'];
  20. $user_id = $item['mch_order_info']['user_id'];
  21. $text_arr = HiGoEnum::getHigoWalletText($mall_id); // 中文名称
  22. $deduct_money = 0; // 抵扣总额
  23. $deduct_arr = []; // 抵扣冻结跟金额数组
  24. // 商品处理
  25. $higo_wallet = HigoWallet::findOne(['user_id' => $user_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  26. foreach ($item['goods'] as $goods_id => &$goods) {
  27. $mch_id = 0;
  28. foreach ($goods['goods_details'] as $k => $detail) {
  29. if ($mch_id == 0 && $detail['mch_id'] > 0) {
  30. $mch_id = $detail['mch_id'];
  31. }
  32. if ($mch_id > 0) {
  33. break;
  34. }
  35. }
  36. // 查询该商品是否设置了抵扣
  37. $goods_setting = HigoGoodsSetting::find()->where([
  38. 'goods_id' => $goods_id, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED, 'source' => AddonsEnum::ADDONS_NAME_MCH, 'source_id' => $mch_id
  39. ])->asArray()->all();
  40. if (empty($goods_setting)) continue;
  41. // 单件抵扣:比如单件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额30元
  42. // 多件抵扣:比如多件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额10元
  43. foreach ($goods_setting as $value) {
  44. $total_goods_price = array_sum(array_column($goods['goods_details'], 'goods_price')); //同一种商品不同规格总价格
  45. $total_num = array_sum(array_column($goods['goods_details'], 'num')); // 同一种商品不同规格总数量
  46. $money = $value['deduct_type'] == 2 ? $value['deduct_money'] : $value['deduct_money'] * $total_num; // 计算抵扣金额
  47. if ($higo_wallet[$value['wallet_type']] < $money) {
  48. $money = $higo_wallet[$value['wallet_type']];
  49. }
  50. if ($money <= 0) continue;
  51. $cn = $text_arr[$value['wallet_type']]; // 中文名称
  52. $en = $value['wallet_type']; // 英文标识
  53. // 检查是否有被选中
  54. $temp_total_deduct_money = 0;
  55. $is_use = false;
  56. if ($total_goods_price <= 0) continue;
  57. if (!empty($form->data['is_use_' . $en])) $is_use = true;
  58. foreach ($goods['goods_details'] as $k => &$detail) {
  59. $rate = $detail['goods_price'] / $total_goods_price;
  60. if (!isset($goods['goods_details'][$k + 1])) {
  61. //最后一个
  62. $temp_deduct_money = $money - $temp_total_deduct_money;
  63. } else {
  64. $temp_deduct_money = intval(($rate * $money) * 100) / 100;
  65. }
  66. $temp_deduct_money = intval($temp_deduct_money * 100) / 100;
  67. if ($detail['goods_price'] > $temp_deduct_money) {
  68. $is_use && $detail['goods_price'] -= $temp_deduct_money;
  69. } else {
  70. $temp_deduct_money = $detail['goods_price'];
  71. $is_use && $detail['goods_price'] = 0;
  72. }
  73. $temp_total_deduct_money += $temp_deduct_money;
  74. if ($is_use) {
  75. $deduct_currency = $detail['marketing']['deduct_currency'];
  76. if ($deduct_currency) {
  77. $deduct_currency = json_decode($deduct_currency, true);
  78. $deduct_currency = (array_merge($deduct_currency, [$en => ['name' => $text_arr[$en], 'money' => $temp_deduct_money]])); // 将合并的数组转为json字符串
  79. } else {
  80. $deduct_currency = [$en => ['name' => $text_arr[$en], 'money' => $temp_deduct_money]];
  81. }
  82. $detail['marketing']['deduct_currency'] = json_encode($deduct_currency); // 插入order_marketing表数据
  83. }
  84. }
  85. if ($is_use) {
  86. $item['goods'][$goods_id]['goods_price'] -= $temp_total_deduct_money; // 此商品减扣后的金额计算
  87. if ($item['goods'][$goods_id]['goods_price'] < 0) $item['goods'][$goods_id]['goods_price'] = 0;
  88. $deduct_arr[$en] = empty($deduct_arr[$en]) ? $temp_total_deduct_money : $deduct_arr[$en] + $temp_total_deduct_money;
  89. }
  90. // 前端抵扣数据展示组合
  91. if ($form->action == AppEnum::ACTION_PREVIEW) {
  92. $form->marketing['deduct'][$en] = [
  93. 'name' => $cn,
  94. 'deduct_val' => empty($form->marketing['deduct'][$en]['deduct_val']) ? intval($temp_total_deduct_money * 100) / 100 : intval(($form->marketing['deduct'][$en]['deduct_val'] + $temp_total_deduct_money) * 100) / 100,
  95. 'money' => empty($form->marketing['deduct'][$en]['money']) ? intval($temp_total_deduct_money * 100) / 100 : intval(($form->marketing['deduct'][$en]['money'] + $temp_total_deduct_money) * 100) / 100,
  96. 'is_show_radio' => true, // 是否显示单选按钮
  97. 'is_use' => $is_use, // 是否被选中单选按钮
  98. ];
  99. }
  100. $is_use && $deduct_money += $temp_total_deduct_money;
  101. }
  102. }
  103. // 计算订单减扣后的金额
  104. if ($deduct_money > 0) {
  105. $item['mch_order_info']['pay_money'] -= $deduct_money;
  106. $item['mch_order_info']['goods_price'] -= $deduct_money;
  107. // 扣减红包金额
  108. if ($form->action == AppEnum::ACTION_CREATE && $deduct_arr) {
  109. $from_type_arr = [
  110. HiGoEnum::WALLET_TYPE_HI_VALUE => HiGoEnum::FROM_TYPE_GOODS_HI_VALUE_DEDUTION,
  111. HiGoEnum::WALLET_TYPE_HI_BEI => HiGoEnum::FROM_TYPE_GOODS_HI_BEI_DEDUTION
  112. ];
  113. foreach ($deduct_arr as $key => $deduct_redpack_money) {
  114. $res = HigoWallet::setData([
  115. 'mall_id' => $mall_id,
  116. 'user_id' => $user_id,
  117. 'wallet_type' => $key,
  118. 'money' => -$deduct_redpack_money,
  119. 'from_type' => $from_type_arr[$key],
  120. ], false);
  121. if ($res == false) throw new \Exception(HigoWallet::getStaticError());
  122. }
  123. }
  124. }
  125. return $item;
  126. } catch (\Exception $e) {
  127. self::setStaticError($e->getMessage());
  128. return false;
  129. }
  130. }
  131. }