You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1672 lines
70 KiB
PHP

<?php
namespace Admin\Controller;
use Admin\Model\RewardModel;
use User\Api\UserApi as UserApi;
/**
* 推广查询控制器
* @author 王贺
*/
class StatementMangementController extends ThinkController
{
const INSIDE_COMPANY = [
"0"=>"其他",
"1"=>"市场部",
"2"=>"运营部"
];
public function lists() {
$map = [];
if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
$map['create_time'] = ['between', [strtotime(I('time_start')), strtotime(I('time_end')) + 86399]];
} elseif (isset($_REQUEST['time_start'])) {
$map['create_time'] = ['GT', strtotime(I('time_start'))];
} elseif (isset($_REQUEST['time_end'])) {
$map['create_time'] = ['LT', strtotime(I('time_end')) + 86399];
}
if ($_REQUEST['verify_status']||$_REQUEST['verify_status']=='0') {
$map['verify_status'] = $_REQUEST['verify_status'];
}
if ($_REQUEST['statement_type']||$_REQUEST['statement_type']=='0') {
if($_REQUEST['statement_type']=='2') {
$map['second_party_info'] = ['like','%"order_type":1%'];
} else {
$map['statement_type'] = $_REQUEST['statement_type'];
if ($_REQUEST['statement_type']=='1') {
$map['second_party_info'] = ['like','%"order_type":0%'];
}
}
// dump($map['second_party_info']);die();
}
// var_dump($map);die();
if ($_REQUEST['company_id']) {
$map['company_name'] = $_REQUEST['company_id'];
// if ($_REQUEST['company_id']=='万盟天下科技') {
// $map['company_name'] = '海南万盟天下科技有限公司';
// }
}
$data = M("statement","tab_")
->where($map)
->order("create_time Desc")
->select();
// var_dump($data);die();
foreach ($data as $key => $value) {
$data[$key]['order'] = json_decode($value['second_party_info'],true)['order_type'];
}
$sumData = M("statement","tab_")
->field('sum(`statement_money`) as sum_money,statement_type')
->where($map)
->group('statement_type')
->select();
$handleSumData = [];
foreach ($sumData as $key => $value) {
$handleSumData[$value['statement_type']] = $value['sum_money'];
}
if (!$handleSumData[1]) {
$handleSumData[1] = 0;
}
if (!$handleSumData[0]) {
$handleSumData[0] = 0;
}
$this->checkListOrCountAuthRestMap($map,[]);
$this->assign('sum',$handleSumData);
// dump($sumData);die();
foreach($data as $key => $value) {
$data[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
$data[$key]['statement_begin_time'] = date('Y-m-d H:i:s',$value['statement_begin_time']);
$data[$key]['statement_end_time'] = date('Y-m-d H:i:s',$value['statement_end_time']+86399);
// $data[$key]['is_confirm'] = $data[$key]['is_confirm']?'确认':'未确认';
// $data[$key]['statement_type'] = $value['statement_type']?'公会对账单':'CP对账单';
}
$this->meta_title = '结算单管理';
$this->assign('data',$data);
$this->display();
}
public function createDownstreamOrder() {
$this->display();
}
public function saveDownstreamOrder() {
$second_party_info = $_REQUEST['second_party_info'];
$first_party_info = $_REQUEST['first_party_info'];
$statement_info = $_REQUEST['statement_info'];
$sumData = $_REQUEST['sum_data'];
if ($second_party_info['settlement_type']=='补点订单') {
$second_party_info['order_type'] = 1;
} else {
$second_party_info['order_type'] = 0;
}
if (!$second_party_info||!$first_party_info||!$statement_info||!$statement_info) {
$this->ajaxReturn(['code'=>0, 'msg'=>'数据错误' ]);
}
$insert['statement_type'] = 1;
$insert['company_id'] = $_REQUEST['company_id'];
$insert['company_name'] = $second_party_info['partner'];
$insert['link_phone'] = $second_party_info['link_phone'];;
$statement_begin_time = strtotime($statement_info[0]['statement_begin_time']);
$statement_end_time = strtotime($statement_info[0]['statement_end_time']);
foreach($statement_info as $key => $value) {
if ($statement_begin_time>strtotime($value['statement_begin_time'])&&$value['statement_begin_time']) {
$statement_begin_time = strtotime($value['statement_begin_time']);
}
// var_dump(strtotime($value['statement_begin_time']);
if ($statement_end_time<strtotime($value['statement_end_time'])&&$value['statement_end_time']) {
$statement_end_time=strtotime($value['statement_end_time']);
}
}
$insert['statement_begin_time'] = $statement_begin_time;
$insert['statement_end_time'] = $statement_end_time;
$insert['create_time'] = time();
$insert['statement_money'] =$sumData['statement_money'];
$insert['pay_amount'] = $sumData['pay_amount'];
$insert['ext_field'] = $_REQUEST['ext_field'];
$insert['first_party_info'] = json_encode($first_party_info);
$insert['second_party_info'] = json_encode($second_party_info);
$insert['statement_info'] = json_encode($statement_info);
if (!(M('statement','tab_')->where(['ext_field'=>$_REQUEST['ext_field']])->find())) {
addOperationLog(['op_type'=>0,'key'=>$second_party_info['partner'].'/'.getNowDate(),'op_name'=>'新增下游对账单','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-新增下游对账单']);
M('statement','tab_')->add($insert);
$this->ajaxReturn(['code'=>1, 'msg'=>'生成结算单成功']);
} else {
$this->ajaxReturn(['code'=>0, 'msg'=>'生成结算单失败,结算单已经存在']);
}
}
public function editDownstreamOrder() {
$this->display('edit');
}
public function editDownstreamOrderSave() {
$second_party_info = $_REQUEST['second_party_info'];
$first_party_info = $_REQUEST['first_party_info'];
$statement_info = $_REQUEST['statement_info'];
$sumData = $_REQUEST['sum_data'];
if (!$second_party_info||!$first_party_info||!$statement_info||!$statement_info) {
$this->ajaxReturn(['code'=>0, 'msg'=>'数据错误' ]);
}
$insert['statement_type'] = 1;
$insert['company_id'] = $_REQUEST['company_id'];
$insert['company_name'] = $second_party_info['partner'];
$insert['link_phone'] = $second_party_info['link_phone'];;
$statement_begin_time = strtotime($statement_info[0]['statement_begin_time']);
$statement_end_time = strtotime($statement_info[0]['statement_end_time']);
foreach($statement_info as $key => $value) {
if ($statement_begin_time>strtotime($value['statement_begin_time'])&&$value['statement_begin_time']) {
$statement_begin_time = strtotime($value['statement_begin_time']);
}
// var_dump(strtotime($value['statement_begin_time']);
if ($statement_end_time<strtotime($value['statement_end_time'])&&$value['statement_end_time']) {
$statement_end_time=strtotime($value['statement_end_time']);
}
}
$insert['statement_begin_time'] = $statement_begin_time;
$insert['statement_end_time'] = $statement_end_time;
$insert['create_time'] = time();
$insert['statement_money'] =$sumData['statement_money'];
$insert['pay_amount'] = $sumData['pay_amount'];
$insert['ext_field'] = $_REQUEST['ext_field'];
$insert['first_party_info'] = json_encode($first_party_info);
$insert['second_party_info'] = json_encode($second_party_info);
$insert['statement_info'] = json_encode($statement_info);
if ((M('statement','tab_')->where(['ext_field'=>$_REQUEST['ext_field']])->save($insert))) {
addOperationLog(['op_type'=>1,'key'=>$second_party_info['partner'].'/'.getNowDate(),'op_name'=>'编辑下游对账单','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-新增下游对账单']);
$this->ajaxReturn(['code'=>1, 'msg'=>'结算单编辑成功']);
} else {
$this->ajaxReturn(['code'=>0, 'msg'=>'结算单编辑失败']);
}
}
public function delStatementData() {
$id = $_REQUEST['id'];
if (!$id) {
$this->ajaxReturn(['code'=>2000,'error'=>'no']);
}
$statementData = M('statement','tab_')->where(['id'=>$id])->find();
$is_del = M('statement','tab_')->where(['id'=>$id])->delete();
if ($is_del) {
addOperationLog(['op_type'=>2,'key'=>$statementData['company_name'].'/'.date('YmdHis',$statementData['statement_begin_time']).'/'.date('YmdHis',$statementData['statement_end_time']),'op_name'=>'删除下游对账单','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-删除下游对账单']);
$this->ajaxReturn(['code'=>0,'success'=>'ok']);
} else {
$this->ajaxReturn(['code'=>2000,'error'=>'no']);
}
}
public function rewardManageList($row = 10, $p = 1)
{
$companys[1] = array_column(SM('partner', 'tab_')->field('id, partner as name')->select(), 'name', 'id');
$companys[2] = array_column(SM('promote_company', 'tab_')->field('id, company_name as name')->select(), 'name', 'id');
$games = array_column(SM('game', 'tab_')->field('relation_game_id, relation_game_name, partner_id')->group('relation_game_id')->select(), null, 'relation_game_id');
$map = ' 1=1 ';
$params = $_GET;
$isExport = I('is_export', false);
if (!empty(I('reward_type'))) {
$map .= " and reward_type = {$params['reward_type']}";
}
if (I('game_id', -1) !== '' && I('game_id', -1) >= 0) {
$map .= " and r.relation_game_id = {$params['game_id']}";
}
if (!empty(I('partner_id'))) {
$gameIds = [];
foreach ($games as $item) {
if ($item['partner_id'] == I('partner_id')) {
$gameIds[] = $item['relation_game_id'];
}
}
if ($gameIds) {
$map .= " and r.relation_game_id in (".implode(',', $gameIds).")";
} else {
$map .= " and 1<>1 ";
}
}
if (!empty($params['settlement_type'])) {
$map .= " and rd.settlement_type = {$params['settlement_type']} ";
}
if (!empty($params['detail_reward_type'])) {
$map .= " and rd.type = {$params['detail_reward_type']} ";
}
if (!empty($params['settlement_time_range'])) {
list($settlment_time_start, $settlment_time_end) = explode('~', $params['settlement_time_range']);
$map .= " and r.settlement_time between " . strtotime(trim($settlment_time_start)) . " and " . (strtotime(trim($settlment_time_end))+86400);
}
if (!empty($params['confirm_time_range'])) {
list($confirm_time_start, $confirm_time_end) = explode('~', $params['confirm_time_range']);
$map .= " and r.confirm_time between " . strtotime(trim($confirm_time_start)) . " and " . (strtotime(trim($confirm_time_end))+86400);
}
if (!empty($params['detail_company_type'])) {
$map .= " and rd.company_type = {$params['detail_company_type']} ";
}
if (!empty($params['detail_settlement_type'])) {
$map .= " and rd.settlement_type = {$params['detail_settlement_type']} ";
}
//按部门搜索
// if (!empty($params['detail_company_name'])) {
// $map .= " and rd.company_name like '%{$params['detail_company_name']}%' ";
// }
$selectCompanyId = true;
if(!empty($params['detail_company_type']) && $params['detail_company_type']== 3 && (I('detail_company_id', -1) >= 0)){
$detail_company_name = self::INSIDE_COMPANY[$params['detail_company_id']];
if($detail_company_name == "其他"){
foreach (self::INSIDE_COMPANY as $inside_company_name) {
if($inside_company_name != "其他"){
$map .= " and rd.company_name NOT LIKE '%{$inside_company_name}%' ";
}
}
}else{
$map .= " and rd.company_name like '%{$detail_company_name}%' ";
}
$selectCompanyId = false;
}
// 判断公司
if (I('detail_company_id', -1) >= 0 && $selectCompanyId) {
$map .= " and rd.company_id = {$params['detail_company_id']} ";
// if (!empty($params['detail_company_name'])) {
// $map .= " and rd.company_name = '{$params['detail_company_name']}' ";
// }
}
//判断内外团
if(!empty($params['detail_company_type']) && $params['detail_company_type']== 2 && (I('company_belong', -1) >= 0)){
$company_belong = $params['company_belong'];
$company_id = M("promote_company","tab_")->where("company_belong = '{$company_belong}'")->getField("id",true);
$company_id = implode(",",$company_id);
$detail_company_name = self::INSIDE_COMPANY[$params['detail_company_id']];
$map .= " and rd.company_id in ({$company_id }) ";
}
// 判断会长
if (I('detail_promote_account', -1) >= 0) {
$map .= " and rd.promote_account like ('%{$params['detail_promote_account']}%') ";
}
if (!empty($params['detail_promote_account_input'])) {
$map .= " and rd.promote_account like ('%{$params['detail_promote_account_input']}%') ";
}
//市场部判断
$marketCompanyIds = A("Market","Event")->getAdminCompanyId();
if($marketCompanyIds != 'all'){
$map .= ' and rd.company_id in (' . $marketCompanyIds .')';
}
// dd($map);
$result = SM('reward_record', 'tab_')->alias('r')
->field('r.id')
->join('tab_reward_detail as rd on rd.record_id = r.id', 'LEFT')
->where($map)
->group('r.id')
->order('create_time DESC')
->select();
// dd(M()->getLastSql());
if ($result) {
$map = [];
$allIds = array_column($result, 'id');
// 分页
$count = count($allIds);
$currentIds = I('is_export', false) ? $allIds : array_slice($allIds, ($p-1)*$row, $row);
$map['r.id'] = ['in', $currentIds];
} else {
$map = '1<>1';
}
$list = SM('reward_record', 'tab_')->alias('r')
->field('
r.id, r.relation_game_id, r.reward_time,r.settlement_time, r.confirm_time, r.content, r.reward_type, r.creater_id,r.is_verify,rd.is_check,rd.check_detail,IFNULL(pc.company_belong,0) company_belong,
rd.id as rid, rd.type as detail_type, rd.company_type as detail_company_type, rd.company_id as detail_company_id , rd.company_name as detail_company_name, rd.settlement_type as detail_settlement_type, rd.promote_account as detail_promote_account,rd.amount as detail_amount
')
->where($map)
->join('tab_reward_detail as rd on rd.record_id = r.id', 'LEFT')
->join('tab_promote_company as pc on rd.company_id =pc.id', 'LEFT')
->order('r.create_time DESC,rd.type asc')
->select();
if ($list) {
$record = [];
foreach ($list as $item) {
// $item["verify_detail"] = json_decode($item["verify_detail"],true);
$item["check_detail"] = json_decode($item["check_detail"],true);
if($item["is_verify"] == 0) {
$verify_detail = "未审核";
} elseif($item["is_verify"] == 1) {
$verify_detail="审核通过";
} else{
$verify_detail="审核拒绝";
}
if (!isset($record[$item['id']]))
{
$record[$item['id']] = [
'id' => $item['id'],
'relation_game_name' => $games[$item['relation_game_id']]['relation_game_name'] ?? '其他',
'relation_game_partner' => $companys[1][$games[$item['relation_game_id']]['partner_id']] ?? '无',
'settlement_time' => $item['settlement_time'] ? date('Y年m月', $item['settlement_time']) : '-',
'confirm_time' => $item['confirm_time'] ? date('Y-m-d', $item['confirm_time']) : '-',
'reward_time' => $item['reward_time'] ? date('Y-m-d', $item['reward_time']) : '-',
'content' => $item['content'],
'reward_type_text' => RewardModel::$rewardTypes[$item['reward_type']],
'maxCount' => 0,
'creater_id' => $item['creater_id'],
'totalReward' => 0,
'totalPunish' => 0,
'is_verify'=>$item['is_verify'],
'verify'=>$verify_detail,
'row'=>1
];
}
if (!empty($item['rid'])) {
if($isExport){
$linkStr = '';
}else{
$linkStr = '<br>';
}
if($item["is_check"] == 0) {
$verify_detail = "未审核";
} elseif($item["is_check"] == 1) {
$verify_detail="审核通过"."(".$item["check_detail"]["user"]."){$linkStr}".$item["check_detail"]["time"];
} else{
$verify_detail="审核拒绝"."(".$item["check_detail"]["user"]."){$linkStr}".$item["check_detail"]["time"];
}
$record[$item['id']]['detail'][] = [
'company_type_text' => RewardModel::$companyTypes[$item['detail_company_type']] ?? '-',
'settlement_type_text' => RewardModel::$settlementTypes[$item['detail_settlement_type']] ?? '-',
'company_name' => empty($item['detail_company_name']) && !empty($item['detail_company_id']) ? ($companys[$item['detail_company_type']][$item['detail_company_id']] ?? '无') : ($item['detail_company_name'] ?? '无'),
'amount' => $item['detail_type'] == RewardModel::REWARD_TYPE_PUNISH ? -$item['detail_amount'] : $item['detail_amount'],
'promote_account' => $item['detail_promote_account'] ?? '无',
'company_belong'=>$item['detail_company_type'] == 2 ? getCompanyBlong($item['company_belong']) : "",
'detail_type' =>RewardModel::$rewardTypes[$item['detail_type']],
'is_check'=>$item["is_check"],
'check_detail'=>$verify_detail,
];
$record[$item['id']]['row'] += 1;
if ($item['detail_type'] == RewardModel::REWARD_TYPE_REWARD) {
$record[$item['id']]['totalReward'] += $item['detail_amount'];
} else {
$record[$item['id']]['totalPunish'] += $item['detail_amount'];
}
}
// 获取最大行数
if (sizeof($record[$item['id']]['detail'][$item['detail_type']]) > $record[$item['id']]['maxCount']) {
$record[$item['id']]['maxCount'] = sizeof($record[$item['id']]['detail'][$item['detail_type']]);
}
}
}
$count = sizeof($result);
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
if(IS_SUBSITE) {
$this->assign("IS_SUBSITE",true);
}
// ksort($record);
// dd($record);
$this->assign('list', $record);
// dd($record);
$this->assign('promoters', array_column(SM('promote', 'tab_')->where(['level'=>1])->field('id, nickname, account')->select(), null, 'id'));
$this->assign('company_belong',getCompanyBlong());
$this->checkListOrCountAuthRestMap($map,[]);
if(I('is_export', false)){
$getData = $_GET;
unset($getData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'op_name'=>'导出推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-推广员管理-推广公司管理-导出推广公司']);
//导出
$this->display("rewardExport");
die();
}
$this->display();
}
public function rewardManageSave($id=0)
{
if (IS_POST) {
$data = [
'reward_type' => I('reward_type'),
'company_type' => I('company_type'),
'company_id' => I('company_id'),
'relation_game_id' => I('relation_game_id'),
'money' => I('money'),
'content' => I('content'),
'remark' => I('remark'),
'promote_id' => I('promote_id'),
//'settlement_info' => json_encode(I('settlement_info') ? array_values(I('settlement_info')) : []),
'reward_time' => strtotime(I('reward_time')),
'confirm_time' => strtotime(I('confirm_time')),
'settlement_time' => strtotime(I('settlement_time')),
];
$settlment_info = I('settlement_info');
$accounts = I('account');
$user_types = I('user_type');
// 处理结算信息
if ($settlment_info) {
$tmp = [];
foreach($settlment_info as &$item) {
if (empty($item)) {
unset($item);
continue;
}
// if (!is_numeric($item['price']) || empty($item['price'])) {
// return $this->error('请填写正确的奖惩金额');
// }
$item = json_decode($item, true);
if (!empty($id) && empty($item['id'])) {
$item['record_id'] = $id;
//$item['id'] = null;
}
if (!empty($item['company_id']) && $item['company_type'] !=3 )
{
$item['company_name'] = '';
}
if ( $item['company_type'] ==3 )
{
$item['company_id'] = 0;
}
$item['confirm_time'] = $data['settlement_time'];
//$item['settlement_flag'] = sprintf('%s-%s-%s', $item['company_type'], $item['company_name'], $item[''])
// 归类奖罚数组
$tmp[$item['type']][] = $item;
}
if (empty($tmp)) {
return $this->error('请填写结算信息');
}
$settlment_info = $tmp;
// $promoterIds = array_column($settlment_info, 'promote_id');
// if (in_array(0, $promoterIds)) {
// return $this->error('请选择合作方后再提交');
// }
// if (sizeof(array_unique($promoterIds)) != sizeof($promoterIds)) {
// return $this->error('含有重复的结算方,请确认!');
// }
} else {
return $this->error('请填写结算信息');
}
if ($accounts) {
foreach ($accounts as $index => $account) {
if (trim($account) == '') {
unset($accounts[$index]);
} else if (checkAccount($user_types[$index],$account)==1) {
$data['accounts'][] = array_merge(['account'=>$account], ['user_type'=>$user_types[$index]]);
} else {
return $this->error('账号'.$account.'不存在,请重新输入');
}
// $data['accounts'][] = array_merge(['account'=>$account], ['user_type'=>$user_types[$index]]);
}
} else {
$data['accounts'] = [];
}
$data['accounts']=a_array_unique($data['accounts']);
// dump($data['accounts']);die();
$data['accounts'] = json_encode($data['accounts']);
// if (!is_numeric(I('money'))) {
// return $this->error('请输入正确的金额');
// }
if (empty(I('content'))) {
return $this->error('请填写事件内容');
}
//dd($settlment_info);
// 更新结算信息
if ($id) {
SM('reward_detail', 'tab_')->where(['record_id'=>$id])->delete();
}
if (!empty($settlment_info[RewardModel::REWARD_TYPE_REWARD]) && !empty($settlment_info[RewardModel::REWARD_TYPE_PUNISH])) {
$data['reward_type'] = RewardModel::REWARD_TYPE_ALL;
} else if (!empty($settlment_info[RewardModel::REWARD_TYPE_REWARD])) {
$data['reward_type'] = RewardModel::REWARD_TYPE_REWARD;
} else if (!empty($settlment_info[RewardModel::REWARD_TYPE_PUNISH])) {
$data['reward_type'] = RewardModel::REWARD_TYPE_PUNISH;
}
$settlmentSet = array_merge($settlment_info[RewardModel::REWARD_TYPE_REWARD] ?? [], $settlment_info[RewardModel::REWARD_TYPE_PUNISH] ?? []);
// dump($settlmentSet);die();
if ($id) {
$checkReward = $this->checkRewardDetail($settlmentSet);
if ($checkReward) {
return $this->error("奖惩确认时间需在结算时间以后,请重新选择");
}
$upt = SM('reward_record', 'tab_')->where(['id'=>$id])->save($data);
if ($upt === false) {
$this->error('保存失败');
} else {
foreach ($settlmentSet as $item) {
SM('reward_detail', 'tab_')->add($item);
}
$companyName = SM('partner','tab_')->field('partner')->where(['id'=>$data['company_id']])->find();
addOperationLog(['op_type'=>1,'key'=>$companyName['partner'].'/'.(($data['reward_type']==1)?'奖励':'惩罚').'/'.$data['money'],'op_name'=>'修改奖惩记录','url'=>U('Query/settlement'),'menu'=>'推广员-结算单管理-奖罚记录管理-修改奖惩记录']);
$this->success('保存成功', U('StatementMangement/rewardManageList'));
}
} else {
$checkReward = $this->checkRewardDetail($settlmentSet);
if ($checkReward) {
return $this->error("奖惩确认时间需在结算时间以后,请重新选择");
}
$data['create_time'] = time();
$data['creater_id'] = is_login();
$ins = SM('reward_record', 'tab_')->add($data);
$record_id = SM('reward_record', 'tab_')->getLastInsID();
$this->autoVerify($settlmentSet,$record_id);
foreach ($settlmentSet as $item) {
$item['record_id'] = $record_id;
SM('reward_detail', 'tab_')->add($item);
}
$companyName = SM('partner','tab_')->field('partner')->where(['id'=>$data['company_id']])->find();
addOperationLog(['op_type'=>0,'key'=>$companyName['partner'].'/'.(($data['reward_type']==1)?'奖励':'惩罚').'/'.$data['money'],'op_name'=>'新增奖惩记录','url'=>U('Query/settlement'),'menu'=>'推广员-结算单管理-奖罚记录管理-新增奖惩记录']);
return $ins ? $this->success('新增成功', U('StatementMangement/rewardManageList')) : $this->error('新增失败');
}
} else {
$games = SM('game', 'tab_')->field('relation_game_id, relation_game_name')->group('relation_game_id')->select();
$this->assign('games', $games);
if ($id) {
if (I('delete')) {
$rewardRecordData = SM('reward_record', 'tab_')->where(['id'=>$id])->find();
$upt = SM('reward_record', 'tab_')->where(['id'=>$id])->delete();
SM('reward_detail', 'tab_')->where(['record_id'=>$id])->delete();
$companyName = SM('partner','tab_')->field('partner')->where(['id'=>$rewardRecordData['company_id']])->find();
addOperationLog(['op_type'=>2,'key'=>$companyName['partner'].'/'.(($rewardRecordData['reward_type']==1)?'奖励':'惩罚').'/'.$rewardRecordData['money'],'op_name'=>'删除奖惩记录','url'=>U('StatementMangement/rewardManageList'),'menu'=>'推广员-结算单管理-奖罚记录管理']);
return $upt ? $this->success('删除成功') : $this->error('删除失败');
} else {
$data = SM('reward_record', 'tab_')->where(['id'=>$id])->find();
if (!$data) return $this->error("未找到该记录");
$data['confirm_time'] = $data['confirm_time'] ? date('Y-m-d', $data['confirm_time']) : '';
$data['accounts'] = json_decode($data['accounts'], true) ?: [];
$data['settlement_info'] = json_decode($data['settlement_info'], true) ?: [];
//$this->assign('data', $data);
}
} else {
$data['reward_time'] = strtotime(date('Y-m-01'));
}
$details = SM('reward_detail', 'tab_')->where(['record_id'=>$id])->select();
// 根据type 来合并
foreach ($details as $item) {
$item['info'] = $item;
if (empty($item['company_name']) && $item['company_id']) {
if ($item['company_type'] == RewardModel::COMPANY_TYPE_PARTNER) {
$item['company_name'] = SM('partner', 'tab_')->where(['id'=>$item['company_id']])->getField('partner');
} else {
$item['company_name'] = SM('promote_company', 'tab_')->where(['id'=>$item['company_id']])->getField('company_name');
}
}
$tmp[$item['type']][] = $item;
}
$data['rewardCount'] = sizeof($tmp[RewardModel::REWARD_TYPE_REWARD]);
$data['punishCount'] = sizeof($tmp[RewardModel::REWARD_TYPE_PUNISH]);
$data['rewardAmount'] = $data['rewardCount'] > 0 ? array_sum(array_column($tmp[RewardModel::REWARD_TYPE_REWARD], 'amount')) : 0;
$data['punishAmount'] = $data['punishCount'] > 0 ? array_sum(array_column($tmp[RewardModel::REWARD_TYPE_PUNISH], 'amount')) : 0;
$data['maxCount'] = $data['rewardCount'] > $data['punishCount'] ? $data['rewardCount'] : $data['punishCount'];
$this->assign('details', $tmp);
$this->assign('data', $data);
$this->assign('promoters', SM('promote', 'tab_')->where(['level'=>1])->field('id, nickname')->select());
$this->assign('rewardTypes', RewardModel::$rewardTypes);
$this->assign('settlementTypes', RewardModel::$settlementTypes);
$this->assign('companyTypes', RewardModel::$companyTypes);
$this->display();
}
}
public function checkRewardDetail($settlmentSet = []) {
$pool_data = M("company_statement_pool","tab_")->field('id,statement_num,create_lack_ids,verify_status,is_payment,withdraw_type,statement_begin_time,statement_end_time')
->where([
'verify_status'=>['egt',2],
'withdraw_type'=>['neq',3]
])
->select();
foreach ($settlmentSet as $key => $value) {
if ($value['company_id']) {
foreach ($pool_data as $pkey => $pvalue) {
$infolist = [];
if ($pvalue['verify_status'] != 6) {
//获取基本信息
$infolist = M("company_statement_info", "tab_")->field("*,'1' as st")
// ->where("pool_id = '{$pvalue['id']}'")
->where([
'statement_begin_time' => ['elt', $value['confirm_time']],
'statement_end_time' => ['egt', $value['confirm_time']],
'company_id' => $value['company_id'],
'pool_id'=>$pvalue['id'],
])
->select();
//获取母单
if (!empty($pvalue['create_lack_ids'])) {
$l_ids = explode(',',$pvalue['create_lack_ids']);
$lack_info = M("company_lack_statement_info", "tab_")
->field("*,'0' as st")
// ->where("id in ({$l_ids})")
->where([
'id'=>['in',$l_ids],
'statement_begin_time' => ['elt', $value['confirm_time']],
'statement_end_time' => ['egt', $value['confirm_time']],
'company_id' => $value['company_id'],
])
->select();
$infolist = array_merge($infolist, $lack_info);
}
if ($infolist) {
return true;
}
}
}
}
}
return false;
}
public function rewardManageDetail($id=0)
{
$games = SM('game', 'tab_')->field('relation_game_id, relation_game_name')->group('relation_game_id')->select();
$this->assign('games', $games);
if ($id) {
if (I('delete')) {
$rewardRecordData = SM('reward_record', 'tab_')->where(['id'=>$id])->find();
$upt = SM('reward_record', 'tab_')->where(['id'=>$id])->delete();
SM('reward_detail', 'tab_')->where(['record_id'=>$id])->delete();
$companyName = SM('partner','tab_')->field('partner')->where(['id'=>$rewardRecordData['company_id']])->find();
addOperationLog(['op_type'=>2,'key'=>$companyName['partner'].'/'.(($rewardRecordData['reward_type']==1)?'奖励':'惩罚').'/'.$rewardRecordData['money'],'op_name'=>'删除奖惩记录','url'=>U('StatementMangement/rewardManageList'),'menu'=>'推广员-结算单管理-奖罚记录管理']);
return $upt ? $this->success('删除成功') : $this->error('删除失败');
} else {
$data = SM('reward_record', 'tab_')->where(['id'=>$id])->find();
if (!$data) return $this->error("未找到该记录");
$data['confirm_time'] = $data['confirm_time'] ? date('Y-m-d', $data['confirm_time']) : '';
$data['accounts'] = json_decode($data['accounts'], true) ?: [];
$data['settlement_info'] = json_decode($data['settlement_info'], true) ?: [];
//$this->assign('data', $data);
}
} else {
$data['reward_time'] = strtotime(date('Y-m-01'));
}
$details = SM('reward_detail', 'tab_')->where(['record_id'=>$id])->select();
// 根据type 来合并
foreach ($details as $item) {
$item['info'] = $item;
if (empty($item['company_name']) && $item['company_id']) {
if ($item['company_type'] == RewardModel::COMPANY_TYPE_PARTNER) {
$item['company_name'] = SM('partner', 'tab_')->where(['id'=>$item['company_id']])->getField('partner');
} else {
$item['company_name'] = SM('promote_company', 'tab_')->where(['id'=>$item['company_id']])->getField('company_name');
}
}
$tmp[$item['type']][] = $item;
}
$data['rewardCount'] = sizeof($tmp[RewardModel::REWARD_TYPE_REWARD]);
$data['punishCount'] = sizeof($tmp[RewardModel::REWARD_TYPE_PUNISH]);
$data['rewardAmount'] = $data['rewardCount'] > 0 ? array_sum(array_column($tmp[RewardModel::REWARD_TYPE_REWARD], 'amount')) : 0;
$data['punishAmount'] = $data['punishCount'] > 0 ? array_sum(array_column($tmp[RewardModel::REWARD_TYPE_PUNISH], 'amount')) : 0;
$data['maxCount'] = $data['rewardCount'] > $data['punishCount'] ? $data['rewardCount'] : $data['punishCount'];
$this->assign('details', $tmp);
$this->assign('data', $data);
$this->assign('promoters', SM('promote', 'tab_')->where(['level'=>1])->field('id, nickname')->select());
$this->assign('rewardTypes', RewardModel::$rewardTypes);
$this->assign('settlementTypes', RewardModel::$settlementTypes);
$this->assign('companyTypes', RewardModel::$companyTypes);
$this->display('rewardManagerDetail');
}
public function addreward() {
$batch = date('Ymd') . date('His') . sp_random_num(3);
$this->assign("batch", $batch);
$this->meta_title = 'EXCEL导入';
$this->display();
}
public function loopAdd()
{
Vendor('PHPExcel.PHPExcel');
$p = $_REQUEST;
// $batch = $p['batch'];
$checkarr = $p['checkarr'];
//循环获取添加
if (count($checkarr) < 1) {
$this->ajaxReturn(["status" => 0, "msg" => "没有数据"]);
}
$reward_type = [
""=>0,
"奖励"=>1,
"惩罚"=>2,
"双向"=>3
];
$company_type = [
""=>0,
"上游公司"=>1,
"下游公司"=>2,
"内部公司"=>3
];
$type = [
""=>0,
"奖励方"=>1,
"惩罚方"=>2
];
$settlement_type = [
"线下结算"=>1,
"线上结算"=>2
];
$all_record = [];
$all_detail = [];
$record = [];
$detail = [];
$md5 = '';
foreach ($checkarr as $k => $v) {
if ($v['game_name']) {
$record = [];
$detail = [];
$game_data = SM("game","tab_")->field("relation_game_id")->where(['relation_game_name'=>$v['game_name']])->find();
$record["reward_type"] = $reward_type[$v['reward_type']];
$record["accounts"] = json_encode([]);
$record["relation_game_id"] = $game_data["relation_game_id"]?$game_data["relation_game_id"]:0;
$record["content"] = $v["content"];
$record["reward_time"] = strtotime($v["reward_time"]);
$record["confirm_time"] = strtotime($v["confirm_time"]);
$record["settlement_time"] = strtotime($v["settlement_time"]);
$record["create_time"] = time();
$record["creater_id"] = $_SESSION['onethink_admin']['user_auth']['uid'];
$md5 = md5($record["relation_game_id"].$record["content"].$record["create_time"].$record["confirm_time"].$record["reward_time"].$record["settlement_time"]);
$all_record[$md5] = $record;
if ($v["company_name"]!="") {
$company_data = SM("promote_company","tab_")->field("id")->where(['company_name'=>$v["company_name"]])->find();
$detail["company_type"] = $company_type[$v["company_type"]]?$company_type[$v["company_type"]]:0;
$detail["type"] = $type[$v["type"]]?$type[$v["type"]]:0;
$detail["company_id"] = $company_data["id"]?$company_data["id"]:0;
$detail["company_name"] = $v["company_name"]?$v["company_name"]:'';
$detail["promote_account"] = $v["promote_account"]?$v["promote_account"]:'';
$detail["settlement_type"] = $settlement_type[$v["settlement_type"]]?$settlement_type[$v["settlement_type"]]:0;
$detail["amount"] = abs($v["amount"])?abs($v["amount"]):0;
$detail["confirm_time"] = $record["confirm_time"];
$all_detail[$md5][] = $detail;
}
$detail = [];
if ($v["company_name_1"]!="") {
$company_data = SM("promote_company","tab_")->field("id")->where(['company_name'=>$v["company_name_1"]])->find();
$detail["company_type"] = $company_type[$v["company_type_1"]]?$company_type[$v["company_type_1"]]:0;
$detail["type"] = $type[$v["type_1"]]?$type[$v["type_1"]]:0;
$detail["company_id"] = $company_data["id"]?$company_data["id"]:0;
$detail["company_name"] = $v["company_name_1"]?$v["company_name_1"]:'';
$detail["promote_account"] = $v["promote_account_1"]?$v["promote_account_1"]:'';
$detail["settlement_type"] = $settlement_type[$v["settlement_type_1"]]?$settlement_type[$v["settlement_type_1"]]:0;
$detail["amount"] = abs($v["amount_1"])?abs($v["amount_1"]):0;
$detail["confirm_time"] = $record["confirm_time"];
$all_detail[$md5][] = $detail;
}
} else {
$detail = [];
if ($v["company_name"]!="") {
$company_data = SM("promote_company","tab_")->field("id")->where(['company_name'=>$v["company_name"]])->find();
$detail["company_type"] = $company_type[$v["company_type"]]?$company_type[$v["company_type"]]:0;
$detail["type"] = $type[$v["type"]]?$type[$v["type"]]:0;
$detail["company_id"] = $company_data["id"]?$company_data["id"]:0;
$detail["company_name"] = $v["company_name"]?$v["company_name"]:'';
$detail["promote_account"] = $v["promote_account"]?$v["promote_account"]:'';
$detail["settlement_type"] = $settlement_type[$v["settlement_type"]]?$settlement_type[$v["settlement_type"]]:0;
$detail["amount"] = abs($v["amount"])?abs($v["amount"]):0;
$detail["confirm_time"] = $record["confirm_time"];
$all_detail[$md5][] = $detail;
}
$detail = [];
if ($v["company_name_1"]!="") {
$company_data = SM("promote_company","tab_")->field("id")->where(['company_name'=>$v["company_name_1"]])->find();
$detail["company_type"] = $company_type[$v["company_type_1"]];
$detail["type"] = $type[$v["type_1"]]?$type[$v["type_1"]]:0;
$detail["company_id"] = $company_data["id"]?$company_data["id"]:0;
$detail["company_name"] = $v["company_name_1"]?$v["company_name_1"]:'';
$detail["promote_account"] = $v["promote_account_1"]?$v["promote_account_1"]:'';
$detail["settlement_type"] = $settlement_type[$v["settlement_type_1"]]?$settlement_type[$v["settlement_type_1"]]:0;
$detail["amount"] = abs($v["amount_1"])?abs($v["amount_1"]):0;
$detail["confirm_time"] = $record["confirm_time"];
$all_detail[$md5][] = $detail;
}
}
}
// die();
foreach ($all_record as $key => $value) {
$insert_id = SM("reward_record","tab_")->add($value);
foreach ($all_detail[$key] as $k => $v) {
$all_detail[$key][$k]['record_id'] = $insert_id;
}
$is_success = SM("reward_detail","tab_")->addAll($all_detail[$key]);
}
$this->ajaxReturn(["status" => 1, "msg" => "导入成功"]);
}
//模板导出
public function download()
{
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
//设置模板文件
$objPHPExcel = $objReader->load("Public/Admin/excel/rewardOrfina.xlsx");
$fileName = "奖惩导入";
ob_end_clean();//清除缓冲区,避免乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName .'".xls');
header("Content-Disposition:attachment;filename={$fileName}.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
public function getCompanys($type=1)
{
if ($type == 1) {
$data = SM('partner', 'tab_')->field('id, partner as name')->select();
} elseif ($type == 0) {
$data = [];
} elseif ($type == 3){
$data = [];
foreach (self::INSIDE_COMPANY as $key => $value) {
$data[] = [
"id"=>$key,
"name"=>$value
];
}
} else {
$data = SM('promote_company', 'tab_')->field('id, company_name as name')->select();
}
return $this->success($data, '', true);
}
public function PersonOrderList() {
if ($_REQUEST['time_start'] || $_REQUEST['time_end'] ) {
$withdrawMap = [];
if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
$withdrawMap['settlement_begin_time'] = ['egt',strtotime($_REQUEST['time_start'])];
}
if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
$withdrawMap['settlement_end_time'] = ['elt',strtotime($_REQUEST['time_end'])];
}
if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
$withdrawMap['settlement_begin_time'] = ['egt',strtotime($_REQUEST['time_start'])];
$withdrawMap['settlement_end_time'] = ['elt',strtotime($_REQUEST['time_end'])];
}
$data = M('withdraw',"tab_")
->field("widthdraw_number,settlement_begin_time,settlement_end_time,settlement_type,promote_account")
->join('left join tab_promote_belong on tab_withdraw.promote_id=tab_promote_belong.promote_id')
->join("left join tab_statement as statement on tab_withdraw.widthdraw_number = statement.ext_field")
->where("ext_field is null and tab_promote_belong.company_type=2")
->where($withdrawMap)
->select();
$statement = M('statement','tab_')
->field('ext_field')
->where(['ext_field'=>['like','%,%']])
->select();
$withdrawData = [];
foreach($statement as $key => $value) {
$handleData = explode(',',$value['ext_field']);
foreach ($handleData as $hk => $hv) {
array_push($withdrawData,$hv);
}
}
foreach ($data as $key => $value) {
$data[$key]['settlement_begin_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_begin_time']);
$data[$key]['settlement_end_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_end_time']);
if (in_array($value['widthdraw_number'],$withdrawData)) {
unset($data[$key]);
}
}
$this->assign('withdraw_data',$data);
}
if ($_REQUEST['withdraw']&&$data) {
$map = [];
$map['widthdraw_number'] = ['in',$_REQUEST['withdraw']];
$data = M('withdraw','tab_')
->field("tab_withdraw.id,pc.company_name,pc.id as cid,tab_withdraw.promote_id,tab_withdraw.promote_account,username,'个人' as type,game_ratio
,mobile_phone,real_name,pc.bank_card,bank_address as account_openin,tab_promote.address,pc.settlement_contact as bank_account")
->join('left join tab_promote_belong on tab_withdraw.promote_id=tab_promote_belong.promote_id')
->join("left join tab_statement as statement on tab_withdraw.widthdraw_number = statement.ext_field")
->join("left join tab_promote on tab_promote.id=tab_promote_belong.promote_id")
->join("left join tab_promote_company as pc on pc.id = tab_promote.company_id")
->join("left join sys_ucenter_member on sys_ucenter_member.id=tab_promote.admin_id")
->where("ext_field is null and tab_promote_belong.company_type=2")
->where($map)
->select();
$game_ratio = [];
foreach($data as $dkey => &$dval) {
$gameStr = [];
// $address = '';
// if (is_array($data['address'])) {
// foreach($data['address'] as $key => $value) {
// $address = $address.' '.$value;
// }
// }
// $dval['address'] = $address;
$rowspan = 0;
$dval['rowspan'] = 0;
$dval['game_ratio'] = json_decode($dval['game_ratio'],true);
if (is_array($dval['game_ratio'])) {
foreach ($dval['game_ratio'] as $key => &$value) {
foreach ($value as $k => &$v) {
// $v['game_id'] = $key;
$v['game_name'] = substr(get_gamename($key),0,strpos(get_gamename($key), '('));
if (!in_array($v['game_name'],$gameStr)) {
array_push($gameStr,$v['game_name']);
} else {
// var_dump($key);
if (!$game_ratio[$v['game_name']]) {
$game_ratio[$v['game_name']] = $v;
}
else {
$game_ratio[$v['game_name']]['sum_amount'] += $v['sum_amount'];
}
unset($value[$k]);
}
//
}
if (!$value) {
unset($dval['game_ratio'][$key]);
}
}
foreach ($dval['game_ratio'] as $key => $val) {
$val[0]['reward'] = 0;
$val[0]['forfeit'] = 0;
$startTime = strtotime($val[0]['begin_time']);
$endTime = strtotime($val[0]['end_time']);
$rewardData = M('reward_record','tab_')
->field("sum(tab_reward_record.money) as money,reward_type,relation_game_name")
->join("left join (select * from tab_game group by relation_game_id)tab_game on tab_reward_record.relation_game_id=tab_game.relation_game_id")
->where(['company_type'=>2,'relation_game_name'=>['like',"%{$val[0]['game_name']}%"],'reward_time'=>['between',[$startTime,$endTime]]])
->group('reward_type')
->select();
foreach ($rewardData as $rk => $rv) {
if ($rv['reward_type'] == 1) {
$val[0]['reward'] = $rv['money'];
} else {
$val[0]['forfeit'] = $rv['money'];
}
}
foreach ($val as $k => &$v) {
if ($game_ratio[$v['game_name']]) {
$v['sum_amount'] = number_format($v['sum_amount'] + $game_ratio[$v['game_name']]['sum_amount'],2,'.','');
}
//计算结算金额
$val[0]['real_amount'] = number_format($val[0]['sum_amount'] * ($val[0]['selle_ratio']/100)+$val[0]['reward']-$val[0]['forfeit'],2,'.','');
}
$rowspan++;
unset($dval['game_ratio'][$key]);
array_push($dval['game_ratio'],$val[0]);
}
$dval['rowspan'] = $rowspan;
}
}
$this->assign('data',$data);
}
$this->display();
}
public function savePersonOrder() {
// dump($_POST);
$data['statement_info'] = $_POST['statement_info'];
$data['first_party_info'] = json_encode([]);
$data['second_party_info'] = $_POST['second_party_info'];
$data['statement_begin_time'] = strtotime($_POST['time_start']);
$data['statement_end_time'] = strtotime($_POST['time_end']);
$data['create_time'] = time();
$data['statement_money'] = 0;
$data['pay_amount'] = 0;
$data['ext_field'] = '';
$data['company_name'] = '个人结账单';
$data['statement_type'] = 2;
if($data['second_party_info'] == '[]' || $data['statement_info'] == '[]') {
$this->ajaxReturn(['status'=>1,'msg'=>'数据错误']);
}
$statement_info = json_decode($data['statement_info'],true);
foreach($statement_info as $key => $value) {
$data['statement_money'] += $value['sum_amount'];
$data['pay_amount'] += $value['real_amount'];
}
$second_party_info = json_decode($data['second_party_info'],true);
foreach($second_party_info as $key => $value) {
$data['ext_field'] = $data['ext_field'].$value['widthdraw_number'].',';
}
$is_exist = M('statement','tab_')->where(['ext_field'=>$data['ext_field'],'statement_type'=>2])->find();
if ($is_exist) {
$this->ajaxReturn(['status'=>0,'msg'=>'请勿重复添加']);
}
$is_save = M('statement','tab_')->add($data);
if ($is_save) {
addOperationLog(['op_type'=>0,'key'=>'个人结算单'.'/'.getNowDate(),'op_name'=>'生成下游个人结算单','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-生成下游个人结算单']);
$this->ajaxReturn(['status'=>1,'msg'=>'生成成功']);
} else {
$this->ajaxReturn(['status'=>0,'msg'=>'生成失败']);
}
}
public function PersonOrderEdit() {
if ($_REQUEST['time_start'] || $_REQUEST['time_end'] ) {
$withdrawMap = [];
if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
$withdrawMap['settlement_begin_time'] = ['egt',strtotime($_REQUEST['time_start'])];
}
if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
$withdrawMap['settlement_end_time'] = ['elt',strtotime($_REQUEST['time_end'])];
}
if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
$withdrawMap['settlement_begin_time'] = ['egt',strtotime($_REQUEST['time_start'])];
$withdrawMap['settlement_end_time'] = ['elt',strtotime($_REQUEST['time_end'])];
}
$data = M('withdraw',"tab_")
->field("widthdraw_number,settlement_begin_time,settlement_end_time,settlement_type,promote_account")
->join('left join tab_promote_belong on tab_withdraw.promote_id=tab_promote_belong.promote_id')
->join("left join tab_statement as statement on tab_withdraw.widthdraw_number = statement.ext_field")
->where("tab_promote_belong.company_type=2")
->where($withdrawMap)
->select();
// dump($data);die();
$statement = M('statement','tab_')
->field('ext_field')
->where('ext_field is not null')
->select();
$withdrawData = [];
foreach($statement as $key => $value) {
$handleData = explode(',',$value['ext_field']);
foreach ($handleData as $hk => $hv) {
array_push($withdrawData,$hv);
}
}
// $thisWithdraw = explode(',',$_REQUEST['withdraw']);
$thisWithdraw = explode(',',M('statement','tab_')->where(['id'=>$_REQUEST['id']])->find()['ext_field']);
// dump($thisWithdraw);die();
foreach ($data as $key => $value) {
// $data[$key]['settlement_begin_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_begin_time']);
// $data[$key]['settlement_end_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_end_time']);
// var_dump($value['widthdraw_number'].$_REQUEST['withdraw']);die();
if (in_array($value['widthdraw_number'],$withdrawData)&&!in_array($value['widthdraw_number'],$thisWithdraw)) {
unset($data[$key]);
}
}
foreach ($data as $key => $value) {
$data[$key]['settlement_begin_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_begin_time']);
$data[$key]['settlement_end_time'] = date("Y-m-d H:i:s", $data[$key]['settlement_end_time']);
}
$this->assign('withdraw_data',$data);
}
if ($_REQUEST['withdraw']&&$data) {
$map = [];
$map['widthdraw_number'] = ['in',$_REQUEST['withdraw']];
$data = M('withdraw','tab_')
->field("tab_withdraw.id,pc.company_name,pc.id as cid,tab_withdraw.promote_id,tab_withdraw.promote_account,username,'个人' as type,game_ratio
,mobile_phone,real_name,pc.bank_card,bank_address as account_openin,tab_promote.address,pc.settlement_contact as bank_account")
->join('left join tab_promote_belong on tab_withdraw.promote_id=tab_promote_belong.promote_id')
->join("left join tab_statement as statement on tab_withdraw.widthdraw_number = statement.ext_field")
->join("left join tab_promote on tab_promote.id=tab_promote_belong.promote_id")
->join("left join tab_promote_company as pc on pc.id = tab_promote.company_id")
->join("left join sys_ucenter_member on sys_ucenter_member.id=tab_promote.admin_id")
->where("tab_promote_belong.company_type=2")
->where($map)
->select();
$game_ratio = [];
foreach($data as $dkey => &$dval) {
$gameStr = [];
$rowspan = 0;
$dval['rowspan'] = 0;
$dval['game_ratio'] = json_decode($dval['game_ratio'],true);
if (is_array($dval['game_ratio'])) {
foreach ($dval['game_ratio'] as $key => &$value) {
foreach ($value as $k => &$v) {
// $v['game_id'] = $key;
$v['game_name'] = substr(get_gamename($key),0,strpos(get_gamename($key), '('));
if (!in_array($v['game_name'],$gameStr)) {
array_push($gameStr,$v['game_name']);
} else {
// var_dump($key);
if (!$game_ratio[$v['game_name']]) {
$game_ratio[$v['game_name']] = $v;
}
else {
$game_ratio[$v['game_name']]['sum_amount'] += $v['sum_amount'];
}
unset($value[$k]);
}
//
}
if (!$value) {
unset($dval['game_ratio'][$key]);
}
}
foreach ($dval['game_ratio'] as $key => $val) {
$val[0]['reward'] = 0;
$val[0]['forfeit'] = 0;
$startTime = strtotime($val[0]['begin_time']);
$endTime = strtotime($val[0]['end_time']);
$rewardData = M('reward_record','tab_')
->field("sum(tab_reward_record.money) as money,reward_type,relation_game_name")
->join("left join (select * from tab_game group by relation_game_id)tab_game on tab_reward_record.relation_game_id=tab_game.relation_game_id")
->where(['company_type'=>2,'relation_game_name'=>['like',"%{$val[0]['game_name']}%"],'reward_time'=>['between',[$startTime,$endTime]]])
->group('reward_type')
->select();
foreach ($rewardData as $rk => $rv) {
if ($rv['reward_type'] == 1) {
$val[0]['reward'] = $rv['money'];
} else {
$val[0]['forfeit'] = $rv['money'];
}
}
foreach ($val as $k => &$v) {
if ($game_ratio[$v['game_name']]) {
$v['sum_amount'] = number_format($v['sum_amount'] + $game_ratio[$v['game_name']]['sum_amount'],2,'.','');
}
//计算结算金额
$val[0]['real_amount'] = number_format($val[0]['sum_amount'] * ($val[0]['selle_ratio']/100)+$val[0]['reward']-$val[0]['forfeit'],2,'.','');
}
$rowspan++;
unset($dval['game_ratio'][$key]);
array_push($dval['game_ratio'],$val[0]);
}
$dval['rowspan'] = $rowspan;
}
}
$this->assign('data',$data);
}
$this->display();
}
public function savePersonOrderEdit() {
$data['statement_info'] = $_POST['statement_info'];
$data['first_party_info'] = json_encode([]);
$data['second_party_info'] = $_POST['second_party_info'];
$data['statement_begin_time'] = strtotime($_POST['time_start']);
$data['statement_end_time'] = strtotime($_POST['time_end']);
$data['create_time'] = time();
$data['statement_money'] = 0;
$data['pay_amount'] = 0;
$data['ext_field'] = '';
$data['company_name'] = '个人结账单';
$data['statement_type'] = 2;
if($data['second_party_info'] == '[]' || $data['statement_info'] == '[]') {
$this->ajaxReturn(['status'=>1,'msg'=>'数据错误']);
}
$statement_info = json_decode($data['statement_info'],true);
foreach($statement_info as $key => $value) {
$data['statement_money'] += $value['sum_amount'];
$data['pay_amount'] += $value['real_amount'];
}
$second_party_info = json_decode($data['second_party_info'],true);
foreach($second_party_info as $key => $value) {
$data['ext_field'] = $data['ext_field'].$value['widthdraw_number'].',';
}
$data['ext_field'] = rtrim($data['ext_field'], ",");
$is_save = M('statement','tab_')->where(['id'=>$_REQUEST['withdraw_id']])->save($data);
// echo M('statement','tab_')->_sql();die();
if ($is_save) {
addOperationLog(['op_type'=>1,'key'=>'个人结算单'.'/'.getNowDate(),'op_name'=>'编辑下游个人结算单','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-编辑下游个人结算单']);
$this->ajaxReturn(['status'=>1,'msg'=>'修改成功']);
} else {
$this->ajaxReturn(['status'=>0,'msg'=>'修改失败']);
}
}
public function personOrderDetail() {
$id = $_REQUEST['id'];
$map['id'] = $id;
$data = M('statement','tab_')
->field("second_party_info,statement_info")
->where($map)
->find();
$data['second_party_info'] = json_decode($data['second_party_info'],true);
$data['statemnt_info'] = json_decode($data['statement_info'],true);
// dump($data['statemnt_info']);die();
$this->assign('second_party_info',$data['second_party_info']);
$this->assign('statemnt_info',$data['statemnt_info']);
$this->display();
}
public function verifyStatementStatus() {
$order = $_REQUEST['order'];
if ($order) {
$isVerify = M('statement','tab_')->where(['id'=>['in',$order]])->save(['verify_status'=>1]);
if ($isVerify) {
$data = M('statement','tab_')->where(['id'=>['in',$order]])->select();
foreach ($data as $key => $value) {
addOperationLog(['op_type'=>1,'key'=>$value['company_name'].'/'.date('YmdHis',$value['statement_begin_time']).'/'.date('YmdHis',$value['statement_end_time']),'op_name'=>'编辑结算单审核通过','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-结算单审核通过']);
}
$this->ajaxReturn(['status'=>1]);
} else {
$this->ajaxReturn(['status'=>0]);
}
} else {
$this->ajaxReturn(['status'=>0]);
}
}
public function rejectStatementStatus() {
$order = $_REQUEST['order'];
if ($order) {
$isVerify = M('statement','tab_')->where(['id'=>['in',$order]])->save(['verify_status'=>2]);
if ($isVerify) {
$data = M('statement','tab_')->where(['id'=>['in',$order]])->select();
foreach ($data as $key => $value) {
addOperationLog(['op_type'=>1,'key'=>$value['company_name'].'/'.date('YmdHis',$value['statement_begin_time']).'/'.date('YmdHis',$value['statement_end_time']),'op_name'=>'编辑结算单审核通过','url'=>U('StatementMangement/lists'),'menu'=>'推广员-结算管理-结算单管理-结算单审核通过']);
}
$this->ajaxReturn(['status'=>1]);
} else {
$this->ajaxReturn(['status'=>0]);
}
} else {
$this->ajaxReturn(['status'=>0]);
}
}
//审核
public function verify() {
$id = $_REQUEST["id"];
$status = $_REQUEST["status"];
$rewardDetailModel = SM("reward_detail","tab_");
$rewardRecordModel = SM("reward_record","tab_");
if (!isset($id)||!isset($status)) {
$this->ajaxReturn(["status"=>0,"msg"=>"参数错误"]);
}
$dmap = [
"record_id"=>['in',$id],
];
if($status != 2) {
$dmap['is_check']=0;
$saveStatus = $status;
$saveDetail = json_encode(["user"=>$_SESSION['onethink_admin']['user_auth']['username'],"time"=>date("Y-m-d H:i:s",time())]);
}else{
$dmap['is_check']=['neq',0];
$saveStatus = 0;
$saveDetail = '';
}
$marketCompanyIds = A("Market","Event")->getAdminCompanyId();
if($marketCompanyIds != 'all'){
$dmap['company_id']=['in',$marketCompanyIds];
$dmap['company_type']=['neq',1];
}
//获取需要审核的细则表
$data = $rewardDetailModel
->field("id,record_id,is_check,company_id,check_detail")
->where($dmap)
->select();
if(empty($data)){
$this->ajaxReturn(["status"=>1,"msg"=>"操作失败"]);
}
//改母单
$recordIds = array_unique( array_column($data,"record_id") );
$rdIds = array_unique( array_column($data,"id") );
foreach ($data as $key => $value) {
$value['is_check'] = $saveStatus;
$value['check_detail'] = $saveDetail;
$rewardDetailModel->save($value);
}
if($status == 1){
//同意
$map = [
'record_id'=>['in',$recordIds],
'is_check'=>['neq',1],
'id'=>['NOT IN',$rdIds]
];
$record = $rewardDetailModel->where($map)->group("record_id")->getField("record_id,count(*) count",TRUE);
foreach ($recordIds as $key => $value) {
if(isset($record[$value])) continue;
$rSave = [
'id'=>$value,
'is_verify'=>$status
];
$rewardRecordModel->save($rSave);
}
}else{
foreach ($recordIds as $key => $value) {
$rSave = [
'id'=>$value,
'is_verify'=>$saveStatus
];
$rewardRecordModel->save($rSave);
}
}
$this->ajaxReturn(["status"=>1,"msg"=>"操作成功"]);
}
//自动审核通过
public function autoVerify(&$settlmentSet = [],$id = 0) {
if (!$id) {
return false;
}
$i = 0;
$count = count($settlmentSet);
$detail = json_encode(["user"=>$_SESSION['onethink_admin']['user_auth']['username'],"time"=>date("Y-m-d H:i:s",time())]);
foreach ($settlmentSet as $key => $value) {
$data = SM("promote_company","tab_")->where(["id"=>$value['company_id']])->find();
if (!$data || in_array($data["company_belong"],[0,3])) {
$settlmentSet[$key]['is_check'] = 1;
$settlmentSet[$key]['check_detail'] = $detail;
$i++;
}
}
if ($i == $count) {
SM("reward_record","tab_")->where(["id"=>['in',$id]])->save(["is_verify"=>1]);
}
}
}