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.
wkcrm/application/crm/controller/Customer.php

1176 lines
47 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
use app\crm\logic\CustomerLogic;
4 years ago
use app\crm\traits\SearchConditionTrait;
4 years ago
use app\crm\traits\StarTrait;
3 years ago
use phpDocumentor\Reflection\Types\False_;
use think\Cache;
4 years ago
use think\Hook;
use think\Request;
use think\Db;
class Customer extends ApiCommon
{
4 years ago
use StarTrait, SearchConditionTrait;
3 years ago
4 years ago
/**
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
4 years ago
**/
4 years ago
public function _initialize()
{
$action = [
4 years ago
'permission' => ['exceldownload', 'setfollow', 'delete'],
4 years ago
'allow' => ['read', 'system', 'count', 'poolauthority', 'level']
4 years ago
];
4 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
4 years ago
$a = strtolower($request->action());
4 years ago
if (!in_array($a, $action['permission'])) {
parent::_initialize();
} else {
4 years ago
$param = Request::instance()->param();
$this->param = $param;
4 years ago
}
4 years ago
}
3 years ago
4 years ago
/**
* 客户列表
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function index()
{
$customerModel = model('Customer');
$param = $this->param;
$userInfo = $this->userInfo;
4 years ago
$param['user_id'] = $userInfo['id'];
4 years ago
$data = $customerModel->getDataList($param);
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 客户公海(没有负责人或已经到期)
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function pool()
{
$param = $this->param;
$param['action'] = 'pool';
4 years ago
unset($param['poolId']); # todo uniApp传来的参数临时删除掉 fanqi。
4 years ago
$data = model('Customer')->getDataList($param);
return resultArray(['data' => $data]);
4 years ago
}
3 years ago
4 years ago
/**
* 添加客户
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function save()
{
$customerModel = model('Customer');
$param = $this->param;
$userInfo = $this->userInfo;
3 years ago
$param['user_id'] = $userInfo['id'];
4 years ago
$param['create_user_id'] = $userInfo['id'];
$param['owner_user_id'] = $userInfo['id'];
if ($res = $customerModel->createData($param)) {
return resultArray(['data' => $res]);
} else {
return resultArray(['error' => $customerModel->getError()]);
}
}
3 years ago
4 years ago
/**
* 客户详情
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function read()
{
$customerModel = model('Customer');
3 years ago
$cutomerLogic = new CustomerLogic();
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$data = $customerModel->getDataById($param['id'], $userInfo['id']);
if (!$data) {
return resultArray(['error' => $customerModel->getError()]);
}
//数据权限判断
$userModel = new \app\admin\model\User();
$auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'read');
//读权限
$roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
$rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
//判断是否客户池数据
$wherePool = $customerModel->getWhereByPool();
$resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['id']])->where($wherePool)->find();
2 years ago
if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
4 years ago
$authData['dataAuth'] = (int)0;
4 years ago
return resultArray(['data' => $authData]);
}
2 years ago
// 判断是否有客户捞取审批
$customerCheck = db('crm_customer_check')->where(['customer_id' => $param['id']])->where(['check_status' => 0])->find();
if (!empty($customerCheck)) {
$data['flow_id'] = $customerCheck['flow_id'];
2 years ago
$data['owner_user_id']=$customerCheck['user_id'];
2 years ago
}
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 编辑客户
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function update()
{
$customerModel = model('Customer');
$param = $this->param;
$userInfo = $this->userInfo;
//数据详情
$data = $customerModel->getDataById($param['id']);
if (!$data) {
return resultArray(['error' => $customerModel->getError()]);
4 years ago
}
3 years ago
4 years ago
$param['user_id'] = $userInfo['id'];
if ($customerModel->updateDataById($param, $param['id'])) {
return resultArray(['data' => '编辑成功']);
} else {
return resultArray(['error' => $customerModel->getError()]);
4 years ago
}
4 years ago
}
3 years ago
4 years ago
/**
* 删除客户
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function delete()
{
$param = $this->param;
3 years ago
$user = new ApiCommon();
4 years ago
$userInfo = $user->userInfo;
4 years ago
// 是否客户池
if ($param['isSeas'] == 1) {
$permission = checkPerByAction('crm', 'customer', 'poolDelete');
} else {
$permission = checkPerByAction('crm', 'customer', 'delete');
}
if ($permission == false) {
return resultArray(['error' => '无权操作']);
}
$customerModel = model('Customer');
$userModel = new \app\admin\model\User();
$recordModel = new \app\admin\model\Record();
$fileModel = new \app\admin\model\File();
$actionRecordModel = new \app\admin\model\ActionRecord();
if (!is_array($param['id'])) {
$customer_id[] = $param['id'];
} else {
$customer_id = $param['id'];
}
$delIds = [];
$errorMessage = [];
3 years ago
4 years ago
//数据权限判断
$auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'delete');
//判断是否客户池数据(客户池数据只有管理员可以删)
$adminId = $userModel->getAdminId();
$wherePool = $customerModel->getWhereByPool();
4 years ago
foreach ($customer_id as $k => $v) {
4 years ago
$isDel = true;
//数据详情
$data = db('crm_customer')->where(['customer_id' => $v])->find();
if (!$data) {
$isDel = false;
4 years ago
$errorMessage[] = 'id为' . $v . '的客户删除失败,错误原因:' . $customerModel->getError();
4 years ago
}
$resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
4 years ago
if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && $isDel) {
4 years ago
$isDel = false;
3 years ago
$errorMessage[] = '无权操作';
4 years ago
}
// 公海 (原逻辑,公海仅允许管理员删除,修改为授权,不再限制)
// if ($resPool && !in_array($data['owner_user_id'],$adminId)) {
// $isDel = false;
// $errorMessage[] = '名称为'.$data['name'].'的客户删除失败,错误原因:无权操作';
// }
3 years ago
//有商机、合同、联系人则不能删除
4 years ago
if ($isDel) {
$resBusiness = db('crm_business')->where(['customer_id' => $v])->find();
if ($resBusiness) {
$isDel = false;
3 years ago
$errorMessage[] = '客户下存在商机,不能删除';
4 years ago
}
}
if ($isDel) {
$resContacts = db('crm_contacts')->where(['customer_id' => $v])->find();
if ($resContacts) {
$isDel = false;
3 years ago
// $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在联系人,不能删除';
$errorMessage[] = '客户下存在联系人,不能删除';
4 years ago
}
}
4 years ago
if ($isDel) {
4 years ago
$resContract = db('crm_contract')->where(['customer_id' => $v])->find();
4 years ago
if ($resContract) {
$isDel = false;
3 years ago
$errorMessage[] = '客户下存在合同,不能删除';
4 years ago
}
}
4 years ago
if ($isDel) {
$delIds[] = $v;
4 years ago
}
4 years ago
}
3 years ago
$dataInfo = $customerModel->where('customer_id', ['in', $delIds])->select();
4 years ago
if ($delIds) {
$delRes = $customerModel->delDatas($delIds);
if (!$delRes) {
return resultArray(['error' => $customerModel->getError()]);
}
3 years ago
// 删除客户扩展数据
db('crm_customer_data')->whereIn('customer_id', $delIds)->delete();
// 删除跟进记录
4 years ago
$recordModel->delDataByTypes(2, $delIds);
3 years ago
// 删除关联附件
4 years ago
$fileModel->delRFileByModule('crm_customer', $delIds);
3 years ago
// 删除关联操作记录
4 years ago
$actionRecordModel->delDataById(['types' => 'crm_customer', 'action_id' => $delIds]);
4 years ago
foreach ($dataInfo as $k => $v) {
RecordActionLog($userInfo['id'], 'crm_customer', 'delete', $v['name'], '', '', '删除了客户:' . $v['name']);
}
4 years ago
}
if ($errorMessage) {
return resultArray(['error' => $errorMessage]);
} else {
return resultArray(['data' => '删除成功']);
}
}
3 years ago
4 years ago
/**
* 客户转移
* @param owner_user_id 变更负责人
* @param is_remove 1移出2转为团队成员
* @param types business,contract 相关模块
* @param type 权限 1只读2读写
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function transfer()
{
$param = $this->param;
$userInfo = $this->userInfo;
$customerModel = model('Customer');
$businessModel = model('Business');
$contractModel = model('Contract');
$contactsModel = model('Contacts');
4 years ago
$settingModel = model('Setting');
$customerConfigModel = model('CustomerConfig');
$userModel = new \app\admin\model\User();
3 years ago
4 years ago
if (!$param['owner_user_id']) {
return resultArray(['error' => '变更负责人不能为空']);
}
if (!$param['customer_id'] || !is_array($param['customer_id'])) {
4 years ago
return resultArray(['error' => '请选择需要转移的客户']);
}
4 years ago
$is_remove = ($param['is_remove'] == 2) ? 2 : 1;
4 years ago
$type = $param['type'] == 2 ?: 1;
$types = $param['types'] ?: [];
3 years ago
4 years ago
$data = [];
$data['owner_user_id'] = $param['owner_user_id'];
$data['update_time'] = time();
$data['follow'] = '待跟进';
# 获取客户的时间
$data['obtain_time'] = time();
3 years ago
4 years ago
$ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
$errorMessage = [];
foreach ($param['customer_id'] as $customer_id) {
$customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
if (!$customerInfo) {
4 years ago
$errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户转移失败,错误原因:数据不存在;';
4 years ago
continue;
}
$resCustomer = true;
//权限判断
if (!$customerModel->checkData($customer_id)) {
4 years ago
$errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:无权限;';
4 years ago
continue;
}
//拥有客户数上限检测
4 years ago
if (!$customerConfigModel->checkData($param['owner_user_id'], 1)) {
$errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:' . $customerConfigModel->getError();
4 years ago
continue;
}
3 years ago
4 years ago
//团队成员
$teamData = [];
$teamData['type'] = $type; //权限 1只读2读写
$teamData['user_id'] = [$customerInfo['owner_user_id']]; //协作人
$teamData['types'] = 'crm_customer'; //类型
$teamData['types_id'] = $customer_id; //类型ID
$teamData['is_del'] = ($is_remove == 1) ? 1 : '';
4 years ago
$res = $settingModel->createTeamData($teamData);
4 years ago
4 years ago
# 处理分配标识,待办事项专用
$data['is_allocation'] = 1;
3 years ago
4 years ago
$resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
if (!$resCustomer) {
4 years ago
$errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:数据出错;';
4 years ago
continue;
4 years ago
} else {
# 处理转移时,负责人出现在只读和读写成员列表中
$customerArray = [];
$teamCustomer = db('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customer_id)->find();
if (!empty($teamCustomer['ro_user_id'])) {
$customerRo = arrayToString(array_diff(stringToArray($teamCustomer['ro_user_id']), [$teamCustomer['owner_user_id']]));
$customerArray['ro_user_id'] = $customerRo;
}
if (!empty($teamCustomer['rw_user_id'])) {
$customerRo = arrayToString(array_diff(stringToArray($teamCustomer['rw_user_id']), [$teamCustomer['owner_user_id']]));
$customerArray['rw_user_id'] = $customerRo;
}
db('crm_customer')->where('customer_id', $customer_id)->update($customerArray);
}
3 years ago
4 years ago
if (in_array('crm_contacts', $types)) {
4 years ago
$contactsIds = [];
$contactsIds = db('crm_contacts')->where(['customer_id' => $customer_id])->column('contacts_id');
if ($contactsIds) {
$resContacts = $contactsModel->transferDataById($contactsIds, $param['owner_user_id'], $type, $is_remove);
if ($resContacts !== true) {
$errorMessage[] = $resContacts;
4 years ago
continue;
4 years ago
}
4 years ago
}
}
3 years ago
4 years ago
//商机、合同转移
4 years ago
if (in_array('crm_business', $types)) {
4 years ago
$businessIds = [];
$businessIds = db('crm_business')->where(['customer_id' => $customer_id])->column('business_id');
if ($businessIds) {
$resBusiness = $businessModel->transferDataById($businessIds, $param['owner_user_id'], $type, $is_remove);
if ($resBusiness !== true) {
4 years ago
$errorMessage = $errorMessage ? array_merge($errorMessage, $resBusiness) : $resBusiness;
continue;
4 years ago
}
4 years ago
}
}
3 years ago
4 years ago
if (in_array('crm_contract', $types)) {
4 years ago
$contractIds = [];
$contractIds = db('crm_contract')->where(['customer_id' => $customer_id])->column('contract_id');
if ($contractIds) {
$resContract = $contractModel->transferDataById($contractIds, $param['owner_user_id'], $type, $is_remove);
if ($resContract !== true) {
4 years ago
$errorMessage = $errorMessage ? array_merge($errorMessage, $resContract) : $resContract;
continue;
4 years ago
}
4 years ago
}
}
4 years ago
//修改记录
4 years ago
updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户转移给:' . $ownerUserName);
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'transfer', $customerInfo['name'], '', '', '将客户:' . $customerInfo['name'] . '转移给:' . $ownerUserName);
4 years ago
}
if (!$errorMessage) {
return resultArray(['data' => '转移成功']);
} else {
return resultArray(['error' => $errorMessage]);
}
4 years ago
}
3 years ago
4 years ago
/**
* 客户放入公海(负责人置为0)
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function putInPool()
{
4 years ago
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要放入公海的客户!']);
if (!is_array($this->param['customer_id'])) return resultArray(['error' => '客户ID格式不正确']);
if (empty($this->param['pool_id'])) return resultArray(['error' => '请选择公海!']);
4 years ago
$userInfo = $this->userInfo;
4 years ago
$userId = $userInfo['id'];
$customerIds = $this->param['customer_id'];
$poolId = $this->param['pool_id'];
3 years ago
$customerModel = new \app\crm\model\Customer();
4 years ago
# 消息数据
$message = [];
# 获取客户数据
$customerData = [];
$customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name'])->whereIn('customer_id', $customerIds)->select();
3 years ago
foreach ($customerList as $key => $value) {
4 years ago
$customerData[$value['customer_id']] = $value;
4 years ago
}
4 years ago
# 整理数据
$ip = request()->ip();
$poolRelationData = [];
$poolRecordData = [];
$fieldRecordData = [];
$operationLogData = [];
3 years ago
foreach ($customerIds as $key => $value) {
4 years ago
if (empty($customerData[$value])) {
$message[] = '将客户放入公海失败,错误原因:数据不存在!';
unset($customerIds[(int)$key]);
4 years ago
continue;
}
3 years ago
if (!$customerModel->checkData($value)) {
$message[] = '"' . $customerData[$value]['name'] . '"放入公海失败,错误原因:无权限';
continue;
}
4 years ago
if (isset($customerData[$value]['owner_user_id']) && empty($customerData[$value]['owner_user_id'])) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》放入公海失败,错误原因:已经处于公海!';
unset($customerIds[(int)$key]);
4 years ago
continue;
}
4 years ago
# 公海关联数据
$poolRelationData[] = [
'pool_id' => $poolId,
'customer_id' => $value
];
# 公海操作记录数据
$poolRecordData[] = [
'customer_id' => $value,
'user_id' => $userId,
'pool_id' => $poolId,
'type' => 2,
'create_time' => time()
];
# 字段操作记录数据
$fieldRecordData[] = [
3 years ago
'user_id' => $userId,
'types' => 'crm_customer',
'action_id' => $value,
'content' => '将客户放入公海',
4 years ago
'create_time' => time()
];
# 数据操作日志数据
$operationLogData[] = [
3 years ago
'user_id' => $userId,
'client_ip' => $ip,
'module' => 'crm_customer',
'action_id' => $value,
'content' => '将客户放入公海',
4 years ago
'create_time' => time(),
'action_name' => 'update',
'target_name' => !empty($customerData[$value]['name']) ? $customerData[$value]['name'] : ''
];
4 years ago
}
4 years ago
if (empty($customerIds)) return resultArray(['error' => $message]);
Db::startTrans();
try {
# 修改客户数据
Db::name('crm_customer')->whereIn('customer_id', $customerIds)->exp('before_owner_user_id', 'owner_user_id')->update([
'ro_user_id' => '',
'rw_user_id' => '',
'owner_user_id' => 0,
'into_pool_time' => time()
]);
3 years ago
4 years ago
# 删除联系人的负责人
Db::name('crm_contacts')->whereIn('customer_id', $customerIds)->update(['owner_user_id' => 0]);
3 years ago
4 years ago
# 将客户放入公海
Db::name('crm_customer_pool_relation')->insertAll($poolRelationData);
3 years ago
4 years ago
# 公海操作记录
Db::name('crm_customer_pool_record')->insertAll($poolRecordData);
3 years ago
4 years ago
# 字段操作记录
Db::name('admin_action_record')->insertAll($fieldRecordData);
# 数据操作日志
Db::name('admin_operation_log')->insertAll($operationLogData);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$message = ['操作失败!'];
4 years ago
}
4 years ago
return resultArray(!empty($message) ? ['error' => $message] : ['data' => '操作成功!']);
// $param = $this->param;
// $userInfo = $this->userInfo;
// $customerModel = model('Customer');
// $settingModel = new \app\crm\model\Setting();
// if (!$param['customer_id'] || !is_array($param['customer_id'])) {
// return resultArray(['error' => '请选择需要放入公海的客户']);
// }
// $data = [];
// $data['owner_user_id'] = 0;
// $data['is_lock'] = 0;
// $data['update_time'] = time();
// $errorMessage = [];
// foreach ($param['customer_id'] as $customer_id) {
// $customerInfo = [];
// $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
// if (!$customerInfo) {
// $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户放入公海失败,错误原因:数据不存在;';
// continue;
// }
// //权限判断
// if (!$customerModel->checkData($customer_id)) {
// $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:无权限';
// continue;
// }
// //将团队成员全部清除
// $data['ro_user_id'] = '';
// $data['rw_user_id'] = '';
// $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
// if (!$resCustomer) {
// $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:数据出错;';
// continue;
// }
// //联系人负责人清除
// db('crm_contacts')->where(['customer_id' => $customer_id])->update(['owner_user_id' => 0]);
// //修改记录
// updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户放入公海');
// RecordActionLog($userInfo['id'],'crm_pool','pool',$customerInfo['name'],'','','将客户'.$customerInfo['name'].'放入公海');
// }
// if (!$errorMessage) {
// return resultArray(['data' => '操作成功']);
// } else {
// return resultArray(['error' => $errorMessage]);
// }
4 years ago
}
3 years ago
4 years ago
/**
* 客户锁定,解锁
* @param is_lock 1锁定2解锁
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function lock()
{
$param = $this->param;
$userInfo = $this->userInfo;
$customerModel = model('Customer');
4 years ago
$customerConfigModel = model('CustomerConfig');
4 years ago
$is_lock = ((int)$param['is_lock'] == 2) ? (int)$param['is_lock'] : 1;
$lock_name = ($is_lock == 2) ? '解锁' : '锁定';
if (!$param['customer_id'] || !is_array($param['customer_id'])) {
4 years ago
return resultArray(['error' => '请选择需要' . $lock_name . '的客户']);
4 years ago
}
$data = [];
$data['is_lock'] = ($is_lock == 1) ? $is_lock : 0;
4 years ago
$data['update_time'] = time();
4 years ago
$errorMessage = [];
foreach ($param['customer_id'] as $customer_id) {
$customerInfo = [];
$customerInfo = $customerModel->getDataById($customer_id);
if (!$customerInfo) {
4 years ago
$errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户' . $lock_name . '失败,错误原因:数据不存在;';
4 years ago
continue;
}
//权限判断
if (!$customerModel->checkData($customer_id)) {
4 years ago
$errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:无权限';
4 years ago
continue;
}
//锁定上限检测
4 years ago
if ($is_lock == 1 && !$customerConfigModel->checkData($customerInfo['owner_user_id'], 2)) {
$errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:' . $customerConfigModel->getError();
continue;
}
4 years ago
//已成交客户,锁定,提示无需锁定
// if ($customerInfo['deal_status'] == '已成交' && $is_lock == 1) {
// $errorMessage[] = $customerInfo['name'].$lock_name.'失败,错误原因:已成交状态,无需锁定';
3 years ago
// continue;
// }
4 years ago
$resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
if (!$resCustomer) {
4 years ago
$errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:数据出错;';
4 years ago
}
//修改记录
4 years ago
updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户' . $lock_name);
3 years ago
if ($is_lock == 2) {
RecordActionLog($userInfo['id'], 'crm_customer', 'islock', $customerInfo['name'], '', '', '将客户' . $customerInfo['name'] . $lock_name);
} else {
RecordActionLog($userInfo['id'], 'crm_customer', 'lock', $customerInfo['name'], '', '', '将客户' . $customerInfo['name'] . $lock_name);
4 years ago
}
4 years ago
}
if (!$errorMessage) {
return resultArray(['data' => '操作成功']);
} else {
return resultArray(['error' => $errorMessage]);
4 years ago
}
4 years ago
}
3 years ago
4 years ago
/**
* 客户领取
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function receive()
{
$param = $this->param;
$userInfo = $this->userInfo;
$customerModel = model('Customer');
$customerConfigModel = model('CustomerConfig');
3 years ago
4 years ago
$customer_ids = $param['customer_id'] ?: $userInfo['id'];
4 years ago
if (!$customer_ids || !is_array($customer_ids)) {
4 years ago
return resultArray(['error' => '请选择需要领取的客户']);
4 years ago
}
$errorMessage = [];
$wherePool = $customerModel->getWhereByPool();
4 years ago
foreach ($customer_ids as $k => $v) {
4 years ago
$dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
//判断是否是客户池数据
$resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
if (!$resPool) {
4 years ago
$errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:非公海数据无权操作;';
4 years ago
continue;
}
//拥有客户数上限检测
4 years ago
if (!$customerConfigModel->checkData($userInfo['id'], 1)) {
$errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:' . $customerConfigModel->getError();
4 years ago
continue;
4 years ago
}
4 years ago
$data = [];
$data['owner_user_id'] = $userInfo['id'];
$data['update_time'] = time();
$data['deal_time'] = time();
$data['follow'] = '待跟进';
//将团队成员全部清除
$data['ro_user_id'] = '';
$data['rw_user_id'] = '';
# 获取客户的时间
$data['obtain_time'] = time();
$resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
if (!$resCustomer) {
4 years ago
$errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:数据出错;';
4 years ago
continue;
}
//联系人领取
db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $userInfo['id']]);
//修改记录
4 years ago
updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '领取了客户');
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'update', $dataName, '', '', '领取了客户:' . $dataName);
4 years ago
}
if (!$errorMessage) {
return resultArray(['data' => '领取成功']);
} else {
return resultArray(['error' => $errorMessage]);
4 years ago
}
}
3 years ago
4 years ago
/**
* 客户分配
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function distribute()
{
$param = $this->param;
$userInfo = $this->userInfo;
$customerModel = model('Customer');
$userModel = new \app\admin\model\User();
$customerConfigModel = model('CustomerConfig');
3 years ago
4 years ago
$customer_ids = $param['customer_id'];
$owner_user_id = $param['owner_user_id'];
if (!$customer_ids || !is_array($customer_ids)) {
4 years ago
return resultArray(['error' => '请选择需要分配的客户']);
4 years ago
}
if (!$owner_user_id) {
4 years ago
return resultArray(['error' => '请选择分配人']);
4 years ago
}
$ownerUserName = $userModel->getUserNameById($owner_user_id);
3 years ago
4 years ago
$errorMessage = [];
$wherePool = $customerModel->getWhereByPool();
4 years ago
foreach ($customer_ids as $k => $v) {
4 years ago
$dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
//判断是否是客户池数据
$resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
if (!$resPool) {
4 years ago
$errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:非公海数据无权操作;';
4 years ago
continue;
}
//拥有客户数上限检测
4 years ago
if (!$customerConfigModel->checkData($owner_user_id, 1)) {
$errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:' . $customerConfigModel->getError();
continue;
4 years ago
}
$data = [];
$data['owner_user_id'] = $owner_user_id;
$data['update_time'] = time();
$data['deal_time'] = time();
$data['follow'] = '待跟进';
//将团队成员全部清除
$data['ro_user_id'] = '';
4 years ago
$data['rw_user_id'] = '';
# 处理分配标识,待办事项专用
$data['is_allocation'] = 1;
4 years ago
# 获取客户的时间
$data['obtain_time'] = time();
4 years ago
$resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
if (!$resCustomer) {
4 years ago
$errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:数据出错;';
4 years ago
}
db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $owner_user_id]);
//修改记录
4 years ago
updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '将客户分配给:' . $ownerUserName);
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'distribute', $dataName, '', '', '将客户' . $dataName . '分配给:' . $ownerUserName);
4 years ago
//站内信
$send_user_id[] = $owner_user_id;
4 years ago
$sendContent = $userInfo['realname'] . '将客户《' . $dataName . '》,分配给您';
4 years ago
if ($send_user_id) {
sendMessage($send_user_id, $sendContent, $v, 1);
4 years ago
}
4 years ago
}
if (!$errorMessage) {
return resultArray(['data' => '分配成功']);
} else {
return resultArray(['error' => $errorMessage]);
4 years ago
}
}
3 years ago
4 years ago
/**
* 客户导出
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function excelExport()
{
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
$action_name = '导出全部';
4 years ago
if ($param['customer_id']) {
4 years ago
$param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
3 years ago
$action_name = '导出选中';
4 years ago
}
$param['is_excel'] = 1;
$excelModel = new \app\admin\model\Excel();
// 导出的字段列表
$fieldModel = new \app\admin\model\Field();
3 years ago
$field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id'], '', 'excel');
4 years ago
// 文件名
4 years ago
$file_name = '5kcrm_customer_' . date('Ymd');
3 years ago
4 years ago
$model = model('Customer');
$temp_file = $param['temp_file'];
unset($param['temp_file']);
$page = $param['page'] ?: 1;
unset($param['page']);
unset($param['export_queue_index']);
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出客户');
4 years ago
return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
4 years ago
$param['page'] = $page;
$param['limit'] = $limit;
$data = $model->getDataList($param);
$data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
return $data;
});
3 years ago
4 years ago
}
3 years ago
4 years ago
/**
* 客户导入模板下载
* @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function excelDownload($save_path = '')
{
3 years ago
# 下次升级
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$excelModel = new \app\admin\model\Excel();
// 导入的字段列表
$fieldModel = new \app\admin\model\Field();
4 years ago
$fieldParam['types'] = 'crm_customer';
$fieldParam['action'] = 'excel';
4 years ago
$field_list = $fieldModel->field($fieldParam);
3 years ago
$field = [1 => [
'field' => 'owner_user_id',
'types' => 'crm_customer',
'name' => '负责人',
'form_type' => 'user',
'default_value' => '',
'is_null' => 1,
'input_tips' => '',
'setting' => array(),
'is_hidden' => 0,
3 years ago
'writeStatus' => 1,
'value' => '']
];
3 years ago
$first_array = array_splice($field_list, 2, 0, $field);
4 years ago
$excelModel->excelImportDownload($field_list, 'crm_customer', $save_path);
}
3 years ago
4 years ago
/**
* 客户数据导入
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function excelImport()
{
$param = $this->param;
$userInfo = $this->userInfo;
$excelModel = new \app\admin\model\Excel();
$param['create_user_id'] = $userInfo['id'];
4 years ago
$param['owner_user_id'] = $param['owner_user_id'] ?: 0;
4 years ago
$param['deal_time'] = time();
$param['deal_status'] = '未成交';
$param['types'] = 'crm_customer';
$file = request()->file('file');
// $res = $excelModel->importExcel($file, $param, $this);
$res = $excelModel->batchImportData($file, $param, $this);
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'excel', '导入客户', '', '', '导入客户');
4 years ago
return resultArray(['data' => $excelModel->getError()]);
4 years ago
}
3 years ago
4 years ago
/**
* 客户标记为已跟进
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
4 years ago
public function setFollow()
{
4 years ago
$param = $this->param;
4 years ago
$customerIds = $param['id'] ?: [];
4 years ago
if (!$customerIds || !is_array($customerIds)) {
4 years ago
return resultArray(['error' => '参数错误']);
4 years ago
}
$data['follow'] = '已跟进';
$data['update_time'] = time();
4 years ago
$res = db('crm_customer')->where(['customer_id' => ['in', $customerIds]])->update($data);
4 years ago
if (!$res) {
4 years ago
return resultArray(['error' => '操作失败,请重试']);
4 years ago
}
4 years ago
return resultArray(['data' => '跟进成功']);
4 years ago
}
3 years ago
4 years ago
/**
* 置顶 / 取消置顶
* @return [type] [description]
*/
public function top()
{
2 years ago
$customerModel = model('Customer');
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['create_role_id'] = $userInfo['id'];
$param['top_time'] = time();
3 years ago
4 years ago
$top_id = Db::name('crm_top')->where(['module' => ['eq', $param['module']], 'create_role_id' => ['eq', $userInfo['id']], 'module_id' => ['eq', $param['module_id']]])->column('top_id');
4 years ago
if ($top_id) {
4 years ago
if ($res = Db::name('crm_top')->where('top_id', $top_id[0])->update($param)) {
4 years ago
return resultArray(['data' => $res]);
} else {
return resultArray(['error' => Db::name('crm_top')->getError()]);
}
} else {
4 years ago
if ($res = Db::name('crm_top')->data($param)->insert()) {
4 years ago
return resultArray(['data' => $res]);
} else {
return resultArray(['error' => $customerModel->getError()]);
}
}
}
/**
* 客户成交状态
* @param status 1已成交,2未成交
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function deal_status()
{
$param = $this->param;
$userInfo = $this->userInfo;
4 years ago
$statusArr = ['1' => '已成交', '2' => '未成交'];
$statusList = ['1', '2'];
if (!$param['customer_id'] || !in_array($param['status'], $statusList)) {
4 years ago
return resultArray(['error' => '参数错误']);
}
$customerModel = model('Customer');
$customerConfigModel = model('CustomerConfig');
$userModel = new \app\admin\model\User();
$customer_ids = $param['customer_id'];
if (!is_array($customer_ids) || !$customer_ids) {
$customer_ids[] = $customer_ids;
}
$data = [];
$data['update_time'] = time();
$data['deal_time'] = time();
4 years ago
$data['deal_status'] = $statusArr[$param['status']];
4 years ago
$errorMessage = [];
foreach ($customer_ids as $customer_id) {
$dataInfo = [];
$dataInfo = db('crm_customer')->where(['customer_id' => $customer_id])->field('owner_user_id,deal_status,name')->find();
//权限判断
if (!$customerModel->checkData($customer_id, 1)) {
4 years ago
$errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerModel->getError();
4 years ago
continue;
}
$owner_user_id = $dataInfo['owner_user_id'];;
if (!$owner_user_id) {
4 years ago
$errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:公海数据无权操作';
continue;
4 years ago
}
//拥有客户数上限检测
if ($statusArr[$param['status']] == '未成交' && $dataInfo['deal_status'] == '已成交') {
4 years ago
if (!$customerConfigModel->checkData($owner_user_id, 1, 1)) {
$errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerConfigModel->getError();
4 years ago
continue;
4 years ago
}
4 years ago
}
if ($statusArr[$param['status']] == '已成交') {
4 years ago
$data['is_lock'] = 0;
4 years ago
}
$res = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
if (!$res) {
4 years ago
$errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:操作失败,请重试!';
4 years ago
continue;
}
//修改记录
updateActionLog($userInfo['id'], 'crm_customer', $customer_id, ['deal_status' => $dataInfo['deal_status']], ['deal_status' => $data['deal_status']]);
3 years ago
if ($param['status'] == 1) {
RecordActionLog($userInfo['id'], 'crm_customer', 'status', $dataInfo['name'], '', '', '修改客户:' . $dataInfo['name'] . '成交状态:' . $statusArr[$param['status']]);
} else {
RecordActionLog($userInfo['id'], 'crm_customer', 'status', $dataInfo['name'], '', '', '修改客户:' . $dataInfo['name'] . '成交状态:' . $statusArr[$param['status']]);
4 years ago
}
4 years ago
}
4 years ago
if (!$errorMessage) {
4 years ago
return resultArray(['data' => '操作成功']);
} else {
return resultArray(['error' => $errorMessage]);
4 years ago
}
4 years ago
}
3 years ago
4 years ago
/**
* 设置关注
*
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function star()
{
4 years ago
$userId = $this->userInfo['id'];
4 years ago
$targetId = $this->param['target_id'];
4 years ago
$type = $this->param['type'];
3 years ago
4 years ago
if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
3 years ago
4 years ago
if (!$this->setStar($type, $userId, $targetId)) {
return resultArray(['error' => '设置关注失败!']);
}
3 years ago
4 years ago
return resultArray(['data' => '设置关注成功!']);
}
3 years ago
4 years ago
/**
* 附近客户
*
* @return \think\response\Json
*/
public function nearby()
{
4 years ago
if (empty($this->param['lng'])) return resultArray(['error' => '缺少经度参数!']);
if (empty($this->param['lat'])) return resultArray(['error' => '缺少纬度参数!']);
4 years ago
if (empty($this->param['distance'])) return resultArray(['error' => '请选择距离!']);
3 years ago
4 years ago
$customerModel = model('Customer');
3 years ago
4 years ago
$data = $customerModel->getNearbyList($this->param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 系统信息
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function system()
{
if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
3 years ago
4 years ago
$customerModel = new \app\crm\model\Customer();
3 years ago
4 years ago
$data = $customerModel->getSystemInfo($this->param['id']);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* table标签栏数量
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function count()
{
if (empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
$userInfo = $this->userInfo;
3 years ago
4 years ago
$customerId = $this->param['customer_id'];
3 years ago
4 years ago
# 联系人
4 years ago
$contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
$contactsCount = Db::name('crm_contacts')->where('customer_id', $customerId)->where($contactsAuth)->count();
4 years ago
# 团队成员
$customer = Db::name('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customerId)->find();
4 years ago
$customer['ro_user_id'] = explode(',', trim($customer['ro_user_id'], ','));
$customer['rw_user_id'] = explode(',', trim($customer['rw_user_id'], ','));
4 years ago
$customer['owner_user_id'] = [$customer['owner_user_id']];
$teamCount = array_filter(array_unique(array_merge($customer['ro_user_id'], $customer['rw_user_id'], $customer['owner_user_id'])));
4 years ago
4 years ago
# 商机
4 years ago
$businessAuth = $this->getBusinessSearchWhere($userInfo['id']);
$businessCount = Db::name('crm_business')->where('customer_id', $customerId)->where($businessAuth)->count();
4 years ago
# 合同
4 years ago
$contractAuth = $this->getContractSearchWhere($userInfo['id']);
$contractCount = Db::name('crm_contract')->where('customer_id', $customerId)->where($contractAuth)->count();
4 years ago
# 回款
4 years ago
$receivablesAuth = $this->getReceivablesSearchWhere();
$receivablesCount = Db::name('crm_receivables')->where('customer_id', $customerId)->whereIn('owner_user_id', $receivablesAuth)->count();
4 years ago
# 回访
4 years ago
$visitAuth = $this->getVisitSearchWhere($userInfo['id']);
$visitCount = Db::name('crm_visit')->where(['customer_id' => $customerId, 'deleted_state' => 0])->where($visitAuth)->count();
4 years ago
# 发票
4 years ago
$invoiceAuth = $this->getInvoiceSearchWhere();
$invoiceCount = Db::name('crm_invoice')->where('customer_id', $customerId)->whereIn('owner_user_id', $invoiceAuth)->count();
4 years ago
# 附件
$fileCount = Db::name('crm_customer_file')->alias('customer')->join('__ADMIN_FILE__ file', 'file.file_id = customer.file_id')->where('customer_id', $customerId)->count();
3 years ago
4 years ago
$data = [
3 years ago
'businessCount' => $businessCount,
'contactCount' => $contactsCount,
'contractCount' => $contractCount,
'fileCount' => $fileCount,
'invoiceCount' => $invoiceCount,
'memberCount' => count($teamCount),
4 years ago
'receivablesCount' => $receivablesCount,
'returnVisitCount' => $visitCount
];
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 公海权限
*
* @param CustomerLogic $customerLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function poolAuthority(CustomerLogic $customerLogic)
{
$authority = [
4 years ago
'delete' => false, # 删除
'distribute' => false, # 分配
4 years ago
'excelexport' => false, # 导出
4 years ago
'index' => false, # 列表
'receive' => false, # 领取
4 years ago
];
3 years ago
4 years ago
$userId = $this->userInfo['id'];
3 years ago
4 years ago
if (empty($userId)) return resultArray(['data' => $authority]);
3 years ago
4 years ago
# 员工角色数据
$groupIds = $customerLogic->getEmployeeGroups($userId);
# 员工角色下的规则数据
$ruleIds = $customerLogic->getEmployeeRules($groupIds);
# 公海规则数据
$poolRules = $customerLogic->getPoolRules();
3 years ago
4 years ago
# 整理员工规则数据
$rules = [];
4 years ago
$ruleIds = implode(',', $ruleIds);
4 years ago
$rules = array_filter(array_unique(explode(',', $ruleIds)));
# 整理公海规则数据
$deleteId = $distributeId = $exportId = $indexId = $receiveId = 0;
4 years ago
foreach ($poolRules as $key => $value) {
if ($value['name'] == 'pool') $indexId = $value['id'];
if ($value['name'] == 'distribute') $distributeId = $value['id'];
if ($value['name'] == 'receive') $receiveId = $value['id'];
if ($value['name'] == 'poolExcelExport') $exportId = $value['id'];
if ($value['name'] == 'poolDelete') $deleteId = $value['id'];
4 years ago
}
3 years ago
4 years ago
# 权限判断
4 years ago
$authority['delete'] = $userId == 1 || in_array(1, $groupIds) || in_array($deleteId, $rules) ? true : false;
$authority['distribute'] = $userId == 1 || in_array(1, $groupIds) || in_array($distributeId, $rules) ? true : false;
$authority['excelexport'] = $userId == 1 || in_array(1, $groupIds) || in_array($exportId, $rules) ? true : false;
$authority['index'] = $userId == 1 || in_array(1, $groupIds) || in_array($indexId, $rules) ? true : false;
$authority['receive'] = $userId == 1 || in_array(1, $groupIds) || in_array($receiveId, $rules) ? true : false;
3 years ago
4 years ago
return resultArray(['data' => $authority]);
}
4 years ago
/**
* 客户级别列表
*
* @return \think\response\Json
3 years ago
* @since 2021-03-29
* @author fanqi
4 years ago
*/
public function level()
{
$data = db('admin_field')->where(['types' => 'crm_customer', 'field' => 'level'])->value('setting');
$data = explode(chr(10), $data);
return resultArray(['data' => $data]);
}
4 years ago
}