<?php

function display_employee_rate_selection_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        value_list_cells('Employee Rate Category:', 'emp_rate_category', 'EMPLOYEE_RATE_CATEGORY', null, '', true);
        submit_cells('Refresh', 'Refresh');
    end_table(1);
}

function display_employee_rates_details($emp_rate_category) {
    start_table(TABLESTYLE, "width=70%");
        $th = array(_('Id'), _('From Date'), _('Unit'), _('Rate'), _('Comment'), "", "");
        table_header($th);

        $k = 0; 
        $result = get_employee_rates_details($emp_rate_category);
            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["unit"]);
                    qty_cell($row["rate"], false, 2);
                    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_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_rates($selected_id);
                    if (is_array($row)) {
                        $_POST['from_date']  = sql2date($row["from_date"]);
                        $_POST['unit'] = $row["unit"];
                        $_POST['rate'] = $row["rate"];
                        $_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');
            text_row(_("Unit:"), 'unit', null, 20, 20);
            amount_row(_("Rate:"), 'rate');
            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"), '', 'default');
        } else {
            submit_center_first('ADD_EMP_RATE', _("Add Employee Rate"), '', 'default');    
        }

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

function can_save_employee_rate($selected_id) {
    if (! ($_POST['emp_rate_category'] > 0)) {
        return array('success' => false, 'message' => _("Employee rate category can not be empty."), 'focus_to' => 'emp_rate_category');
    }
    
    if (! is_date($_POST['from_date'])){
        return array('success' => false, 'message' => _("From date is not valid date."), 'focus_to' => 'from_date');
    }
    
    if (! (strlen($_POST['unit']) > 0)){
        return array('success' => false, 'message' => _("Please enter an unit."), 'focus_to' => 'unit');
    }
    
    if (! check_num('rate', 0) || ! (input_num('rate') > 0)) {
        return array('success' => false, 'message' => _("Rate must be numeric and can not be negative."), 'focus_to' => 'rate');
    }
    
        return array('success' => true, 'message' => _(""));
}

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

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

function handle_delete_employee_rate($selected_id) {
    $msg = can_delete_employee_rate($selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $af_rw = delete_employee_rate($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_data() {
    global $selected_id;

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

        $selected_id = -1;
}