Data.php 17 KB

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