公海客户捞取审批

pull/10/head
张萌 2 years ago
parent c33c0f45c3
commit 9a3badcb4f

@ -22,7 +22,7 @@ class ExamineFlow extends Common
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $autoWriteTimestamp = true;
protected $typesArr = ['crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine', 'jxc_purchase', 'jxc_retreat', 'jxc_sale', 'jxc_salereturn', 'jxc_payment', 'jxc_collection', 'jxc_allocation', 'jxc_inventory'];
protected $typesArr = ['crm_customer_check', 'crm_business','crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine', 'jxc_purchase', 'jxc_retreat', 'jxc_sale', 'jxc_salereturn', 'jxc_payment', 'jxc_collection', 'jxc_allocation', 'jxc_inventory'];
/**
* [getDataList 审批流程list]

@ -9,6 +9,7 @@ namespace app\admin\model;
use app\admin\model\ImportRecord as ImportRecordModel;
use app\admin\model\User as UserModel;
use app\crm\model\Contract as ContractModel;
use app\crm\model\CustomerCheck;
use app\crm\model\Invoice;
use app\crm\model\Receivables as ReceivablesModel;
use app\oa\model\Announcement as AnnouncementModel;
@ -265,7 +266,11 @@ class Message extends Common
* 盘点审批通过
*/
const INVENTORY_PASS = 56;
/**
* 客户待审批
*/
const CUSTOMER_TO_DO = 57;
/**
* 消息类型
*
@ -423,6 +428,7 @@ class Message extends Common
self::BUSINESS_PASS,
self::CUSTOMER_PASS,
self::CONTRACT_END,
self::CUSTOMER_TO_DO,
],
'jxc' => [
self::PURCHASE_TO_DO,
@ -482,7 +488,7 @@ class Message extends Common
foreach ($data as $key => $val) {
$content = str_replace('{' . $key . '}', $val, $content);
}
$advance_time= $data['advance_time'];
$advance_time = $data['advance_time'];
$content = str_replace('{from_user}', User::userInfo('realname'), $content);
$content = str_replace('{date}', date('Y-m-d'), $content);
$data = [];
@ -499,7 +505,7 @@ class Message extends Common
$data['action_name'] = strtolower($request->action());
$from_user_id = $data['from_user_id'];
if (!in_array($type,[9,17,18,19,20,27])) {
if (!in_array($type, [9, 17, 18, 19, 20, 27])) {
$user_id_list = array_filter($user_id_list, function ($val) use ($from_user_id) {
return $val !== $from_user_id;
});
@ -559,6 +565,9 @@ class Message extends Common
case self::CONTRACT_PASS:
case self::TEAM_CONTRACT:
return ContractModel::where(['contract_id' => $data['action_id']])->value('name') ?: '';
// 客户
case self::CUSTOMER_TO_DO:
return CustomerCheck::where(['customer_check_id' => $data['action_id']])->value('name') ?: '';
// 回款
case self::RECEIVABLES_TO_DO:

@ -172,7 +172,7 @@ class CustomerPool extends ApiCommon
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
$result = (new CustomerPoolLogic())->receiveCustomers($param);
$result = (new CustomerPoolLogic())->customerReceiveExamine($param);
if (!empty($result)) return resultArray(['error' => $result]);
@ -398,4 +398,25 @@ class CustomerPool extends ApiCommon
$pool_list=array_merge($pool_list,$list);
return $pool_list;
}
/**
* 领取公海池客户
*
* @return Json
* @since 2021-04-15
* @author fanqi
*/
public function checkCustomerReveive()
{
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要领取的公海客户!']);
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
$result = (new CustomerPoolLogic())->customerReceiveExamine($param);
if (!empty($result)) return resultArray(['error' => $result]);
return resultArray(['data' => '领取成功!']);
}
}

