cqp 3 weeks ago
parent
commit
488b68c9da

+ 12 - 0
app/Http/Controllers/Api/StatisticsController.php

@@ -54,4 +54,16 @@ class StatisticsController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function statisticsProfit(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->statisticsProfit($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 5 - 4
app/Model/RevenueCost.php

@@ -25,8 +25,9 @@ class RevenueCost extends Model
     const job = 'yf_revenue_cost';
 
     public static $field = ['id','order_time','order_type','price_1','price_1_total','price_2','price_2_total','price_3','price_3_total','price_4','price_4_total','payment_amount','profit','profit_rate','employee_id_1','employee_id_1_title'];
-    public static $field_xhd = ['id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2_title','customer_code','customer_title','channel_finance','channel_details','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_3','price_3_total','price_4','price_4_total','profit','profit_rate'];
-    public static $field_xsfp = ['id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2_title','customer_code','customer_title','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_4','price_4_total','profit','profit_rate'];
-    public static $field_hkd_main = ['order_id as id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2_title','payment_amount','profit','order_id'];
-    public static $field_hkd_detail = ['order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2_title','customer_code','customer_title','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_3','price_3_total','price_4','price_4_total','profit','profit_rate','order_number_upstream','payment_amount'];
+    public static $field_xhd = ['id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2','employee_id_2_title','customer_code','customer_title','channel_finance','channel_details','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_3','price_3_total','price_4','price_4_total','profit','profit_rate'];
+    public static $field_xsfp = ['id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2','employee_id_2_title','customer_code','customer_title','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_4','price_4_total','profit','profit_rate'];
+    public static $field_hkd_main = ['order_id as id','order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2','employee_id_2_title','payment_amount','profit','order_id'];
+    public static $field_hkd_detail = ['order_type','order_number','order_time','employee_id_1','employee_id_1_title','employee_id_2','employee_id_2_title','customer_code','customer_title','product_code','product_title','product_size','unit','price_1','price_1_total','price_2','price_2_total','quantity','price_3','price_3_total','price_4','price_4_total','profit','profit_rate','order_number_upstream','payment_amount'];
+    public static $field_hkd_profit_main = ['rc.employee_id_2','rc.employee_id_2_title','rc.payment_amount','employee_index.start_time','employee_index.end_time','employee_index.index','employee_index.id'];
 }

+ 47 - 1
app/Service/ExportFileService.php

@@ -51,7 +51,7 @@ class ExportFileService extends Service
         self::type_five => '销售发票统计表',
         self::type_six => '回款单统计表',
         self::type_seven => '发放统计表',
-        self::type_eight => '进销存报表',
+        self::type_eight => '利润分配统计表',
         self::type_nine => '虚拟采购单',
         self::type_ten => '客户',
         self::type_eve => '现存量',
@@ -303,6 +303,46 @@ class ExportFileService extends Service
         return $this->saveExportData($return,$header);
     }
 
+    public function eight($ergs,$user){
+        $id = $ergs['id'];
+
+        $field = RevenueCost::$field_hkd_profit_main;
+        $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
+        $type = RevenueCost::ORDER_THREE;
+
+        $return = [];
+        $header_default = config("header.74") ?? [];
+        $column = array_column($header_default, 'key');
+        $header = array_column($header_default, 'value');
+        DB::table('employee_index')
+            ->where('employee_index.del_time', 0)
+            ->whereIn('employee_index.id', $id)
+            ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
+                $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
+                    ->where('rc.del_time', 0)
+                    ->where('rc.order_type', $type)
+                    ->whereRaw('rc.order_time >= employee_index.start_time')
+                    ->whereRaw('rc.order_time <= employee_index.end_time');
+            })
+            ->select($field)
+            ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
+            ->orderBy('employee_index.end_time','desc')
+            ->chunkById(500, function ($data) use (&$return, $column) {
+                $data = Collect($data)->map(function ($object) {
+                    return (array)$object;
+                })->toArray();
+                $list['data'] = $data;
+
+                //订单数据
+                $service = new StatisticsService();
+                $list = $service->statisticsProfitFillData($list);
+
+                $this->fillData($list['data'], $column, $return);
+            });
+
+        return $this->saveExportData($return,$header);
+    }
+
     public function getListForSearch($ergs, $user){
         $data = $ergs['order_search'];
         $id = [];
@@ -346,6 +386,12 @@ class ExportFileService extends Service
             $model = $service->giveOutCommon($data, $user, ['id']);
             $return = $this->limitData($model,'',$data);
             $id = array_column($return,'id');
+        }elseif ($ergs['type'] == self::type_eight){
+            $service = new StatisticsService();
+            list($status,$model) = $service->statisticsProfitCommon($data, $user, ['employee_index.id']);
+            if(! $status) return [false, $model];
+            $return = $this->limitData($model,'',$data);
+            $id = array_column($return,'id');
         }
 
         return [true, $id];

