Alexander Musikhin 1 mese fa
parent
commit
ec16247614

+ 7 - 0
app/Http/Controllers/OrderController.php

@@ -11,6 +11,8 @@ use App\Jobs\GenerateFilesPack;
 use App\Jobs\GenerateHandoverPack;
 use App\Jobs\GenerateInstallationPack;
 use App\Jobs\GenerateTtnPack;
+use App\Jobs\NotifyManagerChangeStatusJob;
+use App\Jobs\NotifyManagerNewOrderJob;
 use App\Models\Dictionary\Area;
 use App\Models\Dictionary\District;
 use App\Models\File;
@@ -135,8 +137,12 @@ class OrderController extends Controller
 
         if(isset($data['id'])) {
             $order = Order::query()->where('id', $data['id'])->first();
+            $status = $order->order_status_id;
             $order->update($data);
             $order->refresh();
+            if($order->order_status_id != $status) {
+                NotifyManagerChangeStatusJob::dispatch($order);
+            }
         } else {
             $data['order_status_id'] = Order::STATUS_NEW;
             $order = Order::query()->create($data);
@@ -145,6 +151,7 @@ class OrderController extends Controller
                 . ' (' . $order->area->name . ')'
                 . ' - ' . $order->user->name;
             $order->update(['tg_group_name' => $tg_group_name]);
+            NotifyManagerNewOrderJob::dispatch($order);
         }
 
         // меняем список товаров заказа только если статус новый

+ 18 - 5
app/Http/Controllers/ScheduleController.php

@@ -9,6 +9,8 @@ use App\Http\Requests\CreateScheduleFromReclamationRequest;
 use App\Http\Requests\ExportScheduleRequest;
 use App\Http\Requests\UpdateScheduleRequest;
 use App\Jobs\ExportScheduleJob;
+use App\Jobs\NotifyManagerNewOrderJob;
+use App\Jobs\NotifyOrderInScheduleJob;
 use App\Models\Dictionary\Area;
 use App\Models\Dictionary\District;
 use App\Models\Order;
@@ -99,10 +101,11 @@ class ScheduleController extends Controller
             $mafsText .= $article . ' - ' . $count . "\r\n";
             $mafsCount += $count;
         }
-
+        $first = true;
         for ($iDays = 1; $iDays < $order->install_days + 1; $iDays++) {
+
             $instDate = date('Y-m-d', strtotime('+' . $iDays - 1 . ' days', strtotime($order->installation_date)));
-            Schedule::query()
+            $schedule = Schedule::query()
                 ->create([
                     'order_id' => $validated['order_id'],
                     'address_code' => $validated['order_id'],
@@ -118,6 +121,10 @@ class ScheduleController extends Controller
                     'brigadier_id' => $order->brigadier_id,
                     'comment'   => $validated['comment'],
                 ]);
+            if($first) {
+                $first = false;
+                NotifyOrderInScheduleJob::dispatch($schedule);
+            }
         }
         return redirect()->route('schedule.index');
 
@@ -160,9 +167,10 @@ class ScheduleController extends Controller
             $mafsText .= $article . ' - ' . $count . "\r\n";
         }
 
+        $first = true;
         for ($iDays = 1; $iDays < $reclamation->work_days + 1; $iDays++) {
             $instDate = date('Y-m-d', strtotime('+' . $iDays - 1 . ' days', strtotime($reclamation->start_work_date)));
-            Schedule::query()
+            $schedule = Schedule::query()
                 ->create([
                     'order_id' => $order->id,
                     'address_code' => 'РЕКЛ-' . $validated['reclamation_id'],
@@ -178,6 +186,10 @@ class ScheduleController extends Controller
                     'brigadier_id' => $reclamation->brigadier_id,
                     'comment'   => $reclamation->guarantee,
                 ]);
+            if($first) {
+                $first = false;
+                NotifyOrderInScheduleJob::dispatch($schedule);
+            }
         }
         return redirect()->route('schedule.index');
 
