RangeService.php 42 KB

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