cqp 4 viikkoa sitten
vanhempi
sitoutus
ab098ea187

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

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

+ 4 - 1
app/Model/RevenueCost.php

@@ -24,5 +24,8 @@ class RevenueCost extends Model
 
     const job = 'yf_revenue_cost';
 
-    public static $field = ['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','order_time'];
+    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 = ['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 = ['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 = ['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'];
 }

+ 16 - 0
app/Model/RevenueCostTotal.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class RevenueCostTotal extends Model
+{
+    protected $guarded = [];
+    protected $table = "revenue_cost_total"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+    public static $field = ['order_type','profit','profit_rate','order_time','income','adjust','business','employee_id_1','employee_id_1_title'];
+}

+ 1 - 1
app/Service/Service.php

@@ -27,7 +27,7 @@ class Service
     //分页共用
     public function limit($db, $columns, $request)
     {
-        $per_page = $request['page_size'] ?? 9999;
+        $per_page = $request['page_size'] ?? 100;
         $page = $request['page_index'] ?? 1;
         $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
 

+ 75 - 14
app/Service/StatisticsService.php

@@ -3,22 +3,22 @@
 namespace App\Service;
 
 use App\Model\RevenueCost;
+use App\Model\RevenueCostTotal;
 
 class StatisticsService extends Service
 {
     public function statisticsRevenueCostCommon($data,$user, $field = []){
-        if(empty($data['order_time']) || ! is_array($data['order_time'])) return [false, '单据日期不能为空'];
-        list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
-        if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
-        list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
-        if(! $bool) return [false, $bool_msg];
-
-        if(empty($field)) $field = RevenueCost::$field;
-        $model = RevenueCost::where('del_time',0)
+        if(empty($field)) $field = RevenueCostTotal::$field;
+        $model = RevenueCostTotal::where('del_time',0)
             ->select($field)
             ->orderby('id', 'desc');
-        $model->where('order_time', '>=', $start_time)
-            ->where('order_time', '<', $end_time);
+        if(! empty($data['order_time'])){
+            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('order_time', '>=', $start_time)
+                ->where('order_time', '<', $end_time);
+        }
+        if(! empty($data['order_type'])) $model->where('order_type',$data['order_type']);
 
         return [true, $model];
     }
@@ -26,13 +26,27 @@ class StatisticsService extends Service
     public function statisticsRevenueCost($data,$user){
         list($status, $model) = $this->statisticsRevenueCostCommon($data, $user);
         if(! $status) return [false, $model];
-        $result = $model->get()->toArray();
-        $return = $this->statisticsRevenueCostFillData($result);
+        $list = $this->limit($model,'',$data);
+        $list = $this->statisticsRevenueCostFillData($list);
 
-        return [true, $return];
+        return [true, $list];
     }
 
     public function statisticsRevenueCostFillData($data){
+        if(empty($data['data'])) return $data;
+
+        foreach ($data['data'] as $key => $value){
+            $time = date("Y-m", $value['order_time']);
+            $data['data'][$key]['order_time'] = $time;
+            $data['data'][$key]['order_type_title'] = RevenueCost::$order_type[$value['order_type']] ?? "";
+            $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
+        }
+
+        return $data;
+    }
+
+    //无用
+    public function statisticsRevenueCostFillData1($data){
         if(empty($data)) return $data;
 
         $return = [];
@@ -56,7 +70,7 @@ class StatisticsService extends Service
                 $return[$time][$value['order_type']]['business'] = $business_total;
             }else{
                 $return[$time][$value['order_type']] = [
-                    'income' => $value['price_3_total'],
+                    'income' => $income,
                     'adjust' => $adjust,
                     'business' => $business,
                     'time' => $time,
@@ -70,6 +84,7 @@ class StatisticsService extends Service
                 $val['title'] = $title;
                 $profit = bcsub($val['income'], $val['business'],2);
                 $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0;
+                $profit_rate = bcmul($profit_rate, 100,2);
                 $val['profit'] = $profit;
                 $val['profit_rate'] = $profit_rate;
                 $val['order_type'] = $key;
@@ -79,4 +94,50 @@ class StatisticsService extends Service
 
         return $final;
     }
+
+    public function statisticsRevenueCostOneAndTwoCommon($data,$user, $field = []){
+        if(empty($data['order_type']) || ! isset(RevenueCost::$order_type[$data['order_type']])) return [false, '单据类型不存在或错误'];
+
+        if(empty($field)) {
+            if($data['order_type'] == 1){
+                $field = RevenueCost::$field_xhd;
+            }else{
+                $field = RevenueCost::$field_xsfp;
+            }
+        }
+        $model = RevenueCost::where('del_time',0)
+            ->select($field)
+            ->orderby('id', 'desc');
+        if(! empty($data['order_time'])){
+            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('order_time', '>=', $start_time)
+                ->where('order_time', '<=', $end_time);
+        }
+        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
+        if(! empty($data['customer_title'])) $model->where('customer_title', 'LIKE', '%'.$data['customer_title'].'%');
+        if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
+
+        return [true, $model];
+    }
+
+    public function statisticsRevenueCostOneAndTwo($data,$user){
+        list($status, $model) = $this->statisticsRevenueCostOneAndTwoCommon($data, $user);
+        if(! $status) return [false, $model];
+        $list = $this->limit($model,'',$data);
+        $list = $this->statisticsRevenueCostOneAndTwoFillData($list,$user,$data);
+
+        return [true, $list];
+    }
+
+    public function statisticsRevenueCostOneAndTwoFillData($data, $user, $ergs){
+        if(empty($data['data'])) return $data;
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
+            $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
+        }
+
+        return $data;
+    }
 }

+ 92 - 3
app/Service/TPlusServerService.php

@@ -8,6 +8,7 @@ use App\Model\Employee;
 use App\Model\EmployeeDepartPermission;
 use App\Model\Product;
 use App\Model\RevenueCost;
+use App\Model\RevenueCostTotal;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Schema;
@@ -223,10 +224,14 @@ class TPlusServerService extends Service
             if(! $status) return [false, $msg];
         }
 
