/ app / Models / Revision.php
Revision.php
  1  <?php
  2  
  3  namespace App\Models;
  4  
  5  use App\Http\Services\DocumentService;
  6  use Illuminate\Database\Eloquent\Factories\HasFactory;
  7  use Illuminate\Database\Eloquent\Model;
  8  use Illuminate\Database\Eloquent\Relations\MorphToMany;
  9  use Illuminate\Database\Eloquent\SoftDeletes;
 10  use Illuminate\Support\Facades\Storage;
 11  use Spatie\Activitylog\LogOptions;
 12  use Spatie\Activitylog\Traits\LogsActivity;
 13  
 14  class Revision extends Model
 15  {
 16      use HasFactory, LogsActivity, SoftDeletes;
 17  
 18      protected $guarded = [];
 19      protected $dates = ['reviewed_at', 'approved_at'];
 20      protected $casts = [
 21          'needs_review' => 'boolean',
 22      ];
 23  
 24      public function isInRevision(Revision $otherRevision)
 25      {
 26          $revisionsRelated = $otherRevision->revisionRevision->toArray();
 27  
 28          return in_array($this->id, array_column($revisionsRelated, 'id'));
 29      }
 30  
 31      public function document()
 32      {
 33          return $this->belongsTo(Document::class);
 34      }
 35  
 36      public function previous()
 37      {
 38          return $this->belongsTo(Revision::class, 'previous_id');
 39      }
 40  
 41      public function createdBy()
 42      {
 43          return $this->belongsTo(User::class, 'created_by');
 44      }
 45  
 46      public function approvedBy()
 47      {
 48          return $this->belongsTo(User::class, 'approved_by');
 49      }
 50  
 51      public function reviewedBy()
 52      {
 53          return $this->belongsTo(User::class, 'reviewed_by');
 54      }
 55  
 56      public function related()
 57      {
 58          return $this->morphTo('related');
 59      }
 60  
 61  
 62      public function template()
 63      {
 64          return $this->belongsTo(Template::class);
 65      }
 66  
 67      public function task()
 68      {
 69          return $this->hasMany(Task::class);
 70      }
 71  
 72      public function validations() //los documentos que son INF
 73      {
 74          return $this->hasMany(Validation::class);
 75      }
 76  
 77      public function validationsAsReported() //los documentos que son EXT
 78      {
 79          return $this->hasMany(Validation::class, 'validated_report_id');
 80      }
 81  
 82      public function revisionRevision()
 83      {
 84          return $this->belongsToMany(Revision::class, 'revision_revision', 'revision_id', 'referenced_revision_id');
 85      }
 86  
 87      public function notifications() //los documentos que son NOT
 88      {
 89          return $this->hasMany(Notification::class);
 90      }
 91  
 92      public function notificationsAsReported() //los documentos que son EXT
 93      {
 94          return $this->hasMany(Notification::class, 'notificated_report_id');
 95      }
 96  
 97      public function outputs(): MorphToMany
 98      {
 99          return $this->morphToMany(OutPut::class, 'outputable');
100      }
101  
102      public function makeReviewed(): Revision
103      {
104          $this->reviewed_by = auth()->id();
105          $this->reviewed_at = now();
106          $this->save();
107          return $this;
108      }
109  
110      public function makeApproved(): Revision
111      {
112          $prev = $this->previous;
113  
114          $this->approved_by = auth()->id();
115          $this->approved_at = now();
116          $this->reviewed_at = $prev->reviewed_at;
117          $this->reviewed_by = $prev->reviewed_by;
118          $this->save();
119          return $this;
120      }
121  
122      public function getDownloadNameAttribute(): string
123      {
124          return $this->document->name . '.' . DocumentService::getExtensions($this->path);
125      }
126  
127      public function moveToFilesystem(): string
128      {
129          $to = DocumentService::getRevisionCorrectPath($this);
130  
131          Storage::move($this->path, $to);
132          $this->update(['path' => $to]);
133          return $to;
134      }
135  
136      public function getActivitylogOptions(): LogOptions
137      {
138          return LogOptions::defaults()
139              ->logOnly([
140                  'path',
141                  'name',
142                  'version',
143                  'size',
144                  'previous_id',
145                  'document_id',
146                  'from',
147                  'approved',
148                  'reviewed',
149                  'template_id',
150                  'created_by',
151                  'createdBy.username',
152                  'approved_by',
153                  'approvedBy.username',
154                  'template.name',
155              ])->logOnlyDirty()
156              ->dontSubmitEmptyLogs();
157      }
158  }