Daniel Camargo 1f5b36ee15 Driver onboarding #2969
1) When creating an account as a driver, it should automatically add notifications using their phone number (assume they have a smart phone) and email
2) It should text them their username
3) I never received an email to nickhunter1991@gmail.com
4) When I accessed order 16844, I couldn't see the customer price total or subtotal
2014-05-27 18:59:11 -03:00

115 lines
3.2 KiB
PHP

<?php
class Controller_api_driver_save extends Crunchbutton_Controller_RestAccount {
public function init() {
$id_admin = c::getPagePiece( 3 );
$user = c::user();
$hasPermission = ( c::admin()->permission()->check( ['global', 'drivers-all'] ) || ( $id_admin == $user->id_admin ) );
if( !$hasPermission ){
$this->_error();
exit;
}
if( $this->method() != 'post' ){
$this->_error();
}
$newDriver = false;
// saves a new driver
if( !$id_admin ){
$newDriver = true;
$driver = new Crunchbutton_Admin();
// create the new driver as inactive
$driver->active = 0;
} else {
$driver = Crunchbutton_Admin::o( $id_admin );
}
$driver->name = $this->request()[ 'name' ];
$driver->phone = preg_replace( '/[^0-9]/i', '', $this->request()[ 'phone' ] );
$driver->email = $this->request()[ 'email' ];
$pass = $this->request()[ 'pass' ];
if( $pass && trim( $pass ) != '' ){
$driver->pass = $driver->makePass( $pass );
}
$driver->save();
if( !$driver->login ){
// create an username
$driver->login = $driver->createLogin();
$driver->save();
}
// add the community
$id_community = $this->request()[ 'id_community' ];
// first remove the driver from the delivery groups
$_communities = Crunchbutton_Community::q( 'SELECT * FROM community ORDER BY name ASC' );;
foreach( $_communities as $community ){
$group = $community->groupOfDrivers();
if( $group->id_group ){
$driver->removeGroup( $group->id_group );
}
}
if( $id_community ){
$community = Crunchbutton_Community::o( $id_community );
$driver->timezone = $community->timezone;
$driver->save();
if( $community->id_community ){
$group = $community->groupOfDrivers();
$adminGroup = new Crunchbutton_Admin_Group();
$adminGroup->id_admin = $driver->id_admin;
$adminGroup->id_group = $group->id_group;
$adminGroup->save();
}
}
if( $newDriver ){
// Create phone notification
if( $driver->phone ){
$notification = new Crunchbutton_Admin_Notification();
$notification->value = $driver->phone;
$notification->type = Crunchbutton_Admin_Notification::TYPE_SMS;
$notification->active = 1;
$notification->id_admin = $driver->id_admin;
$notification->save();
}
// Create email notification
if( $driver->email ){
$notification = new Crunchbutton_Admin_Notification();
$notification->value = $driver->email;
$notification->type = Crunchbutton_Admin_Notification::TYPE_EMAIL;
$notification->active = 1;
$notification->id_admin = $driver->id_admin;
$notification->save();
}
}
Log::debug( [ 'action' => 'driver saved', 'driver' => $driver->id_admin, 'type' => 'drivers-onboarding'] );
$log = new Cockpit_Driver_Log();
$log->id_admin = $driver->id_admin;
$log->action = ( $newDriver ) ? Cockpit_Driver_Log::ACTION_CREATED_COCKIPT : Cockpit_Driver_Log::ACTION_UPDATED_COCKIPT;
$log->datetime = date('Y-m-d H:i:s');
$log->save();
if ( $this->request()[ 'notify' ] ) {
Cockpit_Driver_Notify::send( $driver->id_admin, Cockpit_Driver_Notify::TYPE_WELCOME );
}
echo json_encode( [ 'success' => $driver->exports() ] );
return;
}
private function _error( $error = 'invalid request' ){
echo json_encode( [ 'error' => $error ] );
exit();
}
}