Data.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. $zixun_bids = Db::name('consult_bids')
  206. ->alias('cb')
  207. ->where('cb.is_del', 0)
  208. ->join('consult_order co', 'co.zxNo = cb.zxNo ')
  209. ->where('co.endtime', '>=', date('Y-m-d H:i:s'))
  210. ->group('cb.infoNo')
  211. ->count('cb.id');
  212. //未竞价单数(未反馈单数)
  213. $not_feedback = $zixun_total - $zixun_bids;
  214. //采购单数
  215. $purchease_total = Db::name('purchease_order')
  216. ->where('is_del', 0)
  217. ->count('id');
  218. //采购单未下单数(状态为待与供应商确认)
  219. $purchease_wait_confirm = Db::name('purchease_order')
  220. ->where(['is_del' => 0, 'status' => 0])
  221. ->count('id');
  222. //采购单 采购下单数???????
  223. return app_show(0, '请求成功', [
  224. 'zixun_total' => $zixun_total,
  225. 'not_feedback' => $not_feedback,
  226. 'purchease_total' => $purchease_total,
  227. 'purchease_wait_confirm' => $purchease_wait_confirm,
  228. ]);
  229. }
  230. //6.未发货
  231. public function waitSendTotal()
  232. {
  233. $rs = Db::name('sale')
  234. ->field('count(id) order_num,sum(wsend_num) wsend_num')
  235. ->where(['is_del' => 0, 'status' => 1])//status==1 待发货
  236. ->find();
  237. return app_show(0, '请求成功', $rs);
  238. }
  239. //7.今日订单
  240. public function todaySale()
  241. {
  242. $temp = Db::name('sale')
  243. ->alias('s')
  244. ->field('s.id,s.apply_id,s.total_price,u.nickname,u.itemid')
  245. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  246. ->where('s.is_del', 0)
  247. ->whereDay('s.addtime', 'today')
  248. ->buildSql();
  249. $rs = Db::table($temp)
  250. ->alias('t')
  251. ->field('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order')
  252. ->leftJoin('company_item c', 'c.id=t.itemid')
  253. ->group('t.itemid')
  254. ->order('total_price', 'desc')
  255. ->limit(3)
  256. ->select()
  257. ->toArray();
  258. return app_show(0, '请求成功', $rs);
  259. }
  260. //8.本月完成率(数据在结算库里,部门完成率=部门净销售额 / 部门销售指标)
  261. //没有销售指标的部门,不纳入统计范围
  262. public function monthFinishRate()
  263. {
  264. //部门净销售额
  265. $sales_volume = Db::name('sale')
  266. ->alias('s')
  267. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  268. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  269. ->group('u.itemid')
  270. ->column('(sum(s.total_price) - sum(s.th_fee)) sales_volume,u.itemid,c.name', 'c.name');
  271. //部门销售指标
  272. //数据在结算库invoice,连接配置信息待更新
  273. $sale_indicators = Db::connect('mysql3')
  274. ->table('cfp_depart_tips')
  275. ->field('id,total_tips,department')
  276. ->where(['year' => date('Y年'), 'month' => date('n月')])
  277. ->select()
  278. ->toArray();
  279. //计算完成率
  280. foreach ($sale_indicators as &$value) {
  281. $temp = isset($sales_volume[$value['department']]['sales_volume']) ? $sales_volume[$value['department']]['sales_volume'] : 0;
  282. $value['finish_rate'] = round(($temp / $value['total_tips']) * 100, 5);
  283. }
  284. //按照完成率排序
  285. usort($sale_indicators, function ($left, $right) {
  286. return ($left['finish_rate'] > $right['finish_rate']) ? -1 : 1;
  287. });
  288. //计算汇总完成率
  289. $total_finish_rate = round((array_sum(array_column($sales_volume, 'sales_volume')) / array_sum(array_column($sale_indicators, 'total_tips'))) * 100, 2);
  290. return app_show(0, '请求成功', ['list' => $sale_indicators, 'total_finish_rate' => $total_finish_rate]);
  291. }
  292. //9.转单率-今日
  293. public function orderTransferRateToday()
  294. {
  295. $consulting = Db::name('sale')
  296. ->alias('s')
  297. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  298. ->where(['s.order_type' => 2, 's.is_del' => 0])
  299. ->whereDay('s.addtime')
  300. ->group('u.itemid')
  301. ->column('count(s.id) consulting', 'u.itemid');
  302. $rs = Db::name('consult_order')
  303. ->alias('c')
  304. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  305. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  306. ->where(['c.is_del' => 0])
  307. ->whereDay('c.addtime')
  308. ->group('c.depart')
  309. ->append(['transfer_rate'])
  310. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  311. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  312. return round(($consult / $data['total']) * 100, 2);
  313. })
  314. ->select()
  315. ->toArray();
  316. return app_show(0, '请求成功', $rs);
  317. }
  318. //9.转单率-本月
  319. public function orderTransferRateMonth()
  320. {
  321. $consulting = Db::name('sale')
  322. ->alias('s')
  323. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  324. ->where(['s.order_type' => 2, 's.is_del' => 0])
  325. ->whereMonth('s.addtime')
  326. ->group('u.itemid')
  327. ->column('count(s.id) consulting', 'u.itemid');
  328. $rs = Db::name('consult_order')
  329. ->alias('c')
  330. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  331. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  332. ->where(['c.is_del' => 0])
  333. ->whereMonth('c.addtime')
  334. ->group('c.depart')
  335. ->append(['transfer_rate'])
  336. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  337. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  338. return round(($consult / $data['total']) * 100, 2);
  339. })
  340. ->select()
  341. ->toArray();
  342. return app_show(0, '请求成功', $rs);
  343. }
  344. //9.转单率-今年
  345. public function orderTransferRateYear()
  346. {
  347. $consulting = Db::name('sale')
  348. ->alias('s')
  349. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  350. ->where(['s.order_type' => 2, 's.is_del' => 0])
  351. ->whereYear('s.addtime', date('Y'))
  352. ->group('u.itemid')
  353. ->column('count(s.id) consulting', 'u.itemid');
  354. $rs = Db::name('consult_order')
  355. ->alias('c')
  356. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  357. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  358. ->where(['c.is_del' => 0])
  359. ->whereYear('c.addtime', date('Y'))
  360. ->group('c.depart')
  361. ->append(['transfer_rate'])
  362. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  363. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  364. return round(($consult / $data['total']) * 100, 2);
  365. })
  366. ->select()
  367. ->toArray();
  368. return app_show(0, '请求成功', $rs);
  369. }
  370. }