SendSms.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Foundation\Auth\User;
  6. class SendSms extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'send:sms';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Send SMS';
  20. /**
  21. * Execute the console command.
  22. *
  23. * @return int
  24. */
  25. public function handle()
  26. {
  27. $users = User::whereNotNull('tenant_host')
  28. ->whereNotNull('tenant_database')
  29. ->whereNotNull('tenant_username')
  30. ->whereNotNull('tenant_password')
  31. ->get([
  32. 'id',
  33. 'tenant_host',
  34. 'tenant_database',
  35. 'tenant_username',
  36. 'tenant_password',
  37. ]);
  38. if ($users->isEmpty()) {
  39. $this->warn('Nessun utente con info di database trovata.');
  40. return Command::SUCCESS;
  41. }
  42. $tenants = $users->unique(function ($u) {
  43. return $u->tenant_host . '|' . $u->tenant_database . '|' . $u->tenant_username;
  44. });
  45. foreach ($tenants as $userTenant) {
  46. $this->info("Processo tenant db={$userTenant->tenant_database} (user id={$userTenant->id})");
  47. app(\App\Http\Middleware\TenantMiddleware::class)->setupTenantConnection($userTenant);
  48. $expire_date = date("Y-m-d", strtotime("+1 month"));
  49. $expire_date_it = date("d/m/Y", strtotime("+1 month"));
  50. $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
  51. foreach ($certificates as $certificate) {
  52. $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
  53. if ($new == 0) {
  54. $phone = $certificate->member->phone;
  55. $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!';
  56. $params = array(
  57. 'to' => '+39' . $phone,
  58. 'from' => env('SMS_FROM', 'Leezard'),
  59. 'message' => $message,
  60. 'format' => 'json',
  61. );
  62. $r = sms_send($params);
  63. Log::info("SMS");
  64. Log::info($r);
  65. sleep(1);
  66. }
  67. }
  68. $expire_date = date("Y-m-d", strtotime("+15 days"));
  69. $expire_date_it = date("d/m/Y", strtotime("+15 days"));
  70. $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
  71. foreach ($certificates as $certificate) {
  72. $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->count();
  73. if ($new == 0) {
  74. $phone = $certificate->member->phone;
  75. $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!';
  76. $params = array(
  77. 'to' => '+39' . $phone,
  78. 'from' => env('SMS_FROM', 'Leezard'),
  79. 'message' => $message,
  80. 'format' => 'json',
  81. );
  82. $r = sms_send($params);
  83. Log::info("SMS");
  84. Log::info($r);
  85. sleep(1);
  86. }
  87. }
  88. }
  89. return Command::SUCCESS;
  90. }
  91. }