Change 1st driver phone call to a text message #2812

This commit is contained in:
Daniel Camargo 2014-04-17 09:29:22 -03:00
parent d1c0f295ef
commit 067c28e328
3 changed files with 78 additions and 34 deletions

View File

@ -56,6 +56,19 @@ class Crunchbutton_Admin extends Cana_Table {
return $phone; return $phone;
} }
public function getTxtNumber(){
if( $this->txt ){
return $this->txt;
}
$notifications = Crunchbutton_Admin_Notification::q( "SELECT * FROM admin_notification WHERE id_admin = {$this->id_admin} AND active = 1" );
foreach( $notifications as $notification ){
if( $notification->type == Crunchbutton_Admin_Notification::TYPE_SMS ){
return $notification->value;
}
}
return false;
}
public function getPhoneNumber(){ public function getPhoneNumber(){
if( $this->phone ){ if( $this->phone ){
return $this->phone; return $this->phone;

View File

@ -109,6 +109,8 @@ class Crunchbutton_Admin_Notification extends Cana_Table {
} }
} }
$driversAlreadyReminded = [];
// Send notification to drivers - legacy // Send notification to drivers - legacy
if( count( $driversToNotify ) > 0 ){ if( count( $driversToNotify ) > 0 ){
foreach( $driversToNotify as $driver ){ foreach( $driversToNotify as $driver ){
@ -120,11 +122,23 @@ class Crunchbutton_Admin_Notification extends Cana_Table {
continue; continue;
} }
foreach( $driver->activeNotifications() as $adminNotification ){ foreach( $driver->activeNotifications() as $adminNotification ){
$adminNotification->send( $order ); // first notification
if( $attempts == 0 ){
$adminNotification->send( $order );
$message = '#'.$order->id_order.' sending notification to ' . $driver->name . ' # ' . $adminNotification->value . ' attempt: ' . $attempts;
Log::debug( [ 'order' => $order->id_order, 'action' => $message, 'type' => 'delivery-driver' ] );
echo $message."\n";
} else {
// next notifications
if( !$driversAlreadyReminded[ $driver->id_admin ] ){
$adminNotification->send( $order );
$message = '#'.$order->id_order.' sending warning notification to ' . $driver->name . ' attempt: ' . $attempts;
Log::debug( [ 'order' => $order->id_order, 'action' => $message, 'type' => 'delivery-driver' ] );
echo $message."\n";
}
}
$driversAlreadyReminded[ $driver->id_admin ] = true;
$hasDriversWorking = true; $hasDriversWorking = true;
$message = '#'.$order->id_order.' sending notification to ' . $driver->name . ' # ' . $adminNotification->value;
Log::debug( [ 'order' => $order->id_order, 'action' => $message, 'type' => 'delivery-driver' ] );
echo $message."\n";
} }
} }
} }
@ -214,8 +228,10 @@ class Crunchbutton_Admin_Notification extends Cana_Table {
$env = c::getEnv(); $env = c::getEnv();
$attempts = Crunchbutton_Admin_Notification_Log::attempts( $order->id_order );
if( $env != 'live' ){ if( $env != 'live' ){
Log::debug( [ 'order' => $order->id_order, 'action' => 'notification to admin at DEV - not sent', 'notification_type' => $this->type, 'value'=> $this->value, 'type' => 'delivery-driver' ]); Log::debug( [ 'order' => $order->id_order, 'action' => 'notification to admin at DEV - not sent', 'notification_type' => $this->type, 'value'=> $this->value, 'attempt' => $attempts, 'type' => 'delivery-driver' ]);
return; return;
} }
@ -231,12 +247,8 @@ class Crunchbutton_Admin_Notification extends Cana_Table {
return; return;
} }
$attempts = Crunchbutton_Admin_Notification_Log::attempts( $order->id_order );
Log::debug( [ 'order' => $order->id_order, 'attempts' => $attempts, 'action' => 'notification to admin starting', 'notification_type' => $this->type, 'value'=> $this->value, 'type' => 'delivery-driver' ]); Log::debug( [ 'order' => $order->id_order, 'attempts' => $attempts, 'action' => 'notification to admin starting', 'notification_type' => $this->type, 'value'=> $this->value, 'type' => 'delivery-driver' ]);
// Send the regular notification just at the first time
// More info: https://github.com/crunchbutton/crunchbutton/issues/2352#issuecomment-34780213
if( $attempts == 0 ){ if( $attempts == 0 ){
switch ( $this->type ) { switch ( $this->type ) {
case Crunchbutton_Admin_Notification::TYPE_FAX : case Crunchbutton_Admin_Notification::TYPE_FAX :
@ -260,31 +272,49 @@ class Crunchbutton_Admin_Notification extends Cana_Table {
break; break;
} }
} else if( $attempts >= 1 ){ } else if( $attempts >= 1 ){
// Send the phone call
$admin = $this->admin(); $admin = $this->admin();
$phoneNumber = $admin->getPhoneNumber(); switch ( $attempts ) {
if( $phoneNumber ){ case 1:
$call = ''; // Change 1st driver phone call to a text message #2812
switch ( $attempts ) { $txtNumber = $admin->getTxtNumber();
case 1: if( $txtNumber ){
$call = 'driver-first-call-warning'; $env = c::getEnv();
break;
case 2: $sms = $txtNumber;
$twilio = new Twilio( c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token );
$message = "Remember: ACCEPT this order cbtn.io/" . $order->id_order . ". Next reminder is a phone call in 3 minutes. Then we'll reach out manually, which is annoying for us ;)";
$message = str_split( $message , 160 );
Log::debug( [ 'order' => $order->id_order, 'action' => 'send second sms to admin', 'num' => $sms, 'message' => join( ' ', $message ), 'type' => 'admin_notification' ]);
foreach ($message as $msg) {
$twilio->account->sms_messages->create(
c::config()->twilio->{$env}->outgoingTextDriver,
'+1'.$sms,
$msg
);
continue;
}
}
break;
case 2:
$phoneNumber = $admin->getPhoneNumber();
if( $phoneNumber ){
$call = 'driver-second-call-warning'; $call = 'driver-second-call-warning';
break; $env = c::getEnv();
} $num = $phoneNumber;
if( $call != '' ){ $url = 'http://'.$this->host_callback().'/api/order/'.$order->id_order.'/'.$call;
$env = c::getEnv(); Log::debug( [ 'order' => $order->id_order, 'action' => 'send call to admin', 'num' => $num, 'host' => $this->host_callback(), 'env' => $env, 'url' => $url, 'type' => 'admin_notification' ]);
$num = $phoneNumber; $twilio = new Services_Twilio(c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token);
$url = 'http://'.$this->host_callback().'/api/order/'.$order->id_order.'/'.$call; $call = $twilio->account->calls->create(
Log::debug( [ 'order' => $order->id_order, 'action' => 'send call to admin', 'num' => $num, 'host' => $this->host_callback(), 'env' => $env, 'url' => $url, 'type' => 'admin_notification' ]); c::config()->twilio->{$env}->outgoingDriver,
$twilio = new Services_Twilio(c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token); '+1'.$num,
$call = $twilio->account->calls->create( $url
c::config()->twilio->{$env}->outgoingDriver, );
'+1'.$num, }
$url break;
);
}
} }
} }
} }

View File

@ -48,11 +48,12 @@ class Crunchbutton_Admin_Notification_Log extends Cana_Table {
} }
if( $attempts == 1 ){ if( $attempts == 1 ){
$description .= ' First phone call'; // Change 1st driver phone call to a text message #2812
$description .= ' Second txt message';
} }
if( $attempts == 2 ){ if( $attempts == 2 ){
$description .= ' Second phone call'; $description .= ' Phone call';
} }
if( $attempts == 3 ){ if( $attempts == 3 ){