+ 3 - 1
app/Service/GiveOutService.php

@@ -25,6 +25,7 @@ class GiveOutService extends Service
             $model->send_time = $data['send_time'] ?? 0;
             $model->start_time = $data['start_time'] ?? 0;
             $model->end_time = $data['end_time'] ?? 0;
+            $model->mark = $data['mark'] ?? "";
             $model->save();
 
             DB::commit();
@@ -51,6 +52,7 @@ class GiveOutService extends Service
             $model->start_time = $data['start_time'] ?? 0;
             $model->end_time = $data['end_time'] ?? 0;
             $model->crt_id = $user['id'];
+            $model->mark = $data['mark'] ?? "";
             $model->save();
 
             DB::commit();
@@ -145,7 +147,7 @@ class GiveOutService extends Service
         list($start_time,$end_time) = $this->changeDateToTimeStampAboutRange($data['belong_time'], false);
         if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "归属日期的区间无效"];
         $data['start_time'] = $start_time;
-        $data['end_time'] = $start_time;
+        $data['end_time'] = $end_time;
 
         if($is_add){
 

+ 177 - 0
app/Service/StatisticsService.php

@@ -2,6 +2,8 @@
 
 namespace App\Service;
 
+use App\Model\EmployeeIndex;
+use App\Model\GiveOut;
 use App\Model\RevenueCost;
 use App\Model\RevenueCostTotal;
 use Illuminate\Support\Facades\DB;
@@ -229,4 +231,179 @@ class StatisticsService extends Service
 
         return [true, $order];
     }
