cqp 3 місяців тому
батько
коміт
4bd1be5c88

+ 5 - 1
app/Http/Controllers/Api/ImportController.php

@@ -42,7 +42,11 @@ class ImportController extends BaseController
         if($status){
             return $this->json_return(200,'',$data);
         }else{
-            return $this->json_return(201,$data);
+            if($status === 0){
+                return $this->json_return(199,$data);
+            }else{
+                return $this->json_return(201,$data);
+            }
         }
     }
 }

+ 13 - 1
app/Import/ImportAll.php

@@ -13,6 +13,7 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
     public $crt_id = 0;
     public $type = "";
     public $user = [];
+    public $is_long_text = false;
 
     public function array (array $array){
         $this->handleData($array);
@@ -38,6 +39,14 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
         $this->msg = $msg;
     }
 
+    public function setIsLongText($bool = true){
+        $this->is_long_text = $bool;
+    }
+
+    public function getIsLongText(){
+        return $this->is_long_text;
+    }
+
     public function handleData (array $array) {
         $func = $this->type . "Import";
         if(! $func) {
@@ -46,6 +55,9 @@ class ImportAll implements ToArray,WithCalculatedFormulas {
         }
 
         list($status,$msg) = (new ImportService())->$func($array,$this->user);
-        if(empty($status)) $this->setMsg($msg);
+        if(empty($status)) {
+            if($status === 0) $this->setIsLongText();
+            $this->setMsg($msg);
+        }
     }
 }

+ 243 - 116
app/Service/ImportService.php

@@ -268,7 +268,14 @@ class ImportService extends Service
 
             //导入
             \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
-            if($import->getMsg()) return [false, $import->getMsg()];
+            if($import->getMsg()) {
+                $bool = $import->getIsLongText();
+                if($bool) {
+                    return [0, $import->getMsg()];
+                }else{
+                    return [false, $import->getMsg()];
+                }
+            }
         }catch (\Throwable $exception) {
             return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
         }
