Data.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. //数据统计类(数据看板,获取相关统计数据)
  8. class Data extends BaseController
  9. {
  10. //1.今日销量
  11. public function todaySalesVolume()
  12. {
  13. $rs = Db::name('sale')
  14. ->field('count(id) orders_number,sum(total_price) money')
  15. ->where('is_del', 0)
  16. ->whereDay('addtime', 'today')
  17. ->find();
  18. //跨库查询网络部和客服部
  19. $internet = Db::connect('mysql3')
  20. ->table('source_all')
  21. ->field('SUM(sale_total) money,COUNT(id) orders_number')
  22. ->whereDay('ordertime')
  23. ->whereIn('depart', ['网络部', '客服部'])
  24. ->find();
  25. $temp = [
  26. 'orders_number' => isset($rs['orders_number']) ? (isset($internet['orders_number']) ? $rs['orders_number'] + $internet['orders_number'] : $rs['orders_number']) : 0,
  27. 'money' => isset($rs['money']) ? (isset($internet['money']) ? $rs['money'] + $internet['money'] : $rs['money']) : 0,
  28. ];
  29. return app_show(0, '请求成功', $temp);
  30. }
  31. //2.今日销冠
  32. public function todaySalesChampion()
  33. {
  34. $temp = Db::name('sale')
  35. ->field('count(id) orders,sum(total_price) money,apply_id')
  36. ->where('is_del', 0)
  37. ->whereDay('addtime', 'today')
  38. ->group('apply_id')
  39. ->order('money', 'desc')
  40. ->limit(1)
  41. ->buildSql();
  42. $rs = Db::table($temp)
  43. ->alias('t')
  44. ->field('t.apply_id,t.orders,t.money,u.itemid company_id, c.`name` company, u.nickname')
  45. ->leftJoin('depart_user u', 'u.uid=t.apply_id AND u.is_del=0')
  46. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  47. ->select()
  48. ->toArray();
  49. return app_show(0, '请求成功', empty($rs[0]) ? [] : $rs[0]);
  50. }
  51. //3.今日采购
  52. public function todayPurchase()
  53. {
  54. $rs = Db::name('purchease_order')
  55. ->field('count(id) orders_number,sum(good_num) good_num')
  56. ->where('is_del', 0)
  57. ->whereDay('addtime', 'today')
  58. ->find();
  59. $consult_info_total = Db::name('consult_info')
  60. ->where('is_del', 0)
  61. ->whereDay('addtime', 'today')
  62. ->count('id');
  63. $consult_bids_total = Db::name('consult_bids')
  64. ->where('is_del', 0)
  65. ->whereDay('addtime', 'today')
  66. ->count('id');
  67. $data = [
  68. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,
  69. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,
  70. 'consult_info_total' => $consult_info_total,
  71. 'consult_bids_total' => $consult_bids_total,
  72. ];
  73. return app_show(0, '请求成功', $data);
  74. }
  75. //4.票(相关,暂不做)
  76. //5.竞价单和采购单
  77. public function totalZixunPurchease()
  78. {
  79. //招标单数(咨询单数)
  80. $zixun_total = Db::name('consult_info')
  81. ->where(['is_del' => 0])
  82. ->count('id');
  83. //已反馈单数,
  84. $sql = Db::name('consult_info')
  85. ->field('zxNo')
  86. ->where(['is_del' => 0, 'status' => 1])
  87. ->buildSql();
  88. //正在进行中的咨询单(招标工作台上的数据)
  89. $zixun_num_ing = Db::name('consult_info')
  90. ->field('zxNo')
  91. ->where(['is_del' => 0, 'status' => 1])
  92. ->count('id');
  93. //已反馈的单数
  94. $temp_sql = Db::name('consult_bids')
  95. ->field('zxNo')
  96. ->where('zxNo IN ' . $sql)
  97. ->group('zxNo')
  98. ->buildSql();
  99. $not_feedback = Db::table($temp_sql)
  100. ->alias('t')
  101. ->count('t.zxNo');
  102. //未竞价单数(未反馈单数) = 进行中的单数-已反馈单数
  103. $not_feedback = $zixun_num_ing - $not_feedback;
  104. //采购单数
  105. $purchease_total = Db::name('purchease_order')
  106. ->where('is_del', 0)
  107. ->count('id');
  108. //采购单未下单数(状态为待与供应商确认)
  109. $purchease_wait_confirm = Db::name('purchease_order')
  110. ->where(['is_del' => 0, 'status' => 0])
  111. ->count('id');
  112. //采购单 采购下单数???????
  113. return app_show(0, '请求成功', [
  114. 'zixun_total' => $zixun_total,
  115. 'not_feedback' => $not_feedback,
  116. 'purchease_total' => $purchease_total,
  117. 'purchease_wait_confirm' => $purchease_wait_confirm,
  118. ]);
  119. }
  120. //6.未发货
  121. public function waitSendTotal()
  122. {
  123. $rs = Db::name('sale')
  124. ->field('count(id) order_num,sum(wsend_num) wsend_num')
  125. ->where(['is_del' => 0, 'status' => 1])//status==1 待发货
  126. ->find();
  127. return app_show(0, '请求成功', $rs);
  128. }
  129. //7.今日订单
  130. public function todaySale()
  131. {
  132. $temp = Db::name('sale')
  133. ->alias('s')
  134. ->field('s.id,s.apply_id,s.total_price,u.nickname,u.itemid')
  135. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  136. ->where('s.is_del', 0)
  137. ->whereDay('s.addtime', 'today')
  138. ->buildSql();
  139. $rs = Db::table($temp)
  140. ->alias('t')
  141. ->field('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order')
  142. ->leftJoin('company_item c', 'c.id=t.itemid')
  143. ->group('t.itemid')
  144. ->order('total_price', 'desc')
  145. ->column('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order', 'name');
  146. //跨库添加网络部数据和客服部数据
  147. $internet = Db::connect('mysql3')
  148. ->table('source_all')
  149. ->whereDay('ordertime')
  150. ->group('depart')
  151. ->whereIn('depart', ['网络部', '客服部'])
  152. ->column('depart,SUM(sale_total) total_price,COUNT(id) total_order ', 'depart');
  153. if (!empty($internet)) {
  154. if (isset($internet['网络部'])) {
  155. if (isset($rs['网络部'])) {
  156. $rs['网络部']['total_price'] += $internet['网络部']['total_price'];
  157. $rs['网络部']['total_order'] += $internet['网络部']['total_order'];
  158. } else {
  159. $rs[] = [
  160. 'companyId' => '',
  161. 'companyName' => '网络部',
  162. 'total_price' => $internet['网络部']['total_price'],
  163. 'total_order' => $internet['网络部']['total_order'],
  164. ];
  165. }
  166. }
  167. if (isset($internet['客服部'])) {
  168. if (isset($rs['客服部'])) {
  169. $rs['客服部']['total_price'] += $internet['客服部']['total_price'];
  170. $rs['客服部']['total_order'] += $internet['客服部']['total_order'];
  171. } else {
  172. $rs[] = [
  173. 'companyId' => '',
  174. 'companyName' => '客服部',
  175. 'total_price' => $internet['客服部']['total_price'],
  176. 'total_order' => $internet['客服部']['total_order'],
  177. ];
  178. }
  179. }
  180. //重新按照总金额排序
  181. usort($rs, function ($left, $right) {
  182. return ($left['total_price'] > $right['total_price']) ? -1 : 1;
  183. });
  184. }
  185. return app_show(0, '请求成功', $rs);
  186. }
  187. //8.本月完成率(数据在结算库里,部门完成率=部门净销售额 / 部门销售指标)
  188. //没有销售指标的部门,不纳入统计范围
  189. public function monthFinishRate()
  190. {
  191. //部门净销售额
  192. $sales_volume = Db::name('sale')
  193. ->alias('s')
  194. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  195. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  196. ->group('u.itemid')
  197. ->whereMonth('s.addtime')
  198. ->column('(sum(s.total_price) - sum(s.th_fee)) sales_volume,u.itemid,c.name', 'c.name');
  199. //额外把网络部和客服部的销售数据跨数据库查询出来
  200. $other_network = Db::connect('mysql3')
  201. ->table('source_all')
  202. ->whereMonth('ordertime')
  203. ->whereIn('depart', ['网络部', '客服部'])
  204. ->group('depart')
  205. ->column('SUM(sale_total) sale_total', 'depart');
  206. if (isset($other_network['网络部'])) {
  207. if (isset($sales_volume['网络部'])) $sales_volume['网络部']['sales_volume'] += $other_network['网络部'];
  208. else {
  209. $sales_volume['网络部'] = [
  210. 'sales_volume' => $other_network['网络部'],
  211. 'itemid' => -1,
  212. 'name' => '网络部'
  213. ];
  214. }
  215. }
  216. if (isset($other_network['客服部'])) {
  217. if (isset($sales_volume['客服部'])) $sales_volume['客服部']['sales_volume'] += $other_network['客服部'];
  218. else {
  219. $sales_volume['客服部'] = [
  220. 'sales_volume' => $other_network['客服部'],
  221. 'itemid' => -2,
  222. 'name' => '客服部'
  223. ];
  224. }
  225. }
  226. //部门销售指标
  227. $sale_indicators = Db::name('depart_tips')
  228. ->field('id,total_tips,depart_item department')
  229. ->where(['year' => date('Y'), 'month' => date('n')])
  230. ->select()
  231. ->toArray();
  232. $da = [];
  233. //计算完成率
  234. foreach ($sale_indicators as $value) {
  235. if (isset($sales_volume[$value['department']]['sales_volume'])) {
  236. $value['finish'] = $sales_volume[$value['department']]['sales_volume'];
  237. $value['finish_rate'] = round(($value['finish'] / $value['total_tips']) * 100, 5);
  238. $da[] = $value;
  239. } else continue;
  240. }
  241. //按照完成率排序
  242. usort($da, function ($left, $right) {
  243. return ($left['finish_rate'] > $right['finish_rate']) ? -1 : 1;
  244. });
  245. //计算汇总完成率
  246. $tmp_total_tips = array_sum(array_column($da, 'total_tips'));
  247. $total_finish_rate = $tmp_total_tips ? round((array_sum(array_column($sales_volume, 'sales_volume')) / $tmp_total_tips) * 100, 2) : 0;
  248. return app_show(0, '请求成功', ['list' => $da, 'total_finish_rate' => $total_finish_rate]);
  249. }
  250. //9.转单率-今日
  251. public function orderTransferRateToday()
  252. {
  253. $consulting = Db::name('sale')
  254. ->alias('s')
  255. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  256. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  257. ->whereDay('s.addtime')
  258. ->group('u.itemid')
  259. ->column('count(s.id) consulting', 'u.itemid');
  260. $rs = Db::name('consult_order')
  261. ->alias('c')
  262. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  263. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  264. ->where(['c.is_del' => 0])
  265. ->whereDay('c.addtime')
  266. ->group('c.depart')
  267. ->append(['transfer_rate'])
  268. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  269. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  270. return round(($consult / $data['total']) * 100, 2);
  271. })
  272. ->select()
  273. ->toArray();
  274. return app_show(0, '请求成功', $rs);
  275. }
  276. //9.转单率-本月
  277. public function orderTransferRateMonth()
  278. {
  279. $consulting = Db::name('sale')
  280. ->alias('s')
  281. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  282. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  283. ->whereMonth('s.addtime')
  284. ->group('u.itemid')
  285. ->column('count(s.id) consulting', 'u.itemid');
  286. $rs = Db::name('consult_order')
  287. ->alias('c')
  288. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  289. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  290. ->where(['c.is_del' => 0])
  291. ->whereMonth('c.addtime')
  292. ->group('c.depart')
  293. ->append(['transfer_rate'])
  294. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  295. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  296. return round(($consult / $data['total']) * 100, 2);
  297. })
  298. ->select()
  299. ->toArray();
  300. return app_show(0, '请求成功', $rs);
  301. }
  302. //9.转单率-今年
  303. public function orderTransferRateYear()
  304. {
  305. $consulting = Db::name('sale')
  306. ->alias('s')
  307. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  308. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  309. ->whereYear('s.addtime', date('Y'))
  310. ->group('u.itemid')
  311. ->column('count(s.id) consulting', 'u.itemid');
  312. $rs = Db::name('consult_order')
  313. ->alias('c')
  314. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  315. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  316. ->where(['c.is_del' => 0])
  317. ->whereYear('c.addtime', date('Y'))
  318. ->group('c.depart')
  319. ->append(['transfer_rate'])
  320. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  321. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  322. return round(($consult / $data['total']) * 100, 2);
  323. })
  324. ->select()
  325. ->toArray();
  326. return app_show(0, '请求成功', $rs);
  327. }
  328. //******* 以下是新版数据大屏 的内容***********************************
  329. //1.今日销售
  330. public function dnTodaySale()
  331. {
  332. $rs = Db::name('sale')
  333. ->field('count(id) orders_number,sum(total_price)-sum(th_fee) money,sum(good_num)-sum(th_num) good_num ')
  334. ->where('is_del', 0)
  335. ->whereDay('addtime', 'today')
  336. ->find();
  337. //跨库查询网络部和客服部
  338. $internet = Db::connect('mysql3')
  339. ->table('source_all')
  340. ->field('SUM(sale_total) money,SUM(order_num) good_num,COUNT(id) orders_number')
  341. ->whereDay('ordertime')
  342. ->whereIn('depart', ['网络部', '客服部'])
  343. ->find();
  344. //组织数据
  345. $data = [
  346. 'orders_number' => bcadd(isset($rs['orders_number']) ? $rs['orders_number'] : '0', isset($internet['orders_number']) ? $internet['orders_number'] : '0'),
  347. 'money' => bcadd(isset($rs['money']) ? $rs['money'] : '0', isset($internet['money']) ? $internet['money'] : '0', 2),
  348. 'good_num' => bcadd(isset($rs['good_num']) ? $rs['good_num'] : '0', isset($internet['good_num']) ? $internet['good_num'] : '0', 2),
  349. ];
  350. return app_show(0, '请求成功', $data);
  351. }
  352. //2.今日采购
  353. public function dnTodayPurcheaseOrder()
  354. {
  355. $rs = Db::name('purchease_order')
  356. ->field('count(id) orders_number,sum(good_num) good_num')
  357. ->where('is_del', 0)
  358. ->whereDay('addtime')
  359. ->find();
  360. $consult_info_total = Db::name('consult_info')
  361. ->where(['is_del' => 0, 'status' => 5])//status==5成功转单
  362. ->whereDay('updatetime')
  363. ->count('id');
  364. $consult_bids_total = Db::name('consult_bids')
  365. ->where('is_del', 0)
  366. ->whereDay('addtime')
  367. ->count('id');
  368. $data = [
  369. 'consult_info_total' => $consult_info_total,//竞价中标数
  370. 'consult_bids_total' => $consult_bids_total,//反馈商品数
  371. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,//采购订单
  372. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,//商品数量
  373. ];
  374. return app_show(0, '请求成功', $data);
  375. }
  376. //3.今日结算
  377. //4.销售转单数
  378. public function dnTodayTransferOrder()
  379. {
  380. $param = $this->request->filter('trim')->only(['itemid' => '0', 'type' => '3', 'date' => date('Y-m-d')], 'post');
  381. $val = Validate::rule([
  382. 'itemid|部门id' => 'require|integer',
  383. 'type|日期类型' => 'require|number|in:1,2,3',
  384. 'date|筛选日期' => 'require|date',
  385. ]);
  386. if (!$val->check($param)) return error_show(1005, $val->getError());
  387. $rs = Db::name('consult_info')
  388. ->alias('a')
  389. ->leftJoin('consult_order b', 'b.zxNo=a.zxNo AND b.is_del=0')
  390. ->leftJoin('depart_user u', 'u.uid=b.saleid AND u.is_del=0')
  391. ->where(['a.is_del' => 0, 'bargain_num' => 1]);
  392. //查询符合条件的竞价单
  393. if ($param['itemid'] == '0') {
  394. //以部门为维度
  395. $rs = $rs
  396. ->field('a.id,a.status,u.itemid total_id,c.name')
  397. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0');
  398. } else {
  399. //以部门下的人为维度
  400. $rs = $rs
  401. ->field('a.id,a.status,b.saleid total_id,b.salesman name')
  402. ->where('u.itemid', $param['itemid']);
  403. }
  404. //时间段判断
  405. switch ($param['type']) {
  406. case '1':
  407. $rs->whereYear('a.addtime', $param['date']);
  408. break;
  409. case '2':
  410. $rs->whereMonth('a.addtime', $param['date']);
  411. break;
  412. default:
  413. $rs->whereDay('a.addtime', $param['date']);
  414. break;
  415. }
  416. $data = $rs->cursor();
  417. $da = [];
  418. //组织数据
  419. foreach ($data as $value) {
  420. if (!isset($da[$value['total_id']])) {
  421. $da[$value['total_id']] = [
  422. 'finish_total' => 0,//销售单(中标单数)
  423. 'total' => 0,//竞价单(总单数)
  424. 'name' => $value['name']//名称
  425. ];
  426. }
  427. $da[$value['total_id']]['total']++;
  428. if ($value['status'] == '5') $da[$value['total_id']]['finish_total']++;
  429. }
  430. //计算转单率
  431. foreach ($da as &$val) {
  432. $val['finish_rate'] = bcmul(round(bcdiv($val['finish_total'], $val['total'], 5), 4), '100', 2) . '%';
  433. }
  434. return app_show(0, '请求成功', array_merge($da));
  435. }
  436. }