From 84c860e9dd6fd3ad28b24653b2fe738e91dfc5ef Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 24 Apr 2024 19:00:27 +0200 Subject: [PATCH] wip #6883 @3 --- .env.production | 2 +- app/Http/Controllers/FluidbookController.php | 13 ++- app/Models/Client.php | 12 +++ app/Notifications/ResahNotification.php | 92 +++++++++++++++++++ .../views/vendor/mail/html/message.blade.php | 27 ++++++ 5 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 app/Notifications/ResahNotification.php create mode 100644 resources/views/vendor/mail/html/message.blade.php diff --git a/.env.production b/.env.production index 22e8d4f..f23217f 100644 --- a/.env.production +++ b/.env.production @@ -1,4 +1,4 @@ -APP_NAME=bastide-resah +APP_NAME=Bastide-resah.fr APP_ENV=production APP_KEY=base64:OpYzvRTnIiViilkl5ngdoPNJ6UQeQUB8WBKhR4XD/UY= APP_DEBUG=true diff --git a/app/Http/Controllers/FluidbookController.php b/app/Http/Controllers/FluidbookController.php index 7fe0735..f80ff64 100644 --- a/app/Http/Controllers/FluidbookController.php +++ b/app/Http/Controllers/FluidbookController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use App\Models\Client; use App\Models\Order; +use App\Models\User; +use App\Notifications\ResahNotification; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; @@ -51,6 +53,8 @@ class FluidbookController extends Controller $client->enabled = false; $client->save(); + User::withoutGlobalScopes()->find(3)->notify(new ResahNotification(ResahNotification::ACCOUNT_CREATED, $client)); + return response()->json(['success' => 'ok'])->setStatusCode(200); } @@ -128,13 +132,18 @@ class FluidbookController extends Controller . 'ECOTAXE : ' . self::formatNumber($cumul_ecotaxe) . "\n" . 'TOTAL TTC : ' . self::formatNumber($total + $cumul_ecotaxe + $cumul_tva); + /** @var Client $user */ + $user = auth()->guard('client')->user(); $order = new Order(); - $order->client = auth()->guard('client')->user()->id; + $order->client = $user->id; $order->details = implode("\n\n----\n\n", $details); $order->quantity = $quantity; $order->total_ht = $total; - $order->save(); + + $user->notify(new ResahNotification(ResahNotification::QUOTE_REQUEST_SENT, $order)); + User::withoutGlobalScopes()->find(3)->notify(new ResahNotification(ResahNotification::QUOTE_REQUEST, $order)); + } protected static function formatNumber($n, $suffix = ' €') diff --git a/app/Models/Client.php b/app/Models/Client.php index 1950a98..d92e800 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -4,12 +4,16 @@ namespace App\Models; use App\Http\Controllers\Operations\Client\Fluidbook; use App\Http\Controllers\Operations\Client\Landing; +use App\Notifications\ResahNotification; use Cubist\Backpack\Magic\Fields\Text; use Cubist\Backpack\Magic\Models\CubistMagicAuthenticatable; +use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Password; class Client extends CubistMagicAuthenticatable { + use Notifiable; + protected $table = 'client'; protected $_options = ['name' => 'client', 'singular' => 'client', @@ -31,4 +35,12 @@ class Client extends CubistMagicAuthenticatable $this->addField('function', Text::class, 'Fonction', ['tab' => 'Login']); $this->addField('phone', Text::class, 'Numéro de téléphone', ['tab' => 'Login']); } + + public function onSaving(): bool + { + if ($this->isDirty('enabled') && $this->enabled) { + $this->notify(new ResahNotification(ResahNotification::ACCOUNT_VALIDATED, null)); + } + return parent::onSaving(); + } } diff --git a/app/Notifications/ResahNotification.php b/app/Notifications/ResahNotification.php new file mode 100644 index 0000000..a116ce6 --- /dev/null +++ b/app/Notifications/ResahNotification.php @@ -0,0 +1,92 @@ +type = $type; + $this->data = $data; + } + + public function via($notifiable) + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $greetings = 'Bonjour,'; + $salutation = 'Cordialement,

L\'équipe Bastide-resah.fr'; + $subjectPrefix = ''; + if ($notifiable instanceof User) { + $greetings = ''; + $salutation = 'Message envoyé automatiquement depuis le site Bastide-resah.fr'; + $subjectPrefix = '[Bastide-resah.fr] '; + } + + if ($this->type === self::ACCOUNT_CREATED) { + $url = backpack_url('/client/' . $this->data->id . '/edit'); + $subject = 'Nouveau compte en attente de validation'; + $html = 'Un nouveau compte client a été créé.
Pour le valider, rendez-vous sur ' . $url . ''; + } else if ($this->type === self::ACCOUNT_VALIDATED) { + $subject = 'Votre compte Bastide-resah.fr a été validé'; + $url = url('/'); + $html = 'Votre compte client Bastide-resah.fr a été validé.

Pous pouvez dès à présent vous connecter sur ' . $url . ' pour consulter le catalogue interactif complet avec les tarifs. Vous pourrez y sélectionner vos produits, déterminer votre budget en instantané, et recevoir un devis par le Resah dans les 72h.'; + } else if ($this->type === self::QUOTE_REQUEST) { + $subject = 'Demande de devis'; + $url = backpack_url('/order/' . $this->data->id . '/edit'); + $html = 'Une nouvelle demande de devis a été envoyée.
Pour la visualiser, rendez-vous sur ' . $url . ''; + //$file = storage_path('orders/' . $this->data->id . '.pdf'); + } else if ($this->type === self::QUOTE_REQUEST_SENT) { + $subject = 'Votre demande de devis sur Bastide-resah.fr'; + $html = 'Votre demande de devis est en cours de traitement (récapitulatif en pièce jointe).
Nous vous recontacterons dans les meilleurs délais.'; + //$file = storage_path('orders/' . $this->data->id . '.pdf'); + } + + + $m = (new MailMessage); + $m->subject($subjectPrefix . $subject); + $m->greeting($greetings); + if ($html) { + $m->line(new HtmlString($html)); + } + if ($salutation) { + $m->salutation(new HtmlString($salutation)); + } + if (isset($file) && file_exists($file)) { + $m->attach($file); + } + + return $m; + + } +} diff --git a/resources/views/vendor/mail/html/message.blade.php b/resources/views/vendor/mail/html/message.blade.php new file mode 100644 index 0000000..986fa24 --- /dev/null +++ b/resources/views/vendor/mail/html/message.blade.php @@ -0,0 +1,27 @@ + + {{-- Header --}} + + + {{ config('app.name') }} + + + + {{-- Body --}} + {{ $slot }} + + {{-- Subcopy --}} + @isset($subcopy) + + + {{ $subcopy }} + + + @endisset + + {{-- Footer --}} + + + © {{ date('Y') }} {{ config('app.name') }}. + + + -- 2.39.5