| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\BasicType;
 
- use App\Model\BasicTypeAllUse;
 
- use App\Model\Construction;
 
- use App\Model\ConstructionInfo;
 
- use App\Model\Customer;
 
- use App\Model\CustomerInfo;
 
- use App\Model\Depart;
 
- use App\Model\Employee;
 
- use App\Model\Inventory;
 
- use App\Model\InvoiceOrder;
 
- use App\Model\OaOrder;
 
- use App\Model\OaOrderSub;
 
- use App\Model\OaOrderSubEmployee;
 
- use App\Model\PaymentReceipt;
 
- use App\Model\PaymentReceiptInfo;
 
- use App\Model\Product;
 
- use App\Model\ProductAdjustment;
 
- use App\Model\PurchaseOrder;
 
- use App\Model\ReturnExchangeOrder;
 
- use App\Model\SalesOrder;
 
- use App\Model\SalesOrderInfo;
 
- use App\Model\SeeRange;
 
- use App\Model\SportsBag;
 
- use Illuminate\Support\Facades\DB;
 
- class RangeService extends Service
 
- {
 
-     //设置可见范围 除了合同
 
-     public function seeRange($data,$user){
 
-         if(empty($data['data_type'])) return [false, "可见范围数据类型不能为空"];
 
-         if(! in_array($data['data_type'], SeeRange::$type)) return [false, "可见范围数据类型错误"];
 
-         if(empty($data['data_id'])) return [false,'可见范围数据ID不能为空'];
 
-         $time = time();
 
-         SeeRange::where('del_time',0)
 
-             ->where('data_type',$data['data_type'])
 
-             ->where('data_id',$data['data_id'])
 
-             ->whereIn('type',[SeeRange::data_one, SeeRange::data_two])
 
-             ->update(['del_time' => $time]);
 
-         if(! empty($data['depart'])){
 
-             $insert = [];
 
-             foreach ($data['depart'] as $value){
 
-                 $insert[] = [
 
-                     'data_id' => $data['data_id'],
 
-                     'data_type' => $data['data_type'],
 
-                     'param_id' => $value,
 
-                     'type' => SeeRange::data_one,
 
-                     'crt_time' => $time,
 
-                 ];
 
-             }
 
-             SeeRange::insert($insert);
 
-         }
 
-         if(! empty($data['employee'])){
 
-             $insert = [];
 
-             foreach ($data['employee'] as $value){
 
-                 $insert[] = [
 
-                     'data_id' => $data['data_id'],
 
-                     'data_type' => $data['data_type'],
 
-                     'param_id' => $value,
 
-                     'type' => SeeRange::data_two,
 
-                     'crt_time' => $time,
 
-                 ];
 
-             }
 
-             SeeRange::insert($insert);
 
-         }
 
-         return [true,''];
 
-     }
 
-     //可见范围删除
 
-     public function RangeDelete($data_id = 0, $data_type = 0){
 
-         if(empty($data_id) || empty($data_type)) return;
 
-         SeeRange::where('del_time',0)
 
-             ->where('data_id',$data_id)
 
-             ->where('data_type',$data_type)
 
-             ->update(['del_time'=> time()]);
 
-     }
 
-     //获取可见范围详情
 
-     public function RangeDetail($data_id = 0, $data_type = 0){
 
-         if(empty($data_id) || empty($data_type)) return [];
 
-         $see = SeeRange::where('del_time',0)
 
-             ->where('data_id',$data_id)
 
-             ->where('data_type',$data_type)
 
-             ->get()->toArray();
 
-         $depart_map = Depart::where('del_time',0)->pluck('title','id')->toArray();
 
-         $emp_map = Employee::where('del_time',0)->pluck('emp_name','id')->toArray();
 
-         $depart = $employee = $depart2 = [];
 
-         foreach ($see as $value){
 
-             if ($value['type'] == SeeRange::data_one){
 
-                 $name = $depart_map[$value['param_id']] ?? "";
 
-                 if(! empty($name)){
 
-                     $tmp = [
 
-                         'id' => $value['param_id'],
 
-                         'name' => $depart_map[$value['param_id']] ?? "",
 
-                     ];
 
-                     $depart[] = $tmp;
 
-                 }
 
-             }elseif ($value['type'] == SeeRange::data_two){
 
-                 $name = $emp_map[$value['param_id']] ?? '';
 
-                 if(! empty($name)){
 
-                     $tmp = [
 
-                         'id' => $value['param_id'],
 
-                         'name' => $emp_map[$value['param_id']] ?? '',
 
-                     ];
 
-                     $employee[] = $tmp;
 
-                 }
 
-             }elseif ($value['type'] == SeeRange::data_three){
 
-                 $name = $depart_map[$value['param_id']] ?? '';
 
-                 if(! empty($name)) {
 
-                     $tmp = [
 
-                         'id' => $value['param_id'],
 
-                         'name' => $depart_map[$value['param_id']] ?? '',
 
-                     ];
 
-                     $depart2[] = $tmp;
 
-                 }
 
-             }
 
-         }
 
-         return [$depart, $employee, $depart2];
 
-     }
 
-     //获取可见范围数据id
 
-     public static function getRangeDataId($user,$data_type){
 
-         $user_id = $user['id'];
 
-         $depart_id = $user['depart_range'];
 
-         $type = SeeRange::data_two;
 
-         $type2 = [SeeRange::data_one,SeeRange::data_three];
 
-         $type2 = implode(',',$type2);
 
-         $depart_str = implode(',',$depart_id);
 
-         if(empty($depart_str)) {
 
-             $string = "param_id = 0";
 
-         }else{
 
-             $string = "param_id IN({$depart_str})";
 
-         }
 
-         // 人为当前用户时, 或部门在当前用户范围内
 
-         $str = "(param_id = $user_id AND type = $type) OR ($string AND type IN ({$type2}))";
 
-         // 可见部门 可见人 可以看见
 
-         $data_id = SeeRange::where('del_time',0)
 
-             ->where('data_type', $data_type)
 
-             ->where(function ($query) use($str) {
 
-                 $query->whereRaw($str);
 
-             })->select('data_id')->get()->toArray();
 
-         return array_unique(array_column($data_id,'data_id'));
 
-     }
 
-     //获取客户可见数据
 
-     public static function customerRange($user,$search){
 
-         // 销售人员/负责人 3协同人  可以看见
 
-         $customer_id = CustomerInfo::where('del_time',0)
 
-            ->where('data_id',$user['id'])
 
-            ->whereIn('type',CustomerInfo::$see_man)
 
-            ->select('customer_id')->get()->toArray();
 
-         $return_id = array_unique(array_column($customer_id,'customer_id'));
 
-         //可见范围id
 
-         $rang_id = Self::getRangeDataId($user,SeeRange::type_one);
 
-         //并和
 
-         $return_id = array_unique(array_merge_recursive($return_id,$rang_id));
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             // 分批查询数据库,避免 IN 查询太长
 
-             $filtered_ids = [];
 
-             foreach (array_chunk($return_id, 500) as $chunk) {
 
-                 $filtered_ids = array_merge($filtered_ids, Customer::whereIn('id', $chunk)
 
-                     ->where('top_depart_id', $search['top_depart_id'])
 
-                     ->where('del_time', 0)
 
-                     ->pluck('id')
 
-                     ->toArray());
 
-             }
 
-             $return_id = $filtered_ids;
 
- //            $id = DB::table('customer')
 
- //                ->where('del_time',0)
 
- //                ->where('top_depart_id',$search['top_depart_id'])
 
- //                ->select('id')->get()->toArray();
 
- //            $id = array_column($id,'id');
 
- //            foreach ($return_id as $key => $value){
 
- //                if(! in_array($value,$id)) unset($return_id[$key]);
 
- //            }
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取施工单可见数据
 
-     public static function constructionRange($user,$search){
 
-         //单据中选择的签订负责协同人
 
-         $construction_id = ConstructionInfo::where('del_time',0)
 
-             ->where('employee_id',$user['id'])
 
-             ->select('construction_id')
 
-             ->get()->toArray();
 
-         $return_id = array_unique(array_column($construction_id,'construction_id'));
 
-         //可见范围id
 
-         $return = Self::getRangeDataId($user,SeeRange::type_two);
 
-         $return_id = array_unique(array_merge_recursive($return_id,$return));
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('construction')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         if(isset($search['is_check'])){
 
-             $args = self::constructionCheck($user,$search);
 
-             $result = Construction::whereIn('id',$return_id)
 
-                 ->when(! empty($args), function ($query) use ($args) {
 
-                     return $query->whereRaw($args);
 
-                 })
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $return_id = array_column($result,'id');
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取发货单可见数据
 
-     public static function invoiceRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_three);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('invoice_order')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         if(isset($search['is_check'])){
 
-             $args = self::invoiceCheck($user,$search);
 
-             $result = InvoiceOrder::whereIn('id',$return_id)
 
-                 ->when(! empty($args), function ($query) use ($args) {
 
-                     return $query->whereRaw($args);
 
-                 })
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $return_id = array_column($result,'id');
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取产品可见数据
 
-     public static function productRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_four);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('product')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取产品不可见数据
 
-     public static function productRangeNot($user,$search){
 
-         //不可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_four);
 
-         //分社管理员
 
-         if(empty($user['is_all_depart']) && ! empty($user['is_manager'])) {
 
-             $depart = array_shift($user['rule_depart']);
 
-             $depart_id = $depart['depart_id'] ?? 0;
 
-             $id = DB::table('product')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$depart_id)
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取采购单可见数据
 
-     public static function purchaseRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_five);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('purchase_order')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         if(isset($search['is_check'])){
 
-             $args = self::purchaseCheck($user,$search);
 
-             $result = PurchaseOrder::whereIn('id',$return_id)
 
-                 ->when(! empty($args), function ($query) use ($args) {
 
-                     return $query->whereRaw($args);
 
-                 })
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $return_id = array_column($result,'id');
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取退换货单可见数据
 
-     public static function returnExchangeOrderRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_six);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('return_exchange_order')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         if(isset($search['is_check'])){
 
-             $args = self::returnExchangeOrderCheck($user,$search);
 
-             $result = ReturnExchangeOrder::whereIn('id',$return_id)
 
-                 ->when(! empty($args), function ($query) use ($args) {
 
-                     return $query->whereRaw($args);
 
-                 })
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $return_id = array_column($result,'id');
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取合同可见数据
 
-     public static function salesOrderRange($user,$search){
 
-         //单据中选择的签订负责协同人
 
-         $sales_order_id = SalesOrderInfo::where('del_time',0)
 
-             ->whereIn('type',SalesOrderInfo::$man)
 
-             ->where('data_id',$user['id'])
 
-             ->select('sales_order_id')
 
-             ->get()->toArray();
 
-         $sales_order_id = array_unique(array_column($sales_order_id,'sales_order_id'));
 
-         $bool = ! empty($search['top_depart_id']) && ! empty($user['is_all_depart']);
 
-         if(! $bool){
 
-             $current_top_depart_id = $user['depart_top'][0] ?? [];
 
-             $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
 
-             //查找是否合同指派了门店
 
-             $dispatch_company = SeeRange::where('del_time',0)
 
-                 ->whereIn('data_id',$sales_order_id)
 
-                 ->where('param_id', $current_top_depart_id)
 
-                 ->where('data_type',SeeRange::type_seven)
 
-                 ->where('type',SeeRange::data_three)
 
-                 ->select('data_id')
 
-                 ->get()->toArray();
 
-             $dispatch_company = array_column($dispatch_company,'data_id');
 
-             // 找出仅在 $array1 中存在的元素
 
-             $diff1 = array_diff($sales_order_id, $dispatch_company);
 
-             $sales_order_id = SalesOrder::whereIn('id', $diff1)
 
-                 ->where('top_depart_id',$current_top_depart_id)
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $sales_order_id = array_column($sales_order_id,'sales_order_id');
 
-             $sales_order_id = array_merge_recursive($sales_order_id,$dispatch_company);
 
-         }
 
-         //指派后 可见范围id
 
-         $return = Self::getRangeDataId($user,SeeRange::type_seven);
 
-         $return_id = array_unique(array_merge_recursive($sales_order_id,$return));
 
-         if($bool){
 
-             $id = DB::table('sales_order')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         if(isset($search['is_check'])){
 
-             $args = self::salesOrderCheck($user,$search);
 
-             $result = SalesOrder::whereIn('id',$return_id)
 
-                 ->when(! empty($args), function ($query) use ($args) {
 
-                     return $query->whereRaw($args);
 
-                 })
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $return_id = array_column($result,'id');
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取特殊采购单可见数据
 
-     public static function purchaseSpecialRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_ten);
 
- //        if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
- //            $id = DB::table('purchase_order')
 
- //                ->where('del_time',0)
 
- //                ->where('top_depart_id',$search['top_depart_id'])
 
- //                ->select('id')->get()->toArray();
 
- //            $id = array_column($id,'id');
 
- //            foreach ($return_id as $key => $value){
 
- //                if(! in_array($value,$id)) unset($return_id[$key]);
 
- //            }
 
- //        }
 
-         return $return_id;
 
-     }
 
-     //获取供应商可见数据
 
-     public static function supplierRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_nine);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('supplier')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         return $return_id;
 
-     }
 
-     //获取活动包可见数据
 
-     public static function sportsBagRange($user,$search){
 
-         //可见范围id
 
-         $return_id = Self::getRangeDataId($user,SeeRange::type_eight);
 
-         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
 
-             $id = DB::table('sports_bag')
 
-                 ->where('del_time',0)
 
-                 ->where('top_depart_id',$search['top_depart_id'])
 
-                 ->select('id')->get()->toArray();
 
-             $id = array_column($id,'id');
 
-             foreach ($return_id as $key => $value){
 
-                 if(! in_array($value,$id)) unset($return_id[$key]);
 
-             }
 
-         }
 
-         return $return_id;
 
-     }
 
-     //产品不可见部门
 
-     public static function productNotSeeRange($product_id){
 
-         $return = [];
 
-         $result = SeeRange::where('del_time',0)
 
-             ->where('data_type', SeeRange::type_four)
 
-             ->whereIn('data_id',$product_id)
 
-             ->where('type',SeeRange::data_one)
 
-             ->select('param_id as depart_id','data_id as product_id')
 
-             ->get()->toArray();
 
-         foreach ($result as $value){
 
-             $return[$value['product_id']][] = $value['depart_id'];
 
-         }
 
-         return $return;
 
-     }
 
-     //产品签订人负责人
 
-     public function salesOrderSearch($data){
 
-         $return1 = $return2 = [];
 
-         if(! empty($data['qd'])){
 
-             $emp_id = Employee::where('del_time',0)
 
-                 ->where('emp_name','LIKE', '%'.$data['qd'].'%')
 
-                 ->select('id')->get()->toArray();
 
-             $emp_id = array_column($emp_id,'id');
 
-             //单据中选择的签订人
 
-             $sales_order_id = SalesOrderInfo::where('del_time',0)
 
-                 ->where('type',SalesOrderInfo::type_one)
 
-                 ->whereIn('data_id',$emp_id)
 
-                 ->select('sales_order_id')
 
-                 ->get()->toArray();
 
-             $return1 = array_unique(array_column($sales_order_id,'sales_order_id'));
 
-         }
 
-         if(! empty($data['fz'])){
 
-             $emp_id = Employee::where('del_time',0)
 
-                 ->where('emp_name','LIKE', '%'.$data['fz'].'%')
 
-                 ->select('id')->get()->toArray();
 
-             $emp_id = array_column($emp_id,'id');
 
-             //单据中选择的负责人
 
-             $sales_order_id = SalesOrderInfo::where('del_time',0)
 
-                 ->where('type',SalesOrderInfo::type_two)
 
-                 ->whereIn('data_id',$emp_id)
 
-                 ->select('sales_order_id')
 
-                 ->get()->toArray();
 
-             $return2 = array_unique(array_column($sales_order_id,'sales_order_id'));
 
-         }
 
-         if(! empty($data['qd']) && ! empty($data['fz'])){
 
-             $return = array_intersect($return1, $return2);
 
-         }elseif(!empty($data['qd'])){
 
-             $return = $return1;
 
-         }else{
 
-             $return = $return2;
 
-         }
 
-         return $return;
 
-     }
 
-     //指派门店
 
-     public function salesOrderZpSearch($data){
 
-         $return = SeeRange::where('del_time',0)
 
-             ->where('param_id',$data['zp'])
 
-             ->where('data_type',SeeRange::type_seven)
 
-             ->where('type',SeeRange::data_three)
 
-             ->select('data_id')
 
-             ->get()->toArray();
 
-         return array_column($return,'data_id');
 
-     }
 
-     //客户创建人
 
-     public function salesOrderCustomerCrtSearch($user,$data){
 
-         $emp_id = Employee::where('del_time',0)
 
-             ->where('emp_name','LIKE', '%'.$data['customer_crt_name'].'%')
 
-             ->select('id')->get()->toArray();
 
-         $emp_id = array_column($emp_id,'id');
 
-         $model2 = Customer::Clear($user,$data);
 
-         $customer = $model2->where('del_time',0)
 
-             ->whereIn('crt_id', $emp_id)
 
-             ->select('id')
 
-             ->get()->toArray();
 
-         return array_column($customer,'id');
 
-     }
 
-     //收付款人搜索
 
-     public function paymentReceiptSearch($data){
 
-         $emp_id = Employee::where('del_time',0)
 
-             ->where('emp_name','LIKE', '%'.$data['belong'].'%')
 
-             ->select('id')->get()->toArray();
 
-         $emp_id = array_column($emp_id,'id');
 
-         //单据中选择的签订人
 
-         $id = PaymentReceiptInfo::where('del_time',0)
 
-             ->where('type',PaymentReceiptInfo::type_two)
 
-             ->whereIn('data_id',$emp_id)
 
-             ->select('payment_receipt_id')
 
-             ->get()->toArray();
 
-         return array_unique(array_column($id,'payment_receipt_id'));
 
-     }
 
-     //创建人
 
-     public function crtNameSearch($data){
 
-         $emp_id = Employee::where('del_time',0)
 
-             ->where('emp_name','LIKE', '%'.$data['crt_name'].'%')
 
-             ->select('id')->get()->toArray();
 
-         return array_column($emp_id,'id');
 
-     }
 
-     public function crtContactSearch($data){
 
-         $id = CustomerInfo::where('del_time',0)
 
-             ->where('type',CustomerInfo::type_one)
 
-             ->where('contact_info','LIKE', '%'.$data['title_t'].'%')
 
-             ->select('customer_id')->get()->toArray();
 
-         return array_column($id,'customer_id');
 
-     }
 
-     //负责人
 
-     public function customerSearch($data){
 
-         $emp_id = Employee::where('del_time',0)
 
-             ->where('emp_name','LIKE', '%'.$data['fz'].'%')
 
-             ->select('id')->get()->toArray();
 
-         $emp_id = array_column($emp_id,'id');
 
-         //单据中选择的负责人
 
-         $customer_id = CustomerInfo::where('del_time',0)
 
-             ->where('type',CustomerInfo::type_two)
 
-             ->whereIn('data_id',$emp_id)
 
-             ->select('customer_id')
 
-             ->get()->toArray();
 
-         return array_unique(array_column($customer_id,'customer_id'));;
 
-     }
 
-     //获取可见人施工单
 
-     public function RangeConstructionEmpDetail($data_id = 0){
 
-         if(empty($data_id)) return [];
 
-         $see = ConstructionInfo::where('del_time',0)
 
-             ->whereIn('construction_id',$data_id)
 
-             ->where('type',ConstructionInfo::type_three)
 
-             ->get()->toArray();
 
-         $emp_map = Employee::where('del_time',0)
 
-             ->whereIn('id',array_column($see,'employee_id'))
 
-             ->pluck('emp_name','id')->toArray();
 
-         $employee = [];
 
-         foreach ($see as $value){
 
-             $name = $emp_map[$value['employee_id']] ?? '';
 
-             if(! empty($name)){
 
-                 $tmp = [
 
-                     'id' => $value['employee_id'],
 
-                     'emp_name' => $emp_map[$value['employee_id']] ?? '',
 
-                 ];
 
-                 $employee[$value['construction_id']][] = $tmp;
 
-             }
 
-         }
 
-         return $employee;
 
-     }
 
-     //客户类型
 
-     public function customerBasicTypeSearch($customer_type, $type){
 
-         $result = BasicType::where('del_time',0)
 
-             ->whereIn('type',$type)
 
-             ->where('title', $customer_type)
 
-             ->select('id')->get()->toArray();
 
-         return array_column($result,'id');
 
-     }
 
-     public function customerBasicTypeAllUseSearch($customer_type, $type){
 
-         $result = BasicTypeAllUse::where('del_time',0)
 
-             ->whereIn('type',$type)
 
-             ->where('title', $customer_type)
 
-             ->select('id')->get()->toArray();
 
-         return array_column($result,'id');
 
-     }
 
-     //全部 待审 已审核 -----------------------------------------------
 
-     public static function sportsBagCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForSportsCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", SportsBag::$wait_check);
 
-             $args = "(state IN (" . implode(",", SportsBag::$wait_check) ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(SportsBag::$wait_check, [SportsBag::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state = ". SportsBag::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForSportsCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核
 
-         $args = "(state = " . SportsBag::STATE_ONE . ")";
 
-         $data = SportsBag::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function paymentReceiptCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForPaymentCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", SportsBag::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $id = implode(",", $id);
 
-                 $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PaymentReceipt::STATE_ONE]));
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state = ". PaymentReceipt::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForPaymentCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核
 
-         $args = "(state = " . PaymentReceipt::STATE_ONE . ")";
 
-         $data = PaymentReceipt::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function salesOrderCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForSalesCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", SalesOrder::$wait_check);
 
-             $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(SalesOrder::$wait_check, [SalesOrder::State_one]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审 线上订单的已审核是2  其它是 3
 
-             $args = SalesOrder::search;
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForSalesCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核合同
 
-         $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state = " . SalesOrder::State_one . ")";
 
-         $data = SalesOrder::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function invoiceCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForInvoiceCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", InvoiceOrder::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(InvoiceOrder::$wait_check, [InvoiceOrder::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state = ". InvoiceOrder::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForInvoiceCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . InvoiceOrder::STATE_ONE . ")";
 
-         $data = InvoiceOrder::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function returnExchangeOrderCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForReturnExchangeCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", ReturnExchangeOrder::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(ReturnExchangeOrder::$wait_check, [ReturnExchangeOrder::State_one]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state = ". ReturnExchangeOrder::State_two . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForReturnExchangeCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . ReturnExchangeOrder::State_one . ")";
 
-         $data = ReturnExchangeOrder::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function constructionCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForConstructionCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", Construction::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(Construction::$wait_check, [Construction::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state >= ". Construction::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForConstructionCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . Construction::STATE_ONE . ")";
 
-         $data = Construction::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function purchaseCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForPurchaseCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", PurchaseOrder::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(PurchaseOrder::$wait_check, [PurchaseOrder::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state >= ". PurchaseOrder::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForPurchaseCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . PurchaseOrder::STATE_ONE . ")";
 
-         $data = PurchaseOrder::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function inventoryCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getWaitForinventoryCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", Inventory::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(Inventory::$wait_check, [Inventory::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state >= ". Inventory::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getWaitForinventoryCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . Inventory::STATE_ONE . ")";
 
-         $data = Inventory::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     public static function productAdjustmentCheck($user,$search){
 
-         $args = "";
 
-         if($search['is_check'] == 1) {
 
-             list($status, $id) = self::getProductAdjustmentCheck($user,$search);
 
-             //待审核
 
-             $check = implode(",", ProductAdjustment::$wait_check);
 
-             $args = "(state IN (" . $check ."))";
 
-             if($status) {
 
-                 $wait_for_me = $search['wait_for_me'] ?? 0;
 
-                 $check_2 = implode(',', array_diff(ProductAdjustment::$wait_check, [ProductAdjustment::STATE_ONE]));
 
-                 $id = implode(",", $id);
 
-                 if($wait_for_me){
 
-                     if(empty($id)) {
 
-                         $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }else{
 
-                         $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
 
-                     }
 
-                 }
 
-             }
 
-         }elseif($search['is_check'] == 2){
 
-             //已审
 
-             $args = "(state >= ". ProductAdjustment::STATE_TWO . ")";
 
-         }
 
-         return $args;
 
-     }
 
-     private static function getProductAdjustmentCheck($user, $search){
 
-         if(! isset($search['wait_for_me'])) return [false, []];
 
-         //获取待审核数据
 
-         $args = "(state = " . ProductAdjustment::STATE_ONE . ")";
 
-         $data = ProductAdjustment::where('del_time',0)
 
-             ->whereRaw($args)
 
-             ->select('order_number','id')
 
-             ->get()->toArray();
 
-         if(empty($data)) return [true, []];
 
-         list($status,$msg) = self::getWaitCommon($data,$user);
 
-         return [$status, $msg];
 
-     }
 
-     private static function getWaitCommon($data,$user){
 
-         $data_map = array_column($data,'id','order_number');
 
-         //查找对应审批流数据
 
-         $orderNoGroups = OaOrder::whereIn('order_no', array_column($data,'order_number'))
 
-             ->get()
 
-             ->groupBy('order_no');
 
-         $maxIds = $orderNoGroups->map(function ($group) {
 
-             return $group->max('id');
 
-         });
 
-         $map = $maxIds->toArray();
 
-         if(empty($map)) return [true, []];
 
-         $oa_order_id = array_values($map);
 
-         $map2 = array_flip($map);
 
-         unset($map);
 
-         //获取审批流下的人
 
-         $list = OaOrderSub::whereIn('oa_order_id', $oa_order_id)
 
-             ->whereIn('state',[0,1])
 
-             ->select('id','state','oa_order_id')
 
-             ->orderBy('id', 'desc')
 
-             ->get()->toArray();
 
-         $subEmployeeList = OaOrderSubEmployee::whereIn('oa_order_id', $oa_order_id)
 
-             ->where('employee_id',$user['id'])
 
-             ->get()->toArray();
 
-         if(empty($subEmployeeList)) return [true, []];
 
-         //每条数据对应的人
 
-         $emp_id_key_list = [];
 
-         foreach ($subEmployeeList as $v) {
 
-             $emp_id_key_list[$v['oa_order_sub_id']][] = $v['employee_id'];
 
-         }
 
-         unset($subEmployeeList);
 
-         $flag = $id = [];
 
-         foreach ($list as $v) {
 
-             //不存在单号或者已存在单号返回数据
 
-             if(empty($v['oa_order_id']) || isset($flag[$v['oa_order_id']])) continue;
 
-             $emp_tmp = $emp_id_key_list[$v['id']] ?? [];
 
-             $flag[$v['oa_order_id']] = $emp_tmp;
 
-             $order_number = $map2[$v['oa_order_id']] ?? "";
 
-             $sales_id = $data_map[$order_number] ?? 0;
 
-             if(in_array($user['id'], $emp_tmp)) $id[] = $sales_id;
 
-         }
 
-         unset($flag);
 
-         return [true, $id];
 
-     }
 
-     //全部 待审 已审核 -----------------------------------------------
 
- }
 
 
  |