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 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; }