cqpCow hai 1 ano
pai
achega
303b62c759

+ 74 - 0
app/Http/Controllers/Api/SupplierController.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\SupplierService;
+use Illuminate\Http\Request;
+
+class SupplierController extends BaseController
+{
+    //供应商
+    public function customerAdd(Request $request)
+    {
+        $service = new SupplierService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerAdd($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerEdit(Request $request)
+    {
+        $service = new SupplierService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerEdit($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerDel(Request $request)
+    {
+        $service = new SupplierService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerDel($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerList(Request $request)
+    {
+        $service = new SupplierService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerList($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerDetail(Request $request)
+    {
+        $service = new SupplierService();
+        list($status,$data) = $service->customerDetail($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 41 - 0
app/Model/Supplier.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Model;
+
+use App\Scopes\DepartmentScope;
+use Illuminate\Database\Eloquent\Model;
+
+class Supplier extends Model
+{
+//    protected $fillable = [];
+    protected $guarded = [];
+    protected $table = "supplier"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const is_main = 1;//是否总供应商
+
+    public static $user = [];
+    public static $search = [];
+    public static $is_search = false;
+    const range_function = 'supplierRange';
+
+    public function __construct(array $attributes = [])
+    {
+        if(! empty($attributes['userData'])) {
+            self::$user = $attributes['userData'];
+            self::$user['range_function'] = self::range_function;
+            self::$user['is_search_main'] = 1;
+            self::$search = $attributes['search'] ?? [];
+            self::$is_search = true;
+        }
+        parent::__construct($attributes);
+    }
+
+    protected static function boot(){
+        parent::boot();
+        if(self::$is_search){
+            static::addGlobalScope(new DepartmentScope(self::$user, self::$search));
+        }
+    }
+}

+ 19 - 0
app/Model/SupplierInfo.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class SupplierInfo extends Model
+{
+    protected $table = "supplier_info"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const type_one = 1; // 可见部门
+    const type_two = 2; // 可见人
+    public static $type = [
+        self::type_one,
+        self::type_two,
+    ];
+}

+ 2 - 0
app/Scopes/DepartmentScope.php

@@ -48,6 +48,8 @@ class DepartmentScope implements Scope
         $id = [];
         if($is_see && $is_function_range) $id = RangeService::$range_function($this->user);
         $builder->orWhereIn('id', $id);
+
+        if(! empty($this->user['is_search_main'])) $builder->orWhere('is_main', '>',0);
     }
 
     function hasMethod($class, $methodName)

+ 11 - 5
app/Service/CheckService.php

@@ -4,6 +4,7 @@ namespace App\Service;
 
 use App\Model\Construction;
 use App\Model\ConstructionProductInfo;
+use App\Model\Depart;
 use App\Model\InOutRecord;
 use App\Model\InvoiceOrder;
 use App\Model\InvoiceOrderInfo;
@@ -16,6 +17,7 @@ use App\Model\ReturnExchangeOrderProductInfo;
 use App\Model\SalesOrder;
 use App\Model\SalesOrderProductInfo;
 use App\Model\Setting;
+use App\Model\Supplier;
 use Illuminate\Support\Facades\DB;
 
 class CheckService extends Service
@@ -163,8 +165,11 @@ class CheckService extends Service
         return [true,''];
     }
 
-    public function createPurchaseOrderSales($data, $order){//todo
-        if(empty($order['is_purchase_to_main'])) return [true, ''];
+    public function createPurchaseOrderSales($data, $order){
+        if(empty($order['supplier'])) return [true, ''];
+        $is_create = Supplier::where('id',$order['supplier'])->value('is_main');
+        if(empty($is_create)) return [true, ''];
+
         $prefix = SalesOrder::$prefix[SalesOrder::Model_type_two];
         $order_number = OrderNoService::createSalesOrderNumber($prefix);
         $product = PurchaseOrderInfo::where('del_time',0)
@@ -172,13 +177,14 @@ class CheckService extends Service
             ->get()->toArray();
         if(empty($product)) return [false, '采购订单产品数据不能为空'];
 
+        $depart_id = Depart::where('del_time',0)->where('parent_id',0)->where('is_main',1)->value('id');
         $model = new SalesOrder();
         $model->model_type = SalesOrder::Model_type_two;
         $model->sales_order_type = SalesOrder::Order_type_one;
         $model->order_number = $order_number;
         $model->crt_id = $order['crt_id'];
-        $model->depart_id = $order['depart_id'];
-        $model->top_depart_id = $order['top_depart_id'];
+        $model->depart_id = $depart_id;
+        $model->top_depart_id = $depart_id;
         $model->other_fee = $order['other_fee'];
         $model->discount_fee = $order['discount_fee'];
         $model->contract_fee = $order['purchase_total'];
@@ -396,7 +402,7 @@ class CheckService extends Service
             ->when(! empty($number_symbol), function ($query) use ($number_symbol) {
                 return $query->where('number', $number_symbol,0);
             })
-            ->select('number','crt_time','product_id');
+            ->select('number','crt_time','product_id','storehouse_id','top_depart_id');
         if(! empty($latest)) {
             $t = $latest->toArray();
             $model->where('crt_time',$t['crt_time']);

+ 1 - 1
app/Service/ConstructionService.php

@@ -279,7 +279,7 @@ class ConstructionService extends Service
         $construction['sales_order_number'] = $sales;
         $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
         $construction['customer_title'] = $customer_title;
-        $construction['storehouse_title'] = Storehouse::where('id',$data['storehouse_id'])->value('title');
+        $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
         $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
         $construction['customer_contact_title'] = $emp_title;
         $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];

+ 2 - 2
app/Service/CustomerService.php

@@ -44,8 +44,8 @@ class CustomerService extends Service
             $model->importance = $data['importance'] ?? '';
             $model->company = $data['company'] ?? '';
             $model->company_short_name = $data['company_short_name'] ?? '';
-            $model->depart_id = $data['depart_id'] ?? 0;
-            $model->top_depart_id = $data['top_depart_id'] ?? 0;
+//            $model->depart_id = $data['depart_id'] ?? 0;
+//            $model->top_depart_id = $data['top_depart_id'] ?? 0;
             $model->state_type = $data['state_type'] ?? 0;
             $model->customer_state = $data['customer_state'] ?? 0;
             $model->customer_grade = $data['customer_grade'] ?? 0;

+ 13 - 14
app/Service/InvoiceOrderService.php

@@ -2,6 +2,7 @@
 
 namespace App\Service;
 
+use App\Model\BasicType;
 use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\InvoiceOrder;
@@ -53,11 +54,6 @@ class InvoiceOrderService extends Service
                         'product_id' => $value['product_id'],
                         'number' => $value['number'],
                         'mark' => $value['mark'] ?? '',
-                        'title' => $value['title'] ?? '',
-                        'code' => $value['code'] ?? '',
-                        'size' => $value['size'] ?? '',
-                        'unit' => $value['unit'] ?? 0,
-                        'bar_code' => $value['bar_code'] ?? '',
                     ];
                 }
                 InvoiceOrderInfo::insert($sub);
@@ -114,11 +110,6 @@ class InvoiceOrderService extends Service
                         'product_id' => $value['product_id'],
                         'number' => $value['number'],
                         'mark' => $value['mark'] ?? '',
-                        'title' => $value['title'] ?? '',
-                        'code' => $value['code'] ?? '',
-                        'size' => $value['size'] ?? '',
-                        'unit' => $value['unit'] ?? 0,
-                        'bar_code' => $value['bar_code'] ?? '',
                     ];
                 }
                 InvoiceOrderInfo::insert($sub);
@@ -146,7 +137,7 @@ class InvoiceOrderService extends Service
 
         $order = InvoiceOrder::where('order_number',$data['order_number'])
             ->where('del_time',0)
-            ->select('id','order_number','sales_order_id','send','send_time','take','take_address','take_phone','logistics_company','logistics_number','crt_id','mark','state','crt_time')
+            ->select('id','order_number','sales_order_id','send','send_time','take','take_address','take_phone','logistics_company','logistics_number','crt_id','mark','state','crt_time','storehouse_id')
             ->first();
         if(empty($order)) return [false, '发货订单不存在或已被删除'];
         $order = $order->toArray();
@@ -160,9 +151,17 @@ class InvoiceOrderService extends Service
             ->toArray();
         $info = InvoiceOrderInfo::where('del_time',0)
             ->where('order_number',$data['order_number'])
-            ->select('id','order_number','storehouse_id','product_id','number','mark','title','code','size','unit','bar_code','invoice_id')
             ->get()->toArray();
-        $order['product'] = $info;
+        $map = (new ProductService())->getProductDetail(array_column($info,'product_id'));
+        foreach ($info as $value){
+            $tmp = $map[$value['product_id']] ?? [];
+            $value['title'] = $tmp['title'] ?? "";
+            $value['code'] = $tmp['code'] ?? "";
+            $value['size'] = $tmp['size'] ?? "";
+            $value['unit'] = $tmp['unit'] ?? "";
+            $value['bar_code'] = $tmp['bar_code'] ?? "";
+            $order['product'][] = $value;
+        }
         $order['send_name'] = $emp_map[$order['send']] ?? '';
         $order['crt_name'] = $emp_map[$order['crt_id']] ?? '';
         $order['crt_time'] = $order['crt_time'] ? date("Y-m-d H:i:s",$order['crt_time']): '';
@@ -271,7 +270,7 @@ class InvoiceOrderService extends Service
     public function orderRule(&$data, $user, $is_check = true){
         if($this->isEmpty($data,'send')) return [false,'发货人不能为空!'];
         if($this->isEmpty($data,'product')) return [false,'产品不能为空!'];
-        if(! empty($data['storehouse_id'])) return [false,'请选择仓库'];
+        if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
         if(! empty($data['send_time'])) $data['send_time'] = $this->changeDateToDateMin($data['send_time']);
         if(! empty($data['sales_order_id'])) {
             $sales = SalesOrder::where('del_time',0)

+ 29 - 21
app/Service/PurchaseOrderService.php

@@ -2,13 +2,14 @@
 
 namespace App\Service;
 
+use App\Model\BasicType;
 use App\Model\Depart;
 use App\Model\Employee;
-use App\Model\Product;
 use App\Model\PurchaseOrder;
 use App\Model\PurchaseOrderInfo;
 use App\Model\SalesOrder;
 use App\Model\Storehouse;
+use App\Model\Supplier;
 use Illuminate\Support\Facades\DB;
 
 /**
@@ -36,12 +37,11 @@ class PurchaseOrderService extends Service
             $material_model->other_fee = $data['other_fee'] ?? 0;
             $material_model->discount_fee = $data['discount_fee'] ?? 0;
             $material_model->purchase_total = $data['purchase_total'] ?? 0;
-            $material_model->supplier = $data['supplier'] ?? '';
+            $material_model->supplier = $data['supplier'] ?? 0;
             $material_model->purchase_id = $data['purchase_id'] ?? 0;
             $material_model->order_type = $data['order_type'];
             $material_model->sales_order_id = $data['sales_order_id'] ?? 0;
             $material_model->mark = $data['mark'] ?? '';
-            $material_model->is_purchase_to_main = $data['is_purchase_to_main'];
             $material_model->storehouse_id = $data['storehouse_id'];
             $material_model->save();
 
@@ -57,13 +57,9 @@ class PurchaseOrderService extends Service
                         'storehouse_id' => $data['storehouse_id'] ?? 0,
                         'product_id' => $value['product_id'],
                         'number' => $value['number'],
+                        'basic_type_id' => $value['basic_type_id'],
                         'price' => $value['price'],
                         'mark' => $value['mark'] ?? '',
-                        'title' => $value['title'] ?? '',
-                        'code' => $value['code'] ?? '',
-                        'size' => $value['size'] ?? '',
-                        'unit' => $value['unit'] ?? 0,
-                        'bar_code' => $value['bar_code'] ?? '',
                     ];
                 }
                 PurchaseOrderInfo::insert($sub);
@@ -97,12 +93,11 @@ class PurchaseOrderService extends Service
             $material_model->other_fee = $data['other_fee'] ?? 0;
             $material_model->discount_fee = $data['discount_fee'] ?? 0;
             $material_model->purchase_total = $data['purchase_total'] ?? 0;
-            $material_model->supplier = $data['supplier'] ?? '';
+            $material_model->supplier = $data['supplier'] ?? 0;
             $material_model->purchase_id = $data['purchase_id'] ?? 0;
             $material_model->order_type = $data['order_type'];
             $material_model->sales_order_id = $data['sales_order_id'] ?? 0;
             $material_model->mark = $data['mark'] ?? '';
-            $material_model->is_purchase_to_main = $data['is_purchase_to_main'];
             $material_model->crt_id = $user['id'];
             $material_model->storehouse_id = $data['storehouse_id'];
             $material_model->save();
@@ -116,13 +111,9 @@ class PurchaseOrderService extends Service
                         'storehouse_id' => $data['storehouse_id'] ?? 0,
                         'product_id' => $value['product_id'],
                         'number' => $value['number'],
+                        'basic_type_id' => $value['basic_type_id'],
                         'price' => $value['price'],
                         'mark' => $value['mark'] ?? '',
-                        'title' => $value['title'] ?? '',
-                        'code' => $value['code'] ?? '',
-                        'size' => $value['size'] ?? '',
-                        'unit' => $value['unit'] ?? 0,
-                        'bar_code' => $value['bar_code'] ?? '',
                     ];
                 }
                 PurchaseOrderInfo::insert($sub);
@@ -147,10 +138,12 @@ class PurchaseOrderService extends Service
 
         $order = PurchaseOrder::where('order_number',$data['order_number'])
             ->where('del_time',0)
-            ->select('id','order_number','depart_id','mark','state','crt_id','order_type','sales_order_id','supplier','purchase_id','total','other_fee','discount_fee','purchase_total','crt_time','is_purchase_to_main')
+            ->select('id','order_number','depart_id','mark','state','crt_id','order_type','sales_order_id','supplier','purchase_id','total','other_fee','discount_fee','purchase_total','crt_time','storehouse_id')
             ->first();
         if(empty($order)) return [false, '采购订单不存在或已被删除'];
         $order = $order->toArray();
+        $order['storehouse_title'] = Storehouse::where('id',$order['storehouse_id'])->value('title');
+        $order['supplier_title'] = Supplier::where('id',$order['supplier'])->value('title');
         $order['order_type_title'] = PurchaseOrder::$order_type[$order['order_type']] ?? '';
         $order['order_type_title'] = PurchaseOrder::$order_type[$order['order_type']] ?? '';
         $order['state_title'] = PurchaseOrder::$name[$order['state']] ?? '';
@@ -161,11 +154,21 @@ class PurchaseOrderService extends Service
             ->toArray();
         $order['purchase_title'] = $emp_map[$order['purchase_id']] ?? '';
 
-        $info = PurchaseOrderInfo::where('del_time',0)
+        $p_info = PurchaseOrderInfo::where('del_time',0)
             ->where('order_number',$data['order_number'])
-            ->select('id','order_number','storehouse_id','product_id','number','mark','price','title','size','code','bar_code','purchase_order_id')
             ->get()->toArray();
-        $order['product'] = $info;
+        $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
+        $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
+        foreach ($p_info as $value){
+            $tmp = $map[$value['product_id']] ?? [];
+            $value['title'] = $tmp['title'] ?? "";
+            $value['code'] = $tmp['code'] ?? "";
+            $value['size'] = $tmp['size'] ?? "";
+            $value['unit'] = $tmp['unit'] ?? "";
+            $value['bar_code'] = $tmp['bar_code'] ?? "";
+            $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
+            $order['product'][] = $value;
+        }
         $order['crt_name'] = $emp_map[$order['crt_id']] ?? '';
         $order['crt_time'] = $order['crt_time'] ? date("Y-m-d H:i:s",$order['crt_time']): '';
 
@@ -210,7 +213,7 @@ class PurchaseOrderService extends Service
      */
     public function getList($data,$user){
         $model = new PurchaseOrder(['userData' => $user, 'search' => $data]);
-        $model = $model->where('del_time',0)->select('order_number','id','depart_id','top_depart_id','mark','crt_time','state','crt_id','order_type','sales_order_id','supplier','purchase_id','purchase_total','discount_fee','other_fee','total','is_purchase_to_main','storehouse_id')
+        $model = $model->where('del_time',0)->select('order_number','id','depart_id','top_depart_id','mark','crt_time','state','crt_id','order_type','sales_order_id','supplier','purchase_id','purchase_total','discount_fee','other_fee','total','storehouse_id')
             ->orderby('id', 'desc');
         if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
         if(! empty($data['depart_id'])) $model->where('depart_id', $data['depart_id']);
@@ -249,6 +252,9 @@ class PurchaseOrderService extends Service
         $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
             ->pluck('title','id')
             ->toArray();
+        $supplier = Supplier::whereIn('id',array_unique(array_column($data['data'],'supplier')))
+            ->pluck('title','id')
+            ->toArray();
         foreach ($data['data'] as $key => $value){
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
             $data['data'][$key]['depart_name'] = $depart_map[$value['depart_id']] ?? '';
@@ -258,6 +264,7 @@ class PurchaseOrderService extends Service
             $data['data'][$key]['state_title'] = PurchaseOrder::$name[$value['state']] ?? '';
             $data['data'][$key]['sales_order_number'] = $sales[$value['sales_order_id']] ?? '';
             $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
+            $data['data'][$key]['supplier_title'] = $supplier[$value['supplier']] ?? '';
         }
 
         return $data;
@@ -271,8 +278,9 @@ class PurchaseOrderService extends Service
      */
     public function orderRule(&$data, $user, $is_check = true){
         if(empty($data['order_type'])) return [false,'采购订单类型不能为空'];
+        if($data['order_type'] == PurchaseOrder::Order_type_two && empty($data['sales_order_id'])) return [false, PurchaseOrder::$order_type[PurchaseOrder::Order_type_two] . '必须选择合同'];
         if($this->isEmpty($data,'product')) return [false,'产品不能为空'];
-        if(! empty($data['storehouse_id'])) return [false,'请选择仓库'];
+        if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
         if(! empty($data['other_fee'])){
             $res = $this->checkNumber($data['other_fee']);
             if(! $res) return [false, '其它费用请输入不超过两位小数并且大于0的数值'];

+ 18 - 0
app/Service/RangeService.php

@@ -8,6 +8,7 @@ use App\Model\ProductRange;
 use App\Model\ReturnExchangeOrderRange;
 use App\Model\SalesOrderInfo;
 use App\Model\SalesOrderRange;
+use App\Model\SupplierInfo;
 
 class RangeService extends Service
 {
@@ -28,6 +29,23 @@ class RangeService extends Service
         return array_unique(array_column($customer_id,'customer_id'));
     }
 
+    //获取供应商可见数据
+    public static function supplierRange($user){
+        $user_id = $user['id'];
+        $depart_id = $user['depart_range'];
+        $type = SupplierInfo::type_two;
+        $type2 = SupplierInfo::type_one;
+        $depart_str = implode(',',$depart_id);
+        $str = "(data_id = $user_id AND type IN({$type})) OR (data_id IN({$depart_str}) AND type = {$type2})";
+
+        // 可见部门 可见人 可以看见
+        $supplier_id = SupplierInfo::where('del_time',0)
+            ->where(function ($query) use($str) {
+                $query->whereRaw($str);
+            })->select('supplier_id')->get()->toArray();
+        return array_unique(array_column($supplier_id,'supplier_id'));
+    }
+
     //获取产品可见数据
     public static function productRange($user){
         $user_id = $user['id'];

+ 317 - 0
app/Service/SupplierService.php

@@ -0,0 +1,317 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Supplier;
+use App\Model\SupplierInfo;
+use App\Model\Depart;
+use App\Model\Employee;
+use Illuminate\Support\Facades\DB;
+
+class SupplierService extends Service
+{
+    /**
+     * 供应商编辑
+     * @param $data
+     * @param $user
+     * @return array
+     */
+    public function customerEdit($data,$user){
+        list($status,$msg) = $this->customerRule($data,$user, false);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = Supplier::where('id',$data['id'])->first();
+            $model->title = $data['title'];
+            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
+            $model->address2 = $data['address2'] ?? '';
+            $model->mark = $data['mark'] ?? '';
+            $model->is_main = $data['is_main'] ?? 0;
+            $model->save();
+            $time = time();
+
+            SupplierInfo::where('del_time',0)
+                ->where('supplier_id',$data['id'])
+                ->update(['del_time' => $time]);
+
+            if(! empty($data['employee'])){
+                $insert = [];
+                foreach ($data['employee'] as $value){
+                    $insert[] = [
+                        'supplier_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => SupplierInfo::type_two,
+                        'crt_time' => $time,
+                    ];
+                }
+                SupplierInfo::insert($insert);
+            }
+
+            if(! empty($data['depart'])){
+                $insert = [];
+                foreach ($data['depart'] as $value){
+                    $insert[] = [
+                        'supplier_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => SupplierInfo::type_one,
+                        'crt_time' => $time,
+                    ];
+                }
+                SupplierInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    /**
+     * 供应商新增
+     * @param $data
+     * @param $user
+     * @return array
+     */
+    public function customerAdd($data,$user){
+        list($status,$msg) = $this->customerRule($data,$user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = new Supplier();
+            $model->title = $data['title'];
+            $model->mobile = $data['mobile'] ?? "";
+            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
+            $model->address2 = $data['address2'] ?? '';
+            $model->crt_id = $user['id'];
+            $model->mark = $data['mark'] ?? '';
+            $model->depart_id = $data['depart_id'] ?? 0;
+            $model->top_depart_id = $data['top_depart_id'] ?? 0;
+            $model->is_main = $data['is_main'] ?? 0;
+            $model->save();
+            $time = time();
+
+            if(! empty($data['employee'])){
+                $insert = [];
+                foreach ($data['employee'] as $value){
+                    $insert[] = [
+                        'supplier_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => SupplierInfo::type_two,
+                        'crt_time' => $time,
+                    ];
+                }
+                SupplierInfo::insert($insert);
+            }
+
+            if(! empty($data['depart'])){
+                $insert = [];
+                foreach ($data['depart'] as $value){
+                    $insert[] = [
+                        'supplier_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => SupplierInfo::type_one,
+                        'crt_time' => $time,
+                    ];
+                }
+                SupplierInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    /**
+     * 供应商删除
+     * @param $data
+     * @return array
+     */
+    public function customerDel($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+
+        try {
+            DB::beginTransaction();
+
+            Supplier::where('id',$data['id'])->update([
+                'del_time'=> time()
+            ]);
+            SupplierInfo::where('del_time',0)
+                ->where('supplier_id',$data['id'])
+                ->update(['del_time' => time()]);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    /**
+     * 供应商详情
+     * @param $data
+     * @return array
+     */
+    public function customerDetail($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+
+        $customer = Supplier::where('del_time',0)
+            ->where('id',$data['id'])
+            ->first();
+        if(empty($customer)) return [false,'供应商不存在或已被删除'];
+        $customer = $customer->toArray();
+        $address = '';
+        if(! empty($customer['address1'])) {
+            $tmp = json_decode($customer['address1'],true);
+            $customer['address1'] = $tmp;
+            $tmp = implode(' ',$tmp);
+            $tmp .= ' ' . $customer['address2'];
+            $address = $tmp;
+        }
+        $customer['address'] = $address;
+
+        $customer_info = SupplierInfo::where('del_time',0)
+            ->where('supplier_id',$customer['id'])
+            ->get()->toArray();
+        $emp_id = [];
+        $emp_id[] = $customer['crt_id'];
+        $depart = [];
+        foreach ($customer_info as $value){
+            if($value['type'] == SupplierInfo::type_two){
+                $emp_id[] = $value['data_id'];
+            }else{
+                $depart[] = $value['data_id'];
+            }
+        }
+        $emp_map = Employee::whereIn('id',array_unique($emp_id))
+            ->pluck('emp_name','id')
+            ->toArray();
+        $depart_map = Depart::whereIn('id',array_unique($depart))
+            ->pluck('title','id')
+            ->toArray();
+        foreach ($customer_info as $value){
+            if($value['type'] == SupplierInfo::type_one){
+                $tmp = [
+                    'id' => $value['data_id'],
+                    'name' => $depart_map[$value['data_id']],
+                ];
+                $customer['depart'][] = $tmp;
+            }elseif ($value['type'] == CustomerInfo::type_two){
+                $tmp = [
+                    'id' => $value['data_id'],
+                    'name' => $emp_map[$value['data_id']] ?? '',
+                ];
+                $customer['employee'][] = $tmp;
+            }
+        }
+        $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
+        $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
+
+        return [true, $customer];
+    }
+
+    /**
+     * 供应商列表
+     * @param $data
+     * @param $user
+     * @return array
+     */
+    public function customerList($data,$user){
+        $model = new Supplier(['userData' => $user,'search'=> $data]);
+        $model = $model->where('del_time',0)
+            ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main')
+            ->orderby('id', 'desc');
+
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['time_type'])) {
+            if($data['time_type'] == 1) {
+                $start = strtotime('today');
+                $end = strtotime('tomorrow') - 1;
+            }elseif ($data['time_type'] == 2){
+                $start = strtotime('this week',strtotime('today'));
+                $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
+            }
+            if(! empty($start) && ! empty($end)) {
+                $model->where('crt_time','>=',$start);
+                $model->where('crt_time','<=',$end);
+            }
+        }
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list);
+
+        return [true, $list];
+    }
+
+    /**
+     * 供应商参数规则
+     * @param $data
+     * @param $is_add
+     * @return array
+     */
+    public function customerRule(&$data, $user, $is_add = true){
+        if(empty($data['title'])) return [false,'供应商名称不能为空'];
+        //所属部门 以及  顶级部门
+        if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
+        $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
+
+        if($is_add){
+            $bool = Supplier::where('del_time',0)
+                ->where('title',$data['title'])
+                ->where('top_depart_id',$data['top_depart_id'])
+                ->exists();
+        }else{
+            if(empty($data['id'])) return [false,'ID不能为空'];
+            $bool = Supplier::where('del_time',0)
+                ->where('id','<>',$data['id'])
+                ->where('title',$data['title'])
+                ->exists();
+        }
+        if($bool) return [false,'供应商名称不能重复'];
+
+        return [true, ''];
+    }
+
+    /**
+     * 拼接数据
+     * @param $data
+     * @return array
+     */
+    public function fillData($data){
+        if(empty($data['data'])) return $data;
+
+        $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
+            ->pluck('title','id')
+            ->toArray();
+        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
+            ->pluck('emp_name','id')
+            ->toArray();
+
+        foreach ($data['data'] as $key => $value){
+            $address = '';
+            if(! empty($value['address1'])) {
+                $tmp = json_decode($value['address1'],true);
+                $tmp = implode(' ',$tmp);
+                $tmp .= ' ' . $value['address2'];
+                $address = $tmp;
+            }
+            $data['data'][$key]['address'] = $address;
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+        }
+
+        return $data;
+    }
+}

+ 7 - 0
routes/api.php

@@ -96,6 +96,13 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('customerDetail', 'Api\CustomerController@customerDetail');
     $route->any('customerGrabbing', 'Api\CustomerController@customerGrabbing');
 
+    //供应商
+    $route->any('supplierList', 'Api\SupplierController@customerList');
+    $route->any('supplierEdit', 'Api\SupplierController@customerEdit');
+    $route->any('supplierAdd', 'Api\SupplierController@customerAdd');
+    $route->any('supplierDel', 'Api\SupplierController@customerDel');
+    $route->any('supplierDetail', 'Api\SupplierController@customerDetail');
+
     //联系人
     $route->any('contactsList', 'Api\ContactsController@contactsList');
     $route->any('contactsEdit', 'Api\ContactsController@contactsEdit');