/ app / Models / Oracle / Expedientes.php
Expedientes.php
  1  <?php
  2  
  3  namespace App\Models\Oracle;
  4  
  5  use Illuminate\Database\Query\JoinClause;
  6  use Illuminate\Support\Collection;
  7  
  8  /**
  9   * App\Models\Oracle\Expedientes
 10   *
 11   * @property int $expcode_year
 12   * @property int $expcode_seq
 13   * @property string $expdate
 14   * @property string $explab
 15   * @property string|null $expdesc
 16   * @property string $expdominio
 17   * @property string $exptipo
 18   * @property string $expsubtipo
 19   * @property string $expestado
 20   * @property string $expmodo
 21   * @property string $expweb
 22   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes newModelQuery()
 23   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes newQuery()
 24   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes query()
 25   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPCODESEQ($value)
 26   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPCODEYEAR($value)
 27   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPDATE($value)
 28   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPDESC($value)
 29   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPDOMINIO($value)
 30   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPESTADO($value)
 31   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPLAB($value)
 32   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPMODO($value)
 33   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPSUBTIPO($value)
 34   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPTIPO($value)
 35   * @method static \Illuminate\Database\Eloquent\Builder|Expedientes whereEXPWEB($value)
 36   * @mixin \Eloquent
 37   */
 38  class Expedientes extends OracleModel
 39  {
 40  
 41      protected $table = 'expedientes';
 42      const MAX_DEPTH = 30;
 43  
 44  
 45      public function getFullCode()
 46      {
 47          return $this->expcode_year . '-' . str_pad($this->expcode_seq, '3', '0', STR_PAD_LEFT);
 48      }
 49  
 50      public function solicitante()
 51      {
 52          return Expsolicitante::where('expsolexp_year', $this->expcode_year)
 53              ->where('expsolexp_seq', $this->expcode_seq)
 54              ->first()
 55              ?->expsolicitante;
 56      }
 57  
 58      public function alcancecert()
 59      {
 60          return $this->hasOne(Alcancecert::class, 'alcexp_year')
 61              ->where('alcexp_seq', $this->expcode_seq)
 62              ->ofMany('alcproducto', 'min');
 63      }
 64  
 65      public function alcancenorma()
 66      {
 67          //we only get the first one
 68          return $this->hasOne(Alcancenormas::class, 'alanexp_year', 'expcode_year')
 69              ->where('alanexp_seq', $this->expcode_seq)
 70              ->where('alanorden', 1) //requested by the client
 71              ->ofMany('alanorden', 'min');
 72      }
 73  
 74      public function related()
 75      {
 76          return $this->hasMany(Rexpedientes::class, 'rexpcode_year', 'expcode_year')
 77              ->where('rexpcode_seq', $this->expcode_seq);
 78      }
 79  
 80  
 81      public function certificadorPrincipal()
 82      {
 83          return $this->hasMany(Expcertificadores::class, 'expccode_year', 'expcode_year')
 84              ->where('expccode_seq', $this->expcode_seq)
 85              ->where('expcerpapel', 'CP')
 86              ->first();
 87      }
 88  
 89      public function certificadoresSecundarios()
 90      {
 91          return $this->hasMany(Expcertificadores::class, 'expccode_year', 'expcode_year')
 92              ->where('expccode_seq', $this->expcode_seq)
 93              ->where('expcerpapel', 'CA');
 94      }
 95  
 96      public function certificadoresExternos()
 97      {
 98          return $this->hasMany(Expcertificadores::class, 'expccode_year', 'expcode_year')
 99              ->where('expccode_seq', $this->expcode_seq)
100              ->where('expcerpapel', 'CE');
101      }
102  
103      /**
104       * Hops through related relationship, until desired result is found or yields nothing
105       * @param string $column
106       * @return Expedientes|void
107       */
108      public function getRelatedWhereNotNUll(string $column): ?Expedientes
109      {
110          $count = self::MAX_DEPTH;
111  
112          do {
113              $count--;
114              $testRelated = $this->related->first();
115              if ($testRelated) {
116                  $testRelated = Expedientes::where('expcode_year', $testRelated->rexpcode_year)
117                      ->where('expcode_seq', $testRelated->rexpcode_seq)
118                      ->first();
119  
120                  $field = $testRelated->{$column};
121  
122                  if ($field) {
123                      if (get_class($field) == Collection::class) {
124                          if ($field->isNotEmpty())
125                              return $testRelated;
126                      } else
127                          return $testRelated;
128                  }
129              }
130          } while ($count && $testRelated);
131  
132          return null;
133      }
134  
135      public function documents()
136      {
137          return Docs::leftJoin('docexp', function (JoinClause $join) {
138              $join->on('doctipo', '=', 'dedoctipo')
139                  ->on('doccode', '=', 'dedoccode');
140          })
141              ->where('docactivo', 1)
142              ->where('deexp_year', $this->expcode_year)
143              ->where('deexp_seq', $this->expcode_seq)
144              ->get();
145      }
146  
147      public function resoluciones()
148      {
149          return Expresoluciones::where('exprescode_year', $this->expcode_year)
150              ->where('exprescode_seq', $this->expcode_seq)
151              ->get();
152      }
153  
154      public function getStageNumber()
155      {
156          $number = Docs::leftJoin('docexp', function (JoinClause $join) {
157              $join->on('doctipo', '=', 'dedoctipo')
158                  ->on('doccode', '=', 'dedoccode');
159          })
160              ->where('deexp_year', $this->expcode_year)
161              ->where('deexp_seq', $this->expcode_seq)
162              ->max('defase');
163  
164          return match ($number) {
165              '0' => 1,
166              '1' => 1,
167              '2' => 2,
168              '3' => 2,
169              '4' => 3,
170              '5' => 4,
171              '6' => 5,
172              '7' => 6,
173              '8' => 8,
174              '9' => 9,
175              '12' => 12,
176              default => 12
177          };
178      }
179  }