diff --git a/application/crm/controller/Business.php b/application/crm/controller/Business.php index fd44a5e..d6473d6 100644 --- a/application/crm/controller/Business.php +++ b/application/crm/controller/Business.php @@ -17,7 +17,7 @@ use think\Db; class Business extends ApiCommon { use StarTrait, SearchConditionTrait; - + /** * 用于判断权限 * @permission 无限制 @@ -37,7 +37,7 @@ class Business extends ApiCommon parent::_initialize(); } } - + /** * 商机列表 * @return @@ -52,7 +52,7 @@ class Business extends ApiCommon $data = $businessModel->getDataList($param); return resultArray(['data' => $data]); } - + /** * 添加商机 * @param @@ -66,14 +66,14 @@ class Business extends ApiCommon $userInfo = $this->userInfo; $param['create_user_id'] = $userInfo['id']; $param['owner_user_id'] = $userInfo['id']; - + if ($businessModel->createData($param)) { return resultArray(['data' => '添加成功']); } else { return resultArray(['error' => $businessModel->getError()]); } } - + /** * 商机详情 * @param @@ -108,7 +108,7 @@ class Business extends ApiCommon } return resultArray(['data' => $data]); } - + /** * 编辑商机 * @param @@ -137,7 +137,7 @@ class Business extends ApiCommon return resultArray(['error' => $businessModel->getError()]); } } - + /** * 删除商机(逻辑删) * @param @@ -159,7 +159,7 @@ class Business extends ApiCommon } $delIds = []; $errorMessage = []; - + //数据权限判断 $userModel = new \app\admin\model\User(); $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'delete'); @@ -198,7 +198,7 @@ class Business extends ApiCommon $fileModel->delRFileByModule('crm_business', $delIds); //删除关联操作记录 $actionRecordModel->delDataById(['types' => 'crm_business', 'action_id' => $delIds]); - $dataInfo = $businessModel->where('business_id',['in',$delIds])->select(); + $dataInfo = $businessModel->where('business_id', ['in', $delIds])->select(); foreach ($dataInfo as $k => $v) { RecordActionLog($userInfo['id'], 'crm_business', 'delete', $v['name'], '', '', '删除了商机:' . $v['name']); } @@ -209,7 +209,7 @@ class Business extends ApiCommon return resultArray(['data' => '删除成功']); } } - + /** * 符合条件的商机状态组 * @param @@ -223,10 +223,10 @@ class Business extends ApiCommon $list = cache($key); if (!$list) { $userInfo = $this->userInfo; - $authMap = function($query) use ($userInfo){ - $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%'],'is_display'=> 1,'status'=> 1]) + $authMap = function ($query) use ($userInfo) { + $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%'], 'is_display' => 1, 'status' => 1]) ->whereOr(function ($query) use ($userInfo) { - $query->where(['structure_id' => ''])->where(['is_display'=> 1,'status'=> 1]); + $query->where(['structure_id' => ''])->where(['is_display' => 1, 'status' => 1]); }); }; $list = db('crm_business_type') @@ -237,13 +237,13 @@ class Business extends ApiCommon $list[$k]['statusList'] = $businessStatusModel->getDataList($v['type_id']); } cache($key, $list, config('business_status_cache_time')); - }else{ + } else { cache($key, NULL); } return resultArray(['data' => $list]); } - + /** * 商机转移 * @param owner_user_id 变更负责人 @@ -266,30 +266,30 @@ class Business extends ApiCommon if (!$param['business_id'] || !is_array($param['business_id'])) { return resultArray(['error' => '请选择需要转移的商机']); } - + $is_remove = $param['is_remove'] == 2 ? 2 : 1; $type = $param['type'] == 2 ? 2 : 1; - + $data = []; $data['owner_user_id'] = $param['owner_user_id']; $data['update_time'] = time(); - + $ownerUserName = $userModel->getUserNameById($param['owner_user_id']); $errorMessage = []; foreach ($param['business_id'] as $business_id) { $businessInfo = $businessModel->getDataById($business_id); - + if (!$businessInfo) { $errorMessage[] = '名称:为《' . $businessInfo['name'] . '》的商机转移失败,错误原因:数据不存在;'; continue; } //权限判断 if (!in_array($businessInfo['owner_user_id'], $authIds)) { - + $errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:无权限;'; continue; } - + //团队成员 teamUserId( $param, @@ -300,7 +300,7 @@ class Business extends ApiCommon $is_remove, 0 ); - + $resBusiness = db('crm_business')->where(['business_id' => $business_id])->update($data); if (!$resBusiness) { $errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:数据出错;'; @@ -318,11 +318,11 @@ class Business extends ApiCommon } db('crm_business')->where('business_id', $business_id)->update($businessArray); } - + //修改记录 updateActionLog($userInfo['id'], 'crm_business', $business_id, '', '', '将商机转移给:' . $ownerUserName); RecordActionLog($userInfo['id'], 'crm_business', 'transfer', $businessInfo['name'], '', '', '将商机:' . $businessInfo['name'] . '转移给:' . $ownerUserName); - + } if (!$errorMessage) { return resultArray(['data' => '转移成功']); @@ -330,7 +330,7 @@ class Business extends ApiCommon return resultArray(['error' => $errorMessage]); } } - + /** * 相关产品 * @param @@ -370,7 +370,7 @@ class Business extends ApiCommon $list['discount_rate'] = $businessInfo['discount_rate'] ?: '0.00'; return resultArray(['data' => $list]); } - + /** * 商机状态推进 * @param business_id 商机ID @@ -392,73 +392,23 @@ class Business extends ApiCommon if ($businessInfo['is_end']) { return resultArray(['error' => '已结束,不能推进']); } - //判断权限 - $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update'); - //读写权限 - $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update'); - if (!in_array($businessInfo['owner_user_id'], $auth_user_ids) && !$rwPre) { - header('Content-Type:application/json; charset=utf-8'); - exit(json_encode(['code' => 102, 'error' => '无权操作'])); - } - - $status_id = $param['status_id'] ?: $businessInfo['status_id']; - $statusInfo = db('crm_business_status')->where(['type_id' => $businessInfo['type_id'], 'status_id' => $status_id])->find(); - if (!$statusInfo && !$is_end) { - return resultArray(['error' => '参数错误']); - } + $data = []; $data['update_time'] = time(); $data['is_end'] = $is_end; $data['remark'] = $remark; -// if ($is_end) { -// $status_id = $is_end; -// } - $data['status_id'] = $status_id; - $data['status_time'] = time(); + $data['check_status'] = 1; + $data['flow_id'] = 13; + $data['check_user_id'] = $param['check_user_id']; + $data['flow_user_id'] = $param['check_user_id']; + $res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data); if (!$res) { return resultArray(['error' => '推进失败,请重试']); - } else { - # 商机变更后的名称 - $businessStatusName = Db::name('crm_business_status')->where('status_id', $param['status_id'])->value('name'); - if (empty($businessStatusName) && $is_end == 1) $businessStatusName = '赢单'; - if (empty($businessStatusName) && $is_end == 2) $businessStatusName = '输单'; - if (empty($businessStatusName) && $is_end == 3) $businessStatusName = '无效'; - # 添加活动记录 - Db::name('crm_activity')->insert([ - 'type' => 3, - 'activity_type' => 5, - 'activity_type_id' => $businessInfo['business_id'], - 'content' => '阶段变更为 ' . $businessStatusName, - 'create_user_id' => $businessInfo['owner_user_id'], - 'update_time' => time(), - 'create_time' => time(), - 'customer_ids' => ',' . $businessInfo['customer_id'] . ',' - ]); - - //推进记录添加 - $temp['status_id'] = $status_id ?: 0; - $temp['is_end'] = $is_end ?: 0; - $temp['business_id'] = $param['business_id']; - $temp['create_time'] = time(); - $temp['owner_user_id'] = $userInfo['id']; - $temp['remark'] = $param['statusRemark'] ?: ''; - Db::name('CrmBusinessLog')->insert($temp); - - # 返回商机阶段数据 - $typeId = db('crm_business')->where('business_id', $param['business_id'])->value('type_id'); - $businessStatus = db('crm_business_status')->where('type_id', $typeId)->select(); - $result = [ - 'business_id' => $param['business_id'], - 'type_id' => $typeId, - 'status_id' => $param['status_id'], - 'status_list' => $businessStatus - ]; - - return resultArray(['data' => $result]); } + return resultArray(['success' => '提交成功']); } - + /** * 商机导出 * @param @@ -476,14 +426,14 @@ class Business extends ApiCommon $param['is_excel'] = 1; $action_name = '导出选中'; } - + $excelModel = new \app\admin\model\Excel(); // 导出的字段列表 $fieldModel = new \app\admin\model\Field(); $field_list = $fieldModel->getIndexFieldConfig('crm_business', $userInfo['id']); // 文件名 $file_name = '5kcrm_business_' . date('Ymd'); - + $model = model('Business'); $temp_file = $param['temp_file']; unset($param['temp_file']); @@ -499,7 +449,7 @@ class Business extends ApiCommon return $data; }); } - + /** * 设置关注 * @@ -512,16 +462,16 @@ class Business extends ApiCommon $userId = $this->userInfo['id']; $targetId = $this->param['target_id']; $type = $this->param['type']; - + if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']); - + if (!$this->setStar($type, $userId, $targetId)) { return resultArray(['error' => '设置关注失败!']); } - + return resultArray(['data' => '设置关注成功!']); } - + /** * 系统信息 * @@ -533,14 +483,14 @@ class Business extends ApiCommon public function system() { if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']); - + $businessModel = new \app\crm\model\Business(); - + $data = $businessModel->getSystemInfo($this->param['id']); - + return resultArray(['data' => $data]); } - + /** * table栏数量统计 * @@ -552,39 +502,39 @@ class Business extends ApiCommon public function count() { if (empty($this->param['business_id'])) return resultArray(['error' => '参数错误!']); - + $businessId = $this->param['business_id']; - + $userInfo = $this->userInfo; - + # 查询联系人和商机关联数据 $contactsIds = Db::name('crm_contacts_business')->where('business_id', $businessId)->column('contacts_id'); - + # 联系人 $contactsAuth = $this->getContactsSearchWhere($userInfo['id']); $contactsCount = Db::name('crm_contacts')->whereIn('contacts_id', $contactsIds)->where($contactsAuth)->count(); - + # 合同 $contractAuth = $this->getContractSearchWhere($userInfo['id']); $contractCount = Db::name('crm_contract')->where('business_id', $businessId)->where($contractAuth)->count(); - + # 查询商机和产品的关联表 $productIds = Db::name('crm_business_product')->where('business_id', $businessId)->column('product_id'); - + # 产品 $productAuth = $this->getProductSearchWhere(); $productCount = Db::name('crm_product')->whereIn('product_id', $productIds)->whereIn('owner_user_id', $productAuth)->count(); - + # 附件 $fileCount = Db::name('crm_business_file')->alias('business')->join('__ADMIN_FILE__ file', 'file.file_id = business.file_id', 'LEFT')->where('business_id', $businessId)->count(); - + # 团队 $business = Db::name('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $businessId)->find(); $business['ro_user_id'] = explode(',', trim($business['ro_user_id'], ',')); $business['rw_user_id'] = explode(',', trim($business['rw_user_id'], ',')); $business['owner_user_id'] = [$business['owner_user_id']]; $teamCount = array_filter(array_unique(array_merge($business['ro_user_id'], $business['rw_user_id'], $business['owner_user_id']))); - + $data = [ 'contactCount' => $contactsCount, 'contractCount' => $contractCount, @@ -592,10 +542,10 @@ class Business extends ApiCommon 'memberCount' => count($teamCount), 'productCount' => $productCount ]; - + return resultArray(['data' => $data]); } - + /** * 设置首要联系人 * @@ -607,13 +557,109 @@ class Business extends ApiCommon { $businessId = $this->param['business_id']; $contactsId = $this->param['contacts_id']; - + if (empty($businessId) || empty($contactsId)) return resultArray(['error' => '参数错误!']); - + if (!Db::name('crm_business')->where('business_id', $businessId)->update(['contacts_id' => $contactsId])) { return resultArray(['error' => '操作失败!']); } - + return resultArray(['data' => '操作成功!']); } + + public function check() + { + + $param = $this->param; + $userInfo = $this->userInfo; + $userModel = new \app\admin\model\User(); +// $is_end = $param['is_end'] ?: 0; //1赢单2输单3无效 +// $remark = $param['statusRemark']; + $check_status = $param['check_status']; + if (!$param['business_id']) { + return resultArray(['error' => '参数错误']); + } + + if ($check_status == 2) { + $businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find(); +// if ($businessInfo['is_end']) { +// return resultArray(['error' => '已结束,不能推进']); +// } + + //判断权限 + $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update'); + //读写权限 + $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update'); + if (!in_array($businessInfo['owner_user_id'], $auth_user_ids) && !$rwPre) { + header('Content-Type:application/json; charset=utf-8'); + exit(json_encode(['code' => 102, 'error' => '无权操作'])); + } + + $status_id = $param['status_id'] ?: $businessInfo['status_id']; + $statusInfo = db('crm_business_status')->where(['type_id' => $businessInfo['type_id'], 'status_id' => $status_id])->find(); + if (!$statusInfo && $businessInfo['is_end']) { + return resultArray(['error' => '参数错误']); + } + $data = []; + $data['update_time'] = time(); +// $data['is_end'] = $is_end; +// $data['remark'] = $remark; + $data['check_status'] = $check_status; + $data['status_id'] = $status_id; + $data['status_time'] = time(); + $res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data); + if (!$res) { + return resultArray(['error' => '推进失败,请重试']); + } else { + # 商机变更后的名称 + $businessStatusName = Db::name('crm_business_status')->where('status_id', $param['status_id'])->value('name'); + if (empty($businessStatusName) && $businessInfo['is_end'] == 1) $businessStatusName = '赢单'; + if (empty($businessStatusName) && $businessInfo['is_end'] == 2) $businessStatusName = '输单'; + if (empty($businessStatusName) && $businessInfo['is_end'] == 3) $businessStatusName = '无效'; + # 添加活动记录 + Db::name('crm_activity')->insert([ + 'type' => 3, + 'activity_type' => 5, + 'activity_type_id' => $businessInfo['business_id'], + 'content' => '阶段变更为 ' . $businessStatusName, + 'create_user_id' => $businessInfo['owner_user_id'], + 'update_time' => time(), + 'create_time' => time(), + 'customer_ids' => ',' . $businessInfo['customer_id'] . ',' + ]); + + //推进记录添加 + $temp['status_id'] = $status_id ?: 0; + $temp['is_end'] = $businessInfo['is_end'] ?: 0; + $temp['business_id'] = $param['business_id']; + $temp['create_time'] = time(); + $temp['owner_user_id'] = $userInfo['id']; + $temp['remark'] = $param['statusRemark'] ?: ''; + Db::name('CrmBusinessLog')->insert($temp); + + # 返回商机阶段数据 + $typeId = db('crm_business')->where('business_id', $param['business_id'])->value('type_id'); + $businessStatus = db('crm_business_status')->where('type_id', $typeId)->select(); + $result = [ + 'business_id' => $param['business_id'], + 'type_id' => $typeId, + 'status_id' => $param['status_id'], + 'status_list' => $businessStatus + ]; + + return resultArray(['data' => $result]); + } + } else { + $data = []; + $data['update_time'] = time(); + $data['is_end'] = 0; + $data['remark'] = ''; + $data['check_status'] = $check_status; + $res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data); + if (!$res) { + return resultArray(['error' => '推进失败,请重试']); + } + } + } + } diff --git a/application/oa/logic/ExamineLogic.php b/application/oa/logic/ExamineLogic.php index cd66a3e..04fcd03 100644 --- a/application/oa/logic/ExamineLogic.php +++ b/application/oa/logic/ExamineLogic.php @@ -447,14 +447,15 @@ class ExamineLogic extends Common ->count(); break; case 4:// 商机 - $list = db('crm_bussiness') + $list = db('crm_business') ->alias('a') ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT') ->join('__ADMIN_EXAMINE_FLOW__ examine_flow', 'examine_flow.flow_id = a.flow_id', 'LEFT') ->where($where) + ->where('a.is_end',['in',[1,2,3]]) ->where($whereOr) ->field( - 'a.invoice_id as catagory_id ,a.invoice_apple_number as name,a.create_time,a.check_status,a.create_user_id,a.check_user_id,a.flow_user_id,user.realname,examine_flow.name as examine_name' + 'a.business_id as catagory_id ,a.name as name,a.create_time,a.check_status,a.create_user_id,a.check_user_id,a.flow_user_id,user.realname,examine_flow.name as examine_name' ) ->page($param['page'], $param['limit']) ->order('a.create_time desc') @@ -463,7 +464,7 @@ class ExamineLogic extends Common $list[$k]['create_user_info'] = $userModel->getUserById($v['create_user_id']); } - $dataCount = db('crm_invoice') + $dataCount = db('crm_business') ->alias('a') ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT') ->where($where)