Stat.php 13 KB

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