<?php
namespace Admin\Controller;
/**
* 聚合下游结算
* @author 王贺
*/
class AggregateFinanceStatementController extends ThinkController
{
public $verify_status = [
"-4"=>"汇总审批拒绝",
"-3"=>"撤销汇总",
"-1"=>"审批拒绝",
"0"=>"未审批",
"1"=>"申请开票",
"2"=>"审批同意",
"3"=>"已开票",
"4"=>"已上传凭证",
"5"=>"已到账",
"6"=>"已汇总",
"7"=>"汇总审批同意",
];
public $admininfo;
public $OpAuthList=[];
//TODO:
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
parent::_initialize();
}
public function lists() {
$is_export= false;
if (isset($_REQUEST['export']) & & $_REQUEST['export']==1){
$is_export = true;
}
//权限
if(!IS_ROOT){
$this->getAuth();
}
//分页
$page = intval(I('get.p', 0));
$page = $page ? $page : 1; //默认显示第一页数据
$row = intval(I('row', 0));
$row = empty($row) ? 10 : $row;//每页条数
$map = [];
if (isset($_REQUEST['time_start']) & & isset($_REQUEST['time_end'])) {
$time_start = strtotime($_REQUEST['time_start']);
$time_end = strtotime($_REQUEST['time_end'])+ 86399;
$map["_string"] = "(begintime BETWEEN {$time_start} AND {$time_end}) OR (endtime BETWEEN {$time_start} AND {$time_end})";
} elseif (isset($_REQUEST['time_start'])) {
$time_start = strtotime($_REQUEST['time_start']);
$map["_string"] = "(begintime >= {$time_start} ) OR (endtime >= {$time_start})";
} elseif (isset($_REQUEST['time_end'])) {
$time_end = strtotime($_REQUEST['time_end'])+ 86399;
$map["_string"] = "(begintime < = {$time_end} ) OR (endtime < = {$time_end})";
}
if (isset($_REQUEST['time_start2']) & & isset($_REQUEST['time_end2'])) {
$map['create_time'] = ['between', [strtotime($_REQUEST['time_start2']), strtotime($_REQUEST['time_end2']) + 86399]];
} elseif (isset($_REQUEST['time_start2'])) {
$map['create_time'] = ['EGT', strtotime($_REQUEST['time_start2'])];
} elseif (isset($_REQUEST['time_end2'])) {
$map['create_time'] = ['ELT', strtotime($_REQUEST['time_end2']) + 86399];
}
if (isset($_REQUEST['verify_status'])) {
$map['verify_status'] = $_REQUEST['verify_status'];
}
if (isset($_REQUEST['channel_id'])){
$map['channel_id'] = $_REQUEST['channel_id'];
}
$typeflag = false;
if (isset($_REQUEST['withdraw_type'])) {
if($_REQUEST['withdraw_type'] == 1){
$typeflag = 1;//月结
$map["withdraw_type"] = 1;
}else{
$typeflag = 2;//周结
$map["withdraw_type"] = ["in",[0,2]];
}
}
if (isset($_REQUEST['withdraw_type2'])) {
if($_REQUEST['withdraw_type2'] == 2){
if($typeflag == 1){ //月结的补点找不到
$map['withdraw_type'] = 999;
}else{ //周结的补点就是补点
$map['withdraw_type'] = 2;
}
}else{
if($typeflag == 1){//月结的正常=月结
$map['withdraw_type'] = 1;
}elseif($typeflag == 2){//周结的正常=周结
$map['withdraw_type'] = 0;
}else{//没选的正常是 非补点
$map['withdraw_type'] = ["in",[0,1]];
}
}
}
$data = M("aggregate_statement","tab_")
->where($map)
->order("create_time Desc")->page($page,$row)->select();
foreach($data as $key => $value) {
$data[$key]['verify_status_str'] = $this->verify_status[$value['verify_status']];
$data[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
$data[$key]['begintime'] = date('Y-m-d',$value['begintime']);
$data[$key]['endtime'] = date('Y-m-d',$value['endtime']);
$data[$key]['withdraw_type_1'] = ($value['withdraw_type'] == 1 ? "月结" :"周结");
$data[$key]['withdraw_type_2'] = ($value['withdraw_type'] == 2 ? "补点" :"正常结算");
$data[$key]['detail'] = json_decode($value['detail'],true);
$data[$key]['detail']['pool'] = str_replace(")",")< br > ",$data[$key]['detail']['pool']);
$data[$key]['detail']['apply'] = str_replace(")",")< br > ",$data[$key]['detail']['apply']);
$data[$key]['detail']['invoiced'] = str_replace(")",")< br > ",$data[$key]['detail']['invoiced']);
$data[$key]['detail']['received'] = str_replace(")",")< br > ",$data[$key]['detail']['received']);
$data[$key]['oplist'] = $this->OpAuth($value);
}
// dump($data);die();
$count = M("aggregate_statement","tab_")->field("count(id) count,SUM(ratio_money) ratio_money")->where($map)->find();
$map['withdraw_type'] = ["NEQ",2];
$pay_money_count = M("aggregate_statement","tab_")->field("SUM(pay_money) pay_money")->where($map)->find()['pay_money'];
//分页
$parameter['p'] = $page;
$parameter['row'] = $row;
$page = set_pagination($count['count'], $row, $parameter);
if ($page) {
$this->assign('_page', $page);
}
//
$this->assign('menubtn',$this->menuAuth());
$this->assign('data',$data);
$this->assign('total',$count['ratio_money']);
$this->assign('pay_money',$pay_money_count);
$this->assign('channel',$this->getAggChannel());
$this->assign('verify_status',$this->verify_status);
$this->display();
}
//获取所有下游渠道
public function getAggChannel()
{
return M("aggregate_statement","tab_")->field("channel_id,channel_name")->group("channel_id")->select();
}
//申请开票
public function createStatement(){
if (isset($_REQUEST['id'])){
$id = $_REQUEST['id'];
}else{
$this->error("参数错误");
}
$info = M("aggregate_statement","tab_")->field("statement_info,pay_money,ratio_money,channel_id")->where("id={$id}")->find();
$company_info = M("company_relation","tab_")->field("first_company_id,second_company_id,first_company_type,collection")->where("first_company_id={$info['channel_id']} or second_company_id={$info['channel_id']}")->find();
if($company_info['first_company_type'] == 3) {
$first_partner_type = 1;
} else {
$first_partner_type = 0;
}
$this->setAggregateDetail(2,1,'申请开票',$id);
if ($company_info['collection'] == 1) {
$pay_company_id = 0;
$this->assign('first_channel_id',$id);
$this->assign('second_company_id',$id);
} else if ($company_info['collection'] == 2) {
$this->assign('first_channel_id',$id);
$this->assign('second_company_id',$id);
$pay_company_id = 1;
}
// dump($pay_company_id);die();
$this->assign("pay_company_id",$pay_company_id);
$this->assign('first_partner_type',$first_partner_type);
$statement_info = json_decode($info['statement_info'],true);
$this->assign('company_info',$company_info);
$this->assign('data',$statement_info);
$this->assign('data_count',["pay_money"=>$info['pay_money'],"ratio_money"=>$info['ratio_money']]);
$this->assign('id',$id);
$this->display();
}
public function doAddStatement(){
$first_partner_type = $_REQUEST['first_partner_type'];
$second_party_info = $_REQUEST['second_party_info'];
$statement_info = $_REQUEST['statement_info'];
$admininfo = $_SESSION['onethink_admin']['user_auth'];
$data_count = $_REQUEST['statement_count'];
if(!isset($_REQUEST['statement_id'])){
$this->error("参数错误");
}
if (!$_REQUEST['first_party_info']['id']& & !$_REQUEST['first_partner_type']) {
$this->error("编辑失败,聚合公司绑定关系未添加,请添加公司绑定关系后重算");
}
if ($_REQUEST['is_edit']) {
$verify_status = 0;
} else {
$verify_status = 1;
}
//拼凑数据
$adddata = array(
"pay_money"=>$data_count['pay_money'],
"ratio_money"=>$data_count['ratio_money'],
"pay_type"=>$_REQUEST['statement_type'] == 0 ? 1 :0,
"first_party_info"=>json_encode($_REQUEST['first_party_info'],JSON_UNESCAPED_UNICODE),
"second_party_info"=>json_encode($_REQUEST['second_party_info'],JSON_UNESCAPED_UNICODE),
"statement_info"=>json_encode($statement_info,JSON_UNESCAPED_UNICODE),
"admin_name"=>$admininfo['username'],
"admin_id"=>$admininfo['uid'],
"verify_status"=>$verify_status,
"create_time"=>time()
);
$id = $_REQUEST['statement_id'];
$res = M("aggregate_statement","tab_")->where("id = '{$id}'")->save($adddata);
if($res !== false){
$this->doAddOperationLog($id,"申请开票");
$this->ajaxReturn(array("success"=>"ok","code"=>0));
}else{
$this->ajaxReturn(array("error"=>"database error","code"=>2000));
}
}
//获取公司信息
public function getCompanyList()
{
$id = $_REQUEST['id'];
$cpCompany = M("aggregate_statement","tab_")->field("id,first_party_info,second_party_info")->where("id={$id}")->find();
$data = array();
// $data['ptCompany']= M("CompanyInfo","tab_")->field('id,partner')->select();
$data['ptCompany']=[[
"id"=> $cpCompany['id'],
"partner"=>json_decode($cpCompany['first_party_info'],true)['partner']
]];
$data['cpCompany']= [[
"id"=> $cpCompany['id'],
"partner"=>json_decode($cpCompany['second_party_info'],true)['partner']
]];
$this->ajaxReturn(array("success"=>$data,"code"=>2000));
}
public function getCpCompanyInfo()
{
$CompanyId = $_REQUEST['company_id'];
$company_type = $_REQUEST['company_type'];
if($company_type == 'pt'){
// $info = M("CompanyInfo","tab_")->field("id,partner,link_man,link_phone,address,company_tax_no,payee_name,bank_account,opening_bank")->where("id = '{$CompanyId}'")->find();
$cpCompany = M("aggregate_statement","tab_")->field("channel_id id,first_party_info,channel_id")->where("id={$CompanyId}")->find();
$info = json_decode($cpCompany['first_party_info'],true);
}else{
$cpCompany = M("aggregate_statement","tab_")->field("channel_id id,second_party_info,channel_id")->where("id={$CompanyId}")->find();
$info = json_decode($cpCompany['second_party_info'],true);
// $relation = M("company_relation","tab_")->where("(first_company_id={$cpCompany['channel_id']} and first_company_type=3) or (second_company_id = {$cpCompany['channel_id']} and first_company_type=3)")->find();
// $info['link_man'] = $info['connection_person'];
// $info['link_phone'] = $info['phone'];
// $info['address'] = $info['send_address'];
// $info['company_tax_no'] = $info['tax_identification_number'];
//
// $info['payee_name'] = $info['bank_user'];
// $info['bank_account'] = $info['bank_card'];
// $info['company_tax_no'] = $info['tax_identification_number'];
}
// $info['invoice_item'] = "信息技术服务费";
// $info['invoice_type'] = "增值税专用发票";
//
// if ($relation['invoice_type'] == 1) {
// $info['invoice_type'] = "专票";
// } else if($relation['invoice_type'] == 2) {
// $info['invoice_type'] = "普票";
// }
// if($relation['invoice_content']) {
// $info['invoice_item'] = $relation['invoice_content'];
// }
if(empty($info)){
$this->ajaxReturn(array("error"=>"no find","code"=>2000));
}else{
$this->ajaxReturn(array("success"=>$info,"code"=>0));
}
}
public function editStatement(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$dbres = M("aggregate_statement","tab_")->where("id = '{$id}'")->find();
$first_party_info = json_decode($dbres['first_party_info'],true);
$second_party_info = json_decode($dbres['second_party_info'],true);
$statement_info = json_decode($dbres['statement_info'],true);
// if($dbres['company_name'] == $first_party_info['partner']){
// $first_partner_type=0;
// }else{
// $first_partner_type=1;
// }
// $fine = array_pop($statement_info);
$senddata = array(
// "first_partner_type"=>1,
"statement_id"=>$dbres['id'],
"first_part_company"=>$first_party_info['partner'],
"second_part_company"=>$second_party_info['partner'],
"statement_type"=>$dbres['pay_type'] == 0 ? 1 :0,
"first_party_info"=>$first_party_info,
"second_party_info"=>$second_party_info,
"statement_info"=>$statement_info,
"statement_count"=>array("pay_money"=>$dbres['pay_money'],"ratio_money"=>$dbres['ratio_money']),
"is_edit"=>1,
);
$this->assign("data",$senddata);
if($dbres['pay_type'] == 0){
$this->assign("company",$second_party_info['partner']);
}else{
$this->assign("company",$first_party_info['partner']);
}
$this->assign("company_id",$dbres['company_id']);
$this->assign("company_name",$dbres['company_name']);
$this->assign("id",$dbres['id']);
$this->display();
}
//查看发票信息
public function viewStatement(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$dbres = M("aggregate_statement","tab_")->where("id = '{$id}'")->find();
$first_party_info = json_decode($dbres['first_party_info'],true);
$second_party_info = json_decode($dbres['second_party_info'],true);
$statement_info = json_decode($dbres['statement_info'],true);
if($dbres['pay_type'] == 0){
$company = $second_party_info['partner'];
$pyinfo = $second_party_info;
}else{
$company = $first_party_info['partner'];
$pyinfo = $first_party_info;
}
$senddata = array(
"company"=>$company,
"payinfo"=>$pyinfo,
"first_part_company"=>$first_party_info['partner'],
"second_part_company"=>$second_party_info['partner'],
"statement_type"=>$dbres['pay_type'],
"first_party_info"=>$first_party_info,
"second_party_info"=>$second_party_info,
"statement_info"=>$statement_info,
"statement_count"=>array("pay_money"=>$dbres['pay_money'],"ratio_money"=>$dbres['ratio_money'],"big_ratio_money"=>convertAmountToCn($dbres['ratio_money']))
);
$this->assign("data",$senddata);
$this->display();
}
//上传凭证
public function uploadVoucher()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$this->assign("id",$id);
$this->display();
}
//修改凭证
public function editVoucher()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$info = M("aggregate_statement","tab_")->field("id,verify_status,ext_field")->where("id = '{$id}'")->find();
$this->assign("id",$id);
$this->assign("ext_field",$info['ext_field']);
$this->display();
}
//保存凭证
public function saveVoucher()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$path = '/Uploads/';
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 0 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');;// 设置附件上传类型
$upload->rootPath = '.'.$path; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$msg = $upload->getError();
$array= array('status' => 0, 'info' => $msg);
}else{// 上传成功
//
$pf = $path.$info['file']['savepath'].$info['file']['savename'];
$save = [
"ext_field"=>$pf,
];
//获取旧值,存在删除
$info = M("aggregate_statement","tab_")->field("id,verify_status,ext_field")->where("id = '{$id}'")->find();
if(!empty($info['ext_field'])){
unlink("./".$info['ext_field']);
}
//如果是开票状态就改状态
if($info["verify_status"] == 3){
$save['verify_status'] = 4;
$save['create_time'] = time();
$save["admin_name"]=$this->admininfo["username"];
$save["admin_id"]=$this->admininfo["uid"];
}
M("aggregate_statement","tab_")->where("id = '{$id}'")->save($save);
$this->doAddOperationLog($id,"上传凭证");
$array=array(
'status' => 1,
"info"=>"上传成功",
"file_path"=>$pf,
);
}
$this->ajaxReturn($array);
}
//删除凭证
public function delVoucher(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$info = M("aggregate_statement","tab_")->field("id,ext_field,verify_status")->where("id = '{$id}'")->find();
if(!empty($info['ext_field'])){
unlink("./".$info['ext_field']);
}
$save = [
"ext_field"=>'',
];
//如果是已上传凭证状态就回退到审核通过
if($info["verify_status"] == 4){
$save['verify_status'] = 3;
$save['create_time'] = time();
$save["admin_name"]=$this->admininfo["username"];
$save["admin_id"]=$this->admininfo["uid"];
}
M("aggregate_statement","tab_")->where("id = '{$id}'")->save($save);
$this->doAddOperationLog($id,"删除凭证");
$this->ajaxReturn(array(
'status' => 1,
'info' => "删除成功"
));
}
//新增备注
public function saveRemark(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$remark = $_REQUEST['remark'];
M("aggregate_statement","tab_")->where("id = '{$id}'")->save(['remark'=>$remark]);
$this->doAddOperationLog($id,"新增备注");
$this->ajaxReturn(array(
'status' => 1,
'info' => "操作成功"
));
}
//审批通过
public function agreeApply(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$aggregate_statement = M("aggregate_statement","tab_")->where("id in ({$ids})")->select();
foreach($aggregate_statement as $key => $value) {
if (!$value['first_party_info']) {
$this->ajaxReturn(array(
'status' => 1,
"info"=>"审批失败,聚合公司绑定关系未添加,请添加公司绑定关系后重算"
));
}
}
$savedata = ["verify_status"=>2,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
M("aggregate_statement","tab_")->where("id in ({$ids})")->save($savedata);
$ids = explode(",",$ids);
foreach($ids as $k=>$v){
$this->doAddOperationLog($v,"审批通过");
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"审批通过成功"
));
}
//审核拒绝
public function refuseApply(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$aggregate_statement = M("aggregate_statement","tab_")->where("id in ({$ids})")->select();
foreach($aggregate_statement as $key => $value) {
if (!$value['first_party_info']) {
$this->ajaxReturn(array(
'status' => 1,
"info"=>"审批失败,存在没有绑定关系的对账单,请绑定关系后重算"
));
}
}
$savedata = ["verify_status"=>-1,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
M("aggregate_statement","tab_")->where("id in ({$ids})")->save($savedata);
$ids = explode(",",$ids);
foreach($ids as $k=>$v){
$this->doAddOperationLog($v,"审批拒绝");
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"审批拒绝成功"
));
}
//已开票
public function Invoiced(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$savedata = ["verify_status"=>3,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
M("aggregate_statement","tab_")->where("id in ({$ids})")->save($savedata);
$ids = explode(",",$ids);
foreach($ids as $k=>$v){
$this->setAggregateDetail(2,1,'已开票',$v);
$this->doAddOperationLog($v,"已开票");
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"已开票成功"
));
}
//已到账
public function Received(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$savedata = ["verify_status"=>5,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
M("aggregate_statement","tab_")->where("id in ({$ids})")->save($savedata);
$ids = explode(",",$ids);
foreach($ids as $k=>$v){
$this->setAggregateDetail(3,1,'已到账',$v);
$this->doAddOperationLog($id,"已到账");
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"已到账成功"
));
}
//重算金额
public function updateStatement()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = explode(",",$_REQUEST['ids']);
foreach ($ids as $k => $v) {
$res = A("AggregateFinanceSet")->updateAggregateFinanceData($v);
if(!$res){
$this->ajaxReturn(array(
'status' => 0,
"info"=>"重算结算金额失败"
));
}else{
M("aggregate_statement","tab_")->where(['id'=>$v])->save(['detail'=>'']);
$this->doAddOperationLog($v,"重算结算金额");
}
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"算结算金额成功"
));
}
//撤销审核
public function cancelVerify(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$savedata = ["verify_status"=>0,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
M("aggregate_statement","tab_")->where("id ='{$id}'")->save($savedata);
$this->doAddOperationLog($id,"撤销审核");
$this->ajaxReturn(array(
'status' => 1,
"info"=>"撤销审核成功"
));
}
//撤销申请
public function cancelApply(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$data = M("aggregate_statement","tab_")->where("id = '{$id}'")->find();
$savedata = ["verify_status"=>7,"create_time"=>time(),"admin_name"=>$this->admininfo["username"],"admin_id"=>$this->admininfo["uid"]];
$first_party_info = json_decode($data['first_party_info'], 1);
if($first_party_info['partner'] == $data['channel_name']){
$savedata['second_party_info'] = $data['first_party_info'];
$savedata['first_party_info'] = $data['second_party_info'];
$savedata['pay_type'] =0;
}
$this->setAggregateDetail(2,0,'撤销申请',$id);
M("aggregate_statement","tab_")->where("id ='{$id}'")->save($savedata);
$this->doAddOperationLog($id,"撤销申请");
$this->ajaxReturn(array(
'status' => 1,
"info"=>"撤销申请成功"
));
}
//导出
public function export()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$data = M("aggregate_statement","tab_")->where("id = '{$id}'")->find();
$data['first_party_info'] = json_decode($data['first_party_info'], 1);//甲方
$data['second_party_info'] = json_decode($data['second_party_info'], 1);//乙方
$data['statement_info'] = json_decode($data['statement_info'], 1);//结算记录
//收款方
$big_pay_money = convertAmountToCn($data['ratio_money']);//大写
if($data['pay_type'] == 0){
//甲-乙
$data['pay_company'] = $data['second_party_info'];
$data['invoice'] = $data['first_party_info'];
}else{
$data['pay_company'] = $data['first_party_info'];
$data['invoice'] = $data['second_party_info'];
}
$this->doAddOperationLog($id,"导出");
$relation = M("company_relation","tab_")->where("(first_company_id={$data['channel_id']} and first_company_type=3) or (second_company_id = {$data['channel_id']} and second_company_type=3)")->find();
$company_type = 0;
$company_info = [];
if ($relation['collection'] == 1) {
$company_info = json_decode($relation['first_company_info'],true);
} elseif($relation['collection'] == 2) {
$company_info = json_decode($relation['second_company_info'],true);
}
if ($company_info['invoice_type']) {
$company_type = 1;
} else {
$company_type = 2;
}
if ($company_type == 2) {
$this->excelDownStreamPersonTemplate($data,$big_pay_money);
} else {
$this->excelDownStreamTemplate($data,$big_pay_money);
}
}
public function excelDownStreamTemplate($data,$big_all_sum_money) {
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 / > ');
date_default_timezone_set('Asia/Shanghai');
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
//设置模板文件
$objPHPExcel = $objReader->load("Public/Admin/excel/aggregate_stream.xls");
$statement_begin_time = date('Y.m.d', $data['begintime']);//对账开始时间
$statement_end_time = date('Y.m.d',$data['endtime']);//对账截止时间
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "《 {$data['channel_name']} 》结算单\n( {$statement_begin_time}-{$statement_end_time}) ");
if ($data['withdraw_type'] == '2') {
$objPHPExcel->getActiveSheet()->setCellValue('D2','补点比例');
}
$line = 3;
$pay_amount_str = "=SUM(F3:";
$plat_amount_str = "=SUM(C3:";
foreach ($data['statement_info'] as $key => $value) {
$objPHPExcel->getActiveSheet()->insertNewRowBefore($line,1);
$objPHPExcel->getActiveSheet()->removeConditionalStyles();
$objPHPExcel->getActiveSheet()->setCellValue('A'.$line, $value['begintime'] . '-' . $value['endtime']);
$product_name = $value['game_name'];
$objPHPExcel->getActiveSheet()->setCellValue('D'.$line, $value['ratio'] . '%');
$objPHPExcel->getActiveSheet()->setCellValue('E'.$line, $value['fax_ratio'] . '%');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$line, $value['money']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$line, $product_name);
// $objPHPExcel->getActiveSheet()->setCellValue('F'.$line, $value['ratio_money']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$line,"=ROUND(C{$line}*D{$line}*(1-E{$line}),2)");
$objPHPExcel->getActiveSheet()->getStyle('A'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('B'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('C'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('D'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('E'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('F'.$line)->getFont()->setBold(false);
$line++;
}
$objPHPExcel->getActiveSheet()->setCellValue('C'.($line), $plat_amount_str."C".($line-1).")");//合计-平台总额
$objPHPExcel->getActiveSheet()->setCellValue('F'.($line), $pay_amount_str."F".($line-1).")");//合计-结算金额
// $objPHPExcel->getActiveSheet()->setCellValue('C'.($line), $data['pay_money']);//合计-平台总额
// $objPHPExcel->getActiveSheet()->setCellValue('F'.($line), $data['ratio_money']);//合计-结算金额
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+1), $big_all_sum_money);//大写支付结算金额
//甲方信息
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+3), $data['first_party_info']['partner']);//甲方
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+4), $data['first_party_info']['link_man']);//联系人
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+5), $data['first_party_info']['link_phone']);//联系电话
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+6), $data['first_party_info']['address']);//邮寄地址
// $objPHPExcel->getActiveSheet()->setCellValue('B'.($line+7), $data['second_party_info']['invoice_type']);//发票类型
//乙方信息
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+3), $data['second_party_info']['partner']);//乙方
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+4), $data['second_party_info']['link_man']);//联系人
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+5), $data['second_party_info']['link_phone']);//联系电话
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+6), $data['second_party_info']['address']);//邮寄地址
$height = 20;
$font_size = 56;
if (strlen($data['second_party_info']['partner'])< strlen ( $ data [ ' first_party_info ' ] [ ' partner ' ] ) ) {
$partner_height = intval(strlen($data['first_party_info']['partner'])/$font_size)+1;
} else {
$partner_height = intval(strlen($data['second_party_info']['partner'])/$font_size)+1;
}
if (strlen($data['second_party_info']['address'])< strlen ( $ data [ ' first_party_info ' ] [ ' address ' ] ) ) {
$address_height = intval(strlen($data['first_party_info']['address'])/$font_size)+1;
} else {
$address_height = intval(strlen($data['second_party_info']['address'])/$font_size)+1;
}
$objPHPExcel->getActiveSheet()->getStyle("E".($line+3).":E".($line+6))->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle("B".($line+3).":B".($line+6))->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getRowDimension(($line+3))->setRowHeight($height*$partner_height);
$objPHPExcel->getActiveSheet()->getRowDimension(($line+6))->setRowHeight($address_height*$height);
//开票信息
$invoice_data = "发票类型:".$data['invoice']['invoice_type'];
$invoice_data .= "\n发票抬头: ".$data['invoice']['partner'];
$invoice_data .= "\n纳税人识别号: ".$data['invoice']['company_tax_no'];
$invoice_data .= "\n开户银行: ".$data['invoice']['opening_bank'];
$invoice_data .= "\n开户账号: ".$data['invoice']['bank_account'];
$invoice_data .= "\n开票内容: ".$data['invoice']['invoice_item'];
$invoice_data .= "\n注册地址及电话: ".$data['invoice']['register_address'].",".$data['second_party_info']['register_phone'];
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+7), $invoice_data);//乙方
$first_data = "户名:{$data['pay_company']['payee_name']}";
$first_data .= "\n账号: {$data['pay_company']['bank_account']}";
$first_data .= "\n开户行: {$data['pay_company']['opening_bank']}";
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+8), $first_data);//户名
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+8), $data['pay_company']['payee_name']);//户名
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+9), $data['pay_company']['bank_account']);//银行账号
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+10), $data['pay_company']['opening_bank']);//开户行
if($data['withdraw_type'] == 1){
$m = date('Y.m', $data['begintime']);
$fileName = "{$data['second_party_info']['partner']}&{$data['first_party_info']['partner']}-对账单{$m}";
}else{
$fileName = "{$data['second_party_info']['partner']}&{$data['first_party_info']['partner']}-对账单{$statement_begin_time}-{$statement_end_time}";
}
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');
}
public function excelDownStreamPersonTemplate($data,$big_all_sum_money) {
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 / > ');
date_default_timezone_set('Asia/Shanghai');
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
//设置模板文件
$objPHPExcel = $objReader->load("Public/Admin/excel/aggregate_person_stream.xls");
$statement_begin_time = date('Y.m.d', $data['begintime']);//对账开始时间
$statement_end_time = date('Y.m.d',$data['endtime']);//对账截止时间
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "《 {$data['channel_name']} 》结算单\n( {$statement_begin_time}-{$statement_end_time}) ");
if ($data['withdraw_type'] == '2') {
$objPHPExcel->getActiveSheet()->setCellValue('D2','补点比例');
}
$line = 3;
$pay_amount_str = "=SUM(F3:";
$plat_amount_str = "=SUM(C3:";
foreach ($data['statement_info'] as $key => $value) {
$objPHPExcel->getActiveSheet()->insertNewRowBefore($line,1);
$objPHPExcel->getActiveSheet()->removeConditionalStyles();
$objPHPExcel->getActiveSheet()->setCellValue('A'.$line, $value['begintime'] . '-' . $value['endtime']);
$product_name = $value['game_name'];
$objPHPExcel->getActiveSheet()->setCellValue('D'.$line, $value['ratio'] . '%');
$objPHPExcel->getActiveSheet()->setCellValue('E'.$line, $value['fax_ratio'] . '%');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$line, $value['money']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$line, $product_name);
// $objPHPExcel->getActiveSheet()->setCellValue('F'.$line, $value['ratio_money']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$line,"=ROUND(C{$line}*D{$line}*(1-E{$line}),2)");
$objPHPExcel->getActiveSheet()->getStyle('A'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('B'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('C'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('D'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('E'.$line)->getFont()->setBold(false);
$objPHPExcel->getActiveSheet()->getStyle('F'.$line)->getFont()->setBold(false);
$line++;
}
$objPHPExcel->getActiveSheet()->setCellValue('C'.($line), $plat_amount_str."C".($line-1).")");//合计-平台总额
$objPHPExcel->getActiveSheet()->setCellValue('F'.($line), $pay_amount_str."F".($line-1).")");//合计-结算金额
// $objPHPExcel->getActiveSheet()->setCellValue('C'.($line), $data['pay_money']);//合计-平台总额
// $objPHPExcel->getActiveSheet()->setCellValue('F'.($line), $data['ratio_money']);//合计-结算金额
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+1), $big_all_sum_money);//大写支付结算金额
//甲方信息
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+3), $data['first_party_info']['partner']);//甲方
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+4), $data['first_party_info']['link_man']);//联系人
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+5), $data['first_party_info']['link_phone']);//联系电话
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+6), $data['first_party_info']['address']);//邮寄地址
// $objPHPExcel->getActiveSheet()->setCellValue('B'.($line+7), $data['second_party_info']['invoice_type']);//发票类型
//乙方信息
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+3), $data['second_party_info']['partner']);//乙方
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+4), $data['second_party_info']['link_man']);//联系人
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+5), $data['second_party_info']['link_phone']);//联系电话
$objPHPExcel->getActiveSheet()->setCellValue('E'.($line+6), $data['second_party_info']['address']);//邮寄地址
$height = 20;
$font_size = 44;
if (strlen($data['second_party_info']['partner'])< strlen ( $ data [ ' first_party_info ' ] [ ' partner ' ] ) ) {
$partner_height = intval(strlen($data['first_party_info']['partner'])/$font_size)+1;
} else {
$partner_height = intval(strlen($data['second_party_info']['partner'])/$font_size)+1;
}
if (strlen($data['second_party_info']['address'])< strlen ( $ data [ ' first_party_info ' ] [ ' address ' ] ) ) {
$address_height = intval(strlen($data['first_party_info']['address'])/$font_size)+1;
} else {
$address_height = intval(strlen($data['second_party_info']['address'])/$font_size)+1;
}
$objPHPExcel->getActiveSheet()->getStyle("E".($line+3).":E".($line+6))->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle("B".($line+3).":B".($line+6))->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getRowDimension(($line+3))->setRowHeight($height*$partner_height);
$objPHPExcel->getActiveSheet()->getRowDimension(($line+6))->setRowHeight($address_height*$height);
//开票信息
// $invoice_data = "发票类型:".$data['second_party_info']['invoice_type'];
// $invoice_data .= "\n发票抬头: ".$data['second_party_info']['partner'];
// $invoice_data .= "\n纳税人识别号: ".$data['second_party_info']['company_tax_no'];
//
// $invoice_data .= "\n开户银行: ".$data['second_party_info']['opening_bank'];
// $invoice_data .= "\n开户账号: ".$data['second_party_info']['payee_name'];
// $invoice_data .= "\n开票内容: ".$data['second_party_info']['invoice_item'];
// $invoice_data .= "\n注册地址及电话: ".$data['second_party_info']['register_address'].",".$data['second_party_info']['register_phone'];
//
// $objPHPExcel->getActiveSheet()->setCellValue('B'.($line+7), $invoice_data);//乙方
$first_data = "户名:{$data['pay_company']['payee_name']}";
$first_data .= "\n账号: {$data['pay_company']['bank_account']}";
$first_data .= "\n开户行: {$data['pay_company']['opening_bank']}";
$objPHPExcel->getActiveSheet()->setCellValue('B'.($line+7), $first_data);//户名
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+8), $data['pay_company']['payee_name']);//户名
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+9), $data['pay_company']['bank_account']);//银行账号
// $objPHPExcel->getActiveSheet()->setCellValue('E'.($line+10), $data['pay_company']['opening_bank']);//开户行
if($data['withdraw_type'] == 1){
$m = date('Y.m', $data['begintime']);
$fileName = "{$data['second_party_info']['partner']}&{$data['first_party_info']['partner']}-对账单{$m}";
}else{
$fileName = "{$data['second_party_info']['partner']}&{$data['first_party_info']['partner']}-对账单{$statement_begin_time}-{$statement_end_time}";
}
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');
}
//获取权限
public function getAuth()
{
$group = $_SESSION['onethink_admin']['user_group_id'];
//获取全部权限列表
$ruleList = M("AuthGroup")->field("rules")->where("id='{$group}'")->find()['rules'];
//获取所有含有规则的数据
$authlist = M("AuthRule")->field('name')->where("name like 'Admin/AggregateFinanceStatement%' AND id in ($ruleList)")->select();
$Auth = [];
foreach ($authlist as $k => $v) {
$a = str_replace("Admin/AggregateFinanceStatement/","",$v['name']);
$Auth[] = $a;
}
$this->OpAuthList = $Auth;
}
public function OpAuth($info)
{
$id = $info['id'];
$img = $info['ext_field'];
$remark = $info['remark'];
//原始列表
$opBtn = [
"viewStatement"=>"< a class = 'confirm viewStatement' data-id = '{$id}' > 查看< / a > ",
"createStatement"=>"< a class = 'confirm createStatement' data-id = '{$id}' > 申请开票< / a > ",
"editStatement"=>"< a class = 'confirm editStatement' data-id = '{$id}' > 编辑< / a > ",
"uploadVoucher"=>"< a class = 'confirm uploadVoucher' data-id = '{$id}' > 上传凭证< / a > ",
"viewVoucher"=>"< a class = 'confirm viewVoucher' data-img = '{$img}' data-id = '{$id}' > 查看凭证< / a > ",
"editVoucher"=>" < a class = 'confirm editVoucher' data-id = '{$id}' > 编辑凭证< / a > ",
"addRemark"=>"< a class = 'confirm addRemark' data-id = '{$id}' > 新增备注< / a > ",
"editRemark"=>"< a class = 'confirm editRemark' data-remark = '{$remark}' data-id = '{$id}' > 修改备注< / a > ",
"viewRemark"=>"< a class = 'confirm viewRemark' data-remark = '{$remark}' data-id = '{$id}' > 查看备注< / a > ",
"cancelVerify"=>"< a class = 'confirm cancelVerify' data-id = '{$id}' style = 'color: red;' > 撤销审核< / a > ",
"cancelApply"=>"< a class = 'confirm cancelApply' data-id = '{$id}' style = 'color: red;' > 撤销申请< / a > ",
];
//操作对应菜单
$optist = [];
switch ($info['verify_status']) {
case '-4':
$optist = ["viewStatement"];
break;
case '-3':
$optist = ["viewStatement","addRemark","viewRemark","editRemark"];
break;
case '-1':
$optist = ["viewStatement","cancelVerify","addRemark","viewRemark","editRemark"];
break;
case '0':
$optist = ["viewStatement","editStatement"];
break;
case '1':
$optist = ["viewStatement","cancelApply"];
break;
case '2':
$optist = ["viewStatement","cancelVerify","addRemark","viewRemark","editRemark"];
break;
case '3':
$optist = ["viewStatement","uploadVoucher","viewVoucher","editVoucher","addRemark","viewRemark","editRemark"];
break;
case '4':
$optist = ["viewStatement","viewVoucher","editVoucher","addRemark","viewRemark","editRemark"];
break;
case '5':
$optist = ["viewStatement","uploadVoucher","viewVoucher","editVoucher","addRemark","viewRemark","editRemark"];
break;
case "6":
$optist = ["viewStatement","addRemark","viewRemark","editRemark"];
break;
case "7":
$optist = ["viewStatement","createStatement"];
break;
default:
break;
}
//判断凭证和备注
if(empty($info['ext_field'])){
//未上传凭证
$optist = array_diff($optist, ["viewVoucher", "editVoucher"]);
}else{
$optist = array_diff($optist, ["uploadVoucher"]);
}
if(empty($info['remark'])){
//未上传备注
$optist = array_diff($optist, ["editRemark", "viewRemark"]);
}else{
$optist = array_diff($optist, ["addRemark"]);
}
//
$resarr = [];
foreach ($optist as $k => $v) {
if(IS_ROOT){
$resarr[] = $opBtn[$v];
}else{
if(in_array($v,$this->OpAuthList)){
$resarr[] = $opBtn[$v];
}
}
}
return $resarr;
}
public function menuAuth()
{
$optist = ['agreeApply',"refuseApply","pool","Invoiced","Received","updateStatement","export"];
$mentBtn = [
"agreeApply"=>"< a class = 'butn' id = 'agreeApply' > 审批通过< / a > ",
"refuseApply"=>"< a class = 'butn' id = 'refuseApply' style = 'background-color: red;' > 审批拒绝< / a > ",
"pool"=>"< a class = 'butn' id = 'pool' > 发起汇总< / a > ",
"Invoiced"=>"< a class = 'butn' id = 'Invoiced' > 已开票< / a > ",
"Received"=>"< a class = 'butn' id = 'Received' > 已到账< / a > ",
"updateStatement"=>"< a class = 'butn' id = 'updateStatement' style = 'width: 150px;background-color:green;' > 重算结算金额< / a > ",
"export"=>"< a class = 'butn' id = 'export' > 批量导出< / a > "
];
$resarr = [];
foreach ($optist as $k => $v) {
if(IS_ROOT){
$resarr[] = $mentBtn[$v];
}else{
if(in_array($v,$this->OpAuthList)){
$resarr[] = $mentBtn[$v];
}
}
}
return $resarr;
}
//操作日志
public function doAddOperationLog($id,$op_name)
{
$d= M("aggregate_statement","tab_")->where("id = '{$id}'")->find();
$first_party_info = json_decode($d['first_party_info'],true);
$second_party_info = json_decode($d['second_party_info'],true);
$statement_begin_time = date('Y.m.d', $d['begintime']);//对账开始时间
$statement_end_time = date('Y.m.d',$d['endtime']);//对账截止时间
$menu = "财务-结算管理-聚合渠道结算单管理";
if($d['withdraw_type'] == 1){
$m = date('Y.m', $d['begintime']);
$key = "{$second_party_info['partner']}& {$first_party_info['partner']}【{$m} 月结】";
}elseif($d['withdraw_type'] == 0){
$key ="{$second_party_info['partner']}& {$first_party_info['partner']}【{$statement_begin_time}-{$statement_end_time} 周结 正常结算】";
}else{
$key ="{$second_party_info['partner']}& {$first_party_info['partner']}【{$statement_begin_time}-{$statement_end_time} 周结 补点】";
}
addOperationLog(['op_type'=>1,'key'=>$key,"op_name"=>$op_name,'url'=>U(CONTROLLER_NAME.'/lists',$GetData),'menu'=>$menu]);
}
//汇总
public function pool()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = M("aggregate_statement","tab_")->field("*")->where("id in ({$ids})")->select();
//分单
$basedata = [
"pay_amount"=>0,
"statement_money"=>0,
"lack_statement_money"=>0,
"platform_amount"=>0,
"lack_platform_amount"=>0,
"create_lack_ids"=>[],
"del_lack_ids"=>[],
"statement_ids"=>[],
"info_ids"=>[],
"statement_begin_time"=>0,
"statement_end_time"=>0,
"fine"=>0,
"reward"=>0,
"verify_status"=>0,
"verify_log"=>json_encode(["create_user"=>$this->admininfo["username"],"create_time"=>date("Y-m-d H:i:s")]),
"op_time"=>time(),
"company_list"=>[]
];
$datas = ["aggregate"];
//初始化数据
foreach($datas as $k=>$v){
if($v =="aggregate"){
$basedata['company_type']=3;
$basedata['is_payment']=2;
}
$datas[$v]=$basedata;
unset($datas[$k]);
}
foreach ($dbres as $k=>$v) {
if($v['verify_status'] == 0 || $v['verify_status'] == -1 ||$v['verify_status'] == 6) continue;
$this->setDf($datas['aggregate'],$v);
}
$Pool = M("aggregate_statement_pool","tab_");
$Statemen = M("aggregate_statement","tab_");
foreach ($datas as $k => $v) {
if(count($v['company_list']) < = 0){continue;}
$v['statement_num'] = "PL_".date('Ymd').date('His').sp_random_num(3);
//保存公司信息
$company_list = $v['company_list'];
foreach ($company_list as $ke => $va) {
$this->addStatementInfo($va,$k,$v);
}
$get_statement_ids = $v['statement_ids'];
foreach ($get_statement_ids as $key => $value) {
$this->setAggregateDetail(0,1,'已汇总',$value);
}
if(empty($v['create_lack_ids']) & & empty($v['del_lack_ids']) & & empty($v['info_ids'])){ continue;}
$v['statement_ids'] = implode(",",$v['statement_ids']);
$v['create_lack_ids'] = implode(",",$v['create_lack_ids']);
$v['del_lack_ids'] = implode(",",$v['del_lack_ids']);
$info = implode(",",$v['info_ids']);
unset($v['info_ids']);
unset($v['company_list']);
if($v['statement_money'] == 0){
// $v['verify_status'] = 5;
// $v['verify_log'] = json_encode(["create_user"=>$this->admininfo["username"],"create_time"=>date("Y-m-d H:i:s"),"verify_user"=>"system","verify_time"=>date("Y-m-d H:i:s"),"payment_user"=>"system","payment_time"=>date("Y-m-d H:i:s")]);
}
//判断周月结
if(($v["statement_end_time"]-$v['statement_begin_time']) > 7*24*3600){
$v["withdraw_type"] = 1;
}else{
$v["withdraw_type"] = 0;
}
$pool_id = $Pool->add($v);
if($pool_id == false){
$this->ajaxReturn(array('status' => 0,"info"=>"汇总失败"));
}
if(!empty($info)){
$save["pool_id"]=$pool_id;
M("aggregate_statement_info","tab_")->where("id in ({$info})")->save($save);
}
if(!empty($v['del_lack_ids'])){
M("aggregate_lack_statement_info","tab_")->where("id in ({$v['del_lack_ids']})")->save(["is_pool"=>1]);
}
//修改结算单规则
$this->updatePoolVerifyStatus($v['statement_ids'],3,6,"pool",$v['statement_num']);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"汇总成功"
));
}
//设定组合的子操作
protected function setDf(& $savedata,$v)
{
$savedata['statement_ids'][] = $v['id'];
unset($v['id']);
$v['statement_info'] = json_decode( $v['statement_info'],true);
$fine = 0;
$reward = 0;
foreach($v['statement_info'] as $ka=>$va){
if($va['statement_type'] == 2){
//奖励
// $savedata['reward'] += $va['sum_money'];
$reward += $va['sum_money'];
unset($v['statement_info'][$ka]);
}else if($va['statement_type'] == 1){
// $savedata['fine'] += $va['sum_money'];
$fine += $va['sum_money'];
unset($v['statement_info'][$ka]);
}
}
if(isset($savedata['company_list'][$v['channel_id']])){
//存在游戏合并
$a = &$savedata['company_list'][$v['channel_id']];
$a['fine'] += $fine;
$a['reward'] += $reward;
$a['statement_info'] = array_merge($a['statement_info'],$v['statement_info']);
// dump($a['statement_info']);
$a['platform_amount'] += $v['pay_money'];
$a['pay_amount'] += $v['pay_money'];
$a['statement_money'] += $v['ratio_money'];
}else{
if($v['pay_type'] == 2){
//乙方收款
$v['company_info'] =json_decode($v['second_party_info'],true);
$v['company_info']['pay_type'] = 2;
$v['company_info_other'] = json_decode($v['first_party_info'],true);
}else{
$v['company_info'] = json_decode($v['first_party_info'],true);
$v['company_info']['pay_type'] = 1;
$v['company_info_other'] = json_decode($v['second_party_info'],true);
}
//先分配好订单号,防止重复提交
$tdata = [
"company_info"=>$v['company_info'],
"company_info_other"=>$v['company_info_other'],
"company_name"=>$v['channel_name'],
"platform_amount"=>$v['pay_money'],
"pay_amount"=>$v['pay_money'],
"statement_money"=>$v['ratio_money'],
"fine"=>$fine,
"reward"=>$reward,
"statement_info"=>$v['statement_info'],
"statement_num"=>"JS_".date('Ymd').date('His').$v['company_id'].sp_random_string(5)
];
$savedata['company_list'][$v['channel_id']] = $tdata;
}
// $savedata['pay_amount'] += $v['pay_amount'];
// $savedata['statement_money'] += $v['statement_money'];
// $savedata['platform_amount'] += $v['platform_amount'];
if($savedata['statement_begin_time'] == 0){
$savedata['statement_begin_time'] = $v['begintime'];
}elseif($v['begintime'] < $savedata['statement_begin_time']){
$savedata['statement_begin_time'] = $v['begintime'];
}
if($savedata['statement_end_time'] == 0){
$savedata['statement_end_time'] = $v['endtime'];
}elseif($v['endtime'] > $savedata['statement_end_time']){
$savedata['statement_end_time'] = $v['endtime'];
}
}
protected function addStatementInfo($va,$k,& $v){
$StatementInfo = M("aggregate_statement_info","tab_");
$company_info = $va['company_info'];
$company_info_other = $va['company_info_other'];
$LackStatement = M("aggregate_lack_statement_info","tab_");
$no_pool_money = 300;
//统一比例字段
if($k == "up" || $k=="ups"){
$no_pool_money = 500;
foreach ($va['statement_info'] as $key => & $value) {
if($company_info['pay_type'] == 2){
$value["ratio"] = $value["second_ratio"];
}else{
$value["ratio"] = $value["first_ratio"];
}
unset($value["first_ratio"]);
unset($value["second_ratio"]);
}
}
if($k=="ups" || $k=='downs'){
$is_payment = 1;
$verify_status = 0;
}else{
$is_payment = 2;
$verify_status = 2;
}
//创建基础未满基础信息
if($va['platform_amount'] < $no_pool_money){
$lackcompany =[
"company_id"=>$company_info['id'],
"company_name"=>$company_info['partner'],
'company_belong_name'=>$company_info_other['partner'],
"company_info"=>json_encode($company_info,JSON_UNESCAPED_UNICODE),
"statement_money"=>$va['statement_money'],
"pay_amount"=>$va['pay_amount'],
"platform_amount"=>$va['platform_amount'],
"fine"=>$va['fine'],
"reward"=>$va['reward'],
"statement_begin_time"=>$v['statement_begin_time'],
"statement_end_time"=>$v['statement_end_time'],
"is_payment"=>$is_payment,
"statement_pool_num"=>$v['statement_num'],
"statement_info"=>json_encode($va['statement_info'],JSON_UNESCAPED_UNICODE),
];
}
//整合数据
$lsres = $LackStatement->where("company_name='{$company_info['partner']}' and is_pool = 0")->select();
$del_lack_ids = [];
if(count($lsres) > 0){
//进行聚合
foreach($lsres as $key=>$val){
$del_lack_ids[] = $val['id'];
$va['statement_money'] += $val['statement_money'];
$va['pay_amount'] += $val['pay_amount'];
$va['fine'] += $val['fine'];
$va['reward'] += $val['reward'];
$va['platform_amount'] += $val['platform_amount'];
$va['statement_info'] = array_merge($va['statement_info'],json_decode($val['statement_info'],true));
$last_names = array_column($va['statement_info'],'game_name');
array_multisort($last_names,SORT_DESC,SORT_STRING,$va['statement_info']);
}
}
// dump($no_pool_money);
// dump($va);
// dump($k);die();
// if($va['platform_amount'] < $no_pool_money){
// //依旧小于不结算金额存旧版
// if($lackcompany['pay_amount'] != 0){
// $lask_id = $LackStatement->add($lackcompany);
// $v['create_lack_ids'][] =$lask_id;
// $v['lack_statement_money'] +=$lackcompany['statement_money'];
// $v['lack_platform_amount'] +=$lackcompany['platform_amount'];
// }
// }else{
if($va['pay_amount'] != 0){
//存成功
$company =[
"pool_id"=>0,
"company_id"=>$company_info['id'],
"company_name"=>$company_info['partner'],
'company_belong_name'=>$company_info_other['partner'],
"company_info"=>json_encode($company_info,JSON_UNESCAPED_UNICODE),
"fine"=>$va['fine'],
"reward"=>$va['reward'],
"statement_money"=>$va['statement_money'],
"pay_amount"=>$va['pay_amount'],
"platform_amount"=>$va['platform_amount'],
"statement_begin_time"=>$v['statement_begin_time'],
"statement_end_time"=>$v['statement_end_time'],
"statement_info"=>json_encode($va['statement_info'],JSON_UNESCAPED_UNICODE),
"statement_num"=>$va['statement_num'],
"verify_status"=>$verify_status
];
$companyid = $StatementInfo->add($company);
$v['info_ids'][] =$companyid;
$v['del_lack_ids'] =array_merge($v['del_lack_ids'],$del_lack_ids);
$v['statement_money'] +=$va['statement_money'];
$v['pay_amount'] +=$va['pay_amount'];
$v['platform_amount'] +=$va['platform_amount'];
$v['fine'] +=$va['fine'];
$v['reward'] +=$va['reward'];
}
// }
}
//结算单规则
protected function updatePoolVerifyStatus($ids,$old_status,$change_status,$op_pre,$num){
$dbres = M("aggregate_statement","tab_")->field("id,verify_status")->where("id in ({$ids})")->select();
foreach($dbres as $k=>& $v){
$v['verify_status']=$change_status;
$v['op_time']=time();
M("aggregate_statement","tab_")->save($v);
}
}
//type 0 汇总 1 审批 2 开票 3 到账
public function setAggregateDetail($type=0, $status = 1,$detail_info = "",$id = 0) {
$data = M("aggregate_statement","tab_")->where(['id'=>$id])->find();
$detail['detail'] = json_decode($data['detail'],true);
$adminInfo = $_SESSION['onethink_admin']['user_auth'];
if ($type == 0) {
$detail['detail']['pool'] = $detail_info."({$adminInfo["username"]})".date("Y-m-d H:i:s");
$detail['detail']['pool_status'] = $status;
} else if($type == 1) {
$detail['detail']['apply'] = $detail_info."({$adminInfo["username"]})".date("Y-m-d H:i:s");
$detail['detail']['apply_status'] = $status;
} else if($type == 2) {
$detail['detail']['invoiced'] = $detail_info."({$adminInfo["username"]})".date("Y-m-d H:i:s");
$detail['detail']['invoiced_status'] = $status;
} else if($type == 3) {
$detail['detail']['received'] = $detail_info."({$adminInfo["username"]})".date("Y-m-d H:i:s");
$detail['detail']['received_status'] = $status;
}
M("aggregate_statement","tab_")->where(['id'=>$id])->save(['detail'=>json_encode($detail['detail'])]);
}
}