RangeService.php 41 KB

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