RangeService.php 38 KB

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