File: /home/imensosw/liftcincy.imenso.co/app/Console/Commands/OpportunitySignedUpNotify.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Opportunity;
use App\OpportunityVolunteer;
use App\Partner;
use App\User;
use App\Notifications\OpportunitySignedUpNotifyPartner;
use App\Notifications\OpportunityReminderToVolunteer;
use Notification;
use Mail;
use Twilio\Rest\Client;
use Illuminate\Support\Facades\Log;
use App\CountryCode;
class OpportunitySignedUpNotify extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:opportunitysignedup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Log::info('In OpportunitySignedUpNotify - '.date('Y-m-d H:i:s'));
$start_date = date('Y-m-d', strtotime(' +1 day'));
$start_time = date('H:i:s');
// Use Query For partner
$opportunities = Opportunity::whereDate('start_date',$start_date)
->whereTime('start_time','<=', $start_time)
->whereTime('end_time','>=', $start_time)
->where('notification_status',0)
->where('on_going',0)
// ->where('opportunity_type',1) // Comment for both public and private
->get();
// Use Query For Volunteers
$opportunities1 = Opportunity::whereDate('start_date',$start_date)
->whereTime('start_time','<=', $start_time)
->whereTime('end_time','>=', $start_time)
->where('notification_status',0)
->where('on_going',0)->get();
if($opportunities->count()){
foreach ($opportunities as $key => $opportunity) {
$partner = Partner::find($opportunity->partner_id);
$volunteers_signedup = OpportunityVolunteer::where('opportunity_id',$opportunity->id)->whereIn('volunteer_status',[0])->get()->count();
if($volunteers_signedup > 0){
try {
$mailData = [
'partner' => $partner->name,
'opportunity_title' => $opportunity->opportunity_title,
'opportunity_id' => $opportunity->id,
'total_volunteer' => $volunteers_signedup,
];
//echo 1;
$partner->notify(new OpportunitySignedUpNotifyPartner($mailData));
$notifyStatusUpdate = Opportunity::find($opportunity->id);
$notifyStatusUpdate->notification_status = 1;
$notifyStatusUpdate->save();
Log::info('mail send to partner - '.$partner->name.' at '.date('Y-m-d H:i:s'));
} catch (\Exception $e) {
//Log::info('mail send to partner ERROR - ', (array) $e);
}
}else{
//echo 2;
}
}
}
$start_date = date('Y-m-d', strtotime(' +1 day'));
$opportunities = $opportunities1;
if($opportunities->count()){
foreach ($opportunities as $key => $opportunity) {
$partner = Partner::find($opportunity->partner_id);
$volunteers_signedup = OpportunityVolunteer::where('opportunity_id',$opportunity->id)->whereIn('volunteer_status',[0])->get()->count();
if($volunteers_signedup>0){
$volunteers = User::join('opportunity_volunteers','opportunity_volunteers.volunteer_id','=','users.id')->where('opportunity_volunteers.opportunity_id',$opportunity->id)->whereIn('opportunity_volunteers.volunteer_status',[0])->select('users.*')->distinct()->get();
if($volunteers->count()){
$mailData = [
'start_date' => $opportunity->start_date,
'opportunity_title' => $opportunity->opportunity_title,
'start_time' => $opportunity->start_time,
'address_line1' => $opportunity->address_line1,
'address_location' => $opportunity->address_location,
'address_city' => $opportunity->address_city,
'address_state' => $opportunity->address_state,
'address_postcode' => $opportunity->address_postcode,
'sender_name' => 'Concerted',
];
try {
Notification::send($volunteers, (new OpportunityReminderToVolunteer($mailData)));
// Message Send Functionality
$partnerData = User::find($opportunity->partner_id);
$opportunity_start_time = date('h:i A', strtotime($opportunity->start_time));
$nonprofit_partner_name = $partnerData->first_name;
foreach ($volunteers as $key => $volunteer) {
//$volunteer_contact = "+91".$volunteer->org_contact;
$volunteer_name = $volunteer->first_name;
$message = "Hey ".$volunteer_name."! This is your reminder that you have signed up for a volunteer shift with ".$nonprofit_partner_name." tomorrow at ".$opportunity_start_time."! We would love it if you'd text us any photos from your volunteer shift - it helps us motivate folks to get involved in the community! Above all, THANK YOU for serving your community. 💖";
if($volunteer->org_contact!=""){
try {
$this->send_text_message($message, $volunteer);
Log::info('Message (TRIGGER 24 HOURS BEFORE A SCHEDULED VOLUNTEER OPPORTUNITY) send to - '.$volunteer_name.' at '.date('Y-m-d H:i:s'));
} catch (\Exception $e) {
//Log::info('Message (TRIGGER 24 ERROR - ', (array) $e);
}
}
}
Log::info('mail send to Volunteers - '.$volunteers.' at '.date('Y-m-d H:i:s'));
} catch (\Exception $e) {
//Log::info('mail send to Volunteers ERROR - ', (array) $e);
}
}
}
}
}
}
public function send_text_message($message,$volunteer)
{
//try {
$account_sid = \Config::get("app.TWILIO_SID");
$auth_token = \Config::get("app.TWILIO_AUTH_TOKEN");
$twilio_number = \Config::get("app.TWILIO_NUMBER");
$countryCode = \Config::get('app.COUNTRY_CODE');
// $countryCode = $this->getCountryCode($volunteer->address_country);
if($volunteer->org_contact!=null){
$contact_volunteer = $countryCode."".$volunteer->org_contact;
//$contact_volunteer = $volunteer->org_contact;
$client = new Client($account_sid, $auth_token);
$recipients = $contact_volunteer;
$message = $message;
$client->messages->create($recipients, ['from' => $twilio_number, 'body' => $message]);
}
return true;
//} catch (\Exception $e) {
//return false;
//}
return false;
}
}