diff --git a/include/library/Crunchbutton/Message/Incoming/Customer.php b/include/library/Crunchbutton/Message/Incoming/Customer.php index 32005cdb4..7db26f2ca 100644 --- a/include/library/Crunchbutton/Message/Incoming/Customer.php +++ b/include/library/Crunchbutton/Message/Incoming/Customer.php @@ -146,7 +146,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { 'Restaurant: '.$this->order->restaurant()->name.$community.' / '.$this->order->restaurant()->phone.$notifications; // Notify reps $this->notifyReps($newMessageNotification, $this->support); - Crunchbutton_Message_Incoming_Support::notifyReps($newMessageNotification); + return; } } diff --git a/include/library/Crunchbutton/Support/Action.php b/include/library/Crunchbutton/Support/Action.php index 4ac67d260..5df36000c 100644 --- a/include/library/Crunchbutton/Support/Action.php +++ b/include/library/Crunchbutton/Support/Action.php @@ -32,12 +32,137 @@ class Crunchbutton_Support_Action extends Cana_Table { return $action; } + public function isWaitingResponse(){ + if($this->action == self::ACTION_MESSAGE_RECEIVED || $this->action == self::ACTION_NOTIFICATION_SENT){ + return true; + } + return false; + } + public static function checkStatus(){ - // check here - $actions = self::q('SELECT * FROM support_action save + $actions = self::q('SELECT * FROM support_action sa INNER JOIN( SELECT MAX(id_support_action) id_support_action, id_support FROM support_action GROUP BY id_support) max ON max.id_support_action = sa.id_support_action'); + foreach($actions as $action){ + $action->runVerification(); + } + } + + public function runVerification(){ + if($this->isWaitingResponse()){ + $lastChange = $this->date(); + $now = new DateTime('now', new DateTimeZone(c::config()->timezone)); + $minutes = Util::interval2Hours($lastChange->diff($now)); + + if($minutes >= 15){ + // send notification to cs + if(!self::hasCSNotification($this->id_support)){ + $this->notifyCS(); + } + } else if ($minutes >= 8){ + // sent ticket to drivers + if(!self::hasDriversNotification($this->id_support)){ + $this->notifyDrivers(); + } + } + } + } + + public function getMessage(){ + $id_support = $this->id_support; + $support = Support_Message::q('SELECT * FROM support_message WHERE id_support = ? AND `from` = ? ORDER BY id_support_message DESC LIMIT 1', [$id_support, Support_Message::TYPE_FROM_CLIENT])->get(0); + if($support->body){ + $order = $this->order(); + $message = 'Support ticket @'.$id_support."\n"; + if($order->id_order){ + $message .= 'Last Order: #'.$order->id_order. "\n". + $message .= 'Customer: '.$order->name.' / '.$order->phone.($order->address ? ' / '.$order->address : '')."\n"; + $message .= 'Restaurant: '.$order->restaurant()->name.$community.' / '.$order->restaurant()->phone.$notifications."\n"; + } + $message .= $support->body; + return $message; + } + return null; + } + + public function order(){ + return Order::q('SELECT * FROM `order` WHERE phone=? AND TIMESTAMPDIFF( hour, date, NOW() ) < 24 ORDER BY id_order DESC LIMIT 1',[$this->support()->phone])->get(0); + } + + public function notifyDrivers(){ + $reps = Support::getUsers(); + $order = $this->order(); + $id_community = $order->id_community; + if(!$id_community && $this->support()->phone){ + $community = Crunchbutton_Community::customerCommunityByPhone($this->support()->phone); + if($community->id_community){ + $id_community = $community->id_community; + } + } + + $data = []; + $reps = []; + + if($id_community){ + $community = Community::o($id_community); + $drivers = $community->getWorkingDrivers(); + foreach ($drivers as $driver) { + $reps[$driver->name] = $driver->phone; + $data['reps'][] = ['id_admin' => $driver->id_admin]; + $type = Support_Action::TYPE_NOTIFICATION_SENT_TO_DRIVERS; + } + } + + $type = self::TYPE_NOTIFICATION_SENT_TO_DRIVERS; + $message = $this->getMessage(); + self::create(['id_support' => $this->id_support, + 'action' => self::ACTION_NOTIFICATION_SENT, + 'type' => $type, + 'data' => $data]); + if($reps && count($reps)){ + Message_Sms::send([ + 'to' => $reps, + 'message' => $message, + 'reason' => Message_Sms::REASON_SUPPORT + ]); + } + } + + public function notifyCS(){ + $reps = Support::getUsers(); + $data = ['reps' => $reps]; + $type = self::TYPE_NOTIFICATION_SENT_TO_CS; + $message = $this->getMessage(); + self::create(['id_support' => $this->id_support, + 'action' => self::ACTION_NOTIFICATION_SENT, + 'type' => $type, + 'data' => $data]); + Message_Sms::send([ + 'to' => $reps, + 'message' => $message, + 'reason' => Message_Sms::REASON_SUPPORT + ]); + } + + public static function hasDriverNotification($id_support){ + return self::supportHasActionType($id_support, self::ACTION_NOTIFICATION_SENT, self::TYPE_NOTIFICATION_SENT_TO_DRIVER); + } + + public static function hasDriversNotification($id_support){ + return self::supportHasActionType($id_support, self::ACTION_NOTIFICATION_SENT, self::TYPE_NOTIFICATION_SENT_TO_DRIVERS); + } + + public static function hasCSNotification($id_support){ + return self::supportHasActionType($id_support, self::ACTION_NOTIFICATION_SENT, self::TYPE_NOTIFICATION_SENT_TO_CS); + } + + public static function supportHasActionType($id_support, $action, $type){ + $action = self::q('SELECT * FROM support_action WHERE id_support = ? AND action = ?', [$id_support, $action]); + if($action->id_support){ + return true; + } + return false; } public static function createMessageReceived($id_support, $data){ @@ -47,14 +172,14 @@ class Crunchbutton_Support_Action extends Cana_Table { } public function support() { - if( !$this->_support ){ + if(!$this->_support){ $this->_support = Support::o($this->id_support); } return $this->_support; } public function support_message() { - if( !$this->_support_message ){ + if(!$this->_support_message){ $this->_support_message = Support_Message::o($this->id_support_message); } return $this->_support_message; @@ -76,4 +201,4 @@ class Crunchbutton_Support_Action extends Cana_Table { } return $this->_date; } -} +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Util.php b/include/library/Crunchbutton/Util.php index ae754068e..c97f4293f 100755 --- a/include/library/Crunchbutton/Util.php +++ b/include/library/Crunchbutton/Util.php @@ -243,15 +243,22 @@ class Crunchbutton_Util extends Cana_Model { return $result; } - function interval2Hours( $difference, $accuracy = 2 ) { + public static function interval2Hours( $difference, $accuracy = 2 ) { + $minutes = self::interval2Minutes($difference, $accuracy); + if($minutes){ + return $minutes / 60; + } + return 0; + } + + public static function interval2Minutes( $difference, $accuracy = 2 ) { $seconds = ( $difference->s ) + ( $difference->i * 60 ) + ( $difference->h * 60 * 60 ) + ( $difference->d * 60 * 60 * 24 ) + ( $difference->m * 60 * 60 * 24 * 30 ) + ( $difference->y * 60 * 60 * 24 * 365 ); - $hours = $seconds / 60 / 60; - return floatval( number_format( $hours, 2 ) ); + return intval($seconds / 60); } public static function randomPass( $length = 6 ){