From: Louis Jeckel Date: Wed, 28 Jul 2021 21:45:27 +0000 (+0200) Subject: unique addresse for organsiations X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=5c666761422b8b4ae98ce404f74f6b660623092c;p=psq.git unique addresse for organsiations --- diff --git a/app/HasPlace.php b/app/HasPlace.php index 91a9d4d..450aab9 100644 --- a/app/HasPlace.php +++ b/app/HasPlace.php @@ -16,10 +16,10 @@ trait HasPlace public function place(): array { return [ - $this->address_line_1, - $this->city, - $this->postal_code, - $this->country, + 'address_line_1' => $this->address_line_1, + 'city' => $this->city, + 'postal_code' => $this->postal_code, + 'country' => $this->country, ]; } } diff --git a/app/Helpers/Helpers.php b/app/Helpers/Helpers.php index da3be1f..315c1a4 100644 --- a/app/Helpers/Helpers.php +++ b/app/Helpers/Helpers.php @@ -26,4 +26,17 @@ class Helpers return $line; } + + /** + * @param array $place + * @return string + */ + public static function placeArrayToString(array $place): string + { + if(empty($place['address_line_1'])) { + return ""; + } + return "{$place['address_line_1']}, {$place['postal_code']} {$place['city']} {$place['country']}"; + } + } diff --git a/app/Nova/Actions/ExportUsersToExcel.php b/app/Nova/Actions/ExportUsersToExcel.php index 07d74b7..597cae1 100644 --- a/app/Nova/Actions/ExportUsersToExcel.php +++ b/app/Nova/Actions/ExportUsersToExcel.php @@ -27,15 +27,12 @@ class ExportUsersToExcel extends DownloadExcel implements WithMapping, WithHeadi */ public function map($user): array { - $place = $user->organization !== null && $user->organization->unique_address ? - $user->organization->place() : - $user->place(); return array_merge([ $user->first_name, $user->last_name, $user->email - ], $place, [ + ], array_values($user->computed_place), [ $user->phone, $user->organization_str ?? (string) $user->organization, $user->position, diff --git a/app/Nova/User.php b/app/Nova/User.php index 49b7336..974ee7f 100644 --- a/app/Nova/User.php +++ b/app/Nova/User.php @@ -92,8 +92,9 @@ class User extends Resource Text::make('Position'), Text::make('Service'), Text::make('Téléphone', 'phone'), - - new Panel('Adresse', $this->addressFields()), + Text::make('Adresse expédition', 'computedFullAddress')->readonly()->hideWhenCreating()->hideWhenUpdating(), + Boolean::make('Adresse héritée orga', 'inheritsAddress')->readonly()->hideWhenCreating()->hideWhenUpdating(), + new Panel('Adresse utilisateur', $this->addressFields()), new Panel('Réglages', [ Select::make('Type')->options([ AppUser::TYPE_SUBSCRIBER => 'Abonné', diff --git a/app/User.php b/app/User.php index 5d8b9dc..139fdee 100644 --- a/app/User.php +++ b/app/User.php @@ -3,7 +3,7 @@ namespace App; -use App\Notifications\EmailValidated; +use App\Helpers\Helpers; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -35,6 +35,8 @@ use Laravel\Scout\Searchable; * @property bool $unsubscribed * @property bool $reg_complete * @property bool $receives_pdf + * @property array $computed_place + * @property boolean $inherits_address * * @property string $status */ @@ -652,10 +654,28 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails public function getFullAddressAttribute(): string { - if(empty($this->address_line_1)) { - return ""; - } - return "$this->address_line_1, $this->postal_code $this->city $this->country"; + return Helpers::placeArrayToString($this->place()); + } + + public function getInheritsAddressAttribute(): bool + { + return $this->organization !== null && $this->organization->unique_address; + } + + /** + * @return array + * Returns either user or organization place array + */ + public function getComputedPlaceAttribute(): array + { + return $this->inherits_address ? + $this->organization->place() : + $this->place(); + } + + public function getComputedFullAddressAttribute(): string + { + return Helpers::placeArrayToString($this->getComputedPlaceAttribute()); } /**