<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_VALUE_TABLES';
$path_to_root = "../..";
include_once($path_to_root . "/includes/session.inc");

page(_($help_context = "Value Tables"));

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");

//below file should have been included by the system already. But it is not. So included manually. Once the issues found remove this inclusion
include_once($path_to_root . "/payroll/includes/payroll_db.inc");

//check_db_has_purchasable_items(_("There are no inventory items defined in the system."));

simple_page_mode(true);
//--------------------------------------------------------------------------------------------------

function check_data($selected_id)
{	
    if (!check_num('upto', 0))
    {
        display_error( _("Up To value entered was not numeric or positive."));
        set_focus('upto');
        return false;
    }

    if (!check_num('calculation_value', 0))
    {
        display_error( _("Calculation value entered was not numeric or positive."));
        set_focus('calculation_value');
        return false;
    }
    
    if (!check_num('fixed_value'))
    {
        display_error( _("Fixed value entered was not numeric"));
        set_focus('fixed_value');
        return false;
    }
    
    return true;

}



if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id)) 
{
    $input_error = 0;

    if ($_POST['table_id'] == "" || !isset($_POST['table_id'])){
        $input_error = 1;
        display_error( _("There is no table selected."));
        set_focus('table_id');
    }

    if(!isset($_POST['salary_total_id']))
        $salary_total_id = 0;
    else {
        $salary_total_id = $_POST['salary_total_id'];
    }

    if($_POST['calculation_type'] == "V" ){
        $salary_total_id = 0;
    }
	
	if ($input_error == 0){
            if ($Mode == 'ADD_ITEM'){
                    add_table_value(input_num('table_id'), input_num('upto'), 
                            $_POST['calculation_type'], input_num('calculation_value'),
                            $salary_total_id,input_num('fixed_value'), get_post('value2')); 
                    
                    update_value_table_all($_POST['table_id'],$_POST['accumulated_total']);

                    display_notification(_("New table value has been added."));
            } 
            else{
                    update_table_value($selected_id, input_num('upto'), 
                            $_POST['calculation_type'], input_num('calculation_value'),
                            $salary_total_id, input_num('fixed_value'), get_post('value2')); 
                    
                    update_value_table_all($_POST['table_id'],$_POST['accumulated_total']);

                    display_notification(_("Table value has been updated."));
            }
		$Mode = 'RESET';
	}
}

//--------------------------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
        delete_table_value($selected_id);
	
	display_notification(_("Table value has been sucessfully deleted."));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	unset($_POST);
}

if (list_updated('table_id')) 
	$Ajax->activate('code_table');

//--------------------------------------------------------------------------------------------------

start_form();

if (!isset($_POST['table_id']))
        $_POST['table_id'] = get_global_table_id ();



//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
value_list_row('Select Table:', 'table_id', 'VALUE_TABLES', $_POST['table_id'],false,true);

$result = get_all_table_values($_POST['table_id']);

set_global_table_id($_POST['table_id']);

br(2);

div_start('code_table');
	start_table(TABLESTYLE, "width=60%");

	$th = array(_("ID"), _("Up To"), _("Calculation Type"), _("Percentage Of"), 
                    _("Calculation Value"), _("Fixed Value"), _("Counting Factor"), 
                    _("Accum. Total"), "", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
			alt_table_row_color($k);

            label_cell($myrow["id"]);
            label_cell($myrow["upto"], "align=right");
            
            if($myrow["calculation_type"] == "V"){
                $calculation_type_name = "Value";
                $dec = 2;
            }
            if($myrow["calculation_type"] == "P"){
                $calculation_type_name = "Percentage";
                $dec = 5;
            }
            
            label_cell($calculation_type_name);
            label_cell($myrow["salary_total_name"]);        
            //label_cell($myrow["calculation_value"]);
            qty_cell($myrow["calculation_value"], false, $dec);
            label_cell($myrow["fixed_value"], "align=right");
            qty_cell($myrow["value2"], false, 2);
            
            if($myrow["accumulated_total"] == "1")
                $accumulated_total = "Yes";
            else {
                $accumulated_total = "No";
            }
            label_cell($accumulated_total, "align=center");

		 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
		 	edit_button_cell("Delete".$myrow['id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

	end_table();
div_end();

//-----------------------------------------------------------------------------------------------
br();

if ($selected_id != '') {
	if ($Mode =='Edit')
	{
		$myrow = get_table_value($selected_id);
		$_POST['upto'] = $myrow["upto"];
		$_POST['calculation_type'] = $myrow["calculation_type"];
		$_POST['calculation_value'] = $myrow["calculation_value"];
        $_POST['salary_total_id'] = $myrow['salary_total_id'];
		$_POST['fixed_value'] = $myrow["fixed_value"];
        $_POST['value2'] = $myrow["value2"];
        $_POST['accumulated_total'] = $myrow["accumulated_total"];
	}
	hidden('selected_id', $selected_id);
} 

If(!isset($_POST['upto'])){
	$_POST['upto'] = 0;
}
If(!isset($_POST['calculation_type'])){
	$_POST['calculation_type'] = 'P';
}
If(!isset($_POST['calculation_value'])){
        $_POST['calculation_value'] = 0;
}
If(!isset($_POST['salary_total_id'])){
	$_POST['salary_total_id'] = 0;
}
If(!isset($_POST['fixed_value'])){
	$_POST['fixed_value'] = 0;
}
If(!isset($_POST['accumulated_total'])){
	$_POST['accumulated_total'] = 0;
}
If(!isset($_POST['value2'])){
	$_POST['value2'] = 0;
}


start_table(TABLESTYLE2);

hidden('id', $selected_id);

$dec = 2;
if(get_post('calculation_type') == "P") {
        $dec = 5;
};


amount_row(_("Up To:"), 'upto', $_POST['upto'], null, null,2);
value_percentage_list_row("Calculation Type:", 'calculation_type',$_POST['calculation_type'],null,null,true);
amount_row(_("Calculation Value:"), 'calculation_value', $_POST['calculation_value'], null, null,$dec);


if(get_post('calculation_type') == "P") {
        value_list_row("Percentage Of:", "salary_total_id", "SALARY_TOTALS", $_POST['salary_total_id']);
        $dec = 5;
};

amount_row(_("Fixed Value:"), 'fixed_value', $_POST['fixed_value'], null, null,2);

amount_row(_("Counting Factor:"), 'value2', $_POST['value2'], null, null,2);

yesno_list_row("Accumulated Total:", "accumulated_total", $_POST['accumulated_total']);

//record_status_list_row(_("Value Status:"), 'inactive1');

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');

end_form();
end_page();

?>