-        //更新主表数据
+        //更新数据
         list($status,$msg) = $this->updateRevenueCost($data);
         if(! $status) return [false, $msg];
 
+//        //更新主表数据
+//        list($status,$msg) = $this->updateRevenueCostTotal($data);
+//        if(! $status) return [false, $msg];
+
         //都成功后 清理临时表
         $this->clearTmpTable();
         //释放redis
@@ -492,8 +497,9 @@ class TPlusServerService extends Service
             $end_timeStamp = $data['end_timeStamp'];
             $tmpTable = $this->table;
             $time = time();
+            $ergs = $data;
 
-            DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time) {
+            DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
                 // 1. 先软删除旧数据(你已有)
                 RevenueCost::where('del_time', 0)
                     ->where('order_time', '>=', $start_timeStamp)
@@ -503,7 +509,6 @@ class TPlusServerService extends Service
                 // 2. 分批从临时表插入新数据
                 $batchSize = 500;
                 $lastId = 0;
-
                 do {
                     $chunk = DB::table($tmpTable)
                         ->where('id', '>', $lastId)
@@ -558,7 +563,91 @@ class TPlusServerService extends Service
                     $lastId = $chunk->last()->id;
 
                 } while ($chunk->count() == $batchSize);
+
+                // 3. 更新主表
+                list($status, $msg) = $this->updateRevenueCostTotal($ergs);
+                if (! $status) {
+                    throw new \Exception($msg);
+                }
             });