@ -12,7 +12,9 @@ use app\admin\model\ActionRecord;
use app\admin\model\Common;
use app\admin\model\Field;
use app\admin\model\File;
use app\admin\model\Message;
use app\admin\model\Record;
use app\admin\model\User;
use app\crm\model\CustomerConfig;
use PDOStatement;
use think\Collection;
@ -25,9 +27,9 @@ class CustomerPoolLogic extends Common
* 公海列表
*
* @param $param
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getPoolList($param)
{
@ -46,10 +48,10 @@ class CustomerPoolLogic extends Common
$searchMap = [];
if ($param['search'] == '0' || !empty($param['search'])) {
$search = $param['search'];
$searchMap = function($query) use ($search) {
$query->where('customer.name',array('like','%'.$search.'%'))
->whereOr('customer.mobile',array('like','%'.$search.'%'))
->whereOr('customer.telephone',array('like','%'.$search.'%'));
$searchMap = function ($query) use ($search) {
$query->where('customer.name', array('like', '%' . $search . '%'))
->whereOr('customer.mobile', array('like', '%' . $search . '%'))
->whereOr('customer.telephone', array('like', '%' . $search . '%'));
};
}
@ -60,11 +62,11 @@ class CustomerPoolLogic extends Common
}
// 公海条件
if ($param['is_excel'] == 1 && !empty($param['customer_id'])) {
$authMap['customer.customer_id'] = ['in', trim(arrayToString($param['customer_id']),',')];
$authMap['customer.customer_id'] = ['in', trim(arrayToString($param['customer_id']), ',')];
}
// 排序
if (!empty($orderField) && !empty($orderType)) {
$order = $fieldModel->getOrderByFormtype('crm_customer','customer', $orderField, $orderType);
$order = $fieldModel->getOrderByFormtype('crm_customer', 'customer', $orderField, $orderType);
} else {
$order = 'customer.update_time desc';
}
@ -76,7 +78,7 @@ class CustomerPoolLogic extends Common
unset($param['is_excel']);
unset($param['customer_id']);
// 格式化参数
$request = $this->fmtRequest( $param );
$request = $this->fmtRequest($param);
$requestMap = !empty($request['map']) ? $request['map'] : [];
// 高级搜索
@ -89,7 +91,7 @@ class CustomerPoolLogic extends Common
->join('__CRM_CUSTOMER__ customer', 'customer.customer_id = relation.customer_id', 'LEFT')
->where($where)->where($searchMap)->where($map)->where($authMap)->count();
if (empty($customerPoolCount)) return $result;
$customerPoolList = db('crm_customer_pool_relation')->alias('relation')->field($customerFieldString)
->join('__CRM_CUSTOMER__ customer', 'customer.customer_id = relation.customer_id', 'LEFT')
->limit($request['offset'], $request['length'])->where($where)->where($searchMap)->where($map)->where($authMap)->orderRaw($order)->select();
@ -114,12 +116,12 @@ class CustomerPoolLogic extends Common
$extraData = [];
$customerIds = array_column($customerPoolList, 'customer_id');
$extraList = db('crm_customer_data')->whereIn('customer_id', $customerIds)->select();
foreach ($extraList AS $key => $value) {
foreach ($extraList as $key => $value) {
$extraData[$value['customer_id']][$value['field']] = $value['content'];
}
// 整理公海数据
foreach ($customerPoolList AS $key => $value) {
foreach ($customerPoolList as $key => $value) {
$customerPoolList[$key]['create_user_name'] = !empty($userData[$value['create_user_id']]) ? $userData[$value['create_user_id']] : '';
$customerPoolList[$key]['before_owner_user_name'] = !empty($userData[$value['before_owner_user_id']]) ? $userData[$value['before_owner_user_id']] : '';
@ -130,38 +132,38 @@ class CustomerPoolLogic extends Common
$customerPoolList[$key]['into_pool_time'] = !empty($value['into_pool_time']) ? date('Y-m-d H:i:s', $value['into_pool_time']) : null;
// 处理日期时间类型的自定义字段
foreach ($datetimeField AS $k => $v) {
foreach ($datetimeField as $k => $v) {
if (!empty($value[$v])) $customerPoolList[$key][$v] = date('Y-m-d H:i:s', $value[$v]);
}
// 处理人员类型的自定义字段
foreach ($userField AS $k => $v) {
foreach ($userField as $k => $v) {
if (!empty($value[$v])) {
$customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $userData);
}
}
// 处理部门类型的自定义字段
foreach ($structureField AS $k => $v) {
foreach ($structureField as $k => $v) {
if (!empty($value[$v])) $customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $structureData);
}
// 布尔值类型字段
foreach ($booleanField AS $k => $v) {
foreach ($booleanField as $k => $v) {
$customerPoolList[$key][$v] = !empty($value[$v]) ? (string)$value[$v] : '0';
}
// 处理日期区间类型字段的格式
foreach ($dateIntervalField AS $k => $v) {
foreach ($dateIntervalField as $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
// 处理地址类型字段的格式
foreach ($positionField AS $k => $v) {
foreach ($positionField as $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
// 手写签名类型字段
foreach ($handwritingField AS $k => $v) {
foreach ($handwritingField as $k => $v) {
$handwritingData = !empty($value[$v]) ? db('admin_file')->where('file_id', $value[$v])->value('file_path') : null;
$customerPoolList[$key][$v] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
}
// 定位类型字段
foreach ($locationField AS $k => $v) {
foreach ($locationField as $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
}
@ -176,9 +178,9 @@ class CustomerPoolLogic extends Common
* 公海详情
*
* @param array $param pool_id 公海IDcustomer_id 客户ID
* @author fanqi
* @since 2021-04-14
* @return array|bool|PDOStatement|string|Model|null
* @since 2021-04-14
* @author fanqi
*/
public function getPoolData($param)
{
@ -214,19 +216,19 @@ class CustomerPoolLogic extends Common
$data['into_pool_time'] = !empty($data['into_pool_time']) ? date('Y-m-d H:i:s', $data['into_pool_time']) : null;
# 处理日期时间类型的自定义字段
foreach ($datetimeField AS $k => $v) {
foreach ($datetimeField as $k => $v) {
if (isset($data[$v])) {
$data[$v] = !empty($data[$v]) ? date('Y-m-d H:i:s', $data[$v]) : null;
}
}
# 处理人员类型的自定义字段
foreach ($userField AS $k => $v) {
foreach ($userField as $k => $v) {
if (isset($data[$v]) && !empty($data[$v])) {
$data[$v] = $this->fieldTransformToText($data[$v], $userData);
}
}
# 处理部门类型的自定义字段
foreach ($structureField AS $k => $v) {
foreach ($structureField as $k => $v) {
if (isset($data[$v]) && !empty($data[$v])) {
$data[$v] = $this->fieldTransformToText($data[$v], $structureData);
}
@ -252,7 +254,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
# 验证是否是公海数据
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
if (empty($customerData[$value])) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:公海客户不存在!';
@ -275,7 +277,7 @@ class CustomerPoolLogic extends Common
$customerContractData = db('crm_contract')->whereIn('customer_id', $customerId)->column('customer_id');
# 验证客户下是否存在联系人、商机、合同
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
if (in_array($value, $customerContactsData)) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:客户下存在联系人!';
@ -307,13 +309,13 @@ class CustomerPoolLogic extends Common
# 记录到数据操作日志
$ip = request()->ip();
$addOperationLogData = [];
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
$addOperationLogData[] = [
'user_id' => $param['user_id'],
'client_ip' => $ip,
'module' => 'crm_customer',
'action_id' => $value,
'content' => '删除了客户:' . $customerData[$value]['name'],
'user_id' => $param['user_id'],
'client_ip' => $ip,
'module' => 'crm_customer',
'action_id' => $value,
'content' => '删除了客户:' . $customerData[$value]['name'],
'create_time' => time(),
'action_name' => 'delete',
'target_name' => $customerData[$value]['name']
@ -332,9 +334,9 @@ class CustomerPoolLogic extends Common
* 获取公海池列表
*
* @param array $param 查询参数user_id 用户idstructure_id 部门id
* @author fanqi
* @since 2021-04-21
* @return bool|PDOStatement|string|Collection
* @since 2021-04-21
* @author fanqi
*/
public function getPondList($param)
{
@ -345,7 +347,7 @@ class CustomerPoolLogic extends Common
->where(function ($query) use ($param, $adminTypes) {
if (!in_array(1, $adminTypes)) $query->where('admin_user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('department_ids', '%,' . $param['structure_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('department_ids', '%,' . $param['structure_id'] . ',%');
})->select();
}
@ -353,9 +355,9 @@ class CustomerPoolLogic extends Common
* 获取公海字段
*
* @param array $param pool_id 公海IDaction 操作类型action_id 数据id
* @author fanqi
* @since 2021-04-13
* @return array
* @since 2021-04-13
* @author fanqi
*/
public function getFieldList($param)
{
@ -363,14 +365,14 @@ class CustomerPoolLogic extends Common
# 自定义字段
$where = [
'pool_id' => $param['pool_id'],
'pool_id' => $param['pool_id'],
'is_hidden' => 0,
'form_type' => ['notin', ['file', 'desc_text', 'detail_table']]
];
$list = db('crm_customer_pool_field_setting')->where($where)->select();
# 处理公海字段
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
$list[$key]['field'] = $value['field_name'];
# 处理别名
@ -392,12 +394,12 @@ class CustomerPoolLogic extends Common
}
$data[$list[$key]['field']] = [
'field' => $list[$key]['field'],
'field' => $list[$key]['field'],
'fieldName' => $list[$key]['fieldName'],
'name' => $list[$key]['name'],
'width' => '',
'name' => $list[$key]['name'],
'width' => '',
'is_hidden' => 1,
'system' => $list[$key]['system'],
'system' => $list[$key]['system'],
'form_type' => $list[$key]['form_type']
];
}
@ -411,9 +413,9 @@ class CustomerPoolLogic extends Common
/**
* 高级筛选字段列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getAdvancedFilterFieldList()
{
@ -434,7 +436,7 @@ class CustomerPoolLogic extends Common
$list = array_merge($list, $base);
# 整理数据
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
if (!empty($value['setting'])) $list[$key]['setting'] = explode(chr(10), $value['setting']);
}
@ -573,22 +575,22 @@ class CustomerPoolLogic extends Common
try {
# 领取客户
Db::name('crm_customer')->whereIn('customer_id', $addCustomerId)->update($addCustomerData);
# 设置客户的联系人数据
Db::name('crm_contacts')->whereIn('customer_id', $addCustomerId)->update(['owner_user_id' => $userId]);
# 删除公海与客户关联数据
Db::name('crm_customer_pool_relation')->whereIn('customer_id', $addCustomerId)->delete();
# 字段操作日志
Db::name('admin_action_record')->insertAll($addActionRecordData);
# 数据操作日志
Db::name('admin_operation_log')->insertAll($addOperationLogData);
# 记录领取的客户
Db::name('crm_customer_pool_record')->insertAll($addReceiveData);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
@ -603,16 +605,16 @@ class CustomerPoolLogic extends Common
* 分配客户
*
* @param array $param user_id 员工IDcustomer_id 客户ID
* @author fanqi
* @since 2021-04-15
* @return array
* @since 2021-04-15
* @author fanqi
*/
public function distributeCustomer($param)
{
# 查询参数
$userId = $param['user_id'];
$userId = $param['user_id'];
$customerId = $param['customer_id'];
$username = db('admin_user')->where('id', $userId)->value('realname');
$username = db('admin_user')->where('id', $userId)->value('realname');
# 消息数据
$message = [];
@ -621,7 +623,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
# 剔除非公海客户
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
if (!empty($customerData[$value]['owner_user_id'])) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:不是公海客户!';
@ -641,7 +643,7 @@ class CustomerPoolLogic extends Common
$failCustomer = [];
if (!is_bool($exceedCount) && !empty($exceedCount) && $exceedCount > 0) {
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
foreach ($failCustomer AS $key => $value) {
foreach ($failCustomer as $key => $value) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:持有客户数达到上限!';
}
}
@ -652,16 +654,16 @@ class CustomerPoolLogic extends Common
# 检查是否还有要领取的客户
if (empty($addCustomerId)) return $message;
# 查询分配客户的公海id
$poolId=db('crm_customer_pool_relation')->whereIn('customer_id',$customerId)->value('pool_id');
$poolId = db('crm_customer_pool_relation')->whereIn('customer_id', $customerId)->value('pool_id');
# 整理客户更新数据
$addCustomerData = [
'owner_user_id' => $userId,
'owner_user_id' => $userId,
'before_owner_user_id' => 0,
'into_pool_time' => 0,
'obtain_time' => time(),
'is_dealt' => 0,
'is_allocation' => 1,
'follow' => '待跟进'
'into_pool_time' => 0,
'obtain_time' => time(),
'is_dealt' => 0,
'is_allocation' => 1,
'follow' => '待跟进'
];
# 整理字段操作记录和数据日志的数据
@ -669,29 +671,29 @@ class CustomerPoolLogic extends Common
$addActionRecordData = [];
$addOperationLogData = [];
$addReceiveData = [];
foreach ($addCustomerId AS $key => $value) {
foreach ($addCustomerId as $key => $value) {
$addActionRecordData[] = [
'user_id' => $userId,
'types' => 'crm_customer',
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'user_id' => $userId,
'types' => 'crm_customer',
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'create_time' => time()
];
$addOperationLogData[] = [
'user_id' => $userId,
'client_ip' => $ip,
'module' => 'crm_customer',
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'user_id' => $userId,
'client_ip' => $ip,
'module' => 'crm_customer',
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'create_time' => time(),
'action_name' => 'update',
'target_name' => $customerData[$value]['name']
];
$addReceiveData[] = [
'customer_id' => $value,
'user_id' => $userId,
'pool_id' => $poolId,
'type' => 3,
'user_id' => $userId,
'pool_id' => $poolId,
'type' => 3,
'create_time' => time()
];
}
@ -730,26 +732,26 @@ class CustomerPoolLogic extends Common
* 公海权限
*
* @param $param
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getAuthorityData($param)
{
# 权限
$authority = [
'index' => false, # 列表
'receive' => false, # 领取
'distribute' => false, # 分配
'index' => false, # 列表
'receive' => false, # 领取
'distribute' => false, # 分配
'excelexport' => false, # 导出
'excelimport' => false, # 导入
'delete' => false, # 删除
'delete' => false, # 删除
];
if (empty($param['pool_id']) || empty($param['user_id']) || empty($param['structure_id'])) return $authority;
$poolId = $param['pool_id'];
$userId = $param['user_id'];
$poolId = $param['pool_id'];
$userId = $param['user_id'];
$structureId = $param['structure_id'];
# 是否是超级管理员
@ -759,17 +761,17 @@ class CustomerPoolLogic extends Common
$data = db('crm_customer_pool')->field(['admin_user_ids', 'user_ids', 'department_ids'])->where('pool_id', $poolId)->find();
# 管理员、成员、部门
$adminUserIds = !empty($data['admin_user_ids']) ? explode(',', trim($data['admin_user_ids'], ',')) : [];
$userIds = !empty($data['user_ids']) ? explode(',', trim($data['user_ids'], ',')) : [];
$structureIds = !empty($data['department_ids']) ? explode(',', trim($data['department_ids'], ',')) : [];
$adminUserIds = !empty($data['admin_user_ids']) ? explode(',', trim($data['admin_user_ids'], ',')) : [];
$userIds = !empty($data['user_ids']) ? explode(',', trim($data['user_ids'], ',')) : [];
$structureIds = !empty($data['department_ids']) ? explode(',', trim($data['department_ids'], ',')) : [];
# 权限判断
$authority['index'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['receive'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['distribute'] = $userLevel || in_array($userId, $adminUserIds);
$authority['index'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['receive'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['distribute'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelexport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelimport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['delete'] = $userLevel || in_array($userId, $adminUserIds);
$authority['delete'] = $userLevel || in_array($userId, $adminUserIds);
return $authority;
}
@ -778,9 +780,9 @@ class CustomerPoolLogic extends Common
* 获取用户公海字段样式
*
* @param array $param pool 公海IDuser_is 用户ID
* @author fanqi
* @since 2021-04-22
* @return array[]
* @since 2021-04-22
* @author fanqi
*/
public function getFieldConfigIndex($param)
{
@ -789,12 +791,12 @@ class CustomerPoolLogic extends Common
# 公海字段-用户配置数据
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = !empty($data) ? json_decode($data, true) :[];
$data = !empty($data) ? json_decode($data, true) : [];
if (!empty($data)) {
$exceptFields = db('admin_field')->where(['types' => 'crm_customer', 'form_type' => ['in', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]])->column('field');
foreach ($data AS $key => $value) {
foreach ($data as $key => $value) {
if (in_array($value['field'], $exceptFields)) continue;
if (!empty($value['is_hidden'])) {
@ -806,12 +808,12 @@ class CustomerPoolLogic extends Common
} else {
# 公海字段-后台配置数据
$where = [
'pool_id' => $param['pool_id'],
'pool_id' => $param['pool_id'],
'is_hidden' => 0,
'form_type' => ['notin', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]
];
$poolField = db('crm_customer_pool_field_setting')->where($where)->select();
foreach ($poolField AS $key => $value) {
foreach ($poolField as $key => $value) {
if (empty($value['is_hidden'])) {
$showList[] = [
'field' => $value['field_name'],
@ -837,10 +839,10 @@ class CustomerPoolLogic extends Common
public function setFieldWidth($param)
{
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = !empty($data) ? json_decode($data, true) :[];
$data = !empty($data) ? json_decode($data, true) : [];
if (!empty($data)) {
foreach ($data AS $key => $value) {
foreach ($data as $key => $value) {
if ($param['field'] == $value['field']) {
$data[$key]['width'] = $param['width'];
}
@ -852,7 +854,7 @@ class CustomerPoolLogic extends Common
} else {
$result = [];
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
foreach ($poolField AS $key => $value) {
foreach ($poolField as $key => $value) {
if ($param['field'] == $value['field_name']) {
$value['width'] = $param['width'];
}
@ -884,11 +886,11 @@ class CustomerPoolLogic extends Common
*/
public function setFieldConfig($param)
{
$data = [];
$data = [];
$showList = $param['value'];
$hideList = $param['hide_value'];
foreach ($showList AS $key => $value) {
foreach ($showList as $key => $value) {
$data[] = [
'field' => $value['field'],
'name' => $value['name'],
@ -896,7 +898,7 @@ class CustomerPoolLogic extends Common
'width' => $value['width']
];
}
foreach ($hideList AS $key => $value) {
foreach ($hideList as $key => $value) {
$data[] = [
'field' => $value['field'],
'name' => $value['name'],
@ -926,9 +928,9 @@ class CustomerPoolLogic extends Common
*
* @param string $source 源ids
* @param array $target 目标数组
* @author fanqi
* @since 2021-04-14
* @return string
* @since 2021-04-14
* @author fanqi
*/
private function fieldTransformToText($source, $target)
{
@ -936,7 +938,7 @@ class CustomerPoolLogic extends Common
$array = explode(',', trim($source, ','));
foreach ($array AS $kk => $vv) {
foreach ($array as $kk => $vv) {
if (!empty($target[$vv])) $result[] = $target[$vv];
}
@ -947,14 +949,14 @@ class CustomerPoolLogic extends Common
* 查询表字段
*
* @param int $poolId 公海ID
* @author fanqi
* @since 2021-04-14
* @return string
* @since 2021-04-14
* @author fanqi
*/
private function getPoolQueryField($poolId)
{
# 自定义字段
$customerFields = db('crm_customer_pool_field_setting')->where('pool_id', $poolId)->where('is_hidden',0)->column('field_name');
$customerFields = db('crm_customer_pool_field_setting')->where('pool_id', $poolId)->where('is_hidden', 0)->column('field_name');
$customerFields[] = 'customer_id';
# 自定增加表别名
@ -968,9 +970,9 @@ class CustomerPoolLogic extends Common
/**
* 获取员工列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
private function getUserList()
{
@ -978,7 +980,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_user')->field(['id', 'realname'])->select();
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
$result[$value['id']] = $value['realname'];
}
@ -988,9 +990,9 @@ class CustomerPoolLogic extends Common
/**
* 获取部门列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
private function getStructureList()
{
@ -998,7 +1000,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_structure')->field(['id', 'name'])->select();
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
$result[$value['id']] = $value['name'];
}
@ -1009,9 +1011,9 @@ class CustomerPoolLogic extends Common
* 获取客户列表
*
* @param array $customerId 客户ID
* @author fanqi
* @since 2021-04-15
* @return array
* @since 2021-04-15
* @author fanqi
*/
private function getCustomerList($customerId)
{
@ -1021,7 +1023,7 @@ class CustomerPoolLogic extends Common
$customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name', 'into_pool_time', 'before_owner_user_id'])->whereIn('customer_id', $customerId)->select();
# 整理客户数据
foreach ($customerList AS $key => $value) {
foreach ($customerList as $key => $value) {
$result[$value['customer_id']] = $value;
}
@ -1050,16 +1052,16 @@ class CustomerPoolLogic extends Common
$list = json_decode($list, true);
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
if (!empty($value['is_hidden']) || empty($data[$value['field']])) continue;
$result[] = [
'field' => $value['field'],
'field' => $value['field'],
'fieldName' => $data[$value['field']]['fieldName'],
'name' => $value['name'],
'width' => $value['width'],
'name' => $value['name'],
'width' => $value['width'],
'is_hidden' => 0,
'system' => $data[$value['field']]['system'],
'system' => $data[$value['field']]['system'],
'form_type' => $data[$value['field']]['form_type'],
];
}
@ -1086,12 +1088,12 @@ class CustomerPoolLogic extends Common
# 公海字段-后台配置数据
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
$poolData = [];
foreach ($poolField AS $key => $value) {
foreach ($poolField as $key => $value) {
$poolData[$value['field_name']] = $value;
}
# 去掉隐藏的字段 + 去掉已经存在的字段 = 剩下的就是新增(隐藏后又开启)的字段
foreach ($fieldStyleList AS $key => $value) {
foreach ($fieldStyleList as $key => $value) {
# 去掉隐藏的字段
if (empty($poolData[$value['field']]) || (!empty($poolData[$value['field']]) && !empty($poolData[$value['field']]['is_hidden']))) {
unset($fieldStyleList[$key]);
@ -1108,7 +1110,7 @@ class CustomerPoolLogic extends Common
# 新增(隐藏后又开启)字段
if (!empty($poolData)) {
foreach ($poolData AS $key => $value) {
foreach ($poolData as $key => $value) {
if (empty($value['is_hidden'])) {
$fieldStyleList[] = [
'field' => $value['field_name'],
@ -1127,4 +1129,64 @@ class CustomerPoolLogic extends Common
}
}
}
/**
* 发起捞起客户审批
* @param array $param user_id 领取人IDcustomer_id 要领取的客户ID check_user_id 审核人id
*/
public function customerReceiveExamine($param)
{
$customerCheckModel = model('CustomerCheck');
$examineStepModel = new \app\admin\model\ExamineStep();
$examineStatus = $param['examineStatus'] ?? 1; // 审批是否停用
$param['check_user_id'] =1 ;// 临时测试需删除
unset($param['examineStatus']);
$dataInfo = db('crm_customer_check')->where('customer_id', 'in', $param['customer_id'])->find();
if ($dataInfo) {
return '客户已被领取待审批,请联系管理员';
}
if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
// 审核判断(是否有符合条件的审批流)
$examineFlowModel = new \app\admin\model\ExamineFlow();
if (!$examineFlowModel->checkExamine($param['user_id'], 'crm_customer_check')) {
return '暂无审批人,无法创建';
}
//添加审批相关信息
$examineFlowData = $examineFlowModel->getFlowByTypes($param['user_id'], 'crm_customer_check');
if (!$examineFlowData) {
return '无可用审批流,请联系管理员';
}
$param['flow_id'] = $examineFlowData['flow_id'];
//获取审批人信息
if ($examineFlowData['config'] == 1) {
//固定审批流
$nextStepData = $examineStepModel->nextStepUser($param['user_id'], $examineFlowData['flow_id'], 'crm_customer_check', 0, 0, 0);
$next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
$check_user_id = $next_user_ids ?: [];
$param['order_id'] = 1;
} else {
$check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
}
if (!$check_user_id) {
return '无可用审批人,请联系管理员';
}
$param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
} else {
# 审批流停用,将状态改为审核通过
// $param['check_status'] = 2;
# 审批流停用,将状态改为正常 zjf 20210727 默认值为7
$param['check_status'] = 7;
}
if ($customerCheckModel->createData($param)) {
return '添加成功';
} else {
return $customerCheckModel->getError();
}
}
}

@ -0,0 +1,343 @@
<?php
namespace app\crm\model;
use app\admin\model\Common;
use app\admin\model\Message;
use app\admin\traits\FieldVerificationTrait;
use think\Db;
class CustomerCheck extends Common
{
use FieldVerificationTrait;
/**
* 为了数据库的整洁同时又不影响Model和Controller的名称
* 我们约定每个模块的数据表都加上相同的前缀比如CRM模块用crm作为数据表前缀
*/
protected $name = 'crm_customer_check';
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $autoWriteTimestamp = true;
/**
* 待审核客户列表
* @param $request
* @return
*/
public function getDataList($request)
{
$userModel = new \app\admin\model\User();
$structureModel = new \app\admin\model\Structure();
$fieldModel = new \app\admin\model\Field();
$search = $request['search'];
$user_id = $request['user_id'];
$scene_id = (int)$request['scene_id'];
$contacts_id = $request['contacts_id'];
$order_field = $request['order_field'];
$order_type = $request['order_type'];
$is_excel = $request['is_excel']; //导出
$getCount = $request['getCount'];
$businessTypeId = $request['typesId']; // 针对mobile
$businessStatusId = $request['statusId']; // 针对mobile
$overdue = $request['overdue']; // 待办事项下需联系商机(逾期)
$businessIdArray = $request['businessIdArray']; // 待办事项提醒参数
$isMessage = !empty($request['isMessage']);
unset($request['scene_id']);
unset($request['search']);
unset($request['user_id']);
unset($request['contacts_id']);
unset($request['order_field']);
unset($request['order_type']);
unset($request['is_excel']);
unset($request['getCount']);
unset($request['typesId']);
unset($request['statusId']);
unset($request['overdue']);
unset($request['isMessage']);
unset($request['businessIdArray']);
$request = $this->fmtRequest($request);
$requestMap = $request['map'] ?: [];
$sceneModel = new \app\admin\model\Scene();
# getCount是代办事项传来的参数代办事项不需要使用场景
$sceneMap = [];
if (empty($getCount)) {
if ($scene_id) {
//自定义场景
$sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'business') ?: [];
} else {
//默认场景
$sceneMap = $sceneModel->getDefaultData('crm_business', $user_id) ?: [];
}
}
if ($search || $search == '0') {
//普通筛选
$sceneMap['name'] = ['condition' => 'contains', 'value' => $search, 'form_type' => 'text', 'name' => '商机名称'];
}
if (isset($requestMap['type_id'])) {
$requestMap['type_id']['value'] = $requestMap['type_id']['type_id'];
if (in_array($requestMap['type_id']['status_id'], [1, 2, 3])) {
$requestMap['is_end'] = $requestMap['type_id']['status_id'];
} else {
if ($requestMap['type_id']['status_id']) $requestMap['status_id']['value'] = $requestMap['type_id']['status_id'];
$requestMap['is_end'] = 0;
}
}
if ($sceneMap['type_id']) {
$requestMap['type_id']['value'] = $sceneMap['type_id']['type_id'];
if (in_array($sceneMap['type_id']['status_id'], [1, 2, 3])) {
$sceneMap['is_end'] = $sceneMap['type_id']['status_id'];
} else {
if ($sceneMap['type_id']['status_id']) $requestMap['status_id']['value'] = $sceneMap['type_id']['status_id'];
$sceneMap['is_end'] = 0;
}
unset($sceneMap['type_id']);
}
$partMap = [];
$teamMap = $requestMap['team_id'];
//团队成员 高级筛选
if ($teamMap) {
$partMap = advancedQueryFormatForTeam($teamMap, 'business', '');
unset($requestMap['team_id']);
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
} else {
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
}
//高级筛选
$map = advancedQuery($map, 'crm', 'business', 'index');
$authMap = [];
$a = 'index';
if ($is_excel) $a = 'excelExport';
$auth_user_ids = $userModel->getUserByPer('crm', 'business', $a);
if (isset($map['business.owner_user_id'])) {
if (!is_array($map['business.owner_user_id'][1])) {
$map['business.owner_user_id'][1] = [$map['business.owner_user_id'][1]];
}
if (in_array($map['business.owner_user_id'][0], ['neq', 'notin'])) {
$auth_user_ids = array_diff($auth_user_ids, $map['business.owner_user_id'][1]) ?: []; //取差集
} else {
$auth_user_ids = array_intersect($map['business.owner_user_id'][1], $auth_user_ids) ?: []; //取交集
}
unset($map['business.owner_user_id']);
$auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
$authMap['business.owner_user_id'] = array('in', $auth_user_ids);
} else {
if (!$isMessage) {
$authMapData = [];
$authMapData['auth_user_ids'] = $auth_user_ids;
$authMapData['user_id'] = $user_id;
$authMap = function ($query) use ($authMapData) {
$query->where('business.owner_user_id', array('in', $authMapData['auth_user_ids']))
->whereOr('business.ro_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'))
->whereOr('business.rw_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'));
};
}
}
//联系人商机
if ($contacts_id) {
$business_id = Db::name('crm_contacts_business')->where(['contacts_id' => $contacts_id])->column('business_id');
if ($business_id) {
$map['business.business_id'] = array('in', $business_id);
} else {
$map['business.business_id'] = array('eq', -1);
}
}
//列表展示字段
$indexField = $fieldModel->getIndexField('crm_business', $user_id, 1) ?: array('name');
if (!empty($indexField)) {
foreach ($indexField as $key => $value) {
if ($value == 'business.customer_name') unset($indexField[(int)$key]);
}
}
$userField = $fieldModel->getFieldByFormType('crm_business', 'user'); //人员类型
$structureField = $fieldModel->getFieldByFormType('crm_business', 'structure'); //部门类型
// $fieldGrant = db('admin_field_mask')->where('types', 'business')->select();
# 处理人员和部门类型的排序报错问题(前端传来的是包含_name的别名字段)
$temporaryField = str_replace('_name', '', $order_field);
if (in_array($temporaryField, $userField) || in_array($temporaryField, $structureField)) {
$order_field = $temporaryField;
}
//排序
if ($order_type && $order_field) {
$order = $fieldModel->getOrderByFormtype('crm_business', 'business', $order_field, $order_type);
} else {
$order = 'business.update_time desc';
}
# 商机组和商机状态搜索
if (!empty($businessTypeId)) $map['business.type_id'] = ['eq', $businessTypeId];
if (!empty($businessStatusId)) {
if (preg_match("/^[1-9][0-9]*$/", $businessStatusId)) {
$map['is_end'] = 0;
$map['business.status_id'] = ['eq', $businessStatusId];
} else {
$map['is_end'] = abs($businessStatusId);
}
}
# 待办事项查询参数
$dealtWhere = [];
if (!empty($businessIdArray)) $dealtWhere['business.business_id'] = ['in', $businessIdArray];
// 待办事项下需联系商机(逾期)
$overdueWhere = '';
if (!empty($overdue)) {
$overdueWhere = "(FROM_UNIXTIME(`business`.`last_time`,'%Y-%m-%d') < FROM_UNIXTIME(`business`.`next_time`,'%Y-%m-%d') OR (ISNULL(`business`.`last_time`) AND `business`.`next_time` < " . time() . "))";
}
$readAuthIds = $userModel->getUserByPer('crm', 'business', 'read');
$updateAuthIds = $userModel->getUserByPer('crm', 'business', 'update');
$deleteAuthIds = $userModel->getUserByPer('crm', 'business', 'delete');
$dataCount = db('crm_business')
->alias('business')
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
->where($map)->where($partMap)->where($authMap)->where($overdueWhere)->where($dealtWhere)->count('business_id');
if (!empty($getCount) && $getCount == 1) {
$data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
# 商机总金额
$sumMoney = Db::name('crm_business')->alias('business')
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
return $data;
}
$list = db('crm_business')
->alias('business')
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
->where($map)
->where($partMap)
->where($authMap)
->where($overdueWhere)
->where($dealtWhere)
->limit($request['offset'], $request['length'])
->field('business.*,customer.name as customer_name')
->orderRaw($order)
->select();
$endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
# 扩展数据
$extraData = [];
$list = getFieldData($list, 'crm_business', $user_id);
foreach ($list as $k => $v) {
$list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
$list[$k]['customer_id_info']['name'] = $v['customer_name'];
$list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
$list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
$list[$k]['create_user_name'] = !empty($list[$k]['create_user_id_info']['realname']) ? $list[$k]['create_user_id_info']['realname'] : '';
$list[$k]['owner_user_name'] = !empty($list[$k]['owner_user_id_info']['realname']) ? $list[$k]['owner_user_id_info']['realname'] : '';
$statusInfo = [];
$status_count = 0;
if (!$v['is_end']) {
$statusInfo = db('crm_business_status')->where('status_id', $v['status_id'])->find();
if ($statusInfo['order_id'] < 99) {
$status_count = db('crm_business_status')->where('type_id', ['eq', $v['type_id']])->count();
}
//进度
$list[$k]['status_progress'] = [$statusInfo['order_id'], $status_count + 1];
} else {
$statusInfo['name'] = $endStatus[$v['is_end']];
}
$list[$k]['status_id_info'] = $statusInfo['name'];//销售阶段
$list[$k]['type_id_info'] = db('crm_business_type')->where('type_id', $v['type_id'])->value('name');//商机状态组
//权限
$roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
$rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
$permission = [];
$is_read = 0;
$is_update = 0;
$is_delete = 0;
if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
$permission['is_read'] = $is_read;
$permission['is_update'] = $is_update;
$permission['is_delete'] = $is_delete;
$list[$k]['permission'] = $permission;
# 下次联系时间
$list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
# 关注
$starWhere = ['user_id' => $user_id, 'target_id' => $v['business_id'], 'type' => 'crm_business'];
$star = Db::name('crm_star')->where($starWhere)->value('star_id');
$list[$k]['star'] = !empty($star) ? 1 : 0;
# 日期
$list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
$list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
$list[$k]['last_time'] = !empty($v['last_time']) ? date('Y-m-d H:i:s', $v['last_time']) : null;
# 系统字段 负责人部门 zjf 20210726
$list[$k]['owner_user_structure_name'] = $list[$k]['owner_user_id_info']['structure_name'];
}
$data = [];
$data['list'] = $list ?: [];
$data['dataCount'] = $dataCount ?: 0;
# 商机总金额
$sumMoney = Db::name('crm_business')->alias('business')
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
return $data;
}
/**
* 创建客户捞取审批信息
* @param
* @return
*/
public function createData($param)
{
$createData = [];
foreach ($param['customer_id'] as $key => $value) {
$createData[$key]['customer_id'] = $value;
$createData[$key]['check_status'] = $param['check_status'] ?? 0;
$createData[$key]['flow_id'] = $param['flow_id'] ?? 0;
$createData[$key]['order_id'] = $param['order_id'] ?? 0;
$createData[$key]['check_user_id'] = $param['check_user_id'] ?? 0;
$createData[$key]['flow_user_id'] = $param['flow_user_id'] ?? 0;
$createData[$key]['user_id'] = $param['user_id'] ?? 0;
}
$this->startTrans();
try {
$data = $this->saveAll($createData);
$this->commit();
} catch (\Exception $e) {
$this->error = '领取失败';
$this->rollback();
return false;
}
foreach ($data as $k => $v) {
//站内信
$send_user_id = stringToArray($param['check_user_id']);
if ($send_user_id && empty($param['check_status'])) {
(new Message())->send(
Message::CUSTOMER_TO_DO,
[
'title' => $param['name'],
'action_id' => $v['customer_check_id']
],
$send_user_id
);
}
$data = [];
$data['customer_check_id'] = $v['customer_check_id'];
echo $v['customer_check_id'];
# 创建待办事项的关联数据
$checkUserIds = db('crm_customer_check')->where('customer_check_id', $data['customer_check_id'])->value('check_user_id');
$checkUserIdArray = stringToArray($checkUserIds);
$dealtData = [];
foreach ($checkUserIdArray as $kk => $vv) {
$dealtData[] = [
'types' => 'crm_customer_check',
'types_id' => $data['customer_check_id'],
'user_id' => $vv
];
}
if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
}
}
}

@ -3,13 +3,13 @@ return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => 'mysql',
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'wkcrm',
// 用户名
'username' => 'wkcrm',
// 密码
'password' => 'lpC049ZvZdmNIzDvbY0rXKgl',
'password' => 'YNKdaMS2XBHKcAh7',
// 端口
'hostport' => '3306',
// 连接dsn

Loading…
Cancel
Save