| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024 |
- <?php
- namespace backend\modules\mall\controllers;
- use backend\controllers\MallController;
- use backend\modules\mall\services\WithdrawalService;
- use common\enums\AddonsEnum;
- use common\enums\UserWalletEnum;
- use common\enums\WithdrawWayEnum;
- use common\globals\GlobalInvoke;
- use common\logic\common\AddonsLimit;
- use common\models\backend\Admin;
- use common\models\census\CensusMall;
- use common\models\census\CensusMallCapital;
- use common\models\census\CensusMallCapitalStatistics;
- use common\models\census\CensusMallPaymentSingleMonth;
- use common\models\census\CensusMallSingleMonth;
- use common\models\common\CapitalLog;
- use common\models\common\BalanceLog;
- use common\models\common\CommissionLog;
- use common\models\common\GlobalVarMap;
- use common\models\common\PlatformSetting;
- use common\models\common\UserBankCard;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserWallet;
- use common\models\common\CapitalStatistics;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\Store\common\models\StoreCouponUserRelate;
- use common\enums\ApiStatusEnum;
- use common\models\user\UserWithdrawLog;
- use common\models\statistics\UserStatistics;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\enums\DownloadEnum;
- use common\models\user\BalanceRechargeLog;
- use yii\helpers\Json;
- class FinanceController extends MallController
- {
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $filename = '会员资产-' . date('YmdHis').'_'.rand(10000,99999);
- $data = \Yii::$app->request->post();
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_ASSETS,CapitalLog::class,'exportMemberAssets',$data);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- $params = \Yii::$app->request->get();
- $where[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
- if (!empty($params['user_id'])) $where[] = ['id' => trim($params['user_id'])];
- if (!empty($params['keyword'])) {
- $where[] = [
- 'or', ['like', 'nickname', trim($params['keyword'])], ['like', 'mobile', trim($params['keyword'])]
- ];
- }
- $params['where'] = $where;
- $params['select'] = 'id,nickname,avatar_url,mobile';
- $params['order'] = 'updated_at desc';
- $user_list = User::listPage($params);
- if(!empty($user_list['list'])){
- $user_id_arr = array_column($user_list['list'],'id');
- $user_wallet_list = UserWallet::lists([
- 'where'=>[['user_id'=>$user_id_arr]],
- 'select'=>'user_id,score,total_score,balance,total_balance,commission,total_commission',
- 'index'=>'user_id',
- ]);
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
- if($is_install) {
- $user_coupon_list = AddonsCouponUserRelate::lists([
- 'where'=>[['user_id'=>$user_id_arr], ['status' => [StatusEnum::ENABLED]]], // 过滤掉无效的优惠券
- 'select'=>'user_id,count(user_id) as coupon_count',
- 'group'=>'user_id',
- 'index'=>'user_id',
- ]);
- // 因为 StoreCouponUserRelate 是门店优惠券。所以需要判断门店插件是否是否安装
- $isStoreInstall = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
- $store_user_coupon_list = $store_can_user_coupon_list = []; // 因为下面用到两个字段判断。 所以初始为空
- if ($isStoreInstall) {
- $store_user_coupon_list = StoreCouponUserRelate::lists([
- 'where' => [['user_id' => $user_id_arr], ['status' => [StatusEnum::ENABLED]]],
- 'select' => 'user_id,count(user_id) as coupon_count',
- 'group' => 'user_id',
- 'index' => 'user_id',
- ]);
- $store_can_user_coupon_list = StoreCouponUserRelate::lists([
- 'where' => [['user_id' => $user_id_arr], ['status' => StatusEnum::ENABLED], ['coupon_status' => 2], ['is_use' => 0]],
- 'select' => 'user_id,count(user_id) as can_coupon_count',
- 'group' => 'user_id',
- 'index' => 'user_id',
- ]);
- }
- $can_user_coupon_list = AddonsCouponUserRelate::lists([
- 'where'=>[['user_id'=>$user_id_arr], ['status'=>StatusEnum::ENABLED], ['coupon_status'=>2], ['is_use'=>0]],
- 'select'=>'user_id,count(user_id) as can_coupon_count',
- 'group'=>'user_id',
- 'index'=>'user_id',
- ]);
- }
- foreach ($user_list['list'] as &$item){
- $item['score'] = $item['total_score'] = $item['balance'] = $item['total_balance'] = $item['commission'] = $item['total_commission'] =$item['coupon_count'] = 0;
- if(isset($user_wallet_list[$item['id']])){
- $item['score'] = $user_wallet_list[$item['id']]['score'];
- $item['total_score'] = $user_wallet_list[$item['id']]['total_score'];
- $item['balance'] = $user_wallet_list[$item['id']]['balance'];
- $item['total_balance'] = $user_wallet_list[$item['id']]['total_balance'];
- $item['commission'] = $user_wallet_list[$item['id']]['commission'];
- $item['total_commission'] = $user_wallet_list[$item['id']]['total_commission'];
- }
- if(isset($user_coupon_list[$item['id']])){
- $item['coupon_count'] = ($user_coupon_list[$item['id']]['coupon_count'] ?? 0) + ($store_user_coupon_list[$item['id']]['coupon_count'] ?? 0);
- $item['can_coupon_count'] = ($can_user_coupon_list[$item['id']]['can_coupon_count'] ?? 0) + ($store_can_user_coupon_list[$item['id']]['can_coupon_count'] ?? 0);
- }
- }
- //导出字段
- $user_list['export_list'] = CapitalLog::MEMBER_ASSETS_EXPORT_FIELD_LIST;
- }
- return $this->success('操作成功', $user_list);
- } else {
- return $this->render('index');
- }
- }
- /**
- * 资产概况-订单概况&支付统计
- *
- * @return mixed
- */
- public function actionStat()
- {
- date_default_timezone_set('PRC');
- if (\Yii::$app->request->isAjax) {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['order_time'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $params['where'][] = ['=', 'mall_id', $this->mall_id];
- if (isset($get['order_time']) && !empty($get['order_time'])) {
- $date = date($get['order_time'] . '-01');
- } else {
- $date = date('Y-m-01');
- }
- $start_time = strtotime($date);
- $end_time = strtotime($date . '+1 month -1 day 23:59:59');
- $params['where'][] = ['>=', 'created_at', $start_time];
- $params['where'][] = ['<=', 'created_at', $end_time];
- $list = UserStatistics::userOrderStatistics($params, $this->mall_id);//订单概况,支付统计
- $date = explode('-',$date)[0] . '-' . explode('-', $date)[1];
- $update_time = date('Y-m-d H:i:s');
- $config = \Yii::$app->services->setting->mall->get($this->mall_id, 'pay', true);
- $data = [];
- if(!empty($config)&&is_array($config)){
- foreach ($config as $k=>&$item){
- $item = json_decode($item,true);
- $data[$k]=[
- 'is_open'=>!empty($item['status'])?$item['status']:0,
- ];
- }
- }
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_JOIN_PAY);
- $data['joinpay_pay']['is_open'] = 0;
- if(!empty($is_install)){
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_JOIN_PAY, 'basic');
- $data['joinpay_pay']['is_open'] = !empty($settings['joinpay_status'])?$settings['joinpay_status']:0;
- }
- $month = date('Y-m',strtotime($date.' 05:00:00'));
- $data_cache = CensusMallSingleMonth::getCensusMallCache($this->mall_id,$month);
- if(!isset($data_cache['total_order_money'])) $data_cache = CensusMallSingleMonth::getCensusMall($this->mall_id,$month);
- $list['order_money'] = $data_cache['total_order_money']??0;
- if(!isset($data_cache['total_pay_money'])) $data_cache = CensusMallSingleMonth::getCensusMall($this->mall_id,$month);
- $list['pay_money'] = $data_cache['total_pay_money']??0;
- $list['not_pay_money'] = bcsub($list['order_money'],$list['pay_money'],2);
- $list['not_pay_money'] = $list['not_pay_money'] > 0 ? $list['not_pay_money'] : 0;
- if(!isset($data_cache['total_refund_money'])) $data_cache = CensusMallSingleMonth::getCensusMall($this->mall_id,$month);
- $list['refund_order_money'] = $data_cache['total_refund_money']??0;
- if(!isset($data_cache['total_deduction_money'])) $data_cache = CensusMallSingleMonth::getCensusMall($this->mall_id,$month);
- $list['deduction_money'] = $data_cache['total_deduction_money']??0;
- if(!isset($data_cache['total_recharge_money'])) $data_cache = CensusMallSingleMonth::getCensusMall($this->mall_id,$month);
- $list['recharge_money'] = $data_cache['total_recharge_money']??0;
- $payment_data = CensusMallPaymentSingleMonth::getCensusMallCache($this->mall_id,$month);
- return $this->success('操作成功', ['orderStatistics' => $list,'pay_config'=>$data,'payment_data'=>$payment_data, 'date' => $date, 'update_time' => $update_time]);
- } else {
- return $this->render('stat');
- }
- }
- /**
- * 资产概况-收支趋势图表数据(佣金收支)
- *
- * @return array
- */
- public function actionIncome() {
- if (\Yii::$app->request->isAjax) {
- $date_type = \Yii::$app->request->get('date_type') ?? 1;
- $search_type = [ 1 => 7, 2 => 30 ];
- $days = $search_type[$date_type] ?? 7; //默认查询7天
- CapitalLog::$capitalType = 'commission';
- $list = CapitalLog::incomeStatistics($days, $this->mall_id);
- //暂时不查统计表
- //$list = CapitalStatistics::incomeStatistics($days, $this->mall_id);
- return $this->success('succfess', $list);
- }
- }
- /**
- * 资产概况-订单概况&会员账户资产
- *
- * @return array
- */
- public function actionAccountAssets() {
- if (\Yii::$app->request->isAjax) {
- $census_mall_list = CensusMallCapital::lists(['where'=>[['mall_id' => $this->mall_id]],'index'=>'type'], true);
- $list['balance_total'] = $census_mall_list['balance']['surplus_nums']??0;
- $list['score_total'] = $census_mall_list['score']['surplus_nums']??0;
- $list['commission_total'] = $census_mall_list['commission']['surplus_nums']??0;
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 积分明细
- * @author hpei
- */
- public function actionScoreLog()
- {
- date_default_timezone_set('PRC');
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $filename = '积分列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $data = \Yii::$app->request->post();
- $data['table'] = 'score';
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_ASSETS,CapitalLog::class,'exportAssets',$data);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- $get = \Yii::$app->request->get();
- $params['alias'] = 'b';
- $params['joinWith'] = ['user' => function($query){$query->select(['id', 'avatar_url', 'nickname', 'mobile']);}];
- if (isset($get['keyword']) && !empty($get['keyword'])) $params['where'] = [['or',['like','nickname',$get['keyword']],['=','mobile',$get['keyword']]]];
- if (isset($get['user_id']) && !empty($get['user_id'])) $params['where'][] = ['=', 'user_id', $get['user_id']];
- if (!empty($get['order_no'])){
- $order = Order::getOne([['order_no'=>$get['order_no']],['mall_id'=>$this->mall_id]],true,['detail']);
- if(!empty($order)){
- $order_detail_ids = array_column($order['detail'],'id');
- $params['where'][] = ['or', ['b.order_no' => $get['order_no']], ['b.source_table_id'=>$order_detail_ids]];
- } else {
- $params['where'][] = ['b.order_no'=>$get['order_no']];
- }
- }
- if (isset($get['desc']) && !empty($get['desc'])) $params['where'][] = ['like', 'b.desc', $get['desc']];
- if (isset($get['capital_type']) && !empty($get['capital_type'])) $params['where'][] = ['=', 'b.capital_type', $get['capital_type']];
- if (!empty($get['date'])) {
- $start_time = strtotime($get['date'][0]);
- $end_time = strtotime($get['date'][1]);
- $params['where'][] = ['>=', 'b.created_at', $start_time];
- $params['where'][] = ['<=', 'b.created_at', $end_time];
- }
- $params['where'][] = ['=', 'b.mall_id', $this->mall_id];
- $params['order'] = 'b.created_at desc,b.id desc';
- $params['limit'] = 20;
- $params['select'] = ['b.change_num', 'b.change_type', 'b.current_num','b.desc', 'b.capital_type','b.created_at', 'b.user_id', 'b.from_type','b.source_table_id','b.source_table'];
- CapitalLog::$capitalType = 'score';
- if ($get['status'] == 0) {
- CapitalLog::$capitalType = 'score_frozen';
- $params['where'][] = ['=', 'b.status',$get['status']];
- }
- $params['page'] = empty($get['page']) ? 1 : $get['page'];
- $pageData = CapitalLog::listPage($params, false);
- if ($pageData === false) return $this->error(CapitalLog::getStaticError());
- // $from_type_map = UserWalletEnum::getFromTypeMapByScore();
- $from_type_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- // $capital_type_map = UserWalletEnum::getCapitalTypeMapByScore();
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- foreach ($pageData['list'] as &$val) {
- $val['source_text'] = ($from_type_map[$val['from_type']] ?? '').($capital_type_map[$val['capital_type']]?'-'.$capital_type_map[$val['capital_type']]:'');
- }
- $pageData['incomeCapitalTypeList'] = $capital_type_map;
- $where['where'] = [['=', 'mall_id', $this->mall_id], ['=', 'type', CapitalStatistics::SCORE]];
- $total = CapitalStatistics::statistics($where, ['sum(capital_total) capital_total', 'sum(expend_capital_total) expend_capital_total']);
- // $score = $total[0]['capital_total'] ? round($total[0]['capital_total'], 2) : 0;//总积分
- // $score_used = $total[0]['expend_capital_total'] ? round($total[0]['expend_capital_total'], 2) : 0;//已使用的积分
- // $score_no_used = $score - $score_used;
- // $pageData['total'] = ['score' => $score, 'score_used' => $score_used, 'score_no_used' => $score_no_used];
- $pageData['date'] = $get['date'];
- // 导出字段列表
- $pageData['export_list'] = CapitalLog::exportFieldsList();
- $type='score';
- $result = CensusMallCapital::findOne(['mall_id'=>$this->mall_id,'type'=>$type]);
- $pageData['total'] = ['score' => $result['total_nums']??0, 'score_used' => $result['use_nums']??0, 'score_no_used' => $result['surplus_nums']??0];
- return $this->success('操作成功', $pageData);
- } else {
- return $this->render('score-log');
- }
- }
- /**
- * 余额明细
- * @return mixed
- */
- public function actionBalanceLog()
- {
- date_default_timezone_set('PRC');
- CapitalLog::$capitalType = 'balance';
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $filename = '余额列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $data = \Yii::$app->request->post();
- $data['table'] = 'balance';
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_ASSETS,CapitalLog::class,'exportAssets',$data);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- $keyword = \Yii::$app->request->get('keyword');
- $user_id = \Yii::$app->request->get('user_id');
- $date = \Yii::$app->request->get('date');
- $remark = \Yii::$app->request->get('remark');
- $type = \Yii::$app->request->get('balance_from_type');
- $flag = \Yii::$app->request->get('flag');
- $order_no = \Yii::$app->request->get('order_no');
- $where[] = ['log.mall_id' => $this->mall_id];
- $where[] = ['log.status' => [0,1]];
- if (\Yii::$app->request->get('status') == 0) {
- //查询冻结表,未结算数据
- //$where[] = ['in', 'log.status', [-1, 0]];
- $where[] = ['in', 'log.status', [0]];
- CapitalLog::$capitalType = 'balance_frozen';
- }
- if (!empty($keyword)) $where[] = ['or', ['like', 'user.nickname', $keyword], ['like', 'user.mobile', $keyword]];
- if (!empty($user_id)) $where[] = ['log.user_id' => $user_id];
- if (!empty($remark)) $where[] = ['like', 'log.desc', $remark];
- if (!empty($type)) $where[] = ['log.capital_type' => $type];
- if (!empty($order_no)){
- $order = Order::getOne([['order_no'=>$order_no],['mall_id'=>$this->mall_id]],true,['detail']);
- if(!empty($order)){
- $order_detail_ids = array_column($order['detail'],'id');
- $where[] = ['or', ['log.order_no' => $order_no], ['log.source_table_id'=>$order_detail_ids]];
- } else {
- $where[] = ['log.order_no'=>$order_no];
- }
- }
- if (!empty($date)) {
- $start_time = strtotime($date[0]);
- $end_time = strtotime($date[1]);
- $where[] = ['>=', 'log.created_at', $start_time];
- $where[] = ['<=', 'log.created_at', $end_time];
- }
- $join = [
- ['LEFT JOIN', User::tableName() . 'as user', 'log.user_id = user.id and log.mall_id = user.mall_id'],
- ];
- $params = [
- 'alias' => 'log',
- 'select' => 'log.id,log.mall_id,log.user_id,log.change_type,log.from_type,log.capital_type,log.change_num,log.current_num,log.desc,log.source_table,log.source_table_id,log.created_at,user.nickname,user.avatar_url,user.mobile,user.username',
- 'join' => $join,
- 'where' => $where,
- 'order' => 'log.created_at desc,log.id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- if ($flag == "EXPORT") {
- } else {
- $list = CapitalLog::listPage($params, false, true);
- }
- // $capital_type_map = UserWalletEnum::getCapitalTypeMapByBalance();
- // $from_type_map = UserWalletEnum::getFromTypeMapByBalance();
- $from_type_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$v) {
- $v['source_text'] = ($from_type_map[$v['from_type']] ?? '').($capital_type_map[$v['capital_type']]?'-'.$capital_type_map[$v['capital_type']]:'');
- $v['created_at'] = date("Y-m-d H:i:s", $v["created_at"]);
- };
- }
- // 业务类型
- $list['incomeCapitalTypeList'] = $capital_type_map;
- // 导出字段列表
- $list['export_list'] = CapitalLog::exportFieldsList();
- $where['where'] = [['=', 'mall_id', $this->mall_id], ['=', 'type', CapitalStatistics::BALANCE]];
- $total = CapitalStatistics::statistics($where,
- ['sum(capital_total) capital_total', //总额
- 'sum(expend_capital_total) expend_capital_total', //使用总额
- 'sum(cash_out_total) cash_out_total', //提现额
- 'sum(service_charges_tatoal) service_charges_tatoal']);//手续费
- // $statistics_data['income_capital_total'] = $total[0]['capital_total'] ? round($total[0]['capital_total'], 2) : 0;
- // $statistics_data['expend_capital_total'] = $total[0]['expend_capital_total'] ? round($total[0]['expend_capital_total'], 2) : 0;
- // $statistics_data['cash_out_total'] = $total[0]['cash_out_total'] ? round($total[0]['cash_out_total'], 2) : 0;
- // $statistics_data['service_charges_tatoal'] = $total[0]['service_charges_tatoal'] ? round($total[0]['service_charges_tatoal'], 2) : 0;
- // $statistics_data['available_capital_total'] = UserWallet::find()->where(['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED])->sum('balance');
- // $statistics_data['available_capital_total'] = $statistics_data['available_capital_total'] ? round($statistics_data['available_capital_total'], 2) : 0;
- $list['date'] = $date;
- $type='balance';
- $result = CensusMallCapital::findOne(['mall_id'=>$this->mall_id,'type'=>$type]);
- $statistics_data['income_capital_total'] = $result['total_nums']??0;
- $statistics_data['expend_capital_total'] = $result['use_nums']??0;
- $statistics_data['available_capital_total'] = $result['surplus_nums']??0;
- $statistics_data['cash_out_total'] = $result['withdrawal_nums']??0;
- $list['statistics_data'] = $statistics_data;
- return $this->success('操作成功', $list);
- }
- return $this->render('balance-log');
- }
- /**
- * 佣金明细
- */
- public function actionCommissionLog()
- {
- date_default_timezone_set('PRC');
- CapitalLog::$capitalType = 'commission';
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $filename = '佣金列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $data = \Yii::$app->request->post();
- $data['table'] = 'commission';
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_ASSETS,CapitalLog::class,'exportAssets',$data);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- $get = \Yii::$app->request->get();
- $params[] = ['log.mall_id' => $this->mall_id];
- if ($get['status'] == 0) {
- //查询冻结表,未结算数据
- $params[] = ['in', 'log.status', [ 0]];
- CapitalLog::$capitalType = 'commission_frozen';
- }
- if (!empty($get['keyword'])){
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id], ['or', ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']]]],'select'=>'id']);
- if(!empty($user_list)){
- $user_ids = array_column($user_list,'id');
- $params[] = ['log.user_id' => $user_ids];
- }else{
- $params[] = ['log.user_id' => []];
- }
- }
- if (!empty($get['user_id'])) $params[] = ['log.user_id' => $get['user_id']];
- if (!empty($get['order_no'])){
- if(substr($get['order_no'], 0, 1) == 'C'){
- // 对收银台做单独处理
- $cashier_order_class = \addons\Store\common\models\StoreCashierOrder::class;
- $tmp_capital_log = CapitalLog::find()->where(['or', ['source_table' => 'store_cashier_order'], ['source_table' => $cashier_order_class::tableName()]])
- ->andWhere(['and', ['order_no'=>$get['order_no']], ['mall_id'=>$this->mall_id]])
- ->select('id')
- ->column();
- $params[] = ['in','log.id',$tmp_capital_log ?? []];
- }else{
- $order_detail_ids = [];
- $order = Order::getOne([['order_no'=>$get['order_no']],['mall_id'=>$this->mall_id]],true,['detail']);
- if(!empty($order)){
- $order_detail_ids = array_column($order['detail'],'id');
- }
- $params[] = ['log.source_table_id'=>$order_detail_ids];
- }
- }
- if (!empty($get['capital_type'])) $params[] = ['log.capital_type' => $get['capital_type']];
- if(!empty($get['order_num'])) $params[] = ['log.order_no'=>$get['order_num']];//订单号查询
- if (!empty($get['date'])) {
- $start_time = strtotime($get['date'][0]);
- $end_time = strtotime($get['date'][1]);
- $params[] = ['>=', 'log.created_at', $start_time];
- $params[] = ['<=', 'log.created_at', $end_time];
- }
- //过滤该插件
- $params[]= ['!=', 'from_type', AddonsEnum::ADDONS_NAME_NI_BAO_ACHIEVEMENT];
- $params = [
- 'alias' => 'log',
- 'select' => 'log.id,log.mall_id,log.user_id,log.change_type,log.change_num,log.current_num,log.desc,log.from_type,log.capital_type,log.created_at,log.order_no,source_table,source_table_id',
- 'with'=>['user'],
- 'where' => $params,
- 'order' => 'log.created_at desc,log.id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = CapitalLog::listPage($params, false, true);
- // $commission_map = UserWalletEnum::getCommissionMap();
- $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- $list['date'] = $get['date'];
- // $capital_type_map = UserWalletEnum::getCapitalTypeMapByCommission();
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- if (!empty($list['list'])) {
- $douyin_cps_ids = [];
- /** @var \addons\DouyinCps\common\models\DouyinCpsOrder $douyin_cps_class */
- $douyin_cps_class = \addons\DouyinCps\common\models\DouyinCpsOrder::class;
- //user.nickname,user.avatar_url,user.mobile,user.username
- foreach ($list['list'] as &$lv) {
- $lv['nickname'] = '';
- $lv['avatar_url'] = '';
- $lv['mobile'] = '';
- $lv['username'] = '';
- if(!empty($lv['user'])){
- $lv['nickname'] = $lv['user']['nickname'];
- $lv['avatar_url'] = $lv['user']['avatar_url'];
- $lv['mobile'] = $lv['user']['mobile'];
- $lv['username'] = $lv['user']['username'];
- }
- $lv['source_text'] = ($commission_map[$lv['from_type']] ?? '').($capital_type_map[$lv['capital_type']]?'-'.$capital_type_map[$lv['capital_type']]:'');
- $lv['created_at'] = date('Y-m-d H:i:s', $lv['created_at']);
- // 获取订单号
- if ($lv['source_table'] == 'order_detail' || $lv['source_table'] == OrderDetail::tableName()) {
- $detail_ids[] = $lv['source_table_id'];
- } elseif (class_exists((string)$douyin_cps_class) && $lv['source_table'] == $douyin_cps_class::tableName()) {
- $douyin_cps_ids[] = $lv['source_table_id'];
- } elseif ($lv['source_table'] == 'order' || $lv['source_table'] == '{{%order}}') {
- $order_ids[] = $lv['source_table_id'];
- }
- }
- // 根据订单详情ID获取订单号
- if (!empty($detail_ids)) {
- $order_params = [
- 'where' => [
- ['id' => $detail_ids]
- ],
- 'with' => ['user', 'order'],
- 'select' => 'id,user_id,order_id',
- 'index' => 'id'
- ];
- $commission_orders = OrderDetail::lists($order_params, true);
- // print_r($commission_orders);exit;
- foreach ($list['list'] as &$item) {
- if ($item['source_table'] == 'order_detail' || $item['source_table'] == OrderDetail::tableName()) {
- if (!empty($commission_orders[$item['source_table_id']])) {
- $item['order_no'] = $commission_orders[$item['source_table_id']]['order']['order_no'];
- $item['buy_user'] = [
- 'id' => $commission_orders[$item['source_table_id']]['user']['id'],
- 'nickname' => $commission_orders[$item['source_table_id']]['user']['nickname'],
- 'avatar_url' => $commission_orders[$item['source_table_id']]['user']['avatar_url']
- ];
- }
- }
- }
- }
- //显示佣金数据来源为order表的下单用户数据
- if (!empty($order_ids)) {
- $order_params = [
- 'where' => [
- ['id' => $order_ids]
- ],
- 'with' => ['user'],
- 'select' => 'id,user_id,order_no',
- 'index' => 'id'
- ];
- $commission_orders = Order::lists($order_params, true);
- // print_r($commission_orders);exit;
- foreach ($list['list'] as $key => $item2) {
- if ($item2['source_table'] == 'order' || $item2['source_table'] == '{{%order}}') {
- // print_r($item2);exit;
- if (!empty($commission_orders[$item2['source_table_id']])) {
- $list['list'][$key]['order_no'] = $commission_orders[$item2['source_table_id']]['order_no'];
- $list['list'][$key]['buy_user'] = [
- 'id' => $commission_orders[$item2['source_table_id']]['user']['id'],
- 'nickname' => $commission_orders[$item2['source_table_id']]['user']['nickname'],
- 'avatar_url' => $commission_orders[$item2['source_table_id']]['user']['avatar_url']
- ];
- }
- }
- }
- }
- if (!empty($douyin_cps_ids)) {
- $commission_orders = $douyin_cps_class::getOrderNoByIds(array_unique($douyin_cps_ids));
- foreach ($list['list'] as &$item) {
- if ($item['source_table'] == $douyin_cps_class::tableName()) {
- $item['order_no'] = $commission_orders[$item['source_table_id']];
- }
- }
- }
- }
- // 业务类型
- $list['commissionFromTypeList'] = $commission_map;//来源类型
- $list['incomeCapitalTypeList'] = $capital_type_map;//佣金类型
- // 导出字段列表
- $list['export_list'] = CommissionLog::EXPORT_FIELD_LIST;
- $where['where'] = [['=', 'mall_id', $this->mall_id], ['=', 'type', CapitalStatistics::COMMISSIOIN]];
- // $total = CapitalStatistics::statistics($where,
- // ['sum(capital_total) capital_total', //总额
- // 'sum(invalid_capital_total) invalid_capital_total']);//无效额
- // $statistics_data['commission_total'] = $total[0]['capital_total'] ? round($total[0]['capital_total'], 2) : 0;
- // $statistics_data['invalid_capital_total'] = $total[0]['invalid_capital_total'] ? round($total[0]['invalid_capital_total'], 2) : 0;
- CapitalLog::$capitalType = 'commission_frozen';
- $statistics_list = CapitalLog::find()->where(['mall_id' => $this->mall_id, 'status' => 0])->asArray()->all();
- if (empty($statistics_list)) {
- // $statistics_data['frozen_capital_total'] = 0;
- } else {
- // foreach ($statistics_list as $val) {
- // if ($val['change_type'] == 'add') $statistics_data['frozen_capital_total'] += $val['change_num'];
- // if ($val['change_type'] == 'sub') $statistics_data['frozen_capital_total'] -= $val['change_num'];
- // }
- // $statistics_data['frozen_capital_total'] = round($statistics_data['frozen_capital_total'], 2);
- }
- $type='commission';
- $result = CensusMallCapital::findOne(['mall_id'=>$this->mall_id,'type'=>$type]);
- $total_nums = $result['total_nums']??0;
- $unsettled = $result['unsettled']??0;
- $surplus_nums = $result['surplus_nums']??0;
- $statistics_data['commission_total'] = $total_nums+$unsettled;
- $statistics_data['frozen_capital_total'] = $unsettled;
- $statistics_data['settled_commission_total'] = $surplus_nums;
- // $statistics_data['settled_commission_total'] = $total_nums;
- $statistics_data['invalid_capital_total'] = $result['cancel_nums']??0;
- $list['statistics_data'] = $statistics_data;
- $list['export_list'] = CapitalLog::exportFieldsList();
- return $this->success('操作成功', $list);
- } else {
- $flag = \Yii::$app->request->post('flag');
- if (empty($flag)) {
- return $this->render('commission-log');
- } else {
- // 导出数据
- }
- }
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-05-15
- * @Time: 9:58
- * @Note:提现记录
- * @return string|\yii\web\Response
- */
- public function actionCash()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- if (\Yii::$app->request->post('flag') === 'EXPORT') {
- $post = \Yii::$app->request->post();
- if (isset($post['status']) && ($post['status'] == -1 || $post['status'] === '')) unset($post['status']);
- $post['mall_id'] = $this->mall_id;
- $filename = '提现审核-' . date('YmdHis').'_'.rand(10000,99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_CASHDRAW_AUDIT,UserWithdrawLog::class,'exportHandle',$post);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- } else {
- $log = new UserWithdrawLog();
- $params = \Yii::$app->request->get();
- $params['mall_id'] = $this->mall_id;
- $logs = $log->searchLogList($params);
- //公告调用:提现是否需要验证码
- $need_risk = GlobalInvoke::exec(GlobalInvoke::WITHDRAW_RISK, $this->mall_id, ['mall_id' => $this->mall_id]);
- $mobile = '';
- if($need_risk && $need_risk['is_enable']){
- // //该商城的管理员
- // $mall_admin = Admin::findOne(['status' => StatusEnum::ENABLED,'mall_id' => $this->mall_id,'is_default' => 1]);
- // $mobile = $mall_admin->account;
- $mobile = $need_risk['cash_mobile'];
- }
- // 统计
- list(
- $audit_commision,
- $pending_commision,
- $paid_commision,
- $rejected_commision,
- $total_commision
- ) = UserWithdrawLog::getWithdrawLogStatistic($this->mall_id);
- $data = [
- 'list' => $logs['data']['list'],
- 'export_list' => $logs['data']['export_list'],
- 'pagination' => $logs['data']['pagination'],
- 'need_risk' => $need_risk ? $need_risk['is_enable'] : false,
- 'mobile' => $mobile,
- 'statistic' => compact(
- 'audit_commision', 'pending_commision', 'paid_commision', 'rejected_commision', 'total_commision'
- ),
- 'mall_id' => $this->mall_id,
- ];
- // 更改为调用统一提现方法
- $data['DefaultWithDrawWayMap'] = UserWithdrawLog::getUserWithdrawType($this->mall_id);
- $data['DefaultWithDrawWayMap']['alipay'] = '支付宝线下转账';
- $data['withdraw_from_list'] = [
- ['key' => '0', 'label' => "全部", 'value' => ""],
- ['key' => '2', 'label' => "余额", 'value' => "2"],
- ['key' => '1', 'label' => "佣金", 'value' => "1"],
- ['key' => '3', 'label' => "货款", 'value' => "3"],
- ['key' => '4', 'label' => "积分", 'value' => "4"],
- ['key' => '9', 'label' => "积分增长赢", 'value' => "9"],
- ];
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_HUI_DIAN)) {
- $huidian_custom_name = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_HUI_DIAN, 'basic.custom_name');
- $data['withdraw_from_list'][] = ['key' => '10', 'label' => $huidian_custom_name . '提现', 'value' => "10"];
- }
- return $this->success('success', $data, ApiStatusEnum::CODE_SUCCESS, 0);
- }
- }else{
- }
- return $this->render('cash');
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-05-30
- * @Time: 10:15
- * @Note:提现处理
- * @throws \Exception
- */
- public function actionCashApply()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $post = \Yii::$app->request->post();
- if($post['status'] == 2){
- $check = $this->checkCashRisk($post);
- if(!$check){
- return $this->error('验证码错误');
- }
- }
- $form = new UserWithdrawLog();
- $form->attributes = \Yii::$app->request->post();
- $res = $form->updateLog();
- if($res) return $this->success('操作成功',['res'=>$res]);
- return $this->error('错误');
- }
- }
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 批量审核处理
- * @Author: lun
- * @DateTime: 2022/7/16 0016 15:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- public function actionBatchCashApply()
- {
- try {
- $request = \Yii::$app->request;
- if ($request->isAjax && $request->isPost) {
- $params = $request->post();
- // 检查提交的参数
- $res = \Yii::$app->services->validate->validate($params,[
- [['ids', 'status'], 'required'],
- [['status'], 'integer'],
- [['content'], 'string'],
- ['ids', 'safe']
- ]);
- if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $ids = $params['ids'];// 上传的提现记录id
- foreach ($ids as $id) {
- $model = UserWithdrawLog::findOne(['id' => $id]);
- if ($model) {
- $model->status = $params['status'];
- $model->content = $params['content']??'';
- $res = $model->updateLog();// 处理提现
- if ($res === false) throw new \Exception($model->getErrorMessage());
- }
- }
- }
- return $this->success('批量操作成功');
- } catch (\Exception $e) {
- return $this->error('批量操作失败:'.$e->getMessage());
- }
- }
- // 支付宝代付提现
- public function actionAlitopay(){
- $data = $this->request->post();
- $data['mall_id'] = $this->mall_id;
- $data['log_id'] = is_string($data['id']) ? explode(',',$data['id']) : $data["id"];
- // $data['log_id'] = [272,274];
- $service = new WithdrawalService($data);
- $res = $service->aliToPay();
- if ($res === false){
- return $this->error($service->error);
- }
- return $this->success('操作成功',['res'=>$res]);
- }
- // 代税宝提现
- public function actionAvoidtax(){
- $data = $this->request->post();
- $data['mall_id'] = $this->mall_id;
- $data['log_id'] = is_string($data['id']) ? explode(',',$data['id']) : $data["id"];
- $service = new WithdrawalService($data);
- $res = $service->avoidtaxToPay();
- if ($res === false){
- return $this->error($service->error);
- }
- return $this->success('操作成功',['res'=>$res]);
- }
- /**
- * 检查 支付宝代付、代税宝 插件是否开启
- */
- public function actionCheckChannel(){
- $res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_WITHDRAWAL_CHANNEL,$this->mall_id,[
- 'mall_id'=>$this->mall_id
- ]);
- return $this->success('操作成功',$res);
- }
- /**
- * 银典支付
- *
- * @return void
- */
- public function actionYunfastPay()
- {
- if ($this->request->isPost) {
- $data = $this->request->post();
- $data['mall_id'] = $this->mall_id;
- $data['log_id'] = is_string($data['id']) ? explode(',',$data['id']) : $data["id"];
- $service = new WithdrawalService($data);
- $res = $service->yunfastPay();
- return $res === false
- ? $this->error($service->error)
- : $this->success(sprintf('%s笔订单打款中', count($res)));
- }
- }
- /**
- * 检测是否需要手机验证码
- * @param $params
- * @return bool
- */
- private function checkCashRisk($params)
- {
- //打款操作:检测是否需要手机验证码,全局调用
- $need_risk = GlobalInvoke::exec(GlobalInvoke::WITHDRAW_RISK, $this->mall_id, ['mall_id' => $this->mall_id]);
- if($need_risk && $need_risk['is_enable']){
- if(empty($params['verification_code'])){
- return false;
- }
- $check = GlobalInvoke::exec(GlobalInvoke::WITHDRAW_RISK_CAPTCHA, $this->mall_id, ['mall_id' => $this->mall_id,'cash_id' => $params['id'],'captcha' => $params['verification_code']]);
- if(!$check['status']){
- return false;
- }
- }
- return true;
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-05-15
- * @Time: 9:58
- * @Note:充值记录
- * @return string|\yii\web\Response
- */
- public function actionRechargeAudit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $params['order'] = 'id desc';
- $params['alias'] = 'b';
- $params['joinWith'] = ['user'];
- if (isset($get['status']) && $get['status'] != -1) {
- $params['where'][] = ['=', 'b.withdraw_status', $get['status']];
- }
- if (!empty($get['keyword'])) {
- $params['where'] = [[
- 'or',
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]];
- }
- if (!empty($get['user_id'])) {
- $params['where'][] = ['=', 'user_id', $get['user_id']];
- }
- if (!empty($get['start_date']) && !empty($get['end_date'])) {
- $get['start_date'] = str_replace('+', ' ', $get['start_date']);
- $get['end_date'] = str_replace('+', ' ', $get['end_date']);
- $params['where'][] = ['>=', 'b.created_at', strtotime($get['start_date'])];
- $params['where'][] = ['<=', 'b.created_at', strtotime($get['end_date'])];
- }
- $params['where'][] = ['=', 'b.mall_id', $this->mall_id];
- $params['order'] = ['id' => SORT_DESC];
- $params['limit'] = 10;
- $list = BalanceRechargeLog::listPage($params);
- return $this->success('操作成功',$list);
- }
- }
- return $this->render('recharge-audit');
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-05-30
- * @Time: 10:15
- * @Note:提现处理
- * @throws \Exception
- */
- public function actionRechargeApply()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $post = \Yii::$app->request->post();
- $res = BalanceRechargeLog::updateLog($post);
- if($res) return $this->success('操作成功',['res'=>$res]);
- return $this->error(BalanceRechargeLog::getStaticError());
- }
- }
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 银行卡合规提现处理
- *
- * @return string
- */
- public function actionBanktax()
- {
- $data = $this->request->post();
- $data['mall_id'] = $this->mall_id;
- $data['log_id'] = is_string($data['id']) ? explode(',', $data['id']) : $data['id'];
- $service = new WithdrawalService($data);
- $res = $service->banktaxToPay();
- if ($res === false) {
- return $this->error($service->error);
- }
- return $this->success('操作成功', ['res' => $res]);
- }
- /**
- * 资产概况-订单概况&会员账户资产(每日)
- *
- * @return array
- */
- public function actionAccountAssetsDaily()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $date = $get['date'];
- $first = CensusMallCapitalStatistics::find()->where([
- 'mall_id' => $this->mall_id
- ])->asArray()->one();
- if ($first) {
- $start_date = date('Y-m-d', ($first['created_at']-86400));
- }
- if ($date) {
- $statistics = CensusMallCapitalStatistics::find()->where([
- 'mall_id' => $this->mall_id,
- 'statistics_date' => $date,
- 'status' => StatusEnum::ENABLED
- ])->asArray()->one();
- }
- return $this->success('操作成功', [
- 'limit_date' => $start_date ?? date('Y-m-d', time()),
- 'balance_surplus_nums' => $statistics['balance_surplus_nums'] ?? 0,
- 'commission_surplus_nums' => $statistics['commission_surplus_nums'] ?? 0,
- 'score_surplus_nums' => $statistics['score_surplus_nums'] ?? 0,
- ]);
- }
- }
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionLinggong()
- {
- $data = $this->request->post();
- $data['mall_id'] = $this->mall_id;
- $data['log_id'] = is_string($data['id']) ? explode(',', $data['id']) : $data['id'];
- $service = new WithdrawalService($data);
- $res = $service->linggongToPay($data);
- if ($res === false) {
- return $this->error($service->error);
- }
- return $this->success('操作成功', ['res' => $res]);
- }
- }
|