db->beginTransaction(); try { $time_type = $redpack['time_type'];// 活动类型[1=每日红包,2=时间段红包] $date = date('ymd', time()); // 活动-每日抢红包,每天都有一次无需邀请人抢红包机会 // 活动-时间段抢红包,在这个时间段内,最多只有一次无需邀请人可抢红包机会 // 获取用户是否首次抢红包 if ($time_type == 1) { $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one(); } else { $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); } $award = $redpack->award;// 活动奖金池剩余金额 // 今日抢红包统计 $today_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one(); // 解析红包规则内容 $rule_values = Json::decode($redpack->rule_values, true); if (empty($is_first_grad)) { // 首次抢红包金额配置 $money = rand($rule_values['first_integral_start'] * 100, $rule_values['first_integral_end'] * 100); } else { // 非首次抢到红包,检查最后一次抢红包次数是否是第一次抢,不是则找到今日最后一次抢的统计内容 // 判断今日抢红包次数是否已经超出 if ($today_grad['grab_num_total'] >= $rule_values['times']) throw new \Exception('今日抢积分次数已用完'); // 判断可抢次数是否还足够 $user_grad_num = empty($today_grad['inviter_num']) ? 0 : floor($today_grad['inviter_num'] / $rule_values['everyday_invitation']);// 用户已抢红包次数 if ($user_grad_num - $today_grad['grab_num_total'] + 1 <= 0) throw new \Exception('今日抢积分次数不足'); $money = rand($rule_values['everyday_invitation_start'] * 100, $rule_values['everyday_invitation_end'] * 100); } // 将奖励金额单位改为元 $money = $money / 100; // 扣减奖金池 if ($redpack->integral_type != ScoreExpansionEnum::FREEZE_TO_ACTIVE) { // 判断可得金额是否大于奖金池剩余金额 if ($money >= $award) $money = $award; $redpack->award = $redpack->award - $money; } // 判断用户曾经是否抢过红包 $has_grab = ScoreExpansionUserGrab::find()->where(['mall_id' => $redpack->mall_id, 'rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); empty($has_grab) && $redpack->grab_user_num = $redpack->grab_user_num + 1; // 红包表数据统计汇总 $redpack->grab_integral = $redpack->grab_integral + $money; if (!$redpack->save()) throw new Exception($redpack->getErrorMessage()); // 增加用户红包金额 switch ($redpack->integral_type) { case ScoreExpansionEnum::FREEZE_TO_ACTIVE : $is_frozen = false; $desc = '抢冻结到激活积分'; // 将冻结红包的金额先减扣掉 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $redpack->mall_id, 'user_id' => $user_id, 'is_frozen' => true, 'is_deduct' => true, 'integral' => -$money, 'source_table' => ScoreExpansion::briefTableName(), 'source_table_id' => $redpack->id, 'from_type' => ScoreExpansionEnum::FROM_RECEIVE, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); $money = $wallet['money']; if ($money <= 0) throw new \Exception("暂无可激活的冻结积分"); break; case ScoreExpansionEnum::INCREMENT_FREEZE : $is_frozen = true; $desc = '抢冻结积分'; break; case ScoreExpansionEnum::INCREMENT_ACTIVE : $is_frozen = false; $desc = '抢激活积分'; break; } // 增加用户的红包余额 $res = ScoreExpansionWallet::setData([ 'mall_id' => $redpack->mall_id, 'user_id' => $user_id, 'is_frozen' => $is_frozen, 'integral' => $money, 'source_table' => ScoreExpansion::briefTableName(), 'source_table_id' => $redpack->id, 'from_type' => ScoreExpansionEnum::FROM_RECEIVE, 'desc' => $desc, ]); if ($res == false) throw new Exception(ScoreExpansionWallet::getStaticError()); // 增加抢红包次数 $count_day = [ 'mall_id' => $redpack->mall_id, 'rid' => $redpack->id, 'user_id' => $user_id, 'grab_num' => empty($today_grad['grab_num_total']) ? $rule_values['times'] - 1 : $rule_values['times'] - $today_grad['grab_num_total'] - 1, 'grab_num_total' => empty($today_grad['grab_num_total']) ? 1 : $today_grad['grab_num_total'] + 1, ]; !empty($today_grad['id']) && $count_day['id'] = $today_grad['id']; $res = ScoreExpansionUserCountDay::setData($count_day); if ($res == false) throw new Exception(ScoreExpansionUserCountDay::getStaticError()); // 增加抢红包数据 $res = ScoreExpansionUserGrab::setData([ 'mall_id' => $redpack->mall_id, 'rid' => $redpack->id, 'user_id' => $user_id, 'grab_money' => $money, ]); if ($res == false) throw new Exception(ScoreExpansionUserGrab::getStaticError()); $trans->commit(); return $money; } catch (\Exception $e) { self::setStaticError($e->getMessage()); $trans->rollBack(); return false; } } /** * 赠送积分 * @Author: lun * @DateTime: 2022/3/26 0026 14:51 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order_id * @param $order_status 1=支付完成,2=订单完成 * @param $order_type order=平台, StoreOrder=商户 * @return bool */ public static function giveRedpack($order_id, $order_status = 2, $order_type = "order") { try { // 查询订单 $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate,num"; $source_table = Order::tableName(); //判断来源 订单表 if ($order_type == 'order') { $orderData = Order::findOne(['id' => $order_id]); $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all(); $source_table = Order::tableName(); } elseif ($order_type == 'StoreOrder') { $orderData = StoreOrder::findOne(['order_no' => $order_id]); $order_id = $orderData->id; $orderData = StoreOrder::findOne(['id' => $order_id]); $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all(); $source_table = StoreOrder::tableName(); } if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空"); if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空"); $user_id = $orderData->user_id; $mall_id = $orderData->mall_id; //20230404 $user_data = User::findOne(['id' => $user_id]); //2024/09/13 liuzhantao 加上注释且优化条件 // “赠送积分方式” 配置说明: // 当配置为“1”:订单支付后,有两种条件 // 条件一. 订单确认收货且完成评价后,这笔订单赠送的冻结积分变为激活积分(释放所有积分) // 条件二. 订单确认收货但未完成评价,过n天后,释放所有积分 // 当配置为“2”:订单支付后将不能获得任何积分,要订单完成后才能获得 // 获取配置【1=支付完成,2=订单完成】 $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type'); if ($give_type == 2 && $order_status != 2) { // 当配置为“2”,需要订单完成才能有积分,所以就直接return,反之配置为“1”,继续执行下面的 return true; } //循环订单商品列表,没有单独设置则应用全局设置20230404 //获取全局设置 $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic'); $setting_data = []; if (!empty($config['buy_frozen_integral'])) { $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true); $config['buy_frozen_integral']['parent'] = 0; $setting_data[] = $config['buy_frozen_integral']; } if (!empty($config['buy_integral'])) { $config['buy_integral'] = json_decode($config['buy_integral'], true); $config['buy_integral']['parent'] = 0; $setting_data[] = $config['buy_integral']; } if (!empty($config['parent_frozen_integral'])) { $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true); $config['parent_frozen_integral']['parent'] = 1; $setting_data[] = $config['parent_frozen_integral']; } if (!empty($config['parent_integral'])) { $config['parent_integral'] = json_decode($config['parent_integral'], true); $config['parent_integral']['parent'] = 1; $setting_data[] = $config['parent_integral']; } foreach ($order_details as $goodsDetail) { //判断单独设置 $list = ''; if ($order_type == 'order') { $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all(); } if (!empty($list)) { foreach ($list as $item) { // 计算可赠送的红包金额 $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money']; $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2); if ($money <= 0) continue; //判断受益者20230404 $desc = '购物赠送'; if ($item['parent'] == 1) { $user_id = $user_data['parent_id']; $desc = '直推下级购物赠送'; if($user_id <= 0) continue; } // 订单完成 && 完成评价 && 符合条件一,把冻结的积分转换为激活积分 if ($order_status == 2 && $give_type == 1) { if ($orderData->is_comment != 1 || $config['change_activation_integral'] != 1) return true; // 没有评价 或 不符合条件一,就return if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) { // 增加激活积分 $frozenWallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => false, 'integral' => $money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //扣减冻结积分 $activationWallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => true, 'integral' => -$money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } else { // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); if ($give_type == 2 && $order_status == 2) { //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } } } else { foreach ($setting_data as $setting) { // 计算可赠送的红包金额 $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money']; $money = $setting['is_percent'] == 0 ? $setting['give'] * $goodsDetail['num'] : bcmul(($setting['give'] / 100), $pay_money, 2); if ($money <= 0) continue; //判断受益者20230404 $desc = '购物赠送'; if ($setting['parent'] == 1) { $user_id = $user_data['parent_id']; $desc = '直推下级购物赠送'; if($user_id <= 0) continue; } //订单完成并且完成评价时 把冻结的积分转换为激活积分 if ($order_status == 2 && $give_type == 1) { if ($orderData->is_comment != 1) return true; if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) { // 增加激活积分 $frozenWallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => false, 'integral' => $money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //扣减冻结积分 $activationWallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => true, 'integral' => -$money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } else { // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); if ($give_type == 2 && $order_status == 2) { //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } } } } //循环订单商品列表,没有单独设置则应用全局设置20230404 end // 获取设置了红包的商品 // $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goods_ids, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();true; // if (empty($list)) return true; // $orderData = Order::findOne(['id'=>$order_id]); // foreach ($list as $item) { // $is_evaluate = $order_details[$item['goods_id']]['is_evaluate']; // // 计算可赠送的红包金额 // $pay_money = $order_details[$item['goods_id']]['goods_price'] + $order_details[$item['goods_id']]['adjust_money']; // $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2); // if ($money <= 0) continue; // // //判断受益者20230404 // if($item['parent'] == 1) $user_id = $user_data['parent_id']; // // //订单完成并且完成评价时 把冻结的积分转换为激活积分 // if($order_status == 2 && $give_type == 1){ // if($orderData->is_comment != 1) return true; // if($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN){ // // 增加激活积分 //// $money = $item['give']; // $frozenWallet = ScoreExpansionWallet::setData([ // 'mall_id' => $item['mall_id'], // 'user_id' => $user_id, // 'is_frozen' => false, // 'integral' => $money, // 'source_table' => Order::tableName(), // 'source_table_id' => $order_id, // 'from_type' => ScoreExpansionEnum::FROM_GOODS, // 'desc' => '购物赠送', // ]); // if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); // //扣减冻结积分 // $activationWallet = ScoreExpansionWallet::setData([ // 'mall_id' => $item['mall_id'], // 'user_id' => $user_id, // 'is_frozen' => true, // 'integral' => -$money, // 'source_table' => Order::tableName(), // 'source_table_id' => $order_id, // 'from_type' => ScoreExpansionEnum::FROM_GOODS, // 'desc' => '购物赠送', // ]); // if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); // //修改定时任务状态 // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]); // $task->is_work = 1; // if(!$task->save()){ // throw new Exception(ScoreExpansionRateTask::getStaticError()); // } // } // }else{ // // 插入用户积分表 // $wallet = ScoreExpansionWallet::setData([ // 'mall_id' => $item['mall_id'], // 'user_id' => $user_id, // 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, // 'integral' => $money, // 'source_table' => Order::tableName(), // 'source_table_id' => $order_id, // 'from_type' => ScoreExpansionEnum::FROM_GOODS, // 'desc' => '购物赠送', // ]); // if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); // // if($give_type == 2 && $order_status == 2){ // //修改定时任务状态 // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]); // $task->is_work = 1; // if(!$task->save()){ // throw new Exception(ScoreExpansionRateTask::getStaticError()); // } // } // } // } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 红包返回 * @Author: lun * @DateTime: 2022/9/21 0021 9:54 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order_detail_id * @param array $params * @return bool */ public static function backRedpack($order_detail_id, $params = []) { $trans = Yii::$app->db->beginTransaction(); try { $deduct = OrderMarketing::find()->select('deduct_currency')->where(['order_detail_id' => $order_detail_id, 'status' => StatusEnum::ENABLED])->scalar(); if (empty($deduct)) { $trans->commit(); return true; } $deduct_arr = Json::decode($deduct, true); foreach ($deduct_arr as $key => $item) { // 判断里面是否存在激活积分跟冻结积分 if (in_array($key, [ScoreExpansionEnum::WALLET_INTEGRAL, ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN]) && $item['money'] > 0) { $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'is_frozen' => $key == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $item['money'], 'source_table' => $params['source_table'], 'source_table_id' => $params['source_table_id'], 'from_type' => $params['from_type'], 'desc' => $params['desc'] ?? '', 'no_icr_total' => true,// 不增加积分累计总额 ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); } } $trans->commit(); return true; } catch (\Exception $e) { $trans->rollBack(); self::setStaticError($e->getMessage()); return false; } } /** * 收银台赠送积分 * @Author: lun * @DateTime: 2022/3/26 0026 14:51 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order_id * @param $order_status 1=支付完成,2=订单完成 * @param $order_type order=平台, StoreOrder=商户 * @return bool */ public static function giveCashierRedpack($order_id, $order_status = 2) { try { // 查询订单 $source_table = StoreCashierOrder::tableName(); $orderData = StoreCashierOrder::findOne(['id' => $order_id]); if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空"); //判断来源 商户 门店 平台20230404 $user_id = $orderData->user_id; $mall_id = $orderData->mall_id; //20230404 $user_data = User::findOne(['id' => $user_id]); //循环订单商品列表,没有单独设置则应用全局设置20230404 //获取全局设置 $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic'); $setting_data = []; if (!empty($config['buy_frozen_integral'])) { $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true); $config['buy_frozen_integral']['parent'] = 0; $setting_data[] = $config['buy_frozen_integral']; } if (!empty($config['buy_integral'])) { $config['buy_integral'] = json_decode($config['buy_integral'], true); $config['buy_integral']['parent'] = 0; $setting_data[] = $config['buy_integral']; } if (!empty($config['parent_frozen_integral'])) { $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true); $config['parent_frozen_integral']['parent'] = 1; $setting_data[] = $config['parent_frozen_integral']; } if (!empty($config['parent_integral'])) { $config['parent_integral'] = json_decode($config['parent_integral'], true); $config['parent_integral']['parent'] = 1; $setting_data[] = $config['parent_integral']; } foreach ($setting_data as $setting) { // 计算可赠送的红包金额 $pay_money = $orderData['pay_money']; $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2); if ($money <= 0) continue; //判断受益者 $desc = '购物赠送'; if ($setting['parent'] == 1) { $user_id = $user_data['parent_id']; $desc = '直推下级购物赠送'; } // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $mall_id, 'user_id' => $user_id, 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); } //循环订单商品列表,没有单独设置则应用全局设置20230404 end return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 店铺赠送积分 * @param $order_id * @param $order_status * @param $order_type * @return bool */ public static function giveStoreRedpack($order_id, $order_status = 2, $order_type = 'order') { try { // 查询订单 $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate"; $source_table = Order::tableName(); //判断来源 订单表 if ($order_type == 'order') { $orderData = Order::findOne(['id' => $order_id]); $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all(); $source_table = Order::tableName(); } elseif ($order_type == 'StoreOrder') { $orderData = StoreOrder::findOne(['id' => $order_id]); $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all(); $source_table = StoreOrder::tableName(); } if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空"); if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空"); //查找店铺持有者的user_id if(empty($orderData->store_id)) throw new \Exception("订单id={$order_id}的店铺id为空"); $storeData = Store::findOne(['id'=>$orderData->store_id]); $user_id = $storeData->user_id; $mall_id = $storeData->mall_id; //20230404 $user_data = User::findOne(['id' => $user_id]); //当配置为1时 订单完成 将订单获得的冻结积分转变为激活积分 //当配置为2时 订单支付完成将不能获得任何积分 // 获取配置【1=支付完成,2=订单完成】 $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type'); if ($give_type == 2 && $order_status != 2) { return true; } //循环订单商品列表,没有单独设置则应用全局设置20230404 //获取全局设置 $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic'); $setting_data = []; //门店设置 if(!empty($config['store_frozen_integral'])){ $config['store_frozen_integral'] = json_decode($config['store_frozen_integral'],true); $config['store_frozen_integral']['parent'] = 0; $setting_data[] = $config['store_frozen_integral']; } if(!empty($config['store_integral'])){ $config['store_integral'] = json_decode($config['store_integral'],true); $config['store_integral']['parent'] = 0; $setting_data[] = $config['store_integral']; } if(!empty($config['parent_store_frozen_integral'])){ $config['parent_store_frozen_integral'] = json_decode($config['parent_store_frozen_integral'],true); $config['parent_store_frozen_integral']['parent'] = 1; $setting_data[] = $config['parent_store_frozen_integral']; } if(!empty($config['parent_store_integral'])){ $config['parent_store_integral'] = json_decode($config['parent_store_integral'],true); $config['parent_store_integral']['parent'] = 1; $setting_data[] = $config['parent_store_integral']; } foreach ($order_details as $goodsDetail) { //判断单独设置 $list = ''; if ($order_type == 'order') { $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all(); } if (!empty($list)) { foreach ($list as $item) { // 计算可赠送的红包金额 $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money']; $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2); if ($money <= 0) continue; //判断受益者20230404 $desc = '购物赠送'; if ($item['parent'] == 1) { $user_id = $user_data['parent_id']; $desc = '直推下级购物赠送'; } //订单完成并且完成评价时 把冻结的积分转换为激活积分 if ($order_status == 2 && $give_type == 1) { if ($orderData->is_comment != 1) return true; if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) { // 增加激活积分 $frozenWallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => false, 'integral' => $money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //扣减冻结积分 $activationWallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => true, 'integral' => -$money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } else { // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $user_id, 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $money, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); if ($give_type == 2 && $order_status == 2) { //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } } } else { foreach ($setting_data as $setting) { // 计算可赠送的红包金额 $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money']; $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2); if ($money <= 0) continue; //判断受益者20230404 $desc = '购物赠送'; if ($setting['parent'] == 1) { $user_id = $user_data['parent_id']; $desc = '直推下级购物赠送'; } //订单完成并且完成评价时 把冻结的积分转换为激活积分 if ($order_status == 2 && $give_type == 1) { if ($orderData->is_comment != 1) return true; if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) { // 增加激活积分 $frozenWallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => false, 'integral' => $money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //扣减冻结积分 $activationWallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => true, 'integral' => -$money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } else { // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $goodsDetail['mall_id'], 'user_id' => $user_id, 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false, 'integral' => $money, 'source_table' => $source_table, 'source_table_id' => $order_id, 'from_type' => ScoreExpansionEnum::FROM_GOODS, 'desc' => $desc, ]); if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError()); if ($give_type == 2 && $order_status == 2) { //修改定时任务状态 $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]); $task->is_work = 1; if (!$task->save()) { throw new Exception(ScoreExpansionRateTask::getStaticError()); } } } } } } //循环订单商品列表,没有单独设置则应用全局设置20230404 end return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } }