Data.php 17 KB

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