Data.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. $temp = [
  147. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,
  148. 'money' => isset($rs['money']) ? $rs['money'] : 0,
  149. ];
  150. return app_show(0, '请求成功', $temp);
  151. }
  152. //2.今日销冠
  153. public function todaySalesChampion()
  154. {
  155. $temp = Db::name('sale')
  156. ->field('count(id) orders,sum(total_price) money,apply_id')
  157. ->where('is_del', 0)
  158. ->whereDay('addtime', 'today')
  159. ->group('apply_id')
  160. ->order('money', 'desc')
  161. ->limit(1)
  162. ->buildSql();
  163. $rs = Db::table($temp)
  164. ->alias('t')
  165. ->field('t.apply_id,t.orders,t.money,u.itemid company_id, c.`name` company, u.nickname')
  166. ->leftJoin('depart_user u', 'u.uid=t.apply_id AND u.is_del=0')
  167. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  168. ->select()
  169. ->toArray();
  170. return app_show(0, '请求成功', empty($rs[0]) ? [] : $rs[0]);
  171. }
  172. //3.今日采购
  173. public function todayPurchase()
  174. {
  175. $rs = Db::name('purchease_order')
  176. ->field('count(id) orders_number,sum(good_num) good_num')
  177. ->where('is_del', 0)
  178. ->whereDay('addtime', 'today')
  179. ->find();
  180. $consult_info_total = Db::name('consult_info')
  181. ->where('is_del', 0)
  182. ->whereDay('addtime', 'today')
  183. ->count('id');
  184. $consult_bids_total = Db::name('consult_bids')
  185. ->where('is_del', 0)
  186. ->whereDay('addtime', 'today')
  187. ->count('id');
  188. $data = [
  189. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,
  190. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,
  191. 'consult_info_total' => $consult_info_total,
  192. 'consult_bids_total' => $consult_bids_total,
  193. ];
  194. return app_show(0, '请求成功', $data);
  195. }
  196. //4.票(相关,暂不做)
  197. //5.竞价单和采购单
  198. public function totalZixunPurchease()
  199. {
  200. //招标单数(咨询单数)
  201. $zixun_total = Db::name('consult_info')
  202. ->where(['is_del' => 0])
  203. ->count('id');
  204. //已反馈单数,
  205. $sql = Db::name('consult_info')
  206. ->field('zxNo')
  207. ->where(['is_del' => 0, 'status' => 1])
  208. ->buildSql();
  209. //正在进行中的咨询单(招标工作台上的数据)
  210. $zixun_num_ing = Db::name('consult_info')
  211. ->field('zxNo')
  212. ->where(['is_del' => 0, 'status' => 1])
  213. ->count('id');
  214. //已反馈的单数
  215. $not_feedback = Db::name('consult_bids')
  216. ->where('zxNo IN ' . $sql)
  217. ->count('id');
  218. //未竞价单数(未反馈单数) = 进行中的单数-已反馈单数
  219. $not_feedback = $zixun_num_ing - $not_feedback;
  220. //采购单数
  221. $purchease_total = Db::name('purchease_order')
  222. ->where('is_del', 0)
  223. ->count('id');
  224. //采购单未下单数(状态为待与供应商确认)
  225. $purchease_wait_confirm = Db::name('purchease_order')
  226. ->where(['is_del' => 0, 'status' => 0])
  227. ->count('id');
  228. //采购单 采购下单数???????
  229. return app_show(0, '请求成功', [
  230. 'zixun_total' => $zixun_total,
  231. 'not_feedback' => $not_feedback,
  232. 'purchease_total' => $purchease_total,
  233. 'purchease_wait_confirm' => $purchease_wait_confirm,
  234. ]);
  235. }
  236. //6.未发货
  237. public function waitSendTotal()
  238. {
  239. $rs = Db::name('sale')
  240. ->field('count(id) order_num,sum(wsend_num) wsend_num')
  241. ->where(['is_del' => 0, 'status' => 1])//status==1 待发货
  242. ->find();
  243. return app_show(0, '请求成功', $rs);
  244. }
  245. //7.今日订单
  246. public function todaySale()
  247. {
  248. $temp = Db::name('sale')
  249. ->alias('s')
  250. ->field('s.id,s.apply_id,s.total_price,u.nickname,u.itemid')
  251. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  252. ->where('s.is_del', 0)
  253. ->whereDay('s.addtime', 'today')
  254. ->buildSql();
  255. $rs = Db::table($temp)
  256. ->alias('t')
  257. ->field('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order')
  258. ->leftJoin('company_item c', 'c.id=t.itemid')
  259. ->group('t.itemid')
  260. ->order('total_price', 'desc')
  261. // ->limit(3)
  262. ->select()
  263. ->toArray();
  264. //跨库添加网络部数据
  265. $internet = Db::connect('mysql3')
  266. ->table('source_all')
  267. ->field('SUM(sale_total) total_price,COUNT(id) total_order,depart ')
  268. ->whereDay('ordertime')
  269. ->group('depart')
  270. ->having('depart = "网络部"')
  271. ->select()
  272. ->toArray();
  273. if (!empty($internet[0])) {
  274. $rs[] = [
  275. 'companyId' => '',
  276. 'companyName' => $internet[0]['depart'],
  277. 'total_price' => $internet[0]['total_price'],
  278. 'total_order' => $internet[0]['total_order'],
  279. ];
  280. //重新按照总金额排序
  281. //按照完成率排序
  282. usort($rs, function ($left, $right) {
  283. return ($left['total_price'] > $right['total_price']) ? -1 : 1;
  284. });
  285. }
  286. return app_show(0, '请求成功', $rs);
  287. }
  288. //8.本月完成率(数据在结算库里,部门完成率=部门净销售额 / 部门销售指标)
  289. //没有销售指标的部门,不纳入统计范围
  290. public function monthFinishRate()
  291. {
  292. //部门净销售额
  293. $sales_volume = Db::name('sale')
  294. ->alias('s')
  295. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  296. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  297. ->group('u.itemid')
  298. ->whereMonth('s.addtime')
  299. ->column('(sum(s.total_price) - sum(s.th_fee)) sales_volume,u.itemid,c.name', 'c.name');
  300. //额外把网络部的销售数据跨数据库查询出来
  301. $sales_volume['网络部'] = [
  302. 'sales_volume' => Db::connect('mysql3')->table('source_all')->whereMonth('ordertime')->where('depart', '网络部')->sum('sale_total'),
  303. 'itemid' => -1,
  304. 'name' => '网络部',
  305. ];
  306. //部门销售指标
  307. //数据在结算库invoice,连接配置信息待更新
  308. $sale_indicators = Db::name('depart_tips')
  309. ->field('id,total_tips,depart_item department')
  310. ->where(['year' => date('Y'), 'month' => date('n')])
  311. ->select()
  312. ->toArray();
  313. $da = [];
  314. //计算完成率
  315. foreach ($sale_indicators as $value) {
  316. if (isset($sales_volume[$value['department']]['sales_volume'])) {
  317. $value['finish'] = $sales_volume[$value['department']]['sales_volume'];
  318. $value['finish_rate'] = round(($value['finish'] / $value['total_tips']) * 100, 5);
  319. $da[] = $value;
  320. } else continue;
  321. }
  322. //按照完成率排序
  323. usort($da, function ($left, $right) {
  324. return ($left['finish_rate'] > $right['finish_rate']) ? -1 : 1;
  325. });
  326. //计算汇总完成率
  327. $total_finish_rate = round((array_sum(array_column($sales_volume, 'sales_volume')) / array_sum(array_column($da, 'total_tips'))) * 100, 2);
  328. return app_show(0, '请求成功', ['list' => $da, 'total_finish_rate' => $total_finish_rate]);
  329. }
  330. //9.转单率-今日
  331. public function orderTransferRateToday()
  332. {
  333. $consulting = Db::name('sale')
  334. ->alias('s')
  335. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  336. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  337. ->whereDay('s.addtime')
  338. ->group('u.itemid')
  339. ->column('count(s.id) consulting', 'u.itemid');
  340. $rs = Db::name('consult_order')
  341. ->alias('c')
  342. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  343. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  344. ->where(['c.is_del' => 0])
  345. ->whereDay('c.addtime')
  346. ->group('c.depart')
  347. ->append(['transfer_rate'])
  348. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  349. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  350. return round(($consult / $data['total']) * 100, 2);
  351. })
  352. ->select()
  353. ->toArray();
  354. return app_show(0, '请求成功', $rs);
  355. }
  356. //9.转单率-本月
  357. public function orderTransferRateMonth()
  358. {
  359. $consulting = Db::name('sale')
  360. ->alias('s')
  361. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  362. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  363. ->whereMonth('s.addtime')
  364. ->group('u.itemid')
  365. ->column('count(s.id) consulting', 'u.itemid');
  366. $rs = Db::name('consult_order')
  367. ->alias('c')
  368. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  369. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  370. ->where(['c.is_del' => 0])
  371. ->whereMonth('c.addtime')
  372. ->group('c.depart')
  373. ->append(['transfer_rate'])
  374. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  375. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  376. return round(($consult / $data['total']) * 100, 2);
  377. })
  378. ->select()
  379. ->toArray();
  380. return app_show(0, '请求成功', $rs);
  381. }
  382. //9.转单率-今年
  383. public function orderTransferRateYear()
  384. {
  385. $consulting = Db::name('sale')
  386. ->alias('s')
  387. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  388. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  389. ->whereYear('s.addtime', date('Y'))
  390. ->group('u.itemid')
  391. ->column('count(s.id) consulting', 'u.itemid');
  392. $rs = Db::name('consult_order')
  393. ->alias('c')
  394. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  395. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  396. ->where(['c.is_del' => 0])
  397. ->whereYear('c.addtime', date('Y'))
  398. ->group('c.depart')
  399. ->append(['transfer_rate'])
  400. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  401. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  402. return round(($consult / $data['total']) * 100, 2);
  403. })
  404. ->select()
  405. ->toArray();
  406. return app_show(0, '请求成功', $rs);
  407. }
  408. }