<?php
// 公司关系绑定
namespace Admin\Controller;
class CompanyRelationController extends AdminController
{
public $admininfo;
public $DBModel;
public $DBlogModel;
public $OpAuthList=[];
public $Status = [
"-2"=>"管理员拒绝",
"-1"=>"市场部拒绝",
"0"=>"待审核",
"1"=>"市场部通过",
"2"=>"管理员通过"
];
public $CompanyType = [
"0"=>"己方公司",
"1"=>"上游cp公司",
"2"=>"联运下游渠道公司"
];
public $SettlementType=[
"0"=>"无",
"1"=>"周结",
"2"=>"月结"
];
public $InvoiceType=[
"1"=>"专票",
"2"=>"普票"
];
public $IsPayment=[
"1"=>"是",
"2"=>"否"
];
public $Collection=[
"1"=>"甲方",
"2"=>"乙方"
];
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
$this->DBlogModel = M("company_relation_log","tab_");
$this->DBModel = M("company_relation","tab_");
parent::_initialize();
}
//生效列表
public function index()
{
$params = I('get.');
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
$where['_string'] = '1 = 1';
if(isset($params['company_type'])){
$where['_string'] .= " AND (first_company_type='{$params['company_type']}' OR second_company_type='{$params['company_type']}')";
}
if(isset($params['company_id'])){
$where['_string'] .= " AND (first_company_id='{$params['company_id']}' OR second_company_id='{$params['company_id']}')";
}
if(isset($params['company_type']) & & isset($params['company_id'])){
unset($where['_string']);
$where['_string'] .= " (first_company_id='{$params['company_id']}' AND first_company_type='{$params['company_type']}') OR (second_company_id='{$params['company_id']}' AND second_company_type='{$params['company_type']}')";
}
if(isset($params['settlement_type'])){
$where['settlement_type'] = $params['settlement_type'];
}
if(isset($params['invoice_type'])){
$where['invoice_type'] = $params['invoice_type'];
}
if(isset($params['is_payment'])){
$where['is_payment'] = $params['is_payment'];
}
// $this->checkListOrCountAuthRestMap($where);//导出权限
$dbres = $this->DBModel->where($where)->order("id desc");;
if(isset($params['export'])){
$dbres = $dbres->select();
}else{
$dbres = $dbres->page($page, $row)->select();
}
foreach($dbres as $k=>& $v){
$v['settlement_type'] =$this->SettlementType[$v['settlement_type']];
$v['first_company_type'] =$this->CompanyType[$v['first_company_type']];
$v['second_company_type'] =$this->CompanyType[$v['second_company_type']];
$v['invoice_type'] =$this->InvoiceType[$v['invoice_type']];
$v['is_payment'] =$this->IsPayment[$v['is_payment']];
$v['collection'] =$this->Collection[$v['collection']];
}
if(isset($_REQUEST['export'])){
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),"op_name"=>"导出",'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData)]);
data2csv($dbres,'公司绑定生效列表',array(
"first_company_name"=>"甲方公司类型",
"first_company_type"=>"甲方公司",
"second_company_name"=>"乙方公司",
"second_company_type"=>"乙方公司类型",
"settlement_type"=>"结算周期",
"invoice_type"=>"开票类型",
"invoice_content"=>"开票内容",
"is_payment"=>"打款流程",
"collection"=>"收款方"
));
}
$count = $this->DBModel->field("count(id) count")->where($where)->find()['count'];
$this->assign('data', $dbres);
$page = set_pagination($count, $row);
if($page) {
$this->assign('_page', $page);
}
$this->assign('Status',$this->Status);
$this->assign('SettlementType',$this->SettlementType);
$this->assign('CompanyType',$this->CompanyType);
$this->assign('InvoiceType',$this->InvoiceType);
$this->assign('IsPayment',$this->IsPayment);
$this->display();
}
//审核列表
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();
}
$where['_string'] = '1 = 1';
if(isset($params['company_type'])){
$where['_string'] .= " AND (first_company_type='{$params['company_type']}' OR second_company_type='{$params['company_type']}')";
}
if(isset($params['company_id'])){
$where['_string'] .= " AND (first_company_id='{$params['company_id']}' OR second_company_id='{$params['company_id']}')";
}
if(isset($params['company_type']) & & isset($params['company_id'])){
unset($where['_string']);
$where['_string'] .= " (first_company_id='{$params['company_id']}' AND first_company_type='{$params['company_type']}') OR (second_company_id='{$params['company_id']}' AND second_company_type='{$params['company_type']}')";
}
if(isset($params['settlement_type'])){
$where['settlement_type'] = $params['settlement_type'];
}
if(isset($params['invoice_type'])){
$where['invoice_type'] = $params['invoice_type'];
}
if(isset($params['is_payment'])){
$where['is_payment'] = $params['is_payment'];
}
if(isset($params['status'])){
$where['status'] = $params['status'];
}
// $this->checkListOrCountAuthRestMap($where);//导出权限
$dbres = $this->DBlogModel->where($where)->order("FIELD(status,0,1,-1,-2,2),id desc");;
if(isset($params['export'])){
$dbres = $dbres->select();
}else{
$dbres = $dbres->page($page, $row)->select();
}
foreach($dbres as $k=>& $v){
$v['settlement_type'] =$this->SettlementType[$v['settlement_type']];
$v['first_company_type'] =$this->CompanyType[$v['first_company_type']];
$v['second_company_type'] =$this->CompanyType[$v['second_company_type']];
$v['invoice_type'] =$this->InvoiceType[$v['invoice_type']];
$v['is_payment'] =$this->IsPayment[$v['is_payment']];
$v['collection'] =$this->Collection[$v['collection']];
$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']['market_user'])){
if($v['status'] == -1){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["market"]= "{$ts}({$v['verify_log']['market_user']}) {$symbol} {$v['verify_log']['market_time']}";
}else{
$v["market"] = '--';
}
if(isset($v['verify_log']['admin_user'])){
if($v['status'] == -2){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["admin"]= "{$ts}({$v['verify_log']['admin_user']}) {$symbol} {$v['verify_log']['admin_time']}";
}else{
$v["admin"] = '--';
}
$v['oplist'] = $this->OpAuth($v);
}
if(isset($_REQUEST['export'])){
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),"op_name"=>"导出",'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData)]);
data2csv($dbres,'公司绑定审核列表',array(
"first_company_name"=>"甲方公司类型",
"first_company_type"=>"甲方公司",
"second_company_name"=>"乙方公司",
"second_company_type"=>"乙方公司类型",
"settlement_type"=>"结算周期",
"invoice_type"=>"开票类型",
"invoice_content"=>"开票内容",
"is_payment"=>"打款流程",
"collection"=>"收款方",
"create"=>"创建记录",
"market"=>"市场部审批",
"admin"=>"管理员审批",
"remark"=>"备注"
));
}
$count = $this->DBlogModel->field("count(id) count")->where($where)->find()['count'];
$this->assign('data', $dbres);
$page = set_pagination($count, $row);
if($page) {
$this->assign('_page', $page);
}
$this->assign('Status',$this->Status);
$this->assign('SettlementType',$this->SettlementType);
$this->assign('CompanyType',$this->CompanyType);
$this->assign('InvoiceType',$this->InvoiceType);
$this->assign('IsPayment',$this->IsPayment);
$this->assign('menubtn',$this->menuAuth());
$this->display();
}
public function marketAgree(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>& $v){
if($v['status'] != 0) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['market_user']=$this->admininfo["username"];
$v['verify_log']['market_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['status']=1;
$this->DBlogModel->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"市场部审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"市场部审核通过成功"
));
}
public function marketRefuse(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>& $v){
if($v['status'] != 0) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['market_user']=$this->admininfo["username"];
$v['verify_log']['market_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['status']=-1;
$this->DBlogModel->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"市场部审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"市场部审核拒绝成功"
));
}
public function adminRefuse(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>& $v){
if($v['status'] != 1) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['admin_user']=$this->admininfo["username"];
$v['verify_log']['admin_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['status']=-2;
$this->DBlogModel->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"管理员审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"管理员审核拒绝成功"
));
}
//管理员成功
public function adminAgree(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = $this->DBlogModel->field("*")->where("id in ({$ids})")->select();
foreach($dbres as $k=>& $v){
if($v['status'] != 1) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['admin_user']=$this->admininfo["username"];
$v['verify_log']['admin_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['status']=2;
$this->DBlogModel->save($v);
$id = $v['id'];
//保存到其他表
//查找是否存在
unset($v['id']);
unset($v['remark']);
unset($v['status']);
unset($v['verify_log']);
//判断公司id及类型
if($v['first_company_type'] == 0){
$t_company_id = $v['second_company_id'];
$t_company_type = $v['second_company_type'];
}else{
$t_company_id = $v['first_company_id'];
$t_company_type = $v['first_company_type'];
}
$where = "
(first_company_type ='{$t_company_type}' and first_company_id = '{$t_company_id}')
OR
(second_company_type ='{$t_company_type}' and second_company_id = '{$t_company_id}')
";
$hasdb = $this->DBModel->where($where)->find();
if(isset($hasdb['id'])){
$v['id'] = $hasdb['id'];
$this->DBModel->save($v);
}else{
$this->DBModel->add($v);
}
addOperationLog(['op_type'=>1,'key'=>$id,"op_name"=>"管理员审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"管理员审核通过成功"
));
}
public function addRelation()
{
if ($_POST) {
$params = I('post.');
if ($params['first_company_type'] == $params['second_company_type']) {
$this->error('合作甲乙双方不能是同类型公司');
}
if ($params['first_company_type']!=0 & & $params['second_company_type']!=0) {
$this->error('合作甲乙双方必须有个是己方公司');
}
$params['remark'] = $params['remark'] ?? '';
$params['status'] = 0;
$params['verify_log']=json_encode(["create_user"=>$this->admininfo["username"],"create_time"=>date("Y-m-d H:i:s")]);
//判断已有未审核
$where = "
(
(first_company_type ='{$params['first_company_type']}' and first_company_id = '{$params['first_company_id']}')
OR
(first_company_type ='{$params['second_company_type']}' and first_company_id = '{$params['second_company_id']}')
)
AND
(
(second_company_type ='{$params['first_company_type']}' and second_company_id = '{$params['first_company_id']}')
OR
(second_company_type ='{$params['second_company_type']}' and second_company_id = '{$params['second_company_id']}')
)
AND
status < 2
";
$hasdb = $this->DBlogModel->field("count(id) count")->where($where)->find()['count'];
if($hasdb > 0){
$this->error('当前甲乙双方已有审批单,请直接搜索后修改');
}
$id = $this->DBlogModel->add($params);
addOperationLog(['op_type'=>0,'key'=>$id,'op_name'=>'新增','url'=>U('lists')]);
$this->ajaxReturn(["msg"=>"添加成功","code"=>1,"url"=>U("lists")]);
} else {
$this->assign('companyType',$this->CompanyType);
$this->display();
}
}
public function editRelation()
{
if ($_POST) {
$p= I('post.');
if(!isset($p['id'])){
$this->error('参数错误');
}
//查询
$y = $this->DBlogModel->where("id='{$p['id']}'")->find();
$p['remark'] = $p['remark'] ?? '';
if($y['settlement_type'] != $p['settlement_type'] || $y['invoice_type'] != $p['invoice_type'] || $y['invoice_content'] != $p['invoice_content'] || $y['is_payment'] != $p['is_payment'] || $y['collection'] != $p['collection']){
$p['status'] = 0;
$p['verify_log'] = json_encode(["create_user"=>$this->admininfo["username"],"create_time"=>date("Y-m-d H:i:s")]);
}
$this->DBlogModel->save($p);
addOperationLog(['op_type'=>1,'key'=>$p['id'],'op_name'=>'修改','url'=>U('lists')]);
$this->ajaxReturn(["msg"=>"修改成功","code"=>1,"url"=>U("lists")]);
} else {
$params = I('get.');
$id = $params['id'] ?? 0;
$id = intval($id);
$map['id'] = $id;
$dbres = $this->DBlogModel->where($map)->find();
$this->assign('first_company_info',$this->getCompanyInfo($dbres['first_company_type'],$dbres['first_company_id']));
$this->assign('second_company_info',$this->getCompanyInfo($dbres['second_company_type'],$dbres['second_company_id']));
$dbres['first_company_type'] =$this->CompanyType[$dbres['first_company_type']];
$dbres['second_company_type'] =$this->CompanyType[$dbres['second_company_type']];
$this->assign('data', $dbres);
$this->display();
}
}
public function delRelation()
{
if(!isset($_REQUEST['id'])){
$this->error('参数错误');
}
$id = $_REQUEST['id'];
$res = $this->DBlogModel->where("id='{$id}'")->delete();
if($res !== false){
addOperationLog(['op_type'=>2,'key'=>$id,'op_name'=>'删除','url'=>U('lists')]);
$this->ajaxReturn(["msg"=>"删除成功","code"=>1,"url"=>U("lists")]);
}else{
$this->error('删除错误');
}
}
public function getCompanyList($type)
{
if($type ==''){
$this->success([],'',true);
}
$companyInfo = [];
if($type == 0){
//己方公司
$companyInfo = M("CompanyInfo","tab_")->field("id,partner company_name,link_man,link_phone")->where("status='1'")->select();
}
if($type == 1){
//上游
$companyInfo = M("Partner","tab_")->field("id,partner company_name,company_type,link_man,link_phone")->where("status='1'")->select();
}
if($type == 2){
//下游游
$companyInfo = M("PromoteCompany","tab_")->field("id,company_belong,company_name,company_type,settlement_contact link_man,contact_phone link_phone")->where("status='1'")->select();
}
foreach($companyInfo as $k=>& $v){
if(isset($v['company_belong'])){
$v['company_belong'] = getCompanyBlong($v['company_belong']);
}
if(isset($v['company_type'])){
$v['company_type'] = ($v['company_type'] == 1 ?"公司" :'个人');
}
if(isset($v['link_phone']) & & $v['link_phone']==0){
$v['link_phone'] = '';
}
}
$this->success($companyInfo,'',true);
}
public function getCompanyInfo($type,$id)
{
$companyInfo = [];
if($type == 0){
//己方公司
$companyInfo = M("CompanyInfo","tab_")->field("id,partner company_name,link_man,link_phone")->where("status='1' AND id='{$id}'")->find();
}
if($type == 1){
//上游
$companyInfo = M("Partner","tab_")->field("id,partner company_name,company_type,link_man,link_phone")->where("status='1' AND id='{$id}'")->find();
}
if($type == 2){
//下游游
$companyInfo = M("PromoteCompany","tab_")->field("id,company_belong,company_name,company_type,settlement_contact link_man,contact_phone link_phone")->where("status='1' AND id='{$id}'")->find();
}
if(isset($companyInfo['company_belong'])){
$companyInfo['company_belong'] = getCompanyBlong($companyInfo['company_belong']);
}
if(isset($companyInfo['company_type'])){
$companyInfo['company_type'] = ($companyInfo['company_type'] == 1 ? "公司" :'个人');
}
if(isset($companyInfo['link_phone']) & & $companyInfo['link_phone']==0){
$companyInfo['link_phone'] = '';
}
return $companyInfo;
}
public function menuAuth()
{
$addurl = U("addRelation");
$mentBtn = [
"addRelation"=>"< a class = 'butn' href = '{$addurl}' > 新增公司绑定< / a > ",
"marketAgree"=>"< a class = 'butn' id = 'marketAgree' > 市场部审批通过< / a > ",
"marketRefuse"=>"< a class = 'butn' id = 'marketRefuse' style = 'background-color: red;' > 市场部审核拒绝< / a > ",
"adminAgree"=>"< a class = 'butn' id = 'adminAgree' > 管理员审核通过< / a > ",
"adminRefuse"=>"< a class = 'butn' id = 'adminRefuse' style = 'background-color: red;' > 管理员审核拒绝< / a > "
];
$resarr = [];
foreach ($mentBtn as $k => $v) {
if(IS_ROOT){
$resarr[] = $v;
}else{
if(in_array($k,$this->OpAuthList)){
$resarr[] = $v;
}
}
}
return $resarr;
}
public function OpAuth($info)
{
$id = $info['id'];
//原始列表
$opBtn = [
"editRelation"=>"< a class = 'confirm editRelation' data-id = '{$id}' > 编辑< / a > ",
"delRelation"=>"< a class = 'confirm delRelation' data-id = '{$id}' style = 'color: red;' > 删除< / a > ",
];
//操作对应菜单
if($info['status'] == 2){
$optist = [];
}else{
$optist = ["editRelation","delRelation"];
}
//
$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 error($data)
{
header('Content-Type:application/json; charset=utf-8');
$data =json_encode(['msg'=>$data,"code"=>4000],JSON_UNESCAPED_UNICODE);
exit($data);
}
}