소스 검색

modifiche invio sms certificati

ferrari 1 개월 전
부모
커밋
23f05b01aa
3개의 변경된 파일116개의 추가작업 그리고 59개의 파일을 삭제
  1. 66 26
      app/Console/Commands/SendSms.php
  2. 3 0
      app/Console/Kernel.php
  3. 47 33
      routes/web.php

+ 66 - 26
app/Console/Commands/SendSms.php

@@ -3,6 +3,8 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Foundation\Auth\User;
 
 class SendSms extends Command
 {
@@ -27,35 +29,73 @@ class SendSms extends Command
      */
     public function handle()
     {
+        $users = User::whereNotNull('tenant_host')
+            ->whereNotNull('tenant_database')
+            ->whereNotNull('tenant_username')
+            ->whereNotNull('tenant_password')
+            ->get([
+                'id',
+                'tenant_host',
+                'tenant_database',
+                'tenant_username',
+                'tenant_password',
+            ]);
 
-        $expire_date = date("Y-m-d", strtotime("+1 month"));
-        $expire_date_it = date("d/m/Y", strtotime("+1 month"));
-        $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
-        foreach ($certificates as $certificate) {
-            $phone = $certificate->member->phone;
-            $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
-            $params = array(
-                'to'            => '+39' . $phone,
-                'from'          => env('SMS_FROM', 'Test'),
-                'message'       => $message,
-                'format'        => 'json',
-            );
-            sms_send($params);
+        if ($users->isEmpty()) {
+            $this->warn('Nessun utente con info di database trovata.');
+            return Command::SUCCESS;
         }
 
-        $expire_date = date("Y-m-d", strtotime("+15 days"));
-        $expire_date_it = date("d/m/Y", strtotime("+15 days"));
-        $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
-        foreach ($certificates as $certificate) {
-            $phone = $certificate->member->phone;
-            $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
-            $params = array(
-                'to'            => '+39' . $phone,
-                'from'          => env('SMS_FROM', 'Test'),
-                'message'       => $message,
-                'format'        => 'json',
-            );
-            sms_send($params);
+        $tenants = $users->unique(function ($u) {
+            return $u->tenant_host . '|' . $u->tenant_database . '|' . $u->tenant_username;
+        });
+
+        foreach ($tenants as $userTenant) {
+            $this->info("Processo tenant db={$userTenant->tenant_database} (user id={$userTenant->id})");
+
+            app(\App\Http\Middleware\TenantMiddleware::class)->setupTenantConnection($userTenant);
+
+            $expire_date = date("Y-m-d", strtotime("+1 month"));
+            $expire_date_it = date("d/m/Y", strtotime("+1 month"));
+            $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
+            foreach ($certificates as $certificate) {
+                $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
+                if ($new == 0) {
+                    $phone = $certificate->member->phone;
+                    $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo!';
+                    $params = array(
+                        'to'            => '+39' . $phone,
+                        'from'          => env('SMS_FROM', 'Leezard'),
+                        'message'       => $message,
+                        'format'        => 'json',
+                    );
+                    $r = sms_send($params);
+                    Log::info("SMS");
+                    Log::info($r);
+                    sleep(1);
+                }
+            }
+
+            $expire_date = date("Y-m-d", strtotime("+15 days"));
+            $expire_date_it = date("d/m/Y", strtotime("+15 days"));
+            $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
+            foreach ($certificates as $certificate) {
+                $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
+                if ($new == 0) {
+                    $phone = $certificate->member->phone;
+                    $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo!';
+                    $params = array(
+                        'to'            => '+39' . $phone,
+                        'from'          => env('SMS_FROM', 'Leezard'),
+                        'message'       => $message,
+                        'format'        => 'json',
+                    );
+                    $r = sms_send($params);
+                    Log::info("SMS");
+                    Log::info($r);
+                    sleep(1);
+                }
+            }
         }
 
         return Command::SUCCESS;

+ 3 - 0
app/Console/Kernel.php

@@ -17,6 +17,9 @@ class Kernel extends ConsoleKernel
     {
         $schedule->command('password:cleanup')->dailyAt('02:00');
 
+        // invia sms certificati
+        $schedule->command('send:sms')->dailyAt('14:00');
+
         // invia email programmate
         $schedule->command('emails:dispatch-due')->everyMinute();
         // invia sms programmati

+ 47 - 33
routes/web.php

@@ -2456,39 +2456,53 @@ Route::get('/updateCourseCausal', function () {
     }
 });
 
-Route::get('/send_sms', function () {
-
-    $expire_date = date("Y-m-d", strtotime("+1 month"));
-    $expire_date_it = date("d/m/Y", strtotime("+1 month"));
-    $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
-    foreach ($certificates as $certificate) {
-        $phone = $certificate->member->phone;
-        $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
-        $params = array(
-            'to'            => '+39' . $phone,
-            'from'          => env('SMS_FROM', 'Test'),
-            'message'       => $message,
-            'format'        => 'json',
-        );
-        sms_send($params);
-    }
-
-    $expire_date = date("Y-m-d", strtotime("+15 days"));
-    $expire_date_it = date("d/m/Y", strtotime("+15 days"));
-    $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
-    foreach ($certificates as $certificate) {
-        $phone = $certificate->member->phone;
-        $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
-        $params = array(
-            'to'            => '+39' . $phone,
-            'from'          => env('SMS_FROM', 'Test'),
-            'message'       => $message,
-            'format'        => 'json',
-        );
-        sms_send($params);
-    }
-
-});
+// Route::get('/send_sms', function () {
+
+//     $expire_date = date("Y-m-d", strtotime("+1 month"));
+//     $expire_date_it = date("d/m/Y", strtotime("+1 month"));
+//     $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
+//     foreach ($certificates as $certificate) {
+//         $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
+//         if ($new == 0)
+//         {
+//             $phone = $certificate->member->phone;
+//             $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
+//             $params = array(
+//                 'to'            => '+39' . $phone,
+//                 'from'          => env('SMS_FROM', 'Test'),
+//                 'message'       => $message,
+//                 'format'        => 'json',
+//             );
+//             $r = sms_send($params);
+//             Log::info("SMS");
+//             Log::info($r);
+//             sleep(1);
+//         }
+//     }
+
+//     $expire_date = date("Y-m-d", strtotime("+15 days"));
+//     $expire_date_it = date("d/m/Y", strtotime("+15 days"));
+//     $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
+//     foreach ($certificates as $certificate) {
+//         $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
+//         if ($new == 0)
+//         {
+//             $phone = $certificate->member->phone;
+//             $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
+//             $params = array(
+//                 'to'            => '+39' . $phone,
+//                 'from'          => env('SMS_FROM', 'Test'),
+//                 'message'       => $message,
+//                 'format'        => 'json',
+//             );
+//             $r = sms_send($params);
+//             Log::info("SMS");
+//             Log::info($r);
+//             sleep(1);
+//         }
+//     }
+
+// });
 
 Route::get('/test_mail', function(){