RangeService.php 44 KB

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