@@ -189,11 +201,11 @@ class ScheduleController extends Controller
         $validated = $request->validated();
 
         if(isset($validated['id'])) {
-            Schedule::query()
+            $schedule = Schedule::query()
                 ->where('id', $validated['id'])
                 ->update($validated);
         } else {
-            Schedule::query()
+            $schedule = Schedule::query()
                 ->create([
                     'address_code' => $validated['address_code'] ?? '-',
                     'installation_date' => $validated['installation_date'],
@@ -208,6 +220,7 @@ class ScheduleController extends Controller
                     'comment'   => $validated['comment'],
                 ]);
         }
+        NotifyOrderInScheduleJob::dispatch($schedule);
 
         return redirect()->back();
     }

+ 32 - 0
app/Jobs/NotifyManagerChangeStatusJob.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Order;
+use App\Notifications\FireBaseNotification;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Queue\Queueable;
+
+class NotifyManagerChangeStatusJob implements ShouldQueue
+{
+    use Queueable;
+
+    /**
+     * Create a new job instance.
+     */
+    public function __construct( private readonly Order $order )
+    {}
+
+    /**
+     * Execute the job.
+     */
+    public function handle(): void
+    {
+        $title = 'Изменён статус площадки';
+        $body = 'Площадка ' . $this->order->common_name . '. Статус: ' . $this->order->orderStatus->name;
+        if(!is_null($this->order->user->token_fcm)) {
+            $this->order->user->notify(new FireBaseNotification($title, $body));
+        }
+
+    }
+}

+ 31 - 0
app/Jobs/NotifyManagerNewOrderJob.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Order;
+use App\Notifications\FireBaseNotification;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Queue\Queueable;
+
+class NotifyManagerNewOrderJob implements ShouldQueue
+{
+    use Queueable;
+
+    /**
+     * Create a new job instance.
+     */
+    public function __construct( private readonly Order $order )
+    {}
+
+    /**
+     * Execute the job.
+     */
+    public function handle(): void
+    {
+        $title = 'Новая площадка!';
+        $body = 'Площадка ' . $this->order->common_name . '. Статус: ' . $this->order->orderStatus->name;
+        if(!is_null($this->order->user->token_fcm)) {
+            $this->order->user->notify(new FireBaseNotification($title, $body));
+        }
+    }
+}

+ 44 - 0
app/Jobs/NotifyOrderInScheduleJob.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Helpers\DateHelper;
+use App\Models\Order;
+use App\Models\Schedule;
+use App\Notifications\FireBaseNotification;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Queue\Queueable;
+
+class NotifyOrderInScheduleJob implements ShouldQueue
+{
+    use Queueable;
+
+    /**
+     * Create a new job instance.
+     */
+    public function __construct(
+        private readonly Schedule $schedule,
+    )
+    {}
+
+    /**
+     * Execute the job.
+     */
+    public function handle(): void
+    {
+        $title = 'График монтажа';
+        $body = 'Адрес ' . $this->schedule->object_address . ' поставлен на монтаж с ' . DateHelper::getHumanDate($this->schedule->installation_date, true);
+
+        if(!is_null($this->schedule->brigadier->token_fcm)) {
+            $this->schedule->brigadier->notify(new FireBaseNotification($title, $body));
+        }
+
+        if(!is_null($this->schedule->order_id)) {
+            $order = Order::query()->where('id', $this->schedule->order_id)->first();
+            if(!is_null($order->user->token_fcm)) {
+                $order->user->notify(new FireBaseNotification($title, $body));
+            }
+
+        }
+    }
+}

+ 7 - 6
app/Notifications/FireBaseNotification.php

@@ -14,10 +14,11 @@ class FireBaseNotification extends Notification
     /**
      * Create a new notification instance.
      */
-    public function __construct()
-    {
-        //
-    }
+    public function __construct(
+        private readonly string $title,
+        private readonly string $body,
+    )
+    {}
 
     /**
      * Get the notification's delivery channels.
@@ -34,8 +35,8 @@ class FireBaseNotification extends Notification
         return [
             'to' => $notifiable->token_fcm,
             'notification' => [
-                'title'    => 'Notification Title',
-                'body'     => 'Notification Body',
+                'title'    => $this->title,
+                'body'     => $this->body,
                 'image'    => config('app.addr') . '/logo.png', // Optional image URL
             ],
         ];