RangeService.php 41 KB

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