RangeService.php 37 KB

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