RangeService.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. $bool = ! empty($search['top_depart_id']) && ! empty($user['is_all_depart']);
  320. if(! $bool){
  321. $current_top_depart_id = $user['depart_top'][0] ?? [];
  322. $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
  323. //查找是否合同指派了门店
  324. $dispatch_company = SeeRange::where('del_time',0)
  325. ->whereIn('data_id',$sales_order_id)
  326. ->where('param_id', $current_top_depart_id)
  327. ->where('data_type',SeeRange::type_seven)
  328. ->where('type',SeeRange::data_three)
  329. ->select('data_id')
  330. ->get()->toArray();
  331. $dispatch_company = array_column($dispatch_company,'data_id');
  332. // 找出仅在 $array1 中存在的元素
  333. $diff1 = array_diff($sales_order_id, $dispatch_company);
  334. $sales_order_id = SalesOrder::whereIn('id', $diff1)
  335. ->where('top_depart_id',$current_top_depart_id)
  336. ->select('id')
  337. ->get()->toArray();
  338. $sales_order_id = array_column($sales_order_id,'sales_order_id');
  339. $sales_order_id = array_merge_recursive($sales_order_id,$dispatch_company);
  340. }
  341. //指派后 可见范围id
  342. $return = Self::getRangeDataId($user,SeeRange::type_seven);
  343. $return_id = array_unique(array_merge_recursive($sales_order_id,$return));
  344. if($bool){
  345. $id = DB::table('sales_order')
  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. if(isset($search['is_check'])){
  355. $args = self::salesOrderCheck($user,$search);
  356. $result = SalesOrder::whereIn('id',$return_id)
  357. ->when(! empty($args), function ($query) use ($args) {
  358. return $query->whereRaw($args);
  359. })
  360. ->select('id')
  361. ->get()->toArray();
  362. $return_id = array_column($result,'id');
  363. }
  364. return $return_id;
  365. }
  366. //获取特殊采购单可见数据
  367. public static function purchaseSpecialRange($user,$search){
  368. //可见范围id
  369. $return_id = Self::getRangeDataId($user,SeeRange::type_ten);
  370. // if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  371. // $id = DB::table('purchase_order')
  372. // ->where('del_time',0)
  373. // ->where('top_depart_id',$search['top_depart_id'])
  374. // ->select('id')->get()->toArray();
  375. // $id = array_column($id,'id');
  376. // foreach ($return_id as $key => $value){
  377. // if(! in_array($value,$id)) unset($return_id[$key]);
  378. // }
  379. // }
  380. return $return_id;
  381. }
  382. //获取供应商可见数据
  383. public static function supplierRange($user,$search){
  384. //可见范围id
  385. $return_id = Self::getRangeDataId($user,SeeRange::type_nine);
  386. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  387. $id = DB::table('supplier')
  388. ->where('del_time',0)
  389. ->where('top_depart_id',$search['top_depart_id'])
  390. ->select('id')->get()->toArray();
  391. $id = array_column($id,'id');
  392. foreach ($return_id as $key => $value){
  393. if(! in_array($value,$id)) unset($return_id[$key]);
  394. }
  395. }
  396. return $return_id;
  397. }
  398. //获取活动包可见数据
  399. public static function sportsBagRange($user,$search){
  400. //可见范围id
  401. $return_id = Self::getRangeDataId($user,SeeRange::type_eight);
  402. if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
  403. $id = DB::table('sports_bag')
  404. ->where('del_time',0)
  405. ->where('top_depart_id',$search['top_depart_id'])
  406. ->select('id')->get()->toArray();
  407. $id = array_column($id,'id');
  408. foreach ($return_id as $key => $value){
  409. if(! in_array($value,$id)) unset($return_id[$key]);
  410. }
  411. }
  412. return $return_id;
  413. }
  414. //产品不可见部门
  415. public static function productNotSeeRange($product_id){
  416. $return = [];
  417. $result = SeeRange::where('del_time',0)
  418. ->where('data_type', SeeRange::type_four)
  419. ->whereIn('data_id',$product_id)
  420. ->where('type',SeeRange::data_one)
  421. ->select('param_id as depart_id','data_id as product_id')
  422. ->get()->toArray();
  423. foreach ($result as $value){
  424. $return[$value['product_id']][] = $value['depart_id'];
  425. }
  426. return $return;
  427. }
  428. //产品签订人负责人
  429. public function salesOrderSearch($data){
  430. $return1 = $return2 = [];
  431. if(! empty($data['qd'])){
  432. $emp_id = Employee::where('del_time',0)
  433. ->where('emp_name','LIKE', '%'.$data['qd'].'%')
  434. ->select('id')->get()->toArray();
  435. $emp_id = array_column($emp_id,'id');
  436. //单据中选择的签订人
  437. $sales_order_id = SalesOrderInfo::where('del_time',0)
  438. ->where('type',SalesOrderInfo::type_one)
  439. ->whereIn('data_id',$emp_id)
  440. ->select('sales_order_id')
  441. ->get()->toArray();
  442. $return1 = array_unique(array_column($sales_order_id,'sales_order_id'));
  443. }
  444. if(! empty($data['fz'])){
  445. $emp_id = Employee::where('del_time',0)
  446. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  447. ->select('id')->get()->toArray();
  448. $emp_id = array_column($emp_id,'id');
  449. //单据中选择的负责人
  450. $sales_order_id = SalesOrderInfo::where('del_time',0)
  451. ->where('type',SalesOrderInfo::type_two)
  452. ->whereIn('data_id',$emp_id)
  453. ->select('sales_order_id')
  454. ->get()->toArray();
  455. $return2 = array_unique(array_column($sales_order_id,'sales_order_id'));
  456. }
  457. if(! empty($data['qd']) && ! empty($data['fz'])){
  458. $return = array_intersect($return1, $return2);
  459. }elseif(!empty($data['qd'])){
  460. $return = $return1;
  461. }else{
  462. $return = $return2;
  463. }
  464. return $return;
  465. }
  466. //指派门店
  467. public function salesOrderZpSearch($data){
  468. $return = SeeRange::where('del_time',0)
  469. ->where('param_id',$data['zp'])
  470. ->where('data_type',SeeRange::type_seven)
  471. ->where('type',SeeRange::data_three)
  472. ->select('data_id')
  473. ->get()->toArray();
  474. return array_column($return,'data_id');
  475. }
  476. //客户联系方式
  477. public function salesOrderCustomerMessageSearch($data){
  478. $return = CustomerInfo::where('del_time',0)
  479. ->where("type",CustomerInfo::type_one)
  480. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  481. ->select('customer_id')
  482. ->get()->toArray();
  483. return array_column($return,'customer_id');
  484. }
  485. //客户创建人
  486. public function salesOrderCustomerCrtSearch($user,$data){
  487. $emp_id = Employee::where('del_time',0)
  488. ->where('emp_name','LIKE', '%'.$data['customer_crt_name'].'%')
  489. ->select('id')->get()->toArray();
  490. $emp_id = array_column($emp_id,'id');
  491. $model2 = Customer::Clear($user,$data);
  492. $customer = $model2->where('del_time',0)
  493. ->whereIn('crt_id', $emp_id)
  494. ->select('id')
  495. ->get()->toArray();
  496. return array_column($customer,'id');
  497. }
  498. //收付款人搜索
  499. public function paymentReceiptSearch($data){
  500. $emp_id = Employee::where('del_time',0)
  501. ->where('emp_name','LIKE', '%'.$data['belong'].'%')
  502. ->select('id')->get()->toArray();
  503. $emp_id = array_column($emp_id,'id');
  504. //单据中选择的签订人
  505. $id = PaymentReceiptInfo::where('del_time',0)
  506. ->where('type',PaymentReceiptInfo::type_two)
  507. ->whereIn('data_id',$emp_id)
  508. ->select('payment_receipt_id')
  509. ->get()->toArray();
  510. return array_unique(array_column($id,'payment_receipt_id'));
  511. }
  512. //创建人
  513. public function crtNameSearch($data){
  514. $emp_id = Employee::where('del_time',0)
  515. ->where('emp_name','LIKE', '%'.$data['crt_name'].'%')
  516. ->select('id')->get()->toArray();
  517. return array_column($emp_id,'id');
  518. }
  519. public function crtContactSearch($data){
  520. $id = CustomerInfo::where('del_time',0)
  521. ->where('type',CustomerInfo::type_one)
  522. ->where('contact_info','LIKE', '%'.$data['title_t'].'%')
  523. ->select('customer_id')->get()->toArray();
  524. return array_column($id,'customer_id');
  525. }
  526. //负责人
  527. public function customerSearch($data){
  528. $emp_id = Employee::where('del_time',0)
  529. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  530. ->select('id')->get()->toArray();
  531. $emp_id = array_column($emp_id,'id');
  532. //单据中选择的负责人
  533. $customer_id = CustomerInfo::where('del_time',0)
  534. ->where('type',CustomerInfo::type_two)
  535. ->whereIn('data_id',$emp_id)
  536. ->select('customer_id')
  537. ->get()->toArray();
  538. return array_unique(array_column($customer_id,'customer_id'));;
  539. }
  540. //获取可见人施工单
  541. public function RangeConstructionEmpDetail($data_id = 0){
  542. if(empty($data_id)) return [];
  543. $see = ConstructionInfo::where('del_time',0)
  544. ->whereIn('construction_id',$data_id)
  545. ->where('type',ConstructionInfo::type_three)
  546. ->get()->toArray();
  547. $emp_map = Employee::where('del_time',0)
  548. ->whereIn('id',array_column($see,'employee_id'))
  549. ->pluck('emp_name','id')->toArray();
  550. $employee = [];
  551. foreach ($see as $value){
  552. $name = $emp_map[$value['employee_id']] ?? '';
  553. if(! empty($name)){
  554. $tmp = [
  555. 'id' => $value['employee_id'],
  556. 'emp_name' => $emp_map[$value['employee_id']] ?? '',
  557. ];
  558. $employee[$value['construction_id']][] = $tmp;
  559. }
  560. }
  561. return $employee;
  562. }
  563. //客户类型
  564. public function customerBasicTypeSearch($customer_type, $type){
  565. $result = BasicType::where('del_time',0)
  566. ->whereIn('type',$type)
  567. ->where('title', $customer_type)
  568. ->select('id')->get()->toArray();
  569. return array_column($result,'id');
  570. }
  571. //全部 待审 已审核 -----------------------------------------------
  572. public static function sportsBagCheck($user,$search){
  573. $args = "";
  574. if($search['is_check'] == 1) {
  575. list($status, $id) = self::getWaitForSportsCheck($user,$search);
  576. //待审核
  577. $check = implode(",", SportsBag::$wait_check);
  578. $args = "(state IN (" . implode(",", SportsBag::$wait_check) ."))";
  579. if($status) {
  580. $wait_for_me = $search['wait_for_me'] ?? 0;
  581. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [SportsBag::STATE_ONE]));
  582. $id = implode(",", $id);
  583. if($wait_for_me){
  584. if(empty($id)) {
  585. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  586. }else{
  587. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  588. }
  589. }
  590. }
  591. }elseif($search['is_check'] == 2){
  592. //已审
  593. $args = "(state = ". SportsBag::STATE_TWO . ")";
  594. }
  595. return $args;
  596. }
  597. private static function getWaitForSportsCheck($user, $search){
  598. if(! isset($search['wait_for_me'])) return [false, []];
  599. //获取待审核
  600. $args = "(state = " . SportsBag::STATE_ONE . ")";
  601. $data = SportsBag::where('del_time',0)
  602. ->whereRaw($args)
  603. ->select('order_number','id')
  604. ->get()->toArray();
  605. if(empty($data)) return [true, []];
  606. list($status,$msg) = self::getWaitCommon($data,$user);
  607. return [$status, $msg];
  608. }
  609. public static function paymentReceiptCheck($user,$search){
  610. $args = "";
  611. if($search['is_check'] == 1) {
  612. list($status, $id) = self::getWaitForPaymentCheck($user,$search);
  613. //待审核
  614. $check = implode(",", SportsBag::$wait_check);
  615. $args = "(state IN (" . $check ."))";
  616. if($status) {
  617. $wait_for_me = $search['wait_for_me'] ?? 0;
  618. $id = implode(",", $id);
  619. $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PaymentReceipt::STATE_ONE]));
  620. if($wait_for_me){
  621. if(empty($id)) {
  622. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  623. }else{
  624. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  625. }
  626. }
  627. }
  628. }elseif($search['is_check'] == 2){
  629. //已审
  630. $args = "(state = ". PaymentReceipt::STATE_TWO . ")";
  631. }
  632. return $args;
  633. }
  634. private static function getWaitForPaymentCheck($user, $search){
  635. if(! isset($search['wait_for_me'])) return [false, []];
  636. //获取待审核
  637. $args = "(state = " . PaymentReceipt::STATE_ONE . ")";
  638. $data = PaymentReceipt::where('del_time',0)
  639. ->whereRaw($args)
  640. ->select('order_number','id')
  641. ->get()->toArray();
  642. if(empty($data)) return [true, []];
  643. list($status,$msg) = self::getWaitCommon($data,$user);
  644. return [$status, $msg];
  645. }
  646. public static function salesOrderCheck($user,$search){
  647. $args = "";
  648. if($search['is_check'] == 1) {
  649. list($status, $id) = self::getWaitForSalesCheck($user,$search);
  650. //待审核
  651. $check = implode(",", SalesOrder::$wait_check);
  652. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . $check ."))";
  653. if($status) {
  654. $wait_for_me = $search['wait_for_me'] ?? 0;
  655. $check_2 = implode(',', array_diff(SalesOrder::$wait_check, [SalesOrder::State_one]));
  656. $id = implode(",", $id);
  657. if($wait_for_me){
  658. if(empty($id)) {
  659. $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 ."))))";
  660. }else{
  661. $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 ."))))";
  662. }
  663. }
  664. }
  665. }elseif($search['is_check'] == 2){
  666. //已审 线上订单的已审核是2 其它是 3
  667. $args = SalesOrder::search;
  668. }
  669. return $args;
  670. }
  671. private static function getWaitForSalesCheck($user, $search){
  672. if(! isset($search['wait_for_me'])) return [false, []];
  673. //获取待审核合同
  674. $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state = " . SalesOrder::State_one . ")";
  675. $data = SalesOrder::where('del_time',0)
  676. ->whereRaw($args)
  677. ->select('order_number','id')
  678. ->get()->toArray();
  679. if(empty($data)) return [true, []];
  680. list($status,$msg) = self::getWaitCommon($data,$user);
  681. return [$status, $msg];
  682. }
  683. public static function invoiceCheck($user,$search){
  684. $args = "";
  685. if($search['is_check'] == 1) {
  686. list($status, $id) = self::getWaitForInvoiceCheck($user,$search);
  687. //待审核
  688. $check = implode(",", InvoiceOrder::$wait_check);
  689. $args = "(state IN (" . $check ."))";
  690. if($status) {
  691. $wait_for_me = $search['wait_for_me'] ?? 0;
  692. $check_2 = implode(',', array_diff(InvoiceOrder::$wait_check, [InvoiceOrder::STATE_ONE]));
  693. $id = implode(",", $id);
  694. if($wait_for_me){
  695. if(empty($id)) {
  696. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  697. }else{
  698. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  699. }
  700. }
  701. }
  702. }elseif($search['is_check'] == 2){
  703. //已审
  704. $args = "(state = ". InvoiceOrder::STATE_TWO . ")";
  705. }
  706. return $args;
  707. }
  708. private static function getWaitForInvoiceCheck($user, $search){
  709. if(! isset($search['wait_for_me'])) return [false, []];
  710. //获取待审核数据
  711. $args = "(state = " . InvoiceOrder::STATE_ONE . ")";
  712. $data = InvoiceOrder::where('del_time',0)
  713. ->whereRaw($args)
  714. ->select('order_number','id')
  715. ->get()->toArray();
  716. if(empty($data)) return [true, []];
  717. list($status,$msg) = self::getWaitCommon($data,$user);
  718. return [$status, $msg];
  719. }
  720. public static function returnExchangeOrderCheck($user,$search){
  721. $args = "";
  722. if($search['is_check'] == 1) {
  723. list($status, $id) = self::getWaitForReturnExchangeCheck($user,$search);
  724. //待审核
  725. $check = implode(",", ReturnExchangeOrder::$wait_check);
  726. $args = "(state IN (" . $check ."))";
  727. if($status) {
  728. $wait_for_me = $search['wait_for_me'] ?? 0;
  729. $check_2 = implode(',', array_diff(ReturnExchangeOrder::$wait_check, [ReturnExchangeOrder::State_one]));
  730. $id = implode(",", $id);
  731. if($wait_for_me){
  732. if(empty($id)) {
  733. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  734. }else{
  735. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  736. }
  737. }
  738. }
  739. }elseif($search['is_check'] == 2){
  740. //已审
  741. $args = "(state = ". ReturnExchangeOrder::State_two . ")";
  742. }
  743. return $args;
  744. }
  745. private static function getWaitForReturnExchangeCheck($user, $search){
  746. if(! isset($search['wait_for_me'])) return [false, []];
  747. //获取待审核数据
  748. $args = "(state = " . ReturnExchangeOrder::State_one . ")";
  749. $data = ReturnExchangeOrder::where('del_time',0)
  750. ->whereRaw($args)
  751. ->select('order_number','id')
  752. ->get()->toArray();
  753. if(empty($data)) return [true, []];
  754. list($status,$msg) = self::getWaitCommon($data,$user);
  755. return [$status, $msg];
  756. }
  757. public static function constructionCheck($user,$search){
  758. $args = "";
  759. if($search['is_check'] == 1) {
  760. list($status, $id) = self::getWaitForConstructionCheck($user,$search);
  761. //待审核
  762. $check = implode(",", Construction::$wait_check);
  763. $args = "(state IN (" . $check ."))";
  764. if($status) {
  765. $wait_for_me = $search['wait_for_me'] ?? 0;
  766. $check_2 = implode(',', array_diff(Construction::$wait_check, [Construction::STATE_ONE]));
  767. $id = implode(",", $id);
  768. if($wait_for_me){
  769. if(empty($id)) {
  770. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  771. }else{
  772. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  773. }
  774. }
  775. }
  776. }elseif($search['is_check'] == 2){
  777. //已审
  778. $args = "(state >= ". Construction::STATE_TWO . ")";
  779. }
  780. return $args;
  781. }
  782. private static function getWaitForConstructionCheck($user, $search){
  783. if(! isset($search['wait_for_me'])) return [false, []];
  784. //获取待审核数据
  785. $args = "(state = " . Construction::STATE_ONE . ")";
  786. $data = Construction::where('del_time',0)
  787. ->whereRaw($args)
  788. ->select('order_number','id')
  789. ->get()->toArray();
  790. if(empty($data)) return [true, []];
  791. list($status,$msg) = self::getWaitCommon($data,$user);
  792. return [$status, $msg];
  793. }
  794. public static function purchaseCheck($user,$search){
  795. $args = "";
  796. if($search['is_check'] == 1) {
  797. list($status, $id) = self::getWaitForPurchaseCheck($user,$search);
  798. //待审核
  799. $check = implode(",", PurchaseOrder::$wait_check);
  800. $args = "(state IN (" . $check ."))";
  801. if($status) {
  802. $wait_for_me = $search['wait_for_me'] ?? 0;
  803. $check_2 = implode(',', array_diff(PurchaseOrder::$wait_check, [PurchaseOrder::STATE_ONE]));
  804. $id = implode(",", $id);
  805. if($wait_for_me){
  806. if(empty($id)) {
  807. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  808. }else{
  809. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  810. }
  811. }
  812. }
  813. }elseif($search['is_check'] == 2){
  814. //已审
  815. $args = "(state >= ". PurchaseOrder::STATE_TWO . ")";
  816. }
  817. return $args;
  818. }
  819. private static function getWaitForPurchaseCheck($user, $search){
  820. if(! isset($search['wait_for_me'])) return [false, []];
  821. //获取待审核数据
  822. $args = "(state = " . PurchaseOrder::STATE_ONE . ")";
  823. $data = PurchaseOrder::where('del_time',0)
  824. ->whereRaw($args)
  825. ->select('order_number','id')
  826. ->get()->toArray();
  827. if(empty($data)) return [true, []];
  828. list($status,$msg) = self::getWaitCommon($data,$user);
  829. return [$status, $msg];
  830. }
  831. public static function inventoryCheck($user,$search){
  832. $args = "";
  833. if($search['is_check'] == 1) {
  834. list($status, $id) = self::getWaitForinventoryCheck($user,$search);
  835. //待审核
  836. $check = implode(",", Inventory::$wait_check);
  837. $args = "(state IN (" . $check ."))";
  838. if($status) {
  839. $wait_for_me = $search['wait_for_me'] ?? 0;
  840. $check_2 = implode(',', array_diff(Inventory::$wait_check, [Inventory::STATE_ONE]));
  841. $id = implode(",", $id);
  842. if($wait_for_me){
  843. if(empty($id)) {
  844. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  845. }else{
  846. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  847. }
  848. }
  849. }
  850. }elseif($search['is_check'] == 2){
  851. //已审
  852. $args = "(state >= ". Inventory::STATE_TWO . ")";
  853. }
  854. return $args;
  855. }
  856. private static function getWaitForinventoryCheck($user, $search){
  857. if(! isset($search['wait_for_me'])) return [false, []];
  858. //获取待审核数据
  859. $args = "(state = " . Inventory::STATE_ONE . ")";
  860. $data = Inventory::where('del_time',0)
  861. ->whereRaw($args)
  862. ->select('order_number','id')
  863. ->get()->toArray();
  864. if(empty($data)) return [true, []];
  865. list($status,$msg) = self::getWaitCommon($data,$user);
  866. return [$status, $msg];
  867. }
  868. public static function productAdjustmentCheck($user,$search){
  869. $args = "";
  870. if($search['is_check'] == 1) {
  871. list($status, $id) = self::getProductAdjustmentCheck($user,$search);
  872. //待审核
  873. $check = implode(",", ProductAdjustment::$wait_check);
  874. $args = "(state IN (" . $check ."))";
  875. if($status) {
  876. $wait_for_me = $search['wait_for_me'] ?? 0;
  877. $check_2 = implode(',', array_diff(ProductAdjustment::$wait_check, [ProductAdjustment::STATE_ONE]));
  878. $id = implode(",", $id);
  879. if($wait_for_me){
  880. if(empty($id)) {
  881. $args = "(state IN (" . $check .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  882. }else{
  883. $args = "(state IN (" . $check .") and (id IN (" . $id .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
  884. }
  885. }
  886. }
  887. }elseif($search['is_check'] == 2){
  888. //已审
  889. $args = "(state >= ". ProductAdjustment::STATE_TWO . ")";
  890. }
  891. return $args;
  892. }
  893. private static function getProductAdjustmentCheck($user, $search){
  894. if(! isset($search['wait_for_me'])) return [false, []];
  895. //获取待审核数据
  896. $args = "(state = " . ProductAdjustment::STATE_ONE . ")";
  897. $data = ProductAdjustment::where('del_time',0)
  898. ->whereRaw($args)
  899. ->select('order_number','id')
  900. ->get()->toArray();
  901. if(empty($data)) return [true, []];
  902. list($status,$msg) = self::getWaitCommon($data,$user);
  903. return [$status, $msg];
  904. }
  905. private static function getWaitCommon($data,$user){
  906. $data_map = array_column($data,'id','order_number');
  907. //查找对应审批流数据
  908. $orderNoGroups = OaOrder::whereIn('order_no', array_column($data,'order_number'))
  909. ->get()
  910. ->groupBy('order_no');
  911. $maxIds = $orderNoGroups->map(function ($group) {
  912. return $group->max('id');
  913. });
  914. $map = $maxIds->toArray();
  915. if(empty($map)) return [true, []];
  916. $oa_order_id = array_values($map);
  917. $map2 = array_flip($map);
  918. unset($map);
  919. //获取审批流下的人
  920. $list = OaOrderSub::whereIn('oa_order_id', $oa_order_id)
  921. ->whereIn('state',[0,1])
  922. ->select('id','state','oa_order_id')
  923. ->orderBy('id', 'desc')
  924. ->get()->toArray();
  925. $subEmployeeList = OaOrderSubEmployee::whereIn('oa_order_id', $oa_order_id)
  926. ->where('employee_id',$user['id'])
  927. ->get()->toArray();
  928. if(empty($subEmployeeList)) return [true, []];
  929. //每条数据对应的人
  930. $emp_id_key_list = [];
  931. foreach ($subEmployeeList as $v) {
  932. $emp_id_key_list[$v['oa_order_sub_id']][] = $v['employee_id'];
  933. }
  934. unset($subEmployeeList);
  935. $flag = $id = [];
  936. foreach ($list as $v) {
  937. //不存在单号或者已存在单号返回数据
  938. if(empty($v['oa_order_id']) || isset($flag[$v['oa_order_id']])) continue;
  939. $emp_tmp = $emp_id_key_list[$v['id']] ?? [];
  940. $flag[$v['oa_order_id']] = $emp_tmp;
  941. $order_number = $map2[$v['oa_order_id']] ?? "";
  942. $sales_id = $data_map[$order_number] ?? 0;
  943. if(in_array($user['id'], $emp_tmp)) $id[] = $sales_id;
  944. }
  945. unset($flag);
  946. return [true, $id];
  947. }
  948. //全部 待审 已审核 -----------------------------------------------
  949. }