| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 商机
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
- namespace app\crm\model;
-
- use app\admin\traits\FieldVerificationTrait;
- use think\Db;
- use app\admin\model\Common;
- use app\crm\model\Business as CrmBusinessModel;
-
- class Business extends Common
- {
- use FieldVerificationTrait;
-
- /**
- * 为了数据库的整洁,同时又不影响Model和Controller的名称
- * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
- */
- protected $name = 'crm_business';
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $autoWriteTimestamp = true;
-
- /**
- * [getDataList 商机list]
- *
- * @param $request
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- 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']; // 待办事项下需联系商机(逾期)
- 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']);
- $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 = [];
- //优先级:普通筛选>高级筛选>场景
- if ($requestMap['team_id']) {
- //相关团队查询
- $map = $requestMap;
- $partMap= advancedQueryFormatForTeam($requestMap,'crm_business','business_id');
- unset($map['team_id']);
- } else {
- $map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
- }
- //高级筛选
- $map = advancedQuery($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);
- }
- }
- //列表展示字段
- $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'); //部门类型
- $datetimeField = $fieldModel->getFieldByFormType('crm_business', 'datetime'); //日期时间类型
- $booleanField = $fieldModel->getFieldByFormType('crm_business', 'boolean_value'); //布尔值
- $dateIntervalField = $fieldModel->getFieldByFormType('crm_business', 'date_interval'); // 日期区间类型字段
- $positionField = $fieldModel->getFieldByFormType('crm_business', 'position'); // 地址类型字段
- $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign'); // 手写签名类型字段
- $locationField = $fieldModel->getFieldByFormType('crm_business', 'location'); // 定位类型字段
- # 处理人员和部门类型的排序报错问题(前端传来的是包含_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);
- }
- }
-
- // 待办事项下需联系商机(逾期)
- $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)->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)
- ->limit($request['offset'], $request['length'])
- ->field('business.*,customer.name as customer_name')
- ->orderRaw($order)
- ->select();
- $endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
- # 扩展数据
- $extraData = [];
- $business_id_list = !empty($list) ? array_column($list, 'business_id') : [];
- $extraList = db('crm_business_data')->whereIn('business_id', $business_id_list)->select();
- foreach ($extraList AS $key => $value) {
- $extraData[$value['business_id']][$value['field']] = $value['content'];
- }
- 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'] : '';
- foreach ($userField as $key => $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) {
- $structureNameField = !empty($v[$val]) ? db('admin_structure')->whereIn('id', stringToArray($v[$val]))->column('name') : [];
- $list[$k][$val] = implode($structureNameField, ',');
- }
- foreach ($datetimeField as $key => $val) {
- $list[$k][$val] = !empty($v[$val]) ? date('Y-m-d H:i:s', $v[$val]) : null;
- }
- foreach ($booleanField as $key => $val) {
- $list[$k][$val] = !empty($v[$val]) ? (string)$v[$val] : '0';
- }
- // 处理日期区间类型字段的格式
- foreach ($dateIntervalField AS $key => $val) {
- $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
- }
- // 处理地址类型字段的格式
- foreach ($positionField AS $key => $val) {
- $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
- }
- // 手写签名类型字段
- foreach ($handwritingField AS $key => $val) {
- $handwritingData = !empty($v[$val]) ? db('admin_file')->where('file_id', $v[$val])->value('file_path') : null;
- $list[$k][$val] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
- }
- // 定位类型字段
- foreach ($locationField AS $key => $val) {
- $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
- }
- $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;
- }
- $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
- * @author Michael_xu
- */
- public function createData($param)
- {
- // 商机扩展表数据
- $businessData = [];
-
- $fieldModel = new \app\admin\model\Field();
- $productModel = new \app\crm\model\Product();
- // 数据验证
- $validateResult = $this->fieldDataValidate($param, $this->name, $param['create_user_id']);
- if (!empty($validateResult)) {
- $this->error = $validateResult;
- return false;
- }
- if (!$param['customer_id']) {
- $this->error = '请选择相关客户';
- return false;
- }
-
- // 处理部门、员工、附件、多选类型字段
- $arrFieldAtt = $fieldModel->getArrayField('crm_business');
- foreach ($arrFieldAtt as $k => $v) {
- $param[$v] = arrayToString($param[$v]);
- }
- // 处理日期(date)类型
- $dateField = $fieldModel->getFieldByFormType('crm_business', 'date');
- if (!empty($dateField)) {
- foreach ($param AS $key => $value) {
- if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
- }
- }
- // 处理手写签名类型
- $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign');
- if (!empty($handwritingField)) {
- foreach ($param AS $key => $value) {
- if (in_array($key, $handwritingField)) {
- $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
- }
- }
- }
- // 处理地址、定位、日期区间、明细表格类型字段
- $positionField = $fieldModel->getFieldByFormType($this->name, 'position');
- $locationField = $fieldModel->getFieldByFormType($this->name, 'location');
- $dateIntervalField = $fieldModel->getFieldByFormType($this->name, 'date_interval');
- $detailTableField = $fieldModel->getFieldByFormType($this->name, 'detail_table');
- foreach ($param AS $key => $value) {
- // 处理地址类型字段数据
- if (in_array($key, $positionField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $positionNames = array_column($value, 'name');
- $param[$key] = implode(',', $positionNames);
- } else {
- $param[$key] = '';
- }
- }
- // 处理定位类型字段数据
- if (in_array($key, $locationField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = $value['address'];
- } else {
- $param[$key] = '';
- }
- }
- // 处理日期区间类型字段数据
- if (in_array($key, $dateIntervalField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = implode('_', $value);
- } else {
- $param[$key] = '';
- }
- }
- // 处理明细表格类型字段数据
- if (in_array($key, $detailTableField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = $key;
- } else {
- $param[$key] = '';
- }
- }
- }
-
- # 设置今日需联系商机
- if (!empty($param['next_time']) && $param['next_time'] >= strtotime(date('Y-m-d 00:00:00'))) $param['is_dealt'] = 0;
-
- $param['money'] = $param['money'] ?: '0.00';
- $param['discount_rate'] = $param['discount_rate'] ?: '0.00';
- if ($this->data($param)->allowField(true)->save()) {
- updateActionLog($param['create_user_id'], 'crm_business', $this->business_id, '', '', '创建了商机');
- RecordActionLog($param['create_user_id'],'crm_business','save',$param['name'],'','','新增了商机'.$param['name']);
- $business_id = $this->business_id;
- $data['business_id'] = $business_id;
- if ($param['product']) {
- //产品数据处理
- $resProduct = $productModel->createObject('crm_business', $param, $business_id);
- if ($resProduct == false) {
- $this->error = '产品添加失败';
- return false;
- }
- }
- //添加商机日志
- $data_log['business_id'] = $business_id;
- $data_log['is_end'] = 0;
- $data_log['status_id'] = $param['status_id'];
- $data_log['create_time'] = time();
- $data_log['owner_user_id'] = $param['owner_user_id'];
- $data_log['remark'] = '新建商机';
- Db::name('CrmBusinessLog')->insert($data_log);
- # 添加活动记录
- Db::name('crm_activity')->insert([
- 'type' => 2,
- 'activity_type' => 5,
- 'activity_type_id' => $data['business_id'],
- 'content' => $param['name'],
- 'create_user_id' => $param['create_user_id'],
- 'update_time' => time(),
- 'create_time' => time(),
- 'customer_ids' => ',' . $param['customer_id'] . ','
- ]);
- // 添加商机扩展数据
- array_walk($businessData, function (&$val) use ($data) {
- $val['business_id'] = $data['business_id'];
- });
- db('crm_business_data')->insertAll($businessData);
-
- return $data;
- } else {
- $this->error = '添加失败';
- return false;
- }
- }
-
- /**
- * 编辑商机主表信息
- * @param
- * @return
- * @author Michael_xu
- */
- public function updateDataById($param, $business_id = '')
- {
- // 商机扩展表数据
- $businessData = [];
-
- $productModel = new \app\crm\model\Product();
- $dataInfo = $this->getDataById($business_id);
- if (!$dataInfo) {
- $this->error = '数据不存在或已删除';
- return false;
- }
- $param['business_id'] = $business_id;
- //过滤不能修改的字段
- $unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
- foreach ($unUpdateField as $v) {
- unset($param[$v]);
- }
-
- $fieldModel = new \app\admin\model\Field();
-
- // 数据验证
- $validateResult = $this->fieldDataValidate($param, $this->name, $param['user_id'], $param['business_id']);
- if (!empty($validateResult)) {
- $this->error = $validateResult;
- return false;
- }
-
- # 商机金额小数处理
- if (!empty($param['money']) && is_numeric($param['money']) && strpos($param['money'], ".") === false) {
- $param['money'] .= '.00';
- }
-
- // 处理部门、员工、附件、多选类型字段
- $arrFieldAtt = $fieldModel->getArrayField('crm_business');
- foreach ($arrFieldAtt as $k => $v) {
- if (isset($param[$v])) $param[$v] = arrayToString($param[$v]);
- }
- // 处理日期(date)类型
- $dateField = $fieldModel->getFieldByFormType('crm_business', 'date');
- if (!empty($dateField)) {
- foreach ($param AS $key => $value) {
- if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
- }
- }
- // 处理手写签名类型
- $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign');
- if (!empty($handwritingField)) {
- foreach ($param AS $key => $value) {
- if (in_array($key, $handwritingField)) {
- $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
- }
- }
- }
- // 处理地址、定位、日期区间、明细表格类型字段
- $positionField = $fieldModel->getFieldByFormType($this->name, 'position');
- $locationField = $fieldModel->getFieldByFormType($this->name, 'location');
- $dateIntervalField = $fieldModel->getFieldByFormType($this->name, 'date_interval');
- $detailTableField = $fieldModel->getFieldByFormType($this->name, 'detail_table');
- foreach ($param AS $key => $value) {
- // 处理地址类型字段数据
- if (in_array($key, $positionField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $positionNames = array_column($value, 'name');
- $param[$key] = implode(',', $positionNames);
- } else {
- $param[$key] = '';
- }
- }
- // 处理定位类型字段数据
- if (in_array($key, $locationField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = $value['address'];
- } else {
- $param[$key] = '';
- }
- }
- // 处理日期区间类型字段数据
- if (in_array($key, $dateIntervalField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = implode('_', $value);
- } else {
- $param[$key] = '';
- }
- }
- // 处理明细表格类型字段数据
- if (in_array($key, $detailTableField)) {
- if (!empty($value)) {
- $businessData[] = [
- 'field' => $key,
- 'content' => json_encode($value, JSON_NUMERIC_CHECK),
- 'create_time' => time()
- ];
- $param[$key] = $key;
- } else {
- $param[$key] = '';
- }
- }
- }
-
- # 设置今日需联系商机
- if (!empty($param['next_time']) && $param['next_time'] >= strtotime(date('Y-m-d 00:00:00'))) $param['is_dealt'] = 0;
-
- $param['money'] = $param['money'] ?: '0.00';
- $param['discount_rate'] = $param['discount_rate'] ?: '0.00';
- //商机状态改变
- $statusInfo = db('crm_business_status')->where(['status_id' => $param['status_id']])->find();
- if ($statusInfo['type_id']) {
- $param['is_end'] = 0;
- } else {
- $param['is_end'] = $param['status_id'];
- }
- if ($this->update($param, ['business_id' => $business_id], true)) {
- $data['business_id'] = $business_id;
- //产品数据处理
- $resProduct = $productModel->createObject('crm_business', $param, $business_id);
- //修改记录
- updateActionLog($param['user_id'], 'crm_business', $business_id, $dataInfo, $param);
- RecordActionLog($param['user_id'], 'crm_business', 'update',$dataInfo['name'], $dataInfo, $param);
- // 添加商机扩展数据
- db('crm_business_data')->where('business_id', $business_id)->delete();
- array_walk($businessData, function (&$val) use ($business_id) {
- $val['business_id'] = $business_id;
- });
- db('crm_business_data')->insertAll($businessData);
-
- return $data;
- } else {
- $this->rollback();
- $this->error = '编辑失败';
- return false;
- }
- }
-
- /**
- * 商机数据
- *
- * @param string $id
- * @param int $userId
- * @return Common|array|bool|\PDOStatement|string|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getDataById($id = '', $userId = 0)
- {
- $dataInfo = db('crm_business')->where('business_id', $id)->find();
- if (!$dataInfo) {
- $this->error = '暂无此数据';
- return false;
- }
- $userModel = new \app\admin\model\User();
- $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['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['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['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'] : '';
- # 关注
- $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;
- # 首要联系人
- $primaryId = Db::name('crm_contacts')->where(['contacts_id' => $dataInfo['contacts_id']])->value('contacts_id');
- $dataInfo['contacts_id'] = !empty($primaryId) && $this->getContactsAuth($primaryId) ? $primaryId : 0;
- # 处理日期格式
- $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;
- }
- if($dataInfo['is_end']!=1){
- $dataInfo['statusRemark']=db('crm_business_log')->where(['business_id'=>$id,'is_end'=>$dataInfo['is_end']])->value('remark');
- }
- $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['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;
- // 字段授权
- if (!empty($userId)) {
- $grantData = getFieldGrantData($userId);
- $userLevel = isSuperAdministrators($userId);
- foreach ($dataInfo AS $key => $value) {
- if (!$userLevel && !empty($grantData['crm_business'])) {
- $status = getFieldGrantStatus($key, $grantData['crm_business']);
-
- # 查看权限
- if ($status['read'] == 0) unset($dataInfo[$key]);
- }
- }
- }
- return $dataInfo;
- }
-
- //根据IDs获取数组
- public function getDataByStr($idstr)
- {
- $idArr = stringToArray($idstr);
- if (!$idArr) {
- return [];
- }
- $list = Db::name('CrmBusiness')->where(['business_id' => ['in', $idArr]])->select();
- return $list;
- }
-
- /**
- * [商机漏斗]
- * @param
- * @return
- * @author Michael_xu
- */
- public function getFunnel($request)
- {
- $merge = $request['merge'] ?: 0;
- $perUserIds = $request['perUserIds'] ?: [];
- $adminModel = new \app\admin\model\Admin();
- $whereArr = $adminModel->getWhere($request, $merge, $perUserIds); //统计查询
- $userIds = $whereArr['userIds'];
- $between_time = $whereArr['between_time'];
- $where['owner_user_id'] = array('in', $userIds);
- $where['create_time'] = array('between', $between_time);
- //商机状态组
- $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;
- $statusList = db('crm_business_status')->where(['type_id' => $type_id])->select();
- $map = [];
- $map['create_time'] = $where['create_time'];
- $map['owner_user_id'] = ['in', $userIds];
- $map['type_id'] = $type_id;
-
- $sql_a = CrmBusinessModel::field([
- 'SUM(CASE WHEN is_end = 1 THEN money ELSE 0 END) AS sum_ying',
- 'SUM(CASE WHEN is_end = 2 THEN money ELSE 0 END) AS sum_shu',
- 'type_id'
- ])
- ->where($map)
- ->fetchSql()
- ->find();
- $res_a = queryCache($sql_a, 200);
- $sql = CrmBusinessModel::field([
- "status_id",
- 'COUNT(*)' => 'count',
- 'SUM(`money`)' => 'sum',
- 'type_id'
- ])
- ->where($where)
- ->whereNotIn('is_end', '1,2,3')
- ->group('status_id')
- ->fetchSql()
- ->select();
- $res = queryCache($sql, 200);
- $res = array_column($res, null, 'status_id');
-
- $sum_money = 0;
- $count = 0; # 商机数总和
- $moneyCount = 0; # 金额总和
- foreach ($statusList as $k => $v) {
- $v['count'] = $res[$v['status_id']]['count'] ?: 0;
- $v['money'] = $res[$v['status_id']]['sum'] ?: 0;
- $v['status_name'] = $v['name'];
-
- $statusList[$k] = $v;
-
- $sum_money += $v['money'];
- $moneyCount += $v['money'];
- $count += $v['count'];
- }
-
- $data['list'] = $statusList;
- $data['sum_ying'] = $res_a[0]['sum_ying'] ?: 0;
- $data['sum_shu'] = $res_a[0]['sum_shu'] ?: 0;
- $data['sum_money'] = $sum_money ?: 0;
- $data['total'] = ['name' => '合计', 'money_count' => $moneyCount, 'count' => $count];
-
- return $data ?: [];
- }
-
- /**
- * [商机转移]
- * @param ids 商机ID数组
- * @param owner_user_id 变更负责人
- * @param is_remove 1移出,2转为团队成员
- * @return
- * @author Michael_xu
- */
- public function transferDataById($ids, $owner_user_id, $type = 1, $is_remove)
- {
- $settingModel = new \app\crm\model\Setting();
- $errorMessage = [];
- foreach ($ids as $id) {
- $businessInfo = db('crm_business')->where(['business_id' => $id])->find();
- //团队成员
- $teamData = [];
- $teamData['type'] = $type; //权限 1只读2读写
- $teamData['user_id'] = [$businessInfo['owner_user_id']]; //协作人
- $teamData['types'] = 'crm_business'; //类型
- $teamData['types_id'] = $id; //类型ID
- $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
- $res = $settingModel->createTeamData($teamData);
-
- $data = [];
- $data['owner_user_id'] = $owner_user_id;
- $data['update_time'] = time();
- if (!db('crm_business')->where(['business_id' => $id])->update($data)) {
- $errorMessage[] = '商机:' . $businessInfo['name'] . '"转移失败,错误原因:数据出错;';
- continue;
- } else {
- $businessArray = [];
- $teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $id)->find();
- if (!empty($teamBusiness['ro_user_id'])) {
- $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['ro_user_id']), [$teamBusiness['owner_user_id']]));
- $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;
- }
- }
-
- /**
- * [商机统计]
- * @param
- * @return
- */
- public function getTrendql($map)
- {
- $prefix = config('database.prefix');
- $sql = "SELECT
- '{$map['type']}' AS type,
- '{$map['start_time']}' AS start_time,
- '{$map['end_time']}' AS end_time,
- IFNULL(
- (
- SELECT
- sum(money)
- FROM
- {$prefix}crm_business
- WHERE
- create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
- AND owner_user_id IN ({$map['owner_user_id']})
- ),
- 0
- ) AS business_money,
- IFNULL(
- count(business_id),
- 0
- ) AS business_num
- FROM
- {$prefix}crm_business
- WHERE
- create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
- AND owner_user_id IN ({$map['owner_user_id']})";
- return $sql;
- }
-
- /**
- * [赢单机会转化率趋势分析]
- * @param
- * @return
- */
- public function getWinSql($map)
- {
- $prefix = config('database.prefix');
- $sql = "SELECT
- '{$map['type']}' AS type,
- '{$map['start_time']}' AS start_time,
- '{$map['end_time']}' AS end_time,
- IFNULL(
- (
- SELECT
- count(business_id)
- FROM
- {$prefix}crm_business
- WHERE
- create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
- AND owner_user_id IN ({$map['owner_user_id']})
- AND is_end = 1
- ),
- 0
- ) AS business_end,
- IFNULL(
- count(business_id),
- 0
- ) AS business_num
- FROM
- {$prefix}crm_business
- WHERE
- create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
- AND owner_user_id IN ({$map['owner_user_id']})";
- return $sql;
- }
-
- /**
- * 获取系统信息
- *
- * @param $id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getSystemInfo($id)
- {
- # 商机
- $business = Db::name('crm_business')->field(['create_user_id', 'create_time', 'update_time', 'last_time'])->where('business_id', $id)->find();
- # 创建人
- $realname = Db::name('admin_user')->where('id', $business['create_user_id'])->value('realname');
-
- return [
- 'create_user_id' => $realname,
- 'create_time' => date('Y-m-d H:i:s', $business['create_time']),
- 'update_time' => date('Y-m-d H:i:s', $business['update_time']),
- 'last_time' => !empty($business['last_time']) ? date('Y-m-d H:i:s', $business['last_time']) : ''
- ];
- }
-
- /**
- * 判断联系人详情权限 todo 客户模块也在用,以后抽成一个公共的方法
- *
- * @param $contactsId
- * @return bool
- */
- private function getContactsAuth($contactsId)
- {
- $ownerUserId = db('crm_contacts')->where('contacts_id', $contactsId)->value('owner_user_id');
-
- $authUserIds = (new \app\admin\model\User())->getUserByPer('crm', 'contacts', 'read');
-
- return in_array($ownerUserId, $authUserIds);
- }
- }
|