Stat.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. sum( total_fee )
  37. FROM
  38. wsm_sale_return e
  39. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  40. WHERE
  41. k.itemid = c.id
  42. AND e.platform_id = a.platform_id
  43. AND DATE_FORMAT( e.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )) + (
  44. SELECT
  45. sum( total_fee )
  46. FROM
  47. wsm_order_back f
  48. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  49. WHERE
  50. p.itemid = c.id
  51. AND f.platform_id = a.platform_id
  52. AND DATE_FORMAT( f.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' ))) AS month_thfee,
  53. a.platform_id,
  54. c.`name`,
  55. c.id,
  56. d.platform_name
  57. FROM
  58. wsm_sale a
  59. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  60. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  61. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  62. WHERE
  63. DATE_FORMAT( a.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )
  64. GROUP BY
  65. a.platform_id,
  66. platform_name,
  67. c.NAME,
  68. c.id,
  69. `month`
  70. ) AS a
  71. LEFT JOIN (
  72. SELECT WEEK
  73. ( a.addtime, 1 ) AS `week`,
  74. SUM( total_price ) AS week_total_fee,
  75. ((
  76. SELECT
  77. sum( total_fee )
  78. FROM
  79. wsm_sale_return e
  80. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  81. WHERE
  82. k.itemid = c.id
  83. AND e.platform_id = a.platform_id
  84. AND WEEK ( e.addtime, 1 ) = WEEK ( '$endtime', 1 )
  85. AND DATE_FORMAT ( e.addtime, '%Y' ) = DATE_FORMAT ( '$endtime', '%Y' )) + (
  86. SELECT
  87. sum( total_fee )
  88. FROM
  89. wsm_order_back f
  90. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  91. WHERE
  92. p.itemid = c.id
  93. AND f.platform_id = a.platform_id
  94. AND WEEK ( f.addtime, 1 ) = WEEK ( '$endtime', 1 )
  95. AND DATE_FORMAT ( f.addtime, '%Y' ) = DATE_FORMAT ( '$endtime', '%Y' )
  96. )) AS week_thfee,
  97. a.platform_id,
  98. c.`name`,
  99. c.id,
  100. platform_name
  101. FROM
  102. wsm_sale a
  103. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  104. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  105. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  106. WHERE
  107. WEEK ( a.addtime, 1 ) = WEEK ( '$endtime', 1 )
  108. GROUP BY
  109. a.platform_id,
  110. platform_name,
  111. c.NAME,
  112. c.`id`,
  113. `week`
  114. ) AS b ON a.NAME = b.NAME
  115. AND a.platform_id = b.platform_id
  116. LEFT JOIN (
  117. SELECT
  118. date_format( a.addtime, '%Y-%m-%d' ) AS `day`,
  119. SUM( total_price ) AS day_total_fee,
  120. ((
  121. SELECT
  122. sum( total_fee )
  123. FROM
  124. wsm_sale_return e
  125. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  126. WHERE
  127. k.itemid = c.id
  128. AND e.platform_id = a.platform_id
  129. AND DATE_FORMAT( e.addtime, '%Y-%m-%d' ) = DATE_FORMAT( '$endtime', '%Y-%m-%d' )) + (
  130. SELECT
  131. sum( total_fee )
  132. FROM
  133. wsm_order_back f
  134. LEFT JOIN wsm_depart_user p ON f.apply_id = p.uid
  135. WHERE
  136. p.itemid = c.id
  137. AND f.platform_id = a.platform_id
  138. AND DATE_FORMAT( f.addtime, '%Y-%m-%d' ) = DATE_FORMAT( '$endtime', '%Y-%m-%d' ))) AS day_thfee,
  139. a.platform_id,
  140. c.`name`,
  141. c.id as depar_id,
  142. platform_name
  143. FROM
  144. wsm_sale a
  145. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  146. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  147. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  148. WHERE
  149. date_format( a.addtime, '%Y-%m-%d' ) = date_format ( '$endtime', '%Y-%m-%d' )
  150. GROUP BY
  151. a.platform_id,
  152. platform_name,
  153. c.NAME,
  154. c.id,
  155. `day`
  156. ) AS d ON a.NAME = d.NAME
  157. AND a.platform_id = d.platform_id
  158. ";
  159. $data = Db::query($sql);
  160. $dtae = isset($endtime) && $endtime != "" ? $endtime . " 00:00:00" : date("Y-m-d H:i:s");
  161. $temp = ["p" => "总计", 'item' => "-", "thtotal" => 0, "total" => 0, "mthfee" => 0, "wtotal" => 0, "wthfee" => 0, "dtotal" => 0, "thfee" => 0, "tips" => 0, "rate" => 0, "thrate" => 0];
  162. $tips = Db::name("depart_tips")->field("depart_id ,total_tips")->where("year=YEAR('{$dtae}') and month=month('{$dtae}')")->select();
  163. $kdata = [];
  164. foreach ($data as $key => $value) {
  165. $value['tips'] = 0;
  166. foreach ($tips as $val) {
  167. if ($value["depar_id"] == $val['depart_id']) {
  168. $value['tips'] = $val['total_tips'];
  169. }
  170. }
  171. $value['thtotal'] = $value['total'] - $value['mthfee'];
  172. $kdata[] = $value;
  173. }
  174. $list = $this->check($kdata, $temp);
  175. array_walk($list, function (&$value) {
  176. if (is_null($value['tips']) || $value['tips'] == 0) {
  177. $value['tips'] = "-";
  178. $value["rate"] = "-";
  179. $value["thrate"] = "-";
  180. } else {
  181. $value["rate"] = number_format($value['total'] / $value['tips'] * 100, 2);
  182. $value["thrate"] = number_format(($value['total'] - $value['mthfee']) / $value['tips'] * 100, 2);
  183. $value['tips'] = sprintf("%.2f", $value['tips']);
  184. }
  185. $value['total'] = sprintf("%.2f", $value['total']);
  186. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  187. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  188. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  189. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  190. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  191. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  192. });
  193. $list=array_values($list);
  194. if ($temp['tips'] == 0) {
  195. $temp['rate'] = "-";
  196. $temp['thrate'] = "-";
  197. $temp['tips'] = "-";
  198. } else {
  199. $temp['rate'] = number_format($temp['total'] / $temp['tips'] * 100, 2);
  200. $temp['thrate'] = number_format(($temp['total'] - $temp['mthfee']) / $temp['tips'] * 100, 2);;
  201. $temp['tips'] = round($temp['tips'], 2);
  202. }
  203. $temp['total'] = sprintf("%.2f", $temp['total']);
  204. $temp['mthfee'] = sprintf("%.2f", $temp['mthfee']);
  205. $temp['wtotal'] = sprintf("%.2f", $temp['wtotal']);
  206. $temp['wthfee'] = sprintf("%.2f", $temp['wthfee']);
  207. $temp['dtotal'] = sprintf("%.2f", $temp['dtotal']);
  208. $temp['thfee'] = sprintf("%.2f", $temp['thfee']);
  209. $temp['thtotal'] = sprintf("%.2f", $temp['thtotal']);
  210. $sort = array_column($list, "thrate");
  211. array_multisort($sort, SORT_ASC, $list);
  212. array_push($list, $temp);
  213. $list = array_reverse($list);
  214. return app_show(0, "获取成功", $list);
  215. }
  216. public function check($data, &$temp)
  217. {
  218. $list = [];
  219. foreach ($data as $key => $value) {
  220. isset($list[$value['depar_id']]) ? "" : $list[$value['depar_id']] = ['total' => 0, 'mthfee' => 0, 'tips' => 0, 'wtotal' => 0, 'wthfee' => 0, 'dtotal' => 0,
  221. 'child' => [], 'depart' => '', 'thfee' => 0, 'thtotal' => 0];
  222. $list[$value['depar_id']]['total'] += $value['total'];
  223. $list[$value['depar_id']]['mthfee'] += $value['mthfee'];
  224. $list[$value['depar_id']]['wtotal'] += $value['wtotal'];
  225. $list[$value['depar_id']]['wthfee'] += $value['wthfee'];
  226. $list[$value['depar_id']]['dtotal'] += $value['dtotal'];
  227. $list[$value['depar_id']]['thfee'] += $value['thfee'];
  228. $list[$value['depar_id']]['thtotal'] += $value['thtotal'];
  229. $list[$value['depar_id']]['tips'] += is_null($value['tips']) || $value['tips'] == 0 ? 0 : $value['tips'];
  230. $temp['total'] += $value['total'];
  231. $temp['mthfee'] += $value['mthfee'];
  232. $temp['wtotal'] += $value['wtotal'];
  233. $temp['wthfee'] += $value['wthfee'];
  234. $temp['thfee'] += $value['thfee'];
  235. $temp['dtotal'] += $value['dtotal'];
  236. $temp['tips'] += is_null($value['tips']) || $value['tips'] == 0 ? 0 : $value['tips'];
  237. $temp['thtotal'] += $value['thtotal'];
  238. if (is_null($value['tips']) || $value['tips'] == 0) {
  239. $value['tips'] = "-";
  240. } else {
  241. $value['tips'] = sprintf("%.2f", $value['tips']);
  242. }
  243. $value['total'] = sprintf("%.2f", $value['total']);
  244. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  245. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  246. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  247. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  248. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  249. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  250. $list[$value['depar_id']]['child'][] = $value;
  251. $list[$value['depar_id']]['depart'] = $value['depart'];
  252. }
  253. return $list;
  254. }
  255. }