+        }catch  (\Throwable $exception){
+            return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
+        }
+
+        return [true, ''];
+    }
+
+    private function updateRevenueCostTotal($data){
+        try {
+            $start_timeStamp = $data['start_timeStamp'];
+            $end_timeStamp = $data['end_timeStamp'];
+            $time = time();
+
+            //组织写入数据
+            $return = [];
+            $update_stamp = [];
+            DB::table('revenue_cost')
+                ->where('del_time',0)
+                ->where('order_time', '>=', $start_timeStamp)
+                ->where('order_time', '<=', $end_timeStamp)
+                ->select(RevenueCost::$field)
+                ->chunkById(100, function ($data) use(&$return,&$update_stamp){
+                    $dataArray = Collect($data)->map(function ($object){
+                        return (array)$object;
+                    })->toArray();
+                    foreach ($dataArray as $value){
+                        //变成每个月第一天的时间戳
+                        $time = date("Y-m-01", $value['order_time']);
+                        $stamp = strtotime($time);
+                        if(! in_array($stamp, $update_stamp)) $update_stamp[] = $stamp;
+                        if($value['order_type'] == RevenueCost::ORDER_ONE){
+                            $income = $value['price_3_total'];
+                        }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
+                            $income = $value['price_1_total'];
+                        }else{
+                            $income = $value['payment_amount'];
+                        }
+                        $adjust = $income < 0 ? $income : 0;
+                        $business = $value['price_4_total'];
+                        if(isset($return[$stamp][$value['order_type']][$value['employee_id_1']])){
+                            $income_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['income'], $income,2);
+                            $adjust_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'], $adjust,2);
+                            $business_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['business'], $business,2);
+                            $return[$stamp][$value['order_type']][$value['employee_id_1']]['income'] = $income_total;
+                            $return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'] = $adjust_total;
+                            $return[$stamp][$value['order_type']][$value['employee_id_1']]['business'] = $business_total;
+                        }else{
+                            $return[$stamp][$value['order_type']][$value['employee_id_1']] = [
+                                'income' => $income,
+                                'adjust' => $adjust,
+                                'business' => $business,
+                                'order_time' => $stamp,
+                                'employee_id_1_title' => $value['employee_id_1_title'],
+                            ];
+                        }
+                    }
+                });
+            $insert = [];
+            foreach ($return as $value){
+                foreach ($value as $order_type => $val){
+                   foreach ($val as $employee_id => $v){
+                        $profit = bcsub($v['income'], $v['business'],2);
+                        $profit_rate = $v['income'] > 0 ? bcdiv($profit, $v['income'],2) : 0;
+                        $v['profit'] = $profit;
+                        $v['profit_rate'] = $profit_rate;
+                        $v['order_type'] = $order_type;
+                        $v['employee_id_1'] = $employee_id;
+                        $v['crt_time'] = $time;
+                        $insert[] = $v;
+                    }
+                }
+            }
+
+            RevenueCostTotal::where('del_time', 0)
+                ->whereIn('order_time', $update_stamp)
+                ->update(['del_time' => $time]);
+
+            RevenueCostTotal::insert($insert);
         }catch  (\Throwable $exception){
             return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
         }

+ 9 - 5
config/header/68.php