+
+    public function statisticsProfitCommon($data,$user, $field = []){
+        if(empty($field)) {
+            $field = RevenueCost::$field_hkd_profit_main;
+            $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
+        }
+
+        $type = RevenueCost::ORDER_THREE;
+        $model = EmployeeIndex::where('employee_index.del_time',0)
+            ->where('employee_index.type',EmployeeIndex::TYPE_THREE)
+            ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
+                $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
+                    ->where('rc.del_time', 0)
+                    ->where('rc.order_type', $type)
+                    ->whereRaw('rc.order_time >= employee_index.start_time')
+                    ->whereRaw('rc.order_time <= employee_index.end_time');
+            })
+            ->select($field)
+            ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
+            ->orderBy('employee_index.end_time','desc');
+
+        if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){;
+            list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
+            if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
+            $model->where('employee_index.start_time', '<=', $end_time)
+                ->where('employee_index.end_time', '>=', $start_time);
+        }
+        if(! empty($data['employee_id_2_title'])) $model->where('rc.employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
+
+        return [true, $model];
+    }
+
+    public function statisticsProfit($data,$user){
+        list($status, $model) = $this->statisticsProfitCommon($data, $user);
+        if(! $status) return [false, $model];
+        $list = $this->limit($model,'',$data);
+        $list = $this->statisticsProfitFillData($list);
+
+        return [true, $list];
+    }
+
+    public function statisticsProfitFillData($data){
+        if(empty($data['data'])) return $data;
+
+        //获取应发放金额需要的指标
+        $indexes = $this->getEmployeeIndex($data['data']);
+        //已发
+        $indexes_give = $this->getEmployeeGiveOut($data['data']);
+
+        foreach ($data['data'] as $key => $value){
+            $start = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
+            $end = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
+            $string = "";
+            if(! empty($start) && ! empty($end)) $string = $start . "-" . $end;
+            $value['check_time'] = $string;
+            $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
+            $value['index_' . EmployeeIndex::TYPE_FIVE] = 0;
+            //指标
+            $this->makeIndex($indexes, $value, $data['data'][$key]);
+            //应付
+            $this->makeShouldPay($value,$data['data'][$key]);
+            //已付
+            $this->makePayed($indexes_give, $value,$data['data'][$key]);
+            //未付
+            $not_pay = bcsub($data['data'][$key]['should_pay'], $data['data'][$key]['payed'],2);
+            $data['data'][$key]['not_pay'] = $not_pay;
+        }
+
+        return $data;
+    }
+
+    private function getEmployeeIndex($existingData)
+    {
+        if (empty($existingData)) {
+            return collect();
+        }
+
+        // 取出所有涉及的 employee_id 和时间区间
+        $employeeIds = array_column($existingData, 'employee_id_2');
+        $minStart = min(array_column($existingData, 'start_time'));
+        $maxEnd   = max(array_column($existingData, 'end_time'));
+
+        // 一次性查出这些员工在最大区间范围内的所有指标
+        $results = EmployeeIndex::where('del_time', 0)
+            ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_FIVE])
+            ->whereIn('employee_id', $employeeIds)
+            ->where('start_time', '<=', $maxEnd)
+            ->where('end_time', '>=', $minStart)
+            ->select('start_time','end_time','employee_id','index','type')
+            ->get();
+
+        return $results;
+    }
+
+    private function getEmployeeGiveOut($existingData)
+    {
+        if (empty($existingData)) {
+            return collect();
+        }
+
+        // 取出所有涉及的 employee_id 和时间区间
+        $employeeIds = array_column($existingData, 'employee_id_2');
+        $minStart = min(array_column($existingData, 'start_time'));
+        $maxEnd   = max(array_column($existingData, 'end_time'));
+
+        // 一次性查出这些员工在最大区间范围内的所有指标
+        $results = GiveOut::where('del_time', 0)
+            ->whereIn('employee_id_1', $employeeIds)
+            ->where('start_time', '<=', $maxEnd)
+            ->where('end_time', '>=', $minStart)
+            ->select('start_time','end_time','employee_id_1 as employee_id','give_out_amount')
+            ->get();
+
+        return $results;
+    }
+
+    private function makeIndex($indexes, $value, &$data){
+        // 找到所有符合条件的 index(可能多个 type)
+        $matchedIndexes = $indexes->filter(function ($item) use ($value) {
+            return $item['employee_id'] == $value['employee_id_2']
+                && $item['start_time'] <= $value['end_time']
+                && $item['end_time'] >= $value['start_time'];
+        });
+        // 按 type 去重,只保留第一次出现的
+        $uniqueByType = [];
+        foreach ($matchedIndexes as $item) {
+            $index = "index_" . $item['type'];
+            if (! isset($uniqueByType[$index])) {
+                $uniqueByType[$index] = $item['index'];
+            }
+        }
+        $data = array_merge($value, $uniqueByType);
+    }
+
+    private function makeShouldPay($value, &$data){
+        $index_1 = bcdiv($value['index_1'],100,2);
+        $index_5 = bcdiv($value['index_5'],100,2);
+        // 分红比例/2
+        $a = bcdiv($index_5,2,2);
+        // 回款金额*季度预支分红比例/2
+        $b = bcdiv(bcmul($value['payment_amount'], $index_1),2,2);
+        //(分红比例/2-回款金额*季度预支分红比例/2)
+        $c = bcsub($a, $b,2);
+        $data['part_left'] = $c;
+        if($value['payment_amount'] < $value['index']){
+            //回款金额*(分红比例/2-回款金额*季度预支分红比例/2);
+            $shouldPay = bcmul($value['payment_amount'], $c,2);
+        }else{
+            //回款金额-季度指标金额
+            $d = bcsub($value['payment_amount'], $value['index'],2);
+            //(回款金额-季度指标金额)*15%
+            $e = bcmul($d, 0.15,2);
+            //(回款金额-季度指标金额)*15%/2
+            $e = bcdiv($e,2,2);
+            //(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
+            $rate = bcadd($c, $e,2);
+//                $data['data'][$key]['rate'] = $rate;
+            //回款金额*(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
+            $shouldPay = bcmul($value['payment_amount'], $rate,2);
+        }
+        $data['should_pay'] = $shouldPay;
+    }
+
+    private function makePayed($indexes, $value, &$data){
+        $matchedIndexes_give = $indexes->filter(function ($item) use ($value) {
+            return $item['employee_id'] == $value['employee_id_2']
+                && $item['start_time'] <= $value['end_time']
+                && $item['end_time'] >= $value['start_time'];
+        });
+        $give_out = 0;
+        foreach ($matchedIndexes_give as $item) {
+            $give_out = bcadd($item['give_out_amount'], $give_out,2);
+        }
+        $data['payed'] = $give_out;
+    }
 }

