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.

473 lines
24 KiB
PHTML

5 years ago
<?php
namespace Admin\Controller;
/**
* 结算单
* @author ylw
*/
class SettlementController extends ThinkController
{
// 汇总结算单列表
5 years ago
public function sheetList($row = 10, $p = 1)
5 years ago
{
5 years ago
$map = [];
if (I('settlement_type')) {
$map['settlement_type'] = I('settlement_type');
}
if (I('settlement_time_type')) {
$map['settlement_time_type'] = I('settlement_time_type');
}
if (I('creater_id')) {
$map['creater_id'] = I('creater_id');
}
if (I('all_status')) {
$map['all_status'] = I('all_status');
}
5 years ago
$map['_string'] = "1 = 1";
5 years ago
if (I('create_time_start')) {
5 years ago
$map['_string'] .= " and create_time >= ".strtotime(I('create_time_start'));
5 years ago
}
if (I('create_time_end')) {
5 years ago
$map['_string'] .= " and create_time <= ".(strtotime(I('create_time_end')) + 86400);
5 years ago
}
$login_uid = is_login();
$list = M('settlement_sheet', 'tab_')->where($map)->page($p, $row)->order("if (audit_user ={$login_uid}, 0,1 ) and status = 3, all_status desc, create_time desc")->select();
5 years ago
$count = M('settlement_sheet', 'tab_')->where($map)->count();
if (!empty($list)) {
foreach ($list as &$item) {
switch ($item['settlement_type']) {
case '1':
$item['settlement_type_name'] = '上游结算单';
break;
case '2':
$item['settlement_type_name'] = '下游-内团结算单';
break;
case '3':
$item['settlement_type_name'] = '下游-外团结算单';
break;
case '4':
$item['settlement_type_name'] = '下游-个人结算单';
break;
}
switch ($item['settlement_time_type']) {
case '1':
$item['settlement_time_type_name'] = '周结';
break;
case '2':
$item['settlement_time_type_name'] = '月结';
break;
}
5 years ago
5 years ago
switch ($item['status']) {
case '1':
$item['status_name'] = '审核通过';
break;
case '2':
$item['status_name'] = '审核未通过';
break;
case '3':
$item['status_name'] = '待审核';
break;
}
$item['audit_users'] = json_decode($item['audit_users'], true);
}
}
$this->assign('admin_users', M('member')->field('uid, nickname')->select());
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('list_data', $list);
$this->display();
5 years ago
}
// 生成结算单 月结算补点 周结不算
5 years ago
public function generateSettlementSheet($settlement_type = 0, $settlement_time_type = 0, $time_start=0, $time_end = 0, $delete=0, $id=0, $action='')
5 years ago
{
$msg = '';
5 years ago
$this->assign(I(''));
$this->assign('action', $action);
if ($delete) {
$res = M('settlement_sheet', 'tab_')->where(['id'=>$id])->delete();
$GetData = $_GET;
unset($GetData['id']);
unset($GetData['delete']);
addOperationLog(['op_type'=>2,'key'=>getNowDate(),'op_name'=>'删除汇总结算单管理','url'=>U('settlement/sheetList',$GetData),'menu'=>'推广员-结算单管理-汇总结算单管理']);
return $res ? $this->success('删除成功', '', true) : $this->success('删除失败', '', true);
5 years ago
}
$this->meta_title = '结算单管理';
if (empty($settlement_type)) {
$msg = '请选择汇总结算单类型';
}
if (empty($settlement_time_type)) {
$msg = '请选择结算方式';
}
$settlement_time = I('settlement_time', '');
if ($settlement_time_type == 1) {
$settlement_time = explode(' 至 ', $settlement_time);
if (count($settlement_time) != 2) {
$msg = '请选择正确的时间区间';
} else {
$time_start = strtotime($settlement_time[0]);
$time_end = strtotime($settlement_time[1]) + 3600 * 24 - 1;
}
} else {
$time_start = strtotime($settlement_time);
$time_end = strtotime('+1 month', $time_start) - 1;
}
if ($time_end > time()) {
$msg = "结算结算时间无法大于当前时间";
}
5 years ago
if (empty($time_start) || empty($time_end)) {
$msg = '请选择正确的时间区间';
}
5 years ago
if (IS_GET) {
5 years ago
5 years ago
// 判断结算时间是否冲突
$isExsist = M('settlement_sheet', 'tab_')->where(['settlement_type'=>$settlement_type, 'time_start'=>$time_start, 'time_end'=>$time_end, 'settlement_time_type'=>$settlement_time_type])->find();
if ($isExsist) {
$msg = "已存在此类型结算单";
}
5 years ago
// 生成结算单数据
5 years ago
$generate = false;
5 years ago
if (!$msg) {
5 years ago
$generate = true;
5 years ago
if ($settlement_type == 1) {
if ($settlement_time_type == 1) {
$map = " and p.settlement_type = 1";
} else {
$map = '';
}
$result = M('spend', 'tab_')->query("
SELECT
5 years ago
TRUNCATE(sum(s.pay_amount), 2) as amount, g.relation_game_id, g.relation_game_name, p.partner, p.id as p_id, p.channel_rate, p.invoice_rate, g.id as game_id
5 years ago
FROM
tab_spend as s
INNER JOIN tab_game g on s.game_id = g.id
INNER JOIN tab_partner p on p.id = g.partner_id {$map}
where s.pay_status = 1 and s.pay_time BETWEEN {$time_start} and {$time_end}
GROUP BY relation_game_id, p.id
ORDER BY p_id asc");
$list = [];
foreach ($result as $key => $item) {
if (!isset($list[$item['p_id']])) {
// 奖罚金额
$reward = M('reward_record', 'tab_')
->field('sum(money) as money, reward_type')
->where("company_type = 1 and company_id = {$item['p_id']} and reward_time between {$time_start} and {$time_end}")
->group('reward_type')
->select();
if (!empty($reward)) {
$reward = array_column($reward, 'money', 'reward_type');
}
$list[$item['p_id']] = [
'company_id'=>$item['p_id'],
'company_name'=>$item['partner'],
'bonuses'=>isset($reward[1]) ? $reward[1] : 0,
'fine'=>isset($reward[2]) ? $reward[2] : 0 ,
'channel_rate' => $item['channel_rate'], // 渠道费
'invoice_rate' => $item['invoice_rate'], // 税费
'time_start' => date('Y-m-d', $time_start),
'time_end' => date('Y-m-d', $time_end)
5 years ago
];
}
$data = [];
// 获取分成比例
if ($settlement_time_type == 1) {
$data['game_ratio'] = getGameCpRadio($item['game_id'], $item['amount'], false);
} else {
$data['game_ratio'] = getGameCpRadio($item['game_id'], $item['amount'], true);
}
$data['relation_game_id'] = $item['relation_game_id'];
$data['relation_game_name'] = $item['relation_game_name'];
$data['amount'] = $item['amount'];
$data['self_game_ratio'] = 100 - $data['game_ratio'];
5 years ago
$data['parter_settlement'] = floor(($item['amount'] * ($data['game_ratio']/100) - ($item['amount']*$item['channel_rate'] ) + $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'])*100)/100;
5 years ago
$list[$item['p_id']]['channels'][] = array_merge($data, [
'company_id'=>$item['p_id'],
'company_name'=>$item['partner'],
'bonuses'=>$list[$item['p_id']]['bonuses'],
'fine'=>$list[$item['p_id']]['fine'],
'channel_rate' => $item['channel_rate'],
'invoice_rate' => $item['invoice_rate']]);
5 years ago
}
} else if (\in_array($settlement_type, [2, 3])) {
$map = "p.account_type = 1 and p.`level` = 1 and p.company_belong " . ($settlement_type == 2 ? " = 0 " : " in (1, 2) ") ;
if ($settlement_time_type == 1) {
$map .= " and p.settlement_type = {$settlement_time_type} ";
}
$result = M()->query("
SELECT
p.id, p.account, pc.company_name, pc.id as p_id, pc.fax_ratio, pc.settlement_contact
FROM
tab_promote p
INNER JOIN tab_promote_company pc on p.company_id = pc.id
where {$map}
order by pc.id asc
");
$list = [];
foreach ($result as $key => $item) {
$res = M()->query("
select
5 years ago
g.relation_game_name, g.relation_game_id, TRUNCATE(sum(s.pay_amount), 2) as amount
5 years ago
from
tab_spend s
5 years ago
inner join tab_promote p on (p.chain like '/{$item['id']}/%' or p.id = {$item['id']}) and s.promote_id = p.id
5 years ago
inner join tab_game g on s.game_id = g.id
where s.pay_status = 1 and s.pay_time BETWEEN {$time_start} and {$time_end}
group by g.relation_game_id
");
if ($res) {
5 years ago
if (!isset($list[$item['p_id']])) {
// 奖罚金额
$reward = M('reward_record', 'tab_')
->field('sum(money) as money, reward_type')
->where("company_type = 2 and company_id = {$item['p_id']} and reward_time between {$time_start} and {$time_end}")
->group('reward_type')
->select();
if (!empty($reward)) {
$reward = array_column($reward, 'money', 'reward_type');
}
$list[$item['p_id']] = [
'company_id'=>$item['p_id'],
'company_name'=>$item['company_name'],
'fax_ratio' => $item['fax_ratio'],
'bonuses'=>isset($reward[1]) ? $reward[1] : 0,
'fine'=>isset($reward[2]) ? $reward[2] : 0 ,
'settlement_contact' => $item['settlement_contact'],
'create_time' => time(),
'time_start' => date('Y-m-d', $time_start),
'time_end' => date('Y-m-d', $time_end)
5 years ago
];
5 years ago
$list[$item['p_id']]['total_amount'] = floor(($list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'])*100)/100;
5 years ago
}
5 years ago
foreach ($res as $k => $val) {
$gameRatio = getPromoteGameRatio($item['id'], $val['relation_game_id'], $val['amount'], $time_start, $time_end, true);
if ($settlement_time_type == 2) {
$gameRatioMax = getPromoteGameRatio($item['id'], $val['relation_game_id'], $val['amount'], $time_start, $time_end, false);
} else {
$gameRatioMax = $gameRatio;
}
$price = $val['amount']*$gameRatioMax/100 - $val['amount']*$item['fax_ratio']/100;
$list[$item['p_id']]['channels'][] = [
'promote_id' => $item['id'],
'account'=>$item['account'],
'relation_game_name'=>$val['relation_game_name'],
'relation_game_id'=>$val['relation_game_id'],
'game_ratio'=>$gameRatio,
'game_ratio_max'=>$gameRatioMax-$gameRatio,
5 years ago
'price'=> $price,
5 years ago
'amount' => $val['amount']
];
5 years ago
$list[$item['p_id']]['total_amount'] += $price;
5 years ago
}
}
}
} else if ($settlement_type == 4) {
$map = "p.account_type = 2 and p.`level` = 1 " ;
if ($settlement_time_type == 1) {
$map .= " and p.settlement_type = {$settlement_time_type} ";
}
$result = M()->query("
SELECT
p.id, p.account, pc.company_name, pc.id as p_id, company_relation, p.admin_id, pc.bank_card, pc.bank_cardname, pc.bank_name, pc.bank_address
FROM
tab_promote p
INNER JOIN tab_promote_company pc on p.company_id = pc.id
where {$map}
order by pc.id asc
");
$list = [];
foreach ($result as $key => $item) {
$res = M()->query("
select
5 years ago
g.relation_game_name, g.relation_game_id, TRUNCATE(sum(s.pay_amount), 2) as amount
5 years ago
from
tab_spend s
5 years ago
inner join tab_promote p on (p.chain like '/{$item['id']}/%' or p.id = {$item['id']}) and s.promote_id = p.id
5 years ago
inner join tab_game g on s.game_id = g.id
where s.pay_status = 1 and s.pay_time BETWEEN {$time_start} and {$time_end}
group by g.relation_game_id
");
if ($res) {
5 years ago
if (!isset($list[$item['p_id']])) {
// 奖罚金额
$reward = M('reward_record', 'tab_')
->field('sum(money) as money, reward_type')
->where("company_type = 2 and company_id = {$item['p_id']} and reward_time between {$time_start} and {$time_end}")
->group('reward_type')
->select();
if (!empty($reward)) {
$reward = array_column($reward, 'money', 'reward_type');
}
$list[$item['p_id']] = [
'company_id'=>$item['p_id'],
'company_name'=>$item['company_name'],
'bonuses'=>isset($reward[1]) ? $reward[1] : 0,
'fine'=>isset($reward[2]) ? $reward[2] : 0 ,
'settlement_contact' => $item['settlement_contact'],
'bank_card' => $item['bank_card'],
'bank_address' => $item['bank_address'],
'create_time' => time(),
'time_start' => date('Y-m-d', $time_start),
'time_end' => date('Y-m-d', $time_end)
5 years ago
];
5 years ago
$list[$item['p_id']]['total_amount'] = floor(($list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'])*100)/100;
5 years ago
}
5 years ago
foreach ($res as $k => $val) {
$gameRatio = getPromoteGameRatio($item['id'], $val['relation_game_id'], $val['amount'], $time_start, $time_end, true);
if ($settlement_time_type == 2) {
$gameRatioMax = getPromoteGameRatio($item['id'], $val['relation_game_id'], $val['amount'], $time_start, $time_end, false);
} else {
$gameRatioMax = $gameRatio;
}
$price = $val['amount']*$gameRatioMax/100;
$list[$item['p_id']]['channels'][] = [
'promote_id' => $item['id'],
'account'=>$item['account'],
'relation_game_name'=>$val['relation_game_name'],
'relation_game_id'=>$val['relation_game_id'],
'game_ratio'=>$gameRatio,
'game_ratio_max'=>$gameRatioMax-$gameRatio,
5 years ago
'price'=> $price,
5 years ago
'amount' => $val['amount'],
'type_name' => '个人',
'company_relation_name' => $item['company_relation'] == 0 ? "自主开发及维护" : ($item['company_relation'] == 1 ? "只维护" : "无"),
'admin_name' => get_admin_nickname($item['admin_id']) ?: '无'
];
5 years ago
$list[$item['p_id']]['total_amount'] += $price;
5 years ago
}
}
}
} else {
5 years ago
$generate = false;
$msg = "请选择正确的结算单类型";
5 years ago
}
}
5 years ago
$this->assign("generate", $generate);
$this->assign('admin_users', M('member')->where(['status'=>1])->field('uid, nickname')->select());
5 years ago
$this->assign('listData', array_values($list));
$this->assign('msg', $msg);
$this->assign('settlement_type', $settlement_type);
$this->meta_title = '结算单管理';
5 years ago
$this->display('generatesettlementsheet');
5 years ago
} else {
// 判断结算时间是否冲突
5 years ago
if ($msg) {
return $this->error($msg, true, true);
}
if ($id) {
$info = M('settlement_sheet', 'tab_')->where(['id'=>$id])->find();
if (!$info) {
return $this->error('未找到该记录');
}
} else {
if (\sizeof($_POST['settlement_sheet']) == 0) {
return $this->error('无法生成无记录的结算单', true, true);
}
$isExsist = M('settlement_sheet', 'tab_')->where(['settlement_type'=>$settlement_type, 'time_start'=>$time_start, 'time_end'=>$time_end, 'settlement_time_type'=>$settlement_time_type])->find();
if ($isExsist) {
return $this->error('已存在此类型结算单', true, true);
}
$data['creater_id'] = is_login();
$data['create_time'] = time();
$data['orderid'] = $settlement_type.$settlement_time_type.date('YmdHis').$data['creater_id'].rand(1000, 9999);
$data['settlement_type'] = $settlement_type;
$data['settlement_time_type'] = $settlement_time_type;
$data['time_start'] = $time_start;
$data['time_end'] = $time_end;
$data['audit_users'] = json_encode($_POST['audit_users']);
$data['settlement_sheet'] = json_encode($_POST['settlement_sheet']);
$data['audit_user'] = $_POST['audit_users'][0];
5 years ago
$res = M('settlement_sheet', 'tab_')->add($data);
// 生成消息
$notice['user_id'] = $_POST['audit_users'][0];
$notice['content'] = "结算单汇总审核:有一个结算单待你审核,请尽快处理!";
$notice['type'] = 3;
$notice['status'] = 2;
$notice['create_time'] = time();
$notice['game_id'] = $res;
M('msg', 'tab_')->add($notice);
$GetData = $_POST;
addOperationLog(['op_type'=>0,'key'=>$data['orderid'],'op_name'=>'生成汇总结算单','url'=>U('settlement/generateSettlementSheet',$GetData),'menu'=>'推广员-结算单管理-生成汇总结算单']);
5 years ago
return $res ? $this->success('生成成功', true, true) : $this->error('生成失败', true, true);
}
5 years ago
}
}
5 years ago
public function sheetDetail($id=0)
5 years ago
{
5 years ago
$info = M('settlement_sheet', 'tab_')->where(['id'=>$id])->find();
if (!$info) {
return $this->error('未找到该记录');
}
$info['listData'] = json_decode($info['settlement_sheet'], true);
$info['audit_users'] = json_decode($info['audit_users']);
$login_uid = is_login();
$arr_index = array_search($login_uid, $info['audit_users']);
$audit_auth = true;
if ($arr_index === false) {
$audit_auth = false;
} else if ($arr_index + 1 != $info['step']) {
$audit_auth = false;
} else if ($arr_index + 1 == sizeof($info['audit_users']) && $info['status'] != 3) {
$audit_auth = false;
} else if ($info['status'] != 3) {
$audit_auth = false;
}
$this->assign(['audit_auth'=>$audit_auth]);
$this->assign($info);
$this->meta_title = '结算单管理';
$this->display();
}
5 years ago
5 years ago
public function auditSheet($id=0, $audit_type = 2, $remark = '' )
{
$info = M('settlement_sheet', 'tab_')->where(['id'=>$id])->find();
if (!$info) {
return $this->error('未找到该记录', true, true);
}
$info['audit_users'] = json_decode($info['audit_users']);
$login_uid = is_login();
$arr_index = array_search($login_uid, $info['audit_users']);
if ($arr_index === false) {
return $this->error('无权审核改结算单', true, true);
} else if ($arr_index + 1 != $info['step']) {
return $this->error('您已审核过此结算单', true, true);
} else if ($arr_index + 1 == sizeof($info['audit_users']) && $info['status'] != 3) {
return $this->error('该结算单已经审核完毕', true, true);
}
5 years ago
$is_last = ($info['step'] >= sizeof($info['audit_users']));
5 years ago
$update['remark'] = $remark;
$update['status'] = $audit_type == 1 ? ($is_last ? 1 : 3) : 2;
$update['step'] = ($audit_type == 1 && !$is_last) ? $info['step'] + 1 : $info['step'];
$update['audit_user'] = $login_uid;
if ($audit_type == 1 && $is_last) {
$update['all_status'] = 1;
} else if ($audit_type == 2) {
$update['all_status'] = 2;
}
// 通知审核
if ($update['status'] == 3 && !$is_last) {
$notice['user_id'] = $info['audit_users'][$info['step']];
$notice['content'] = "结算单汇总审核:有一个结算单待你审核,请尽快处理!";
$notice['type'] = 3;
$notice['status'] = 2;
$notice['create_time'] = time();
$notice['game_id'] = $id;
M('msg', 'tab_')->add($notice);
}
5 years ago
$upt = M('settlement_sheet', 'tab_')->where(['id'=>$id])->save($update);
return $upt ? $this->success('审核成功', true, true) : $this->error('审核失败', true, true);
5 years ago
}
}