/ app / Http / Livewire / InboxFiles / Validate.php
Validate.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\InboxFiles;
  4  
  5  use App\Enums\ValidationRequirementResponseTypeEnum;
  6  use App\Enums\ValidationRequirementResponseTypeEnum as TypeEnum;
  7  use App\Http\Services\InboxFileService;
  8  use App\Models\ValidationRequirement;
  9  use App\Models\ValidationRequirementResponse;
 10  use Exception;
 11  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 12  use Illuminate\Support\Collection;
 13  use Livewire\Component;
 14  use WireUi\Traits\Actions;
 15  
 16  
 17  class Validate extends Component
 18  {
 19      use AuthorizesRequests, Actions;
 20  
 21      public int $entityId;
 22      public ValidationRequirement $requirement;
 23      public $oldNonConformities = [];
 24      public $responses = [];
 25      public $inboxFile;
 26      public $lines2 = [];
 27      public $lines = [];
 28      public $toggles = [];
 29      public $heads = [];
 30  
 31      public $newResponses = [];
 32      public $newResponse = [
 33          'type' => null,
 34          'description' => null,
 35          'pass' => false,
 36      ];
 37  
 38      protected function getListeners()
 39      {
 40          return [
 41              'refresh' => '$refresh',
 42              'changedRequirement' => 'changedRequirement'
 43          ];
 44      }
 45  
 46      public function changedRequirement($_, $rowId)
 47      {
 48          $this->entityId = $rowId;
 49          $this->requirement = ValidationRequirement::findOrFail($this->entityId);
 50          $this->inboxFile = $this->requirement->validation->inboxFile ?? $this->requirement->validation->jsonDraft;
 51          $this->oldNonConformities = $this->getPreviousNonConformitiesgetProperty()->toArray();
 52          $this->loadResponses();
 53      }
 54  
 55      public function mount()
 56      {
 57          $this->requirement = new ValidationRequirement();
 58      }
 59  
 60      private function getPreviousNonConformitiesgetProperty()
 61      {
 62          return InboxFileService::getPreviousNonConformities($this->inboxFile);
 63      }
 64  
 65      public function getActivitiesProperty()
 66      {
 67          return ($this->requirement->responses()->withTrashed()->get()->map(function ($response) {
 68              return $response->activities;
 69          }))->flatten();
 70      }
 71  
 72      public function getResponseArrayProperty()
 73      {
 74          return $this->responses;
 75      }
 76  
 77      public function openNonConformities()
 78      {
 79          $this->emit('openPanel', __('inbox-files.side-panel.old-nonconformities-title'), 'inbox-files.non-conformities', $this->inboxFile->id);
 80      }
 81  
 82      public function render()
 83      {
 84          return view('livewire.inbox-files.validate');
 85      }
 86  
 87      private function loadResponses(): void
 88      {
 89          $this->heads = $this->requirement->responses()->where('parent_id', null)->get();
 90  
 91          foreach ($this->heads as $head) {
 92              if (isset($this->newResponses['p' . $head->id])) {
 93                  continue;
 94              } else {
 95                  $this->newResponses['p' . $head->id] = [
 96                      'type' => null,
 97                      'description' => null,
 98                      'pass' => false,
 99                  ];
100              }
101          }
102      }
103  
104      public function save()
105      {
106          foreach ($this->responses as $key => $response) {
107              if (array_key_exists('id', $response)) {
108                  ValidationRequirementResponse::find($response['id'])->update([
109                      'description' => $response['description'],
110                      'closure_date' => $response['pass'] ? now() : null,
111                  ]);
112              } else {
113                  $response['closure_date'] = $response['pass'] ? now() : null;
114                  $res = ValidationRequirementResponse::create($response);
115                  $this->responses[$key]['id'] = $res->id;
116              }
117          }
118  
119          $this->requirement->checkStatus(); // update the status of the requirement
120          $this->requirement->validation->checkStatus(); // update the status of the validation
121  
122          // UI Trick for delaying spinner
123          if (config('app.env') !== 'testing') {
124              sleep(1);
125          }
126  
127          $this->emitUp('closePanel');
128          $this->emitTo('inbox-files.view', 'refresh');
129  
130          $this->notification()->success(
131              __('dossiers.template.validation.requirement.notifications.create.success.title'),
132              __('dossiers.template.validation.requirement.notifications.create.success.message')
133          );
134  
135          $this->lines = $this->requirement->responses()->where('parent_id', null)->get();
136      }
137  
138      public function trash(string $key)
139      {
140          $this->notification()->confirm([
141              'title' => __('panel.labels.remove.title'),
142              'description' => __('panel.labels.remove.subtitle'),
143              'icon' => 'question',
144              'accept' => [
145                  'label' => 'Yes, Delete',
146                  'method' => 'delete',
147                  'params' => $key,
148              ],
149              'reject' => [
150                  'label' => 'Cancelar',
151              ],
152          ]);
153      }
154  
155      public function delete(string $key)
156      {
157          try {
158              $response = $this->responses[$key];
159  
160              validationrequirementresponse::findorfail($response['id'])->delete();
161  
162              unset($this->responses[$key]);
163  
164              $this->emitto('inbox-files.view', 'refresh');
165  
166              $this->notification()->success(
167                  __('dossiers.template.validation.requirement.notifications.remove.success.title'),
168                  __('dossiers.template.validation.requirement.notifications.remove.success.message')
169              );
170          } catch (exception $e) {
171              logger($e->getmessage());
172              $this->notification()->error(
173                  'error',
174                  $e->getmessage()
175              );
176          }
177      }
178  
179      public function updating($name, $value)
180      {
181          if ($name == 'newResponse.type') {
182              $this->newResponse['pass'] = match ($value) {
183                  'COM' => true,
184                  default => false,
185              };
186          }
187      }
188  
189      public function createNewHead($id = null)
190      {
191          $this->validate([
192              'newResponse.type' => 'required|string|max:255', // 'COM' or 'NC' or 'OBS
193              'newResponse.description' => 'required|string|max:255',
194              'newResponse.pass' => 'required|boolean',
195          ]);
196  
197          $type = TypeEnum::acronym2id($this->newResponse['type']);
198  
199          $new = new ValidationRequirementResponse();
200          $new->description = $this->newResponse['description'];
201          $new->closure_date = $this->newResponse['pass'] ? now() : null;
202          $new->parent_id = null;
203          $new->validation_requirement_response_type_id = $type;
204          $new->validation_requirement_id = $this->requirement->id;
205          $new->save();
206  
207          $this->requirement->checkStatus(); // update the status of the requirement
208          $this->requirement->validation->checkStatus(); // update the status of the requirement
209          $this->reset('newResponse');
210          $this->changedRequirement('', $this->requirement->id);
211          $this->emitUp('refresh');
212          $this->notification()->success(TypeEnum::id2acronym($type) . ' ' . __('inbox-files.validate.created'));
213          $this->emitTo(View::class, 'forgetRequirements');
214  
215          $this->dispatchBrowserEvent('reset');
216      }
217  
218      public function create($id = null, $headId = null)
219      {
220          $this->validate([
221              'newResponses.p' . $headId . '.description' => 'required|string|max:255',
222              'newResponses.p' . $headId . '.pass' => 'required|boolean',
223          ]);
224  
225          $type = ValidationRequirementResponse::find($id)->validation_requirement_response_type_id;
226  
227          $new = new ValidationRequirementResponse();
228          $new->description = $this->newResponses['p' . $headId]['description'];
229          $new->closure_date = ($type == ValidationRequirementResponseTypeEnum::comment()->value || $this->newResponses['p' . $headId]['pass']) ? now() : null;
230          $new->parent_id = $id;
231          $new->validation_requirement_response_type_id = $type;
232          $new->validation_requirement_id = $this->requirement->id;
233          $new->save();
234  
235          $this->loadResponses();
236          $this->requirement->checkStatus(); // update the status of the requirement
237          $this->requirement->validation->checkStatus(); // update the status of the requirement
238          $this->notification()->success(TypeEnum::id2acronym($type) . ' ' . __('inbox-files.validate.created'));
239          $this->emitTo(View::class, 'forgetRequirements');
240          $this->dispatchBrowserEvent('reset');
241      }
242  }