RangeService.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\BasicTypeAllUse;
  5. use App\Model\Construction;
  6. use App\Model\ConstructionInfo;
  7. use App\Model\Customer;
  8. use App\Model\CustomerInfo;
  9. use App\Model\Depart;
  10. use App\Model\Employee;
  11. use App\Model\Inventory;
  12. use App\Model\InvoiceOrder;
  13. use App\Model\OaOrder;
  14. use App\Model\OaOrderSub;
  15. use App\Model\OaOrderSubEmployee;
  16. use App\Model\PaymentReceipt;
  17. use App\Model\PaymentReceiptInfo;
  18. use App\Model\Product;
  19. use App\Model\ProductAdjustment;
  20. use App\Model\PurchaseOrder;
  21. use App\Model\ReturnExchangeOrder;
  22. use App\Model\SalesOrder;
  23. use App\Model\SalesOrderInfo;
  24. use App\Model\SeeRange;
  25. use App\Model\SportsBag;
  26. use Illuminate\Support\Facades\DB;
  27. class RangeService extends Service
  28. {
  29. //设置可见范围 除了合同
  30. public function seeRange($data,$user){
  31. if(empty($data['data_type'])) return [false, "可见范围数据类型不能为空"];
  32. if(! in_array($data['data_type'], SeeRange::$type)) return [false, "可见范围数据类型错误"];
  33. if(empty($data['data_id'])) return [false,'可见范围数据ID不能为空'];
  34. $time = time();
  35. SeeRange::where('del_time',0)
  36. ->where('data_type',$data['data_type'])
  37. ->where('data_id',$data['data_id'])
  38. ->whereIn('type',[SeeRange::data_one, SeeRange::data_two])
  39. ->update(['del_time' => $time]);
  40. if(! empty($data['depart'])){
  41. $insert = [];
  42. foreach ($data['depart'] as $value){
  43. $insert[] = [
  44. 'data_id' => $data['data_id'],
  45. 'data_type' => $data['data_type'],
  46. 'param_id' => $value,
  47. 'type' => SeeRange::data_one,
  48. 'crt_time' => $time,
  49. ];
  50. }
  51. SeeRange::insert($insert);
  52. }
  53. if(! empty($data['employee'])){
  54. $insert = [];
  55. foreach ($data['employee'] as $value){
  56. $insert[] = [
  57. 'data_id' => $data['data_id'],
  58. 'data_type' => $data['data_type'],
  59. 'param_id' => $value,
  60. 'type' => SeeRange::data_two,
  61. 'crt_time' => $time,
  62. ];
  63. }
  64. SeeRange::insert($insert);
  65. }
  66. return [true,''];
  67. }
  68. //可见范围删除
  69. public function RangeDelete($data_id = 0, $data_type = 0){
  70. if(empty($data_id) || empty($data_type)) return;
  71. SeeRange::where('del_time',0)
  72. ->where('data_id',$data_id)
  73. ->where('data_type',$data_type)
  74. ->update(['del_time'=> time()]);
  75. }
  76. //获取可见范围详情
  77. public function RangeDetail($data_id = 0, $data_type = 0){
  78. if(empty($data_id) || empty($data_type)) return [];
  79. $see = SeeRange::where('del_time',0)
  80. ->where('data_id',$data_id)
  81. ->where('data_type',$data_type)
  82. ->get()->toArray();
  83. $depart_map = Depart::where('del_time',0)->pluck('title','id')->toArray();
  84. $emp_map = Employee::where('del_time',0)->pluck('emp_name','id')->toArray();
  85. $depart = $employee = $depart2 = [];
  86. foreach ($see as $value){
  87. if ($value['type'] == SeeRange::data_one){
  88. $name = $depart_map[$value['param_id']] ?? "";
  89. if(! empty($name)){
  90. $tmp = [
  91. 'id' => $value['param_id'],
  92. 'name' => $depart_map[$value['param_id']] ?? "",
  93. ];
  94. $depart[] = $tmp;
  95. }
  96. }elseif ($value['type'] == SeeRange::data_two){
  97. $name = $emp_map[$value['param_id']] ?? '';
  98. if(! empty($name)){
  99. $tmp = [
  100. 'id' => $value['param_id'],
  101. 'name' => $emp_map[$value['param_id']] ?? '',
  102. ];
  103. $employee[] = $tmp;
  104. }
  105. }elseif ($value['type'] == SeeRange::data_three){
  106. $name = $depart_map[$value['param_id']] ?? '';
  107. if(! empty($name)) {
  108. $tmp = [
  109. 'id' => $value['param_id'],
  110. 'name' => $depart_map[$value['param_id']] ?? '',
  111. ];
  112. $depart2[] = $tmp;
  113. }
  114. }
  115. }
  116. return [$depart, $employee, $depart2];
  117. }
  118. //获取可见范围数据id
  119. public static function getRangeDataId($user,$data_type){
  120. $user_id = $user['id'];
  121. $depart_id = $user['depart_range'];
  122. $type = SeeRange::data_two;
  123. $type2 = [SeeRange::data_one,SeeRange::data_three];
  124. $type2 = implode(',',$type2);
  125. $depart_str = implode(',',$depart_id);
  126. if(empty($depart_str)) {
  127. $string = "param_id = 0";
  128. }else{
  129. $string = "param_id IN({$depart_str})";
  130. }
  131. // 人为当前用户时, 或部门在当前用户范围内
  132. $str = "(param_id = $user_id AND type = $type) OR ($string AND type IN ({$type2}))";
  133. // 可见部门 可见人 可以看见
  134. $data_id = SeeRange::where('del_time',0)
  135. ->where('data_type', $data_type)
  136. ->where(function ($query) use($str) {
  137. $query->whereRaw($str);
  138. })->select('data_id')->get()->toArray();
  139. return array_unique(array_column($data_id,'data_id'));
  140. }
  141. //获取客户可见数据
  142. public static function customerRange($user,$search){
  143. // 销售人员/负责人 3协同人 可以看见
  144. $customer_id = CustomerInfo::where('del_time',0)
  145. ->where('data_id',$user['id'])
  146. ->whereIn('type',CustomerInfo::$see_man)
  147. ->select('customer_id')->get()->toArray();
  148. $return_id = array_unique(array_column($customer_id,'customer_id'));
  149. //可见范围id
  150. $rang_id = Self::getRangeDataId($user,SeeRange::type_one);
  151. //并和
  152. $return_id = array_unique(array_merge_recursive($return_id,$rang_id));
  153. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  154. $id = DB::table('customer')
  155. ->where('del_time',0)
  156. ->where('top_depart_id',$search['top_depart_id'])
  157. ->select('id')->get()->toArray();
  158. $id = array_column($id,'id');
  159. foreach ($return_id as $key => $value){
  160. if(! in_array($value,$id)) unset($return_id[$key]);
  161. }
  162. }
  163. return $return_id;
  164. }
  165. //获取施工单可见数据
  166. public static function constructionRange($user,$search){
  167. //单据中选择的签订负责协同人
  168. $construction_id = ConstructionInfo::where('del_time',0)
  169. ->where('employee_id',$user['id'])
  170. ->select('construction_id')
  171. ->get()->toArray();
  172. $return_id = array_unique(array_column($construction_id,'construction_id'));
  173. //可见范围id
  174. $return = Self::getRangeDataId($user,SeeRange::type_two);
  175. $return_id = array_unique(array_merge_recursive($return_id,$return));
  176. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  177. $id = DB::table('construction')
  178. ->where('del_time',0)
  179. ->where('top_depart_id',$search['top_depart_id'])
  180. ->select('id')->get()->toArray();
  181. $id = array_column($id,'id');
  182. foreach ($return_id as $key => $value){
  183. if(! in_array($value,$id)) unset($return_id[$key]);
  184. }
  185. }
  186. if(isset($search['is_check'])){
  187. $args = self::constructionCheck($user,$search);
  188. $result = Construction::whereIn('id',$return_id)
  189. ->when(! empty($args), function ($query) use ($args) {
  190. return $query->whereRaw($args);
  191. })
  192. ->select('id')
  193. ->get()->toArray();
  194. $return_id = array_column($result,'id');
  195. }
  196. return $return_id;
  197. }
  198. //获取发货单可见数据
  199. public static function invoiceRange($user,$search){
  200. //可见范围id
  201. $return_id = Self::getRangeDataId($user,SeeRange::type_three);
  202. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  203. $id = DB::table('invoice_order')
  204. ->where('del_time',0)
  205. ->where('top_depart_id',$search['top_depart_id'])
  206. ->select('id')->get()->toArray();
  207. $id = array_column($id,'id');
  208. foreach ($return_id as $key => $value){
  209. if(! in_array($value,$id)) unset($return_id[$key]);
  210. }
  211. }
  212. if(isset($search['is_check'])){
  213. $args = self::invoiceCheck($user,$search);
  214. $result = InvoiceOrder::whereIn('id',$return_id)
  215. ->when(! empty($args), function ($query) use ($args) {
  216. return $query->whereRaw($args);
  217. })
  218. ->select('id')
  219. ->get()->toArray();
  220. $return_id = array_column($result,'id');
  221. }
  222. return $return_id;
  223. }
  224. //获取产品可见数据
  225. public static function productRange($user,$search){
  226. //可见范围id
  227. $return_id = Self::getRangeDataId($user,SeeRange::type_four);
  228. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  229. $id = DB::table('product')
  230. ->where('del_time',0)
  231. ->where('top_depart_id',$search['top_depart_id'])
  232. ->select('id')->get()->toArray();
  233. $id = array_column($id,'id');
  234. foreach ($return_id as $key => $value){
  235. if(! in_array($value,$id)) unset($return_id[$key]);
  236. }
  237. }
  238. return $return_id;
  239. }
  240. //获取产品不可见数据
  241. public static function productRangeNot($user,$search){
  242. //不可见范围id
  243. $return_id = Self::getRangeDataId($user,SeeRange::type_four);
  244. //分社管理员
  245. if(empty($user['is_all_depart']) && ! empty($user['is_manager'])) {
  246. $depart = array_shift($user['rule_depart']);
  247. $depart_id = $depart['depart_id'] ?? 0;
  248. $id = DB::table('product')
  249. ->where('del_time',0)
  250. ->where('top_depart_id',$depart_id)
  251. ->select('id')->get()->toArray();
  252. $id = array_column($id,'id');
  253. foreach ($return_id as $key => $value){
  254. if(in_array($value,$id)) unset($return_id[$key]);
  255. }
  256. }
  257. return $return_id;
  258. }
  259. //获取采购单可见数据
  260. public static function purchaseRange($user,$search){
  261. //可见范围id
  262. $return_id = Self::getRangeDataId($user,SeeRange::type_five);
  263. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  264. $id = DB::table('purchase_order')
  265. ->where('del_time',0)
  266. ->where('top_depart_id',$search['top_depart_id'])
  267. ->select('id')->get()->toArray();
  268. $id = array_column($id,'id');
  269. foreach ($return_id as $key => $value){
  270. if(! in_array($value,$id)) unset($return_id[$key]);
  271. }
  272. }
  273. if(isset($search['is_check'])){
  274. $args = self::purchaseCheck($user,$search);
  275. $result = PurchaseOrder::whereIn('id',$return_id)
  276. ->when(! empty($args), function ($query) use ($args) {
  277. return $query->whereRaw($args);
  278. })
  279. ->select('id')
  280. ->get()->toArray();
  281. $return_id = array_column($result,'id');
  282. }
  283. return $return_id;
  284. }
  285. //获取退换货单可见数据
  286. public static function returnExchangeOrderRange($user,$search){
  287. //可见范围id
  288. $return_id = Self::getRangeDataId($user,SeeRange::type_six);
  289. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  290. $id = DB::table('return_exchange_order')
  291. ->where('del_time',0)
  292. ->where('top_depart_id',$search['top_depart_id'])
  293. ->select('id')->get()->toArray();
  294. $id = array_column($id,'id');
  295. foreach ($return_id as $key => $value){
  296. if(! in_array($value,$id)) unset($return_id[$key]);
  297. }
  298. }
  299. if(isset($search['is_check'])){
  300. $args = self::returnExchangeOrderCheck($user,$search);
  301. $result = ReturnExchangeOrder::whereIn('id',$return_id)
  302. ->when(! empty($args), function ($query) use ($args) {
  303. return $query->whereRaw($args);
  304. })
  305. ->select('id')
  306. ->get()->toArray();
  307. $return_id = array_column($result,'id');
  308. }
  309. return $return_id;
  310. }
  311. //获取合同可见数据
  312. public static function salesOrderRange($user,$search){
  313. //单据中选择的签订负责协同人
  314. $sales_order_id = SalesOrderInfo::where('del_time',0)
  315. ->whereIn('type',SalesOrderInfo::$man)
  316. ->where('data_id',$user['id'])
  317. ->select('sales_order_id')
  318. ->get()->toArray();
  319. $sales_order_id = array_unique(array_column($sales_order_id,'sales_order_id'));
  320. $bool = ! empty($search['top_depart_id']) && ! empty($user['is_all_depart']);
  321. if(! $bool){
  322. $current_top_depart_id = $user['depart_top'][0] ?? [];
  323. $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
  324. //查找是否合同指派了门店
  325. $dispatch_company = SeeRange::where('del_time',0)
  326. ->whereIn('data_id',$sales_order_id)
  327. ->where('param_id', $current_top_depart_id)
  328. ->where('data_type',SeeRange::type_seven)
  329. ->where('type',SeeRange::data_three)
  330. ->select('data_id')
  331. ->get()->toArray();
  332. $dispatch_company = array_column($dispatch_company,'data_id');
  333. // 找出仅在 $array1 中存在的元素
  334. $diff1 = array_diff($sales_order_id, $dispatch_company);
  335. $sales_order_id = SalesOrder::whereIn('id', $diff1)
  336. ->where('top_depart_id',$current_top_depart_id)
  337. ->select('id')
  338. ->get()->toArray();
  339. $sales_order_id = array_column($sales_order_id,'sales_order_id');
  340. $sales_order_id = array_merge_recursive($sales_order_id,$dispatch_company);
  341. }
  342. //指派后 可见范围id
  343. $return = Self::getRangeDataId($user,SeeRange::type_seven);
  344. $return_id = array_unique(array_merge_recursive($sales_order_id,$return));
  345. if($bool){
  346. $id = DB::table('sales_order')
  347. ->where('del_time',0)
  348. ->where('top_depart_id',$search['top_depart_id'])
  349. ->select('id')->get()->toArray();
  350. $id = array_column($id,'id');
  351. foreach ($return_id as $key => $value){
  352. if(! in_array($value,$id)) unset($return_id[$key]);
  353. }
  354. }
  355. if(isset($search['is_check'])){
  356. $args = self::salesOrderCheck($user,$search);
  357. $result = SalesOrder::whereIn('id',$return_id)
  358. ->when(! empty($args), function ($query) use ($args) {
  359. return $query->whereRaw($args);
  360. })
  361. ->select('id')
  362. ->get()->toArray();
  363. $return_id = array_column($result,'id');
  364. }
  365. return $return_id;
  366. }
  367. //获取特殊采购单可见数据
  368. public static function purchaseSpecialRange($user,$search){
  369. //可见范围id
  370. $return_id = Self::getRangeDataId($user,SeeRange::type_ten);
  371. // if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  372. // $id = DB::table('purchase_order')
  373. // ->where('del_time',0)
  374. // ->where('top_depart_id',$search['top_depart_id'])
  375. // ->select('id')->get()->toArray();
  376. // $id = array_column($id,'id');
  377. // foreach ($return_id as $key => $value){
  378. // if(! in_array($value,$id)) unset($return_id[$key]);
  379. // }
  380. // }
  381. return $return_id;
  382. }
  383. //获取供应商可见数据
  384. public static function supplierRange($user,$search){
  385. //可见范围id
  386. $return_id = Self::getRangeDataId($user,SeeRange::type_nine);
  387. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  388. $id = DB::table('supplier')
  389. ->where('del_time',0)
  390. ->where('top_depart_id',$search['top_depart_id'])
  391. ->select('id')->get()->toArray();
  392. $id = array_column($id,'id');
  393. foreach ($return_id as $key => $value){
  394. if(! in_array($value,$id)) unset($return_id[$key]);
  395. }
  396. }
  397. return $return_id;
  398. }
  399. //获取活动包可见数据
  400. public static function sportsBagRange($user,$search){
  401. //可见范围id
  402. $return_id = Self::getRangeDataId($user,SeeRange::type_eight);
  403. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  404. $id = DB::table('sports_bag')
  405. ->where('del_time',0)
  406. ->where('top_depart_id',$search['top_depart_id'])
  407. ->select('id')->get()->toArray();
  408. $id = array_column($id,'id');
  409. foreach ($return_id as $key => $value){
  410. if(! in_array($value,$id)) unset($return_id[$key]);
  411. }
  412. }
  413. return $return_id;
  414. }
  415. //产品不可见部门
  416. public static function productNotSeeRange($product_id){
  417. $return = [];
  418. $result = SeeRange::where('del_time',0)
  419. ->where('data_type', SeeRange::type_four)
  420. ->whereIn('data_id',$product_id)
  421. ->where('type',SeeRange::data_one)
  422. ->select('param_id as depart_id','data_id as product_id')
  423. ->get()->toArray();
  424. foreach ($result as $value){
  425. $return[$value['product_id']][] = $value['depart_id'];
  426. }
  427. return $return;
  428. }
  429. //产品签订人负责人
  430. public function salesOrderSearch($data){
  431. $return1 = $return2 = [];
  432. if(! empty($data['qd'])){
  433. $emp_id = Employee::where('del_time',0)
  434. ->where('emp_name','LIKE', '%'.$data['qd'].'%')
  435. ->select('id')->get()->toArray();
  436. $emp_id = array_column($emp_id,'id');
  437. //单据中选择的签订人
  438. $sales_order_id = SalesOrderInfo::where('del_time',0)
  439. ->where('type',SalesOrderInfo::type_one)
  440. ->whereIn('data_id',$emp_id)
  441. ->select('sales_order_id')
  442. ->get()->toArray();
  443. $return1 = array_unique(array_column($sales_order_id,'sales_order_id'));
  444. }
  445. if(! empty($data['fz'])){
  446. $emp_id = Employee::where('del_time',0)
  447. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  448. ->select('id')->get()->toArray();
  449. $emp_id = array_column($emp_id,'id');
  450. //单据中选择的负责人
  451. $sales_order_id = SalesOrderInfo::where('del_time',0)
  452. ->where('type',SalesOrderInfo::type_two)
  453. ->whereIn('data_id',$emp_id)
  454. ->select('sales_order_id')
  455. ->get()->toArray();
  456. $return2 = array_unique(array_column($sales_order_id,'sales_order_id'));
  457. }
  458. if(! empty($data['qd']) && ! empty($data['fz'])){
  459. $return = array_intersect($return1, $return2);
  460. }elseif(!empty($data['qd'])){
  461. $return = $return1;
  462. }else{
  463. $return = $return2;
  464. }
  465. return $return;
  466. }
  467. //指派门店
  468. public function salesOrderZpSearch($data){
  469. $return = SeeRange::where('del_time',0)
  470. ->where('param_id',$data['zp'])
  471. ->where('data_type',SeeRange::type_seven)
  472. ->where('type',SeeRange::data_three)
  473. ->select('data_id')
  474. ->get()->toArray();
  475. return array_column($return,'data_id');
  476. }
  477. //客户创建人
  478. public function salesOrderCustomerCrtSearch($user,$data){
  479. $emp_id = Employee::where('del_time',0)
  480. ->where('emp_name','LIKE', '%'.$data['customer_crt_name'].'%')
  481. ->select('id')->get()->toArray();
  482. $emp_id = array_column($emp_id,'id');
  483. $model2 = Customer::Clear($user,$data);
  484. $customer = $model2->where('del_time',0)
  485. ->whereIn('crt_id', $emp_id)
  486. ->select('id')
  487. ->get()->toArray();
  488. return array_column($customer,'id');
  489. }
  490. //收付款人搜索
  491. public function paymentReceiptSearch($data){
  492. $emp_id = Employee::where('del_time',0)
  493. ->where('emp_name','LIKE', '%'.$data['belong'].'%')
  494. ->select('id')->get()->toArray();
  495. $emp_id = array_column($emp_id,'id');
  496. //单据中选择的签订人
  497. $id = PaymentReceiptInfo::where('del_time',0)
  498. ->where('type',PaymentReceiptInfo::type_two)
  499. ->whereIn('data_id',$emp_id)
  500. ->select('payment_receipt_id')
  501. ->get()->toArray();
  502. return array_unique(array_column($id,'payment_receipt_id'));
  503. }
  504. //创建人
  505. public function crtNameSearch($data){
  506. $emp_id = Employee::where('del_time',0)
  507. ->where('emp_name','LIKE', '%'.$data['crt_name'].'%')
  508. ->select('id')->get()->toArray();
  509. return array_column($emp_id,'id');
  510. }
  511. public function crtContactSearch($data){
  512. $id = CustomerInfo::where('del_time',0)
  513. ->where('type',CustomerInfo::type_one)
  514. ->where('contact_info','LIKE', '%'.$data['title_t'].'%')
  515. ->select('customer_id')->get()->toArray();
  516. return array_column($id,'customer_id');
  517. }
  518. //负责人
  519. public function customerSearch($data){
  520. $emp_id = Employee::where('del_time',0)
  521. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  522. ->select('id')->get()->toArray();
  523. $emp_id = array_column($emp_id,'id');
  524. //单据中选择的负责人
  525. $customer_id = CustomerInfo::where('del_time',0)
  526. ->where('type',CustomerInfo::type_two)
  527. ->whereIn('data_id',$emp_id)
  528. ->select('customer_id')
  529. ->get()->toArray();
  530. return array_unique(array_column($customer_id,'customer_id'));;
  531. }
  532. //获取可见人施工单
  533. public function RangeConstructionEmpDetail($data_id = 0){
  534. if(empty($data_id)) return [];
  535. $see = ConstructionInfo::where('del_time',0)
  536. ->whereIn('construction_id',$data_id)
  537. ->where('type',ConstructionInfo::type_three)
  538. ->get()->toArray();
  539. $emp_map = Employee::where('del_time',0)
  540. ->whereIn('id',array_column($see,'employee_id'))
  541. ->pluck('emp_name','id')->toArray();
  542. $employee = [];
  543. foreach ($see as $value){
  544. $name = $emp_map[$value['employee_id']] ?? '';
  545. if(! empty($name)){
  546. $tmp = [
  547. 'id' => $value['employee_id'],
  548. 'emp_name' => $emp_map[$value['employee_id']] ?? '',
  549. ];
  550. $employee[$value['construction_id']][] = $tmp;
  551. }
  552. }
  553. return $employee;
  554. }
  555. //客户类型
  556. public function customerBasicTypeSearch($customer_type, $type){
  557. $result = BasicType::where('del_time',0)
  558. ->whereIn('type',$type)
  559. ->where('title', $customer_type)
  560. ->select('id')->get()->toArray();
  561. return array_column($result,'id');
  562. }
  563. public function customerBasicTypeAllUseSearch($customer_type, $type){
  564. $result = BasicTypeAllUse::where('del_time',0)
  565. ->whereIn('type',$type)
  566. ->where('title', $customer_type)
  567. ->select('id')->get()->toArray();
  568. return array_column($result,'id');
  569. }
  570. //全部 待审 已审核 -----------------------------------------------
  571. public static function sportsBagCheck($user,$search){
  572. $args = "";
  573. if($search['is_check'] == 1) {
  574. list($status, $id) = self::getWaitForSportsCheck($user,$search);
  575. //待审核
  576. $check = implode(",", SportsBag::$wait_check);
  577. $args = "(state IN (" . implode(",", SportsBag::$wait_check) ."))";
  578. if($status) {
  579. $wait_for_me = $search['wait_for_me'] ?? 0;
  580. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [SportsBag::STATE_ONE]));
  581. $id = implode(",", $id);
  582. if($wait_for_me){
  583. if(empty($id)) {
  584. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  585. }else{
  586. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  587. }
  588. }
  589. }
  590. }elseif($search['is_check'] == 2){
  591. //已审
  592. $args = "(state = ". SportsBag::STATE_TWO . ")";
  593. }
  594. return $args;
  595. }
  596. private static function getWaitForSportsCheck($user, $search){
  597. if(! isset($search['wait_for_me'])) return [false, []];
  598. //获取待审核
  599. $args = "(state = " . SportsBag::STATE_ONE . ")";
  600. $data = SportsBag::where('del_time',0)
  601. ->whereRaw($args)
  602. ->select('order_number','id')
  603. ->get()->toArray();
  604. if(empty($data)) return [true, []];
  605. list($status,$msg) = self::getWaitCommon($data,$user);
  606. return [$status, $msg];
  607. }
  608. public static function paymentReceiptCheck($user,$search){
  609. $args = "";
  610. if($search['is_check'] == 1) {
  611. list($status, $id) = self::getWaitForPaymentCheck($user,$search);
  612. //待审核
  613. $check = implode(",", SportsBag::$wait_check);
  614. $args = "(state IN (" . $check ."))";
  615. if($status) {
  616. $wait_for_me = $search['wait_for_me'] ?? 0;
  617. $id = implode(",", $id);
  618. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PaymentReceipt::STATE_ONE]));
  619. if($wait_for_me){
  620. if(empty($id)) {
  621. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  622. }else{
  623. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  624. }
  625. }
  626. }
  627. }elseif($search['is_check'] == 2){
  628. //已审
  629. $args = "(state = ". PaymentReceipt::STATE_TWO . ")";
  630. }
  631. return $args;
  632. }
  633. private static function getWaitForPaymentCheck($user, $search){
  634. if(! isset($search['wait_for_me'])) return [false, []];
  635. //获取待审核
  636. $args = "(state = " . PaymentReceipt::STATE_ONE . ")";
  637. $data = PaymentReceipt::where('del_time',0)
  638. ->whereRaw($args)
  639. ->select('order_number','id')
  640. ->get()->toArray();
  641. if(empty($data)) return [true, []];
  642. list($status,$msg) = self::getWaitCommon($data,$user);
  643. return [$status, $msg];
  644. }
  645. public static function salesOrderCheck($user,$search){
  646. $args = "";
  647. if($search['is_check'] == 1) {
  648. list($status, $id) = self::getWaitForSalesCheck($user,$search);
  649. //待审核
  650. $check = implode(",", SalesOrder::$wait_check);
  651. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check ."))";
  652. if($status) {
  653. $wait_for_me = $search['wait_for_me'] ?? 0;
  654. $check_2 = implode(',', array_diff(SalesOrder::$wait_check, [SalesOrder::State_one]));
  655. $id = implode(",", $id);
  656. if($wait_for_me){
  657. if(empty($id)) {
  658. $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 ."))))";
  659. }else{
  660. $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 ."))))";
  661. }
  662. }
  663. }
  664. }elseif($search['is_check'] == 2){
  665. //已审 线上订单的已审核是2 其它是 3
  666. $args = SalesOrder::search;
  667. }
  668. return $args;
  669. }
  670. private static function getWaitForSalesCheck($user, $search){
  671. if(! isset($search['wait_for_me'])) return [false, []];
  672. //获取待审核合同
  673. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state = " . SalesOrder::State_one . ")";
  674. $data = SalesOrder::where('del_time',0)
  675. ->whereRaw($args)
  676. ->select('order_number','id')
  677. ->get()->toArray();
  678. if(empty($data)) return [true, []];
  679. list($status,$msg) = self::getWaitCommon($data,$user);
  680. return [$status, $msg];
  681. }
  682. public static function invoiceCheck($user,$search){
  683. $args = "";
  684. if($search['is_check'] == 1) {
  685. list($status, $id) = self::getWaitForInvoiceCheck($user,$search);
  686. //待审核
  687. $check = implode(",", InvoiceOrder::$wait_check);
  688. $args = "(state IN (" . $check ."))";
  689. if($status) {
  690. $wait_for_me = $search['wait_for_me'] ?? 0;
  691. $check_2 = implode(',', array_diff(InvoiceOrder::$wait_check, [InvoiceOrder::STATE_ONE]));
  692. $id = implode(",", $id);
  693. if($wait_for_me){
  694. if(empty($id)) {
  695. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  696. }else{
  697. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  698. }
  699. }
  700. }
  701. }elseif($search['is_check'] == 2){
  702. //已审
  703. $args = "(state = ". InvoiceOrder::STATE_TWO . ")";
  704. }
  705. return $args;
  706. }
  707. private static function getWaitForInvoiceCheck($user, $search){
  708. if(! isset($search['wait_for_me'])) return [false, []];
  709. //获取待审核数据
  710. $args = "(state = " . InvoiceOrder::STATE_ONE . ")";
  711. $data = InvoiceOrder::where('del_time',0)
  712. ->whereRaw($args)
  713. ->select('order_number','id')
  714. ->get()->toArray();
  715. if(empty($data)) return [true, []];
  716. list($status,$msg) = self::getWaitCommon($data,$user);
  717. return [$status, $msg];
  718. }
  719. public static function returnExchangeOrderCheck($user,$search){
  720. $args = "";
  721. if($search['is_check'] == 1) {
  722. list($status, $id) = self::getWaitForReturnExchangeCheck($user,$search);
  723. //待审核
  724. $check = implode(",", ReturnExchangeOrder::$wait_check);
  725. $args = "(state IN (" . $check ."))";
  726. if($status) {
  727. $wait_for_me = $search['wait_for_me'] ?? 0;
  728. $check_2 = implode(',', array_diff(ReturnExchangeOrder::$wait_check, [ReturnExchangeOrder::State_one]));
  729. $id = implode(",", $id);
  730. if($wait_for_me){
  731. if(empty($id)) {
  732. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  733. }else{
  734. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  735. }
  736. }
  737. }
  738. }elseif($search['is_check'] == 2){
  739. //已审
  740. $args = "(state = ". ReturnExchangeOrder::State_two . ")";
  741. }
  742. return $args;
  743. }
  744. private static function getWaitForReturnExchangeCheck($user, $search){
  745. if(! isset($search['wait_for_me'])) return [false, []];
  746. //获取待审核数据
  747. $args = "(state = " . ReturnExchangeOrder::State_one . ")";
  748. $data = ReturnExchangeOrder::where('del_time',0)
  749. ->whereRaw($args)
  750. ->select('order_number','id')
  751. ->get()->toArray();
  752. if(empty($data)) return [true, []];
  753. list($status,$msg) = self::getWaitCommon($data,$user);
  754. return [$status, $msg];
  755. }
  756. public static function constructionCheck($user,$search){
  757. $args = "";
  758. if($search['is_check'] == 1) {
  759. list($status, $id) = self::getWaitForConstructionCheck($user,$search);
  760. //待审核
  761. $check = implode(",", Construction::$wait_check);
  762. $args = "(state IN (" . $check ."))";
  763. if($status) {
  764. $wait_for_me = $search['wait_for_me'] ?? 0;
  765. $check_2 = implode(',', array_diff(Construction::$wait_check, [Construction::STATE_ONE]));
  766. $id = implode(",", $id);
  767. if($wait_for_me){
  768. if(empty($id)) {
  769. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  770. }else{
  771. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  772. }
  773. }
  774. }
  775. }elseif($search['is_check'] == 2){
  776. //已审
  777. $args = "(state >= ". Construction::STATE_TWO . ")";
  778. }
  779. return $args;
  780. }
  781. private static function getWaitForConstructionCheck($user, $search){
  782. if(! isset($search['wait_for_me'])) return [false, []];
  783. //获取待审核数据
  784. $args = "(state = " . Construction::STATE_ONE . ")";
  785. $data = Construction::where('del_time',0)
  786. ->whereRaw($args)
  787. ->select('order_number','id')
  788. ->get()->toArray();
  789. if(empty($data)) return [true, []];
  790. list($status,$msg) = self::getWaitCommon($data,$user);
  791. return [$status, $msg];
  792. }
  793. public static function purchaseCheck($user,$search){
  794. $args = "";
  795. if($search['is_check'] == 1) {
  796. list($status, $id) = self::getWaitForPurchaseCheck($user,$search);
  797. //待审核
  798. $check = implode(",", PurchaseOrder::$wait_check);
  799. $args = "(state IN (" . $check ."))";
  800. if($status) {
  801. $wait_for_me = $search['wait_for_me'] ?? 0;
  802. $check_2 = implode(',', array_diff(PurchaseOrder::$wait_check, [PurchaseOrder::STATE_ONE]));
  803. $id = implode(",", $id);
  804. if($wait_for_me){
  805. if(empty($id)) {
  806. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  807. }else{
  808. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  809. }
  810. }
  811. }
  812. }elseif($search['is_check'] == 2){
  813. //已审
  814. $args = "(state >= ". PurchaseOrder::STATE_TWO . ")";
  815. }
  816. return $args;
  817. }
  818. private static function getWaitForPurchaseCheck($user, $search){
  819. if(! isset($search['wait_for_me'])) return [false, []];
  820. //获取待审核数据
  821. $args = "(state = " . PurchaseOrder::STATE_ONE . ")";
  822. $data = PurchaseOrder::where('del_time',0)
  823. ->whereRaw($args)
  824. ->select('order_number','id')
  825. ->get()->toArray();
  826. if(empty($data)) return [true, []];
  827. list($status,$msg) = self::getWaitCommon($data,$user);
  828. return [$status, $msg];
  829. }
  830. public static function inventoryCheck($user,$search){
  831. $args = "";
  832. if($search['is_check'] == 1) {
  833. list($status, $id) = self::getWaitForinventoryCheck($user,$search);
  834. //待审核
  835. $check = implode(",", Inventory::$wait_check);
  836. $args = "(state IN (" . $check ."))";
  837. if($status) {
  838. $wait_for_me = $search['wait_for_me'] ?? 0;
  839. $check_2 = implode(',', array_diff(Inventory::$wait_check, [Inventory::STATE_ONE]));
  840. $id = implode(",", $id);
  841. if($wait_for_me){
  842. if(empty($id)) {
  843. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  844. }else{
  845. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  846. }
  847. }
  848. }
  849. }elseif($search['is_check'] == 2){
  850. //已审
  851. $args = "(state >= ". Inventory::STATE_TWO . ")";
  852. }
  853. return $args;
  854. }
  855. private static function getWaitForinventoryCheck($user, $search){
  856. if(! isset($search['wait_for_me'])) return [false, []];
  857. //获取待审核数据
  858. $args = "(state = " . Inventory::STATE_ONE . ")";
  859. $data = Inventory::where('del_time',0)
  860. ->whereRaw($args)
  861. ->select('order_number','id')
  862. ->get()->toArray();
  863. if(empty($data)) return [true, []];
  864. list($status,$msg) = self::getWaitCommon($data,$user);
  865. return [$status, $msg];
  866. }
  867. public static function productAdjustmentCheck($user,$search){
  868. $args = "";
  869. if($search['is_check'] == 1) {
  870. list($status, $id) = self::getProductAdjustmentCheck($user,$search);
  871. //待审核
  872. $check = implode(",", ProductAdjustment::$wait_check);
  873. $args = "(state IN (" . $check ."))";
  874. if($status) {
  875. $wait_for_me = $search['wait_for_me'] ?? 0;
  876. $check_2 = implode(',', array_diff(ProductAdjustment::$wait_check, [ProductAdjustment::STATE_ONE]));
  877. $id = implode(",", $id);
  878. if($wait_for_me){
  879. if(empty($id)) {
  880. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  881. }else{
  882. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  883. }
  884. }
  885. }
  886. }elseif($search['is_check'] == 2){
  887. //已审
  888. $args = "(state >= ". ProductAdjustment::STATE_TWO . ")";
  889. }
  890. return $args;
  891. }
  892. private static function getProductAdjustmentCheck($user, $search){
  893. if(! isset($search['wait_for_me'])) return [false, []];
  894. //获取待审核数据
  895. $args = "(state = " . ProductAdjustment::STATE_ONE . ")";
  896. $data = ProductAdjustment::where('del_time',0)
  897. ->whereRaw($args)
  898. ->select('order_number','id')
  899. ->get()->toArray();
  900. if(empty($data)) return [true, []];
  901. list($status,$msg) = self::getWaitCommon($data,$user);
  902. return [$status, $msg];
  903. }
  904. private static function getWaitCommon($data,$user){
  905. $data_map = array_column($data,'id','order_number');
  906. //查找对应审批流数据
  907. $orderNoGroups = OaOrder::whereIn('order_no', array_column($data,'order_number'))
  908. ->get()
  909. ->groupBy('order_no');
  910. $maxIds = $orderNoGroups->map(function ($group) {
  911. return $group->max('id');
  912. });
  913. $map = $maxIds->toArray();
  914. if(empty($map)) return [true, []];
  915. $oa_order_id = array_values($map);
  916. $map2 = array_flip($map);
  917. unset($map);
  918. //获取审批流下的人
  919. $list = OaOrderSub::whereIn('oa_order_id', $oa_order_id)
  920. ->whereIn('state',[0,1])
  921. ->select('id','state','oa_order_id')
  922. ->orderBy('id', 'desc')
  923. ->get()->toArray();
  924. $subEmployeeList = OaOrderSubEmployee::whereIn('oa_order_id', $oa_order_id)
  925. ->where('employee_id',$user['id'])
  926. ->get()->toArray();
  927. if(empty($subEmployeeList)) return [true, []];
  928. //每条数据对应的人
  929. $emp_id_key_list = [];
  930. foreach ($subEmployeeList as $v) {
  931. $emp_id_key_list[$v['oa_order_sub_id']][] = $v['employee_id'];
  932. }
  933. unset($subEmployeeList);
  934. $flag = $id = [];
  935. foreach ($list as $v) {
  936. //不存在单号或者已存在单号返回数据
  937. if(empty($v['oa_order_id']) || isset($flag[$v['oa_order_id']])) continue;
  938. $emp_tmp = $emp_id_key_list[$v['id']] ?? [];
  939. $flag[$v['oa_order_id']] = $emp_tmp;
  940. $order_number = $map2[$v['oa_order_id']] ?? "";
  941. $sales_id = $data_map[$order_number] ?? 0;
  942. if(in_array($user['id'], $emp_tmp)) $id[] = $sales_id;
  943. }
  944. unset($flag);
  945. return [true, $id];
  946. }
  947. //全部 待审 已审核 -----------------------------------------------
  948. }