public function dashboardContact($contact_id)
 {
     $data['contact'] = $contact = CompanyPerson::find($contact_id);
     $data['title'] = Auth::user()->active_contact_id == $contact_id ? "My Dashboard" : $contact->person->name() . " Dashboard";
     $data['user_tickets_status_data'] = ChartsManager::userTicketsStatusData($contact_id);
     $data['user_tickets_involvement_data'] = ChartsManager::userTicketsInvolvementData($contact_id);
     $data['user_tickets_status'] = ChartsManager::userTicketsStatus($contact_id);
     $data['user_tickets_involvement'] = ChartsManager::userTicketsInvolvement($contact_id);
     return view('dashboard/index', $data);
 }
 public function run()
 {
     $person = DB::table('people')->where('first_name', 'James')->where('last_name', 'Sample')->first();
     $company = Company::where('name', 'Sample Company')->first();
     $group_type = GroupType::where('name', 'employee')->first();
     $group = Group::where('name', 'e80-helpdesk')->first();
     if (isset($person)) {
         CompanyPerson::create(['person_id' => $person->id, 'company_id' => $company->id, 'group_type_id' => $group_type->id, 'group_id' => $group->id]);
     }
 }
 private function query($params = array())
 {
     $raw1 = DB::raw("CASE \n                            WHEN company_main_contacts.main_contact_id = company_person.id \n                            THEN 1 \n                            ELSE 0 \n                         END as 'is_main_contact'");
     $contacts = CompanyPerson::select("company_person.*", "people.first_name", "people.last_name", $raw1);
     $contacts->leftJoin('people', 'company_person.person_id', '=', 'people.id');
     $contacts->leftJoin('companies', 'company_person.company_id', '=', 'companies.id');
     $contacts->leftJoin('titles', 'company_person.title_id', '=', 'titles.id');
     $contacts->leftJoin('groups', 'company_person.group_id', '=', 'groups.id');
     $contacts->leftJoin('departments', 'company_person.department_id', '=', 'departments.id');
     $contacts->leftJoin('company_main_contacts', 'company_main_contacts.company_id', '=', 'companies.id');
     if (Auth::check() && !Auth::user()->active_contact->isE80() && (!isset($params['debugger_list']) || $params['debugger_list'] != "true" || Auth::user()->owner->id != ADMIN_PERSON_ID)) {
         $contacts->where("company_person.company_id", "=", Auth::user()->active_contact->company_id);
     }
     return $contacts;
 }
 public function storeInfo(StartRequest $request)
 {
     if (Session::get("start_session")) {
         $user = User::find(Session::get("start_session.user_id"));
         $person = $user->owner;
         if (Input::file('profile_picture') && Input::file('profile_picture')->isValid()) {
             $request['file'] = Input::file('profile_picture');
             $request['target'] = "people";
             $request['target_id'] = $person->id;
             $request['uploader_id'] = $user->active_contact->id;
             $repo = new FilesRepository();
             $result = $repo->upload($request);
             if (!$result['error']) {
                 $old_profile_picture = $person->profile_picture_id;
                 if (!is_null($old_profile_picture)) {
                     $person->profile_picture_id = NULL;
                     $person->save();
                     $repo->destroy($old_profile_picture);
                 }
                 $person->profile_picture_id = $result['id'];
                 $person->save();
             }
         }
         if (Session::get("start_session.safe_enough") == false) {
             $user->password = Hash::make($request->get('password'));
         }
         $user->last_login = Carbon::now();
         $user->save();
         $person->first_name = $request->get('first_name');
         $person->last_name = $request->get('last_name');
         $person->save();
         if ($request->get('use_info_all_contacts') == "true") {
             $new_contact = $request->get('contact');
             foreach ($person->company_person as $contact) {
                 $contact->phone = $new_contact['phone'];
                 $contact->extension = $new_contact['extension'];
                 $contact->cellphone = $new_contact['cellphone'];
                 $contact->email = $new_contact['email'];
                 $contact->department_id = $new_contact['department_id'];
                 $contact->title_id = $new_contact['title_id'];
                 $contact->save();
             }
         } else {
             $contacts = $request->get('contacts');
             foreach ($contacts as $key => $new_contact) {
                 $contact = CompanyPerson::find($key);
                 $contact->phone = $new_contact['phone'];
                 $contact->extension = $new_contact['extension'];
                 $contact->cellphone = $new_contact['cellphone'];
                 $contact->email = $new_contact['email'];
                 $contact->department_id = $new_contact['department_id'];
                 $contact->title_id = $new_contact['title_id'];
                 $contact->save();
             }
         }
         Session::flush();
     }
     return redirect()->route('login.login')->withErrors(['Please, login again']);
 }
 public function edit($id)
 {
     if (Auth::user()->can('update-company')) {
         $data['company'] = Company::find($id);
         $selected_account_manager = CompanyAccountManager::where('company_id', '=', $id)->first();
         $data['company']->account_manager_id = isset($selected_account_manager) ? $selected_account_manager->account_manager_id : null;
         $selected_main_contact = CompanyMainContact::where('company_id', '=', $id)->first();
         $data['company']->main_contact_id = isset($selected_main_contact) ? $selected_main_contact->main_contact_id : null;
         $data['account_managers'] = CompanyPersonController::API()->all(["where" => ["company_person.company_id|=|" . ELETTRIC80_COMPANY_ID, "company_person.title_id|=|" . ACCOUNT_MANAGER_TITLE_ID], "order" => ["people.last_name|ASC", "people.first_name|ASC"], "paginate" => "false"]);
         $data['main_contacts'] = CompanyPerson::where('company_person.company_id', '=', $id)->get();
         $data['support_types'] = SupportType::orderBy("name")->get();
         $data['connection_types'] = ConnectionType::orderBy("name")->get();
         $data['escalation_profiles'] = EscalationProfile::orderBy("name")->get();
         $data['title'] = "Edit " . $data['company']->name;
         return view('companies/edit', $data);
     } else {
         return redirect()->back()->withErrors(['Access denied to companies edit page']);
     }
 }
 public static function workingTimeData($days, $type)
 {
     $result = null;
     if ($type == "division") {
         $grouping = Division::whereIn('id', [LGV_DIVISION_ID, PLC_DIVISION_ID, PC_DIVISION_ID, BEMA_DIVISION_ID, FIELD_DIVISION_ID, SPARE_PARTS_DIVISION_ID, RELIABILITY_DIVISION_ID, OTHERS_DIVISION_ID])->orderBy("name")->get();
     } elseif ($type == "priority") {
         $grouping = Priority::orderBy("name")->get();
     } elseif ($type == "level") {
         $grouping = Level::orderBy("name")->get();
     } elseif ($type == "company") {
         $grouping = Company::orderBy("name")->get();
     } elseif ($type == "assignee") {
         $grouping = CompanyPerson::select("company_person.*", "people.first_name", "people.last_name")->leftJoin('people', 'company_person.person_id', '=', 'people.id')->where('company_person.company_id', '=', ELETTRIC80_COMPANY_ID)->orderBy("last_name")->get();
         foreach ($grouping as $group) {
             $group->name = $group->last_name . " " . $group->first_name;
         }
     }
     if (isset($grouping)) {
         foreach ($grouping as $index => $group) {
             for ($i = 0; $i < 50; $i++) {
                 $query = "SELECT SUM(resolution_time) as sum, COUNT(*) as ticket_count, AVG(resolution_time) as average, DATE_SUB(NOW(), INTERVAL " . $days * ($i + 1) . " day) as date\n                            FROM (\n                            SELECT before.ticket_id, SUM(\n                                TIMESTAMPDIFF(SECOND, \n                                    GREATEST(DATE_SUB(NOW(), INTERVAL " . $days * ($i + 1) . " day), before.created_at), \n                                    LEAST(DATE_SUB(NOW(), INTERVAL " . $days * $i . " day), IFNULL(after.created_at,NOW()))\n                                )\n                            )/3600 as resolution_time\n                            FROM tickets_history as `before`\n                            LEFT JOIN tickets_history as `after` ON before.id = after.previous_id\n                            INNER JOIN tickets ON tickets.id = before.ticket_id \n                            WHERE (\n                                after.status_id IN (" . TICKET_NEW_STATUS_ID . "," . TICKET_IN_PROGRESS_STATUS_ID . ",\n                                " . TICKET_REQUESTING_STATUS_ID . "," . TICKET_SOLVED_STATUS_ID . "," . TICKET_CLOSED_STATUS_ID . ")\n                                OR \n                                (before.status_id IN (" . TICKET_NEW_STATUS_ID . "," . TICKET_IN_PROGRESS_STATUS_ID . ",\n                                " . TICKET_REQUESTING_STATUS_ID . ") AND after.id IS NULL)\n                            )\n                            AND tickets.deleted_at IS NULL\n                            AND tickets." . $type . "_id = {$group->id}\n                            AND TIMESTAMPDIFF(SECOND, \n                                GREATEST(before.created_at,DATE_SUB(NOW(), INTERVAL " . $days * ($i + 1) . " day)), \n                                LEAST(DATE_SUB(NOW(), INTERVAL " . $days * $i . " day), IFNULL(after.created_at,NOW()))) > 0\n                            GROUP BY before.ticket_id\n                            ) as " . $type[0] . "_{$index} ";
                 $temp = DB::select(DB::raw($query));
                 foreach ($temp[0] as $key => $value) {
                     if ($key != "date") {
                         $div_key = str_replace(" ", "_", $group->name);
                         if ($i == 0) {
                             $result[$div_key][$key]['current'] = round($temp[0]->{$key}, 2);
                         }
                         if ($i == 1) {
                             $result[$div_key][$key]['previous'] = round($temp[0]->{$key}, 2);
                         }
                         $result[$div_key][$key]['historical'][$temp[0]->date] = $temp[0]->{$key};
                     }
                 }
             }
         }
     }
     return $result;
 }
 /**
  * Determine if the user is authorized to make this request.
  *
  * @return bool
  */
 public function authorize()
 {
     $company_person = CompanyPerson::find($this->route('company_person_id'));
     return Auth::user()->can('update-contact') || Auth::user()->can('update-own-contact') && Auth::user()->active_contact->id == $this->route('company_person_id') || !$company_person->isE80() && Auth::user()->can('update-customer-contact');
 }
 public function update($id, UpdateCompanyPersonRequest $request)
 {
     $contact = CompanyPerson::find($id);
     $contact->department_id = Input::get('department_id');
     $contact->title_id = Input::get('title_id');
     $contact->phone = Input::get('phone');
     $contact->extension = Input::get('extension');
     $contact->cellphone = Input::get('cellphone');
     $contact->division_ids = Input::get('division_ids') ? implode(",", Input::get('division_ids')) : "";
     $contact->email = Input::get('email');
     $contact->group_id = Auth::user()->can('update-group-contact') ? Input::get('group_id') : $contact->group_id;
     $contact->save();
     return redirect()->route('company_person.show', $id)->with('successes', ['Contact updated successfully']);
 }