+ 3 - 1
app/Service/TableHeadService.php

@@ -55,6 +55,8 @@ class TableHeadService extends Service
 
     public function tableheadGet($data, $user){
         if(empty($data['menu_id'])) return [false,'menu_id不能为空!'];
+        $header_common = config("header.common") ?? [];
+        $header_common = $header_common[$data['menu_id']] ?? [];
 
         $header = TableSetting::where('del_time',0)
             ->where('menu_id',$data['menu_id'])
@@ -94,7 +96,7 @@ class TableHeadService extends Service
             return $a['sort'] - $b['sort'];
         });
 
-        return [true, $header_default];
+        return [true, ['column' => $header_default, 'common' => $header_common]];
     }
 
     public function filterAdd($data, $user){

+ 37 - 0
config/header/74.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' =>'check_time',
+        'value' => '核算时间区间',
+    ],
+    [
+        'key' =>'employee_id_2_title',
+        'value' => '上级管理人员',
+    ],
+    [
+        'key' =>'payment_amount',
+        'value' => '回款金额',
+    ],
+    [
+        'key' =>'index',
+        'value' => '季度指标金额',
+    ],
+    [
+        'key' =>'should_pay',
+        'value' => '应发放金额',
+    ],
+    [
+        'key' =>'payed',
+        'value' => '应发放金额',
+    ],
+    [
+        'key' =>'not_pay',
+        'value' => '未发放金额',
+    ],
+];

+ 20 - 0
config/header/common.php

@@ -0,0 +1,20 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    62 => ['has_detail' => true, 'is_jump' => false], // 存货
+    63 => ['has_detail' => false, 'is_jump' => false],// 部门
+    64 => ['has_detail' => true, 'is_jump' => false], // 人员
+    65 => ['has_detail' => true, 'is_jump' => false], // 角色
+    66 => ['has_detail' => true, 'is_jump' => false], // 运费档案
+    68 => ['has_detail' => false, 'is_jump' => true], // 收入成本
+    69 => ['has_detail' => false, 'is_jump' => false], // 销货单
+    70 => ['has_detail' => false, 'is_jump' => false], // 销售发票
+    71 => ['has_detail' => true, 'is_jump' => false], // 回款单
+    72 => ['has_detail' => false, 'is_jump' => false], // 发放
+    74 => ['has_detail' => true, 'is_jump' => false], // 利润分配
+];

+ 2 - 0
routes/api.php

@@ -140,4 +140,6 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('statisticsRevenueCostOneAndTwo', 'Api\StatisticsController@statisticsRevenueCostOneAndTwo');
     $route->any('statisticsRevenueCostThree', 'Api\StatisticsController@statisticsRevenueCostThree');
     $route->any('statisticsRevenueCostThreeDetail', 'Api\StatisticsController@statisticsRevenueCostThreeDetail');
+    //利润分配
+    $route->any('statisticsProfit', 'Api\StatisticsController@statisticsProfit');
 });