Pārlūkot izejas kodu

线上订单导入

cqpCow 11 mēneši atpakaļ
vecāks
revīzija
38f35fbf93

+ 4 - 0
app/Model/SalesOrder.php

@@ -15,6 +15,10 @@ class SalesOrder extends UseScopeBaseModel
         self::Order_type_one => '安装件',
         self::Order_type_two => '快递件',
     ];
+    public static $order_type_name = [
+        '安装件' => self::Order_type_one,
+        '快递件' => self::Order_type_two,
+    ];
 
     const Model_type_one = 1; // 线下订单合同
     const Model_type_two = 2; // 分社订货合同

+ 132 - 25
app/Service/ImportService.php

@@ -13,6 +13,7 @@ use App\Model\Product;
 use App\Model\ProductCategory;
 use App\Model\ProductPriceDetail;
 use App\Model\SalesOrder;
+use App\Model\SalesOrderInfo;
 use App\Model\SalesOrderProductInfo;
 use Illuminate\Support\Facades\DB;
 use Maatwebsite\Excel\Facades\Excel;
@@ -591,26 +592,67 @@ class ImportService extends Service
         if(empty($array)) return [false, '导入数据不能为空'];
 
         $model = BasicType::TopClear($user,[]);
-        $basic_type = $model->where('del_time',0)
-            ->where('type',24)
-            ->pluck('id','title')
+        $basic = $model->where('del_time',0)
+            ->whereIn('type',[18,23,24,29])
+            ->select('id','title','type')
+            ->get()
             ->toArray();
+        $basic_list = [];
+        foreach ($basic as $value){
+            if($value['type'] == 18){
+                $basic_list[10][$value['title']] = $value['id'];
+            }elseif ($value['type'] == 23){
+                $basic_list[11][$value['title']] = $value['id'];
+            }elseif ($value['type'] == 24){
+                $basic_list[3][$value['title']] = $value['id'];
+            }elseif ($value['type'] == 29){
+                $basic_list[2][$value['title']] = $value['id'];
+            }
+        }
+
         $search = "";
+        $customer = [];
         foreach ($array as $key => $value){
             $rowData = array_filter($value);
             if (empty($rowData)) {
                 unset($array[$key]);
-            } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || empty($value[6])) {
+            } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || empty($value[6]) || empty($value[7])) {
                 return [false, '带*号的字段项必填'];
             }else{
                 foreach ($value as $k => $v){
                     $value[$k] = trim($v);
                 }
-                if(! isset($basic_type[$value[1]])) return [false, '店铺(平台类型):' . $value[1] .'不存在'];
-                $value[1] = $basic_type[$value[1]];
-                if(! isset(SalesOrder::$order_type[$value[6]])) return [false, '产品类型填写错误'];
+                if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
+                if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
+                $value[3] = $basic_list[3][$value[3]];
+                if(! empty($value[10])){
+                    if(! isset($basic_list[10][$value[10]])) return [false, '安装方式:' . $value[3] .'不存在'];
+                    $value[10] = $basic_list[10][$value[10]];
+                }
+                if(! empty($value[11])){
+                    if(! isset($basic_list[11][$value[11]])) return [false, '安装地点:' . $value[3] .'不存在'];
+                    $value[11] = $basic_list[11][$value[11]];
+                }
+
+                if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
+                $value[1] = SalesOrder::$order_type_name[$value[1]];
+
+                if(! empty($value[12])) {
+                    list($status,$msg) = $this->changeAndReturnDate($value[12]);
+                    if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
+                    $value[12] = $msg;
+                }
+
+                if(! empty($value[13])) {
+                    list($status,$msg) = $this->changeAndReturnDate($value[13]);
+                    if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
+                    $value[13] = $msg;
+                }
+
                 $array[$key] = $value;
-                $search .= "(code = '".$value[2]."' and title ='".$value[3]."') or";
+                $search .= "(code = '".$value[4]."' and title ='".$value[5]."') or";
+
+                if(! empty($value[8]) && ! in_array($value[8], $customer)) $customer[] = $value[8];
             }
         }
         if(empty($array)) return [false, '导入数据不能为空'];
@@ -620,6 +662,8 @@ class ImportService extends Service
             ->where('del_time',0)
             ->select('title','id','code','cost','retail_price')
             ->get()->toArray();
