cqp 1 ngày trước cách đây
mục cha
commit
677155cb36

+ 11 - 0
app/Http/Controllers/Api/ReportFormsController.php

@@ -45,6 +45,17 @@ class ReportFormsController extends BaseController
         }
     }
 
+    public function badGoodsReason(Request $request){
+        $service = new ReportFormsService();
+        list($status,$data) = $service->badGoodsReason($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     //不良品
     public function badGoodsReport(Request $request){
         $service = new ReportFormsService();

+ 75 - 20
app/Service/ReportFormsService.php

@@ -407,6 +407,45 @@ class ReportFormsService extends Service
         return [true,$list];
     }
 
+    public function badGoodsReason($data){
+        $reason = $data['reason'] ?? "";
+
+        $scrapp = Scrapp::where('del_time',0)
+            ->when(! empty($reason), function ($query) use ($reason){
+                return $query->where('title', $reason);
+            })
+            ->pluck('title','id')
+            ->toArray();
+        if(empty($scrapp)) return [true ,''];
+
+        $scrapp_count = ScrappCount::where('del_time',0)
+            ->whereIn('scrapp_id',array_keys($scrapp))
+            ->where('crt_time','>=',strtotime(date("Y-01-01 00:00:00")))
+            ->where('scrapp_num',">=",0)
+            ->select('scrapp_id as id',DB::raw("sum(scrapp_num) as waste"))
+            ->groupBy('scrapp_id')
+            ->get()->toArray();
+        $sum = ! empty($scrapp_count) ? array_sum(array_column($scrapp_count,'waste')) : 0;
+        $scrapp_count_map = array_column($scrapp_count,'waste','id');
+
+        $return = [];
+        foreach ($scrapp as $id => $title){
+            $num = $scrapp_count_map[$id] ?? 0;
+            $rate = floatval($sum) > 0.0 ? bcmul(bcdiv($num, $sum,4),100,2) : 0 ;
+            $return[] = [
+                'title' => $title,
+                'num' => $num,
+                'rate' => $rate . "%"
+            ];
+        }
+
+        usort($return, function($a, $b) {
+            return $b['num'] <=>  $a['num'];
+        });
+
+        return [true, $return];
+    }
+
     /**
      * 不良品
      * @param $data
@@ -416,7 +455,7 @@ class ReportFormsService extends Service
         if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
 
         //检索条件 生产订单主表----------------
-        $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man','id');
+        $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man','id','technology_name');
         $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
         if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
         if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
@@ -468,20 +507,23 @@ class ReportFormsService extends Service
         $scrapp = ScrappCount::where('del_time',0)
             ->whereIn('order_product_id',$production_id)
             ->where('scrapp_num','>',0)
-            ->select('order_product_id','scrapp_num as bad_goods_num')
+            ->select('order_product_id','scrapp_num as bad_goods_num','technology_name')
             ->get()->toArray();
         foreach ($scrapp as $value){
-            if(isset($detail[$value['order_product_id']])){
-                $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
-                $detail[$value['order_product_id']] = $tmp;
+            $key = $value['order_product_id'] . $value['technology_name'];
+            if(isset($detail[$key])){
+                $tmp = bcadd($detail[$key],$value['bad_goods_num'],3);
+                $detail[$key] = $tmp;
             }else{
-                $detail[$value['order_product_id']] = $value['bad_goods_num'];
+                $detail[$key] = $value['bad_goods_num'];
             }
         }
 
         //返回统计数据
         foreach ($list as $key => $value) {
-            $del_num = $detail[$value['id']] ?? 0;
+            $n_key = $value['id'] . $value['technology_name'];
+
+            $del_num = $detail[$n_key] ?? 0;
             if(floatval($del_num) <= 0.0) {
                 unset($list[$key]);
                 continue;
@@ -489,7 +531,7 @@ class ReportFormsService extends Service
             $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
             $list[$key]['bad_goods_num'] = $del_num;
-            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
+            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 4) : 0;
         }
 
         return [true,array_values($list)];
@@ -502,8 +544,10 @@ class ReportFormsService extends Service
      */
     public function badGoodsReportDetail($data){
         if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
+        if(empty($data['technology_name'])) return [false,'产品颜色不能为空!'];
 
         $list = OrdersProduct::where('production_no',$data['production_no'])
+            ->where('technology_name',$data['technology_name'])
             ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity','order_quantity')
             ->get()->toArray();
 
@@ -561,7 +605,7 @@ class ReportFormsService extends Service
             $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
             $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
-            $list[$key]['rate'] =  number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
+            $list[$key]['rate'] = bcdiv($list[$key]['bad_goods_num'] ,$value['production_quantity'], 4);
             $scrapp = $detail_scrapp[$value['id']] ?? '';
             $tmp_str = '';
             if(! empty($scrapp)){
@@ -585,7 +629,7 @@ class ReportFormsService extends Service
         if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
 
         //检索条件 生产订单主表----------------
-        $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man','id');
+        $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man','id','technology_name');
         $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
         if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
         if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
@@ -618,14 +662,21 @@ class ReportFormsService extends Service
         $scrapp = ScrappCount::where('del_time',0)
             ->whereIn('order_product_id',$production_id)
             ->where('scrapp_num','>',0)
-            ->select('order_product_id','scrapp_num as bad_goods_num')
+            ->select('order_product_id','scrapp_num as bad_goods_num','technology_name')
             ->get()->toArray();
         foreach ($scrapp as $value){
-            if(isset($detail[$value['order_product_id']])){
-                $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
-                $detail[$value['order_product_id']] = $tmp;
+//            if(isset($detail[$value['order_product_id']])){
+//                $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
+//                $detail[$value['order_product_id']] = $tmp;
+//            }else{
+//                $detail[$value['order_product_id']] = $value['bad_goods_num'];
+//            }
+            $key = $value['order_product_id'] . $value['technology_name'];
+            if(isset($detail[$key])){
+                $tmp = bcadd($detail[$key],$value['bad_goods_num'],3);
+                $detail[$key] = $tmp;
             }else{
-                $detail[$value['order_product_id']] = $value['bad_goods_num'];
+                $detail[$key] = $value['bad_goods_num'];
             }
         }
 //        foreach ($out_time as $value){
@@ -648,9 +699,10 @@ class ReportFormsService extends Service
 //        }
 
         //返回统计数据
-
         foreach ($list as $key => $value) {
-            $del_num = $detail[$value['id']] ?? 0;
+            $n_key = $value['id'] . $value['technology_name'];
+
+            $del_num = $detail[$n_key] ?? 0;
             if(floatval($del_num) <= 0.0) {
                 unset($list[$key]);
                 continue;
@@ -658,7 +710,7 @@ class ReportFormsService extends Service
             $list[$key]['bad_goods_num'] = $del_num;
             $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
-            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
+            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 4) : 0;
         }
 
         return [true,array_values($list)];
@@ -667,8 +719,10 @@ class ReportFormsService extends Service
     //不良品原因 详情
     public function badGoodsReasonReportDetail($data){
         if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
+        if(empty($data['technology_name'])) return [false,'产品颜色不能为空!'];
 
         $list = OrdersProduct::where('production_no',$data['production_no'])
+            ->where('technology_name',$data['technology_name'])
             ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity')
             ->get()->toArray();
 
@@ -682,6 +736,7 @@ class ReportFormsService extends Service
 
         $scrapp = ScrappCount::where('del_time',0)
             ->whereIn('order_product_id',$order_product_id)
+            ->where('technology_name',$data['technology_name'])
             ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id','team_id','finished_id')
             ->get()->toArray();
         foreach ($scrapp as $value){
@@ -746,9 +801,9 @@ class ReportFormsService extends Service
             $sum = 0;
             $d_t = $detail[$value['id']] ?? [];
             if(! empty($d_t)) $sum = array_sum(array_column($d_t,'bad_goods_num'));
-            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($sum ,$value['production_quantity'], 2) * 100 ."%" : 0 ."%";
+            $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($sum ,$value['production_quantity'], 4) * 100 ."%" : 0 ."%";
             foreach ($d_t as $dk => $dv){
-                $d_t[$dk]['rate'] = $value['production_quantity'] > 0 ? bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 2) * 100 . "%" : 0 . "%";
+                $d_t[$dk]['rate'] = $value['production_quantity'] > 0 ? bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 4) * 100 . "%" : 0 . "%";
                 $d_t[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
                 $d_t[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
                 $d_t[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';

+ 1 - 0
routes/api.php

@@ -213,6 +213,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('badGoodsReportDetail','Api\ReportFormsController@badGoodsReportDetail');
     $route->any('badGoodsReasonReport', 'Api\ReportFormsController@badGoodsReasonReport');
     $route->any('badGoodsReasonReportDetail','Api\ReportFormsController@badGoodsReasonReportDetail');
+    $route->any('badGoodsReason','Api\ReportFormsController@badGoodsReason');
     $route->any('statisticsReportWorkingChart','Api\ReportFormsController@statisticsReportWorkingChart');
     $route->any('statisticsReportWorkingChartDetail','Api\ReportFormsController@statisticsReportWorkingChartDetail');