Data.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. $total_finish_rate = round((array_sum(array_column($sales_volume, 'sales_volume')) / array_sum(array_column($da, 'total_tips'))) * 100, 2);
  247. return app_show(0, '请求成功', ['list' => $da, 'total_finish_rate' => $total_finish_rate]);
  248. }
  249. //9.转单率-今日
  250. public function orderTransferRateToday()
  251. {
  252. $consulting = Db::name('sale')
  253. ->alias('s')
  254. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  255. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  256. ->whereDay('s.addtime')
  257. ->group('u.itemid')
  258. ->column('count(s.id) consulting', 'u.itemid');
  259. $rs = Db::name('consult_order')
  260. ->alias('c')
  261. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  262. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  263. ->where(['c.is_del' => 0])
  264. ->whereDay('c.addtime')
  265. ->group('c.depart')
  266. ->append(['transfer_rate'])
  267. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  268. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  269. return round(($consult / $data['total']) * 100, 2);
  270. })
  271. ->select()
  272. ->toArray();
  273. return app_show(0, '请求成功', $rs);
  274. }
  275. //9.转单率-本月
  276. public function orderTransferRateMonth()
  277. {
  278. $consulting = Db::name('sale')
  279. ->alias('s')
  280. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  281. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  282. ->whereMonth('s.addtime')
  283. ->group('u.itemid')
  284. ->column('count(s.id) consulting', 'u.itemid');
  285. $rs = Db::name('consult_order')
  286. ->alias('c')
  287. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  288. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  289. ->where(['c.is_del' => 0])
  290. ->whereMonth('c.addtime')
  291. ->group('c.depart')
  292. ->append(['transfer_rate'])
  293. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  294. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  295. return round(($consult / $data['total']) * 100, 2);
  296. })
  297. ->select()
  298. ->toArray();
  299. return app_show(0, '请求成功', $rs);
  300. }
  301. //9.转单率-今年
  302. public function orderTransferRateYear()
  303. {
  304. $consulting = Db::name('sale')
  305. ->alias('s')
  306. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  307. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  308. ->whereYear('s.addtime', date('Y'))
  309. ->group('u.itemid')
  310. ->column('count(s.id) consulting', 'u.itemid');
  311. $rs = Db::name('consult_order')
  312. ->alias('c')
  313. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  314. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  315. ->where(['c.is_del' => 0])
  316. ->whereYear('c.addtime', date('Y'))
  317. ->group('c.depart')
  318. ->append(['transfer_rate'])
  319. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  320. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  321. return round(($consult / $data['total']) * 100, 2);
  322. })
  323. ->select()
  324. ->toArray();
  325. return app_show(0, '请求成功', $rs);
  326. }
  327. //******* 以下是新版数据大屏 的内容***********************************
  328. //1.今日销售
  329. public function dnTodaySale()
  330. {
  331. $rs = Db::name('sale')
  332. ->field('count(id) orders_number,sum(total_price)-sum(th_fee) money,sum(good_num)-sum(th_num) good_num ')
  333. ->where('is_del', 0)
  334. ->whereDay('addtime', 'today')
  335. ->find();
  336. //跨库查询网络部和客服部
  337. $internet = Db::connect('mysql3')
  338. ->table('source_all')
  339. ->field('SUM(sale_total) money,SUM(order_num) good_num,COUNT(id) orders_number')
  340. ->whereDay('ordertime')
  341. ->whereIn('depart', ['网络部', '客服部'])
  342. ->find();
  343. //组织数据
  344. $data = [
  345. 'orders_number' => bcadd(isset($rs['orders_number']) ? $rs['orders_number'] : '0', isset($internet['orders_number']) ? $internet['orders_number'] : '0'),
  346. 'money' => bcadd(isset($rs['money']) ? $rs['money'] : '0', isset($internet['money']) ? $internet['money'] : '0', 2),
  347. 'good_num' => bcadd(isset($rs['good_num']) ? $rs['good_num'] : '0', isset($internet['good_num']) ? $internet['good_num'] : '0', 2),
  348. ];
  349. return app_show(0, '请求成功', $data);
  350. }
  351. //2.今日采购
  352. public function dnTodayPurcheaseOrder()
  353. {
  354. $rs = Db::name('purchease_order')
  355. ->field('count(id) orders_number,sum(good_num) good_num')
  356. ->where('is_del', 0)
  357. ->whereDay('addtime')
  358. ->find();
  359. $consult_info_total = Db::name('consult_info')
  360. ->where(['is_del' => 0, 'status' => 5])//status==5成功转单
  361. ->whereDay('updatetime')
  362. ->count('id');
  363. $consult_bids_total = Db::name('consult_bids')
  364. ->where('is_del', 0)
  365. ->whereDay('addtime')
  366. ->count('id');
  367. $data = [
  368. 'consult_info_total' => $consult_info_total,//竞价中标数
  369. 'consult_bids_total' => $consult_bids_total,//反馈商品数
  370. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,//采购订单
  371. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,//商品数量
  372. ];
  373. return app_show(0, '请求成功', $data);
  374. }
  375. //3.今日结算
  376. //4.销售转单数
  377. public function dnTodayTransferOrder()
  378. {
  379. $param = $this->request->filter('trim')->only(['itemid' => '0', 'type' => '3', 'date' => date('Y-m-d')], 'post');
  380. $val = Validate::rule([
  381. 'itemid|部门id' => 'require|integer',
  382. 'type|日期类型' => 'require|number|in:1,2,3',
  383. 'date|筛选日期' => 'require|date',
  384. ]);
  385. if (!$val->check($param)) return error_show(1005, $val->getError());
  386. $rs = Db::name('consult_info')
  387. ->alias('a')
  388. ->leftJoin('consult_order b', 'b.zxNo=a.zxNo AND b.is_del=0')
  389. ->leftJoin('depart_user u', 'u.uid=b.saleid AND u.is_del=0')
  390. ->where(['a.is_del' => 0, 'bargain_num' => 1]);
  391. //查询符合条件的竞价单
  392. if ($param['itemid'] == '0') {
  393. //以部门为维度
  394. $rs = $rs
  395. ->field('a.id,a.status,u.itemid total_id,c.name')
  396. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0');
  397. } else {
  398. //以部门下的人为维度
  399. $rs = $rs
  400. ->field('a.id,a.status,b.saleid total_id,b.salesman name')
  401. ->where('u.itemid', $param['itemid']);
  402. }
  403. //时间段判断
  404. switch ($param['type']) {
  405. case '1':
  406. $rs->whereYear('a.addtime', $param['date']);
  407. break;
  408. case '2':
  409. $rs->whereMonth('a.addtime', $param['date']);
  410. break;
  411. default:
  412. $rs->whereDay('a.addtime', $param['date']);
  413. break;
  414. }
  415. $data = $rs->cursor();
  416. $da = [];
  417. //组织数据
  418. foreach ($data as $value) {
  419. if (!isset($da[$value['total_id']])) {
  420. $da[$value['total_id']] = [
  421. 'finish_total' => 0,//销售单(中标单数)
  422. 'total' => 0,//竞价单(总单数)
  423. 'name' => $value['name']//名称
  424. ];
  425. }
  426. $da[$value['total_id']]['total']++;
  427. if ($value['status'] == '5') $da[$value['total_id']]['finish_total']++;
  428. }
  429. //计算转单率
  430. foreach ($da as &$val) {
  431. $val['finish_rate'] = bcmul(round(bcdiv($val['finish_total'], $val['total'], 5), 4), '100', 2) . '%';
  432. }
  433. return app_show(0, '请求成功', array_merge($da));
  434. }
  435. }