public function delete($user_id)
 {
     $tickets = Tickets::where('customer_id', $user_id)->get();
     //Delete tickets
     foreach ($tickets as $ticket) {
         TicketAttachments::where('thread_id', $ticket->id)->delete();
         MessageThread::where('id', $ticket->thread_id)->delete();
         ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
     }
     Tickets::where('customer_id', $user_id)->delete();
     //Delete Chat and Conversations
     $online_users = OnlineUsers::where('user_id', $user_id)->get();
     foreach ($online_users as $online_user) {
         MessageThread::where('id', $online_user->thread_id)->delete();
         ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
     }
     OnlineUsers::where('user_id', $user_id)->delete();
     $closed_conversations = ClosedConversations::where('user_id', $user_id)->get();
     foreach ($closed_conversations as $closed_conversation) {
         MessageThread::where('id', $closed_conversation->thread_id)->delete();
         ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
     }
     ClosedConversations::where('user_id', $user_id)->delete();
     UsersGroups::where('user_id', $user_id)->delete();
     CompanyCustomers::where("customer_id", $user_id)->delete();
     User::where("id", $user_id)->delete();
     RecentActivities::createActivity("Customer deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.customer_deleted_success'));
     return Redirect::to('/customers/all');
 }
 public function delete($company_id)
 {
     $departments = Department::where('company_id', $company_id)->get();
     if (Config::get('site-config.is_demo') && $company_id == 1) {
         Session::flash('error_msg', 'Demo : Feature is disabled');
         return Redirect::to('/dashboard');
     }
     foreach ($departments as $department) {
         if (!empty($department)) {
             $tickets = Tickets::where('department_id', $department->id)->get();
             //Delete tickets
             foreach ($tickets as $ticket) {
                 TicketAttachments::where('thread_id', $ticket->id)->delete();
                 MessageThread::where('id', $ticket->thread_id)->delete();
                 ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
             }
             Tickets::where('department_id', $department->id)->delete();
             //Delete Chat and Conversations
             $online_users = OnlineUsers::where('department_id', $department->id)->get();
             foreach ($online_users as $online_user) {
                 MessageThread::where('id', $online_user->thread_id)->delete();
                 ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
             }
             OnlineUsers::where('department_id', $department->id)->delete();
             $closed_conversations = ClosedConversations::where('department_id', $department->id)->get();
             foreach ($closed_conversations as $closed_conversation) {
                 MessageThread::where('id', $closed_conversation->thread_id)->delete();
                 ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
             }
             ClosedConversations::where('department_id', $department->id)->delete();
             $operators = OperatorsDepartment::where('department_id', $department->id)->lists('user_id');
             if (sizeof($operators) > 0) {
                 User::whereIn('id', $operators)->delete();
                 UsersGroups::whereIn('user_id', $operators)->delete();
             }
             OperatorsDepartment::where('department_id', $department->id)->delete();
             $department_admin = DepartmentAdmins::where('department_id', $department->id)->first();
             if (!empty($department_admin)) {
                 UsersGroups::where('user_id', $department_admin->user_id)->delete();
                 User::where("id", $department_admin->user_id)->delete();
                 CompanyDepartmentAdmins::where("user_id", $department_admin->user_id)->delete();
                 CannedMessages::where('operator_id', $operators)->delete();
             }
         }
         DepartmentAdmins::where('department_id', $department->id)->delete();
         Department::where('id', $department->id)->delete();
     }
     $company = Company::where('id', $company_id)->first();
     RecentActivities::createActivity("Company <a href='/companies/all'>" . $company->name . "</a> deleted by User Name " . Auth::user()->name . " User ID " . Auth::user()->id);
     Company::where('id', $company_id)->delete();
     Session::flash('success_msg', trans('msgs.company_deleted_success'));
     return Redirect::to('/companies/all');
 }
 public function deleteConversation($thread_id)
 {
     ThreadMessages::where('thread_id', $thread_id)->delete();
     MessageThread::where('id', $thread_id)->delete();
     ThreadGeoInfo::where('thread_id', $thread_id)->delete();
     ClosedConversations::where('thread_id', $thread_id)->delete();
     RecentActivities::createActivity("Conversation deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.conversation_deleted_success'));
     return Redirect::to('/conversations/closed');
 }
 public function delete($admin_id)
 {
     $department_admin = DepartmentAdmins::where("user_id", $admin_id)->first();
     if (Config::get('site-config.is_demo') && $admin_id == 2) {
         Session::flash('error_msg', 'Demo : Feature is disabled');
         return Redirect::to('/dashboard');
     }
     if (!empty($department_admin)) {
         $department = Department::where('id', $department_admin->department_id)->first();
         if (!empty($department)) {
             $company = Company::where('id', $department->company_id)->first();
             //Change all conversations , tickets , threads , thread_messages operator_id
             OnlineUsers::where('operator_id', $admin_id)->update(['operator_id' => $company->user_id]);
             ClosedConversations::where('operator_id', $admin_id)->update(['operator_id' => $company->user_id]);
             MessageThread::where('operator_id', $admin_id)->update(['operator_id' => $company->user_id]);
             Tickets::where('operator_id', $admin_id)->update(['operator_id' => $company->user_id]);
             ThreadMessages::where('sender_id', $admin_id)->update(['sender_id' => $company->user_id]);
         }
     }
     CompanyDepartmentAdmins::where("user_id", $admin_id)->delete();
     DepartmentAdmins::where('user_id', $admin_id)->delete();
     UsersGroups::where('user_id', $admin_id)->delete();
     User::where('id', $admin_id)->delete();
     RecentActivities::createActivity("Department admin deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.department_admin_deleted_success'));
     return Redirect::to('/departments/admins/all');
 }
 public function checkNewMessages()
 {
     $v_data = ["user_id" => Input::get('user_id'), "thread_id" => Input::get('thread_id'), "company_id" => Input::get('company_id'), "last_message_id" => Input::get('last_message_id')];
     $v_rules = ["user_id" => 'required', "thread_id" => 'required', "company_id" => 'required', "last_message_id" => 'required'];
     //Check any operators online from company_id
     //Check if we have any messages from user_id , thread_id
     $v = Validator::make($v_data, $v_rules);
     $response['is_online'] = false;
     if ($v->passes()) {
         //check any operator online
         $response['is_online'] = Company::operatorsOnline(Input::get('company_id'));
         $response['success_msg'] = "";
         $company = Company::find(Input::get('company_id'));
         $response['departments'] = Department::where('company_id', Input::get('company_id'))->get();
         foreach ($response['departments'] as $department) {
             $department_admin = DepartmentAdmins::where('department_id', $department->id)->first();
             $status = trans('msgs._offline_');
             $admin = User::where('id', $company->user_id)->first();
             if (!empty($admin)) {
                 if ($admin->is_online == 1) {
                     $status = trans('msgs._online_');
                 }
             }
             if (!empty($department_admin)) {
                 $user = User::where('id', $department_admin->user_id)->first();
                 if (!empty($user) && $user->is_online == 1) {
                     $status = trans('msgs._online_');
                 }
             }
             $operators = OperatorsDepartment::where('department_id', $department->id)->get();
             foreach ($operators as $operator) {
                 if (sizeof(User::where('id', $operator->user_id)->get()) > 0) {
                     $user = User::find($operator->user_id);
                     if ($user->is_online == 1) {
                         $status = trans('msgs._online_');
                     }
                 }
             }
             $department->name = $department->name . $status;
         }
         if (Input::has('token') && sizeof(OnlineUsers::where('token', Input::get('token'))->get()) > 0) {
             $token = Input::get('token');
             $response['token'] = $token;
             $online_user = OnlineUsers::where('token', $token)->first();
             $response['in_conversation'] = 1;
             $response['conversation_closed'] = 0;
             $response['thread_id'] = $online_user->thread_id;
             $thread_geo_info = ThreadGeoInfo::where('thread_id', $online_user->thread_id)->first();
             $this->fillPage(Input::get('page'), $thread_geo_info);
             $response['user_id'] = $online_user->user_id;
             $response['messages'] = MessageThread::getClientMessages($online_user->thread_id, Input::get('last_message_id'));
         } else {
             $response['in_conversation'] = 0;
             $response['token'] = "";
             $response['messages'] = [];
             $response['conversation_closed'] = 0;
             //Is conversation already closed
             if (sizeof(ClosedConversations::where('thread_id', Input::get('thread_id'))->get()) > 0) {
                 $response['success_msg'] = trans('msgs.thanks_for_contacting_support');
                 $response['token'] = 0;
                 $response['in_conversation'] = 1;
                 $response['conversation_closed'] = 1;
                 $response['thread_id'] = 0;
                 $response['user_id'] = 0;
             }
         }
     }
     return $this->send($response);
 }
 public function delete($user_id)
 {
     $operators_department = OperatorsDepartment::where("user_id", $user_id)->first();
     if (!empty($operators_department)) {
         $department_admin = DepartmentAdmins::where('department_id', $operators_department->department_id)->first();
         if (!empty($department_admin)) {
             //Change all conversations , tickets , threads , thread_messages operator_id
             OnlineUsers::where('operator_id', $user_id)->update(['operator_id' => $department_admin->user_id]);
             ClosedConversations::where('operator_id', $user_id)->update(['operator_id' => $department_admin->user_id]);
             MessageThread::where('operator_id', $user_id)->update(['operator_id' => $department_admin->user_id]);
             Tickets::where('operator_id', $user_id)->update(['operator_id' => $department_admin->user_id]);
             ThreadMessages::where('sender_id', $user_id)->update(['sender_id' => $department_admin->user_id]);
         }
     }
     RecentActivities::createActivity("User " . User::where('id', $user_id)->pluck('name') . " deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     User::find($user_id)->delete();
     OperatorsDepartment::where("user_id", $user_id)->delete();
     CannedMessages::where('operator_id', $user_id)->delete();
     UsersGroups::where('user_id', $user_id)->delete();
     Session::flash('success_msg', trans('msgs.operator_deleted_success'));
     return Redirect::to('/operators/all');
 }