Stat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;
  5. class Stat extends \app\BaseController
  6. {
  7. public function __construct(App $app)
  8. {
  9. parent::__construct($app);
  10. }
  11. public function list()
  12. {
  13. $post = request()->post();
  14. $endtime = isset($post['date']) ? $post['date'] : date("Y-m-d");
  15. $sql = "
  16. SELECT
  17. `month` AS m,
  18. ifnull( month_total_fee, 0 ) AS total,
  19. ifnull( `week`, '' ) AS w,
  20. ifnull( week_total_fee, 0 ) AS wtotal,
  21. ifnull( `day`, '' ) AS d,
  22. ifnull( day_total_fee, 0 ) AS dtotal,
  23. ifnull( a.NAME, '' ) AS depart,
  24. a.id AS depar_id,
  25. ifnull( month_thfee, 0 ) AS mthfee,
  26. ifnull( week_thfee, 0 ) AS wthfee,
  27. ifnull( day_thfee, 0 ) AS thfee,
  28. ifnull( a.platform_name, '' ) AS p
  29. FROM
  30. (
  31. SELECT MONTH
  32. ( a.addtime ) AS `month`,
  33. SUM( total_price ) AS month_total_fee,
  34. ((
  35. SELECT
  36. ifnull(sum(total_fee),0)
  37. FROM
  38. wsm_sale_return e
  39. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  40. AND k.is_del = 0
  41. AND k.STATUS = 1
  42. WHERE
  43. k.itemid = c.id
  44. AND e.platform_id = a.platform_id
  45. AND DATE_FORMAT( e.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )) + (
  46. SELECT
  47. ifnull(sum(total_fee),0)
  48. FROM
  49. wsm_order_back f
  50. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  51. AND p.is_del = 0
  52. AND p.STATUS = 1
  53. WHERE
  54. p.itemid = c.id
  55. AND f.platform_id = a.platform_id
  56. AND DATE_FORMAT( f.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' ))) AS month_thfee,
  57. a.platform_id,
  58. c.`name`,
  59. c.id,
  60. d.platform_name
  61. FROM
  62. wsm_sale a
  63. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  64. AND b.is_del = 0
  65. AND b.STATUS = 1
  66. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  67. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  68. WHERE
  69. DATE_FORMAT( a.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )
  70. GROUP BY
  71. a.platform_id,
  72. platform_name,
  73. c.NAME,
  74. c.id,
  75. `month`
  76. ) AS a
  77. LEFT JOIN (
  78. SELECT WEEK
  79. ( a.addtime, 1 ) AS `week`,
  80. SUM( total_price ) AS week_total_fee,
  81. ((
  82. SELECT
  83. ifnull(sum(total_fee),0)
  84. FROM
  85. wsm_sale_return e
  86. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  87. AND k.is_del = 0
  88. AND k.STATUS = 1
  89. WHERE
  90. k.itemid = c.id
  91. AND e.platform_id = a.platform_id
  92. AND WEEK ( e.addtime, 1 ) = WEEK ( '$endtime', 1 )
  93. AND DATE_FORMAT ( e.addtime, '%Y' ) = DATE_FORMAT ( '$endtime', '%Y' )) + (
  94. SELECT
  95. ifnull(sum(total_fee),0)
  96. FROM
  97. wsm_order_back f
  98. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  99. AND p.is_del = 0
  100. AND p.STATUS = 1
  101. WHERE
  102. p.itemid = c.id
  103. AND f.platform_id = a.platform_id
  104. AND WEEK ( f.addtime, 1 ) = WEEK ( '$endtime', 1 )
  105. AND DATE_FORMAT ( f.addtime, '%Y' ) = DATE_FORMAT ( '$endtime', '%Y' )
  106. )) AS week_thfee,
  107. a.platform_id,
  108. c.`name`,
  109. c.id,
  110. platform_name
  111. FROM
  112. wsm_sale a
  113. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  114. AND b.is_del = 0
  115. AND b.STATUS = 1
  116. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  117. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  118. WHERE
  119. WEEK ( a.addtime, 1 ) = WEEK ( '$endtime', 1 )
  120. GROUP BY
  121. a.platform_id,
  122. platform_name,
  123. c.NAME,
  124. c.`id`,
  125. `week`
  126. ) AS b ON a.NAME = b.NAME
  127. AND a.platform_id = b.platform_id
  128. LEFT JOIN (
  129. SELECT
  130. date_format( a.addtime, '%Y-%m-%d' ) AS `day`,
  131. SUM( total_price ) AS day_total_fee,
  132. ((
  133. SELECT
  134. ifnull(sum(total_fee),0)
  135. FROM
  136. wsm_sale_return e
  137. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  138. AND k.is_del = 0
  139. AND k.STATUS = 1
  140. WHERE
  141. k.itemid = c.id
  142. AND e.platform_id = a.platform_id
  143. AND DATE_FORMAT( e.addtime, '%Y-%m-%d' ) = DATE_FORMAT( '$endtime', '%Y-%m-%d' )) + (
  144. SELECT
  145. ifnull(sum(total_fee),0)
  146. FROM
  147. wsm_order_back f
  148. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  149. AND p.is_del = 0
  150. AND p.STATUS = 1
  151. WHERE
  152. p.itemid = c.id
  153. AND f.platform_id = a.platform_id
  154. AND DATE_FORMAT( f.addtime, '%Y-%m-%d' ) = DATE_FORMAT( '$endtime', '%Y-%m-%d' ))) AS day_thfee,
  155. a.platform_id,
  156. c.`name`,
  157. c.id AS depar_id,
  158. platform_name
  159. FROM
  160. wsm_sale a
  161. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  162. AND b.is_del = 0
  163. AND b.STATUS = 1
  164. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  165. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  166. WHERE
  167. date_format( a.addtime, '%Y-%m-%d' ) = date_format ( '$endtime', '%Y-%m-%d' )
  168. GROUP BY
  169. a.platform_id,
  170. platform_name,
  171. c.NAME,
  172. c.id,
  173. `day`
  174. ) AS d ON a.NAME = d.NAME
  175. AND a.platform_id = d.platform_id
  176. ";
  177. $data = Db::query($sql);
  178. $dtae = isset($endtime) && $endtime != "" ? $endtime . " 00:00:00" : date("Y-m-d H:i:s");
  179. $temp = ["p" => "总计", 'item' => "-", "thtotal" => 0, "total" => 0, "mthfee" => 0, "wtotal" => 0, "wthfee" => 0, "dtotal" => 0, "thfee" => 0, "tips" => 0, "rate" => 0, "thrate" => 0];
  180. $tips = Db::name("depart_tips")->field("depart_id ,total_tips")->where("year=YEAR('{$dtae}') and month=month('{$dtae}')")->select();
  181. $kdata = [];
  182. try{
  183. $wlb =$this->innterOrder($endtime);
  184. if(!empty($wlb)) $data=array_merge($data,$wlb);
  185. foreach ($data as $key => $value) {
  186. $value['tips'] = 0;
  187. foreach ($tips as $val) {
  188. if ($value["depar_id"] == $val['depart_id']) {
  189. $value['tips'] = $val['total_tips'];
  190. }
  191. }
  192. $value['thtotal'] = $value['total'] - $value['mthfee'];
  193. $kdata[] = $value;
  194. }
  195. $list = $this->check($kdata, $temp);
  196. array_walk($list, function (&$value) {
  197. if (is_null($value['tips']) || $value['tips'] == 0) {
  198. $value['tips'] = "-";
  199. $value["rate"] = "-";
  200. $value["thrate"] = "-";
  201. } else {
  202. $value["rate"] = number_format($value['total'] / $value['tips'] * 100, 2);
  203. $value["thrate"] = number_format(($value['total'] - $value['mthfee']) / $value['tips'] * 100, 2);
  204. $value['tips'] = sprintf("%.2f", $value['tips']);
  205. }
  206. $value['total'] = sprintf("%.2f", $value['total']);
  207. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  208. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  209. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  210. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  211. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  212. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  213. });
  214. $list=array_values($list);
  215. if ($temp['tips'] == 0) {
  216. $temp['rate'] = "-";
  217. $temp['thrate'] = "-";
  218. $temp['tips'] = "-";
  219. } else {
  220. $temp['rate'] = number_format($temp['total'] / $temp['tips'] * 100, 2);
  221. $temp['thrate'] = number_format(($temp['total'] - $temp['mthfee']) / $temp['tips'] * 100, 2);;
  222. $temp['tips'] = round($temp['tips'], 2);
  223. }
  224. $temp['total'] = sprintf("%.2f", $temp['total']);
  225. $temp['mthfee'] = sprintf("%.2f", $temp['mthfee']);
  226. $temp['wtotal'] = sprintf("%.2f", $temp['wtotal']);
  227. $temp['wthfee'] = sprintf("%.2f", $temp['wthfee']);
  228. $temp['dtotal'] = sprintf("%.2f", $temp['dtotal']);
  229. $temp['thfee'] = sprintf("%.2f", $temp['thfee']);
  230. $temp['thtotal'] = sprintf("%.2f", $temp['thtotal']);
  231. $sort = array_column($list, "thrate");
  232. array_multisort($sort, SORT_ASC, $list);
  233. array_push($list, $temp);
  234. $list = array_reverse($list);
  235. }catch (\Exception $e){
  236. return error_show(1004, $e->getFile()."|".$e->getLine()."|".$e->getMessage());
  237. }
  238. return app_show(0, "获取成功", $list);
  239. }
  240. public function check($data, &$temp)
  241. {
  242. $list = [];
  243. foreach ($data as $key => $value) {
  244. $temp['total'] += $value['total'];
  245. $temp['mthfee'] += $value['mthfee'];
  246. $temp['wtotal'] += $value['wtotal'];
  247. $temp['wthfee'] += $value['wthfee'];
  248. $temp['thfee'] += $value['thfee'];
  249. $temp['dtotal'] += $value['dtotal'];
  250. isset($list[$value['depar_id']]) ? "" : $list[$value['depar_id']] = ['total' => 0, 'mthfee' => 0, 'tips' => $value['tips'], 'wtotal' => 0, 'wthfee' => 0, 'dtotal' => 0,
  251. 'child' => [], 'depart' => '', 'thfee' => 0, 'thtotal' => 0];
  252. $list[$value['depar_id']]['total'] += $value['total'];
  253. $list[$value['depar_id']]['mthfee'] += $value['mthfee'];
  254. $list[$value['depar_id']]['wtotal'] += $value['wtotal'];
  255. $list[$value['depar_id']]['wthfee'] += $value['wthfee'];
  256. $list[$value['depar_id']]['dtotal'] += $value['dtotal'];
  257. $list[$value['depar_id']]['thfee'] += $value['thfee'];
  258. $list[$value['depar_id']]['thtotal'] += $value['thtotal'];
  259. $value['tips']="-";
  260. $value['total'] = sprintf("%.2f", $value['total']);
  261. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  262. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  263. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  264. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  265. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  266. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  267. $list[$value['depar_id']]['child'][] = $value;
  268. $list[$value['depar_id']]['depart'] = $value['depart'];
  269. }
  270. $temp['tips'] = array_sum(array_column($list,'tips'));
  271. $temp['thtotal'] = sprintf("%.2f", array_sum(array_column($list,'thtotal')));
  272. return $list;
  273. }
  274. public function innterOrder($endtime)
  275. {
  276. $db =Db::connect("mysql3");
  277. $list = $db->query("SELECT
  278. a.suppitem as p,
  279. a.depart,
  280. 43 as depar_id,
  281. m,
  282. a.sale_total AS total,
  283. `a`.`th_total` AS `mthfee`,
  284. IFNULL(w,WEEK('{$endtime}',1)) as w,
  285. IFNULL(b.sale_total,0) AS wtotal,
  286. ifnull( `b`.`th_total`, 0 ) AS `wthfee`,
  287. IFNULL(c.d,date_format('{$endtime}', '%Y-%m-%d' )) as d,
  288. IFNULL(c.sale_total,0) AS dtotal,
  289. ifnull( `c`.`th_total`, 0 ) AS `thfee`
  290. FROM
  291. (
  292. SELECT MONTH
  293. ( ordertime ) AS m,
  294. sum( sale_total ) AS sale_total,
  295. suppitem,
  296. depart,
  297. (
  298. SELECT
  299. ifnull( sum( t.th_qrd_fee ), 0 )
  300. FROM
  301. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  302. WHERE
  303. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  304. AND DATE_FORMAT ( t.createtime,'%Y-%m' ) = DATE_FORMAT ( '{$endtime}' ,'%Y-%m')
  305. ) AS th_total
  306. FROM
  307. source_all k
  308. WHERE
  309. DATE_FORMAT(ordertime,'%Y-%m') = DATE_FORMAT('{$endtime}','%Y-%m')
  310. GROUP BY
  311. suppitem,depart,
  312. m
  313. ) AS a
  314. LEFT JOIN (
  315. SELECT WEEK
  316. ( ordertime, 1 ) AS w,
  317. sum( sale_total ) AS sale_total,
  318. suppitem,
  319. depart,
  320. (
  321. SELECT
  322. ifnull( sum( t.th_qrd_fee ), 0 )
  323. FROM
  324. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  325. WHERE
  326. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  327. AND week ( t.createtime,1 ) = WEEK ( '{$endtime}', 1 ) and DATE_FORMAT ( t.createtime,'%Y' ) = DATE_FORMAT ( '{$endtime}' ,'%Y')
  328. ) AS th_total
  329. FROM
  330. source_all k
  331. WHERE
  332. WEEK ( ordertime, 1 ) = WEEK ( '{$endtime}', 1 ) and DATE_FORMAT ( ordertime,'%Y' ) = DATE_FORMAT ( '{$endtime}' ,'%Y')
  333. GROUP BY
  334. suppitem,depart,
  335. w
  336. ) AS b ON a.suppitem = b.suppitem and a.depart=b.depart
  337. LEFT JOIN (
  338. SELECT
  339. date_format( ordertime, '%Y-%m-%d' ) AS d,
  340. sum( sale_total ) AS sale_total,
  341. suppitem,depart,
  342. (
  343. SELECT
  344. ifnull( sum( t.th_qrd_fee ), 0 )
  345. FROM
  346. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  347. WHERE
  348. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  349. AND date_format ( t.createtime,'%Y-%m-%d') = date_format ( '{$endtime}', '%Y-%m-%d' )
  350. ) AS th_total FROM
  351. source_all k
  352. WHERE
  353. date_format( ordertime, '%Y-%m-%d' ) = date_format( '{$endtime}', '%Y-%m-%d' )
  354. GROUP BY
  355. suppitem,depart,
  356. d
  357. ) AS c ON a.suppitem = c.suppitem and a.depart=c.depart
  358. ");
  359. return $list;
  360. }
  361. }