| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * 嗨值,嗨贝抵扣计算
- */
- namespace addons\HiGo\common\logic\order;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\models\HigoGoodsSetting;
- use addons\HiGo\common\models\HigoWallet;
- use common\enums\AddonsEnum;
- use common\enums\AppEnum;
- use common\enums\StatusEnum;
- use common\traits\ErrorTrait;
- class HigoMchMarketing
- {
- use ErrorTrait;
- public static function compute($item, &$form, array &$extra = [])
- {
- try {
-
- $mall_id = $item['mch_order_info']['mall_id'];
- $user_id = $item['mch_order_info']['user_id'];
- $text_arr = HiGoEnum::getHigoWalletText($mall_id); // 中文名称
- $deduct_money = 0; // 抵扣总额
- $deduct_arr = []; // 抵扣冻结跟金额数组
- // 商品处理
- $higo_wallet = HigoWallet::findOne(['user_id' => $user_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- foreach ($item['goods'] as $goods_id => &$goods) {
- $mch_id = 0;
- foreach ($goods['goods_details'] as $k => $detail) {
- if ($mch_id == 0 && $detail['mch_id'] > 0) {
- $mch_id = $detail['mch_id'];
- }
- if ($mch_id > 0) {
- break;
- }
- }
- // 查询该商品是否设置了抵扣
- $goods_setting = HigoGoodsSetting::find()->where([
- 'goods_id' => $goods_id, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED, 'source' => AddonsEnum::ADDONS_NAME_MCH, 'source_id' => $mch_id
- ])->asArray()->all();
- if (empty($goods_setting)) continue;
- // 单件抵扣:比如单件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额30元
- // 多件抵扣:比如多件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额10元
- foreach ($goods_setting as $value) {
- $total_goods_price = array_sum(array_column($goods['goods_details'], 'goods_price')); //同一种商品不同规格总价格
- $total_num = array_sum(array_column($goods['goods_details'], 'num')); // 同一种商品不同规格总数量
- $money = $value['deduct_type'] == 2 ? $value['deduct_money'] : $value['deduct_money'] * $total_num; // 计算抵扣金额
- if ($higo_wallet[$value['wallet_type']] < $money) {
- $money = $higo_wallet[$value['wallet_type']];
- }
- if ($money <= 0) continue;
- $cn = $text_arr[$value['wallet_type']]; // 中文名称
- $en = $value['wallet_type']; // 英文标识
- // 检查是否有被选中
- $temp_total_deduct_money = 0;
- $is_use = false;
- if ($total_goods_price <= 0) continue;
- if (!empty($form->data['is_use_' . $en])) $is_use = true;
- foreach ($goods['goods_details'] as $k => &$detail) {
- $rate = $detail['goods_price'] / $total_goods_price;
- if (!isset($goods['goods_details'][$k + 1])) {
- //最后一个
- $temp_deduct_money = $money - $temp_total_deduct_money;
- } else {
- $temp_deduct_money = intval(($rate * $money) * 100) / 100;
- }
- $temp_deduct_money = intval($temp_deduct_money * 100) / 100;
- if ($detail['goods_price'] > $temp_deduct_money) {
- $is_use && $detail['goods_price'] -= $temp_deduct_money;
- } else {
- $temp_deduct_money = $detail['goods_price'];
- $is_use && $detail['goods_price'] = 0;
- }
- $temp_total_deduct_money += $temp_deduct_money;
- if ($is_use) {
- $deduct_currency = $detail['marketing']['deduct_currency'];
- if ($deduct_currency) {
- $deduct_currency = json_decode($deduct_currency, true);
- $deduct_currency = (array_merge($deduct_currency, [$en => ['name' => $text_arr[$en], 'money' => $temp_deduct_money]])); // 将合并的数组转为json字符串
- } else {
- $deduct_currency = [$en => ['name' => $text_arr[$en], 'money' => $temp_deduct_money]];
- }
- $detail['marketing']['deduct_currency'] = json_encode($deduct_currency); // 插入order_marketing表数据
- }
- }
- if ($is_use) {
- $item['goods'][$goods_id]['goods_price'] -= $temp_total_deduct_money; // 此商品减扣后的金额计算
- if ($item['goods'][$goods_id]['goods_price'] < 0) $item['goods'][$goods_id]['goods_price'] = 0;
- $deduct_arr[$en] = empty($deduct_arr[$en]) ? $temp_total_deduct_money : $deduct_arr[$en] + $temp_total_deduct_money;
- }
- // 前端抵扣数据展示组合
- if ($form->action == AppEnum::ACTION_PREVIEW) {
- $form->marketing['deduct'][$en] = [
- 'name' => $cn,
- '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,
- '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,
- 'is_show_radio' => true, // 是否显示单选按钮
- 'is_use' => $is_use, // 是否被选中单选按钮
- ];
- }
- $is_use && $deduct_money += $temp_total_deduct_money;
- }
- }
- // 计算订单减扣后的金额
- if ($deduct_money > 0) {
- $item['mch_order_info']['pay_money'] -= $deduct_money;
- $item['mch_order_info']['goods_price'] -= $deduct_money;
- // 扣减红包金额
- if ($form->action == AppEnum::ACTION_CREATE && $deduct_arr) {
- $from_type_arr = [
- HiGoEnum::WALLET_TYPE_HI_VALUE => HiGoEnum::FROM_TYPE_GOODS_HI_VALUE_DEDUTION,
- HiGoEnum::WALLET_TYPE_HI_BEI => HiGoEnum::FROM_TYPE_GOODS_HI_BEI_DEDUTION
- ];
- foreach ($deduct_arr as $key => $deduct_redpack_money) {
- $res = HigoWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'wallet_type' => $key,
- 'money' => -$deduct_redpack_money,
- 'from_type' => $from_type_arr[$key],
- ], false);
- if ($res == false) throw new \Exception(HigoWallet::getStaticError());
- }
- }
- }
- return $item;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|