@@ -277,6 +284,8 @@ class ImportService extends Service
     }
 
     public function customerImport($array, $user){
+        $time = time();
+
         $head = $user['depart_top'][0] ?? [];
         $head = $head['depart_id'] ?? 0;
         if(empty($head)) return [false, '导入异常错误,门店信息丢失'];
@@ -286,39 +295,10 @@ class ImportService extends Service
         if(empty($array)) return [false, '导入数据不能为空'];
 
         //第一次表格数据校验 非空  已经过滤数据
-        $array_clean = $contact_info = [];
-        $now = time();
-        foreach ($array as $key => $value){
-            $rowData = array_filter($value);
-            if (empty($rowData)) {
-                unset($array[$key]);
-            } elseif(empty($value[0]) || empty($value[1])) {
-                return [false, '带*号的字段项必填'];
-            }else{
-                foreach ($value as $k => $v){
-                    $value[$k] = trim($v);
-                }
-                if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
-                $value[0] = Customer::dk[$value[0]];
-                if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
-                $array_clean[] = $value[1];
-
-                if(! empty($value[15])){
-                    list($status, $msg) = $this->convertExcelCellToDate($value[15]);
-                    if(! $status) return [false, $msg];
-                    $value[15] = $msg;
-                } else{
-                    $value[15] = $now;
-                }
+        list($status, $msg) = $this->customerFilterFirst($array, $time);
+        if(! $status) return [0, $msg];
+        $contact_info = $msg;
 
-                $array[$key] = $value;
-
-                if(! empty($value[13])){
-                    if(in_array($value[13],$contact_info)) return [false, '联系方式内容不能重复'];
-                    $contact_info[] = $value[13];
-                }
-            }
-        }unset($array_clean);
         if(empty($array)) return [false, '导入数据不能为空'];
 
         //客户
@@ -372,10 +352,117 @@ class ImportService extends Service
             ->select('a.contact_info')->get()->toArray();
         $contact_info_array = array_column($contact_info_array,'contact_info');
 
-        $time = time();
-        $insert = [];
-        $insert_detail = $insert_detail2 = [];
-        foreach ($array as $value){
+        list($status, $msg) = $this->customerFilterSecond($array,$time,$head,$user,$contact_info_array,$customer,$basic_list,$product,$emp);
+        if(! $status) return [0, $msg];
+        list($insert, $insert_detail, $insert_detail2) = $msg;
+
+        try{
+            DB::beginTransaction();
+            if(! empty($insert)) Customer::insert($insert);
+
+            //获取上一次所有id
+            $last_insert_id = Customer::where('crt_time',$time)
+                ->where('crt_time',$time)
+                ->where('depart_id',$head)
+                ->where('top_depart_id',$head)
+                ->where('crt_id',$user['id'])
+                ->select('id')->get()->toArray();
+            $last_insert_id = array_column($last_insert_id,'id');
+
+            //组织数据 写入与主表的关联id
+            $insert_detail_1 = [];
+            foreach ($insert_detail as $key => $value){
+                if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
+                $value['customer_id'] = $last_insert_id[$key];
+                $insert_detail_1[] = $value;
+            }unset($insert_detail);
+            $insert_detail_2 = [];
+            foreach ($insert_detail2 as $key => $value){
+                if(empty($value['data_id'])) continue;
+                $value['customer_id'] = $last_insert_id[$key];
+                $insert_detail_2[] = $value;
+            }unset($insert_detail2);
+
+            if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
+            if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
+        }
+
+        return [true, ''];
+    }
+
+    function customerFilterFirst(&$array, $time){
+        $array_clean = $contact_info = [];
+
+        $error = [];
+        foreach ($array as $key => $value){
+            $line = '第' . $key . '行';
+            $rowData = array_filter($value);
+            if (empty($rowData)) {
+                unset($array[$key]);
+            } else{
+                foreach ($value as $k => $v){
+                    $value[$k] = trim($v);
+                }
+
+                if(empty($value[0])){
+                    $error[] = $line . '客户模板字段项必填';
+                }else{
+                    if(! isset(Customer::dk[$value[0]])) {
+                        $error[] = $line . '客户模板填写错误';
+                    }else{
+                        $value[0] = Customer::dk[$value[0]];
+                    }
+                }
+                if(empty($value[1])){
+                    $error[] = $line . '客户名称字段项必填';
+                }else{
+                    if(in_array($value[1],$array_clean)) {
+                        $error[] = $line . '客户名称' . $value[1] .'在导入文件中重复出现';
+                    }else{
+                        $array_clean[] = $value[1];
+                    }
+                }
+
+                if(! empty($value[13])){
+                    if(in_array($value[13],$contact_info)) {
+                        $error[] = $line . '联系方式内容'. $value[13] .'在导入文件内重复出现';
+                    }else{
+                        $contact_info[] = $value[13];
+                    }
+                }
+
+                if(! empty($value[15])){
+                    list($status, $msg) = $this->convertExcelCellToDate($value[15]);
+                    if(! $status) {
+                        $error[] = $line . $msg;
+                    }else{
+                        $value[15] = $msg;
+                    }
+                } else{
+                    $value[15] = $time;
+                }
+
+                $array[$key] = $value;
+            }
+        }unset($array_clean);
+
+        if(! empty($error)){
+            $error = implode(',', $error);
+            return [false, $error];
+        }
+
+        return [true, $contact_info];
+    }
+
+    function customerFilterSecond($array, $time, $head, $user, $contact_info_array,$customer,$basic_list,$product,$emp){
+        $error = $insert = $insert_detail = $insert_detail2 = [];
+        foreach ($array as $key => $value){
+            $line = '第' . $key . '行';
             $tmp = [
                 'model_type' => '',
                 'title' => '',
@@ -400,48 +487,81 @@ class ImportService extends Service
             $tmp['consulting_product'] = $value['5'];
             $tmp['mark'] = $value['10'];
             $tmp['address2'] = $value['11'];
-            if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
-            $tmp['title'] = $value['1'];
+
+            if(! empty($customer[$value['1']])) {
+                $error[] = $line . '客户名称:' . $value['1'] . '已存在';
+            }else{
+                $tmp['title'] = $value['1'];
+            }
             if($value['2']){
-                if(empty($basic_list[2][$value['2']])) return [false, '客户意向度:' . $value['2'] . '不存在'];
-                $tmp['customer_intention'] = $basic_list[2][$value['2']];
+                if(empty($basic_list[2][$value['2']])) {
+                    $error[] = $line . '客户意向度:' . $value['2'] . '不存在';
+                }else{
+                    $tmp['customer_intention'] = $basic_list[2][$value['2']];
+                }
             }
             if($value['3']){
-                if(empty($basic_list[3][$value['3']])) return [false, '客户来源:' . $value['3'] . '不存在'];
-                $tmp['customer_from'] = $basic_list[3][$value['3']];
+                if(empty($basic_list[3][$value['3']])) {
+                    $error[] = $line . '客户来源:' . $value['3'] . '不存在';
+                }else{
+                    $tmp['customer_from'] = $basic_list[3][$value['3']];
+                }
             }
             if($value['4']){
                 $keys = 4 . $value[0];
                 $model_title = Customer::dk2[$value[0]] ?? "";
-                if(empty($basic_list[$keys][$value['4']])) return [false, '模板:' . $model_title . '下客户类别:' . $value['4'] . '不存在'];
-                $tmp['customer_type'] = $basic_list[$keys][$value['4']];
+                if(empty($basic_list[$keys][$value['4']])) {
+                    $error[] = $line . '模板:' . $model_title . '下客户类别:' . $value['4'] . '不存在';
+                }else{
+                    $tmp['customer_type'] = $basic_list[$keys][$value['4']];
+                }
             }
             if($value['6']){
-                if(empty($product[$value['6']])) return [false, '意向产品:' . $value['6'] . '不存在'];
-                $tmp['intention_product'] = $product[$value['6']];
+                if(empty($product[$value['6']])) {
+                    $error[] = $line . '意向产品:' . $value['6'] . '不存在';
+                }else{
+                    $tmp['intention_product'] = $product[$value['6']];
+                }
             }
             if($value['7']){
-                if(empty($basic_list[7][$value['7']])) return [false, '进展阶段:' . $value['7'] . '不存在'];
-                $tmp['progress_stage'] = $basic_list[7][$value['7']];
+                if(empty($basic_list[7][$value['7']])) {
+                    $error[] = $line . '进展阶段:' . $value['7'] . '不存在';
+                }else{
+                    $tmp['progress_stage'] = $basic_list[7][$value['7']];
+                }
             }
             if($value['8']){
-                if(empty($basic_list[8][$value['8']])) return [false, '状态:' . $value['8'] . '不存在'];
-                $tmp['state_type'] = $basic_list[8][$value['8']];
+                if(empty($basic_list[8][$value['8']])) {
+                    $error[] = $line . '状态:' . $value['8'] . '不存在';
+                }else{
+                    $tmp['state_type'] = $basic_list[8][$value['8']];
+                }
             }
             if($value['9']){
-                if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
-                $tmp['car_type'] = $basic_list[9][$value['9']];
+                if(empty($basic_list[9][$value['9']])) {
+                    $error[] = $line . '车型:' . $value['9'] . '不存在';
+                }else{
+                    $tmp['car_type'] = $basic_list[9][$value['9']];
+                }
             }
             $contact_id = 0;
             if($value['12']){
-                if(empty($basic_list[12][$value['12']])) return [false, '联系方式类型:' . $value['12'] . '不存在'];
-                $contact_id = $basic_list[12][$value['12']];
+                if(empty($basic_list[12][$value['12']])) {
+                    $error[] = $line . '联系方式类型:' . $value['12'] . '不存在';
+                }else{
+                    $contact_id = $basic_list[12][$value['12']];
+                }
+            }
+            if($value['13'] && in_array($value['13'],$contact_info_array)){
+                $error[] = $line . '联系方式内容:' . $value['13'] . '已存在';
             }
-            if($value['13'] && in_array($value['13'],$contact_info_array)) return [false, '联系方式内容:' . $value['13'] . '已存在'];
             $man = 0;
             if($value['14']){
-                if(empty($emp[$value['14']])) return [false, '负责人:' . $value['14'] . '不存在'];
-                $man = $emp[$value['14']];
+                if(empty($emp[$value['14']])) {
+                    $error[] = $line . '负责人:' . $value['14'] . '不存在';
+                }else{
+                    $man = $emp[$value['14']];
+                }
             }
 //            if($value['12']){
 //                if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
@@ -464,43 +584,12 @@ class ImportService extends Service
             ];
         }
 
-        try{
-            DB::beginTransaction();
-            if(! empty($insert)) Customer::insert($insert);
-
-            //获取上一次所有id
-            $last_insert_id = Customer::where('crt_time',$time)
-                ->where('crt_time',$time)
-                ->where('depart_id',$head)
-                ->where('top_depart_id',$head)
-                ->where('crt_id',$user['id'])
-                ->select('id')->get()->toArray();
-            $last_insert_id = array_column($last_insert_id,'id');
-
-            //组织数据 写入与主表的关联id
-            $insert_detail_1 = [];
-            foreach ($insert_detail as $key => $value){
-                if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
-                $value['customer_id'] = $last_insert_id[$key];
-                $insert_detail_1[] = $value;
-            }unset($insert_detail);
-            $insert_detail_2 = [];
-            foreach ($insert_detail2 as $key => $value){
-                if(empty($value['data_id'])) continue;
-                $value['customer_id'] = $last_insert_id[$key];
-                $insert_detail_2[] = $value;
-            }unset($insert_detail2);
-
-            if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
-            if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
-
-            DB::commit();
-        }catch (\Exception $e){
-            DB::rollBack();
-            return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
+        if(! empty($error)){
+            $error = implode(',', $error);
+            return [false, $error];
         }
 
-        return [true, ''];
+        return [true, [$insert, $insert_detail, $insert_detail2]];
     }
 
     function convertExcelCellToDate($cellValue) {
@@ -1410,7 +1499,7 @@ class ImportService extends Service
         unset($array[0]);
         if(empty($array)) return [false, '导入数据不能为空'];
 
-        $shop_name = $product_code = $counted_number = [];
+        $shop_name = $product_code = [];
         foreach ($array as $key => $value){
             $rowData = array_filter($value);
             if (empty($rowData)) {
@@ -1432,40 +1521,74 @@ class ImportService extends Service
                     return [false, '产品编号:' . $value[1] .'已存在'];
                 }
                 if(! is_numeric($value[2])) return [false, '* 盘点数量请填写正确的数值'];
+
+                if(! empty($value[3])){
+                    $bool = $this->checkNumber($value[3]);
+                    if(! $bool) return [false, '盘点单价请输入大于等于0的两位小数的数字'];
+                }
             }
         }
         if(count($shop_name) != 1) return [false, '只允许单门店盘点'];
 
-        $model = Product::ProductClear($user,[]);
-        $product_map = $model->whereIn('code',$product_code)
+        //门店信息
+        $depart = Depart::whereIn('title',$shop_name)
             ->where('del_time',0)
-            ->pluck('id','code')
-            ->toArray();
-
-        $depart_map = Depart::whereIn('title',$shop_name)
+            ->where('parent_id',0)
+            ->select('id','title','basic_type_id')
+            ->first();
+        if(empty($depart)) return [false, "门店:" .$shop_name . "不存在或已被删除"];
+        $depart = $depart->toArray();
+        $top_depart_id = $depart['id'];
+        $basic_type_id = $depart['basic_type_id'];
+
+        //门店的仓库
+        $storehouse = Storehouse::where('top_depart_id', $top_depart_id)
             ->where('del_time',0)
-            ->pluck('id','title')
-            ->toArray();
-        $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
+            ->select('id')
+            ->first();
+        if(empty($storehouse)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
+        $storehouse = $storehouse->toArray();
+        $storehouse_id = $storehouse['id'];
+        if(empty($storehouse_id)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
+
+        //产品
+        $model = Product::ProductClear($user,[]);
+        $product = $model->whereIn('code',$product_code)
             ->where('del_time',0)
-            ->pluck('id','top_depart_id')
-            ->toArray();
+            ->select('id','code','retail_price')
+            ->get()->toArray();
+        $product_map = array_column($product,'id','code');
+        $product_map_2 = array_column($product,'retail_price','id');
+
+        //产品使用价格
+        $product_use = [];
+        if(! empty($basic_type_id)){
+            $product_id = array_column($product,'id');
+            $service = new ProductService();
+            $detail_map = $service->getProductPrice($product_id, 1, $basic_type_id);
+            foreach ($detail_map as $key => $value){
+                if(isset($value[$basic_type_id]) && ! empty($value[$basic_type_id]['price'])) {
+                    $product_use[$key] = $value[$basic_type_id]['price'];
+                }else{
+                    $product_use[$key] = $product_map_2[$key] ?? 0;
+                }
+            }
+        }
 
         $time = time();
-
-        $top_depart_id = $storehouse_id = 0;
         $product_submit = $product_id = $sub = [];
         foreach ($array as $value){
-            if($value[0]){
-                $depart_tmp = $depart_map[$value[0]] ?? 0;
-                if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
-                if(! $top_depart_id) $top_depart_id = $depart_tmp;
-                $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
-                if(! $storehouse_id) $storehouse_id = $storehouse_tmp;
-            }
             $product_tmp = $product_map[$value[1]] ?? 0;
             if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
-            if(! $storehouse_id) return [false, '门店下仓库信息未找到'];
+
+            if(isset($value[3])){
+                $bool = $this->checkNumber($value[3]);
+                if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
+                $price = $value[3];
+            }else{
+                if(! isset($product_use[$product_tmp]) || floatval($product_use[$product_tmp]) < 0) return [false, "产品编码:" . $value[1] . "获取单价所得小于0,请检查门店所设置价格以及产品零售价"];
+                $price = $product_use[$product_tmp];
+            }
 
             $key = $product_tmp . ',' . $storehouse_id;
             if(! isset($product_submit[$key])) $product_submit[$key] = 0;
@@ -1480,7 +1603,8 @@ class ImportService extends Service
                 'counted_num' => $value[2],
                 'book_num' => 0,
                 'final_num' => 0,
-                'inventory_id' => 0
+                'inventory_id' => 0,
+                'price' => $price,
             ];
         }
 
@@ -1512,6 +1636,9 @@ class ImportService extends Service
                 $sub[$key]['final_num'] = $value['counted_num'];
             }
 
+            $final_amount = bcmul($sub[$key]['final_num'], $value['price'],2);
+            $sub[$key]['final_amount'] = $final_amount;
+
             if($sub[$key]['final_num'] < 0) {
                 $product_reduce_id[] = $value['product_id'];
                 $product_submit_reduce[$keys] = $sub[$key]['final_num'];

+ 11 - 4
app/Service/InventoryService.php

@@ -39,7 +39,8 @@ class InventoryService extends Service
                         'counted_num' => $value['counted_num'],
                         'final_num' => $value['final_num'],
                         'price' => $value['price'] ?? 0,
-                        'inventory_id' => $inventory_model->id
+                        'inventory_id' => $inventory_model->id,
+                        'final_amount' => $value['final_amount'] ?? 0,
                     ];
                 }
                 InventorySub::insert($sub);
@@ -96,7 +97,8 @@ class InventoryService extends Service
                         'counted_num' => $value['counted_num'],
                         'final_num' => $value['final_num'],
                         'price' => $value['price'] ?? 0,
-                        'inventory_id' => $inventory_model->id
+                        'inventory_id' => $inventory_model->id,
+                        'final_amount' => $value['final_amount'] ?? 0,
                     ];
                 }
                 InventorySub::insert($sub);
