<?php

function display_employee_rate_category_selection_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        start_cell("colspan=2");
            start_table(false, 'align=left');
                employee_search_list_row(_("Employee:"), 'employee_code', null, false, true);
            end_table();
        end_cell();
        submit_cells('Refresh', 'Refresh');
    end_table(1);
}

function display_employee_rate_category_details($employee_code) {
    start_table(TABLESTYLE, "width=70%");
        $th = array(_('Id'), _('From Date'), _('Employee Rate Category'), _('Comment'), "", "");
        table_header($th);

        $k = 0; 
        $result = get_employee_rate_category_details($employee_code);
            while ($row = db_fetch($result)) {
                alt_table_row_color($k);

                    label_cell($row["id"], 'align=center');
                    label_cell(sql2date($row["from_date"]), 'align=center');
                    label_cell($row["rate_category_name"]);
                    label_cell(nl2br($row["comment"]));

                    edit_button_cell("Edit".$row["id"], _("Edit"));
                    delete_button_cell("Delete".$row["id"], _("Delete"));

                end_row();
            }

    end_table(1);
}

function display_employee_rate_category_controls(&$Mode, &$selected_id, $state=true) {
    global $Ajax;
    
    $Ajax->activate('employee rate_control');
    div_start('employee rate_control');
    
        start_table(TABLESTYLE2);
            if ($Mode != 'Delete' && $selected_id > 0 && $state === true) {
                $row = get_employee_rate_category($selected_id);
                    if (is_array($row)) {
                        $_POST['from_date'] = sql2date($row["from_date"]);
                        $_POST['emp_rate_category'] = $row["emp_rate_category"];
                        $_POST['comment'] = $row["comment"];
                    }
                    
                hidden('selected_id', $selected_id);
            }
            
            if ($selected_id > 0 && $Mode != 'Delete') {
                label_row('Id #:', $selected_id);
            }
            
            date_row(_("From Date:"), 'from_date');
            value_list_row('Employee Rate Category:', 'emp_rate_category', 'EMPLOYEE_RATE_CATEGORY', null, '');
            textarea_row('Comment:', 'comment', null, 40, 2);
        end_table(1);

        if ($Mode != 'Delete' && $selected_id > 0) {
            submit_center_first('UPDATE_EMP_RATE', _("Update Employee Rate Category"), '', 'default');
        } else {
            submit_center_first('ADD_EMP_RATE', _("Add Employee Rate Category"), '', 'default');    
        }

        submit_center_last("CLEAR_DATA", 'Clear', 'Clear entered data', false, ICON_ESCAPE); br();
        
    div_end();
}

function can_save_employee_rate_category($selected_id) {
    $emp_row = get_employee($_POST['employee_code']);
        if (! is_array($emp_row)) {
            return array('success' => false, 'message' => _("Please select an employee."), 'focus_to' => 'employee_code');
        }
    
    if (! is_date($_POST['from_date'])){
        return array('success' => false, 'message' => _("From date is not valid date."), 'focus_to' => 'from_date');
    }
    
    $cat_row = get_employee_rate_category_on_date($_POST['employee_code'], $_POST['from_date'], true, $selected_id);
        if (is_array($cat_row)) {
            return array('success' => false, 'message' => _("Record #".$cat_row['id']." has same from date. Cannot use same from date for both records."), 
                         'focus_to' => 'from_date');
        }
    
    if (! ($_POST['emp_rate_category'] > 0)) {
        return array('success' => false, 'message' => _("Employee rate category cannot empty."), 'focus_to' => 'emp_rate_category');
    }
    
        return array('success' => true, 'message' => _(""));
}

function save_employee_rate_category($selected_id) {
    $msg = can_save_employee_rate_category($selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
    
        if ($selected_id > 0) {
            $af_rw = update_employee_rate_category($selected_id, $_POST['from_date'], $_POST['emp_rate_category'], $_POST['comment']); 
                if (! ($af_rw > 0)) {
                    return array('success' => false, 'message' => "Employee rate category record #$selected_id could not update.");
                } else {
                    return array('success' => true, 'message' => "Employee rate category record #$selected_id has been updated.");
                }
        } else {
            $nw_id = add_employee_rate_category($_POST['employee_code'], $_POST['from_date'], $_POST['emp_rate_category'], $_POST['comment']);
                if (! ($nw_id > 0)) {
                    return array('success' => false, 'message' => "Could not add new employee rate category record.");  
                } else {
                    return array('success' => true, 'message' => "New employee rate category record #$nw_id has been added.");
                }
        }
}

function can_delete_employee_rate_category($selected_id) {
    // check with employee rate category
    
    return array('success' => true, 'message' => "");
}

function handle_delete_employee_rate_category($selected_id) {
    $msg = can_delete_employee_rate_category($selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $af_rw = delete_employee_rate_category($selected_id);
            if ($af_rw != 1) {
                return array('success' => false, 'message' => "Could not delete employee rate record #$selected_id.");  
            } else {
                return array('success' => true, 'message' => "Employee rate record #$selected_id has been deleted.");
            }
}

function clear_employee_rate_category_data() {
    global $selected_id;

        $_POST['from_date'] = Today();
        unset($_POST['emp_rate_category']);
        unset($_POST['comment']);

        $selected_id = -1;
}