Browse Source

财务统计

gorden 9 months ago
parent
commit
b3b7f69c45

+ 77 - 8
app/admin/controller/finance/GoodsSalesController.php

@@ -15,6 +15,11 @@ class GoodsSalesController
         $page = $request->get('page', 1);
         $pageSize = $request->get('pageSize', 20);
         $days = $request->get('days', []);
+        $classify = $request->get('classify','');
+        $categoryId = $request->get('category_id','');
+        $goodsName = $request->get('goods_name','');
+        $premisesId = $request->get('premises_id','');
+        $payType = $request->get('pay_type','');
         if (!empty($days)) {
             $days[0] = strtotime($days[0]);
             $days[1] = strtotime($days[1]);
@@ -27,12 +32,37 @@ class GoodsSalesController
         } else {
             $month = date('Ym');
         }
+        
+        $categorySearchIds = '';
+        if (!empty($categoryId)){
+            $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
+            $categorySearchPath .= '#'. $categoryId.'#';
+            
+            $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
+            $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
+            $categorySearchIds = implode(',',$categorySearchIds);
+        }
 
         $recordStart = ($page - 1) * 20;
 
-        $where = "";
+        $where = "where 1";
         if (!empty($days)) {
-            $where .= " where di.data_income_addtimes between {$days[0]} and {$days[1]}";
+            $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
+        }
+        if (!empty($classify)){
+            $where .= " and di.data_income_classify = '{$classify}'";
+        }
+        if (!empty($categorySearchIds)){
+            $where .= " and g.join_goods_category_id in({$categorySearchIds})";
+        }
+        if (!empty($goodsName)){
+            $where .= " and g.goods_name like '%{$goodsName}%' ";
+        }
+        if (!empty($premisesId)){
+            $where .= " and di.join_data_income_dept_id = {$premisesId}";
+        }
+        if (!empty($payType)){
+            $where .= " and di.data_income_pay_type = '{$payType}'";
         }
 
         $totalSql = "
@@ -40,7 +70,8 @@ class GoodsSalesController
                 select 
                     data_income_classify 
                 from app_data_income_{$month} as di
-                {$where}
+                    left join app_goods as g on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id') = g.goods_id 
+                    {$where}
                 group by JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_id'), JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_sku_id'),data_income_classify
             ) COUNT
         ";
@@ -52,7 +83,7 @@ class GoodsSalesController
             from app_data_income_{$month} as di 
                 left join app_goods as g on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id') = g.goods_id 
                 left join app_goods_sku as gs on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_sku_id') = gs.goods_sku_id 
-            {$where} 
+                {$where} 
             group by g.goods_id,gs.goods_sku_id,di.data_income_classify 
             order by addtimes desc 
             limit " . $recordStart . ',' . $pageSize;
@@ -87,6 +118,11 @@ class GoodsSalesController
     public function statistics(Request $request)
     {
         $days = $request->get('days', []);
+        $classify = $request->get('classify','');
+        $categoryId = $request->get('category_id','');
+        $goodsName = $request->get('goods_name','');
+        $premisesId = $request->get('premises_id','');
+        $payType = $request->get('pay_type','');
         if (!empty($days)) {
             $days[0] = strtotime($days[0]);
             $days[1] = strtotime($days[1]);
@@ -95,25 +131,58 @@ class GoodsSalesController
             }
 
             $month = date('Ym', $days[0]);
+            $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
         } else {
             $month = date('Ym');
         }
