| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers\report;
- use app\jobs\LgCashJob;
- use app\models\AccountLog;
- use app\models\Cash;
- use app\models\Goods;
- use app\models\Lg;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\Option;
- use app\models\ReportCommitIntegral;
- use app\models\ReportForm;
- use app\models\ReportInfo;
- use app\models\ReportPool;
- use app\models\ReportPoolDetail;
- use app\models\ReportPoolLevel;
- use app\models\ReportSetting;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\User;
- use app\modules\admin\controllers\adopt\FoodTableNumber;
- use app\modules\admin\controllers\adopt\Land;
- use app\modules\admin\controllers\adopt\LandCat;
- use app\modules\admin\controllers\BaseController;
- use app\modules\admin\models\adopt\AdoptGoodsForm;
- use app\modules\admin\models\adopt\DeviceForm;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MdGoodsForm;
- use app\plugins\adopt\models\AdoptCat;
- use app\plugins\adopt\models\AdoptGoods;
- use app\plugins\adopt\models\AdoptSetting;
- use app\utils\Alipay\Alipay;
- use app\utils\LgApi;
- use app\utils\OrderNo;
- use app\utils\QrCode;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class ReportController extends BaseController
- {
- /**
- * 全域分红配置
- * @return \yii\web\Response
- */
- public function actionSaveReportSetting() {
- $store_id = get_store_id();
- // print_r($store_id);
- $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
- if (!$report_setting){
- $report_setting = new ReportSetting();
- }
- $form_name = post_params('form_name', '');
- $form = post_params('form', '');
- $is_share_self = post_params('is_share_self', 0);
- $is_auto_send = post_params('is_auto_send', 0);
- $auto_send_day = post_params('auto_send_day', 0);
- $dividend_day = post_params('dividend_day', 0);
- $reporr_limit = post_params('reporr_limit', 0);
- $third_commission = post_params('third_commission', 0);
- $second_commission = post_params('second_commission', 0);
- $first_commission = post_params('first_commission', 0);
- $report_setting->store_id = $store_id;//表单名称
- $report_setting->form_name = $form_name;//表单名称
- $report_setting->is_share_self = $is_share_self;//是否分销内购
- $report_setting->is_auto_send = $is_auto_send;//是否自动发放
- $report_setting->auto_send_day = $auto_send_day;//自动发放天数
- $report_setting->dividend_day = $dividend_day;//分红周期
- $report_setting->reporr_limit = $reporr_limit;//每天限制提交报单次数
- $report_setting->third_commission = $third_commission;//三级分销
- $report_setting->second_commission = $second_commission;//二级分销
- $report_setting->first_commission = $first_commission;//一级分销
- $report_setting->updated_at = time();
- if (!$report_setting->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $report_setting->errors
- ]);
- }else{
- //处理form数据
- // var_dump($form);die();
- if (is_array($form)){
- // print_r(Json::decode($form));die;
- ReportForm::updateAll(['is_delete' => 1], ['store_id'=>$store_id,'report_setting_id' => $report_setting->id,'is_delete'=> 0]);
- // $form = Json::decode($form);
- foreach ($form as $key => $value) {
- $form_data = new ReportForm();
- $form_data->store_id = $store_id;
- $form_data->report_setting_id = $report_setting->id;
- $form_data->name = $value['name'];
- $form_data->type = $value['type'];
- $form_data->required = $value['required'];
- $form_data->default = $value['default'];
- $form_data->tip = $value['tip'];
- $form_data->sort = $value['sort'];
- $form_data->is_delete = 0;
- $form_data->created_at = time();
- $form_data->save();
- }
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- }
- /**
- * 全域分红配置查询
- * @return \yii\web\Response
- */
- public function actionGetReportSetting() {
- $store_id = get_store_id();
- $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
- if (!$report_setting) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'first_commission' => '',
- 'second_commission' => '',
- 'third_commission' => '',
- 'dividend_day' => '',
- 'auto_send_day' =>'',
- 'is_auto_send' => 0,
- 'is_share_self' => 0,
- 'form_name' => '',
- 'reporr_limit' => '',
- 'form' => [],
- ]
- ]);
- }
- //如果存在的话查询表单内容
- $report_form = ReportForm::find()->where(['report_setting_id'=>$report_setting->id,'store_id' => $store_id,'is_delete'=> 0])
- ->orderBy('id asc,sort asc')
- ->asArray()
- ->all()??[];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'first_commission' => $report_setting->first_commission,
- 'second_commission' => $report_setting->second_commission,
- 'third_commission' => $report_setting->third_commission,
- 'dividend_day' => $report_setting->dividend_day,
- 'auto_send_day' => $report_setting->auto_send_day,
- 'is_auto_send' => $report_setting->is_auto_send,
- 'is_share_self' => $report_setting->is_share_self,
- 'form_name' => $report_setting->form_name,
- 'reporr_limit' => $report_setting->reporr_limit,
- 'form' => $report_form,
- ]
- ]);
- }
- /**
- * 报单列表展示
- */
- public function actionList() {
- $store_id = get_store_id();
- $report_id = get_params('report_id',0);
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $audit_start_time = get_params('audit_start_time');
- $audit_end_time = get_params('audit_end_time');
- $name = get_params('nickname');
- $mobile = get_params('binding');
- $status = get_params('status');
- $query = ReportInfo::find()->alias('r')
- ->leftJoin(['u' => User::tableName()], 'u.id=r.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['r.is_delete' => 0, 'r.store_id' => $store_id]);
- if ($start_time) {
- $query->andWhere(['>=', 'r.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'r.created_at', strtotime($end_time)]);
- }
- if ($report_id){
- $query->andWhere(['r.id' => $report_id]);
- }
- if ($audit_start_time) {
- $query->andWhere(['>=', 'r.audit_time', strtotime($audit_start_time)]);
- }
- if ($audit_end_time) {
- $query->andWhere(['<=', 'r.audit_time', strtotime($audit_end_time)]);
- }
- if ($name) {
- $query->andWhere([
- 'or',
- ['like', 'su.name', $name],
- ['like', 'u.nickname', $name],
- ]);
- }
- if ($mobile) {
- $query->andWhere([
- 'or',
- ['like', 'su.mobile', $mobile],
- ['like', 'u.binding', $mobile],
- ]);
- }
- // if ($audit_start_time) {
- // $query->andWhere(['>=', 'c.updated_at', strtotime($audit_start_time)]);
- // }
- // if ($audit_end_time) {
- // $query->andWhere(['<=', 'c.updated_at', strtotime($audit_end_time)]);
- // }
- if ($status == 0) { //待审核
- $query->andWhere(['r.status' => 0]);
- }
- if ($status == 1) { //已通过
- $query->andWhere(['r.status' =>1]);
- }
- if ($status == 2) { // 已拒绝
- $query->andWhere(['r.status' => 2]);
- }
- $query->distinct()->orderBy('r.status ASC,r.created_at DESC')
- ->select([
- 'r.*',
- 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar',
- 'u.nickname', 'u.avatar_url', 'u.binding'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- //表单内容
- $value['audit_info'] = Json::decode($value['audit_info']);
- //提交时间
- $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
- //审核时间
- $value['audit_time'] = $value['audit_time']?date('Y-m-d H:i:s',$value['audit_time']):'';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- /**
- * 报单新建
- */
- public function actionAddReport()
- {
- $store_id = get_store_id();
- $user_id = post_params('user_id');
- $audit_info = post_params('audit_info');
- if (!$user_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请选择报单用户'
- ]);
- }
- $user = User::findOne($user_id);
- if (!$user || empty($user->binding)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '报单用户查询失败'
- ]);
- }
- $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- if (!$saas_user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '报单用户查询失败_'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- if (!$audit_info) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '审核信息不能为空'
- ]);
- }
- //判断是否有设置配置
- $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
- if (!$report_setting->dividend_day){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请先配置分红周期'
- ]);
- }
- //检查是否有本期分红池
- $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
- ->andWhere(['<=','start_time',time()])
- ->andWhere(['>=','end_time',time()])
- ->orderBy('id desc')
- ->one();
- //如果没有奖金池创建第一个池子
- if (!$pool_info) {
- $pool_info = new ReportPool();
- $pool_info->store_id = $store_id;
- // $pool_info->saas_id = get_saas_user_id();
- $pool_info->start_time = time();
- $pool_info->end_time = time() + $report_setting->dividend_day * 24 * 60 * 60;
- $pool_info->created_at = time();
- $pool_info_copy = ReportPool::find()->where(['store_id'=>get_store_id()])
- ->orderBy('id desc')
- ->one();
- if ($pool_info_copy){
- $pool_info->number = $pool_info_copy->number+1;
- }else{
- $pool_info->number =1;
- }
- if (!$pool_info->save()){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '添加失败'
- ]);
- }
- }
- //保存信息
- $report_info = new ReportInfo();
- $report_info->store_id = $store_id;
- $report_info->user_id = $user_id;
- $report_info->saas_id = $saas_user->id;
- $report_info->audit_info = Json::encode($audit_info);
- $report_info->status = 0;
- $report_info->is_delete = 0;
- $report_info->order_no = OrderNo::getOrderNo(OrderNo::REPORT_INFO);
- $report_info->pool_id = $pool_info->id;
- $report_info->created_at = time();
- if (!$report_info->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $report_info->getErrors()
- ]);
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功'
- ]);
- }
- /**
- * 报单列表删除
- */
- public function actionReportDelete()
- {
- $report_id= get_params('report_id');
- $form = ReportInfo::find()
- ->where('id = :report_id', [':report_id' => $report_id])
- ->andWhere(['store_id' => get_store_id()])
- ->one();
- $form->is_delete = 1;
- if ($form && $form->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败',
- ]);
- }
- /**
- * 报单列表通过或拒绝
- */
- public function actionBatchAudit() {
- $report_id = post_params('report_id');
- $money = post_params('money');
- $status = post_params('status');
- if (!in_array($status, [1, 2])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- // var_dump($report_id);die;
- if (empty($report_id) || !is_array($report_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- if ($status == 1) {
- //审核通过
- foreach ($report_id as $value) {
- $report_info = ReportInfo::findOne($value);
- if (!$report_info){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '该报单不存在'
- ]);
- }
- $report_info->status = 1;
- $report_info->bonus_points = $money;
- $report_info->audit_time = time();
- //审核通过需要根据报单配置进行积分分配
- $report_setting = ReportSetting::find()->where(['store_id'=>get_store_id()])->one();
- if (!$report_setting){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请先配置全域分红配置'
- ]);
- }
- //查询本期奖金池
- $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
- ->andWhere(['<=','start_time',time()])
- ->andWhere(['>=','end_time',time()])
- ->orderBy('id desc')
- ->one();
- // print_r($pool_info);die();
- if (!$pool_info){
- $pool_info = new ReportPool();
- $pool_info->store_id = get_store_id();
- // $pool_info->saas_id = get_saas_user_id();
- $pool_info->start_time = time();
- $pool_info->end_time = time() + $report_setting->dividend_day * 24 * 60 * 60;
- $pool_info->created_at = time();
- $pool_info_copy = ReportPool::find()->where(['store_id'=>get_store_id()])
- ->orderBy('id desc')
- ->one();
- if ($pool_info_copy){
- $pool_info->number = $pool_info_copy->number+1;
- }else{
- $pool_info->number =1;
- }
- if (!$pool_info->save()){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '添加失败'
- ]);
- }
- }
- // print_r($report_id);die;
- //生成报单奖金池明细
- $pool_detail = new ReportPoolDetail();
- $pool_detail->report_id = $value;
- $pool_detail->store_id = get_store_id();
- $pool_detail->money = $money;
- $pool_detail->pool_level_id = 0;
- $pool_detail->desc = '奖金池明细';
- $pool_detail->created_at = time();
- $pool_detail->pool_id = $pool_info->id;
- $pool_detail->save();
- //执行分红记录逻辑
- $this->addProfit($report_setting,$report_info->user_id,get_store_id(),$value,$money,$pool_info->id,1);
- //审核通过需要把提交的金额记录起来
- $report_commit_integral = ReportCommitIntegral::find()
- ->where(['is_delete'=>0,'store_id'=>get_store_id(),'user_id'=>$report_info->user_id])
- ->andWhere(['=','money',$money])
- ->one();
- if (!$report_commit_integral){
- $new_commit = new ReportCommitIntegral();
- $new_commit->store_id = get_store_id();
- $new_commit->report_id = $report_info->id;
- $new_commit->user_id = $report_info->user_id;
- $new_commit->money = $money;
- $new_commit->is_delete = 0;
- $new_commit->created_at = time();
- $new_commit->save();
- }
- $report_info->status = 1;
- $report_info->audit_time = time();
- $report_info->save();
- }
- }
- if ($status == 2){
- //审核拒绝
- foreach ($report_id as $value) {
- $report_info = ReportInfo::findOne($value);
- $report_info->status = 2;
- $report_info->audit_time = time();
- $report_info->save();
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '审核成功'
- ]);
- }
- /**
- * 执行分红逻辑
- * type 1是执行通过,2是修改奖励
- * @return void
- */
- public function addProfit($report_setting,$user_id,$store_id,$report_id,$money,$pool_id,$type)
- {
- $first_commission = $report_setting->first_commission;
- $second_commission = $report_setting->second_commission;
- $third_commission = $report_setting->third_commission;
- //执行分红逻辑
- //分销内购
- //报单的,肯定获取积分,如果设置了分销比例,对应上级获得分销奖励积分
- $user = User::findOne($user_id);
- if ($user) {
- ReportPoolLevel::addProfitLog($store_id, $pool_id, $money, $user->id, $report_id,'',-1, $type);
- $user_parent = User::findOne($user->old_parent_id);
- if ($user_parent){
- $one_money = bcdiv(bcmul($first_commission,$money), 100, 0);
- ReportPoolLevel::addProfitLog($store_id, $pool_id, $one_money, $user_parent->id, $report_id, '', 0, $type);
- $user_parent_parent = User::findOne($user_parent->old_parent_id);
- if ($user_parent_parent){
- //上级
- $two_money = bcdiv(bcmul($second_commission,$money), 100,0);
- ReportPoolLevel::addProfitLog($store_id, $pool_id, $two_money, $user_parent_parent->id, $report_id, '', 1, $type);
- $user_parent_parent_parent = User::findOne($user_parent_parent->old_parent_id);
- if ($user_parent_parent_parent){
- //上上级
- $three_money = bcdiv(bcmul($third_commission,$money), 100,0);
- ReportPoolLevel::addProfitLog($store_id, $pool_id, $three_money, $user_parent_parent_parent->id, $report_id, '', 2, $type);
- }
- }
- }
- }
- }
- /**
- * 积分记录
- */
- public function actionIntegralLog(){
- $store_id = get_store_id();
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $audit_start_time = get_params('audit_start_time');
- $audit_end_time = get_params('audit_end_time');
- $name = get_params('nikename');
- $mobile = get_params('binding');
- $status = get_params('status','-1');
- $query = ReportInfo::find()->alias('r')
- ->leftJoin(['u' => User::tableName()], 'u.id=r.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['r.is_delete' => 0, 'r.store_id' => $store_id,'r.status'=>1]);
- if ($start_time) {
- $query->andWhere(['>=', 'r.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'r.created_at', strtotime($end_time)]);
- }
- if ($audit_start_time) {
- $query->andWhere(['>=', 'r.audit_time', strtotime($audit_start_time)]);
- }
- if ($audit_end_time) {
- $query->andWhere(['<=', 'r.audit_time', strtotime($audit_end_time)]);
- }
- if ($name) {
- $query->andWhere([
- 'or',
- ['like', 'su.name', $name],
- ['like', 'u.nickname', $name],
- ]);
- }
- if ($mobile) {
- $query->andWhere([
- 'or',
- ['like', 'su.mobile', $mobile],
- ['like', 'u.binding', $mobile],
- ]);
- }
- if ($audit_start_time) {
- $query->andWhere(['>=', 'r.updated_at', strtotime($audit_start_time)]);
- }
- if ($audit_end_time) {
- $query->andWhere(['<=', 'r.updated_at', strtotime($audit_end_time)]);
- }
- if ($status == 1) { //已发放
- $query->andWhere(['r.is_send' =>1]);
- }
- if ($status == 2) { // 未发放
- $query->andWhere(['r.is_send' => 0]);
- }
- $query->distinct()->orderBy('r.status ASC,r.created_at DESC')
- ->select([
- 'r.*',
- 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar',
- 'u.nickname', 'u.avatar_url', 'u.binding'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- //查询分销情况
- //查询一级
- $report_pool_level_one = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>0])->one();
- $user_one = User::findOne($report_pool_level_one->user_id);
- $value['first_name'] = $user_one['nickname'];
- $value['first_avatar'] = $user_one['avatar_url'];
- $value['first_money'] = $report_pool_level_one['money'];
- //查询二级
- $report_pool_level_two = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>1])->one();
- $user_two = User::findOne($report_pool_level_two->user_id);
- $value['second_name'] = $user_two['nickname'];
- $value['second_avatar'] = $user_two['avatar_url'];
- $value['second_money'] = $report_pool_level_two['money'];
- //查询三级
- $report_pool_level_three = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>2])->one();
- $user_three = User::findOne($report_pool_level_three->user_id);
- $value['third_name'] = $user_three['nickname'];
- $value['third_avatar'] = $user_three['avatar_url'];
- $value['third_money'] = $report_pool_level_three['money'];
- //提交时间
- $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
- //审核时间
- $value['audit_time'] = $value['audit_time']?date('Y-m-d H:i:s',$value['audit_time']):'';
- //发放时间
- $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- /**
- * 修改奖励
- *
- */
- public function actionEditReward(){
- $report_id = post_params('report_id');
- $money = post_params('money');
- if (empty($report_id) || !is_array($report_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- //print_r($report_id);die();
- $t = \Yii::$app->db->beginTransaction();
- foreach ($report_id as $value) {
- // print_r($value);die;
- $report_info = ReportInfo::findOne($value);
- if (!$report_info){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '该报单不存在'
- ]);
- }
- if ($report_info->is_send == 1){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '报单已发放奖励'
- ]);
- }
- // print_r($report_info);die;
- $report_info->bonus_points = $money;
- $report_info->audit_time = time();
- $report_info->save();
- $report_detail = ReportPoolDetail::find()->where(['report_id'=>$value,'is_delete'=>0])->one();
- $report_detail->money = $money;
- $report_detail->save();
- //审核通过需要根据报单配置进行积分分配
- $report_setting = ReportSetting::find()->where(['store_id'=>get_store_id()])->one();
- if (!$report_setting){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请先配置全域分红配置'
- ]);
- }
- //查询本期奖金池
- $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
- ->andWhere(['<=','start_time',time()])
- ->andWhere(['>=','end_time',time()])
- ->one();
- if (!$pool_info){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池错误'
- ]);
- }
- //执行分红记录逻辑
- $this->addProfit($report_setting,$report_info->user_id,get_store_id(),$value,$money,$pool_info->id,2);
- //审核通过需要把提交的金额记录起来
- $report_commit_integral = ReportCommitIntegral::find()
- ->where(['is_delete'=>0,'store_id'=>get_store_id(),'user_id'=>$report_info->user_id,'report_id'=>$report_info->id])
- ->andWhere(['=','money',$money])
- ->one();
- if (!$report_commit_integral){
- $new_commit = new ReportCommitIntegral();
- $new_commit->store_id = get_store_id();
- $new_commit->report_id = $report_info->id;
- $new_commit->user_id = $report_info->user_id;
- $new_commit->money = $money;
- $new_commit->is_delete = 0;
- $new_commit->created_at = time();
- $new_commit->save();
- }else{
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '修改失败'
- // ]);
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '修改成功'
- ]);
- }
- /**
- * 报单奖金池
- */
- public function actionReportBonusPool() {
- $store_id = get_store_id();
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $send_start_time = get_params('send_start_time');
- $send_end_time = get_params('send_end_time');
- $status = get_params('status');
- $query = ReportPool::find()->alias('r')
- ->where(['r.store_id' => $store_id]);
- if ($start_time) {
- $query->andWhere(['>=', 'r.start_time', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'r.end_time', strtotime($end_time)]);
- }
- if ($send_start_time) {
- $query->andWhere(['>=', 'r.send_time', strtotime($send_start_time)]);
- }
- if ($send_end_time) {
- $query->andWhere(['<=', 'r.send_time', strtotime($send_end_time)]);
- }
- if ($status == 1) { //已发放
- $query->andWhere(['r.is_send' => 1]);
- }
- if ($status == 2) { //未发放
- $query->andWhere(['r.is_send' =>0]);
- }
- $query->distinct()->orderBy('r.created_at DESC')
- ->select([
- 'r.*'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- //表单内容
- $value['audit_info'] = Json::decode($value['audit_info']);
- //生成时间
- $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
- //开始时间
- $value['start_time'] = $value['start_time']?date('Y-m-d H:i:s',$value['start_time']):'';
- //结束时间
- $value['end_time'] = $value['end_time']?date('Y-m-d H:i:s',$value['end_time']):'';
- //发放时间
- $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
- //查询参与人数
- $report_info_num = ReportPoolDetail::find()
- ->alias('rp')
- ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = rp.report_id')
- ->where(['rp.pool_id'=>$value['id'],'rp.is_delete'=>0])
- ->andWhere(['=','ri.status',1])
- ->groupBy('ri.user_id')
- ->count()??0;
- $value['report_info_num']= $report_info_num;
- //查询累计积分
- $report_bonus_sum = ReportPoolLevel::find()
- ->where(['pool_id'=>$value['id'],'is_delete'=>0])
- ->sum('money')??0;
- $value['report_bonus_sum']= $report_bonus_sum;
- //查询奖金池金额(是后台设置的)
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- /**
- * 报单奖金池提交记录
- */
- public function actionReportCommitLog() {
- $store_id = get_store_id();
- $report_id = get_params('report_id');
- $info = ReportInfo::find()
- ->where(['store_id' => $store_id,'id'=>$report_id])->one();
- if (!$info){
- return $this->asJson([
- 'code' => 0,
- 'data' => []
- ]);
- }
- $query = ReportCommitIntegral::find()->where(['user_id'=>$info->user_id,'is_delete'=>0]);
- $query->orderBy('money asc')
- ->select([
- '*'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- /**
- * 设置奖金池金额
- */
- public function actionSetBonusPoolMoney()
- {
- $store_id = get_store_id();
- $money = post_params('money',0);
- $pool_id = post_params('pool_id',0);
- $t = \Yii::$app->db->beginTransaction();
- if (!$money) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池奖励不能为0'
- ]);
- }
- $report_pool = ReportPool::findOne(['store_id' => $store_id,'id'=>$pool_id]);
- if (!$report_pool){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池不存在'
- ]);
- }
- if ($report_pool->is_send == 1){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池已发放不能设置金额'
- ]);
- }
- $report_pool->reward_amount = $money;
- if (!$report_pool->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '设置失败'
- ]);
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '设置成功'
- ]);
- }
- /**
- * 奖金池明细
- */
- public function actionBonusPoolDetail()
- {
- $store_id = get_store_id();
- $pool_id = get_params('pool_id',0);
- if (!$pool_id){
- return $this->asJson([
- 'code' => 0,
- 'msg' => '奖金池id不能为空'
- ]);
- }
- //查询奖金池信息
- $pool_info = ReportPool::findOne(['store_id' => $store_id,'id'=>$pool_id]);
- // print_r($pool_id);die();
- //查询参与人数
- $report_info_num = ReportPoolDetail::find()
- ->alias('rp')
- ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = rp.report_id')
- ->where(['rp.pool_id'=>$pool_id,'rp.is_delete'=>0])
- ->andWhere(['=','ri.status',1])
- ->groupBy('ri.user_id')
- ->count()??0;
- //查询累计积分
- $report_bonus_sum = ReportPoolLevel::find()
- ->where(['pool_id'=>$pool_id,'is_delete'=>0])
- ->sum('money')??0;
- //查询发放金额
- $report_dividend_money_sum = ReportPoolLevel::find()
- ->where(['pool_id'=>$pool_id,'is_delete'=>0])
- ->andWhere(['is_send'=>1])
- ->sum('dividend_money')??0;
- //查询设置金额
- $bonus_money = $pool_info->reward_amount??0;
- //发放时间
- $send_time = $pool_info->send_time?date('Y-m-d H:i:s',$pool_info->send_time):'';
- //奖池周期
- $cycle_start = date('Y-m-d H:i:s',$pool_info->start_time);
- $cycle_end = date('Y-m-d H:i:s',$pool_info->end_time);
- //报单名称
- //用户名称
- //获得积分 每个报单三个用户获得的积分
- //奖励金额 奖金池金额 /总积分 * 个人积分 10 / 100 * 5 = 50金额
- $query = ReportPoolLevel::find()->alias('r')
- ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = r.report_id')
- ->where(['r.store_id' => $store_id,'r.pool_id'=>$pool_id,'r.is_delete'=>0]);
- $query->orderBy('r.created_at DESC')
- ->select([
- 'r.*','ri.order_no'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- //生成时间
- $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
- //发放时间
- $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
- //获取用户信息'u.nickname', 'u.avatar_url', 'u.binding
- $user_info = User::find()->where(['id'=>$value['user_id']])->asArray()->one();
- $value['nickname'] = $user_info['nickname'];
- $value['binding']= $user_info['binding'];
- $value['avatar_url']= $user_info['avatar_url'];
- //查询总积分
- $report_bonus_sum = ReportPoolLevel::find()
- ->where(['pool_id'=>$value['pool_id'],'is_delete'=>0])
- ->sum('money')??0;
- //查询获得积分
- $level_money = $value['money'];
- $reward_amount = 0;
- // print_r($value['id']);die;
- //奖励计算公式 奖励金额 /总积分 * 个人奖励积分
- if ($bonus_money && $report_bonus_sum && $level_money){
- //奖励金额计算
- // print_r($level_money);die;
- $reward_amount = round(($bonus_money / $report_bonus_sum * $level_money ),2) ;
- }
- $value['reward_amount'] = $reward_amount;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'report_info_num'=>$report_info_num, //查询参与人数
- 'report_bonus_sum'=>$report_bonus_sum, //查询累计积分
- 'report_dividend_money_sum'=>$report_dividend_money_sum, //查询发放金额
- 'bonus_money'=>$bonus_money,//发放前设置金额
- 'is_send'=>$pool_info->is_send,//发放前设置金额
- 'cycle_start'=>$cycle_start,
- 'cycle_end'=>$cycle_end,
- 'send_time'=>$send_time,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- /**
- * 手动发放奖金池
- */
- public function actionSendBonusPool()
- {
- $store_id = get_store_id();
- $pool_id = get_params('pool_id',0);
- //判断是否发放
- $pool_info = ReportPool::find()->where(['id'=>$pool_id])->one();
- if ($pool_info->is_send == 1){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池已发放'
- ]);
- }
- //print_r($pool_info);die;
- //判断是否设置奖金
- if ($pool_info->reward_amount < 0.1){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池未设置金额'
- ]);
- }
- //判断是否有报单
- $report_info_num = ReportInfo::find()->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'status'=>1])->one();
- if (!$report_info_num){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '奖金池无报单'
- ]);
- }
- //判断是否到分红周期时间
- // $bonus_pool_end = $pool_info->end_time;
- // if ($bonus_pool_end > time()){
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '奖金池未到发放周期'
- // ]);
- // }
- //发放奖励到每个level中
- //查看所有奖励记录的积分
- $report_bonus_sum = ReportPoolLevel::find()
- ->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'is_send'=>0])
- ->sum('money');
- $report_level_list = ReportPoolLevel::find()
- ->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'is_send'=>0,'is_delete'=>0])
- ->asArray()
- ->all();
- $t = \Yii::$app->db->beginTransaction();
- foreach ($report_level_list as $key=>$value){
- // 奖励金额 /总积分 * 个人奖励积分
- //奖励金额计算
- $reward_amount = 0;
- if ($pool_info->reward_amount && $report_bonus_sum && $value['money']){
- //奖励金额计算
- $reward_amount = round(($pool_info->reward_amount/$report_bonus_sum * $value['money']),2);
- }
- //发放奖励到用户表当中记录
- $user = User::findOne($value['user_id']);
- $user->global_money_total += $reward_amount;
- $user->global_money +=$reward_amount;
- if (!$user->save()){
- $t->rollBack();
- return $this->asJson([
- 'code'=>1,
- 'msg'=>'发放失败'
- ]);
- }
- $report_level_info = ReportPoolLevel::findOne($value['id']);
- $report_level_info->is_send = 1;
- $report_level_info->dividend_money = $reward_amount;
- $report_level_info->send_time = time();
- if (!$report_level_info->save()){
- $t->rollBack();
- return $this->asJson([
- 'code'=>1,
- 'msg'=>'发放失败'
- ]);
- }
- }
- //奖金池也设置已完成
- ReportPool::updateAll(['is_send'=>1,'send_time'=>time()],['id'=>$pool_info->id]);
- //报单列表更新已完成
- ReportInfo::updateAll(['is_send'=>1,'send_time'=>time()],['pool_id'=>$pool_info->id,'is_delete'=>0]);
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '发放成功'
- ]);
- }
- /**
- * 提现列表
- * @return \yii\web\Response
- */
- public function actionCashList() {
- $store_id = get_store_id();
- $start_time = get_params('start_time');
- $end_time = get_params('end_time');
- $status = get_params('status');
- $name = get_params('name');
- // $binding = get_params('binding');
- $audit_start_time = get_params('audit_start_time');
- $audit_end_time = get_params('audit_end_time');
- $query = Cash::find()->alias('c')
- ->leftJoin(['u' => User::tableName()], 'u.id=c.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $store_id]);
- if ($start_time) {
- $query->andWhere(['>=', 'c.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'c.created_at', strtotime($end_time)]);
- }
- if ($name) {
- $query->andWhere([
- 'or',
- ['like', 'su.name', $name],
- ['like', 'u.nickname', $name],
- ]);
- }
- // if ($binding) {
- // $query->andWhere([
- // 'or',
- // ['like', 'su.mobile', $binding],
- // ['like', 'u.binding', $binding],
- // ]);
- // }
- if ($audit_start_time) {
- $query->andWhere(['>=', 'c.updated_at', strtotime($audit_start_time)]);
- }
- if ($audit_end_time) {
- $query->andWhere(['<=', 'c.updated_at', strtotime($audit_end_time)]);
- }
- if ($status == Cash::STATUS_APPLY) { //待审核
- $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
- }
- if ($status == Cash::STATUS_CONFIRM) { // 待打款
- $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
- }
- if ($status == Cash::STATUS_GIVEN) { // 已打款
- $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
- }
- if ($status == Cash::STATUS_REFUSE) { // 已拒绝
- $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
- }
- $query->andWhere(['c.cash_type' => 17]);
- $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
- 'c.*', 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar', 'u.nickname', 'u.avatar_url', 'u.binding'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
- $value['money'] = Cash::getServiceMoney($value);
- $value['status_name'] = Cash::getCashStatusName($value);
- $saas_user = SaasUser::findOne($value['saas_user_id']);
- $value['card_no'] = '';
- // if (intval($value['type']) === Cash::TYPE_BANK) {
- if ($saas_user->withdraw_method) {
- $withdraw_method = json_decode($saas_user->withdraw_method, true);
- if (!empty($withdraw_method)) {
- foreach ($withdraw_method as $method_item) {
- if ($method_item['type'] === 'bank_card') {
- $value['card_no'] = $method_item['card_no'];
- }
- }
- }
- }
- $value['created_at'] = date('Y-m-d H:i:s',$value['created_at']);
- $value['pay_time'] = !empty($value['pay_time']) ?? date('Y-m-d H:i:s',$value['pay_time']);
- $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s',$value['updated_at']) : '';
- // }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ]);
- }
- public function actionReportDetail()
- {
- $report_id = get_params('report_id');
- if (!$report_id){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- $report_info = ReportInfo::find()->where(['id'=>$report_id])->asArray()->one();
- $user_info = User::findOne($report_info['user_id']);
- $report_info['created_at'] = date('Y-m-d H:i:s', $report_info['created_at']);
- $report_info['audit_time'] = $report_info['audit_time']?date('Y-m-d H:i:s', $report_info['audit_time']):'';
- if ($report_info['status'] == 0 ){
- //待审核
- $report_info['status_text'] = '待审核';
- }else if ($report_info['status'] == 1 ){
- //审核成功
- $report_info['status_text'] = '审核成功';
- }else if ($report_info['status'] == 2){
- //审核失败
- $report_info['status_text'] = '审核失败';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'report_info'=> $report_info,
- 'nickname'=> $user_info->nickname,
- 'avatar_url'=> $user_info->avatar_url,
- 'binding'=> $user_info->binding,
- ],
- ]);
- }
- private function export($list) {
- $rows = [[
- 'ID',
- '会员信息',
- '会员等级',
- '手机号',
- '申请提现金额(元)',
- '手续费(元)',
- '实际打款(元)',
- '姓名',
- '提现到',
- '账户',
- '身份证号',
- '开户行',
- '支行',
- '状态',
- '申请时间',
- '审核时间',
- ]];
- foreach($list as $item){
- $r = [
- $item['id'],
- $item['nickname'],
- $item['level_name'],
- $item['binding'],
- $item['price'],
- $item['service_money'],
- $item['money'],
- $item['name'],
- Cash::$type[$item['type']] ?? '--',
- $item['mobile'],
- $item['card_no'],
- $item['bank_name'],
- $item['bank_branch'],
- Cash::$status[$item['status']] ?? '',
- date('Y-m-d H:i:s', $item['created_at']),
- $item['updated_at'] <= 0 ? '' : date('Y-m-d H:i:s', $item['updated_at']),
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- /**
- * 提现申请审核
- * @return \yii\web\Response
- */
- public function actionCashApply()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- // if (empty($id) || !is_array($id)) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '数据格式错误'
- // ]);
- // }
- // print_r($store_id);die;
- $id = [$id];
- $t = \Yii::$app->db->beginTransaction();
- foreach ($id as $value) {
- $cash = Cash::findOne(['id' => $value, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id]);
- if (!$cash) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- \Yii::$app->cache->set('report_cash_cache_' . $value, $cash->order_no);
- if (!in_array($status, [1, 3])) {
- \Yii::$app->cache->set('report_cash_cache_' . $value, false);
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录ID: ' . $value . '已审核,请刷新重试'
- ]);
- }
- $cash->status = $status;
- if ($status == Cash::STATUS_REFUSE) {
- $user = User::findOne($cash->user_id);
- $user->global_money += $cash->price;
- if (!$user->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常'
- ]);
- }
- // NoticeSend::CashFail($cash->user_id, $user->binding, $cash->price, '提现被驳回', '提现被驳回');
- }
- if (!$cash->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '审核成功'
- ]);
- }
- /**
- * 打款
- * @return \yii\web\Response
- */
- public function actionCashConfirm()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $cash = Cash::findOne(['id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- if ($cash->status != 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,请刷新重试'
- ]);
- }
- $store = Store::findOne($store_id);
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- if($store->is_platform_transfers == 1){
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- }
- $res = [];
- $price = ($cash->price - ($cash->price * ($cash->service_charge / 100)));
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- if ((int)$cash->type === 0) {
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $user = User::findOne($cash->user_id);
- $wechat_open_id = $user->wechat_open_id;
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账'
- ];
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ((int)$cash->type === 1) {
- $order = (object)[
- 'store_id' => $cash->store_id,
- 'order_no' => $cash->order_no,
- 'pay_price' => $price,
- 'name' => $cash->name
- ];
- $result = Alipay::transfer($order, $cash->mobile);
- if (isset($result['code']) && $result['code'] == 1) {
- if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
- $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
- }
- if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
- $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
- }
- if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
- $result['msg'] = '商户余额不足';
- }
- return $this->asJson($result);
- }
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::TYPE_ALIPAY;
- $cash->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }elseif ((int)$cash->type === 4) {
- $user = User::findOne($cash->user_id);
- $saas = SaasUser::findOne(['mobile' => $user->binding]);
- $lg_info = Lg::find()->where(['user_id'=>$saas->id,'status'=>1,'is_delete'=>0])->one();
- $lgApi = new LgApi($cash->store_id);
- //灵工提现
- $post_data = [
- 'store_id' => $cash->store_id,
- 'outTradeNo' => $cash->order_no,//唯一批次号
- 'accNo' => bcmul($price,100),
- 'amt' => bcmul($price,100),
- 'name' => $lg_info->name,
- 'certCard' => $lg_info->cert_card//身份证号
- ];
- $result = $lgApi->FlexiblePay($post_data);
- if (isset($result['status']) && $result['status'] == 999) {
- return $this->asJson($result);
- }
- $cash->status = 6;//灵工待打款
- $cash->save();
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id'=>$cash->id,'store_id'=>$store_id,'type'=>17,'retry' => 5]), 60);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- if ((int)$cash->type === 3) {
- AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '全域分佣提现打款');
- }
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- $res['result_code'] = "SUCCESS";
- // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ]);
- }
- }
- }
|