123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商机
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\crm\traits\SearchConditionTrait;
  10. use app\crm\traits\StarTrait;
  11. use think\Hook;
  12. use think\Request;
  13. use think\Db;
  14. class Business extends ApiCommon
  15. {
  16. use StarTrait, SearchConditionTrait;
  17. /**
  18. * 用于判断权限
  19. * @permission 无限制
  20. * @allow 登录用户可访问
  21. * @other 其他根据系统设置
  22. **/
  23. public function _initialize()
  24. {
  25. $action = [
  26. 'permission'=>[''],
  27. 'allow'=>['statuslist','advance','product','system','count','setprimary']
  28. ];
  29. Hook::listen('check_auth',$action);
  30. $request = Request::instance();
  31. $a = strtolower($request->action());
  32. if (!in_array($a, $action['permission'])) {
  33. parent::_initialize();
  34. }
  35. }
  36. /**
  37. * 商机列表
  38. * @author Michael_xu
  39. * @return
  40. */
  41. public function index()
  42. {
  43. $businessModel = model('Business');
  44. $param = $this->param;
  45. $userInfo = $this->userInfo;
  46. $param['user_id'] = $userInfo['id'];
  47. $data = $businessModel->getDataList($param);
  48. return resultArray(['data' => $data]);
  49. }
  50. /**
  51. * 添加商机
  52. * @author Michael_xu
  53. * @param
  54. * @return
  55. */
  56. public function save()
  57. {
  58. $businessModel = model('Business');
  59. $param = $this->param;
  60. $userInfo = $this->userInfo;
  61. $param['create_user_id'] = $userInfo['id'];
  62. $param['owner_user_id'] = $userInfo['id'];
  63. if ($businessModel->createData($param)) {
  64. return resultArray(['data' => '添加成功']);
  65. } else {
  66. return resultArray(['error' => $businessModel->getError()]);
  67. }
  68. }
  69. /**
  70. * 商机详情
  71. * @author Michael_xu
  72. * @param
  73. * @return
  74. */
  75. public function read()
  76. {
  77. $businessModel = model('Business');
  78. $businessStatusModel = model('BusinessStatus');
  79. $userModel = new \app\admin\model\User();
  80. $param = $this->param;
  81. $userInfo = $this->userInfo;
  82. $data = $businessModel->getDataById($param['id'], $userInfo['id']);
  83. //判断权限
  84. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
  85. //读权限
  86. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  87. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  88. if (!in_array($data['owner_user_id'],$auth_user_ids) && !$rwPre && !$roPre) {
  89. $authData['dataAuth'] = (int)0;
  90. return resultArray(['data' => $authData]);
  91. }
  92. //商机状态组
  93. $data['status_list'] = $businessStatusModel->getDataById($data['type_id']);
  94. $data['lose_reason'] = Db::name('CrmBusinessLog')
  95. ->where(['business_id' => $data['business_id']])
  96. ->order(['id' => 'DESC'])
  97. ->value('remark');
  98. if (!$data) {
  99. return resultArray(['error' => $businessModel->getError()]);
  100. }
  101. return resultArray(['data' => $data]);
  102. }
  103. /**
  104. * 编辑商机
  105. * @author Michael_xu
  106. * @param
  107. * @return
  108. */
  109. public function update()
  110. {
  111. $businessModel = model('Business');
  112. $userModel = new \app\admin\model\User();
  113. $param = $this->param;
  114. $userInfo = $this->userInfo;
  115. $param['user_id'] = $userInfo['id'];
  116. //判断权限
  117. $data = $businessModel->getDataById($param['id']);
  118. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  119. //读写权限
  120. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  121. if (!in_array($data['owner_user_id'],$auth_user_ids) && !$rwPre) {
  122. header('Content-Type:application/json; charset=utf-8');
  123. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  124. }
  125. if ($businessModel->updateDataById($param, $param['id'])) {
  126. return resultArray(['data' => '编辑成功']);
  127. } else {
  128. return resultArray(['error' => $businessModel->getError()]);
  129. }
  130. }
  131. /**
  132. * 删除商机(逻辑删)
  133. * @author Michael_xu
  134. * @param
  135. * @return
  136. */
  137. public function delete()
  138. {
  139. $param = $this->param;
  140. $businessModel = model('Business');
  141. $recordModel = new \app\admin\model\Record();
  142. $fileModel = new \app\admin\model\File();
  143. $actionRecordModel = new \app\admin\model\ActionRecord();
  144. if (!is_array($param['id'])) {
  145. $business_id[] = $param['id'];
  146. } else {
  147. $business_id = $param['id'];
  148. }
  149. $delIds = [];
  150. $errorMessage = [];
  151. //数据权限判断
  152. $userModel = new \app\admin\model\User();
  153. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'delete');
  154. foreach ($business_id as $k=>$v) {
  155. $isDel = true;
  156. //数据详情
  157. $data = $businessModel->getDataById($v);
  158. if (!$data) {
  159. $isDel = false;
  160. $errorMessage[] = 'id为'.$v.'的商机删除失败,错误原因:'.$businessModel->getError();
  161. }
  162. if (!in_array($data['owner_user_id'],$auth_user_ids)) {
  163. $isDel = false;
  164. $errorMessage[] = '名称为'.$data['name'].'的商机删除失败,错误原因:无权操作';
  165. }
  166. if ($isDel) {
  167. if (db('crm_contract')->where(['business_id'=> $v, 'check_status' => ['in', '0,1,2']])->value('contract_id')) {
  168. $isDel = false;
  169. $errorMessage[] = '名称为'.$data['name'].'的商机删除失败,错误原因:商机下关联的有合同,无法删除!';
  170. }
  171. }
  172. if ($isDel) {
  173. $delIds[] = $v;
  174. }
  175. }
  176. if ($delIds) {
  177. $data = $businessModel->delDatas($delIds);
  178. if (!$data) {
  179. return resultArray(['error' => $businessModel->getError()]);
  180. }
  181. //删除跟进记录
  182. $recordModel->delDataByTypes(5,$delIds);
  183. //删除关联附件
  184. $fileModel->delRFileByModule('crm_business',$delIds);
  185. //删除关联操作记录
  186. $actionRecordModel->delDataById(['types'=>'crm_business','action_id'=>$delIds]);
  187. actionLog($delIds,'','','');
  188. }
  189. if ($errorMessage) {
  190. return resultArray(['error' => $errorMessage]);
  191. } else {
  192. return resultArray(['data' => '删除成功']);
  193. }
  194. }
  195. /**
  196. * 符合条件的商机状态组
  197. * @author Michael_xu
  198. * @param
  199. * @return
  200. */
  201. public function statusList()
  202. {
  203. $businessStatusModel = model('BusinessStatus');
  204. $key = 'BI_queryCache_StatusList_Data';
  205. $list = cache($key);
  206. if (!$list) {
  207. $userInfo = $this->userInfo;
  208. $list = db('crm_business_type')
  209. ->field(['name', 'status', 'structure_id', 'type_id'])
  210. ->where(['structure_id' => ['like','%,'.$userInfo['structure_id'].',%'],'status' => 1])
  211. ->where('is_display', 1)
  212. ->whereOr('structure_id','')
  213. ->select();
  214. foreach ($list as $k=>$v) {
  215. $list[$k]['statusList'] = $businessStatusModel->getDataList($v['type_id']);
  216. }
  217. cache($key, $list, config('business_status_cache_time'));
  218. }
  219. return resultArray(['data' => $list]);
  220. }
  221. /**
  222. * 商机转移
  223. * @author Michael_xu
  224. * @param owner_user_id 变更负责人
  225. * @param is_remove 1移出,2转为团队成员
  226. * @param type 权限 1只读2读写
  227. * @return
  228. */
  229. public function transfer()
  230. {
  231. $param = $this->param;
  232. $userInfo = $this->userInfo;
  233. $businessModel = model('Business');
  234. $settingModel = model('Setting');
  235. $userModel = new \app\admin\model\User();
  236. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  237. if (!$param['owner_user_id']) {
  238. return resultArray(['error' => '变更负责人不能为空']);
  239. }
  240. if (!$param['business_id'] || !is_array($param['business_id'])) {
  241. return resultArray(['error' => '请选择需要转移的商机']);
  242. }
  243. $is_remove = $param['is_remove'] == 2 ? 2 : 1;
  244. $type = $param['type'] == 2 ? 2 : 1;
  245. $data = [];
  246. $data['owner_user_id'] = $param['owner_user_id'];
  247. $data['update_time'] = time();
  248. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  249. $errorMessage = [];
  250. foreach ($param['business_id'] as $business_id) {
  251. $businessInfo = $businessModel->getDataById($business_id);
  252. if (!$businessInfo) {
  253. $errorMessage[] = '名称:为《'.$businessInfo['name'].'》的商机转移失败,错误原因:数据不存在;';
  254. continue;
  255. }
  256. //权限判断
  257. if (!in_array($businessInfo['owner_user_id'],$authIds)) {
  258. $errorMessage[] = $businessInfo['name'].'"转移失败,错误原因:无权限;';
  259. continue;
  260. }
  261. //团队成员
  262. teamUserId(
  263. 'crm_business',
  264. $business_id,
  265. $type,
  266. [$businessInfo['owner_user_id']],
  267. $is_remove,
  268. 0
  269. );
  270. $resBusiness = db('crm_business')->where(['business_id' => $business_id])->update($data);
  271. if (!$resBusiness) {
  272. $errorMessage[] = $businessInfo['name'].'"转移失败,错误原因:数据出错;';
  273. continue;
  274. } else {
  275. $businessArray = [];
  276. $teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $business_id)->find();
  277. if (!empty($teamBusiness['ro_user_id'])) {
  278. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['ro_user_id']), [$teamBusiness['owner_user_id']]));
  279. $businessArray['ro_user_id'] = $businessRo;
  280. }
  281. if (!empty($teamBusiness['rw_user_id'])) {
  282. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['rw_user_id']), [$teamBusiness['owner_user_id']]));
  283. $businessArray['rw_user_id'] = $businessRo;
  284. }
  285. db('crm_business')->where('business_id', $business_id)->update($businessArray);
  286. }
  287. //修改记录
  288. updateActionLog($userInfo['id'], 'crm_business', $business_id, '', '', '将商机转移给:'.$ownerUserName);
  289. }
  290. if (!$errorMessage) {
  291. return resultArray(['data' => '转移成功']);
  292. } else {
  293. return resultArray(['error' => $errorMessage]);
  294. }
  295. }
  296. /**
  297. * 相关产品
  298. * @author Michael_xu
  299. * @param
  300. * @return
  301. */
  302. public function product()
  303. {
  304. $productModel = model('Product');
  305. $userModel = new \app\admin\model\User();
  306. $param = $this->param;
  307. $userInfo = $this->userInfo;
  308. if (!$param['business_id']) {
  309. return resultArray(['error' => '参数错误']);
  310. }
  311. $businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
  312. //判断权限
  313. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
  314. //读写权限
  315. $roPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'read');
  316. $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update');
  317. if (!in_array($businessInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  318. header('Content-Type:application/json; charset=utf-8');
  319. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  320. }
  321. $dataList = db('crm_business_product')->where(['business_id' => $param['business_id']])->select();
  322. foreach ($dataList as $k=>$v) {
  323. $where = [];
  324. $where['product_id'] = $v['product_id'];
  325. $productInfo = db('crm_product')->where($where)->field('name,category_id')->find();
  326. $category_name = db('crm_product_category')->where(['category_id' => $productInfo['category_id']])->value('name');
  327. $dataList[$k]['name'] = $productInfo['name'] ? : '';
  328. $dataList[$k]['category_id_info'] = $category_name ? : '';
  329. }
  330. $list['list'] = $dataList ? : [];
  331. $list['total_price'] = $businessInfo['total_price'] ? : '0.00';
  332. $list['discount_rate'] = $businessInfo['discount_rate'] ? : '0.00';
  333. return resultArray(['data' => $list]);
  334. }
  335. /**
  336. * 商机状态推进
  337. * @author Michael_xu
  338. * @param business_id 商机ID
  339. * @param status_id 推进商机状态ID
  340. * @return
  341. */
  342. public function advance()
  343. {
  344. $param = $this->param;
  345. $userInfo = $this->userInfo;
  346. $userModel = new \app\admin\model\User();
  347. $is_end = $param['is_end'] ? : 0; //1赢单2输单3无效
  348. if (!$param['business_id']) {
  349. return resultArray(['error' => '参数错误']);
  350. }
  351. $businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
  352. if ($businessInfo['is_end']) {
  353. return resultArray(['error' => '已结束,不能推进']);
  354. }
  355. //判断权限
  356. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  357. //读写权限
  358. $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update');
  359. if (!in_array($businessInfo['owner_user_id'],$auth_user_ids) && !$rwPre) {
  360. header('Content-Type:application/json; charset=utf-8');
  361. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  362. }
  363. $status_id = $param['status_id'] ? : $businessInfo['status_id'];
  364. $statusInfo = db('crm_business_status')->where(['type_id' => $businessInfo['type_id'],'status_id' => $status_id])->find();
  365. if (!$statusInfo && !$is_end) {
  366. return resultArray(['error' => '参数错误']);
  367. }
  368. $data = [];
  369. $data['update_time'] = time();
  370. $data['is_end'] = $is_end;
  371. // if ($is_end) {
  372. // $status_id = $is_end;
  373. // }
  374. $data['status_id'] = $status_id;
  375. $data['status_time'] = time();
  376. $res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data);
  377. if (!$res) {
  378. return resultArray(['error' => '推进失败,请重试']);
  379. } else {
  380. # 商机变更后的名称
  381. $businessStatusName = Db::name('crm_business_status')->where('status_id', $param['status_id'])->value('name');
  382. if (empty($businessStatusName) && $is_end == 1) $businessStatusName = '赢单';
  383. if (empty($businessStatusName) && $is_end == 2) $businessStatusName = '输单';
  384. if (empty($businessStatusName) && $is_end == 3) $businessStatusName = '无效';
  385. # 添加活动记录
  386. Db::name('crm_activity')->insert([
  387. 'type' => 3,
  388. 'activity_type' => 5,
  389. 'activity_type_id' => $businessInfo['business_id'],
  390. 'content' => '阶段变更为 ' . $businessStatusName,
  391. 'create_user_id' => $businessInfo['owner_user_id'],
  392. 'update_time' => time(),
  393. 'create_time' => time(),
  394. 'customer_ids' => ',' . $businessInfo['customer_id'] . ','
  395. ]);
  396. //推进记录添加
  397. $temp['status_id'] = $status_id ? : 0;
  398. $temp['is_end'] = $is_end ? : 0;
  399. $temp['business_id'] = $param['business_id'];
  400. $temp['create_time'] = time();
  401. $temp['owner_user_id'] = $userInfo['id'];
  402. $temp['remark'] = $param['remark'] ? : '';
  403. Db::name('CrmBusinessLog')->insert($temp);
  404. # 返回商机阶段数据
  405. $typeId = db('crm_business')->where('business_id', $param['business_id'])->value('type_id');
  406. $businessStatus = db('crm_business_status')->where('type_id', $typeId)->select();
  407. $result = [
  408. 'business_id' => $param['business_id'],
  409. 'type_id' => $typeId,
  410. 'status_id' => $param['status_id'],
  411. 'status_list' => $businessStatus
  412. ];
  413. return resultArray(['data' => $result]);
  414. }
  415. }
  416. /**
  417. * 商机导出
  418. * @author Michael_xu
  419. * @param
  420. * @return
  421. */
  422. public function excelExport()
  423. {
  424. $param = $this->param;
  425. $userInfo = $this->userInfo;
  426. $param['user_id'] = $userInfo['id'];
  427. if ($param['business_id']) {
  428. $param['business_id'] = ['condition' => 'in','value' => $param['business_id'],'form_type' => 'text','name' => ''];
  429. $param['is_excel'] = 1;
  430. }
  431. $excelModel = new \app\admin\model\Excel();
  432. // 导出的字段列表
  433. $fieldModel = new \app\admin\model\Field();
  434. $field_list = $fieldModel->getIndexFieldConfig('crm_business', $userInfo['id']);
  435. // 文件名
  436. $file_name = '5kcrm_business_'.date('Ymd');
  437. $model = model('Business');
  438. $temp_file = $param['temp_file'];
  439. unset($param['temp_file']);
  440. $page = $param['page'] ?: 1;
  441. unset($param['page']);
  442. unset($param['export_queue_index']);
  443. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function($page, $limit) use ($model, $param, $field_list) {
  444. $param['page'] = $page;
  445. $param['limit'] = $limit;
  446. $data = $model->getDataList($param);
  447. $data['list'] = $model->exportHandle($data['list'], $field_list, 'business');
  448. return $data;
  449. });
  450. }
  451. /**
  452. * 设置关注
  453. *
  454. * @return \think\response\Json
  455. * @throws \think\Exception
  456. * @throws \think\exception\PDOException
  457. */
  458. public function star()
  459. {
  460. $userId = $this->userInfo['id'];
  461. $targetId = $this->param['target_id'];
  462. $type = $this->param['type'];
  463. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  464. if (!$this->setStar($type, $userId, $targetId)) {
  465. return resultArray(['error' => '设置关注失败!']);
  466. }
  467. return resultArray(['data' => '设置关注成功!']);
  468. }
  469. /**
  470. * 系统信息
  471. *
  472. * @return \think\response\Json
  473. * @throws \think\db\exception\DataNotFoundException
  474. * @throws \think\db\exception\ModelNotFoundException
  475. * @throws \think\exception\DbException
  476. */
  477. public function system()
  478. {
  479. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  480. $businessModel = new \app\crm\model\Business();
  481. $data = $businessModel->getSystemInfo($this->param['id']);
  482. return resultArray(['data' => $data]);
  483. }
  484. /**
  485. * table栏数量统计
  486. *
  487. * @return \think\response\Json
  488. * @throws \think\db\exception\DataNotFoundException
  489. * @throws \think\db\exception\ModelNotFoundException
  490. * @throws \think\exception\DbException
  491. */
  492. public function count()
  493. {
  494. if (empty($this->param['business_id'])) return resultArray(['error' => '参数错误!']);
  495. $businessId = $this->param['business_id'];
  496. $userInfo = $this->userInfo;
  497. # 查询联系人和商机关联数据
  498. $contactsIds = Db::name('crm_contacts_business')->where('business_id', $businessId)->column('contacts_id');
  499. # 联系人
  500. $contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
  501. $contactsCount = Db::name('crm_contacts')->whereIn('contacts_id', $contactsIds)->where($contactsAuth)->count();
  502. # 合同
  503. $contractAuth = $this->getContractSearchWhere($userInfo['id']);
  504. $contractCount = Db::name('crm_contract')->where('business_id', $businessId)->where($contractAuth)->count();
  505. # 查询商机和产品的关联表
  506. $productIds = Db::name('crm_business_product')->where('business_id', $businessId)->column('product_id');
  507. # 产品
  508. $productAuth = $this->getProductSearchWhere();
  509. $productCount = Db::name('crm_product')->whereIn('product_id', $productIds)->whereIn('owner_user_id', $productAuth)->count();
  510. # 附件
  511. $fileCount = Db::name('crm_business_file')->alias('business')->join('__ADMIN_FILE__ file', 'file.file_id = business.file_id', 'LEFT')->where('business_id', $businessId)->count();
  512. # 团队
  513. $business = Db::name('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $businessId)->find();
  514. $business['ro_user_id'] = explode(',', trim($business['ro_user_id'], ','));
  515. $business['rw_user_id'] = explode(',', trim($business['rw_user_id'], ','));
  516. $business['owner_user_id'] = [$business['owner_user_id']];
  517. $teamCount = array_filter(array_unique(array_merge($business['ro_user_id'], $business['rw_user_id'], $business['owner_user_id'])));
  518. $data = [
  519. 'contactCount' => $contactsCount,
  520. 'contractCount' => $contractCount,
  521. 'fileCount' => $fileCount,
  522. 'memberCount' => count($teamCount),
  523. 'productCount' => $productCount
  524. ];
  525. return resultArray(['data' => $data]);
  526. }
  527. /**
  528. * 设置首要联系人
  529. *
  530. * @return \think\response\Json
  531. * @throws \think\Exception
  532. * @throws \think\exception\PDOException
  533. */
  534. public function setPrimary()
  535. {
  536. $businessId = $this->param['business_id'];
  537. $contactsId = $this->param['contacts_id'];
  538. if (empty($businessId) || empty($contactsId)) return resultArray(['error' => '参数错误!']);
  539. if (!Db::name('crm_business')->where('business_id', $businessId)->update(['contacts_id' => $contactsId])) {
  540. return resultArray(['error' => '操作失败!']);
  541. }
  542. return resultArray(['data' => '操作成功!']);
  543. }
  544. }