+        
+        $categorySearchIds = '';
+        if (!empty($categoryId)){
+            $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
+            $categorySearchPath .= '#'. $categoryId.'#';
+            
+            $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
+            $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
+            $categorySearchIds = implode(',',$categorySearchIds);
+        }
+
+        $where = "where 1";
+        if (!empty($days)) {
+            $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
+        }
+        if (!empty($classify)){
+            $where .= " and di.data_income_classify = '{$classify}'";
+        }
+        if (!empty($categorySearchIds)){
+            $where .= " and g.join_goods_category_id in({$categorySearchIds})";
+        }
+        if (!empty($goodsName)){
+            $where .= " and g.goods_name like '%{$goodsName}%' ";
+        }
+        if (!empty($premisesId)){
+            $where .= " and di.join_data_income_dept_id = {$premisesId}";
+        }
+        if (!empty($payType)){
+            $where .= " and di.data_income_pay_type = '{$payType}'";
+        }
 
         $statisticsSql = "
             select 
                 SUM(JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(data_income_amount) as amount
-            from app_data_income_{$month}
+            from app_data_income_{$month} as di
+                left join app_goods as g on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id') = g.goods_id 
+                {$where}
         ";
         $statistics = [];
         $category = ['all', 'entity', 'service', 'package'];
         foreach ($category as $item) {
             $execSql = $statisticsSql;
             if ($item == 'entity') {
-                $execSql .= " where data_income_classify in('GOODS')";
+                $execSql .= " and data_income_classify in('GOODS')";
             } elseif ($item == 'service') {
-                $execSql .= " where data_income_classify in('MEALS','CHNMED')";
+                $execSql .= " and data_income_classify in('MEALS','CHNMED')";
             } elseif ($item == 'package') {
-                $execSql .= " where data_income_classify = 'PACKAGE'";
+                $execSql .= " and data_income_classify = 'PACKAGE'";
             }
             $res = Db::select($execSql . ' limit 1');
             $statistics[$item] = $res[0];

+ 93 - 38
app/admin/controller/finance/WriteOffController.php

@@ -14,7 +14,11 @@ class WriteOffController
     {
         $page = $request->get('page',1);
         $pageSize = $request->get('pageSize',20);
-        $days = $request->get('days', ['2024-06-01','2024-06-30']);
+        $days = $request->get('days',[]);
+        $salePremisesId = $request->get('sale_premises_id','');
+        $writeOffPremisesId = $request->get('write_off_premises_id','');
+        $categoryId = $request->get('category_id','');
+        $goodsName = $request->get('goods_name','');
         if (!empty($days)) {
             $days[0] = strtotime($days[0]);
             $days[1] = strtotime($days[1]);
@@ -31,20 +35,51 @@ class WriteOffController
         if(!Util::schema()->hasTable("data_used_{$month}")){
             return json_fail('暂无数据');
         }
+        $categorySearchIds = '';
+        if (!empty($categoryId)){
+            $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
+            $categorySearchPath .= '#'. $categoryId.'#';
+            
+            $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
+            $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
+            $categorySearchIds = implode(',',$categorySearchIds);
+        }
+        
+        $where = " where 1 ";
+        if (!empty($days)) {
+            $where .= "and du.data_used_addtimes between {$days[0]} and {$days[1]} ";
+        }
+        if(!empty($salePremisesId)){
+            $where .= " and JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = {$salePremisesId} ";
+        }
+        if (!empty($writeOffPremisesId)){
+            $where .= " and du.join_data_used_dept_id = {$writeOffPremisesId}";
+        }
+        if (!empty($categorySearchIds)){
+            $where .= " and g.join_goods_category_id in({$categorySearchIds})";
+        }
+        if (!empty($goodsName)){
+            $where .= " and g.goods_name like '%{$goodsName}%' ";
+        }
 
         $totalSql = "
-            select count(*) as total from app_data_used_{$month}";
+            select 
+                count(*) as total 
+            from app_data_used_{$month} as du
+                left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id 
+                {$where}";
         $total = Db::select($totalSql);
 
         $recordStart = ($page - 1) * 20;
         $sql = "select 
                 g.goods_name,g.goods_sales_price,g.join_goods_category_id,gs.goods_sku_specs_json,wu.user_name as wu_username, su.user_name as su_username,du.data_used_addtimes,
-                JSON_EXTRACT(du.join_data_used_object_json,'$.charge.charge_premises') as write_off_premises, su.join_user_dept_id as premises_id 
+                JSON_EXTRACT(du.join_data_used_object_json,'$.charge.charge_premises') as write_off_premises, su.join_user_dept_id as premises_id,du.data_used_count as nbr 
             from app_data_used_{$month} as du 
                 left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id 
                 left join app_goods_sku as gs on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_sku_id') = gs.goods_sku_id 
                 left join app_sys_user as wu on du.join_data_used_user_id = wu.user_id 
                 left join app_sys_user as su on JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_id') = su.user_id 
+                {$where} 
             order by du.data_used_addtimes desc 
             limit " . $recordStart . ',' . $pageSize;
         $rows = Db::select($sql);
@@ -77,6 +112,7 @@ class WriteOffController
                 $categoryClassify = $category->category_classify ?? 'SERVICE';
             }
             $row->write_off_premises = trim($row->write_off_premises,"\"");
+            $row->data_used_addtimes = date('Y-m-d H:i:s',$row->data_used_addtimes);
         }
 
         return json_success('', [
@@ -89,7 +125,11 @@ class WriteOffController
 
     public function statistics(Request $request)
     {
-        $days = $request->get('days', ['2024-06-01','2024-06-30']);
+        $days = $request->get('days',[]);
+        $salePremisesId = $request->get('sale_premises_id','');
+        $writeOffPremisesId = $request->get('write_off_premises_id','');
+        $categoryId = $request->get('category_id','');
+        $goodsName = $request->get('goods_name','');
         if (!empty($days)) {
             $days[0] = strtotime($days[0]);
             $days[1] = strtotime($days[1]);
@@ -106,47 +146,62 @@ class WriteOffController
         if(!Util::schema()->hasTable("data_used_{$month}")){
             return json_fail('暂无数据');
         }
+        $categorySearchIds = '';
+        if (!empty($categoryId)){
+            $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
+            $categorySearchPath .= '#'. $categoryId.'#';
+            
+            $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
+            $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
+            $categorySearchIds = implode(',',$categorySearchIds);
+        }
+        
+        $where = " where 1 ";
+        if (!empty($days)) {
+            $where .= "and du.data_used_addtimes between {$days[0]} and {$days[1]} ";
+        }
+        if(!empty($salePremisesId)){
+            $where .= " and JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = {$salePremisesId} ";
+        }
+        if (!empty($writeOffPremisesId)){
+            $where .= " and du.join_data_used_dept_id = {$writeOffPremisesId}";
+        }
+        if (!empty($categorySearchIds)){
+            $where .= " and g.join_goods_category_id in({$categorySearchIds})";
+        }
+        if (!empty($goodsName)){
+            $where .= " and g.goods_name like '%{$goodsName}%' ";
+        }
 
-        $totalSql = "
-            select count(*) as total from app_data_used_{$month}";
-        $total = Db::select($totalSql);
         $sql = "select 
-                COUNT(*) as count,g.join_goods_category_id 
-            from app_data_used_{$month} as du 
+                SUM(data_used_count) as total,data_used_classify 
+            from app_data_used_{$month} as du
                 left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id 
-            ";
+                {$where}
+            group by data_used_classify";
+            dump($sql);
         $rows = Db::select($sql);
-        dump($rows);
-
+        $rows = json_decode(json_encode($rows),true);
+        
+        //核销归类 ORDER-PACKAGE=套包订单|ORDER-DISHES=点餐订单|ROLE-SERVICE=角色权益服务卡|ROLE-GOODS=角色权益产品卡|APPOINTMENT=后付费预约|ORDER=预付费订单
+        $statistics = ['service'=>0,'meals'=>0,'quota'=>0,'package'=>0,'goods'=>0,'appointment'=>0];
         foreach ($rows as &$row){
-            $row->salesman_premises = '';
-            if (!empty($row->premises_id) && $row->premises_id != 0){
-                $premises = SysDept::where('dept_id',$row->premises_id)
-                    ->select('dept_id','dept_name')
-                    ->first();
-                $row->salesman_premises = !empty($premises) ? $premises->dept_name : '';
+            if($row['data_used_classify'] == 'ORDER-DISHES'){
+                $statistics['meals'] = $row['total'];
+            }elseif($row['data_used_classify'] == 'ORDER-PACKAGE'){
+                $statistics['package'] = $row['total'];
+            }elseif($row['data_used_classify'] == 'ROLE-SERVICE'){
+                $statistics['quota'] = $row['total'];
+            }elseif($row['data_used_classify'] == 'ROLE-GOODS'){
+                $statistics['goods'] = $row['total'];
+            }elseif($row['data_used_classify'] == 'APPOINTMENT'){
+                $statistics['appointment'] = $row['total'];
+            }elseif($row['data_used_classify'] == 'ORDER'){
+                $statistics['service'] = $row['total'];
             }
-            if(!empty($row->goods_sku_specs_json)){
-                $specsJson = json_decode($row->goods_sku_specs_json,true);
-                $skuTitle = '';
-                foreach($specsJson as $key => $item){
-                    if(is_array($item)){
-                        $item = implode('',$item);
-                    }
-                    $skuTitle .= $key.':'.$item.',';
-                }
-
-                $row->sku_title = rtrim($skuTitle,',');
-            }
-            $categoryClassify = 'SERVICE';
-            $row->category = '';
-            if(!empty($row->join_goods_category_id)){
-                $category = SysCategory::where('category_id',$row->join_goods_category_id)->select('category_name','category_classify')->first();
-                $row->category = $category->category_name ?? '';
-                $categoryClassify = $category->category_classify ?? 'SERVICE';
-            }
-            $row->write_off_premises = trim($row->write_off_premises,"\"");
         }
+
+        return json_success('',$statistics);
     }
 
     public function records()

+ 1 - 0
route/admin.php

@@ -96,6 +96,7 @@ Route::group('/admin', function () {
     Route::group('/finance',function (){
         Route::group('/writeOff',function (){
             Route::get('/list',[\app\admin\controller\finance\WriteOffController::class,'list']);
+            Route::get('/statistics',[\app\admin\controller\finance\WriteOffController::class,'statistics']);
         });
         Route::group('/goodsSales',function (){
             Route::get('/statistics',[\app\admin\controller\finance\GoodsSalesController::class,'statistics']);