RangeService.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. $sales_order_id = SalesOrder::whereIn('id', $sales_order_id)
  324. ->where('top_depart_id',$current_top_depart_id)
  325. ->select('id')
  326. ->get()->toArray();
  327. $sales_order_id = array_column($sales_order_id,'sales_order_id');
  328. }
  329. //指派后 可见范围id
  330. $return = Self::getRangeDataId($user,SeeRange::type_seven);
  331. $return_id = array_unique(array_merge_recursive($sales_order_id,$return));
  332. if($bool){
  333. $id = DB::table('sales_order')
  334. ->where('del_time',0)
  335. ->where('top_depart_id',$search['top_depart_id'])
  336. ->select('id')->get()->toArray();
  337. $id = array_column($id,'id');
  338. foreach ($return_id as $key => $value){
  339. if(! in_array($value,$id)) unset($return_id[$key]);
  340. }
  341. }
  342. if(isset($search['is_check'])){
  343. $args = self::salesOrderCheck($user,$search);
  344. $result = SalesOrder::whereIn('id',$return_id)
  345. ->when(! empty($args), function ($query) use ($args) {
  346. return $query->whereRaw($args);
  347. })
  348. ->select('id')
  349. ->get()->toArray();
  350. $return_id = array_column($result,'id');
  351. }
  352. return $return_id;
  353. }
  354. //获取特殊采购单可见数据
  355. public static function purchaseSpecialRange($user,$search){
  356. //可见范围id
  357. $return_id = Self::getRangeDataId($user,SeeRange::type_ten);
  358. // if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  359. // $id = DB::table('purchase_order')
  360. // ->where('del_time',0)
  361. // ->where('top_depart_id',$search['top_depart_id'])
  362. // ->select('id')->get()->toArray();
  363. // $id = array_column($id,'id');
  364. // foreach ($return_id as $key => $value){
  365. // if(! in_array($value,$id)) unset($return_id[$key]);
  366. // }
  367. // }
  368. return $return_id;
  369. }
  370. //获取供应商可见数据
  371. public static function supplierRange($user,$search){
  372. //可见范围id
  373. $return_id = Self::getRangeDataId($user,SeeRange::type_nine);
  374. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  375. $id = DB::table('supplier')
  376. ->where('del_time',0)
  377. ->where('top_depart_id',$search['top_depart_id'])
  378. ->select('id')->get()->toArray();
  379. $id = array_column($id,'id');
  380. foreach ($return_id as $key => $value){
  381. if(! in_array($value,$id)) unset($return_id[$key]);
  382. }
  383. }
  384. return $return_id;
  385. }
  386. //获取活动包可见数据
  387. public static function sportsBagRange($user,$search){
  388. //可见范围id
  389. $return_id = Self::getRangeDataId($user,SeeRange::type_eight);
  390. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  391. $id = DB::table('sports_bag')
  392. ->where('del_time',0)
  393. ->where('top_depart_id',$search['top_depart_id'])
  394. ->select('id')->get()->toArray();
  395. $id = array_column($id,'id');
  396. foreach ($return_id as $key => $value){
  397. if(! in_array($value,$id)) unset($return_id[$key]);
  398. }
  399. }
  400. return $return_id;
  401. }
  402. //产品不可见部门
  403. public static function productNotSeeRange($product_id){
  404. $return = [];
  405. $result = SeeRange::where('del_time',0)
  406. ->where('data_type', SeeRange::type_four)
  407. ->whereIn('data_id',$product_id)
  408. ->where('type',SeeRange::data_one)
  409. ->select('param_id as depart_id','data_id as product_id')
  410. ->get()->toArray();
  411. foreach ($result as $value){
  412. $return[$value['product_id']][] = $value['depart_id'];
  413. }
  414. return $return;
  415. }
  416. //产品签订人负责人
  417. public function salesOrderSearch($data){
  418. $return1 = $return2 = [];
  419. if(! empty($data['qd'])){
  420. $emp_id = Employee::where('del_time',0)
  421. ->where('emp_name','LIKE', '%'.$data['qd'].'%')
  422. ->select('id')->get()->toArray();
  423. $emp_id = array_column($emp_id,'id');
  424. //单据中选择的签订人
  425. $sales_order_id = SalesOrderInfo::where('del_time',0)
  426. ->where('type',SalesOrderInfo::type_one)
  427. ->whereIn('data_id',$emp_id)
  428. ->select('sales_order_id')
  429. ->get()->toArray();
  430. $return1 = array_unique(array_column($sales_order_id,'sales_order_id'));
  431. }
  432. if(! empty($data['fz'])){
  433. $emp_id = Employee::where('del_time',0)
  434. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  435. ->select('id')->get()->toArray();
  436. $emp_id = array_column($emp_id,'id');
  437. //单据中选择的负责人
  438. $sales_order_id = SalesOrderInfo::where('del_time',0)
  439. ->where('type',SalesOrderInfo::type_two)
  440. ->whereIn('data_id',$emp_id)
  441. ->select('sales_order_id')
  442. ->get()->toArray();
  443. $return2 = array_unique(array_column($sales_order_id,'sales_order_id'));
  444. }
  445. if(! empty($data['qd']) && ! empty($data['fz'])){
  446. $return = array_intersect($return1, $return2);
  447. }elseif(!empty($data['qd'])){
  448. $return = $return1;
  449. }else{
  450. $return = $return2;
  451. }
  452. return $return;
  453. }
  454. //指派门店
  455. public function salesOrderZpSearch($data){
  456. $return = SeeRange::where('del_time',0)
  457. ->where('param_id',$data['zp'])
  458. ->where('data_type',SeeRange::type_seven)
  459. ->where('type',SeeRange::data_three)
  460. ->select('data_id')
  461. ->get()->toArray();
  462. return array_column($return,'data_id');
  463. }
  464. //客户联系方式
  465. public function salesOrderCustomerMessageSearch($data){
  466. $return = CustomerInfo::where('del_time',0)
  467. ->where("type",CustomerInfo::type_one)
  468. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  469. ->select('customer_id')
  470. ->get()->toArray();
  471. return array_column($return,'customer_id');
  472. }
  473. //客户创建人
  474. public function salesOrderCustomerCrtSearch($user,$data){
  475. $emp_id = Employee::where('del_time',0)
  476. ->where('emp_name','LIKE', '%'.$data['customer_crt_name'].'%')
  477. ->select('id')->get()->toArray();
  478. $emp_id = array_column($emp_id,'id');
  479. $model2 = Customer::Clear($user,$data);
  480. $customer = $model2->where('del_time',0)
  481. ->whereIn('crt_id', $emp_id)
  482. ->select('id')
  483. ->get()->toArray();
  484. return array_column($customer,'id');
  485. }
  486. //收付款人搜索
  487. public function paymentReceiptSearch($data){
  488. $emp_id = Employee::where('del_time',0)
  489. ->where('emp_name','LIKE', '%'.$data['belong'].'%')
  490. ->select('id')->get()->toArray();
  491. $emp_id = array_column($emp_id,'id');
  492. //单据中选择的签订人
  493. $id = PaymentReceiptInfo::where('del_time',0)
  494. ->where('type',PaymentReceiptInfo::type_two)
  495. ->whereIn('data_id',$emp_id)
  496. ->select('payment_receipt_id')
  497. ->get()->toArray();
  498. return array_unique(array_column($id,'payment_receipt_id'));
  499. }
  500. //创建人
  501. public function crtNameSearch($data){
  502. $emp_id = Employee::where('del_time',0)
  503. ->where('emp_name','LIKE', '%'.$data['crt_name'].'%')
  504. ->select('id')->get()->toArray();
  505. return array_column($emp_id,'id');
  506. }
  507. public function crtContactSearch($data){
  508. $id = CustomerInfo::where('del_time',0)
  509. ->where('type',CustomerInfo::type_one)
  510. ->where('contact_info','LIKE', '%'.$data['title_t'].'%')
  511. ->select('customer_id')->get()->toArray();
  512. return array_column($id,'customer_id');
  513. }
  514. //负责人
  515. public function customerSearch($data){
  516. $emp_id = Employee::where('del_time',0)
  517. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  518. ->select('id')->get()->toArray();
  519. $emp_id = array_column($emp_id,'id');
  520. //单据中选择的负责人
  521. $customer_id = CustomerInfo::where('del_time',0)
  522. ->where('type',CustomerInfo::type_two)
  523. ->whereIn('data_id',$emp_id)
  524. ->select('customer_id')
  525. ->get()->toArray();
  526. return array_unique(array_column($customer_id,'customer_id'));;
  527. }
  528. //获取可见人施工单
  529. public function RangeConstructionEmpDetail($data_id = 0){
  530. if(empty($data_id)) return [];
  531. $see = ConstructionInfo::where('del_time',0)
  532. ->whereIn('construction_id',$data_id)
  533. ->where('type',ConstructionInfo::type_three)
  534. ->get()->toArray();
  535. $emp_map = Employee::where('del_time',0)
  536. ->whereIn('id',array_column($see,'employee_id'))
  537. ->pluck('emp_name','id')->toArray();
  538. $employee = [];
  539. foreach ($see as $value){
  540. $name = $emp_map[$value['employee_id']] ?? '';
  541. if(! empty($name)){
  542. $tmp = [
  543. 'id' => $value['employee_id'],
  544. 'emp_name' => $emp_map[$value['employee_id']] ?? '',
  545. ];
  546. $employee[$value['construction_id']][] = $tmp;
  547. }
  548. }
  549. return $employee;
  550. }
  551. //客户类型
  552. public function customerBasicTypeSearch($customer_type, $type){
  553. $result = BasicType::where('del_time',0)
  554. ->whereIn('type',$type)
  555. ->where('title', $customer_type)
  556. ->select('id')->get()->toArray();
  557. return array_column($result,'id');
  558. }
  559. //全部 待审 已审核 -----------------------------------------------
  560. public static function sportsBagCheck($user,$search){
  561. $args = "";
  562. if($search['is_check'] == 1) {
  563. list($status, $id) = self::getWaitForSportsCheck($user,$search);
  564. //待审核
  565. $check = implode(",", SportsBag::$wait_check);
  566. $args = "(state IN (" . implode(",", SportsBag::$wait_check) ."))";
  567. if($status) {
  568. $wait_for_me = $search['wait_for_me'] ?? 0;
  569. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [SportsBag::STATE_ONE]));
  570. $id = implode(",", $id);
  571. if($wait_for_me){
  572. if(empty($id)) {
  573. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  574. }else{
  575. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  576. }
  577. }
  578. }
  579. }elseif($search['is_check'] == 2){
  580. //已审
  581. $args = "(state = ". SportsBag::STATE_TWO . ")";
  582. }
  583. return $args;
  584. }
  585. private static function getWaitForSportsCheck($user, $search){
  586. if(! isset($search['wait_for_me'])) return [false, []];
  587. //获取待审核
  588. $args = "(state = " . SportsBag::STATE_ONE . ")";
  589. $data = SportsBag::where('del_time',0)
  590. ->whereRaw($args)
  591. ->select('order_number','id')
  592. ->get()->toArray();
  593. if(empty($data)) return [true, []];
  594. list($status,$msg) = self::getWaitCommon($data,$user);
  595. return [$status, $msg];
  596. }
  597. public static function paymentReceiptCheck($user,$search){
  598. $args = "";
  599. if($search['is_check'] == 1) {
  600. list($status, $id) = self::getWaitForPaymentCheck($user,$search);
  601. //待审核
  602. $check = implode(",", SportsBag::$wait_check);
  603. $args = "(state IN (" . $check ."))";
  604. if($status) {
  605. $wait_for_me = $search['wait_for_me'] ?? 0;
  606. $id = implode(",", $id);
  607. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PaymentReceipt::STATE_ONE]));
  608. if($wait_for_me){
  609. if(empty($id)) {
  610. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  611. }else{
  612. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  613. }
  614. }
  615. }
  616. }elseif($search['is_check'] == 2){
  617. //已审
  618. $args = "(state = ". PaymentReceipt::STATE_TWO . ")";
  619. }
  620. return $args;
  621. }
  622. private static function getWaitForPaymentCheck($user, $search){
  623. if(! isset($search['wait_for_me'])) return [false, []];
  624. //获取待审核
  625. $args = "(state = " . PaymentReceipt::STATE_ONE . ")";
  626. $data = PaymentReceipt::where('del_time',0)
  627. ->whereRaw($args)
  628. ->select('order_number','id')
  629. ->get()->toArray();
  630. if(empty($data)) return [true, []];
  631. list($status,$msg) = self::getWaitCommon($data,$user);
  632. return [$status, $msg];
  633. }
  634. public static function salesOrderCheck($user,$search){
  635. $args = "";
  636. if($search['is_check'] == 1) {
  637. list($status, $id) = self::getWaitForSalesCheck($user,$search);
  638. //待审核
  639. $check = implode(",", SalesOrder::$wait_check);
  640. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check ."))";
  641. if($status) {
  642. $wait_for_me = $search['wait_for_me'] ?? 0;
  643. $check_2 = implode(',', array_diff(SalesOrder::$wait_check, [SalesOrder::State_one]));
  644. $id = implode(",", $id);
  645. if($wait_for_me){
  646. if(empty($id)) {
  647. $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 ."))))";
  648. }else{
  649. $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 ."))))";
  650. }
  651. }
  652. }
  653. }elseif($search['is_check'] == 2){
  654. //已审 线上订单的已审核是2 其它是 3
  655. $args = SalesOrder::search;
  656. }
  657. return $args;
  658. }
  659. private static function getWaitForSalesCheck($user, $search){
  660. if(! isset($search['wait_for_me'])) return [false, []];
  661. //获取待审核合同
  662. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state = " . SalesOrder::State_one . ")";
  663. $data = SalesOrder::where('del_time',0)
  664. ->whereRaw($args)
  665. ->select('order_number','id')
  666. ->get()->toArray();
  667. if(empty($data)) return [true, []];
  668. list($status,$msg) = self::getWaitCommon($data,$user);
  669. return [$status, $msg];
  670. }
  671. public static function invoiceCheck($user,$search){
  672. $args = "";
  673. if($search['is_check'] == 1) {
  674. list($status, $id) = self::getWaitForInvoiceCheck($user,$search);
  675. //待审核
  676. $check = implode(",", InvoiceOrder::$wait_check);
  677. $args = "(state IN (" . $check ."))";
  678. if($status) {
  679. $wait_for_me = $search['wait_for_me'] ?? 0;
  680. $check_2 = implode(',', array_diff(InvoiceOrder::$wait_check, [InvoiceOrder::STATE_ONE]));
  681. $id = implode(",", $id);
  682. if($wait_for_me){
  683. if(empty($id)) {
  684. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  685. }else{
  686. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  687. }
  688. }
  689. }
  690. }elseif($search['is_check'] == 2){
  691. //已审
  692. $args = "(state = ". InvoiceOrder::STATE_TWO . ")";
  693. }
  694. return $args;
  695. }
  696. private static function getWaitForInvoiceCheck($user, $search){
  697. if(! isset($search['wait_for_me'])) return [false, []];
  698. //获取待审核数据
  699. $args = "(state = " . InvoiceOrder::STATE_ONE . ")";
  700. $data = InvoiceOrder::where('del_time',0)
  701. ->whereRaw($args)
  702. ->select('order_number','id')
  703. ->get()->toArray();
  704. if(empty($data)) return [true, []];
  705. list($status,$msg) = self::getWaitCommon($data,$user);
  706. return [$status, $msg];
  707. }
  708. public static function returnExchangeOrderCheck($user,$search){
  709. $args = "";
  710. if($search['is_check'] == 1) {
  711. list($status, $id) = self::getWaitForReturnExchangeCheck($user,$search);
  712. //待审核
  713. $check = implode(",", ReturnExchangeOrder::$wait_check);
  714. $args = "(state IN (" . $check ."))";
  715. if($status) {
  716. $wait_for_me = $search['wait_for_me'] ?? 0;
  717. $check_2 = implode(',', array_diff(ReturnExchangeOrder::$wait_check, [ReturnExchangeOrder::State_one]));
  718. $id = implode(",", $id);
  719. if($wait_for_me){
  720. if(empty($id)) {
  721. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  722. }else{
  723. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  724. }
  725. }
  726. }
  727. }elseif($search['is_check'] == 2){
  728. //已审
  729. $args = "(state = ". ReturnExchangeOrder::State_two . ")";
  730. }
  731. return $args;
  732. }
  733. private static function getWaitForReturnExchangeCheck($user, $search){
  734. if(! isset($search['wait_for_me'])) return [false, []];
  735. //获取待审核数据
  736. $args = "(state = " . ReturnExchangeOrder::State_one . ")";
  737. $data = ReturnExchangeOrder::where('del_time',0)
  738. ->whereRaw($args)
  739. ->select('order_number','id')
  740. ->get()->toArray();
  741. if(empty($data)) return [true, []];
  742. list($status,$msg) = self::getWaitCommon($data,$user);
  743. return [$status, $msg];
  744. }
  745. public static function constructionCheck($user,$search){
  746. $args = "";
  747. if($search['is_check'] == 1) {
  748. list($status, $id) = self::getWaitForConstructionCheck($user,$search);
  749. //待审核
  750. $check = implode(",", Construction::$wait_check);
  751. $args = "(state IN (" . $check ."))";
  752. if($status) {
  753. $wait_for_me = $search['wait_for_me'] ?? 0;
  754. $check_2 = implode(',', array_diff(Construction::$wait_check, [Construction::STATE_ONE]));
  755. $id = implode(",", $id);
  756. if($wait_for_me){
  757. if(empty($id)) {
  758. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  759. }else{
  760. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  761. }
  762. }
  763. }
  764. }elseif($search['is_check'] == 2){
  765. //已审
  766. $args = "(state >= ". Construction::STATE_TWO . ")";
  767. }
  768. return $args;
  769. }
  770. private static function getWaitForConstructionCheck($user, $search){
  771. if(! isset($search['wait_for_me'])) return [false, []];
  772. //获取待审核数据
  773. $args = "(state = " . Construction::STATE_ONE . ")";
  774. $data = Construction::where('del_time',0)
  775. ->whereRaw($args)
  776. ->select('order_number','id')
  777. ->get()->toArray();
  778. if(empty($data)) return [true, []];
  779. list($status,$msg) = self::getWaitCommon($data,$user);
  780. return [$status, $msg];
  781. }
  782. public static function purchaseCheck($user,$search){
  783. $args = "";
  784. if($search['is_check'] == 1) {
  785. list($status, $id) = self::getWaitForPurchaseCheck($user,$search);
  786. //待审核
  787. $check = implode(",", PurchaseOrder::$wait_check);
  788. $args = "(state IN (" . $check ."))";
  789. if($status) {
  790. $wait_for_me = $search['wait_for_me'] ?? 0;
  791. $check_2 = implode(',', array_diff(PurchaseOrder::$wait_check, [PurchaseOrder::STATE_ONE]));
  792. $id = implode(",", $id);
  793. if($wait_for_me){
  794. if(empty($id)) {
  795. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  796. }else{
  797. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  798. }
  799. }
  800. }
  801. }elseif($search['is_check'] == 2){
  802. //已审
  803. $args = "(state >= ". PurchaseOrder::STATE_TWO . ")";
  804. }
  805. return $args;
  806. }
  807. private static function getWaitForPurchaseCheck($user, $search){
  808. if(! isset($search['wait_for_me'])) return [false, []];
  809. //获取待审核数据
  810. $args = "(state = " . PurchaseOrder::STATE_ONE . ")";
  811. $data = PurchaseOrder::where('del_time',0)
  812. ->whereRaw($args)
  813. ->select('order_number','id')
  814. ->get()->toArray();
  815. if(empty($data)) return [true, []];
  816. list($status,$msg) = self::getWaitCommon($data,$user);
  817. return [$status, $msg];
  818. }
  819. public static function inventoryCheck($user,$search){
  820. $args = "";
  821. if($search['is_check'] == 1) {
  822. list($status, $id) = self::getWaitForinventoryCheck($user,$search);
  823. //待审核
  824. $check = implode(",", Inventory::$wait_check);
  825. $args = "(state IN (" . $check ."))";
  826. if($status) {
  827. $wait_for_me = $search['wait_for_me'] ?? 0;
  828. $check_2 = implode(',', array_diff(Inventory::$wait_check, [Inventory::STATE_ONE]));
  829. $id = implode(",", $id);
  830. if($wait_for_me){
  831. if(empty($id)) {
  832. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  833. }else{
  834. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  835. }
  836. }
  837. }
  838. }elseif($search['is_check'] == 2){
  839. //已审
  840. $args = "(state >= ". Inventory::STATE_TWO . ")";
  841. }
  842. return $args;
  843. }
  844. private static function getWaitForinventoryCheck($user, $search){
  845. if(! isset($search['wait_for_me'])) return [false, []];
  846. //获取待审核数据
  847. $args = "(state = " . Inventory::STATE_ONE . ")";
  848. $data = Inventory::where('del_time',0)
  849. ->whereRaw($args)
  850. ->select('order_number','id')
  851. ->get()->toArray();
  852. if(empty($data)) return [true, []];
  853. list($status,$msg) = self::getWaitCommon($data,$user);
  854. return [$status, $msg];
  855. }
  856. public static function productAdjustmentCheck($user,$search){
  857. $args = "";
  858. if($search['is_check'] == 1) {
  859. list($status, $id) = self::getProductAdjustmentCheck($user,$search);
  860. //待审核
  861. $check = implode(",", ProductAdjustment::$wait_check);
  862. $args = "(state IN (" . $check ."))";
  863. if($status) {
  864. $wait_for_me = $search['wait_for_me'] ?? 0;
  865. $check_2 = implode(',', array_diff(ProductAdjustment::$wait_check, [ProductAdjustment::STATE_ONE]));
  866. $id = implode(",", $id);
  867. if($wait_for_me){
  868. if(empty($id)) {
  869. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  870. }else{
  871. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  872. }
  873. }
  874. }
  875. }elseif($search['is_check'] == 2){
  876. //已审
  877. $args = "(state >= ". ProductAdjustment::STATE_TWO . ")";
  878. }
  879. return $args;
  880. }
  881. private static function getProductAdjustmentCheck($user, $search){
  882. if(! isset($search['wait_for_me'])) return [false, []];
  883. //获取待审核数据
  884. $args = "(state = " . ProductAdjustment::STATE_ONE . ")";
  885. $data = ProductAdjustment::where('del_time',0)
  886. ->whereRaw($args)
  887. ->select('order_number','id')
  888. ->get()->toArray();
  889. if(empty($data)) return [true, []];
  890. list($status,$msg) = self::getWaitCommon($data,$user);
  891. return [$status, $msg];
  892. }
  893. private static function getWaitCommon($data,$user){
  894. $data_map = array_column($data,'id','order_number');
  895. //查找对应审批流数据
  896. $orderNoGroups = OaOrder::whereIn('order_no', array_column($data,'order_number'))
  897. ->get()
  898. ->groupBy('order_no');
  899. $maxIds = $orderNoGroups->map(function ($group) {
  900. return $group->max('id');
  901. });
  902. $map = $maxIds->toArray();
  903. if(empty($map)) return [true, []];
  904. $oa_order_id = array_values($map);
  905. $map2 = array_flip($map);
  906. unset($map);
  907. //获取审批流下的人
  908. $list = OaOrderSub::whereIn('oa_order_id', $oa_order_id)
  909. ->whereIn('state',[0,1])
  910. ->select('id','state','oa_order_id')
  911. ->orderBy('id', 'desc')
  912. ->get()->toArray();
  913. $subEmployeeList = OaOrderSubEmployee::whereIn('oa_order_id', $oa_order_id)
  914. ->where('employee_id',$user['id'])
  915. ->get()->toArray();
  916. if(empty($subEmployeeList)) return [true, []];
  917. //每条数据对应的人
  918. $emp_id_key_list = [];
  919. foreach ($subEmployeeList as $v) {
  920. $emp_id_key_list[$v['oa_order_sub_id']][] = $v['employee_id'];
  921. }
  922. unset($subEmployeeList);
  923. $flag = $id = [];
  924. foreach ($list as $v) {
  925. //不存在单号或者已存在单号返回数据
  926. if(empty($v['oa_order_id']) || isset($flag[$v['oa_order_id']])) continue;
  927. $emp_tmp = $emp_id_key_list[$v['id']] ?? [];
  928. $flag[$v['oa_order_id']] = $emp_tmp;
  929. $order_number = $map2[$v['oa_order_id']] ?? "";
  930. $sales_id = $data_map[$order_number] ?? 0;
  931. if(in_array($user['id'], $emp_tmp)) $id[] = $sales_id;
  932. }
  933. unset($flag);
  934. return [true, $id];
  935. }
  936. //全部 待审 已审核 -----------------------------------------------
  937. }