/ app / Http / Livewire / Resolutions / Index.php
Index.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\Resolutions;
  4  
  5  use App\Exports\ResolutionExport;
  6  use App\Http\Services\ResolutionService;
  7  use App\Models\Dossier;
  8  use App\Models\Resolution;
  9  use App\Traits\WithPerPagePagination;
 10  use Excel;
 11  use Exception;
 12  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 13  use Livewire\Component;
 14  use Livewire\WithPagination;
 15  use WireUi\Traits\Actions;
 16  
 17  class Index extends Component
 18  {
 19      use AuthorizesRequests, Actions, WithPagination, WithPerPagePagination;
 20  
 21      public bool $isMainView = true;
 22  
 23      public ?int $dossierId = null;
 24  
 25      public array $filters = [
 26          'code' => [],
 27          'resolution_year' => '',
 28          'approved' => null,
 29          'created_at_since' => null,
 30          'created_at_to' => null,
 31          'withTrashed' => false,
 32      ];
 33  
 34      public bool $readyToLoad = false;
 35  
 36      public string $search = '';
 37  
 38      public string $sortField = 'created_at';
 39  
 40      public string $sortDirection = 'asc';
 41  
 42      protected $rules = [
 43          'filters.created_at_since' => 'nullable|date',
 44          'filters.created_at_to' => 'nullable|date|after:filters.created_at_since',
 45      ];
 46  
 47      public int $pageNumber = 5;
 48  
 49      public string $pageName = 'resolutionPage';
 50  
 51      public function fetchData()
 52      {
 53          $this->readyToLoad = true;
 54      }
 55  
 56      protected function getListeners()
 57      {
 58          return ['refresh' => '$refresh'];
 59      }
 60  
 61      public function mount()
 62      {
 63          $this->dossierId ?
 64              $this->authorize('viewResolutions', Dossier::find($this->dossierId)) :
 65              $this->authorize('viewAny', Resolution::class);
 66      }
 67  
 68      public function updatingSearch(): void
 69      {
 70          $this->resetPage();
 71      }
 72  
 73      public function updatedFilters(): void
 74      {
 75          $this->getResolutionsProperty();
 76      }
 77  
 78      public function confirmDelete(int $id): void
 79      {
 80          $this->emit('openPanel', __('resolutions.panel.title.remove'), 'resolutions.delete', $id);
 81      }
 82  
 83  
 84      public function sortBy(string $field): void
 85      {
 86          $this->sortField === $field
 87              ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'
 88              : $this->sortDirection = 'asc';
 89  
 90          $this->sortField = $field;
 91      }
 92  
 93      public function getRowsQueryProperty()
 94      {
 95          return ResolutionService::search($this->search, $this->sortField, $this->sortDirection, $this->filters);
 96      }
 97  
 98      public function getResolutionsProperty()
 99      {
100          return $this->readyToLoad ? $this->applyPagination($this->rowsQuery) : [];
101      }
102  
103      public function getResolutionResultsProperty()
104      {
105          return [
106              [
107                  'name' => __('resolutions.panel.status.rejected'),
108                  'value' => 0,
109              ],
110              [
111                  'name' => __('resolutions.panel.status.approved'),
112                  'value' => 1,
113              ]
114          ];
115      }
116  
117      public function exportToCSV(int $dossierId = null)
118      {
119          $path = 'tmp/resoluciones.xlsx';
120          try {
121              Excel::store(new ResolutionExport(ResolutionService::getAllOrByDossierId($dossierId)), $path, 'local');
122              return redirect()->to('/download/' . $path);
123          } catch (Exception $e) {
124              logger()->error($e->getMessage());
125              $this->notification()->error(
126                  __('resolutions.cards.actions.download.notification.error'),
127                  __('resolutions.cards.actions.download.notification.error_message')
128              );
129          }
130      }
131  
132      public function render()
133      {
134          return view('livewire.resolutions.index')->layoutData([
135              'title' => __('resolutions.title'),
136              'entity' => null,
137              'breadcrumb' => 'resolutions.index',
138          ]);
139      }
140  }