Message.php 35KB

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