Index.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Achievement;
  4. use addons\qingdongams\model\Event;
  5. use addons\qingdongams\model\Feedback;
  6. use addons\qingdongams\model\PartsStockReload;
  7. use addons\qingdongams\model\Customer;
  8. use addons\qingdongams\model\Contacts;
  9. use addons\qingdongams\model\Contract;
  10. use addons\qingdongams\model\Leads;
  11. use addons\qingdongams\model\Receivables;
  12. use addons\qingdongams\model\Staff;
  13. use addons\qingdongams\model\Business;
  14. use addons\qingdongams\model\Workorder;
  15. use addons\qingdongams\model\Parts;
  16. use addons\qingdongams\model\Approval;
  17. use addons\qingdongams\model\Consume;
  18. use addons\qingdongams\model\Quote;
  19. use addons\qingdongams\model\Record;
  20. use addons\qingdongams\model\ExamineRecord;
  21. use addons\qingdongams\model\ReceivablesPlan;
  22. use think\Db;
  23. use think\Exception;
  24. /**
  25. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  26. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  27. * @desc 售后微信:qingdong_crm
  28. */
  29. /**
  30. * 首页接口
  31. */
  32. class Index extends StaffApi
  33. {
  34. protected $noNeedLogin = [];
  35. protected $noNeedRight = ['*'];
  36. public function _initialize()
  37. {
  38. parent::_initialize();
  39. try{
  40. \think\Db::execute("SET @@sql_mode='';");
  41. }catch (Exception $e){
  42. }
  43. }
  44. //数据简报
  45. public function briefing()
  46. {
  47. $times = input('times');//2021-01,2021-07
  48. $type = input('type', 2);//2下属 1本人 0团队
  49. if (empty($times)) {
  50. $this->error('筛选时间不能为空');
  51. }
  52. $whereStaff2=[];
  53. if ($type == 1) {//我的
  54. $whereStaff2['staff_id'] = $this->auth->id;
  55. } elseif ($type == 2) {//下属
  56. $whereStaff2['staff_id'] = ['in', Staff::getLowerStaffId()];
  57. } else {
  58. $whereStaff2['staff_id'] = ['in', Staff::getMyStaffIds()];
  59. }
  60. $times = setTimes($times, 'datetime');
  61. $startTime = strtotime($times[0]);
  62. $endTime = strtotime($times[1]);
  63. $where = ['createtime' => ['between', [$startTime, $endTime]]];
  64. //拜访次数
  65. $baifang = Event::where(['type' => ['neq',4]])->where($where)->where($whereStaff2)->count();
  66. $where = [
  67. 'createtime' => ['between', [$startTime, $endTime]]
  68. ];
  69. $whereC = [
  70. 'order_date' => ['between', [date('Y-m-d 00:00:00',$startTime), date('Y-m-d 23:59:59',$endTime)]]
  71. ];
  72. $whereR = [
  73. 'return_time' => ['between', [date('Y-m-d 00:00:00',$startTime), date('Y-m-d 23:59:59',$endTime)]]
  74. ];
  75. if ($type == 1) {
  76. $where['owner_staff_id'] = $this->auth->id;
  77. $whereC['owner_staff_id'] = $this->auth->id;
  78. $whereR['owner_staff_id'] = $this->auth->id;
  79. } elseif ($type == 2) {
  80. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  81. $whereC['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  82. $whereR['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  83. } else {
  84. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  85. $whereC['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  86. $whereR['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  87. }
  88. //报价次数
  89. $quote = Quote::where($where)->count();
  90. //客户 线索 联系人 合同 回款 跟进次数 处理审批
  91. $leads = Leads::where($where)->count();
  92. $customer = Customer::where($where)->count();
  93. $contacts = Contacts::where($where)->count();
  94. $contract = Contract::where(['check_status'=>2])->where($whereC)->count();
  95. $contract_money = Contract::where(['check_status' => 2])->where($whereC)->sum('money');
  96. $receivables_money = Receivables::where(['check_status' => 2])->where($whereR)->sum('money');
  97. $record = Record::where([
  98. 'create_staff_id' => $where['owner_staff_id'],
  99. 'createtime' => $where['createtime']
  100. ])->where(['follow_type' => ['neq', '其它']])->count();
  101. //商机总数
  102. $business = Business::where($where)->count();
  103. //客户成交量
  104. $customer_complate = Customer::where($where)->where(['contract_status' => 1])->count();
  105. //客户未成交量
  106. $customer_nocomplate = Customer::where($where)->where(['contract_status' => 0])->count();
  107. //商机成交总数
  108. $business_complate = Business::where($where)->where(['contract_status' => 1])->count();
  109. //商机未成交总数
  110. $business_nocomplate = Business::where($where)->where(['contract_status' => 0])->count();
  111. //商机成交金额
  112. $business_complate_money = Business::where($where)->where(['contract_status' => 1])->sum('money');
  113. //商机未成交金额
  114. $business_nocomplate_money = Business::where($where)->where(['contract_status' => 0])->sum('money');
  115. $this->success('请求成功', [
  116. 'baifang' => $baifang,
  117. 'quote' => $quote,
  118. 'leads' => $leads,
  119. 'customer' => $customer,
  120. 'contacts' => $contacts,
  121. 'contract' => $contract,
  122. 'contract_money' => $contract_money,
  123. 'receivables' => $receivables_money,
  124. 'record' => $record,
  125. 'business' => $business,
  126. 'customer_complate' => $customer_complate,
  127. 'customer_nocomplate' => $customer_nocomplate,
  128. 'business_complate' => $business_complate,
  129. 'business_nocomplate' => $business_nocomplate,
  130. 'business_complate_money' => $business_complate_money,
  131. 'business_nocomplate_money' => $business_nocomplate_money,]);
  132. }
  133. //售后数据简报
  134. public function afterBriefing()
  135. {
  136. $todayStart = strtotime(date('Y-m-d'));
  137. $todayEnd = strtotime(date('Y-m-d')) + 86400 - 1;
  138. $where = function ($query) {
  139. $query->where('create_staff_id|owner_staff_id', 'in', Staff::getMyStaffIds())
  140. ->whereOr('', 'exp', "FIND_IN_SET({$this->auth->id},show_staff_id)");
  141. };
  142. $peisong = Workorder::where([ 'createtime' => ['between', [$todayStart, $todayEnd]], 'workorder_type' => '产品发货'])->where($where)->count();
  143. $anzhuang = Workorder::where([ 'createtime' => ['between', [$todayStart, $todayEnd]], 'workorder_type' => '上门安装'])->where($where)->count();
  144. $shangmeng = Workorder::where([ 'createtime' => ['between', [$todayStart, $todayEnd]], 'workorder_type' => '上门维修'])->where($where)->count();
  145. $fanchang = Workorder::where([ 'createtime' => ['between', [$todayStart, $todayEnd]], 'workorder_type' => '返厂维修'])->where($where)->count();
  146. $this->success('请求成功', ['peisong' => $peisong, 'anzhuang' => $anzhuang, 'shangmeng' => $shangmeng, 'fanchang' => $fanchang]);
  147. }
  148. //获取售后目标
  149. public function getAfterAchievement()
  150. {
  151. $times = input('times');//2021-01,2021-07
  152. $team_type = input('type', 2);//3我的 2我的团队
  153. if (empty($times)) {
  154. $this->error('筛选时间不能为空');
  155. }
  156. if ($team_type == 3) {//我的
  157. $whereStaff['create_staff_id'] = $this->auth->id;
  158. } elseif ($team_type == 2) {//团队
  159. $whereStaff = function ($query) {
  160. $query->where('create_staff_id|owner_staff_id', 'in', Staff::getMyStaffIds())
  161. ->whereOr('', 'exp', "FIND_IN_SET({$this->auth->id},show_staff_id)");
  162. };
  163. } else {
  164. $this->error('参数错误');
  165. }
  166. $times = setTimes($times, 'datetime');
  167. $startTime = strtotime($times[0]);
  168. $endTime = strtotime($times[1]);
  169. $where = [];
  170. $where['createtime']= ['between', [$startTime, $endTime]];
  171. $peisong = Workorder::where(['workorder_type' => '产品发货'])->where($where)->where($whereStaff)->count();
  172. $anzhuang = Workorder::where(['workorder_type' => '上门安装'])->where($where)->where($whereStaff)->count();
  173. $shangmeng = Workorder::where(['workorder_type' => '上门维修'])->where($where)->where($whereStaff)->count();
  174. $fanchang = Workorder::where(['workorder_type' => '返厂维修'])->where($where)->where($whereStaff)->count();
  175. $peisong_w = Workorder::where(['workorder_type' => '产品发货', 'status' => 3])->where($where)->where($whereStaff)->count();
  176. $anzhuang_w = Workorder::where(['workorder_type' => '上门安装', 'status' => 3])->where($where)->where($whereStaff)->count();
  177. $shangmeng_w = Workorder::where(['workorder_type' => '上门维修', 'status' => 3])->where($where)->where($whereStaff)->count();
  178. $fanchang_w = Workorder::where(['workorder_type' => '返厂维修', 'status' => 3])->where($where)->where($whereStaff)->count();
  179. $this->success('请求成功', [
  180. 'peisong' => [
  181. 'achievement' => $peisong,
  182. 'count' => $peisong_w,
  183. 'ratio' => getRatio($peisong_w, $peisong)
  184. ],
  185. 'anzhuang' => [
  186. 'achievement' => $anzhuang,
  187. 'count' => $anzhuang_w,
  188. 'ratio' => getRatio($anzhuang_w, $anzhuang)
  189. ],
  190. 'shangmeng' => [
  191. 'achievement' => $shangmeng,
  192. 'count' => $shangmeng_w,
  193. 'ratio' => getRatio($shangmeng_w, $shangmeng)
  194. ],
  195. 'fanchang' => [
  196. 'achievement' => $fanchang,
  197. 'count' => $fanchang_w,
  198. 'ratio' => getRatio($fanchang_w, $fanchang)
  199. ],
  200. ]);
  201. }
  202. //首页目标完成情况
  203. public function getAchievement()
  204. {
  205. $times = input('times');//2021-01,2021-07
  206. $team_type = input('team_type', 3);//3我的 2我的团队
  207. if (empty($times)) {
  208. $this->error('筛选时间不能为空');
  209. }
  210. $times = setTimes($times, 'datetime');
  211. // $times = explode(',', $times);
  212. //业绩目标
  213. $contractAchievement = Achievement::getStaffTeamAchievement($times[0], $times[1], $this->auth, Achievement::CONTRACT_TYPE, $team_type) ?? 0;
  214. //获客目标
  215. $customerAchievement = Achievement::getStaffTeamAchievement($times[0], $times[1], $this->auth, Achievement::CUSTOMER_TYPE, $team_type) ?? 0;
  216. //拜访目标
  217. $visitAchievement = Achievement::getStaffTeamAchievement($times[0], $times[1], $this->auth, Achievement::VISIT_TYPE, $team_type) ?? 0;
  218. //工单目标
  219. $workorderAchievement = Achievement::getStaffTeamAchievement($times[0], $times[1], $this->auth, Achievement::WORKORDER_TYPE, $team_type) ?? 0;
  220. $startTime = strtotime($times[0] . '-1');
  221. $endTime = strtotime(date('Y-m-t', strtotime($times[1]))) + 86400 - 1;
  222. if ($team_type == 3) {//我的
  223. $showStaffIds = [$this->auth->id];
  224. } elseif ($team_type == 2) {//团队
  225. $showStaffIds = Staff::getLowerStaffId();
  226. } else {
  227. $this->error('参数错误');
  228. }
  229. //业绩
  230. $contractCount = Contract::where(['check_status' => 2, 'create_staff_id' => ['in', $showStaffIds], 'createtime' => ['between', [$startTime, $endTime]]])->sum('money');
  231. //获客
  232. $customerCount = Customer::where(['createtime' => ['between',[$startTime, $endTime]], 'create_staff_id' => ['in', $showStaffIds]])->count();
  233. //拜访
  234. $visitCount = Event::where(['type' => 1, 'status' => 2, 'actual_end_time' => ['between', [$startTime, $endTime]],
  235. 'staff_id' => ['in', $showStaffIds]])->count();
  236. //工单
  237. $workorderCount = Workorder::where(['status' => 3, 'end_time' => ['between', [$startTime, $endTime]], 'owner_staff_id' => ['in', $showStaffIds]])->count();
  238. $this->success('请求成功', [
  239. 'contract' => [
  240. 'achievement' => $contractAchievement,
  241. 'count' => $contractCount,
  242. 'ratio' => getRatio($contractCount, $contractAchievement)
  243. ],
  244. 'customer' => [
  245. 'achievement' => $customerAchievement,
  246. 'count' => $customerCount,
  247. 'ratio' => getRatio($customerCount, $customerAchievement)
  248. ],
  249. 'event' => [
  250. 'achievement' => $visitAchievement,
  251. 'count' => $visitCount,
  252. 'ratio' => getRatio($visitCount, $visitAchievement)
  253. ],
  254. 'workorder' => [
  255. 'achievement' => $workorderAchievement,
  256. 'count' => $workorderCount,
  257. 'ratio' => getRatio($workorderCount, $workorderAchievement)
  258. ],
  259. ]);
  260. }
  261. //财务简报
  262. public function financeBriefing()
  263. {
  264. $todayStart = strtotime(date('Y-m-d'));
  265. $todayEnd = strtotime(date('Y-m-d')) + 86400 - 1;
  266. $approval = Approval::where(['createtime' => ['between', [$todayStart, $todayEnd]]])->where('', 'exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)'))->count();
  267. $consume = Consume::where(['createtime' => ['between', [$todayStart, $todayEnd]]])->where('', 'exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)'))->count();
  268. $receivables = Receivables::where(['createtime' => ['between', [$todayStart, $todayEnd]]])->where('', 'exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)'))->count();
  269. $contract = Contract::where(['createtime' => ['between', [$todayStart, $todayEnd]]])->where('', 'exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)'))->count();
  270. $this->success('请求成功', [
  271. 'approval' => $approval,
  272. 'consume' => $consume,
  273. 'receivables' => $receivables,
  274. 'contract' => $contract
  275. ]);
  276. }
  277. //报价单
  278. public function quote()
  279. {
  280. $where = function ($query) {
  281. $query->where('create_staff_id|owner_staff_id', 'in', Staff::getMyStaffIds())
  282. ->whereOr('', 'exp', "FIND_IN_SET({$this->auth->id},show_staff_id)");
  283. };
  284. $daishenhe = Quote::where([ 'check_status' => ['in', [0, 1]]])
  285. ->where($where)->count();
  286. $shenhetongguo = Quote::where([ 'check_status' => 2])
  287. ->where($where)->count();
  288. $shenhejujue = Quote::where([ 'check_status' => 3])
  289. ->where($where)->count();
  290. $zuofei = Quote::where([ 'check_status' => 4])
  291. ->count();
  292. $this->success('请求成功', ['daishenhe' => $daishenhe, 'shenhetongguo' => $shenhetongguo, 'shenhejujue' => $shenhejujue, 'zuofei' => $zuofei]);
  293. }
  294. //合同
  295. public function contract()
  296. {
  297. $where = function ($query) {
  298. $query->where('create_staff_id|owner_staff_id', 'in', Staff::getMyStaffIds())
  299. ->whereOr('', 'exp', "FIND_IN_SET({$this->auth->id},show_staff_id)");
  300. };
  301. $daishenhe = Contract::where([ 'check_status' => ['in', [0, 1]]])->where($where)->count();
  302. $shenhetongguo = Contract::where([ 'check_status' => 2])->where($where)->count();
  303. $shenhejujue = Contract::where([ 'check_status' => 3])->where($where)->count();
  304. $zuofei = Contract::where([ 'check_status' => 4])->where($where)->count();
  305. $zongzhi = Contract::where([ 'check_status' => 9])->where($where)->count();
  306. $this->success('请求成功', ['daishenhe' => $daishenhe, 'shenhetongguo' => $shenhetongguo, 'shenhejujue' => $shenhejujue, 'zuofei' => $zuofei, 'zongzhi' => $zongzhi]);
  307. }
  308. //费用
  309. public function consume()
  310. {
  311. $where=[];
  312. $where['staff_id']=$this->auth->id;
  313. $daishenhe = Consume::where([ 'check_status' => ['in', [0, 1]]])
  314. ->where($where)->count();
  315. $shenhetongguo = Consume::where([ 'check_status' => 2])
  316. ->where($where)->count();
  317. $shenhejujue = Consume::where([ 'check_status' => 3])
  318. ->where($where)->count();
  319. $zuofei = Consume::where([ 'check_status' => 4])
  320. ->where($where)->count();
  321. $this->success('请求成功', ['daishenhe' => $daishenhe, 'shenhetongguo' => $shenhetongguo, 'shenhejujue' => $shenhejujue, 'zuofei' => $zuofei]);
  322. }
  323. //工单统计
  324. public function workorder_count()
  325. {
  326. $workorder_type = input('workorder_type');
  327. $where = function ($query) {
  328. $query->where('create_staff_id|owner_staff_id', 'in', Staff::getMyStaffIds())
  329. ->whereOr('', 'exp', "FIND_IN_SET({$this->auth->id},show_staff_id)");
  330. };
  331. $daichuli = Workorder::where([ 'status' => ['in', [0, 1]], 'workorder_type' => $workorder_type])->where($where)->count();
  332. $chulizhong = Workorder::where([ 'status' => 2, 'workorder_type' => $workorder_type])->where($where)->count();
  333. $yiwancheng = Workorder::where([ 'status' => ['in', [3, 9]], 'workorder_type' => $workorder_type])->where($where)->count();
  334. $this->success('请求成功', ['daichuli' => $daichuli, 'chulizhong' => $chulizhong, 'yiwancheng' => $yiwancheng]);
  335. }
  336. //工单
  337. public function workorder()
  338. {
  339. $team_type = input('team_type', 3);//3我的 2我的团队
  340. if ($team_type == 3) {//我的
  341. $showStaffIds = [$this->auth->id];
  342. } elseif ($team_type == 2) {//团队
  343. $showStaffIds = Staff::getLowerStaffId();
  344. } else {
  345. $this->error('参数错误');
  346. }
  347. //未完成 所有未完成
  348. $weiwancheng = Workorder::where(['status' => ['in', [0, 1, 2]], 'owner_staff_id' => ['in', $showStaffIds]])->count();
  349. //进行中
  350. $jinxingzhong = Workorder::where(['status' => ['in', [1, 2]], 'owner_staff_id' => ['in', $showStaffIds]])->count();
  351. //待指派
  352. $daizhipai = Workorder::where(['status' => ['in', [0]], 'owner_staff_id' => ['in', $showStaffIds]])->count();
  353. //异常工单
  354. $yichang = Workorder::where([
  355. 'status' => ['in', [0, 1, 2]],
  356. 'owner_staff_id' => ['in', $showStaffIds],
  357. 'is_pause|is_timeout' => 1,
  358. ])->count();
  359. $this->success('请求成功', ['weiwancheng' => $weiwancheng, 'jinxingzhong' => $jinxingzhong,
  360. 'daizhipai' => $daizhipai, 'yichang' => $yichang]);
  361. }
  362. //备件
  363. public function parts()
  364. {
  365. $whereFlow = [];
  366. $whereFlow[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)')];
  367. $ruku = PartsStockReload::where(['type' => 1, 'check_status' => ['in', [0, 1]]])->where($whereFlow)->count();
  368. $chuku = PartsStockReload::where(['type' => 2, 'check_status' => ['in', [0, 1]]])->where($whereFlow)->count();
  369. $quejian = Parts::where(['status' => '上架'])->where('stock <= warning_stock')->count();
  370. $all = Parts::where(['status' => '上架'])->count();
  371. $this->success('请求成功', ['ruku' => $ruku, 'chuku' => $chuku, 'quejian' => $quejian, 'all' => $all]);
  372. }
  373. //审批
  374. public function examine()
  375. {
  376. $wofaqi = Approval::where([ 'check_status' => ['in', [0, 1]], 'create_staff_id' => $this->auth->id])->count();
  377. $whereFlow = [];
  378. $whereFlow[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_id)')];
  379. $whereFlow[] = ['exp', Db::raw('!FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  380. $daishenhe = Approval::where([
  381. 'check_status' => ['in', [0, 1]]
  382. ])->where($whereFlow)->count();
  383. $this->success('请求成功', ['wofaqi' => $wofaqi, 'daishenhe' => $daishenhe]);
  384. }
  385. //排行
  386. public function ranking()
  387. {
  388. //1 获客 2拜访 3业绩 4回款 5工单
  389. $ranking = input('ranking');
  390. $times = input('times');//2021-01,2021-07
  391. if (empty($times)) {
  392. $this->error('筛选时间不能为空');
  393. }
  394. $times = setTimes($times, 'time');
  395. $startTime = $times[0];
  396. $endTime = $times[1];
  397. $where = [];
  398. if ($ranking == 1) {//获客
  399. $rankingList = Customer::where(['createtime' => ['between', [$startTime, $endTime]]])->where($where)
  400. ->with(['createStaff'])
  401. ->field('count(*) as number,create_staff_id')->order('number desc')->group('create_staff_id')->select();
  402. $rankingList = collection($rankingList)->toArray();
  403. $rankingList = array_slice($rankingList, 0, 10);
  404. foreach ($rankingList as $k => &$v) {
  405. $v['ranking'] = $k + 1;
  406. $v['staff'] = $v['create_staff'];
  407. unset($v['create_staff']);
  408. }
  409. } elseif ($ranking == 2) {//拜访
  410. $rankingList = Event::where(['type' => 3, 'status' => 2, 'actual_end_time' => ['between', [$startTime, $endTime]]])->where($where)
  411. ->with(['staff'])->field('count(*) as number,staff_id')->order('number desc')->group('staff_id')->select();
  412. $rankingList = collection($rankingList)->toArray();
  413. $rankingList = array_slice($rankingList, 0, 10);
  414. foreach ($rankingList as $k => &$v) {
  415. $v['ranking'] = $k + 1;
  416. }
  417. } elseif ($ranking == 3) {//业绩
  418. $rankingList = Contract::where(['check_status' => 2, 'createtime' => ['between', [$startTime, $endTime]]])->where($where)->with(['staff'])->field('count(*) as number,owner_staff_id')->order('number desc')->group('owner_staff_id')->select();
  419. $rankingList = collection($rankingList)->toArray();
  420. $rankingList = array_slice($rankingList, 0, 10);
  421. foreach ($rankingList as $k => &$v) {
  422. $v['ranking'] = $k + 1;
  423. }
  424. } elseif ($ranking == 4) {//回款
  425. $rankingList = Receivables::where(['check_status' => 2, 'createtime' => ['between', [$startTime, $endTime]]])->where($where)
  426. ->with(['staff'])->field('count(*) as number,owner_staff_id')->order('number desc')->group('owner_staff_id')->select();
  427. $rankingList = collection($rankingList)->toArray();
  428. $rankingList = array_slice($rankingList, 0, 10);
  429. foreach ($rankingList as $k => &$v) {
  430. $v['ranking'] = $k + 1;
  431. }
  432. } elseif ($ranking == 5) {//工单
  433. $rankingList = Workorder::where(['status' => 3, 'createtime' => ['between', [$startTime, $endTime]]])
  434. ->where($where)
  435. ->with(['ownerStaff'])->field('count(*) as number,owner_staff_id')->order('number desc')->group('owner_staff_id')->select();
  436. $rankingList = collection($rankingList)->toArray();
  437. $rankingList = array_slice($rankingList, 0, 10);
  438. foreach ($rankingList as $k => &$v) {
  439. $v['ranking'] = $k + 1;
  440. $v['staff'] = $v['owner_staff'];
  441. unset($v['create_staff']);
  442. }
  443. }
  444. foreach ($rankingList as $kes => $ves) {
  445. if(empty($ves['staff'])){
  446. unset($rankingList[$kes]);
  447. }
  448. }
  449. $this->success('请求成功', array_values($rankingList));
  450. }
  451. //搜索
  452. public function search()
  453. {
  454. $keywords = input('keywords');
  455. $type = input('type');
  456. if (empty($keywords)) {
  457. $this->error('筛选字段不能为空');
  458. }
  459. //客户
  460. $customer = Customer::where([
  461. 'name' => ['like', "%$keywords%"],
  462. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  463. ])->with([
  464. 'ownerStaff',
  465. 'contacts'
  466. ])->field('id,name,next_time,owner_staff_id,level,follow')->select();
  467. //联系人
  468. $contacts = Contacts::where([
  469. 'name' => ['like', "%$keywords%"],
  470. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  471. ])->with(['customer'])->field('id,name,mobile,customer_id')->select();
  472. //线索
  473. $leads = Leads::where([
  474. 'name' => ['like', "%$keywords%"],
  475. 'owner_staff_id' => ['in', Staff::getMyStaffIds()],
  476. ])->with(['ownerStaff'])->field('id,owner_staff_id,name,follow,mobile')->select();
  477. $this->success('请求成功', [
  478. 'customer' => $customer,
  479. 'contacts' => $contacts,
  480. 'leads' => $leads
  481. ]);
  482. }
  483. //反馈
  484. public function feedback()
  485. {
  486. $content = input('content', '');
  487. $file = input('file', '');
  488. if (empty($content)) {
  489. $this->error('请输入反馈内容!');
  490. }
  491. $model = new Feedback();
  492. $model->save([
  493. 'staff_id' => $this->auth->id,
  494. 'content' => $content,
  495. 'file_ids' => $file
  496. ]);
  497. $this->success('反馈成功');
  498. }
  499. //服务协议
  500. public function serviceContent()
  501. {
  502. $this->success('请求成功', [
  503. 'content' => "<h1>用户服务协议</h1>",
  504. 'name' => '青动',
  505. 'logo' => '/assets/img/logo.png'
  506. ]);
  507. }
  508. //待办事项
  509. public function agent() {
  510. $where['create_staff_id'] = $this->auth->id;
  511. $where['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
  512. $where['follow_type'] = ['neq', '其它'];
  513. $where['status'] = 0;
  514. // 待跟进客户
  515. $where1['relation_type'] = 1;
  516. $customerlist = Record::where($where)->where($where1)->column('id');
  517. $customerlist1 = 0;
  518. if($customerlist){
  519. $whereExit['id'] = array('in',$customerlist);
  520. $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  521. $customerlist1 = Record::where($whereExit)->count();
  522. }
  523. $customer = count($customerlist) - $customerlist1;
  524. //待跟进合同
  525. $where2['relation_type'] = 3;
  526. $contractlist = Record::where($where)->where($where2)->column('id');
  527. $contractlist1 = 0;
  528. if($contractlist){
  529. $whereExitC['id'] = array('in',$contractlist);
  530. $whereExitC['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  531. $contractlist1 = Record::where($whereExitC)->count();
  532. }
  533. $contract = count($contractlist)-$contractlist1;
  534. //待跟进线索
  535. $where3['relation_type'] = 4;
  536. $leadlist = Record::where($where)->where($where3)->column('id');
  537. $leadlist1 = 0;
  538. if($leadlist){
  539. $whereExitL['id'] = array('in',$leadlist);
  540. $whereExitL['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  541. $leadlist1 = Record::where($whereExitL)->count();
  542. }
  543. $lead = count($leadlist)-$leadlist1;
  544. //待跟进联系人
  545. $where4['relation_type'] = 2;
  546. $contactslist = Record::where($where)->where($where4)->column('id');
  547. $contactslist1 = 0;
  548. if($contactslist1){
  549. $whereExitCs['id'] = array('in',$contactslist);
  550. $whereExitCs['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  551. $contactslist1 = Record::where($whereExitCs)->count();
  552. }
  553. $contacts = count($contactslist)-$contactslist1;
  554. //待跟进商机
  555. $where5['relation_type'] = 5;
  556. $businesslist = Record::where($where)->where($where5)->column('id');
  557. $businesslist1 = 0;
  558. if($businesslist1){
  559. $whereExitB['id'] = array('in',$businesslist);
  560. $whereExitB['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  561. $businesslist1 = Record::where($whereExitB)->count();
  562. }
  563. $business = count($businesslist)-$businesslist1;
  564. //待审核合同
  565. $examine = ExamineRecord::where([
  566. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  567. 'status' => 0,
  568. 'check_staff_id' => $this->auth->id
  569. ])->count();
  570. //待审核回款
  571. $receivables = ExamineRecord::where([
  572. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  573. 'status' => 0,
  574. 'check_staff_id' => $this->auth->id
  575. ])->count();
  576. //待回款提醒
  577. $receivablesPlan = ReceivablesPlan::where([
  578. 'remind_date' => ['elt', date('Y-m-d')],
  579. 'status' => 0,
  580. 'owner_staff_id' => $this->auth->id
  581. ])->count();
  582. //待处理日程
  583. $eventOne = Event::where([
  584. 'type' => 1,
  585. 'status' => ['in', [0, 1]],
  586. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  587. 'staff_id|owner_staff_id' => $this->auth->id,
  588. ])->count();
  589. $this->success('请求成功', [
  590. 'customer' => $customer,
  591. 'business' => $business,
  592. 'contract' => $contract,
  593. 'lead' => $lead,
  594. 'contacts' => $contacts,
  595. 'event_one' => $eventOne,
  596. 'examine' => $examine,
  597. 'receivables' => $receivables,
  598. 'receivables_plan' => $receivablesPlan
  599. ]);
  600. }
  601. //待处理日程
  602. public function event_one() {
  603. //待办日程
  604. $eventWhere = [
  605. 'status' => ['in', [0, 1]],
  606. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  607. 'end_time' => ['gt', date('Y-m-d H:i:s')],
  608. 'staff_id' => $this->auth->id,
  609. ];
  610. $event = Event::where($eventWhere)->with([
  611. 'staff',
  612. 'customer',
  613. ])->order('id desc')->select();
  614. $event=collection($event)->toArray();
  615. foreach ($event as $k => $v) {
  616. $v['level'] = Event::getLevel($v['level']);
  617. $v['status'] = Event::getStatus($v['status']);
  618. $event[$k] = $v;
  619. }
  620. $this->success('请求成功', $event);
  621. }
  622. //待回款提醒
  623. public function examine_receivables_plan() {
  624. //待回款提醒
  625. $receivablesPlan = ReceivablesPlan::where([
  626. 'remind_date' => ['elt', date('Y-m-d')],
  627. 'status' => 0,
  628. 'owner_staff_id' => $this->auth->id,
  629. ])->order('id desc')->with(['contract', 'customer'])->select();
  630. $this->success('请求成功', $receivablesPlan);
  631. }
  632. }