|
|
@ -29,25 +29,16 @@ class CustomerCheck extends Common
|
|
|
|
public function getDataList($request)
|
|
|
|
public function getDataList($request)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$userModel = new \app\admin\model\User();
|
|
|
|
$userModel = new \app\admin\model\User();
|
|
|
|
$structureModel = new \app\admin\model\Structure();
|
|
|
|
|
|
|
|
$fieldModel = new \app\admin\model\Field();
|
|
|
|
$fieldModel = new \app\admin\model\Field();
|
|
|
|
$search = $request['search'];
|
|
|
|
$customer_check_id= $request['customer_check_id'];
|
|
|
|
$user_id = $request['user_id'];
|
|
|
|
|
|
|
|
$scene_id = (int)$request['scene_id'];
|
|
|
|
|
|
|
|
$contacts_id = $request['contacts_id'];
|
|
|
|
|
|
|
|
$order_field = $request['order_field'];
|
|
|
|
$order_field = $request['order_field'];
|
|
|
|
$order_type = $request['order_type'];
|
|
|
|
$order_type = $request['order_type'];
|
|
|
|
$is_excel = $request['is_excel']; //导出
|
|
|
|
|
|
|
|
$getCount = $request['getCount'];
|
|
|
|
$getCount = $request['getCount'];
|
|
|
|
$businessTypeId = $request['typesId']; // 针对mobile
|
|
|
|
$customerCheckIdArray = $request['customerCheckIdArray']; // 待办事项提醒参数
|
|
|
|
$businessStatusId = $request['statusId']; // 针对mobile
|
|
|
|
|
|
|
|
$overdue = $request['overdue']; // 待办事项下需联系商机(逾期)
|
|
|
|
|
|
|
|
$businessIdArray = $request['businessIdArray']; // 待办事项提醒参数
|
|
|
|
|
|
|
|
$isMessage = !empty($request['isMessage']);
|
|
|
|
|
|
|
|
unset($request['scene_id']);
|
|
|
|
unset($request['scene_id']);
|
|
|
|
unset($request['search']);
|
|
|
|
unset($request['search']);
|
|
|
|
unset($request['user_id']);
|
|
|
|
unset($request['user_id']);
|
|
|
|
unset($request['contacts_id']);
|
|
|
|
unset($request['$customer_check_id']);
|
|
|
|
unset($request['order_field']);
|
|
|
|
unset($request['order_field']);
|
|
|
|
unset($request['order_type']);
|
|
|
|
unset($request['order_type']);
|
|
|
|
unset($request['is_excel']);
|
|
|
|
unset($request['is_excel']);
|
|
|
@ -56,228 +47,58 @@ class CustomerCheck extends Common
|
|
|
|
unset($request['statusId']);
|
|
|
|
unset($request['statusId']);
|
|
|
|
unset($request['overdue']);
|
|
|
|
unset($request['overdue']);
|
|
|
|
unset($request['isMessage']);
|
|
|
|
unset($request['isMessage']);
|
|
|
|
unset($request['businessIdArray']);
|
|
|
|
unset($request['customerCheckIdArray']);
|
|
|
|
$request = $this->fmtRequest($request);
|
|
|
|
$request = $this->fmtRequest($request);
|
|
|
|
$requestMap = $request['map'] ?: [];
|
|
|
|
$requestMap = $request['map'] ?: [];
|
|
|
|
$sceneModel = new \app\admin\model\Scene();
|
|
|
|
|
|
|
|
# getCount是代办事项传来的参数,代办事项不需要使用场景
|
|
|
|
# getCount是代办事项传来的参数,代办事项不需要使用场景
|
|
|
|
$sceneMap = [];
|
|
|
|
$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 = [];
|
|
|
|
$partMap = [];
|
|
|
|
$teamMap = $requestMap['team_id'];
|
|
|
|
$teamMap = $requestMap['team_id'];
|
|
|
|
//团队成员 高级筛选
|
|
|
|
//团队成员 高级筛选
|
|
|
|
if ($teamMap) {
|
|
|
|
if ($teamMap) {
|
|
|
|
$partMap = advancedQueryFormatForTeam($teamMap, 'business', '');
|
|
|
|
$partMap = advancedQueryFormatForTeam($teamMap, 'customer_check', '');
|
|
|
|
unset($requestMap['team_id']);
|
|
|
|
unset($requestMap['team_id']);
|
|
|
|
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
|
|
|
|
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
|
|
|
|
$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) {
|
|
|
|
if ($order_type && $order_field) {
|
|
|
|
$order = $fieldModel->getOrderByFormtype('crm_business', 'business', $order_field, $order_type);
|
|
|
|
$order = $fieldModel->getOrderByFormtype('crm_customer_check', 'customer_check', $order_field, $order_type);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$order = 'business.update_time desc';
|
|
|
|
$order = 'customer_check.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 = [];
|
|
|
|
$dealtWhere = [];
|
|
|
|
if (!empty($businessIdArray)) $dealtWhere['business.business_id'] = ['in', $businessIdArray];
|
|
|
|
if (!empty($customerCheckIdArray)) $dealtWhere['customer_check.customer_check_id'] = ['in', $customerCheckIdArray];
|
|
|
|
// 待办事项下需联系商机(逾期)
|
|
|
|
$dataCount = db('crm_customer_check')
|
|
|
|
$overdueWhere = '';
|
|
|
|
->alias('customer_check')
|
|
|
|
if (!empty($overdue)) {
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'customer_check.customer_check_id = customer.customer_id', 'LEFT')
|
|
|
|
$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() . "))";
|
|
|
|
->where($map)->where($partMap)->where($dealtWhere)->count('customer_check_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
if (!empty($getCount) && $getCount == 1) {
|
|
|
|
$data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
|
|
|
|
$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;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$list = db('crm_business')
|
|
|
|
$list = db('crm_customer_check')
|
|
|
|
->alias('business')
|
|
|
|
->alias('customer_check')
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
|
|
|
|
->where($map)
|
|
|
|
->where($map)
|
|
|
|
->where($partMap)
|
|
|
|
->where($partMap)
|
|
|
|
->where($authMap)
|
|
|
|
|
|
|
|
->where($overdueWhere)
|
|
|
|
|
|
|
|
->where($dealtWhere)
|
|
|
|
->where($dealtWhere)
|
|
|
|
->limit($request['offset'], $request['length'])
|
|
|
|
->limit($request['offset'], $request['length'])
|
|
|
|
->field('business.*,customer.name as customer_name')
|
|
|
|
->field('customer_check.*,customer.name as customer_name')
|
|
|
|
->orderRaw($order)
|
|
|
|
->orderRaw($order)
|
|
|
|
->select();
|
|
|
|
->select();
|
|
|
|
$endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
|
|
|
|
|
|
|
|
# 扩展数据
|
|
|
|
# 扩展数据
|
|
|
|
$extraData = [];
|
|
|
|
$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 = [];
|
|
|
|
$data['list'] = $list ?: [];
|
|
|
|
$data['list'] = $list ?: [];
|
|
|
|
$data['dataCount'] = $dataCount ?: 0;
|
|
|
|
$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;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|