sharing_name; $user_name = $this->user_name; $store_name = $this->store_name; $mobile = $this->mobile; $start_time = $this->start_time; $end_time = $this->end_time; $sharing_type = $this->sharing_type; $sharing_way = $this->sharing_way; $query = SharingReceiverCustom::find()->alias('src') ->leftJoin(['su' => SaasUser::tableName()], 'su.id = src.type_id AND src.sharing_type = ' . SharingReceiverCustom::SHARING_TYPE_USER) ->leftJoin(['su_' => SaasUser::tableName()], 'su.id = src.extra_saas_user_id AND src.sharing_type = ' . SharingReceiverCustom::SHARING_TYPE_MERCHANT) ->leftJoin(['s' => Store::tableName()], 's.id = src.sharing_store_id AND src.sharing_way = ' . SharingReceiverCustom::SHARING_WAY_STORE) ->where(['src.is_delete' => SharingReceiverCustom::IS_DELETE_NO]); if (isset($sharing_name) && !empty($sharing_name)) { $query->andWhere(['LIKE', 'src.name', $sharing_name]); } if (isset($user_name) && !empty($user_name)) { $query->andWhere(['OR', ['LIKE', 'su.name', $user_name], ['LIKE', 'su_.name', $user_name], ['LIKE', 'src.type_id', $user_name]]); } if (isset($store_name) && !empty($store_name)) { $query->andWhere(['LIKE', 's.name', $store_name]); } if (isset($mobile) && !empty($mobile)) { $query->andWhere(['OR', ['LIKE', 'su.mobile', $mobile], ['LIKE', 'su_.mobile', $mobile]]); } if (isset($start_time) && !empty($start_time)) { $query->andWhere(['>=', 'src.created_at', strtotime($start_time)]); } if (isset($end_time) && !empty($end_time)) { $query->andWhere(['<=', 'src.created_at', strtotime($end_time)]); } if (isset($sharing_type) && in_array($sharing_type, SharingReceiverCustom::SHARING_TYPE)) { $query->andWhere(['src.sharing_type' => $sharing_type]); } if (isset($sharing_way) && in_array($sharing_way, SharingReceiverCustom::SHARING_WAY)) { $query->andWhere(['src.sharing_way' => $sharing_way]); } $query->select('src.id, src.name, src.sharing_type, src.type_id, src.sharing_way, s.name store_name, src.sharing_profit, src.amount, src.created_at, src.sharing_store_id, src.extra_saas_user_id') ->orderBy('src.id DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['sharing_type'] = intval($item['sharing_type']); $item['sharing_way'] = intval($item['sharing_way']); $item['store_name'] = $item['store_name'] ?: ''; $saasUser = null; if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_USER) { $saasUser = SaasUser::findOne($item['type_id']); } if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_MERCHANT) { $saasUser = SaasUser::findOne($item['extra_saas_user_id']); } $item['user_name'] = $saasUser['name'] ?: ''; $item['avatar'] = $saasUser['avatar'] ?: ''; $item['mobile'] = $saasUser['mobile'] ?: ''; } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function save() { try { $id = $this->id; $sharing_name = $this->sharing_name; $sharing_type_id = $this->sharing_type_id; $sharing_type = intval($this->sharing_type); $sharing_way = intval($this->sharing_way); $sharing_store_id = $this->sharing_store_id; $sharing_profit = $this->sharing_profit; $extra_saas_user_id = $this->extra_saas_user_id; if (!in_array($sharing_type, SharingReceiverCustom::SHARING_TYPE)) { throw new \Exception('分账类型枚举值错误'); } if (!in_array($sharing_way, SharingReceiverCustom::SHARING_WAY)) { throw new \Exception('分账方式枚举值错误'); } if ($sharing_type === SharingReceiverCustom::SHARING_TYPE_USER) { $saasUser = SaasUser::findOne(['id' => $sharing_type_id, 'is_delete' => 0]); if (!$saasUser) { throw new \Exception('用户不存在'); } } else { $saasUser = SaasUser::findOne(['id' => $extra_saas_user_id, 'is_delete' => 0]); if (!$saasUser) { throw new \Exception('商户绑定用户不存在'); } } if ($sharing_way == SharingReceiverCustom::SHARING_WAY_STORE) { $store_id = $sharing_store_id; $store = Store::findOne($store_id); if (bcadd(union_share_scale($store, false, $id), $sharing_profit, 2) > 100) { throw new \Exception('保存失败,分销比例超过100%'); } } $sharingReceiverCustom = SharingReceiverCustom::findOne(['id' => $id, 'is_delete' => 0]); if (!$sharingReceiverCustom) { $is_edit = SharingReceiverCustom::find()->where([ 'sharing_type' => $sharing_type, 'type_id' => $sharing_type_id, 'sharing_way' => $sharing_way, 'sharing_store_id' => $sharing_store_id, 'is_delete' => 0 ])->asArray()->one(); $sharingReceiverCustom = SharingReceiverCustom::findOne($is_edit['id']); if (!$sharingReceiverCustom) { $sharingReceiverCustom = new SharingReceiverCustom(); } } $sharingReceiverCustom->name = $sharing_name; $sharingReceiverCustom->sharing_type = $sharing_type; $sharingReceiverCustom->type_id = $sharing_type_id; $sharingReceiverCustom->sharing_way = $sharing_way; $sharingReceiverCustom->sharing_store_id = $sharing_store_id; $sharingReceiverCustom->sharing_profit = $sharing_profit; $sharingReceiverCustom->extra_saas_user_id = $extra_saas_user_id; if (!$sharingReceiverCustom->save()) { throw new \Exception(implode(';', array_values($sharingReceiverCustom->firstErrors))); }; return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function del() { try { $ids = $this->ids; $ids = explode(',', $ids ?: ''); if (!$ids) { throw new \Exception('请选择要删除的数据'); } foreach ($ids as $id) { $sharingReceiverCustom = SharingReceiverCustom::findOne($id); if (!$sharingReceiverCustom) { throw new \Exception('数据不存在'); } $sharingReceiverCustom->is_delete = SharingReceiverCustom::IS_DELETE_YES; if (!$sharingReceiverCustom->save()) { throw new \Exception(implode(';', array_values($sharingReceiverCustom->firstErrors))); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //让利分配明细 public function profitList() { try { $store_name = $this->store_name; $store_category_id = $this->store_category_id; $query = Store::find()->where(['is_delete' => 0]); if ($store_name) { $query->andWhere(['like', 'name', $store_name]); } if ($store_category_id) { $query->andWhere(['category_id' => $store_category_id]); } $query->orderBy('id DESC')->select('id, name, logo, category_id, self_rebate_value'); $list = pagination_make($query); foreach ($list['list'] as &$item) { //分类名称 $saasCategory = SaasCategory::findOne(['is_delete' => 0, 'id' => $item['category_id']]); $item['category_name'] = $saasCategory->name ?: ''; //省级佣金 $item['provider_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_AREA_AGENT_PROVIDER ])->sum('amount') ?: '0.00'; //市级佣金 $item['city_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_AREA_AGENT_CITY ])->sum('amount') ?: '0.00'; //区级佣金 $item['district_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_AREA_AGENT_DISTRICT ])->sum('amount') ?: '0.00'; //推广代理佣金 $item['bd_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_BD_AGENT ])->sum('amount') ?: '0.00'; //店铺分销佣金 $item['store_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_STORE_TO_STORE ])->sum('amount') ?: '0.00'; //分销一级佣金 $item['first_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_PLATFORM ])->andWhere(['LIKE', 'remark', '一级分销'])->sum('amount') ?: '0.00'; //分销二级佣金 $item['second_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_PLATFORM ])->andWhere(['LIKE', 'remark', '二级分销'])->sum('amount') ?: '0.00'; //分销三级佣金 $item['third_profit'] = SharingReceiver::find()->alias('sr') ->where([ 'sr.store_id' => $item['id'], 'sr.from' => SharingReceiver::FROM_PLATFORM ])->andWhere(['LIKE', 'remark', '三级分销'])->sum('amount') ?: '0.00'; //灵活分润比例 $item['custom_profit'] = SharingReceiverCustom::find()->where([ 'is_delete' => SharingReceiverCustom::IS_DELETE_NO ])->andWhere(['OR', [ 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE, 'sharing_store_id' => $item['id'], ], [ 'sharing_way' => SharingReceiverCustom::SHARING_WAY_PLATFORM, ]])->sum('sharing_profit') ?: '0.00'; } return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商城比例详情 public function storeProfitDetail() { try { $store_id = $this->id; $store = Store::findOne($store_id); //分类名称 $saasCategory = SaasCategory::findOne(['is_delete' => 0, 'id' => $store->category_id]); $admin = Admin::findOne([ 'type' => 'store', 'type_id' => $store_id ]); $admin_saas_user = SaasUser::findOne($store->store_admin); //显示比例汇总 $ratio = union_share_scale($store); $custom_profit = SharingReceiverCustom::find()->where([ 'is_delete' => SharingReceiverCustom::IS_DELETE_NO, 'sharing_way' => SharingReceiverCustom::SHARING_WAY_PLATFORM, ])->sum('sharing_profit') ?: 0; $ratio = bcadd($ratio, $custom_profit, 2); $agency_price_config = Option::get('agency_price_config', 0, 'saas')['value']; $agency_price_config = json_decode($agency_price_config, true) ?: []; $province_percent = $agency_price_config['province_percent'] ?: 0; $ratio = bcadd($ratio, $province_percent, 2); $city_percent = $agency_price_config['city_percent'] ?: 0; $ratio = bcadd($ratio, $city_percent, 2); $district_percent = $agency_price_config['district_percent'] ?: 0; $ratio = bcadd($ratio, $district_percent, 2); $bd_agency_price_config = Option::get('bd_agency_price_config', 0, 'saas')['value']; $bd_agency_price_config = json_decode($bd_agency_price_config, true) ?: []; $bd_agent_percent = $bd_agency_price_config['bd_agent_percent'] ?: 0; $ratio = bcadd($ratio, $bd_agent_percent, 2); $transfer_profit = $store->transfer_profit; $store_info = [ 'store_name' => $store->name, 'logo' => $store->logo, 'category_name' => $saasCategory->name, 'admin_user_name' => $admin_saas_user->name ?? '', 'contact_tel' => $store->contact_tel ?: ($admin_saas_user->mobile ?: ''), 'store_ratio' => ($ratio ?: '0.00') . '%', 'real_ratio' => (bcsub(100, $ratio ?: '0.00', 2)) . '%' ]; return [ 'code' => 0, 'msg' => '', 'data' => [ 'store_info' => $store_info, 'tab_list' => [ [ 'key' => 'provider_profit', 'value' => '省级比例', ], [ 'key' => 'city_profit', 'value' => '市级比例' ], [ 'key' => 'district_profit', 'value' => '区级比例' ], [ 'key' => 'bd_profit', 'value' => '推广代理比例' ], [ 'key' => 'store_profit', 'value' => '店铺分销比例' ], [ 'key' => 'first_profit', 'value' => '一级分销比例' ], [ 'key' => 'second_profit', 'value' => '二级分销比例' ], [ 'key' => 'third_profit', 'value' => '三级分销比例' ], [ 'key' => 'self_profit', 'value' => '消费返利比例' ], [ 'key' => 'platform_custom_profit', 'value' => '平台灵活分润比例' ], [ 'key' => 'store_custom_profit', 'value' => '店铺灵活分润比例' ], ] ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商城比例详情 public function storeProfitList() { try { $key = $this->key; $store_id = $this->id; $store = Store::findOne($store_id); // 联盟分销 $union_option = json_decode($store->share_setting, true); $list = []; $totalCount = 1; $pageNo = 1; $pageSize = 10; if (in_array($key, ['provider_profit', 'city_profit', 'district_profit'])) { $agency_price_config = Option::get('agency_price_config', 0, 'saas')['value']; $agency_price_config = json_decode($agency_price_config, true) ?: []; $percent = 0; $where = [ 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_DEFAULT ]; if ($key === 'provider_profit') { $percent = $agency_price_config['province_percent'] ?: 0; $where['area_level'] = 3; $where['province_id'] = $store->province_id; } if ($key === 'city_profit') { $percent = $agency_price_config['city_percent'] ?: 0; $where['area_level'] = 2; $where['city_id'] = $store->city_id; } if ($key === 'district_profit') { $percent = $agency_price_config['district_percent'] ?: 0; $where['area_level'] = 1; $where['district_id'] = $store->district_id; } $admin = Admin::findOne($where); if ($admin) { $admin_saas_user = SaasUser::findOne($admin->saas_user_id); } $list[] = [ 'name' => $admin_saas_user->name ?? '', 'mobile' => $admin_saas_user->mobile ?? '', 'avatar' => $admin_saas_user->avatar ?? '', 'profit' => $percent . '%' ]; } if ($key === 'bd_profit') { $bd_agency_price_config = Option::get('bd_agency_price_config', 0, 'saas')['value']; $bd_agency_price_config = json_decode($bd_agency_price_config, true) ?: []; $percent = $bd_agency_price_config['bd_agent_percent'] ?: 0; $admin_id = 0; //查询推广代理 if ($store->salesman_id) { $salesman = Salesman::findOne($store->salesman_id); $admin_id = $salesman->admin_id; } if (empty($admin_id) && $store->admin_id) { $admin_id = $store->admin_id; } $admin = Admin::findOne([ 'id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_BD_AGENT ]); if ($admin) { $admin_saas_user = SaasUser::findOne($admin->saas_user_id); } $list[] = [ 'name' => $admin_saas_user->name ?? '', 'mobile' => $admin_saas_user->mobile ?? '', 'avatar' => $admin_saas_user->avatar ?? '', 'profit' => $percent. '%' ]; } if (in_array($key, ['store_profit', 'first_profit', 'second_profit', 'third_profit', 'self_profit'])) { $percent = 0; if ($key === 'store_profit') { $percent = $store->store_share_value; } if ($key === 'first_profit') { $percent = $union_option['level_one'] ?: '0.00'; } if ($key === 'second_profit') { $percent = $union_option['level_two'] ?: '0.00'; } if ($key === 'third_profit') { $percent = $union_option['level_three'] ?: '0.00'; } if ($key === 'self_profit') { $percent = $union_option['self_profit'] ?: '0.00'; } $list[] = [ 'name' => $store->name, 'mobile' => $store->contact_tel ?: '', 'avatar' => $store->logo ?: '', 'profit' => $percent. '%' ]; } if (in_array($key, ['platform_custom_profit', 'store_custom_profit'])) { $where = [ 'is_delete' => SharingReceiverCustom::IS_DELETE_NO ]; if ($key === 'platform_custom_profit') { $where['sharing_way'] = SharingReceiverCustom::SHARING_WAY_PLATFORM; } if ($key === 'store_custom_profit') { $where['sharing_way'] = SharingReceiverCustom::SHARING_WAY_STORE; $where['sharing_store_id'] = $store->id; } $query = SharingReceiverCustom::find() ->where($where) ->orderBy('id DESC') ->select('id, name, sharing_type, type_id, extra_saas_user_id, sharing_profit'); $data = pagination_make($query); foreach ($data['list'] as $item) { $saas_user = null; $item['sharing_type'] = intval($item['sharing_type']); if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_USER) { $saas_user = SaasUser::findOne($item['type_id']); } if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_MERCHANT) { $saas_user = SaasUser::findOne($item['extra_saas_user_id']); if (!$saas_user) { $item['name'] = $item['name'] . "({$item['type_id']})"; } } $list[] = [ 'name' => $saas_user->name ?: $item['name'], 'mobile' => $saas_user->mobile ?: '', 'avatar' => $saas_user->avatar ?: '', 'profit' => $item['sharing_profit']. '%' ]; } $totalCount = $data['totalCount']; $pageNo = $data['pageNo']; $pageSize = $data['pageSize']; } return [ 'code' => 0, 'msg' => '', 'data' => [ 'totalCount' => $totalCount, 'list' => $list, 'pageNo' => $pageNo, 'pageSize' => $pageSize, ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }