RangeService.php 42 KB

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