/ app / Models / Resolution.php
Resolution.php
  1  <?php
  2  
  3  namespace App\Models;
  4  
  5  use Illuminate\Database\Eloquent\Casts\Attribute;
  6  use Illuminate\Database\Eloquent\Factories\HasFactory;
  7  use Illuminate\Database\Eloquent\Model;
  8  use Spatie\Activitylog\LogOptions;
  9  use Spatie\Activitylog\Traits\LogsActivity;
 10  
 11  class Resolution extends Model
 12  {
 13      use HasFactory, LogsActivity;
 14  
 15      protected $guarded = [];
 16  
 17      protected $casts = [
 18          'approved' => 'boolean',
 19      ];
 20  
 21      /**
 22       * The attributes that should be mutated to dates.
 23       *
 24       * @var array
 25       */
 26      protected $dates = ['resolution_date', 'publication_date', 'certification_date'];
 27  
 28      public static function create(array $data = [])
 29      {
 30          $year = date('Y');
 31  
 32          $last_seq_code = Resolution::where('code_year', $year)->orderByDesc('created_at')->first();
 33          if ($last_seq_code) {
 34              $number = str_pad($last_seq_code->code_seq + 1, 4, '0', STR_PAD_LEFT);
 35          } else {
 36              $number = str_pad(1, 4, '0', STR_PAD_LEFT);
 37          }
 38  
 39          $resolution = static::query()->create([
 40              'dossier_id' => $data['dossier_id'],
 41              'code_year' => $year,
 42              'code_seq' => $number,
 43              'approved' => $data['approved'] ?? null,
 44              'infor_cert_id' => $data['infor_cert_id'] ?? null,
 45              'resolved_doc_id' => $data['resolved_doc_id'] ?? null,
 46              'BOE_code' => $data['BOE_code'] ?? null,
 47              'publication_date' => $data['publication_date'] ?? null,
 48              'certification_date' => $data['certification_date'] ?? null,
 49              'resolution_date' => $data['resolution_date'] ?? null,
 50          ]);
 51  
 52          $resolution->save();
 53  
 54          return $resolution;
 55      }
 56  
 57      public function inforCert()
 58      {
 59          return $this->belongsTo(Revision::class, 'infor_cert_id');
 60      }
 61  
 62      public function resolvedDoc()
 63      {
 64          return $this->belongsTo(Revision::class, 'resolved_doc_id');
 65      }
 66  
 67      public function dossier()
 68      {
 69          return $this->belongsTo(Dossier::class);
 70      }
 71  
 72      public function norms()
 73      {
 74          return $this->belongsToMany(Norm::class, 'norm_resolution', 'resolution_id', 'norm_id');
 75      }
 76  
 77      // Attributes
 78      public function getResolutionNumberAttribute()
 79      {
 80          return $this->code_year . '-' . $this->code_seq;
 81      }
 82  
 83      /**
 84       * PHPWord
 85       * @return Attribute
 86       */
 87      public function getDateYearAttribute()
 88      {
 89          return $this->certification_date->format('Y');
 90      }
 91  
 92      /**
 93       * PHPWord
 94       * @return Attribute
 95       */
 96      public function getDateWithoutYearAttribute()
 97      {
 98          return $this->certification_date->format('d') . '/' .  $this->certification_date->format('m');
 99      }
100  
101      /**
102       * PHPWord
103       * @return Attribute
104       */
105      public function getRecommendedValidityDateAttribute()
106      {
107          $publicationDate = $this->publication_date;
108  
109          if (empty($publicationDate)) {
110              return "N/A";
111          }
112  
113          return $publicationDate->addDay()->addYear(2)->format('d/m/Y');
114      }
115  
116      public function getActivitylogOptions(): LogOptions
117      {
118          return LogOptions::defaults()
119              ->logOnly([
120                  'code_year',
121                  'code_seq',
122                  'approved',
123                  'dossier_id',
124                  'infor_cert_id',
125                  'resolved_doc_id',
126                  'inforCert.name',
127                  'resolvedDoc.name',
128                  'BOE_code',
129              ])->logOnlyDirty()
130              ->dontSubmitEmptyLogs();
131      }
132  }