Example #1
0
 public function diff($id_ticket_history1 = null, $id_ticket_history2 = null)
 {
     $changes = [];
     if (!isset($id_ticket_history1) && !isset($id_ticket_history2)) {
         $id_ticket_history1 = $this->anchestor(1)->id;
         $id_ticket_history2 = $this->anchestor(0)->id;
     } elseif (!isset($id_ticket_history2)) {
         $id_ticket_history2 = $this->anchestor(0)->id;
     }
     $assignee = DB::raw("CONCAT(assignees.last_name,' ',assignees.first_name) as assignee");
     $contact = DB::raw("CONCAT(contacts.last_name,' ',contacts.first_name) as contact");
     $equipment = DB::raw("CONCAT(COALESCE(NULLIF(equipment.serial_number, ''), '[ND]'),' - ',CONCAT(COALESCE(NULLIF(equipment.name, ''), '[ND]'))) as equipment");
     $tickets = TicketHistory::select('tickets_history.id', 'tickets_history.title', 'tickets_history.post', $assignee, 'divisions.name as division', $equipment, $contact, 'job_types.name as job_type', 'levels.name as level', 'priorities.name as priority', 'tickets_history.emails', 'statuses.name as status');
     $tickets->leftJoin('company_person as assignee_contacts', 'tickets_history.assignee_id', '=', 'assignee_contacts.id');
     $tickets->leftJoin('people as assignees', 'assignee_contacts.person_id', '=', 'assignees.id');
     $tickets->leftJoin('company_person as ticket_contacts', 'tickets_history.contact_id', '=', 'ticket_contacts.id');
     $tickets->leftJoin('people as contacts', 'ticket_contacts.person_id', '=', 'contacts.id');
     $tickets->leftJoin('equipment', 'equipment.id', '=', 'tickets_history.equipment_id');
     $tickets->leftJoin('divisions', 'divisions.id', '=', 'tickets_history.division_id');
     $tickets->leftJoin('job_types', 'job_types.id', '=', 'tickets_history.job_type_id');
     $tickets->leftJoin('priorities', 'priorities.id', '=', 'tickets_history.priority_id');
     $tickets->leftJoin('statuses', 'statuses.id', '=', 'tickets_history.status_id');
     $tickets->leftJoin('levels', 'levels.id', '=', 'tickets_history.level_id');
     $tickets->whereIn('tickets_history.id', [$id_ticket_history1, $id_ticket_history2]);
     $tickets->where('tickets_history.ticket_id', $this->id);
     $temp = $tickets->get()->toArray();
     foreach ($temp as $record) {
         $key = $record['id'] == $id_ticket_history1 ? 'first' : 'second';
         $result[$key] = $record;
     }
     if (isset($result['first']) && isset($result['second'])) {
         foreach ($result['first'] as $key => $attribute) {
             if ($result['first'][$key] != $result['second'][$key] && $key != 'id') {
                 $label = ucfirst(str_replace("_", " ", $key));
                 $changes[$label] = new \StdClass();
                 $changes[$label]->old = $result['first'][$key];
                 $changes[$label]->new = $result['second'][$key];
             }
         }
     }
     return $changes;
 }