| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Validator;
- class Azienda extends Model
- {
- use HasFactory;
- protected $fillable = [
- 'ragione_sociale',
- 'nome_associazione',
- 'tipologia',
- 'logo',
- 'sede_legale_nazione',
- 'sede_legale_provincia',
- 'sede_legale_comune',
- 'sede_legale_indirizzo',
- 'sede_legale_cap',
- 'sede_operativa_nazione',
- 'sede_operativa_provincia',
- 'sede_operativa_comune',
- 'sede_operativa_indirizzo',
- 'sede_operativa_cap',
- 'same_address',
- 'email',
- 'pec',
- 'telefono',
- 'cellulare',
- 'partita_iva',
- 'codice_fiscale',
- 'codice_sdi',
- 'discipline',
- ];
- protected $casts = [
- 'chiusura_anno_fiscale' => 'date',
- 'scadenza_abbonamenti' => 'date',
- 'scadenza_pagamenti_uscita' => 'date',
- ];
- /**
- * Valida i campi richiesti prima della generazione del PDF.
- *
- * @return array|bool true se valido, oppure array di errori se non valido
- */
- public function validate()
- {
- $rules = [
- 'ragione_sociale' => 'required|string|max:255',
- 'email' => 'required|email|max:255',
- 'pec' => 'required|email|max:255',
- 'cellulare' => 'required|string|max:20',
- ];
- $rules_human = [
- 'ragione_sociale' => "Ragione sociale",
- 'email' => "Email",
- 'pec' => "Pec",
- 'cellulare' => "Cellulare",
- ];
- $validator = Validator::make($this->attributesToArray(), $rules);
- if ($validator->fails()) {
- $errors = [];
- foreach ($validator->errors()->messages() as $field => $error) {
- $errors[$field] = isset($rules_human[$field]) ? $rules_human[$field] : $field;
- }
- return $errors;
- }
- return true;
- }
- /**
- * Restituisce true se l’azienda è valida (tutti i campi richiesti presenti).
- */
- public function isValid()
- {
- return $this->validate() === true;
- }
- /**
- * Get the logo URL attribute.
- *
- * @return string|null
- */
- public function getLogoUrlAttribute()
- {
- if ($this->logo) {
- return asset('storage/' . $this->logo);
- }
- return null;
- }
- /**
- * Get a formatted list of discipline names.
- *
- * @return string
- */
- public function getDisciplineListAttribute()
- {
- return $this->disciplines->pluck('name')->implode(', ');
- }
- }
|