From ccdbb687a54d092e7ce26d4b502b2254504354f7 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 22 Jun 2024 10:07:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E4=BA=86WorkbenchLog?= =?UTF-8?q?ic=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BA=86=E6=9F=A5=E8=AF=A2=E6=9D=A1=E4=BB=B6=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86=E9=83=A8=E5=88=86=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/WorkbenchController.php | 2 +- .../logic/store_order/StoreOrderLogic.php | 4 +- app/store/logic/WorkbenchLogic.php | 194 +++++++++--------- 3 files changed, 96 insertions(+), 104 deletions(-) diff --git a/app/admin/controller/WorkbenchController.php b/app/admin/controller/WorkbenchController.php index 395202d87..ab6a65cec 100644 --- a/app/admin/controller/WorkbenchController.php +++ b/app/admin/controller/WorkbenchController.php @@ -49,7 +49,7 @@ class WorkbenchController extends BaseAdminController { $params = $this->request->get(); if(!isset($params['store_id']) ||$params['store_id']==''){ - $params['store_id'] =1; + $params['store_id'] =0; } if(!isset($params['start_time']) ||$params['start_time']==''){ $time=explode('-', $this->getDay('')); diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index b892b8083..3cb9a5ec9 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -306,9 +306,9 @@ class StoreOrderLogic extends BaseLogic * @param $extra * @return float|\think\db\Query */ - public function storeOrderSumByDate($storeId, $start, $end, $extra = [], $field = 'pay_price') + public function storeOrderSumByDate($start, $end, $extra = [], $field = 'pay_price') { - return StoreOrder::where('store_id', $storeId)->where('paid', 1)->where($extra)->whereBetweenTime('pay_time', $start, $end)->sum($field); + return StoreOrder::where($extra)->whereBetweenTime('pay_time', $start, $end)->sum($field); } /** diff --git a/app/store/logic/WorkbenchLogic.php b/app/store/logic/WorkbenchLogic.php index da4c06140..f65e6eca7 100644 --- a/app/store/logic/WorkbenchLogic.php +++ b/app/store/logic/WorkbenchLogic.php @@ -52,30 +52,33 @@ class WorkbenchLogic extends BaseLogic $endTime = $params['end_time']; $endTime = date('Y-m-d', strtotime($endTime) + 86400); $dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime)); - + $where = ['paid' => 1]; + $cashFinanceWhere = []; + $storeFinanceWhere = []; + if ($params['store_id'] != 0) { + $where['store_id'] = $params['store_id']; + $cashFinanceWhere = ['store_id' => $params['store_id']]; + $storeFinanceWhere = ['store_id' => $params['store_id'],'financial_type'=>11,'financial_pm'=>0]; + } $orderLogic = new StoreOrderLogic(); //订单总金额 - $data['order_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime); + $data['order_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, $where); //余额支付总金额 - $data['balance_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['pay_type' => PayEnum::BALANCE_PAY]); + $data['balance_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['pay_type' => PayEnum::BALANCE_PAY])); //线下收银总金额 - $data['cashier_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 3]); + $data['cashier_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['shipping_type' => 3])); //现金收银总金额 - $data['cash_amount'] = StoreCashFinanceFlow::where('store_id', $params['store_id'])->whereBetweenTime('create_time', $startTime, $endTime)->sum('cash_price'); + $data['cash_amount'] = StoreCashFinanceFlow::where($cashFinanceWhere)->whereBetweenTime('create_time', $startTime, $endTime)->sum('cash_price'); //核销订单金额 - $data['verify_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 2]); + $data['verify_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['shipping_type' => 2])); //门店收益金额 - $data['income_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, [], 'profit'); + $data['income_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, [], 'profit'); //门店收款金额 - $data['receipt_amount'] = UserRecharge::where([ - 'store_id'=>$params['store_id'], - 'paid'=>YesNoEnum::YES - ])->sum('price'); + $data['receipt_amount'] = UserRecharge::where($where)->sum('price'); - $data['deposit_amount'] = StoreFinanceFlow::where('store_id', $params['store_id'])->where('financial_type',11)->where('financial_pm',0)->whereBetweenTime('create_time', $startTime, $endTime)->sum('number'); + $data['deposit_amount'] = StoreFinanceFlow::where($storeFinanceWhere)->whereBetweenTime('create_time', $startTime, $endTime)->sum('number'); //门店成交用户数 - $data['user_number'] = StoreOrder::where('store_id', $params['store_id']) - ->where('paid', 1) + $data['user_number'] = StoreOrder::where($where) ->whereBetweenTime('pay_time', $startTime, $endTime) ->group('uid') ->count(); @@ -96,8 +99,7 @@ class WorkbenchLogic extends BaseLogic $i++; } $field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price'; - } - else { + } else { $group = 'MONTH(pay_time)'; $i = 0; $month = 0; @@ -117,15 +119,13 @@ class WorkbenchLogic extends BaseLogic $field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price'; } $orderList = StoreOrder::field($field) - ->where('store_id', $params['store_id']) - ->where('paid', 1) + ->where($where) ->whereBetweenTime('pay_time', $startTime, $endTime) ->group($group) ->select() ->toArray(); $userList = StoreOrder::field($field . ',count(uid) as user_num') - ->where('store_id', $params['store_id']) - ->where('paid', 1) + ->where($where) ->whereBetweenTime('pay_time', $startTime, $endTime) ->group($group . ',uid') ->select() @@ -155,8 +155,7 @@ class WorkbenchLogic extends BaseLogic 'user_number' => array_values($userListTmp) ] ]; - $data['order_list'] = StoreOrder::with('user')->where('store_id', $params['store_id']) - ->where('paid', 1) + $data['order_list'] = StoreOrder::with('user')->where($where) ->whereBetweenTime('pay_time', $startTime, $endTime) ->order('pay_time', 'desc') ->limit(10) @@ -554,7 +553,7 @@ class WorkbenchLogic extends BaseLogic { //当日营业额的统计 - $today = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]); + $today = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']]); $turnover_today = $today ->whereDay('create_time') ->sum('pay_price'); @@ -568,35 +567,34 @@ class WorkbenchLogic extends BaseLogic ->sum('cost'); //当日加到保证金的 - $deposit = StoreFinanceFlow::where(['store_id'=>$params['store_id'],'status'=>YesNoEnum::YES]) - ->where('financial_type',OrderEnum::ORDER_MARGIN); - $deposit_today =$deposit - ->whereDay('create_time') - ->sum('number'); - //当日的现金收银 - $cash_today = StoreCashFinanceFlow::where('store_id',$params['store_id']) + $deposit = StoreFinanceFlow::where(['store_id' => $params['store_id'], 'status' => YesNoEnum::YES]) + ->where('financial_type', OrderEnum::ORDER_MARGIN); + $deposit_today = $deposit ->whereDay('create_time') - ->where('status',YesNoEnum::YES) + ->sum('number'); + //当日的现金收银 + $cash_today = StoreCashFinanceFlow::where('store_id', $params['store_id']) + ->whereDay('create_time') + ->where('status', YesNoEnum::YES) ->sum('receivable'); //总的营业额的统计 总的利润的统计 总的成本合集的统计 总的加到保证金的 - $all = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]); + $all = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']]); - $deposit_all = StoreFinanceFlow::where(['store_id'=>$params['store_id'],'status'=>YesNoEnum::YES]) + $deposit_all = StoreFinanceFlow::where(['store_id' => $params['store_id'], 'status' => YesNoEnum::YES]) ->sum('number'); - $cash_all = StoreCashFinanceFlow::where('store_id',$params['store_id']) - ->where('status',YesNoEnum::YES) + $cash_all = StoreCashFinanceFlow::where('store_id', $params['store_id']) + ->where('status', YesNoEnum::YES) ->sum('receivable'); - if(isset($params['month']) && $params['month']){ - $all = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]) - ->whereMonth('create_time', $params['month']) - ; - $deposit_all = SystemStore::where('id',$params['store_id']) + if (isset($params['month']) && $params['month']) { + $all = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']]) + ->whereMonth('create_time', $params['month']); + $deposit_all = SystemStore::where('id', $params['store_id']) ->whereMonth('create_time', $params['month']) ->value('paid_deposit'); - $cash_all = StoreCashFinanceFlow::where('store_id',$params['store_id']) - ->where('status',YesNoEnum::YES) + $cash_all = StoreCashFinanceFlow::where('store_id', $params['store_id']) + ->where('status', YesNoEnum::YES) ->whereMonth('create_time', $params['month']) ->sum('receivable'); } @@ -605,32 +603,31 @@ class WorkbenchLogic extends BaseLogic $profit_all = $all ->sum('profit'); //消耗余额 V2.0 - $cost_all = CapitalFlow:: - where(['category'=>'user_order_balance_pay','store_id'=>$params['store_id']]) + $cost_all = CapitalFlow::where(['category' => 'user_order_balance_pay', 'store_id' => $params['store_id']]) ->sum('amount'); $time = self::getLastSevenDays(); $newArr = []; - foreach ($time as $value){ - $data = self::dealSearch($params['store_id'],$value); + foreach ($time as $value) { + $data = self::dealSearch($params['store_id'], $value); $newArr[$value] = $data; } return [ - 'today'=>[ - 'turnover_today'=>$turnover_today, - 'profit_today'=>$profit_today, - 'cost_today'=>$cost_today, - 'deposit_today'=>$deposit_today, - 'cash_today'=>$cash_today, + 'today' => [ + 'turnover_today' => $turnover_today, + 'profit_today' => $profit_today, + 'cost_today' => $cost_today, + 'deposit_today' => $deposit_today, + 'cash_today' => $cash_today, ], - 'all'=>[ - 'turnover_all'=>$turnover_all, - 'profit_all'=>$profit_all, - 'cost_all'=>$cost_all, - 'deposit_all'=>$deposit_all, - 'cash_all'=>$cash_all, + 'all' => [ + 'turnover_all' => $turnover_all, + 'profit_all' => $profit_all, + 'cost_all' => $cost_all, + 'deposit_all' => $deposit_all, + 'cash_all' => $cash_all, ], - 'time'=>$newArr + 'time' => $newArr ]; @@ -665,7 +662,6 @@ class WorkbenchLogic extends BaseLogic ->where('status',YesNoEnum::YES); $cash_count = $cash->count(); $cash_receipt = $cash->sum('receipts');*/ - } @@ -684,11 +680,11 @@ class WorkbenchLogic extends BaseLogic } - public static function dealSearch($store_id,$startTime) + public static function dealSearch($store_id, $startTime) { $endTime = date('Y-m-d', strtotime($startTime) + 86400); //当日营业额的统计 当日利润的统计 当日成本合集的统计 当日加到保证金的 当日的现金收银 - $today = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$store_id]); + $today = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $store_id]); $turnover_today = $today ->whereBetweenTime('create_time', $startTime, $endTime) ->sum('pay_price'); @@ -699,33 +695,32 @@ class WorkbenchLogic extends BaseLogic ->whereBetweenTime('create_time', $startTime, $endTime) ->sum('cost'); - $deposit = StoreFinanceFlow::where(['store_id'=>$store_id]) - ->where('financial_type',OrderEnum::ORDER_MARGIN); - $deposit_today =$deposit + $deposit = StoreFinanceFlow::where(['store_id' => $store_id]) + ->where('financial_type', OrderEnum::ORDER_MARGIN); + $deposit_today = $deposit ->whereBetweenTime('create_time', $startTime, $endTime) ->sum('number'); - $cash_today = StoreCashFinanceFlow::where('store_id',$store_id) + $cash_today = StoreCashFinanceFlow::where('store_id', $store_id) ->whereBetweenTime('create_time', $startTime, $endTime) - ->where('status',YesNoEnum::YES) + ->where('status', YesNoEnum::YES) ->sum('receipts'); return [ - 'turnover_today'=>$turnover_today, - 'profit_today'=>$profit_today, - 'cost_today'=>$cost_today, - 'deposit_today'=>$deposit_today, - 'cash_today'=>$cash_today, + 'turnover_today' => $turnover_today, + 'profit_today' => $profit_today, + 'cost_today' => $cost_today, + 'deposit_today' => $deposit_today, + 'cash_today' => $cash_today, ]; - } public static function rechargeData($params) { $data['receipt_amount'] = UserRecharge::where([ - 'store_id'=>$params['store_id'], - 'paid'=>YesNoEnum::YES + 'store_id' => $params['store_id'], + 'paid' => YesNoEnum::YES ])->sum('price'); - return $data??[]; + return $data ?? []; } @@ -750,32 +745,31 @@ class WorkbenchLogic extends BaseLogic "series" => [ [ "name" => "商品浏览量", - "data" => self::store_visit_count($dates,$store_id), + "data" => self::store_visit_count($dates, $store_id), "type" => "line", "smooth" => "true", "yAxisIndex" => 1 ], [ "name" => "商品访客量", - "data" => self::store_visit_user($dates,$store_id), + "data" => self::store_visit_user($dates, $store_id), "type" => "line", "smooth" => "true", "yAxisIndex" => 1 ], [ "name" => "支付金额", - "data" => self::payPrice($dates,$store_id), + "data" => self::payPrice($dates, $store_id), "type" => "bar" ], [ "name" => "退款金额", - "data" => self::refundPrice($dates,$store_id), + "data" => self::refundPrice($dates, $store_id), "type" => "bar" ] ] ]; return $data; - } @@ -783,11 +777,11 @@ class WorkbenchLogic extends BaseLogic /** * 商品浏览量 */ - public static function store_visit_count($dates,$store_id) + public static function store_visit_count($dates, $store_id) { $data = []; foreach ($dates as $date) { - $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_count_' . $date, 300)->sum('count'); + $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id', $store_id)->cache('store_visit_count_' . $date, 300)->sum('count'); } return $data; } @@ -796,11 +790,11 @@ class WorkbenchLogic extends BaseLogic /** * 商品浏览量 */ - public static function store_visit_user($dates,$store_id) + public static function store_visit_user($dates, $store_id) { $data = []; foreach ($dates as $date) { - $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_user_' . $date, 300)->count('uid'); + $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id', $store_id)->cache('store_visit_user_' . $date, 300)->count('uid'); } return $data; } @@ -809,11 +803,11 @@ class WorkbenchLogic extends BaseLogic /** * 支付金额 */ - public static function payPrice($dates,$store_id) + public static function payPrice($dates, $store_id) { $data = []; foreach ($dates as $date) { - $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price'); + $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id', $store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price'); } return $data; } @@ -822,11 +816,11 @@ class WorkbenchLogic extends BaseLogic /** * 退款金额 */ - public static function refundPrice($dates,$store_id) + public static function refundPrice($dates, $store_id) { $data = []; foreach ($dates as $date) { - $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price'); + $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id', $store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price'); } return $data; } @@ -836,19 +830,19 @@ class WorkbenchLogic extends BaseLogic /** * 获取商品排名数据 */ - public static function get_product_ranking($where,$store_id) + public static function get_product_ranking($where, $store_id) { - return self::product_ranking($where,$store_id); + return self::product_ranking($where, $store_id); } - public static function product_ranking($where,$store_id) + public static function product_ranking($where, $store_id) { $time = explode('-', $where['create_time']); $time = [strtotime($time[0]), strtotime($time[1])]; - $list = StoreProductLog::whereBetweenTime('create_time',$time[0],$time[1]) - ->where('store_id',$store_id) + $list = StoreProductLog::whereBetweenTime('create_time', $time[0], $time[1]) + ->where('store_id', $store_id) ->field([ 'store_id', 'product_id', @@ -863,21 +857,19 @@ class WorkbenchLogic extends BaseLogic 'SUM(collect_num) as collect', 'ROUND((COUNT(distinct(pay_uid))-1)/COUNT(distinct(uid)),2) as changes', 'COUNT(distinct(pay_uid))-1 as repeats' - ])->group('product_id')->order('pay' , ' desc')->limit(20)->select()->toArray(); + ])->group('product_id')->order('pay', ' desc')->limit(20)->select()->toArray(); foreach ($list as $key => &$item) { - $find=StoreProduct::where('id',$item['product_id'])->field('store_name,image')->find(); - $item['store_name']=$find['store_name']; - $item['image']=$find['image']; + $find = StoreProduct::where('id', $item['product_id'])->field('store_name,image')->find(); + $item['store_name'] = $find['store_name']; + $item['image'] = $find['image']; if ($item['profit'] == null) $item['profit'] = 0; if ($item['changes'] == null) $item['changes'] = 0; if ($item['repeats'] == null) { $item['repeats'] = 0; } else { - $item['repeats'] = bcdiv(count(StoreProductLog::where($where)->where('type', 'pay')->where('product_id', $item['product_id'])->where('store_id',$store_id)->field('count(pay_uid) as p')->group('pay_uid')->having('p>1')->select()), $item['repeats'], 2); + $item['repeats'] = bcdiv(count(StoreProductLog::where($where)->where('type', 'pay')->where('product_id', $item['product_id'])->where('store_id', $store_id)->field('count(pay_uid) as p')->group('pay_uid')->having('p>1')->select()), $item['repeats'], 2); } } return array_merge($list); } - - }