cqpCow il y a 1 an
Parent
commit
8eadc58b41
2 fichiers modifiés avec 98 ajouts et 41 suppressions
  1. 96 40
      app/Service/ImportService.php
  2. 2 1
      app/Service/ScheduleService.php

+ 96 - 40
app/Service/ImportService.php

@@ -8,6 +8,7 @@ use App\Import\ImportAll;
 use App\Model\BasicType;
 use App\Model\Customer;
 use App\Model\CustomerInfo;
+use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\Product;
 use App\Model\ProductCategory;
@@ -424,7 +425,9 @@ class ImportService extends Service
     }
 
     public function productImport($array, $user){
-        $head = $user['head']['id'] ?? 0;
+        //当前门店
+        $depart_id = $this->getDepart($user);
+        $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
 
         // 去除表头
         $upload = $array[0];
@@ -448,23 +451,24 @@ class ImportService extends Service
                 if(in_array($t,$array_clean)) return [false, '产品名称与编码不能重复'];
                 $array_clean[] = $t;
                 $array[$key] = $value;
-                $search .= "(title = '".$value[0]."' and code ='".$value[1]."') or";
+                $search .= "(binary code ='".$value[1]."') or";
             }
         }unset($array_clean);
         if(empty($array)) return [false, '导入数据不能为空'];
+
         $search = rtrim($search,' or');
         $product = Product::whereRaw($search)
             ->where('del_time',0)
-            ->select('code','title')
+            ->select('code','top_depart_id','id')
             ->get()->toArray();
         $product_array = [];
         foreach ($product as $value){
-            $product_array[] = $value['title'] . $value['code'];
+            $product_array[$value['code']] = [
+                'id' => $value['id'],
+                'top_depart_id' => $value['top_depart_id']
+            ];
         }
 
-        //默认进来 自身顶级公司
-        $top_depart_id = $user['depart_top'][0] ?? [];
-        $top_depart_id = $top_depart_id['depart_id'] ?? 0;
         $category_list = ProductCategory::where('del_time',0)
             ->where('top_depart_id',$top_depart_id)
             ->get()->toArray();
@@ -486,12 +490,11 @@ class ImportService extends Service
         $tmp = array_column($heads,'key');
         $tmp = array_fill_keys($tmp, '');
         $tmp['product_category'] = '';
-        $tmp['depart_id'] = $head;
-        $tmp['top_depart_id'] = $head;
-        $tmp['crt_id'] = $user['id'];
-        $tmp['crt_time'] = $time;
         $tmp['upd_time'] = $time;
-        $tmp['crt_id'] = $user['id'];
+
+        $top_message = Depart::where('parent_id',0)
+            ->pluck('title','id')
+            ->toArray();
 
         $upload = array_flip($upload);
         $map = [];
@@ -505,10 +508,17 @@ class ImportService extends Service
         }
 
         $array = array_values($array);