@@ -157,7 +159,7 @@ class InventoryService extends Service
 
         $sub = InventorySub::where('del_time',0)
             ->where('inventory_id', $data['id'])
-            ->select('product_id','counted_num','final_num','price','book_num')
+            ->select('product_id','counted_num','final_num','price','book_num','final_amount')
             ->get()->toArray();
         $map = (new ProductService())->getProductDetail(array_column($sub,'product_id'));
         foreach ($sub as $key => $value){
@@ -191,7 +193,7 @@ class InventoryService extends Service
                 'del_time'=> $time
             ]);
 
-            $is_check_stock = OrderInventoryStock::where('order_number', $data['order_number'])
+            $is_check_stock = OrderInventoryStock::where('order_number', $inventory['order_number'])
                     ->where('del_time',0)
                     ->value('is_check_stock') ?? 0;
 
@@ -298,6 +300,11 @@ class InventoryService extends Service
                 if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
             }
 
+            if(isset($value['final_amount'])){
+                $bool = $this->checkNumber($value['final_amount']);
+                if(! $bool) return [false, '盈亏金额不合法,需是大于等于0的两位数值'];
+            }
+
             $key = $value['product_id'] . ',' .$data['storehouse_id'];
             if(! isset($product_submit[$key])) $product_submit[$key] = 0;
 

