Index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Contract as ContractModel;
  4. use addons\qingdong\model\Event;
  5. use addons\qingdong\model\ExamineRecord;
  6. use addons\qingdong\model\Feedback;
  7. use addons\qingdong\model\ReceivablesPlan;
  8. use addons\qingdong\model\Customer;
  9. use addons\qingdong\model\Contacts;
  10. use addons\qingdong\model\Contract;
  11. use addons\qingdong\model\Leads;
  12. use addons\qingdong\model\Receivables;
  13. use addons\qingdong\model\Staff;
  14. use addons\qingdong\model\Record;
  15. use addons\qingdong\model\Message;
  16. use addons\qingdong\model\Business;
  17. /**
  18. * * 操作文档:https://doc.fastadmin.net/qingdong
  19. * 软件介绍:https://www.fastadmin.net/store/qingdong.html
  20. * 售后微信:qingdong_crm
  21. * 首页接口
  22. */
  23. class Index extends StaffApi
  24. {
  25. protected $noNeedLogin = [];
  26. protected $noNeedRight = ['*'];
  27. public function index()
  28. {
  29. $this->error("当前插件暂无前台页面");
  30. }
  31. //搜索
  32. public function search()
  33. {
  34. $keywords = input('keywords');
  35. $type = input('type');
  36. if (empty($keywords)) {
  37. $this->error('筛选字段不能为空');
  38. }
  39. //客户
  40. $customer = Customer::where([
  41. 'name' => ['like', "%$keywords%"],
  42. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  43. ])->with([
  44. 'ownerStaff',
  45. 'contacts'
  46. ])->field('id,name,next_time,owner_staff_id,level,follow')->select();
  47. //联系人
  48. $contacts = Contacts::where([
  49. 'name' => ['like', "%$keywords%"],
  50. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  51. ])->with(['customer'])->field('id,name,mobile,customer_id')->select();
  52. //线索
  53. $leads = Leads::where([
  54. 'name' => ['like', "%$keywords%"],
  55. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  56. ])->with(['ownerStaff'])->field('id,owner_staff_id,name,follow,level,next_time,mobile')->select();
  57. $this->success('请求成功', [
  58. 'customer' => $customer,
  59. 'contacts' => $contacts,
  60. 'leads' => $leads
  61. ]);
  62. }
  63. //反馈
  64. public function feedback()
  65. {
  66. $content = input('content', '', 'trim');
  67. $file = input('file', '', 'trim');
  68. if (empty($content)) {
  69. $this->error('请输入反馈内容!');
  70. }
  71. $model = new Feedback();
  72. $model->save([
  73. 'staff_id' => $this->auth->id,
  74. 'content' => $content,
  75. 'file_ids' => $file
  76. ]);
  77. $this->success('反馈成功');
  78. }
  79. //待办事项
  80. public function agent()
  81. {
  82. $where['create_staff_id'] = $this->auth->id;
  83. $where['next_time'] = array(array('egt', date('Y-m-d 00:00:00')), array('lt', date('Y-m-d 23:59:59')));
  84. $where['follow_type'] = ['neq', '其它'];
  85. $where['status'] = 0;
  86. // 待跟进客户
  87. $where1['relation_type'] = 1;
  88. $customerlist = Record::where($where)->where($where1)->column('id');
  89. $customerlist1 = 0;
  90. if ($customerlist) {
  91. $whereExit['id'] = array('in', $customerlist);
  92. $whereExit['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  93. $customerlist1 = Record::where($whereExit)->count();
  94. }
  95. $customer = count($customerlist) - $customerlist1;
  96. //待跟进合同
  97. $where2['relation_type'] = 3;
  98. $contractlist = Record::where($where)->where($where2)->column('id');
  99. $contractlist1 = 0;
  100. if ($contractlist) {
  101. $whereExitC['id'] = array('in', $contractlist);
  102. $whereExitC['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  103. $contractlist1 = Record::where($whereExitC)->count();
  104. }
  105. $contract = count($contractlist) - $contractlist1;
  106. //待跟进线索
  107. $where3['relation_type'] = 4;
  108. $leadlist = Record::where($where)->where($where3)->column('id');
  109. $leadlist1 = 0;
  110. if ($leadlist) {
  111. $whereExitL['id'] = array('in', $leadlist);
  112. $whereExitL['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  113. $leadlist1 = Record::where($whereExitL)->count();
  114. }
  115. $lead = count($leadlist) - $leadlist1;
  116. //待跟进联系人
  117. $where4['relation_type'] = 2;
  118. $contactslist = Record::where($where)->where($where4)->column('id');
  119. $contactslist1 = 0;
  120. if ($contactslist1) {
  121. $whereExitCs['id'] = array('in', $contactslist);
  122. $whereExitCs['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  123. $contactslist1 = Record::where($whereExitCs)->count();
  124. }
  125. $contacts = count($contactslist) - $contactslist1;
  126. //待跟进商机
  127. $where5['relation_type'] = 5;
  128. $businesslist = Record::where($where)->where($where5)->column('id');
  129. $businesslist1 = 0;
  130. if ($businesslist1) {
  131. $whereExitB['id'] = array('in', $businesslist);
  132. $whereExitB['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  133. $businesslist1 = Record::where($whereExitB)->count();
  134. }
  135. $business = count($businesslist) - $businesslist1;
  136. //待审核合同
  137. $examine = ExamineRecord::where([
  138. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  139. 'status' => 0,
  140. 'check_staff_id' => $this->auth->id
  141. ])->count();
  142. //待审核回款
  143. $receivables = ExamineRecord::where([
  144. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  145. 'status' => 0,
  146. 'check_staff_id' => $this->auth->id
  147. ])->count();
  148. //待回款提醒
  149. $receivablesPlan = ReceivablesPlan::where([
  150. 'remind_date' => ['elt', date('Y-m-d')],
  151. 'status' => 0,
  152. 'owner_staff_id' => $this->auth->id
  153. ])->count();
  154. //待处理日程
  155. $eventOne = Event::where([
  156. 'type' => 1,
  157. 'status' => ['in', [0, 1]],
  158. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  159. 'staff_id|owner_staff_id' => $this->auth->id,
  160. ])->count();
  161. $this->success('请求成功', [
  162. 'customer' => $customer,
  163. 'business' => $business,
  164. 'contract' => $contract,
  165. 'lead' => $lead,
  166. 'contacts' => $contacts,
  167. 'event_one' => $eventOne,
  168. 'examine' => $examine,
  169. 'receivables' => $receivables,
  170. 'receivables_plan' => $receivablesPlan
  171. ]);
  172. }
  173. //待处理日程
  174. public function event_one()
  175. {
  176. // 待处理日程
  177. $eventTask = Event::where([
  178. 'type' => 1,
  179. 'status' => ['in', [0, 1]],
  180. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  181. 'staff_id|owner_staff_id' => $this->auth->id,
  182. ])->order('id desc')->select();
  183. //标记通知已读
  184. Message::where([
  185. 'relation_type' => Message::EVENT_TYPE,
  186. 'to_staff_id' => $this->auth->id,
  187. 'status' => 0
  188. ])->update(['read_time' => time(), 'status' => 1]);
  189. foreach ($eventTask as &$ves) {
  190. $ves['start_time'] = date('Y-m-d H:i', strtotime($ves['start_time']));
  191. $ves['end_time'] = date('Y-m-d H:i', strtotime($ves['end_time']));
  192. }
  193. $this->success('请求成功', $eventTask);
  194. }
  195. //跟进任务
  196. public function event_task()
  197. {
  198. // 待跟进任务
  199. $eventTask = Event::where([
  200. 'type' => 2,
  201. 'status' => ['in', [0, 1]],
  202. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  203. 'staff_id' => $this->auth->id
  204. ])->select();
  205. foreach ($eventTask as &$ves) {
  206. $ves['start_time'] = date('Y-m-d H:i', strtotime($ves['start_time']));
  207. $ves['end_time'] = date('Y-m-d H:i', strtotime($ves['end_time']));
  208. }
  209. $this->success('请求成功', $eventTask);
  210. }
  211. //待审核合同
  212. public function examine_contract()
  213. {
  214. //待审核合同
  215. $ids = ExamineRecord::where([
  216. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  217. 'status' => 0,
  218. 'check_staff_id' => $this->auth->id
  219. ])->column('relation_id');
  220. $contracts = ContractModel::where(['id' => ['in', $ids]])->with([
  221. 'customer',
  222. 'contacts',
  223. 'ownerStaff',
  224. 'orderStaff',
  225. 'receivables'
  226. ])->order('id desc')->select();
  227. $contracts = collection($contracts)->toArray();
  228. foreach ($contracts as $k => $v) {
  229. if (empty($v['receivables'])) {
  230. $v['receivables'] = [
  231. 'repayment_money' => 0,
  232. 'be_money' => $v['money'],
  233. 'ratio' => 0
  234. ];
  235. } else {
  236. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  237. $v['receivables'] = [
  238. 'repayment_money' => $v['receivables']['repayment_money'],
  239. 'be_money' => ($be_money > 0) ? $be_money : 0,
  240. 'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)
  241. ];
  242. }
  243. $contracts[$k] = $v;
  244. }
  245. $this->success('请求成功', $contracts);
  246. }
  247. //待审核回款
  248. public function examine_receivables()
  249. {
  250. //待审核回款
  251. $ids = ExamineRecord::where([
  252. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  253. 'status' => 0,
  254. 'check_staff_id' => $this->auth->id,
  255. ])->column('relation_id');
  256. $receivables = Receivables::where(['id' => ['in', $ids]])->with([
  257. 'contract',
  258. 'createStaff'
  259. ])->order('id desc')->select();
  260. $this->success('请求成功', $receivables);
  261. }
  262. //待回款提醒
  263. public function examine_receivables_plan()
  264. {
  265. //待回款提醒
  266. $receivablesPlan = ReceivablesPlan::where([
  267. 'remind_date' => ['elt', date('Y-m-d')],
  268. 'status' => 0,
  269. 'owner_staff_id' => $this->auth->id,
  270. ])->order('id desc')->with(['contract', 'customer'])->select();
  271. $this->success('请求成功', $receivablesPlan);
  272. }
  273. //数据简报
  274. public function briefing()
  275. {
  276. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属
  277. $times = input('times', '');
  278. $times = explode(',', $times);
  279. if (empty($times)) {
  280. $this->error('参数不能为空');
  281. }
  282. $startDate=strtotime($times[0]);
  283. $endDate=strtotime($times[1])+86400-1;
  284. $where = [
  285. 'createtime' => ['between', [$startDate, $endDate]]
  286. ];
  287. $whereC = [
  288. 'order_date' => ['between', [date('Y-m-d 00:00:00',$startDate), date('Y-m-d 23:59:59',$endDate)]]
  289. ];
  290. $whereR = [
  291. 'return_time' => ['between', [date('Y-m-d 00:00:00',$startDate), date('Y-m-d 23:59:59',$endDate)]]
  292. ];
  293. if ($type == 1) {
  294. $where['owner_staff_id'] = $this->auth->id;
  295. $whereC['owner_staff_id'] = $this->auth->id;
  296. $whereR['owner_staff_id'] = $this->auth->id;
  297. } elseif ($type == 2) {
  298. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  299. $whereC['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  300. $whereR['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  301. } else {
  302. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  303. $whereC['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  304. $whereR['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  305. }
  306. //客户 线索 联系人 合同 回款 跟进次数 处理审批
  307. $leads = Leads::where($where)->count();
  308. $customer = Customer::where($where)->count();
  309. $contacts = Contacts::where($where)->count();
  310. $contract = Contract::where([])->where($whereC)->count();
  311. $contract_money = Contract::where(['check_status'=>2])->where($whereC)->sum('money');
  312. $receivables_money = Receivables::where(['check_status'=>2])->where($whereR)->sum('money');
  313. $record = Record::where([
  314. 'create_staff_id' => $where['owner_staff_id'],
  315. 'createtime' => $where['createtime']
  316. ])->where(['follow_type' => ['neq', '其它']])->count();
  317. $examine = ExamineRecord::where([
  318. 'check_staff_id' => $where['owner_staff_id'],
  319. 'createtime' => $where['createtime']
  320. ])->count();
  321. //商机总数
  322. $business = Business::where($where)->count();
  323. //客户成交量
  324. $customer_complate = Customer::where($where)->where(['contract_status' => 1])->count();
  325. //客户未成交量
  326. $customer_nocomplate = Customer::where($where)->where(['contract_status' => 0])->count();
  327. //商机成交总数
  328. $business_complate = Business::where($where)->where(['contract_status' => 1])->count();
  329. //商机未成交总数
  330. $business_nocomplate = Business::where($where)->where(['contract_status' => 0])->count();
  331. //商机成交金额
  332. $business_complate_money = Business::where($where)->where(['contract_status' => 1])->sum('money');
  333. //商机未成交金额
  334. $business_nocomplate_money = Business::where($where)->where(['contract_status' => 0])->sum('money');
  335. $this->success('请求成功', [
  336. 'leads' => $leads,
  337. 'customer' => $customer,
  338. 'contacts' => $contacts,
  339. 'contract' => $contract,
  340. 'contract_money' => $contract_money,
  341. 'receivables' => $receivables_money,
  342. 'record' => $record,
  343. 'business' => $business,
  344. 'examine' => $examine,
  345. 'customer_complate' => $customer_complate,
  346. 'customer_nocomplate' => $customer_nocomplate,
  347. 'business_complate' => $business_complate,
  348. 'business_nocomplate' => $business_nocomplate,
  349. 'business_complate_money' => $business_complate_money,
  350. 'business_nocomplate_money' => $business_nocomplate_money,
  351. ]);
  352. }
  353. /**
  354. *交易额排行
  355. */
  356. public function contractRanding()
  357. {
  358. $date = input('date', date('Y-m'));
  359. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属
  360. //月底
  361. $endDate = date('Y-m-d', strtotime('+1 month', strtotime(date($date . '-1'))) - 1);
  362. $where = [
  363. 'order_date' => ['between', [$date . '-1', $endDate]],
  364. 'check_status' => 2,
  365. ];
  366. $contracts = Contract::where($where)->group('owner_staff_id')->field('owner_staff_id,sum(money) as money')->order('money desc')->select();
  367. $list = [];
  368. foreach ($contracts as $v) {
  369. $list[$v['owner_staff_id']] = $v['money'];
  370. }
  371. $contracts = $list;
  372. $data = [];
  373. $staffs = Staff::getList();
  374. foreach ($staffs as $v) {
  375. if (isset($contracts[$v['id']])) {
  376. $data[$v['id']] = $contracts[$v['id']];
  377. } else {
  378. $data[$v['id']] = 0;
  379. }
  380. }
  381. arsort($data);
  382. $staffs = Staff::getKeyList();
  383. $result = [];
  384. $i = 1;
  385. $oneMoney = 0;
  386. if ($type == 1) {//本人
  387. $showStaffIds = [$this->auth->id];
  388. } elseif ($type == 2) {//下属
  389. $showStaffIds = Staff::getLowerStaffId();
  390. } else {//全部
  391. $showStaffIds = Staff::getMyStaffIds();
  392. }
  393. foreach ($data as $id => $money) {
  394. if ($i == 1) {
  395. $oneMoney = $money;
  396. }
  397. $val = $staffs[$id];
  398. $val['money'] = $money;
  399. $val['ratio'] = $oneMoney ? sprintf("%.2f", $money / $oneMoney * 100) : 0;
  400. $val['rank'] = $i;
  401. $i++;
  402. if (in_array($id, $showStaffIds)) {
  403. $result[] = $val;
  404. }
  405. }
  406. if (count($result) >= 10) {
  407. $top = array_slice($result, 0, 3);
  408. $bottom = array_slice($result, -3, 3);
  409. $middle = array_slice($result, 3, 4);
  410. $result = array_merge($top, $bottom, $middle);
  411. }
  412. $this->success('请求成功', $result);
  413. }
  414. //服务协议
  415. public function serviceContent()
  416. {
  417. $this->success('请求成功', [
  418. 'content' => "<h1>用户服务协议</h1>",
  419. 'name' => '青动时代',
  420. 'logo' => ''
  421. ]);
  422. }
  423. }