RangeService.php 41 KB

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