+ 8 - 2
app/Service/ProductService.php

@@ -1008,7 +1008,7 @@ class ProductService extends Service
     }
 
     //获取产品使用价格
-    public function getProductPrice($product_id = [], $type = 1){
+    public function getProductPrice($product_id = [], $type = 1, $basic_type_id_for = 0){
         if(! is_array($product_id)) $product_id = [$product_id];
         //type 1 采购    2 合同 和 活动包
 
@@ -1023,6 +1023,9 @@ class ProductService extends Service
                 ->where('a.start_time','<=',$time)
                 ->where('a.end_time','>=',$time)
                 ->where('b.type',ProductActivity::type_two)
+                ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
+                    return $query->where('a.basic_type_id',$basic_type_id_for);
+                })
                 ->select('a.product_id','a.basic_type_id','a.price')
                 ->get()->toArray();
             foreach ($activity as $value){
@@ -1031,6 +1034,9 @@ class ProductService extends Service
             //分社价 没有供应商活动价格使用分社价格
             $detail = ProductPriceDetail::where('del_time',0)
                 ->whereIn('product_id',$product_id)
+                ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
+                    return $query->where('basic_type_id',$basic_type_id_for);
+                })
                 ->select('product_id','basic_type_id','price')
                 ->get()->toArray();
             foreach ($detail as $value){
@@ -1303,7 +1309,7 @@ class ProductService extends Service
         return $product;
     }
 
-    public function fillProductPrice($detail_map, $value, $top_depart,$basic_map){
+    public function fillProductPrice($detail_map, $value, $top_depart,$basic_map = []){
         $tmp = [];
         if(isset($detail_map[$value['id']])){
             $d = $detail_map[$value['id']];

+ 4 - 0
config/excel/inventory.php

@@ -18,4 +18,8 @@ return [
         'key' => 'counted_num',
         'value' => '* 盘点数量',
     ],
+    [
+        'key' => 'price',
+        'value' => '盘点单价',
+    ],
 ];