@@ -7,9 +7,17 @@
 
 return [
     [
-        'key' =>'title',
+        'key' =>'order_type_title',
         'value' => '单据类型',
     ],
+    [
+        'key' =>'employee_id_1_title',
+        'value' => '业务员',
+    ],
+    [
+        'key' =>'order_time',
+        'value' => '单据年月',
+    ],
     [
         'key' =>'income',
         'value' => '收入金额',
@@ -30,8 +38,4 @@ return [
         'key' =>'profit_rate',
         'value' => '毛利率',
     ],
-    [
-        'key' =>'time',
-        'value' => '单据年月',
-    ],
 ];

+ 101 - 0
config/header/69.php

@@ -0,0 +1,101 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' =>'order_number',
+        'value' => '销货单号',
+    ],
+    [
+        'key' =>'order_time',
+        'value' => '单据日期',
+    ],
+    [
+        'key' =>'employee_id_1_title',
+        'value' => '业务员',
+    ],
+    [
+        'key' =>'employee_id_2_title',
+        'value' => '上级管理人员',
+    ],
+    [
+        'key' =>'customer_code',
+        'value' => '结算客户编码',
+    ],
+    [
+        'key' =>'customer_title',
+        'value' => '结算客户',
+    ],
+    [
+        'key' =>'channel_finance',
+        'value' => '渠道财务',
+    ],
+    [
+        'key' =>'channel_details',
+        'value' => '渠道明细',
+    ],
+    [
+        'key' =>'product_code',
+        'value' => '存货编码',
+    ],
+    [
+        'key' =>'product_title',
+        'value' => '存货名称',
+    ],
+    [
+        'key' =>'size',
+        'value' => '规格型号',
+    ],
+    [
+        'key' =>'unit',
+        'value' => '单位',
+    ],
+    [
+        'key' =>'price_1',
+        'value' => '核销单价',
+    ],
+    [
+        'key' =>'price_1_total',
+        'value' => '核销总价',
+    ],
+    [
+        'key' =>'price_2',
+        'value' => '运费单价',
+    ],
+    [
+        'key' =>'price_2_total',
+        'value' => '运费总价',
+    ],
+    [
+        'key' =>'quantity',
+        'value' => '销货数量',
+    ],
+    [
+        'key' =>'price_3',
+        'value' => '含税单价',
+    ],
+    [
+        'key' =>'price_3_total',
+        'value' => '含税金额',
+    ],
+    [
+        'key' =>'price_4',
+        'value' => '业务成本单价',
+    ],
+    [
+        'key' =>'price_4_total',
+        'value' => '业务成本金额',
+    ],
+    [
+        'key' =>'profit',
+        'value' => '毛利',
+    ],
+    [
+        'key' =>'profit_rate',
+        'value' => '毛利率',
+    ],
+];

+ 77 - 0
config/header/70.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' =>'order_number',
+        'value' => '销售发票号',
+    ],
+    [
+        'key' =>'order_time',
+        'value' => '单据日期',
+    ],
+    [
+        'key' =>'employee_id_1_title',
+        'value' => '业务员',
+    ],
+    [
+        'key' =>'employee_id_2_title',
+        'value' => '上级管理人员',
+    ],
+    [
+        'key' =>'customer_code',
+        'value' => '结算客户编码',
+    ],
+    [
+        'key' =>'customer_title',
+        'value' => '结算客户',
+    ],
+    [
+        'key' =>'product_code',
+        'value' => '存货编码',
+    ],
+    [
+        'key' =>'product_title',
+        'value' => '存货名称',
+    ],
+    [
+        'key' =>'size',
+        'value' => '规格型号',
+    ],
+    [
+        'key' =>'unit',
+        'value' => '单位',
+    ],
+    [
+        'key' =>'quantity',
+        'value' => '开票数量',
+    ],
+    [
+        'key' =>'price_1',
+        'value' => '单价',
+    ],
+    [
+        'key' =>'price_1_total',
+        'value' => '开票金额',
+    ],
+    [
+        'key' =>'price_4',
+        'value' => '业务成本单价',
+    ],
+    [
+        'key' =>'price_4_total',
+        'value' => '业务成本金额',
+    ],
+    [
+        'key' =>'profit',
+        'value' => '毛利',
+    ],
+    [
+        'key' =>'profit_rate',
+        'value' => '毛利率',
+    ],
+];

+ 2 - 1
routes/api.php

@@ -128,6 +128,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('synPersonDepart', 'Api\TPlusController@synPersonDepart');
     $route->any('synRevenueCost', 'Api\TPlusController@synRevenueCost');
 
-    //
+    //收入成本
     $route->any('statisticsRevenueCost', 'Api\StatisticsController@statisticsRevenueCost');
+    $route->any('statisticsRevenueCostOneAndTwo', 'Api\StatisticsController@statisticsRevenueCostOneAndTwo');
 });