RangeService.php 45 KB

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