/ app / Http / Livewire / InboxFiles / Index.php
Index.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\InboxFiles;
  4  
  5  use App\Enums\InboxFileStatusEnum;
  6  use App\Enums\InboxFileTypeEnum;
  7  use App\Http\Services\DocumentService;
  8  use App\Http\Services\InboxFileService;
  9  use App\Http\Services\TaskService;
 10  use App\Models\DocumentType;
 11  use App\Models\InboxFile;
 12  use App\Models\Review;
 13  use App\Models\Role;
 14  use App\Models\User;
 15  use App\Traits\WithPerPagePagination;
 16  use Illuminate\Support\Facades\Storage;
 17  use Livewire\Component;
 18  use Livewire\Redirector;
 19  use Livewire\WithPagination;
 20  use App\Enums\RoleEnum;
 21  use App\Models\ValidationStatus;
 22  
 23  class Index extends Component
 24  {
 25      use WithPagination, WithPerPagePagination;
 26  
 27      public bool $readyToLoad = false;
 28  
 29      public array $filters = [
 30          'search' => '',
 31          'created_at_since' => null,
 32          'created_at_to' => null,
 33          'updated_at_since' => null,
 34          'updated_at_to' => null,
 35          'types' => [],
 36          'uploaded_by' => [],
 37          'reviewed_by' => [],
 38          'certifiers' => [],
 39          'reviewed' => null,
 40          'approved' => null,
 41          'onlyCompleted' => false,
 42          'status_ids' => [],
 43          'report_status_ids' => [],
 44      ];
 45  
 46      public string $sortField = 'updated_at';
 47      public string $sortDirection = 'desc';
 48      public array $validationTypes = [];
 49      public array $reviewers = [];
 50  
 51      protected $rules = [
 52          'filters.created_at_since' => 'nullable|date',
 53          'filters.created_at_to' => 'nullable|date|after:filters.created_at_since',
 54          'filters.updated_at_since' => 'nullable|date',
 55          'filters.updated_at_to' => 'nullable|date|after:filters.updated_at_since',
 56          'filters.search' => 'nullable|string',
 57          'filters.types' => 'nullable|array',
 58          'filters.types.*' => 'nullable|integer',
 59          'filters.uploaded_by' => 'nullable|array',
 60          'filters.uploaded_by.*' => 'nullable|integer',
 61          'filters.reviewed_by' => 'nullable|array',
 62          'filters.reviewed_by.*' => 'nullable|integer',
 63          'filters.reviewed' => 'nullable|boolean',
 64          'filters.approved' => 'nullable|boolean',
 65          'filters.onlyCompleted' => 'nullable|boolean',
 66          'filters.status_ids' => 'nullable|array',
 67          'filters.status_ids.*' => 'nullable|integer',
 68          'filters.report_status_ids' => 'nullable|array',
 69          'filters.report_status_ids.*' => 'nullable|integer',
 70      ];
 71  
 72      public function checkReviewedByFilter()
 73      {
 74          if ($this->filters['reviewed'] === []) {
 75              return true;
 76          }
 77  
 78          foreach ($this->filters['reviewed'] as $reviewed) {
 79              if ($reviewed === true) {
 80                  return true;
 81              }
 82          }
 83      }
 84  
 85      public function downloadFile(int $id)
 86      {
 87          $inboxFile = InboxFile::findOrFail($id);
 88  
 89          return Storage::download($inboxFile->path);
 90      }
 91  
 92      public function fetchData()
 93      {
 94          $this->readyToLoad = true;
 95          $this->validationTypes = InboxFileTypeEnum::valueNameArray();
 96          $this->reviewers = Review::reviewers()->toArray();
 97      }
 98  
 99      public function updated($propertyName)
100      {
101          $this->validateOnly($propertyName);
102      }
103  
104      protected function getListeners()
105      {
106          return [
107              'refresh' => '$refresh',
108              'validationSaved' => '$refresh'
109          ];
110      }
111  
112      public function updatingSearch(): void
113      {
114          $this->resetPage();
115      }
116  
117      public function updatedFilters(): void
118      {
119          $this->getInboxFilesProperty();
120      }
121  
122      public function sortBy(string $field): void
123      {
124          $this->sortField === $field
125              ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'
126              : $this->sortDirection = 'asc';
127  
128          $this->sortField = $field;
129      }
130  
131      public function getRowsQueryProperty()
132      {
133          return InboxFileService::search($this->sortField, $this->sortDirection, $this->filters);
134      }
135  
136      public function getInboxFilesProperty()
137      {
138          return $this->readyToLoad ? $this->applyPagination($this->rowsQuery) : [];
139      }
140  
141      public function getCertifiersProperty()
142      {
143          return User::all(['id', 'name', 'lastname'])->filter(function ($user) {
144              return $user->allDossiers()->count() > 0;
145          })->map(function ($user) {
146              return [
147                  'id' => $user->id,
148                  'name' => $user->fullname
149              ];
150          });
151      }
152  
153      public function getPartialClass(InboxFile $inboxFile): string
154      {
155          if ($partialClass = InboxFileService::isPartialReportType($inboxFile->path))
156              return $partialClass;
157  
158          return '';
159      }
160  
161      public function getYesNoOptionsProperty()
162      {
163          return [
164              ['value' => true, 'name' => __('app.yes')],
165              ['value' => false, 'name' => __('app.no')],
166          ];
167      }
168  
169      public function getStatusOptionsProperty()
170      {
171          return InboxFileStatusEnum::cases();
172      }
173  
174      public function getReportStatusOptionsProperty()
175      {
176          return ValidationStatus::all()
177              ->map(function ($status) {
178                  return [
179                      'value' => $status->id,
180                      'name' => __('inbox-files.requirement.status.' . $status->name),
181                  ];
182              })
183              ->toArray();
184      }
185  
186      public function viewRow(int $rowId)
187      {
188          $inboxFile = InboxFile::findOrFail($rowId);
189  
190          if (!$inboxFile->completed) {
191              return redirect()->to(route('validations.inbox-files.view', ['inboxFile' => $rowId]));
192          }
193      }
194  
195      public function redirectToValidation(int $id)
196      {
197          $inboxFile = InboxFile::findOrFail($id);
198          return redirect()->to(route('validations.inbox-files.validations.index', ['inboxFile' => $inboxFile]));
199      }
200  
201      public function render()
202      {
203          return view('livewire.inbox-files.index')
204              ->layoutData([
205                  'title' => __('inbox-files.title'),
206                  'entity' => null,
207                  'breadcrumb' => 'validations.inbox-files.index',
208              ]);
209      }
210  }