diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index 20a2a92..ca52b4d 100644 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -16,12 +16,10 @@ use think\facade\Db; use app\common\model\Shang; use app\common\model\ProfitExpenses; use app\common\model\ProfitRvenue; + class Statistics extends Base { - /** - * 利润统计 - */ public function profit(Request $request) { $title = trim(input('get.title')); @@ -29,178 +27,170 @@ class Statistics extends Base $status = trim(input('get.status')); $type = trim(input('get.type')); $addtime = trim(input('get.addtime')); - $whe3 = array(); - $whe4 = array(); - $whe = array(); - if ($title) { - $whe[] = ['title', 'like', '%' . $title . '%']; - } - if ($id) { - $whe[] = ['id', '=', $id]; - } - if ($status) { + // 构建查询条件 + $whe = []; + if ($title) + $whe[] = ['title', 'like', '%' . $title . '%']; + if ($id) + $whe[] = ['id', '=', $id]; + if ($status) $whe[] = ['status', '=', $status]; - } - if ($type) { + if ($type) $whe[] = ['type', '=', $type]; + // 解析时间范围 + $whe3 = []; + $whe4 = []; + if ($addtime) { + [$start_time, $end_time] = $this->parseTimeRange($addtime); + $whe3[] = ['addtime', 'BETWEEN', [$start_time, $end_time]]; + $whe4[] = ['addtime', 'BETWEEN', [$start_time, $end_time]]; } + // 获取商品ID列表 + $goodList = GoodsModel::where($whe)->column('id'); + if (empty($goodList)) { + return View::fetch("Statistics/profit", ['list' => [], 'count' => 0, 'page' => '']); + } + + // 获取测试用户ID列表 + $userList = User::where('istest', 1)->column('id'); + + // 订单筛选条件 + $whe3[] = ['user_id', 'not in', $userList]; + $whe3[] = ['status', '=', 1]; + $whe3[] = ['goods_id', 'in', $goodList]; + + $whe4[] = ['user_id', 'not in', $userList]; + $whe4[] = ['goods_id', 'in', $goodList]; + + // 统计订单数据 + $orderTotals = OrderModel::where($whe3) + ->field('SUM(order_total) as order_total_all, + SUM(order_zhe_total) as order_zhe_total_all, + SUM(price) as price_all, + SUM(use_money) as use_money_all') + ->find(); + + $goodslistMoneyAll = OrderList::where($whe4)->sum('goodslist_money'); + + // 获取商品列表 + $data = GoodsModel::getList($whe, "*", "id desc", 20); + + // 计算每个商品的订单详情 + foreach ($data['list'] as &$value) { + $value = array_merge($value, $this->calculateOrderDetails($value['id'], $userList, $addtime)); + } + + // 计算利润 + $sum_shijilirun = round($orderTotals->order_zhe_total_all - $goodslistMoneyAll, 2); + $sum_dingdanlirun = round($orderTotals->order_total_all - $goodslistMoneyAll, 2); + + // 传递数据给视图 + View::assign([ + + 'sum_dingdan' => $orderTotals->order_total_all, + 'sum_shiji' => $orderTotals->order_zhe_total_all, + 'sum_chuhuo' => $goodslistMoneyAll, + 'price_all' => $orderTotals->price_all, + 'sum_shijilirun' => $sum_shijilirun, + 'sum_dingdanlirun' => $sum_dingdanlirun, + 'use_money_all' => $orderTotals->use_money_all, + 'order_total_all' => $orderTotals->order_total_all, + 'list' => $data['list'], + 'count' => $data['count'], + 'page' => $data['page'], + ]); + + return View::fetch("Statistics/profit"); + } + + /** + * 解析时间范围 + */ + private function parseTimeRange($addtime) + { + $time = explode(' - ', $addtime); + $start_time = strtotime($time[0]); + $end_time = strtotime($time[1]) - 1; + if ($start_time > $end_time) { + throw new \Exception('开始时间不能大于结束时间'); + } + return [$start_time, $end_time]; + } + + /** + * 计算商品的订单详情 + */ + private function calculateOrderDetails($goodsId, $userList, $addtime) + { + $whe1 = [['goods_id', '=', $goodsId], ['status', '=', 1], ['user_id', 'not in', $userList]]; + $whe2 = [['user_id', 'not in', $userList], ['goods_id', '=', $goodsId]]; + if ($addtime) { - $time = explode(' - ', $addtime); - $start_time = strtotime($time[0]); - $end_time = strtotime($time[1]) - 1; - if ($start_time > $end_time) { - $this->err('开始时间不能大于结束时间'); - } - $whe3[] = ['addtime', 'BETWEEN', array($start_time, $end_time)]; - $whe4[] = ['addtime', 'BETWEEN', array($start_time, $end_time)]; + [$start_time, $end_time] = $this->parseTimeRange($addtime); + $whe1[] = ['addtime', 'BETWEEN', [$start_time, $end_time]]; + $whe2[] = ['addtime', 'BETWEEN', [$start_time, $end_time]]; } - $goodList = GoodsModel::where($whe)->field('id')->select(); - $field = "*"; - $order = "id desc"; - $data = GoodsModel::getList($whe, $field, $order, 20); - $userList = User::where('istest', '=', 1)->field('id')->select(); - $userArray = array_column($userList->toArray(), 'id'); - $goodArray = array_column($goodList->toArray(), 'id'); - $whe3[] = ['user_id', 'not in', $userArray]; - $whe3[] = ['status', '=', 1]; - $whe3[] = ['goods_id', 'in', $goodArray]; - $whe4[] = ['user_id', 'not in', $userArray]; - $whe4[] = ['goods_id', 'in', $goodArray]; + // 订单统计 + $orderStats = OrderModel::where($whe1) + ->field('COUNT(*) as count, + SUM(order_total) as order_total, + SUM(price) as count_price, + SUM(use_money) as count_yue, + SUM(use_integral) / 100 as use_integral, + SUM(use_score) as use_score, + SUM(use_coupon) as use_coupon, + SUM(order_zhe_total) as order_zhe_total') + ->find(); - $order_total_all = OrderModel::where($whe3)->sum('order_total'); - $order_zhe_total_all = OrderModel::where($whe3)->sum('order_zhe_total'); - $goodslist_money_all = OrderList::where($whe4)->sum('goodslist_money'); - $price_all = OrderModel::where($whe3)->sum('price'); - $use_money_all = OrderModel::where($whe3)->sum('use_money'); - // $value['lirulv'] = round(($order_zhe_total - $goodslist_price1) / $goodslist_price1 * 100, 2); - - View::assign('sum_dingdan', $order_total_all); - View::assign('sum_shiji', $order_zhe_total_all); - View::assign('sum_chuhuo', $goodslist_money_all); - View::assign('price_all', $price_all); - View::assign('sum_shijilirun', round(($order_zhe_total_all - $goodslist_money_all), 2)); - View::assign('sum_dingdanlirun', round(($order_total_all - $goodslist_money_all), 2)); - View::assign('use_money_all', $use_money_all); - View::assign('order_total_all', $order_total_all); - //订单收入:实际收入:出货价值:实际利润率:订单利润率: - $sum_dingdan = 0; - $sum_shiji = 0; - $sum_chuhuo = 0; - $sum_shijilirun = 0; - $sum_dingdanlirun = 0; - foreach ($data['list'] as &$value) { - $whe1 = array(); - $whe2 = array(); - $whe1[] = ['goods_id', '=', $value['id']]; - $whe1[] = ['status', '=', 1]; - $whe1[] = ['user_id', 'not in', $userArray]; - $whe2[] = ['user_id', 'not in', $userArray]; - if ($addtime) { - $time = explode(' - ', $addtime); - $start_time = strtotime($time[0]); - $end_time = strtotime($time[1]) - 1; - if ($start_time > $end_time) { - $this->err('开始时间不能大于结束时间'); - } - $whe1[] = ['addtime', 'BETWEEN', array($start_time, $end_time)]; - $whe2[] = ['addtime', 'BETWEEN', array($start_time, $end_time)]; - } - - $list1 = OrderModel::where($whe1)->select(); - $count = count($list1); - // $price = $list1 -> count('price'); - $numbers = $list1->toArray(); - $count_price = array_sum(array_map(function ($price) { - return $price['price']; //支付金额 2 - }, $numbers)); - - $count_yue = array_sum(array_map(function ($price) { - return $price['use_money']; //用户余额 2 - }, $numbers)); - - $use_integral = array_sum(array_map(function ($price) { - return $price['use_integral']; //潮币抵扣 2 - }, $numbers)); - $use_score = array_sum(array_map(function ($price) { - return $price['use_score']; //积分抵扣 2 - }, $numbers)); - $use_coupon = array_sum(array_map(function ($price) { - return $price['use_coupon']; //优惠卷抵扣 2 - }, $numbers)); - $order_zhe_total = array_sum(array_map(function ($price) { - return $price['order_zhe_total']; //折扣后总金额 2 - }, $numbers)); - $order_total = array_sum(array_map(function ($price) { - return $price['order_total']; //实际总金额 2 - }, $numbers)); - - if ($use_integral > 0) { - $use_integral = $use_integral / 100; - } - // 订单价值 - - $whe2[] = ['goods_id', '=', $value['id']]; - - $orderList = OrderList::where($whe2)->select(); - $orderArray = $orderList->toArray(); - - $count_OrderList = count($orderList); - $goodslist_price = 0; - if ($count_OrderList > 0) { - $goodslist_price = array_sum(array_map(function ($goodslist) { - return $goodslist['goodslist_money']; //实际总金额 2 - }, $orderArray)); - } - $value['order_count'] = $count; - $value['count_price'] = $count_price; - $value['order_total'] = $order_total; - - $value['count_yue'] = $count_yue; - $value['count_use_integral'] = $use_integral; - $value['count_use_score'] = $use_score; - $value['order_zhe_total'] = $order_zhe_total; - $value['count_use_coupon'] = $use_coupon; - $value['count_heji'] = $count_price + $count_yue + $use_integral + $use_score + $use_coupon; - $value['count_heji_yanzheng'] = $count_OrderList * $value['price']; - - $value['count_OrderList'] = $count_OrderList; - $goodslist_price1 = round($goodslist_price, 2); - $value['goodslist_price'] = $goodslist_price1; - $value['lirun'] = round($order_zhe_total - $goodslist_price1, 2); - $count_heji_yanzheng = $count_OrderList * $value['price']; - $value['liruns'] = round($count_heji_yanzheng - $goodslist_price1, 2); - if ($goodslist_price1 == 0) { - $value['lirulv'] = 0; - $value['lirulvs'] = 0; - } else { - $value['lirulv'] = round(($order_zhe_total - $goodslist_price1) / $goodslist_price1 * 100, 2); - - $value['lirulvs'] = round(($count_heji_yanzheng - $goodslist_price1) / $goodslist_price1 * 100, 2); - } - - $sum_dingdan += $count_OrderList * $value['price']; - $sum_shiji += $order_zhe_total; - $sum_chuhuo += $goodslist_price1; - $sum_shijilirun += $value['lirun']; - $sum_dingdanlirun += $value['liruns']; + if (!$orderStats) { + $orderStats = (object) [ + 'count' => 0, + 'order_total' => 0, + 'count_price' => 0, + 'count_yue' => 0, + 'use_integral' => 0, + 'use_score' => 0, + 'use_coupon' => 0, + 'order_zhe_total' => 0 + ]; } - View::assign('list', $data['list']); - View::assign('count', $data['count']); - View::assign('page', $data['page']); - - // View::assign('sum_dingdan', $sum_dingdan); - // View::assign('sum_shiji', $sum_shiji); - // View::assign('sum_chuhuo', $sum_chuhuo); - // View::assign('sum_shijilirun', $sum_shijilirun); - // View::assign('sum_dingdanlirun', $sum_dingdanlirun); - return View::fetch("Statistics/profit"); + $goodslistMoney = OrderList::where($whe2)->sum('goodslist_money'); + // 计算利润 + $lirun = round($orderStats->order_zhe_total - $goodslistMoney, 2); + $liruns = round(($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon) - $goodslistMoney, 2); + + return [ + 'order_count' => round($orderStats->count, 2), + 'count_price' =>round($orderStats->count_price, 2), + 'order_total' =>round( $orderStats->order_total, 2), + 'count_yue' => round($orderStats->count_yue, 2), + 'count_use_integral' => round($orderStats->use_integral, 2), + 'count_use_score' => round($orderStats->use_score, 2), + 'count_use_coupon' => round($orderStats->use_coupon, 2), + 'count_heji' => round($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon, 2), + 'order_zhe_total'=>round($orderStats->order_zhe_total, 2), + // 订单列表数量 + 'count_OrderList' => OrderList::where($whe2)->count(), // ✅ 添加这一行 + + // 订单总价验证 + // 'count_heji_yanzheng' => $count_OrderList * $value['price'], // ✅ 使用 count_OrderList + + 'goodslist_price' => round($goodslistMoney, 2), + 'lirun' => $lirun, + 'liruns' => $liruns, + 'lirulv' => $goodslistMoney == 0 ? 0 : round(($lirun / $goodslistMoney) * 100, 2), + 'lirulvs' => $goodslistMoney == 0 ? 0 : round(($liruns / $goodslistMoney) * 100, 2), + ]; + + } + /** * 数据统计 * @param \think\Request $request diff --git a/app/admin/controller/User.php b/app/admin/controller/User.php index 23dd288..f8bc3cd 100755 --- a/app/admin/controller/User.php +++ b/app/admin/controller/User.php @@ -658,7 +658,7 @@ class User extends Base if ($user_id) { $user_id = $user_id - 1260; $whe[] = ['pid', '=', $user_id]; - } + } //select * from ( SELECT pid,count(1) n FROM xinglanmh_shequt_test.`user` where pid>0 group by pid ) t where n>1 order by n desc LIMIT 10 $list = UserModel::where($whe)-> where('pid', '>', 0) @@ -680,8 +680,11 @@ class User extends Base $user_info = UserModel::field('nickname,headimg,mobile')->where('id', '=', $pid)->find(); if ($user_info != null) { - $user_list = UserModel::field('id,nickname,addtime')->where($whe1)->where('pid', '=', $pid)->order('addtime desc')->select(); + $user_list = UserModel::field('id,nickname,addtime,mobile')->where($whe1)->where('pid', '=', $pid)->order('addtime desc')->select(); $u_index = 1; + $u_price = 0; + $u_order = 0; + $u_mobile_count = 0; foreach ($user_list as $user_item) { $user_item['adddate'] = date('Y-m-d H:i:s', $user_item['addtime']); $user_item['index'] = $u_index; @@ -691,6 +694,11 @@ class User extends Base $user_item['user_item_order'] = $user_item_order; $user_item['user_item_price'] = $user_item_price; $u_index++; + $u_price += $user_item_price; + $u_order += $user_item_order; + if ($user_item['mobile'] && $user_item['mobile'] != "") { + $u_mobile_count++; + } } $data[] = [ 'index' => $index, @@ -698,7 +706,10 @@ class User extends Base 'invitenumber' => $item['n'], 'nickname' => $user_info['nickname'], 'headimg' => imageUrl($user_info['headimg']), - 'info' => $user_list + 'info' => $user_list, + 'sum_order' => $u_order, + 'sum_price' => $u_price, + 'count_mobile' => $u_mobile_count, ]; $index++; } diff --git a/app/admin/view/Statistics/profit.html b/app/admin/view/Statistics/profit.html index fd64c9a..480822c 100755 --- a/app/admin/view/Statistics/profit.html +++ b/app/admin/view/Statistics/profit.html @@ -161,7 +161,7 @@ 优惠卷抵扣:{$vo['count_use_coupon']} - 订单收入:{$vo['count_heji_yanzheng']}
+ 订单收入:{$vo['order_total']}
实际收入:{$vo['order_zhe_total']}(去除折扣)
收入核算1:{$vo['order_total']}(订单数量*价格)
收入核算2:{$vo['count_heji']}(微信+余额+各种抵扣)
diff --git a/app/admin/view/User/user_invite.html b/app/admin/view/User/user_invite.html index d5f779f..25fea25 100644 --- a/app/admin/view/User/user_invite.html +++ b/app/admin/view/User/user_invite.html @@ -39,7 +39,12 @@ 用户昵称/id 用户头像 邀请人数 + + 邀请信息 + 绑定手机数 + 消费人数 + 消费总金额 操作 @@ -52,6 +57,7 @@ {$vo['invitenumber']}人 +
@@ -69,6 +75,9 @@
+ {$vo['count_mobile']}人 + {$vo['sum_order']}人 + {$vo['sum_price']}人 @@ -133,6 +142,19 @@ /* width: 157px; */ height: 35px !important; } + .laytable-cell-1-0-6 { + /* width: 157px; */ + height: 35px !important; + } + .laytable-cell-1-0-7 { + /* width: 157px; */ + height: 35px !important; + } + .laytable-cell-1-0-8 { + /* width: 157px; */ + height: 35px !important; + } +