-        $insert = $insert2 = [];
-        foreach ($array as $key => $value){
-            $pro_str = $value['0'] . $value['1'];
-            if(in_array($pro_str, $product_array)) return [false, '产品名称、编码:' . $value['0']. '、' . $value['1'] . '已存在'];
+        $insert = $insert2 = $update = $update2 = [];
+        foreach ($array as $value){
+            if(isset($product_array[$value['1']])){
+                $pro_tmp = $product_array[$value['1']] ?? [];
+                if($pro_tmp['top_depart_id'] != $top_depart_id){
+                    $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
+                    $now = $top_message[$top_depart_id] ?? "";
+                    return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
+                }
+            }
+
             $tmp['title'] = $value['0'];
             $tmp['code'] = $value['1'];
             if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
@@ -535,39 +545,85 @@ class ImportService extends Service
                 $tmp[$m] = $value[$v['col']];
             }
 
-            foreach ($tmp as $k => $v){
-                if(strpos($k, 'table_id.') !== false){
-                    $tmp2 = [];
-                    $k_n = str_replace('table_id.', "", $k);
-                    $tmp2['basic_type_id'] = $k_n;
-                    $tmp2['price'] = $v;
-                    $tmp2['crt_time'] = $time;
-                    $tmp2['upd_time'] = $time;
-                    $insert2[$key][] = $tmp2;
-                    unset($tmp[$k]);
+            if(isset($product_array[$value['1']])){
+                //更新
+                $pro_tmp = $product_array[$value['1']] ?? [];
+                $product_id = $pro_tmp['id'];
+
+                //产品价格子表
+                foreach ($tmp as $k => $v){
+                    if(strpos($k, 'table_id.') !== false){
+                        $tmp2 = [];
+                        $k_n = str_replace('table_id.', "", $k);
+                        $tmp2['product_id'] = $product_id;
+                        $tmp2['basic_type_id'] = $k_n;
+                        $tmp2['price'] = $v;
+                        $tmp2['crt_time'] = $time;
+                        $tmp2['upd_time'] = $time;
+                        $update2[] = $tmp2;
+                        unset($tmp[$k]);
+                    }
                 }
+
+                //产品主表
+                $update[$product_id] = $tmp;
+            }else{
+                $tmp['depart_id'] = $depart_id;
+                $tmp['top_depart_id'] = $top_depart_id;
+                $tmp['crt_id'] = $user['id'];
+                $tmp['crt_time'] = $time;
+                //产品价格子表
+                foreach ($tmp as $k => $v){
+                    if(strpos($k, 'table_id.') !== false){
+                        $tmp2 = [];
+                        $k_n = str_replace('table_id.', "", $k);
+                        $tmp2['basic_type_id'] = $k_n;
+                        $tmp2['price'] = $v;
+                        $tmp2['crt_time'] = $time;
+                        $tmp2['upd_time'] = $time;
+                        $insert2[$tmp['code']][] = $tmp2;
+                        unset($tmp[$k]);
+                    }
+                }
+
+                //产品主表
+                $insert[$tmp['code']] = $tmp;
             }
-            $insert[] = $tmp;
         }
 
         try{
             DB::beginTransaction();
-            if(! empty($insert)) Product::insert($insert);
-            if(! empty($insert2)){
-                $insert_detail = [];
-                $last_insert_id = Product::where('crt_time',$time)
-                    ->select('id')
-                    ->get()->toArray();
-                $last_insert_id = array_column($last_insert_id,'id');
-                foreach ($last_insert_id as $key => $value){
-                    if(isset($insert2[$key])) {
-                        foreach ($insert2[$key] as $val){
-                            $val['product_id'] = $value;
-                            $insert_detail[] = $val;
+            //新增
+            if(! empty($insert)){
+                Product::insert($insert);
+                if(! empty($insert2)){
+                    $insert_detail = [];
+                    $last_insert_id = Product::where('crt_time',$time)
+                        ->pluck('id','code')
+                        ->toArray();
+                    foreach ($insert2 as $code => $val){
+                        foreach ($val as $v2){
+                            $v2['product_id'] = $last_insert_id[$code] ?? 0;
+                            $insert_detail[] = $v2;
                         }
                     }
+                    ProductPriceDetail::insert($insert_detail);
+                }
+            }
+
+            //编辑
+            if(! empty($update)){
+                foreach ($update as $p_id => $value){
+                    Product::where('id',$p_id)
+                        ->update($value);
+                }
+
+                if(! empty($update2)){
+                    $product_id_array = array_keys($update);
+                    ProductPriceDetail::whereIn('product_id',$product_id_array)
+                        ->update(['del_time' => $time]);
+                    ProductPriceDetail::insert($update2);
                 }
-                ProductPriceDetail::insert($insert_detail);
             }
             DB::commit();
         }catch (\Exception $e){

+ 2 - 1
app/Service/ScheduleService.php

@@ -618,7 +618,8 @@ class ScheduleService extends Service
             ->pluck('emp_name','id')
             ->toArray();
         $product_title = [];
-        $product = ConstructionProductInfo::whereIn('construction_id',array_unique(array_column($construction,'id')))
+        $product = ConstructionProductInfo::where('del_time',0)
+            ->whereIn('construction_id',array_unique(array_column($construction,'id')))
             ->select('product_id','construction_id')
             ->get()->toArray();
         $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))