+//        $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
+
         $product_map = [];
         foreach ($product as $value){
             $product_map[$value['code'] . $value['title']] = [
@@ -630,15 +674,43 @@ class ImportService extends Service
         }
 
         $time = time();
+
+        $model = Customer::Clear($user,[]);
+        $customer_map = $model->where('del_time',0)
+            ->whereIn('title',$customer)
+            ->pluck('id','title')
+            ->toArray();
+
+        $customer_info = CustomerInfo::where('del_time',0)
+            ->whereIn('customer_id',array_values($customer_map))
+            ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
+            ->select('customer_id','type','contact_info','data_id')
+            ->orderBy('id','asc')
+            ->get()->toArray();
+        $customer_contact = $customer_man = [];
+        foreach ($customer_info as $value){
+            if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
+                $customer_contact[$value['customer_id']] = $value['contact_info'];
+            }elseif ($value['type'] == CustomerInfo::type_two){
+                $customer_man[$value['customer_id']][] = [
+                    'type' => SalesOrderInfo::type_two,
+                    'data_id' => $value['data_id'],
+                    'crt_time' => $time,
+                ];
+            }
+        }
+
         $tmp = [
             'model_type' => SalesOrder::Model_type_four,
             'sales_order_type' => 0,
             'order_number' => '',
+            'customer_short_name' => '',
             'plat_type' => '',
             'plat_order' => '',
             'sign_time' => $time,
             'product_total' => 0,
             'contract_fee' => 0,
+            'discount_fee' => 0,
             'rate' => 100,
             'depart_id' => $head,
             'top_depart_id' => $head,
@@ -651,53 +723,76 @@ class ImportService extends Service
             'product_id' => 0,
             'cost' => 0,
             'retail_price' => 0,
+            'basic_type_id' => 0,
             'price' => 0,
             'final_amount' => 0,
             'number' => '',
             'crt_time' => $time,
         ];
-        $insert = $insert_detail = [];
+        $insert = $insert_detail = $insert_detail_man =  [];
         $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
         foreach ($array as $value){
-            $product_str = $value[2] . $value[3];
-            if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[2]. ']' . '[' . $value[3]. ']' . '不存在'];
+            $product_str = $value[4] . $value[5];
+            if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '[' . $value[5]. ']' . '不存在'];
             $product_tmp = $product_map[$product_str] ?? [];
 
+            $customer_tmp = 0;
+            $customer_contact_tmp = "";
+            $customer_man_tmp = [];
+            if(! empty($value[8])){
+                $customer_tmp = $customer_map[$value[8]] ?? 0;
+                $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
+                $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
+            }
+            $customer_man_tmp[] = [
+                'type' => SalesOrderInfo::type_one,
+                'data_id' => $user['id'],
+                'crt_time' => $time,
+            ];
+
             $tmp['product_total'] = $tmp['contract_fee'] = 0;
-            $keys = $value[0] . $value[6];
+            $keys = $value[0] . $value[1];
+            $insert_detail_man[$keys] = $customer_man_tmp;
             if(! isset($insert[$keys])){
                 $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
-                $tmp['sales_order_type'] = $value[6];
-                $tmp['plat_type'] = $value[1];
+                $tmp['sales_order_type'] = $value[1];
+                $tmp['customer_short_name'] = $value[2];
+                $tmp['customer_id'] = $customer_tmp;
+                $tmp['customer_contact'] = $customer_contact_tmp;
+                $tmp['plat_type'] = $value[3];
                 $tmp['plat_order'] = $value[0];
-                $tmp['product_total'] += $value[4] * $value[5];
-                $tmp['contract_fee'] += $value[4] * $value[5];
+                $tmp['discount_fee'] = $value[9];
+                $tmp['product_total'] += $value[7];
+                $tmp['contract_fee'] += $value[7] - ($value[9]);
+                $tmp['construction_time'] = $value[12] ?? 0;
+                $tmp['handover_time'] = $value[13] ?? 0;
                 $insert[$keys] = $tmp;
             }else{
-                $insert[$keys]['product_total'] += $value[4] * $value[5];
-                $insert[$keys]['contract_fee'] += $value[4] * $value[5];
+                $insert[$keys]['product_total'] += $value[7];
+                $insert[$keys]['contract_fee'] += $value[7];
             }
 
             $tmp_detail['product_id'] = $product_tmp['id'];
             $tmp_detail['cost'] = $product_tmp['cost'];
             $tmp_detail['retail_price'] = $product_tmp['retail_price'];
