RangeService.php 40 KB

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