HigoMarketing.php 7.2 KB

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