| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Admin;
- use app\models\Option;
- use app\models\SaasCategory;
- use app\models\SaasUser;
- use app\models\Salesman;
- use app\models\SharingReceiver;
- use app\models\SharingReceiverCustom;
- use app\models\Store;
- use app\models\User;
- use yii\base\Model;
- class SharingReceiverForm extends Model
- {
- public $id;
- public $ids;
- /**
- * 分账名称
- */
- public $sharing_name;
- /**
- * 用户名称
- */
- public $user_name;
- /**
- * 商城名称
- */
- public $store_name;
- /**
- * 用户手机号
- */
- public $mobile;
- /**
- * 分账类型
- */
- public $sharing_type;
- /**
- * 分账类型ID:用户ID 商户ID
- */
- public $sharing_type_id;
- /**
- * 分账方式
- */
- public $sharing_way;
- /**
- * 分账方式基于店铺:店铺ID
- */
- public $sharing_store_id;
- /**
- * 分账比例
- */
- public $sharing_profit;
- /**
- * 起止时间-开始时间
- */
- public $start_time;
- /**
- * 起止时间-结束时间
- */
- public $end_time;
- /**
- * 额外联盟用户ID
- */
- public $extra_saas_user_id;
- /**
- * 商城分类ID
- */
- public $store_category_id;
- public $key;
- public function rules()
- {
- return [
- [['sharing_name', 'user_name', 'mobile', 'start_time', 'end_time', 'ids', 'sharing_type_id', 'store_name', 'key'], 'string'],
- [['id', 'sharing_type', 'sharing_way', 'sharing_store_id', 'extra_saas_user_id', 'store_category_id'], 'integer'],
- [['sharing_name', 'user_name', 'mobile'], 'trim'],
- [['sharing_profit'], 'number']
- ];
- }
- public function list() {
- try {
- $sharing_name = $this->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()
- ];
- }
- }
- }
|