cqp 4 週間 前
コミット
f90c2cbfaa

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

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\StatisticsService;
+use Illuminate\Http\Request;
+
+class StatisticsController extends BaseController
+{
+    public function statisticsRevenueCost(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->statisticsRevenueCost($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 2 - 0
app/Model/RevenueCost.php

@@ -23,4 +23,6 @@ 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'];
 }

+ 15 - 29
app/Service/Service.php

@@ -739,39 +739,25 @@ class Service
         return [true, json_decode($r, true)];
     }
 
-    function isValidMobile($mobile) {
-        // 匹配以1开头,第二位为3/4/5/7/8/9,总共11位的手机号
-        return preg_match('/^1[345789]\d{9}$/', $mobile);
-    }
+    function isOverThreeMonths($timestamp1, $timestamp2, $default_month = 3) {
+        $date1 = new \DateTime();
+        $date1->setTimestamp($timestamp1);
 
-    function isValidVin($vin) {
-        $vin = strtoupper(trim($vin));
+        $date2 = new \DateTime();
+        $date2->setTimestamp($timestamp2);
 
-        // 检查是否为17位
-        if (strlen($vin) !== 17) {
-            return false;
-        }
+        // 计算两个日期的间隔
+        $interval = $date1->diff($date2);
 
-        return true;
-        // 计算校验位
-        $weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
-        $sum = 0;
-        for ($i = 0; $i < 17; $i++) {
-            $char = $vin[$i];
-            if (is_numeric($char)) {
-                $value = intval($char);
-            } elseif (ctype_alpha($char)) {
-                $value = ord(strtoupper($char)) - 55; // 'A' starts at 10, so subtract 55 ('A' is ASCII 65)
-            } else {
-                return false;
-            }
-            $sum += $value * $weights[$i];
-        }
+        // 总月数 = 年差 * 12 + 月差
+        $months = $interval->y * 12 + $interval->m;
 
-        $remainder = $sum % 11;
-        $checkDigit = $remainder == 10 ? 'X' : strval($remainder);
+        if ($months >= $default_month) {
+            return [false, "时间区间须在" . $default_month . "个月内"];
+        }elseif ($months == 3 && ($interval->d > 0 || $interval->h > 0 || $interval->i > 0 || $interval->s > 0)) {
+            return [false, "时间区间须在" . $default_month . "个月内"];
+        }
 
-        // 比较计算出的校验位与第9位是否匹配
-        return $checkDigit === $vin[8];
+        return [true, ''];
     }
 }

+ 82 - 0
app/Service/StatisticsService.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\RevenueCost;
+
+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)
+            ->select($field)
+            ->orderby('id', 'desc');
+        $model->where('order_time', '>=', $start_time)
+            ->where('order_time', '<', $end_time);
+
+        return [true, $model];
+    }
+
+    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);
+
+        return [true, $return];
+    }
+
+    public function statisticsRevenueCostFillData($data){
+        if(empty($data)) return $data;
+
+        $return = [];
+        foreach ($data as $key => $value){
+            $time = date("Y-m", $value['order_time']);
+            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[$time][$value['order_type']])){
+                $income_total = bcadd($return[$time][$value['order_type']]['income'], $income,2);
+                $adjust_total = bcadd($return[$time][$value['order_type']]['adjust'], $adjust,2);
+                $business_total = bcadd($return[$time][$value['order_type']]['business'], $business,2);
+                $return[$time][$value['order_type']]['income'] = $income_total;
+                $return[$time][$value['order_type']]['adjust'] = $adjust_total;
+                $return[$time][$value['order_type']]['business'] = $business_total;
+            }else{
+                $return[$time][$value['order_type']] = [
+                    'income' => $value['price_3_total'],
+                    'adjust' => $adjust,
+                    'business' => $business,
+                    'time' => $time,
+                ];
+            }
+        }
+        $final = [];
+        foreach ($return as $value){
+            foreach ($value as $key => $val){
+                $title = RevenueCost::$order_type[$key] ?? "";
+                $val['title'] = $title;
+                $profit = bcsub($val['income'], $val['business'],2);
+                $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0;
+                $val['profit'] = $profit;
+                $val['profit_rate'] = $profit_rate;
+                $val['order_type'] = $key;
+                $final[] = $val;
+            }
+        }
+
+        return $final;
+    }
+}

+ 0 - 22
app/Service/TPlusServerService.php

@@ -197,28 +197,6 @@ class TPlusServerService extends Service
         return [true, '收入成本相关信息同步已进入后台任务'];
     }
 
-    function isOverThreeMonths($timestamp1, $timestamp2, $default_month = 3) {
-        $date1 = new \DateTime();
-        $date1->setTimestamp($timestamp1);
-
-        $date2 = new \DateTime();
-        $date2->setTimestamp($timestamp2);
-
-        // 计算两个日期的间隔
-        $interval = $date1->diff($date2);
-
-        // 总月数 = 年差 * 12 + 月差
-        $months = $interval->y * 12 + $interval->m;
-
-        if ($months > $default_month) {
-            return [false, "时间区间要在" . $default_month . "个月内"];
-        }elseif ($months == 3 && ($interval->d > 0 || $interval->h > 0 || $interval->i > 0 || $interval->s > 0)) {
-            return [false, "时间区间要在" . $default_month . "个月内"];
-        }
-
-        return [true, ''];
-    }
-
     public function synRevenueCostFromTPlus($data, $user){
         //创建临时表 如果不存在
         $this->createTmpTable();

+ 37 - 0
config/header/68.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' =>'title',
+        'value' => '单据类型',
+    ],
+    [
+        'key' =>'income',
+        'value' => '收入金额',
+    ],
+    [
+        'key' =>'adjust',
+        'value' => '调整金额',
+    ],
+    [
+        'key' =>'business',
+        'value' => '业务成本金额',
+    ],
+    [
+        'key' =>'profit',
+        'value' => '毛利',
+    ],
+    [
+        'key' =>'profit_rate',
+        'value' => '毛利率',
+    ],
+    [
+        'key' =>'time',
+        'value' => '单据年月',
+    ],
+];

+ 3 - 0
routes/api.php

@@ -127,4 +127,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     //同步T+人员部门结构
     $route->any('synPersonDepart', 'Api\TPlusController@synPersonDepart');
     $route->any('synRevenueCost', 'Api\TPlusController@synRevenueCost');
+
+    //
+    $route->any('statisticsRevenueCost', 'Api\StatisticsController@statisticsRevenueCost');
 });