GoodsData.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\OrderInfo;
  5. use app\admin\model\Refund;
  6. use app\admin\model\User;
  7. use app\common\controller\Backend;
  8. use app\common\model\Attachment;
  9. use app\common\model\SysConfig;
  10. use fast\Date;
  11. use think\Db;
  12. /**
  13. * 控制台
  14. *
  15. * @icon fa fa-dashboard
  16. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  17. */
  18. class GoodsData extends Backend
  19. {
  20. /**
  21. * 查看
  22. */
  23. public function index()
  24. {
  25. try {
  26. \think\Db::execute("SET @@sql_mode='';");
  27. } catch (\Exception $e) {
  28. }
  29. list($postTime,$postTimeArr) = $this->rangeTime('startTime');
  30. $where = [];
  31. $map = [];
  32. if ($postTime) {
  33. $time = explode(' - ', $postTime);
  34. $startTime = strtotime($time[0]);
  35. $endTime = strtotime($time[1]);
  36. $where['create_time'] = array('between', [$startTime, $endTime]);
  37. $map['createtime'] = array('between', [$startTime, $endTime]);
  38. }
  39. //某一时间段各单品订单数量占比
  40. $infoModel = new OrderInfo();
  41. $orderIds = Db("Orders")->where($where)->where('status', 'in', '5,10,20,30')->column('id');
  42. $goodsList = $infoModel
  43. ->where($where)
  44. ->where('order_id','in',$orderIds)
  45. ->where('is_return_goods',0)
  46. ->field('goods_id,goods_name, sum(amount_pay) AS amount_total')
  47. ->group('goods_id')
  48. ->select();
  49. $goods = [];
  50. $goodsSale = [];
  51. $goodsGross = [];
  52. $goodsGrossRate = [];
  53. foreach ($goodsList as $v){
  54. $v['nums'] = $infoModel
  55. ->where($where)
  56. ->where('order_id','in',$orderIds)
  57. ->where('goods_id',$v['goods_id'])
  58. ->where('is_return_goods',0)
  59. ->group('order_id')
  60. ->count();
  61. $goods[$v['goods_name']] = $v['nums'];
  62. $goodsSale[$v['goods_name']] = $v['amount_total'];
  63. $saleTotal = $infoModel
  64. ->where($where)
  65. ->where('order_id','in',$orderIds)
  66. ->where('goods_id',$v['goods_id'])
  67. ->where('is_return_goods',0)
  68. ->sum('amount');//某商品总销售额
  69. $saleTotalNum =$infoModel
  70. ->where($where)
  71. ->where('order_id','in',$orderIds) ->where('is_return_goods',0)
  72. ->where('goods_id',$v['goods_id'])->count();
  73. $saleTotal = $saleTotal*$saleTotalNum;
  74. $amountCostTotal = $infoModel
  75. ->where($where)
  76. ->where('order_id','in',$orderIds)
  77. ->where('is_return_goods',0)
  78. ->where('goods_id',$v['goods_id'])->sum('amount_cost_total');//某商品总成本
  79. $amountCostTotal = $amountCostTotal*$saleTotalNum;
  80. $gross = bcsub($saleTotal, $amountCostTotal);//某商品毛利
  81. $grossRate = 0;
  82. if ($saleTotal > 0) $grossRate = round($gross / $saleTotal, 2) * 100;//某商品毛利率
  83. $goodsGross[$v['goods_name']] = $gross;
  84. $goodsGrossRate[$v['goods_name']] = $grossRate;
  85. }
  86. $payTotalCount = Db("Orders")->where($where)->where('status', 'in', '5,10,20,30')->count();
  87. // foreach ($goodsList as $k => $v) {
  88. // $rate = 0;
  89. // if($payTotalCount > 0)$rate = round($v['nums']/$payTotalCount,2)*100;
  90. // $rate = $rate.'%';
  91. // $goods[$v['goods_name']] = $v['nums'];
  92. // $goods[$v['goods_name']] = $rate;
  93. // }
  94. //平台所有商品退货率
  95. $allRefund = Db('order_info_refund')->count();
  96. $allRefundRate = 0;
  97. if($payTotalCount > 0)$allRefundRate = round($allRefund/$payTotalCount,2)*100;
  98. $allRefundRate = $allRefundRate.'%';
  99. //单品退货率
  100. $refundWhere=$where;
  101. if(!empty($refundWhere['create_time'])){
  102. $refundWhere[Refund::getTable().'.create_time']=$refundWhere['create_time'];
  103. unset($refundWhere['create_time']);
  104. }
  105. $refundList = Refund::with('goods')
  106. ->where($refundWhere)
  107. ->field('goods_id, sum(num) AS nums')
  108. ->group('goods_id')
  109. ->select();
  110. $refundRate = [];
  111. foreach ($refundList as &$v){
  112. $orderIds = OrderInfo::where('goods_id',$v['goods_id'])->column('order_id');
  113. $orderIds = Db("Orders")->where($where)->where('status', 'in', '5,10,20,30')->where('id', 'in', $orderIds)->column('id');
  114. $goodsNum = OrderInfo::where('order_id','in',$orderIds)->where('goods_id',$v['goods_id'])->sum('num');
  115. $rate = 0;
  116. if($goodsNum > 0)$rate = round($v['nums']/$goodsNum,2)*100;
  117. $refundRate[$v['goods']['name']] = $rate;
  118. }
  119. //总销售额
  120. $sale = Db('Orders')->where($where)->where('status', 'in', '5,10,20')->sum('amount_pay');
  121. $refund = Db('OrderInfoRefund')->where($where)->where('refund_status', 20)->sum('amount');
  122. $sale = bcsub($sale, $refund);
  123. //毛利、毛利率
  124. $install = Db('Orders')->where($where)->where('status', 'in', '5,10,20')->sum('amount_install');//总安装费
  125. $orderIds = Db('Orders')->where($where)->where('status', 'in', '5,10,20')->column('id');
  126. $cost = Db('OrderInfo')->where('order_id', 'in', $orderIds)->sum('amount_cost_total');//总成本
  127. $gross_profit = bcsub($sale, $install);
  128. $gross_profit = bcsub($gross_profit, $cost);
  129. $gross_profit_margin = 0;
  130. if ($sale > 0) $gross_profit_margin = round($gross_profit / $sale, 2) * 100;//毛利率
  131. //销售金额排名
  132. $list = OrderInfo
  133. ::with(['goods'])
  134. ->where($where)
  135. ->where('is_return_goods',0)
  136. ->where('order_id','in',$orderIds)
  137. ->field('goods_id, sum(amount*num) AS total,sum(num) AS total_num')
  138. ->group('goods_id')
  139. ->order('total desc')->select();
  140. $list = $list->toArray();
  141. $saleMoney = [];
  142. $saleNum = [];
  143. foreach ($list as $v){
  144. $saleMoney[$v['goods']['name']] = $v['total'];
  145. $saleNum[$v['goods']['name']] = $v['total_num'];
  146. }
  147. #售后率
  148. $sellNum=OrderInfo::payed()->sum('num');
  149. $refundNum=Refund::filterRefund()->sum('num');
  150. $this->assign('svcPer',bcmul(100,$sellNum>0?bcdiv($refundNum,$sellNum):0));
  151. $this->view->assign([
  152. 'sale' => $sale,
  153. 'gross_profit' => $gross_profit,
  154. 'gross_profit_margin' => $gross_profit_margin,
  155. 'post_time' => $postTime,
  156. 'all_refund_rate'=>$allRefundRate
  157. ]);
  158. $this->assignconfig('goods_column', array_keys($goods));
  159. $this->assignconfig('goods_data', array_values($goods));
  160. $this->assignconfig('goods_sale_column', array_keys($goodsSale));
  161. $this->assignconfig('goods_sale_data', array_values($goodsSale));
  162. $this->assignconfig('goods_gross_column', array_keys($goodsGross));
  163. $this->assignconfig('goods_gross_data', array_values($goodsGross));
  164. $this->assignconfig('goods_gross_rate_column', array_keys($goodsGrossRate));
  165. $this->assignconfig('goods_gross_rate_data', array_values($goodsGrossRate));
  166. $this->assignconfig('refund_rate_column', array_keys($refundRate));
  167. $this->assignconfig('refund_rate_data', array_values($refundRate));
  168. $this->assignconfig('sale_money_column', array_keys($saleMoney));
  169. $this->assignconfig('sale_money_data', array_values($saleMoney));
  170. $this->assignconfig('sale_num_column', array_keys($saleNum));
  171. $this->assignconfig('sale_num_data', array_values($saleNum));
  172. return $this->view->fetch();
  173. }
  174. }