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.

530 lines
19 KiB
PHTML

5 years ago
<?php
namespace Home\Controller;
use Base\Model\Model;
5 years ago
use OT\DataDictionary;
use Base\Repository\PromoteRepository;
use Base\Repository\SpendRepository;
use Base\Repository\UserRepository;
/**
* 财务管理
*/
class FinanceController extends BaseController
{
//提现状态
public static $withdrawStatus = [
-1 => '审核未通过',
0 => '待审核',
1 => '汇款中',
2 => '已汇款',
];
//权限过滤
private function purview()
{
$this->loginPromote = $this->getLoginPromote();
if ($this->loginPromote['level'] != 1) {
$this->error('权限异常');
}
return true;
}
private function getDayList($beginTime, $endTime)
{
$dayList = [];
do {
$dayList[] = date('Y-m-d', $beginTime);
$beginTime += 24 * 60 * 60;
} while ($beginTime < $endTime);
return $dayList;
}
private function assembleRecords($items, $keys, $valueColumn, $keyColumn = 'day')
{
$records = [];
foreach ($keys as $key) {
$value = 0;
foreach ($items as $item) {
if ($item[$keyColumn] == $key) {
$value = $item[$valueColumn];
}
}
$records[$key] = $value;
}
return $records;
}
//生成提现单号
private function produceWithdrawNumber()
{
$prefix = 'WD_';
$number = '';
while (true) {
$randomNum = rand(10, 99);
$number = $prefix . time() . $randomNum;
$map['widthdraw_number'] = $number;
$res = M('withdraw', 'tab_')->where($map)->getField('id');
if (!$res) {
break;
}
}
return $number;
}
//结算中心
public function index()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '结算明细';
$modelList = ['财务管理', $metaTitle];
$this->verifyPasswordView($modelList);
$model = M('spend', 'tab_');
$thisDay = strtotime(date('Y-m-d'));
$thisMonth = strtotime(date('Y-m'));
$yesterday = strtotime(date('Y-m-d', strtotime('-1 day', time())));
$nowTime = date('Y-m-d');
$initBegTime = date('Y-m-d', strtotime('-6 day', strtotime($nowTime)));
$initBegTime = empty(I('begtime')) ? $initBegTime : I('begtime');
$initEndTime = $nowTime;
$initEndTime = empty(I('endtime')) ? $initEndTime : I('endtime');
$begTime = strtotime($initBegTime);
$endTime = strtotime($initEndTime);
$endTime = $endTime + 3600 * 24;
$map['chain'] = ['like', $this->loginPromote['chain'] . $this->loginPromote['id'] . '/%'];
$promoteIds = M('promote', 'tab_')->where($map)->getField('id', true);
$promoteIds[] = $this->loginPromote['id'];
$promoteIds = implode(',', $promoteIds);
$map = [];
$map['pay_status'] = 1;
$map['promote_id'] = ['in', $promoteIds];
$income = $model->field("sum(if(pay_time < $thisDay, if(selle_ratio > 0, pay_amount * selle_ratio, 0), 0)) as history_income,
sum(if(pay_time >= $thisMonth, if(selle_ratio > 0, pay_amount * selle_ratio, 0), 0)) as this_month_income,
sum(if((pay_time >= $yesterday and pay_time < $thisDay), if(selle_ratio > 0, pay_amount * selle_ratio, 0), 0)) as yesterday_income,
sum(if(pay_time < $thisDay, if(selle_status = 0, pay_amount * selle_ratio, 0), 0)) as balance")
->where($map)
->find();
foreach ($income as &$value) {
$value = bcdiv($value, 100, 2);
}
$map['pay_time'] = ['between', [$begTime, $endTime]];
$dayList = $this->getDayList($begTime, $endTime);
$data = $model->field('FROM_UNIXTIME(pay_time, "%Y-%m-%d") as day,
sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as income')
->where($map)
->group('day')
->select();
$data = $this->assembleRecords($data, $dayList, 'income');
foreach ($dayList as $day) {
$date = date('Ymd', strtotime($day));
$records[] = [
'day' => $date,
'income' => bcdiv($data[$day], 100, 2),
'url' => U('settlementDtl', array('begtime' => $day, 'endtime' => $day))
];
}
$this->assign('income', $income);
$this->assign('listData', $records);
$this->assign('initBegTime', $initBegTime);
$this->assign('initEndTime', $initEndTime);
$this->assign('yesterday', date('Y-m-d', $yesterday));
$this->assign('meta_title', $metaTitle);
$this->display();
}
//结算明细
public function settlementDtl()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '结算明细';
$modelList = ['财务管理', $metaTitle];
$this->verifyPasswordView($modelList);
$model = M('spend', 'tab_');
$page = intval(I('get.p', 1));
$page = $page ? $page : 1; //默认显示第一页数据arraypage
$row = intval(I('get.row', 10));
$payOrderNumber = I('pay_order_number', '');
$nowTime = date('Y-m-d');
$initBegTime = date('Y-m-d', strtotime('-6 day', strtotime($nowTime)));
$initBegTime = empty(I('begtime')) ? $initBegTime : I('begtime');
$initEndTime = $nowTime;
$initEndTime = empty(I('endtime')) ? $initEndTime : I('endtime');
$begTime = strtotime($initBegTime);
$endTime = strtotime($initEndTime);
$parameter['p'] = $page;
$parameter['row'] = $row;
$parameter['pay_order_number'] = $payOrderNumber;
$parameter['begtime'] = $initBegTime;
$parameter['endtime'] = $initEndTime;
$map['chain'] = ['like', $this->loginPromote['chain'] . $this->loginPromote['id'] . '/%'];
$ids = M('promote', 'tab_')->where($map)->getField('id', true);
$ids[] = $this->loginPromote['id'];
$ids = implode(',', $ids);
$map = [];
$map['promote_id'] = ['in', $ids];
if (!empty($payOrderNumber)) {
$map['pay_order_number'] = $payOrderNumber;
}
if (!empty($begTime) && empty($endTime)) {
$map['pay_time'] = ['egt', $begTime];
} elseif (empty($begTime) && !empty($endTime)) {
$map['pay_time'] = ['elt', $endTime + 86399];
} elseif (!empty($begTime) && !empty($endTime)) {
$map['pay_time'] = ['between', [$begTime, $endTime + 86399]];
}
$data = $model->field('pay_order_number,game_name,user_account,promote_account,pay_amount,pay_way,if(selle_ratio >= 0,selle_ratio,0) as selle_ratio,pay_time,selle_status,pay_status,withdraw_id')
->where($map)
->order('id desc')
->page($page, $row)
->select();
$count = $model->where($map)->count();
//提现状态
$withdrawIds = [];
foreach ($data as $key => $value) {
if ($value['withdraw_id'] > 0 && !in_array($value['withdraw_id'], $withdrawIds)) {
$withdrawIds[] = $value['withdraw_id'];
}
}
$withdrawStatus = M('withdraw', 'tab_')
->where(array('id' => ['in', $withdrawIds]))
->getField('id,status');
foreach ($data as &$list) {
//提现状态
if ($list['pay_status'] == 1) {
switch ($list['selle_status']) {
case 0:
$list['selle_status'] = '未提现';
break;
default:
if ($list['withdraw_id'] > 0) {
$list['selle_status'] = FinanceController::$withdrawStatus[$withdrawStatus[$list['withdraw_id']]];
} else {
$list['selle_status'] = '';
}
break;
}
} else {
$list['selle_status'] = '支付失败';
}
$list['income'] = bcmul($list['pay_amount'], bcdiv($list['selle_ratio'], 100, 2), 2);
$list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
}
$page = set_pagination($count, $row, $parameter);
$this->assign('listData', $data);
$this->assign('count', $count);
$this->assign('initBegTime', $initBegTime);
$this->assign('initEndTime', $initEndTime);
$this->assign('_page', $page);
$this->assign('meta_title', $metaTitle);
$this->assign('modelList', $modelList);
$this->display();
}
//提现
public function withdraw()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '结算明细';
$modelList = ['财务管理', $metaTitle];
$res = $this->verifyPasswordView($modelList, false);
if ($res == false) {
$data['status'] = 0;
$data['msg'] = '权限异常';
$this->ajaxReturn($data);
}
$spendModel = M('spend', 'tab_');
$thisDay = strtotime(date('Y-m-d'));
$map['chain'] = ['like', $this->loginPromote['chain'] . $this->loginPromote['id'] . '/%'];
$promoteIds = M('promote', 'tab_')->where($map)->getField('id', true);
$promoteIds[] = $this->loginPromote['id'];
$promoteIds = implode(',', $promoteIds);
$map = [];
$map['pay_status'] = 1;
$map['selle_status'] = 0;
$map['promote_id'] = ['in', $promoteIds];
$map['pay_time'] = ['lt', $thisDay];
$balance = $spendModel->field("sum(pay_amount * selle_ratio) as balance")
->where($map)
->find()['balance'];
$balance = bcdiv($balance, 100, 2);
if ($balance < 100) {
$data['status'] = 0;
$data['msg'] = '账户余额低于100元无法提现';
$this->ajaxReturn($data);
}
$spendIds = $spendModel->where($map)->getField('id', true);
if (!empty($spendIds)) {
$data['status'] = 0;
$data['msg'] = '数据异常';
$this->ajaxReturn($data);
}
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $this->loginPromote['id'];
$add['promote_account'] = $this->loginPromote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = $this->produceWithdrawNumber();
$add['settlement_end_time'] = $thisDay - 1;
M()->startTrans();//开启事物
$withdrawId = M('withdraw', 'tab_')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '提现失败';
$this->ajaxReturn($data);
}
$map = [];
$map['id'] = ['in', $spendIds];
$save['selle_status'] = 1;
$save['selle_time'] = $thisTime;
$save['withdraw_id'] = $withdrawId;
$res = $spendModel->where($map)->save($save);
if ($res === false) {
M('withdraw', 'tab_')->delete($withdrawId);
M()->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '提现失败';
$this->ajaxReturn($data);
}
M()->commit();//事物提交
$data['status'] = 1;
$data['msg'] = '提现成功';
$this->ajaxReturn($data);
}
//提现记录
public function withdrawRecord()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '提现记录';
$modelList = ['财务管理', $metaTitle];
$this->verifyPasswordView($modelList);
$model = M('withdraw', 'tab_');
$page = intval(I('get.p', 1));
$page = $page ? $page : 1; //默认显示第一页数据arraypage
$row = intval(I('get.row', 10));
$withdrawNumber = I('widthdraw_number', '');
$initBegTime = I('begtime', '');
$initEndTime = I('endtime', '');
$begTime = strtotime(date('Y-m-d', strtotime($initBegTime)));
$endTime = strtotime(date('Y-m-d', strtotime($initEndTime))) + 3600 * 24;
$status = '';
$parameter['p'] = $page;
$parameter['row'] = $row;
$parameter['widthdraw_number'] = $withdrawNumber;
$parameter['begtime'] = $initBegTime;
$parameter['endtime'] = $initEndTime;
$map['promote_id'] = $this->loginPromote['id'];
if (!empty($withdrawNumber)) {
$map['widthdraw_number'] = $withdrawNumber;
}
if (!empty($initBegTime) && empty($initEndTime)) {
$map['create_time'] = ['egt', $begTime];
} elseif (empty($initBegTime) && !empty($initEndTime)) {
$map['create_time'] = ['lt', $endTime];
} elseif (!empty($initBegTime) && !empty($initEndTime)) {
$map['create_time'] = ['between', [$begTime, $endTime - 1]];
}
if (isset($_GET['status']) && $_GET['status'] !== '') {
$status = intval($_GET['status']);
if (!isset(FinanceController::$withdrawStatus[$status])) {
$this->error('参数异常');
}
$map['status'] = $status;
}
$data = $model->field('id,widthdraw_number,create_time,settlement_end_time,sum_money,status,respond')
->where($map)
->order('id desc')
->page($page, $row)
->select();
$count = $model
->where($map)
->count();
if (!empty($data)) {
foreach ($data as &$list) {
$list['create_time'] = date('Y-m-d H:i:s', $list['create_time']);
$list['settlement_end_time'] = date('Y-m-d H:i:s', $list['settlement_end_time']);
$list['status'] = FinanceController::$withdrawStatus[$list['status']];
}
}
$page = set_pagination($count, $row, $parameter);
$this->assign('listData', $data);
$this->assign('count', $count);
$this->assign('initBegTime', $initBegTime);
$this->assign('initEndTime', $initEndTime);
$this->assign('status', $status);
$this->assign('_page', $page);
$this->assign('meta_title', $metaTitle);
$this->assign('modelList', $modelList);
$this->display();
}
//提现明细
public function withdrawDtl()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '提现明细';
$modelList = ['财务管理', '提现记录', $metaTitle];
$this->verifyPasswordView($modelList);
$model = M('withdraw', 'tab_');
$spendModel = M('spend', 'tab_');
$page = intval(I('get.p', 1));
$page = $page ? $page : 1; //默认显示第一页数据arraypage
$row = intval(I('get.row', 10));
$withdrawId = intval(I('get.id', 0));
if ($withdrawId == 0) {
$this->error('参数异常');
}
$withdraw = $model->field('promote_id,status')->where(array('id' => $withdrawId))->find();
if (empty($withdraw) || $withdraw['promote_id'] != $this->loginPromote['id']) {
$this->error('参数异常');
}
$gameId = intval(I('game_id', 0));
$userAccount = I('user_account', '');
$payOrderNumber = I('pay_order_number', '');
$parameter['p'] = $page;
$parameter['row'] = $row;
$parameter['id'] = $withdrawId;
$parameter['game_id'] = $gameId;
$parameter['user_account'] = $userAccount;
$parameter['pay_order_number'] = $payOrderNumber;
$map['withdraw_id'] = $withdrawId;
if (!empty($gameId)) {
$map['game_id'] = $gameId;
}
if (!empty($userAccount)) {
$map['user_account'] = $userAccount;
}
if (!empty($payOrderNumber)) {
$map['pay_order_number'] = $payOrderNumber;
}
$data = $spendModel
->field('id,pay_order_number,game_name,sdk_version,user_account,pay_amount,pay_way,selle_ratio,pay_time')
->where($map)
->order('id desc')
->page($page, $row)
->select();
$count = $spendModel
->where($map)
->count();
if (!empty($data)) {
foreach ($data as &$list) {
$list['income'] = bcmul($list['pay_amount'], bcdiv($list['selle_ratio'], 100, 2), 2);
$list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
switch ($list['pay_way']) {
case 2:
case 3:
case 4:
$list['pay_way_name'] = QueryController::$payWay[2];
break;
default:
$list['pay_way_name'] = QueryController::$payWay[$list['pay_way']];
break;
}
}
}
$page = set_pagination($count, $row, $parameter);
$this->assign('listData', $data);
$this->assign('count', $count);
$this->assign('status', FinanceController::$withdrawStatus[$withdraw['status']]);
$this->assign('_page', $page);
$this->assign('meta_title', $metaTitle);
$this->assign('modelList', $modelList);
$this->display();
}
//结算单
public function settlementOrder()
{
//是否是会长
$this->purview();
//验证安全密码
$metaTitle = '结算单';
$modelList = ['财务管理', '提现记录', $metaTitle];
$this->verifyPasswordView($modelList);
$model = M('withdraw', 'tab_');
$withdrawId = intval(I('get.id', 0));
if ($withdrawId == 0) {
$this->error('参数异常');
}
$withdraw = $model->find($withdrawId);
if (empty($withdraw) || $withdraw['promote_id'] != $this->loginPromote['id']) {
$this->error('参数异常');
}
$withdraw['create_time'] = date('Y-m-d H:i:s', $withdraw['create_time']);
$withdraw['settlement_end_time'] = date('Y-m-d H:i:s', $withdraw['settlement_end_time']);
$withdraw['status'] = FinanceController::$withdrawStatus[$withdraw['status']];
$map['withdraw_id'] = $withdrawId;
$data = M('spend', 'tab_')
->field('game_name,if(selle_ratio >= 0,selle_ratio,0) as selle_ratio,pay_way,sum(pay_amount) as pay_amount_all,sum(if(selle_ratio > 0,pay_amount * selle_ratio,0)) as income')
->where($map)
->group('game_id')
->order('game_id')
->select();
if (!empty($data)) {
foreach ($data as &$list) {
$list['income'] = bcdiv($list['income'], 100, 2);
}
}
$this->assign('withdraw', $withdraw);
$this->assign('listData', $data);
$this->assign('promoteData', $this->loginPromote);
$this->assign('meta_title', $metaTitle);
$this->assign('modelList', $modelList);
$this->display();
}
5 years ago
}