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.

537 lines
21 KiB
PHP

<?php
namespace Payment\Controller;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class ExcelPaymentController extends BaseController
{
public $PayStatus=[
"-1"=>"打款失败",
"0"=>"未打款",
"1"=>"打款成功"
];
public $CompanyType = [
"1"=>"下游公司",
"2"=>"下游个人",
"3"=>"上游CP"
];
public $ALIRSP=[
"out_biz_no"=>"商户订单号",
"order_id"=>"支付订单号",
"pay_fund_order_id"=>"资金流水号",
"status"=>"转账状态",
"trans_date"=>"订单支付时间",
"msg"=>"支付信息",
"sub_msg"=>"失败描述",
"sub_code"=>"失败码",
"remark"=>"打款备注",
"code"=>"打款状态码"
];
public function _initialize()
{
$this->admininfo = session('payment_user');;
// $this->DBModel = M("CompanyStatementPool","tab_");
parent::_initialize();
}
public function lists()
{
$params = I('get.');
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
$map=[
"_string"=>"1=1",
];
if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
$time_start = strtotime($_REQUEST['time_start']);
$time_end = strtotime($_REQUEST['time_end'])+ 86399;
$map["_string"] = "(i.statement_begin_time BETWEEN {$time_start} AND {$time_end}) OR (i.statement_end_time BETWEEN {$time_start} AND {$time_end})";
} elseif (isset($_REQUEST['time_start'])) {
$time_start = strtotime($_REQUEST['time_start']);
$map["_string"] = "(i.statement_begin_time >= {$time_start} ) OR (i.statement_end_time >= {$time_start})";
} elseif (isset($_REQUEST['time_end'])) {
$time_end = strtotime($_REQUEST['time_end'])+ 86399;
$map["_string"] = "(i.statement_begin_time <= {$time_end} ) OR (i.statement_end_time <= {$time_end})";
}
if (isset($_REQUEST['pay_time_start']) && isset($_REQUEST['pay_time_end'])) {
$map['i.pay_time'] = ['between', [strtotime($_REQUEST['pay_time_start']), strtotime($_REQUEST['pay_time_end']) + 86399]];
} elseif (isset($_REQUEST['pay_time_start'])) {
$map['i.pay_time'] = ['EGT', strtotime($_REQUEST['pay_time_start'])];
} elseif (isset($_REQUEST['pay_time_end'])) {
$map['i.pay_time'] = ['ELT', strtotime($_REQUEST['pay_time_end']) + 86399];
}
if(isset($_REQUEST['company_name'])){
$map['i.company_name'] = $_REQUEST['company_name'];
}
if(isset($_REQUEST['pay_status'])){
$map['i.pay_status'] = $_REQUEST['pay_status'];
}
$CompanyInfo = M("excel_statement_info","tab_")
->alias('i')
->field("i.*")
->where($map)
->page($page,$row)
->order("FIELD(pay_status,0,-1,1)")
->select();
foreach($CompanyInfo as $k=>&$v){
$v['company_type'] = $this->CompanyType[$v['company_type']];
$v["pay_status_str"] = $this->PayStatus[$v['pay_status']];
$v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
$v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
if(empty($v['pay_time'])){
$v['pay_time'] = "--";
}else{
$v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
}
$v["valid"] = "{$v['statement_begin_time']} ~ {$v['statement_end_time']}";
}
//统计待打款金额
$countmap = $map;
$countmap['pay_status']=["neq",1];
$money = M("excel_statement_info","tab_")->alias('i')->field("sum(statement_money) statement_money")->where($countmap)->find();
$count = M("excel_statement_info","tab_")->alias('i')->field("count(id) count")->where($map)->find();
// dd($count);
$parameter['p'] = $page;
$parameter['row'] = $row;
$page = set_pagination($count['count'], $row, $parameter);
if ($page) {
$this->assign('_page', $page);
}
$this->meta_title = 'EXCEL打款';
$this->assign("data",$CompanyInfo);
$this->assign("money",$money);
$this->assign("CompanyType", $this->CompanyType);
$this->assign("PayStatus", $this->PayStatus);
$this->display();
}
public function add()
{
$this->display();
}
//
public function loopAdd()
{
$p = $_REQUEST;
$statement_begin_time = strtotime($p['statement_begin_time']);
$statement_end_time = strtotime($p['statement_end_time'])+86399;
$batch = $p['batch'];
$checkarr = $p['checkarr'];
//循环获取添加
if(count($checkarr) < 1){
$this->ajaxReturn(["status"=>1,"msg"=>"ok"]);
}
$ExcelStatementInfo = M('ExcelStatementInfo',"tab_");
foreach ($checkarr as $k => &$v) {
$v['statement_begin_time'] = $statement_begin_time;
$v['statement_end_time'] = $statement_end_time;
$v['batch_num'] = $batch;
$v['statement_info'] = json_encode($v['statement_info'],JSON_UNESCAPED_UNICODE);
$v['company_info'] = json_encode($v['company_info'],JSON_UNESCAPED_UNICODE);
$v['company_type'] = 2;
$v['create_time'] = time();
$v['statement_num'] = "JS_".date('Ymd').date('His').$v['company_id'].sp_random_string(5);
if(!isset($v['company_info']['ali_user']) || !isset($v['company_info']['ali_account']) || $v['company_info']['ali_user'] == '' || $v['company_info']['ali_account'] == ''){
M('ExcelStatementInfo')->where("statement_num = '{$v['statement_num']}'")->delete();
$this->ajaxReturn(["status"=>0,"msg"=>"添加失败"]);
}
$res = $ExcelStatementInfo->add($v);
if($res === false){
M('ExcelStatementInfo')->where("statement_num = '{$v['statement_num']}'")->delete();
$this->ajaxReturn(["status"=>0,"msg"=>"添加失败"]);
}
}
$this->ajaxReturn(["status"=>1,"msg"=>"ok"]);
}
//查看
public function viewPool()
{
if(!isset($_REQUEST['id'])){
$this->error('参数错误');
}
$id = $_REQUEST['id'];
$is_export= false;
if (isset($_REQUEST['export']) && $_REQUEST['export']==1){
$is_export = true;
}
//获取基本信息
$dbres = M("company_statement_info","tab_")->where("id='{$id}'")->select();
$title = $dbres[0]['company_name'];
$this->assign("title",$title);
if($dbres[0]['company_type'] == 3){
//上游
$this->viewCpPool($dbres,$is_export);
}elseif($dbres[0]['company_type'] == 1){
//下游公司
$this->viewPcPool($dbres,$is_export);
}else{
$this->viewPuPool($dbres,$is_export);
}
}
//个人汇总结算查看
public function viewPuPool(&$infolist,$is_export){
$line = 1;
$count = [];
//获取对接人
foreach($infolist as $k=>&$v){
$v['statement_info'] = json_decode($v['statement_info'],true);
$v['company_info'] = json_decode($v['company_info'],true);
$v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
$v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
$cline = $line+1;
if($is_export){
$v['sum_money_exp'] = "=";
}
foreach($v['statement_info'] as $ke=>&$va){
$line ++;
if(isset($va['ratio'])){
$va['increment_ratio'] = 0;
}else{
$va['ratio'] = 0;
}
if($is_export){
//J3*(K3+L3)+M3-N3
$va['sum_money'] = "=J{$line}*(K{$line}+L{$line})+M{$line}-N{$line}";
$count['sum_money_exp'] .= "O{$line}+";
$count['platform_amount_exp'] .= "J{$line}+";
}else{
$count['platform_amount'] += $va['pay_amount'];
$count['sum_money'] += $va['sum_money'];
}
}
$v['statement_count'] = count($v['statement_info']);
}
if($is_export){
$count["platform_amount"] = "=".trim($count["platform_amount_exp"],"+");
$count["sum_money"] = "=".trim($count["sum_money_exp"],"+");
}
$this->assign("data",$infolist);
$this->assign("count",$count);
$this->assign("is_export",$is_export);
$this->display("viewPuPool");
}
//下游汇总结算查看
public function viewPcPool(&$infolist,$is_export){
$line = 1;
$count = [];
//获取对接人
$p_id = array_column($infolist,'company_id');
$map['id'] = ['in',$p_id];
$pl = M("promote_company","tab_")->field("id,settlement_contact")->where($map)->select();
$Partner = [];
foreach($pl as $k=>$v){
$Partner[$v['id']] = $v['settlement_contact'];
}
unset($pl);
foreach($infolist as $k=>&$v){
$v['statement_info'] = json_decode($v['statement_info'],true);
$v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
$v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
$cline = $line+1;
if($is_export){
$v['statement_money_exp'] = "=";
}
foreach($v['statement_info'] as $ke=>&$va){
$line ++;
if(isset($va['ratio'])){
$va['increment_ratio'] = 0;
}else{
$va['ratio'] = 0;
}
if($is_export){
$v['statement_money_exp'] .= "H{$line}+";
$va['d_statement_money'] = "=D{$line}*(1-G{$line})*(E{$line}+F{$line})";
$count['platform_amount_exp'] .= "D{$line}+";
$count['d_statement_money_exp'] .= "H{$line}+";
}else{
$va['d_statement_money'] = round($va['pay_amount']*($va['ratio']+$va['increment_ratio'])*(100-$va['fax_ratio'])/100/100,2);
$count['platform_amount'] += $va['pay_amount'];
$count['d_statement_money'] += $va['d_statement_money'];
}
}
$v['settlement_contact'] = $Partner[$v['company_id']];
if($is_export){
$v['statement_money_exp'] .= "J{$cline}-I{$cline}";
$v['statement_money'] = $v['statement_money_exp'];
$count['fine_exp'] .= "I{$cline}+";
$count['reward_exp'] .= "J{$cline}+";
$count['statement_money_exp'] .= "K{$cline}+";
}else{
$count['fine'] += $v['fine'];
$count['reward'] += $v['reward'];
$count['statement_money'] += $v['statement_money'];
}
$v['statement_count'] = count($v['statement_info']);
}
if($is_export){
$count["d_statement_money"] = "=".trim($count["d_statement_money_exp"],"+");
$count["platform_amount"] = "=".trim($count["platform_amount_exp"],"+");
$count["fine"] = "=".trim($count["fine_exp"],"+");
$count["reward"] = "=".trim($count["reward_exp"],"+");
$count["statement_money"] = "=".trim($count["statement_money_exp"],"+");
}
$this->assign("data",$infolist);
$this->assign("count",$count);
$this->assign("is_export",$is_export);
$this->display("viewPcPool");
}
//上游汇总查看及导出
public function viewCpPool(&$infolist,$is_export)
{
$line = 2;
$count = [];
//获取平台名
$p_id = array_column($infolist,'company_id');
$map['id'] = ['in',$p_id];
$pl = M("Partner","tab_")->field("id,matche_platform")->where($map)->select();
$Partner = [];
foreach($pl as $k=>$v){
$Partner[$v['id']] = $v['matche_platform'];
}
unset($pl);
//
foreach($infolist as $k=>&$v){
$v['statement_info'] = json_decode($v['statement_info'],true);
$v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
$v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
$cline = $line+1;
if($is_export){
$v['statement_money_exp'] = "=";
}
foreach($v['statement_info'] as $ke=>&$va){
$line ++;
$va['company_ratio'] = 100-$va['ratio'];
if($is_export){
$v['statement_money_exp'] .= "K{$line}+";
$va['d_statement_money'] = "=F{$line}*G{$line}";
$count['platform_amount_exp'] .= "E{$line}+";
$count['platform_amount_exp2'] .= "F{$line}+";
$count['d_statement_money_exp'] .= "K{$line}+";
}else{
$va['d_statement_money'] = round($va['pay_amount']*$va['ratio']/100,2);
$count['platform_amount'] += $va['pay_amount'];
$count['platform_amount2'] += $va['pay_amount'];
$count['d_statement_money'] += $va['d_statement_money'];
}
}
$v['matche_platform'] = $Partner[$v['company_id']];
if($is_export){
$v['statement_money_exp'] .= "M{$cline}-L{$cline}";
$v['statement_money'] = $v['statement_money_exp'];
$count['fine_exp'] .= "L{$cline}+";
$count['reward_exp'] .= "M{$cline}+";
$count['statement_money_exp'] .= "N{$cline}+";
}else{
$count['fine'] += $v['fine'];
$count['reward'] += $v['reward'];
$count['statement_money'] += $v['statement_money'];
}
$v['statement_count'] = count($v['statement_info']);
}
if($is_export){
$count["d_statement_money"] = "=".trim($count["d_statement_money_exp"],"+");
$count["platform_amount"] = "=".trim($count["platform_amount_exp"],"+");
$count["platform_amount2"] = "=".trim($count["platform_amount_exp2"],"+");
$count["fine"] = "=".trim($count["fine_exp"],"+");
$count["reward"] = "=".trim($count["reward_exp"],"+");
$count["statement_money"] = "=".trim($count["statement_money_exp"],"+");
}
$this->assign("data",$infolist);
$this->assign("count",$count);
$this->assign("is_export",$is_export);
$this->display("viewCpPool");
}
public function transfer_set()
{
$this->getAccountMoney();
$this->meta_title = '打款设置';
$this->assign("mobile",$this->admininfo['mobile']);
$this->display();
}
public function saveTransferSet()
{
$mobile = $_REQUEST['mobile'];
$verify = $_REQUEST['verify'];
if (!$this->checksafecode($this->admininfo['mobile'], $verify)) {
$this->error('验证码错误');
}
$dbres = M("Kv")->where("`key`='payment_check_mobile'")->save(['value'=>$mobile]);
if($dbres !== false){
$logout = U('Public/logout');
$this->ajaxReturn(["status"=>1,"msg"=>"验证手机修改成功,即将跳转","url"=>"{$logout}"]);
}
$this->ajaxReturn(["status"=>0,"msg"=>"验证手机修改失败"]);
}
public function showPayment()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$CompanyInfo = M("company_statement_info","tab_")
->alias('i')
->field("i.id,i.pool_id,i.company_name,i.company_info,i.statement_money,p.statement_num,i.remark,i.pay_status")
->join("left join tab_company_statement_pool p ON p.id = i.pool_id")
->where("i.id in ({$ids})")
->select();
foreach($CompanyInfo as $k=>&$v){
$v['company_info'] = json_decode($v['company_info'],true);
$v['company_info']['ali_user'] ?? '';
$v['company_info']['ali_account'] ?? '';
}
$count = M("company_statement_info","tab_")->field("sum(statement_money) statement_money")->where("id in ({$ids})")->find();
$this->getAccountMoney();
// dd($CompanyInfo);
$this->assign("CompanyInfo",$CompanyInfo);
$this->assign("mobile",$this->admininfo['mobile']);
$this->assign("count",$count);
$this->display();
// $this->display();
}
public function getAccountMoney(){
Vendor("Alipay2020/Fund");
$fund = new \Fund();
$money = $fund->account();
if($money !== -1){
$money = $money['amount'];
}else{
$money = "--";
}
$this->assign("money",$money);
}
//以下打款流程
public function checkVerify()
{
$mobile = $this->admininfo['mobile'];
$verify = $_REQUEST['verify'];
if (!A("Public")->checksafecode($mobile, $verify)) {
$this->ajaxReturn(["error"=>"验证码错误"]);
}
$this->ajaxReturn(["success"=>"验证码验证成功","data"=>[]]);
}
//执行打款
public function doPayment()
{
$id = $_REQUEST['id'];
$remark = $_REQUEST['remark'];
Vendor("Alipay2020/Fund");
$fund = new \Fund();
$dbres = M("company_statement_info","tab_")->where("id='{$id}'")->find();
$company_info = json_decode($dbres['company_info'],true);
if($dbres['pay_status'] != 1 && $dbres['verify_status'] == 1){
//执行打款
$title = $dbres['company_name']."结算";
$amount = $dbres['statement_money'];
$payres = $fund->transfer($company_info['ali_account'],$company_info['ali_user'],$dbres['statement_num'],$amount, $title);
$resultCode = $payres->code;
$savedata = ["id"=>$dbres['id']];
if(!empty($resultCode)&&$resultCode == 10000){
$savedata["pay_status"]=1;
} else {
$savedata["pay_status"]=-1;
}
$payres = json_decode( json_encode($payres),true);
$payres["remark"] = $remark;
$savedata["pay_info"] = json_encode($payres,JSON_UNESCAPED_UNICODE);
$savedata["pay_type"]=2;
$savedata["pay_time"]=time();
}
M("company_statement_info","tab_")->save($savedata);
$this->ajaxReturn(["success"=>"打款成功","data"=>[]]);
# code...
}
/**
* 执行最后聚合表统计
* 整合数据,全部成功则支付成功,否则为支付中
*/
public function poolCount(){
$pool_id = $_REQUEST['pool_id'];
$CompanyInfo = M("company_statement_info","tab_");
if(count($pool_id) > 0){
foreach($pool_id as $k=>$v){
$f = $CompanyInfo->where("pool_id = {$v} AND pay_status <> 1")->find();
if(empty($f)){
//全部完成
$this->setOneVerifyStatus(4,"payment",$v);
}else{
//打款中
$this->setOneVerifyStatus(3,"payment",$v);
}
}
}
$this->ajaxReturn(["success"=>"打款成功","data"=>[]]);
}
/**
* 打款详情
*/
public function paymentInfo()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$info = M("company_statement_info","tab_")->field("pay_info")->where("id='{$id}'")->find()['pay_info'];
$info = json_decode($info,true);
$senddata = [];
foreach($info as $k => &$v){
if(isset($this->ALIRSP[$k])){
$senddata[$this->ALIRSP[$k]] = $v;
}else{
$senddata[$k] = $v;
}
}
$this->assign("info",$senddata);
$this->display();
}
protected function setOneVerifyStatus($change_status,$op_pre,$id)
{
$dbres = M("company_statement_pool","tab_")->field("id,verify_status,verify_log")->where("id = {$id}")->find();
$dbres['verify_log'] = json_decode($dbres['verify_log'],true);
$dbres['verify_log'][$op_pre.'_user']=$this->admininfo["mobile"];
$dbres['verify_log'][$op_pre.'_time']=date("Y-m-d H:i:s");
$dbres['verify_log'] = json_encode($dbres['verify_log']);
$dbres['verify_status']=$change_status;
M("company_statement_pool","tab_")->save($dbres);
}
}