$mall_id, // 'date' => $date, // 'revenue' => $revenue // ]); // if ($res === false) throw new Exception('营业额生成失败' . AddonsBossRevenueLog::getStaticError()); // 判断用户插件是否安装 if (MallAddons::isInstall($mall_id, BossEnum::ADDONS_NAME) == 0) return true; // 获取基础配置 $config = Yii::$app->services->setting->addons->get($mall_id, 'Boss', 'basic'); // 判断是否开启插件分润 if ($config['is_enable'] == StatusEnum::DISABLED) return true; // 判断结算方式,0=支付完成,1=订单完成 // echo "订单状态" . $order['order_status']; // $settlement_method = $config['settlement_method'] == 0 ? 1 : 4; // if ($order['order_status'] != $settlement_method) return true; // 获取股东等级列表 $level_arr = AddonsBossLevelSetting::getBossLevelSetting([['mall_id' => $mall_id]], [], true); if (empty($level_arr)) return true; $level_arr = array_column($level_arr, null, 'level'); // 查询关系链上的股东ID $user_id = $order['user_id']; $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id); if (empty($parent_id_arr)) return true; $parent_id_arr = array_reverse($parent_id_arr); $where = ['user_id' => $parent_id_arr, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]; $addons_boss_list = AddonsBoss::find()->where($where)->indexBy('user_id')->all(); //查询股东列表 $boss_list = []; foreach ($parent_id_arr as $parent_id) { if (!empty($addons_boss_list[$parent_id])) $boss_list[] = $addons_boss_list[$parent_id]; //按照关系排序 } if (empty($boss_list)) return true; // 计算团队业绩 $performance = 0; // 业绩 $revenue = 0;//按照配置重新计算营业额 $order_detail_ids = []; foreach ($order['detail'] as $detail) { // 获取商品的股东分红独立配置 $goods_setting = AddonsBossCommissionItemSetting::getOne([['mall_id' => $mall_id], ['item_id' => $detail['goods_id']]], true, ['detail']); // 商品没参与股东分红则直接退出 if (!empty($goods_setting) && $goods_setting['is_enable'] == 0) continue; $goods_setting = AddonsBossCommissionItemSetting::getOne([['mall_id' => $mall_id], ['item_id' => $detail['goods_id']], ['commission_type' => 'commission']], true, ['detail']); if ($config['compute_type'] == 0) $order_money = $detail['goods_price']; // 支付金额 if ($config['compute_type'] == 1) $order_money = $detail['goods_price'] - bcmul($detail['cost_price'], $detail['num'], 2) + $detail['adjust_money']; // 商品利润 if ($config['compute_type'] == 2) $order_money = bcmul($detail['price'], $detail['num'], 2); // 商品售价 if ($goods_setting['is_extra_scale_ratio']) { //开启了额外计算比例 $performance += bcmul($order_money, $goods_setting['extra_scale_ratio'] / 100, 2); } else { $performance += $order_money; } $order_detail_ids[] = $detail['id']; } $trans = Yii::$app->db->beginTransaction(); try { // 生成营业额数据,按天 $res = AddonsBossRevenueLog::setData([ 'mall_id' => $mall_id, 'date' => $date, 'revenue' => $performance ]); if ($res === false) throw new Exception('营业额生成失败' . AddonsBossRevenueLog::getStaticError()); // 剔除不计算业绩的股东 $current_level = 0; foreach ($boss_list as $key => $boss) { $boss->performance += $performance; if ($boss['level'] <= $current_level) break; $current_level = $boss['level']; // 业绩日志 $rows[] = [ 'mall_id' => $boss->mall_id, 'order_id' => $order['id'], 'order_no' => $order['order_no'], 'user_id' => $boss->user_id, 'level' => $boss->level, // 股东等级 'performance' => $performance, // 股东业绩 'order_detail_ids' => json_encode($order_detail_ids), 'date' => $date, 'created_at' => $time, 'updated_at' => $time, ]; $res = $boss->save(); if ($res === false) throw new Exception('修改股东团队业绩失败' . $boss->getErrorMessage()); } // 保存数据 if (!empty($rows)) { // 生成业绩数据 $field = ['mall_id', 'order_id', 'order_no', 'user_id', 'level', 'performance', 'order_detail_ids', 'date', 'created_at', 'updated_at']; Yii::$app->db->createCommand()->batchInsert(AddonsBossPerformanceLog::tableName(), $field, $rows)->execute(); } $trans->commit(); return true; } catch (Exception $e) { // echo FormatHelper::exception($e, '保存数据出错'); $trans->rollBack(); $this->setError($e->getMessage()); return false; } } catch (Exception $e) { // echo FormatHelper::exception($e, '订单触发股东业绩计算错误'); $exception = FormatHelper::exception($e, '订单触发股东业绩计算错误'); $this->setError($exception); return false; } } /** * 发放业绩奖励 * @return void */ public function sendPerformancePrize($mall_id, $config, $start_day, $end_day, $job_id = 0) { if ($config['is_enable'] == StatusEnum::DISABLED) { Yii::error('系统没有开启股东提成'); return true; } // 获取总营业额 $revenue = AddonsBossRevenueLog::find()->select('sum(revenue) as total_revenue') ->where(['mall_id' => $mall_id, 'is_settlement' => StatusEnum::DISABLED]) ->andFilterWhere(['between', 'date', $start_day, $end_day]) ->asArray()->one(); if (empty($revenue)) return true; $total_revenue = $revenue['total_revenue']; // 获取股东等级列表 $level_setting = AddonsBossLevelSetting::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->indexBy('level')->asArray()->all(); $performance_list = AddonsBossPerformanceLog::find()->select('user_id,level,sum(performance) as total_performance') ->where(['mall_id' => $mall_id, 'is_settlement' => StatusEnum::DISABLED]) ->andFilterWhere(['between', 'date', $start_day, $end_day]) ->groupBy('user_id,level') ->asArray()->all(); if (empty($performance_list)) return true; // 按等级重新组装用户数据 $level_user_performance_list = []; foreach ($performance_list as $performance) { if (!isset($level_user_performance_list[$performance['level']])) $level_user_performance_list[$performance['level']] = []; if (!isset($level_user_performance_list[$performance['level']]['level_performance'])) $level_user_performance_list[$performance['level']]['level_performance'] = 0; $level_user_performance_list[$performance['level']]['user_performance_data'][] = $performance; $level_user_performance_list[$performance['level']]['level_performance'] += $performance['total_performance']; } $commission_logs = []; //佣金日志 $wallet_log = []; //钱包日志 $now = time(); $table_name = 'addons_boss_commission_log'; // 计算佣金,总营业额 * 股东分红业绩奖励比例 * (用户业绩/同级别用户总业绩) foreach ($level_user_performance_list as $level => $level_user_performance) { // var_dump($level_setting[$level]['is_performance']);die; // 是否开启股东业绩奖励 $is_performance = $level_setting[$level]['is_performance'] ?? 0; if ($is_performance) { $performance_rate = $level_setting[$level]['performance_rate'] ?? 0; // 设置了分红比例,进行用户佣金计算 if ($performance_rate > 0) { // 可分红金额 $share_money = $total_revenue * $performance_rate / 100; // 同等级总业绩 $level_performance = $level_user_performance['level_performance']; // 用户分佣 foreach ($level_user_performance['user_performance_data'] as $user_performance) { $user_total_proformance = $user_performance['total_performance']; $share_rate = bcdiv($user_total_proformance, $level_performance, 2); $commission = bcmul($share_money, $share_rate, 2); $commission_logs[] = [ 'mall_id' => $mall_id, 'user_id' => $user_performance['user_id'], 'amounts' => $user_total_proformance, 'commission' => $commission, 'revenue' => $total_revenue, 'sedimen_money' => 0, 'type' => LevelEnum::PERFORMANCE_PRIZE, 'compute_period' => $config['compute_period'], 'start_time' => $start_day, 'end_time' => $end_day, 'created_at' => $now, 'updated_at' => $now, ]; } } } } $trans = Yii::$app->db->beginTransaction(); try { // 生成数据 $field = ['mall_id', 'user_id', 'amounts', 'commission', 'revenue', 'sedimen_money', 'type', 'compute_period', 'start_time', 'end_time', 'created_at', 'updated_at']; $res = Yii::$app->db->createCommand()->batchInsert(AddonsBossCommissionLog::tableName(), $field, $commission_logs)->execute(); if (!$res) throw new Exception('批量生成分红数据失败'); // 修改业绩表结算状态 AddonsBossPerformanceLog::updateAll( ['is_settlement' => StatusEnum::ENABLED, 'updated_at' => $now], ['and', ['mall_id' => $mall_id, 'is_settlement' => StatusEnum::DISABLED], ['between', 'date', $start_day, $end_day]] ); // 营业额结算状态修改 AddonsBossRevenueLog::updateAll( ['is_settlement' => StatusEnum::ENABLED, 'settlement_time' => $now, 'updated_at' => $now], ['and', ['mall_id' => $mall_id, 'is_settlement' => StatusEnum::DISABLED], ['between', 'date', $start_day, $end_day]] ); $k = 1; $page_log = 1000; // 增加对应股东分佣金额 while ($log_list = AddonsBossCommissionLog::find()->where(['mall_id' => $mall_id, 'start_time' => $start_day, 'end_time' => $end_day,'type'=>LevelEnum::PERFORMANCE_PRIZE]) ->offset(($k - 1) * $page_log)->limit($page_log)->asArray()->all() ) { foreach ($log_list as $item) { $wallet_log[] = [ 'mall_id' => $mall_id, 'user_id' => $item['user_id'], 'is_frozen' => false, 'wallet_type' => 'commission', 'money' => $item['commission'], 'desc' => '股东分红业绩佣金', 'source_table' => $table_name, 'source_table_id' => $item['id'], 'from_type' => BossEnum::ADDONS_NAME, 'capital_type' => UserWalletEnum::BOSS_PERFORMANCE, ]; if (!isset($boss_commission[$item['user_id']])) $boss_commission[$item['user_id']] = []; if (!isset($boss_commission[$item['user_id']]['performance_commission'])) $boss_commission[$item['user_id']]['performance_commission'] = 0; $boss_commission[$item['user_id']]['performance_commission'] += $item['commission']; } $k++; } // 增加用户股东金额 foreach ($boss_commission as $user_id => $commission) { $boss = AddonsBoss::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]); if (!empty($boss)) { $boss->performance_commission += $commission['performance_commission']; $boss->total_commission = $boss->commission + $boss->extra_commission + $boss->performance_commission; if (!$boss->save()) throw new \Exception($boss->getErrorMessage()); } } $trans->commit(); } catch (Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "mall_id={$mall_id} 股东定时分红任务失败"); // var_dump($e->getMessage(),$e->getLine());die; Yii::error($exception); $this->setError($exception); return false; } // 写入用户钱包 if ($wallet_log) { UserWalletService::batchHandleByQueue($wallet_log); } } }