Browse Source

报表相关修改

cqp 2 tháng trước cách đây
mục cha
commit
a6ef8d67a0

+ 1 - 1
app/Console/Commands/CustomerWriteReport.php

@@ -74,7 +74,7 @@ class CustomerWriteReport extends Command
             ->where('enter_time','<=', $nowStamp)
             ->orderBy('enter_time','asc')
             ->select('id as customer_id','title','model_type','consulting_product_new','enter_time','address2','top_depart_id','customer_from','car_type')
-            ->chunk(200, function ($data) use($provinceMap, $cityMap, $countyMap,$nowStamp){
+            ->chunkById(200, function ($data) use($provinceMap, $cityMap, $countyMap,$nowStamp){
                 // 开启事务
                 DB::transaction(function () use ($data, $provinceMap, $cityMap, $countyMap, $nowStamp) {
                     $dataArray = $data->toArray();

+ 295 - 0
app/Console/Commands/SalesWriteReport.php

@@ -0,0 +1,295 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Model\AreaMap;
+use App\Model\BasicType;
+use App\Model\BasicTypeAllUse;
+use App\Model\Construction;
+use App\Model\Customer;
+use App\Model\CustomerInfo;
+use App\Model\CustomerReport;
+use App\Model\CustomerReportDepart;
+use App\Model\Depart;
+use App\Model\Employee;
+use App\Model\FollowUpRecord;
+use App\Model\PaymentReceipt;
+use App\Model\PaymentReceiptInfo;
+use App\Model\Product;
+use App\Model\PurchaseOrder;
+use App\Model\SalesOrder;
+use App\Model\SalesOrderInfo;
+use App\Model\SalesOrderOtherFee;
+use App\Model\SalesOrderProductInfo;
+use App\Model\SalesOrderReport;
+use App\Model\SeeRange;
+use App\Service\ProductService;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class SalesWriteReport extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:sales_write_report';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        echo "执行任务--------start---------------\n";
+
+        try {
+            $this->SalesWrite();
+        }catch (\Throwable $exception){
+            echo "发生异常:" . $exception->getFile() . "|" . $exception->getMessage() . "|" . $exception->getLine() . "\n";
+        }
+
+        echo "执行任务--------end---------------\n";
+    }
+
+    private function SalesWrite(){
+        //今天到上月今天的时间戳
+        $nowStamp = time();
+        $lastMonth = date("Y-m-d 00:00:00", strtotime("-1 month"));
+        $lastMonthStamp = strtotime($lastMonth);
+
+        $area = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->pluck('area','id')
+            ->toArray();
+        SalesOrder::where("del_time",0)
+            ->where('crt_time','>=', $lastMonthStamp)
+            ->where('crt_time','<=', $nowStamp)
+            ->orderBy('crt_time','asc')
+            ->chunkById(200, function ($data) use($nowStamp, $area){
+                // 开启事务
+                DB::transaction(function () use ($data, $nowStamp, $area) {
+                    $dataArray = $data->toArray();
+                    $main = $this->processingData($dataArray, $nowStamp,$area);
+                    if (! empty($main)) {
+
+                        SalesOrderReport::whereIn('sale_order_id',array_column($dataArray,'id'))
+                            ->update(['del_time' => $nowStamp]);
+
+                        SalesOrderReport::insert($main);
+                        echo '200条写入中' . PHP_EOL;
+                    } else {
+                        echo '暂无数据写入' . PHP_EOL;
+                    }
+                });
+            });
+    }
+
+    private function processingData($data,$time,$area){
+        $id = array_column($data,'id');
+        $contact_product = $this->getSalesOrderProduct($id);
+        $customer = $this->getCustomer(array_column($data,'customer_id'));
+        $basic = $this->getBasic($data);
+        $zp_money = $this->getOtherMoney($id);
+        $fz_man = $this->getFzMan($id);
+        $construction = $this->getConstruction($id);
+        $payment = $this->getPayment(array_column($data,'order_number'));
+        $purchase = $this->getPurchaseFrom(array_filter(array_column($data,'contact_order_no')));
+
+        $insert = [];
+        foreach ($data as $value){
+            $receipt = $payment[$value['order_number']] ?? 0;
+            $contact_product_tmp = $contact_product[$value['id']] ?? [];
+            $contact_product_tmp_str = implode('、',$contact_product_tmp);
+            $customer_tmp = $customer[$value['customer_id']] ?? "";
+            $basic_tmp = $basic[$value['plat_type']] ?? "";
+            $area_tmp = $area[$value['top_depart_id']] ?? 0;
+            $zp_tmp = $zp_money[$value['id']] ?? 0;
+            $fz_tmp = $fz_man[$value['id']] ?? "";
+            $purchase_tmp = $purchase[$value['contact_order_no']] ?? 0;
+            $state = $this->makeState($value);
+            if(isset($construction[$value['id']])){
+                $c = $construction[$value['id']];
+                if($c <= Construction::STATE_THREE){
+                    $state_tmp = 2;
+                }elseif($c <= Construction::STATE_FIVE){
+                    $state_tmp = 3;
+                }else{
+                    $state_tmp = 4;
+                }
+            }else{
+                $state_tmp = $state;
+            }
+
+            $insert[] = [
+                'sale_order_id' => $value['id'],
+                'sales_order_type' => $value['sales_order_type'],
+                'receipt' => $receipt,
+                'model_type' => $value['model_type'],
+                'order_number' => $value['order_number'],
+                'contact_product' => $contact_product_tmp_str,
+                'fz_man' => $fz_tmp,
+                'plat_type_title' => $basic_tmp,
+                'customer_title' => $customer_tmp,
+                'crt_id' => $value['crt_id'],
+                'crt_time' => $value['crt_time'],
+                'contract_fee' => $value['contract_fee'],
+                'zp_contract_fee' => $zp_tmp,
+                'top_depart_id' => $value['top_depart_id'],
+                'from_top_depart_id' => $purchase_tmp,
+                'area' => $area_tmp,
+                'state' => $state_tmp,
+            ];
+        }
+
+        return $insert;
+    }
+
+    private function getSalesOrderProduct($data_id)
+    {
+        $product = [];
+        $sales_p_info = SalesOrderProductInfo::where('del_time',0)
+            ->whereIn('sales_order_id',$data_id)
+            ->select('product_id','sales_order_id')
+            ->get()->toArray();
+        $map = Product::whereIn('id',array_unique(array_column($sales_p_info,'product_id')))
+            ->pluck('title','id')
+            ->toArray();
+        foreach ($sales_p_info as $value){
+            $tmp = $map[$value['product_id']] ?? "";
+            $product[$value['sales_order_id']][] = $tmp;
+        }
+
+        return $product;
+    }
+
+    private function getCustomer($data_id)
+    {
+        return Customer::whereIn('id',$data_id)
+            ->pluck('title','id')
+            ->toArray();
+    }
+
+    private function getBasic($data)
+    {
+        $array = array_unique(array_column($data,'plat_type'));
+        return BasicType::whereIn('id',$array)
+            ->pluck('title','id')
+            ->toArray();
+    }
+
+    private function getOtherMoney($id){
+        $fee = [];
+        $sales_o_info = SalesOrderOtherFee::where('del_time',0)
+            ->whereIn('sales_order_id',$id)
+            ->get()->toArray();
+        foreach ($sales_o_info as $value){
+            $fee[$value['sales_order_id']] = $value['other_fee_1'];
+        }
+
+        return $fee;
+    }
+
+    private function getFzMan($id){
+        //单据中选择的负责人
+        $sales_order = SalesOrderInfo::where('del_time',0)
+            ->where('type',SalesOrderInfo::type_two)
+            ->whereIn('sales_order_id',$id)
+            ->select('sales_order_id','data_id')
+            ->get()->toArray();
+        $empList = Employee::whereIn('id', array_unique(array_column($sales_order,'data_id')))
+            ->pluck('emp_name','id')
+            ->toArray();
+        $employee_two = [];
+        foreach ($sales_order as $value){
+            $str = $empList[$value['data_id']] ?? "";
+            if(! $str) continue;
+            if(isset($employee_two[$value['sales_order_id']])){
+                $employee_two[$value['sales_order_id']] .= ',' . $str;
+            }else{
+                $employee_two[$value['sales_order_id']] = $str;
+            }
+        }
+
+        return $employee_two;
+    }
+
+    private function makeState($value){
+        if($value['sales_order_type'] == SalesOrder::Order_type_one){
+            //安装件
+            if(in_array($value['state'], [SalesOrder::State_minus_one, SalesOrder::State_zero, SalesOrder::State_one])){
+               return 0;
+            }elseif(in_array($value['state'], [SalesOrder::State_two, SalesOrder::State_three, SalesOrder::State_four])){
+                return 1;
+            }else{
+                return 1;
+            }
+        }else{
+            return 0;
+        }
+
+        return $state;
+    }
+
+    private function getConstruction($id){
+        $construction = Construction::where('del_time',0)
+            ->whereIn('sales_order_id', $id)
+            ->select('sales_order_id','state')
+            ->get()->toArray();
+        $return = [];
+        foreach ($construction as $value){
+            if(isset($return[$value['sales_order_id']])){
+                if($value['state'] > $return[$value['sales_order_id']]) $return[$value['sales_order_id']] = $value['state'];
+            }else{
+                $return[$value['sales_order_id']] = $value['state'];
+            }
+        }
+
+        return $return;
+    }
+
+    private function getPayment($id){
+        $infos = PaymentReceiptInfo::where('del_time',0)
+            ->where('type',PaymentReceiptInfo::type_three)//回款信息
+            ->where('data_order_type', PaymentReceipt::data_type_one)//合同
+            ->where('data_type', PaymentReceipt::type_one)//收款
+            ->whereIn('data_order_no',$id)
+            ->get()->toArray();
+        $infos_map = [];
+        foreach ($infos as $value){
+            if(isset($infos_map[$value['data_order_no']])){
+                $tmp = bcadd($value['amount'], $infos_map[$value['data_order_no']],2);
+                $infos_map[$value['data_order_no']] = $tmp;
+            }else{
+                $infos_map[$value['data_order_no']] = $value['amount'];
+            }
+        }
+
+        return $infos_map;
+    }
+
+    private function getPurchaseFrom($order_no){
+        return PurchaseOrder::whereIn('order_number',$order_no)
+            ->pluck('top_depart_id','order_number')
+            ->toArray();
+    }
+}

+ 13 - 0
app/Http/Controllers/Api/ProductInventoryController.php

@@ -73,4 +73,17 @@ class ProductInventoryController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function manSetting(Request $request)
+    {
+        $service = new ProductInventoryService();
+        $user = $request->userData->toArray();
+        list($status,$data) = $service->manSetting($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

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

@@ -150,4 +150,64 @@ class StatisticsController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function saleReportStepOfFirst(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->saleReportStepOfFirst($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function saleReportStepOfSecond(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->saleReportStepOfSecond($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function saleReportStepOfThird(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->saleReportStepOfThird($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function saleReportStepOfFour(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->saleReportStepOfFour($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function saleReportStepOfFive(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->saleReportStepOfFive($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 6 - 1
app/Import/ImportAll.php

@@ -14,6 +14,7 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
     public $type = "";
     public $user = [];
     public $is_long_text = false;
+    public $other_param = [];
 
     public function array (array $array){
         $this->handleData($array);
@@ -31,6 +32,10 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
         $this->user = $user;
     }
 
+    public function setOtherParam($param){
+        $this->other_param = $param;
+    }
+
     public function getMsg(){
         return $this->msg;
     }
@@ -54,7 +59,7 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
             return;
         }
 
-        list($status,$msg) = (new ImportService())->$func($array,$this->user);
+        list($status,$msg) = (new ImportService())->$func($array,$this->user,$this->other_param);
         if(empty($status)) {
             if($status === 0) $this->setIsLongText();
             $this->setMsg($msg);

+ 11 - 0
app/Model/CustomerPerFormance.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Model;
+
+class CustomerPerFormance extends UseScopeBaseModel
+{
+    protected $table = "customer_performance"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+}

+ 11 - 0
app/Model/MonthlyPerFormance.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Model;
+
+class MonthlyPerFormance extends UseScopeBaseModel
+{
+    protected $table = "monthly_performance"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+}

+ 38 - 0
app/Model/SalesOrderReport.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Model;
+
+class SalesOrderReport extends UseScopeBaseModel
+{
+    protected $table = "sales_order_report"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+
+    const type_one = 1;
+    const type_two = 2;
+    const type_three = 3;
+    const type_four = 4;
+    const type_five = 5;
+    public static $all_list = [
+        self::type_one => '门店零售',
+        self::type_two => '私域营销',
+        self::type_three => '线上销售',
+        self::type_four => '门店业绩',
+        self::type_five => '客资业绩',
+    ];
+
+    const state_zero = 0;
+    const state_one = 1;
+    const state_two = 2;
+    const state_three = 3;
+    const state_four = 4;
+
+    public static $all_state_list = [
+        self::state_zero => '全部',
+        self::state_one => '已审核',
+        self::state_two => '施工中',
+        self::state_three => '施工完结',
+        self::state_four => '已交车',
+    ];
+}

+ 58 - 0
app/Service/ImportService.php

@@ -9,6 +9,7 @@ use App\Model\BasicType;
 use App\Model\BasicTypeAllUse;
 use App\Model\Customer;
 use App\Model\CustomerInfo;
+use App\Model\CustomerPerFormance;
 use App\Model\CustomerRemain;
 use App\Model\CustomerRepeat;
 use App\Model\Depart;
@@ -43,6 +44,7 @@ class ImportService extends Service
         'lastJc', //上月结存
         'inventory', // 盘点
         'productAdjustment', // 产品金额调整单
+        'customerPerformance', // 客资金额
     ];
 
     //写活的导入------------------- 暂时不好用
@@ -251,6 +253,17 @@ class ImportService extends Service
         return [true, $config_array,$filename];
     }
 
+    private function customerPerformance($data,$user){
+        //生成下载文件
+        $filename =  "客资业绩导入模板_" . time() . '.' . 'xlsx';
+
+        //获取配置文件
+        $config = "excel.customerPerformanceTable";
+        $config_array = config($config) ?? [];
+        if(empty($config_array)) return [false, '配置文件不存在',''];
+        return [true, $config_array,$filename];
+    }
+
     //导入入口
     public function importAll($data,$user){
 //        //不超时
@@ -282,6 +295,9 @@ class ImportService extends Service
             $import->setCrt($user['id']);
             $import->setUser($user);
             $import->setType($data['type']);
+            $other = $data;
+            unset($other['file']);
+            $import->setOtherParam($other);
 
             //导入
             \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
@@ -1938,5 +1954,47 @@ class ImportService extends Service
 
         return [true, ''];
     }
+
+    public function customerPerformanceImport($array, $user, $other_param){
+        if(empty($other_param["ym"])) return [false,'请选择导入客资业绩年月'];
+
+        // 去除表头
+        unset($array[0]);
+        if(empty($array)) return [false, '导入数据不能为空'];
+
+        $insert = [];
+        foreach ($array as $key => $value){
+            $rowData = array_filter($value);
+            if (empty($rowData)) {
+                unset($array[$key]);
+            } elseif(empty($value[0]) || empty($value[1])) {
+                return [false, '带*号的字段项必填'];
+            }else{
+                foreach ($value as $k => $v){
+                    $value[$k] = trim($v);
+                }
+
+                if(! empty($value[2]) && ! is_numeric($value[2])) return [false, '当月打款金额请填写正确的数值'];
+            }
+
+            $insert[] = [
+                'area' => $value['0'],
+                'title' => $value['1'],
+                'current_month_payment' => $value['2'] ?? 0,
+                'crt_time' => $other_param['ym']
+            ];
+        }
+
+        $start_time = strtotime(date("Y-m-01 00:00:00", $other_param['ym']));
+        $end_time = strtotime(date("Y-m-t 23:59:59", $other_param['ym']));
+        CustomerPerFormance::where('del_time',0)
+            ->where('crt_time','>=', $start_time)
+            ->where('crt_time','<=', $end_time)
+            ->update(['del_time' => time()]);
+
+        if(! empty($insert)) CustomerPerFormance::insert($insert);
+
+        return [true, ''];
+    }
 }
 

+ 36 - 0
app/Service/ProductInventoryService.php

@@ -10,6 +10,7 @@ use App\Model\InOutRecord;
 use App\Model\Inventory;
 use App\Model\InvoiceOrder;
 use App\Model\InvoiceOrderInfo;
+use App\Model\MonthlyPerFormance;
 use App\Model\OrderOperation;
 use App\Model\OutBoundOrder;
 use App\Model\Product;
@@ -449,4 +450,39 @@ class ProductInventoryService extends Service
         }
         return [true, $list];
     }
+
+    public function manSetting($data, $user){
+        if(empty($data['ym'])) return [false, '业绩目标年月不能为空'];
+        $start_time = strtotime(date("Y-m-01 00:00:00",$data['ym']));
+        $end_time = strtotime(date("Y-m-t 23:59:59",$data['ym']));
+        if(empty($data['data'])) return [false, '人员业绩不能为空'];
+        $insert = [];
+        foreach ($data['data'] as $value){
+            if(empty($value['employee_id'])) return [false, '人员Id不能为空'];
+            if(! isset($value['sales_index'])) return [false, '人员业绩不能为空'];
+            $insert[] = [
+                'employee_id' => $value['employee_id'],
+                'sales_index' => $value['sales_index'],
+                'crt_time' => $data['ym'],
+            ];
+        }
+        $employee_id = array_column($data['data'],'employee_id');
+
+        try {
+            $time = time();
+
+            MonthlyPerFormance::where('del_time',0)
+                ->where('crt_time',">=",$start_time)
+                ->where('crt_time','<=',$end_time)
+                ->whereIn('employee_id',$employee_id)
+                ->update(['del_time' => $time]);
+
+            if(! empty($insert)) MonthlyPerFormance::insert($insert);
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
 }

+ 1 - 1
app/Service/SalesOrderService.php

@@ -1905,7 +1905,7 @@ class SalesOrderService extends Service
             //安装件
             if(! empty($state_array[$value['order_number']])){
                 $return = $state_array[$value['order_number']];
-                if($value['state'] == PurchaseOrder::State_minus_one){
+                if($value['state'] == SalesOrder::State_minus_one){
                     $state = "驳回:" . $return;
                 }else{
                     $state = "待" . $return . "审核";

+ 1120 - 1
app/Service/StatisticsService.php

@@ -5,6 +5,7 @@ namespace App\Service;
 use App\Model\BasicType;
 use App\Model\BasicTypeAllUse;
 use App\Model\Customer;
+use App\Model\CustomerPerFormance;
 use App\Model\CustomerReport;
 use App\Model\CustomerReportDepart;
 use App\Model\Depart;
@@ -15,6 +16,7 @@ use App\Model\InOutRecord;
 use App\Model\InvoiceOrder;
 use App\Model\InvoiceOrderInfo;
 use App\Model\LastJc;
+use App\Model\MonthlyPerFormance;
 use App\Model\Product;
 use App\Model\ProductAdjustment;
 use App\Model\ProductCategory;
@@ -24,6 +26,7 @@ use App\Model\ReturnExchangeOrder;
 use App\Model\ReturnExchangeOrderProductInfo;
 use App\Model\SalesOrder;
 use App\Model\SalesOrderProductInfo;
+use App\Model\SalesOrderReport;
 use App\Model\SeeRange;
 use App\Model\Setting;
 use Carbon\Carbon;
@@ -1899,6 +1902,12 @@ class StatisticsService extends Service
         return $adjustedData;
     }
 
+    private function aboutTime($return){
+        $day = $this->returnDays($return);
+        if($day > 60) return [false, '查询时间仅支持范围区间在两月(60天)内'];
+        return [true ,''];
+    }
+
     //改装 客户模板 t9改装  不是分配的客户 是所有的 =》 点击省 展示市  只有这个有产品
     //分社 客户模板 t9改装  不是分配的客户 是所有的 =》 点击省 展示分社
     //加盟 客户模板 t9分社  不是分配的客户 是所有的 =》 点击省 展示市
@@ -1906,6 +1915,8 @@ class StatisticsService extends Service
     public function customerReportStepOfFirst($data, $user){
         if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
         $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
         if(empty($data['type'])) return [false, '请选择报表查看类型'];
         $type = $data['type'];
         $result = [];
@@ -2253,6 +2264,8 @@ class StatisticsService extends Service
     public function customerReportStepOfSecond($data, $user){
         if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
         $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
         if(empty($data['type'])) return [false, '请选择报表查看类型'];
         if(empty($data['value'])) return [false, 'value不能为空'];
         $type = $data['type'];
@@ -2564,11 +2577,12 @@ class StatisticsService extends Service
     public function customerReportStepOfThird($data, $user){
         if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
         $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
         if(empty($data['type'])) return [false, '请选择报表查看类型'];
         if(empty($data['value'])) return [false, 'value不能为空'];
         if(empty($data['value2'])) return [false, 'value2不能为空'];
         $type = $data['type'];
-        $result = [];
         if($type == 1){
             $result = $this->customerReportStepThirdForDetail($return,$data,$type);
         }elseif ($type == 2){
@@ -2794,4 +2808,1109 @@ class StatisticsService extends Service
 
         return $customer;
     }
+
+    //门店零售:统计各个门店所有的订单总金额
+    //私域营销:统计T9品牌中心和杭州旗舰店的营销部部门下所有的人员的线下订单成交金额的总计
+    //线上销售:来自T9品牌中心所有的线上订单的合同金额总计
+    //门店业绩:来自分社采购单转成的订单的合同金额汇总
+    //客资业绩:导入的数据
+    public function saleReportStepOfFirst($data,$user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
+        $result = $this->saleReportStepOfFirstStart($return,$user);
+
+        return [true, ['list' => $result]];
+    }
+
+    private function saleReportStepOfFirstStart($time,$user){
+        $result = $type = [];
+        foreach (SalesOrderReport::$all_list as $key => $value){
+            $result[] = [
+                'label' => $value,
+                'value' => $key,
+                'all' => ""
+            ];
+            $type[$key] = 0;
+        }
+
+        $setting = Setting::where('setting_name','bt_top_depart_id')->first();
+        $bt_top_depart_id = $setting['setting_value'] ?? [];
+        $bt_top_depart_id = json_decode($bt_top_depart_id,true);
+
+        $id = [];
+        $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
+        foreach ($depart_list as $value){
+            if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
+                $id[] = $value['id'];
+            }
+        }
+        // 查找所有部门id
+        $all_depart_id = [];
+        foreach ($id as $value){
+            $childIds = $this->findChildIds($value, $depart_list);
+            $all_depart_id_tmp = array_merge($childIds, [$value]);
+            $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
+        }
+        // 获取部门下的人
+        $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
+            ->select('employee_id')
+            ->get()->toArray();
+        $employee = array_unique(array_column($employee,'employee_id'));
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->get()->toArray();
+
+        $head = $user['head']['id'] ?? 0;
+
+        foreach ($list as $value){
+            //门店零售
+            $type[SalesOrderReport::type_one] = bcadd($type[SalesOrderReport::type_one], $value['contract_fee'],2);
+            //私域营销
+            $bool = in_array($value['top_depart_id'], $bt_top_depart_id);
+            if($bool && in_array($value['crt_id'], $employee) && $value['model_type'] == SalesOrder::Model_type_one) $type[SalesOrderReport::type_two] = bcadd($type[SalesOrderReport::type_two], $value['contract_fee'],2);
+            //线上销售
+            if($value['top_depart_id'] == $head && $value['model_type'] == SalesOrder::Model_type_four) $type[SalesOrderReport::type_three] = bcadd($type[SalesOrderReport::type_three], $value['contract_fee'],2);
+            //门店业绩
+            if($value['from_top_depart_id'] > 0 && $value['model_type'] == SalesOrder::Model_type_two) $type[SalesOrderReport::type_four] = bcadd($type[SalesOrderReport::type_four], $value['contract_fee'],2);
+        }
+
+        //客资业绩
+        $start_time = strtotime(date("Y-m-01 00:00:00", $start));
+        $type_five = CustomerPerFormance::where('del_time',0)
+            ->where('crt_time','>=', $start_time)
+            ->where('crt_time','<=', $end)
+            ->select(DB::raw('sum(current_month_payment) as current_month_payment'))
+            ->first()->toArray();
+        $type[SalesOrderReport::type_five] = $type_five['current_month_payment'] ?? 0;
+
+        foreach ($result as $key => $value){
+            if(isset($type[$value['value']])){
+                $result[$key]['all'] = $type[$value['value']];
+            }
+        }
+
+        return $result;
+    }
+
+    public function saleReportStepOfSecond($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        $type = $data['type'];
+        if($type == 1){
+            $result = $this->saleReportStepOfSecondTypeOne($return);
+        }elseif ($type == 2){
+            $result = $this->saleReportStepOfSecondTypeTwo($return);
+        }elseif ($type == 3){
+            $result = $this->saleReportStepOfSecondTypeThree($return, $user);
+        }elseif ($type == 4){
+            $result = $this->saleReportStepOfSecondTypeFour($return, $user);
+        }elseif ($type == 5){
+            $result = $this->saleReportStepOfSecondTypeFive($return);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function saleReportStepOfSecondTypeOne($time){
+        //大区
+        $area = [];
+        foreach (Depart::$area as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'receipt' => 0,
+                'all' => 0
+            ];
+        }
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'receipt' => 0,
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['area']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
+                $map[$value['area']]['num_1'] = $tmp;
+                $map[$value['area']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['area']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+
+            $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
+            $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
+            $total['receipt'] = $tmp_t;
+            $total['all'] = $tmp_t1;
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['receipt'] = $tmp['num_1'];
+                $area[$key]['all'] = $tmp['num_2'];
+            }
+        }
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+        array_unshift($area, $total);
+
+        return $area;
+    }
+
+    private function saleReportStepOfSecondTypeTwo($time){
+        $setting = Setting::where('setting_name','bt_top_depart_id')->first();
+        $bt_top_depart_id = $setting['setting_value'] ?? [];
+        $bt_top_depart_id = json_decode($bt_top_depart_id,true);
+
+        $id = [];
+        $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
+        foreach ($depart_list as $value){
+            if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
+                $id[] = $value['id'];
+            }
+        }
+        // 查找所有部门id
+        $all_depart_id = [];
+        foreach ($id as $value){
+            $childIds = $this->findChildIds($value, $depart_list);
+            $all_depart_id_tmp = array_merge($childIds, [$value]);
+            $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
+        }
+        // 获取部门下的人
+        $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
+            ->select('employee_id')
+            ->get()->toArray();
+        $employee = array_unique(array_column($employee,'employee_id'));
+
+        $empList = Employee::whereIn('id', $employee)
+            ->select('id','emp_name as title')
+            ->get()->toArray();
+
+        $man = [];
+        foreach ($empList as $value){
+            $man[] = [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'receipt' => 0,
+                'all' => 0,
+                'goal' => 0,
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->whereIn('top_depart_id', $bt_top_depart_id)
+            ->where('model_type',SalesOrder::Model_type_one)
+            ->whereIn('crt_id',$employee)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['crt_id']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
+                $map[$value['crt_id']]['num_1'] = $tmp;
+                $map[$value['crt_id']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['crt_id']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+        }
+
+        $start_time = strtotime(date("Y-m-01 00:00:00", $start));
+        $month = MonthlyPerFormance::where('del_time',0)
+            ->where('crt_time',">=",$start_time)
+            ->where('crt_time','<=',$end)
+            ->select('employee_id','sales_index')
+            ->get()->toArray();
+        $month_map = [];
+        foreach ($month as $value){
+            $month_map[$value['employee_id']] = $value['sales_index'];
+        }
+
+        foreach ($man as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $man[$key]['receipt'] = $tmp['num_1'];
+                $man[$key]['all'] = $tmp['num_2'];
+            }
+            $man[$key]['goal'] = $month_map[$value['value']] ?? 0;
+        }
+
+        usort($man, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+
+        return $man;
+    }
+
+    private function saleReportStepOfSecondTypeThree($time, $user){
+        $head = $user['head']['id'] ?? 0;
+        $list = BasicType::where('del_time',0)
+            ->where('type',24)
+            ->where('top_depart_id', $head)
+            ->select('title')
+            ->get()->toArray();
+
+        //大区
+        $area = [];
+        foreach ($list as $value){
+            $area[] = [
+                'label' => $value['title'],
+                'value' => $value['title'],
+                'all' => 0
+            ];
+        }
+        $label = "无平台来源";
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('model_type', SalesOrder::Model_type_four)
+            ->where('top_depart_id', $head)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(empty($value['plat_type_title'])) $value['plat_type_title'] = $label;
+            if(isset($map[$value['plat_type_title']])) {
+                $tmp = bcadd($value['contract_fee'], $map[$value['plat_type_title']]['num_1']);
+                $map[$value['plat_type_title']]['num_1'] = $tmp;
+            }else{
+                $map[$value['plat_type_title']] = [
+                    'num_1' => $value['contract_fee'],
+                ];
+            }
+        }
+        if(isset($map[$label])){
+            $area[] = [
+                'label' => $label,
+                'value' => $label,
+                'all' => 0
+            ];
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['all'] = $tmp['num_1'];
+            }
+        }
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+        });
+
+
+        return $area;
+    }
+
+    private function saleReportStepOfSecondTypeFour($time, $user){
+        $head = $user['head']['id'] ?? 0;
+
+        //大区
+        $area = [];
+        foreach (Depart::$area as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'receipt' => 0,
+                'all' => 0
+            ];
+        }
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'receipt' => 0,
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('model_type', SalesOrder::Model_type_two)
+            ->where('from_top_depart_id', '>', 0)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['area']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
+                $map[$value['area']]['num_1'] = $tmp;
+                $map[$value['area']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['area']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+
+            $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
+            $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
+            $total['receipt'] = $tmp_t;
+            $total['all'] = $tmp_t1;
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['receipt'] = $tmp['num_1'];
+                $area[$key]['all'] = $tmp['num_2'];
+            }
+        }
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+        array_unshift($area, $total);
+
+        return $area;
+    }
+
+    private function saleReportStepOfSecondTypeFive($time){
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $start_time = strtotime(date("Y-m-01 00:00:00", $start));
+        $list = CustomerPerFormance::where('del_time',0)
+            ->where('crt_time','>=', $start_time)
+            ->where('crt_time','<=', $end)
+            ->get()->toArray();
+
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['area']])) {
+                $tmp = bcadd($value['current_month_payment'], $map[$value['area']]['all']);
+                $map[$value['area']]['all'] = $tmp;
+            }else{
+                $map[$value['area']] = [
+                    'label' => $value['area'],
+                    'value' => $value['area'],
+                    'all' => $value['current_month_payment']
+                ];
+            }
+
+            $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
+            $total['all'] = $tmp_t1;
+        }
+
+        $map = array_values($map);
+
+        usort($map, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+        });
+
+        array_unshift($map, $total);
+
+        return $map;
+    }
+
+    public function saleReportStepOfThird($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        if(empty($data['value'])) return [false, 'value不能为空'];
+        $type = $data['type'];
+        if($type == 1){
+            $result = $this->saleReportStepOfThirdTypeOne($return, $data);
+        }elseif ($type == 2){
+            $result = $this->saleReportStepOfThirdTypeTwo($return, $data);
+        }elseif ($type == 3){
+            $result = $this->saleReportStepOfThirdTypeThree($return, $data, $user);
+        }elseif ($type == 4){
+            $result = $this->saleReportStepOfThirdTypeFour($return, $data, $user);
+        }elseif ($type == 5){
+            $result = $this->saleReportStepOfThirdTypeFive($return,$data);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function saleReportStepOfThirdTypeOne($time,$data){
+        $area_value = $data['value'];
+
+        $depart = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->where('area',$area_value)
+            ->select('id','title')
+            ->get()->toArray();
+        //大区
+        $area = [];
+        foreach ($depart as $value){
+            $area[] = [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'receipt' => 0,
+                'all' => 0
+            ];
+        }
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'receipt' => 0,
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('area',$area_value)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['top_depart_id']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['top_depart_id']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['top_depart_id']]['num_2']);
+                $map[$value['top_depart_id']]['num_1'] = $tmp;
+                $map[$value['top_depart_id']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['top_depart_id']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+
+            $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
+            $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
+            $total['receipt'] = $tmp_t;
+            $total['all'] = $tmp_t1;
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['receipt'] = $tmp['num_1'];
+                $area[$key]['all'] = $tmp['num_2'];
+            }
+        }
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+        array_unshift($area, $total);
+
+        return $area;
+    }
+
+    private function saleReportStepOfThirdTypeTwo($time,$data){
+        $man_value = $data['value'];
+
+        $area = [];
+        foreach (SalesOrderReport::$all_state_list as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => []
+            ];
+        }
+
+        $setting = Setting::where('setting_name','bt_top_depart_id')->first();
+        $bt_top_depart_id = $setting['setting_value'] ?? [];
+        $bt_top_depart_id = json_decode($bt_top_depart_id,true);
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->whereIn('top_depart_id', $bt_top_depart_id)
+            ->where('model_type',SalesOrder::Model_type_one)
+            ->where('crt_id', $man_value)
+            ->orderBy('crt_time','desc')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
+            $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
+            $tmp = [
+                'crt_time' => $crt_time,
+                'model_type_title' => $model_type_title,
+                'customer' => $value['customer_title'],
+                'contact_product' => $value['contact_product'],
+                'fz' => $value['fz_man'],
+                'contract_fee' => $value['contract_fee'],
+            ];
+            $map[SalesOrderReport::state_zero][] = $tmp;
+
+            if($value['state'] == 1){
+                $map[SalesOrderReport::state_one][] = $tmp;
+            }elseif ($value['state'] == 2){
+                $map[SalesOrderReport::state_two][] = $tmp;
+            }elseif ($value['state'] == 3){
+                $map[SalesOrderReport::state_three][] = $tmp;
+            }elseif ($value['state'] == 4){
+                $map[SalesOrderReport::state_four][] = $tmp;
+            }
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['list'] = $tmp;
+                $area[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $area;
+    }
+
+    private function saleReportStepOfThirdTypeThree($time,$data, $user){
+        $head = $user['head']['id'] ?? 0;
+        $basic_value = $data['value'];
+
+        $area = [];
+        foreach (SalesOrderReport::$all_state_list as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => []
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('model_type', SalesOrder::Model_type_four)
+            ->where('top_depart_id', $head)
+            ->where('plat_type_title', $basic_value)
+            ->orderBy('crt_time','desc')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
+            $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
+            $tmp = [
+                'crt_time' => $crt_time,
+                'model_type_title' => $model_type_title,
+                'customer' => $value['customer_title'],
+                'contact_product' => $value['contact_product'],
+                'fz' => $value['fz_man'],
+                'contract_fee' => $value['contract_fee'],
+            ];
+            $map[SalesOrderReport::state_zero][] = $tmp;
+
+            if($value['state'] == 1){
+                $map[SalesOrderReport::state_one][] = $tmp;
+            }elseif ($value['state'] == 2){
+                $map[SalesOrderReport::state_two][] = $tmp;
+            }elseif ($value['state'] == 3){
+                $map[SalesOrderReport::state_three][] = $tmp;
+            }elseif ($value['state'] == 4){
+                $map[SalesOrderReport::state_four][] = $tmp;
+            }
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['list'] = $tmp;
+                $area[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $area;
+    }
+
+    private function saleReportStepOfThirdTypeFour($time,$data, $user){
+        $head = $user['head']['id'] ?? 0;
+        $area_value = $data['value'];
+
+        $depart = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->where('area', $area_value)
+            ->select('id','title')
+            ->get()->toArray();
+        //大区
+        $area = [];
+        foreach ($depart as $value){
+            $area[] = [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'receipt' => 0,
+                'all' => 0
+            ];
+        }
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'receipt' => 0,
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('area',$area_value)
+            ->where('model_type', SalesOrder::Model_type_two)
+            ->where('from_top_depart_id', '>',0)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['top_depart_id']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['top_depart_id']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['top_depart_id']]['num_2']);
+                $map[$value['top_depart_id']]['num_1'] = $tmp;
+                $map[$value['top_depart_id']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['top_depart_id']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+
+            $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
+            $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
+            $total['receipt'] = $tmp_t;
+            $total['all'] = $tmp_t1;
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['receipt'] = $tmp['num_1'];
+                $area[$key]['all'] = $tmp['num_2'];
+            }
+        }
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+        array_unshift($area, $total);
+
+        return $area;
+    }
+
+    private function saleReportStepOfThreeTypeFive($time,$data){
+        $area_value = $data['value'];
+
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'all' => 0
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $start_time = strtotime(date("Y-m-01 00:00:00", $start));
+        $list = CustomerPerFormance::where('del_time',0)
+            ->where('crt_time','>=', $start_time)
+            ->where('crt_time','<=', $end)
+            ->where('area', $area_value)
+            ->get()->toArray();
+
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['title']])) {
+                $tmp = bcadd($value['current_month_payment'], $map[$value['title']]['all']);
+                $map[$value['title']]['all'] = $tmp;
+            }else{
+                $map[$value['title']] = [
+                    'label' => $value['title'],
+                    'value' => $value['title'],
+                    'all' => $value['current_month_payment']
+                ];
+            }
+
+            $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
+            $total['all'] = $tmp_t1;
+        }
+
+        $map = array_values($map);
+
+        usort($map, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all 降序
+            }
+        });
+
+        array_unshift($map, $total);
+
+        return $map;
+    }
+
+    public function saleReportStepOfFour($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        if(empty($data['value'])) return [false, 'value不能为空'];
+        if(empty($data['value2'])) return [false, 'value2不能为空'];
+        $type = $data['type'];
+        if($type == 1){
+            $result = $this->saleReportStepOfFourTypeOne($return, $data);
+        }elseif ($type == 4){
+            $result = $this->saleReportStepOfFourTypeFour($return, $data, $user);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function saleReportStepOfFourTypeOne($time,$data){
+        $area_value = $data['value'];
+        $depart_value = $data['value2'];
+
+        $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
+        // 查找所有部门id
+        $childIds = $this->findChildIds($depart_value, $depart_list);
+        $all_depart_id = array_merge($childIds, [$depart_value]);
+
+        // 获取部门下的人
+        $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
+            ->select('employee_id')
+            ->get()->toArray();
+        $employee = array_unique(array_column($employee,'employee_id'));
+
+        $empMap = Employee::whereIn('id', $employee)
+            ->pluck('emp_name as title','id')
+            ->toArray();
+        $area = [];
+        foreach ($empMap as $key => $value){
+            $area[$key] = [
+                'label' => $value,
+                'value' => $key,
+                'receipt' => 0,
+                'all' => 0,
+                'goal' => 0,
+            ];
+        }
+
+        $label = "全部";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'receipt' => 0,
+            'all' => 0,
+            'goal' => 0,
+        ];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('area',$area_value)
+            ->where('top_depart_id',$depart_value)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['crt_id']])) {
+                $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
+                $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
+                $map[$value['crt_id']]['num_1'] = $tmp;
+                $map[$value['crt_id']]['num_2'] = $tmp2;
+            }else{
+                $map[$value['crt_id']] = [
+                    'num_1' => $value['receipt'],
+                    'num_2' => $value['contract_fee'],
+                ];
+            }
+
+            $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
+            $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
+            $total['receipt'] = $tmp_t;
+            $total['all'] = $tmp_t1;
+        }
+
+        $start_time = strtotime(date("Y-m-01 00:00:00", $start));
+        $month = MonthlyPerFormance::where('del_time',0)
+            ->where('crt_time',">=",$start_time)
+            ->where('crt_time','<=',$end)
+            ->select('employee_id','sales_index')
+            ->get()->toArray();
+        $month_map = [];
+        foreach ($month as $value){
+            $month_map[$value['employee_id']] = $value['sales_index'];
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['receipt'] = $tmp['num_1'];
+                $area[$key]['all'] = $tmp['num_2'];
+                $goal = $month_map[$value['value']] ?? 0;
+                $area[$key]['goal'] = $goal;
+                $total['goal'] = bcadd($goal, $total['goal'],2);
+            }else{
+                unset($area[$key]);
+            }
+        }
+
+        $area = array_values($area);
+
+        usort($area, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all'] !== $a['all']) {
+                return $b['all'] - $a['all']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['receipt'] - $b['receipt']; // value 正序
+        });
+
+        array_unshift($area, $total);
+
+        return $area;
+    }
+
+    private function saleReportStepOfFourTypeFour($time,$data, $user){
+        $head = $user['head']['id'] ?? 0;
+        $area_value = $data['value'];
+        $depart_value = $data['value2'];
+
+        $area = [];
+        foreach (SalesOrderReport::$all_state_list as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => []
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('area',$area_value)
+            ->where('model_type', SalesOrder::Model_type_two)
+            ->where('from_top_depart_id', $depart_value)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
+            $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
+            $tmp = [
+                'crt_time' => $crt_time,
+                'model_type_title' => $model_type_title,
+                'customer' => $value['customer_title'],
+                'contact_product' => $value['contact_product'],
+                'fz' => $value['fz_man'],
+                'contract_fee' => $value['contract_fee'],
+            ];
+            $map[SalesOrderReport::state_zero][] = $tmp;
+
+            if($value['state'] == 1){
+                $map[SalesOrderReport::state_one][] = $tmp;
+            }elseif ($value['state'] == 2){
+                $map[SalesOrderReport::state_two][] = $tmp;
+            }elseif ($value['state'] == 3){
+                $map[SalesOrderReport::state_three][] = $tmp;
+            }elseif ($value['state'] == 4){
+                $map[SalesOrderReport::state_four][] = $tmp;
+            }
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['list'] = $tmp;
+                $area[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $area;
+    }
+
+    public function saleReportStepOfFive($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        list($status, $msg) = $this->aboutTime($return);
+        if(! $status) return [false, $msg];
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        if(empty($data['value'])) return [false, 'value不能为空'];
+        if(empty($data['value2'])) return [false, 'value2不能为空'];
+        if(empty($data['value3'])) return [false, 'value3不能为空'];
+        $type = $data['type'];
+        if($type == 1){
+            $result = $this->saleReportStepOfFiveTypeOne($return, $data);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function saleReportStepOfFiveTypeOne($time,$data){
+        $area_value = $data['value'];
+        $depart_value = $data['value2'];
+        $man_value = $data['value3'];
+
+        $area = [];
+        foreach (SalesOrderReport::$all_state_list as $key => $value){
+            $area[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => []
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = SalesOrderReport::where('del_time',0)
+            ->where('crt_time','>=', $start)
+            ->where('crt_time','<=', $end)
+            ->where('area',$area_value)
+            ->where('top_depart_id',$depart_value)
+            ->where('crt_id', $man_value)
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
+            $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
+            $tmp = [
+                'crt_time' => $crt_time,
+                'model_type_title' => $model_type_title,
+                'customer' => $value['customer_title'],
+                'contact_product' => $value['contact_product'],
+                'fz' => $value['fz_man'],
+                'contract_fee' => $value['contract_fee'],
+            ];
+            $map[SalesOrderReport::state_zero][] = $tmp;
+
+            if($value['state'] == 1){
+                $map[SalesOrderReport::state_one][] = $tmp;
+            }elseif ($value['state'] == 2){
+                $map[SalesOrderReport::state_two][] = $tmp;
+            }elseif ($value['state'] == 3){
+                $map[SalesOrderReport::state_three][] = $tmp;
+            }elseif ($value['state'] == 4){
+                $map[SalesOrderReport::state_four][] = $tmp;
+            }
+        }
+
+        foreach ($area as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $area[$key]['list'] = $tmp;
+                $area[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $area;
+    }
 }

+ 15 - 0
config/excel/customerPerformanceTable.php

@@ -0,0 +1,15 @@
+<?php
+return [
+    [
+        'key' =>'area',
+        'value' =>'* 大区',
+    ],
+    [
+        'key' =>'title',
+        'value' =>'* 客户名称',
+    ],
+    [
+        'key' =>'current_month_payment',
+        'value' => '* 当月打款金额',
+    ],
+];

+ 7 - 0
routes/api.php

@@ -330,6 +330,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     //设置列表
     $route->any('productInventorySetList', 'Api\ProductInventoryController@productInventorySetList');
     $route->any('getMySetting', 'Api\ProductInventoryController@getMySetting');
+    $route->any('manSettingMonthlyPerformance', 'Api\ProductInventoryController@manSetting');
 
     //统计
     $route->any('statisticsBt', 'Api\StatisticsController@statisticsBt');
@@ -345,6 +346,12 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('customerReportStepOfFirst', 'Api\StatisticsController@customerReportStepOfFirst');
     $route->any('customerReportStepOfSecond', 'Api\StatisticsController@customerReportStepOfSecond');
     $route->any('customerReportStepOfThird', 'Api\StatisticsController@customerReportStepOfThird');
+    //销售报表
+    $route->any('saleReportStepOfFirst', 'Api\StatisticsController@saleReportStepOfFirst');
+    $route->any('saleReportStepOfSecond', 'Api\StatisticsController@saleReportStepOfSecond');
+    $route->any('saleReportStepOfThird', 'Api\StatisticsController@saleReportStepOfThird');
+    $route->any('saleReportStepOfFour', 'Api\StatisticsController@saleReportStepOfFour');
+    $route->any('saleReportStepOfFive', 'Api\StatisticsController@saleReportStepOfFive');
 
     //设置开关
     $route->any('productInventorySet', 'Api\ProductInventoryController@productInventorySet');

+ 7 - 0
routes/wx.php

@@ -156,4 +156,11 @@ Route::group(['middleware'=> ['checkWx']],function ($route){
     $route->any('customerReportStepOfFirst', 'Api\StatisticsController@customerReportStepOfFirst');
     $route->any('customerReportStepOfSecond', 'Api\StatisticsController@customerReportStepOfSecond');
     $route->any('customerReportStepOfThird', 'Api\StatisticsController@customerReportStepOfThird');
+
+    //销售报表
+    $route->any('saleReportStepOfFirst', 'Api\StatisticsController@saleReportStepOfFirst');
+    $route->any('saleReportStepOfSecond', 'Api\StatisticsController@saleReportStepOfSecond');
+    $route->any('saleReportStepOfThird', 'Api\StatisticsController@saleReportStepOfThird');
+    $route->any('saleReportStepOfFour', 'Api\StatisticsController@saleReportStepOfFour');
+    $route->any('saleReportStepOfFive', 'Api\StatisticsController@saleReportStepOfFive');
 });