// +---------------------------------------------------------------------- namespace Admin\Controller; use User\Api\UserApi as UserApi; use function GuzzleHttp\Psr7\str; /** * 后台首页控制器 * @author 麦当苗儿 */ class TimingController extends AdminController { public function _initialize(){ } public function caculateDeviceInfo() { $start = strtotime(date("Ymd",time()))-86400; $end = $start+1; // if (I('time')) { // $start = strtotime(I('time')); // $end = $start+1; // } if (I('start')&&I('end')) { $start = strtotime(I('start')); $end = strtotime(I('end'))+1; } else if (I('start')&&!I('end')) { $start = strtotime(I('start')); $end = strtotime(I('start'))+1; } else if (!I('start')&&I('end')) { $start = strtotime(I('end')); $end = strtotime(I('end'))+1; } if ($start>$end) { echo '开始时间不能大于结束时间'; } ini_set('memory_limit','1024M'); // $device = D('DeviceRecord'); // echo 1; for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) { echo date("Y-m-d",$startTime).' 安卓 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; //当日活跃设备 $data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>1]); echo $data['active_device'].' '; $data['time'] = $startTime; $data['version'] = 1; $data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',1)); // echo M('device_record','tab_')->_sql()."\n"; $data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1],'active',5,'time,version')); // echo M('device_record','tab_')->_sql()."\n"; $data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1])); $data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1])); $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]); $newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false); $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data); } echo date("Y-m-d",$startTime).' 苹果 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; //当日活跃设备 $data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>2]); echo $data['active_device'].' '; $data['time'] = $startTime; $data['version'] = 2; $data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',2)); // echo M('device_record','tab_')->_sql();die(); $data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2],'active',5,'time,version')); $data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2])); $data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2])); // $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1]),'version'=>2]); // $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $data['duration7'] = 0; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data); } } } public function caculateTodayDevice() { $start = strtotime(date("Ymd",time())); $end = $start+1; if (I('start')&&I('end')) { $start = strtotime(I('start')); $end = strtotime(I('end'))+1; } else if (I('start')&&!I('end')) { $start = strtotime(I('start')); $end = strtotime(I('start'))+1; } else if (!I('start')&&I('end')) { $start = strtotime(I('end')); $end = strtotime(I('end'))+1; } if ($start>$end) { echo '开始时间不能大于结束时间'; } ini_set('memory_limit','1024M'); // $device = D('DeviceRecord'); // echo 1; for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) { echo date("Y-m-d",$startTime).' 安卓 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); $data['time'] = $startTime; echo $data['new_device'].' '; $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]); $newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false); $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $data['version'] = 1; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data); } $data['duration7'] = 0; echo date("Y-m-d",$startTime).' 苹果 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; $data['version'] = 2; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data); } } } public function duration($map=array()) { return M('device_record','tab_')->where($map)->sum('duration'); } public function active($map=array()) { $sql = M('device_record','tab_')->field('create_time,model,unique_code')->where($map)->select(false); $sql = M('device_record','tab_')->table('(' . $sql . ') as a')->group('unique_code')->order('create_time')->select(false); $sql = "select count(*) as count from ({$sql})t"; $count = M('device_record','tab_')->query($sql); return $count[0]['count']; } public function news_on_time($map=array(),$field='news',$flag=1,$group='time',$order='time',$version = 1) { switch($flag) { case 2:{$dateform = '%Y-%m';};break; case 3:{$dateform = '%Y-%u';};break; case 4:{$dateform = '%Y';};break; case 5:{$dateform = '%Y-%m-%d %H';};break; default:$dateform = '%Y-%m-%d'; } $sql = M('device_record','tab_')->field('min(create_time) as create_time,version,unique_code,id')->where(['version'=>$version])->group('unique_code')->select(false); $news = M('device_record','tab_')->table('(' . $sql . ') as a') ->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field) ->where($map)->group($group)->order($order)->select(); // echo M('device_record','tab_')->_sql();die(); return $news; } public function active_on_time($map=array(),$field='active',$flag=1,$group='time',$order='time') { switch($flag) { case 2:{$dateform = '%Y-%m';};break; case 3:{$dateform = '%Y-%u';};break; case 4:{$dateform = '%Y';};break; case 5:{$dateform = '%Y-%m-%d %H';};break; default:$dateform = '%Y-%m-%d'; } $sql = M('device_record','tab_')->field('create_time,version,unique_code,id')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' . $sql . ') as m')->group('unique_code')->select(false); $active = M('device_record','tab_')->table('(' . $mid . ') as a') ->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field) ->group($group)->order($order)->select(); // echo $this->_sql();die(); return $active; } public function model($map=array()) { $sql = M('device_record','tab_')->field('create_time,model,version,unique_code')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select(false); return M('device_record','tab_')->table('(' .$mid. ') as a')->field('a.model,version,count(a.unique_code) as count')->group('model,version')->order('count desc')->select(); } public function modelDevice($map=array()) { $sql = M('device_record','tab_')->field('model,version,unique_code')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select(); return $mid; } //格式 starttime 20200229 endtime 20200230 public function setAvailableBalance() { if (strtotime($_GET['endtime'])field("sum(`pay_amount`) as pay_amount,promote_id,promote_account,tab_spend.game_id,tab_spend.game_name,tab_spend.server_id,tab_spend.server_name,CASE WHEN old_value_ratio is NULL THEN 0 ELSE sum(`pay_amount`) * old_value_ratio END as available_balance") ->join('left join tab_resource_verify_config as config on tab_spend.game_id = config.game_id and config.status = 1') ->where(['pay_status'=>1,'pay_way'=>['egt',1],'pay_time'=>['between',[$start,$start+86399]]]) ->group('promote_id,game_id,server_id') ->select(); // dump($data);die(); foreach ($data as $key => $value) { $insert['promote_id'] = $value['promote_id']; $insert['promote_account'] = $value['promote_account']; $insert['game_id'] = $value['game_id']; $insert['game_name'] = $value['game_name']; $insert['available_balance'] = round($value['available_balance'],0); $insert['server_id'] = $value['server_id']; $insert['server_name'] = $value['server_name']; $availableBalanceData = M('promote_game_available_balance','tab_') ->where(['promote_id'=>$value['promote_id'],'game_id'=>$value['game_id'],'server_id'=>$value['server_id']]) ->find(); if (round($value['available_balance'],0) > 0 && !$availableBalanceData) { M('promote_game_available_balance','tab_')->add($insert); } else if (round($value['available_balance'],0) > 0 && $availableBalanceData) { M('promote_game_available_balance','tab_') ->where(['promote_id'=>$value['promote_id'],'game_id'=>$value['game_id'],'server_id'=>$value['server_id']]) ->setInc('available_balance',round($value['available_balance'],0)); } // dump(round($value['available_balance'],0)); } } echo '执行成功'; } public function getTogetherUserAccount() { if (I('user_id','')) { $user_id = I('user_id',''); } else { return false; } $data = M('user','tab_')->field('account')->where(['id'=>$user_id])->find(); // var_dump($data); if (!$data) { return false; } $this->ajaxReturn($data); } public function caculateMarketList() { $map = []; $map['my_time'] = date('Y-m',strtotime('-1 month')); if ($_REQUEST['count_date']) { $map['my_time'] = $_REQUEST['count_date']; } if ($_REQUEST['count_date'] == 'all') { $map['my_time'] = ['neq', date('Y-m',time())]; } $spendMap['tab_spend.pay_status'] = 1; //获取spend表中的数据,同时根据会长id进行group分类 $data = M('Spend','tab_') ->field("FROM_UNIXTIME(pay_time,'%Y-%m') as my_time,sum(pay_amount) as pay_amount,game_id,tab_spend.game_name,SUBSTRING_INDEX(tab_spend.`game_name`,\"(\",1) as game_names,promote_id,promote_account,company_relation,company_belong,CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1)='' THEN promote_id ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1) END as root_id,tab_game.ratio,tab_game.relation_game_id,tab_spend.sdk_version ") ->join("left join tab_promote on promote_id = tab_promote.id") ->join("left join tab_game on tab_game.id=tab_spend.game_id") ->where($spendMap) ->group("my_time,game_id,root_id") ->order("my_time Desc") ->select(false); //关联表获取会长账号名 $data = M()->table('('.$data.') as a') ->field("my_time,pay_amount,a.game_id,game_name,game_names,a.promote_id,a.promote_account,a.company_relation,a.company_belong,CASE WHEN root_id is null THEN 0 ELSE root_id END as root_id,CASE WHEN account is null THEN '官方渠道' ELSE account END as account,tab_ratio.ratio as ratio,turnover_ratio,a.ratio as game_ratio,a.sdk_version") ->join("left join tab_promote on root_id = tab_promote.id") ->join("left join tab_promote_game_ratio as tab_ratio on tab_ratio.game_id = a.relation_game_id and tab_ratio.promote_id=root_id and tab_ratio.status=1") ->where($map) ->group("my_time,game_id,root_id") ->order("my_time Desc") ->select(); // dump($data);die(); foreach($data as $key => $value) { echo "{$value['my_time']}-{$value['promote_account']}-{$value['game_name']}:{$value['pay_amount']}\n"; $mytime = $value['my_time']; $nextMonth = date('Y-m',strtotime("$mytime + 1 month")); $game_id = $value['game_id']; $gameInfo = M('game','tab_')->where(['id'=>$game_id])->getField('game_name'); $gameName = substr($gameInfo,0,strpos($gameInfo,'(')); $gameMap['game_name'] = ['like','%'.$gameName.'%']; $gameIdArr = M('game','tab_')->field('id')->where($gameMap)->select(); $gameNameIdArr = []; foreach($gameIdArr as $gameKey => $gameValue){ $gameNameIdArr[] = $gameValue['id']; } $promote_id = $value['promote_id']; $pay_status = 1; $promoteInfo = M('promote','tab_')->where(['id'=>$promote_id])->find(); $level = $promoteInfo['level']; if($promote_id != 0) { $promoteIdArr = []; if ($level == 1) { $rootPromoteId = $promote_id; $promoteIdArr[] = $promote_id; }else { $chain = $promoteInfo['chain']; $chainArr = explode('/',$chain); $rootPromoteId = $chainArr[1]; } $where1['chain'] = array('like', '%/' . $rootPromoteId. '/%'); $promoteIdInfo = M('promote','tab_')->field('id')->where($where1)->select(); foreach($promoteIdInfo as $key2 => $v1) { $promoteIdArr[] = $v1['id']; } $where['promote_id'] = ['in',$promoteIdArr]; if (!$promoteIdArr) { $where['promote_id']= 0; } }else { $where['promote_id'] = 0; } $where['game_id'] = ['in',$gameNameIdArr]; $where['pay_status'] = 1; $begTime = strtotime($mytime); $endTime = strtotime($nextMonth); $where['pay_time'] = ['between',[$begTime,$endTime]]; $spendInfo = M('spend','tab_')->field('pay_amount,pay_way')->where($where)->select(); $initNum = 0; foreach($spendInfo as $key1 => $spendValue) { $pay_way = $spendValue['pay_way']; $channel_rate = $this->getChannelConfig($pay_way) / 100; $initNum += $spendValue['pay_amount'] * $channel_rate; } $data[$key]['pay_amount'] = $value['pay_amount'] - $initNum; $data[$key]['upstream'] = caculateUpstream($value['game_id'],$value['pay_amount']); if ($value['ratio']) { $data[$key]['downstream'] = $value['pay_amount'] * ($value['ratio']*0.01); } else { $data[$key]['downstream'] = $value['pay_amount'] * ($value['game_ratio']*0.01); } $value['turnover_ratio'] = json_decode($value['turnover_ratio'],true); // dd($value); if (is_array($value['turnover_ratio'])) { foreach($value['turnover_ratio'] as $k =>$v) { if(($value['pay_amount']>=$v['turnover']&&$v['instanceof']==1) || $value['pay_amount']>$v['turnover']) { $data[$key]['downstream'] = $value['pay_amount'] * ($v['ratio']*0.01); } } } $data[$key]['highquality'] = number_format(($value['pay_amount'] - $initNum - $data[$key]['upstream']-$data[$key]['downstream']) * (1-0.0672),2,'.',''); if (!$data[$key]['company_belong']) { $data[$key]['company_belong'] = getCompanyBlong(0); } else { $data[$key]['company_belong'] = getCompanyBlong($data[$key]['company_belong']); } $data[$key]['company_relation'] = $data[$key]['company_relation']?'只维护':'自主开发及维护'; } foreach($data as $key => $value) { $is_hav = M('market_balance','tab_')->where(['my_time'=>$value['my_time'], 'game_id'=>$value['game_id'], 'promote_id'=>$value['promote_id'], 'root_id'=>$value['root_id']])->find(); if (!$is_hav) { M('market_balance','tab_')->add($value); } else { M('market_balance','tab_') ->where(['my_time'=>$value['my_time'], 'game_id'=>$value['game_id'], 'promote_id'=>$value['promote_id'], 'root_id'=>$value['root_id']]) ->save(['pay_amount'=>$value['pay_amount'], 'ratio'=>$value['ratio'], 'turnover_ratio'=>$value['turnover_ratio'], 'game_ratio'=>$value['game_ratio'], 'upstream'=>$value['upstream'], 'downstream'=>$value['downstream'], 'highquality'=>$value['highquality']]); } } echo 'success'; // dump($data);die(); } public function getChannelConfig($pay_way = 0,$config=false) { if(!$config) { $payWayName = $this->pay_way_name[$pay_way]; $toolConfig = M('tool','tab_')->where(['name'=>$payWayName])->find(); $config = json_decode($toolConfig['config'],true); if(empty($config['channel_rate'])){ $channel_rate = 0; }else { $channel_rate = $config['channel_rate']; } }else { foreach($this->pay_way_name as $key => $value) { $name = $value; $toolConfig = M('tool','tab_')->where(['name'=>$name])->find(); $config = json_decode($toolConfig['config'],true); if(empty($config['channel_rate'])){ $channel_rate[$key] = 0; }else { $channel_rate[$key] = $config['channel_rate']; } } } return $channel_rate; } public function setPromoteStatistics() { echo "-------------------------------------------------------------\n"; $startTime = strtotime("20190829"); if (I("start_time")=="yesterday") { $startTime = strtotime(date("Y-m-d",strtotime("-1 day"))); } $endTime = time(); if (I("start_time")) { $startTime = strtotime(I("start_time")); } if (I("end_time")) { $endTime = strtotime(I("end_time")); } if ($endTime < $startTime) { echo "错误,开始时间不得大于结束时间";die(); } for($start = $startTime;$start<$endTime;$start=$start+86400) { $map['pay_time'] = ['between', array($start, $start+86399)]; $map['tp1.chain'] = '/'; $map['pay_status'] = 1; $map['promote_id'] = array('egt', 0); $data = M('promote', 'tab_')->alias('tp1') ->field('tp1.account as promote_account,tp1.id as promote_id,floor(sum(pay_amount)*100) as count') ->join("tab_promote AS tp2 ON tp2.`chain` LIKE CONCAT('%/', tp1.id, '/%') OR tp2.id = tp1.id", 'left') ->join("tab_spend as s on tp2.id = s.promote_id", 'left') ->where($map) ->group('tp1.id') ->order('count desc') ->select(); foreach($data as $key => $value) { $insert['promote_id'] = $value['promote_id']; $insert['promote_account'] = $value['promote_account']; $insert['count'] = $value['count']?$value['count']:0; $insert['time'] = $start; $statisticsData = M("promote_statistics","tab_") ->where(['promote_id'=>$value['promote_id'],'time'=>$start]) ->find(); $date = date("Y-m-d",$start); if ($statisticsData) { M("promote_statistics","tab_") ->where(['promote_id'=>$value['promote_id'],'time'=>$start]) ->save($insert); echo "更新{$value['promote_account']}在{$date}充值:{$value['count']}\n"; } else { M("promote_statistics","tab_")->add($insert); echo "新增{$value['promote_account']}在{$date}充值:{$value['count']}\n"; } } } echo "-------------------------------------------------------------\n"; } public function shiftPromoteStatistics() { $data = M("shift_task")->where(['change_status'=>0,'type'=>2])->select(); $promote_data = []; $change_id = []; foreach ($data as $key => $value) { $promote_data[] = $value['from_promote_id']; $promote_data[] = $value['to_promote_id']; $change_id[] = $value['id']; } $promote_data=array_unique($promote_data); sort($promote_data); if(!$promote_data) { echo "没有数据"; die(); } $chain_data = M("promote","tab_") ->field("id") ->where(['chain'=>'/','id'=>['in',$promote_data]]) ->select(); $chain_data = array_column($chain_data,'id'); $chain_data_secord = M("promote","tab_") ->field("substring_index(chain, '/', 2) as id") ->where(['id'=>['in',$promote_data]]) ->select(); $chain_data_secord = array_column($chain_data_secord,'id'); foreach($chain_data_secord as $k=>$v){ if($v == '/'){ unset($chain_data_secord[$k]); } $chain_data_secord[$k] = substr($chain_data_secord[$k],1,strlen($v)); if ($chain_data_secord[$k] == false) { unset($chain_data_secord[$k]); } // var_dump(strlen($chain_data_secord[$k])); } $data = []; $data = array_merge(array_unique($chain_data),array_unique($chain_data_secord)); $this->PromoteStatistics($data); M("shift_task")->where(['id'=>['in',$change_id]])->save(['change_status'=>1]); // dump($data); // dump(array_unique($chain_data_secord)); } public function PromoteStatistics($promote) { echo "-------------------------------------------------------------\n"; $startTime = strtotime("20190829"); $endTime = time(); if (I("start_time")) { $startTime = strtotime(I("start_time")); } if ($promote) { $map['tp2.id'] = ['in',$promote]; } if (I("end_time")) { $endTime = strtotime(I("end_time")); } if ($endTime < $startTime) { echo "错误,开始时间不得大于结束时间";die(); } for($start = $startTime;$start<$endTime;$start=$start+86400) { $map['pay_time'] = ['between', array($start, $start+86399)]; $map['tp1.chain'] = '/'; $map['pay_status'] = 1; $map['promote_id'] = array('egt', 0); $data = M('promote', 'tab_')->alias('tp1') ->field('tp1.account as promote_account,tp1.id as promote_id,floor(sum(pay_amount)*100) as count') ->join("tab_promote AS tp2 ON tp2.`chain` LIKE CONCAT('%/', tp1.id, '/%') OR tp2.id = tp1.id", 'left') ->join("tab_spend as s on tp2.id = s.promote_id", 'left') ->where($map) ->group('tp1.id') ->order('count desc') ->select(); foreach($data as $key => $value) { $insert['promote_id'] = $value['promote_id']; $insert['promote_account'] = $value['promote_account']; $insert['count'] = $value['count']?$value['count']:0; $insert['time'] = $start; $statisticsData = M("promote_statistics","tab_") ->where(['promote_id'=>$value['promote_id'],'time'=>$start]) ->find(); $date = date("Y-m-d",$start); if ($statisticsData) { M("promote_statistics","tab_") ->where(['promote_id'=>$value['promote_id'],'time'=>$start]) ->save($insert); echo "更新{$value['promote_account']}在{$date}充值:{$value['count']}\n"; } else { M("promote_statistics","tab_")->add($insert); echo "新增{$value['promote_account']}在{$date}充值:{$value['count']}\n"; } } } echo "-------------------------------------------------------------\n"; } function getPayway($name) { switch ($name){ case 'alipay': //支付宝 1 return 1; case 'wei_xin': //微信支付 2 3 return 2; case 'heepay': //汇付宝 6 return 6; case 'sqpay': //双乾支付9,10,15 return 9; case 'yeepay': //易宝支付17 return 17; case 'ptb_pay': return 0; default: return ''; } } /** * 获取下游公司的特殊补点金额 * @param int $start_time * @param int $end_time * @return array */ public function getSpecialComplement($start_time=0,$end_time =0,$promote_id = 0) { $data = M("company_statement","tab_")->where("statement_begin_time<={$start_time} and statement_end_time<={$end_time} and withdraw_type=3")->select(); $return = []; foreach ($data as $key => $value) { $statement_info = json_decode($value['statement_info'],true); $is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$value['company_id']}'")->find()['is_inside']; if ($value['company_type'] == 2) { foreach ($statement_info as $skey => $sval) { $info = $sval['game_list']; foreach ($info as $k => $v) { // if (isset($return[$statement_info['promote_id'].$v['relation_game_id']])) { $return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] = !$is_inside?$v['sum_money']:0; } else { $return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] += !$is_inside?$v['sum_money']:0; } } } } else { //判断公司比例是不是隶属于这个会长 $compamy_info = M("promote","tab_")->where(['id'=>$promote_id,'company_id'=>$value['company_id']])->find(); if ($compamy_info) { foreach ($statement_info as $k => $v) { $statement_begin_time = strtotime(str_replace('.','-',$v['statement_begin_time'])); $statement_end_time = strtotime(str_replace('.','-',$v['statement_end_time']))+86399; $amount_time['tab_spend.pay_status'] = 1; $amount_time['pay_way'] = ['egt',0]; $amount_time['_string'] = "payed_time between {$statement_begin_time} and {$statement_end_time} and relation_game_id={$v['relation_game_id']}"; $dataAmount = M("spend","tab_") ->field("pay_amount,relation_game_id,relation_game_name,payed_time,game_type_name,substring_index(substring_index(promote.`chain`,'/',2),'/',-1) promote_id,chain,admin_id") ->join("left join tab_promote promote on tab_spend.promote_id=promote.id") ->join("left join tab_game game on tab_spend.game_id=game.id") ->where($amount_time) ->select(false); $dataAmount = M()->table("({$dataAmount})a") ->field("a.*,sum(pay_amount) pay_amount,promote.account,member.real_name") ->join("left join tab_promote promote on promote_id=promote.id") ->join("left join sys_member member on promote.admin_id=member.uid ") ->where(['promote_id'=>$promote_id]) ->group("promote_id") ->find(); $return[$value['company_id']."-".$promote_id."-".$v['relation_game_id']] += !$is_inside?(($dataAmount['pay_amount']*$v['increment_ratio'])/100):0; } } } } return $return; } public function test() { $this->getSpecialComplement(strtotime(date("Y-m-1",strtotime("2020-07"))),strtotime(date("Y-m-t",strtotime("2020-07")))+86399,2259); } // $admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$date ='',$pay_amount = 0 public function getCompanyGameRadio($admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$date ='',$pay_amount = 0) { // $company_id=273; // $relation_game_id=213; // $pay_amount = 440; // $date = "2020-07"; // $admin_id = 50; // $promote_id = 3711; $begintime = strtotime(date("Y-m-1",strtotime($date))); $endtime = strtotime(date("Y-m-t",strtotime($date)))+86399; $is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$company_id}'")->find()['is_inside']; if ($is_inside == 1) { return 0; } $company_game_ratio = M("company_game_ratio","tab_") ->where(['company_id'=>$company_id,'relation_game_id'=>$relation_game_id]) ->where("(end_time>={$begintime} and (end_time<={$endtime} and end_time!=0)) or (begin_time>={$begintime} and begin_time<={$endtime}) or (begin_time<={$begintime} and (end_time>={$endtime} or end_time=0))") ->order("begin_time ASC") ->select(); $company_data = M('promote_company','tab_') ->field("company_belong") ->where("id=$company_id") ->find(); $game_ratio_mould = M("game_ratio_mould","tab_") ->where(['relation_game_id'=>$relation_game_id,'company_belong'=>$company_data['company_belong']]) ->find(); $mould = json_decode($game_ratio_mould['turnover_ratio'],true); $amount = 0; if (!$company_game_ratio) { $spend = $this->getSpendData($begintime,$endtime,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount); } foreach ($company_game_ratio as $key => $value) { $turnover_ratio = json_decode($value['turnover_ratio'],true); if($value['begin_time'] > $begintime) { if ($key == 0) { $spend = $this->getSpendData($begintime,$value['begin_time']-1,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount); } if ($value['end_time']<$endtime&&($value['end_time']!=0)) { $spend = $this->getSpendData($value['begin_time'],$value['end_time'],$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount); } else { $spend = $this->getSpendData($value['begin_time'],$endtime,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount); break; } } elseif ($value['begin_time']<=$begintime) { $value['begin_time']=$begintime; if ($value['end_time']<$endtime&&($value['end_time']!=0)) { $spend = $this->getSpendData($value['begin_time'],$value['end_time'],$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount); } else { $spend = $this->getSpendData($value['begin_time'],$endtime,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount); break; } } //缺片段的情况下用模板比例补充 if ($company_game_ratio[$key+1]['begin_time'] != $value['end_time']+1 && $company_game_ratio[$key+1]) { $spend = $this->getSpendData($value['end_time']+1,$company_game_ratio[$key+1]['begin_time']-1,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount); } //如果最后还有缺失的片段再用模板补齐 if(!$company_game_ratio[$key+1]&&$endtime>$value['end_time']) { $spend = $this->getSpendData($value['end_time']+1,$endtime,$company_id,$relation_game_id,$admin_id,$promote_id); $amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount); } } return $amount?$amount:0; } public function setSpendData($spend=[],$turnover_ratio=[],$radio=0,$pay_amount=0) { $turnover = 0; $amount= 0; foreach ($spend as $k => $v) { //获取的金额 $amount = ($v['pay_amount'] * ($radio?$radio:0))/100; foreach ( $turnover_ratio as $tk => $tv ) { //判断大于的流水是否比前一个要小 if ($turnover >= $tv['turnover']) { continue; } if ($tv['instanceof']==1) { if ($pay_amount >= $tv['turnover']) { $amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100; $turnover = $tv['turnover']; } } else { if ($pay_amount > $tv['turnover']) { $amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100; $turnover = $tv['turnover']; } } } } return $amount; } public function getSpendData($begintime = 0,$endtime = 0,$company_id=0,$relation_game_id=0,$admin_id=0,$promote_id=0) { $map['tab_spend.pay_status'] = 1; $map['pay_way'] = ['egt',0]; $spend = M("spend","tab_") ->field("sum(pay_amount) pay_amount, CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE promote_id END promote_id,game.relation_game_id,company_id") ->join("left join tab_game game on tab_spend.game_id = game.id") ->join("left join tab_promote promote on tab_spend.promote_id = promote.id") ->where($map) ->where(['payed_time'=>['between',[$begintime,$endtime]],'company_id'=>$company_id,'relation_game_id'=>$relation_game_id,'market_admin_id'=>$admin_id]) ->group("tab_spend.id") ->select(false); $spend = M()->table("({$spend})a") ->field("*,sum(pay_amount) pay_amount") ->where(['promote_id'=>$promote_id]) ->group("promote_id") ->select(); return $spend; } //计算市场专员业绩公式 参数 time 2020-10 public function caculateMarketStream () { echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); if ($_REQUEST['time']) { $map['pay_time'] = $_REQUEST['time']; } $spend_map = []; if($_REQUEST['time'] == 'all') { $map = []; $spend_map['pay_time'] = ['elt',strtotime(date('Y-m-t 23:59:59', strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')))]; } $data = M("auth_group") ->where(['title'=>['like','%市场%']]) ->select(false); $data = M("auth_group_access") ->field("auth.market_percentage,real_name,sys_member.uid,sys_auth_group_access.group_id") ->join("left join ({$data}) auth on auth.id = sys_auth_group_access.group_id") ->join("left join sys_member on sys_member.uid = sys_auth_group_access.uid") ->where("auth.title is not null") ->select(false); $spend = M("spend","tab_") ->field("sum(pay_amount) as pay_amount,FROM_UNIXTIME(pay_time,'%Y-%m') pay_time, substring_index(substring_index(tab_spend.game_name,'(',1),'(',-1) game_name,game_id,relation_game_id,tab_spend.sdk_version, CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE promote_id END promote_id,pay_way,market_admin_id") ->join("left join tab_promote on tab_spend.promote_id = tab_promote.id") ->join("left join tab_game game on game.id=tab_spend.game_id") ->where(['tab_spend.pay_status'=>1,'pay_way'=>['egt',0]]) ->where($spend_map) ->group("tab_spend.id") ->select(false); // $desposit = M("deposit","tab_") // ->field("sum(pay_amount) as pay_amount,FROM_UNIXTIME(payed_time,'%Y-%m') pay_time, // substring_index(substring_index(tab_pay_info.game_name,'(',1),'(',-1) game_name,game_id,relation_game_id,tab_deposit.sdk_version, // CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE tab_pay_info.promote_id END promote_id,tab_deposit.pay_way,market_admin_id") // ->join("inner join tab_pay_info on tab_pay_info.order_id=tab_deposit.pay_order_number") // ->join("left join tab_promote on tab_deposit.promote_id = tab_promote.id") // ->join("left join tab_game game on game.id=tab_pay_info.game_id") // ->where(['tab_deposit.pay_status'=>1,'tab_deposit.pay_way'=>['egt',1],'pay_source'=>2]) // ->where($spend_map) // ->group("tab_deposit.id") // ->select(false); // // $spend = "{$spend} UNION ALL {$desposit}"; // var_dump($desposit);die(); $payway_spend = M()->table("({$spend}) spend") ->field("sum(pay_amount) as pay_amount,pay_time,promote_id,game_name,game_id,relation_game_id,pay_way,sdk_version,market_admin_id") ->where($map) ->group("pay_time,promote_id,game_name,pay_way,market_admin_id") ->select(); $channel_fee = []; $paywayData = M("tool","tab_") ->field("name,config") ->where(['name'=>['in',"alipay,weixin,wei_xin,wei_xin_app,jubaobar,weixin_gf,jft,jft_wap,goldpig,ptb_pay,bind_pay,sqpay,heepay,yeepay,ptb_pay"]]) ->select(); //公司税费计算 $tax_radio = []; $tax_data = M("tax_radio","tab_") ->field("tax_radio,time") ->order("time ASC") ->select(); foreach ($tax_data as $key => $value) { $tax_radio[$key]['start'] = $value['time']; $tax_radio[$key]['end'] = $tax_data[$key+1]['time']; $tax_radio[$key]['tax_radio'] = $value['tax_radio']; if (!$tax_data[$key+1]['time']) { $tax_radio[$key]['end'] = 99999999999; } } $pay_rate = []; foreach ($paywayData as $key => $value) { $pay_config = json_decode($value['config'],true); if ($this->getPayway($value['name'])||$this->getPayway($value['name'])=='0') { $pay_rate[$this->getPayway($value['name'])] = $pay_config['channel_rate']?$pay_config['channel_rate']:0; } } foreach ($payway_spend as $key => $value) { switch ($value['pay_way']){ case 3: //微信同步 $value['pay_way'] = '2'; break; case 10: //双乾同步 $value['pay_way'] = '9'; break; case 15: //双乾同步 $value['pay_way'] = '9'; break; } //将时间、推广员id、游戏名、设备类型作为键值标记渠道费用 if (!$channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['market_admin_id']]) { if (!$value['pay_way']) { $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['market_admin_id']] = 0; } $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['market_admin_id']] = $value['pay_amount'] * ($pay_rate[$value['pay_way']]/100); } else { $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['market_admin_id']] = $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['market_admin_id']]+($value['pay_amount'] * ($pay_rate[$value['pay_way']]/100)); } } $spend = M()->table("({$spend}) spend") ->field("sum(pay_amount) as pay_amount,pay_time,promote_id,game_name,game_id,relation_game_id,sdk_version,market_admin_id") ->where($map) ->group("pay_time,promote_id,game_name,market_admin_id") ->select(false); $promote_data = M()->table("({$spend}) spend") ->field("pay_time,company_id,company.company_name,account as promote_account,company.company_belong,develop_type,spend.promote_id,spend.game_name,game_id,relation_game_id,spend.pay_amount,auth.real_name,market_admin_id as admin_id,market_percentage,sdk_version") ->join("left join tab_promote promote on spend.promote_id = promote.id") ->join("left join tab_promote_company company on promote.company_id = company.id") ->join("left join ({$data}) auth on auth.uid=market_admin_id") ->where(['develop_type'=>['egt',1],'group_id'=>['in',['11','12','21']]]) ->select(); // // dump($tax_radio);die(); $specialPayAmount = []; foreach ($promote_data as $key => $value) { $cp_radio = getGameCpRadio($value['game_id'],$value['pay_amount'],true); if (!$cp_radio) { $cp_radio = getGameCpRadio($value['game_id'],$value['pay_amount'],false); } //上游流水 $promote_data[$key]['cp_amount'] = $value['pay_amount'] * ($cp_radio * 0.01); //下游 // $promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],true,$value['company_belong']); // if (!$promote_radio) { // $promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],false,$value['company_belong']); // } //下游流水 $promote_data[$key]['promote_amount'] = $this->getCompanyGameRadio($value['admin_id'],$value['promote_id'],$value['company_id'],$value['relation_game_id'],$value['pay_time'],$value['pay_amount']); //下游流水 // $promote_data[$key]['promote_amount'] = $value['pay_amount'] * ($promote_radio * 0.01); //特殊补点 $specialPayAmount[$value['pay_time']] = $this->getSpecialComplement(strtotime(date("Y-m-1",strtotime($value['pay_time']))),strtotime(date("Y-m-t",strtotime($value['pay_time'])))+86399,$value['promote_id']); $specialAmount = $specialPayAmount[$value['pay_time']][$value['company_id']."-".$value['promote_id']."-".$value['relation_game_id']]?$specialPayAmount[$value['pay_time']][$value['company_id']."-".$value['promote_id']."-".$value['relation_game_id']]:0; //渠道费用 $promote_data[$key]['channel_amount'] = $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['admin_id']]; $tax = 0; //公司税费 foreach($tax_radio as $k => $v) { if (strtotime($value['pay_time'])>=$v['start'] && strtotime($value['pay_time'])<$v['end']) { $tax = $v['tax_radio']; } } $promote_data[$key]['company_tax'] = ($value['pay_amount']-$promote_data[$key]['cp_amount']- $promote_data[$key]['promote_amount'])*($tax/100); //毛利 $promote_data[$key]['company_profit'] = $value['pay_amount']-$promote_data[$key]['cp_amount']-$promote_data[$key]['promote_amount']-$promote_data[$key]['channel_amount']-$promote_data[$key]['company_tax']-$specialAmount; $promote_data[$key]['promote_amount'] += $specialAmount; if ($value['promote_id'] == 0) { // $promote_data[$key]['promote_account']='官方渠道'; // $promote_data[$key]['company_id']='0'; // $promote_data[$key]['company_belong']='3'; // $promote_data[$key]['develop_type']='0'; continue; } unset($promote_data[$key]['market_percentage']); $marker_data = M("settleup_marketorder","tab_") ->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id']]) ->find(); echo "日期:{$value['pay_time']},游戏:{$value['game_name']},推广员:{$value['promote_account']}\n"; echo "市场员:{$value['admin_id']} 推广员:{$value['promote_id']} 公司id:{$value['company_id']} 游戏关联id:{$value['relation_game_id']} 支付时间:{$value['pay_time']} 金额:{$value['pay_amount']}\n"; // dump($promote_data[$key]);die(); if (!$marker_data) { M("settleup_marketorder","tab_") ->add($promote_data[$key]); } else { M("settleup_marketorder","tab_") ->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id']]) ->save($promote_data[$key]); } } // var_dump($_REQUEST['time']); $this->createMarketBonus($_REQUEST['time']); echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; } public function insertAllUserBonus($time) { // $time = $_REQUEST['time']; $data = M("member") ->field("access.uid admin_id,real_name,nickname,group_id level") ->join("left join sys_auth_group_access access on sys_member.uid = access.uid") ->join("left join sys_auth_group auth on access.group_id=auth.id") ->where(['group_id'=>['in','11,12,21']]) ->select(); foreach ($data as $key => $value) { $value['pay_time'] = $time; $value['remark'] = ""; $value['create_time'] = time(); $sign = M("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time]) ->find(); if (!$sign) { M("market_altogether","tab_")->add($value); } elseif(!$sign['real_name']) { M("market_altogether","tab_")->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])->save(['real_name'=>$value['real_name']]); } } } public function createMarketBonus($pay_time) { if ($pay_time&&$pay_time!='all') { $map['pay_time'] = $pay_time; } if (!$pay_time) { $pay_time = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); } if ($pay_time == 'all') { $end = date("Y-m",time()); $start = '2019-08'; $i = 0; do { $date = date('Y-m', strtotime($start . ' + ' . $i . ' month')); $i++; $this->insertAllUserBonus($date); } while ($date < date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01'))); // $map['pay_time'] = []; unset($map['pay_time']); } else{ $this->insertAllUserBonus($pay_time); } $all_data = M("settleup_marketorder","tab_") ->field("tab_settleup_marketorder.id,pay_time,promote_account,company_belong,tab_settleup_marketorder.real_name,develop_type,game_name, sum(pay_amount) pay_amount,sum(company_profit) company_profit,group_id,market_percentage,nickname,admin_id, sum(if((develop_type = 1 or develop_type = 2), pay_amount, 0)) performance_revenue,sum(if(develop_type = 3, pay_amount, 0)) appraisal_bonuses, sum(company_tax) company_tax, sum(cp_amount) cp_amount,sum(promote_amount) promote_amount") ->join("left join sys_auth_group_access access on admin_id=access.uid") ->join("left join sys_auth_group auth on access.group_id=auth.id") ->join("left join sys_member member on access.uid=member.uid") ->where(['group_id'=>['in','11,12,21']]) ->where($map) ->group("admin_id,pay_time") ->select(); foreach ($all_data as $key => $value) { $sum_amount = $value['performance_revenue'] + $value['appraisal_bonuses']; $market_percentage = json_decode($value['market_percentage'],true);//业绩提成 if ($value['performance_revenue']=='0.00') { $market_percentage['develop_commission_ratio'] = 0; $market_percentage['appraisal_bonuses_ratio'] = 0; } if ($value['appraisal_bonuses']=='0.00') { $market_percentage['maintain_commission_ratio'] = 0; $market_percentage['maintain_appraisal_bonuses_ratio'] = 0; } // echo "{$value['nickname']}:{$value['pay_time']}\n"; // dump($market_percentage); $settleup_marketorder = M("settleup_marketorder","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']]) ->select(); $all_data[$key]['performance_commission'] = 0; $all_data[$key]['achievement_bonus'] = 0; foreach ($settleup_marketorder as $k => $v) { if($v['develop_type'] == 1 || $v['develop_type'] == 2) { $all_data[$key]['performance_commission'] += $v['company_profit']*($market_percentage['develop_commission_ratio']/100); $all_data[$key]['achievement_profit'] += $v['company_profit']; } if ($v['develop_type'] == 3) { $all_data[$key]['performance_commission'] += $v['company_profit']*($market_percentage['maintain_commission_ratio']/100); $all_data[$key]['performance_profit'] += $v['company_profit']; } //绩效考核奖金 if($v['develop_type'] == 1 || $v['develop_type'] == 2) { $all_data[$key]['achievement_bonus'] +=$v['company_profit']*($market_percentage['appraisal_bonuses_ratio']/100); } if ($v['develop_type'] == 3) { $all_data[$key]['achievement_bonus'] += $v['company_profit']*($market_percentage['maintain_appraisal_bonuses_ratio']/100); } } $all_data[$key]['performance_commission'] = number_format($all_data[$key]['performance_commission'], 2, '.', ''); $all_data[$key]['achievement_bonus'] = number_format($all_data[$key]['achievement_bonus'], 2, '.', ''); $sum_amount <$market_percentage['performance_revenue'] ? ($all_data[$key]['performance_commission'] = 0):""; $sum_amount <$market_percentage['performance_revenue'] ? ($all_data[$key]['achievement_profit'] = 0):""; $sum_amount <$market_percentage['appraisal_bonuses'] ? ($all_data[$key]['achievement_bonus'] = 0):""; $sum_amount <$market_percentage['appraisal_bonuses'] ? ($all_data[$key]['performance_profit'] = 0):""; //总提成 $all_data[$key]['commission'] = number_format($all_data[$key]['performance_commission']+$all_data[$key]['achievement_bonus'], 2, '.', ''); //应发金额 $all_data[$key]['extend_commission'] = substr(number_format($all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100)+ $all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100), 4, '.', ''),0,-2); //待发金额 $all_data[$key]['wait_commission'] = substr(number_format($all_data[$key]['commission'] - ( $all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100)+ $all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100) ), 4, '.', ''),0,-2); $insert['pay_time'] = $value['pay_time']; $insert['real_name'] = $value['real_name']; $insert['admin_id'] = $value['admin_id']; $insert['nickname'] = $value['nickname']; $insert['level'] = $value['group_id']; $insert['pay_amount'] = $value['pay_amount']; $insert['extend_commission'] = substr( number_format(($all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100) + $all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100)), 4, '.', ''),0,-2); $insert['wait_commission'] = $all_data[$key]['wait_commission']; $insert['remark'] = ''; $insert['create_time'] = time(); $insert['company_profit'] = $value['company_profit']; $insert['company_tax'] = $value['company_tax']; $insert['cp_amount'] = $value['cp_amount']; $insert['promote_amount'] = $value['promote_amount']; $insert['performance_revenue'] = $value['performance_revenue']; $insert['appraisal_bonuses'] = $value['appraisal_bonuses']; $insert['performance_commission'] = $all_data[$key]['performance_commission']; $insert['achievement_bonus'] = $all_data[$key]['achievement_bonus']; //待发业绩奖金 $insert['wait_performance_commission'] = number_format($all_data[$key]['performance_commission'] * (1-($market_percentage['performance_monthly_ratio']/100)), 2, '.', ''); //待发绩效奖金 $insert['wait_achievement_commission'] = number_format($all_data[$key]['achievement_bonus'] * (1-($market_percentage['month_bonus_ratio']/100)), 2, '.', ''); $insert['achievement_profit'] = number_format($all_data[$key]['achievement_profit'], 2, '.', ''); $insert['performance_profit'] = number_format($all_data[$key]['performance_profit'], 2, '.', ''); // $insert['wait_commission'] = ($insert['wait_performance_commission']+$insert['wait_achievement_commission']); $is_hav = M("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']]) ->find(); if (!$is_hav) { M("market_altogether","tab_")->add($insert); } else { M("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']]) ->save($insert); } } } public function sendMarketBonus(){ echo date("Y-m-d H:i:s")."----------------------月初发放市场专员应发份额----------------------\n"; $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); if ($_REQUEST['time']) { $map['pay_time'] = $_REQUEST['time']; } if ($_REQUEST['time'] == 'all') { unset($map['pay_time']); } $data = M("market_altogether","tab_") ->where($map) ->select(); // dump($map);die(); foreach ($data as $key => $value) { $group_config = M("auth_group_access access") ->field("market_percentage") ->join("left join sys_auth_group auth on access.group_id=auth.id") ->where(['uid'=>$value['admin_id']]) ->find(); $group_config = json_decode($group_config['market_percentage'],true); $month = date("m",strtotime($value['pay_time'])); $yes = date("Y",strtotime($value['pay_time'])); $where =[]; if ($group_config['time'] == 1) { // var_dump($month);die(); if ($month == '07') { $where = [$yes.'-'.'01',$yes.'-'.'02',$yes.'-'.'03']; } elseif($month == '10') { $where = [$yes.'-'.'04',$yes.'-'.'05',$yes.'-'.'06']; } elseif($month == '04') { $where = [($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12']; } elseif($month == '01') { $where = [($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09']; } } elseif($group_config['time'] == 2) { if ($month == '07') { $where = [$yes.'-'.'01',$yes.'-'.'02',$yes.'-'.'03',$yes.'-'.'04',$yes.'-'.'05',$yes.'-'.'06']; } elseif($month == '01') { $where = [($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09',($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12']; } } elseif($group_config['time'] == 3) { if ($month == '01') { $where = [($yes-1).'-'.'01',($yes-1).'-'.'02',($yes-1).'-'.'03',($yes-1).'-'.'04',($yes-1).'-'.'05',($yes-1).'-'.'06', ($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09',($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12']; } } else { $where = [date("Y-m",strtotime("last month",strtotime($value['pay_time'])))]; } echo "{$value['pay_time']}:{$value['real_name']}发放\n"; $save['status'] = 1; $save['remark'] = '自动发放'; if ($where) { $map['pay_time'] = ['in',$where]; $map['admin_id'] = $value['admin_id']; M("market_altogether","tab_") ->where($map) ->save($save); } } echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; } }