You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/crm/controller/Business.php

848 lines
34 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | Description: 商机
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
use app\admin\model\Message;
use app\admin\model\User;
use app\crm\logic\CustomerPoolLogic;
4 years ago
use app\crm\traits\SearchConditionTrait;
4 years ago
use app\crm\traits\StarTrait;
use think\Hook;
use think\Request;
use think\Db;
class Business extends ApiCommon
{
4 years ago
use StarTrait, SearchConditionTrait;
2 years ago
4 years ago
/**
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
4 years ago
**/
4 years ago
public function _initialize()
{
$action = [
4 years ago
'permission' => [''],
'allow' => ['statuslist', 'advance', 'product', 'system', 'count', 'setprimary', 'check']
4 years ago
];
4 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
4 years ago
$a = strtolower($request->action());
4 years ago
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
4 years ago
}
2 years ago
4 years ago
/**
* 商机列表
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function index()
{
$businessModel = model('Business');
$param = $this->param;
$userInfo = $this->userInfo;
4 years ago
$param['user_id'] = $userInfo['id'];
$data = $businessModel->getDataList($param);
4 years ago
return resultArray(['data' => $data]);
}
2 years ago
4 years ago
/**
* 添加商机
4 years ago
* @param
* @return
4 years ago
* @author Michael_xu
*/
public function save()
{
$businessModel = model('Business');
$param = $this->param;
$userInfo = $this->userInfo;
$param['create_user_id'] = $userInfo['id'];
$param['owner_user_id'] = $userInfo['id'];
2 years ago
4 years ago
if ($businessModel->createData($param)) {
return resultArray(['data' => '添加成功']);
} else {
return resultArray(['error' => $businessModel->getError()]);
}
}
2 years ago
4 years ago
/**
* 商机详情
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function read()
{
$businessModel = model('Business');
$businessStatusModel = model('BusinessStatus');
$userModel = new \app\admin\model\User();
$param = $this->param;
$userInfo = $this->userInfo;
$data = $businessModel->getDataById($param['id'], $userInfo['id']);
//判断权限
$auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
//读权限
$roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
4 years ago
$rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre && !$roPre) {
4 years ago
$authData['dataAuth'] = (int)0;
4 years ago
return resultArray(['data' => $authData]);
4 years ago
}
4 years ago
//商机状态组
$data['status_list'] = $businessStatusModel->getDataById($data['type_id']);
$data['lose_reason'] = Db::name('CrmBusinessLog')
->where(['business_id' => $data['business_id']])
->order(['id' => 'DESC'])
->value('remark');
if (!$data) {
return resultArray(['error' => $businessModel->getError()]);
}
return resultArray(['data' => $data]);
}
2 years ago
4 years ago
/**
* 编辑商机
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function update()
4 years ago
{
4 years ago
$businessModel = model('Business');
$userModel = new \app\admin\model\User();
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
//判断权限
$data = $businessModel->getDataById($param['id']);
$auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
//读写权限
4 years ago
$rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre) {
4 years ago
header('Content-Type:application/json; charset=utf-8');
4 years ago
exit(json_encode(['code' => 102, 'error' => '无权操作']));
}
4 years ago
if ($businessModel->updateDataById($param, $param['id'])) {
return resultArray(['data' => '编辑成功']);
} else {
return resultArray(['error' => $businessModel->getError()]);
4 years ago
}
4 years ago
}
2 years ago
4 years ago
/**
* 删除商机(逻辑删)
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function delete()
{
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
4 years ago
$businessModel = model('Business');
$recordModel = new \app\admin\model\Record();
$fileModel = new \app\admin\model\File();
4 years ago
$actionRecordModel = new \app\admin\model\ActionRecord();
4 years ago
if (!is_array($param['id'])) {
$business_id[] = $param['id'];
} else {
$business_id = $param['id'];
}
$delIds = [];
$errorMessage = [];
2 years ago
4 years ago
//数据权限判断
$userModel = new \app\admin\model\User();
$auth_user_ids = $userModel->getUserByPer('crm', 'business', 'delete');
4 years ago
foreach ($business_id as $k => $v) {
4 years ago
$isDel = true;
//数据详情
$data = $businessModel->getDataById($v);
if (!$data) {
$isDel = false;
4 years ago
$errorMessage[] = 'id为' . $v . '的商机删除失败,错误原因:' . $businessModel->getError();
4 years ago
}
4 years ago
if (!in_array($data['owner_user_id'], $auth_user_ids)) {
4 years ago
$isDel = false;
4 years ago
$errorMessage[] = '名称为' . $data['name'] . '的商机删除失败,错误原因:无权操作';
4 years ago
}
if ($isDel) {
4 years ago
if (db('crm_contract')->where(['business_id' => $v, 'check_status' => ['in', '0,1,2']])->value('contract_id')) {
4 years ago
$isDel = false;
4 years ago
$errorMessage[] = '名称为' . $data['name'] . '的商机删除失败,错误原因:商机下关联的有合同,无法删除!';
}
4 years ago
}
if ($isDel) {
$delIds[] = $v;
4 years ago
}
4 years ago
}
if ($delIds) {
$data = $businessModel->delDatas($delIds);
if (!$data) {
return resultArray(['error' => $businessModel->getError()]);
}
3 years ago
// 删除客户扩展数据
db('crm_business_data')->whereIn('business_id', $delIds)->delete();
4 years ago
//删除跟进记录
4 years ago
$recordModel->delDataByTypes(5, $delIds);
4 years ago
//删除关联附件
4 years ago
$fileModel->delRFileByModule('crm_business', $delIds);
4 years ago
//删除关联操作记录
4 years ago
$actionRecordModel->delDataById(['types' => 'crm_business', 'action_id' => $delIds]);
2 years ago
$dataInfo = $businessModel->where('business_id', ['in', $delIds])->select();
4 years ago
foreach ($dataInfo as $k => $v) {
RecordActionLog($userInfo['id'], 'crm_business', 'delete', $v['name'], '', '', '删除了商机:' . $v['name']);
}
4 years ago
}
if ($errorMessage) {
return resultArray(['error' => $errorMessage]);
} else {
return resultArray(['data' => '删除成功']);
4 years ago
}
4 years ago
}
2 years ago
4 years ago
/**
* 符合条件的商机状态组
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function statusList()
{
$businessStatusModel = model('BusinessStatus');
4 years ago
$key = 'BI_queryCache_StatusList_Data';
4 years ago
$list = cache($key);
if (!$list) {
$userInfo = $this->userInfo;
2 years ago
$authMap = function ($query) use ($userInfo) {
$query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%'], 'is_display' => 1, 'status' => 1])
->whereOr(function ($query) use ($userInfo) {
2 years ago
$query->where(['structure_id' => ''])->where(['is_display' => 1, 'status' => 1]);
});
};
4 years ago
$list = db('crm_business_type')
4 years ago
->field(['name', 'status', 'structure_id', 'type_id'])
->where($authMap)
4 years ago
->select();
foreach ($list as $k => $v) {
4 years ago
$list[$k]['statusList'] = $businessStatusModel->getDataList($v['type_id']);
}
4 years ago
cache($key, $list, config('business_status_cache_time'));
2 years ago
} else {
3 years ago
cache($key, NULL);
4 years ago
}
4 years ago
return resultArray(['data' => $list]);
4 years ago
}
2 years ago
4 years ago
/**
* 商机转移
* @param owner_user_id 变更负责人
* @param is_remove 1移出2转为团队成员
* @param type 权限 1只读2读写
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function transfer()
{
$param = $this->param;
$userInfo = $this->userInfo;
$businessModel = model('Business');
$settingModel = model('Setting');
$userModel = new \app\admin\model\User();
$authIds = $userModel->getUserByPer(); //权限范围的user_id
if (!$param['owner_user_id']) {
return resultArray(['error' => '变更负责人不能为空']);
}
if (!$param['business_id'] || !is_array($param['business_id'])) {
4 years ago
return resultArray(['error' => '请选择需要转移的商机']);
4 years ago
}
2 years ago
4 years ago
$is_remove = $param['is_remove'] == 2 ? 2 : 1;
$type = $param['type'] == 2 ? 2 : 1;
2 years ago
4 years ago
$data = [];
$data['owner_user_id'] = $param['owner_user_id'];
$data['update_time'] = time();
2 years ago
4 years ago
$ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
$errorMessage = [];
foreach ($param['business_id'] as $business_id) {
$businessInfo = $businessModel->getDataById($business_id);
2 years ago
4 years ago
if (!$businessInfo) {
4 years ago
$errorMessage[] = '名称:为《' . $businessInfo['name'] . '》的商机转移失败,错误原因:数据不存在;';
4 years ago
continue;
}
//权限判断
4 years ago
if (!in_array($businessInfo['owner_user_id'], $authIds)) {
2 years ago
4 years ago
$errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:无权限;';
4 years ago
continue;
}
2 years ago
4 years ago
//团队成员
teamUserId(
3 years ago
$param,
4 years ago
'crm_business',
$business_id,
$type,
[$businessInfo['owner_user_id']],
$is_remove,
0
);
2 years ago
4 years ago
$resBusiness = db('crm_business')->where(['business_id' => $business_id])->update($data);
if (!$resBusiness) {
4 years ago
$errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:数据出错;';
4 years ago
continue;
} else {
$businessArray = [];
$teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $business_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', $business_id)->update($businessArray);
}
2 years ago
4 years ago
//修改记录
4 years ago
updateActionLog($userInfo['id'], 'crm_business', $business_id, '', '', '将商机转移给:' . $ownerUserName);
RecordActionLog($userInfo['id'], 'crm_business', 'transfer', $businessInfo['name'], '', '', '将商机:' . $businessInfo['name'] . '转移给:' . $ownerUserName);
2 years ago
4 years ago
}
if (!$errorMessage) {
return resultArray(['data' => '转移成功']);
} else {
return resultArray(['error' => $errorMessage]);
}
4 years ago
}
2 years ago
4 years ago
/**
* 相关产品
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function product()
{
$productModel = model('Product');
$userModel = new \app\admin\model\User();
$param = $this->param;
$userInfo = $this->userInfo;
if (!$param['business_id']) {
return resultArray(['error' => '参数错误']);
}
4 years ago
$businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
4 years ago
//判断权限
$auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
//读写权限
$roPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'read');
$rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update');
4 years ago
if (!in_array($businessInfo['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
4 years ago
header('Content-Type:application/json; charset=utf-8');
4 years ago
exit(json_encode(['code' => 102, 'error' => '无权操作']));
4 years ago
}
$dataList = db('crm_business_product')->where(['business_id' => $param['business_id']])->select();
4 years ago
foreach ($dataList as $k => $v) {
4 years ago
$where = [];
$where['product_id'] = $v['product_id'];
$productInfo = db('crm_product')->where($where)->field('name,category_id')->find();
$category_name = db('crm_product_category')->where(['category_id' => $productInfo['category_id']])->value('name');
4 years ago
$dataList[$k]['name'] = $productInfo['name'] ?: '';
$dataList[$k]['category_id_info'] = $category_name ?: '';
4 years ago
}
4 years ago
$list['list'] = $dataList ?: [];
$list['total_price'] = $businessInfo['total_price'] ?: '0.00';
$list['discount_rate'] = $businessInfo['discount_rate'] ?: '0.00';
4 years ago
return resultArray(['data' => $list]);
4 years ago
}
2 years ago
4 years ago
/**
* 商机审批提交
4 years ago
* @param business_id 商机ID
* @param status_id 推进商机状态ID
* @return
4 years ago
* @author Michael_xu
*/
4 years ago
public function advance()
{
$param = $this->param;
$examineStepModel = new \app\admin\model\ExamineStep();
$examineStatus = $param['examineStatus'] ?? 1; // 审批是否停用
4 years ago
$userInfo = $this->userInfo;
$userModel = new \app\admin\model\User();
4 years ago
$is_end = $param['is_end'] ?: 0; //1赢单2输单3无效
$remark = $param['statusRemark'];
unset($param['examineStatus']);
4 years ago
if (!$param['business_id']) {
return resultArray(['error' => '参数错误']);
}
$businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
if ($businessInfo['is_end']) {
return resultArray(['error' => '已结束,请勿重复提交']);
}
if ($businessInfo['flow_id'] && [$businessInfo]['check_status'] != 0) {
return resultArray(['error' => '已有审批流程,请勿重复提交']);
4 years ago
}
2 years ago
4 years ago
$data = [];
$data['submit_end_status'] = $is_end;
$data['submit_remark'] = $remark;
$data['check_status'] = 0;
if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
// 审核判断(是否有符合条件的审批流)
$examineFlowModel = new \app\admin\model\ExamineFlow();
2 years ago
if (!$examineFlowModel->checkExamine($userInfo['id'], 'crm_business')) {
2 years ago
return resultArray(['error' => '暂无审批人,无法创建']);
}
//添加审批相关信息
2 years ago
$examineFlowData = $examineFlowModel->getFlowByTypes($userInfo['id'], 'crm_business');
if (!$examineFlowData) {
2 years ago
return resultArray(['error' => '无可用审批流,请联系管理员']);
}
$data['flow_id'] = $examineFlowData['flow_id'];
//获取审批人信息
if ($examineFlowData['config'] == 1) {
//固定审批流
2 years ago
$nextStepData = $examineStepModel->nextStepUser($userInfo['id'], $examineFlowData['flow_id'], 'crm_business', 0, 0, 0);
$next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
$check_user_id = $next_user_ids ?: [];
$data['order_id'] = 1;
} else {
$check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
}
if (!$check_user_id) {
2 years ago
return resultArray(['error' => '无可用审批人,请联系管理员']);
}
$data['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
} else {
# 审批流停用,将状态改为审核通过
// $param['check_status'] = 2;
2 years ago
# 审批流停用,将状态改为正常 zjf 20210727 默认值为7
$data['check_status'] = 7;
}
4 years ago
$res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data);
if (!$res) {
return resultArray(['error' => '提交失败,请重试']);
4 years ago
}
//站内信
$send_user_id = stringToArray($param['check_user_id']);
if ($send_user_id && empty($param['check_status'])) {
(new Message())->send(
Message::CUSTOMER_CHECK_TO_DO,
[
'title' => $param['name'],
'action_id' => $param['business_id'],
],
$send_user_id
);
}
# 创建待办事项的关联数据
$checkUserIds = db('crm_business')->where('business_id', $param['business_id'])->value('check_user_id');
$checkUserIdArray = stringToArray($checkUserIds);
$dealtData = [];
foreach ($checkUserIdArray as $kk => $vv) {
$dealtData[] = [
'types' => 'crm_business',
'types_id' => $param['business_id'],
'user_id' => $vv
];
}
if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
2 years ago
return resultArray(['success' => '提交成功']);
4 years ago
}
2 years ago
4 years ago
/**
* 商机导出
4 years ago
* @param
4 years ago
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
public function excelExport()
{
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
4 years ago
$action_name = '导出全部';
4 years ago
if ($param['business_id']) {
4 years ago
$param['business_id'] = ['condition' => 'in', 'value' => $param['business_id'], 'form_type' => 'text', 'name' => ''];
$param['is_excel'] = 1;
$action_name = '导出选中';
}
2 years ago
4 years ago
$excelModel = new \app\admin\model\Excel();
// 导出的字段列表
$fieldModel = new \app\admin\model\Field();
$field_list = $fieldModel->getIndexFieldConfig('crm_business', $userInfo['id']);
// 文件名
4 years ago
$file_name = '5kcrm_business_' . date('Ymd');
2 years ago
4 years ago
$model = model('Business');
$temp_file = $param['temp_file'];
unset($param['temp_file']);
$page = $param['page'] ?: 1;
unset($param['page']);
unset($param['export_queue_index']);
4 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出商机');
return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
4 years ago
$param['page'] = $page;
$param['limit'] = $limit;
$data = $model->getDataList($param);
$data['list'] = $model->exportHandle($data['list'], $field_list, 'business');
return $data;
});
}
2 years ago
4 years ago
/**
* 设置关注
*
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function star()
{
4 years ago
$userId = $this->userInfo['id'];
4 years ago
$targetId = $this->param['target_id'];
4 years ago
$type = $this->param['type'];
2 years ago
4 years ago
if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
2 years ago
4 years ago
if (!$this->setStar($type, $userId, $targetId)) {
return resultArray(['error' => '设置关注失败!']);
}
2 years ago
4 years ago
return resultArray(['data' => '设置关注成功!']);
}
2 years ago
4 years ago
/**
* 系统信息
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function system()
{
if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
2 years ago
4 years ago
$businessModel = new \app\crm\model\Business();
2 years ago
4 years ago
$data = $businessModel->getSystemInfo($this->param['id']);
2 years ago
4 years ago
return resultArray(['data' => $data]);
}
2 years ago
4 years ago
/**
* table栏数量统计
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function count()
{
if (empty($this->param['business_id'])) return resultArray(['error' => '参数错误!']);
2 years ago
4 years ago
$businessId = $this->param['business_id'];
2 years ago
4 years ago
$userInfo = $this->userInfo;
2 years ago
4 years ago
# 查询联系人和商机关联数据
$contactsIds = Db::name('crm_contacts_business')->where('business_id', $businessId)->column('contacts_id');
2 years ago
4 years ago
# 联系人
4 years ago
$contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
$contactsCount = Db::name('crm_contacts')->whereIn('contacts_id', $contactsIds)->where($contactsAuth)->count();
2 years ago
4 years ago
# 合同
4 years ago
$contractAuth = $this->getContractSearchWhere($userInfo['id']);
$contractCount = Db::name('crm_contract')->where('business_id', $businessId)->where($contractAuth)->count();
2 years ago
4 years ago
# 查询商机和产品的关联表
$productIds = Db::name('crm_business_product')->where('business_id', $businessId)->column('product_id');
2 years ago
4 years ago
# 产品
4 years ago
$productAuth = $this->getProductSearchWhere();
$productCount = Db::name('crm_product')->whereIn('product_id', $productIds)->whereIn('owner_user_id', $productAuth)->count();
2 years ago
4 years ago
# 附件
$fileCount = Db::name('crm_business_file')->alias('business')->join('__ADMIN_FILE__ file', 'file.file_id = business.file_id', 'LEFT')->where('business_id', $businessId)->count();
2 years ago
4 years ago
# 团队
$business = Db::name('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $businessId)->find();
4 years ago
$business['ro_user_id'] = explode(',', trim($business['ro_user_id'], ','));
$business['rw_user_id'] = explode(',', trim($business['rw_user_id'], ','));
4 years ago
$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'])));
2 years ago
4 years ago
$data = [
4 years ago
'contactCount' => $contactsCount,
4 years ago
'contractCount' => $contractCount,
4 years ago
'fileCount' => $fileCount,
'memberCount' => count($teamCount),
'productCount' => $productCount
4 years ago
];
2 years ago
4 years ago
return resultArray(['data' => $data]);
}
2 years ago
4 years ago
/**
* 设置首要联系人
*
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function setPrimary()
{
$businessId = $this->param['business_id'];
$contactsId = $this->param['contacts_id'];
2 years ago
4 years ago
if (empty($businessId) || empty($contactsId)) return resultArray(['error' => '参数错误!']);
2 years ago
4 years ago
if (!Db::name('crm_business')->where('business_id', $businessId)->update(['contacts_id' => $contactsId])) {
return resultArray(['error' => '操作失败!']);
}
2 years ago
4 years ago
return resultArray(['data' => '操作成功!']);
}
2 years ago
public function check()
{
$param = $this->param;
$userInfo = $this->userInfo;
$user_id = $userInfo['id'];
$businessModel = model('Business');
$examineStepModel = new \app\admin\model\ExamineStep();
$examineRecordModel = new \app\admin\model\ExamineRecord();
$examineFlowModel = new \app\admin\model\ExamineFlow();
$customerModel = model('Customer');
$businessCheckData = [];
$businessCheckData['update_time'] = time();
$businessCheckData['check_status'] = 1; //0待审核1审核通中2审核通过3审核未通过
//权限判断
if (!$examineStepModel->checkExamine($user_id, 'crm_business', $param['id'])) {
return resultArray(['error' => $examineStepModel->getError()]);
2 years ago
}
//审批主体详情
$dataInfo = $businessModel->getDataById($param['id']);
$flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
$is_end = 0; // 1审批结束
$status = $param['status'] ? 1 : 0; //1通过0驳回
$checkData = [];
$checkData['check_user_id'] = $user_id;
$checkData['types'] = 'crm_business';
$checkData['types_id'] = $param['id'];
$checkData['check_time'] = time();
$checkData['content'] = $param['content'];
$checkData['flow_id'] = $dataInfo['flow_id'];
$checkData['order_id'] = $dataInfo['order_id'] ?: 1;
$checkData['status'] = $status;
if ($status == 1) {
if ($flowInfo['config'] == 1) {
//固定流程
//获取下一审批信息
$nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_business', $param['id'], $dataInfo['order_id'], $user_id);
$next_user_ids = $nextStepData['next_user_ids'] ?: [];
$businessCheckData['order_id'] = $nextStepData['order_id'] ?: '';
if (!$next_user_ids) {
$is_end = 1;
//审批结束
$checkData['check_status'] = !empty($status) ? 2 : 3;
$businessCheckData['check_user_id'] = '';
} else {
//修改主体相关审批信息
$businessCheckData['check_user_id'] = arrayToString($next_user_ids);
}
} else {
//自选流程
$is_end = $param['is_end'] ? 1 : '';
$check_user_id = $param['check_user_id'] ?: '';
if ($is_end !== 1 && empty($check_user_id)) {
return resultArray(['error' => '请选择下一审批人']);
}
$businessCheckData['check_user_id'] = arrayToString($param['check_user_id']);
2 years ago
}
if ($is_end == 1) {
$checkData['check_status'] = !empty($status) ? 2 : 3;
$businessCheckData['check_user_id'] = '';
$businessCheckData['check_status'] = 2;
2 years ago
}
} else {
//审批驳回
$is_end = 1;
$businessCheckData['check_status'] = 3;
//将审批记录至为无效
// $examineRecordModel->setEnd(['types' => 'crm_contract','types_id' => $param['id']]);
}
//已审批人ID
$businessCheckData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']), [$user_id])) : arrayToString([$user_id]);
2 years ago
$resBusiness = db('crm_business')->where(['business_id' => $param['id']])->update($businessCheckData);
if ($resBusiness) {
//审批记录
$resRecord = $examineRecordModel->createData($checkData);
//审核通过,修改相关信息
if ($is_end == 1 && !empty($status)) {
// 审批通过消息告知负责人
(new Message())->send(
Message::BUSINESS_ADVANCE_CHECK_PASS,
[
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
$dataInfo['owner_user_id']
);
$businessData = [];
$businessData['business_id'] = $dataInfo['business_id'];
$businessData['is_end'] = $dataInfo['submit_end_status'];
$businessData['status_id'] = $dataInfo['status_id'];
$businessData['statusRemark'] = $dataInfo['submit_remark'];
$result = $this->advanceAfterCheck($businessData);
2 years ago
if (!empty($result)) return $result;
2 years ago
} else {
if ($status) {
//发送站内信
// 通过未完成,发送消息给
(new Message())->send(
Message::BUSINESS_ADVANCE_CHECK_TO_DO,
[
'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
stringToArray($businessCheckData['check_user_id'])
);
} else {
(new Message())->send(
Message::BUSINESS_ADVANCE_CHECK_REJECT,
[
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
$dataInfo['owner_user_id']
);
}
2 years ago
}
2 years ago
return resultArray(['success' => '审批成功']);
2 years ago
} else {
return resultArray(['error' => '审批失败,请重试!']);
}
}
/**
* 审核后商机状态推进
* @param business_id 商机ID
* @param status_id 推进商机状态ID
* @return
* @author Michael_xu
*/
private function advanceAfterCheck($param)
{
$userInfo = $this->userInfo;
$userModel = new \app\admin\model\User();
$is_end = $param['is_end'] ?: 0; //1赢单2输单3无效
if (!$param['business_id']) {
2 years ago
return ['error' => '参数错误'];
}
$businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
if ($businessInfo['is_end']) {
2 years ago
return ['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) {
2 years ago
return ['error' => '参数错误'];
}
$data = [];
$data['update_time'] = time();
$data['is_end'] = $is_end;
// if ($is_end) {
// $status_id = $is_end;
// }
$data['status_id'] = $status_id;
$data['status_time'] = time();
2 years ago
$data['remark'] = $param['submit_remark'];
$res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data);
if (!$res) {
2 years ago
return ['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
];
2 years ago
return '';
2 years ago
}
}
4 years ago
}