<?php

function display_change_job_card_owner_control($jo_row) {
    if (! (isset($_POST['next_job_owner_id']) && strlen($_POST['next_job_owner_id']) > 1)) {
        $_POST['next_job_owner_id'] = $jo_row['next_job_owner_id'];
    }
    if (! (isset($_POST['department_id']) && $_POST['department_id'] > 0)) {
        $_POST['department_id'] = $jo_row['department_id'];
    }
    if (! (isset($_POST['section_id']) && $_POST['section_id'] > 0)) {
        $_POST['section_id'] = $jo_row['section_id'];
    }
    
    start_table(TABLESTYLE, "width=50%");
        $th = array(_("Field Name"), _("Current Value"), _("New Value"));
        table_header($th);

        start_row();
            label_cell('Current Owner');
            label_cell(get_employee_formatted_long_name($jo_row['next_job_owner_id']));
            start_cell();
                start_table(false, 'align=left');
                    employee_search_list_cells(_(""), 'next_job_owner_id', null, false, false);
                end_table();
            end_cell();
        end_row();
        start_row();
            label_cell('Department');
            label_cell(get_listvalue_name_by_id($jo_row['department_id']));
            start_cell();
                start_table(false, 'align=left');
                    value_list_cells('', 'department_id', 'DEPARTMENTS', null, '', true);
                end_table();
            end_cell();
        end_row();
        start_row();
            label_cell('Section');
            label_cell(get_listvalue_name_by_id($jo_row['section_id']));
            start_cell();
                start_table(false, 'align=left');
                    $dep_row = get_listvalue_by_id($_POST['department_id']);
                        if (is_array($dep_row)) {
                            $section_list_code = $dep_row['value_code'];
                            value_list_cells('', 'section_id', "VMS_$section_list_code", null, '', false);
                        } else {
                            $_POST['section_id'] = 0;
                            hidden('section_id', 0);
                            label_cell('');
                        }
                end_table();
            end_cell();
        end_row();
    end_table(2);
}

function can_save_change_job_card_owner($jo_id) {  
    global $systypes_array;
    
    $jo_tp_name = $systypes_array[ST_JOB_ORDER];
    
    $new_field_entered = false;        
    $row_state = 0;
    $jo_row = get_job_order_record($jo_id);
        if (is_array($jo_row)) {
            if ($jo_row['closed'] == 1) {
                return array('success' => false, 'message' => "$jo_tp_name header #$jo_id is already closed. Not allow to further update.");
            } elseif ($jo_row['released'] == 1) {
                return array('success' => false, 'message' => "$jo_tp_name header #$jo_id is already released. Not allow to further update.");
            }
                
            $row_state = $jo_row['row_state'];
        } else {
            return array('success' => false, 'message' => "Could not find Job card #$jo_id.");
        }
        
        if (isset($_POST['next_job_owner_id'])) {
            $emp_row = get_employee($_POST['next_job_owner_id']);
                if (! is_array($emp_row)) {
                    return array("success" => false, "focus_to" => 'next_job_owner_id', "message" => "Please select a owner.");        
                }
        } else {
            return array("success" => false, "focus_to" => 'next_job_owner_id', "message" => "Please select a owner.");        
        }
        
        if ($_POST['next_job_owner_id'] != $jo_row['next_job_owner_id']) {
            $new_field_entered = true;         
        }
      
        if (! ($_POST['department_id'] > 0)) {
            return array("success" => false, "focus_to" => 'department_id', "message" => "Please select a department.");        
        }
        
        if ($jo_row['department_id'] != $_POST['department_id']) {
            $has_trans = check_job_order_has_transactions($jo_id);
                if ($has_trans['has_trans'] === true) {
                    return array("success" => false, "focus_to" => 'department_id', "message" => $has_trans['message']);  
                }
                
                $new_field_entered = true;        
        }
        
        if (! ($_POST['section_id'] > 0)) {
            return array("success" => false, "focus_to" => 'section_id', "message" => "Please select a section.");        
        }
        
        $crt_sec = check_change_job_card_selected_section_belong_to_correct_department($_POST['department_id'], $_POST['section_id']);
            if ($crt_sec['correct_section'] === false) {
                return array("success" => false, "focus_to" => 'section_id', "message" => $crt_sec['message']);        
            }
        
        if ($jo_row['section_id'] != $_POST['section_id']) {
            $new_field_entered = true;        
        }
        
        if ($new_field_entered === false) {
            return array("success" => false, "focus_to" => '', "message" => "Please change new value for at least one field before saving.");  
        }
            
            return array("success" => true, 'jo_row' => $jo_row);    
}

function handle_save_change_job_card_owner($jo_id) {
    $msg = can_save_change_job_card_owner($jo_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
    
    $jo_row = $msg['jo_row'];
    $new_row_state = $row_state = $jo_row['row_state'];
    
        begin_transaction();
    
            $jo_up_rw = update_job_owner($jo_id, $_POST['next_job_owner_id'], $_POST['department_id'], $_POST['section_id'], $row_state); 
                if ($jo_up_rw != 1) {
                    cancel_transaction();
                    return array('success' => false, 'message' => "Could not update new cowner.");
                } 
             
            if ($jo_row['next_job_owner_id'] != $_POST['next_job_owner_id']) {
                $new_row_state = db_new_row_state($new_row_state);
                
                $new_owner = get_employee_formatted_long_name($_POST['next_job_owner_id']);

                $text = " Job card owner is changed to $new_owner.";
                $job_msg = save_job_activity_log($jo_id, 'CHANGE_OWNER', $text, $new_row_state, 0, 0, 0, $_POST['next_job_owner_id'], ST_JOB_ORDER, $jo_id);
                    if ($job_msg['success'] === false) {
                        cancel_transaction();
                        return $job_msg;
                    } 
            }
            
            if ($jo_row['department_id'] != $_POST['department_id']) {
                $new_row_state = db_new_row_state($new_row_state);
                
                $pre_dep = get_listvalue_name_by_id($jo_row['department_id']);
                $new_dep = get_listvalue_name_by_id($_POST['department_id']);

                $text = " Department is changed from $pre_dep to $new_dep.";
                $job_msg = save_job_activity_log($jo_id, 'CHANGE_DEPARTMENT', $text, $new_row_state, 0, 0, 0, $_POST['next_job_owner_id'], ST_JOB_ORDER, $jo_id);
                    if ($job_msg['success'] === false) {
                        cancel_transaction();
                        return $job_msg;
                    } 
            }
            
            if ($jo_row['section_id'] != $_POST['section_id']) {
                $new_row_state = db_new_row_state($new_row_state);
                
                $pre_sec = get_listvalue_name_by_id($jo_row['section_id']);
                $new_sec = get_listvalue_name_by_id($_POST['section_id']);

                $text = " Section is changed from $pre_sec to $new_sec.";
                $job_msg = save_job_activity_log($jo_id, 'CHANGE_SECTION', $text, $new_row_state, 0, 0, 0, $_POST['next_job_owner_id'], ST_JOB_ORDER, $jo_id);
                    if ($job_msg['success'] === false) {
                        cancel_transaction();
                        return $job_msg;
                    } 
            }
                
        commit_transaction();
                
        return array('success' => true, 'message' => "New values have been saved.");
}

?>