Data.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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($rs['网络部'])) {
  155. $rs['网络部']['total_price'] += $internet['网络部']['total_price'];
  156. $rs['网络部']['total_order'] += $internet['网络部']['total_order'];
  157. } else {
  158. $rs[] = [
  159. 'companyId' => '',
  160. 'companyName' => '网络部',
  161. 'total_price' => $internet['网络部']['total_price'],
  162. 'total_order' => $internet['网络部']['total_order'],
  163. ];
  164. }
  165. if (isset($rs['客服部'])) {
  166. $rs['客服部']['total_price'] += $internet['客服部']['total_price'];
  167. $rs['客服部']['total_order'] += $internet['客服部']['total_order'];
  168. } else {
  169. $rs[] = [
  170. 'companyId' => '',
  171. 'companyName' => '客服部',
  172. 'total_price' => $internet['客服部']['total_price'],
  173. 'total_order' => $internet['客服部']['total_order'],
  174. ];
  175. }
  176. //重新按照总金额排序
  177. usort($rs, function ($left, $right) {
  178. return ($left['total_price'] > $right['total_price']) ? -1 : 1;
  179. });
  180. }
  181. return app_show(0, '请求成功', $rs);
  182. }
  183. //8.本月完成率(数据在结算库里,部门完成率=部门净销售额 / 部门销售指标)
  184. //没有销售指标的部门,不纳入统计范围
  185. public function monthFinishRate()
  186. {
  187. //部门净销售额
  188. $sales_volume = Db::name('sale')
  189. ->alias('s')
  190. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  191. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  192. ->group('u.itemid')
  193. ->whereMonth('s.addtime')
  194. ->column('(sum(s.total_price) - sum(s.th_fee)) sales_volume,u.itemid,c.name', 'c.name');
  195. //额外把网络部和客服部的销售数据跨数据库查询出来
  196. $other_network = Db::connect('mysql3')
  197. ->table('source_all')
  198. ->whereMonth('ordertime')
  199. ->whereIn('depart', ['网络部', '客服部'])
  200. ->group('depart')
  201. ->column('SUM(sale_total) sale_total', 'depart');
  202. if (isset($other_network['网络部'])) {
  203. if (isset($sales_volume['网络部'])) $sales_volume['网络部']['sales_volume'] += $other_network['网络部'];
  204. else {
  205. $sales_volume['网络部'] = [
  206. 'sales_volume' => $other_network['网络部'],
  207. 'itemid' => -1,
  208. 'name' => '网络部'
  209. ];
  210. }
  211. }
  212. if (isset($other_network['客服部'])) {
  213. if (isset($sales_volume['客服部'])) $sales_volume['客服部']['sales_volume'] += $other_network['客服部'];
  214. else {
  215. $sales_volume['客服部'] = [
  216. 'sales_volume' => $other_network['客服部'],
  217. 'itemid' => -2,
  218. 'name' => '客服部'
  219. ];
  220. }
  221. }
  222. //部门销售指标
  223. $sale_indicators = Db::name('depart_tips')
  224. ->field('id,total_tips,depart_item department')
  225. ->where(['year' => date('Y'), 'month' => date('n')])
  226. ->select()
  227. ->toArray();
  228. $da = [];
  229. //计算完成率
  230. foreach ($sale_indicators as $value) {
  231. if (isset($sales_volume[$value['department']]['sales_volume'])) {
  232. $value['finish'] = $sales_volume[$value['department']]['sales_volume'];
  233. $value['finish_rate'] = round(($value['finish'] / $value['total_tips']) * 100, 5);
  234. $da[] = $value;
  235. } else continue;
  236. }
  237. //按照完成率排序
  238. usort($da, function ($left, $right) {
  239. return ($left['finish_rate'] > $right['finish_rate']) ? -1 : 1;
  240. });
  241. //计算汇总完成率
  242. $total_finish_rate = round((array_sum(array_column($sales_volume, 'sales_volume')) / array_sum(array_column($da, 'total_tips'))) * 100, 2);
  243. return app_show(0, '请求成功', ['list' => $da, 'total_finish_rate' => $total_finish_rate]);
  244. }
  245. //9.转单率-今日
  246. public function orderTransferRateToday()
  247. {
  248. $consulting = Db::name('sale')
  249. ->alias('s')
  250. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  251. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  252. ->whereDay('s.addtime')
  253. ->group('u.itemid')
  254. ->column('count(s.id) consulting', 'u.itemid');
  255. $rs = Db::name('consult_order')
  256. ->alias('c')
  257. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  258. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  259. ->where(['c.is_del' => 0])
  260. ->whereDay('c.addtime')
  261. ->group('c.depart')
  262. ->append(['transfer_rate'])
  263. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  264. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  265. return round(($consult / $data['total']) * 100, 2);
  266. })
  267. ->select()
  268. ->toArray();
  269. return app_show(0, '请求成功', $rs);
  270. }
  271. //9.转单率-本月
  272. public function orderTransferRateMonth()
  273. {
  274. $consulting = Db::name('sale')
  275. ->alias('s')
  276. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  277. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  278. ->whereMonth('s.addtime')
  279. ->group('u.itemid')
  280. ->column('count(s.id) consulting', 'u.itemid');
  281. $rs = Db::name('consult_order')
  282. ->alias('c')
  283. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  284. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  285. ->where(['c.is_del' => 0])
  286. ->whereMonth('c.addtime')
  287. ->group('c.depart')
  288. ->append(['transfer_rate'])
  289. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  290. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  291. return round(($consult / $data['total']) * 100, 2);
  292. })
  293. ->select()
  294. ->toArray();
  295. return app_show(0, '请求成功', $rs);
  296. }
  297. //9.转单率-今年
  298. public function orderTransferRateYear()
  299. {
  300. $consulting = Db::name('sale')
  301. ->alias('s')
  302. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  303. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  304. ->whereYear('s.addtime', date('Y'))
  305. ->group('u.itemid')
  306. ->column('count(s.id) consulting', 'u.itemid');
  307. $rs = Db::name('consult_order')
  308. ->alias('c')
  309. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  310. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  311. ->where(['c.is_del' => 0])
  312. ->whereYear('c.addtime', date('Y'))
  313. ->group('c.depart')
  314. ->append(['transfer_rate'])
  315. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  316. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  317. return round(($consult / $data['total']) * 100, 2);
  318. })
  319. ->select()
  320. ->toArray();
  321. return app_show(0, '请求成功', $rs);
  322. }
  323. //******* 以下是新版数据大屏 的内容***********************************
  324. //1.今日销售
  325. public function dnTodaySale()
  326. {
  327. $rs = Db::name('sale')
  328. ->field('count(id) orders_number,sum(total_price)-sum(th_fee) money,sum(good_num)-sum(th_num) good_num ')
  329. ->where('is_del', 0)
  330. ->whereDay('addtime', 'today')
  331. ->find();
  332. //跨库查询网络部和客服部
  333. $internet = Db::connect('mysql3')
  334. ->table('source_all')
  335. ->field('SUM(sale_total) money,SUM(order_num) good_num,COUNT(id) orders_number')
  336. ->whereDay('ordertime')
  337. ->whereIn('depart', ['网络部', '客服部'])
  338. ->find();
  339. //组织数据
  340. $data = [
  341. 'orders_number' => bcadd(isset($rs['orders_number']) ? $rs['orders_number'] : '0', isset($internet['orders_number']) ? $internet['orders_number'] : '0'),
  342. 'money' => bcadd(isset($rs['money']) ? $rs['money'] : '0', isset($internet['money']) ? $internet['money'] : '0', 2),
  343. 'good_num' => bcadd(isset($rs['good_num']) ? $rs['good_num'] : '0', isset($internet['good_num']) ? $internet['good_num'] : '0', 2),
  344. ];
  345. return app_show(0, '请求成功', $data);
  346. }
  347. //2.今日采购
  348. public function dnTodayPurcheaseOrder()
  349. {
  350. $rs = Db::name('purchease_order')
  351. ->field('count(id) orders_number,sum(good_num) good_num')
  352. ->where('is_del', 0)
  353. ->whereDay('addtime')
  354. ->find();
  355. $consult_info_total = Db::name('consult_info')
  356. ->where(['is_del' => 0, 'status' => 5])//status==5成功转单
  357. ->whereDay('updatetime')
  358. ->count('id');
  359. $consult_bids_total = Db::name('consult_bids')
  360. ->where('is_del', 0)
  361. ->whereDay('addtime')
  362. ->count('id');
  363. $data = [
  364. 'consult_info_total' => $consult_info_total,//竞价中标数
  365. 'consult_bids_total' => $consult_bids_total,//反馈商品数
  366. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,//采购订单
  367. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,//商品数量
  368. ];
  369. return app_show(0, '请求成功', $data);
  370. }
  371. //3.今日结算
  372. //4.销售转单数
  373. public function dnTodayTransferOrder()
  374. {
  375. $param = $this->request->filter('trim')->only(['itemid' => '0', 'type' => '3', 'date' => date('Y-m-d')], 'post');
  376. $val = Validate::rule([
  377. 'itemid|部门id' => 'require|integer',
  378. 'type|日期类型' => 'require|number|in:1,2,3',
  379. 'date|筛选日期' => 'require|date',
  380. ]);
  381. if (!$val->check($param)) return error_show(1005, $val->getError());
  382. $rs = Db::name('consult_info')
  383. ->alias('a')
  384. ->leftJoin('consult_order b', 'b.zxNo=a.zxNo AND b.is_del=0')
  385. ->leftJoin('depart_user u', 'u.uid=b.saleid AND u.is_del=0')
  386. ->where(['a.is_del' => 0, 'bargain_num' => 1]);
  387. //查询符合条件的竞价单
  388. if ($param['itemid'] == '0') {
  389. //以部门为维度
  390. $rs = $rs
  391. ->field('a.id,a.status,u.itemid total_id,c.name')
  392. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0');
  393. } else {
  394. //以部门下的人为维度
  395. $rs = $rs
  396. ->field('a.id,a.status,b.saleid total_id,b.salesman name')
  397. ->where('u.itemid', $param['itemid']);
  398. }
  399. //时间段判断
  400. switch ($param['type']) {
  401. case '1':
  402. $rs->whereYear('a.addtime', $param['date']);
  403. break;
  404. case '2':
  405. $rs->whereMonth('a.addtime', $param['date']);
  406. break;
  407. default:
  408. $rs->whereDay('a.addtime', $param['date']);
  409. break;
  410. }
  411. $data = $rs->cursor();
  412. $da = [];
  413. //组织数据
  414. foreach ($data as $value) {
  415. if (!isset($da[$value['total_id']])) {
  416. $da[$value['total_id']] = [
  417. 'finish_total' => 0,//销售单(中标单数)
  418. 'total' => 0,//竞价单(总单数)
  419. 'name' => $value['name']//名称
  420. ];
  421. }
  422. $da[$value['total_id']]['total']++;
  423. if ($value['status'] == '5') $da[$value['total_id']]['finish_total']++;
  424. }
  425. //计算转单率
  426. foreach ($da as &$val) {
  427. $val['finish_rate'] = bcmul(round(bcdiv($val['finish_total'], $val['total'], 5), 4), '100', 2) . '%';
  428. }
  429. return app_show(0, '请求成功', array_merge($da));
  430. }
  431. }