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.

903 lines
34 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\InvoiceLogic;
3 years ago
use app\crm\logic\MessageLogic;
use think\Cache;
4 years ago
use think\cache\driver\Redis;
use think\Db;
use think\Hook;
use think\Request;
class Message extends ApiCommon
{
3 years ago
/**
4 years ago
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
3 years ago
**/
4 years ago
public function _initialize()
{
$action = [
'permission' => [],
2 years ago
'allow' => [
4 years ago
'num',
'todayleads',
'todaycustomer',
'todaybusiness',
'followleads',
'followcustomer',
'checkcontract',
'checkreceivables',
'remindreceivablesplan',
'endcontract',
'remindcustomer',
'checkinvoice',
'visitcontract',
2 years ago
'alldeal',
'checkBusiness',
'newBusiness',
'checkCustomerCheck',
4 years ago
]
];
2 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
3 years ago
$a = strtolower($request->action());
4 years ago
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
3 years ago
}
2 years ago
4 years ago
/**
* 系统通知
*
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function index()
{
3 years ago
$messageModel = model('Message');
$param = $this->param;
4 years ago
$userInfo = $this->userInfo;
3 years ago
$param['user_id'] = $userInfo['id'];
$param['module_name'] = 'crm';
4 years ago
$data = $messageModel->getDataList($param);
3 years ago
return resultArray(['data' => $data]);
4 years ago
}
2 years ago
4 years ago
/**
* 消息数
*
* @return \think\response\Json
* @throws \think\exception\DbException
*/
public function num()
{
$userInfo = $this->userInfo;
$configDataModel = model('ConfigData');
$configData = $configDataModel->getData();
$data = [];
# 今日需联系线索
2 years ago
$todayLeadsTime = cache('todayLeadsTime' . $userInfo['id']);
$todayLeadsCount = cache('todayLeadsCount' . $userInfo['id']);
4 years ago
if (time() <= $todayLeadsTime) {
$data['todayLeads'] = (int)$todayLeadsCount;
} else {
$todayLeads = $this->todayLeads(true);
2 years ago
$data['todayLeads'] = $todayLeads['dataCount'] ?: 0;
cache('todayLeadsCount' . $userInfo['id'], $data['todayLeads']);
cache('todayLeadsTime' . $userInfo['id'], time());
4 years ago
}
# 今日需联系客户
2 years ago
$todayCustomerTime = cache('todayCustomerTime' . $userInfo['id']);
$todayCustomerCount = cache('todayCustomerCount' . $userInfo['id']);
4 years ago
if (time() <= $todayCustomerTime) {
$data['todayCustomer'] = (int)$todayCustomerCount;
} else {
$todayCustomer = $this->todayCustomer(true);
2 years ago
$data['todayCustomer'] = $todayCustomer['dataCount'] ?: 0;
cache('todayCustomerCount' . $userInfo['id'], $data['todayCustomer']);
cache('todayCustomerTime' . $userInfo['id'], time());
4 years ago
}
# 今日需联系商机
2 years ago
$todayBusinessTime = cache('todayBusinessTime' . $userInfo['id']);
$todayBusinessCount = cache('todayBusinessCount' . $userInfo['id']);
3 years ago
if (time() <= $todayBusinessTime) {
4 years ago
$data['todayBusiness'] = (int)$todayBusinessCount;
} else {
$todayBusiness = $this->todayBusiness(true);
2 years ago
$data['todayBusiness'] = $todayBusiness['dataCount'] ?: 0;
cache('todayBusinessCount' . $userInfo['id'], $data['todayBusiness']);
cache('todayBusinessTime' . $userInfo['id'], time());
4 years ago
}
# 分配给我的线索
2 years ago
$followLeadsTime = cache('followLeadsTime' . $userInfo['id']);
$followLeadsCount = cache('followLeadsCount' . $userInfo['id']);
4 years ago
if (time() <= $followLeadsTime) {
$data['followLeads'] = (int)$followLeadsCount;
} else {
$followLeads = $this->followLeads(true);
2 years ago
$data['followLeads'] = $followLeads['dataCount'] ?: 0;
cache('followLeadsCount' . $userInfo['id'], $data['followLeads']);
cache('followLeadsTime' . $userInfo['id'], time());
4 years ago
}
# 分配给我的客户
2 years ago
$followCustomerTime = cache('followCustomerTime' . $userInfo['id']);
$followCustomerCount = cache('followCustomerCount' . $userInfo['id']);
4 years ago
if (time() <= $followCustomerTime) {
$data['followCustomer'] = (int)$followCustomerCount;
} else {
$followCustomer = $this->followCustomer(true);
2 years ago
$data['followCustomer'] = $followCustomer['dataCount'] ?: 0;
cache('followCustomerCount' . $userInfo['id'], $data['followCustomer']);
cache('followCustomerTime' . $userInfo['id'], time());
4 years ago
}
# 待审核合同
2 years ago
$checkContractTime = cache('checkContractTime' . $userInfo['id']);
$checkContractCount = cache('checkContractCount' . $userInfo['id']);
4 years ago
if (time() <= $checkContractTime) {
$data['checkContract'] = (int)$checkContractCount;
} else {
$checkContract = $this->checkContract(true);
2 years ago
$data['checkContract'] = $checkContract['dataCount'] ?: 0;
cache('checkContractCount' . $userInfo['id'], $data['checkContract']);
cache('checkContractTime' . $userInfo['id'], time());
4 years ago
}
# 待审核回款
2 years ago
$checkReceivablesTime = cache('checkReceivablesTime' . $userInfo['id']);
$checkReceivablesCount = cache('checkReceivablesCount' . $userInfo['id']);
4 years ago
if (time() <= $checkReceivablesTime) {
$data['checkReceivables'] = (int)$checkReceivablesCount;
} else {
$checkReceivables = $this->checkReceivables(true);
2 years ago
$data['checkReceivables'] = $checkReceivables['dataCount'] ?: 0;
cache('checkReceivablesCount' . $userInfo['id'], $data['checkReceivables']);
cache('checkReceivablesTime' . $userInfo['id'], time());
4 years ago
}
# 待审核发票
2 years ago
$checkInvoiceTime = cache('checkInvoiceTime' . $userInfo['id']);
$checkInvoiceCount = cache('checkInvoiceCount' . $userInfo['id']);
4 years ago
if (time() <= $checkInvoiceTime) {
$data['checkInvoice'] = (int)$checkInvoiceCount;
3 years ago
4 years ago
} else {
$checkInvoice = $this->checkInvoice(true);
2 years ago
$data['checkInvoice'] = $checkInvoice['dataCount'] ?: 0;
cache('checkInvoiceCount' . $userInfo['id'], $data['checkInvoice']);
cache('checkInvoiceTime' . $userInfo['id'], time());
}
# 待审核商机
$checkBusinessTime = cache('checkBusinessTime' . $userInfo['id']);
$checkBusinessCount = cache('checkBusinessCount' . $userInfo['id']);
if (time() <= $checkBusinessTime) {
$data['checkBusiness'] = (int)$checkBusinessCount;
3 years ago
2 years ago
} else {
$checkBusiness = $this->checkBusiness(true);
$data['checkBusiness'] = $checkBusiness['dataCount'] ?: 0;
cache('checkBusinessCount' . $userInfo['id'], $data['checkBusiness']);
cache('checkBusinessTime' . $userInfo['id'], time());
4 years ago
}
# 待审核客户捞取
$checkCustomerCheckTime = cache('$checkCustomerCheckTime' . $userInfo['id']);
$checkCustomerCheckCount = cache('checkCustomerCheckCount' . $userInfo['id']);
if (time() <= $checkCustomerCheckTime) {
$data['checkCustomerCheck'] = (int)$checkCustomerCheckCount;
} else {
$checkCustomerCheck = $this->checkCustomerCheck(true);
$data['checkCustomerCheck'] = $checkCustomerCheck['dataCount'] ?: 0;
cache('checkCustomerCheckCount' . $userInfo['id'], $data['checkCustomerCheck']);
cache('$checkCustomerCheckTime' . $userInfo['id'], time());
}
4 years ago
# 待回款提醒
2 years ago
$remindReceivablesPlanTime = cache('remindReceivablesPlanTime' . $userInfo['id']);
$remindReceivablesPlanCount = cache('remindReceivablesPlanCount' . $userInfo['id']);
4 years ago
if (time() <= $remindReceivablesPlanTime) {
$data['remindReceivablesPlan'] = (int)$remindReceivablesPlanCount;
} else {
$remindReceivablesPlan = $this->remindReceivablesPlan(true);
2 years ago
$data['remindReceivablesPlan'] = $remindReceivablesPlan['dataCount'] ?: 0;
cache('remindReceivablesPlanCount' . $userInfo['id'], $data['remindReceivablesPlan']);
cache('remindReceivablesPlanTime' . $userInfo['id'], time());
4 years ago
}
4 years ago
if ($configData['visit_config'] == 1) {
# 待回访合同
2 years ago
$visitContractTime = cache('visitContractTime' . $userInfo['id']);
$visitContractCount = cache('visitContractCount' . $userInfo['id']);
4 years ago
if (time() <= $visitContractTime) {
$data['returnVisitRemind'] = (int)$visitContractCount;
} else {
$visitContract = $this->visitContract(true);
2 years ago
$data['returnVisitRemind'] = $visitContract['dataCount'] ?: 0;
cache('visitContractCount' . $userInfo['id'], $data['returnVisitRemind']);
cache('visitContractTime' . $userInfo['id'], time());
4 years ago
}
4 years ago
}
# 即将到期合同
if ($configData['contract_config'] == 1) {
2 years ago
$endContractTime = cache('endContractTime' . $userInfo['id']);
$endContractCount = cache('endContractCount' . $userInfo['id']);
4 years ago
if (time() <= $endContractTime) {
$data['endContract'] = (int)$endContractCount;
} else {
$endContract = $this->endContract(true);
2 years ago
$data['endContract'] = $endContract['dataCount'] ?: 0;
cache('endContractCount' . $userInfo['id'], $data['endContract']);
cache('endContractTime' . $userInfo['id'], time());
4 years ago
}
}
2 years ago
# 新增商机
$newBusinessTime = cache('newBusinessTime' . $userInfo['id']);
$newBusinessCount = cache('newBusinessCount' . $userInfo['id']);
if (time() <= $newBusinessTime) {
$data['newBusiness'] = (int)$newBusinessCount;
} else {
$newBusiness = $this->newBusiness(true);
$data['newBusiness'] = $newBusiness['dataCount'] ?: 0;
cache('newBusinessCount' . $userInfo['id'], $data['newBusiness']);
cache('newBusinessTime' . $userInfo['id'], time());
}
4 years ago
# 待进入公海提醒
4 years ago
$pool = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
if (!empty($pool)) {
2 years ago
$remindCustomerTime = cache('remindCustomerTime' . $userInfo['id']);
$remindCustomerCount = cache('remindCustomerCount' . $userInfo['id']);
4 years ago
if (time() <= $remindCustomerTime) {
$data['putInPoolRemind'] = (int)$remindCustomerCount;
} else {
$remindCustomer = $this->remindCustomer(true);
4 years ago
$data['putInPoolRemind'] = !empty($remindCustomer['dataCount']) ? $remindCustomer['dataCount'] : 0;
2 years ago
cache('remindCustomerCount' . $userInfo['id'], $data['putInPoolRemind']);
cache('remindCustomerTime' . $userInfo['id'], time());
4 years ago
}
}
2 years ago
4 years ago
return resultArray(['data' => $data]);
}
2 years ago
4 years ago
/**
* 今日需联系线索
*
* @param false $getCount
* @return array|\think\response\Json
*/
public function todayLeads($getCount = false)
{
2 years ago
$param = $this->param;
3 years ago
$userId = $this->userInfo['id'];
2 years ago
$types = $param['types'];
4 years ago
unset($param['types']);
$param['user_id'] = $userId;
3 years ago
if ($getCount == true) $param['getCount'] = 1;
2 years ago
$messageLogic = new MessageLogic();
4 years ago
3 years ago
$data = $messageLogic->todayLeads($param);
2 years ago
4 years ago
if ($types == 'list') return resultArray(['data' => $data]);
2 years ago
4 years ago
return $data;
}
2 years ago
4 years ago
/**
* 今日需联系客户
*
* @param string $getCount
* @return \think\response\Json
*/
public function todayCustomer($getCount = false)
{
$param = $this->param;
3 years ago
$userId = $this->userInfo['id'];
4 years ago
$types = $param['types'];
if ($getCount == true) {
$param['getCount'] = 1;
}
unset($param['types']);
3 years ago
$param['user_id'] = $userId;
2 years ago
$messageLogic = new MessageLogic();
3 years ago
$data = $messageLogic->remindCustomer($param);
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 今日需联系商机
*
* @param false $getCount
* @return array|\think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function todayBusiness($getCount = false)
{
2 years ago
$param = $this->param;
$userId = $this->userInfo['id'];
$types = $param['types'];
4 years ago
unset($param['types']);
if ($getCount == true) $param['getCount'] = 1;
2 years ago
$messageLogic = new MessageLogic();
4 years ago
$param['user_id'] = $userId;
3 years ago
$data = $messageLogic->todayBusiness($param);
2 years ago
4 years ago
if ($types == 'list') return resultArray(['data' => $data]);
2 years ago
4 years ago
return $data;
}
2 years ago
4 years ago
/**
4 years ago
* 分配给我的线索
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function followLeads($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
unset($param['types']);
if ($getCount == true) $param['getCount'] = 1;
$param['user_id'] = $userInfo['id'];
2 years ago
$messageLogic = new MessageLogic();
3 years ago
$data = $messageLogic->followLeads($param);
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
3 years ago
return $data;
}
2 years ago
4 years ago
/**
4 years ago
* 分配给我的客户
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function followCustomer($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
if ($getCount == true) {
$param['getCount'] = 1;
}
3 years ago
unset($param['types']);
2 years ago
$messageLogic = new MessageLogic();
3 years ago
$param['user_id'] = $userInfo['id'];
$data = $messageLogic->followCustomer($param);
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 待审核合同
*
* @param false $getCount
* @return \think\response\Json
*/
public function checkContract($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
unset($param['types']);
if ($getCount == true) {
$param['getCount'] = 1;
}
2 years ago
$messageLogic = new MessageLogic();
4 years ago
$param['user_id'] = $userInfo['id'];
3 years ago
$data = $messageLogic->checkContract($param);
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 待审核回款
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function checkReceivables($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
unset($param['types']);
if ($getCount == true) $param['getCount'] = 1;
2 years ago
$messageLogic = new MessageLogic();
4 years ago
$param['user_id'] = $userInfo['id'];
3 years ago
$data = $messageLogic->checkReceivables($param);
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 待审核发票
*
* @param InvoiceLogic $invoiceLogic
* @return array|\think\response\Json
* @throws \think\exception\DbException
*/
public function checkInvoice($getCount = false)
{
2 years ago
$param = $this->param;
4 years ago
$userId = $this->userInfo['id'];
2 years ago
$types = $param['types'];
4 years ago
if ($getCount == true) $param['getCount'] = 1;
# 清除与模型无关的数据
unset($param['types']);
3 years ago
$param['user_id'] = $userId;
2 years ago
$messageLogic = new MessageLogic();
3 years ago
$data = $messageLogic->checkInvoice($param);
2 years ago
if ($types == 'list') return resultArray(['data' => $data]);
return $data;
}
/**
* 待审核商机
*
* @return array|\think\response\Json
* @throws \think\exception\DbException
*/
public function checkBusiness($getCount = false)
{
$param = $this->param;
$userId = $this->userInfo['id'];
$types = $param['types'];
if ($getCount == true) $param['getCount'] = 1;
# 清除与模型无关的数据
unset($param['types']);
$param['user_id'] = $userId;
$messageLogic = new MessageLogic();
$data = $messageLogic->checkBusiness($param);
4 years ago
if ($types == 'list') return resultArray(['data' => $data]);
2 years ago
4 years ago
return $data;
}
2 years ago
/**
* 待审核客户捞取
*
* @return array|\think\response\Json
* @throws \think\exception\DbException
*/
public function checkCustomerCheck($getCount = false)
{
$param = $this->param;
$userId = $this->userInfo['id'];
$types = $param['types'];
if ($getCount == true) $param['getCount'] = 1;
# 清除与模型无关的数据
unset($param['types']);
$param['user_id'] = $userId;
$messageLogic = new MessageLogic();
$data = $messageLogic->checkCustomerCheck($param);
if ($types == 'list') return resultArray(['data' => $data]);
return $data;
}
4 years ago
/**
* 待回款提醒
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function remindReceivablesPlan($getCount = false)
{
2 years ago
$param = $this->param;
4 years ago
$userInfo = $this->userInfo;
2 years ago
$types = $param['types'];
$type = $param['type'] ?: 1;
$isSub = $param['isSub'] ?: '';
4 years ago
unset($param['types']);
3 years ago
unset($param['type']);
unset($param['isSub']);
4 years ago
$receivablesPlanModel = model('ReceivablesPlan');
2 years ago
4 years ago
if ($getCount == true) $param['getCount'] = 1;
2 years ago
4 years ago
$param['owner_user_id'] = $userInfo['id'];
if ($isSub) {
3 years ago
$param['owner_user_id'] = ['in', getSubUserId(false)];
}
4 years ago
switch ($type) {
4 years ago
case '1' :
$param['receivables_id'] = 0;
3 years ago
$param['check_status'] = ['lt', 2];
$param['remind_date'] = ['elt', date('Y-m-d', time())];
$param['return_date'] = ['egt', date('Y-m-d', time())];
4 years ago
$param['types'] = 1;
$param['is_dealt'] = 0;
break;
case '2' :
$param['receivables_id'] = ['gt', 0];
$param['check_status'] = 2;
$param['dealt'] = 1;
break;
case '3' :
$param['receivables_id'] = 0;
3 years ago
$param['return_date'] = ['lt', date('Y-m-d', time())];
4 years ago
break;
4 years ago
}
$data = $receivablesPlanModel->getDataList($param);
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 即将到期合同
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function endContract($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
2 years ago
$type = $param['type'] ?: 1;
$isSub = $param['isSub'] ?: '';
4 years ago
if ($getCount == true) $param['getCount'] = 1;
unset($param['types']);
3 years ago
unset($param['type']);
unset($param['isSub']);
4 years ago
$contractModel = model('Contract');
$configModel = new \app\crm\model\ConfigData();
$configInfo = $configModel->getData();
2 years ago
$expireDay = $configInfo['contract_day'] ?: '7';
3 years ago
// 合同到期不提醒
if (empty($configInfo['contract_config'])) return resultArray(['data' => []]);
4 years ago
$param['owner_user_id'] = $userInfo['id'];
if ($isSub) {
2 years ago
$param['owner_user_id'] = array('in', getSubUserId(false));
3 years ago
}
4 years ago
switch ($type) {
case '1' :
2 years ago
$param['end_time'] = array('between', array(date('Y-m-d', time()), date('Y-m-d', time() + 86400 * $expireDay)));
3 years ago
$param['expire_remind'] = 0;
4 years ago
break;
2 years ago
case '2' :
$param['end_time'] = array('lt', date('Y-m-d', time()));
break;
4 years ago
}
$data = $contractModel->getDataList($param);
3 years ago
// p($contractModel->getLastSql());
4 years ago
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
3 years ago
}
2 years ago
4 years ago
/**
2 years ago
* 新增商机
* @return
4 years ago
* @author Michael_xu
2 years ago
*/
public function newBusiness($getCount = false)
{
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
$type = $param['type'] ?: 1;
$isSub = $param['isSub'] ?: '';
if ($getCount == true) $param['getCount'] = 1;
unset($param['types']);
unset($param['type']);
unset($param['isSub']);
$businessModel = model('Business');
$param['owner_user_id'] = $userInfo['id'];
if ($isSub) {
$param['owner_user_id'] = array('in', getSubUserId(false));
}
$data = $businessModel->getDataList($param);
// p($contractModel->getLastSql());
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
/**
* 待进入客户池
3 years ago
* @return
2 years ago
* @author Michael_xu
4 years ago
*/
public function remindCustomer($getCount = false)
{
$customerModel = model('Customer');
2 years ago
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$types = $param['types'];
2 years ago
$isSub = $param['isSub'] ?: '';
4 years ago
if ($getCount == true) $param['getCount'] = 1;
unset($param['types']);
unset($param['type']);
unset($param['isSub']);
unset($param['deal_status']);
unset($param['owner_user_id']);
2 years ago
4 years ago
# 负责人
$param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
2 years ago
4 years ago
# 是否提醒
4 years ago
$data = [];
$remind = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
if (!empty($remind)) {
2 years ago
$whereData = $param ?: [];
4 years ago
$whereData['is_remind'] = 1;
$whereData['user_id'] = $userInfo['id'];
$whereData['pool_remain'] = 0;
2 years ago
$whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer', 'bydata' => 'me'])->value('scene_id');
4 years ago
if ($isSub) {
2 years ago
$whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer', 'bydata' => 'sub'])->value('scene_id');
4 years ago
}
$data = $customerModel->getDataList($whereData);
4 years ago
}
if ($types == 'list') {
return resultArray(['data' => $data]);
}
return $data;
}
2 years ago
4 years ago
/**
* 待回访合同
*
* @param false $getCount
* @return array|\think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function visitContract($getCount = false)
{
2 years ago
$param = $this->param;
$userId = $this->userInfo['id'];
$isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
$types = !empty($param['types']) ? $param['types'] : '';
4 years ago
if ($getCount == true) $param['getCount'] = 1;
unset($param['isSub']);
unset($param['types']);
2 years ago
$param['is_visit'] = 0; # 未回访
4 years ago
$param['check_status'] = 2; # 审核通过
2 years ago
4 years ago
$contractModel = new \app\crm\model\Contract();
2 years ago
4 years ago
# 负责人
$param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
2 years ago
4 years ago
$param['user_id'] = $userId;
$data = $contractModel->getDataList($param);
2 years ago
4 years ago
if ($types == 'list') return resultArray(['data' => $data]);
2 years ago
4 years ago
return $data;
}
2 years ago
4 years ago
/**
* 全部标记已处理
*
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function allDeal()
{
2 years ago
$type = $this->param['type'];
4 years ago
$typeId = !empty($this->param['type_id']) ? $this->param['type_id'] : '';
2 years ago
$isSub = !empty($this->param['isSub']) ? $this->param['isSub'] : 0;
4 years ago
$userId = $this->userInfo['id'];
2 years ago
4 years ago
if (empty($type)) return resultArray(['error' => '缺少模块类型参数']);
2 years ago
4 years ago
# 获得今日开始和结束时间戳
$todayTime = getTimeByType('today');
2 years ago
4 years ago
# 处理今日需联系线索、客户、商机
if (in_array($type, ['todayLeads', 'todayCustomer', 'todayBusiness'])) {
# 负责人
$where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
# 下次联系时间
$where['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
4 years ago
# 是否已处理(联系)
$where['is_dealt'] = 0;
2 years ago
4 years ago
# 线索
if ($type == 'todayLeads') {
$leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update([
'last_time' => time(),
2 years ago
'is_dealt' => 1,
'follow' => '已跟进'
4 years ago
]);
}
# 客户
if ($type == 'todayCustomer') {
$customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
Db::name('crm_customer')->whereIn('customer_id', $customerId)->update([
'last_time' => time(),
2 years ago
'is_dealt' => 1,
'follow' => '已跟进'
4 years ago
]);
}
# 商机
if ($type == 'todayBusiness') {
$businessId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
Db::name('crm_business')->whereIn('business_id', $businessId)->update([
4 years ago
'last_time' => time(),
2 years ago
'is_dealt' => 1
4 years ago
]);
}
}
2 years ago
4 years ago
# 处理分配给我的线索、客户
if (in_array($type, ['followLeads', 'followCustomer'])) {
$where['owner_user_id'] = $userId;
2 years ago
$where['follow'] = [['neq', '已跟进'], null, 'or'];
4 years ago
$where['is_allocation'] = 1;
2 years ago
4 years ago
# 线索
if ($type == 'followLeads') {
$leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update(['follow' => '已跟进']);
}
# 客户
if ($type == 'followCustomer') {
$customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
Db::name('crm_customer')->whereIn('customer_id', $customerId)->update(['follow' => '已跟进']);
}
}
2 years ago
4 years ago
# 处理待审核合同、回款、发票
2 years ago
if (in_array($type, ['checkContract', 'checkReceivables', 'checkInvoice', 'checkBusiness'])) {
2 years ago
$where['check_status'] = ['lt', '2'];
$where['check_user_id'] = ['like', ',%' . $userId . '%,'];
4 years ago
# 合同
if ($type == 'checkContract') {
$contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
4 years ago
db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_contract')->whereIn('types_id', $contractId)->delete();
4 years ago
}
# 回款
if ($type == 'checkReceivables') {
$receivablesId = !empty($typeId) ? $typeId : Db::name('crm_receivables')->where($where)->column('receivables_id');
4 years ago
db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_receivables')->whereIn('types_id', $receivablesId)->delete();
4 years ago
}
# 发票
if ($type == 'checkInvoice') {
$invoiceId = !empty($typeId) ? $typeId : Db::name('crm_invoice')->where($where)->column('invoice_id');
4 years ago
db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_invoice')->whereIn('types_id', $invoiceId)->delete();
4 years ago
}
# 商机
if ($type == 'checkBusiness') {
$invoiceId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_business')->whereIn('types_id', $invoiceId)->delete();
}
2 years ago
4 years ago
}
2 years ago
4 years ago
# 处理到期合同
if ($type == 'endContract') {
$configModel = new \app\crm\model\ConfigData();
2 years ago
$configInfo = $configModel->getData();
$expireDay = $configInfo['contract_day'] ?: '7';
4 years ago
$where['owner_user_id'] = $userId;
2 years ago
$where['end_time'] = ['between', [date('Y-m-d', time()), date('Y-m-d', time() + 86400 * $expireDay)]];
4 years ago
$where['expire_remind'] = 1;
2 years ago
4 years ago
$contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['expire_remind' => 0]);
}
2 years ago
4 years ago
# 处理待回访合同
if ($type == 'returnVisitRemind') {
$where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId; # 负责人
2 years ago
$where['is_visit'] = 0; # 未回访
$where['check_status'] = 2; # 审核通过
4 years ago
$contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['is_visit' => 1]);
}
2 years ago
4 years ago
# 处理待进入公海
if ($type == 'putInPoolRemind') {
if (!empty($typeId)) {
4 years ago
Db::name('crm_customer')->whereIn('customer_id', $typeId)->update(['pool_remain' => 1]);
4 years ago
} else {
4 years ago
$poolConfig = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
if (!empty($poolConfig)) {
$whereData['page'] = 1;
$whereData['limit'] = 100;
$whereData['is_remind'] = 1;
$whereData['user_id'] = $userId;
$whereData['pool_remain'] = 0;
2 years ago
$whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer', 'bydata' => empty($isSub) ? 'me' : 'sub'])->value('scene_id');
4 years ago
$whereData['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
$poolCustomers = (new \app\crm\model\Customer())->getDataList($whereData);
$ids = [];
2 years ago
foreach ($poolCustomers['list'] as $key => $value) {
4 years ago
if (!empty($value['customer_id'])) $ids[] = $value['customer_id'];
}
if (!empty($ids)) Db::name('crm_customer')->whereIn('customer_id', $ids)->update(['pool_remain' => 1]);
4 years ago
}
}
}
2 years ago
4 years ago
# 带回款提醒
if ($type == 'remindReceivablesPlan') {
$planId = [];
if (!empty($typeId)) {
$planId = $typeId;
} else {
2 years ago
$param['owner_user_id'] = $isSub ? ['in', getSubUserId(false)] : $userId;
4 years ago
$param['receivables_id'] = 0;
2 years ago
$param['check_status'] = ['lt', 2];
$param['remind_date'] = ['elt', date('Y-m-d', time())];
$param['return_date'] = ['egt', date('Y-m-d', time())];
$param['types'] = 1;
$param['page'] = 1;
$param['limit'] = 1000;
$receivablesPlanModel = model('ReceivablesPlan');
4 years ago
$data = $receivablesPlanModel->getDataList($param);
2 years ago
foreach ($data['list'] as $key => $value) {
4 years ago
$planId[] = $value['plan_id'];
}
}
if (!empty($planId)) db('crm_receivables_plan')->whereIn('plan_id', $planId)->update(['is_dealt' => 1]);
}
2 years ago
cache::rm('todayLeadsCount' . $userId);
cache::rm('todayCustomerCount' . $userId);
cache::rm('todayBusinessCount' . $userId);
cache::rm('followLeadsCount' . $userId);
cache::rm('followCustomerCount' . $userId);
cache::rm('checkContractCount' . $userId);
cache::rm('checkReceivablesCount' . $userId);
cache::rm('checkInvoiceCount' . $userId);
cache::rm('remindReceivablesPlanCount' . $userId);
cache::rm('visitContractCount' . $userId);
cache::rm('endContractCount' . $userId);
cache::rm('remindCustomerCount' . $userId);
cache::rm('todayLeadsTime' . $userId);
cache::rm('todayCustomerTime' . $userId);
cache::rm('todayBusinessTime' . $userId);
cache::rm('followLeadsTime' . $userId);
cache::rm('followCustomerTime' . $userId);
cache::rm('checkContractTime' . $userId);
cache::rm('checkReceivablesTime' . $userId);
cache::rm('checkInvoiceTime' . $userId);
cache::rm('checkBusinessTime' . $userId);
cache::rm('remindReceivablesPlanTime' . $userId);
cache::rm('visitContractTime' . $userId);
cache::rm('endContractTime' . $userId);
cache::rm('remindCustomerTime' . $userId);
4 years ago
return resultArray(['data' => '操作成功!']);
}
}