-            $tmp_detail['price'] = $value[5];
-            $tmp_detail['final_amount'] = $value[4] * $value[5];
-            $tmp_detail['number'] = $value[4];
+            $tmp_detail['price'] = $product_tmp['retail_price'];
+            $tmp_detail['final_amount'] = $value[7];
+            $tmp_detail['number'] = $value[6];
 
             $insert_detail[$keys][] = $tmp_detail;
         }
 
         $insert_detail = array_values($insert_detail);
+        $insert_detail_man = array_values($insert_detail_man);
         try{
             DB::beginTransaction();
             if(! empty($insert)) SalesOrder::insert($insert);
+            $last_insert_id = SalesOrder::where('crt_time',$time)
+                ->select('id')
+                ->get()->toArray();
+            $last_insert_id = array_column($last_insert_id,'id');
             if(! empty($insert_detail)){
                 $insert2 = [];
-                $last_insert_id = SalesOrder::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($insert_detail[$key])) {
                         foreach ($insert_detail[$key] as $val){
@@ -708,6 +803,18 @@ class ImportService extends Service
                 }
                 SalesOrderProductInfo::insert($insert2);
             }
+            if(! empty($insert_detail_man)){
+                $insert3 = [];
+                foreach ($last_insert_id as $key => $value){
+                    if(isset($insert_detail_man[$key])) {
+                        foreach ($insert_detail_man[$key] as $val){
+                            $val['sales_order_id'] = $value;
+                            $insert3[] = $val;
+                        }
+                    }
+                }
+                SalesOrderInfo::insert($insert3);
+            }
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();

+ 1 - 0
app/Service/ProductService.php

@@ -547,6 +547,7 @@ class ProductService extends Service
             $model->where('crt_time','>=',$return[0]);
             $model->where('crt_time','<=',$return[1]);
         }
+        if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
 
         $list = $this->limit($model,'',$data);
         $list = $this->fillData($list,$user,$data);

+ 11 - 0
app/Service/Service.php

@@ -531,4 +531,15 @@ class Service
             }
         }
     }
+
+    public function changeAndReturnDate($date_int){
+        $int = intval($date_int);
+        $bool = (string)$int === $date_int;
+        if(! $bool || $date_int < 0) return [false, '日期不正确'];
+
+        $epoch = strtotime("1900-01-01 00:00:00");
+        $days = $date_int - 1;
+        $seconds = $days * 86400;
+        return [true,strtotime(date('Y-m-d 00:00:00', $epoch + $seconds))];
+    }
 }

+ 33 - 5
config/excel/salesOnlineTable.php

@@ -11,12 +11,20 @@ return [
         'value' => '* 原始单号(平台单号)',
     ],
     [
+        'key' => 'sales_order_type',
+        'value' => '* 产品类型',
+    ],
+    [
+        'key' => 'customer_detail',
+        'value' => '* 客户简称',
+    ],
+    [
         'key' => 'plat_type',
         'value' => '* 店铺(平台类型)',
     ],
     [
         'key' => 'code',
-        'value' => '* 商家编码(产品编码)',
+        'value' => '* 货品编码(产品编码)',
     ],
     [
         'key' => 'title',
@@ -27,11 +35,31 @@ return [
         'value' => '* 货品数量',
     ],
     [
-        'key' => 'unit_price',
-        'value' => '* 单品支付金额(单价)',
+        'key' => 'final_amount',
+        'value' => '* 产品合同金额',
     ],
     [
-        'key' => 'sales_order_type',
-        'value' => '* 产品类型(安装件:1,快递件:2)',
+        'key' => 'customer',
+        'value' => '客户名称',
+    ],
+    [
+        'key' => 'discount_fee',
+        'value' => '订单优惠金额',
+    ],
+    [
+        'key' => 'install_way',
+        'value' => '安装方式',
+    ],
+    [
+        'key' => 'install_position',
+        'value' => '安装地点',
+    ],
+    [
+        'key' => 'construction_time',
+        'value' => '施工日期',
+    ],
+    [
+        'key' => 'handover_time',
+        'value' => '交车日期',
     ],
 ];