Customer.php 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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\logic\CustomerLogic;
  10. use app\crm\traits\SearchConditionTrait;
  11. use app\crm\traits\StarTrait;
  12. use think\Hook;
  13. use think\Request;
  14. use think\Db;
  15. class Customer extends ApiCommon
  16. {
  17. use StarTrait, SearchConditionTrait;
  18. /**
  19. * 用于判断权限
  20. * @permission 无限制
  21. * @allow 登录用户可访问
  22. * @other 其他根据系统设置
  23. **/
  24. public function _initialize()
  25. {
  26. $action = [
  27. 'permission' => ['exceldownload', 'setfollow', 'delete'],
  28. 'allow' => ['read', 'system', 'count', 'poolauthority']
  29. ];
  30. Hook::listen('check_auth', $action);
  31. $request = Request::instance();
  32. $a = strtolower($request->action());
  33. if (!in_array($a, $action['permission'])) {
  34. parent::_initialize();
  35. } else {
  36. $param = Request::instance()->param();
  37. $this->param = $param;
  38. }
  39. }
  40. /**
  41. * 客户列表
  42. * @return
  43. * @author Michael_xu
  44. */
  45. public function index()
  46. {
  47. $customerModel = model('Customer');
  48. $param = $this->param;
  49. $userInfo = $this->userInfo;
  50. $param['user_id'] = $userInfo['id'];
  51. $data = $customerModel->getDataList($param);
  52. return resultArray(['data' => $data]);
  53. }
  54. /**
  55. * 客户公海(没有负责人或已经到期)
  56. * @return
  57. * @author Michael_xu
  58. */
  59. public function pool()
  60. {
  61. $param = $this->param;
  62. $param['action'] = 'pool';
  63. unset($param['poolId']); # todo uniApp传来的参数,临时删除掉 fanqi。
  64. $data = model('Customer')->getDataList($param);
  65. return resultArray(['data' => $data]);
  66. }
  67. /**
  68. * 添加客户
  69. * @param
  70. * @return
  71. * @author Michael_xu
  72. */
  73. public function save()
  74. {
  75. $customerModel = model('Customer');
  76. $param = $this->param;
  77. $userInfo = $this->userInfo;
  78. $param['create_user_id'] = $userInfo['id'];
  79. $param['owner_user_id'] = $userInfo['id'];
  80. if ($res = $customerModel->createData($param)) {
  81. return resultArray(['data' => $res]);
  82. } else {
  83. return resultArray(['error' => $customerModel->getError()]);
  84. }
  85. }
  86. /**
  87. * 客户详情
  88. * @param
  89. * @return
  90. * @author Michael_xu
  91. */
  92. public function read()
  93. {
  94. $customerModel = model('Customer');
  95. $param = $this->param;
  96. $userInfo = $this->userInfo;
  97. $data = $customerModel->getDataById($param['id'], $userInfo['id']);
  98. if (!$data) {
  99. return resultArray(['error' => $customerModel->getError()]);
  100. }
  101. //数据权限判断
  102. $userModel = new \app\admin\model\User();
  103. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'read');
  104. //读权限
  105. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  106. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  107. //判断是否客户池数据
  108. $wherePool = $customerModel->getWhereByPool();
  109. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['id']])->where($wherePool)->find();
  110. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
  111. $authData['dataAuth'] = (int)0;
  112. return resultArray(['data' => $authData]);
  113. }
  114. return resultArray(['data' => $data]);
  115. }
  116. /**
  117. * 编辑客户
  118. * @param
  119. * @return
  120. * @author Michael_xu
  121. */
  122. public function update()
  123. {
  124. $customerModel = model('Customer');
  125. $param = $this->param;
  126. $userInfo = $this->userInfo;
  127. //数据详情
  128. $data = $customerModel->getDataById($param['id']);
  129. if (!$data) {
  130. return resultArray(['error' => $customerModel->getError()]);
  131. }
  132. $param['user_id'] = $userInfo['id'];
  133. if ($customerModel->updateDataById($param, $param['id'])) {
  134. return resultArray(['data' => '编辑成功']);
  135. } else {
  136. return resultArray(['error' => $customerModel->getError()]);
  137. }
  138. }
  139. /**
  140. * 删除客户
  141. * @param
  142. * @return
  143. * @author Michael_xu
  144. */
  145. public function delete()
  146. {
  147. $param = $this->param;
  148. // 是否客户池
  149. if ($param['isSeas'] == 1) {
  150. $permission = checkPerByAction('crm', 'customer', 'poolDelete');
  151. } else {
  152. $permission = checkPerByAction('crm', 'customer', 'delete');
  153. }
  154. if ($permission == false) {
  155. return resultArray(['error' => '无权操作']);
  156. }
  157. $customerModel = model('Customer');
  158. $userModel = new \app\admin\model\User();
  159. $recordModel = new \app\admin\model\Record();
  160. $fileModel = new \app\admin\model\File();
  161. $actionRecordModel = new \app\admin\model\ActionRecord();
  162. if (!is_array($param['id'])) {
  163. $customer_id[] = $param['id'];
  164. } else {
  165. $customer_id = $param['id'];
  166. }
  167. $delIds = [];
  168. $errorMessage = [];
  169. //数据权限判断
  170. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'delete');
  171. //判断是否客户池数据(客户池数据只有管理员可以删)
  172. $adminId = $userModel->getAdminId();
  173. $wherePool = $customerModel->getWhereByPool();
  174. foreach ($customer_id as $k => $v) {
  175. $isDel = true;
  176. //数据详情
  177. $data = db('crm_customer')->where(['customer_id' => $v])->find();
  178. if (!$data) {
  179. $isDel = false;
  180. $errorMessage[] = 'id为' . $v . '的客户删除失败,错误原因:' . $customerModel->getError();
  181. }
  182. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  183. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && $isDel) {
  184. $isDel = false;
  185. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:无权操作';
  186. }
  187. // 公海 (原逻辑,公海仅允许管理员删除,修改为授权,不再限制)
  188. // if ($resPool && !in_array($data['owner_user_id'],$adminId)) {
  189. // $isDel = false;
  190. // $errorMessage[] = '名称为'.$data['name'].'的客户删除失败,错误原因:无权操作';
  191. // }
  192. //有商机、合同、联系人则不能删除
  193. if ($isDel) {
  194. $resBusiness = db('crm_business')->where(['customer_id' => $v])->find();
  195. if ($resBusiness) {
  196. $isDel = false;
  197. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在商机,不能删除';
  198. }
  199. }
  200. if ($isDel) {
  201. $resContacts = db('crm_contacts')->where(['customer_id' => $v])->find();
  202. if ($resContacts) {
  203. $isDel = false;
  204. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在联系人,不能删除';
  205. }
  206. }
  207. if ($isDel) {
  208. $resContract = db('crm_contract')->where(['customer_id' => $v])->find();
  209. if ($resContract) {
  210. $isDel = false;
  211. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在合同,不能删除';
  212. }
  213. }
  214. if ($isDel) {
  215. $delIds[] = $v;
  216. }
  217. }
  218. if ($delIds) {
  219. $delRes = $customerModel->delDatas($delIds);
  220. if (!$delRes) {
  221. return resultArray(['error' => $customerModel->getError()]);
  222. }
  223. //删除跟进记录
  224. $recordModel->delDataByTypes(2, $delIds);
  225. //删除关联附件
  226. $fileModel->delRFileByModule('crm_customer', $delIds);
  227. //删除关联操作记录
  228. $actionRecordModel->delDataById(['types' => 'crm_customer', 'action_id' => $delIds]);
  229. actionLog($delIds, '', '', '');
  230. }
  231. if ($errorMessage) {
  232. return resultArray(['error' => $errorMessage]);
  233. } else {
  234. return resultArray(['data' => '删除成功']);
  235. }
  236. }
  237. /**
  238. * 客户转移
  239. * @param owner_user_id 变更负责人
  240. * @param is_remove 1移出,2转为团队成员
  241. * @param types business,contract 相关模块
  242. * @param type 权限 1只读2读写
  243. * @return
  244. * @author Michael_xu
  245. */
  246. public function transfer()
  247. {
  248. $param = $this->param;
  249. $userInfo = $this->userInfo;
  250. $customerModel = model('Customer');
  251. $businessModel = model('Business');
  252. $contractModel = model('Contract');
  253. $contactsModel = model('Contacts');
  254. $settingModel = model('Setting');
  255. $customerConfigModel = model('CustomerConfig');
  256. $userModel = new \app\admin\model\User();
  257. if (!$param['owner_user_id']) {
  258. return resultArray(['error' => '变更负责人不能为空']);
  259. }
  260. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  261. return resultArray(['error' => '请选择需要转移的客户']);
  262. }
  263. $is_remove = ($param['is_remove'] == 2) ? 2 : 1;
  264. $type = $param['type'] == 2 ?: 1;
  265. $types = $param['types'] ?: [];
  266. $data = [];
  267. $data['owner_user_id'] = $param['owner_user_id'];
  268. $data['update_time'] = time();
  269. $data['follow'] = '待跟进';
  270. # 获取客户的时间
  271. $data['obtain_time'] = time();
  272. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  273. $errorMessage = [];
  274. foreach ($param['customer_id'] as $customer_id) {
  275. $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  276. if (!$customerInfo) {
  277. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户转移失败,错误原因:数据不存在;';
  278. continue;
  279. }
  280. $resCustomer = true;
  281. //权限判断
  282. if (!$customerModel->checkData($customer_id)) {
  283. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:无权限;';
  284. continue;
  285. }
  286. //拥有客户数上限检测
  287. if (!$customerConfigModel->checkData($param['owner_user_id'], 1)) {
  288. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:' . $customerConfigModel->getError();
  289. continue;
  290. }
  291. //团队成员
  292. $teamData = [];
  293. $teamData['type'] = $type; //权限 1只读2读写
  294. $teamData['user_id'] = [$customerInfo['owner_user_id']]; //协作人
  295. $teamData['types'] = 'crm_customer'; //类型
  296. $teamData['types_id'] = $customer_id; //类型ID
  297. $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
  298. $res = $settingModel->createTeamData($teamData);
  299. # 处理分配标识,待办事项专用
  300. $data['is_allocation'] = 1;
  301. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  302. if (!$resCustomer) {
  303. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:数据出错;';
  304. continue;
  305. } else {
  306. # 处理转移时,负责人出现在只读和读写成员列表中
  307. $customerArray = [];
  308. $teamCustomer = db('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customer_id)->find();
  309. if (!empty($teamCustomer['ro_user_id'])) {
  310. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['ro_user_id']), [$teamCustomer['owner_user_id']]));
  311. $customerArray['ro_user_id'] = $customerRo;
  312. }
  313. if (!empty($teamCustomer['rw_user_id'])) {
  314. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['rw_user_id']), [$teamCustomer['owner_user_id']]));
  315. $customerArray['rw_user_id'] = $customerRo;
  316. }
  317. db('crm_customer')->where('customer_id', $customer_id)->update($customerArray);
  318. }
  319. if (in_array('crm_contacts', $types)) {
  320. $contactsIds = [];
  321. $contactsIds = db('crm_contacts')->where(['customer_id' => $customer_id])->column('contacts_id');
  322. if ($contactsIds) {
  323. $resContacts = $contactsModel->transferDataById($contactsIds, $param['owner_user_id'], $type, $is_remove);
  324. if ($resContacts !== true) {
  325. $errorMessage[] = $resContacts;
  326. continue;
  327. }
  328. }
  329. }
  330. //商机、合同转移
  331. if (in_array('crm_business', $types)) {
  332. $businessIds = [];
  333. $businessIds = db('crm_business')->where(['customer_id' => $customer_id])->column('business_id');
  334. if ($businessIds) {
  335. $resBusiness = $businessModel->transferDataById($businessIds, $param['owner_user_id'], $type, $is_remove);
  336. if ($resBusiness !== true) {
  337. $errorMessage = $errorMessage ? array_merge($errorMessage, $resBusiness) : $resBusiness;
  338. continue;
  339. }
  340. }
  341. }
  342. if (in_array('crm_contract', $types)) {
  343. $contractIds = [];
  344. $contractIds = db('crm_contract')->where(['customer_id' => $customer_id])->column('contract_id');
  345. if ($contractIds) {
  346. $resContract = $contractModel->transferDataById($contractIds, $param['owner_user_id'], $type, $is_remove);
  347. if ($resContract !== true) {
  348. $errorMessage = $errorMessage ? array_merge($errorMessage, $resContract) : $resContract;
  349. continue;
  350. }
  351. }
  352. }
  353. //修改记录
  354. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户转移给:' . $ownerUserName);
  355. }
  356. if (!$errorMessage) {
  357. return resultArray(['data' => '转移成功']);
  358. } else {
  359. return resultArray(['error' => $errorMessage]);
  360. }
  361. }
  362. /**
  363. * 客户放入公海(负责人置为0)
  364. * @param
  365. * @return
  366. * @author Michael_xu
  367. */
  368. public function putInPool()
  369. {
  370. $param = $this->param;
  371. $userInfo = $this->userInfo;
  372. $customerModel = model('Customer');
  373. $settingModel = new \app\crm\model\Setting();
  374. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  375. return resultArray(['error' => '请选择需要放入公海的客户']);
  376. }
  377. $data = [];
  378. $data['owner_user_id'] = 0;
  379. $data['is_lock'] = 0;
  380. $data['update_time'] = time();
  381. $errorMessage = [];
  382. foreach ($param['customer_id'] as $customer_id) {
  383. $customerInfo = [];
  384. $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  385. if (!$customerInfo) {
  386. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户放入公海失败,错误原因:数据不存在;';
  387. continue;
  388. }
  389. //权限判断
  390. if (!$customerModel->checkData($customer_id)) {
  391. $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:无权限';
  392. continue;
  393. }
  394. //将团队成员全部清除
  395. $data['ro_user_id'] = '';
  396. $data['rw_user_id'] = '';
  397. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  398. if (!$resCustomer) {
  399. $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:数据出错;';
  400. continue;
  401. }
  402. //联系人负责人清除
  403. db('crm_contacts')->where(['customer_id' => $customer_id])->update(['owner_user_id' => 0]);
  404. //修改记录
  405. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户放入公海');
  406. }
  407. if (!$errorMessage) {
  408. return resultArray(['data' => '操作成功']);
  409. } else {
  410. return resultArray(['error' => $errorMessage]);
  411. }
  412. }
  413. /**
  414. * 客户锁定,解锁
  415. * @param is_lock 1锁定,2解锁
  416. * @return
  417. * @author Michael_xu
  418. */
  419. public function lock()
  420. {
  421. $param = $this->param;
  422. $userInfo = $this->userInfo;
  423. $customerModel = model('Customer');
  424. $customerConfigModel = model('CustomerConfig');
  425. $is_lock = ((int)$param['is_lock'] == 2) ? (int)$param['is_lock'] : 1;
  426. $lock_name = ($is_lock == 2) ? '解锁' : '锁定';
  427. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  428. return resultArray(['error' => '请选择需要' . $lock_name . '的客户']);
  429. }
  430. $data = [];
  431. $data['is_lock'] = ($is_lock == 1) ? $is_lock : 0;
  432. $data['update_time'] = time();
  433. $errorMessage = [];
  434. foreach ($param['customer_id'] as $customer_id) {
  435. $customerInfo = [];
  436. $customerInfo = $customerModel->getDataById($customer_id);
  437. if (!$customerInfo) {
  438. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户' . $lock_name . '失败,错误原因:数据不存在;';
  439. continue;
  440. }
  441. //权限判断
  442. if (!$customerModel->checkData($customer_id)) {
  443. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:无权限';
  444. continue;
  445. }
  446. //锁定上限检测
  447. if ($is_lock == 1 && !$customerConfigModel->checkData($customerInfo['owner_user_id'], 2)) {
  448. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:' . $customerConfigModel->getError();
  449. continue;
  450. }
  451. //已成交客户,锁定,提示无需锁定
  452. // if ($customerInfo['deal_status'] == '已成交' && $is_lock == 1) {
  453. // $errorMessage[] = $customerInfo['name'].$lock_name.'失败,错误原因:已成交状态,无需锁定';
  454. // continue;
  455. // }
  456. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  457. if (!$resCustomer) {
  458. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:数据出错;';
  459. }
  460. //修改记录
  461. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户' . $lock_name);
  462. }
  463. if (!$errorMessage) {
  464. return resultArray(['data' => '操作成功']);
  465. } else {
  466. return resultArray(['error' => $errorMessage]);
  467. }
  468. }
  469. /**
  470. * 客户领取
  471. * @param
  472. * @return
  473. * @author Michael_xu
  474. */
  475. public function receive()
  476. {
  477. $param = $this->param;
  478. $userInfo = $this->userInfo;
  479. $customerModel = model('Customer');
  480. $customerConfigModel = model('CustomerConfig');
  481. $customer_ids = $param['customer_id'] ?: $userInfo['id'];
  482. if (!$customer_ids || !is_array($customer_ids)) {
  483. return resultArray(['error' => '请选择需要领取的客户']);
  484. }
  485. $errorMessage = [];
  486. $wherePool = $customerModel->getWhereByPool();
  487. foreach ($customer_ids as $k => $v) {
  488. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  489. //判断是否是客户池数据
  490. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  491. if (!$resPool) {
  492. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:非公海数据无权操作;';
  493. continue;
  494. }
  495. //拥有客户数上限检测
  496. if (!$customerConfigModel->checkData($userInfo['id'], 1)) {
  497. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:' . $customerConfigModel->getError();
  498. continue;
  499. }
  500. $data = [];
  501. $data['owner_user_id'] = $userInfo['id'];
  502. $data['update_time'] = time();
  503. $data['deal_time'] = time();
  504. $data['follow'] = '待跟进';
  505. //将团队成员全部清除
  506. $data['ro_user_id'] = '';
  507. $data['rw_user_id'] = '';
  508. # 获取客户的时间
  509. $data['obtain_time'] = time();
  510. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  511. if (!$resCustomer) {
  512. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:数据出错;';
  513. continue;
  514. }
  515. //联系人领取
  516. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $userInfo['id']]);
  517. //修改记录
  518. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '领取了客户');
  519. }
  520. if (!$errorMessage) {
  521. return resultArray(['data' => '领取成功']);
  522. } else {
  523. return resultArray(['error' => $errorMessage]);
  524. }
  525. }
  526. /**
  527. * 客户分配
  528. * @param
  529. * @return
  530. * @author Michael_xu
  531. */
  532. public function distribute()
  533. {
  534. $param = $this->param;
  535. $userInfo = $this->userInfo;
  536. $customerModel = model('Customer');
  537. $userModel = new \app\admin\model\User();
  538. $customerConfigModel = model('CustomerConfig');
  539. $customer_ids = $param['customer_id'];
  540. $owner_user_id = $param['owner_user_id'];
  541. if (!$customer_ids || !is_array($customer_ids)) {
  542. return resultArray(['error' => '请选择需要分配的客户']);
  543. }
  544. if (!$owner_user_id) {
  545. return resultArray(['error' => '请选择分配人']);
  546. }
  547. $ownerUserName = $userModel->getUserNameById($owner_user_id);
  548. $errorMessage = [];
  549. $wherePool = $customerModel->getWhereByPool();
  550. foreach ($customer_ids as $k => $v) {
  551. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  552. //判断是否是客户池数据
  553. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  554. if (!$resPool) {
  555. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:非公海数据无权操作;';
  556. continue;
  557. }
  558. //拥有客户数上限检测
  559. if (!$customerConfigModel->checkData($owner_user_id, 1)) {
  560. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:' . $customerConfigModel->getError();
  561. continue;
  562. }
  563. $data = [];
  564. $data['owner_user_id'] = $owner_user_id;
  565. $data['update_time'] = time();
  566. $data['deal_time'] = time();
  567. $data['follow'] = '待跟进';
  568. //将团队成员全部清除
  569. $data['ro_user_id'] = '';
  570. $data['rw_user_id'] = '';
  571. # 处理分配标识,待办事项专用
  572. $data['is_allocation'] = 1;
  573. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  574. if (!$resCustomer) {
  575. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:数据出错;';
  576. }
  577. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $owner_user_id]);
  578. //修改记录
  579. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '将客户分配给:' . $ownerUserName);
  580. //站内信
  581. $send_user_id[] = $owner_user_id;
  582. $sendContent = $userInfo['realname'] . '将客户《' . $dataName . '》,分配给您';
  583. if ($send_user_id) {
  584. sendMessage($send_user_id, $sendContent, $v, 1);
  585. }
  586. }
  587. if (!$errorMessage) {
  588. return resultArray(['data' => '分配成功']);
  589. } else {
  590. return resultArray(['error' => $errorMessage]);
  591. }
  592. }
  593. /**
  594. * 客户导出
  595. * @param
  596. * @return
  597. * @author Michael_xu
  598. */
  599. public function excelExport()
  600. {
  601. $param = $this->param;
  602. $userInfo = $this->userInfo;
  603. $param['user_id'] = $userInfo['id'];
  604. if ($param['customer_id']) {
  605. $param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
  606. }
  607. $param['is_excel'] = 1;
  608. $excelModel = new \app\admin\model\Excel();
  609. // 导出的字段列表
  610. $fieldModel = new \app\admin\model\Field();
  611. $field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id']);
  612. // 文件名
  613. $file_name = '5kcrm_customer_' . date('Ymd');
  614. $model = model('Customer');
  615. $temp_file = $param['temp_file'];
  616. unset($param['temp_file']);
  617. $page = $param['page'] ?: 1;
  618. unset($param['page']);
  619. unset($param['export_queue_index']);
  620. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  621. $param['page'] = $page;
  622. $param['limit'] = $limit;
  623. $data = $model->getDataList($param);
  624. $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
  625. return $data;
  626. });
  627. }
  628. /**
  629. * 客户导入模板下载
  630. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  631. * @return
  632. * @author Michael_xu
  633. */
  634. public function excelDownload($save_path = '')
  635. {
  636. $param = $this->param;
  637. $userInfo = $this->userInfo;
  638. $excelModel = new \app\admin\model\Excel();
  639. // 导入的字段列表
  640. $fieldModel = new \app\admin\model\Field();
  641. $fieldParam['types'] = 'crm_customer';
  642. $fieldParam['action'] = 'excel';
  643. $field_list = $fieldModel->field($fieldParam);
  644. $excelModel->excelImportDownload($field_list, 'crm_customer', $save_path);
  645. }
  646. /**
  647. * 客户数据导入
  648. * @param
  649. * @return
  650. * @author Michael_xu
  651. */
  652. public function excelImport()
  653. {
  654. $param = $this->param;
  655. $userInfo = $this->userInfo;
  656. $excelModel = new \app\admin\model\Excel();
  657. $param['create_user_id'] = $userInfo['id'];
  658. $param['owner_user_id'] = $param['owner_user_id'] ?: 0;
  659. $param['deal_time'] = time();
  660. $param['deal_status'] = '未成交';
  661. $param['types'] = 'crm_customer';
  662. $file = request()->file('file');
  663. // $res = $excelModel->importExcel($file, $param, $this);
  664. $res = $excelModel->batchImportData($file, $param, $this);
  665. return resultArray(['data' => $excelModel->getError()]);
  666. }
  667. /**
  668. * 客户标记为已跟进
  669. * @param
  670. * @return
  671. * @author Michael_xu
  672. */
  673. public function setFollow()
  674. {
  675. $param = $this->param;
  676. $customerIds = $param['id'] ?: [];
  677. if (!$customerIds || !is_array($customerIds)) {
  678. return resultArray(['error' => '参数错误']);
  679. }
  680. $data['follow'] = '已跟进';
  681. $data['update_time'] = time();
  682. $res = db('crm_customer')->where(['customer_id' => ['in', $customerIds]])->update($data);
  683. if (!$res) {
  684. return resultArray(['error' => '操作失败,请重试']);
  685. }
  686. return resultArray(['data' => '跟进成功']);
  687. }
  688. /**
  689. * 置顶 / 取消置顶
  690. * @return [type] [description]
  691. */
  692. public function top()
  693. {
  694. $param = $this->param;
  695. $userInfo = $this->userInfo;
  696. $param['create_role_id'] = $userInfo['id'];
  697. $param['top_time'] = time();
  698. $top_id = Db::name('crm_top')->where(['module' => ['eq', $param['module']], 'create_role_id' => ['eq', $userInfo['id']], 'module_id' => ['eq', $param['module_id']]])->column('top_id');
  699. if ($top_id) {
  700. if ($res = Db::name('crm_top')->where('top_id', $top_id[0])->update($param)) {
  701. return resultArray(['data' => $res]);
  702. } else {
  703. return resultArray(['error' => Db::name('crm_top')->getError()]);
  704. }
  705. } else {
  706. if ($res = Db::name('crm_top')->data($param)->insert()) {
  707. return resultArray(['data' => $res]);
  708. } else {
  709. return resultArray(['error' => $customerModel->getError()]);
  710. }
  711. }
  712. }
  713. /**
  714. * 客户公海导出
  715. * @param
  716. * @return
  717. * @author Michael_xu
  718. */
  719. public function poolExcelExport()
  720. {
  721. $param = $this->param;
  722. $userInfo = $this->userInfo;
  723. $param['user_id'] = $userInfo['id'];
  724. if ($param['customer_id']) {
  725. $param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
  726. }
  727. $param['is_excel'] = 1;
  728. $excelModel = new \app\admin\model\Excel();
  729. // 导出的字段列表
  730. $fieldModel = new \app\admin\model\Field();
  731. $field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id']);
  732. $field_list = array_filter($field_list, function ($val) {
  733. return $val['field'] != 'owner_user_id';
  734. });
  735. // 文件名
  736. $file_name = '5kcrm_customer_pool_' . date('Ymd');
  737. $param['action'] = 'pool';
  738. $model = model('Customer');
  739. $temp_file = $param['temp_file'];
  740. unset($param['temp_file']);
  741. $page = $param['page'] ?: 1;
  742. unset($param['page']);
  743. unset($param['export_queue_index']);
  744. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param) {
  745. $param['page'] = $page;
  746. $param['limit'] = $limit;
  747. $data = $model->getDataList($param);
  748. $data['list'] = $model->exportHandle($data['list'], $field_list,'crm_customer');
  749. return $data;
  750. });
  751. }
  752. /**
  753. * 客户成交状态
  754. * @param status 1已成交,2未成交
  755. * @return
  756. * @author Michael_xu
  757. */
  758. public function deal_status()
  759. {
  760. $param = $this->param;
  761. $userInfo = $this->userInfo;
  762. $statusArr = ['1' => '已成交', '2' => '未成交'];
  763. $statusList = ['1', '2'];
  764. if (!$param['customer_id'] || !in_array($param['status'], $statusList)) {
  765. return resultArray(['error' => '参数错误']);
  766. }
  767. $customerModel = model('Customer');
  768. $customerConfigModel = model('CustomerConfig');
  769. $userModel = new \app\admin\model\User();
  770. $customer_ids = $param['customer_id'];
  771. if (!is_array($customer_ids) || !$customer_ids) {
  772. $customer_ids[] = $customer_ids;
  773. }
  774. $data = [];
  775. $data['update_time'] = time();
  776. $data['deal_time'] = time();
  777. $data['deal_status'] = $statusArr[$param['status']];
  778. $errorMessage = [];
  779. foreach ($customer_ids as $customer_id) {
  780. $dataInfo = [];
  781. $dataInfo = db('crm_customer')->where(['customer_id' => $customer_id])->field('owner_user_id,deal_status,name')->find();
  782. //权限判断
  783. if (!$customerModel->checkData($customer_id, 1)) {
  784. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerModel->getError();
  785. continue;
  786. }
  787. $owner_user_id = $dataInfo['owner_user_id'];;
  788. if (!$owner_user_id) {
  789. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:公海数据无权操作';
  790. continue;
  791. }
  792. //拥有客户数上限检测
  793. if ($statusArr[$param['status']] == '未成交' && $dataInfo['deal_status'] == '已成交') {
  794. if (!$customerConfigModel->checkData($owner_user_id, 1, 1)) {
  795. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerConfigModel->getError();
  796. continue;
  797. }
  798. }
  799. if ($statusArr[$param['status']] == '已成交') {
  800. $data['is_lock'] = 0;
  801. }
  802. $res = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  803. if (!$res) {
  804. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:操作失败,请重试!';
  805. continue;
  806. }
  807. //修改记录
  808. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, ['deal_status' => $dataInfo['deal_status']], ['deal_status' => $data['deal_status']]);
  809. }
  810. if (!$errorMessage) {
  811. return resultArray(['data' => '操作成功']);
  812. } else {
  813. return resultArray(['error' => $errorMessage]);
  814. }
  815. }
  816. /**
  817. * 设置关注
  818. *
  819. * @return \think\response\Json
  820. * @throws \think\Exception
  821. * @throws \think\exception\PDOException
  822. */
  823. public function star()
  824. {
  825. $userId = $this->userInfo['id'];
  826. $targetId = $this->param['target_id'];
  827. $type = $this->param['type'];
  828. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  829. if (!$this->setStar($type, $userId, $targetId)) {
  830. return resultArray(['error' => '设置关注失败!']);
  831. }
  832. return resultArray(['data' => '设置关注成功!']);
  833. }
  834. /**
  835. * 附近客户
  836. *
  837. * @return \think\response\Json
  838. */
  839. public function nearby()
  840. {
  841. if (empty($this->param['lng'])) return resultArray(['error' => '缺少经度参数!']);
  842. if (empty($this->param['lat'])) return resultArray(['error' => '缺少纬度参数!']);
  843. if (empty($this->param['distance'])) return resultArray(['error' => '请选择距离!']);
  844. $customerModel = model('Customer');
  845. $data = $customerModel->getNearbyList($this->param);
  846. return resultArray(['data' => $data]);
  847. }
  848. /**
  849. * 系统信息
  850. *
  851. * @return \think\response\Json
  852. * @throws \think\db\exception\DataNotFoundException
  853. * @throws \think\db\exception\ModelNotFoundException
  854. * @throws \think\exception\DbException
  855. */
  856. public function system()
  857. {
  858. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  859. $customerModel = new \app\crm\model\Customer();
  860. $data = $customerModel->getSystemInfo($this->param['id']);
  861. return resultArray(['data' => $data]);
  862. }
  863. /**
  864. * table标签栏数量
  865. *
  866. * @return \think\response\Json
  867. * @throws \think\db\exception\DataNotFoundException
  868. * @throws \think\db\exception\ModelNotFoundException
  869. * @throws \think\exception\DbException
  870. */
  871. public function count()
  872. {
  873. if (empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
  874. $userInfo = $this->userInfo;
  875. $customerId = $this->param['customer_id'];
  876. # 联系人
  877. $contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
  878. $contactsCount = Db::name('crm_contacts')->where('customer_id', $customerId)->where($contactsAuth)->count();
  879. # 团队成员
  880. $customer = Db::name('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customerId)->find();
  881. $customer['ro_user_id'] = explode(',', trim($customer['ro_user_id'], ','));
  882. $customer['rw_user_id'] = explode(',', trim($customer['rw_user_id'], ','));
  883. $customer['owner_user_id'] = [$customer['owner_user_id']];
  884. $teamCount = array_filter(array_unique(array_merge($customer['ro_user_id'], $customer['rw_user_id'], $customer['owner_user_id'])));
  885. # 商机
  886. $businessAuth = $this->getBusinessSearchWhere($userInfo['id']);
  887. $businessCount = Db::name('crm_business')->where('customer_id', $customerId)->where($businessAuth)->count();
  888. # 合同
  889. $contractAuth = $this->getContractSearchWhere($userInfo['id']);
  890. $contractCount = Db::name('crm_contract')->where('customer_id', $customerId)->where($contractAuth)->count();
  891. # 回款
  892. $receivablesAuth = $this->getReceivablesSearchWhere();
  893. $receivablesCount = Db::name('crm_receivables')->where('customer_id', $customerId)->whereIn('owner_user_id', $receivablesAuth)->count();
  894. # 回访
  895. $visitAuth = $this->getVisitSearchWhere($userInfo['id']);
  896. $visitCount = Db::name('crm_visit')->where(['customer_id' => $customerId, 'deleted_state' => 0])->where($visitAuth)->count();
  897. # 发票
  898. $invoiceAuth = $this->getInvoiceSearchWhere();
  899. $invoiceCount = Db::name('crm_invoice')->where('customer_id', $customerId)->whereIn('owner_user_id', $invoiceAuth)->count();
  900. # 附件
  901. $fileCount = Db::name('crm_customer_file')->alias('customer')->join('__ADMIN_FILE__ file', 'file.file_id = customer.file_id')->where('customer_id', $customerId)->count();
  902. $data = [
  903. 'businessCount' => $businessCount,
  904. 'contactCount' => $contactsCount,
  905. 'contractCount' => $contractCount,
  906. 'fileCount' => $fileCount,
  907. 'invoiceCount' => $invoiceCount,
  908. 'memberCount' => count($teamCount),
  909. 'receivablesCount' => $receivablesCount,
  910. 'returnVisitCount' => $visitCount
  911. ];
  912. return resultArray(['data' => $data]);
  913. }
  914. /**
  915. * 公海权限
  916. *
  917. * @param CustomerLogic $customerLogic
  918. * @return \think\response\Json
  919. * @throws \think\db\exception\DataNotFoundException
  920. * @throws \think\db\exception\ModelNotFoundException
  921. * @throws \think\exception\DbException
  922. */
  923. public function poolAuthority(CustomerLogic $customerLogic)
  924. {
  925. $authority = [
  926. 'delete' => false, # 删除
  927. 'distribute' => false, # 分配
  928. 'excelexport' => false, # 导出
  929. 'index' => false, # 列表
  930. 'receive' => false, # 领取
  931. ];
  932. $userId = $this->userInfo['id'];
  933. if (empty($userId)) return resultArray(['data' => $authority]);
  934. # 员工角色数据
  935. $groupIds = $customerLogic->getEmployeeGroups($userId);
  936. # 员工角色下的规则数据
  937. $ruleIds = $customerLogic->getEmployeeRules($groupIds);
  938. # 公海规则数据
  939. $poolRules = $customerLogic->getPoolRules();
  940. # 整理员工规则数据
  941. $rules = [];
  942. $ruleIds = implode(',', $ruleIds);
  943. $rules = array_filter(array_unique(explode(',', $ruleIds)));
  944. # 整理公海规则数据
  945. $deleteId = $distributeId = $exportId = $indexId = $receiveId = 0;
  946. foreach ($poolRules as $key => $value) {
  947. if ($value['name'] == 'pool') $indexId = $value['id'];
  948. if ($value['name'] == 'distribute') $distributeId = $value['id'];
  949. if ($value['name'] == 'receive') $receiveId = $value['id'];
  950. if ($value['name'] == 'poolExcelExport') $exportId = $value['id'];
  951. if ($value['name'] == 'poolDelete') $deleteId = $value['id'];
  952. }
  953. # 权限判断
  954. $authority['delete'] = $userId == 1 || in_array(1, $groupIds) || in_array($deleteId, $rules) ? true : false;
  955. $authority['distribute'] = $userId == 1 || in_array(1, $groupIds) || in_array($distributeId, $rules) ? true : false;
  956. $authority['excelexport'] = $userId == 1 || in_array(1, $groupIds) || in_array($exportId, $rules) ? true : false;
  957. $authority['index'] = $userId == 1 || in_array(1, $groupIds) || in_array($indexId, $rules) ? true : false;
  958. $authority['receive'] = $userId == 1 || in_array(1, $groupIds) || in_array($receiveId, $rules) ? true : false;
  959. return resultArray(['data' => $authority]);
  960. }
  961. }