/**
  * If technician is changed, updated order_technicians
  */
 public function edit($assignment_id, $params)
 {
     $assignment = $this->get($assignment_id);
     $previous_technician_id = $assignment->technician_id;
     // Multiple technicians can be assigned to a single unit. In this case, create other assignments as needed, and delete existing ones if needed
     if (!empty($params['technician_id']) && is_array($params['technician_id'])) {
         $all_assignments = $this->get(array('order_id' => $assignment->order_id, 'unit_id' => $assignment->unit_id));
         foreach ($params['technician_id'] as $new_technician_id) {
             $technician_is_already_assigned = false;
             foreach ($all_assignments as $existing_assignment) {
                 if ($existing_assignment->technician_id == $new_technician_id) {
                     $technician_is_already_assigned = true;
                 }
             }
             if (!$technician_is_already_assigned) {
                 $assignment_params = array('order_id' => $assignment->order_id, 'unit_id' => $assignment->unit_id, 'workflow_id' => $assignment->workflow_id, 'technician_id' => $new_technician_id, 'appointment_date' => $assignment->appointment_date, 'estimated_duration' => $assignment->estimated_duration);
                 if (!$this->get(array('order_id' => $assignment->order_id, 'unit_id' => $assignment->unit_id, 'technician_id' => $new_technician_id))) {
                     $this->add($assignment_params);
                 }
             }
         }
         $all_assignments = $this->get(array('order_id' => $assignment->order_id, 'unit_id' => $assignment->unit_id));
         foreach ($all_assignments as $existing_assignment) {
             $assignment_has_a_technician = false;
             foreach ($params['technician_id'] as $new_technician_id) {
                 if ($existing_assignment->technician_id == $new_technician_id) {
                     $assignment_has_a_technician = true;
                 }
             }
             if (!$assignment_has_a_technician) {
                 $this->delete($existing_assignment->id);
             }
         }
         unset($params['technician_id']);
         $result = parent::edit($assignment_id, $params);
         // Update appointment_date and estimated_duration
         return true;
     } else {
         if (empty($params['technician_id'])) {
             return parent::edit($assignment_id, $params);
         } else {
             $this->order_technician_model->add_if_new($assignment->order_id, $params['technician_id']);
         }
         // If the order/unit/tech combination already exists, delete this assignment instead of editing it
         if ($params['technician_id'] != $assignment->technician_id && $this->get(array('order_id' => $assignment->order_id, 'unit_id' => $assignment->unit_id, 'technician_id' => $params['technician_id']))) {
             $result = parent::delete($assignment_id);
         } else {
             $result = parent::edit($assignment_id, $params);
         }
         $this->order_technician_model->delete_if_last($assignment->order_id, $previous_technician_id, $assignment->id);
         return $result;
     }
 }
 public function edit($unit_id, $params)
 {
     $unit = $this->get($unit_id);
     if (!empty($params['tenancy_id']) && $unit->tenancy_id != $params['tenancy_id']) {
         if (!empty($unit->tenancy_id)) {
             $this->tenancy_log_model->remove_unit($unit->tenancy_id, $unit->id);
         }
         $this->tenancy_log_model->add_unit($params['tenancy_id'], $unit->id);
     }
     return parent::edit($unit_id, $params);
 }