123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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\InvoiceLogic;
  10. use think\cache\driver\Redis;
  11. use think\Db;
  12. use think\Hook;
  13. use think\Request;
  14. class Message extends ApiCommon
  15. {
  16. /**
  17. * 用于判断权限
  18. * @permission 无限制
  19. * @allow 登录用户可访问
  20. * @other 其他根据系统设置
  21. **/
  22. public function _initialize()
  23. {
  24. $action = [
  25. 'permission' => [],
  26. 'allow' => [
  27. 'num',
  28. 'todayleads',
  29. 'todaycustomer',
  30. 'todaybusiness',
  31. 'followleads',
  32. 'followcustomer',
  33. 'checkcontract',
  34. 'checkreceivables',
  35. 'remindreceivablesplan',
  36. 'endcontract',
  37. 'remindcustomer',
  38. 'checkinvoice',
  39. 'visitcontract',
  40. 'alldeal'
  41. ]
  42. ];
  43. Hook::listen('check_auth',$action);
  44. $request = Request::instance();
  45. $a = strtolower($request->action());
  46. if (!in_array($a, $action['permission'])) {
  47. parent::_initialize();
  48. }
  49. }
  50. /**
  51. * 系统通知
  52. *
  53. * @author Michael_xu
  54. * @return
  55. */
  56. public function index()
  57. {
  58. $messageModel = model('Message');
  59. $param = $this->param;
  60. $userInfo = $this->userInfo;
  61. $param['user_id'] = $userInfo['id'];
  62. $param['module_name'] = 'crm';
  63. $data = $messageModel->getDataList($param);
  64. return resultArray(['data' => $data]);
  65. }
  66. /**
  67. * 消息数
  68. *
  69. * @return \think\response\Json
  70. * @throws \think\exception\DbException
  71. */
  72. public function num()
  73. {
  74. $userInfo = $this->userInfo;
  75. $configDataModel = model('ConfigData');
  76. $configData = $configDataModel->getData();
  77. $data = [];
  78. # 今日需联系线索
  79. $todayLeadsTime = cache('todayLeadsTime'.$userInfo['id']);
  80. $todayLeadsCount = cache('todayLeadsCount'.$userInfo['id']);
  81. if (time() <= $todayLeadsTime) {
  82. $data['todayLeads'] = (int)$todayLeadsCount;
  83. } else {
  84. $todayLeads = $this->todayLeads(true);
  85. $data['todayLeads'] = $todayLeads['dataCount'] ? : 0;
  86. cache('todayLeadsCount'.$userInfo['id'], $data['todayLeads']);
  87. cache('todayLeadsTime'.$userInfo['id'], time() + 180);
  88. }
  89. # 今日需联系客户
  90. $todayCustomerTime = cache('todayCustomerTime'.$userInfo['id']);
  91. $todayCustomerCount = cache('todayCustomerCount'.$userInfo['id']);
  92. if (time() <= $todayCustomerTime) {
  93. $data['todayCustomer'] = (int)$todayCustomerCount;
  94. } else {
  95. $todayCustomer = $this->todayCustomer(true);
  96. $data['todayCustomer'] = $todayCustomer['dataCount'] ? : 0;
  97. cache('todayCustomerCount'.$userInfo['id'], $data['todayCustomer']);
  98. cache('todayCustomerTime'.$userInfo['id'], time() + 180);
  99. }
  100. # 今日需联系商机
  101. $todayBusinessTime = cache('todayBusinessTime'.$userInfo['id']);
  102. $todayBusinessCount = cache('todayBusinessCount'.$userInfo['id']);
  103. if ($todayBusinessTime <= time()) {
  104. $data['todayBusiness'] = (int)$todayBusinessCount;
  105. } else {
  106. $todayBusiness = $this->todayBusiness(true);
  107. $data['todayBusiness'] = $todayBusiness['dataCount'] ? : 0;
  108. cache('todayBusinessCount'.$userInfo['id'], $data['todayBusiness']);
  109. cache('todayBusinessTime'.$userInfo['id'], time() + 180);
  110. }
  111. # 分配给我的线索
  112. $followLeadsTime = cache('followLeadsTime'.$userInfo['id']);
  113. $followLeadsCount = cache('followLeadsCount'.$userInfo['id']);
  114. if (time() <= $followLeadsTime) {
  115. $data['followLeads'] = (int)$followLeadsCount;
  116. } else {
  117. $followLeads = $this->followLeads(true);
  118. $data['followLeads'] = $followLeads['dataCount'] ? : 0;
  119. cache('followLeadsCount'.$userInfo['id'], $data['followLeads']);
  120. cache('followLeadsTime'.$userInfo['id'], time() + 180);
  121. }
  122. # 分配给我的客户
  123. $followCustomerTime = cache('followCustomerTime'.$userInfo['id']);
  124. $followCustomerCount = cache('followCustomerCount'.$userInfo['id']);
  125. if (time() <= $followCustomerTime) {
  126. $data['followCustomer'] = (int)$followCustomerCount;
  127. } else {
  128. $followCustomer = $this->followCustomer(true);
  129. $data['followCustomer'] = $followCustomer['dataCount'] ? : 0;
  130. cache('followCustomerCount'.$userInfo['id'], $data['followCustomer']);
  131. cache('followCustomerTime'.$userInfo['id'], time() + 180);
  132. }
  133. # 待审核合同
  134. $checkContractTime = cache('checkContractTime'.$userInfo['id']);
  135. $checkContractCount = cache('checkContractCount'.$userInfo['id']);
  136. if (time() <= $checkContractTime) {
  137. $data['checkContract'] = (int)$checkContractCount;
  138. } else {
  139. $checkContract = $this->checkContract(true);
  140. $data['checkContract'] = $checkContract['dataCount'] ? : 0;
  141. cache('checkContractCount'.$userInfo['id'], $data['checkContract']);
  142. cache('checkContractTime'.$userInfo['id'], time() + 180);
  143. }
  144. # 待审核回款
  145. $checkReceivablesTime = cache('checkReceivablesTime'.$userInfo['id']);
  146. $checkReceivablesCount = cache('checkReceivablesCount'.$userInfo['id']);
  147. if (time() <= $checkReceivablesTime) {
  148. $data['checkReceivables'] = (int)$checkReceivablesCount;
  149. } else {
  150. $checkReceivables = $this->checkReceivables(true);
  151. $data['checkReceivables'] = $checkReceivables['dataCount'] ? : 0;
  152. cache('checkReceivablesCount'.$userInfo['id'], $data['checkReceivables']);
  153. cache('checkReceivablesTime'.$userInfo['id'], time() + 180);
  154. }
  155. # 待审核发票
  156. $checkInvoiceTime = cache('checkInvoiceTime'.$userInfo['id']);
  157. $checkInvoiceCount = cache('checkInvoiceCount'.$userInfo['id']);
  158. if (time() <= $checkInvoiceTime) {
  159. $data['checkInvoice'] = (int)$checkInvoiceCount;
  160. } else {
  161. $checkInvoice = $this->checkInvoice(true);
  162. $data['checkInvoice'] = $checkInvoice['dataCount'] ? : 0;
  163. cache('checkInvoiceCount'.$userInfo['id'], $data['checkInvoice']);
  164. cache('checkInvoiceTime'.$userInfo['id'], time() + 180);
  165. }
  166. # 待回款提醒
  167. $remindReceivablesPlanTime = cache('remindReceivablesPlanTime'.$userInfo['id']);
  168. $remindReceivablesPlanCount = cache('remindReceivablesPlanCount'.$userInfo['id']);
  169. if (time() <= $remindReceivablesPlanTime) {
  170. $data['remindReceivablesPlan'] = (int)$remindReceivablesPlanCount;
  171. } else {
  172. $remindReceivablesPlan = $this->remindReceivablesPlan(true);
  173. $data['remindReceivablesPlan'] = $remindReceivablesPlan['dataCount'] ? : 0;
  174. cache('remindReceivablesPlanCount'.$userInfo['id'], $data['remindReceivablesPlan']);
  175. cache('remindReceivablesPlanTime'.$userInfo['id'], time() + 180);
  176. }
  177. # 待回访合同
  178. $visitContractTime = cache('visitContractTime'.$userInfo['id']);
  179. $visitContractCount = cache('visitContractCount'.$userInfo['id']);
  180. if (time() <= $visitContractTime) {
  181. $data['returnVisitRemind'] = (int)$visitContractCount;
  182. } else {
  183. $visitContract = $this->visitContract(true);
  184. $data['returnVisitRemind'] = $visitContract['dataCount'] ? : 0;
  185. cache('visitContractCount'.$userInfo['id'], $data['returnVisitRemind']);
  186. cache('visitContractTime'.$userInfo['id'], time() + 180);
  187. }
  188. # 即将到期合同
  189. if ($configData['contract_config'] == 1) {
  190. $endContractTime = cache('endContractTime'.$userInfo['id']);
  191. $endContractCount = cache('endContractCount'.$userInfo['id']);
  192. if (time() <= $endContractTime) {
  193. $data['endContract'] = (int)$endContractCount;
  194. } else {
  195. $endContract = $this->endContract(true);
  196. $data['endContract'] = $endContract['dataCount'] ? : 0;
  197. cache('endContractCount'.$userInfo['id'], $data['endContract']);
  198. cache('endContractTime'.$userInfo['id'], time() + 180);
  199. }
  200. }
  201. # 待进入公海提醒
  202. if ($configData['remind_config'] == 1) {
  203. $remindCustomerTime = cache('remindCustomerTime'.$userInfo['id']);
  204. $remindCustomerCount = cache('remindCustomerCount'.$userInfo['id']);
  205. if (time() <= $remindCustomerTime) {
  206. $data['putInPoolRemind'] = (int)$remindCustomerCount;
  207. } else {
  208. $remindCustomer = $this->remindCustomer(true);
  209. $data['putInPoolRemind'] = $remindCustomer['dataCount'] ? : 0;
  210. cache('remindCustomerCount'.$userInfo['id'], $data['putInPoolRemind']);
  211. cache('remindCustomerTime'.$userInfo['id'], time() + 180);
  212. }
  213. }
  214. return resultArray(['data' => $data]);
  215. }
  216. /**
  217. * 今日需联系线索
  218. *
  219. * @param false $getCount
  220. * @return array|\think\response\Json
  221. */
  222. public function todayLeads($getCount = false)
  223. {
  224. $param = $this->param;
  225. $userId = $this->userInfo['id'];
  226. $types = $param['types'];
  227. $type = !empty($param['type']) ? $param['type'] : 1;
  228. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  229. $todayTime = getTimeByType('today');
  230. unset($param['types']);
  231. unset($param['type']);
  232. unset($param['isSub']);
  233. if ($getCount == true) $param['getCount'] = 1;
  234. $leadsModel = new \app\crm\model\Leads();
  235. # 负责人
  236. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  237. # 类型:1今日需联系;2已逾期;3已联系
  238. switch ($type) {
  239. case '1' :
  240. $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  241. break;
  242. case '2' :
  243. $param['next_time'] = ['between', [1, time()]];
  244. break;
  245. case '3' :
  246. $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  247. $param['follow'] = ['eq', '已跟进'];
  248. break;
  249. }
  250. $param['user_id'] = $userId;
  251. $data = $leadsModel->getDataList($param);
  252. if ($types == 'list') return resultArray(['data' => $data]);
  253. return $data;
  254. }
  255. /**
  256. * 今日需联系客户
  257. *
  258. * @param string $getCount
  259. * @return \think\response\Json
  260. */
  261. public function todayCustomer($getCount = false)
  262. {
  263. $param = $this->param;
  264. $userInfo = $this->userInfo;
  265. $types = $param['types'];
  266. $type = $param['type'] ? : 1;
  267. $isSub = $param['isSub'] ? : '';
  268. if ($getCount == true) {
  269. $param['getCount'] = 1;
  270. }
  271. unset($param['types']);
  272. unset($param['type']);
  273. unset($param['isSub']);
  274. $customerModel = model('Customer');
  275. $todayTime = getTimeByType('today');
  276. $param['owner_user_id'] = !empty($isSub) ? ['in',getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
  277. switch ($type) {
  278. case '1' :
  279. $param['next_time'] = ['between',array($todayTime[0],$todayTime[1])];
  280. // $param['follow'] = ['neq','已跟进'];
  281. break;
  282. case '2' :
  283. $param['next_time'] = ['between',array(1,time())];
  284. // $param['today_param'] = 'customer.next_time>record.update_time';
  285. break;
  286. case '3' :
  287. $param['last_time'] = ['between',array($todayTime[0],$todayTime[1])];
  288. $param['follow'] = ['eq','已跟进'];
  289. break;
  290. }
  291. $data = $customerModel->getDataList($param);
  292. if ($types == 'list') {
  293. return resultArray(['data' => $data]);
  294. }
  295. return $data;
  296. }
  297. /**
  298. * 今日需联系商机
  299. *
  300. * @param false $getCount
  301. * @return array|\think\response\Json
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. * @throws \think\exception\DbException
  305. */
  306. public function todayBusiness($getCount = false)
  307. {
  308. $param = $this->param;
  309. $userId = $this->userInfo['id'];
  310. $types = $param['types'];
  311. $type = !empty($param['type']) ? $param['type'] : 1;
  312. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  313. $todayTime = getTimeByType('today');
  314. unset($param['types']);
  315. unset($param['type']);
  316. unset($param['isSub']);
  317. if ($getCount == true) $param['getCount'] = 1;
  318. $businessModel = new \app\crm\model\Business();
  319. # 负责人
  320. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  321. # 类型:1今日需联系;2已逾期;3已联系
  322. switch ($type) {
  323. case '1' :
  324. $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  325. break;
  326. case '2' :
  327. $param['next_time'] = ['between', [1, time()]];
  328. break;
  329. case '3' :
  330. $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  331. break;
  332. }
  333. $param['user_id'] = $userId;
  334. $data = $businessModel->getDataList($param);
  335. if ($types == 'list') return resultArray(['data' => $data]);
  336. return $data;
  337. }
  338. /**
  339. * 待跟进线索
  340. * @author Michael_xu
  341. * @return
  342. */
  343. public function followLeads($getCount = false)
  344. {
  345. $param = $this->param;
  346. $userInfo = $this->userInfo;
  347. $types = $param['types'];
  348. $type = $param['type'] ? : 1;
  349. $isSub = $param['isSub'] ? : '';
  350. unset($param['types']);
  351. unset($param['type']);
  352. unset($param['isSub']);
  353. $leadsModel = model('Leads');
  354. if ($getCount == true) $param['getCount'] = 1;
  355. $param['owner_user_id'] = $userInfo['id'];
  356. switch ($type) {
  357. case '1' : $param['follow'] = ['neq','已跟进']; break;
  358. case '2' : $param['follow'] = ['eq','已跟进']; break;
  359. }
  360. $param['user_id'] = $userInfo['id'];
  361. $data = $leadsModel->getDataList($param);
  362. if ($types == 'list') {
  363. return resultArray(['data' => $data]);
  364. }
  365. return $data;
  366. }
  367. /**
  368. * 待跟进客户
  369. * @author Michael_xu
  370. * @return
  371. */
  372. public function followCustomer($getCount = false)
  373. {
  374. $param = $this->param;
  375. $userInfo = $this->userInfo;
  376. $types = $param['types'];
  377. $type = $param['type'] ? : 1;
  378. $isSub = $param['isSub'] ? : '';
  379. if ($getCount == true) {
  380. $param['getCount'] = 1;
  381. }
  382. unset($param['types']);
  383. unset($param['type']);
  384. unset($param['isSub']);
  385. $customerModel = model('Customer');
  386. $param['owner_user_id'] = $userInfo['id'];
  387. switch ($type) {
  388. case '1' : $param['follow'] = ['eq','待跟进']; break;
  389. case '2' : $param['follow'] = ['eq','已跟进']; break;
  390. }
  391. $data = $customerModel->getDataList($param);
  392. if ($types == 'list') {
  393. return resultArray(['data' => $data]);
  394. }
  395. return $data;
  396. }
  397. /**
  398. * 待审核合同
  399. *
  400. * @param false $getCount
  401. * @return \think\response\Json
  402. */
  403. public function checkContract($getCount = false)
  404. {
  405. $param = $this->param;
  406. $userInfo = $this->userInfo;
  407. $types = $param['types'];
  408. $type = $param['type'] ? : 1;
  409. $isSub = $param['isSub'] ? : '';
  410. unset($param['types']);
  411. unset($param['type']);
  412. unset($param['isSub']);
  413. $contractModel = model('Contract');
  414. if ($getCount == true) {
  415. $param['getCount'] = 1;
  416. }
  417. switch ($type) {
  418. case '1' :
  419. $param['check_status'] = ['lt','2'];
  420. $param['check_user_id'] = ['like','%,'.$userInfo['id'].',%'];
  421. break;
  422. case '2' :
  423. // $param['check_status'] = ['egt','2'];
  424. $param['flow_user_id'] = ['like','%,'.$userInfo['id'].',%'];
  425. break;
  426. }
  427. $param['user_id'] = $userInfo['id'];
  428. $data = $contractModel->getDataList($param);
  429. if ($types == 'list') {
  430. return resultArray(['data' => $data]);
  431. }
  432. return $data;
  433. }
  434. /**
  435. * 待审核回款
  436. * @author Michael_xu
  437. * @return
  438. */
  439. public function checkReceivables($getCount = false)
  440. {
  441. $param = $this->param;
  442. $userInfo = $this->userInfo;
  443. $types = $param['types'];
  444. $type = $param['type'] ? : 1;
  445. $isSub = $param['isSub'] ? : '';
  446. unset($param['types']);
  447. unset($param['type']);
  448. unset($param['isSub']);
  449. $receivablesModel = model('Receivables');
  450. if ($getCount == true) $param['getCount'] = 1;
  451. switch ($type) {
  452. case '1' :
  453. # 待审核、审核中
  454. $param['check_status'] = ['lt','2'];
  455. $param['check_user_id'] = ['like','%,'.$userInfo['id'].',%'];
  456. break;
  457. case '2' :
  458. # 全部
  459. $param['flow_user_id'] = ['like','%,'.$userInfo['id'].',%'];
  460. break;
  461. }
  462. $param['user_id'] = $userInfo['id'];
  463. $data = $receivablesModel->getDataList($param);
  464. if ($types == 'list') {
  465. return resultArray(['data' => $data]);
  466. }
  467. return $data;
  468. }
  469. /**
  470. * 待审核发票
  471. *
  472. * @param InvoiceLogic $invoiceLogic
  473. * @return array|\think\response\Json
  474. * @throws \think\exception\DbException
  475. */
  476. public function checkInvoice($getCount = false)
  477. {
  478. $param = $this->param;
  479. $userId = $this->userInfo['id'];
  480. $types = $param['types'];
  481. $type = !empty($param['type']) ? $param['type'] : 1;
  482. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  483. if ($getCount == true) $param['getCount'] = 1;
  484. # 清除与模型无关的数据
  485. unset($param['types']);
  486. unset($param['type']);
  487. unset($param['isSub']);
  488. switch ($type) {
  489. case '1' :
  490. # 待审核、审核中
  491. $param['check_status'] = ['lt', 2];
  492. $param['check_user_id'] = ['like', '%,'. $userId .',%'];
  493. break;
  494. case '2' :
  495. # 全部
  496. $param['flow_user_id'] = ['like', '%,'. $userId .',%'];
  497. break;
  498. }
  499. $data = (new InvoiceLogic())->index($param);
  500. if ($types == 'list') return resultArray(['data' => $data]);
  501. return $data;
  502. }
  503. /**
  504. * 待回款提醒
  505. * @author Michael_xu
  506. * @return
  507. */
  508. public function remindReceivablesPlan($getCount = false)
  509. {
  510. $param = $this->param;
  511. $userInfo = $this->userInfo;
  512. $types = $param['types'];
  513. $type = $param['type'] ? : 1;
  514. $isSub = $param['isSub'] ? : '';
  515. unset($param['types']);
  516. unset($param['type']);
  517. unset($param['isSub']);
  518. $receivablesPlanModel = model('ReceivablesPlan');
  519. if ($getCount == true) $param['getCount'] = 1;
  520. $param['owner_user_id'] = $userInfo['id'];
  521. if ($isSub) {
  522. $param['owner_user_id'] = array('in',getSubUserId(false));
  523. }
  524. switch ($type) {
  525. case '1' : $param['receivables_id'] = 0;
  526. $param['check_status'] = array('lt',2);
  527. $param['remind_date'] = array('elt',date('Y-m-d',time()));
  528. $param['return_date'] = array('egt',date('Y-m-d',time()));
  529. $param['types'] = 1;
  530. break;
  531. case '2' : $param['receivables_id'] = array('gt',0);
  532. $param['check_status'] = 2;
  533. break;
  534. case '3' : $param['receivables_id'] = 0;
  535. $param['remind_date'] = array('lt',date('Y-m-d',time()));
  536. break;
  537. }
  538. $data = $receivablesPlanModel->getDataList($param);
  539. if ($types == 'list') {
  540. return resultArray(['data' => $data]);
  541. }
  542. return $data;
  543. }
  544. /**
  545. * 即将到期合同
  546. * @author Michael_xu
  547. * @return
  548. */
  549. public function endContract($getCount = false)
  550. {
  551. $param = $this->param;
  552. $userInfo = $this->userInfo;
  553. $types = $param['types'];
  554. $type = $param['type'] ? : 1;
  555. $isSub = $param['isSub'] ? : '';
  556. if ($getCount == true) $param['getCount'] = 1;
  557. unset($param['types']);
  558. unset($param['type']);
  559. unset($param['isSub']);
  560. $contractModel = model('Contract');
  561. $configModel = new \app\crm\model\ConfigData();
  562. $configInfo = $configModel->getData();
  563. $expireDay = $configInfo['contract_day'] ? : '7';
  564. $param['owner_user_id'] = $userInfo['id'];
  565. if ($isSub) {
  566. $param['owner_user_id'] = array('in',getSubUserId(false));
  567. }
  568. switch ($type) {
  569. case '1' :
  570. $param['end_time'] = array('between',array(date('Y-m-d',time()),date('Y-m-d',time()+86400*$expireDay)));
  571. $param['expire_remind'] = 1;
  572. break;
  573. case '2' : $param['end_time'] = array('lt',date('Y-m-d',time())); break;
  574. }
  575. $data = $contractModel->getDataList($param);
  576. if ($types == 'list') {
  577. return resultArray(['data' => $data]);
  578. }
  579. return $data;
  580. }
  581. /**
  582. * 待进入客户池(默认5天)
  583. * @author Michael_xu
  584. * @return
  585. */
  586. public function remindCustomer($getCount = false)
  587. {
  588. $customerModel = model('Customer');
  589. $param = $this->param;
  590. $userInfo = $this->userInfo;
  591. $types = $param['types'];
  592. $isSub = $param['isSub'] ? : '';
  593. if ($getCount == true) $param['getCount'] = 1;
  594. unset($param['types']);
  595. unset($param['type']);
  596. unset($param['isSub']);
  597. unset($param['deal_status']);
  598. unset($param['owner_user_id']);
  599. # 负责人
  600. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
  601. # 是否提醒
  602. $remind = db('crm_config')->where('name', 'remind_config')->value('value');
  603. $whereData = $param ? : [];
  604. $whereData['is_remind'] = !empty($remind) ? 1 : 0;
  605. $whereData['user_id'] = $userInfo['id'];
  606. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'me'])->value('scene_id');
  607. if ($isSub) {
  608. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'sub'])->value('scene_id');
  609. }
  610. $data = $customerModel->getDataList($whereData);
  611. if ($types == 'list') {
  612. return resultArray(['data' => $data]);
  613. }
  614. return $data;
  615. }
  616. /**
  617. * 待回访合同
  618. *
  619. * @param false $getCount
  620. * @return array|\think\response\Json
  621. * @throws \think\db\exception\DataNotFoundException
  622. * @throws \think\db\exception\ModelNotFoundException
  623. * @throws \think\exception\DbException
  624. */
  625. public function visitContract($getCount = false)
  626. {
  627. $param = $this->param;
  628. $userId = $this->userInfo['id'];
  629. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  630. $types = !empty($param['types']) ? $param['types'] : '';
  631. if ($getCount == true) $param['getCount'] = 1;
  632. unset($param['isSub']);
  633. unset($param['types']);
  634. $param['is_visit'] = 0; # 未回访
  635. $param['check_status'] = 2; # 审核通过
  636. $contractModel = new \app\crm\model\Contract();
  637. # 负责人
  638. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  639. $param['user_id'] = $userId;
  640. $data = $contractModel->getDataList($param);
  641. if ($types == 'list') return resultArray(['data' => $data]);
  642. return $data;
  643. }
  644. /**
  645. * 全部标记已处理
  646. *
  647. * @return \think\response\Json
  648. * @throws \think\Exception
  649. * @throws \think\exception\PDOException
  650. */
  651. public function allDeal()
  652. {
  653. $type = $this->param['type'];
  654. $typeId = !empty($this->param['type_id']) ? $this->param['type_id'] : '';
  655. $isSub = !empty($this->param['isSub']) ? $this->param['isSub'] : 0;
  656. $userId = $this->userInfo['id'];
  657. if (empty($type)) return resultArray(['error' => '缺少模块类型参数']);
  658. # 获得今日开始和结束时间戳
  659. $todayTime = getTimeByType('today');
  660. # 处理今日需联系线索、客户、商机
  661. if (in_array($type, ['todayLeads', 'todayCustomer', 'todayBusiness'])) {
  662. # 负责人
  663. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  664. # 下次联系时间
  665. $where['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  666. # 线索
  667. if ($type == 'todayLeads') {
  668. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  669. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update([
  670. 'next_time' => 0,
  671. 'last_time' => time(),
  672. 'follow' => '已跟进'
  673. ]);
  674. }
  675. # 客户
  676. if ($type == 'todayCustomer') {
  677. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  678. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update([
  679. 'next_time' => 0,
  680. 'last_time' => time(),
  681. 'follow' => '已跟进'
  682. ]);
  683. }
  684. # 商机
  685. if ($type == 'todayBusiness') {
  686. $businessId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
  687. Db::name('crm_business')->whereIn('business_id', $businessId)->update([
  688. 'next_time' => 0,
  689. 'last_time' => time()
  690. ]);
  691. }
  692. }
  693. # 处理分配给我的线索、客户
  694. if (in_array($type, ['followLeads', 'followCustomer'])) {
  695. $where['owner_user_id'] = $userId;
  696. $where['follow'] = ['neq','已跟进'];
  697. # 线索
  698. if ($type == 'followLeads') {
  699. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  700. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update(['follow' => '已跟进']);
  701. }
  702. # 客户
  703. if ($type == 'followCustomer') {
  704. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  705. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update(['follow' => '已跟进']);
  706. }
  707. }
  708. # 处理待审核合同、回款、发票
  709. if (in_array($type, ['checkContract', 'checkReceivables', 'checkInvoice'])) {
  710. $where['check_status'] = ['lt','2'];
  711. $where['check_user_id'] = ['like','%,' . $userId . ',%'];
  712. $update = [
  713. 'check_status' => 2,
  714. 'flow_id' => 0,
  715. 'order_id' => 0,
  716. 'check_user_id' => '',
  717. 'flow_user_id' => ',' . $userId . ','
  718. ];
  719. # 合同
  720. if ($type == 'checkContract') {
  721. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  722. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update($update);
  723. }
  724. # 回款
  725. if ($type == 'checkReceivables') {
  726. $receivablesId = !empty($typeId) ? $typeId : Db::name('crm_receivables')->where($where)->column('receivables_id');
  727. Db::name('crm_receivables')->whereIn('receivables_id', $receivablesId)->update($update);
  728. }
  729. # 发票
  730. if ($type == 'checkInvoice') {
  731. $invoiceId = !empty($typeId) ? $typeId : Db::name('crm_invoice')->where($where)->column('invoice_id');
  732. Db::name('crm_invoice')->whereIn('invoice_id', $invoiceId)->update($update);
  733. }
  734. }
  735. # 处理到期合同
  736. if ($type == 'endContract') {
  737. $configModel = new \app\crm\model\ConfigData();
  738. $configInfo = $configModel->getData();
  739. $expireDay = $configInfo['contract_day'] ? : '7';
  740. $where['owner_user_id'] = $userId;
  741. $where['end_time'] = ['between', [date('Y-m-d',time()), date('Y-m-d',time()+86400*$expireDay)]];
  742. $where['expire_remind'] = 1;
  743. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  744. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['expire_remind' => 0]);
  745. }
  746. # 处理待回访合同
  747. if ($type == 'returnVisitRemind') {
  748. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId; # 负责人
  749. $where['is_visit'] = 0; # 未回访
  750. $where['check_status'] = 2; # 审核通过
  751. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  752. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['is_visit' => 1]);
  753. }
  754. # 处理待进入公海
  755. if ($type == 'putInPoolRemind') {
  756. if (!empty($typeId)) {
  757. Db::name('crm_customer')->whereIn('customer_id', $typeId)->update([
  758. 'follow' => '已跟进',
  759. 'last_time' => time(),
  760. 'update_time' => time()
  761. ]);
  762. } else {
  763. $whereData['page'] = 1;
  764. $whereData['limit'] = 30;
  765. $whereData['is_remind'] = db('crm_config')->where('name', 'remind_config')->value('value');;
  766. $whereData['user_id'] = $userId;
  767. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => empty($isSub) ? 'me' : 'sub'])->value('scene_id');
  768. $poolCustomers = (new \app\crm\model\Customer())->getDataList($whereData);
  769. $ids = [];
  770. foreach ($poolCustomers['list'] AS $key => $value) {
  771. if (!empty($value['customer_id'])) $ids[] = $value['customer_id'];
  772. }
  773. if (!empty($ids)) Db::name('crm_customer')->whereIn('customer_id', $ids)->update([
  774. 'follow' => '已跟进',
  775. 'last_time' => time(),
  776. 'update_time' => time()
  777. ]);
  778. }
  779. }
  780. return resultArray(['data' => '操作成功!']);
  781. }
  782. }