Message.php 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. if ($configData['visit_config'] == 1) {
  178. # 待回访合同
  179. $visitContractTime = cache('visitContractTime'.$userInfo['id']);
  180. $visitContractCount = cache('visitContractCount'.$userInfo['id']);
  181. if (time() <= $visitContractTime) {
  182. $data['returnVisitRemind'] = (int)$visitContractCount;
  183. } else {
  184. $visitContract = $this->visitContract(true);
  185. $data['returnVisitRemind'] = $visitContract['dataCount'] ? : 0;
  186. cache('visitContractCount'.$userInfo['id'], $data['returnVisitRemind']);
  187. cache('visitContractTime'.$userInfo['id'], time() + 180);
  188. }
  189. }
  190. # 即将到期合同
  191. if ($configData['contract_config'] == 1) {
  192. $endContractTime = cache('endContractTime'.$userInfo['id']);
  193. $endContractCount = cache('endContractCount'.$userInfo['id']);
  194. if (time() <= $endContractTime) {
  195. $data['endContract'] = (int)$endContractCount;
  196. } else {
  197. $endContract = $this->endContract(true);
  198. $data['endContract'] = $endContract['dataCount'] ? : 0;
  199. cache('endContractCount'.$userInfo['id'], $data['endContract']);
  200. cache('endContractTime'.$userInfo['id'], time() + 180);
  201. }
  202. }
  203. # 待进入公海提醒
  204. if ($configData['remind_config'] == 1) {
  205. $remindCustomerTime = cache('remindCustomerTime'.$userInfo['id']);
  206. $remindCustomerCount = cache('remindCustomerCount'.$userInfo['id']);
  207. if (time() <= $remindCustomerTime) {
  208. $data['putInPoolRemind'] = (int)$remindCustomerCount;
  209. } else {
  210. $remindCustomer = $this->remindCustomer(true);
  211. $data['putInPoolRemind'] = $remindCustomer['dataCount'] ? : 0;
  212. cache('remindCustomerCount'.$userInfo['id'], $data['putInPoolRemind']);
  213. cache('remindCustomerTime'.$userInfo['id'], time() + 180);
  214. }
  215. }
  216. return resultArray(['data' => $data]);
  217. }
  218. /**
  219. * 今日需联系线索
  220. *
  221. * @param false $getCount
  222. * @return array|\think\response\Json
  223. */
  224. public function todayLeads($getCount = false)
  225. {
  226. $param = $this->param;
  227. $userId = $this->userInfo['id'];
  228. $types = $param['types'];
  229. $type = !empty($param['type']) ? $param['type'] : 1;
  230. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  231. $todayTime = getTimeByType('today');
  232. unset($param['types']);
  233. unset($param['type']);
  234. unset($param['isSub']);
  235. if ($getCount == true) $param['getCount'] = 1;
  236. $leadsModel = new \app\crm\model\Leads();
  237. # 负责人
  238. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  239. # 类型:1今日需联系;2已逾期;3已联系
  240. switch ($type) {
  241. case '1' :
  242. $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  243. $param['is_dealt'] = ['neq', 1];
  244. break;
  245. case '2' :
  246. $param['next_time'] = ['between', [1, time()]];
  247. $param['is_dealt'] = ['neq', 1];
  248. break;
  249. case '3' :
  250. $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  251. $param['is_dealt'] = ['eq', 1];
  252. break;
  253. }
  254. $param['user_id'] = $userId;
  255. $data = $leadsModel->getDataList($param);
  256. if ($types == 'list') return resultArray(['data' => $data]);
  257. return $data;
  258. }
  259. /**
  260. * 今日需联系客户
  261. *
  262. * @param string $getCount
  263. * @return \think\response\Json
  264. */
  265. public function todayCustomer($getCount = false)
  266. {
  267. $param = $this->param;
  268. $userInfo = $this->userInfo;
  269. $types = $param['types'];
  270. $type = $param['type'] ? : 1;
  271. $isSub = $param['isSub'] ? : '';
  272. if ($getCount == true) {
  273. $param['getCount'] = 1;
  274. }
  275. unset($param['types']);
  276. unset($param['type']);
  277. unset($param['isSub']);
  278. $customerModel = model('Customer');
  279. $todayTime = getTimeByType('today');
  280. $param['owner_user_id'] = !empty($isSub) ? ['in',getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
  281. switch ($type) {
  282. case '1' :
  283. $param['next_time'] = ['between',array($todayTime[0],$todayTime[1])];
  284. $param['is_dealt'] = ['neq', 1];
  285. break;
  286. case '2' :
  287. $param['next_time'] = ['between',array(1,time())];
  288. $param['is_dealt'] = ['neq', 1];
  289. break;
  290. case '3' :
  291. $param['last_time'] = ['between',array($todayTime[0],$todayTime[1])];
  292. $param['is_dealt'] = ['eq', 1];
  293. break;
  294. }
  295. $data = $customerModel->getDataList($param);
  296. if ($types == 'list') {
  297. return resultArray(['data' => $data]);
  298. }
  299. return $data;
  300. }
  301. /**
  302. * 今日需联系商机
  303. *
  304. * @param false $getCount
  305. * @return array|\think\response\Json
  306. * @throws \think\db\exception\DataNotFoundException
  307. * @throws \think\db\exception\ModelNotFoundException
  308. * @throws \think\exception\DbException
  309. */
  310. public function todayBusiness($getCount = false)
  311. {
  312. $param = $this->param;
  313. $userId = $this->userInfo['id'];
  314. $types = $param['types'];
  315. $type = !empty($param['type']) ? $param['type'] : 1;
  316. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  317. $todayTime = getTimeByType('today');
  318. unset($param['types']);
  319. unset($param['type']);
  320. unset($param['isSub']);
  321. if ($getCount == true) $param['getCount'] = 1;
  322. $businessModel = new \app\crm\model\Business();
  323. # 负责人
  324. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  325. # 类型:1今日需联系;2已逾期;3已联系
  326. switch ($type) {
  327. case '1' :
  328. $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  329. $param['is_dealt'] = ['neq', 1];
  330. break;
  331. case '2' :
  332. $param['next_time'] = ['between', [1, time()]];
  333. $param['is_dealt'] = ['neq', 1];
  334. break;
  335. case '3' :
  336. $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  337. $param['is_dealt'] = ['eq', 1];
  338. break;
  339. }
  340. $param['user_id'] = $userId;
  341. $data = $businessModel->getDataList($param);
  342. if ($types == 'list') return resultArray(['data' => $data]);
  343. return $data;
  344. }
  345. /**
  346. * 分配给我的线索
  347. * @author Michael_xu
  348. * @return
  349. */
  350. public function followLeads($getCount = false)
  351. {
  352. $param = $this->param;
  353. $userInfo = $this->userInfo;
  354. $types = $param['types'];
  355. $type = $param['type'] ? : 1;
  356. $isSub = $param['isSub'] ? : '';
  357. unset($param['types']);
  358. unset($param['type']);
  359. unset($param['isSub']);
  360. $leadsModel = model('Leads');
  361. if ($getCount == true) $param['getCount'] = 1;
  362. $param['owner_user_id'] = $userInfo['id'];
  363. switch ($type) {
  364. case '1' :
  365. $param['follow'] = [['neq','已跟进'], null, 'or'];
  366. $param['is_allocation'] = 1;
  367. break;
  368. case '2' :
  369. $param['follow'] = ['eq','已跟进'];
  370. $param['is_allocation'] = 1;
  371. break;
  372. }
  373. $param['user_id'] = $userInfo['id'];
  374. $data = $leadsModel->getDataList($param);
  375. if ($types == 'list') {
  376. return resultArray(['data' => $data]);
  377. }
  378. return $data;
  379. }
  380. /**
  381. * 分配给我的客户
  382. * @author Michael_xu
  383. * @return
  384. */
  385. public function followCustomer($getCount = false)
  386. {
  387. $param = $this->param;
  388. $userInfo = $this->userInfo;
  389. $types = $param['types'];
  390. $type = $param['type'] ? : 1;
  391. $isSub = $param['isSub'] ? : '';
  392. if ($getCount == true) {
  393. $param['getCount'] = 1;
  394. }
  395. unset($param['types']);
  396. unset($param['type']);
  397. unset($param['isSub']);
  398. $customerModel = model('Customer');
  399. $param['owner_user_id'] = $userInfo['id'];
  400. switch ($type) {
  401. case '1' :
  402. $param['follow'] = [['eq','待跟进'], null, 'or'];
  403. $param['is_allocation'] = 1;
  404. break;
  405. case '2' :
  406. $param['follow'] = ['eq','已跟进'];
  407. $param['is_allocation'] = 1;
  408. break;
  409. }
  410. $data = $customerModel->getDataList($param);
  411. if ($types == 'list') {
  412. return resultArray(['data' => $data]);
  413. }
  414. return $data;
  415. }
  416. /**
  417. * 待审核合同
  418. *
  419. * @param false $getCount
  420. * @return \think\response\Json
  421. */
  422. public function checkContract($getCount = false)
  423. {
  424. $param = $this->param;
  425. $userInfo = $this->userInfo;
  426. $types = $param['types'];
  427. $type = $param['type'] ? : 1;
  428. $isSub = $param['isSub'] ? : '';
  429. unset($param['types']);
  430. unset($param['type']);
  431. unset($param['isSub']);
  432. $contractModel = model('Contract');
  433. if ($getCount == true) {
  434. $param['getCount'] = 1;
  435. }
  436. switch ($type) {
  437. case '1' :
  438. $param['check_status'] = ['lt','2'];
  439. $param['check_user_id'] = ['like',',%'.$userInfo['id'].'%,'];
  440. # 要提醒的合同ID
  441. $contractIdArray = db('crm_dealt_relation')->where(['types' => ['eq', 'crm_contract'], 'user_id' => ['eq', $userInfo['id']]])->column('types_id');
  442. $param['contractIdArray'] = !empty($contractIdArray) ? $contractIdArray : -1;
  443. break;
  444. case '2' :
  445. $param['flow_user_id'] = ['like',',%'.$userInfo['id'].'%,'];
  446. break;
  447. }
  448. $param['user_id'] = $userInfo['id'];
  449. $data = $contractModel->getDataList($param);
  450. if ($types == 'list') {
  451. return resultArray(['data' => $data]);
  452. }
  453. return $data;
  454. }
  455. /**
  456. * 待审核回款
  457. * @author Michael_xu
  458. * @return
  459. */
  460. public function checkReceivables($getCount = false)
  461. {
  462. $param = $this->param;
  463. $userInfo = $this->userInfo;
  464. $types = $param['types'];
  465. $type = $param['type'] ? : 1;
  466. $isSub = $param['isSub'] ? : '';
  467. unset($param['types']);
  468. unset($param['type']);
  469. unset($param['isSub']);
  470. $receivablesModel = model('Receivables');
  471. if ($getCount == true) $param['getCount'] = 1;
  472. switch ($type) {
  473. case '1' :
  474. # 待审核、审核中
  475. $param['check_status'] = ['lt','2'];
  476. $param['check_user_id'] = ['like',',%'.$userInfo['id'].'%,'];
  477. # 要提醒的回款ID
  478. $receivablesIdArray = db('crm_dealt_relation')->where(['types' => ['eq', 'crm_receivables'], 'user_id' => ['eq', $userInfo['id']]])->column('types_id');
  479. $param['receivablesIdArray'] = !empty($receivablesIdArray) ? $receivablesIdArray : -1;
  480. break;
  481. case '2' :
  482. # 全部
  483. $param['flow_user_id'] = ['like',',%'.$userInfo['id'].'%,'];
  484. break;
  485. }
  486. $param['user_id'] = $userInfo['id'];
  487. $data = $receivablesModel->getDataList($param);
  488. if ($types == 'list') {
  489. return resultArray(['data' => $data]);
  490. }
  491. return $data;
  492. }
  493. /**
  494. * 待审核发票
  495. *
  496. * @param InvoiceLogic $invoiceLogic
  497. * @return array|\think\response\Json
  498. * @throws \think\exception\DbException
  499. */
  500. public function checkInvoice($getCount = false)
  501. {
  502. $param = $this->param;
  503. $userId = $this->userInfo['id'];
  504. $types = $param['types'];
  505. $type = !empty($param['type']) ? $param['type'] : 1;
  506. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  507. if ($getCount == true) $param['getCount'] = 1;
  508. # 清除与模型无关的数据
  509. unset($param['types']);
  510. unset($param['type']);
  511. unset($param['isSub']);
  512. switch ($type) {
  513. case '1' :
  514. # 待审核、审核中
  515. $param['check_status'] = ['lt', 2];
  516. $param['check_user_id'] = ['like', ',%'. $userId .'%,'];
  517. # 要提醒的发票ID
  518. $invoiceIdArray = db('crm_dealt_relation')->where(['types' => ['eq', 'crm_invoice'], 'user_id' => ['eq', $userId]])->column('types_id');
  519. $param['invoiceIdArray'] = !empty($invoiceIdArray) ? $invoiceIdArray : -1;
  520. $param['dealt'] = 1;
  521. break;
  522. case '2' :
  523. # 全部
  524. $param['flow_user_id'] = ['like', ',%'. $userId .'%,'];
  525. $param['dealt'] = 1;
  526. break;
  527. }
  528. $data = (new InvoiceLogic())->index($param);
  529. if ($types == 'list') return resultArray(['data' => $data]);
  530. return $data;
  531. }
  532. /**
  533. * 待回款提醒
  534. * @author Michael_xu
  535. * @return
  536. */
  537. public function remindReceivablesPlan($getCount = false)
  538. {
  539. $param = $this->param;
  540. $userInfo = $this->userInfo;
  541. $types = $param['types'];
  542. $type = $param['type'] ? : 1;
  543. $isSub = $param['isSub'] ? : '';
  544. unset($param['types']);
  545. unset($param['type']);
  546. unset($param['isSub']);
  547. $receivablesPlanModel = model('ReceivablesPlan');
  548. if ($getCount == true) $param['getCount'] = 1;
  549. $param['owner_user_id'] = $userInfo['id'];
  550. if ($isSub) {
  551. $param['owner_user_id'] = array('in',getSubUserId(false));
  552. }
  553. switch ($type) {
  554. case '1' :
  555. $param['receivables_id'] = 0;
  556. $param['check_status'] = array('lt',2);
  557. $param['remind_date'] = array('elt',date('Y-m-d',time()));
  558. $param['return_date'] = array('egt',date('Y-m-d',time()));
  559. $param['types'] = 1;
  560. $param['is_dealt'] = 0;
  561. break;
  562. case '2' :
  563. $param['receivables_id'] = ['gt', 0];
  564. $param['check_status'] = 2;
  565. $param['dealt'] = 1;
  566. break;
  567. case '3' :
  568. $param['receivables_id'] = 0;
  569. $param['remind_date'] = array('lt',date('Y-m-d',time()));
  570. break;
  571. }
  572. $data = $receivablesPlanModel->getDataList($param);
  573. if ($types == 'list') {
  574. return resultArray(['data' => $data]);
  575. }
  576. return $data;
  577. }
  578. /**
  579. * 即将到期合同
  580. * @author Michael_xu
  581. * @return
  582. */
  583. public function endContract($getCount = false)
  584. {
  585. $param = $this->param;
  586. $userInfo = $this->userInfo;
  587. $types = $param['types'];
  588. $type = $param['type'] ? : 1;
  589. $isSub = $param['isSub'] ? : '';
  590. if ($getCount == true) $param['getCount'] = 1;
  591. unset($param['types']);
  592. unset($param['type']);
  593. unset($param['isSub']);
  594. $contractModel = model('Contract');
  595. $configModel = new \app\crm\model\ConfigData();
  596. $configInfo = $configModel->getData();
  597. $expireDay = $configInfo['contract_day'] ? : '7';
  598. $param['owner_user_id'] = $userInfo['id'];
  599. if ($isSub) {
  600. $param['owner_user_id'] = array('in',getSubUserId(false));
  601. }
  602. switch ($type) {
  603. case '1' :
  604. $param['end_time'] = array('between',array(date('Y-m-d',time()),date('Y-m-d',time()+86400*$expireDay)));
  605. $param['expire_remind'] = 1;
  606. break;
  607. case '2' : $param['end_time'] = array('lt',date('Y-m-d',time())); break;
  608. }
  609. $data = $contractModel->getDataList($param);
  610. if ($types == 'list') {
  611. return resultArray(['data' => $data]);
  612. }
  613. return $data;
  614. }
  615. /**
  616. * 待进入客户池(默认5天)
  617. * @author Michael_xu
  618. * @return
  619. */
  620. public function remindCustomer($getCount = false)
  621. {
  622. $customerModel = model('Customer');
  623. $param = $this->param;
  624. $userInfo = $this->userInfo;
  625. $types = $param['types'];
  626. $isSub = $param['isSub'] ? : '';
  627. if ($getCount == true) $param['getCount'] = 1;
  628. unset($param['types']);
  629. unset($param['type']);
  630. unset($param['isSub']);
  631. unset($param['deal_status']);
  632. unset($param['owner_user_id']);
  633. # 负责人
  634. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
  635. # 是否提醒
  636. $remind = db('crm_config')->where('name', 'remind_config')->value('value');
  637. $whereData = $param ? : [];
  638. $whereData['is_remind'] = !empty($remind) ? 1 : 0;
  639. $whereData['user_id'] = $userInfo['id'];
  640. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'me'])->value('scene_id');
  641. if ($isSub) {
  642. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'sub'])->value('scene_id');
  643. }
  644. $data = $customerModel->getDataList($whereData);
  645. if ($types == 'list') {
  646. return resultArray(['data' => $data]);
  647. }
  648. return $data;
  649. }
  650. /**
  651. * 待回访合同
  652. *
  653. * @param false $getCount
  654. * @return array|\think\response\Json
  655. * @throws \think\db\exception\DataNotFoundException
  656. * @throws \think\db\exception\ModelNotFoundException
  657. * @throws \think\exception\DbException
  658. */
  659. public function visitContract($getCount = false)
  660. {
  661. $param = $this->param;
  662. $userId = $this->userInfo['id'];
  663. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  664. $types = !empty($param['types']) ? $param['types'] : '';
  665. if ($getCount == true) $param['getCount'] = 1;
  666. unset($param['isSub']);
  667. unset($param['types']);
  668. $param['is_visit'] = 0; # 未回访
  669. $param['check_status'] = 2; # 审核通过
  670. $contractModel = new \app\crm\model\Contract();
  671. # 负责人
  672. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  673. $param['user_id'] = $userId;
  674. $data = $contractModel->getDataList($param);
  675. if ($types == 'list') return resultArray(['data' => $data]);
  676. return $data;
  677. }
  678. /**
  679. * 全部标记已处理
  680. *
  681. * @return \think\response\Json
  682. * @throws \think\Exception
  683. * @throws \think\exception\PDOException
  684. */
  685. public function allDeal()
  686. {
  687. $type = $this->param['type'];
  688. $typeId = !empty($this->param['type_id']) ? $this->param['type_id'] : '';
  689. $isSub = !empty($this->param['isSub']) ? $this->param['isSub'] : 0;
  690. $userId = $this->userInfo['id'];
  691. if (empty($type)) return resultArray(['error' => '缺少模块类型参数']);
  692. # 获得今日开始和结束时间戳
  693. $todayTime = getTimeByType('today');
  694. # 处理今日需联系线索、客户、商机
  695. if (in_array($type, ['todayLeads', 'todayCustomer', 'todayBusiness'])) {
  696. # 负责人
  697. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  698. # 下次联系时间
  699. $where['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  700. # 是否已处理(联系)
  701. $where['is_dealt'] = 0;
  702. # 线索
  703. if ($type == 'todayLeads') {
  704. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  705. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update([
  706. 'last_time' => time(),
  707. 'is_dealt' => 1,
  708. 'follow' => '已跟进'
  709. ]);
  710. }
  711. # 客户
  712. if ($type == 'todayCustomer') {
  713. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  714. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update([
  715. 'last_time' => time(),
  716. 'is_dealt' => 1,
  717. 'follow' => '已跟进'
  718. ]);
  719. }
  720. # 商机
  721. if ($type == 'todayBusiness') {
  722. $businessId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
  723. Db::name('crm_business')->whereIn('business_id', $businessId)->update([
  724. 'last_time' => time(),
  725. 'is_dealt' => 1
  726. ]);
  727. }
  728. }
  729. # 处理分配给我的线索、客户
  730. if (in_array($type, ['followLeads', 'followCustomer'])) {
  731. $where['owner_user_id'] = $userId;
  732. $where['follow'] = [['neq','已跟进'], null, 'or'];
  733. $where['is_allocation'] = 1;
  734. # 线索
  735. if ($type == 'followLeads') {
  736. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  737. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update(['follow' => '已跟进']);
  738. }
  739. # 客户
  740. if ($type == 'followCustomer') {
  741. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  742. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update(['follow' => '已跟进']);
  743. }
  744. }
  745. # 处理待审核合同、回款、发票
  746. if (in_array($type, ['checkContract', 'checkReceivables', 'checkInvoice'])) {
  747. $where['check_status'] = ['lt','2'];
  748. $where['check_user_id'] = ['like',',%' . $userId . '%,'];
  749. # 合同
  750. if ($type == 'checkContract') {
  751. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  752. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_contract')->whereIn('types_id', $contractId)->delete();
  753. }
  754. # 回款
  755. if ($type == 'checkReceivables') {
  756. $receivablesId = !empty($typeId) ? $typeId : Db::name('crm_receivables')->where($where)->column('receivables_id');
  757. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_receivables')->whereIn('types_id', $receivablesId)->delete();
  758. }
  759. # 发票
  760. if ($type == 'checkInvoice') {
  761. $invoiceId = !empty($typeId) ? $typeId : Db::name('crm_invoice')->where($where)->column('invoice_id');
  762. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_invoice')->whereIn('types_id', $invoiceId)->delete();
  763. }
  764. }
  765. # 处理到期合同
  766. if ($type == 'endContract') {
  767. $configModel = new \app\crm\model\ConfigData();
  768. $configInfo = $configModel->getData();
  769. $expireDay = $configInfo['contract_day'] ? : '7';
  770. $where['owner_user_id'] = $userId;
  771. $where['end_time'] = ['between', [date('Y-m-d',time()), date('Y-m-d',time()+86400*$expireDay)]];
  772. $where['expire_remind'] = 1;
  773. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  774. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['expire_remind' => 0]);
  775. }
  776. # 处理待回访合同
  777. if ($type == 'returnVisitRemind') {
  778. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId; # 负责人
  779. $where['is_visit'] = 0; # 未回访
  780. $where['check_status'] = 2; # 审核通过
  781. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  782. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['is_visit' => 1]);
  783. }
  784. # 处理待进入公海
  785. if ($type == 'putInPoolRemind') {
  786. if (!empty($typeId)) {
  787. Db::name('crm_customer')->whereIn('customer_id', $typeId)->update([
  788. 'follow' => '已跟进',
  789. 'last_time' => time(),
  790. 'deal_time' => time(),
  791. 'update_time' => time(),
  792. ]);
  793. } else {
  794. $whereData['page'] = 1;
  795. $whereData['limit'] = 100;
  796. $whereData['is_remind'] = db('crm_config')->where('name', 'remind_config')->value('value');;
  797. $whereData['user_id'] = $userId;
  798. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => empty($isSub) ? 'me' : 'sub'])->value('scene_id');
  799. $whereData['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  800. $poolCustomers = (new \app\crm\model\Customer())->getDataList($whereData);
  801. $ids = [];
  802. foreach ($poolCustomers['list'] AS $key => $value) {
  803. if (!empty($value['customer_id'])) $ids[] = $value['customer_id'];
  804. }
  805. if (!empty($ids)) Db::name('crm_customer')->whereIn('customer_id', $ids)->update([
  806. 'follow' => '已跟进',
  807. 'last_time' => time(),
  808. 'deal_time' => time(),
  809. 'update_time' => time(),
  810. ]);
  811. }
  812. }
  813. # 带回款提醒
  814. if ($type == 'remindReceivablesPlan') {
  815. $planId = [];
  816. if (!empty($typeId)) {
  817. $planId = $typeId;
  818. } else {
  819. $param['owner_user_id'] = $isSub ? ['in',getSubUserId(false)] : $userId;
  820. $param['receivables_id'] = 0;
  821. $param['check_status'] = ['lt', 2];
  822. $param['remind_date'] = ['elt', date('Y-m-d',time())];
  823. $param['return_date'] = ['egt', date('Y-m-d',time())];
  824. $param['types'] = 1;
  825. $param['page'] = 1;
  826. $param['limit'] = 1000;
  827. $receivablesPlanModel = model('ReceivablesPlan');
  828. $data = $receivablesPlanModel->getDataList($param);
  829. foreach ($data['list'] AS $key => $value) {
  830. $planId[] = $value['plan_id'];
  831. }
  832. }
  833. if (!empty($planId)) db('crm_receivables_plan')->whereIn('plan_id', $planId)->update(['is_dealt' => 1]);
  834. }
  835. return resultArray(['data' => '操作成功!']);
  836. }
  837. }