@ -14,14 +14,14 @@ use app\crm\model\Business as CrmBusinessModel;
class Business extends Common
class Business extends Common
{
{
/**
/**
* 为了数据库的整洁, 同时又不影响Model和Controller的名称
* 为了数据库的整洁, 同时又不影响Model和Controller的名称
* 我们约定每个模块的数据表都加上相同的前缀, 比如CRM模块用crm作为数据表前缀
* 我们约定每个模块的数据表都加上相同的前缀, 比如CRM模块用crm作为数据表前缀
*/
*/
protected $name = 'crm_business';
protected $name = 'crm_business';
protected $createTime = 'create_time';
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $updateTime = 'update_time';
protected $autoWriteTimestamp = true;
protected $autoWriteTimestamp = true;
/**
/**
* [getDataList 商机list]
* [getDataList 商机list]
@ -32,190 +32,196 @@ class Business extends Common
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\DbException
*/
*/
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();
$structureModel = new \app\admin\model\Structure();
$fieldModel = new \app\admin\model\Field();
$fieldModel = new \app\admin\model\Field();
$search = $request['search'];
$search = $request['search'];
$user_id = $request['user_id'];
$user_id = $request['user_id'];
$scene_id = (int)$request['scene_id'];
$scene_id = (int)$request['scene_id'];
$contacts_id = $request['contacts_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']; //导出
$is_excel = $request['is_excel']; //导出
$getCount = $request['getCount'];
$getCount = $request['getCount'];
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['contacts_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']);
unset($request['getCount']);
unset($request['getCount']);
$request = $this->fmtRequest( $request );
$request = $this->fmtRequest($request);
$requestMap = $request['map'] ? : [];
$requestMap = $request['map'] ?: [];
$sceneModel = new \app\admin\model\Scene();
$sceneModel = new \app\admin\model\Scene();
# getCount是代办事项传来的参数, 代办事项不需要使用场景
# getCount是代办事项传来的参数, 代办事项不需要使用场景
$sceneMap = [];
$sceneMap = [];
if (empty($getCount)) {
if (empty($getCount)) {
if ($scene_id) {
if ($scene_id) {
//自定义场景
//自定义场景
$sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'business') ? : [];
$sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'business') ?: [];
} else {
} else {
//默认场景
//默认场景
$sceneMap = $sceneModel->getDefaultData('crm_business', $user_id) ? : [];
$sceneMap = $sceneModel->getDefaultData('crm_business', $user_id) ?: [];
}
}
if ($search) {
//普通筛选
$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 ($requestMap['type_id']['status_id']) $requestMap['status_id']['value'] = $requestMap['type_id']['status_id'];
}
if ($sceneMap['type_id']) {
$requestMap['type_id']['value'] = $sceneMap['type_id']['type_id'];
if ($sceneMap['type_id']['status_id']) $requestMap['status_id']['value'] = $sceneMap['type_id']['status_id'];
unset($sceneMap['type_id']);
}
$partMap = [];
//优先级:普通筛选>高级筛选>场景
if ($sceneMap['ro_user_id'] & & $sceneMap['rw_user_id']) {
//相关团队查询
$map = $requestMap;
$partMap = function ($query) use ($sceneMap) {
$query->where('business.ro_user_id', array('like', '%,' . $sceneMap['ro_user_id'] . ',%'))
->whereOr('business.rw_user_id', array('like', '%,' . $sceneMap['rw_user_id'] . ',%'));
};
} else {
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
}
//高级筛选
$map = where_arr($map, 'crm', 'business', 'index');
$authMap = [];
if (!$partMap) {
$a = 'index';
if ($is_excel) $a = 'excelExport';
$auth_user_ids = $userModel->getUserByPer('crm', 'business', $a);
if (isset($map['business.owner_user_id']) & & $map['business.owner_user_id'][0] != 'like') {
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 {
$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);
}
}
}
}
if ($search) {
//列表展示字段
//普通筛选
$indexField = $fieldModel->getIndexField('crm_business', $user_id, 1) ?: array('name');
$sceneMap['name'] = ['condition' => 'contains','value' => $search,'form_type' => 'text','name' => '商机名称'];
if (!empty($indexField)) {
}
foreach ($indexField as $key => $value) {
if (isset($requestMap['type_id'])) {
if ($value == 'business.customer_name') unset($indexField[(int)$key]);
$requestMap['type_id']['value'] = $requestMap['type_id']['type_id'];
if ($requestMap['type_id']['status_id']) $requestMap['status_id']['value'] = $requestMap['type_id']['status_id'];
}
if ($sceneMap['type_id']) {
$requestMap['type_id']['value'] = $sceneMap['type_id']['type_id'];
if ($sceneMap['type_id']['status_id']) $requestMap['status_id']['value'] = $sceneMap['type_id']['status_id'];
unset($sceneMap['type_id']);
}
$partMap = [];
//优先级:普通筛选>高级筛选>场景
if ($sceneMap['ro_user_id'] & & $sceneMap['rw_user_id']) {
//相关团队查询
$map = $requestMap;
$partMap = function($query) use ($sceneMap){
$query->where('business.ro_user_id',array('like','%,'.$sceneMap['ro_user_id'].',%'))
->whereOr('business.rw_user_id',array('like','%,'.$sceneMap['rw_user_id'].',%'));
};
} else {
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
}
//高级筛选
$map = where_arr($map, 'crm', 'business', 'index');
$authMap = [];
if (!$partMap) {
$a = 'index';
if ($is_excel) $a = 'excelExport';
$auth_user_ids = $userModel->getUserByPer('crm', 'business', $a);
if (isset($map['business.owner_user_id']) & & $map['business.owner_user_id'][0] != 'like') {
if (!is_array($map['business.owner_user_id'][1])) {
$map['business.owner_user_id'][1] = [$map['business.owner_user_id'][1]];
}
if ($map['business.owner_user_id'][0] == 'neq') {
$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 {
$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'); //人员类型
$userField = $fieldModel->getFieldByFormType('crm_business', 'user'); //人员类型
$structureField = $fieldModel->getFieldByFormType('crm_business', 'structure'); //部门类型
$structureField = $fieldModel->getFieldByFormType('crm_business', 'structure'); //部门类型
//排序
$datetimeField = $fieldModel->getFieldByFormType('crm_business', 'datetime'); //日期时间类型
if ($order_type & & $order_field) {
//排序
$order = $fieldModel->getOrderByFormtype('crm_business','business',$order_field,$order_type);
if ($order_type & & $order_field) {
} else {
$order = $fieldModel->getOrderByFormtype('crm_business', 'business', $order_field, $order_type);
$order = 'business.update_time desc';
} else {
}
$order = 'business.update_time desc';
}
$readAuthIds = $userModel->getUserByPer('crm', 'business', 'read');
$readAuthIds = $userModel->getUserByPer('crm', 'business', 'read');
$updateAuthIds = $userModel->getUserByPer('crm', 'business', 'update');
$updateAuthIds = $userModel->getUserByPer('crm', 'business', 'update');
$deleteAuthIds = $userModel->getUserByPer('crm', 'business', 'delete');
$deleteAuthIds = $userModel->getUserByPer('crm', 'business', 'delete');
$dataCount = db('crm_business')
$dataCount = db('crm_business')
->alias('business')
->alias('business')
->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($partMap)->where($authMap)->count('business_id');
->where($map)->where($partMap)->where($authMap)->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')
$sumMoney = Db::name('crm_business')->alias('business')
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
return $data;
return $data;
}
}
$list = db('crm_business')
$list = db('crm_business')
->alias('business')
->alias('business')
->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($authMap)
->limit($request['offset'], $request['length'])
->limit($request['offset'], $request['length'])
->field(implode(',',$indexField).',business.is_end ,customer.name as customer_name')
->field('business.*,customer.name as customer_name')
->orderRaw($order)
->orderRaw($order)
->select();
->select();
$endStatus = ['1' => '赢单','2' => '输单','3' => '无效'];
$endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
foreach ($list as $k=>$v) {
foreach ($list as $k => $v) {
$list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
$list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
$list[$k]['customer_id_info']['name'] = $v['customer_name'];
$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]['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]['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]['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'] : '';
$list[$k]['owner_user_name'] = !empty($list[$k]['owner_user_id_info']['realname']) ? $list[$k]['owner_user_id_info']['realname'] : '';
foreach ($userField as $key => $val) {
foreach ($userField as $key => $val) {
$list[$k][$val.'_info'] = isset($v[$val]) ? $userModel->getListByStr($v[$val]) : [];
$usernameField = !empty($v[$val]) ? db('admin_user')->whereIn('id', stringToArray($v[$val]))->column('realname') : [];
}
$list[$k][$val] = implode($usernameField, ',');
foreach ($structureField as $key => $val) {
}
$list[$k][$val.'_info'] = isset($v[$val]) ? $structureModel->getDataByStr($v[$val]) : [];
foreach ($structureField as $key => $val) {
}
$structureNameField = !empty($v[$val]) ? db('admin_structure')->whereIn('id', stringToArray($v[$val]))->column('name') : [];
$statusInfo = [];
$list[$k][$val] = implode($structureNameField, ',');
$status_count = 0;
}
if (!$v['is_end']) {
foreach ($datetimeField as $key => $val) {
$statusInfo = db('crm_business_status')->where('status_id',$v['status_id'])->find();
$list[$k][$val] = !empty($v[$val]) ? date('Y-m-d H:i:s', $v[$val]) : null;
if ($statusInfo['order_id'] < 99 ) {
}
$status_count = db('crm_business_status')->where('type_id',['eq',$v['type_id']])->count();
$statusInfo = [];
}
$status_count = 0;
//进度
if (!$v['is_end']) {
$list[$k]['status_progress'] = [$statusInfo['order_id'], $status_count+1];
$statusInfo = db('crm_business_status')->where('status_id', $v['status_id'])->find();
} else {
if ($statusInfo['order_id'] < 99 ) {
$statusInfo['name'] = $endStatus[$v['is_end']];
$status_count = db('crm_business_status')->where('type_id', ['eq', $v['type_id']])->count();
}
}
$list[$k]['status_id_info'] = $statusInfo['name'];//销售阶段
//进度
$list[$k]['type_id_info'] = db('crm_business_type')->where('type_id',$v['type_id'])->value('name');//商机状态组
$list[$k]['status_progress'] = [$statusInfo['order_id'], $status_count + 1];
} else {
//权限
$statusInfo['name'] = $endStatus[$v['is_end']];
$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');
$list[$k]['status_id_info'] = $statusInfo['name'];//销售阶段
$permission = [];
$list[$k]['type_id_info'] = db('crm_business_type')->where('type_id', $v['type_id'])->value('name');//商机状态组
$is_read = 0;
$is_update = 0;
//权限
$is_delete = 0;
$roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
if (in_array($v['owner_user_id'],$readAuthIds) || $roPre || $rwPre) $is_read = 1;
$rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
if (in_array($v['owner_user_id'],$updateAuthIds) || $rwPre) $is_update = 1;
$permission = [];
if (in_array($v['owner_user_id'],$deleteAuthIds)) $is_delete = 1;
$is_read = 0;
$permission['is_read'] = $is_read;
$is_update = 0;
$permission['is_update'] = $is_update;
$is_delete = 0;
$permission['is_delete'] = $is_delete;
if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
$list[$k]['permission'] = $permission;
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;
$list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
# 关注
# 关注
@ -225,159 +231,159 @@ class Business extends Common
# 日期
# 日期
$list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
$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]['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;
$list[$k]['last_time'] = !empty($v['last_time']) ? date('Y-m-d H:i:s', $v['last_time']) : null;
}
}
$data = [];
$data = [];
$data['list'] = $list ? : [];
$data['list'] = $list ?: [];
$data['dataCount'] = $dataCount ? : 0;
$data['dataCount'] = $dataCount ?: 0;
# 商机总金额
# 商机总金额
$sumMoney = Db::name('crm_business')->alias('business')
$sumMoney = Db::name('crm_business')->alias('business')
->join('__CRM_CUSTOMER__ customer','business.customer_id = customer.customer_id','LEFT')
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
return $data;
return $data;
}
}
/**
/**
* 创建商机主表信息
* 创建商机主表信息
* @author Michael_xu
* @param
* @param
* @return
* @return
* @author Michael_xu
*/
*/
public function createData($param)
public function createData($param)
{
{
$fieldModel = new \app\admin\model\Field();
$fieldModel = new \app\admin\model\Field();
$productModel = new \app\crm\model\Product();
$productModel = new \app\crm\model\Product();
// 自动验证
// 自动验证
$validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
$validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
$validate = new Validate($validateArr['rule'], $validateArr['message']);
$validate = new Validate($validateArr['rule'], $validateArr['message']);
$result = $validate->check($param);
$result = $validate->check($param);
if (!$result) {
if (!$result) {
$this->error = $validate->getError();
$this->error = $validate->getError();
return false;
return false;
}
}
if (!$param['customer_id']) {
if (!$param['customer_id']) {
$this->error = '请选择相关客户';
$this->error = '请选择相关客户';
return false;
return false;
}
}
# 处理下次联系时间
# 处理下次联系时间
if (!empty($param['next_time'])) $param['next_time'] = strtotime($param['next_time']);
if (!empty($param['next_time'])) $param['next_time'] = strtotime($param['next_time']);
//处理部门、员工、附件、多选类型字段
//处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_business');
$arrFieldAtt = $fieldModel->getArrayField('crm_business');
foreach ($arrFieldAtt as $k=> $v) {
foreach ($arrFieldAtt as $k => $v) {
$param[$v] = arrayToString($param[$v]);
$param[$v] = arrayToString($param[$v]);
}
}
$param['money'] = $param['money'] ? : '0.00';
$param['money'] = $param['money'] ? : '0.00';
$param['discount_rate'] = $param['discount_rate'] ? : '0.00';
$param['discount_rate'] = $param['discount_rate'] ?: '0.00';
if ($this->data($param)->allowField(true)->save()) {
if ($this->data($param)->allowField(true)->save()) {
$business_id = $this->business_id;
$business_id = $this->business_id;
if ($param['product']) {
if ($param['product']) {
//产品数据处理
//产品数据处理
$resProduct = $productModel->createObject('crm_business', $param, $business_id);
$resProduct = $productModel->createObject('crm_business', $param, $business_id);
if ($resProduct == false) {
if ($resProduct == false) {
$this->error = '产品添加失败';
$this->error = '产品添加失败';
return false;
return false;
}
}
}
}
//添加商机日志
//添加商机日志
$data_log['business_id'] = $business_id;
$data_log['business_id'] = $business_id;
$data_log['is_end'] = 0;
$data_log['is_end'] = 0;
$data_log['status_id'] = $param['status_id'];
$data_log['status_id'] = $param['status_id'];
$data_log['create_time'] = time();
$data_log['create_time'] = time();
$data_log['owner_user_id'] = $param['owner_user_id'];
$data_log['owner_user_id'] = $param['owner_user_id'];
$data_log['remark'] = '新建商机';
$data_log['remark'] = '新建商机';
Db::name('CrmBusinessLog')->insert($data_log);
Db::name('CrmBusinessLog')->insert($data_log);
$data = [];
$data = [];
$data['business_id'] = $business_id;
$data['business_id'] = $business_id;
# 添加活动记录
# 添加活动记录
Db::name('crm_activity')->insert([
Db::name('crm_activity')->insert([
'type' => 2,
'type' => 2,
'activity_type' => 5,
'activity_type' => 5,
'activity_type_id' => $data['business_id'],
'activity_type_id' => $data['business_id'],
'content' => $param['name'],
'content' => $param['name'],
'create_user_id' => $param['create_user_id'],
'create_user_id' => $param['create_user_id'],
'update_time' => time(),
'update_time' => time(),
'create_time' => time(),
'create_time' => time(),
'customer_ids' => $param['customer_id']
'customer_ids' => $param['customer_id']
]);
]);
return $data;
return $data;
} else {
} else {
$this->error = '添加失败';
$this->error = '添加失败';
return false;
return false;
}
}
}
}
/**
/**
* 编辑商机主表信息
* 编辑商机主表信息
* @author Michael_xu
* @param
* @param
* @return
* @return
* @author Michael_xu
*/
*/
public function updateDataById($param, $business_id = '')
public function updateDataById($param, $business_id = '')
{
{
$productModel = new \app\crm\model\Product();
$productModel = new \app\crm\model\Product();
$dataInfo = $this->getDataById($business_id);
$dataInfo = $this->getDataById($business_id);
if (!$dataInfo) {
if (!$dataInfo) {
$this->error = '数据不存在或已删除';
$this->error = '数据不存在或已删除';
return false;
return false;
}
}
$param['business_id'] = $business_id;
$param['business_id'] = $business_id;
//过滤不能修改的字段
//过滤不能修改的字段
$unUpdateField = ['create_user_id','is_deleted', 'delete_time'];
$unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
foreach ($unUpdateField as $v) {
foreach ($unUpdateField as $v) {
unset($param[$v]);
unset($param[$v]);
}
}
$fieldModel = new \app\admin\model\Field();
$fieldModel = new \app\admin\model\Field();
// 自动验证
// 自动验证
$validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
$validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
$validate = new Validate($validateArr['rule'], $validateArr['message']);
$validate = new Validate($validateArr['rule'], $validateArr['message']);
$result = $validate->check($param);
$result = $validate->check($param);
if (!$result) {
if (!$result) {
$this->error = $validate->getError();
$this->error = $validate->getError();
return false;
return false;
}
}
# 处理下次联系时间
# 处理下次联系时间
if (!empty($param['next_time'])) $param['next_time'] = strtotime($param['next_time']);
if (!empty($param['next_time'])) $param['next_time'] = strtotime($param['next_time']);
//处理部门、员工、附件、多选类型字段
//处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_business');
$arrFieldAtt = $fieldModel->getArrayField('crm_business');
foreach ($arrFieldAtt as $k=> $v) {
foreach ($arrFieldAtt as $k => $v) {
$param[$v] = arrayToString($param[$v]);
$param[$v] = arrayToString($param[$v]);
}
}
$param['money'] = $param['money'] ? : '0.00';
$param['money'] = $param['money'] ? : '0.00';
$param['discount_rate'] = $param['discount_rate'] ? : '0.00';
$param['discount_rate'] = $param['discount_rate'] ?: '0.00';
//商机状态改变
//商机状态改变
$statusInfo = db('crm_business_status')->where(['status_id' => $param['status_id']])->find();
$statusInfo = db('crm_business_status')->where(['status_id' => $param['status_id']])->find();
if ($statusInfo['type_id']) {
if ($statusInfo['type_id']) {
$param['is_end'] = 0;
$param['is_end'] = 0;
} else {
} else {
$param['is_end'] = $param['status_id'];
$param['is_end'] = $param['status_id'];
}
}
if ($this->update($param, ['business_id' => $business_id], true)) {
if ($this->update($param, ['business_id' => $business_id], true)) {
//产品数据处理
//产品数据处理
$resProduct = $productModel->createObject('crm_business', $param, $business_id);
$resProduct = $productModel->createObject('crm_business', $param, $business_id);
//修改记录
//修改记录
updateActionLog($param['user_id'], 'crm_business', $business_id, $dataInfo, $param);
updateActionLog($param['user_id'], 'crm_business', $business_id, $dataInfo, $param);
$data = [];
$data = [];
$data['business_id'] = $business_id;
$data['business_id'] = $business_id;
return $data;
return $data;
} else {
} else {
$this->rollback();
$this->rollback();
$this->error = '编辑失败';
$this->error = '编辑失败';
return false;
return false;
}
}
}
}
/**
/**
* 商机数据
* 商机数据
@ -389,84 +395,89 @@ class Business extends Common
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\DbException
*/
*/
public function getDataById($id = '', $userId = 0)
public function getDataById($id = '', $userId = 0)
{
{
$dataInfo = db('crm_business')->where('business_id', $id)->find();
$dataInfo = db('crm_business')->where('business_id', $id)->find();
if (!$dataInfo) {
if (!$dataInfo) {
$this->error = '暂无此数据';
$this->error = '暂无此数据';
return false;
return false;
}
}
$userModel = new \app\admin\model\User();
$userModel = new \app\admin\model\User();
$dataInfo['create_user_id_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
$dataInfo['create_user_id_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
$dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
$dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
$dataInfo['create_user_name'] = !empty($dataInfo['create_user_id_info']['realname']) ? $dataInfo['create_user_id_info']['realname'] : '';
$dataInfo['create_user_name'] = !empty($dataInfo['create_user_id_info']['realname']) ? $dataInfo['create_user_id_info']['realname'] : '';
$dataInfo['owner_user_name'] = !empty($dataInfo['owner_user_id_info']['realname']) ? $dataInfo['owner_user_id_info']['realname'] : '';
$dataInfo['owner_user_name'] = !empty($dataInfo['owner_user_id_info']['realname']) ? $dataInfo['owner_user_id_info']['realname'] : '';
$dataInfo['type_id_info'] = db('crm_business_type')->where(['type_id' => $dataInfo['type_id']])->value('name');
$dataInfo['type_id_info'] = db('crm_business_type')->where(['type_id' => $dataInfo['type_id']])->value('name');
$dataInfo['status_id_info'] = db('crm_business_status')->where(['status_id' => $dataInfo['status_id']])->value('name');
$dataInfo['status_id_info'] = db('crm_business_status')->where(['status_id' => $dataInfo['status_id']])->value('name');
$dataInfo['customer_id_info'] = db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find();
$dataInfo['customer_id_info'] = db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find();
$dataInfo['customer_name'] = !empty($dataInfo['customer_id_info']['name']) ? $dataInfo['customer_id_info']['name'] : '';
$dataInfo['customer_name'] = !empty($dataInfo['customer_id_info']['name']) ? $dataInfo['customer_id_info']['name'] : '';
# 关注
# 关注
$starId = empty($userId) ? 0 : Db::name('crm_star')->where(['user_id' => $userId, 'target_id' => $id, 'type' => 'crm_business'])->value('star_id');
$starId = empty($userId) ? 0 : Db::name('crm_star')->where(['user_id' => $userId, 'target_id' => $id, 'type' => 'crm_business'])->value('star_id');
$dataInfo['star'] = !empty($starId) ? 1 : 0;
$dataInfo['star'] = !empty($starId) ? 1 : 0;
# 首要联系人
# 首要联系人
$primaryId = Db::name('crm_contacts')->where(['contacts_id' => $dataInfo['contacts_id']])->value('contacts_id');
$primaryId = Db::name('crm_contacts')->where(['contacts_id' => $dataInfo['contacts_id']])->value('contacts_id');
$dataInfo['contacts_id'] = !empty($primaryId) & & $this->getContactsAuth($primaryId) ? $primaryId : 0;
$dataInfo['contacts_id'] = !empty($primaryId) & & $this->getContactsAuth($primaryId) ? $primaryId : 0;
# 处理日期格式
# 处理日期格式
$dataInfo['next_time'] = !empty($dataInfo['next_time']) ? date('Y-m-d H:i:s', $dataInfo['next_time']) : null;
$fieldModel = new \app\admin\model\Field();
$datetimeField = $fieldModel->getFieldByFormType('crm_business', 'datetime'); //日期时间类型
foreach ($datetimeField as $key => $val) {
$dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
}
$dataInfo['next_time'] = !empty($dataInfo['next_time']) ? date('Y-m-d H:i:s', $dataInfo['next_time']) : null;
$dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
$dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
$dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
$dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
$dataInfo['last_time'] = !empty($dataInfo['last_time']) ? date('Y-m-d H:i:s', $dataInfo['last_time']) : null;
$dataInfo['last_time'] = !empty($dataInfo['last_time']) ? date('Y-m-d H:i:s', $dataInfo['last_time']) : null;
return $dataInfo;
return $dataInfo;
}
}
//根据IDs获取数组
//根据IDs获取数组
public function getDataByStr($idstr)
public function getDataByStr($idstr)
{
{
$idArr = stringToArray($idstr);
$idArr = stringToArray($idstr);
if (!$idArr) {
if (!$idArr) {
return [];
return [];
}
}
$list = Db::name('CrmBusiness')->where(['business_id' => ['in',$idArr]])->select();
$list = Db::name('CrmBusiness')->where(['business_id' => ['in', $idArr]])->select();
return $list;
return $list;
}
}
/**
/**
* [商机漏斗]
* [商机漏斗]
* @author Michael_xu
* @param
* @param
* @return
* @return
* @author Michael_xu
*/
*/
public function getFunnel($request)
public function getFunnel($request)
{
{
$merge = $request['merge'] ? : 0;
$merge = $request['merge'] ? : 0;
$perUserIds = $request['perUserIds'] ? : [];
$perUserIds = $request['perUserIds'] ? : [];
$adminModel = new \app\admin\model\Admin();
$adminModel = new \app\admin\model\Admin();
$whereArr = $adminModel->getWhere($request, $merge, $perUserIds); //统计查询
$whereArr = $adminModel->getWhere($request, $merge, $perUserIds); //统计查询
$userIds = $whereArr['userIds'];
$userIds = $whereArr['userIds'];
$between_time = $whereArr['between_time'];
$between_time = $whereArr['between_time'];
$where['owner_user_id'] = array('in',$userIds);
$where['owner_user_id'] = array('in', $userIds);
$where['create_time'] = array('between',$between_time);
$where['create_time'] = array('between', $between_time);
//商机状态组
//商机状态组
$default_type_id = db('crm_business_type')->order('type_id asc')->value('type_id');
$default_type_id = db('crm_business_type')->order('type_id asc')->value('type_id');
$type_id = $request['type_id'] ? $request['type_id'] : $default_type_id;
$type_id = $request['type_id'] ? $request['type_id'] : $default_type_id;
$statusList = db('crm_business_status')->where(['type_id' => $type_id])->select();
$statusList = db('crm_business_status')->where(['type_id' => $type_id])->select();
$map = [];
$map = [];
$map['create_time'] = $where['create_time'];
$map['create_time'] = $where['create_time'];
$map['owner_user_id'] = ['in', $userIds];
$map['owner_user_id'] = ['in', $userIds];
$sql_a = CrmBusinessModel::field([
$sql_a = CrmBusinessModel::field([
'SUM(CASE WHEN status_id = 1 THEN money ELSE 0 END) AS sum_ying',
'SUM(CASE WHEN status_id = 1 THEN money ELSE 0 END) AS sum_ying',
'SUM(CASE WHEN status_id = 2 THEN money ELSE 0 END) AS sum_shu'
'SUM(CASE WHEN status_id = 2 THEN money ELSE 0 END) AS sum_shu'
])
])
->where($map)
->where($map)
->fetchSql()
->fetchSql()
->select();
->select();
$res_a = queryCache($sql_a, 200);
$res_a = queryCache($sql_a, 200);
$sql = CrmBusinessModel::field([
$sql = CrmBusinessModel::field([
"status_id",
"status_id",
'COUNT(*)' => 'count',
'COUNT(*)' => 'count',
'SUM(`money`)' => 'sum'
'SUM(`money`)' => 'sum'
])
])
->where($where)
->where($where)
->whereNotIn('is_end', '1,2,3')
->whereNotIn('is_end', '1,2,3')
->group('status_id')
->group('status_id')
@ -476,45 +487,45 @@ class Business extends Common
$res = array_column($res, null, 'status_id');
$res = array_column($res, null, 'status_id');
$sum_money = 0;
$sum_money = 0;
$count = 0; # 商机数总和
$count = 0; # 商机数总和
$moneyCount = 0; # 金额总和
$moneyCount = 0; # 金额总和
foreach ($statusList as $k=> $v) {
foreach ($statusList as $k => $v) {
$v['count'] = $res[$v['status_id']]['count'] ? : 0;
$v['count'] = $res[$v['status_id']]['count'] ?: 0;
$v['money'] = $res[$v['status_id']]['sum'] ? : 0;
$v['money'] = $res[$v['status_id']]['sum'] ?: 0;
$v['status_name'] = $v['name'];
$v['status_name'] = $v['name'];
$statusList[$k] = $v;
$statusList[$k] = $v;
$sum_money += $v['money'];
$sum_money += $v['money'];
$moneyCount += $v['money'];
$moneyCount += $v['money'];
$count += $v['count'];
$count += $v['count'];
}
}
$data['list'] = $statusList;
$data['list'] = $statusList;
$data['list'] = $statusList;
$data['list'] = $statusList;
$data['sum_ying'] = $res_a[0]['sum_ying'] ? : 0;
$data['sum_ying'] = $res_a[0]['sum_ying'] ?: 0;
$data['sum_shu'] = $res_a[0]['sum_shu'] ? : 0;
$data['sum_shu'] = $res_a[0]['sum_shu'] ?: 0;
$data['sum_money'] = $sum_money ? : 0;
$data['sum_money'] = $sum_money ?: 0;
$data['total'] = ['name' => '合计', 'money_count' => $moneyCount, 'count' => $count];
$data['total'] = ['name' => '合计', 'money_count' => $moneyCount, 'count' => $count];
return $data ? : [];
return $data ?: [];
}
}
/**
/**
* [商机转移]
* [商机转移]
* @author Michael_xu
* @param ids 商机ID数组
* @param ids 商机ID数组
* @param owner_user_id 变更负责人
* @param owner_user_id 变更负责人
* @param is_remove 1移出, 2转为团队成员
* @param is_remove 1移出, 2转为团队成员
* @return
* @return
* @author Michael_xu
*/
*/
public function transferDataById($ids, $owner_user_id, $type = 1, $is_remove)
public function transferDataById($ids, $owner_user_id, $type = 1, $is_remove)
{
{
$settingModel = new \app\crm\model\Setting();
$settingModel = new \app\crm\model\Setting();
$errorMessage = [];
$errorMessage = [];
foreach ($ids as $id) {
foreach ($ids as $id) {
$businessInfo = db('crm_business')->where(['business_id' => $id])->find();
$businessInfo = db('crm_business')->where(['business_id' => $id])->find();
//团队成员
//团队成员
$teamData = [];
$teamData = [];
$teamData['type'] = $type; //权限 1只读2读写
$teamData['type'] = $type; //权限 1只读2读写
$teamData['user_id'] = [$businessInfo['owner_user_id']]; //协作人
$teamData['user_id'] = [$businessInfo['owner_user_id']]; //协作人
$teamData['types'] = 'crm_business'; //类型
$teamData['types'] = 'crm_business'; //类型
@ -522,19 +533,31 @@ class Business extends Common
$teamData['is_del'] = ($is_remove == 1) ? 1 : '';
$teamData['is_del'] = ($is_remove == 1) ? 1 : '';
$res = $settingModel->createTeamData($teamData);
$res = $settingModel->createTeamData($teamData);
$data = [];
$data = [];
$data['owner_user_id'] = $owner_user_id;
$data['owner_user_id'] = $owner_user_id;
$data['update_time'] = time();
$data['update_time'] = time();
if (!db('crm_business')->where(['business_id' => $id])->update($data)) {
if (!db('crm_business')->where(['business_id' => $id])->update($data)) {
$errorMessage[] = '商机:'.$businessInfo['name'].'"转移失败,错误原因:数据出错;';
$errorMessage[] = '商机:' . $businessInfo['name'] . '"转移失败,错误原因:数据出错;';
continue;
continue;
}
} else {
}
$businessArray = [];
if ($errorMessage) {
$teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $id)->find();
return $errorMessage;
if (!empty($teamBusiness['ro_user_id'])) {
} else {
$businessRo = arrayToString(array_diff(stringToArray($teamBusiness['ro_user_id']), [$teamBusiness['owner_user_id']]));
return true;
$businessArray['ro_user_id'] = $businessRo;
}
}
if (!empty($teamBusiness['rw_user_id'])) {
$businessRo = arrayToString(array_diff(stringToArray($teamBusiness['rw_user_id']), [$teamBusiness['owner_user_id']]));
$businessArray['rw_user_id'] = $businessRo;
}
db('crm_business')->where('business_id', $id)->update($businessArray);
}
}
if ($errorMessage) {
return $errorMessage;
} else {
return true;
}
}
}
/**
/**
@ -542,10 +565,10 @@ class Business extends Common
* @param
* @param
* @return
* @return
*/
*/
public function getTrendql($map)
public function getTrendql($map)
{
{
$prefix = config('database.prefix');
$prefix = config('database.prefix');
$sql = "SELECT
$sql = "SELECT
'{$map['type']}' AS type,
'{$map['type']}' AS type,
'{$map['start_time']}' AS start_time,
'{$map['start_time']}' AS start_time,
'{$map['end_time']}' AS end_time,
'{$map['end_time']}' AS end_time,
@ -570,18 +593,18 @@ class Business extends Common
WHERE
WHERE
create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
AND owner_user_id IN ({$map['owner_user_id']})";
AND owner_user_id IN ({$map['owner_user_id']})";
return $sql;
return $sql;
}
}
/**
/**
* [赢单机会转化率趋势分析]
* [赢单机会转化率趋势分析]
* @param
* @param
* @return
* @return
*/
*/
public function getWinSql($map)
public function getWinSql($map)
{
{
$prefix = config('database.prefix');
$prefix = config('database.prefix');
$sql = "SELECT
$sql = "SELECT
'{$map['type']}' AS type,
'{$map['type']}' AS type,
'{$map['start_time']}' AS start_time,
'{$map['start_time']}' AS start_time,
'{$map['end_time']}' AS end_time,
'{$map['end_time']}' AS end_time,
@ -607,8 +630,8 @@ class Business extends Common
WHERE
WHERE
create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
AND owner_user_id IN ({$map['owner_user_id']})";
AND owner_user_id IN ({$map['owner_user_id']})";
return $sql;
return $sql;
}
}
/**
/**
* 获取系统信息
* 获取系统信息
@ -619,7 +642,7 @@ class Business extends Common
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\DbException
*/
*/
public function getSystemInfo($id)
public function getSystemInfo($id)
{
{
# 商机
# 商机
$business = Db::name('crm_business')->field(['create_user_id', 'create_time', 'update_time'])->where('business_id', $id)->find();
$business = Db::name('crm_business')->field(['create_user_id', 'create_time', 'update_time'])->where('business_id', $id)->find();
@ -630,9 +653,9 @@ class Business extends Common
return [
return [
'create_user_name' => $realname,
'create_user_name' => $realname,
'create_time' => date('Y-m-d H:i:s', $business['create_time']),
'create_time' => date('Y-m-d H:i:s', $business['create_time']),
'update_time' => date('Y-m-d H:i:s', $business['update_time']),
'update_time' => date('Y-m-d H:i:s', $business['update_time']),
'follow_time' => !empty($followTime) ? date('Y-m-d H:i:s', $followTime) : ''
'follow_time' => !empty($followTime) ? date('Y-m-d H:i:s', $followTime) : ''
];
];
}
}