meta_title = '结算单管理'; if (IS_GET) { $msg = ''; if (empty($settlement_type)) { $msg = '请选择汇总结算单类型'; } if (empty($settlement_time_type)) { $msg = '请选择结算方式'; } if (empty($time_start) || empty($time_end)) { $msg = '请选择正确的时间区间'; } // 判断结算时间是否冲突 // 生成结算单数据 if (!$msg) { if ($settlement_type == 1) { if ($settlement_time_type == 1) { $map = " and p.settlement_type = 1"; } else { $map = ''; } $result = M('spend', 'tab_')->query(" SELECT sum(s.pay_amount) 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 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'], // 税费 ]; } $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']; $data['parter_settlement'] = $item['amount'] * $data['game_ratio']/100 - ($item['amount']*$item['channel_rate'] ) + $list[$item['p_id']]['bonuses'] - $list[$item['p_id']]['fine']; $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']]); } } 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 g.relation_game_name, g.relation_game_id, sum(s.pay_amount) as amount from tab_spend s inner join tab_promote p on p.chain like '/{$item['id']}/%' and s.promote_id = p.id 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 (!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) { 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, 'price'=> $price, 'amount' => $val['amount'] ]; $list[$item['p_id']]['total_amount'] += $price; } } } } 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 g.relation_game_name, g.relation_game_id, sum(s.pay_amount) as amount from tab_spend s inner join tab_promote p on p.chain like '/{$item['id']}/%' and s.promote_id = p.id 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 (!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) { 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, 'price'=> $price, 'amount' => $val['amount'], 'type_name' => '个人', 'company_relation_name' => $item['company_relation'] == 0 ? "自主开发及维护" : ($item['company_relation'] == 1 ? "只维护" : "无"), 'admin_name' => get_admin_nickname($item['admin_id']) ?: '无' ]; $list[$item['p_id']]['total_amount'] += $price; } } } } else { } } $this->assign('listData', array_values($list)); $this->assign('msg', $msg); $this->assign('settlement_type', $settlement_type); $this->meta_title = '结算单管理'; $this->display(); } else { // 判断结算时间是否冲突 } } public function settlementSheetDetail($id=0) { } }