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.

676 lines
26 KiB
PHP

<?php
namespace Admin\Controller;
/**
* 上下游结算单
* @author cz
*/
class CompanyStatementPoolController extends ThinkController
{
public $CompanyType = [
"1"=>"下游公司",
"2"=>"下游个人",
"3"=>"上游CP"
];
public $IsPayment = [
"1"=>"",
"2"=>""
];
public $VerifyStatus=[
"-2"=>"拒绝打款",
"-1"=>"审批拒绝",
"0"=>"未审批",
"1"=>"审批通过",
"2"=>"待打款",
"3"=>"打款中",
"4"=>"打款成功"
];
public $admininfo;
public $DBModel;
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
$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;
//权限分配
if(!IS_ROOT){
$this->OpAuthList= getModuleControllerAuth();
}
$this->assign('menubtn',$this->menuAuth());
$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"] = "(statement_begin_time BETWEEN {$time_start} AND {$time_end}) OR (statement_end_time BETWEEN {$time_start} AND {$time_end})";
} elseif (isset($_REQUEST['time_start'])) {
$time_start = strtotime($_REQUEST['time_start']);
$map["_string"] = "(statement_begin_time >= {$time_start} ) OR (statement_end_time >= {$time_start})";
} elseif (isset($_REQUEST['time_end'])) {
$time_end = strtotime($_REQUEST['time_end'])+ 86399;
$map["_string"] = "(statement_begin_time <= {$time_end} ) OR (statement_end_time <= {$time_end})";
}
if (isset($_REQUEST['time_start2']) && isset($_REQUEST['time_end2'])) {
$map['op_time'] = ['between', [strtotime($_REQUEST['time_start2']), strtotime($_REQUEST['time_end2']) + 86399]];
} elseif (isset($_REQUEST['time_start2'])) {
$map['op_time'] = ['EGT', strtotime($_REQUEST['time_start2'])];
} elseif (isset($_REQUEST['time_end2'])) {
$map['op_time'] = ['ELT', strtotime($_REQUEST['time_end2']) + 86399];
}
//其他
if(isset($_REQUEST['company_type'])){
$map['company_type'] = $_REQUEST['company_type'];
}
if(isset($_REQUEST['verify_status'])){
$map['verify_status'] = $_REQUEST['verify_status'];
}
if(isset($_REQUEST['statement_num'])){
$map['statement_num'] = $_REQUEST['statement_num'];
}
if(isset($_REQUEST['is_payment'])){
$map['is_payment'] = $_REQUEST['is_payment'];
}
$this->checkListOrCountAuthRestMap($map);//导出权限
//条件end
$data = $this->DBModel
->field("*")
->where($map)
->order("op_time Desc")->page($page,$row)->select();
foreach($data as $k => &$v) {
$v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
$v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
$v['company_type'] = $this->CompanyType[$v['company_type']];
$v['is_payment_str'] = $this->IsPayment[$v['is_payment']];
$v['op_time'] = date('Y-m-d H:i:s',$v['op_time']);
$v["valid"] = "{$v['statement_begin_time']} ~ {$v['statement_end_time']}";
$v['ext_file_type'] = empty($v['ext_file']) ? "" : "";
$v['verify_log'] = json_decode($v['verify_log'], true);
if (isset($params['export'])) {
$symbol = "\n";
} else {
$symbol = "<br>";
}
$v["create"]= "{$v['verify_log']['create_user']} {$symbol} {$v['verify_log']['create_time']}";
if(isset($v['verify_log']['verify_user'])){
if($v['verify_status'] == -1){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["verify"]= "{$ts}({$v['verify_log']['verify_user']}) {$symbol} {$v['verify_log']['verify_time']}";
}else{
$v["verify"] = '--';
}
if(isset($v['verify_log']['payment_user'])){
if($v['verify_status'] == -2){
$ts = "拒绝打款";
$v["payment"]= "{$ts}({$v['verify_log']['payment_user']}){$symbol} {$v['verify_log']['payment_time']}";
}elseif($v['verify_status'] == 2){
$ts = "待打款";
$v["payment"]= "{$ts}({$v['verify_log']['payment_user']}){$symbol} {$v['verify_log']['payment_time']}";
}elseif($v['verify_status'] == 3){
$ts = "打款中";
$v["payment"]= "{$ts}({$v['verify_log']['payment_user']}){$symbol} {$v['verify_log']['payment_time']}";
}elseif($v['verify_status'] == 4){
if($v['is_payment']==1){
$ts="线上打款成功";
}else{
$ts="线下打款成功";
}
$v["payment"]= "{$ts}({$v['verify_log']['payment_user']}){$symbol} {$v['verify_log']['payment_time']}";
}
}else{
$v["payment"] = '--';
}
$v['oplist'] = $this->OpAuth($v);
}
$count = $this->DBModel->field("count(id) count,SUM(statement_money) as statement_money")->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->assign('data',$data);
$this->assign('count',$count);
$this->assign('CompanyType',$this->CompanyType);
$this->assign('IsPayment',$this->IsPayment);
$this->assign('VerifyStatus',$this->VerifyStatus);
$this->display();
}
//查看
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;
}
//获取基本信息
$infolist = M("company_statement_info","tab_")->where("pool_id = '{$id}'")->select();
if($infolist[0]['company_type'] == 3){
//上游
$this->viewCpPool($infolist,$is_export);
}elseif($infolist[0]['company_type'] == 1){
//下游公司
$this->viewPcPool($infolist,$is_export);
}else{
$this->viewPuPool($infolist,$is_export);
}
}
//个人汇总结算查看
protected 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");
}
//下游汇总结算查看
protected 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");
}
//上游汇总查看及导出
protected 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 verifyAgree(){
$this->setVerifyStatus(0,1,"verify");
}
public function verifyRefuse(){
$this->setVerifyStatus(0,-1,"verify");
}
protected function setVerifyStatus($old_status,$change_status,$op_pre)
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBModel->field("id,verify_status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>&$v){
if($v['verify_status'] != $old_status) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log'][$op_pre.'_user']=$this->admininfo["username"];
$v['verify_log'][$op_pre.'_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['verify_status']=$change_status;
$this->DBModel->save($v);
$opname = $this->VerifyStatus[$change_status];
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>$opname,'url'=>U('lists')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>$opname."成功"
));
}
//保存备注
public function saveRemark(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$remark = $_REQUEST['remark'];
$this->DBModel->where("id = '{$id}'")->save(['remark'=>$remark]);
$this->ajaxReturn(array(
'status' => 1,
'info' => "操作成功"
));
}
//上传凭证
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 = $this->DBModel->field("id,ext_file")->where("id = '{$id}'")->find();
$this->assign("id",$id);
$this->assign("ext_file",$info['ext_file']);
$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_file"=>$pf,
];
//获取旧值,存在删除
$info = $this->DBModel->field("id,ext_file")->where("id = '{$id}'")->find();
if(!empty($info['ext_file'])){
unlink("./".$info['ext_file']);
}
$this->DBModel->where("id = '{$id}'")->save($save);
$array=array(
'status' => 1,
"info"=>"上传成功",
"file_path"=>$pf,
);
}
$this->ajaxReturn($array);
}
//删除凭证
public function delVoucher(){
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$info = $this->DBModel->field("id,ext_file")->where("id = '{$id}'")->find();
if(!empty($info['ext_file'])){
unlink("./".$info['ext_file']);
}
$save = [
"ext_file"=>'',
];
$this->DBModel->where("id = '{$id}'")->save($save);
// $this->doAddOperationLog($id,"删除凭证");
$this->ajaxReturn(array(
'status' => 1,
'info' => "删除成功"
));
}
//撤销汇总
public function cancelPool()
{
//开启事务
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBModel->field("id,statement_ids,verify_status")->where("id in ({$ids})")->select();
$sids = [];
foreach($dbres as $k=>$v){
if(in_array($v['verify_status'],[3,4])) continue; //有打款就不允许回退
$statement_ids = explode(",",$v['statement_ids']);
$sids = array_merge($sids,$statement_ids);
}
$sids = implode(",",$sids);
M()->startTrans();//事务
//删除info
$infores = M("company_statement_info","tab_")->where("pool_id in ($ids)")->delete();
if($infores === false){
M()->rollback();
$this->ajaxReturn(array('status' => 0,"info"=>"撤销汇总失败"));
}
//删除自己
$poolres = $this->DBModel->where("id in ({$ids})")->delete();
if($poolres === false){
M()->rollback();
$this->ajaxReturn(array('status' => 0,"info"=>"撤销汇总失败"));
}
//修改结算单信息
$st_res = M("company_statement","tab_")->field("id,verify_status,verify_log")->where("id in ({$sids})")->select();
foreach($st_res as $k=>&$v){
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log'][$op_pre.'_user']=$this->admininfo["username"];
$v['verify_log'][$op_pre.'_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['verify_status']=-2;
$stres = M("company_statement","tab_")->save($v);
if($stres === false){
M()->rollback();
$this->ajaxReturn(array('status' => 0,"info"=>"撤销汇总失败"));
}
}
M()->commit();
$this->ajaxReturn(array(
'status' => 1,
"info"=>"重算金额成功"
));
}
//导出
public function export()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$data = M("CompanyStatement","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);//结算记录
//收款方
$all_sum_money = $data['pay_amount'];//合计平台总额
$all_pay_amount = $data['statement_money'];//合计结算金额
$big_all_sum_money = convertAmountToCn($data['statement_money']);//大写
if($data['pay_type'] == 2){
$data['receive_company'] = $data['second_party_info'];//收款方
$data['pay_company'] = $data['first_party_info'];
}else{
$data['receive_company'] = $data['first_party_info'];
$data['pay_company'] = $data['second_party_info'];
}
if($data['receive_company']['invoice_type'] == 1){
$data['receive_company']['invoice_type']="专票";
}else{
$data['receive_company']['invoice_type']="普票";
}
if ($data['company_belong'] == 9) {//上游
excelUpStreamTemplate($data, $all_sum_money, $all_pay_amount, $big_all_sum_money);
} else {//下游
excelDownStreamTemplate($data, $all_sum_money, $all_pay_amount, $big_all_sum_money);
}
}
public function OpAuth($info)
{
$id = $info['id'];
$img = $info['ext_file'];
$remark = $info['remark'];
//原始列表
$opBtn = [
"viewPool"=>"<a class='confirm viewPool' 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>",
"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>",
"setPayment"=>"<a class='confirm setPayment' data-id='{$id}'>打款信息提交</a>",
"editPayment"=>"<a class='confirm setPayment' data-id='{$id}'>打款信息编辑</a>",
"viePayment"=>"<a class='confirm setPayment' data-id='{$id}'>打款详情</a>",
];
//操作对应菜单
$optist = [];
switch ($info['verify_status']) {
case '-2':
case '-1':
case '0':
$optist = ["viewPool"];
break;
case '1':
$optist = ["viewPool","addRemark","viewRemark","editRemark","viewVoucher","editVoucher","uploadVoucher","setPayment"];
break;
case '2':
$optist = ["viewPool","addRemark","viewRemark","editRemark","viewVoucher","editVoucher","uploadVoucher","editPayment"];
break;
case '3':
$optist = ["viewPool","addRemark","viewRemark","editRemark","viewVoucher","editVoucher","uploadVoucher","viePayment"];
break;
default:
break;
}
//
if($info['platform_amount'] <= 0){
$optist = array_diff($optist, ["viewPool"]);
}
if(empty($info['ext_file'])){
//未上传凭证
$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()
{
$mentBtn = [
"verifyAgree"=>"<a class='butn' id='verifyAgree'>审批通过</a>",
"verifyRefuse"=>"<a class='butn' id='verifyRefuse' style='background-color: red;'>审核拒绝</a>",
"cancelPool"=>"<a class='butn' id='cancelPool' style='background-color: green;'>撤销汇总</a>",
"export"=>"<a class='butn' id='export'>批量导出</a>"
];
$resarr = [];
foreach ($mentBtn as $k => $v) {
if(IS_ROOT){
$resarr[] = $v;
}else{
if(in_array($k,$this->OpAuthList)){
$resarr[] = $v;
}
}
}
return $resarr;
}
}