合肥金麓客户积分系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1208 lines
50 KiB

<?php
namespace app\admin\controller;
use app\task\controller\Task;
use think\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\Env;
class Client extends Common{
//客户列表
public function index(){
if(request()->isPost()){
$key=input('post.key');
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_leads')
->where(['status'=>1,'issuccess'=>-1])
->order('ut_time desc')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
if(!empty($list['data'])){
foreach($list['data'] as $k=>$v){
$list['data'][$k]['bn'] = $v['building'].'-'.$v['number'];
}
}
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
$khRankList = Db::table('crm_client_rank')->select();
$khStatusList = Db::table('crm_client_status')->select();
$xsSourceList = Db::table('crm_clues_source')->select();
//查询所有管理员(去除admin)
$adminResult = Db::name('admin')->where('group_id','<>', 1)->field('admin_id,username')->select();
$this -> assign('adminResult',$adminResult);
$this -> assign('khRankList',$khRankList);
$this -> assign('khStatusList',$khStatusList);
$this -> assign('xsSourceList',$xsSourceList); //线索/客户来源
return $this->fetch();
}
//(我的客户)列表
public function perCliList(){
if(request()->isPost()){
$key=input('post.key');
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_leads')
->where(['status'=>1,'issuccess'=>-1])
->where(['pr_user'=> Session::get('username')])
->order('ut_time desc')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
if(!empty($list['data'])){
foreach($list['data'] as $k=>$v){
$list['data'][$k]['bn'] = $v['building'].'-'.$v['number'];
}
}
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
$khRankList = Db::table('crm_client_rank')->select();
$khStatusList = Db::table('crm_client_status')->select();
$xsSourceList = Db::table('crm_clues_source')->select();
$this -> assign('khRankList',$khRankList);
$this -> assign('khStatusList',$khStatusList);
$this -> assign('xsSourceList',$xsSourceList); //线索/客户来源
return $this->fetch('personclient/index');
}
//成交客户列表
public function successCliList(){
if(request()->isPost()){
$where = [];
$where['issuccess'] = 1;
if(session('aid')!=1){
$where['pr_user'] = Session::get('username');
}
$key=input('post.key');
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_leads')
->where($where)
->order('ut_time desc')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
$khRankList = Db::table('crm_client_rank')->select();
$khStatusList = Db::table('crm_client_status')->select();
$xsSourceList = Db::table('crm_clues_source')->select();
$this -> assign('khRankList',$khRankList);
$this -> assign('khStatusList',$khStatusList);
$this -> assign('xsSourceList',$xsSourceList); //线索/客户来源
//查询所有管理员(去除admin)
$adminResult = Db::name('admin')->where('group_id','<>', 1)->field('admin_id,username')->select();
$this -> assign('adminResult',$adminResult);
return $this->fetch('client/chengjiao');
}
//批量导入,客户上传
public function xlsUpload(){
$xlsFile = Request::file('xlsFile');
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $xlsFile -> move(Env::get('root_path'). 'public' .DIRECTORY_SEPARATOR.'uploads');
if ($info) {
$result = importExecl(Env::get('root_path'). 'public' .DIRECTORY_SEPARATOR.'uploads/'.$info -> getSaveName());
$count = count($result); //统计总数据
if ($count>1000){
$msg = ['code' => -200,'msg'=>'数据量过大,请分批导入!','data'=>[]];
return json($msg);
}
unset($result[1]); //移除标题
//$userExists = []; //已存在的线索
foreach ($result as $key =>&$value){
//看下手机号是否存在。将存在的手机号保存在数组里。
$userExist = db('crm_leads')->where('phone', $value['J'])->find();
if ($userExist){
// array_push($userExists, $result[$key]['A']);
unset($result[$key]);
}else{
//客户名称、地区、行业类别、联系人、联系号码、客户级别、客户状态、用户名、备注
$value['kh_name'] = $value['A'];//A客户名称
unset($value['A']);
$value['pro'] = $value['B'];//B 省
unset($value['B']);
$value['city'] = $value['C'];//C 市
unset($value['C']);
$value['area'] = $value['D'];//D 区(县)
unset($value['D']);
$value['house'] = $value['E'];//E 小区名称
unset($value['E']);
$value['building'] = $value['F'];//F 楼栋
unset($value['F']);
$value['number'] = $value['G'];//G 门牌号
unset($value['G']);
//$value['xs_area'] = $value['B'];//B 地区
//unset($value['B']);
$value['kh_hangye'] = $value['H'];//H 行业类别
unset($value['H']);
$value['kh_contact'] = $value['I'];//I 联系人
unset($value['I']);
$value['phone'] = $value['J'];//J 联系号码
unset($value['J']);
$value['kh_rank'] = $value['K'];//K 客户级别
unset($value['K']);
$value['kh_status'] = $value['L'];//L 客户状态
unset($value['L']);
$value['kh_username'] = $value['M'];//M 用户名
unset($value['M']);
$value['remark'] = $value['N'];//N 备注
unset($value['N']);
$value['pr_user'] = Session::get('username');//H 负责人
$value['ut_time'] = date("Y-m-d H:i:s",time());//Q更新于
$value['at_time'] = date("Y-m-d H:i:s",time());//R创建时间
$value['at_user'] = Session::get('username');//T创建人
$value['status'] = 1;
//导入客户
//A客户名称,B客户级别,C客户状态,D最新跟进记录,E实际跟进时间,F下次跟进时间,G手机
//H微信号,I未跟进天数,J客户来源,K备注,L负责人,M所属公海,N划入公海时间
//O前所属部门,P所属部门,Q更新于,R创建时间,S客户需求,T创建人,U前负责人
// $value['kh_name'] = $value['A'];//A客户名称
// unset($value['A']);
// $value['kh_rank'] = $value['B'];//B客户级别
// unset($value['B']);
// $value['kh_status'] = $value['C'];//C客户状态
// unset($value['C']);
// $value['last_up_records'] = $value['D'];//D最新跟进记录
// unset($value['D']);
// $value['last_up_time'] = $value['E'];//E实际跟进时间
// unset($value['E']);
// $value['next_up_time'] = $value['F'];//F下次跟进时间
// unset($value['F']);
// $value['phone'] = $value['G'];//G手机
// unset($value['G']);
// $value['wechat'] = $value['H'];//H微信号
// unset($value['H']);
// //$value['未跟进天数'] = $value['I'];//I未跟进天数(不入库,直接过滤)
// unset($value['I']);
// $value['xs_source'] = $value['J'];//J客户来源/线索来源
// unset($value['J']);
// $value['remark'] = $value['K'];//K备注
// unset($value['K']);
// $value['pr_user'] = $value['L'] ? $value['L']: Session::get('username');//L负责人
// unset($value['L']);
// $value['pr_gh_type'] = $value['M'];//M所属公海
// unset($value['M']);
// $value['to_gh_time'] = $value['N'];//N划入公海时间
// unset($value['N']);
// $value['pr_dep_bef'] = $value['O'];//O前所属部门
// unset($value['O']);
// $value['pr_dep'] = $value['P'];//P所属部门
// unset($value['P']);
// $value['ut_time'] = $value['Q'] ? $value['Q'] : date("Y-m-d H:i:s",time());//Q更新于
// unset($value['Q']);
// $value['at_time'] = $value['R'] ? $value['R'] : date("Y-m-d H:i:s",time());//R创建时间
// unset($value['R']);
// $value['kh_need'] = $value['S'];//S客户需求
// unset($value['S']);
// $value['at_user'] = $value['T'] ? $value['T']: Session::get('username');//T创建人
// unset($value['T']);
// $value['pr_user_bef'] = $value['U'] ? $value['U']: Session::get('username'); //U前负责人
// unset($value['U']);
//$value['status'] = 1; //导入客户
}
}
$failcount = count($result); //最终的总数
$insertAll = Db::table('crm_leads')->insertAll($result);
if ($insertAll){
$msg = ['code' => 0,'msg'=>'导入'.$failcount.'条数据成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'客户导入失败,不可重复导入!','data'=>[]];
return json($msg);
}
}
}
//新建客户
public function add(){
if(request()->isPost()){
//dump($_POST);EXIT;
// <!-- 客户名称、地区、行业类别、联系人、联系号码、客户级别、客户状态、用户名、备注 -->
$data['phone'] = Request::param('phone');
$data['kh_name'] = Request::param('kh_name');
$data['xs_area'] = Request::param('xs_area');
$data['kh_hangye'] = Request::param('kh_hangye');
$data['kh_contact'] = Request::param('kh_contact');
$data['kh_rank'] = Request::param('kh_rank');
$data['kh_status'] = Request::param('kh_status');
// $data['kh_username'] = Request::param('kh_username');
$data['remark'] = Request::param('remark');
// $data['kh_need'] = Request::param('kh_need');
$data['at_user'] = Session::get('username');
$data['pr_user'] = Session::get('username');
$data['pr_user_bef'] = Session::get('username');
$data['ut_time'] = date("Y-m-d H:i:s",time());
$data['at_time'] = date("Y-m-d H:i:s",time());
$data['status'] = 1;
$data['ispublic'] = 3;
//新增字段
$data['pro'] = Request::param('pro');
$data['city'] = Request::param('city');
$data['area'] = Request::param('area');
$data['house'] = Request::param('house');
$data['building'] = Request::param('building');
$data['number'] = Request::param('number');
$wdata = array(
'pro'=>$data['pro'],
'city'=>$data['city'],
'area'=>$data['area'],
'house'=>$data['house'],
'building'=>$data['building'],
'number'=>$data['number']
);
$userExist = db('crm_leads')->where($wdata)->count();
if ($userExist){
$data['sort'] = $userExist;
}else{
$data['sort'] = 0;
}
// $userExist = db('crm_leads')->where('phone', $data['phone'])->find();
// if ($userExist){
// $msg = ['code' => -200,'msg'=>'抱歉,重复号码不可添加!','data'=>[]];
// return json($msg);
// }
// 检测当前客户数最大数量
$curname = Session::get('username');
$curget = Db::table('admin')->where(['username'=>$curname])->field('custlimit')->find();
$custlimit = $curget['custlimit'];
if(!$custlimit){
$sysinfo = Db::table('system')->where(['id'=>1])->field('custlimit')->find();
$custlimit = $sysinfo['custlimit'];
}
$wherecust = [];
$wherecust['pr_user'] = $curname;
$wherecust['status'] = 1;
$wherecust['ispublic'] = 3;
$wherecust['issuccess'] = -1;
$maxcustnum = Db::table('crm_leads')->where($wherecust)->count('id');
if($maxcustnum>=$custlimit){
$msg = ['code' => -200,'msg'=>'抱歉,您的客户数量已达上限'.$custlimit.'!','data'=>[]];
return json($msg);
}
$result = Db::table('crm_leads')->insert($data);
if ($result){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
}
// $xsSourceList = Db::table('crm_clues_source')->select();
$khRankList = Db::table('crm_client_rank')->select();
$khStatusList = Db::table('crm_client_status')->select();
$xsAreaList = Db::table('crm_clues_area')->select();
$xsHangyeList = Db::table('crm_client_hangye')->select();
$this -> assign('xsHangyeList',$xsHangyeList);
$this -> assign('xsAreaList',$xsAreaList);
$this -> assign('khRankList',$khRankList);
$this -> assign('khStatusList',$khStatusList);
return $this->fetch('client/add');
}
//编辑客户
public function edit(){
if (Request::isAjax()){
$data = Request::param();
$data['ut_time'] = date("Y-m-d H:i:s",time());
$wdata = array(
'pro'=>$data['pro'],
'city'=>$data['city'],
'area'=>$data['area'],
'house'=>$data['house'],
'building'=>$data['building'],
'number'=>$data['number']
);
//$userExist = db('crm_leads')->where($wdata)->fetchSql()->count();
//$userExist = db('crm_leads')->where($wdata)->whereOr(['phone'=>$data['phone']])->where([['id', '<>', $data['id']]])->fetchSql()->count();
$userExist = db('crm_leads')->where($wdata)->count();
if ($userExist){
if($userExist >= 1){
$userExist -=1;
}else{
$userExist =0;
}
$data['sort'] = $userExist;
}
$result = Db::table('crm_leads')->where(['id'=>$data['id']])->where('status',1)->update($data);
if ($result){
$msg = ['code' => 0,'msg'=>'编辑成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'编辑失败!','data'=>[]];
return json($msg);
}
}
$result = Db::table('crm_leads') ->where(['id' => Request::param('id')])->find();
$this -> assign('result',$result);
// $xsSourceList = Db::table('crm_clues_source')->select();
$khRankList = Db::table('crm_client_rank')->select();
$khStatusList = Db::table('crm_client_status')->select();
$xsAreaList = Db::table('crm_clues_area')->select();
$xsHangyeList = Db::table('crm_client_hangye')->select();
$this -> assign('xsHangyeList',$xsHangyeList);
$this -> assign('xsAreaList',$xsAreaList);
// $this -> assign('xsSourceList',$xsSourceList);
$this -> assign('khRankList',$khRankList);
$this -> assign('khStatusList',$khStatusList);
return $this -> fetch('client/edit');
}
//删除客户
public function del(){
$id = Request::param('id');
$result = Db::table('crm_leads')->where('id',$id)->where('status',1)->delete();
if ($result){
$msg = ['code' => 0,'msg'=>'删除成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'删除失败!','data'=>[]];
return json($msg);
}
}
//客户级别
public function rankList(){
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_client_rank')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch();
}
//添加客户级别
public function rankAdd(){
if(request()->isPost()){
$data['rank_name'] = Request::param('rank_name');
$data['add_time'] = time();
$result = Db::table('crm_client_rank')->insert($data);
if ($result){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
}
return $this->fetch('client/rank_list_add');
}
//编辑客户级别
public function rankEdit(){
if (Request::isAjax()){
$data = Request::param();
// 获取原状态
$oldstatus = Db::table('crm_client_rank')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['rank_name'];
$ischange = false;
if ($oldstatusname == $data['rank_name']) {
$msg = ['code' => -200,'msg'=>'没有变化无需修改','data'=>[]];
return json($msg);
}else{
$ischange = true;
}
$result = Db::table('crm_client_rank')->where(['id'=>$data['id']])->update($data);
if ($result){
// 状态修改后 客户编辑的原来状态都必须修改
if ($ischange) {
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_rank'=>$oldstatusname])->update(['kh_rank'=>$data['rank_name']]);
}
$msg = ['code' => 0,'msg'=>'编辑成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'编辑失败!','data'=>[]];
return json($msg);
}
}
$result = Db::table('crm_client_rank') ->where(['id' => Request::param('id')])->find();
$this -> assign('result',$result);
return $this -> fetch('client/rank_list_edit');
}
//删除客户级别
public function rankDel(){
$id = Request::param('id');
// 获取原状态
$oldstatus = Db::table('crm_client_rank')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['rank_name'];
$result = Db::table('crm_client_rank')->where('id',$id)->delete();
if ($result){
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_rank'=>$oldstatusname])->update(['kh_rank'=>'']);
$msg = ['code' => 0,'msg'=>'删除成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'删除失败!','data'=>[]];
return json($msg);
}
}
//客户状态
public function statusList(){
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_client_status')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch();
}
//添加客户状态
public function statusAdd(){
if(request()->isPost()){
$data['status_name'] = Request::param('status_name');
$data['add_time'] = time();
$result = Db::table('crm_client_status')->insert($data);
if ($result){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
}
return $this->fetch('client/status_list_add');
}
//编辑客户状态
public function statusEdit(){
if (Request::isAjax()){
$data = Request::param();
// 获取原状态
$oldstatus = Db::table('crm_client_status')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['status_name'];
$newstatusname = $data['status_name'];
$ischange = false;
if ($oldstatusname == $newstatusname) {
$msg = ['code' => -200,'msg'=>'状态没有变化无需修改','data'=>[]];
return json($msg);
}else{
$ischange = true;
}
$result = Db::table('crm_client_status')->where(['id'=>$data['id']])->update($data);
if ($result){
// 状态修改后 客户编辑的原来状态都必须修改
if ($ischange) {
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_status'=>$oldstatusname])->update(['kh_status'=>$newstatusname]);
}
$msg = ['code' => 0,'msg'=>'编辑成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'编辑失败!','data'=>[]];
return json($msg);
}
}
$result = Db::table('crm_client_status') ->where(['id' => Request::param('id')])->find();
$this -> assign('result',$result);
return $this -> fetch('client/status_list_edit');
}
//删除客户状态
public function statusDel(){
$id = Request::param('id');
// 获取原状态
$oldstatus = Db::table('crm_client_status')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['status_name'];
// $ischange = false;
// if ($oldstatusname == $data['status_name']) {
// $msg = ['code' => -200,'msg'=>'状态没有变化无需修改','data'=>[]];
// return json($msg);
// }else{
// $ischange = true;
// }
$result = Db::table('crm_client_status')->where('id',$id)->delete();
if ($result){
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_status'=>$oldstatusname])->update(['kh_status'=>'']);
$msg = ['code' => 0,'msg'=>'删除成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'删除失败!','data'=>[]];
return json($msg);
}
}
//移入公海
public function toMoveGh(){
//1,获取提交的线索ID 【1,2,3,4,】
$ids = Request::param('ids');
$this -> assign('ids',$ids);
if (Request::isAjax()){
$pr_gh_type = Request::param('pr_gh_type');
$idsArr = explode(",",$ids);
$count = 0;
foreach ($idsArr as $key => $value){
// $data['pr_user_bef'] = Db::table('crm_leads')->where(['id'=>$value])->field('pr_user')->find();
// $data['pr_user'] = $username;
// $data['id'] = $value;
// $insertAll = Db::name('crm_leads')->update($data);
$data['pr_gh_type'] = $pr_gh_type;
$data['to_gh_time'] = date("Y-m-d H:i:s",time());
$data['status'] = 2;//0-线索,1客户,2公海
$data['id'] = $value;
$result = Db::table('crm_leads')->where(['id'=>$value])->update($data);
if ($result){
$count ++;
}
}
if ($count > 0){
$msg = ['code' => 0,'msg'=>$count.'个客户移入公海成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'转入公海失败!','data'=>[]];
return json($msg);
}
// $data['pr_gh_type'] = Request::param('pr_gh_type');
// $data['to_gh_time'] = date("Y-m-d H:i:s",time());
// $data['status'] = 2;//0-线索,1客户,2公海
// $data['id'] = Request::param('id');
// $result = Db::table('crm_leads')->where(['id'=>$data['id']])->update($data);
// if ($result){
// $msg = ['code' => 0,'msg'=>'移入公海成功!','data'=>[]];
// return json($msg);
// }else{
// $msg = ['code' => -200,'msg'=>'抱歉,移入公海失败!','data'=>[]];
// return json($msg);
// }
}
$libTypeList = Db::table('crm_liberum_type')->select();
$this -> assign('libTypeList',$libTypeList);
// $result = Db::table('crm_leads') ->where(['id' => Request::param('id')])->find();
// $this -> assign('result',$result);
return $this -> fetch('client/move_gh');
}
//客户搜索
public function clientSearch(){
$page =input('page')?input('page'):1;
$limit =input('limit')?input('limit'):config('pageSize');
$keyword = Request::param('keyword');
$list= model('client') -> getClientSearchList($page,$limit,$keyword);
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
//(我的客户)搜索
public function personClientSearch(){
$page =input('page')?input('page'):1;
$limit =input('limit')?input('limit'):config('pageSize');
$keyword = Request::param('keyword');
$list= model('client') -> getPersonClientSearchList($page,$limit,$keyword);
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
//(我的客户)搜索
public function chengjiaoClientSearch(){
$page =input('page')?input('page'):1;
$limit =input('limit')?input('limit'):config('pageSize');
$keyword = Request::param('keyword');
$list= model('client') -> getChengjiaoClientSearchList($page,$limit,$keyword);
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
//写跟进
public function dialogue(){
$result = Db::table('crm_leads')->where(['id'=>Request::param('id')])->find();
$result['comment']= Db::table('crm_comment')->alias('com')->join('admin adm','com.user_id = adm.admin_id')->where(['leads_id'=>Request::param('id')])->field('com.*,adm.username,adm.avatar')->select();
foreach ($result['comment'] as $k => $v){
$result['comment'][$k]['reply'] = Db::table('crm_reply')->where(['comment_id'=>$v['id']])->select();
}
$cid = Session::get('aid'); //获取当前登录账号
$curname = Session::get('username'); //获取当前登录账号
//$this ->assign('cid',$cid); //获取当前登录账号$data['id']
$group_id = Db::table('admin')->where(['admin_id'=>$cid])->field('group_id')->find();
$this ->assign('group_id',$group_id['group_id']); //获取当前登录权限组账号
$this ->assign('curname',$curname); //获取当前登录账号
$this ->assign('result',$result);
//$this ->assign('result1',integer($result['id'])); //跟进上一个 下一个 获取当前id。
return $this -> fetch('client/dialogue');
}
//评论
public function comment(){
$data['leads_id'] = Request::param('leads_id');
$data['user_id'] = Session::get('aid');
$data['reply_msg'] = Request::param('reply_msg');
$data['create_date'] = time();
//更新跟进记录
$genjin['last_up_records'] = $data['reply_msg'];
$genjin['last_up_time'] = date("Y-m-d H:i:s",$data['create_date']);
$genjin['ut_time'] = date("Y-m-d H:i:s",time());
Db::table('crm_leads')->where(['id'=>$data['leads_id']])->update($genjin);
$result = Db::table('crm_comment')->insert($data);
$data['create_date'] = date("Y年m月d日 H:i",$data['create_date']);
if ($result){
return json(['code'=> 0,'msg'=>'评论成功!','data'=>$data]);
}else{
return json(['code'=>1,'msg'=>'评论失败!']);
}
}
//回复
public function reply(){
$data['comment_id'] = Request::param('cid');
$data['from_user_id'] = Session::get('user.id');
$data['to_user_id'] = Request::param('to_uid');
$data['reply_msg'] = Request::param('reply_msg');
$data['create_date'] = time();
$result = Db::table('crm_reply')->insert($data);
$data['create_date'] = date("Y年m月d日 H:i",$data['create_date']);
if ($result){
return json(['code'=> 0,'msg'=>'回复成功!','data'=>$data]);
}else{
return json(['code'=>1,'msg'=>'回复失败!']);
}
}
//客户转移,变更负责人
public function alterPrUser(){
//1,获取提交的线索ID 【1,2,3,4,】
$ids = Request::param('ids');
$this -> assign('ids',$ids);
//查询所有管理员(去除admin)
$adminResult = Db::name('admin')->where('group_id','<>', 1)->field('admin_id,username')->select();
$this -> assign('adminResult',$adminResult);
if (Request::isAjax()){
$username = Request::param('username');
$idsArr = explode(",",$ids);
$count = 0;
foreach ($idsArr as $key => $value){
$data['pr_user_bef'] = Db::table('crm_leads')->where(['id'=>$value])->field('pr_user')->find();
$data['pr_user'] = $username;
$data['id'] = $value;
$insertAll = Db::name('crm_leads')->update($data);
if ($insertAll){
$count ++;
}
}
if ($count > 0){
$msg = ['code' => 0,'msg'=>'转移'.$count.'个客户成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'转移失败!','data'=>[]];
return json($msg);
}
}
return $this -> fetch('client/alter_pr_user');
}
//客户转移,变更负责人(个人)
public function alterPrUserPri(){
//1,获取提交的线索ID 【1,2,3,4,】
$ids = Request::param('ids');
$this -> assign('ids',$ids);
//查询所有管理员(去除admin)
$adminResult = Db::name('admin')->where('group_id','<>', 1)->field('admin_id,username')->select();
$this -> assign('adminResult',$adminResult);
if (Request::isAjax()){
$username = Request::param('username');
$idsArr = explode(",",$ids);
$count = 0;
foreach ($idsArr as $key => $value){
$data['pr_user_bef'] = Db::table('crm_leads')->where(['id'=>$value])->field('pr_user')->find();
$data['pr_user'] = $username;
$data['id'] = $value;
$insertAll = Db::name('crm_leads')->update($data);
if ($insertAll){
$count ++;
}
}
if ($count > 0){
$msg = ['code' => 0,'msg'=>'转移'.$count.'个客户成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'转移失败!','data'=>[]];
return json($msg);
}
}
return $this -> fetch('personclient/alter_pr_user');
}
//客户行业
public function hangyeList(){
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_client_hangye')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch();
}
//添加客户级别
public function hangyeAdd(){
if(request()->isPost()){
$data['hy_name'] = Request::param('hy_name');
$data['add_time'] = time();
$result = Db::table('crm_client_hangye')->insert($data);
if ($result){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
}
return $this->fetch('client/hangye_list_add');
}
//编辑客户级别
public function hangyeEdit(){
if (Request::isAjax()){
$data = Request::param();
// 获取原状态
$oldstatus = Db::table('crm_client_hangye')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['hy_name'];
$ischange = false;
if ($oldstatusname == $data['hy_name']) {
$msg = ['code' => -200,'msg'=>'没有变化无需修改','data'=>[]];
return json($msg);
}else{
$ischange = true;
}
$result = Db::table('crm_client_hangye')->where(['id'=>$data['id']])->update($data);
if ($result){
// 状态修改后 客户编辑的原来状态都必须修改
if ($ischange) {
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_hangye'=>$oldstatusname])->update(['kh_hangye'=>$data['hy_name']]);
}
$msg = ['code' => 0,'msg'=>'编辑成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'编辑失败!','data'=>[]];
return json($msg);
}
}
$result = Db::table('crm_client_hangye') ->where(['id' => Request::param('id')])->find();
$this -> assign('result',$result);
return $this -> fetch('client/hangye_list_edit');
}
//删除客户级别
public function hangyeDel(){
$id = Request::param('id');
// 获取原状态
$oldstatus = Db::table('crm_client_hangye')->where(['id'=>$data['id']])->find();
$oldstatusname = $oldstatus['hy_name'];
$result = Db::table('crm_client_hangye')->where('id',$id)->delete();
if ($result){
// 所有的客户状态全部膝盖
$result2 = Db::table('crm_leads')->where(['kh_hangye'=>$oldstatusname])->update(['kh_hangye'=>'']);
$msg = ['code' => 0,'msg'=>'删除成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'删除失败!','data'=>[]];
return json($msg);
}
}
//积分活动
public function activeList(){
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('crm_client_active')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch();
}
//添加积分活动
public function activeAdd(){
if(request()->isPost()){
$data = input();
unset($data['/admin/client/activeadd_html']);
$data['add_time'] = time();
$result = Db::table('crm_client_active')->insert($data);
if ($result){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
}
return $this->fetch('client/active_list_add');
}
//编辑积分活动
public function activeEdit(){
if (Request::isAjax()){
$data = input();
unset($data['/admin/client/activeedit_html']);
// 获取原状态
$oldstatus = Db::table('crm_client_active')->where(['id'=>$data['id']])->find();
if (!$oldstatus) {
$msg = ['code' => -200,'msg'=>'数据不存在','data'=>[]];
return json($msg);
}else{
$ischange = true;
}
$result = Db::table('crm_client_active')->where(['id'=>$data['id']])->update($data);
if ($result){
$msg = ['code' => 0,'msg'=>'编辑成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'编辑失败!','data'=>[]];
return json($msg);
}
}
$result = Db::table('crm_client_active') ->where(['id' => Request::param('id')])->find();
$this -> assign('result',$result);
return $this -> fetch('client/active_list_edit');
}
//删除积分活动
public function activeDel(){
$id = Request::param('id');
// 获取原状态
$oldstatus = Db::table('crm_client_active')->where(['id'=>$id])->find();
$oldstatusname = $oldstatus['name'];
$result = Db::table('crm_client_active')->where('id',$id)->delete();
if ($result){
$msg = ['code' => 0,'msg'=>'删除成功!','data'=>[]];
return json($msg);
}else{
$msg = ['code' => -200,'msg'=>'删除失败!','data'=>[]];
return json($msg);
}
}
//添加积分活动
public function take(){
if(request()->isPost()){
$data = input();
$weixin_template_0 = config('weixin_template_0');
$admin = Db::table('admin') ->where(['admin_id' => $data['aid']])->find();
if(!$admin){
$msg = ['code' => -200,'msg'=>'经销商不能为空!','data'=>[]];
return json($msg);
}
//先保存提货单数据
$data['add_time'] = time();
$data['take_time'] = strtotime($data['take_time']);
$result = Db::table('admin_take')->insert($data,0,1);
if (!$result){
$msg = ['code' => -200,'msg'=>'添加失败!','data'=>[]];
return json($msg);
}
$activeInfo = Db::table('crm_client_active')->where(['id'=>$data['active_id']])->find();
if(!$activeInfo){
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}
$get_money_point_rate = $activeInfo['get_point']/$activeInfo['get_money'];
$out_mpney_point_rate = $activeInfo['out_point']/$activeInfo['out_money'];
$admin_point = Db::table('admin_point') ->where(['aid' => $data['aid']])->find();
if(!$admin_point){
$admin_point['aid'] = $data['aid'];
$admin_point['point'] = 0;
$admin_point['in_point'] = 0;
$admin_point['out_point'] = 0;
$admin_point['add_time'] = time();
$admin_point['id'] = Db::table('admin_point')->insert($admin_point,0,1);
}
$pre_point = $admin_point['in_point'];
//再计算抵扣积分 提货金额 = 可抵扣金额 * (积分/金额比)
$can_out_money = intval($data['take_money']);
$out_point = intval($can_out_money*$out_mpney_point_rate);
if($admin_point['in_point']<$out_point){
//看最多可抵扣积分和金额
$can_out_money = intval($admin_point['in_point']/$out_mpney_point_rate);
$out_point = intval($can_out_money*$out_mpney_point_rate);
}
//如果当前用户的在期积分大于抵扣积分 生成抵扣积分记录 扣除想要的积分值
if($out_point>0){
$out_point_log = [];
$out_point_log['aid'] = $data['aid'];
$out_point_log['take_id'] = $result;
$out_point_log['change'] = -$out_point;
$out_point_log['type'] = 1;
$out_point_log['aid'] = $data['aid'];
$out_point_log['in_point'] = $admin_point['in_point'];
$out_point_log['after_point'] = $admin_point['in_point'] - $out_point;
$out_point_log['active_id'] = $data['active_id'];
$out_point_log['active_name'] = $activeInfo['name'];
$out_point_log['take_money'] = intval($data['take_money']);
$out_point_log['money'] = $can_out_money;
$out_point_log['point'] = $out_point;
$out_point_log['remark'] = $activeInfo['content'];
$out_point_log['add_time'] = time();
Db::table('admin_point_log')->insert($out_point_log);
//扣除
Db::table('admin_point')->where(['id'=>$admin_point['id']])->setDec('point',$out_point);
Db::table('admin_point')->where(['id'=>$admin_point['id']])->setDec('in_point',$out_point);
//依次扣除 用户id 要扣除多少分
$this->subAdminPoint($data['aid'],$out_point);
}
//重新获取最新的数据了
$admin_point = Db::table('admin_point') ->where(['aid' => $data['aid']])->find();
//再计算提货积分 提货金额 * (积分/金额比)
$in_point = intval(intval($data['take_money']-$can_out_money)*$get_money_point_rate);
if($in_point>0){
$out_point_log = [];
$out_point_log['aid'] = $data['aid'];
$out_point_log['take_id'] = $result;
$out_point_log['change'] = $in_point;
$out_point_log['type'] = 0;
$out_point_log['in_point'] = $admin_point['in_point'];
$out_point_log['after_point'] = $admin_point['in_point'] + $in_point;
$out_point_log['active_id'] = $data['active_id'];
$out_point_log['active_name'] = $activeInfo['name'];
$out_point_log['take_money'] = intval($data['take_money']);
$out_point_log['money'] = intval($data['take_money']-$can_out_money);
$out_point_log['point'] = $out_point_log['use_point'] = $in_point;
$out_point_log['remark'] = $activeInfo['content'];
$out_point_log['out_time'] = strtotime(date('Y-m-d',strtotime('+1 years +1 days')))-1;
$out_point_log['add_time'] = time();
Db::table('admin_point_log')->insert($out_point_log);
//加上提货积分
Db::table('admin_point')->where(['id'=>$admin_point['id']])->setInc('point',$in_point);
Db::table('admin_point')->where(['id'=>$admin_point['id']])->setInc('in_point',$in_point);
}
//重新获取最新的数据了
$admin_point = Db::table('admin_point') ->where(['aid' => $data['aid']])->find();
//再生成积分变动消息
$message = [];
$message['aid'] = $data['aid'];
$message['openid'] = $admin['openid']?$admin['openid']:'';
$message['tempid'] = 0;
$message['take_time'] = $data['take_time'];
$message['good_name'] = $data['good_name'];
$message['take_money'] = intval($data['take_money']);
$message['out_point'] = $out_point;
$message['get_point'] = $in_point;
$message['point'] = $admin_point['point'];
$message['remark'] = str_replace(["{pre_point}","{take_money}","{out_point}","{get_point}","{point}","{active_content}"],[$pre_point,intval($data['take_money']),$out_point,$in_point,$admin_point['point'],$activeInfo['content']],$weixin_template_0);
$message['add_time'] = time();
$messageId = Db::table('admin_point_message')->insert($message,0,1);
if($messageId){
$task = new Task();
$task->sendWxMessageById($messageId);
}
$msg = ['code' => 0,'msg'=>'添加成功!','data'=>[]];
return json($msg);
}
$aid = input("id");
$admin = Db::table('admin') ->where(['admin_id' => $aid])->find();
$this -> assign('aid',$aid);
$this -> assign('admin',$admin);
$activeList = Db::table('crm_client_active') ->where(['status' => 1])->select();
$this -> assign('activeList',$activeList);
return $this->fetch('client/take_add');
}
//我的积分列表
public function userPointList(){
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('admin')
-> alias("a")
-> join('admin_point b','a.admin_id=b.aid')
->where('a.group_id',11)
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch('client/point_list');
}
//我的积分列表
public function personPointList(){
$aid = $this->sys_admin_info['admin_id'];
$result = db('admin')
-> alias("a")
-> join('admin_point b','a.admin_id=b.aid')
->where('a.admin_id',$aid)
->find();
$this->assign("result",$result);
return $this->fetch('client/point_list3');
}
//用户积分列表
public function userPointDetail(){
$aid = input("id");
if($this->sys_admin_info['group_id'] == 11){
$aid = $this->sys_admin_info['admin_id'];
}
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('admin_point_log')
->field("b.*,a.username,c.remark as take_remark,c.good_name")
-> alias("b")
-> join('admin a','a.admin_id=b.aid')
-> join('admin_take c','b.take_id=c.id')
->where('b.aid',$aid)
->order("id desc")
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
$this->assign("id",$aid);
return $this->fetch('client/point_detail');
}
//用户积分列表
public function personPointDetail(){
$aid = $this->sys_admin_info['admin_id'];
if(request()->isPost()){
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = db('admin_point_log')
->field("b.*,a.username,c.remark as take_remark,c.good_name")
-> alias("b")
-> join('admin a','a.admin_id=b.aid')
-> join('admin_take c','b.take_id=c.id')
->where('b.aid',$aid)
->order("id desc")
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
$this->assign("id",$aid);
return $this->fetch('client/point_detail2');
}
// 将扣除的积分指定到每条记录上
public function subAdminPoint($aid=0,$out_point=0){
if($aid ==0 || $out_point==0 ){
return 0;
}
$now_point = $out_point;
//获取用户所有的提货获取的积分且有效的且有剩余可抵扣的积分记录 按时间正序开始
$admin_point_logs = Db::table('admin_point_log')->where(['aid'=>$aid,'type'=>0,'status'=>0])->where('use_point','>',0)->order('add_time','asc')->select();
foreach ($admin_point_logs as $logs){
if($now_point>0){
$use_point = $now_point>=$logs['use_point'] ? 0 : $logs['use_point'] - $now_point;
$update = ['use_point'=>$use_point];
$now_point = $now_point>=$logs['use_point'] ? $now_point-$logs['use_point'] : 0;
Db::table('admin_point_log')->where(['id' => $logs['id']])->update($update);
}else{
break;
}
}
}
}