ProductInventoryScope.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Scopes;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Scope;
  6. //现存量 库存台账
  7. class ProductInventoryScope implements Scope
  8. {
  9. public $user = [];
  10. public $search = [];
  11. public function __construct($user = [],$search = [])
  12. {
  13. $this->user = $user;
  14. $this->search = $search;
  15. }
  16. public function apply(Builder $builder, Model $model)
  17. {
  18. //是否所有部门
  19. $is_all_depart = $this->user['is_all_depart'] ?? 0;
  20. //权限范围内的部门
  21. $depart_range = $this->user['depart_range'] ?? [];
  22. //顶级部门
  23. $search_depart_id = $this->search['top_depart_id'] ?? 0;
  24. if(empty($search_depart_id)){
  25. //默认进来 自身顶级公司
  26. $top_depart_id = $this->user['depart_top'][0] ?? [];
  27. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  28. }else{
  29. //查询 顶级公司
  30. $top_depart_id = $search_depart_id;
  31. }
  32. if($is_all_depart){
  33. //所有部门
  34. if(empty($search_depart_id)){
  35. //全部
  36. $builder->whereIn('a.top_depart_id', $depart_range);
  37. }else{
  38. //查看某个分社
  39. $builder->where('a.top_depart_id', $top_depart_id);
  40. }
  41. }else{
  42. //某个分社全部
  43. $builder->where('a.top_depart_id', $top_depart_id);
  44. }
  45. }
  46. }