public function getCustomers($company_id)
 {
     $customer_ids = CompanyCustomers::where('company_id', $company_id)->lists('customer_id');
     $customers = [];
     if (sizeof($customer_ids) > 0) {
         $customers = User::whereIn('id', $customer_ids)->get();
     }
     foreach ($customers as $customer) {
         $company_id = CompanyCustomers::where("customer_id", $customer->id)->pluck('company_id');
         $customer->company = Company::find($company_id);
         $customer->all_ticket_count = Tickets::where('customer_id', $customer->id)->count();
         $customer->pending_ticket_count = Tickets::where('customer_id', $customer->id)->where('status', Tickets::TICKET_PENDING)->count();
         $customer->resolved_ticket_count = Tickets::where('customer_id', $customer->id)->where('status', Tickets::TICKET_RESOLVED)->count();
     }
     $this->data['customers'] = $customers;
     return View::make('companies.customers', $this->data);
 }
 public function all()
 {
     $customer_ids = [];
     if (\KodeInfo\Utilities\Utils::isDepartmentAdmin(Auth::user()->id)) {
         $department_admin = DepartmentAdmins::where('user_id', Auth::user()->id)->first();
         $department = Department::where('id', $department_admin->department_id)->first();
         $customer_ids = CompanyCustomers::where("company_id", $department->company_id)->lists('customer_id');
     } elseif (\KodeInfo\Utilities\Utils::isOperator(Auth::user()->id)) {
         $department_admin = OperatorsDepartment::where('user_id', Auth::user()->id)->first();
         $department = Department::where('id', $department_admin->department_id)->first();
         $customer_ids = CompanyCustomers::where("company_id", $department->company_id)->lists('customer_id');
     } else {
         $customer_ids = CompanyCustomers::lists('customer_id');
     }
     if (sizeof($customer_ids) > 0) {
         $this->data["customers"] = User::whereIn("id", $customer_ids)->orderBy('id', 'desc')->get();
     } else {
         $this->data["customers"] = [];
     }
     foreach ($this->data["customers"] as $customer) {
         $company_id = CompanyCustomers::where("customer_id", $customer->id)->pluck('company_id');
         $customer->company = Company::find($company_id);
         $customer->all_ticket_count = Tickets::where('customer_id', $customer->id)->count();
         $customer->pending_ticket_count = Tickets::where('customer_id', $customer->id)->where('status', Tickets::TICKET_PENDING)->count();
         $customer->resolved_ticket_count = Tickets::where('customer_id', $customer->id)->where('status', Tickets::TICKET_RESOLVED)->count();
     }
     return View::make('customers.all', $this->data);
 }
 public function store()
 {
     $v = Validator::make(["name" => Input::get('name'), "email" => Input::get('email'), "priority" => Input::get('priority'), "company" => Input::get('company'), "department" => Input::get('department'), "attachment" => Input::get('attachment'), "subject" => Input::get('subject'), "description" => Input::get('description')], ["name" => 'required', "email" => 'required|email', "priority" => 'required', "company" => 'required', "department" => 'required', "description" => 'required', "attachment" => 'mimes:rar,zip|size:10000', "subject" => 'required']);
     if ($v->passes()) {
         $company_customer_ids = CompanyCustomers::where('company_id', Input::get('company'))->lists('customer_id');
         if (sizeof($company_customer_ids) > 0) {
             $user = User::where('email', Input::get('email'))->first();
         } else {
             $user = null;
         }
         if (!empty($user) && !is_null($user)) {
             $user = User::where('email', Input::get('email'))->first();
             if (!empty($user)) {
                 $company_customer = new CompanyCustomers();
                 $company_customer->company_id = Input::get('company');
                 $company_customer->customer_id = $user->id;
                 $company_customer->save();
             }
         } else {
             $password = Str::random();
             $userManager = new \KodeInfo\UserManagement\UserManagement();
             $user = $userManager->createUser(["name" => Input::get('name'), "email" => Input::get('email'), "password" => $password, "password_confirmation" => $password], 'customer', false);
             $user->avatar = "/assets/img/default-avatar.jpg";
             $user->save();
             $company_customer = new CompanyCustomers();
             $company_customer->company_id = Input::get('company');
             $company_customer->customer_id = $user->id;
             $company_customer->save();
         }
         $repo = new KodeInfo\Repo\MessageRepo();
         $thread = $repo->createNewThread($user->id, Input::get("description"), true);
         $ticket = new Tickets();
         $ticket->thread_id = $thread['thread_id'];
         $ticket->customer_id = $user->id;
         $ticket->priority = Input::get("priority");
         $ticket->company_id = Input::get("company");
         $ticket->department_id = Input::get("department");
         $ticket->subject = Input::get("subject");
         $ticket->description = Input::get("description");
         $ticket->status = Tickets::TICKET_NEW;
         $ticket->requested_on = \Carbon\Carbon::now();
         $ticket->save();
         $ticket_attachment = new TicketAttachments();
         $ticket_attachment->thread_id = $thread['thread_id'];
         $ticket_attachment->message_id = $thread['msg_id'];
         $ticket_attachment->has_attachment = Input::hasFile('attachment');
         $ticket_attachment->attachment_path = Input::hasFile('attachment') ? Utils::fileUpload(Input::file('attachment'), 'attachments') : '';
         $ticket_attachment->save();
         $country = DB::table('countries')->where('countryCode', Input::get('country'))->first();
         $geo_info = new ThreadGeoInfo();
         $geo_info->thread_id = $thread['thread_id'];
         $geo_info->ip_address = Input::get('ip');
         $geo_info->country_code = Input::get('country');
         $geo_info->country = !empty($country) ? $country->countryName : "";
         $geo_info->provider = Input::get('provider');
         $geo_info->current_page = "";
         $geo_info->all_pages = "";
         $geo_info->save();
         $customer = User::find($ticket->customer_id);
         $mailer_extra = ['ticket' => $ticket, 'has_attachment' => $ticket_attachment->has_attachment, 'attachment_path' => $ticket_attachment->attachment_path];
         $this->ticketMailer->created($customer->email, $customer->name, $mailer_extra);
         RecentActivities::createActivity("New Ticket created by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
         Session::flash('success_msg', trans('msgs.ticket_created_success'));
         return Redirect::to('/tickets/all');
     } else {
         Session::flash('error_msg', Utils::buildMessages($v->messages()->all()));
         return Redirect::back()->withInput(Input::except('attachment'));
     }
 }
 public function start()
 {
     $response = [];
     $response['token'] = 0;
     if (Input::has('ip')) {
         //check ip_address in blocking table
         $blocking = Blocking::where('ip_address', Input::get('ip'))->first();
         if (!empty($blocking)) {
             if ($blocking->should_block_chat || $blocking->should_block_web_access) {
                 $response['blocked'] = 1;
                 $response['errors'] = trans('msgs.your_ip_blocked_by_admin_contact_support');
             }
         }
     } else {
         $response['blocked'] = 1;
         $response['errors'] = trans('msgs.your_ip_blocked_by_admin_contact_support');
     }
     $response['blocked'] = 0;
     $v_data = ["email" => Input::get('email'), "name" => Input::get('name'), "company_id" => Input::get('company_id'), "domain" => Input::get('domain'), "department" => Input::get('department'), "message" => Input::get('message')];
     $v_rules = ["email" => 'required|email', "name" => 'required', "company_id" => 'required', "domain" => 'required', "department" => 'required', "message" => 'required'];
     $v = Validator::make($v_data, $v_rules);
     if ($v->passes()) {
         $request_check = Company::where('id', Input::get('company_id'))->where('domain', Input::get('domain'))->get();
         if (sizeof($request_check) <= 0) {
             return $this->send(['result' => 0, 'errors' => trans('msgs.invalid_request_check_id_and_domain')]);
         }
         $company_customers = CompanyCustomers::where('company_id', Input::get('company_id'))->lists('customer_id');
         $user = null;
         if (sizeof($company_customers)) {
             $user = User::whereIn('id', $company_customers)->where('email', Input::get('email'))->first();
         }
         $operator_online = Company::operatorsOnline(Input::get('company_id'));
         $success_msg = trans('msgs.thanks_for_contacting_will_get_back');
         $response['is_online'] = $operator_online;
         $repo = new KodeInfo\Repo\MessageRepo();
         if (!empty($user) && !is_null($user)) {
             //user exists
             $count = OnlineUsers::where('user_id', $user->id)->first();
             if (sizeof($count) > 0) {
                 //user already online
                 $token = OnlineUsers::getToken();
                 if ($operator_online > 0) {
                     $response['token'] = $token;
                 }
                 $response['result'] = 1;
                 $response['user_id'] = $user->id;
                 $response['thread_id'] = $count->thread_id;
                 $response['success_msg'] = $success_msg;
                 return $this->send($response);
             } else {
                 $token = OnlineUsers::getToken();
                 $thread = $repo->createNewThread($user->id, Input::get("message"), true);
                 if ($response['is_online']) {
                     $online_user = new OnlineUsers();
                     $online_user->user_id = $user->id;
                     $online_user->thread_id = $thread['thread_id'];
                     $online_user->operator_id = 0;
                     $online_user->company_id = Input::get('company_id');
                     $online_user->department_id = Input::get('department');
                     $online_user->locked_by_operator = 0;
                     $online_user->requested_on = \Carbon\Carbon::now();
                     $online_user->token = $token;
                     $online_user->save();
                 }
                 $country = DB::table('countries')->where('countryCode', Input::get('country'))->first();
                 $geo_info = new ThreadGeoInfo();
                 $geo_info->thread_id = $thread['thread_id'];
                 $geo_info->ip_address = Input::get('ip');
                 $geo_info->country_code = Input::get('country');
                 $geo_info->country = !empty($country) ? $country->countryName : "";
                 $geo_info->provider = Input::get('provider');
                 $geo_info->current_page = Input::get('page');
                 $geo_info->all_pages = json_encode(['pages' => [Input::get('page')]]);
                 $geo_info->save();
                 if (!$response['is_online']) {
                     $this->convertToTicket($thread['thread_id'], $thread['msg_id'], $user, "", Input::get('message'), Input::get('department'), Input::get('company_id'));
                 }
                 if ($operator_online > 0) {
                     $response['token'] = $token;
                 }
                 $response['result'] = 1;
                 $response['user_id'] = $user->id;
                 $response['thread_id'] = $thread['thread_id'];
                 $response['success_msg'] = $success_msg;
                 return $this->send($response);
             }
         } else {
             $password = Str::random();
             //Is user in users table then get id and put in company-customers table
             $user = User::where('email', Input::get('email'))->first();
             if (!empty($user)) {
                 $company_customer = new CompanyCustomers();
                 $company_customer->company_id = Input::get('company_id');
                 $company_customer->customer_id = $user->id;
                 $company_customer->save();
             } else {
                 $user = $this->userManager->createUser(["name" => Input::get('name'), "email" => Input::get('email'), "password" => $password, "password_confirmation" => $password], 'customer', false);
                 $user->avatar = "/assets/images/default-avatar.jpg";
                 $user->save();
                 $user->getActivationCode();
                 $company_customer = new CompanyCustomers();
                 $company_customer->company_id = Input::get('company_id');
                 $company_customer->customer_id = $user->id;
                 $company_customer->save();
                 $this->mailer->welcome($user->email, $user->name, User::getWelcomeFields(false, $user->id, $password, $company_customer->company_id));
                 $this->mailer->activate($user->email, $user->name, User::getActivateFields(false, $user->id, $company_customer->company_id));
             }
             $token = OnlineUsers::getToken();
             $thread = $repo->createNewThread($user->id, Input::get("message"), true);
             if ($response['is_online']) {
                 $online_user = new OnlineUsers();
                 $online_user->user_id = $user->id;
                 $online_user->thread_id = $thread['thread_id'];
                 $online_user->operator_id = 0;
                 $online_user->company_id = Input::get('company_id');
                 $online_user->department_id = Input::get('department');
                 $online_user->locked_by_operator = 0;
                 $online_user->requested_on = \Carbon\Carbon::now();
                 $online_user->token = $token;
                 $online_user->save();
             }
             $country = DB::table('countries')->where('countryCode', Input::get('country'))->first();
             $geo_info = new ThreadGeoInfo();
             $geo_info->thread_id = $thread['thread_id'];
             $geo_info->ip_address = Input::get('ip');
             $geo_info->country_code = Input::get('country');
             $geo_info->country = !empty($country) ? $country->countryName : "";
             $geo_info->provider = Input::get('provider');
             $geo_info->current_page = Input::get('page');
             $geo_info->all_pages = json_encode(['pages' => [Input::get('page')]]);
             $geo_info->save();
             if (!$response['is_online']) {
                 $this->convertToTicket($thread['thread_id'], $thread['msg_id'], $user, "", Input::get('message'), Input::get('department'), Input::get('company_id'));
             }
             if ($operator_online > 0) {
                 $response['token'] = $token;
             }
             $response['result'] = 1;
             $response['user_id'] = $user->id;
             $response['thread_id'] = $thread['thread_id'];
             $response['success_msg'] = $success_msg;
             return $this->send($response);
         }
     } else {
         return $this->send(['result' => 0, 'errors' => Utils::buildMessages($v->messages()->all())]);
     }
 }