@ -7,30 +7,117 @@ namespace Admin\Controller;
class SettlementController extends ThinkController
{
// 汇总结算单列表
public function getSettlementSheetList( )
public function sheetList($row = 10, $p = 1 )
{
$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('create_time_start')) {
$map['_string'] = "create_time >= ".strtotime(I('create_time_start'));
}
if (I('create_time_end')) {
$map['_string'] = "create_time < = ".strtotime(I('create_time_end'));
}
$list = M('settlement_sheet', 'tab_')->where($map)->page($p, $row)->select();
$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;
}
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();
}
// 生成结算单 月结算补点 周结不算
public function generateSettlementSheet($settlement_type = 0, $settlement_time_type = 0, $time_start=0, $time_end = 0)
public function generateSettlementSheet($settlement_type = 0, $settlement_time_type = 0, $time_start=0, $time_end = 0, $delete=0, $id=0, $action='' )
{
$this->meta_title = '结算单管理';
$this->assign(I(''));
$this->assign('action', $action);
if ($delete) {
$res = M('settlement_sheet', 'tab_'->where(['id'=>$id]))->delete();
return $res ? $this->success('删除成功', true, true) : $this->success('删除失败', true, true);
}
$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 (empty($time_start) || empty($time_end)) {
$msg = '请选择正确的时间区间';
}
if (IS_GET) {
$msg = '';
if (empty($settlement_type)) {
$msg = '请选择汇总结算单类型';
}
if (empty($settlement_time_type)) {
$msg = '请选择结算方式';
}
if (empty($time_start) || empty($time_end)) {
$msg = '请选择正确的时间区间';
}
// 判断结算时间是否冲突
// 生成结算单数据
$generate = false;
if (!$msg) {
$generate = true;
if ($settlement_type == 1) {
if ($settlement_time_type == 1) {
$map = " and p.settlement_type = 1";
@ -114,30 +201,29 @@ class SettlementController extends ThinkController
where s.pay_status = 1 and s.pay_time BETWEEN {$time_start} and {$time_end}
group by g.relation_game_id
");
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()
];
$list[$item['p_id']]['total_amount'] = $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'];
}
if ($res) {
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()
];
$list[$item['p_id']]['total_amount'] = $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'];
}
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) {
@ -186,31 +272,30 @@ class SettlementController extends ThinkController
where s.pay_status = 1 and s.pay_time BETWEEN {$time_start} and {$time_end}
group by g.relation_game_id
");
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()
];
$list[$item['p_id']]['total_amount'] = $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'];
}
if ($res) {
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()
];
$list[$item['p_id']]['total_amount'] = $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine'];
}
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) {
@ -238,9 +323,12 @@ class SettlementController extends ThinkController
}
}
} else {
$generate = false;
$msg = "请选择正确的结算单类型";
}
}
$this->assign("generate", $generate);
$this->assign('admin_users', M('member')->where(['status'=>1])->field('uid, nickname')->select());
$this->assign('listData', array_values($list));
$this->assign('msg', $msg);
$this->assign('settlement_type', $settlement_type);
@ -248,11 +336,84 @@ class SettlementController extends ThinkController
$this->display();
} else {
// 判断结算时间是否冲突
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']);
$res = M('settlement_sheet', 'tab_')->add($data);
return $res ? $this->success('生成成功', true, true) : $this->error('生成失败', true, true);
}
}
}
public function settlementSheetDetail($id=0)
public function sheetDetail($id=0)
{
$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();
}
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);
}
$update['remark'] = $remark;
$update['status'] = $audit_type == 1 ? 3 : 2;
$update['step'] = $audit_type == 1 ? $info['step'] + 1 : $info['step'];
$upt = M('settlement_sheet', 'tab_')->where(['id'=>$id])->save($update);
return $upt ? $this->success('审核成功', true, true) : $this->error('审核失败', true, true);
}
}